diff --git a/src/backend/core/JIT.cpp b/src/backend/core/JIT.cpp index 86331a74..77f98012 100644 --- a/src/backend/core/JIT.cpp +++ b/src/backend/core/JIT.cpp @@ -72,38 +72,40 @@ std::optional JIT::FetchInstruction(s64 vaddr) { } void JIT::SetPC32(const s32 val) { - code.mov(code.SCR1, REG(qword, pc)); - code.mov(REG(qword, oldPC), code.SCR1); - code.mov(code.SCR1, val); - code.mov(REG(qword, pc), code.SCR1); - code.mov(code.SCR1, val + 4); - code.mov(REG(qword, nextPC), code.SCR1); + code.mov(code.SCR1, code.ptr[PC_OFFSET()]); + code.mov(code.ptr[OLD_PC_OFFSET()], code.SCR1); + code.mov(code.SCR1.cvt32(), val); + code.movsxd(code.SCR1.cvt64(), code.SCR1.cvt32()); + code.mov(code.ptr[PC_OFFSET()], code.SCR1); + code.mov(code.SCR1.cvt32(), val + 4); + code.movsxd(code.SCR1.cvt64(), code.SCR1.cvt32()); + code.mov(code.ptr[NEXT_PC_OFFSET()], code.SCR1); } void JIT::SetPC64(const s64 val) { - code.mov(code.SCR1, REG(qword, pc)); - code.mov(REG(qword, oldPC), code.SCR1); + code.mov(code.SCR1, code.ptr[PC_OFFSET()]); + code.mov(code.ptr[OLD_PC_OFFSET()], code.SCR1); code.mov(code.SCR1, val); - code.mov(REG(qword, pc), code.SCR1); + code.mov(code.ptr[PC_OFFSET()], code.SCR1); code.mov(code.SCR1, val + 4); - code.mov(REG(qword, nextPC), code.SCR1); + code.mov(code.ptr[NEXT_PC_OFFSET()], code.SCR1); } void JIT::SetPC32(const Xbyak::Reg32& val) { - code.mov(code.SCR1, REG(qword, pc)); - code.mov(REG(qword, oldPC), code.SCR1); + code.mov(code.SCR1, code.ptr[PC_OFFSET()]); + code.mov(code.ptr[OLD_PC_OFFSET()], code.SCR1); code.movsxd(val.cvt64(), val); - code.mov(REG(qword, pc), val); + code.mov(code.ptr[PC_OFFSET()], val); code.add(val, 4); - code.mov(REG(qword, nextPC), val); + code.mov(code.ptr[NEXT_PC_OFFSET()], val); } void JIT::SetPC64(const Xbyak::Reg64& val) { - code.mov(code.SCR1, REG(qword, pc)); - code.mov(REG(qword, oldPC), code.SCR1); - code.mov(REG(qword, pc), val); + code.mov(code.SCR1, code.ptr[PC_OFFSET()]); + code.mov(code.ptr[OLD_PC_OFFSET()], code.SCR1); + code.mov(code.ptr[PC_OFFSET()], val); code.add(val, 4); - code.mov(REG(qword, nextPC), val); + code.mov(code.ptr[NEXT_PC_OFFSET()], val); } u32 JIT::Step() { @@ -192,15 +194,15 @@ u32 JIT::Step() { if(!branch_taken) { Xbyak::Label runtime_branch_taken; - code.mov(code.SCR1, JIT_VAR(byte, branch_taken)); + code.mov(code.SCR1, code.byte[code.rbp + BRANCH_TAKEN_OFFSET()]); code.cmp(code.SCR1, 0); code.jne(runtime_branch_taken); code.mov(code.SCR1, blockOldPC); - code.mov(REG(qword, oldPC), code.SCR1); + code.mov(code.ptr[OLD_PC_OFFSET()], code.SCR1); code.mov(code.SCR1, blockPC); - code.mov(REG(qword, pc), code.SCR1); + code.mov(code.ptr[PC_OFFSET()], code.SCR1); code.mov(code.SCR1, blockNextPC); - code.mov(REG(qword, nextPC), code.SCR1); + code.mov(code.ptr[NEXT_PC_OFFSET()], code.SCR1); code.L(runtime_branch_taken); } diff --git a/src/backend/core/JIT.hpp b/src/backend/core/JIT.hpp index 2cd06375..cd302ca6 100644 --- a/src/backend/core/JIT.hpp +++ b/src/backend/core/JIT.hpp @@ -16,13 +16,13 @@ static constexpr u32 kUpperSize = kAddressSpaceSize >> kUpperShift; // 0x800000 static constexpr u32 kLowerSize = 0x100; // 0x80 static constexpr u32 kCodeCacheSize = 32_mb; static constexpr u32 kCodeCacheAllocSize = kCodeCacheSize + 4_kb; -#if 1 -#define REG(acc, x) code.acc[reinterpret_cast(®s.x)] -#define JIT_VAR(acc, x) code.acc[reinterpret_cast(&x)] -#else -#define REG(acc, x) code.acc[code.rbp + (reinterpret_cast(®s.x) - reinterpret_cast(this))] -#define JIT_VAR(acc, x) code.acc[code.rbp + (reinterpret_cast(&x) - reinterpret_cast(this))] -#endif +#define OLD_PC_OFFSET() (reinterpret_cast(®s.oldPC)) +#define PC_OFFSET() (reinterpret_cast(®s.pc)) +#define NEXT_PC_OFFSET() (reinterpret_cast(®s.nextPC)) +#define GPR_OFFSET(x) (reinterpret_cast(®s.gpr[(x)])) +#define BRANCH_TAKEN_OFFSET() (reinterpret_cast(&branch_taken) - reinterpret_cast(this)) +#define HI_OFFSET() (reinterpret_cast(®s.hi)) +#define LO_OFFSET() (reinterpret_cast(®s.lo)) #ifdef __aarch64__ struct JIT : BaseCPU {}; @@ -61,15 +61,15 @@ private: csh disassemblerMips{}, disassemblerX86{}; template - Xbyak::Address GPR(const size_t index) const { + Xbyak::Address GPR(const size_t index) { if constexpr (sizeof(T) == 1) { - return REG(byte, gpr[index]); + return code.byte[GPR_OFFSET(index)]; } else if constexpr (sizeof(T) == 2) { - return REG(word, gpr[index]); + return code.word[GPR_OFFSET(index)]; } else if constexpr (sizeof(T) == 4) { - return REG(dword, gpr[index]); + return code.dword[GPR_OFFSET(index)]; } else if constexpr (sizeof(T) == 8) { - return REG(qword, gpr[index]); + return code.ptr[GPR_OFFSET(index)]; } Util::Error::GetInstance().Throw( diff --git a/src/backend/core/jit/instructions.cpp b/src/backend/core/jit/instructions.cpp index 356f2d39..1a07c54b 100644 --- a/src/backend/core/jit/instructions.cpp +++ b/src/backend/core/jit/instructions.cpp @@ -159,7 +159,7 @@ void JIT::bfc0(const Instruction instr) { const s16 imm = instr; const s64 offset = u64((s64)imm) << 2; const s64 address = blockPC + offset; - // code.mov(code.al, REG(byte, cop1.fcr31.compare)); + // code.mov(code.al, code.byte[code.rbp + REG_OFFSET(cop1.fcr31.compare)); // code.test(code.al, code.al); // branch(address, z); } @@ -168,7 +168,7 @@ void JIT::blfc0(const Instruction instr) { const s16 imm = instr; const s64 offset = u64((s64)imm) << 2; const s64 address = blockPC + offset; - // code.mov(code.al, REG(byte, cop1.fcr31.compare)); + // code.mov(code.al, code.byte[code.rbp + REG_OFFSET(cop1.fcr31.compare)); // code.test(code.al, code.al); // branch_likely(address, z); } @@ -177,7 +177,7 @@ void JIT::bfc1(const Instruction instr) { const s16 imm = instr; const s64 offset = u64((s64)imm) << 2; const s64 address = blockPC + offset; - // code.mov(code.al, REG(byte, cop1.fcr31.compare)); + // code.mov(code.al, code.byte[code.rbp + REG_OFFSET(cop1.fcr31.compare)); // code.test(code.al, code.al); // branch(address, nz); } @@ -186,7 +186,7 @@ void JIT::blfc1(const Instruction instr) { const s16 imm = instr; const s64 offset = u64((s64)imm) << 2; const s64 address = blockPC + offset; - // code.mov(code.al, REG(byte, cop1.fcr31.compare)); + // code.mov(code.al, code.byte[code.rbp + REG_OFFSET(cop1.fcr31.compare)); // code.test(code.al, code.al); // branch_likely(address, nz); } @@ -194,13 +194,13 @@ void JIT::blfc1(const Instruction instr) { void JIT::BranchNotTaken() {} void JIT::BranchTaken(const s64 offs) { - code.mov(code.SCR2, REG(qword, pc)); + code.mov(code.SCR2, code.qword[PC_OFFSET()]); code.add(code.SCR2, offs); SetPC64(code.SCR2); } void JIT::BranchTaken(const Xbyak::Reg64 &offs) { - code.mov(code.SCR2, REG(qword, pc)); + code.mov(code.SCR2, code.qword[PC_OFFSET()]); code.add(code.SCR2, offs); SetPC64(code.SCR2); } @@ -245,7 +245,7 @@ void JIT::branch_likely_constant(const bool cond, const s64 offset) { code.jmp(not_taken); \ code.L(taken); \ BranchTaken(offs); \ - code.mov(JIT_VAR(byte, branch_taken), 1); \ + code.mov(code.byte[code.rbp + BRANCH_TAKEN_OFFSET()], 1); \ code.L(not_taken); \ } while(0) @@ -255,7 +255,7 @@ void JIT::branch_likely_constant(const bool cond, const s64 offset) { code.jmp(not_taken); \ code.L(taken); \ BranchAbsTaken(addr); \ - code.mov(JIT_VAR(byte, branch_taken), 1); \ + code.mov(code.byte[code.rbp + BRANCH_TAKEN_OFFSET()], 1); \ code.L(not_taken); \ } while(0) @@ -265,7 +265,7 @@ void JIT::branch_likely_constant(const bool cond, const s64 offset) { code.jmp(not_taken); \ code.L(taken); \ BranchTaken(offs); \ - code.mov(JIT_VAR(byte, branch_taken), 1); \ + code.mov(code.byte[code.rbp + BRANCH_TAKEN_OFFSET()], 1); \ code.jmp(end); \ code.L(not_taken); \ SetPC64(blockNextPC); \ @@ -1064,7 +1064,7 @@ void JIT::lhu(const Instruction instr) { code.mov(code.ARG4, reinterpret_cast(&paddr)); emitMemberFunctionCall(&Cop0::MapVAddr, ®s.cop0); - code.mov(code.ARG2, code.qword[code.ARG4]); + code.mov(code.ARG2, code.ptr[code.ARG4]); emitMemberFunctionCall(&Mem::Read, &mem); regs.Write(instr.rt(), code.rax); } @@ -1107,7 +1107,7 @@ void JIT::lw(const Instruction instr) { code.mov(code.ARG4, reinterpret_cast(&paddr)); emitMemberFunctionCall(&Cop0::MapVAddr, ®s.cop0); - code.mov(code.ARG2, code.qword[code.ARG4]); + code.mov(code.ARG2, code.ptr[code.ARG4]); emitMemberFunctionCall(&Mem::Read, &mem); regs.Write(instr.rt(), code.rax); } @@ -1124,7 +1124,7 @@ void JIT::mfhi(const Instruction instr) { if (regs.GetHIConstant()) { regs.Write(instr.rd(), regs.hi); } else { - code.mov(code.SCR1, REG(qword, hi)); + code.mov(code.SCR1, code.ptr[HI_OFFSET()]); regs.Write(instr.rd(), code.SCR1); } } @@ -1133,7 +1133,7 @@ void JIT::mflo(const Instruction instr) { if (regs.GetLOConstant()) { regs.Write(instr.rd(), regs.lo); } else { - code.mov(code.SCR1, REG(qword, lo)); + code.mov(code.SCR1, code.ptr[LO_OFFSET()]); regs.Write(instr.rd(), code.SCR1); } } @@ -1172,7 +1172,7 @@ void JIT::mthi(const Instruction instr) { regs.SetHIConstant(); } else { regs.Read(instr.rs(), code.SCR1); - code.mov(REG(qword, hi), code.SCR1); + code.mov(code.ptr[HI_OFFSET()], code.SCR1); regs.UnsetHIConstant(); } } @@ -1183,7 +1183,7 @@ void JIT::mtlo(const Instruction instr) { regs.SetLOConstant(); } else { regs.Read(instr.rs(), code.SCR1); - code.mov(REG(qword, lo), code.SCR1); + code.mov(code.ptr[LO_OFFSET()], code.SCR1); regs.UnsetLOConstant(); } } @@ -1395,7 +1395,7 @@ void JIT::sw(const Instruction instr) { code.mov(code.ARG4, reinterpret_cast(&physical)); emitMemberFunctionCall(&Cop0::MapVAddr, ®s.cop0); - code.mov(code.ARG2, code.qword[code.ARG4]); + code.mov(code.ARG2, code.ptr[code.ARG4]); regs.Read(instr.rt(), code.ARG3); emitMemberFunctionCall(&Mem::WriteJIT, &mem); @@ -1411,7 +1411,7 @@ void JIT::sw(const Instruction instr) { code.mov(code.ARG4, reinterpret_cast(&physical)); emitMemberFunctionCall(&Cop0::MapVAddr, ®s.cop0); - code.mov(code.ARG2, code.qword[code.ARG4]); + code.mov(code.ARG2, code.ptr[code.ARG4]); regs.Read(instr.rt(), code.ARG3); emitMemberFunctionCall(&Mem::WriteJIT, &mem); }