Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(t6): merge old compiler fixes as flag #235

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/xsk/arc/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, std::string_view> opcode_map_;
Expand Down
21 changes: 18 additions & 3 deletions src/arc/decompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down Expand Up @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions src/arc/disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ auto disassembler::disassemble(u8 const* data, usize data_size) -> assembly::ptr
header_.flags = script_.read<u8>();

auto string_pool = std::map<usize, std::string>{};

// 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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ namespace arc

std::map<game, std::map<mach, std::unique_ptr<context>>> contexts;
std::map<mode, std::function<result(game game, mach mach, fs::path const& file, fs::path rel)>> funcs;
bool t6fixup = false;

auto assemble_file(game game, mach mach, fs::path const& file, fs::path rel) -> result
{
Expand Down Expand Up @@ -953,6 +954,7 @@ auto init_t6(mach mach, bool dev) -> void
{
contexts[game::t6][mach] = std::make_unique<t6::pc::context>();
contexts[game::t6][mach]->init(dev ? build::dev : build::prod, fs_read);
contexts[game::t6][mach]->fixup(t6fixup);
break;
}
case mach::ps3:
Expand Down Expand Up @@ -1207,6 +1209,7 @@ auto main(u32 argc, char** argv) -> result
("y,dry", "Dry run (do not write files).", cxxopts::value<bool>()->implicit_value("true"))
("d,dev", "Enable developer mode (dev blocks & generate bytecode map).", cxxopts::value<bool>()->implicit_value("true"))
("z,zonetool", "Enable zonetool mode (use .cgsc files).", cxxopts::value<bool>()->implicit_value("true"))
("t6fixup", "Decompile t6 files from broken compilers", cxxopts::value<bool>()->implicit_value("true"))
("h,help", "Display help.")
("v,version", "Display version.");

Expand Down Expand Up @@ -1262,6 +1265,7 @@ auto main(u32 argc, char** argv) -> result
auto mach = mach::_;
auto dev = result["dev"].as<bool>();
gsc::zonetool = result["zonetool"].as<bool>();
arc::t6fixup = result["t6fixup"].as<bool>();
dry_run = result["dry"].as<bool>();

if(!parse_mode(mode_arg, mode))
Expand Down