2016-07-04 11:22:11 +02:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2016-08-05 02:50:31 +02:00
|
|
|
#ifdef DYNARMIC_USE_LLVM
|
|
|
|
#include <llvm-c/Disassembler.h>
|
|
|
|
#include <llvm-c/Target.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-07 19:08:48 +02:00
|
|
|
#include "backend_x64/block_of_code.h"
|
2016-07-04 11:22:11 +02:00
|
|
|
#include "backend_x64/emit_x64.h"
|
|
|
|
#include "backend_x64/jitstate.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/bit_util.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/scope_exit.h"
|
2016-08-05 02:50:31 +02:00
|
|
|
#include "common/string_util.h"
|
2016-07-04 11:22:11 +02:00
|
|
|
#include "frontend/arm_types.h"
|
2016-07-14 15:39:43 +02:00
|
|
|
#include "frontend/translate/translate.h"
|
2016-07-04 11:22:11 +02:00
|
|
|
#include "interface/interface.h"
|
2016-07-21 22:48:45 +02:00
|
|
|
#include "ir_opt/passes.h"
|
2016-07-04 11:22:11 +02:00
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
|
|
|
|
using namespace BackendX64;
|
|
|
|
|
|
|
|
struct Jit::Impl {
|
2016-08-13 01:10:23 +02:00
|
|
|
Impl(Jit* jit, UserCallbacks callbacks)
|
|
|
|
: block_of_code()
|
|
|
|
, jit_state(&block_of_code)
|
|
|
|
, emitter(&block_of_code, callbacks, jit)
|
|
|
|
, callbacks(callbacks)
|
|
|
|
{}
|
|
|
|
|
|
|
|
BlockOfCode block_of_code;
|
|
|
|
JitState jit_state;
|
2016-07-04 11:22:11 +02:00
|
|
|
EmitX64 emitter;
|
2016-07-04 15:37:50 +02:00
|
|
|
const UserCallbacks callbacks;
|
2016-07-04 11:22:11 +02:00
|
|
|
|
|
|
|
size_t Execute(size_t cycle_count) {
|
|
|
|
u32 pc = jit_state.Reg[15];
|
|
|
|
bool TFlag = Common::Bit<5>(jit_state.Cpsr);
|
|
|
|
bool EFlag = Common::Bit<9>(jit_state.Cpsr);
|
|
|
|
|
2016-08-13 01:10:23 +02:00
|
|
|
Arm::LocationDescriptor descriptor{pc, TFlag, EFlag, jit_state.guest_FPSCR_mode};
|
2016-07-04 11:22:11 +02:00
|
|
|
|
2016-08-12 19:17:31 +02:00
|
|
|
CodePtr code_ptr = GetBasicBlock(descriptor).code_ptr;
|
2016-08-07 19:08:48 +02:00
|
|
|
return block_of_code.RunCode(&jit_state, code_ptr, cycle_count);
|
2016-07-04 11:22:11 +02:00
|
|
|
}
|
2016-08-05 02:50:31 +02:00
|
|
|
|
2016-08-06 20:59:09 +02:00
|
|
|
std::string Disassemble(const Arm::LocationDescriptor& descriptor) {
|
2016-08-05 02:50:31 +02:00
|
|
|
auto block = GetBasicBlock(descriptor);
|
2016-08-12 19:17:31 +02:00
|
|
|
std::string result = Common::StringFromFormat("address: %p\nsize: %zu bytes\n", block.code_ptr, block.size);
|
2016-08-05 02:50:31 +02:00
|
|
|
|
|
|
|
#ifdef DYNARMIC_USE_LLVM
|
2016-08-12 19:17:31 +02:00
|
|
|
CodePtr end = block.code_ptr + block.size;
|
|
|
|
size_t remaining = block.size;
|
2016-08-05 02:50:31 +02:00
|
|
|
|
|
|
|
LLVMInitializeX86TargetInfo();
|
|
|
|
LLVMInitializeX86TargetMC();
|
|
|
|
LLVMInitializeX86Disassembler();
|
|
|
|
LLVMDisasmContextRef llvm_ctx = LLVMCreateDisasm("x86_64", nullptr, 0, nullptr, nullptr);
|
|
|
|
LLVMSetDisasmOptions(llvm_ctx, LLVMDisassembler_Option_AsmPrinterVariant);
|
|
|
|
|
2016-08-12 19:17:31 +02:00
|
|
|
for (CodePtr pos = block.code_ptr; pos < end;) {
|
2016-08-05 02:50:31 +02:00
|
|
|
char buffer[80];
|
|
|
|
size_t inst_size = LLVMDisasmInstruction(llvm_ctx, const_cast<u8*>(pos), remaining, (u64)pos, buffer, sizeof(buffer));
|
|
|
|
assert(inst_size);
|
|
|
|
for (CodePtr i = pos; i < pos + inst_size; i++)
|
|
|
|
result.append(Common::StringFromFormat("%02x ", *i));
|
|
|
|
for (size_t i = inst_size; i < 10; i++)
|
|
|
|
result.append(" ");
|
|
|
|
result.append(buffer);
|
|
|
|
result.append("\n");
|
|
|
|
|
|
|
|
pos += inst_size;
|
|
|
|
remaining -= inst_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisasmDispose(llvm_ctx);
|
|
|
|
#else
|
|
|
|
result.append("(recompile with DYNARMIC_USE_LLVM=ON to disassemble the generated x86_64 code)\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-04 11:22:11 +02:00
|
|
|
private:
|
2016-08-12 19:17:31 +02:00
|
|
|
EmitX64::BlockDescriptor GetBasicBlock(Arm::LocationDescriptor descriptor) {
|
2016-08-05 02:50:31 +02:00
|
|
|
auto block = emitter.GetBasicBlock(descriptor);
|
|
|
|
if (block)
|
2016-08-12 19:17:31 +02:00
|
|
|
return *block;
|
2016-07-04 11:22:11 +02:00
|
|
|
|
2016-07-04 15:37:50 +02:00
|
|
|
IR::Block ir_block = Arm::Translate(descriptor, callbacks.MemoryRead32);
|
2016-07-21 22:48:45 +02:00
|
|
|
Optimization::GetSetElimination(ir_block);
|
|
|
|
Optimization::DeadCodeElimination(ir_block);
|
|
|
|
Optimization::VerificationPass(ir_block);
|
2016-07-04 11:22:11 +02:00
|
|
|
return emitter.Emit(descriptor, ir_block);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-12 15:31:43 +02:00
|
|
|
Jit::Jit(UserCallbacks callbacks) : impl(std::make_unique<Impl>(this, callbacks)) {}
|
2016-07-04 11:22:11 +02:00
|
|
|
|
|
|
|
Jit::~Jit() {}
|
|
|
|
|
|
|
|
size_t Jit::Run(size_t cycle_count) {
|
|
|
|
ASSERT(!is_executing);
|
|
|
|
is_executing = true;
|
|
|
|
SCOPE_EXIT({ this->is_executing = false; });
|
|
|
|
|
|
|
|
halt_requested = false;
|
|
|
|
|
|
|
|
size_t cycles_executed = 0;
|
|
|
|
while (cycles_executed < cycle_count && !halt_requested) {
|
|
|
|
cycles_executed += impl->Execute(cycle_count - cycles_executed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cycles_executed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::ClearCache(bool poison_memory) {
|
|
|
|
ASSERT(!is_executing);
|
2016-08-07 19:08:48 +02:00
|
|
|
impl->block_of_code.ClearCache(poison_memory);
|
2016-07-07 13:01:47 +02:00
|
|
|
impl->emitter.ClearCache();
|
2016-08-13 01:10:23 +02:00
|
|
|
impl->jit_state.ResetRSB(&impl->block_of_code);
|
2016-07-04 11:22:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 23:45:54 +02:00
|
|
|
void Jit::Reset() {
|
|
|
|
ASSERT(!is_executing);
|
2016-08-13 01:10:23 +02:00
|
|
|
impl->jit_state = JitState(&impl->block_of_code);
|
2016-08-09 23:45:54 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 11:22:11 +02:00
|
|
|
void Jit::HaltExecution() {
|
|
|
|
ASSERT(is_executing);
|
|
|
|
halt_requested = true;
|
|
|
|
|
|
|
|
// TODO: Uh do other stuff to JitState pls.
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u32, 16>& Jit::Regs() {
|
|
|
|
return impl->jit_state.Reg;
|
|
|
|
}
|
|
|
|
std::array<u32, 16> Jit::Regs() const {
|
|
|
|
return impl->jit_state.Reg;
|
|
|
|
}
|
|
|
|
|
2016-08-05 19:54:19 +02:00
|
|
|
std::array<u32, 64>& Jit::ExtRegs() {
|
|
|
|
return impl->jit_state.ExtReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u32, 64> Jit::ExtRegs() const {
|
|
|
|
return impl->jit_state.ExtReg;
|
|
|
|
}
|
|
|
|
|
2016-07-04 11:22:11 +02:00
|
|
|
u32& Jit::Cpsr() {
|
|
|
|
return impl->jit_state.Cpsr;
|
|
|
|
}
|
2016-08-05 19:54:19 +02:00
|
|
|
|
2016-07-04 11:22:11 +02:00
|
|
|
u32 Jit::Cpsr() const {
|
|
|
|
return impl->jit_state.Cpsr;
|
|
|
|
}
|
|
|
|
|
2016-08-05 19:54:19 +02:00
|
|
|
u32 Jit::Fpscr() const {
|
|
|
|
return impl->jit_state.Fpscr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::SetFpscr(u32 value) const {
|
|
|
|
return impl->jit_state.SetFpscr(value);
|
|
|
|
}
|
|
|
|
|
2016-08-06 20:59:09 +02:00
|
|
|
std::string Jit::Disassemble(const Arm::LocationDescriptor& descriptor) {
|
2016-08-05 02:50:31 +02:00
|
|
|
return impl->Disassemble(descriptor);
|
|
|
|
}
|
|
|
|
|
2016-07-04 11:22:11 +02:00
|
|
|
} // namespace Dynarmic
|