This commit is contained in:
Danila Malyutin 2015-07-23 20:28:03 +03:00 committed by Nekotekina
parent 6832910187
commit 6849192b39
4 changed files with 5982 additions and 5984 deletions

View file

@ -33,7 +33,7 @@ u64 Compiler::s_rotate_mask[64][64];
bool Compiler::s_rotate_mask_inited = false; bool Compiler::s_rotate_mask_inited = false;
Compiler::Compiler(RecompilationEngine & recompilation_engine, const Executable execute_unknown_function, Compiler::Compiler(RecompilationEngine & recompilation_engine, const Executable execute_unknown_function,
const Executable execute_unknown_block, bool (*poll_status_function)(PPUThread * ppu_state)) const Executable execute_unknown_block, bool(*poll_status_function)(PPUThread * ppu_state))
: m_recompilation_engine(recompilation_engine) : m_recompilation_engine(recompilation_engine)
, m_poll_status_function(poll_status_function) { , m_poll_status_function(poll_status_function) {
InitializeNativeTarget(); InitializeNativeTarget();
@ -62,7 +62,6 @@ Compiler::~Compiler() {
delete m_llvm_context; delete m_llvm_context;
} }
class CustomSectionMemoryManager : public llvm::SectionMemoryManager { class CustomSectionMemoryManager : public llvm::SectionMemoryManager {
private: private:
std::unordered_map<std::string, Executable> &executableMap; std::unordered_map<std::string, Executable> &executableMap;
@ -232,7 +231,8 @@ std::pair<Executable, llvm::ExecutionEngine *> Compiler::Compile(const std::stri
m_ir_builder->SetInsertPoint(merge_bb); m_ir_builder->SetInsertPoint(merge_bb);
m_ir_builder->CreateRet(m_ir_builder->getInt32(0)); m_ir_builder->CreateRet(m_ir_builder->getInt32(0));
} else { }
else {
m_ir_builder->CreateRet(exit_instr_i32); m_ir_builder->CreateRet(exit_instr_i32);
} }
} }
@ -258,7 +258,8 @@ std::pair<Executable, llvm::ExecutionEngine *> Compiler::Compile(const std::stri
m_ir_builder->SetInsertPoint(merge_bb); m_ir_builder->SetInsertPoint(merge_bb);
m_ir_builder->CreateRet(m_ir_builder->getInt32(0)); m_ir_builder->CreateRet(m_ir_builder->getInt32(0));
} else { }
else {
m_ir_builder->CreateRet(exit_instr_i32); m_ir_builder->CreateRet(exit_instr_i32);
} }
} }
@ -272,7 +273,6 @@ std::pair<Executable, llvm::ExecutionEngine *> Compiler::Compile(const std::stri
} }
} }
m_recompilation_engine.Log() << *m_module; m_recompilation_engine.Log() << *m_module;
std::string verify; std::string verify;
@ -281,7 +281,6 @@ std::pair<Executable, llvm::ExecutionEngine *> Compiler::Compile(const std::stri
m_recompilation_engine.Log() << "Verification failed: " << verify << "\n"; m_recompilation_engine.Log() << "Verification failed: " << verify << "\n";
} }
auto ir_build_end = std::chrono::high_resolution_clock::now(); auto ir_build_end = std::chrono::high_resolution_clock::now();
m_stats.ir_build_time += std::chrono::duration_cast<std::chrono::nanoseconds>(ir_build_end - compilation_start); m_stats.ir_build_time += std::chrono::duration_cast<std::chrono::nanoseconds>(ir_build_end - compilation_start);
@ -296,7 +295,7 @@ std::pair<Executable, llvm::ExecutionEngine *> Compiler::Compile(const std::stri
auto translate_end = std::chrono::high_resolution_clock::now(); auto translate_end = std::chrono::high_resolution_clock::now();
m_stats.translation_time += std::chrono::duration_cast<std::chrono::nanoseconds>(translate_end - optimize_end); m_stats.translation_time += std::chrono::duration_cast<std::chrono::nanoseconds>(translate_end - optimize_end);
/* m_recompilation_engine.Log() << "\nDisassembly:\n"; /* m_recompilation_engine.Log() << "\nDisassembly:\n";
auto disassembler = LLVMCreateDisasm(sys::getProcessTriple().c_str(), nullptr, 0, nullptr, nullptr); auto disassembler = LLVMCreateDisasm(sys::getProcessTriple().c_str(), nullptr, 0, nullptr, nullptr);
for (size_t pc = 0; pc < mci.size();) { for (size_t pc = 0; pc < mci.size();) {
char str[1024]; char str[1024];
@ -352,7 +351,7 @@ const Executable *RecompilationEngine::GetCompiledExecutableIfAvailable(u32 addr
std::unordered_map<u32, ExecutableStorage>::iterator It = m_address_to_function.find(address); std::unordered_map<u32, ExecutableStorage>::iterator It = m_address_to_function.find(address);
if (It == m_address_to_function.end()) if (It == m_address_to_function.end())
return nullptr; return nullptr;
if(std::get<1>(It->second) == nullptr) if (std::get<1>(It->second) == nullptr)
return nullptr; return nullptr;
mut = &(std::get<3>(It->second)); mut = &(std::get<3>(It->second));
return &(std::get<0>(It->second)); return &(std::get<0>(It->second));
@ -426,7 +425,8 @@ void RecompilationEngine::Task() {
if (!work_done_this_iteration) { if (!work_done_this_iteration) {
// TODO: Reduce the priority of the recompilation engine thread if its set to high priority // TODO: Reduce the priority of the recompilation engine thread if its set to high priority
} else { }
else {
is_idling = false; is_idling = false;
} }
@ -520,7 +520,8 @@ void RecompilationEngine::ProcessExecutionTrace(const ExecutionTrace & execution
const ExecutionTraceEntry * next_trace = nullptr; const ExecutionTraceEntry * next_trace = nullptr;
if (trace_i + 1 != execution_trace.entries.end()) { if (trace_i + 1 != execution_trace.entries.end()) {
next_trace = &(*(trace_i + 1)); next_trace = &(*(trace_i + 1));
} else if (!split_trace && execution_trace.type == ExecutionTrace::Type::Loop) { }
else if (!split_trace && execution_trace.type == ExecutionTrace::Type::Loop) {
next_trace = &(*(execution_trace.entries.begin())); next_trace = &(*(execution_trace.entries.begin()));
} }
@ -556,15 +557,18 @@ void RecompilationEngine::UpdateControlFlowGraph(ControlFlowGraph & cfg, const E
if (next_entry->GetPrimaryAddress() != (this_entry.GetPrimaryAddress() + 4)) { if (next_entry->GetPrimaryAddress() != (this_entry.GetPrimaryAddress() + 4)) {
cfg.branches[this_entry.GetPrimaryAddress()].insert(next_entry->GetPrimaryAddress()); cfg.branches[this_entry.GetPrimaryAddress()].insert(next_entry->GetPrimaryAddress());
} }
} else if (next_entry->type == ExecutionTraceEntry::Type::FunctionCall) { }
else if (next_entry->type == ExecutionTraceEntry::Type::FunctionCall) {
cfg.calls[this_entry.data.instruction.address].insert(next_entry->GetPrimaryAddress()); cfg.calls[this_entry.data.instruction.address].insert(next_entry->GetPrimaryAddress());
} }
} }
} else if (this_entry.type == ExecutionTraceEntry::Type::CompiledBlock) { }
else if (this_entry.type == ExecutionTraceEntry::Type::CompiledBlock) {
if (next_entry) { if (next_entry) {
if (next_entry->type == ExecutionTraceEntry::Type::Instruction || next_entry->type == ExecutionTraceEntry::Type::CompiledBlock) { if (next_entry->type == ExecutionTraceEntry::Type::Instruction || next_entry->type == ExecutionTraceEntry::Type::CompiledBlock) {
cfg.branches[this_entry.data.compiled_block.exit_address].insert(next_entry->GetPrimaryAddress()); cfg.branches[this_entry.data.compiled_block.exit_address].insert(next_entry->GetPrimaryAddress());
} else if (next_entry->type == ExecutionTraceEntry::Type::FunctionCall) { }
else if (next_entry->type == ExecutionTraceEntry::Type::FunctionCall) {
cfg.calls[this_entry.data.compiled_block.exit_address].insert(next_entry->GetPrimaryAddress()); cfg.calls[this_entry.data.compiled_block.exit_address].insert(next_entry->GetPrimaryAddress());
} }
} }
@ -594,8 +598,6 @@ void RecompilationEngine::CompileBlock(BlockEntry & block_entry) {
std::get<0>(m_address_to_function[block_entry.cfg.start_address]) = compileResult.first; std::get<0>(m_address_to_function[block_entry.cfg.start_address]) = compileResult.first;
block_entry.last_compiled_cfg_size = block_entry.cfg.GetSize(); block_entry.last_compiled_cfg_size = block_entry.cfg.GetSize();
block_entry.is_compiled = true; block_entry.is_compiled = true;
} }
std::shared_ptr<RecompilationEngine> RecompilationEngine::GetInstance() { std::shared_ptr<RecompilationEngine> RecompilationEngine::GetInstance() {
@ -698,11 +700,9 @@ ppu_recompiler_llvm::CPUHybridDecoderRecompiler::CPUHybridDecoderRecompiler(PPUT
} }
ppu_recompiler_llvm::CPUHybridDecoderRecompiler::~CPUHybridDecoderRecompiler() { ppu_recompiler_llvm::CPUHybridDecoderRecompiler::~CPUHybridDecoderRecompiler() {
} }
u32 ppu_recompiler_llvm::CPUHybridDecoderRecompiler::DecodeMemory(const u32 address) { u32 ppu_recompiler_llvm::CPUHybridDecoderRecompiler::DecodeMemory(const u32 address) {
ExecuteFunction(&m_ppu, 0); ExecuteFunction(&m_ppu, 0);
return 0; return 0;
} }
@ -751,7 +751,8 @@ u32 ppu_recompiler_llvm::CPUHybridDecoderRecompiler::ExecuteTillReturn(PPUThread
execution_engine->m_tracer.Trace(Tracer::TraceType::ExitFromCompiledBlock, entry, exit); execution_engine->m_tracer.Trace(Tracer::TraceType::ExitFromCompiledBlock, entry, exit);
if (exit == 0) if (exit == 0)
return 0; return 0;
} else { }
else {
execution_engine->m_tracer.Trace(Tracer::TraceType::Instruction, ppu_state->PC, 0); execution_engine->m_tracer.Trace(Tracer::TraceType::Instruction, ppu_state->PC, 0);
u32 instruction = vm::ps3::read32(ppu_state->PC); u32 instruction = vm::ps3::read32(ppu_state->PC);
u32 oldPC = ppu_state->PC; u32 oldPC = ppu_state->PC;

View file

@ -274,7 +274,7 @@ namespace ppu_recompiler_llvm {
}; };
Compiler(RecompilationEngine & recompilation_engine, const Executable execute_unknown_function, Compiler(RecompilationEngine & recompilation_engine, const Executable execute_unknown_function,
const Executable execute_unknown_block, bool (*poll_status_function)(PPUThread * ppu_state)); const Executable execute_unknown_block, bool(*poll_status_function)(PPUThread * ppu_state));
Compiler(const Compiler & other) = delete; Compiler(const Compiler & other) = delete;
Compiler(Compiler && other) = delete; Compiler(Compiler && other) = delete;
@ -735,7 +735,7 @@ namespace ppu_recompiler_llvm {
RecompilationEngine & m_recompilation_engine; RecompilationEngine & m_recompilation_engine;
/// The function that should be called to check the status of the thread /// The function that should be called to check the status of the thread
bool (*m_poll_status_function)(PPUThread * ppu_state); bool(*m_poll_status_function)(PPUThread * ppu_state);
/// The function that will be called to execute unknown functions /// The function that will be called to execute unknown functions
llvm::Function * m_execute_unknown_function; llvm::Function * m_execute_unknown_function;

View file

@ -3232,7 +3232,6 @@ void Compiler::MTSPR(u32 spr, u32 rs) {
assert(0); assert(0);
break; break;
} }
} }
void Compiler::NAND(u32 ra, u32 rs, u32 rb, u32 rc) { void Compiler::NAND(u32 ra, u32 rs, u32 rb, u32 rc) {

View file

@ -12,7 +12,7 @@
#include <sstream> #include <sstream>
#define PPU_LLVM_RECOMPILER_UNIT_TESTS 1 // Uncomment to enable tests //#define PPU_LLVM_RECOMPILER_UNIT_TESTS 1 // Uncomment to enable tests
//#define PPU_LLVM_RECOMPILER_UNIT_TESTS_VERBOSE 1 // Uncomment to print everything (even for passed tests) //#define PPU_LLVM_RECOMPILER_UNIT_TESTS_VERBOSE 1 // Uncomment to print everything (even for passed tests)
using namespace llvm; using namespace llvm;
@ -241,10 +241,9 @@ static std::string StateDiff(PPUState const & recomp, PPUState const & interp) {
} }
return ret; return ret;
} }
#endif // PPU_LLVM_RECOMPILER_UNIT_TESTS #endif // PPU_LLVM_RECOMPILER_UNIT_TESTS
#ifdef PPU_LLVM_RECOMPILER_UNIT_TESTS #ifdef PPU_LLVM_RECOMPILER_UNIT_TESTS
static PPUThread * s_ppu_state = nullptr; static PPUThread * s_ppu_state = nullptr;
static PPUInterpreter * s_interpreter = nullptr; static PPUInterpreter * s_interpreter = nullptr;
@ -326,8 +325,6 @@ void Compiler::RunTest(const char * name, std::function<void()> test_case, std::
return; return;
} }
// Optimize // Optimize
fpm->run(*m_state.function); fpm->run(*m_state.function);
@ -373,7 +370,8 @@ void Compiler::RunTest(const char * name, std::function<void()> test_case, std::
#else #else
m_recompilation_engine.Log() << "Test " << name << " passed\n"; m_recompilation_engine.Log() << "Test " << name << " passed\n";
#endif // PPU_LLVM_RECOMPILER_UNIT_TESTS_VERBOSE #endif // PPU_LLVM_RECOMPILER_UNIT_TESTS_VERBOSE
} else { }
else {
m_recompilation_engine.Log() << logmsg.str() << "Test " << name << " failed\n" << msg << "\n"; m_recompilation_engine.Log() << logmsg.str() << "Test " << name << " failed\n" << msg << "\n";
} }