From 27fe7f22cf2ca40f95b92993fa3e69645c75d922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 15:55:34 +0300 Subject: [PATCH 01/11] refactor: update function name checks to use contains method --- ps2xAnalyzer/src/elf_analyzer.cpp | 66 +++++++++++++++---------------- ps2xRecomp/src/code_generator.cpp | 10 ++--- ps2xRecomp/src/ps2_recompiler.cpp | 8 ++-- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 6552b66..55fb5b7 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -61,8 +61,8 @@ namespace ps2recomp for (auto &func : m_functions) { - if (m_skipFunctions.find(func.name) == m_skipFunctions.end() && - m_libFunctions.find(func.name) == m_libFunctions.end()) + if (!m_skipFunctions.contains(func.name) && + !m_libFunctions.contains(func.name)) { categorizeFunction(func); func.instructions = decodeFunction(func); @@ -317,8 +317,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -460,8 +460,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -498,7 +498,7 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end()) + if (m_skipFunctions.contains(func.name)) { continue; } @@ -605,7 +605,7 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end()) + if (m_skipFunctions.contains(func.name)) { continue; } @@ -632,8 +632,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -687,8 +687,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -785,8 +785,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -845,7 +845,7 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_functionCalls.find(func.start) != m_functionCalls.end()) + if (m_functionCalls.contains(func.start)) { for (const auto &call : m_functionCalls[func.start]) { @@ -856,7 +856,7 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (callGraph[func.name].find(func.name) != callGraph[func.name].end()) + if (callGraph[func.name].contains(func.name)) { std::cout << "Function " << func.name << " is directly recursive" << std::endl; } @@ -864,8 +864,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -875,7 +875,7 @@ namespace ps2recomp detectCycle = [&](const std::string &currFunc) -> bool { - if (visited.find(currFunc) != visited.end()) + if (visited.contains(currFunc)) { return currFunc == func.name; } @@ -907,8 +907,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -987,13 +987,13 @@ namespace ps2recomp std::cout << " - Saves return address ($ra)" << std::endl; } - if (regsRead.find(4) != regsRead.end() || regsRead.find(5) != regsRead.end() || - regsRead.find(6) != regsRead.end() || regsRead.find(7) != regsRead.end()) + if (regsRead.contains(4) || regsRead.contains(5) || + regsRead.contains(6) || regsRead.contains(7)) { std::cout << " - Uses argument registers (a0-a3)" << std::endl; } - if (regsWritten.find(2) != regsWritten.end() || regsWritten.find(3) != regsWritten.end()) + if (regsWritten.contains(2) || regsWritten.contains(3)) { std::cout << " - Sets return values (v0-v1)" << std::endl; } @@ -1006,8 +1006,8 @@ namespace ps2recomp for (const auto &func : m_functions) { - if (m_skipFunctions.find(func.name) != m_skipFunctions.end() || - m_libFunctions.find(func.name) != m_libFunctions.end()) + if (m_skipFunctions.contains(func.name) || + m_libFunctions.contains(func.name)) { continue; } @@ -1371,7 +1371,7 @@ namespace ps2recomp { const auto &inst = instructions[i]; - if (leaders.find(inst.address) != leaders.end()) + if (leaders.contains(inst.address)) { if (currentLeader != 0) { @@ -1404,7 +1404,7 @@ namespace ps2recomp int32_t offset = static_cast(lastInst.immediate) << 2; uint32_t targetAddr = lastInst.address + 4 + offset; - if (cfg.find(targetAddr) != cfg.end()) + if (cfg.contains(targetAddr)) { node.successors.push_back(targetAddr); cfg[targetAddr].predecessors.push_back(addr); @@ -1443,7 +1443,7 @@ namespace ps2recomp // Only add successor if it's within this function if (targetAddr >= function.start && targetAddr < function.end && - cfg.find(targetAddr) != cfg.end()) + cfg.contains(targetAddr)) { node.successors.push_back(targetAddr); cfg[targetAddr].predecessors.push_back(addr); @@ -1527,7 +1527,7 @@ namespace ps2recomp "topThread", "cmd_sem_init"}; - return kDoNotSkipOrStub.find(name) != kDoNotSkipOrStub.end(); + return kDoNotSkipOrStub.contains(name); } bool ElfAnalyzer::isSystemFunction(const std::string &name) const @@ -1543,7 +1543,7 @@ namespace ps2recomp "_ftext", "__bss_start", "__bss_start__", "__bss_end__", "__end__", "_stack", "_dso_handle"}; - return systemFuncs.find(name) != systemFuncs.end() || + return systemFuncs.contains(name) || name.find("__") == 0 || name.find("_Z") == 0 || // C++ mangled names name.find(".") == 0; // .text.* or .plt.* symbols @@ -1634,8 +1634,8 @@ namespace ps2recomp bool ElfAnalyzer::identifyFunctionType(const Function &function) { - if (m_libFunctions.find(function.name) != m_libFunctions.end() || - m_skipFunctions.find(function.name) != m_skipFunctions.end()) + if (m_libFunctions.contains(function.name) || + m_skipFunctions.contains(function.name)) { return false; } diff --git a/ps2xRecomp/src/code_generator.cpp b/ps2xRecomp/src/code_generator.cpp index 111e3e5..f2ee2b5 100644 --- a/ps2xRecomp/src/code_generator.cpp +++ b/ps2xRecomp/src/code_generator.cpp @@ -72,7 +72,7 @@ namespace ps2recomp static bool isReservedCxxKeyword(const std::string &name) { - return kKeywords.find(name) != kKeywords.end(); + return kKeywords.contains(name); } static std::string sanitizeFunctionName(const std::string& name) @@ -255,7 +255,7 @@ namespace ps2recomp std::string targetAction; std::string funcName = getFunctionName(target); - bool isInternalTarget = (internalTargets.find(target) != internalTargets.end()); + bool isInternalTarget = internalTargets.contains(target); if (!funcName.empty()) { @@ -640,7 +640,7 @@ namespace ps2recomp "SetOsdConfigParam", "GetRomName", "sceSifLoadModule", "SifSetDChain"}; - if (systemCallNames.find(function.name) != systemCallNames.end()) + if (systemCallNames.contains(function.name)) { std::string sanitizedName = sanitizeFunctionName(function.name); ss << "// System call wrapper for " << function.name << "\n"; @@ -669,7 +669,7 @@ namespace ps2recomp { const Instruction &inst = instructions[i]; - if (internalTargets.find(inst.address) != internalTargets.end()) + if (internalTargets.contains(inst.address)) { ss << "label_" << std::hex << inst.address << std::dec << ":\n"; } @@ -682,7 +682,7 @@ namespace ps2recomp { const Instruction &delaySlot = instructions[i + 1]; - if (internalTargets.find(delaySlot.address) != internalTargets.end()) + if (internalTargets.contains(delaySlot.address)) { ss << "label_" << std::hex << delaySlot.address << std::dec << ":\n"; } diff --git a/ps2xRecomp/src/ps2_recompiler.cpp b/ps2xRecomp/src/ps2_recompiler.cpp index 6c8b354..df78859 100644 --- a/ps2xRecomp/src/ps2_recompiler.cpp +++ b/ps2xRecomp/src/ps2_recompiler.cpp @@ -558,7 +558,7 @@ namespace ps2recomp continue; } - if (existingStarts.find(target) != existingStarts.end()) + if (existingStarts.contains(target)) { continue; } @@ -660,12 +660,12 @@ namespace ps2recomp bool PS2Recompiler::shouldSkipFunction(const std::string &name) const { - return m_skipFunctions.find(name) != m_skipFunctions.end(); + return m_skipFunctions.contains(name); } bool PS2Recompiler::isStubFunction(const std::string &name) const { - if (m_stubFunctions.find(name) != m_stubFunctions.end()) + if (m_stubFunctions.contains(name)) { return true; } @@ -724,7 +724,7 @@ namespace ps2recomp return "ps2_main"; } - if (ps2recomp::kKeywords.find(sanitized) != ps2recomp::kKeywords.end()) + if (ps2recomp::kKeywords.contains(sanitized)) { return "ps2_" + sanitized; } From 685d90ca701e12d7ca93e7ec44e40f6bf9f5c57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 16:07:09 +0300 Subject: [PATCH 02/11] refactor: update code generation to use static_cast --- ps2xAnalyzer/src/elf_analyzer.cpp | 2 +- ps2xRecomp/src/code_generator.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 55fb5b7..6919d61 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -953,7 +953,7 @@ namespace ps2recomp bool savesFP = false; bool savesRA = false; - for (size_t i = 0; i < std::min(size_t(10), instructions.size()); i++) + for (size_t i = 0; i < std::min(static_cast(10), instructions.size()); i++) { const auto &inst = instructions[i]; diff --git a/ps2xRecomp/src/code_generator.cpp b/ps2xRecomp/src/code_generator.cpp index f2ee2b5..95d7e7b 100644 --- a/ps2xRecomp/src/code_generator.cpp +++ b/ps2xRecomp/src/code_generator.cpp @@ -147,14 +147,14 @@ namespace ps2recomp uint8_t link_reg = (branchInst.function == SPECIAL_JALR) ? ((rd_reg == 0) ? 31 : rd_reg) : 0; if (link_reg != 0) { - ss << " SET_GPR_U32(ctx, " << (int)link_reg << ", 0x" << std::hex << (branchInst.address + 8) << ");\n" + ss << " SET_GPR_U32(ctx, " << static_cast(link_reg) << ", 0x" << std::hex << (branchInst.address + 8) << ");\n" << std::dec; } if (hasValidDelaySlot) { ss << " " << delaySlotCode << "\n"; } - ss << " ctx->pc = GPR_U32(ctx, " << (int)rs_reg << "); return;\n"; + ss << " ctx->pc = GPR_U32(ctx, " << static_cast(rs_reg) << "); return;\n"; } else if (branchInst.isBranch) { @@ -2234,7 +2234,7 @@ namespace ps2recomp // VCALLMS calls a VU0 microprogram at the specified immediate address. // VU0 micro memory is 4KB = 512 instructions (8 bytes each). Index is 0-511. uint16_t instr_index = inst.immediate & 0x1FF; // Mask to 9 bits for VU0 - uint32_t target_byte_addr = (uint32_t)instr_index << 3; // Convert instruction index to byte address + uint32_t target_byte_addr = static_cast(instr_index) << 3; // Convert instruction index to byte address return fmt::format( "{{ " From d79269d9662d70e2ff95d84d75b6b548f68c9e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 17:13:24 +0300 Subject: [PATCH 03/11] refactor: made member methods const --- ps2xAnalyzer/include/ps2recomp/elf_analyzer.h | 26 ++++++------- ps2xAnalyzer/src/elf_analyzer.cpp | 39 +++++++------------ ps2xRecomp/include/ps2recomp/config_manager.h | 4 +- ps2xRecomp/include/ps2recomp/elf_parser.h | 8 ++-- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 2 +- ps2xRecomp/include/ps2recomp/r5900_decoder.h | 2 +- ps2xRecomp/src/config_manager.cpp | 6 +-- ps2xRecomp/src/elf_parser.cpp | 12 ++---- ps2xRecomp/src/ps2_recompiler.cpp | 3 +- ps2xRecomp/src/r5900_decoder.cpp | 3 +- 10 files changed, 42 insertions(+), 63 deletions(-) diff --git a/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h b/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h index ab3c02b..8e438b6 100644 --- a/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h +++ b/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h @@ -52,30 +52,30 @@ namespace ps2recomp void identifyPotentialPatches(); void analyzeControlFlow(); void detectJumpTables(); - void analyzePerformanceCriticalPaths(); + void analyzePerformanceCriticalPaths() const; void identifyRecursiveFunctions(); - void analyzeRegisterUsage(); - void analyzeFunctionSignatures(); + void analyzeRegisterUsage() const; + void analyzeFunctionSignatures() const; void optimizePatches(); - bool identifyMemcpyPattern(const Function &func); - bool identifyMemsetPattern(const Function &func); - bool identifyStringOperationPattern(const Function &func); - bool identifyMathPattern(const Function &func); + bool identifyMemcpyPattern(const Function &func) const; + bool identifyMemsetPattern(const Function &func) const; + bool identifyStringOperationPattern(const Function &func) const; + bool identifyMathPattern(const Function &func) const; bool isSystemFunction(const std::string &name) const; bool isLibraryFunction(const std::string &name) const; - std::vector decodeFunction(const Function &function); - CFG buildCFG(const Function &function); + std::vector decodeFunction(const Function &function) const; + CFG buildCFG(const Function &function) const; std::string formatAddress(uint32_t address) const; std::string escapeBackslashes(const std::string &path); - bool hasMMIInstructions(const Function &function); - bool hasVUInstructions(const Function &function); + bool hasMMIInstructions(const Function &function) const; + bool hasVUInstructions(const Function &function) const; bool identifyFunctionType(const Function &function); void categorizeFunction(Function &function); uint32_t getSuccessor(const Instruction &inst, uint32_t currentAddr); - bool isSelfModifyingCode(const Function &function); - bool isLoopHeavyFunction(const Function &function); + bool isSelfModifyingCode(const Function &function) const; + bool isLoopHeavyFunction(const Function &function) const; }; } diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 6919d61..8b7f596 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -779,8 +779,7 @@ namespace ps2recomp } } - void ElfAnalyzer::analyzePerformanceCriticalPaths() - { + void ElfAnalyzer::analyzePerformanceCriticalPaths() const { std::cout << "Analyzing performance-critical paths..." << std::endl; for (const auto &func : m_functions) @@ -901,8 +900,7 @@ namespace ps2recomp } } - void ElfAnalyzer::analyzeRegisterUsage() - { + void ElfAnalyzer::analyzeRegisterUsage() const { std::cout << "Analyzing register usage patterns..." << std::endl; for (const auto &func : m_functions) @@ -1000,8 +998,7 @@ namespace ps2recomp } } - void ElfAnalyzer::analyzeFunctionSignatures() - { + void ElfAnalyzer::analyzeFunctionSignatures() const { std::cout << "Analyzing function signatures..." << std::endl; for (const auto &func : m_functions) @@ -1160,8 +1157,7 @@ namespace ps2recomp } } - bool ElfAnalyzer::identifyMemcpyPattern(const Function &func) - { + bool ElfAnalyzer::identifyMemcpyPattern(const Function &func) const { std::vector instructions = decodeFunction(func); bool hasLoop = false; @@ -1206,8 +1202,7 @@ namespace ps2recomp return hasLoop && loadsData && storesData && incrementsPointers; } - bool ElfAnalyzer::identifyMemsetPattern(const Function &func) - { + bool ElfAnalyzer::identifyMemsetPattern(const Function &func) const { std::vector instructions = decodeFunction(func); bool hasLoop = false; @@ -1251,8 +1246,7 @@ namespace ps2recomp return hasLoop && usesConstant && storesData && incrementsPointer; } - bool ElfAnalyzer::identifyStringOperationPattern(const Function &func) - { + bool ElfAnalyzer::identifyStringOperationPattern(const Function &func) const { std::vector instructions = decodeFunction(func); bool hasLoop = false; @@ -1293,8 +1287,7 @@ namespace ps2recomp return hasLoop && checksZero && (loadsByte || storesByte); } - bool ElfAnalyzer::identifyMathPattern(const Function &func) - { + bool ElfAnalyzer::identifyMathPattern(const Function &func) const { std::vector instructions = decodeFunction(func); int mathOps = 0; @@ -1324,8 +1317,7 @@ namespace ps2recomp return mathOps > instructions.size() * 0.3 || usesFPU; } - CFG ElfAnalyzer::buildCFG(const Function &function) - { + CFG ElfAnalyzer::buildCFG(const Function &function) const { CFG cfg; std::vector instructions = decodeFunction(function); std::map addrToIndex; @@ -1567,8 +1559,7 @@ namespace ps2recomp return false; } - std::vector ElfAnalyzer::decodeFunction(const Function &function) - { + std::vector ElfAnalyzer::decodeFunction(const Function &function) const { std::vector instructions; for (uint32_t addr = function.start; addr < function.end; addr += 4) @@ -1602,8 +1593,7 @@ namespace ps2recomp return ss.str(); } - bool ElfAnalyzer::hasMMIInstructions(const Function &function) - { + bool ElfAnalyzer::hasMMIInstructions(const Function &function) const { std::vector instructions = decodeFunction(function); for (const auto &inst : instructions) @@ -1617,8 +1607,7 @@ namespace ps2recomp return false; } - bool ElfAnalyzer::hasVUInstructions(const Function &function) - { + bool ElfAnalyzer::hasVUInstructions(const Function &function) const { std::vector instructions = decodeFunction(function); for (const auto &inst : instructions) @@ -1714,8 +1703,7 @@ namespace ps2recomp } } - bool ElfAnalyzer::isSelfModifyingCode(const Function &function) - { + bool ElfAnalyzer::isSelfModifyingCode(const Function &function) const { std::vector instructions = decodeFunction(function); for (size_t i = 0; i < instructions.size(); i++) @@ -1760,8 +1748,7 @@ namespace ps2recomp return false; } - bool ElfAnalyzer::isLoopHeavyFunction(const Function &function) - { + bool ElfAnalyzer::isLoopHeavyFunction(const Function &function) const { std::vector instructions = decodeFunction(function); int loopCount = 0; diff --git a/ps2xRecomp/include/ps2recomp/config_manager.h b/ps2xRecomp/include/ps2recomp/config_manager.h index 9a53d38..2cab24e 100644 --- a/ps2xRecomp/include/ps2recomp/config_manager.h +++ b/ps2xRecomp/include/ps2recomp/config_manager.h @@ -13,8 +13,8 @@ namespace ps2recomp ConfigManager(const std::string &configPath); ~ConfigManager(); - RecompilerConfig loadConfig(); - void saveConfig(const RecompilerConfig &config); + RecompilerConfig loadConfig() const; + void saveConfig(const RecompilerConfig &config) const; private: std::string m_configPath; diff --git a/ps2xRecomp/include/ps2recomp/elf_parser.h b/ps2xRecomp/include/ps2recomp/elf_parser.h index a236511..c0e5528 100644 --- a/ps2xRecomp/include/ps2recomp/elf_parser.h +++ b/ps2xRecomp/include/ps2recomp/elf_parser.h @@ -18,7 +18,7 @@ namespace ps2recomp bool parse(); - std::vector extractFunctions(); + std::vector extractFunctions() const; std::vector extractSymbols(); std::vector
getSections(); std::vector getRelocations(); @@ -26,9 +26,9 @@ namespace ps2recomp // Helper methods bool isValidAddress(uint32_t address) const; uint32_t readWord(uint32_t address) const; - uint8_t *getSectionData(const std::string §ionName); - uint32_t getSectionAddress(const std::string §ionName); - uint32_t getSectionSize(const std::string §ionName); + uint8_t *getSectionData(const std::string §ionName) const; + uint32_t getSectionAddress(const std::string §ionName) const; + uint32_t getSectionSize(const std::string §ionName) const; uint32_t getEntryPoint() const; private: diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index 049e241..026f314 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -48,7 +48,7 @@ namespace ps2recomp void discoverAdditionalEntryPoints(); bool shouldSkipFunction(const std::string &name) const; bool isStubFunction(const std::string &name) const; - std::string generateRuntimeHeader(); + std::string generateRuntimeHeader() const; bool generateFunctionHeader(); bool generateStubHeader(); bool writeToFile(const std::string &path, const std::string &content); diff --git a/ps2xRecomp/include/ps2recomp/r5900_decoder.h b/ps2xRecomp/include/ps2recomp/r5900_decoder.h index 80f0fa7..1b3d178 100644 --- a/ps2xRecomp/include/ps2recomp/r5900_decoder.h +++ b/ps2xRecomp/include/ps2recomp/r5900_decoder.h @@ -14,7 +14,7 @@ namespace ps2recomp R5900Decoder(); ~R5900Decoder(); - Instruction decodeInstruction(uint32_t address, uint32_t rawInstruction); + Instruction decodeInstruction(uint32_t address, uint32_t rawInstruction) const; bool isBranchInstruction(const Instruction &inst) const; bool isJumpInstruction(const Instruction &inst) const; diff --git a/ps2xRecomp/src/config_manager.cpp b/ps2xRecomp/src/config_manager.cpp index d61f4dc..3053589 100644 --- a/ps2xRecomp/src/config_manager.cpp +++ b/ps2xRecomp/src/config_manager.cpp @@ -14,8 +14,7 @@ namespace ps2recomp ConfigManager::~ConfigManager() = default; - RecompilerConfig ConfigManager::loadConfig() - { + RecompilerConfig ConfigManager::loadConfig() const { RecompilerConfig config; try @@ -58,8 +57,7 @@ namespace ps2recomp return config; } - void ConfigManager::saveConfig(const RecompilerConfig &config) - { + void ConfigManager::saveConfig(const RecompilerConfig &config) const { toml::value data; toml::table general; diff --git a/ps2xRecomp/src/elf_parser.cpp b/ps2xRecomp/src/elf_parser.cpp index 9722621..a10a429 100644 --- a/ps2xRecomp/src/elf_parser.cpp +++ b/ps2xRecomp/src/elf_parser.cpp @@ -21,8 +21,7 @@ namespace ps2recomp !(section->get_flags() & ELFIO::SHF_EXECINSTR); } - std::vector ElfParser::extractFunctions() - { + std::vector ElfParser::extractFunctions() const { std::vector functions; for (const auto &symbol : m_symbols) @@ -92,8 +91,7 @@ namespace ps2recomp throw std::runtime_error("Invalid address for readWord: " + std::to_string(address)); } - uint8_t *ElfParser::getSectionData(const std::string §ionName) - { + uint8_t *ElfParser::getSectionData(const std::string §ionName) const { for (const auto §ion : m_sections) { if (section.name == sectionName) @@ -105,8 +103,7 @@ namespace ps2recomp return nullptr; } - uint32_t ElfParser::getSectionAddress(const std::string §ionName) - { + uint32_t ElfParser::getSectionAddress(const std::string §ionName) const { for (const auto §ion : m_sections) { if (section.name == sectionName) @@ -118,8 +115,7 @@ namespace ps2recomp return 0; } - uint32_t ElfParser::getSectionSize(const std::string §ionName) - { + uint32_t ElfParser::getSectionSize(const std::string §ionName) const { for (const auto §ion : m_sections) { if (section.name == sectionName) diff --git a/ps2xRecomp/src/ps2_recompiler.cpp b/ps2xRecomp/src/ps2_recompiler.cpp index df78859..6ca8601 100644 --- a/ps2xRecomp/src/ps2_recompiler.cpp +++ b/ps2xRecomp/src/ps2_recompiler.cpp @@ -672,8 +672,7 @@ namespace ps2recomp return ps2_runtime_calls::isStubName(name); } - std::string PS2Recompiler::generateRuntimeHeader() - { + std::string PS2Recompiler::generateRuntimeHeader() const { return m_codeGenerator->generateMacroHeader(); } diff --git a/ps2xRecomp/src/r5900_decoder.cpp b/ps2xRecomp/src/r5900_decoder.cpp index bb45ce4..8ff7508 100644 --- a/ps2xRecomp/src/r5900_decoder.cpp +++ b/ps2xRecomp/src/r5900_decoder.cpp @@ -12,8 +12,7 @@ namespace ps2recomp { } - Instruction R5900Decoder::decodeInstruction(uint32_t address, uint32_t rawInstruction) - { + Instruction R5900Decoder::decodeInstruction(uint32_t address, uint32_t rawInstruction) const { Instruction inst; inst.address = address; From b17e8e7f554cdfbc4042f3bcd0f5673127bad117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 17:52:43 +0300 Subject: [PATCH 04/11] refactor: made range-based loop --- ps2xAnalyzer/src/elf_analyzer.cpp | 37 +++++++++++-------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 8b7f596..7fb348e 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -133,18 +133,17 @@ namespace ps2recomp file << "# Jump tables detected in the program\n"; file << "[jump_tables]\n"; - for (size_t i = 0; i < m_jumpTables.size(); ++i) + for (const auto & jt : m_jumpTables) { - const auto &jt = m_jumpTables[i]; file << "[[jump_tables.table]]\n"; file << "address = \"0x" << std::hex << jt.address << "\"\n" << std::dec; file << "entries = [\n"; - for (const auto &entry : jt.entries) + for (const auto & [index, target] : jt.entries) { - file << " { index = " << entry.index << ", target = \"0x" - << std::hex << entry.target << "\" },\n" + file << " { index = " << index << ", target = \"0x" + << std::hex << target << "\" },\n" << std::dec; } @@ -792,11 +791,9 @@ namespace ps2recomp std::vector instructions = decodeFunction(func); - for (size_t i = 0; i < instructions.size(); i++) + for (const auto& inst : instructions) { - const auto &inst = instructions[i]; - - if (inst.isBranch) + if (inst.isBranch) { int32_t offset = static_cast(inst.immediate) << 2; uint32_t targetAddr = inst.address + 4 + offset; @@ -813,11 +810,11 @@ namespace ps2recomp << " (size: " << loopSize << " instructions)" << std::endl; bool hasMultimedia = false; - for (size_t j = 0; j < instructions.size(); j++) + for (const auto& instruction : instructions) { - if (instructions[j].address >= targetAddr && instructions[j].address <= inst.address) + if (instruction.address >= targetAddr && instruction.address <= inst.address) { - if (instructions[j].isMultimedia) + if (instruction.isMultimedia) { hasMultimedia = true; break; @@ -1165,10 +1162,8 @@ namespace ps2recomp bool storesData = false; bool incrementsPointers = false; - for (size_t i = 0; i < instructions.size(); i++) + for (const auto & inst : instructions) { - const auto &inst = instructions[i]; - if (inst.isBranch) { int32_t offset = static_cast(inst.immediate) << 2; @@ -1210,10 +1205,8 @@ namespace ps2recomp bool storesData = false; bool incrementsPointer = false; - for (size_t i = 0; i < instructions.size(); i++) + for (const auto & inst : instructions) { - const auto &inst = instructions[i]; - if (inst.isBranch) { int32_t offset = static_cast(inst.immediate) << 2; @@ -1254,10 +1247,8 @@ namespace ps2recomp bool loadsByte = false; bool storesByte = false; - for (size_t i = 0; i < instructions.size(); i++) + for (const auto & inst : instructions) { - const auto &inst = instructions[i]; - if (inst.isBranch) { int32_t offset = static_cast(inst.immediate) << 2; @@ -1752,10 +1743,8 @@ namespace ps2recomp std::vector instructions = decodeFunction(function); int loopCount = 0; - for (size_t i = 0; i < instructions.size(); i++) + for (const auto & inst : instructions) { - const auto &inst = instructions[i]; - if (inst.isBranch) { int32_t offset = static_cast(inst.immediate) << 2; From 797bc3e475db1dcd5afa725dcb9c00540bff6a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 21:32:18 +0300 Subject: [PATCH 05/11] refactor: make one variable constructor explicit --- ps2xAnalyzer/include/ps2recomp/elf_analyzer.h | 2 +- ps2xRecomp/include/ps2recomp/code_generator.h | 2 +- ps2xRecomp/include/ps2recomp/config_manager.h | 2 +- ps2xRecomp/include/ps2recomp/elf_parser.h | 2 +- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h b/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h index 8e438b6..56cf6ce 100644 --- a/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h +++ b/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h @@ -17,7 +17,7 @@ namespace ps2recomp class ElfAnalyzer { public: - ElfAnalyzer(const std::string &elfPath); + explicit ElfAnalyzer(const std::string &elfPath); ~ElfAnalyzer(); bool analyze(); diff --git a/ps2xRecomp/include/ps2recomp/code_generator.h b/ps2xRecomp/include/ps2recomp/code_generator.h index 1a3ff02..06a7306 100644 --- a/ps2xRecomp/include/ps2recomp/code_generator.h +++ b/ps2xRecomp/include/ps2recomp/code_generator.h @@ -15,7 +15,7 @@ namespace ps2recomp class CodeGenerator { public: - CodeGenerator(const std::vector &symbols); + explicit CodeGenerator(const std::vector &symbols); ~CodeGenerator(); struct BootstrapInfo diff --git a/ps2xRecomp/include/ps2recomp/config_manager.h b/ps2xRecomp/include/ps2recomp/config_manager.h index 2cab24e..38be56c 100644 --- a/ps2xRecomp/include/ps2recomp/config_manager.h +++ b/ps2xRecomp/include/ps2recomp/config_manager.h @@ -10,7 +10,7 @@ namespace ps2recomp class ConfigManager { public: - ConfigManager(const std::string &configPath); + explicit ConfigManager(const std::string &configPath); ~ConfigManager(); RecompilerConfig loadConfig() const; diff --git a/ps2xRecomp/include/ps2recomp/elf_parser.h b/ps2xRecomp/include/ps2recomp/elf_parser.h index c0e5528..378efa2 100644 --- a/ps2xRecomp/include/ps2recomp/elf_parser.h +++ b/ps2xRecomp/include/ps2recomp/elf_parser.h @@ -13,7 +13,7 @@ namespace ps2recomp class ElfParser { public: - ElfParser(const std::string &filePath); + explicit ElfParser(const std::string &filePath); ~ElfParser(); bool parse(); diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index 026f314..7f9b574 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -18,7 +18,7 @@ namespace ps2recomp class PS2Recompiler { public: - PS2Recompiler(const std::string &configPath); + explicit PS2Recompiler(const std::string &configPath); ~PS2Recompiler() = default; bool initialize(); From 9c809da086145945ece42187e52eeea6691715d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 21:50:55 +0300 Subject: [PATCH 06/11] refactor: remove redundant else --- ps2xAnalyzer/src/elf_analyzer.cpp | 14 ++++++++------ ps2xRecomp/src/config_manager.cpp | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 7fb348e..923b7a2 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -1665,11 +1665,12 @@ namespace ps2recomp std::cout << "Skipping function " << function.name << " due to hardware I/O" << std::endl; return true; } - else if (hasComplexMMI && isVeryLarge) + + if (hasComplexMMI && isVeryLarge) { - m_skipFunctions.insert(function.name); - std::cout << "Skipping large function " << function.name << " with complex MMI" << std::endl; - return true; + m_skipFunctions.insert(function.name); + std::cout << "Skipping large function " << function.name << " with complex MMI" << std::endl; + return true; } return false; @@ -1766,9 +1767,10 @@ namespace ps2recomp int32_t offset = static_cast(inst.immediate) << 2; return currentAddr + 4 + offset; } - else if (inst.opcode == OPCODE_J || inst.opcode == OPCODE_JAL) + + if (inst.opcode == OPCODE_J || inst.opcode == OPCODE_JAL) { - return (currentAddr & 0xF0000000) | (inst.target << 2); + return (currentAddr & 0xF0000000) | (inst.target << 2); } return currentAddr + 4; diff --git a/ps2xRecomp/src/config_manager.cpp b/ps2xRecomp/src/config_manager.cpp index 3053589..5285ec3 100644 --- a/ps2xRecomp/src/config_manager.cpp +++ b/ps2xRecomp/src/config_manager.cpp @@ -75,11 +75,11 @@ namespace ps2recomp toml::table patches; toml::array instPatches; - for (const auto &patch : config.patches) + for (const auto & [addr, value] : config.patches) { toml::table p; - p["address"] = "0x" + std::to_string(patch.first); - p["value"] = patch.second; + p["address"] = "0x" + std::to_string(addr); + p["value"] = value; instPatches.push_back(p); } patches["instructions"] = instPatches; From 745bfeeeb30cb8c9f7f44331315f431741f0e25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Fri, 30 Jan 2026 22:25:20 +0300 Subject: [PATCH 07/11] refactor: turn includes to forward declarations --- ps2xAnalyzer/include/ps2recomp/elf_analyzer.h | 19 ++++++++++++++----- ps2xAnalyzer/src/elf_analyzer.cpp | 3 +++ ps2xRecomp/include/ps2recomp/code_generator.h | 8 ++++++-- ps2xRecomp/include/ps2recomp/elf_parser.h | 7 +++++-- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 13 ++++++------- ps2xRecomp/include/ps2recomp/types.h | 2 -- ps2xRecomp/src/code_generator.cpp | 1 + ps2xRecomp/src/elf_parser.cpp | 1 + ps2xRecomp/src/ps2_recompiler.cpp | 5 +++++ ps2xTest/src/code_generator_tests.cpp | 1 + 10 files changed, 42 insertions(+), 18 deletions(-) diff --git a/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h b/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h index 56cf6ce..e1a81a2 100644 --- a/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h +++ b/ps2xAnalyzer/include/ps2recomp/elf_analyzer.h @@ -1,9 +1,6 @@ #ifndef PS2RECOMP_ELF_ANALYZER_H #define PS2RECOMP_ELF_ANALYZER_H -#include "ps2recomp/elf_parser.h" -#include "ps2recomp/r5900_decoder.h" -#include "ps2recomp/types.h" #include #include #include @@ -14,7 +11,20 @@ namespace ps2recomp { - class ElfAnalyzer + struct CFGNode; + struct Instruction; + struct FunctionCall; + struct JumpTable; + struct Relocation; + struct Section; + struct Symbol; + struct Function; + class R5900Decoder; + class ElfParser; + + using CFG = std::unordered_map; + + class ElfAnalyzer { public: explicit ElfAnalyzer(const std::string &elfPath); @@ -40,7 +50,6 @@ namespace ps2recomp std::map m_patches; std::map m_patchReasons; - std::unordered_map m_functionCFGs; std::vector m_jumpTables; std::unordered_map> m_functionCalls; diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 923b7a2..ad1d763 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -1,4 +1,7 @@ #include "ps2recomp/elf_analyzer.h" +#include "ps2recomp/elf_parser.h" +#include "ps2recomp/r5900_decoder.h" +#include "ps2recomp/types.h" #include #include #include diff --git a/ps2xRecomp/include/ps2recomp/code_generator.h b/ps2xRecomp/include/ps2recomp/code_generator.h index 06a7306..5aa3280 100644 --- a/ps2xRecomp/include/ps2recomp/code_generator.h +++ b/ps2xRecomp/include/ps2recomp/code_generator.h @@ -1,7 +1,6 @@ #ifndef PS2RECOMP_CODE_GENERATOR_H #define PS2RECOMP_CODE_GENERATOR_H -#include "ps2recomp/types.h" #include #include #include @@ -10,7 +9,12 @@ namespace ps2recomp { - extern const std::unordered_set kKeywords; + struct JumpTableEntry; + struct Instruction; + struct Function; + struct Symbol; + + extern const std::unordered_set kKeywords; class CodeGenerator { diff --git a/ps2xRecomp/include/ps2recomp/elf_parser.h b/ps2xRecomp/include/ps2recomp/elf_parser.h index 378efa2..8ee9da0 100644 --- a/ps2xRecomp/include/ps2recomp/elf_parser.h +++ b/ps2xRecomp/include/ps2recomp/elf_parser.h @@ -1,7 +1,6 @@ #ifndef PS2RECOMP_ELF_PARSER_H #define PS2RECOMP_ELF_PARSER_H -#include "ps2recomp/types.h" #include #include #include @@ -9,8 +8,12 @@ namespace ps2recomp { + struct Relocation; + struct Section; + struct Function; + struct Symbol; - class ElfParser + class ElfParser { public: explicit ElfParser(const std::string &filePath); diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index 7f9b574..89dc6d0 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -1,11 +1,8 @@ #ifndef PS2RECOMP_PS2_RECOMPILER_H #define PS2RECOMP_PS2_RECOMPILER_H -#include "ps2recomp/types.h" -#include "ps2recomp/elf_parser.h" -#include "ps2recomp/r5900_decoder.h" -#include "ps2recomp/code_generator.h" -#include "ps2recomp/config_manager.h" +#include "code_generator.h" +#include "config_manager.h" #include #include #include @@ -14,12 +11,14 @@ namespace ps2recomp { + class R5900Decoder; + class ElfParser; - class PS2Recompiler + class PS2Recompiler { public: explicit PS2Recompiler(const std::string &configPath); - ~PS2Recompiler() = default; + ~PS2Recompiler(); bool initialize(); bool recompile(); diff --git a/ps2xRecomp/include/ps2recomp/types.h b/ps2xRecomp/include/ps2recomp/types.h index 0e5527b..7855ccd 100644 --- a/ps2xRecomp/include/ps2recomp/types.h +++ b/ps2xRecomp/include/ps2recomp/types.h @@ -152,8 +152,6 @@ namespace ps2recomp JumpTable jumpTable; }; - using CFG = std::unordered_map; - // Function call struct FunctionCall { diff --git a/ps2xRecomp/src/code_generator.cpp b/ps2xRecomp/src/code_generator.cpp index 95d7e7b..1afb7ff 100644 --- a/ps2xRecomp/src/code_generator.cpp +++ b/ps2xRecomp/src/code_generator.cpp @@ -1,5 +1,6 @@ #include "ps2recomp/code_generator.h" #include "ps2recomp/instructions.h" +#include "ps2recomp/types.h" #include #include #include diff --git a/ps2xRecomp/src/elf_parser.cpp b/ps2xRecomp/src/elf_parser.cpp index a10a429..04ac2ae 100644 --- a/ps2xRecomp/src/elf_parser.cpp +++ b/ps2xRecomp/src/elf_parser.cpp @@ -1,4 +1,5 @@ #include "ps2recomp/elf_parser.h" +#include "ps2recomp/types.h" #include #include diff --git a/ps2xRecomp/src/ps2_recompiler.cpp b/ps2xRecomp/src/ps2_recompiler.cpp index 6ca8601..f23b795 100644 --- a/ps2xRecomp/src/ps2_recompiler.cpp +++ b/ps2xRecomp/src/ps2_recompiler.cpp @@ -1,5 +1,8 @@ #include "ps2recomp/ps2_recompiler.h" #include "ps2recomp/instructions.h" +#include "ps2recomp/types.h" +#include "ps2recomp/elf_parser.h" +#include "ps2recomp/r5900_decoder.h" #include "ps2_runtime_calls.h" #include #include @@ -44,6 +47,8 @@ namespace ps2recomp { } + PS2Recompiler::~PS2Recompiler() = default; + bool PS2Recompiler::initialize() { try diff --git a/ps2xTest/src/code_generator_tests.cpp b/ps2xTest/src/code_generator_tests.cpp index b0c9f72..4a72cc5 100644 --- a/ps2xTest/src/code_generator_tests.cpp +++ b/ps2xTest/src/code_generator_tests.cpp @@ -1,6 +1,7 @@ #include "MiniTest.h" #include "ps2recomp/code_generator.h" #include "ps2recomp/instructions.h" +#include "ps2recomp/types.h" using namespace ps2recomp; From d5dc7a6e1f52f5c75a9e1818489c66ac94479ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Sat, 31 Jan 2026 12:18:31 +0300 Subject: [PATCH 08/11] refactor: brace placements turned into allman style --- ps2xAnalyzer/src/elf_analyzer.cpp | 39 ++++++++++++++++++++----------- ps2xRecomp/src/config_manager.cpp | 6 +++-- ps2xRecomp/src/elf_parser.cpp | 12 ++++++---- ps2xRecomp/src/ps2_recompiler.cpp | 3 ++- ps2xRecomp/src/r5900_decoder.cpp | 3 ++- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index ad1d763..63a491a 100644 --- a/ps2xAnalyzer/src/elf_analyzer.cpp +++ b/ps2xAnalyzer/src/elf_analyzer.cpp @@ -781,7 +781,8 @@ namespace ps2recomp } } - void ElfAnalyzer::analyzePerformanceCriticalPaths() const { + void ElfAnalyzer::analyzePerformanceCriticalPaths() const + { std::cout << "Analyzing performance-critical paths..." << std::endl; for (const auto &func : m_functions) @@ -900,7 +901,8 @@ namespace ps2recomp } } - void ElfAnalyzer::analyzeRegisterUsage() const { + void ElfAnalyzer::analyzeRegisterUsage() const + { std::cout << "Analyzing register usage patterns..." << std::endl; for (const auto &func : m_functions) @@ -998,7 +1000,8 @@ namespace ps2recomp } } - void ElfAnalyzer::analyzeFunctionSignatures() const { + void ElfAnalyzer::analyzeFunctionSignatures() const + { std::cout << "Analyzing function signatures..." << std::endl; for (const auto &func : m_functions) @@ -1157,7 +1160,8 @@ namespace ps2recomp } } - bool ElfAnalyzer::identifyMemcpyPattern(const Function &func) const { + bool ElfAnalyzer::identifyMemcpyPattern(const Function &func) const + { std::vector instructions = decodeFunction(func); bool hasLoop = false; @@ -1200,7 +1204,8 @@ namespace ps2recomp return hasLoop && loadsData && storesData && incrementsPointers; } - bool ElfAnalyzer::identifyMemsetPattern(const Function &func) const { + bool ElfAnalyzer::identifyMemsetPattern(const Function &func) const + { std::vector instructions = decodeFunction(func); bool hasLoop = false; @@ -1242,7 +1247,8 @@ namespace ps2recomp return hasLoop && usesConstant && storesData && incrementsPointer; } - bool ElfAnalyzer::identifyStringOperationPattern(const Function &func) const { + bool ElfAnalyzer::identifyStringOperationPattern(const Function &func) const + { std::vector instructions = decodeFunction(func); bool hasLoop = false; @@ -1281,7 +1287,8 @@ namespace ps2recomp return hasLoop && checksZero && (loadsByte || storesByte); } - bool ElfAnalyzer::identifyMathPattern(const Function &func) const { + bool ElfAnalyzer::identifyMathPattern(const Function &func) const + { std::vector instructions = decodeFunction(func); int mathOps = 0; @@ -1311,7 +1318,8 @@ namespace ps2recomp return mathOps > instructions.size() * 0.3 || usesFPU; } - CFG ElfAnalyzer::buildCFG(const Function &function) const { + CFG ElfAnalyzer::buildCFG(const Function &function) const + { CFG cfg; std::vector instructions = decodeFunction(function); std::map addrToIndex; @@ -1553,7 +1561,8 @@ namespace ps2recomp return false; } - std::vector ElfAnalyzer::decodeFunction(const Function &function) const { + std::vector ElfAnalyzer::decodeFunction(const Function &function) const + { std::vector instructions; for (uint32_t addr = function.start; addr < function.end; addr += 4) @@ -1587,7 +1596,8 @@ namespace ps2recomp return ss.str(); } - bool ElfAnalyzer::hasMMIInstructions(const Function &function) const { + bool ElfAnalyzer::hasMMIInstructions(const Function &function) const + { std::vector instructions = decodeFunction(function); for (const auto &inst : instructions) @@ -1601,7 +1611,8 @@ namespace ps2recomp return false; } - bool ElfAnalyzer::hasVUInstructions(const Function &function) const { + bool ElfAnalyzer::hasVUInstructions(const Function &function) const + { std::vector instructions = decodeFunction(function); for (const auto &inst : instructions) @@ -1698,7 +1709,8 @@ namespace ps2recomp } } - bool ElfAnalyzer::isSelfModifyingCode(const Function &function) const { + bool ElfAnalyzer::isSelfModifyingCode(const Function &function) const + { std::vector instructions = decodeFunction(function); for (size_t i = 0; i < instructions.size(); i++) @@ -1743,7 +1755,8 @@ namespace ps2recomp return false; } - bool ElfAnalyzer::isLoopHeavyFunction(const Function &function) const { + bool ElfAnalyzer::isLoopHeavyFunction(const Function &function) const + { std::vector instructions = decodeFunction(function); int loopCount = 0; diff --git a/ps2xRecomp/src/config_manager.cpp b/ps2xRecomp/src/config_manager.cpp index 5285ec3..adbafbe 100644 --- a/ps2xRecomp/src/config_manager.cpp +++ b/ps2xRecomp/src/config_manager.cpp @@ -14,7 +14,8 @@ namespace ps2recomp ConfigManager::~ConfigManager() = default; - RecompilerConfig ConfigManager::loadConfig() const { + RecompilerConfig ConfigManager::loadConfig() const + { RecompilerConfig config; try @@ -57,7 +58,8 @@ namespace ps2recomp return config; } - void ConfigManager::saveConfig(const RecompilerConfig &config) const { + void ConfigManager::saveConfig(const RecompilerConfig &config) const + { toml::value data; toml::table general; diff --git a/ps2xRecomp/src/elf_parser.cpp b/ps2xRecomp/src/elf_parser.cpp index 04ac2ae..143a04c 100644 --- a/ps2xRecomp/src/elf_parser.cpp +++ b/ps2xRecomp/src/elf_parser.cpp @@ -22,7 +22,8 @@ namespace ps2recomp !(section->get_flags() & ELFIO::SHF_EXECINSTR); } - std::vector ElfParser::extractFunctions() const { + std::vector ElfParser::extractFunctions() const + { std::vector functions; for (const auto &symbol : m_symbols) @@ -92,7 +93,8 @@ namespace ps2recomp throw std::runtime_error("Invalid address for readWord: " + std::to_string(address)); } - uint8_t *ElfParser::getSectionData(const std::string §ionName) const { + uint8_t *ElfParser::getSectionData(const std::string §ionName) const + { for (const auto §ion : m_sections) { if (section.name == sectionName) @@ -104,7 +106,8 @@ namespace ps2recomp return nullptr; } - uint32_t ElfParser::getSectionAddress(const std::string §ionName) const { + uint32_t ElfParser::getSectionAddress(const std::string §ionName) const + { for (const auto §ion : m_sections) { if (section.name == sectionName) @@ -116,7 +119,8 @@ namespace ps2recomp return 0; } - uint32_t ElfParser::getSectionSize(const std::string §ionName) const { + uint32_t ElfParser::getSectionSize(const std::string §ionName) const + { for (const auto §ion : m_sections) { if (section.name == sectionName) diff --git a/ps2xRecomp/src/ps2_recompiler.cpp b/ps2xRecomp/src/ps2_recompiler.cpp index f23b795..81db3a8 100644 --- a/ps2xRecomp/src/ps2_recompiler.cpp +++ b/ps2xRecomp/src/ps2_recompiler.cpp @@ -677,7 +677,8 @@ namespace ps2recomp return ps2_runtime_calls::isStubName(name); } - std::string PS2Recompiler::generateRuntimeHeader() const { + std::string PS2Recompiler::generateRuntimeHeader() const + { return m_codeGenerator->generateMacroHeader(); } diff --git a/ps2xRecomp/src/r5900_decoder.cpp b/ps2xRecomp/src/r5900_decoder.cpp index 8ff7508..dcafb3a 100644 --- a/ps2xRecomp/src/r5900_decoder.cpp +++ b/ps2xRecomp/src/r5900_decoder.cpp @@ -12,7 +12,8 @@ namespace ps2recomp { } - Instruction R5900Decoder::decodeInstruction(uint32_t address, uint32_t rawInstruction) const { + Instruction R5900Decoder::decodeInstruction(uint32_t address, uint32_t rawInstruction) const + { Instruction inst; inst.address = address; From 93dc266e977480776bac4d13d2dcf34b92e70599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Sun, 1 Feb 2026 02:03:57 +0300 Subject: [PATCH 09/11] refactor: structed binding for readability and clarity, used emplace_back to prevent extra-copied object --- ps2xRecomp/src/code_generator.cpp | 40 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/ps2xRecomp/src/code_generator.cpp b/ps2xRecomp/src/code_generator.cpp index 1afb7ff..dadf013 100644 --- a/ps2xRecomp/src/code_generator.cpp +++ b/ps2xRecomp/src/code_generator.cpp @@ -160,7 +160,7 @@ namespace ps2recomp else if (branchInst.isBranch) { std::string conditionStr = "false"; - std::string linkCode = ""; + std::string linkCode; switch (branchInst.opcode) { @@ -277,7 +277,7 @@ namespace ps2recomp (branchInst.opcode == OPCODE_COP1 && branchInst.rs == COP1_BC && (branchInst.rt == COP1_BC_BCFL || branchInst.rt == COP1_BC_BCTL)) || (branchInst.opcode == OPCODE_COP2 && branchInst.rs == COP2_BC && (branchInst.rt == COP2_BC_BCFL || branchInst.rt == COP2_BC_BCTL))); - if (linkCode != "") + if (!linkCode.empty()) { ss << " " << linkCode << "\n"; } @@ -2491,11 +2491,11 @@ namespace ps2recomp if (function.isStub) { - stubFunctions.push_back({function.start, generatedName}); + stubFunctions.emplace_back(function.start, generatedName); } else { - normalFunctions.push_back({function.start, generatedName}); + normalFunctions.emplace_back(function.start, generatedName); } } @@ -2507,31 +2507,31 @@ namespace ps2recomp } ss << " // Register recompiled functions\n"; - for (const auto &function : normalFunctions) + for (const auto & [first, second] : normalFunctions) { - ss << " runtime.registerFunction(0x" << std::hex << function.first << std::dec - << ", " << function.second << ");\n"; + ss << " runtime.registerFunction(0x" << std::hex << first << std::dec + << ", " << second << ");\n"; } ss << "\n // Register stub functions\n"; - for (const auto &function : stubFunctions) + for (const auto & [first, second] : stubFunctions) { - ss << " runtime.registerFunction(0x" << std::hex << function.first << std::dec - << ", " << function.second << ");\n"; + ss << " runtime.registerFunction(0x" << std::hex << first << std::dec + << ", " << second << ");\n"; } ss << "\n // Register system call stubs\n"; - for (const auto &function : systemCallFunctions) + for (const auto & [first, second] : systemCallFunctions) { - ss << " runtime.registerFunction(0x" << std::hex << function.first << std::dec - << ", " << function.second << ");\n"; + ss << " runtime.registerFunction(0x" << std::hex << first << std::dec + << ", " << second << ");\n"; } ss << "\n // Register library stubs\n"; - for (const auto &function : libraryFunctions) + for (const auto & [first, second] : libraryFunctions) { - ss << " runtime.registerFunction(0x" << std::hex << function.first << std::dec - << ", " << function.second << ");\n"; + ss << " runtime.registerFunction(0x" << std::hex << first << std::dec + << ", " << second << ");\n"; } ss << "}\n"; @@ -2548,18 +2548,18 @@ namespace ps2recomp ss << "switch (ctx->r[" << indexReg << "]) {\n"; - for (const auto &entry : entries) + for (const auto & [index, target] : entries) { - ss << " case " << entry.index << ": {\n"; + ss << " case " << index << ": {\n"; - std::string funcName = getFunctionName(entry.target); + std::string funcName = getFunctionName(target); if (!funcName.empty()) { ss << " " << funcName << "(rdram, ctx, runtime);\n"; } else { - ss << " func_" << std::hex << entry.target << std::dec << "(rdram, ctx, runtime);\n"; + ss << " func_" << std::hex << target << std::dec << "(rdram, ctx, runtime);\n"; } ss << " return;\n"; From 5f4f3aed2e161d1ed68c7f9f6a3c620ebb9fecb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Tue, 3 Feb 2026 01:37:07 +0300 Subject: [PATCH 10/11] fix: add dwarf_private.h include --- ps2xRecomp/src/lib/elf_parser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ps2xRecomp/src/lib/elf_parser.cpp b/ps2xRecomp/src/lib/elf_parser.cpp index 193a521..a2cd126 100644 --- a/ps2xRecomp/src/lib/elf_parser.cpp +++ b/ps2xRecomp/src/lib/elf_parser.cpp @@ -14,7 +14,7 @@ #else #include #endif - +#include "libdwarf_private.h" #include #include #include From 0fda5499e041b9e38a9563fc1a9639f39662aa67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Tue, 3 Feb 2026 03:00:06 +0300 Subject: [PATCH 11/11] fix: update GPR_U32 and GPR_S32 macros to use _mm_extract_epi32 for SSE/AVX intrinsics --- ps2xRuntime/include/ps2_runtime.h | 9 ++++++--- ps2xRuntime/include/ps2_runtime_macros.h | 15 +++++++++------ ps2xRuntime/src/lib/ps2_runtime.cpp | 10 +++++----- ps2xRuntime/src/lib/ps2_syscalls.cpp | 7 ++++++- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/ps2xRuntime/include/ps2_runtime.h b/ps2xRuntime/include/ps2_runtime.h index cd16608..db73007 100644 --- a/ps2xRuntime/include/ps2_runtime.h +++ b/ps2xRuntime/include/ps2_runtime.h @@ -7,6 +7,7 @@ #include #include #include // For SSE/AVX instructions +#include #include #include #include @@ -225,8 +226,10 @@ struct alignas(16) R5900Context for (int i = 0; i < 32; ++i) { std::cout << "R" << std::setw(2) << std::dec << i << ": 0x" << std::hex - << std::setw(8) << r[i].m128i_u32[3] << std::setw(8) << r[i].m128i_u32[2] << "_" - << std::setw(8) << r[i].m128i_u32[1] << std::setw(8) << r[i].m128i_u32[0] << "\n"; + << std::setw(8) << static_cast(_mm_extract_epi32(r[i], 3)) + << std::setw(8) << static_cast(_mm_extract_epi32(r[i], 2)) << "_" + << std::setw(8) << static_cast(_mm_extract_epi32(r[i], 1)) + << std::setw(8) << static_cast(_mm_extract_epi32(r[i], 0)) << "\n"; } std::cout << "Status: 0x" << std::setw(8) << cop0_status << " Cause: 0x" << std::setw(8) << cop0_cause @@ -243,7 +246,7 @@ inline uint32_t getRegU32(const R5900Context *ctx, int reg) // Check if reg is valid (0-31) if (reg < 0 || reg > 31) return 0; - return ctx->r[reg].m128i_u32[0]; + return static_cast(_mm_extract_epi32(ctx->r[reg], 0)); } inline void setReturnU32(R5900Context *ctx, uint32_t value) diff --git a/ps2xRuntime/include/ps2_runtime_macros.h b/ps2xRuntime/include/ps2_runtime_macros.h index fd5f828..1576f1f 100644 --- a/ps2xRuntime/include/ps2_runtime_macros.h +++ b/ps2xRuntime/include/ps2_runtime_macros.h @@ -1,8 +1,11 @@ #ifndef PS2_RUNTIME_MACROS_H #define PS2_RUNTIME_MACROS_H #include -#include // For SSE/AVX intrinsics -#include +#if defined(_MSC_VER) + #include +#else + #include // For SSE/AVX intrinsics +#endif inline uint32_t ps2_clz32(uint32_t val) { #if defined(_MSC_VER) unsigned long idx; @@ -207,10 +210,10 @@ inline __m128i _mm_custom_srav_epi32(__m128i a, __m128i count) { #define PS2_VCALLMS(addr) // VU0 microprogram calls not supported directly #define PS2_VCALLMSR(reg) // VU0 microprogram calls not supported directly -#define GPR_U32(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0U : ctx_ptr->r[reg_idx].m128i_u32[0]) -#define GPR_S32(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0 : ctx_ptr->r[reg_idx].m128i_i32[0]) -#define GPR_U64(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0ULL : ctx_ptr->r[reg_idx].m128i_u64[0]) -#define GPR_S64(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0LL : ctx_ptr->r[reg_idx].m128i_i64[0]) +#define GPR_U32(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0U : static_cast(_mm_extract_epi32(ctx_ptr->r[reg_idx], 0))) +#define GPR_S32(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0 : _mm_extract_epi32(ctx_ptr->r[reg_idx], 0)) +#define GPR_U64(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0ULL : static_cast(_mm_extract_epi64(ctx_ptr->r[reg_idx], 0))) +#define GPR_S64(ctx_ptr, reg_idx) ((reg_idx == 0) ? 0LL : _mm_extract_epi64(ctx_ptr->r[reg_idx], 0)) #define GPR_VEC(ctx_ptr, reg_idx) ((reg_idx == 0) ? _mm_setzero_si128() : ctx_ptr->r[reg_idx]) #define SET_GPR_U32(ctx_ptr, reg_idx, val) \ diff --git a/ps2xRuntime/src/lib/ps2_runtime.cpp b/ps2xRuntime/src/lib/ps2_runtime.cpp index cd82a87..425d069 100644 --- a/ps2xRuntime/src/lib/ps2_runtime.cpp +++ b/ps2xRuntime/src/lib/ps2_runtime.cpp @@ -331,7 +331,7 @@ void PS2Runtime::executeVU0Microprogram(uint8_t *rdram, R5900Context *ctx, uint3 { std::cout << "[VU0] microprogram @0x" << std::hex << address << " pc=0x" << ctx->pc - << " ra=0x" << ctx->r[31].m128i_u32[0] + << " ra=0x" << static_cast(_mm_extract_epi32(ctx->r[31], 0)) << std::dec << std::endl; } ++count; @@ -430,7 +430,7 @@ void PS2Runtime::run() { entryPoint(m_memory.getRDRAM(), &m_cpuContext, this); std::cout << "Game thread returned. PC=0x" << std::hex << m_cpuContext.pc - << " RA=0x" << m_cpuContext.r[31].m128i_u32[0] << std::dec << std::endl; + << " RA=0x" << static_cast(_mm_extract_epi32(m_cpuContext.r[31], 0)) << std::dec << std::endl; } catch (const std::exception &e) { @@ -445,9 +445,9 @@ void PS2Runtime::run() { std::cout << "[run] activeThreads=" << g_activeThreads.load(std::memory_order_relaxed); std::cout << " pc=0x" << std::hex << m_cpuContext.pc - << " ra=0x" << m_cpuContext.r[31].m128i_u32[0] - << " sp=0x" << m_cpuContext.r[29].m128i_u32[0] - << " gp=0x" << m_cpuContext.r[28].m128i_u32[0] << std::dec << std::endl; + << " ra=0x" << static_cast(_mm_extract_epi32(m_cpuContext.r[31], 0)) + << " sp=0x" << static_cast(_mm_extract_epi32(m_cpuContext.r[29], 0)) + << " gp=0x" << static_cast(_mm_extract_epi32(m_cpuContext.r[28], 0)) << std::dec << std::endl; } if ((tick % 600) == 0) { diff --git a/ps2xRuntime/src/lib/ps2_syscalls.cpp b/ps2xRuntime/src/lib/ps2_syscalls.cpp index 129323a..1d4a7a1 100644 --- a/ps2xRuntime/src/lib/ps2_syscalls.cpp +++ b/ps2xRuntime/src/lib/ps2_syscalls.cpp @@ -1,4 +1,3 @@ - #include "ps2_syscalls.h" #include "ps2_runtime.h" #include "ps2_runtime_macros.h" @@ -15,6 +14,12 @@ #include #include +#ifndef _WIN32 +#include // for unlink,rmdir,chdir +#include // for mkdir +#endif + + std::unordered_map g_fileDescriptors; int g_nextFd = 3; // Start after stdin, stdout, stderr