diff --git a/src/binary_translation/ARMFuncs.cpp b/src/binary_translation/ARMFuncs.cpp index d2b71a781..06885a745 100644 --- a/src/binary_translation/ARMFuncs.cpp +++ b/src/binary_translation/ARMFuncs.cpp @@ -27,7 +27,7 @@ ARMFuncs::ResultCarry ARMFuncs::Shift_C(InstructionBlock* instruction, llvm::Val { auto ir_builder = instruction->IrBuilder(); - auto amount_zero = ir_builder->CreateICmpEQ(amount, ir_builder->getInt32(0)); + auto amount_zero = ir_builder->CreateICmpEQ(amount, ir_builder->getInt32(0)); ResultCarry result_amount_not_zero = {}; switch (type) { @@ -39,8 +39,8 @@ ARMFuncs::ResultCarry ARMFuncs::Shift_C(InstructionBlock* instruction, llvm::Val default: assert(false, "Invalid shift type"); } - auto result = ir_builder->CreateSelect(amount_zero, value, result_amount_not_zero.result); - auto carry = ir_builder->CreateSelect(amount_zero, carry_in, result_amount_not_zero.carry); + auto result = ir_builder->CreateSelect(amount_zero, value, result_amount_not_zero.result); + auto carry = ir_builder->CreateSelect(amount_zero, carry_in, result_amount_not_zero.carry); return{ result, carry }; } @@ -52,10 +52,10 @@ llvm::Value* ShiftZeroCheck( { auto ir_builder = instruction->IrBuilder(); - auto amount_zero = ir_builder->CreateICmpEQ(shift, ir_builder->getInt32(0)); + auto amount_zero = ir_builder->CreateICmpEQ(shift, ir_builder->getInt32(0)); auto result_amount_not_zero = non_zero_function(instruction, x, shift); - return ir_builder->CreateSelect(amount_zero, x, result_amount_not_zero.result); + return ir_builder->CreateSelect(amount_zero, x, result_amount_not_zero.result); } ARMFuncs::ResultCarry ARMFuncs::LSL_C(InstructionBlock* instruction, llvm::Value* x, llvm::Value* shift) @@ -128,11 +128,11 @@ llvm::Value* ARMFuncs::ARMExpandImm(InstructionBlock* instruction, u32 imm12) ARMFuncs::ResultCarry ARMFuncs::ARMExpandImm_C(InstructionBlock *instruction, u32 imm12, llvm::Value* carry) { - auto ir_builder = instruction->IrBuilder(); + auto ir_builder = instruction->IrBuilder(); - auto value = ir_builder->getInt32(imm12 & 0xFF); - auto shift = ir_builder->getInt32(2 * (imm12 >> 8)); - return Shift_C(instruction, value, SRType::ROR, shift, carry); + auto value = ir_builder->getInt32(imm12 & 0xFF); + auto shift = ir_builder->getInt32(2 * (imm12 >> 8)); + return Shift_C(instruction, value, SRType::ROR, shift, carry); } // AddWithCarry from armsupp.cpp diff --git a/src/binary_translation/ARMFuncs.h b/src/binary_translation/ARMFuncs.h index c96183f97..0097663fa 100644 --- a/src/binary_translation/ARMFuncs.h +++ b/src/binary_translation/ARMFuncs.h @@ -45,7 +45,7 @@ public: static ResultCarry RRX_C(InstructionBlock *instruction, llvm::Value *x, llvm::Value *carry_in); static llvm::Value *ARMExpandImm(InstructionBlock *instruction, u32 imm12); - static ResultCarry ARMExpandImm_C(InstructionBlock *instruction, u32 imm12, llvm::Value *carry); + static ResultCarry ARMExpandImm_C(InstructionBlock *instruction, u32 imm12, llvm::Value *carry); static ResultCarryOverflow AddWithCarry(InstructionBlock *instruction, llvm::Value *x, llvm::Value *y, llvm::Value *carry_in); }; \ No newline at end of file diff --git a/src/binary_translation/BlockColors.cpp b/src/binary_translation/BlockColors.cpp index 67a5b4ef4..633c487ca 100644 --- a/src/binary_translation/BlockColors.cpp +++ b/src/binary_translation/BlockColors.cpp @@ -8,8 +8,8 @@ using namespace llvm; BlockColors::BlockColors(ModuleGen* module) : module(module) { - auto ir_builder = module->IrBuilder(); - function_type = FunctionType::get(ir_builder->getVoidTy(), ir_builder->getInt32Ty(), false); + auto ir_builder = module->IrBuilder(); + function_type = FunctionType::get(ir_builder->getVoidTy(), ir_builder->getInt32Ty(), false); } BlockColors::~BlockColors() @@ -18,69 +18,69 @@ BlockColors::~BlockColors() void BlockColors::AddBlock(InstructionBlock* block) { - if (block->HasColor()) return; + if (block->HasColor()) return; - std::stack current_color_stack; - current_color_stack.push(block); - auto color = colors.size(); - colors.push_back({ color }); + std::stack current_color_stack; + current_color_stack.push(block); + auto color = colors.size(); + colors.push_back({ color }); - while (current_color_stack.size()) - { - auto item = current_color_stack.top(); - current_color_stack.pop(); + while (current_color_stack.size()) + { + auto item = current_color_stack.top(); + current_color_stack.pop(); - item->SetColor(color); - colors[color].instructions.push_back(item); - for (auto next : item->GetNexts()) - { - if (next->HasColor()) assert(next->GetColor() == color); - else current_color_stack.push(next); - } - for (auto prev : item->GetPrevs()) - { - if (prev->HasColor()) assert(prev->GetColor() == color); - else current_color_stack.push(prev); - } - } + item->SetColor(color); + colors[color].instructions.push_back(item); + for (auto next : item->GetNexts()) + { + if (next->HasColor()) assert(next->GetColor() == color); + else current_color_stack.push(next); + } + for (auto prev : item->GetPrevs()) + { + if (prev->HasColor()) assert(prev->GetColor() == color); + else current_color_stack.push(prev); + } + } } void BlockColors::GenerateFunctions() { - auto ir_builder = module->IrBuilder(); + auto ir_builder = module->IrBuilder(); - LOG_INFO(BinaryTranslator, "%x block colors", colors.size()); + LOG_INFO(BinaryTranslator, "%x block colors", colors.size()); - for (auto &color : colors) - { - auto function = Function::Create(function_type, GlobalValue::PrivateLinkage, - "ColorFunction", module->Module()); - color.function = function; - auto index = &function->getArgumentList().front(); + for (auto &color : colors) + { + auto function = Function::Create(function_type, GlobalValue::PrivateLinkage, + "ColorFunction", module->Module()); + color.function = function; + auto index = &function->getArgumentList().front(); - auto entry_basic_block = BasicBlock::Create(getGlobalContext(), "Entry", function); - auto default_case_basic_block = BasicBlock::Create(getGlobalContext(), "Default", function); + auto entry_basic_block = BasicBlock::Create(getGlobalContext(), "Entry", function); + auto default_case_basic_block = BasicBlock::Create(getGlobalContext(), "Default", function); - ir_builder->SetInsertPoint(default_case_basic_block); - ir_builder->CreateUnreachable(); + ir_builder->SetInsertPoint(default_case_basic_block); + ir_builder->CreateUnreachable(); - ir_builder->SetInsertPoint(entry_basic_block); - auto switch_instruction = ir_builder->CreateSwitch(index, default_case_basic_block, color.instructions.size()); - for (size_t i = 0; i < color.instructions.size(); ++i) - { - switch_instruction->addCase(ir_builder->getInt32(i), color.instructions[i]->GetEntryBasicBlock()); - AddBasicBlocksToFunction(function, color.instructions[i]->GetEntryBasicBlock()); - } - } + ir_builder->SetInsertPoint(entry_basic_block); + auto switch_instruction = ir_builder->CreateSwitch(index, default_case_basic_block, color.instructions.size()); + for (size_t i = 0; i < color.instructions.size(); ++i) + { + switch_instruction->addCase(ir_builder->getInt32(i), color.instructions[i]->GetEntryBasicBlock()); + AddBasicBlocksToFunction(function, color.instructions[i]->GetEntryBasicBlock()); + } + } } void BlockColors::AddBasicBlocksToFunction(Function* function, BasicBlock* basic_block) { - if (basic_block->getParent()) - { - assert(basic_block->getParent() == function); - return; - } + if (basic_block->getParent()) + { + assert(basic_block->getParent() == function); + return; + } std::stack basic_blocks; basic_blocks.push(basic_block); diff --git a/src/binary_translation/BlockColors.h b/src/binary_translation/BlockColors.h index e87afe784..206053ee1 100644 --- a/src/binary_translation/BlockColors.h +++ b/src/binary_translation/BlockColors.h @@ -2,9 +2,9 @@ namespace llvm { - class BasicBlock; - class Function; - class FunctionType; + class BasicBlock; + class Function; + class FunctionType; } class InstructionBlock; class ModuleGen; @@ -19,32 +19,32 @@ And to generate a function for each color class BlockColors { public: - BlockColors(ModuleGen *module); - ~BlockColors(); + BlockColors(ModuleGen *module); + ~BlockColors(); - void AddBlock(InstructionBlock *block); - // Generates a function for each color - void GenerateFunctions(); + void AddBlock(InstructionBlock *block); + // Generates a function for each color + void GenerateFunctions(); - llvm::FunctionType *GetFunctionType() { return function_type; } - size_t GetColorCount() const { return colors.size(); } + llvm::FunctionType *GetFunctionType() { return function_type; } + size_t GetColorCount() const { return colors.size(); } size_t GetColorInstructionCount(size_t color) const { return colors[color].instructions.size(); } - InstructionBlock *GetColorInstruction(size_t color, size_t index) { return colors[color].instructions[index]; } - llvm::Function *GetColorFunction(size_t color) { return colors[color].function; } + InstructionBlock *GetColorInstruction(size_t color, size_t index) { return colors[color].instructions[index]; } + llvm::Function *GetColorFunction(size_t color) { return colors[color].function; } private: - ModuleGen *module; + ModuleGen *module; - // void ColorFunction(int i) - // Runs the code for color->instructions[i] - llvm::FunctionType *function_type; + // void ColorFunction(int i) + // Runs the code for color->instructions[i] + llvm::FunctionType *function_type; - void AddBasicBlocksToFunction(llvm::Function *function, llvm::BasicBlock *basic_block); + void AddBasicBlocksToFunction(llvm::Function *function, llvm::BasicBlock *basic_block); - struct Color - { - size_t color; - std::vector instructions; - llvm::Function *function; - }; - std::vector colors; + struct Color + { + size_t color; + std::vector instructions; + llvm::Function *function; + }; + std::vector colors; }; \ No newline at end of file diff --git a/src/binary_translation/CMakeLists.txt b/src/binary_translation/CMakeLists.txt index 1a548ab53..ed79ccd16 100644 --- a/src/binary_translation/CMakeLists.txt +++ b/src/binary_translation/CMakeLists.txt @@ -1,41 +1,41 @@ set(SRCS - main.cpp - CodeGen.cpp - ModuleGen.cpp - Disassembler.cpp - InstructionBlock.cpp - MachineState.cpp - TBAA.cpp - ARMFuncs.cpp + main.cpp + CodeGen.cpp + ModuleGen.cpp + Disassembler.cpp + InstructionBlock.cpp + MachineState.cpp + TBAA.cpp + ARMFuncs.cpp BlockColors.cpp - - Instructions/Instruction.cpp - Instructions/MovShift.cpp - Instructions/Branch.cpp - Instructions/Arithmetic.cpp - Instructions/Ldr.cpp - Instructions/Str.cpp + + Instructions/Instruction.cpp + Instructions/MovShift.cpp + Instructions/Branch.cpp + Instructions/Arithmetic.cpp + Instructions/Ldr.cpp + Instructions/Str.cpp ) set(HEADERS - CodeGen.h - ModuleGen.h - Disassembler.h - InstructionBlock.h - MachineState.h - TBAA.h - BinarySearch.h - ARMFuncs.h + CodeGen.h + ModuleGen.h + Disassembler.h + InstructionBlock.h + MachineState.h + TBAA.h + BinarySearch.h + ARMFuncs.h BlockColors.h - - Instructions/Types.h - Instructions/Instruction.h - Instructions/MovShift.h - Instructions/Branch.h - Instructions/Arithmetic.h - Instructions/Ldr.h - Instructions/Str.h + + Instructions/Types.h + Instructions/Instruction.h + Instructions/MovShift.h + Instructions/Branch.h + Instructions/Arithmetic.h + Instructions/Ldr.h + Instructions/Str.h ) - + create_directory_groups(${SRCS} ${HEADERS}) include_directories(.) diff --git a/src/binary_translation/CodeGen.cpp b/src/binary_translation/CodeGen.cpp index 56fe35449..1181b0bd2 100644 --- a/src/binary_translation/CodeGen.cpp +++ b/src/binary_translation/CodeGen.cpp @@ -19,8 +19,8 @@ using namespace llvm; CodeGen::CodeGen(const char* output_object_filename, const char* output_debug_filename, bool verify) - : output_object_filename(output_object_filename), - output_debug_filename(output_debug_filename), + : output_object_filename(output_object_filename), + output_debug_filename(output_debug_filename), verify(verify) { } @@ -31,11 +31,11 @@ CodeGen::~CodeGen() void CodeGen::Run() { - if (!Loader::ROMCodeStart) - { - LOG_CRITICAL(BinaryTranslator, "No information from the loader about ROM file."); + if (!Loader::ROMCodeStart) + { + LOG_CRITICAL(BinaryTranslator, "No information from the loader about ROM file."); return; - } + } InitializeLLVM(); GenerateModule(); diff --git a/src/binary_translation/CodeGen.h b/src/binary_translation/CodeGen.h index b718ce659..c462f99af 100644 --- a/src/binary_translation/CodeGen.h +++ b/src/binary_translation/CodeGen.h @@ -15,8 +15,8 @@ class ModuleGen; class CodeGen { public: - CodeGen(const char *output_object_filename, const char *output_debug_filename, bool verify); - ~CodeGen(); + CodeGen(const char *output_object_filename, const char *output_debug_filename, bool verify); + ~CodeGen(); void Run(); void InitializeLLVM(); @@ -25,8 +25,8 @@ public: bool Verify(); void OptimizeAndGenerate(); private: - const char *output_object_filename; - const char *output_debug_filename; + const char *output_object_filename; + const char *output_debug_filename; bool verify; std::unique_ptr moduleGenerator; diff --git a/src/binary_translation/InstructionBlock.cpp b/src/binary_translation/InstructionBlock.cpp index b01b4d13f..3e3c3377d 100644 --- a/src/binary_translation/InstructionBlock.cpp +++ b/src/binary_translation/InstructionBlock.cpp @@ -50,8 +50,8 @@ llvm::BasicBlock *InstructionBlock::CreateBasicBlock(const char *name) void InstructionBlock::Link(InstructionBlock* prev, InstructionBlock* next) { - prev->nexts.push_back(next); - next->prevs.push_back(prev); + prev->nexts.push_back(next); + next->prevs.push_back(prev); } u32 InstructionBlock::Address() const diff --git a/src/binary_translation/InstructionBlock.h b/src/binary_translation/InstructionBlock.h index ecbe7f795..8f87bb73a 100644 --- a/src/binary_translation/InstructionBlock.h +++ b/src/binary_translation/InstructionBlock.h @@ -50,9 +50,9 @@ public: */ llvm::BasicBlock *CreateBasicBlock(const char *name); /* - * Links two instructions, adding to prev and next lists - */ - static void Link(InstructionBlock *prev, InstructionBlock *next); + * Links two instructions, adding to prev and next lists + */ + static void Link(InstructionBlock *prev, InstructionBlock *next); u32 Address() const; ModuleGen *Module() { return module; } @@ -60,11 +60,11 @@ public: llvm::BasicBlock *GetEntryBasicBlock() { return entry_basic_block; } - bool HasColor() const { return has_color; } - void SetColor(size_t color) { this->color = color; has_color = true; } + bool HasColor() const { return has_color; } + void SetColor(size_t color) { this->color = color; has_color = true; } size_t GetColor() const { return color; } - std::list GetNexts() const { return nexts; } + std::list GetNexts() const { return nexts; } std::list GetPrevs() const { return prevs; } private: // Textual representation of the address @@ -77,9 +77,9 @@ private: // The block at the entry to instruction llvm::BasicBlock *entry_basic_block; - bool has_color = false; - size_t color; + bool has_color = false; + size_t color; - std::list nexts; - std::list prevs; + std::list nexts; + std::list prevs; }; \ No newline at end of file diff --git a/src/binary_translation/Instructions/Instruction.cpp b/src/binary_translation/Instructions/Instruction.cpp index 00a39b9ff..95101928c 100644 --- a/src/binary_translation/Instructions/Instruction.cpp +++ b/src/binary_translation/Instructions/Instruction.cpp @@ -56,7 +56,7 @@ void Instruction::GenerateCode(InstructionBlock *instruction_block) // If not, jump to the next instruction if (!ir_builder->GetInsertBlock()->getTerminator()) { - instruction_block->Module()->BranchWritePCConst(instruction_block, Address() + 4); + instruction_block->Module()->BranchWritePCConst(instruction_block, Address() + 4); } } diff --git a/src/binary_translation/Instructions/MovShift.cpp b/src/binary_translation/Instructions/MovShift.cpp index 22a7a2f5f..fc00acfcb 100644 --- a/src/binary_translation/Instructions/MovShift.cpp +++ b/src/binary_translation/Instructions/MovShift.cpp @@ -17,20 +17,20 @@ bool MovShift::Decode() if (rd == Register::PC && s) return false; // SEE SUBS PC, LR and related instructions; return true; } - if (ReadFields({ CondDef(), FieldDef<7>(0x1d), FieldDef<1>(&s), FieldDef<4>(0), - FieldDef<4>(&rd), FieldDef<12>(&imm12) })) - { - form = Form::ImmediateA1; - return true; - } - if (ReadFields({ CondDef(), FieldDef<8>(0x30), FieldDef<4>(&imm4), - FieldDef<4>(&rd), FieldDef<12>(&imm12) })) - { - s = false; - form = Form::ImmediateA2; - if (rd == Register::PC) return false; // UNPREDICTIBLE - return true; - } + if (ReadFields({ CondDef(), FieldDef<7>(0x1d), FieldDef<1>(&s), FieldDef<4>(0), + FieldDef<4>(&rd), FieldDef<12>(&imm12) })) + { + form = Form::ImmediateA1; + return true; + } + if (ReadFields({ CondDef(), FieldDef<8>(0x30), FieldDef<4>(&imm4), + FieldDef<4>(&rd), FieldDef<12>(&imm12) })) + { + s = false; + form = Form::ImmediateA2; + if (rd == Register::PC) return false; // UNPREDICTIBLE + return true; + } return false; } @@ -39,50 +39,50 @@ void MovShift::GenerateInstructionCode(InstructionBlock* instruction_block) auto ir_builder = instruction_block->IrBuilder(); auto carry_in = instruction_block->Read(Register::C); - ARMFuncs::ResultCarry result = {}; + ARMFuncs::ResultCarry result = {}; - switch (form) - { - case Form::Register: - result = { instruction_block->Read(rm), carry_in }; - switch (op2) - { - case Op2Type::MoveAndLSL: - if (imm5 != 0) - { - result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::LSL, - ARMFuncs::DecodeImmShift(instruction_block, 0, imm5).amount, result.carry); - } - break; - case Op2Type::LSR: - result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::LSR, - ARMFuncs::DecodeImmShift(instruction_block, 1, imm5).amount, result.carry); - break; - case Op2Type::ASR: - result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::ASR, - ARMFuncs::DecodeImmShift(instruction_block, 2, imm5).amount, result.carry); - break; - case Op2Type::RRXAndROR: - if (imm5 == 0) - { - result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::RRX, - ir_builder->getInt32(1), result.carry); - } - else - { - result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::ROR, - ARMFuncs::DecodeImmShift(instruction_block, 3, imm5).amount, result.carry); - } - break; - } - break; - case Form::ImmediateA1: - result = ARMFuncs::ARMExpandImm_C(instruction_block, imm12, carry_in); - break; - case Form::ImmediateA2: - result.result = ir_builder->getInt32((imm4 << 12) | imm12); - break; - } + switch (form) + { + case Form::Register: + result = { instruction_block->Read(rm), carry_in }; + switch (op2) + { + case Op2Type::MoveAndLSL: + if (imm5 != 0) + { + result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::LSL, + ARMFuncs::DecodeImmShift(instruction_block, 0, imm5).amount, result.carry); + } + break; + case Op2Type::LSR: + result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::LSR, + ARMFuncs::DecodeImmShift(instruction_block, 1, imm5).amount, result.carry); + break; + case Op2Type::ASR: + result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::ASR, + ARMFuncs::DecodeImmShift(instruction_block, 2, imm5).amount, result.carry); + break; + case Op2Type::RRXAndROR: + if (imm5 == 0) + { + result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::RRX, + ir_builder->getInt32(1), result.carry); + } + else + { + result = ARMFuncs::Shift_C(instruction_block, result.result, ARMFuncs::SRType::ROR, + ARMFuncs::DecodeImmShift(instruction_block, 3, imm5).amount, result.carry); + } + break; + } + break; + case Form::ImmediateA1: + result = ARMFuncs::ARMExpandImm_C(instruction_block, imm12, carry_in); + break; + case Form::ImmediateA2: + result.result = ir_builder->getInt32((imm4 << 12) | imm12); + break; + } instruction_block->Write(rd, result.result); diff --git a/src/binary_translation/Instructions/MovShift.h b/src/binary_translation/Instructions/MovShift.h index 168671093..cbe87ed72 100644 --- a/src/binary_translation/Instructions/MovShift.h +++ b/src/binary_translation/Instructions/MovShift.h @@ -28,6 +28,6 @@ private: Register rm; u32 imm12; u32 imm5; - u32 imm4; + u32 imm4; Op2Type op2; }; \ No newline at end of file diff --git a/src/binary_translation/ModuleGen.cpp b/src/binary_translation/ModuleGen.cpp index 8fe0aeec5..15e366035 100644 --- a/src/binary_translation/ModuleGen.cpp +++ b/src/binary_translation/ModuleGen.cpp @@ -21,7 +21,7 @@ ModuleGen::ModuleGen(llvm::Module* module, bool verify) ir_builder = make_unique>(getGlobalContext()); machine = make_unique(this); tbaa = make_unique(); - block_colors = make_unique(this); + block_colors = make_unique(this); } ModuleGen::~ModuleGen() @@ -42,7 +42,7 @@ void ModuleGen::Run() GenerateInstructionsCode(); - ColorBlocks(); + ColorBlocks(); GenerateBlockAddressArray(); } @@ -99,11 +99,11 @@ void ModuleGen::GenerateGlobals() { machine->GenerateGlobals(); - auto function_pointer = PointerType::get(block_colors->GetFunctionType(), 0); - block_address_type = StructType::get(function_pointer, ir_builder->getInt32Ty(), nullptr); - block_address_not_present = ConstantStruct::get(block_address_type, ConstantPointerNull::get(function_pointer), ir_builder->getInt32(0), nullptr); - - auto get_block_address_function_type = FunctionType::get(block_address_type, ir_builder->getInt32Ty(), false); + auto function_pointer = PointerType::get(block_colors->GetFunctionType(), 0); + block_address_type = StructType::get(function_pointer, ir_builder->getInt32Ty(), nullptr); + block_address_not_present = ConstantStruct::get(block_address_type, ConstantPointerNull::get(function_pointer), ir_builder->getInt32(0), nullptr); + + auto get_block_address_function_type = FunctionType::get(block_address_type, ir_builder->getInt32Ty(), false); get_block_address_function = Function::Create(get_block_address_function_type, GlobalValue::PrivateLinkage, "GetBlockAddress", module); auto can_run_function_type = FunctionType::get(ir_builder->getInt1Ty(), false); @@ -115,7 +115,7 @@ void ModuleGen::GenerateGlobals() block_address_array_base = Loader::ROMCodeStart / 4; block_address_array_size = Loader::ROMCodeSize / 4; - block_address_array_type = ArrayType::get(block_address_type, block_address_array_size); + block_address_array_type = ArrayType::get(block_address_type, block_address_array_size); block_address_array = new GlobalVariable(*module, block_address_array_type, true, GlobalValue::ExternalLinkage, nullptr, "BlockAddressArray"); // bool Verify - contains the value of verify for citra usage @@ -132,27 +132,27 @@ void ModuleGen::GenerateBlockAddressArray() std::fill( local_block_address_array_values.get(), local_block_address_array_values.get() + block_address_array_size, - block_address_not_present); + block_address_not_present); /*for (auto i = 0; i < instruction_blocks.size(); ++i) { auto &block = instruction_blocks[i]; auto entry_basic_block = block->GetEntryBasicBlock(); auto index = block->Address() / 4 - block_address_array_base; - auto color_index = 0; + auto color_index = 0; local_block_address_array_values[index] = BConst }*/ - for (auto color = 0; color < block_colors->GetColorCount(); ++color) - { - auto function = block_colors->GetColorFunction(color); - for (auto i = 0; i < block_colors->GetColorInstructionCount(color); ++i) - { - auto block = block_colors->GetColorInstruction(color, i); - auto index = block->Address() / 4 - block_address_array_base; - auto value = ConstantStruct::get(block_address_type, function, ir_builder->getInt32(i), nullptr); - local_block_address_array_values[index] = value; - } - } + for (auto color = 0; color < block_colors->GetColorCount(); ++color) + { + auto function = block_colors->GetColorFunction(color); + for (auto i = 0; i < block_colors->GetColorInstructionCount(color); ++i) + { + auto block = block_colors->GetColorInstruction(color, i); + auto index = block->Address() / 4 - block_address_array_base; + auto value = ConstantStruct::get(block_address_type, function, ir_builder->getInt32(i), nullptr); + local_block_address_array_values[index] = value; + } + } auto local_block_address_array_values_ref = ArrayRef(local_block_address_array_values.get(), block_address_array_size); auto local_blocks_address_array = ConstantArray::get(block_address_array_type, local_block_address_array_values_ref); @@ -205,9 +205,9 @@ void ModuleGen::GenerateCanRunFunction() ir_builder->SetInsertPoint(basic_block); auto block_address = ir_builder->CreateCall(get_block_address_function, machine->ReadRegiser(Register::PC, true)); - auto function = ir_builder->CreateExtractValue(block_address, 0); - ir_builder->CreateRet(ir_builder->CreateICmpNE(function, - ConstantPointerNull::get(cast(function->getType())))); + auto function = ir_builder->CreateExtractValue(block_address, 0); + ir_builder->CreateRet(ir_builder->CreateICmpNE(function, + ConstantPointerNull::get(cast(function->getType())))); } void ModuleGen::GenerateRunFunction() @@ -231,18 +231,18 @@ void ModuleGen::GenerateRunFunction() auto block_present_basic_block = BasicBlock::Create(getGlobalContext(), "BlockPresent", run_function); auto block_not_present_basic_block = BasicBlock::Create(getGlobalContext(), "BlockNotPresent", run_function); - ir_builder->SetInsertPoint(run_function_entry); - auto block_address = ir_builder->CreateCall(get_block_address_function, Machine()->ReadRegiser(Register::PC, true)); - auto function = ir_builder->CreateExtractValue(block_address, 0); - auto block_present_pred = ir_builder->CreateICmpNE(function, - ConstantPointerNull::get(cast(function->getType()))); + ir_builder->SetInsertPoint(run_function_entry); + auto block_address = ir_builder->CreateCall(get_block_address_function, Machine()->ReadRegiser(Register::PC, true)); + auto function = ir_builder->CreateExtractValue(block_address, 0); + auto block_present_pred = ir_builder->CreateICmpNE(function, + ConstantPointerNull::get(cast(function->getType()))); ir_builder->CreateCondBr(block_present_pred, block_present_basic_block, block_not_present_basic_block); ir_builder->SetInsertPoint(block_present_basic_block); - auto index = ir_builder->CreateExtractValue(block_address, 1); - auto call = ir_builder->CreateCall(function, index); - call->setTailCall(); - ir_builder->CreateRetVoid(); + auto index = ir_builder->CreateExtractValue(block_address, 1); + auto call = ir_builder->CreateCall(function, index); + call->setTailCall(); + ir_builder->CreateRetVoid(); ir_builder->SetInsertPoint(block_not_present_basic_block); ir_builder->CreateRetVoid(); @@ -286,9 +286,9 @@ void ModuleGen::GenerateInstructionsCode() void ModuleGen::ColorBlocks() { - for (auto &instruction : instruction_blocks) - { - block_colors->AddBlock(instruction.get()); - } - block_colors->GenerateFunctions(); + for (auto &instruction : instruction_blocks) + { + block_colors->AddBlock(instruction.get()); + } + block_colors->GenerateFunctions(); } \ No newline at end of file diff --git a/src/binary_translation/ModuleGen.h b/src/binary_translation/ModuleGen.h index 016bf1e8d..23a75fbb0 100644 --- a/src/binary_translation/ModuleGen.h +++ b/src/binary_translation/ModuleGen.h @@ -50,10 +50,10 @@ private: // Generates the entry basic blocks for each instruction void GenerateInstructionsEntry(); // Generates the code of each instruction - void GenerateInstructionsCode(); - // Must be run after the instruction code is generated since it depends on the - // inter block jumps - void ColorBlocks(); + void GenerateInstructionsCode(); + // Must be run after the instruction code is generated since it depends on the + // inter block jumps + void ColorBlocks(); llvm::Module *module; bool verify; @@ -64,16 +64,16 @@ private: std::unique_ptr> ir_builder; size_t block_address_array_base; - size_t block_address_array_size; - /* - * struct BlockAddress - * { - * void (*function)(u32 index); - * u32 index; - * } - */ - llvm::StructType *block_address_type; - llvm::Constant *block_address_not_present; + size_t block_address_array_size; + /* + * struct BlockAddress + * { + * void (*function)(u32 index); + * u32 index; + * } + */ + llvm::StructType *block_address_type; + llvm::Constant *block_address_not_present; /* * i8 **BlockAddressArray; * The array at [i/4 - block_address_array_base] contains the block address for the instruction at i @@ -109,5 +109,5 @@ private: std::vector> instruction_blocks; std::unordered_map instruction_blocks_by_pc; - std::unique_ptr block_colors; + std::unique_ptr block_colors; }; \ No newline at end of file diff --git a/src/binary_translation/main.cpp b/src/binary_translation/main.cpp index 839d38110..9f5c100da 100644 --- a/src/binary_translation/main.cpp +++ b/src/binary_translation/main.cpp @@ -28,30 +28,30 @@ int main(int argc, const char *const *argv) } cl::ParseCommandLineOptions(argc, argv); - std::shared_ptr logger = Log::InitGlobalLogger(); - Log::Filter log_filter(Log::Level::Debug); - Log::SetFilter(&log_filter); - std::thread logging_thread(Log::TextLoggingLoop, logger); - SCOPE_EXIT({ - logger->Close(); - logging_thread.join(); - }); + std::shared_ptr logger = Log::InitGlobalLogger(); + Log::Filter log_filter(Log::Level::Debug); + Log::SetFilter(&log_filter); + std::thread logging_thread(Log::TextLoggingLoop, logger); + SCOPE_EXIT({ + logger->Close(); + logging_thread.join(); + }); auto input_rom = InputFilename.c_str(); auto output_object = OutputFilename.c_str(); auto output_debug = DebugFilename.getNumOccurrences() ? DebugFilename.c_str() : nullptr; bool verify = Verify; - Core::Init(); - Memory::Init(); + Core::Init(); + Memory::Init(); - auto load_result = Loader::LoadFile(input_rom); - if (Loader::ResultStatus::Success != load_result) - { + auto load_result = Loader::LoadFile(input_rom); + if (Loader::ResultStatus::Success != load_result) + { LOG_CRITICAL(BinaryTranslator, "Failed to load ROM (Error %i)!", load_result); - return -1; - } + return -1; + } CodeGen code_generator(output_object, output_debug, verify); - code_generator.Run(); + code_generator.Run(); } \ No newline at end of file