From 954d11c65e16116f1b3c3f2bcd1df75f5e58f96e Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Tue, 24 Sep 2024 14:48:15 +0300 Subject: [PATCH] Handle setjmp/longjmp explicitly. --- PowerRecomp/recompiler.cpp | 25 ++++++++++++++++++++----- PowerRecomp/recompiler.h | 2 ++ PowerRecomp/swa_recompiler.cpp | 3 +++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/PowerRecomp/recompiler.cpp b/PowerRecomp/recompiler.cpp index 6323579..ce93039 100644 --- a/PowerRecomp/recompiler.cpp +++ b/PowerRecomp/recompiler.cpp @@ -35,17 +35,32 @@ bool Recompiler::Recompile(const Function& fn, uint32_t base, const ppc_insn& in { println("\t// {} {}", insn.opcode->name, insn.op_str); + bool printedJmpEnv = false; + auto printFunctionCall = [&](uint32_t ea) { - auto targetSymbol = image.symbols.find(ea); - - if (targetSymbol != image.symbols.end() && targetSymbol->address == ea && targetSymbol->type == Symbol_Function) + if (ea == longJmpAddress) { - println("\t{}(ctx, base);", targetSymbol->name); + println("\tlongjmp(*reinterpret_cast(base + ctx.r3.u32), ctx.r4.s32);"); + } + else if (ea == setJmpAddress) + { + println("\tenv = ctx;"); + println("\tctx.r3.s64 = setjmp(*reinterpret_cast(base + ctx.r3.u32));"); + println("\tif (ctx.r3.s64 != 0) ctx = env;"); } else { - println("\t// ERROR", ea); + auto targetSymbol = image.symbols.find(ea); + + if (targetSymbol != image.symbols.end() && targetSymbol->address == ea && targetSymbol->type == Symbol_Function) + { + println("\t{}(ctx, base);", targetSymbol->name); + } + else + { + println("\t// ERROR", ea); + } } }; diff --git a/PowerRecomp/recompiler.h b/PowerRecomp/recompiler.h index 366fcbe..16d0a50 100644 --- a/PowerRecomp/recompiler.h +++ b/PowerRecomp/recompiler.h @@ -15,6 +15,8 @@ struct Recompiler std::string out; size_t cppFileIndex = 0; std::vector temp; + uint32_t setJmpAddress = 0; + uint32_t longJmpAddress = 0; void LoadSwitchTables(const char* filePath); void LoadExecutable(const char* filePath); diff --git a/PowerRecomp/swa_recompiler.cpp b/PowerRecomp/swa_recompiler.cpp index a4a5a9f..bf83f0a 100644 --- a/PowerRecomp/swa_recompiler.cpp +++ b/PowerRecomp/swa_recompiler.cpp @@ -191,4 +191,7 @@ void SWARecompiler::Analyse() } std::sort(functions.begin(), functions.end(), [](auto& lhs, auto& rhs) { return lhs.base < rhs.base; }); + + setJmpAddress = 0x831B6AB0; + longJmpAddress = 0x831B6790; }