From e8f0c69de6c8d81946f03d89d43550b04ec802e9 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:04:22 +0300 Subject: [PATCH] Use contains and static_cast (#33) * refactor: update function name checks to use contains method * refactor: update code generation to use static_cast --- ps2xAnalyzer/src/elf_analyzer.cpp | 68 +++++++++++++++---------------- ps2xRecomp/src/code_generator.cpp | 16 ++++---- ps2xRecomp/src/ps2_recompiler.cpp | 8 ++-- 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/ps2xAnalyzer/src/elf_analyzer.cpp b/ps2xAnalyzer/src/elf_analyzer.cpp index 6552b66..6919d61 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; } @@ -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]; @@ -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..95d7e7b 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) @@ -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) { @@ -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"; } @@ -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( "{{ " 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; }