From d8676283fd1e8990b415cc0e4810c6db895b9fba Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sun, 17 Nov 2024 21:00:36 +0300 Subject: [PATCH] Implement a better way to combat Clang's unsafe code reordering. --- PowerRecomp/recompiler.cpp | 3 --- PowerUtils/ppc_context.h | 16 ++++++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/PowerRecomp/recompiler.cpp b/PowerRecomp/recompiler.cpp index 36c2f16..0c3af75 100644 --- a/PowerRecomp/recompiler.cpp +++ b/PowerRecomp/recompiler.cpp @@ -2265,9 +2265,6 @@ bool Recompiler::Recompile(const Function& fn) { println("loc_{:X}:", base); - // With a read barrier we prevent the compiler from doing unsafe code reordering. I wonder if we could do this better... - println("\t_ReadBarrier();"); - // Anyone could jump to this label so we wouldn't know what the CSR state would be. csrState = CSRState::Unknown; } diff --git a/PowerUtils/ppc_context.h b/PowerUtils/ppc_context.h index ec96126..17fcdef 100644 --- a/PowerUtils/ppc_context.h +++ b/PowerUtils/ppc_context.h @@ -24,15 +24,15 @@ #define PPC_FUNC_PROLOGUE() __builtin_assume(((size_t)base & 0xFFFFFFFF) == 0) -#define PPC_LOAD_U8(x) *(uint8_t*)(base + (x)) -#define PPC_LOAD_U16(x) __builtin_bswap16(*(uint16_t*)(base + (x))) -#define PPC_LOAD_U32(x) __builtin_bswap32(*(uint32_t*)(base + (x))) -#define PPC_LOAD_U64(x) __builtin_bswap64(*(uint64_t*)(base + (x))) +#define PPC_LOAD_U8(x) *(volatile uint8_t*)(base + (x)) +#define PPC_LOAD_U16(x) __builtin_bswap16(*(volatile uint16_t*)(base + (x))) +#define PPC_LOAD_U32(x) __builtin_bswap32(*(volatile uint32_t*)(base + (x))) +#define PPC_LOAD_U64(x) __builtin_bswap64(*(volatile uint64_t*)(base + (x))) -#define PPC_STORE_U8(x, y) *(uint8_t*)(base + (x)) = (y) -#define PPC_STORE_U16(x, y) *(uint16_t*)(base + (x)) = __builtin_bswap16(y) -#define PPC_STORE_U32(x, y) *(uint32_t*)(base + (x)) = __builtin_bswap32(y) -#define PPC_STORE_U64(x, y) *(uint64_t*)(base + (x)) = __builtin_bswap64(y) +#define PPC_STORE_U8(x, y) *(volatile uint8_t*)(base + (x)) = (y) +#define PPC_STORE_U16(x, y) *(volatile uint16_t*)(base + (x)) = __builtin_bswap16(y) +#define PPC_STORE_U32(x, y) *(volatile uint32_t*)(base + (x)) = __builtin_bswap32(y) +#define PPC_STORE_U64(x, y) *(volatile uint64_t*)(base + (x)) = __builtin_bswap64(y) #define PPC_CALL_FUNC(x) x(ctx, base) #define PPC_CALL_INDIRECT_FUNC(x) (*(PPCFunc**)(ctx.fn + uint64_t(x) * 2))(ctx, base)