From c528cf9ec3964591684accc89dc7f5a36773828d Mon Sep 17 00:00:00 2001 From: Tyler Wilding Date: Sat, 25 May 2024 13:43:40 -0400 Subject: [PATCH] decomp: automatically format the outputs for jak 1 if future updates occur --- decompiler/ObjectFile/ObjectFileDB_IR2.cpp | 18 ++++++++++++++++-- decompiler/config.cpp | 3 +++ decompiler/config.h | 1 + decompiler/config/jak1/jak1_config.jsonc | 4 ++++ out/jak1/obj_before/.gitignore | 3 --- test/offline/framework/execution.cpp | 3 ++- test/offline/framework/orchestration.cpp | 3 +++ 7 files changed, 29 insertions(+), 6 deletions(-) delete mode 100644 out/jak1/obj_before/.gitignore diff --git a/decompiler/ObjectFile/ObjectFileDB_IR2.cpp b/decompiler/ObjectFile/ObjectFileDB_IR2.cpp index 8aed2f23a7..abef0ff791 100644 --- a/decompiler/ObjectFile/ObjectFileDB_IR2.cpp +++ b/decompiler/ObjectFile/ObjectFileDB_IR2.cpp @@ -11,6 +11,7 @@ #include "common/util/FileUtil.h" #include "common/util/Timer.h" #include "common/util/string_util.h" +#include #include "decompiler/IR2/Form.h" #include "decompiler/analysis/analyze_inspect_method.h" @@ -872,9 +873,22 @@ void ObjectFileDB::ir2_write_results(const fs::path& output_dir, auto file_name = output_dir / (obj.to_unique_name() + "_ir2.asm"); file_util::write_text_file(file_name, file_text); - auto final = ir2_final_out(obj, imports, {}); + auto unformatted_code = ir2_final_out(obj, imports, {}); auto final_name = output_dir / (obj.to_unique_name() + "_disasm.gc"); - file_util::write_text_file(final_name, final); + if (config.format_code) { + const auto formatted_code = formatter::format_code(unformatted_code); + if (!formatted_code) { + lg::error( + "Was unable to format the decompiled result of {}, make a github issue. Writing " + "unformatted code", + obj.to_unique_name()); + file_util::write_text_file(final_name, unformatted_code); + } else { + file_util::write_text_file(final_name, formatted_code.value()); + } + } else { + file_util::write_text_file(final_name, unformatted_code); + } } } diff --git a/decompiler/config.cpp b/decompiler/config.cpp index 984b1f564a..d0d7d91275 100644 --- a/decompiler/config.cpp +++ b/decompiler/config.cpp @@ -96,6 +96,9 @@ Config make_config_via_json(nlohmann::json& json) { } config.disassemble_code = json.at("disassemble_code").get(); config.decompile_code = json.at("decompile_code").get(); + if (json.contains("format_code")) { + config.format_code = json.at("format_code").get(); + } config.write_hex_near_instructions = json.at("write_hex_near_instructions").get(); config.write_scripts = json.at("write_scripts").get(); config.disassemble_data = json.at("disassemble_data").get(); diff --git a/decompiler/config.h b/decompiler/config.h index 01ddca559a..9516a212ad 100644 --- a/decompiler/config.h +++ b/decompiler/config.h @@ -111,6 +111,7 @@ struct Config { bool disassemble_code = false; bool decompile_code = false; + bool format_code = false; bool write_scripts = false; bool disassemble_data = false; bool process_tpages = false; diff --git a/decompiler/config/jak1/jak1_config.jsonc b/decompiler/config/jak1/jak1_config.jsonc index b8d20a4310..85e7c3edfd 100644 --- a/decompiler/config/jak1/jak1_config.jsonc +++ b/decompiler/config/jak1/jak1_config.jsonc @@ -23,6 +23,10 @@ // run the first pass of the decompiler "find_functions": true, + // will attempt to run the decompiled output through the OpenGOAL formatter + // this will be skipped in offline tests + "format_code": true, + //////////////////////////// // DATA ANALYSIS OPTIONS //////////////////////////// diff --git a/out/jak1/obj_before/.gitignore b/out/jak1/obj_before/.gitignore deleted file mode 100644 index 268b9c8dd5..0000000000 --- a/out/jak1/obj_before/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -* -!.gitignore -!hash.md5 diff --git a/test/offline/framework/execution.cpp b/test/offline/framework/execution.cpp index 93f4579c86..d7464de454 100644 --- a/test/offline/framework/execution.cpp +++ b/test/offline/framework/execution.cpp @@ -12,7 +12,8 @@ std::unordered_map game_name_to_all_types = { {"jak1", "jak1/all-types.gc"}, {"jak2", "jak2/all-types.gc"}, - {"jak3", "jak3/all-types.gc"}}; + {"jak3", "jak3/all-types.gc"}, + {"jakx", "jakx/all-types.gc"}}; void disassemble(OfflineTestDecompiler& dc) { dc.db->process_link_data(*dc.config); diff --git a/test/offline/framework/orchestration.cpp b/test/offline/framework/orchestration.cpp index b8f1e30be2..1ce36e67cd 100644 --- a/test/offline/framework/orchestration.cpp +++ b/test/offline/framework/orchestration.cpp @@ -38,6 +38,9 @@ OfflineTestDecompiler setup_decompiler(const OfflineTestWorkGroup& work, object_files.insert(file.name_in_dgo); // todo, make this work with unique_name } + // skip formatting, adds unnecessary time to the process and the formatter has not been optimized + // yet + dc.config->format_code = false; dc.config->allowed_objects = object_files; // don't try to do this because we can't write the file dc.config->generate_symbol_definition_map = false;