From f32e47f9301b44287b21451ca3bd8ba6049cbaa3 Mon Sep 17 00:00:00 2001 From: Gillou68310 Date: Fri, 31 May 2024 15:50:29 +0200 Subject: [PATCH] Replace after_vram by before_vram --- include/recomp_port.h | 2 +- src/config.cpp | 8 ++++---- src/main.cpp | 10 +++++----- src/recompilation.cpp | 34 ++++++++++++++++------------------ 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/include/recomp_port.h b/include/recomp_port.h index 3620669..3828f01 100644 --- a/include/recomp_port.h +++ b/include/recomp_port.h @@ -42,7 +42,7 @@ namespace RecompPort { struct FunctionHook { std::string func_name; - int32_t after_vram; + int32_t before_vram; std::string text; }; diff --git a/src/config.cpp b/src/config.cpp index 159eda0..e106ae6 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -240,7 +240,7 @@ std::vector get_function_hooks(const toml::table* patc const toml::table& cur_hook = *el.as_table(); // Get the vram and make sure it's 4-byte aligned. - std::optional after_vram = cur_hook["after_vram"].value(); + std::optional before_vram = cur_hook["before_vram"].value(); std::optional func_name = cur_hook["func"].value(); std::optional text = cur_hook["text"].value(); @@ -248,14 +248,14 @@ std::vector get_function_hooks(const toml::table* patc throw toml::parse_error("Function hook is missing required value(s)", el.source()); } - if (after_vram.has_value() && after_vram.value() & 0b11) { + if (before_vram.has_value() && before_vram.value() & 0b11) { // Not properly aligned, so throw an error (and make it look like a normal toml one). - throw toml::parse_error("after_vram is not word-aligned", el.source()); + throw toml::parse_error("before_vram is not word-aligned", el.source()); } ret.push_back(RecompPort::FunctionHook{ .func_name = func_name.value(), - .after_vram = after_vram.has_value() ? (int32_t)after_vram.value() : 0, + .before_vram = before_vram.has_value() ? (int32_t)before_vram.value() : 0, .text = text.value(), }); } diff --git a/src/main.cpp b/src/main.cpp index e967cbd..9823d6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1514,22 +1514,22 @@ int main(int argc, char** argv) { int32_t func_vram = func.vram; // Check that the function actually contains this vram address. - if (patch.after_vram < func_vram || patch.after_vram >= func_vram + func.words.size() * sizeof(func.words[0])) { - exit_failure(fmt::format("Function {} has a function hook for vram 0x{:08X} but doesn't contain that vram address!", patch.func_name, (uint32_t)patch.after_vram)); + if (patch.before_vram < func_vram || patch.before_vram >= func_vram + func.words.size() * sizeof(func.words[0])) { + exit_failure(fmt::format("Function {} has a function hook for vram 0x{:08X} but doesn't contain that vram address!", patch.func_name, (uint32_t)patch.before_vram)); } // No after_vram means this will be placed at the start of the function size_t instruction_index = -1; // Calculate the instruction index. - if (patch.after_vram != 0) { - instruction_index = (static_cast(patch.after_vram) - func_vram) / sizeof(uint32_t); + if (patch.before_vram != 0) { + instruction_index = (static_cast(patch.before_vram) - func_vram) / sizeof(uint32_t); } // Check if a function hook already exits for that instruction index. auto hook_find = func.function_hooks.find(instruction_index); if (hook_find != func.function_hooks.end()) { - exit_failure(fmt::format("Function {} already has a function hook for vram 0x{:08X}!", patch.func_name, (uint32_t)patch.after_vram)); + exit_failure(fmt::format("Function {} already has a function hook for vram 0x{:08X}!", patch.func_name, (uint32_t)patch.before_vram)); } func.function_hooks[instruction_index] = patch.text; diff --git a/src/recompilation.cpp b/src/recompilation.cpp index b07805a..e34ed3c 100644 --- a/src/recompilation.cpp +++ b/src/recompilation.cpp @@ -24,23 +24,33 @@ bool process_instruction(const RecompPort::Context& context, const RecompPort::C const auto& instr = instructions[instr_index]; needs_link_branch = false; is_branch_likely = false; + uint32_t instr_vram = instr.getVram(); + + auto print_indent = [&]() { + fmt::print(output_file, " "); + }; + + auto hook_find = func.function_hooks.find(instr_index); + if (hook_find != func.function_hooks.end()) { + fmt::print(output_file, " {}\n", hook_find->second); + if (indent) { + print_indent(); + } + } // Output a comment with the original instruction if (instr.isBranch() || instr.getUniqueId() == InstrId::cpu_j) { - fmt::print(output_file, " // {}\n", instr.disassemble(0, fmt::format("L_{:08X}", (uint32_t)instr.getBranchVramGeneric()))); + fmt::print(output_file, " // 0x{:08X}: {}\n", instr_vram, instr.disassemble(0, fmt::format("L_{:08X}", (uint32_t)instr.getBranchVramGeneric()))); } else if (instr.getUniqueId() == InstrId::cpu_jal) { - fmt::print(output_file, " // {}\n", instr.disassemble(0, fmt::format("0x{:08X}", (uint32_t)instr.getBranchVramGeneric()))); + fmt::print(output_file, " // 0x{:08X}: {}\n", instr_vram, instr.disassemble(0, fmt::format("0x{:08X}", (uint32_t)instr.getBranchVramGeneric()))); } else { - fmt::print(output_file, " // {}\n", instr.disassemble(0)); + fmt::print(output_file, " // 0x{:08X}: {}\n", instr_vram, instr.disassemble(0)); } - uint32_t instr_vram = instr.getVram(); - if (skipped_insns.contains(instr_vram)) { return true; } - bool at_reloc = false; bool reloc_handled = false; RecompPort::RelocType reloc_type = RecompPort::RelocType::R_MIPS_NONE; @@ -71,10 +81,6 @@ bool process_instruction(const RecompPort::Context& context, const RecompPort::C } } - auto print_indent = [&]() { - fmt::print(output_file, " "); - }; - auto print_line = [&](fmt::format_string fmt_str, Ts ...args) { print_indent(); fmt::vprint(output_file, fmt_str, fmt::make_format_args(args...)); @@ -1082,14 +1088,6 @@ bool process_instruction(const RecompPort::Context& context, const RecompPort::C return false; } - auto hook_find = func.function_hooks.find(instr_index); - if (hook_find != func.function_hooks.end()) { - if (indent) { - print_indent(); - } - fmt::print(output_file, " {}\n", hook_find->second); - } - // TODO is this used? if (emit_link_branch) { fmt::print(output_file, " after_{}:\n", link_branch_index);