Skip to content
Closed
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
34 changes: 13 additions & 21 deletions ps2xRecomp/src/lib/code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ namespace ps2recomp

if (!funcName.empty())
{
ss << fmt::format(" if (runtime->hasFunction(0x{:X}u)) {{\n", target);
// Always use runtime dispatch (lookupFunction). The else branch
// that direct-called by symbol is dead code: all functions are
// registered before any execute. Removing it eliminates the
// #include "ps2_recompiled_functions.h" dependency from each
// .cpp file, dramatically improving ccache hit rates.
ss << " {\n";
ss << fmt::format(" auto targetFn = runtime->lookupFunction(0x{:X}u);\n", target);
if (branchInst.opcode == OPCODE_J)
{
Expand All @@ -326,20 +331,7 @@ namespace ps2recomp
ss << " const uint32_t __entryPc = ctx->pc;\n";
ss << " targetFn(rdram, ctx, runtime);\n";
ss << fmt::format(" if (ctx->pc == __entryPc) {{ ctx->pc = 0x{:X}u; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ return; }}\n", fallthroughPc);
}
ss << " } else {\n";

if (branchInst.opcode == OPCODE_J)
{
ss << " " << funcName << "(rdram, ctx, runtime); return;\n";
}
else
{
ss << " const uint32_t __entryPc = ctx->pc;\n";
ss << " " << funcName << "(rdram, ctx, runtime);\n";
ss << fmt::format(" if (ctx->pc == __entryPc) {{ ctx->pc = 0x{:X}u; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ return; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ fprintf(stderr, \"[PC_MISMATCH] at 0x{:X}: called 0x%x, expected ret 0x{:X}, got 0x%x\\n\", __entryPc, ctx->pc); return; }}\n", fallthroughPc, branchInst.address, fallthroughPc);
}
ss << " }\n";
}
Expand Down Expand Up @@ -372,7 +364,7 @@ namespace ps2recomp
}
else
{
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ return; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ fprintf(stderr, \"[PC_MISMATCH] at 0x{:X}: called reloc, expected ret 0x{:X}, got 0x%x\\n\", ctx->pc); return; }}\n", fallthroughPc, branchInst.address, fallthroughPc);
}
emittedRelocCall = true;
}
Expand All @@ -391,7 +383,7 @@ namespace ps2recomp
else
{
ss << fmt::format(" if (ctx->pc == __entryPc) {{ ctx->pc = 0x{:X}u; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ return; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ fprintf(stderr, \"[PC_MISMATCH] at 0x{:X}: called 0x{:X}, expected ret 0x{:X}, got 0x%x\\n\", ctx->pc); return; }}\n", fallthroughPc, branchInst.address, target, fallthroughPc);
}
ss << " }\n";
}
Expand Down Expand Up @@ -449,7 +441,7 @@ namespace ps2recomp
ss << " const uint32_t __entryPc = ctx->pc;\n";
ss << " targetFn(rdram, ctx, runtime);\n";
ss << fmt::format(" if (ctx->pc == __entryPc) {{ ctx->pc = 0x{:X}u; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ return; }}\n", fallthroughPc);
ss << fmt::format(" if (ctx->pc != 0x{:X}u) {{ fprintf(stderr, \"[PC_MISMATCH] at 0x{:X}: jalr 0x%x, expected ret 0x{:X}, got 0x%x\\n\", __entryPc, ctx->pc); return; }}\n", fallthroughPc, branchInst.address, fallthroughPc);
ss << " }\n";
}

Expand Down Expand Up @@ -899,7 +891,6 @@ namespace ps2recomp
{
ss << "#include \"ps2_runtime_macros.h\"\n";
ss << "#include \"ps2_runtime.h\"\n";
ss << "#include \"ps2_recompiled_functions.h\"\n";
ss << "#include \"ps2_recompiled_stubs.h\"\n\n";
ss << "#include \"ps2_syscalls.h\"\n";
ss << "#include \"ps2_stubs.h\"\n\n";
Expand Down Expand Up @@ -3717,10 +3708,11 @@ namespace ps2recomp
ss << "#include \"ps2_runtime.h\"\n";
ss << "#include \"ps2_recompiled_functions.h\"\n";
ss << "#include \"ps2_stubs.h\"\n";
ss << "#include \"ps2_recompiled_stubs.h\"//this will give duplicated erros because runtime maybe has it define already, just delete the TODOS ones\n";
ss << "#include \"ps2_recompiled_stubs.h\"\n";
ss << "#include \"ps2_syscalls.h\"\n\n";

// Registration function
// Registration function — this file needs ps2_recompiled_functions.h
// for forward declarations of all function symbols
ss << "void registerAllFunctions(PS2Runtime& runtime) {\n";

std::vector<std::pair<uint32_t, std::string>> normalFunctions;
Expand Down