Skip to content

Commit

Permalink
spu/arm64: clean up assembly code generation
Browse files Browse the repository at this point in the history
Clean up asmjit usage so we don't unnecessarily allocate memory
anymore for SPURecompiler functions.
  • Loading branch information
sguo35 committed Sep 5, 2022
1 parent 2902265 commit 0f1dca9
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions rpcs3/Emu/Cell/SPURecompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,18 +1636,26 @@ void spu_recompiler_base::dispatch(spu_thread& spu, void*, u8* rip)

atomic_storage<u64>::release(*reinterpret_cast<u64*>(rip - 8), result);
#elif defined(ARCH_ARM64)
auto jump_instrs = build_function_asm<spu_function_t>("", [](native_asm& c, auto& args)
{
using namespace asmjit;
union
{
u8 bytes[16];
u128 result;
};

Label branch_target = c.newLabel();
c.ldr(a64::x9, arm::Mem(branch_target)); // PC rel load
c.br(a64::x9);
// ldr x9, #8
bytes[0] = 0x49;
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x58;

c.bind(branch_target);
c.embedUInt64(reinterpret_cast<u64>(spu_runtime::tr_all));
});
u128 result = *reinterpret_cast<u128*>(jump_instrs);
// br x9
bytes[4] = 0x20;
bytes[5] = 0x01;
bytes[6] = 0x1F;
bytes[7] = 0xD6;

const u64 target = reinterpret_cast<u64>(spu_runtime::tr_all);
std::memcpy(bytes + 8, &target, 8);
#if defined(__APPLE__)
pthread_jit_write_protect_np(false);
#endif
Expand Down Expand Up @@ -1768,18 +1776,26 @@ void spu_recompiler_base::branch(spu_thread& spu, void*, u8* rip)

atomic_storage<u64>::release(*reinterpret_cast<u64*>(rip), result);
#elif defined(ARCH_ARM64)
auto jmp_instrs = build_function_asm<spu_function_t>("", [&](native_asm& c, auto& args)
{
using namespace asmjit;
union
{
u8 bytes[16];
u128 result;
};

Label branch_target = c.newLabel();
c.ldr(a64::x9, arm::Mem(branch_target)); // PC rel load
c.br(a64::x9);
// ldr x9, #8
bytes[0] = 0x49;
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x58;

c.bind(branch_target);
c.embedUInt64(reinterpret_cast<u64>(func));
});
u128 result = *reinterpret_cast<u128*>(jmp_instrs);
// br x9
bytes[4] = 0x20;
bytes[5] = 0x01;
bytes[6] = 0x1F;
bytes[7] = 0xD6;

const u64 target = reinterpret_cast<u64>(func);
std::memcpy(bytes + 8, &target, 8);
#if defined(__APPLE__)
pthread_jit_write_protect_np(false);
#endif
Expand Down

0 comments on commit 0f1dca9

Please sign in to comment.