File: HostDisassembler.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (158 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/HostDisassembler.h"

#include <span>

#include <fmt/format.h>
#include <fmt/ostream.h>
#include <fmt/ranges.h>

#if defined(HAVE_LLVM)
#include <llvm-c/Disassembler.h>
#include <llvm-c/Target.h>
#elif defined(_M_X86_64)
#include <disasm.h>  // Bochs
#endif

#if defined(HAVE_LLVM)
class HostDisassemblerLLVM final : public HostDisassembler
{
public:
  explicit HostDisassemblerLLVM(const char* host_disasm, const char* cpu = "",
                                std::size_t inst_size = 0);
  ~HostDisassemblerLLVM();

private:
  LLVMDisasmContextRef m_llvm_context;
  std::size_t m_instruction_size;

  std::size_t Disassemble(const u8* begin, const u8* end, std::ostream& stream) override;
};

HostDisassemblerLLVM::HostDisassemblerLLVM(const char* host_disasm, const char* cpu,
                                           std::size_t inst_size)
    : m_instruction_size(inst_size)
{
  LLVMInitializeAllTargetInfos();
  LLVMInitializeAllTargetMCs();
  LLVMInitializeAllDisassemblers();

  m_llvm_context = LLVMCreateDisasmCPU(host_disasm, cpu, nullptr, 0, nullptr, nullptr);

  // Couldn't create llvm context
  if (!m_llvm_context)
    return;

  LLVMSetDisasmOptions(m_llvm_context, LLVMDisassembler_Option_AsmPrinterVariant |
                                           LLVMDisassembler_Option_PrintLatency);
}

HostDisassemblerLLVM::~HostDisassemblerLLVM()
{
  if (m_llvm_context)
    LLVMDisasmDispose(m_llvm_context);
}

std::size_t HostDisassemblerLLVM::Disassemble(const u8* begin, const u8* end, std::ostream& stream)
{
  std::size_t instruction_count = 0;
  if (!m_llvm_context)
    return instruction_count;

  while (begin < end)
  {
    char inst_disasm[256];

    const auto inst_size =
        LLVMDisasmInstruction(m_llvm_context, const_cast<u8*>(begin), static_cast<u64>(end - begin),
                              reinterpret_cast<u64>(begin), inst_disasm, sizeof(inst_disasm));
    if (inst_size == 0)
    {
      if (m_instruction_size != 0)
      {
        // If we are on an architecture that has a fixed instruction
        // size, we can continue onward past this bad instruction.
        fmt::println(stream, "{}\tInvalid inst: {:02x}", fmt::ptr(begin),
                     fmt::join(std::span{begin, m_instruction_size}, ""));
        begin += m_instruction_size;
      }
      else
      {
        // We can't continue if we are on an architecture that has flexible
        // instruction sizes. Dump the rest of the block instead.
        fmt::println(stream, "{}\tInvalid inst: {:02x}", fmt::ptr(begin),
                     fmt::join(std::span{begin, end}, ""));
        break;
      }
    }
    else
    {
      fmt::println(stream, "{}{}", fmt::ptr(begin), inst_disasm);
      begin += inst_size;
    }
    ++instruction_count;
  }
  return instruction_count;
}
#elif defined(_M_X86_64)
class HostDisassemblerBochs final : public HostDisassembler
{
public:
  explicit HostDisassemblerBochs();
  ~HostDisassemblerBochs() override = default;

private:
  disassembler m_disasm;

  std::size_t Disassemble(const u8* begin, const u8* end, std::ostream& stream) override;
};

HostDisassemblerBochs::HostDisassemblerBochs()
{
  m_disasm.set_syntax_intel();
}

std::size_t HostDisassemblerBochs::Disassemble(const u8* begin, const u8* end, std::ostream& stream)
{
  std::size_t instruction_count = 0;
  while (begin < end)
  {
    char inst_disasm[256];

    const auto inst_size =
        m_disasm.disasm64(0, reinterpret_cast<bx_address>(begin), begin, inst_disasm);
    fmt::println(stream, "{}\t{}", fmt::ptr(begin), inst_disasm);
    begin += inst_size;
    ++instruction_count;
  }
  return instruction_count;
}
#endif

std::unique_ptr<HostDisassembler> HostDisassembler::Factory(Platform arch)
{
  switch (arch)
  {
#if defined(HAVE_LLVM)
  case Platform::x86_64:
    return std::make_unique<HostDisassemblerLLVM>("x86_64-none-unknown");
  case Platform::aarch64:
    return std::make_unique<HostDisassemblerLLVM>("aarch64-none-unknown", "cortex-a57", 4);
#elif defined(_M_X86_64)
  case Platform::x86_64:
    return std::make_unique<HostDisassemblerBochs>();
#else
  case Platform{}:  // warning C4065: "switch statement contains 'default' but no 'case' labels"
#endif
  default:
    return std::make_unique<HostDisassembler>();
  }
}

std::size_t HostDisassembler::Disassemble(const u8* begin, const u8* end, std::ostream& stream)
{
  fmt::println(stream, "{}\t{:02x}", fmt::ptr(begin), fmt::join(std::span{begin, end}, ""));
  return 0;
}