static DWARFExpression MakeRegisterBasedLocationExpressionInternal(
    llvm::codeview::RegisterId reg, llvm::Optional<int32_t> relative_offset,
    lldb::ModuleSP module) {
  return MakeLocationExpressionInternal(
      module, [&](Stream &stream, RegisterKind &register_kind) -> bool {
        uint32_t reg_num = GetRegisterNumber(
            module->GetArchitecture().GetMachine(), reg, register_kind);
        if (reg_num == LLDB_INVALID_REGNUM)
          return false;

        if (reg_num > 31) {
          llvm::dwarf::LocationAtom base = relative_offset
                                               ? llvm::dwarf::DW_OP_bregx
                                               : llvm::dwarf::DW_OP_regx;
          stream.PutHex8(base);
          stream.PutULEB128(reg_num);
        } else {
          llvm::dwarf::LocationAtom base = relative_offset
                                               ? llvm::dwarf::DW_OP_breg0
                                               : llvm::dwarf::DW_OP_reg0;
          stream.PutHex8(base + reg_num);
        }

        if (relative_offset)
          stream.PutSLEB128(*relative_offset);

        return true;
      });
}
static DWARFExpression MakeLocationExpressionInternal(lldb::ModuleSP module,
                                                      StreamWriter &&writer) {
  const ArchSpec &architecture = module->GetArchitecture();
  ByteOrder byte_order = architecture.GetByteOrder();
  uint32_t address_size = architecture.GetAddressByteSize();
  uint32_t byte_size = architecture.GetDataByteSize();
  if (byte_order == eByteOrderInvalid || address_size == 0)
    return DWARFExpression(nullptr);

  RegisterKind register_kind = eRegisterKindDWARF;
  StreamBuffer<32> stream(Stream::eBinary, address_size, byte_order);

  if (!writer(stream, register_kind))
    return DWARFExpression(nullptr);

  DataBufferSP buffer =
      std::make_shared<DataBufferHeap>(stream.GetData(), stream.GetSize());
  DataExtractor extractor(buffer, byte_order, address_size, byte_size);
  DWARFExpression result(module, extractor, nullptr, 0, buffer->GetByteSize());
  result.SetRegisterKind(register_kind);

  return result;
}