2019-11-24 00:15:52 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-11-24 00:15:52 +00:00
|
|
|
|
|
|
|
#include "UICommon/Disassembler.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2017-02-08 02:04:30 +00:00
|
|
|
#if defined(HAVE_LLVM)
|
2019-11-24 00:15:52 +00:00
|
|
|
#include <fmt/format.h>
|
2015-05-31 21:21:47 +00:00
|
|
|
#include <llvm-c/Disassembler.h>
|
|
|
|
#include <llvm-c/Target.h>
|
2022-08-08 00:18:36 +00:00
|
|
|
#elif defined(_M_X86_64)
|
2018-06-22 22:52:34 +00:00
|
|
|
#include <disasm.h> // Bochs
|
2015-05-31 21:21:47 +00:00
|
|
|
#endif
|
|
|
|
|
2022-12-02 01:16:11 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/VariantUtil.h"
|
2015-05-31 21:21:47 +00:00
|
|
|
#include "Core/PowerPC/JitInterface.h"
|
2023-03-25 14:46:19 +00:00
|
|
|
#include "Core/System.h"
|
2015-05-31 21:21:47 +00:00
|
|
|
|
2017-02-08 02:04:30 +00:00
|
|
|
#if defined(HAVE_LLVM)
|
2015-05-31 21:21:47 +00:00
|
|
|
class HostDisassemblerLLVM : public HostDisassembler
|
|
|
|
{
|
|
|
|
public:
|
2015-07-29 02:32:34 +00:00
|
|
|
HostDisassemblerLLVM(const std::string& host_disasm, int inst_size = -1,
|
|
|
|
const std::string& cpu = "");
|
2015-05-31 21:21:47 +00:00
|
|
|
~HostDisassemblerLLVM()
|
|
|
|
{
|
|
|
|
if (m_can_disasm)
|
|
|
|
LLVMDisasmDispose(m_llvm_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_can_disasm;
|
|
|
|
LLVMDisasmContextRef m_llvm_context;
|
|
|
|
int m_instruction_size;
|
|
|
|
|
2015-08-07 07:43:54 +00:00
|
|
|
std::string DisassembleHostBlock(const u8* code_start, const u32 code_size,
|
|
|
|
u32* host_instructions_count, u64 starting_pc) override;
|
2015-05-31 21:21:47 +00:00
|
|
|
};
|
|
|
|
|
2015-07-29 02:32:34 +00:00
|
|
|
HostDisassemblerLLVM::HostDisassemblerLLVM(const std::string& host_disasm, int inst_size,
|
|
|
|
const std::string& cpu)
|
2015-05-31 21:21:47 +00:00
|
|
|
: m_can_disasm(false), m_instruction_size(inst_size)
|
|
|
|
{
|
|
|
|
LLVMInitializeAllTargetInfos();
|
|
|
|
LLVMInitializeAllTargetMCs();
|
|
|
|
LLVMInitializeAllDisassemblers();
|
|
|
|
|
2023-04-15 04:55:53 +00:00
|
|
|
m_llvm_context =
|
|
|
|
LLVMCreateDisasmCPU(host_disasm.c_str(), cpu.c_str(), nullptr, 0, nullptr, nullptr);
|
2015-05-31 21:21:47 +00:00
|
|
|
|
|
|
|
// Couldn't create llvm context
|
|
|
|
if (!m_llvm_context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LLVMSetDisasmOptions(m_llvm_context, LLVMDisassembler_Option_AsmPrinterVariant |
|
|
|
|
LLVMDisassembler_Option_PrintLatency);
|
|
|
|
|
|
|
|
m_can_disasm = true;
|
|
|
|
}
|
|
|
|
|
2015-08-07 07:43:54 +00:00
|
|
|
std::string HostDisassemblerLLVM::DisassembleHostBlock(const u8* code_start, const u32 code_size,
|
|
|
|
u32* host_instructions_count,
|
|
|
|
u64 starting_pc)
|
2015-05-31 21:21:47 +00:00
|
|
|
{
|
|
|
|
if (!m_can_disasm)
|
|
|
|
return "(No LLVM context)";
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
u8* disasmPtr = (u8*)code_start;
|
|
|
|
const u8* end = code_start + code_size;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
std::ostringstream x86_disasm;
|
|
|
|
while ((u8*)disasmPtr < end)
|
|
|
|
{
|
|
|
|
char inst_disasm[256];
|
2015-08-07 07:43:54 +00:00
|
|
|
size_t inst_size = LLVMDisasmInstruction(m_llvm_context, disasmPtr, (u64)(end - disasmPtr),
|
|
|
|
starting_pc, inst_disasm, 256);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-08-07 07:43:54 +00:00
|
|
|
x86_disasm << "0x" << std::hex << starting_pc << "\t";
|
2015-05-31 21:21:47 +00:00
|
|
|
if (!inst_size)
|
|
|
|
{
|
|
|
|
x86_disasm << "Invalid inst:";
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
if (m_instruction_size != -1)
|
|
|
|
{
|
|
|
|
// If we are on an architecture that has a fixed instruction size
|
|
|
|
// We can continue onward past this bad instruction.
|
2019-03-02 18:42:25 +00:00
|
|
|
std::string inst_str;
|
2015-05-31 21:21:47 +00:00
|
|
|
for (int i = 0; i < m_instruction_size; ++i)
|
2019-11-24 00:15:52 +00:00
|
|
|
inst_str += fmt::format("{:02x}", disasmPtr[i]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
x86_disasm << inst_str << std::endl;
|
|
|
|
disasmPtr += 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
|
2019-03-02 18:42:25 +00:00
|
|
|
std::string code_block;
|
2015-05-31 21:21:47 +00:00
|
|
|
for (int i = 0; (disasmPtr + i) < end; ++i)
|
2019-11-24 00:15:52 +00:00
|
|
|
code_block += fmt::format("{:02x}", disasmPtr[i]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
x86_disasm << code_block << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x86_disasm << inst_disasm << std::endl;
|
|
|
|
disasmPtr += inst_size;
|
2015-08-07 07:43:54 +00:00
|
|
|
starting_pc += inst_size;
|
2015-05-31 21:21:47 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
(*host_instructions_count)++;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
return x86_disasm.str();
|
|
|
|
}
|
2022-08-08 00:18:36 +00:00
|
|
|
#elif defined(_M_X86_64)
|
2018-06-22 22:52:34 +00:00
|
|
|
class HostDisassemblerX86 : public HostDisassembler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HostDisassemblerX86();
|
|
|
|
|
|
|
|
private:
|
|
|
|
disassembler m_disasm;
|
|
|
|
|
|
|
|
std::string DisassembleHostBlock(const u8* code_start, const u32 code_size,
|
|
|
|
u32* host_instructions_count, u64 starting_pc) override;
|
|
|
|
};
|
2015-05-31 21:21:47 +00:00
|
|
|
|
|
|
|
HostDisassemblerX86::HostDisassemblerX86()
|
|
|
|
{
|
|
|
|
m_disasm.set_syntax_intel();
|
|
|
|
}
|
|
|
|
|
2015-08-07 07:43:54 +00:00
|
|
|
std::string HostDisassemblerX86::DisassembleHostBlock(const u8* code_start, const u32 code_size,
|
|
|
|
u32* host_instructions_count, u64 starting_pc)
|
2015-05-31 21:21:47 +00:00
|
|
|
{
|
|
|
|
u64 disasmPtr = (u64)code_start;
|
|
|
|
const u8* end = code_start + code_size;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
std::ostringstream x86_disasm;
|
|
|
|
while ((u8*)disasmPtr < end)
|
|
|
|
{
|
|
|
|
char inst_disasm[256];
|
|
|
|
disasmPtr += m_disasm.disasm64(disasmPtr, disasmPtr, (u8*)disasmPtr, inst_disasm);
|
|
|
|
x86_disasm << inst_disasm << std::endl;
|
|
|
|
(*host_instructions_count)++;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-05-31 21:21:47 +00:00
|
|
|
return x86_disasm.str();
|
|
|
|
}
|
2018-06-22 22:52:34 +00:00
|
|
|
#endif
|
2015-05-31 21:21:47 +00:00
|
|
|
|
2017-03-03 02:21:03 +00:00
|
|
|
std::unique_ptr<HostDisassembler> GetNewDisassembler(const std::string& arch)
|
2015-05-31 21:21:47 +00:00
|
|
|
{
|
2017-02-08 02:04:30 +00:00
|
|
|
#if defined(HAVE_LLVM)
|
2015-05-31 21:21:47 +00:00
|
|
|
if (arch == "x86")
|
2017-03-03 02:21:03 +00:00
|
|
|
return std::make_unique<HostDisassemblerLLVM>("x86_64-none-unknown");
|
|
|
|
if (arch == "aarch64")
|
|
|
|
return std::make_unique<HostDisassemblerLLVM>("aarch64-none-unknown", 4, "cortex-a57");
|
|
|
|
if (arch == "armv7")
|
|
|
|
return std::make_unique<HostDisassemblerLLVM>("armv7-none-unknown", 4, "cortex-a15");
|
2022-08-08 00:18:36 +00:00
|
|
|
#elif defined(_M_X86_64)
|
2015-05-31 21:21:47 +00:00
|
|
|
if (arch == "x86")
|
2017-03-03 02:21:03 +00:00
|
|
|
return std::make_unique<HostDisassemblerX86>();
|
2015-05-31 21:21:47 +00:00
|
|
|
#endif
|
2017-03-03 02:21:03 +00:00
|
|
|
return std::make_unique<HostDisassembler>();
|
2015-05-31 21:21:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 01:16:11 +00:00
|
|
|
DisassembleResult DisassembleBlock(HostDisassembler* disasm, u32 address)
|
2015-05-31 21:21:47 +00:00
|
|
|
{
|
2023-03-25 14:46:19 +00:00
|
|
|
auto res = Core::System::GetInstance().GetJitInterface().GetHostCode(address);
|
2022-12-02 01:16:11 +00:00
|
|
|
|
|
|
|
return std::visit(overloaded{[&](JitInterface::GetHostCodeError error) {
|
|
|
|
DisassembleResult result;
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case JitInterface::GetHostCodeError::NoJitActive:
|
|
|
|
result.text = "(No JIT active)";
|
|
|
|
break;
|
|
|
|
case JitInterface::GetHostCodeError::NoTranslation:
|
|
|
|
result.text = "(No translation)";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result.entry_address = address;
|
|
|
|
result.instruction_count = 0;
|
|
|
|
result.code_size = 0;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[&](JitInterface::GetHostCodeResult host_result) {
|
|
|
|
DisassembleResult new_result;
|
|
|
|
u32 instruction_count = 0;
|
|
|
|
new_result.text = disasm->DisassembleHostBlock(
|
|
|
|
host_result.code, host_result.code_size, &instruction_count,
|
|
|
|
(u64)host_result.code);
|
|
|
|
new_result.entry_address = host_result.entry_address;
|
|
|
|
new_result.code_size = host_result.code_size;
|
|
|
|
new_result.instruction_count = instruction_count;
|
|
|
|
return new_result;
|
|
|
|
}},
|
|
|
|
res);
|
2015-05-31 21:21:47 +00:00
|
|
|
}
|