From 8acd1bd9909a684f6339af431190fabb6443ecdd Mon Sep 17 00:00:00 2001 From: xensik Date: Sat, 21 Dec 2024 15:19:24 +0100 Subject: [PATCH] t6: merge old compiler fixes as flag --- include/xsk/arc/context.hpp | 4 ++++ src/arc/decompiler.cpp | 21 ++++++++++++++++++--- src/arc/disassembler.cpp | 11 +++++++++++ src/tool/main.cpp | 4 ++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/xsk/arc/context.hpp b/include/xsk/arc/context.hpp index d57741c6..a0a8db35 100644 --- a/include/xsk/arc/context.hpp +++ b/include/xsk/arc/context.hpp @@ -35,6 +35,9 @@ struct context auto compiler() -> compiler& { return compiler_; } auto decompiler() -> decompiler& { return decompiler_; } + auto fixup(bool value) -> void { fixup_ = value; } + auto fixup() const -> bool { return fixup_; } + auto init(arc::build build, fs_callback callback) -> void; auto cleanup() -> void; auto engine_name() const -> std::string_view; @@ -62,6 +65,7 @@ struct context arc::disassembler disassembler_; arc::compiler compiler_; arc::decompiler decompiler_; + bool fixup_{ false }; fs_callback fs_callback_; std::unordered_map opcode_map_; diff --git a/src/arc/decompiler.cpp b/src/arc/decompiler.cpp index fe8bcc40..7ae0c87d 100644 --- a/src/arc/decompiler.cpp +++ b/src/arc/decompiler.cpp @@ -304,8 +304,23 @@ auto decompiler::decompile_instruction(instruction const& inst, bool last) -> vo } case opcode::OP_EvalLocalVariableCached: { - stack_.push(expr_identifier::make(loc, locals_.at(std::stoi(inst.data[0])))); - break; + if (!ctx_->fixup()) + { + stack_.push(expr_identifier::make(loc, locals_.at(std::stoi(inst.data[0])))); + break; + } + else // fix old compiler bug + { + try + { + stack_.push(expr_identifier::make(loc, locals_.at(std::stoi(inst.data[0])))); + } + catch (const std::exception&) + { + stack_.push(expr_identifier::make(loc, "broken_code!!")); + } + break; + } } case opcode::OP_EvalArray: { @@ -1582,7 +1597,7 @@ auto decompiler::decompile_inf(stmt_list& stm, usize begin, usize end) -> void auto save = locs_; locs_.brk = last_location_index(stm, end) ? locs_.end : stm.list[end + 1]->label(); locs_.end = stm.list[end]->label(); - locs_.cnt = stm.list[end]->label(); + locs_.cnt = stm.list[ctx_->fixup() ? begin : end]->label(); // fix old compiler bug auto loc = stm.list[begin]->loc(); diff --git a/src/arc/disassembler.cpp b/src/arc/disassembler.cpp index ed184294..62c16d74 100644 --- a/src/arc/disassembler.cpp +++ b/src/arc/disassembler.cpp @@ -81,6 +81,13 @@ auto disassembler::disassemble(u8 const* data, usize data_size) -> assembly::ptr header_.flags = script_.read(); auto string_pool = std::map{}; + + // fix old compiler bug + if (ctx_->fixup()) + { + string_pool.insert({ 0x3E, "" }); + } + script_.pos((ctx_->props() & props::headerxx) ? header_size_v3 : (ctx_->props() & props::header72) ? header_size_v2 : header_size_v1); while (script_.pos() < header_.include_offset) @@ -296,6 +303,10 @@ auto disassembler::disassemble(u8 const* data, usize data_size) -> assembly::ptr } } } + else if (ctx_->fixup() && header_.cseg_size == 0) // fix old compiler bug + { + entry->size = (header_.imports_offset) - entry->offset; + } else { entry->size = (header_.cseg_offset + header_.cseg_size) - entry->offset; diff --git a/src/tool/main.cpp b/src/tool/main.cpp index 8f4170b0..0544c279 100644 --- a/src/tool/main.cpp +++ b/src/tool/main.cpp @@ -770,6 +770,7 @@ namespace arc std::map>> contexts; std::map> funcs; +bool t6fixup = false; auto assemble_file(game game, mach mach, fs::path const& file, fs::path rel) -> result { @@ -953,6 +954,7 @@ auto init_t6(mach mach, bool dev) -> void { contexts[game::t6][mach] = std::make_unique(); contexts[game::t6][mach]->init(dev ? build::dev : build::prod, fs_read); + contexts[game::t6][mach]->fixup(t6fixup); break; } case mach::ps3: @@ -1207,6 +1209,7 @@ auto main(u32 argc, char** argv) -> result ("y,dry", "Dry run (do not write files).", cxxopts::value()->implicit_value("true")) ("d,dev", "Enable developer mode (dev blocks & generate bytecode map).", cxxopts::value()->implicit_value("true")) ("z,zonetool", "Enable zonetool mode (use .cgsc files).", cxxopts::value()->implicit_value("true")) + ("t6fixup", "Decompile t6 files from broken compilers", cxxopts::value()->implicit_value("true")) ("h,help", "Display help.") ("v,version", "Display version."); @@ -1262,6 +1265,7 @@ auto main(u32 argc, char** argv) -> result auto mach = mach::_; auto dev = result["dev"].as(); gsc::zonetool = result["zonetool"].as(); + arc::t6fixup = result["t6fixup"].as(); dry_run = result["dry"].as(); if(!parse_mode(mode_arg, mode))