From 5d3be0e2e79035cebee03338b97451a7f9ca088e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Sami=20G=C3=BCrp=C4=B1nar?= Date: Tue, 3 Feb 2026 16:25:22 +0300 Subject: [PATCH] fix portability issue (#41) * refactor: update function name checks to use contains method * refactor: update code generation to use static_cast * refactor: made member methods const * refactor: made range-based loop * refactor: make one variable constructor explicit * refactor: remove redundant else * refactor: turn includes to forward declarations * refactor: brace placements turned into allman style * refactor: structed binding for readability and clarity, used emplace_back to prevent extra-copied object * fix: add dwarf_private.h include * fix: update GPR_U32 and GPR_S32 macros to use _mm_extract_epi32 for SSE/AVX intrinsics --- ps2xRecomp/src/lib/elf_parser.cpp | 2 +- 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 ++++++- 5 files changed, 27 insertions(+), 16 deletions(-) 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 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