From c32498334013a5074b47e72001fdf52b524a486e Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 22 Jan 2013 08:04:01 -0800 Subject: [PATCH 1/2] Make the jit support bltzal and friends. Fixes problems with jit in games. Android changes completely untested. --- Core/MIPS/ARM/ArmCompBranch.cpp | 42 ++++++++++++++++++++------------- Core/MIPS/ARM/ArmJit.h | 2 +- Core/MIPS/MIPSInt.cpp | 8 +++---- Core/MIPS/MIPSTables.cpp | 8 +++---- Core/MIPS/x86/CompBranch.cpp | 28 +++++++++++++--------- Core/MIPS/x86/Jit.h | 2 +- 6 files changed, 53 insertions(+), 37 deletions(-) diff --git a/Core/MIPS/ARM/ArmCompBranch.cpp b/Core/MIPS/ARM/ArmCompBranch.cpp index 32d75392b6..8f4a09f968 100644 --- a/Core/MIPS/ARM/ArmCompBranch.cpp +++ b/Core/MIPS/ARM/ArmCompBranch.cpp @@ -116,7 +116,7 @@ void Jit::BranchRSRTComp(u32 op, ArmGen::CCFlags cc, bool likely) } -void Jit::BranchRSZeroComp(u32 op, ArmGen::CCFlags cc, bool likely) +void Jit::BranchRSZeroComp(u32 op, ArmGen::CCFlags cc, bool andLink, bool likely) { if (js.inDelaySlot) { ERROR_LOG(JIT, "Branch in delay slot at %08x", js.compilerPC); @@ -158,13 +158,19 @@ void Jit::BranchRSZeroComp(u32 op, ArmGen::CCFlags cc, bool likely) } js.inDelaySlot = false; - // Take the branch - WriteExit(targetAddr, 0); + // Take the branch + if (andLink) + { + ADD(R1, R10, MIPS_REG_RA * 4); // compute address of RA in ram + ARMABI_MOVI2R(R0, js.compilerPC + 8); + STR(R1, R0); + } + WriteExit(targetAddr, 0); - SetJumpTarget(ptr); - // Not taken - WriteExit(js.compilerPC + 8, 1); - js.compiling = false; + SetJumpTarget(ptr); + // Not taken + WriteExit(js.compilerPC + 8, 1); + js.compiling = false; } @@ -176,14 +182,14 @@ void Jit::Comp_RelBranch(u32 op) case 4: BranchRSRTComp(op, CC_NEQ, false); break;//beq case 5: BranchRSRTComp(op, CC_EQ, false); break;//bne - case 6: BranchRSZeroComp(op, CC_GT, false); break;//blez - case 7: BranchRSZeroComp(op, CC_LE, false); break;//bgtz + case 6: BranchRSZeroComp(op, CC_GT, false, false); break;//blez + case 7: BranchRSZeroComp(op, CC_LE, false, false); break;//bgtz case 20: BranchRSRTComp(op, CC_NEQ, true); break;//beql case 21: BranchRSRTComp(op, CC_EQ, true); break;//bnel - case 22: BranchRSZeroComp(op, CC_GT, true); break;//blezl - case 23: BranchRSZeroComp(op, CC_LE, true); break;//bgtzl + case 22: BranchRSZeroComp(op, CC_GT, false, true); break;//blezl + case 23: BranchRSZeroComp(op, CC_LE, false, true); break;//bgtzl default: _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); @@ -196,10 +202,14 @@ void Jit::Comp_RelBranchRI(u32 op) { switch ((op >> 16) & 0x1F) { - case 0: BranchRSZeroComp(op, CC_GE, false); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz - case 1: BranchRSZeroComp(op, CC_LT, false); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez - case 2: BranchRSZeroComp(op, CC_GE, true); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 8; break;//bltzl - case 3: BranchRSZeroComp(op, CC_LT, true); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 8; break;//bgezl + case 0: BranchRSZeroComp(op, CC_GE, false, false); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz + case 1: BranchRSZeroComp(op, CC_LT, false, false); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez + case 2: BranchRSZeroComp(op, CC_GE, false, true); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 8; break;//bltzl + case 3: BranchRSZeroComp(op, CC_LT, false, true); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 8; break;//bgezl + case 16: BranchRSZeroComp(op, CC_GE, true, false); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltzal + case 17: BranchRSZeroComp(op, CC_L, true, false); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgezal + case 18: BranchRSZeroComp(op, CC_GE, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzall + case 19: BranchRSZeroComp(op, CC_L, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else SkipLikely(); break;//bgezall default: _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); break; @@ -360,7 +370,7 @@ void Jit::Comp_Jump(u32 op) ADD(R1, R10, MIPS_REG_RA * 4); // compute address of RA in ram ARMABI_MOVI2R(R0, js.compilerPC + 8); STR(R1, R0); - WriteExit(targetAddr, 0); + WriteExit(targetAddr, 0); break; default: diff --git a/Core/MIPS/ARM/ArmJit.h b/Core/MIPS/ARM/ArmJit.h index a6a9116b32..072d40ae04 100644 --- a/Core/MIPS/ARM/ArmJit.h +++ b/Core/MIPS/ARM/ArmJit.h @@ -107,7 +107,7 @@ private: // Utility compilation functions void BranchFPFlag(u32 op, ArmGen::CCFlags cc, bool likely); void BranchVFPUFlag(u32 op, ArmGen::CCFlags cc, bool likely); - void BranchRSZeroComp(u32 op, ArmGen::CCFlags cc, bool likely); + void BranchRSZeroComp(u32 op, ArmGen::CCFlags cc, bool andLink, bool likely); void BranchRSRTComp(u32 op, ArmGen::CCFlags cc, bool likely); // Utilities to reduce duplicated code diff --git a/Core/MIPS/MIPSInt.cpp b/Core/MIPS/MIPSInt.cpp index 1056f3f3c6..51870dd9a6 100644 --- a/Core/MIPS/MIPSInt.cpp +++ b/Core/MIPS/MIPSInt.cpp @@ -206,10 +206,10 @@ namespace MIPSInt case 1: if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez case 2: if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzl case 3: if ((s32)R(rs) >= 0) DelayBranchTo(addr); else SkipLikely(); break;//bgezl - case 16: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz - case 17: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez - case 18: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzl - case 19: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else SkipLikely(); break;//bgezl + case 16: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltzal + case 17: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgezal + case 18: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzall + case 19: R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else SkipLikely(); break;//bgezall default: _dbg_assert_msg_(CPU,0,"Trying to interpret instruction that can't be interpreted"); break; diff --git a/Core/MIPS/MIPSTables.cpp b/Core/MIPS/MIPSTables.cpp index c44c1874e3..f815365c2e 100644 --- a/Core/MIPS/MIPSTables.cpp +++ b/Core/MIPS/MIPSTables.cpp @@ -345,10 +345,10 @@ const MIPSInstruction tableRegImm[32] = INSTR("tnei", &Jit::Comp_Generic, Dis_Generic, 0, 0), {-2}, - INSTR("bltzal", &Jit::Comp_Generic, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT), - INSTR("bgezal", &Jit::Comp_Generic, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT), - INSTR("bltzall", &Jit::Comp_Generic, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT|LIKELY), //L = likely - INSTR("bgezall", &Jit::Comp_Generic, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT|LIKELY), + INSTR("bltzal", &Jit::Comp_RelBranchRI, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT), + INSTR("bgezal", &Jit::Comp_RelBranchRI, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT), + INSTR("bltzall", &Jit::Comp_RelBranchRI, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT|LIKELY), //L = likely + INSTR("bgezall", &Jit::Comp_RelBranchRI, Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_RS|OUT_RA|DELAYSLOT|LIKELY), {-2}, {-2}, {-2}, diff --git a/Core/MIPS/x86/CompBranch.cpp b/Core/MIPS/x86/CompBranch.cpp index 5efb27c3eb..7ac0fd4542 100644 --- a/Core/MIPS/x86/CompBranch.cpp +++ b/Core/MIPS/x86/CompBranch.cpp @@ -178,7 +178,7 @@ void Jit::BranchRSRTComp(u32 op, Gen::CCFlags cc, bool likely) js.compiling = false; } -void Jit::BranchRSZeroComp(u32 op, Gen::CCFlags cc, bool likely) +void Jit::BranchRSZeroComp(u32 op, Gen::CCFlags cc, bool andLink, bool likely) { CONDITIONAL_LOG; if (js.inDelaySlot) { @@ -215,6 +215,8 @@ void Jit::BranchRSZeroComp(u32 op, Gen::CCFlags cc, bool likely) } // Take the branch + if (andLink) + MOV(32, M(&mips_->r[MIPS_REG_RA]), Imm32(js.compilerPC + 8)); CONDITIONAL_LOG_EXIT(targetAddr); WriteExit(targetAddr, 0); @@ -232,16 +234,16 @@ void Jit::Comp_RelBranch(u32 op) switch (op>>26) { case 4: BranchRSRTComp(op, CC_NZ, false); break;//beq - case 5: BranchRSRTComp(op, CC_Z, false); break;//bne + case 5: BranchRSRTComp(op, CC_Z, false); break;//bne - case 6: BranchRSZeroComp(op, CC_G, false); break;//blez - case 7: BranchRSZeroComp(op, CC_LE, false); break;//bgtz + case 6: BranchRSZeroComp(op, CC_G, false, false); break;//blez + case 7: BranchRSZeroComp(op, CC_LE, false, false); break;//bgtz case 20: BranchRSRTComp(op, CC_NZ, true); break;//beql - case 21: BranchRSRTComp(op, CC_Z, true); break;//bnel + case 21: BranchRSRTComp(op, CC_Z, true); break;//bnel - case 22: BranchRSZeroComp(op, CC_G, true); break;//blezl - case 23: BranchRSZeroComp(op, CC_LE, true); break;//bgtzl + case 22: BranchRSZeroComp(op, CC_G, false, true); break;//blezl + case 23: BranchRSZeroComp(op, CC_LE, false, true); break;//bgtzl default: _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); @@ -254,10 +256,14 @@ void Jit::Comp_RelBranchRI(u32 op) { switch ((op >> 16) & 0x1F) { - case 0: BranchRSZeroComp(op, CC_GE, false); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz - case 1: BranchRSZeroComp(op, CC_L, false); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez - case 2: BranchRSZeroComp(op, CC_GE, true); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 8; break;//bltzl - case 3: BranchRSZeroComp(op, CC_L, true); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 8; break;//bgezl + case 0: BranchRSZeroComp(op, CC_GE, false, false); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz + case 1: BranchRSZeroComp(op, CC_L, false, false); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez + case 2: BranchRSZeroComp(op, CC_GE, false, true); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 8; break;//bltzl + case 3: BranchRSZeroComp(op, CC_L, false, true); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 8; break;//bgezl + case 16: BranchRSZeroComp(op, CC_GE, true, false); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltzal + case 17: BranchRSZeroComp(op, CC_L, true, false); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgezal + case 18: BranchRSZeroComp(op, CC_GE, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzall + case 19: BranchRSZeroComp(op, CC_L, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else SkipLikely(); break;//bgezall default: _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); break; diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 2dd9c40ab5..aeff3fd274 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -114,7 +114,7 @@ private: // Utility compilation functions void BranchFPFlag(u32 op, Gen::CCFlags cc, bool likely); void BranchVFPUFlag(u32 op, Gen::CCFlags cc, bool likely); - void BranchRSZeroComp(u32 op, Gen::CCFlags cc, bool likely); + void BranchRSZeroComp(u32 op, Gen::CCFlags cc, bool andLink, bool likely); void BranchRSRTComp(u32 op, Gen::CCFlags cc, bool likely); void BranchLog(u32 op); void BranchLogExit(u32 op, u32 dest, bool useEAX); From 0e33923844a9a6a97e6e16a49dc278b9b816d2c4 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 22 Jan 2013 08:11:37 -0800 Subject: [PATCH 2/2] Belt and suspenders check for branch ops. --- Core/MIPS/x86/Jit.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index 6dfdef1c8a..ba5d159faa 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -232,6 +232,8 @@ void Jit::Comp_Generic(u32 op) { FlushAll(); MIPSInterpretFunc func = MIPSGetInterpretFunc(op); + _dbg_assert_msg_(JIT, (MIPSGetInfo(op) & DELAYSLOT) == 0, "Cannot use interpreter for branch ops."); + if (func) { MOV(32, M(&mips_->pc), Imm32(js.compilerPC));