From 351482e9c667a2b827a50a2eb52571473e679338 Mon Sep 17 00:00:00 2001 From: LittleCube Date: Sat, 4 Jan 2025 21:49:31 -0500 Subject: [PATCH] Fix TRACE_ENTRY and move `function_sizes` (#112) --- src/config.cpp | 55 ++++++++++++++++++++----------------------- src/recompilation.cpp | 2 +- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index f191ba5..35d084e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -127,41 +127,32 @@ std::vector get_renamed_funcs(const toml::table* patches_data) { return renamed_funcs; } -std::vector get_func_sizes(const toml::table* patches_data) { +std::vector get_func_sizes(const toml::array* func_sizes_array) { std::vector func_sizes{}; - // Check if the func size array exists. - const toml::node_view funcs_data = (*patches_data)["function_sizes"]; - if (funcs_data.is_array()) { - const toml::array* sizes_array = funcs_data.as_array(); + // Reserve room for all the funcs in the map. + func_sizes.reserve(func_sizes_array->size()); + func_sizes_array->for_each([&func_sizes](auto&& el) { + if constexpr (toml::is_table) { + std::optional func_name = el["name"].template value(); + std::optional func_size = el["size"].template value(); - // Copy all the sizes into the output vector. - sizes_array->for_each([&func_sizes](auto&& el) { - if constexpr (toml::is_table) { - const toml::table& cur_size = *el.as_table(); - - // Get the function name and size. - std::optional func_name = cur_size["name"].value(); - std::optional func_size = cur_size["size"].value(); - - if (func_name.has_value() && func_size.has_value()) { - // Make sure the size is divisible by 4 - if (func_size.value() & (4 - 1)) { - // It's not, so throw an error (and make it look like a normal toml one). - throw toml::parse_error("Function size is not divisible by 4", el.source()); - } + if (func_name.has_value() && func_size.has_value()) { + // Make sure the size is divisible by 4 + if (func_size.value() & (4 - 1)) { + // It's not, so throw an error (and make it look like a normal toml one). + throw toml::parse_error("Function size is not divisible by 4", el.source()); } - else { - throw toml::parse_error("Manually size function is missing required value(s)", el.source()); - } - func_sizes.emplace_back(func_name.value(), func_size.value()); } else { - throw toml::parse_error("Invalid manually sized function entry", el.source()); + throw toml::parse_error("Manually sized function is missing required value(s)", el.source()); } - }); - } + } + else { + throw toml::parse_error("Missing required value in function_sizes array", el.source()); + } + }); return func_sizes; } @@ -352,6 +343,13 @@ N64Recomp::Config::Config(const char* path) { manual_functions = get_manual_funcs(array); } + // Manual function sizes (optional) + toml::node_view function_sizes_data = input_data["function_sizes"]; + if (function_sizes_data.is_array()) { + const toml::array* array = function_sizes_data.as_array(); + manual_func_sizes = get_func_sizes(array); + } + // Output binary path when using an elf file input, includes patching reference symbol MIPS32 relocs (optional) std::optional output_binary_path_opt = input_data["output_binary_path"].value(); if (output_binary_path_opt.has_value()) { @@ -406,9 +404,6 @@ N64Recomp::Config::Config(const char* path) { // Single-instruction patches (optional) instruction_patches = get_instruction_patches(table); - // Manual function sizes (optional) - manual_func_sizes = get_func_sizes(table); - // Function hooks (optional) function_hooks = get_function_hooks(table); } diff --git a/src/recompilation.cpp b/src/recompilation.cpp index cf12c49..8ea9bdd 100644 --- a/src/recompilation.cpp +++ b/src/recompilation.cpp @@ -765,7 +765,7 @@ bool recompile_function_impl(GeneratorType& generator, const N64Recomp::Context& if (context.trace_mode) { fmt::print(output_file, - " TRACE_ENTRY();", + " TRACE_ENTRY()\n", func.name); }