From 8c0af4e2248597830e2ff575172554b4553ae79b Mon Sep 17 00:00:00 2001 From: Ran-j Date: Mon, 14 Apr 2025 01:41:22 -0300 Subject: [PATCH] feat: added header for link functions --- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 1 + ps2xRecomp/src/code_generator.cpp | 3 +- ps2xRecomp/src/ps2_recompiler.cpp | 50 +++++++++++++++---- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index bc98127..d021399 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -46,6 +46,7 @@ namespace ps2recomp bool shouldSkipFunction(const std::string &name) const; std::string generateRuntimeHeader(); std::string generateStubFunction(const Function& function); + bool generateFunctionHeader(); bool writeToFile(const std::string &path, const std::string &content); std::filesystem::path getOutputPath(const Function &function) const; }; diff --git a/ps2xRecomp/src/code_generator.cpp b/ps2xRecomp/src/code_generator.cpp index 5a119bc..3b623d8 100644 --- a/ps2xRecomp/src/code_generator.cpp +++ b/ps2xRecomp/src/code_generator.cpp @@ -396,7 +396,8 @@ namespace ps2recomp std::stringstream ss; ss << "#include \"ps2_runtime_macros.h\"\n"; - ss << "#include \"ps2_runtime.h\"\n\n"; + ss << "#include \"ps2_runtime.h\"\n"; + ss << "#include \"ps2_recompiled_functions.h\"\n\n"; ss << "// Function: " << function.name << "\n"; ss << "// Address: 0x" << std::hex << function.start << " - 0x" << function.end << std::dec << "\n"; diff --git a/ps2xRecomp/src/ps2_recompiler.cpp b/ps2xRecomp/src/ps2_recompiler.cpp index f7f0815..10909d0 100644 --- a/ps2xRecomp/src/ps2_recompiler.cpp +++ b/ps2xRecomp/src/ps2_recompiler.cpp @@ -72,20 +72,14 @@ namespace ps2recomp size_t processedCount = 0; for (auto &function : m_functions) { + std::cout << "processing function: " << function.name << std::endl; + if (shouldSkipFunction(function.name)) { std::cout << "Skipping function: " << function.name << std::endl; continue; } - if (shouldStubFunction(function.name)) - { - std::cout << "Stubbing function: " << function.name << std::endl; - function.isStub = true; - // TODO: Generate stub implementation - continue; - } - if (shouldStubFunction(function.name)) { std::cout << "Stubbing function: " << function.name << std::endl; @@ -129,12 +123,15 @@ namespace ps2recomp { try { + generateFunctionHeader(); + if (m_config.singleFileOutput) { std::stringstream combinedOutput; combinedOutput << "#include \"ps2_runtime_macros.h\"\n"; - combinedOutput << "#include \"ps2_runtime.h\"\n\n"; + combinedOutput << "#include \"ps2_runtime.h\"\n"; + combinedOutput << "#include \"ps2_recompiled_functions.h\"\n\n"; for (const auto &function : m_functions) { @@ -193,6 +190,41 @@ namespace ps2recomp } } + bool PS2Recompiler::generateFunctionHeader() + { + try + { + std::stringstream ss; + + ss << "#ifndef PS2_RECOMPILED_FUNCTIONS_H\n"; + ss << "#define PS2_RECOMPILED_FUNCTIONS_H\n\n"; + + ss << "#include \n\n"; + ss << "struct R5900Context;\n\n"; + + for (const auto &function : m_functions) + { + if (function.isRecompiled) + { + ss << "void " << function.name << "(uint8_t* rdram, R5900Context* ctx);\n"; + } + } + + ss << "\n#endif // PS2_RECOMPILED_FUNCTIONS_H\n"; + + fs::path headerPath = fs::path(m_config.outputPath) / "ps2_recompiled_functions.h"; + writeToFile(headerPath.string(), ss.str()); + + std::cout << "Generated function header file: " << headerPath << std::endl; + return true; + } + catch (const std::exception &e) + { + std::cerr << "Error generating function header: " << e.what() << std::endl; + return false; + } + } + bool PS2Recompiler::decodeFunction(Function &function) { std::vector instructions;