From 4ea08199b07cc1a93687f22d0d246dd1cde83203 Mon Sep 17 00:00:00 2001 From: Ran-j Date: Sat, 29 Nov 2025 12:26:39 -0300 Subject: [PATCH] feat: now we generate header file for stub functions --- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 1 + ps2xRecomp/include/ps2recomp/types.h | 2 +- ps2xRecomp/src/code_generator.cpp | 3 +- ps2xRecomp/src/config_manager.cpp | 20 ++-------- ps2xRecomp/src/ps2_recompiler.cpp | 37 +++++++++++++++++++ 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index 95bb39d..905304b 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -44,6 +44,7 @@ namespace ps2recomp bool shouldSkipFunction(const std::string &name) const; std::string generateRuntimeHeader(); bool generateFunctionHeader(); + bool generateStubHeader(); bool writeToFile(const std::string &path, const std::string &content); std::filesystem::path getOutputPath(const Function &function) const; }; diff --git a/ps2xRecomp/include/ps2recomp/types.h b/ps2xRecomp/include/ps2recomp/types.h index 402396e..0e5527b 100644 --- a/ps2xRecomp/include/ps2recomp/types.h +++ b/ps2xRecomp/include/ps2recomp/types.h @@ -170,7 +170,7 @@ namespace ps2recomp bool singleFileOutput; std::vector skipFunctions; std::unordered_map patches; - std::map stubImplementations; + std::vector stubImplementations; }; } // namespace ps2recomp diff --git a/ps2xRecomp/src/code_generator.cpp b/ps2xRecomp/src/code_generator.cpp index e6a04d7..05a4c20 100644 --- a/ps2xRecomp/src/code_generator.cpp +++ b/ps2xRecomp/src/code_generator.cpp @@ -479,7 +479,8 @@ namespace ps2recomp { ss << "#include \"ps2_runtime_macros.h\"\n"; ss << "#include \"ps2_runtime.h\"\n"; - ss << "#include \"ps2_recompiled_functions.h\"\n\n"; + ss << "#include \"ps2_recompiled_functions.h\"\n"; + ss << "#include \"ps2_recompiled_stubs.h\"\n\n"; } ss << "// Function: " << function.name << "\n"; diff --git a/ps2xRecomp/src/config_manager.cpp b/ps2xRecomp/src/config_manager.cpp index 9c50c94..d61f4dc 100644 --- a/ps2xRecomp/src/config_manager.cpp +++ b/ps2xRecomp/src/config_manager.cpp @@ -20,11 +20,13 @@ namespace ps2recomp try { + std::cout << "Parsing toml file: " << m_configPath << std::endl; auto data = toml::parse(m_configPath); config.inputPath = toml::find(data, "general", "input"); config.outputPath = toml::find(data, "general", "output"); config.singleFileOutput = toml::find(data, "general", "single_file_output"); + config.stubImplementations = toml::find>(data, "general", "stubs"); config.skipFunctions = toml::find>(data, "general", "skip"); @@ -46,17 +48,6 @@ namespace ps2recomp } } } - - if (data.contains("stubs") && data.at("stubs").is_table()) - { - const auto &stubImpls = toml::find(data, "stubs"); - for (const auto &item : stubImpls.as_table()) - { - const std::string &funcName = item.first; - const std::string &implementation = toml::find(stubImpls, funcName); - config.stubImplementations[funcName] = implementation; - } - } } catch (const std::exception &e) { @@ -98,12 +89,7 @@ namespace ps2recomp if (!config.stubImplementations.empty()) { - toml::table stubImpls; - for (const auto &impl : config.stubImplementations) - { - stubImpls[impl.first] = impl.second; - } - data["stubs"] = stubImpls; + data["stubs"] = config.stubImplementations; } std::ofstream file(m_configPath); diff --git a/ps2xRecomp/src/ps2_recompiler.cpp b/ps2xRecomp/src/ps2_recompiler.cpp index 0cfd1ed..a527bbf 100644 --- a/ps2xRecomp/src/ps2_recompiler.cpp +++ b/ps2xRecomp/src/ps2_recompiler.cpp @@ -119,6 +119,7 @@ namespace ps2recomp combinedOutput << "#include \"ps2_recompiled_functions.h\"\n\n"; combinedOutput << "#include \"ps2_runtime_macros.h\"\n"; combinedOutput << "#include \"ps2_runtime.h\"\n"; + combinedOutput << "#include \"ps2_recompiled_stubs.h\"\n"; for (const auto &function : m_functions) { @@ -199,6 +200,8 @@ namespace ps2recomp fs::path registerPath = fs::path(m_config.outputPath) / "register_functions.cpp"; writeToFile(registerPath.string(), registerFunctions); std::cout << "Generated function registration file: " << registerPath << std::endl; + + generateStubHeader(); } catch (const std::exception &e) { @@ -206,6 +209,40 @@ namespace ps2recomp } } + bool PS2Recompiler::generateStubHeader() + { + try + { + std::stringstream ss; + + ss << "#pragma once\n\n"; + ss << "#include \n"; + ss << "#include \"ps2_runtime.h\"\n"; + ss << "#include \"ps2_syscalls.h\"\n\n"; + ss << "namespace ps2recomp {\n"; + ss << "namespace stubs {\n\n"; + + for (const auto &funcName : m_config.stubImplementations) + { + ss << "void " << funcName << "(uint8_t* rdram, R5900Context* ctx, PS2Runtime* runtime) { ps2_syscalls::TODO(rdram, ctx, runtime); }\n"; + } + + ss << "\n} // namespace stubs\n"; + ss << "} // namespace ps2recomp\n"; + + fs::path headerPath = fs::path(m_config.outputPath) / "ps2_recompiled_stubs.h"; + writeToFile(headerPath.string(), ss.str()); + + std::cout << "Generated generating header file: " << headerPath << std::endl; + return true; + } + catch (const std::exception &e) + { + std::cerr << "Error generating stub header: " << e.what() << std::endl; + return false; + } + } + bool PS2Recompiler::generateFunctionHeader() { try