From 7a5eb68c5ecd4e6914e11ee84405ef0e7accd3c5 Mon Sep 17 00:00:00 2001 From: Mr-Wiseguy Date: Thu, 12 Sep 2024 00:45:08 -0400 Subject: [PATCH] Fix function calls and add missing runtime function pointers in offline mod recompiler --- OfflineModRecomp/main.cpp | 28 +++++++++++++++++++++++++++- src/mod_symbols.cpp | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/OfflineModRecomp/main.cpp b/OfflineModRecomp/main.cpp index b686dc5..aa25dc8 100644 --- a/OfflineModRecomp/main.cpp +++ b/OfflineModRecomp/main.cpp @@ -174,11 +174,27 @@ int main(int argc, const char** argv) { // Write the event trigger function pointer. output_file << "// Pointer to the runtime function for triggering events.\n"; output_file << "RECOMP_EXPORT void (*recomp_trigger_event)(uint8_t* rdram, recomp_context* ctx, uint32_t) = NULL;\n\n"; - // Write the get_function pointer. + // Write the get_function pointer. output_file << "// Pointer to the runtime function for looking up functions from vram address.\n"; output_file << "RECOMP_EXPORT recomp_func_t* (*get_function)(int32_t vram) = NULL;\n\n"; + // Write the cop0_status_write pointer. + output_file << "// Pointer to the runtime function for performing a cop0 status register write.\n"; + output_file << "RECOMP_EXPORT void (*cop0_status_write)(recomp_context* ctx, gpr value) = NULL;\n\n"; + + // Write the cop0_status_read pointer. + output_file << "// Pointer to the runtime function for performing a cop0 status register read.\n"; + output_file << "RECOMP_EXPORT gpr (*cop0_status_read)(recomp_context* ctx) = NULL;\n\n"; + + // Write the switch_error pointer. + output_file << "// Pointer to the runtime function for reporting switch case errors.\n"; + output_file << "RECOMP_EXPORT void (*switch_error)(const char* func, uint32_t vram, uint32_t jtbl) = NULL;\n\n"; + + // Write the do_break pointer. + output_file << "// Pointer to the runtime function for handling the break instruction.\n"; + output_file << "RECOMP_EXPORT void (*do_break)(uint32_t vram) = NULL;\n\n"; + // Write the section_addresses pointer. output_file << "// Pointer to the runtime's array of loaded section addresses for the base ROM.\n"; output_file << "RECOMP_EXPORT int32_t* reference_section_addresses = NULL;\n\n"; @@ -191,11 +207,21 @@ int main(int argc, const char** argv) { // Create a set of the export indices to avoid renaming them. std::unordered_set export_indices{mod_context.exported_funcs.begin(), mod_context.exported_funcs.end()}; + // Name all the functions in a first pass so function calls emitted in the second are correct. Also emit function prototypes. + output_file << "// Function prototypes.\n"; for (size_t func_index = 0; func_index < mod_context.functions.size(); func_index++) { auto& func = mod_context.functions[func_index]; + // Don't rename exports since they already have a name from the mod symbol file. if (!export_indices.contains(func_index)) { func.name = "mod_func_" + std::to_string(func_index); } + output_file << "RECOMP_FUNC void " << func.name << "(uint8_t* rdram, recomp_context* ctx);\n"; + } + output_file << "\n"; + + // Perform a second pass for recompiling all the functions. + for (size_t func_index = 0; func_index < mod_context.functions.size(); func_index++) { + auto& func = mod_context.functions[func_index]; if (!N64Recomp::recompile_function(mod_context, func, output_file, static_funcs_by_section, true)) { output_file.close(); std::error_code ec; diff --git a/src/mod_symbols.cpp b/src/mod_symbols.cpp index cd1faa3..24675fe 100644 --- a/src/mod_symbols.cpp +++ b/src/mod_symbols.cpp @@ -199,6 +199,8 @@ bool parse_v1(std::span data, const std::unordered_map