diff --git a/Core/Config.cpp b/Core/Config.cpp index a5b4bb79d5..6faa97a075 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -354,6 +354,7 @@ static bool DefaultSasThread() { static ConfigSetting cpuSettings[] = { ReportedConfigSetting("Jit", &g_Config.bJit, &DefaultJit, true, true), + ReportedConfigSetting("CPUCore", &g_Config.bJit, &DefaultJit, true, true), ReportedConfigSetting("SeparateCPUThread", &g_Config.bSeparateCPUThread, false, true, true), ReportedConfigSetting("SeparateSASThread", &g_Config.bSeparateSASThread, &DefaultSasThread, true, true), ReportedConfigSetting("SeparateIOThread", &g_Config.bSeparateIOThread, true, true, true), diff --git a/Core/Config.h b/Core/Config.h index b2cfc1351e..825091bd0c 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -120,6 +120,7 @@ public: bool bIgnoreBadMemAccess; bool bFastMemory; bool bJit; + int iCpuCore; bool bCheckForNewVersion; bool bForceLagSync; bool bFuncReplacements; diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 83552556d0..af4b459fb1 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -181,6 +181,15 @@ + + + + + + + + + @@ -507,6 +516,9 @@ + + + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index d4ba376f94..99af2a2696 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -67,6 +67,9 @@ {67687dba-8313-4442-b4eb-4be8c4867b65} + + {119ac973-e457-4025-9e1e-4fb34022ae23} + @@ -634,6 +637,33 @@ Core + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + @@ -1179,6 +1209,15 @@ Core + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + diff --git a/Core/CoreParameter.h b/Core/CoreParameter.h index ac33c91f58..1517b50c03 100644 --- a/Core/CoreParameter.h +++ b/Core/CoreParameter.h @@ -24,6 +24,7 @@ enum CPUCore { CPU_INTERPRETER, CPU_JIT, + CPU_IRJIT, }; enum GPUCore { diff --git a/Core/MIPS/IR/IRAsm.cpp b/Core/MIPS/IR/IRAsm.cpp new file mode 100644 index 0000000000..f5d9c7ad31 --- /dev/null +++ b/Core/MIPS/IR/IRAsm.cpp @@ -0,0 +1,28 @@ +// Copyright (c) 2015- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include "base/logging.h" + +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/System.h" +#include "Core/CoreTiming.h" +#include "Common/MemoryUtil.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/JitCommon/JitCommon.h" + + diff --git a/Core/MIPS/IR/IRCompALU.cpp b/Core/MIPS/IR/IRCompALU.cpp new file mode 100644 index 0000000000..67059e371e --- /dev/null +++ b/Core/MIPS/IR/IRCompALU.cpp @@ -0,0 +1,420 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include + +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Common/CPUDetect.h" + +using namespace MIPSAnalyst; + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +namespace MIPSComp { + +void IRJit::CompImmLogic(MIPSGPReg rs, MIPSGPReg rt, u32 uimm, IROp OP) { + if (gpr.IsImm(rs)) { + switch (OP) { + case IROp::AddConst: gpr.SetImm(rt, rs + uimm); break; + case IROp::SubConst: gpr.SetImm(rt, rs - uimm); break; + case IROp::AndConst: gpr.SetImm(rt, rs & uimm); break; + case IROp::OrConst: gpr.SetImm(rt, rs | uimm); break; + case IROp::XorConst: gpr.SetImm(rt, rs ^ uimm); break; + } + } else { + gpr.MapDirtyIn(rt, rs); + ir.Write(OP, rt, ir.AddConstant(uimm)); + } +} + +void IRJit::Comp_IType(MIPSOpcode op) { + CONDITIONAL_DISABLE; + s32 simm = (s32)(s16)(op & 0xFFFF); // sign extension + u32 uimm = op & 0xFFFF; + u32 suimm = (u32)(s32)simm; + + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + + // noop, won't write to ZERO. + if (rt == 0) + return; + + switch (op >> 26) { + case 8: // same as addiu? + case 9: // R(rt) = R(rs) + simm; break; //addiu + // Special-case for small adjustments of pointerified registers. Commonly for SP but happens for others. + if (simm >= 0) { + CompImmLogic(rs, rt, simm, IROp::AddConst); + } else if (simm < 0) { + CompImmLogic(rs, rt, -simm, IROp::SubConst); + } + break; + + case 12: CompImmLogic(rs, rt, uimm, IROp::AndConst); break; + case 13: CompImmLogic(rs, rt, uimm, IROp::OrConst); break; + case 14: CompImmLogic(rs, rt, uimm, IROp::XorConst); break; + + case 10: // R(rt) = (s32)R(rs) < simm; break; //slti + if (gpr.IsImm(rs)) { + gpr.SetImm(rt, (s32)gpr.GetImm(rs) < simm ? 1 : 0); + break; + } + gpr.MapDirtyIn(rt, rs); + // Grab the sign bit (< 0) as 1/0. Slightly faster than a shift. + ir.Write(IROp::Slt, rt, rs, ir.AddConstant(simm)); + break; + + case 11: // R(rt) = R(rs) < suimm; break; //sltiu + if (gpr.IsImm(rs)) { + gpr.SetImm(rt, gpr.GetImm(rs) < suimm ? 1 : 0); + break; + } + gpr.MapDirtyIn(rt, rs); + ir.Write(IROp::SltU, rt, rs, ir.AddConstant(suimm)); + break; + + case 15: // R(rt) = uimm << 16; //lui + gpr.SetImm(rt, uimm << 16); + break; + + default: + Comp_Generic(op); + break; + } +} + +void IRJit::Comp_RType2(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + + // Don't change $zr. + if (rd == 0) + return; + + switch (op & 63) { + case 22: //clz + gpr.MapDirtyIn(rd, rs); + ir.Write(IROp::Clz, rd, rs); + break; + case 23: //clo + gpr.MapDirtyIn(rd, rs); + ir.Write(IROp::Not, IRTEMP_0, rs); + ir.Write(IROp::Clz, rd, IRTEMP_0); + break; + default: + DISABLE; + } +} + +void IRJit::CompType3(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt, IROp op, IROp constOp, bool symmetric) { + if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + switch (op) { + case IROp::Add: gpr.SetImm(rd, gpr.GetImm(rs) + gpr.GetImm(rt)); break; + case IROp::Sub: gpr.SetImm(rd, gpr.GetImm(rs) - gpr.GetImm(rt)); break; + case IROp::And: gpr.SetImm(rd, gpr.GetImm(rs) & gpr.GetImm(rt)); break; + case IROp::Or: gpr.SetImm(rd, gpr.GetImm(rs) | gpr.GetImm(rt)); break; + case IROp::Xor: gpr.SetImm(rd, gpr.GetImm(rs) ^ gpr.GetImm(rt)); break; + } + return; + } + + if (gpr.IsImm(rt) || (gpr.IsImm(rs) && symmetric)) { + MIPSGPReg lhs = gpr.IsImm(rs) ? rt : rs; + MIPSGPReg rhs = gpr.IsImm(rs) ? rs : rt; + u32 rhsImm = gpr.GetImm(rhs); + gpr.MapDirtyIn(rd, lhs); + ir.Write(constOp, rd, lhs, ir.AddConstant(rhsImm)); + // If rd is rhs, we may have lost it in the MapDirtyIn(). lhs was kept. + // This means the rhsImm value was never flushed to rhs, and would be garbage. + if (rd == rhs) { + // Luckily, it was just an imm. + gpr.SetImm(rhs, rhsImm); + } + } + + // Can't do the RSB optimization on ARM64 - no RSB! + + // Generic solution. If it's an imm, better to flush at this point. + gpr.MapDirtyInIn(rd, rs, rt); + ir.Write(op, rd, rs, rt); +} + +void IRJit::Comp_RType3(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + + // noop, won't write to ZERO. + if (rd == 0) + return; + + switch (op & 63) { + case 10: //if (!R(rt)) R(rd) = R(rs); break; //movz + gpr.MapDirtyInIn(rd, rt, rs); + ir.Write(IROp::MovZ, rd, rt, rs); + break; + case 11:// if (R(rt)) R(rd) = R(rs); break; //movn + gpr.MapDirtyInIn(rd, rt, rs); + ir.Write(IROp::MovNZ, rd, rt, rs); + break; + + case 32: //R(rd) = R(rs) + R(rt); break; //add + case 33: //R(rd) = R(rs) + R(rt); break; //addu + CompType3(rd, rs, rt, IROp::Add, IROp::AddConst, true); + break; + + case 34: //R(rd) = R(rs) - R(rt); break; //sub + case 35: //R(rd) = R(rs) - R(rt); break; //subu + CompType3(rd, rs, rt, IROp::Sub, IROp::SubConst, false); + break; + + case 36: //R(rd) = R(rs) & R(rt); break; //and + CompType3(rd, rs, rt, IROp::And, IROp::AndConst, true); + break; + case 37: //R(rd) = R(rs) | R(rt); break; //or + CompType3(rd, rs, rt, IROp::Or, IROp::OrConst, true); + break; + case 38: //R(rd) = R(rs) ^ R(rt); break; //xor/eor + CompType3(rd, rs, rt, IROp::Xor, IROp::XorConst, true); + break; + + case 39: // R(rd) = ~(R(rs) | R(rt)); break; //nor + if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + gpr.SetImm(rd, ~(gpr.GetImm(rs) | gpr.GetImm(rt))); + } + + ir.Write(IROp::Or, IRTEMP_0, rs, rt); + ir.Write(IROp::Not, rd, IRTEMP_0); + break; + + case 42: //R(rd) = (int)R(rs) < (int)R(rt); break; //slt + if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + gpr.SetImm(rd, (s32)gpr.GetImm(rs) < (s32)gpr.GetImm(rt)); + } else { + gpr.MapDirtyInIn(rd, rt, rs); + ir.Write(IROp::Slt, rd, rs, rt); + } + break; + + case 43: //R(rd) = R(rs) < R(rt); break; //sltu + if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + gpr.SetImm(rd, gpr.GetImm(rs) < gpr.GetImm(rt)); + } else { + gpr.MapDirtyInIn(rd, rt, rs); + ir.Write(IROp::SltU, rd, rs, rt); + } + break; + + case 44: //R(rd) = max(R(rs), R(rt); break; //max + if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + gpr.SetImm(rd, std::max(gpr.GetImm(rs), gpr.GetImm(rt))); + break; + } + gpr.MapDirtyInIn(rd, rs, rt); + ir.Write(IROp::Max, rd, rs, rt); + break; + + case 45: //R(rd) = min(R(rs), R(rt)); break; //min + if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + gpr.SetImm(rd, std::min(gpr.GetImm(rs), gpr.GetImm(rt))); + break; + } + gpr.MapDirtyInIn(rd, rs, rt); + ir.Write(IROp::Min, rd, rs, rt); + break; + + default: + Comp_Generic(op); + break; + } +} + +void IRJit::CompShiftImm(MIPSOpcode op, IROp shiftOpConst, int sa) { + MIPSGPReg rd = _RD; + MIPSGPReg rt = _RT; + if (gpr.IsImm(rt)) { + switch (shiftOpConst) { + case IROp::ShlImm: + gpr.SetImm(rd, gpr.GetImm(rt) << sa); + break; + case IROp::ShrImm: + gpr.SetImm(rd, gpr.GetImm(rt) >> sa); + break; + case IROp::SarImm: + gpr.SetImm(rd, (int)gpr.GetImm(rt) >> sa); + break; + case IROp::RorImm: + gpr.SetImm(rd, (gpr.GetImm(rt) >> sa) | (gpr.GetImm(rt) << (32 - sa))); + break; + default: + DISABLE; + } + } else { + gpr.MapDirtyIn(rd, rt); + ir.Write(shiftOpConst, rd, rt, sa); + } +} + +void IRJit::CompShiftVar(MIPSOpcode op, IROp shiftOp, IROp shiftOpConst) { + MIPSGPReg rd = _RD; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + if (gpr.IsImm(rs)) { + int sa = gpr.GetImm(rs) & 0x1F; + CompShiftImm(op, shiftOpConst, sa); + return; + } + gpr.MapDirtyInIn(rd, rs, rt); + // Not sure if ARM64 wraps like this so let's do it for it. (TODO: According to the ARM ARM, it will indeed mask for us so this is not necessary) + // ANDI2R(SCRATCH1, gpr.R(rs), 0x1F, INVALID_REG); + ir.Write(IROp::AndConst, IRTEMP_0, rs, ir.AddConstant(31)); + ir.Write(shiftOp, rd, rt, IRTEMP_0); +} + +void IRJit::Comp_ShiftType(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + int fd = _FD; + int sa = _SA; + + // noop, won't write to ZERO. + if (rd == 0) + return; + + // WARNING : ROTR + switch (op & 0x3f) { + case 0: CompShiftImm(op, IROp::Shl, sa); break; //sll + case 2: CompShiftImm(op, rs == 1 ? IROp::Ror : IROp::Shr, sa); break; //srl + case 3: CompShiftImm(op, IROp::Sar, sa); break; //sra + case 4: CompShiftVar(op, IROp::Shl, IROp::ShlImm); break; //sllv + case 6: CompShiftVar(op, (fd == 1 ? IROp::Ror : IROp::Shr), (fd == 1 ? IROp::RorImm : IROp::ShrImm)); break; //srlv + case 7: CompShiftVar(op, IROp::Sar, IROp::SarImm); break; //srav + default: + DISABLE; + break; + } +} + +void IRJit::Comp_Special3(MIPSOpcode op) { + DISABLE; +} + +void IRJit::Comp_Allegrex(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rt = _RT; + MIPSGPReg rd = _RD; + // Don't change $zr. + if (rd == 0) + return; + + switch ((op >> 6) & 31) { + case 16: // seb // R(rd) = (u32)(s32)(s8)(u8)R(rt); + if (gpr.IsImm(rt)) { + gpr.SetImm(rd, (s32)(s8)(u8)gpr.GetImm(rt)); + return; + } + gpr.MapDirtyIn(rd, rt); + ir.Write(IROp::Ext8to32, rd, rt); + break; + + case 24: // seh + if (gpr.IsImm(rt)) { + gpr.SetImm(rd, (s32)(s16)(u16)gpr.GetImm(rt)); + return; + } + gpr.MapDirtyIn(rd, rt); + ir.Write(IROp::Ext16to32, rd, rt); + break; + + case 20: //bitrev + default: + Comp_Generic(op); + return; + } +} + +void IRJit::Comp_Allegrex2(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rt = _RT; + MIPSGPReg rd = _RD; + // Don't change $zr. + if (rd == 0) + return; + + switch (op & 0x3ff) { + case 0xA0: //wsbh + if (gpr.IsImm(rt)) { + gpr.SetImm(rd, ((gpr.GetImm(rt) & 0xFF00FF00) >> 8) | ((gpr.GetImm(rt) & 0x00FF00FF) << 8)); + } else { + gpr.MapDirtyIn(rd, rt); + ir.Write(IROp::BSwap16, rd, rt); + } + break; + case 0xE0: //wsbw + if (gpr.IsImm(rt)) { + gpr.SetImm(rd, swap32(gpr.GetImm(rt))); + } else { + gpr.MapDirtyIn(rd, rt); + ir.Write(IROp::BSwap16, rd, rt); + } + break; + default: + Comp_Generic(op); + break; + } +} + +void IRJit::Comp_MulDivType(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + + // Note that in all cases below, LO is actually mapped to HI:LO. + // That is, the host reg is 64 bits and has HI at the top. + // HI is not mappable. + + DISABLE; +} + +} diff --git a/Core/MIPS/IR/IRCompBranch.cpp b/Core/MIPS/IR/IRCompBranch.cpp new file mode 100644 index 0000000000..16c7245b82 --- /dev/null +++ b/Core/MIPS/IR/IRCompBranch.cpp @@ -0,0 +1,363 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include "profiler/profiler.h" + +#include "Core/Reporting.h" +#include "Core/Config.h" +#include "Core/MemMap.h" +#include "Core/HLE/HLE.h" +#include "Core/HLE/HLETables.h" + +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSAnalyst.h" +#include "Core/MIPS/MIPSTables.h" + +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/JitCommon/JitBlockCache.h" + +#include "Common/Arm64Emitter.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +#define LOOPOPTIMIZATION 0 + +using namespace MIPSAnalyst; + +namespace MIPSComp +{ + using namespace Arm64Gen; + +void IRJit::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) +{ + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in RSRTComp delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rt, rs); + + ir.Write(IROp::Downcount, 0, js.downcountAmount & 0xFF, js.downcountAmount >> 8); + + MIPSGPReg lhs = rs; + MIPSGPReg rhs = rt; + if (!delaySlotIsNice) { + ir.Write(IROp::Mov, IRTEMP_0, rs); + ir.Write(IROp::Mov, IRTEMP_1, rt); + lhs = (MIPSGPReg)IRTEMP_0; + rhs = (MIPSGPReg)IRTEMP_1; + } + + if (!likely) + CompileDelaySlot(); + + gpr.MapInIn(lhs, rhs); + ir.Write(ComparisonToExit(cc), ir.AddConstant(GetCompilerPC() + 8), lhs, rhs); + // This makes the block "impure" :( + if (likely) + CompileDelaySlot(); + + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + + js.compiling = false; +} + +void IRJit::BranchRSZeroComp(MIPSOpcode op, IRComparison cc, bool andLink, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in RSZeroComp delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + MIPSGPReg rs = _RS; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rs); + + if (!likely && delaySlotIsNice) + CompileDelaySlot(); + int lhs = rs; + gpr.MapIn(rs); + if (!delaySlotIsNice) { + ir.Write(IROp::Mov, IRTEMP_0, rs); + lhs = IRTEMP_0; + } + ir.Write(ComparisonToExit(cc), ir.AddConstant(GetCompilerPC() + 8), lhs); + if (likely) { + CompileDelaySlot(); + } + // Taken + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + js.compiling = false; +} + +void IRJit::Comp_RelBranch(MIPSOpcode op) { + // The CC flags here should be opposite of the actual branch becuase they skip the branching action. + switch (op >> 26) { + case 4: BranchRSRTComp(op, IRComparison::NotEqual, false); break;//beq + case 5: BranchRSRTComp(op, IRComparison::Equal, false); break;//bne + + case 6: BranchRSZeroComp(op, IRComparison::Greater, false, false); break;//blez + case 7: BranchRSZeroComp(op, IRComparison::LessEqual, false, false); break;//bgtz + + case 20: BranchRSRTComp(op, IRComparison::NotEqual, true); break;//beql + case 21: BranchRSRTComp(op, IRComparison::Equal, true); break;//bnel + + case 22: BranchRSZeroComp(op, IRComparison::Greater, false, true); break;//blezl + case 23: BranchRSZeroComp(op, IRComparison::LessEqual, false, true); break;//bgtzl + + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } +} + +void IRJit::Comp_RelBranchRI(MIPSOpcode op) { + switch ((op >> 16) & 0x1F) { + case 0: BranchRSZeroComp(op, IRComparison::GreaterEqual, false, false); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz + case 1: BranchRSZeroComp(op, IRComparison::Less, false, false); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez + case 2: BranchRSZeroComp(op, IRComparison::GreaterEqual, false, true); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 8; break;//bltzl + case 3: BranchRSZeroComp(op, IRComparison::Less, false, true); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 8; break;//bgezl + case 16: BranchRSZeroComp(op, IRComparison::GreaterEqual, 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, IRComparison::Less, 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, IRComparison::GreaterEqual, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzall + case 19: BranchRSZeroComp(op, IRComparison::Less, 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; + } +} + +// If likely is set, discard the branch slot if NOT taken. +void IRJit::BranchFPFlag(MIPSOpcode op, IRComparison cc, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in FPFlag delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + ir.Write(IROp::FpCondToReg, IRTEMP_0); + if (!likely) + CompileDelaySlot(); + + FlushAll(); + // Not taken + ir.Write(ComparisonToExit(cc), ir.AddConstant(GetCompilerPC() + 8), IRTEMP_0, 0); + // Taken + if (likely) + CompileDelaySlot(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + js.compiling = false; +} + +void IRJit::Comp_FPUBranch(MIPSOpcode op) { + switch((op >> 16) & 0x1f) { + case 0: BranchFPFlag(op, IRComparison::NotEqual, false); break; // bc1f + case 1: BranchFPFlag(op, IRComparison::Equal, false); break; // bc1t + case 2: BranchFPFlag(op, IRComparison::NotEqual, true); break; // bc1fl + case 3: BranchFPFlag(op, IRComparison::Equal, true); break; // bc1tl + default: + _dbg_assert_msg_(CPU, 0, "Trying to interpret instruction that can't be interpreted"); + break; + } +} + +// If likely is set, discard the branch slot if NOT taken. +void IRJit::BranchVFPUFlag(MIPSOpcode op, IRComparison cc, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in VFPU delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + + ir.Write(IROp::VfpCondToReg, IRTEMP_0); + + // Sometimes there's a VFPU branch in a delay slot (Disgaea 2: Dark Hero Days, Zettai Hero Project, La Pucelle) + // The behavior is undefined - the CPU may take the second branch even if the first one passes. + // However, it does consistently try each branch, which these games seem to expect. + bool delaySlotIsBranch = MIPSCodeUtils::IsVFPUBranch(delaySlotOp); + if (!likely) + CompileDelaySlot(); + + if (delaySlotIsBranch && (signed short)(delaySlotOp & 0xFFFF) != (signed short)(op & 0xFFFF) - 1) + ERROR_LOG_REPORT(JIT, "VFPU branch in VFPU delay slot at %08x with different target", GetCompilerPC()); + + int imm3 = (op >> 18) & 7; + + u32 notTakenTarget = GetCompilerPC() + (delaySlotIsBranch ? 4 : 8); + + ir.Write(IROp::AndConst, IRTEMP_0, IRTEMP_0, ir.AddConstant(imm3)); + ir.Write(ComparisonToExit(cc), ir.AddConstant(notTakenTarget), IRTEMP_0, 0); + + if (likely) + CompileDelaySlot(); + + // Taken + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + js.compiling = false; +} + +void IRJit::Comp_VBranch(MIPSOpcode op) { + switch ((op >> 16) & 3) { + case 0: BranchVFPUFlag(op, IRComparison::NotEqual, false); break; // bvf + case 1: BranchVFPUFlag(op, IRComparison::Equal, false); break; // bvt + case 2: BranchVFPUFlag(op, IRComparison::NotEqual, true); break; // bvfl + case 3: BranchVFPUFlag(op, IRComparison::Equal, true); break; // bvtl + } +} + +void IRJit::Comp_Jump(MIPSOpcode op) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in Jump delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + + u32 off = _IMM26 << 2; + u32 targetAddr = (GetCompilerPC() & 0xF0000000) | off; + + // Might be a stubbed address or something? + if (!Memory::IsValidAddress(targetAddr)) { + if (js.nextExit == 0) { + ERROR_LOG_REPORT(JIT, "Jump to invalid address: %08x", targetAddr); + } else { + js.compiling = false; + } + // TODO: Mark this block dirty or something? May be indication it will be changed by imports. + return; + } + + switch (op >> 26) { + case 2: //j + CompileDelaySlot(); + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + break; + + case 3: //jal + if (ReplaceJalTo(targetAddr)) + return; + gpr.SetImm(MIPS_REG_RA, GetCompilerPC() + 8); + CompileDelaySlot(); + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + break; + + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } + js.compiling = false; +} + +void IRJit::Comp_JumpReg(MIPSOpcode op) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in JumpReg delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + bool andLink = (op & 0x3f) == 9 && rd != MIPS_REG_ZERO; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rs); + if (andLink && rs == rd) + delaySlotIsNice = false; + + int destReg; + if (IsSyscall(delaySlotOp)) { + gpr.MapDirty(rs); + ir.Write(IROp::SetPC, 0, rs); + if (andLink) + gpr.SetImm(rd, GetCompilerPC() + 8); + CompileDelaySlot(); + return; // Syscall (delay slot) wrote exit code. + } else if (delaySlotIsNice) { + if (andLink) + gpr.SetImm(rd, GetCompilerPC() + 8); + CompileDelaySlot(); + gpr.MapDirty(rs); + destReg = rs; // Safe because FlushAll doesn't change any regs + FlushAll(); + } else { + // Bad delay slot. + gpr.MapDirty(rs); + ir.Write(IROp::Mov, IRTEMP_0, rs); + destReg = IRTEMP_0; + if (andLink) + gpr.SetImm(rd, GetCompilerPC() + 8); + CompileDelaySlot(); + FlushAll(); + } + + switch (op & 0x3f) + { + case 8: //jr + break; + case 9: //jalr + break; + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } + + ir.Write(IROp::ExitToReg, ir.AddConstant(js.downcountAmount), rs, 0); + js.compiling = false; +} + +void IRJit::Comp_Syscall(MIPSOpcode op) { + // If we're in a delay slot, this is off by one. + const int offset = js.inDelaySlot ? -1 : 0; + RestoreRoundingMode(); + js.downcountAmount = -offset; + + FlushAll(); + + ir.Write(IROp::Syscall, 0, ir.AddConstant(op.encoding)); + + ApplyRoundingMode(); + js.compiling = false; +} + +void IRJit::Comp_Break(MIPSOpcode op) +{ + Comp_Generic(op); + js.compiling = false; +} + +} // namespace Mipscomp diff --git a/Core/MIPS/IR/IRCompFPU.cpp b/Core/MIPS/IR/IRCompFPU.cpp new file mode 100644 index 0000000000..00a8ec6399 --- /dev/null +++ b/Core/MIPS/IR/IRCompFPU.cpp @@ -0,0 +1,226 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include "Core/Config.h" +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSTables.h" + +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Common/CPUDetect.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + + +// FPCR interesting bits: +// 24: FZ (flush-to-zero) +// 23:22: RMode (0 = nearest, 1 = +inf, 2 = -inf, 3 = zero) +// not much else is interesting for us, but should be preserved. +// To access: MRS Xt, FPCR ; MSR FPCR, Xt + + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +namespace MIPSComp { + +void IRJit::Comp_FPU3op(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int ft = _FT; + int fs = _FS; + int fd = _FD; + + switch (op & 0x3f) { + case 0: ir.Write(IROp::FAdd, fd, fs, ft); break; //F(fd) = F(fs) + F(ft); //add + case 1: ir.Write(IROp::FSub, fd, fs, ft); break; //F(fd) = F(fs) - F(ft); //sub + case 2: ir.Write(IROp::FMul, fd, fs, ft); break; //F(fd) = F(fs) * F(ft); //mul + case 3: ir.Write(IROp::FDiv, fd, fs, ft); break; //F(fd) = F(fs) / F(ft); //div + default: + DISABLE; + return; + } +} + +void IRJit::Comp_FPULS(MIPSOpcode op) { + DISABLE; +} + +void IRJit::Comp_FPUComp(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int opc = op & 0xF; + if (opc >= 8) opc -= 8; // alias + if (opc == 0) { // f, sf (signalling false) + gpr.SetImm(MIPS_REG_FPCOND, 0); + return; + } + + int fs = _FS; + int ft = _FT; + + IROp irOp; + switch (opc) { + case 1: // un, ngle (unordered) + irOp = IROp::FCmpUnordered; + break; + case 2: // eq, seq (equal, ordered) + irOp = IROp::FCmpEqual; + break; + case 3: // ueq, ngl (equal, unordered) + irOp = IROp::FCmpEqualUnordered; + return; + case 4: // olt, lt (less than, ordered) + irOp = IROp::FCmpLessOrdered; + break; + case 5: // ult, nge (less than, unordered) + irOp = IROp::FCmpLessUnordered; + break; + case 6: // ole, le (less equal, ordered) + irOp = IROp::FCmpLessEqualOrdered; + break; + case 7: // ule, ngt (less equal, unordered) + irOp = IROp::FCmpLessEqualUnordered; + break; + default: + Comp_Generic(op); + return; + } + ir.Write(irOp, fs, ft); +} + +void IRJit::Comp_FPU2op(MIPSOpcode op) { + CONDITIONAL_DISABLE; + int fs = _FS; + int fd = _FD; + + switch (op & 0x3f) { + case 4: //F(fd) = sqrtf(F(fs)); break; //sqrt + ir.Write(IROp::FSqrt, fd, fs); + break; + case 5: //F(fd) = fabsf(F(fs)); break; //abs + ir.Write(IROp::FAbs, fd, fs); + break; + case 6: //F(fd) = F(fs); break; //mov + ir.Write(IROp::FMov, fd, fs); + break; + case 7: //F(fd) = -F(fs); break; //neg + ir.Write(IROp::FNeg, fd, fs); + break; + + case 12: //FsI(fd) = (int)floorf(F(fs)+0.5f); break; //round.w.s + { + ir.Write(IROp::FRound, fd, fs); + break; + } + + case 13: //FsI(fd) = Rto0(F(fs))); break; //trunc.w.s + { + ir.Write(IROp::FTrunc, fd, fs); + break; + } + + case 14://FsI(fd) = (int)ceilf (F(fs)); break; //ceil.w.s + { + ir.Write(IROp::FCeil, fd, fs); + break; + } + case 15: //FsI(fd) = (int)floorf(F(fs)); break; //floor.w.s + { + ir.Write(IROp::FFloor, fd, fs); + break; + } + + case 32: //F(fd) = (float)FsI(fs); break; //cvt.s.w + ir.Write(IROp::FCvtSW, fd, fs); + break; + + case 36: //FsI(fd) = (int) F(fs); break; //cvt.w.s + ir.Write(IROp::FCvtWS, fd, fs); + break; + + default: + DISABLE; + } +} + +void IRJit::Comp_mxc1(MIPSOpcode op) +{ + CONDITIONAL_DISABLE; + + int fs = _FS; + MIPSGPReg rt = _RT; + + switch ((op >> 21) & 0x1f) { + case 0: // R(rt) = FI(fs); break; //mfc1 + if (rt == MIPS_REG_ZERO) { + return; + } + gpr.MapDirty(rt); + ir.Write(IROp::FMovToGPR, rt, fs); + return; + + case 2: //cfc1 + if (rt == MIPS_REG_ZERO) { + return; + } + if (fs == 31) { + DISABLE; + } else if (fs == 0) { + gpr.SetImm(rt, MIPSState::FCR0_VALUE); + } else { + // Unsupported regs are always 0. + gpr.SetImm(rt, 0); + } + return; + + case 4: //FI(fs) = R(rt); break; //mtc1 + gpr.MapDirty(rt); + ir.Write(IROp::FMovFromGPR, fs, rt); + return; + + case 6: //ctc1 + if (fs == 31) { + // Set rounding mode + DISABLE; + } else { + Comp_Generic(op); + } + return; + default: + DISABLE; + break; + } +} + +} // namespace MIPSComp diff --git a/Core/MIPS/IR/IRCompLoadStore.cpp b/Core/MIPS/IR/IRCompLoadStore.cpp new file mode 100644 index 0000000000..53ea1f866f --- /dev/null +++ b/Core/MIPS/IR/IRCompLoadStore.cpp @@ -0,0 +1,162 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + + +// Optimization ideas: +// +// It's common to see sequences of stores writing or reading to a contiguous set of +// addresses in function prologues/epilogues: +// sw s5, 104(sp) +// sw s4, 100(sp) +// sw s3, 96(sp) +// sw s2, 92(sp) +// sw s1, 88(sp) +// sw s0, 84(sp) +// sw ra, 108(sp) +// mov s4, a0 +// mov s3, a1 +// ... +// Such sequences could easily be detected and turned into nice contiguous +// sequences of ARM stores instead of the current 3 instructions per sw/lw. +// +// Also, if we kept track of the likely register content of a cached register, +// (pointer or data), we could avoid many BIC instructions. + + +#include "Core/MemMap.h" +#include "Core/Config.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSAnalyst.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRRegCache.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +namespace MIPSComp { + void IRJit::Comp_ITypeMemLR(MIPSOpcode op, bool load) { + DISABLE; + } + + void IRJit::Comp_ITypeMem(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int offset = (signed short)(op & 0xFFFF); + bool load = false; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + int o = op >> 26; + if (((op >> 29) & 1) == 0 && rt == MIPS_REG_ZERO) { + // Don't load anything into $zr + return; + } + + u32 iaddr = gpr.IsImm(rs) ? offset + gpr.GetImm(rs) : 0xFFFFFFFF; + int addrReg = IRTEMP_0; + switch (o) { + // Load + case 35: + ir.Write(IROp::Load32, rt, rs, ir.AddConstant(offset)); + break; + case 37: + ir.Write(IROp::Load16, rt, rs, ir.AddConstant(offset)); + break; + case 33: + ir.Write(IROp::Load16Ext, rt, rs, ir.AddConstant(offset)); + break; + case 36: + ir.Write(IROp::Load8, rt, rs, ir.AddConstant(offset)); + break; + case 32: + ir.Write(IROp::Load8Ext, rt, rs, ir.AddConstant(offset)); + break; + // Store + case 43: + ir.Write(IROp::Store32, rt, rs, ir.AddConstant(offset)); + break; + case 41: + ir.Write(IROp::Store16, rt, rs, ir.AddConstant(offset)); + break; + case 40: + ir.Write(IROp::Store8, rt, rs, ir.AddConstant(offset)); + break; + + case 34: //lwl + case 38: //lwr + load = true; + case 42: //swl + case 46: //swr + DISABLE; + break; + default: + Comp_Generic(op); + return; + } + } + + void IRJit::Comp_Cache(MIPSOpcode op) { +// int imm = (s16)(op & 0xFFFF); +// int rs = _RS; +// int addr = R(rs) + imm; + int func = (op >> 16) & 0x1F; + + // It appears that a cache line is 0x40 (64) bytes, loops in games + // issue the cache instruction at that interval. + + // These codes might be PSP-specific, they don't match regular MIPS cache codes very well + switch (func) { + // Icache + case 8: + // Invalidate the instruction cache at this address + DISABLE; + break; + // Dcache + case 24: + // "Create Dirty Exclusive" - for avoiding a cacheline fill before writing to it. + // Will cause garbage on the real machine so we just ignore it, the app will overwrite the cacheline. + break; + case 25: // Hit Invalidate - zaps the line if present in cache. Should not writeback???? scary. + // No need to do anything. + break; + case 27: // D-cube. Hit Writeback Invalidate. Tony Hawk Underground 2 + break; + case 30: // GTA LCS, a lot. Fill (prefetch). Tony Hawk Underground 2 + break; + + default: + DISABLE; + break; + } + } +} diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp new file mode 100644 index 0000000000..d7b807fe63 --- /dev/null +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -0,0 +1,326 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include +#include "math/math_util.h" + +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSTables.h" +#include "Core/MIPS/MIPSAnalyst.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Common/CPUDetect.h" +#include "Core/Config.h" +#include "Core/Reporting.h" + +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRRegCache.h" + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { fpr.ReleaseSpillLocksAndDiscardTemps(); Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +namespace MIPSComp { + + void IRJit::Comp_VPFX(MIPSOpcode op) { + CONDITIONAL_DISABLE; + int data = op & 0xFFFFF; + int regnum = (op >> 24) & 3; + switch (regnum) { + case 0: // S + js.prefixS = data; + js.prefixSFlag = JitState::PREFIX_KNOWN_DIRTY; + break; + case 1: // T + js.prefixT = data; + js.prefixTFlag = JitState::PREFIX_KNOWN_DIRTY; + break; + case 2: // D + js.prefixD = data; + js.prefixDFlag = JitState::PREFIX_KNOWN_DIRTY; + break; + default: + ERROR_LOG(CPU, "VPFX - bad regnum %i : data=%08x", regnum, data); + break; + } + } + + void IRJit::ApplyPrefixST(u8 *vregs, u32 prefix, VectorSize sz) { + if (prefix == 0xE4) + return; + + int n = GetNumVectorElements(sz); + u8 origV[4]; + static const float constantArray[8] = { 0.f, 1.f, 2.f, 0.5f, 3.f, 1.f / 3.f, 0.25f, 1.f / 6.f }; + + for (int i = 0; i < n; i++) + origV[i] = vregs[i]; + + for (int i = 0; i < n; i++) { + int regnum = (prefix >> (i * 2)) & 3; + int abs = (prefix >> (8 + i)) & 1; + int negate = (prefix >> (16 + i)) & 1; + int constants = (prefix >> (12 + i)) & 1; + + // Unchanged, hurray. + if (!constants && regnum == i && !abs && !negate) + continue; + + /* + // This puts the value into a temp reg, so we won't write the modified value back. + vregs[i] = fpr.GetTempV(); + if (!constants) { + fpr.MapDirtyInV(vregs[i], origV[regnum]); + fpr.SpillLockV(vregs[i]); + + // Prefix may say "z, z, z, z" but if this is a pair, we force to x. + // TODO: But some ops seem to use const 0 instead? + if (regnum >= n) { + WARN_LOG(CPU, "JIT: Invalid VFPU swizzle: %08x : %d / %d at PC = %08x (%s)", prefix, regnum, n, GetCompilerPC(), MIPSDisasmAt(GetCompilerPC())); + regnum = 0; + } + + if (abs) { + fp.FABS(fpr.V(vregs[i]), fpr.V(origV[regnum])); + if (negate) + fp.FNEG(fpr.V(vregs[i]), fpr.V(vregs[i])); + } else { + if (negate) + fp.FNEG(fpr.V(vregs[i]), fpr.V(origV[regnum])); + else + fp.FMOV(fpr.V(vregs[i]), fpr.V(origV[regnum])); + } + } else { + fpr.MapRegV(vregs[i], MAP_DIRTY | MAP_NOINIT); + fpr.SpillLockV(vregs[i]); + fp.MOVI2F(fpr.V(vregs[i]), constantArray[regnum + (abs << 2)], SCRATCH1, (bool)negate); + } + */ + } + } + + void IRJit::GetVectorRegsPrefixD(u8 *regs, VectorSize sz, int vectorReg) { + _assert_(js.prefixDFlag & JitState::PREFIX_KNOWN); + + GetVectorRegs(regs, sz, vectorReg); + if (js.prefixD == 0) + return; + + int n = GetNumVectorElements(sz); + for (int i = 0; i < n; i++) { + // Hopefully this is rare, we'll just write it into a reg we drop. + //if (js.VfpuWriteMask(i)) + // regs[i] = fpr.GetTempV(); + } + } + + void IRJit::ApplyPrefixD(const u8 *vregs, VectorSize sz) { + _assert_(js.prefixDFlag & JitState::PREFIX_KNOWN); + if (!js.prefixD) + return; + + /* + int n = GetNumVectorElements(sz); + for (int i = 0; i < n; i++) { + if (js.VfpuWriteMask(i)) + continue; + + int sat = (js.prefixD >> (i * 2)) & 3; + if (sat == 1) { + // clamped = x < 0 ? (x > 1 ? 1 : x) : x [0, 1] + fpr.MapRegV(vregs[i], MAP_DIRTY); + + fp.MOVI2F(S0, 0.0f, SCRATCH1); + fp.MOVI2F(S1, 1.0f, SCRATCH1); + fp.FMIN(fpr.V(vregs[i]), fpr.V(vregs[i]), S1); + fp.FMAX(fpr.V(vregs[i]), fpr.V(vregs[i]), S0); + } else if (sat == 3) { + // clamped = x < -1 ? (x > 1 ? 1 : x) : x [-1, 1] + fpr.MapRegV(vregs[i], MAP_DIRTY); + + fp.MOVI2F(S0, -1.0f, SCRATCH1); + fp.MOVI2F(S1, 1.0f, SCRATCH1); + fp.FMIN(fpr.V(vregs[i]), fpr.V(vregs[i]), S1); + fp.FMAX(fpr.V(vregs[i]), fpr.V(vregs[i]), S0); + } + } + */ + } + + void IRJit::Comp_SV(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_SVQ(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VVectorInit(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VIdt(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VMatrixInit(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VHdp(MIPSOpcode op) { + DISABLE; + } + + static const float MEMORY_ALIGNED16(vavg_table[4]) = { 1.0f, 1.0f / 2.0f, 1.0f / 3.0f, 1.0f / 4.0f }; + + void IRJit::Comp_Vhoriz(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VDot(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VecDo3(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VV2Op(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vi2f(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vh2f(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vf2i(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Mftv(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vmfvc(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vmtvc(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vmmov(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VScl(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vmmul(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vmscl(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vtfm(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VCrs(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VDet(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vi2x(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vx2i(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_VCrossQuat(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vcmp(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vcmov(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Viim(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vfim(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vcst(MIPSOpcode op) { + DISABLE; + } + + // Very heavily used by FF:CC. Should be replaced by a fast approximation instead of + // calling the math library. + void IRJit::Comp_VRot(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vsgn(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vocp(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_ColorConv(MIPSOpcode op) { + DISABLE; + } + + void IRJit::Comp_Vbfy(MIPSOpcode op) { + DISABLE; + } +} diff --git a/Core/MIPS/IR/IRInst.cpp b/Core/MIPS/IR/IRInst.cpp new file mode 100644 index 0000000000..cfcbe43497 --- /dev/null +++ b/Core/MIPS/IR/IRInst.cpp @@ -0,0 +1,316 @@ +#include "Core/MIPS/IR/IRInst.h" +#include "Core/MemMap.h" + +IRMeta meta[] = { + { IROp::SetConst, "SetConst", "GC" }, + { IROp::Add, "Add", "GGG" }, + { IROp::Sub, "Sub", "GGG" }, + { IROp::Neg, "Neg", "GG" }, + { IROp::Not, "Not", "GG" }, + { IROp::And, "And", "GGG" }, + { IROp::Or, "Or", "GGG" }, + { IROp::Xor, "Xor", "GGG" }, + { IROp::AddConst, "AddConst", "GGC" }, + { IROp::SubConst, "SubConst", "GGC" }, + { IROp::AndConst, "AndConst", "GGC" }, + { IROp::OrConst, "OrConst", "GGC" }, + { IROp::XorConst, "XorConst", "GGC" }, + { IROp::Shl, "Shl", "GGG" }, + { IROp::Shr, "Shr", "GGG" }, + { IROp::Sar, "Sar", "GGG" }, + { IROp::Ror, "Ror", "GGG" }, + { IROp::ShlImm, "ShlImm", "GGI" }, + { IROp::ShrImm, "ShrImm", "GGI" }, + { IROp::SarImm, "SarImm", "GGI" }, + { IROp::RorImm, "RorImm", "GGI" }, + { IROp::Slt, "Slt","GGC" }, + { IROp::SltConst, "SltConst","GGC" }, + { IROp::SltU, "SltU", "GGC" }, + { IROp::SltUConst, "SltUConst", "GGC" }, + { IROp::Clz, "Clz", "GG" }, + { IROp::MovZ, "MovZ", "GGG" }, + { IROp::MovNZ, "MovNZ", "GGG" }, + { IROp::Max, "Max", "GGG" }, + { IROp::Min, "Min", "GGG" }, + { IROp::BSwap16, "BSwap16", "GG" }, + { IROp::BSwap32, "BSwap32", "GG" }, + { IROp::Mul, "Mul", "_GG" }, + { IROp::Ext8to32, "Ext8to32", "GG" }, + { IROp::Ext16to32, "Ext16to32", "GG" }, + { IROp::FAdd, "FAdd", "FFF" }, + { IROp::FSub, "FSub", "FFF" }, + { IROp::FMul, "FMul", "FFF" }, + { IROp::FDiv, "FDiv", "FFF" }, + { IROp::FMov, "FMov", "FF" }, + { IROp::FSqrt, "FSqrt", "FF" }, + { IROp::FNeg, "FNeg", "FF" }, + { IROp::FAbs, "FAbs", "FF" }, + { IROp::FRound, "FRound", "FF" }, + { IROp::FTrunc, "FTrunc", "FF" }, + { IROp::FCeil, "FCeil", "FF" }, + { IROp::FFloor, "FFloor", "FF" }, + { IROp::FCvtWS, "FCvtWS", "FF" }, + { IROp::FCvtSW, "FCvtSW", "FF" }, + { IROp::FMovFromGPR, "FMovFromGPR", "FG" }, + { IROp::FMovToGPR, "FMovToGPR", "GF" }, + { IROp::FpCondToReg, "FpCondToReg", "G" }, + { IROp::SetCtrlVFPU, "SetCtrlVFPU", "T" }, + { IROp::Interpret, "Interpret", "_C" }, + { IROp::Downcount, "Downcount", "_II" }, + { IROp::Syscall, "Syscall", "_C"}, + { IROp::SetPC, "SetPC", "_C"}, +}; + +const IRMeta *metaIndex[256]; + +void InitIR() { + for (size_t i = 0; i < ARRAY_SIZE(meta); i++) { + metaIndex[(int)meta[i].op] = &meta[i]; + } +} + +u32 IRInterpret(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count) { + const IRInst *end = inst + count; + while (inst != end) { + switch (inst->op) { + case IROp::SetConst: + mips->r[inst->dest] = constPool[inst->src1]; + break; + case IROp::Add: + mips->r[inst->dest] = mips->r[inst->src1] + mips->r[inst->src2]; + break; + case IROp::Sub: + mips->r[inst->dest] = mips->r[inst->src1] - mips->r[inst->src2]; + break; + case IROp::Neg: + mips->r[inst->dest] = -(s32)mips->r[inst->src1]; + break; + case IROp::Ext8to32: + mips->r[inst->dest] = (s32)(s8)mips->r[inst->src1]; + break; + case IROp::Ext16to32: + mips->r[inst->dest] = (s32)(s16)mips->r[inst->src1]; + break; + + case IROp::Load8: + mips->r[inst->dest] = Memory::ReadUnchecked_U8(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load8Ext: + mips->r[inst->dest] = (s32)(s8)Memory::ReadUnchecked_U8(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load16: + mips->r[inst->dest] = Memory::ReadUnchecked_U16(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load16Ext: + mips->r[inst->dest] = (s32)(s16)Memory::ReadUnchecked_U16(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load32: + mips->r[inst->dest] = Memory::ReadUnchecked_U32(mips->r[inst->src1] + constPool[inst->src2]); + break; + + case IROp::Store8: + Memory::WriteUnchecked_U8(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Store16: + Memory::WriteUnchecked_U16(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Store32: + Memory::WriteUnchecked_U32(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + + case IROp::ShlImm: + mips->r[inst->dest] = mips->r[inst->src1] << inst->src2; + break; + case IROp::ShrImm: + mips->r[inst->dest] = mips->r[inst->src1] >> inst->src2; + break; + case IROp::SarImm: + mips->r[inst->dest] = (s32)mips->r[inst->src1] >> inst->src2; + break; + case IROp::RorImm: + { + u32 x = mips->r[inst->src1]; + int sa = inst->src2; + mips->r[inst->dest] = (x >> sa) | (x << (32 - sa)); + } + break; + + case IROp::Shl: + mips->r[inst->dest] = mips->r[inst->src1] << (mips->r[inst->src2] & 31); + break; + case IROp::Shr: + mips->r[inst->dest] = mips->r[inst->src1] >> (mips->r[inst->src2] & 31); + break; + case IROp::Sar: + mips->r[inst->dest] = (s32)mips->r[inst->src1] >> (mips->r[inst->src2] & 31); + break; + case IROp::Ror: + { + u32 x = mips->r[inst->src1]; + int sa = mips->r[inst->src2] & 31; + mips->r[inst->dest] = (x >> sa) | (x << (32 - sa)); + } + break; + + case IROp::MovZ: + if (mips->r[inst->src1] == 0) + mips->r[inst->dest] = mips->r[inst->src2]; + break; + case IROp::MovNZ: + if (mips->r[inst->src1] != 0) + mips->r[inst->dest] = mips->r[inst->src2]; + break; + + case IROp::Max: + mips->r[inst->dest] = (s32)mips->r[inst->src1] > (s32)mips->r[inst->src2] ? mips->r[inst->src1] : mips->r[inst->src2]; + break; + case IROp::Min: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)mips->r[inst->src2] ? mips->r[inst->src1] : mips->r[inst->src2]; + break; + + case IROp::BSwap16: + { + u32 x = mips->r[inst->src1]; + mips->r[inst->dest] = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8); + break; + } + case IROp::BSwap32: + mips->r[inst->dest] = swap32(mips->r[inst->src1]); + break; + + case IROp::FAdd: + mips->f[inst->dest] = mips->f[inst->src1] + mips->f[inst->src2]; + break; + case IROp::FSub: + mips->f[inst->dest] = mips->f[inst->src1] - mips->f[inst->src2]; + break; + case IROp::FMul: + mips->f[inst->dest] = mips->f[inst->src1] * mips->f[inst->src2]; + break; + case IROp::FDiv: + mips->f[inst->dest] = mips->f[inst->src1] / mips->f[inst->src2]; + break; + + case IROp::FMov: + mips->f[inst->dest] = mips->f[inst->src1]; + break; + case IROp::FAbs: + mips->f[inst->dest] = fabsf(mips->f[inst->src1]); + break; + case IROp::FSqrt: + mips->f[inst->dest] = sqrtf(mips->f[inst->src1]); + break; + case IROp::FNeg: + mips->f[inst->dest] = -mips->f[inst->src1]; + break; + case IROp::FpCondToReg: + mips->r[inst->dest] = mips->fpcond; + break; + + case IROp::ExitToConst: + return constPool[inst->src1]; + + case IROp::ExitToReg: + return mips->r[inst->src1]; + + case IROp::ExitToConstIfEq: + if (mips->r[inst->src1] == mips->r[inst->src2]) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfNeq: + if (mips->r[inst->src1] != mips->r[inst->src2]) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfGtZ: + if ((s32)mips->r[inst->src1] > 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfGeZ: + if ((s32)mips->r[inst->src1] >= 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfLtZ: + if ((s32)mips->r[inst->src1] < 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfLeZ: + if ((s32)mips->r[inst->src1] <= 0) + return constPool[inst->dest]; + break; + + case IROp::SetPC: + return mips->pc = mips->r[inst->src1]; + + default: + Crash(); + } + inst++; + } + + // If we got here, the block was badly constructed. + // Crash(); + return 0; +} + +void IRWriter::Write(IROp op, u8 dst, u8 src1, u8 src2) { + IRInst inst; + inst.op = op; + inst.dest = dst; + inst.src1 = src1; + inst.src2 = src2; + insts_.push_back(inst); +} + +void IRWriter::WriteSetConstant(u8 dst, u32 value) { + // TODO: Check for the fixed ones first. + Write(IROp::SetConstImm, AddConstant(value)); +} + +int IRWriter::AddConstant(u32 value) { + for (size_t i = 0; i < constPool_.size(); i++) { + if (constPool_[i] == value) + return i; + } + constPool_.push_back(value); + return (int)constPool_.size() - 1; +} + +int IRWriter::AddConstantFloat(float value) { + u32 val; + memcpy(&val, &value, 4); + return AddConstant(val); +} + +void DisassembleParam(char *buf, int bufSize, u8 param, char type, const u32 *constPool) { + switch (type) { + case 'G': + snprintf(buf, bufSize, "r%d", param); + break; + case 'F': + snprintf(buf, bufSize, "r%d", param); + break; + case 'C': + snprintf(buf, bufSize, "%08x", constPool[param]); + break; + default: + snprintf(buf, bufSize, "?"); + break; + } +} + +void DisassembleIR(char *buf, size_t bufsize, IRInst inst, const u32 *constPool) { + const IRMeta *meta = metaIndex[(int)inst.op]; + char bufDst[16]; + char bufSrc1[16]; + char bufSrc2[16]; + DisassembleParam(bufDst, sizeof(bufDst) - 2, inst.dest, meta->types[0], constPool); + DisassembleParam(bufSrc1, sizeof(bufSrc1) - 2, inst.dest, meta->types[1], constPool); + DisassembleParam(bufSrc2, sizeof(bufSrc2), inst.dest, meta->types[2], constPool); + if (meta->types[1]) { + strcat(bufDst, ", "); + } + if (meta->types[2]) { + strcat(bufSrc1, ", "); + } + snprintf(buf, bufsize, "%s %s%s%s", meta->name, bufDst, bufSrc1, bufSrc2); +} \ No newline at end of file diff --git a/Core/MIPS/IR/IRInst.h b/Core/MIPS/IR/IRInst.h new file mode 100644 index 0000000000..a1aa75edc7 --- /dev/null +++ b/Core/MIPS/IR/IRInst.h @@ -0,0 +1,260 @@ +#pragma once + +#include + +#include "Common/CommonTypes.h" +#include "Core/MIPS/MIPS.h" + +// Basic IR +// +// This IR refers implicitly to the MIPS register set and is simple to interpret. +// To do real compiler things with it and do full-function compilation, it probably +// needs to be lifted to a higher IR first, before being lowered onto each target. +// But this gets rid of a lot of MIPS idiosyncrasies that makes it tricky, like +// delay slots, and is very suitable for translation into other IRs. Can of course +// even be directly JIT-ed, but the gains will probably be tiny over our older direct +// MIPS->target JITs. + +enum class IROp : u8 { + SetConst, + SetConstImm, + FSetConst, + + Mov, + + Add, + Sub, + Neg, + Not, + + And, + Or, + Xor, + + AddConst, + SubConst, + + AndConst, + OrConst, + XorConst, + + Shl, + Shr, + Sar, + Ror, + + // The shift is stored directly, not in the const table, so Imm instead of Const + ShlImm, + ShrImm, + SarImm, + RorImm, + + Slt, + SltConst, + SltU, + SltUConst, + + Clz, + + // Conditional moves + MovZ, + MovNZ, + + Max, + Min, + + // Byte swaps. All CPUs have native ones so worth keeping. + BSwap16, // Swaps both the high and low byte pairs. + BSwap32, + + // Hi/Lo semantics preserved. + Mul, + MulU, + Madd, + MaddU, + Msub, + MsubU, + + // These take a constant from the pool as an offset. + // Loads from a constant address can be represented by using r0. + Load8, + Load8Ext, + Load16, + Load16Ext, + Load32, + LoadFloat, + + Store8, + Store16, + Store32, + StoreFloat, + + Ext8to32, + Ext16to32, + + FAdd, + FSub, + FMul, + FDiv, + + FMov, + FSqrt, + FNeg, + FAbs, + + FRound, + FTrunc, + FCeil, + FFloor, + + FCvtWS, + FCvtSW, + + FMovFromGPR, + FMovToGPR, + + FpCondToReg, + VfpCondToReg, + + FCmpUnordered, + FCmpEqual, + FCmpEqualUnordered, + FCmpLessOrdered, + FCmpLessUnordered, + FCmpLessEqualOrdered, + FCmpLessEqualUnordered, + + // Rounding Mode + RestoreRoundingMode, + ApplyRoundingMode, + UpdateRoundingMode, + + SetCtrlVFPU, + + // Fake/System instructions + Interpret, + + // Emit this before you exits. Semantic is to set the downcount + // that will be used at the actual exit. + Downcount, // src1 + (src2<<8) + + // End-of-basic-block. + ExitToConst, // 0, const, downcount + ExitToReg, + ExitToConstIfEq, // const, reg1, reg2 + ExitToConstIfNeq, // const, reg1, reg2 + ExitToConstIfGtZ, // const, reg1, 0 + ExitToConstIfGeZ, // const, reg1, 0 + ExitToConstIfLtZ, // const, reg1, 0 + ExitToConstIfLeZ, // const, reg1, 0 + + ExitToConstIfFpTrue, + ExitToConstIfFpFalse, + + Syscall, + SetPC, // hack to make syscall returns work + Break, +}; + +enum IRComparison { + Greater, + GreaterEqual, + Less, + LessEqual, + Equal, + NotEqual, + Bad, +}; + +// Hm, unused +inline IRComparison Invert(IRComparison comp) { + switch (comp) { + case IRComparison::Equal: return IRComparison::NotEqual; + case IRComparison::NotEqual: return IRComparison::Equal; + case IRComparison::Greater: return IRComparison::LessEqual; + case IRComparison::GreaterEqual: return IRComparison::Less; + case IRComparison::Less: return IRComparison::GreaterEqual; + case IRComparison::LessEqual: return IRComparison::Greater; + default: + return IRComparison::Bad; + } +} + +inline IROp ComparisonToExit(IRComparison comp) { + switch (comp) { + case IRComparison::Equal: return IROp::ExitToConstIfEq; + case IRComparison::NotEqual: return IROp::ExitToConstIfNeq; + case IRComparison::Greater: return IROp::ExitToConstIfGtZ; + case IRComparison::GreaterEqual: return IROp::ExitToConstIfGeZ; + case IRComparison::Less: return IROp::ExitToConstIfLtZ; + case IRComparison::LessEqual: return IROp::ExitToConstIfLeZ; + default: + return IROp::Break; + } +} + +enum { + IRTEMP_0 = 192, + IRTEMP_1, + IRTEMP_2, + IRTEMP_3, + + // Hacky way to get to other state + IRREG_LO = 226, // offset of lo in MIPSState / 4 + IRREG_HI = 227, +}; + +enum class IRParam { + Ignore = '_', + UImm8 = 'U', + Const = 'C', + GPR = 'G', + FPR = 'F', + VPR = 'V', + VCtrl = 'T', +}; + +struct IRMeta { + IROp op; + const char *name; + const char types[4]; // GGG + u32 flags; +}; + +// 32 bits. +struct IRInst { + IROp op; + union { + u8 dest; + u8 src3; + }; + u8 src1; + u8 src2; +}; + +// Returns the new PC. +u32 IRInterpret(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count); + +// Each IR block gets a constant pool. +class IRWriter { +public: + void Write(IROp op, u8 dst = 0, u8 src1 = 0, u8 src2 = 0); + void WriteSetConstant(u8 dst, u32 value); + + int AddConstant(u32 value); + int AddConstantFloat(float value); + + void Clear() { + insts_.clear(); + constPool_.clear(); + } + + const std::vector &GetInstructions() { return insts_; } + const std::vector &GetConstants() { return constPool_; } + +private: + std::vector insts_; + std::vector constPool_; +}; + +void DisassembleIR(char *buf, size_t bufsize, IRInst inst, const u32 *constPool); diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp new file mode 100644 index 0000000000..b16706b2d6 --- /dev/null +++ b/Core/MIPS/IR/IRJit.cpp @@ -0,0 +1,333 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include "base/logging.h" +#include "profiler/profiler.h" +#include "Common/ChunkFile.h" +#include "Common/CPUDetect.h" +#include "Common/StringUtils.h" + +#include "Core/Reporting.h" +#include "Core/Config.h" +#include "Core/Core.h" +#include "Core/CoreTiming.h" +#include "Core/Debugger/SymbolMap.h" +#include "Core/MemMap.h" + +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSInt.h" +#include "Core/MIPS/MIPSTables.h" +#include "Core/HLE/ReplaceTables.h" +#include "Core/HLE/sceKernelMemory.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/JitCommon/JitCommon.h" + +void DisassembleArm64Print(const u8 *data, int size) { + std::vector lines = DisassembleArm64(data, size); + for (auto s : lines) { + ILOG("%s", s.c_str()); + } + /* + ILOG("+++"); + // A format friendly to Online Disassembler which gets endianness wrong + for (size_t i = 0; i < lines.size(); i++) { + uint32_t opcode = ((const uint32_t *)data)[i]; + ILOG("%d/%d: %08x", (int)(i+1), (int)lines.size(), swap32(opcode)); + } + ILOG("==="); + ILOG("===");*/ +} + +namespace MIPSComp +{ + +IRJit::IRJit(MIPSState *mips) : gpr(), mips_(mips) { + logBlocks = 0; + dontLogBlocks = 0; + js.startDefaultPrefix = mips_->HasDefaultPrefix(); + js.currentRoundingFunc = convertS0ToSCRATCH1[0]; + u32 size = 128 * 1024; + blTrampolines_ = kernelMemory.Alloc(size, true, "trampoline"); +} + +IRJit::~IRJit() { +} + +void IRJit::DoState(PointerWrap &p) { + auto s = p.Section("Jit", 1, 2); + if (!s) + return; + + p.Do(js.startDefaultPrefix); + if (s >= 2) { + p.Do(js.hasSetRounding); + js.lastSetRounding = 0; + } else { + js.hasSetRounding = 1; + } + + if (p.GetMode() == PointerWrap::MODE_READ) { + js.currentRoundingFunc = convertS0ToSCRATCH1[(mips_->fcr31) & 3]; + } +} + +// This is here so the savestate matches between jit and non-jit. +void IRJit::DoDummyState(PointerWrap &p) { + auto s = p.Section("Jit", 1, 2); + if (!s) + return; + + bool dummy = false; + p.Do(dummy); + if (s >= 2) { + dummy = true; + p.Do(dummy); + } +} + +void IRJit::FlushAll() { + FlushPrefixV(); +} + +void IRJit::FlushPrefixV() { + if ((js.prefixSFlag & JitState::PREFIX_DIRTY) != 0) { + ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_SPREFIX, ir.AddConstant(js.prefixS)); + js.prefixSFlag = (JitState::PrefixState) (js.prefixSFlag & ~JitState::PREFIX_DIRTY); + } + + if ((js.prefixTFlag & JitState::PREFIX_DIRTY) != 0) { + ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_TPREFIX, ir.AddConstant(js.prefixT)); + js.prefixTFlag = (JitState::PrefixState) (js.prefixTFlag & ~JitState::PREFIX_DIRTY); + } + + if ((js.prefixDFlag & JitState::PREFIX_DIRTY) != 0) { + ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_DPREFIX, ir.AddConstant(js.prefixD)); + js.prefixDFlag = (JitState::PrefixState) (js.prefixDFlag & ~JitState::PREFIX_DIRTY); + } +} + +void IRJit::ClearCache() { + ILOG("ARM64Jit: Clearing the cache!"); + blocks_.Clear(); +} + +void IRJit::InvalidateCache() { + blocks_.Clear(); +} + +void IRJit::InvalidateCacheAt(u32 em_address, int length) { + blocks_.InvalidateICache(em_address, length); +} + +void IRJit::EatInstruction(MIPSOpcode op) { + MIPSInfo info = MIPSGetInfo(op); + if (info & DELAYSLOT) { + ERROR_LOG_REPORT_ONCE(ateDelaySlot, JIT, "Ate a branch op."); + } + if (js.inDelaySlot) { + ERROR_LOG_REPORT_ONCE(ateInDelaySlot, JIT, "Ate an instruction inside a delay slot."); + } + + js.numInstructions++; + js.compilerPC += 4; + js.downcountAmount += MIPSGetInstructionCycleEstimate(op); +} + +void IRJit::CompileDelaySlot() { + js.inDelaySlot = true; + MIPSOpcode op = GetOffsetInstruction(1); + MIPSCompileOp(op, this); + js.inDelaySlot = false; +} + +void IRJit::Compile(u32 em_address) { + PROFILE_THIS_SCOPE("jitc"); + + int block_num = blocks_.AllocateBlock(em_address); + IRBlock *b = blocks_.GetBlock(block_num); + DoJit(em_address, b); + + bool cleanSlate = false; + + if (js.hasSetRounding && !js.lastSetRounding) { + WARN_LOG(JIT, "Detected rounding mode usage, rebuilding jit with checks"); + // Won't loop, since hasSetRounding is only ever set to 1. + js.lastSetRounding = js.hasSetRounding; + cleanSlate = true; + } + + // Drat. The VFPU hit an uneaten prefix at the end of a block. + if (js.startDefaultPrefix && js.MayHavePrefix()) { + WARN_LOG(JIT, "An uneaten prefix at end of block: %08x", GetCompilerPC() - 4); + js.LogPrefix(); + + // Let's try that one more time. We won't get back here because we toggled the value. + js.startDefaultPrefix = false; + // TODO ARM64: This crashes. + //cleanSlate = true; + } + + if (cleanSlate) { + // Our assumptions are all wrong so it's clean-slate time. + ClearCache(); + Compile(em_address); + } +} + +void IRJit::RunLoopUntil(u64 globalticks) { + PROFILE_THIS_SCOPE("jit"); + ((void (*)())enterDispatcher)(); +} + +u32 IRJit::GetCompilerPC() { + return js.compilerPC; +} + +MIPSOpcode IRJit::GetOffsetInstruction(int offset) { + return Memory::Read_Instruction(GetCompilerPC() + 4 * offset); +} + +void IRJit::DoJit(u32 em_address, IRBlock *b) { + js.cancel = false; + js.blockStart = mips_->pc; + js.compilerPC = mips_->pc; + js.lastContinuedPC = 0; + js.initialBlockSize = 0; + js.nextExit = 0; + js.downcountAmount = 0; + js.curBlock = nullptr; + js.compiling = true; + js.inDelaySlot = false; + js.PrefixStart(); + ir.Clear(); + + gpr.Start(&ir); + + int partialFlushOffset = 0; + + js.numInstructions = 0; + while (js.compiling) { + MIPSOpcode inst = Memory::Read_Opcode_JIT(GetCompilerPC()); + js.downcountAmount += MIPSGetInstructionCycleEstimate(inst); + MIPSCompileOp(inst, this); + js.compilerPC += 4; + js.numInstructions++; + } + + b->SetInstructions(ir.GetInstructions(), ir.GetConstants()); + + char temp[256]; + if (logBlocks > 0 && dontLogBlocks == 0) { + ILOG("=============== mips %d ===============", blocks_.GetNumBlocks()); + for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) { + MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp, true); + ILOG("M: %08x %s", cpc, temp); + } + } + + if (logBlocks > 0 && dontLogBlocks == 0) { + ILOG("=============== IR (%d instructions) ===============", js.numInstructions); + for (int i = 0; i < js.numInstructions; i++) { + char buf[256]; + DisassembleIR(buf, sizeof(buf), ir.GetInstructions()[i], ir.GetConstants().data()); + ILOG("%s", buf); + } + } + + if (logBlocks > 0) + logBlocks--; + if (dontLogBlocks > 0) + dontLogBlocks--; +} + +bool IRJit::DescribeCodePtr(const u8 *ptr, std::string &name) { + // Used in disassembly viewer. + return false; +} + +void IRJit::Comp_RunBlock(MIPSOpcode op) { + // This shouldn't be necessary, the dispatcher should catch us before we get here. + ERROR_LOG(JIT, "Comp_RunBlock should never be reached!"); +} + +void IRJit::LinkBlock(u8 *exitPoint, const u8 *checkedEntry) { + Crash(); +} + +void IRJit::UnlinkBlock(u8 *checkedEntry, u32 originalAddress) { + Crash(); +} + +bool IRJit::ReplaceJalTo(u32 dest) { + Crash(); + return false; +} + +void IRJit::Comp_ReplacementFunc(MIPSOpcode op) { + Crash(); +} + +void IRJit::Comp_Generic(MIPSOpcode op) { + ir.Write(IROp::Interpret, ir.AddConstant(op.encoding)); + const MIPSInfo info = MIPSGetInfo(op); + if ((info & IS_VFPU) != 0 && (info & VFPU_NO_PREFIX) == 0) { + // If it does eat them, it'll happen in MIPSCompileOp(). + if ((info & OUT_EAT_PREFIX) == 0) + js.PrefixUnknown(); + } +} + +// Destroys SCRATCH2 +void IRJit::RestoreRoundingMode(bool force) { + // If the game has never set an interesting rounding mode, we can safely skip this. + if (force || js.hasSetRounding) { + ir.Write(IROp::RestoreRoundingMode); + } +} + +// Destroys SCRATCH1 and SCRATCH2 +void IRJit::ApplyRoundingMode(bool force) { + // If the game has never set an interesting rounding mode, we can safely skip this. + if (force || js.hasSetRounding) { + ir.Write(IROp::ApplyRoundingMode); + } +} + +// Destroys SCRATCH1 and SCRATCH2 +void IRJit::UpdateRoundingMode() { + ir.Write(IROp::UpdateRoundingMode); +} + +void IRJit::Comp_DoNothing(MIPSOpcode op) { +} + +int IRJit::Replace_fabsf() { + Crash(); + return 0; +} + +void IRBlockCache::Clear() { + blocks_.clear(); +} + +void IRBlockCache::InvalidateICache(u32 addess, u32 length) { + // TODO +} + +} // namespace MIPSComp \ No newline at end of file diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h new file mode 100644 index 0000000000..686eefe6c2 --- /dev/null +++ b/Core/MIPS/IR/IRJit.h @@ -0,0 +1,276 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#pragma once + +#include "Common/CPUDetect.h" +#include "Core/MIPS/JitCommon/JitState.h" +#include "Core/MIPS/JitCommon/JitBlockCache.h" +#include "Core/MIPS/JitCommon/JitCommon.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRInst.h" +#include "Core/MIPS/MIPSVFPUUtils.h" + +#ifndef offsetof +#include "stddef.h" +#endif + +namespace MIPSComp { + +// TODO : Use arena allocators. For now let's just malloc. +class IRBlock { +public: + IRBlock() {} + IRBlock(u32 emAddr) : instr_(nullptr), const_(nullptr), origAddr_(emAddr), numInstructions_(0) {} + ~IRBlock() { + delete[] instr_; + delete[] const_; + } + + void SetInstructions(const std::vector &inst, const std::vector &constants) { + instr_ = new IRInst[inst.size()]; + numInstructions_ = (u16)inst.size(); + memcpy(instr_, inst.data(), sizeof(IRInst) * inst.size()); + const_ = new u32[constants.size()]; + numConstants_ = (u16)constants.size(); + memcpy(const_, constants.data(), sizeof(u32) * constants.size()); + } + +private: + IRInst *instr_; + u32 *const_; + u16 numInstructions_; + u16 numConstants_; + u32 origAddr_; +}; + +class IRBlockCache { +public: + void Clear(); + void InvalidateICache(u32 addess, u32 length); + int GetNumBlocks() const { return (int)blocks_.size(); } + int AllocateBlock(int emAddr) { + blocks_.emplace_back(IRBlock(emAddr)); + return (int)blocks_.size() - 1; + } + IRBlock *GetBlock(int i) { + return &blocks_[i]; + } +private: + std::vector blocks_; +}; + +class IRJit : public JitInterface { +public: + IRJit(MIPSState *mips); + virtual ~IRJit(); + + void DoState(PointerWrap &p) override; + void DoDummyState(PointerWrap &p) override; + + const JitOptions &GetJitOptions() { return jo; } + + // Compiled ops should ignore delay slots + // the compiler will take care of them by itself + // OR NOT + void Comp_Generic(MIPSOpcode op) override; + + void RunLoopUntil(u64 globalticks) override; + + void Compile(u32 em_address) override; // Compiles a block at current MIPS PC + void DoJit(u32 em_address, IRBlock *b); + + bool DescribeCodePtr(const u8 *ptr, std::string &name) override; + + void Comp_RunBlock(MIPSOpcode op) override; + void Comp_ReplacementFunc(MIPSOpcode op) override; + + // Ops + void Comp_ITypeMem(MIPSOpcode op) override; + void Comp_Cache(MIPSOpcode op) override; + + void Comp_RelBranch(MIPSOpcode op) override; + void Comp_RelBranchRI(MIPSOpcode op) override; + void Comp_FPUBranch(MIPSOpcode op) override; + void Comp_FPULS(MIPSOpcode op) override; + void Comp_FPUComp(MIPSOpcode op) override; + void Comp_Jump(MIPSOpcode op) override; + void Comp_JumpReg(MIPSOpcode op) override; + void Comp_Syscall(MIPSOpcode op) override; + void Comp_Break(MIPSOpcode op) override; + + void Comp_IType(MIPSOpcode op) override; + void Comp_RType2(MIPSOpcode op) override; + void Comp_RType3(MIPSOpcode op) override; + void Comp_ShiftType(MIPSOpcode op) override; + void Comp_Allegrex(MIPSOpcode op) override; + void Comp_Allegrex2(MIPSOpcode op) override; + void Comp_VBranch(MIPSOpcode op) override; + void Comp_MulDivType(MIPSOpcode op) override; + void Comp_Special3(MIPSOpcode op) override; + + void Comp_FPU3op(MIPSOpcode op) override; + void Comp_FPU2op(MIPSOpcode op) override; + void Comp_mxc1(MIPSOpcode op) override; + + void Comp_DoNothing(MIPSOpcode op) override; + + void Comp_SV(MIPSOpcode op) override; + void Comp_SVQ(MIPSOpcode op) override; + void Comp_VPFX(MIPSOpcode op) override; + void Comp_VVectorInit(MIPSOpcode op) override; + void Comp_VMatrixInit(MIPSOpcode op) override; + void Comp_VDot(MIPSOpcode op) override; + void Comp_VecDo3(MIPSOpcode op) override; + void Comp_VV2Op(MIPSOpcode op) override; + void Comp_Mftv(MIPSOpcode op) override; + void Comp_Vmfvc(MIPSOpcode op) override; + void Comp_Vmtvc(MIPSOpcode op) override; + void Comp_Vmmov(MIPSOpcode op) override; + void Comp_VScl(MIPSOpcode op) override; + void Comp_Vmmul(MIPSOpcode op) override; + void Comp_Vmscl(MIPSOpcode op) override; + void Comp_Vtfm(MIPSOpcode op) override; + void Comp_VHdp(MIPSOpcode op) override; + void Comp_VCrs(MIPSOpcode op) override; + void Comp_VDet(MIPSOpcode op) override; + void Comp_Vi2x(MIPSOpcode op) override; + void Comp_Vx2i(MIPSOpcode op) override; + void Comp_Vf2i(MIPSOpcode op) override; + void Comp_Vi2f(MIPSOpcode op) override; + void Comp_Vh2f(MIPSOpcode op) override; + void Comp_Vcst(MIPSOpcode op) override; + void Comp_Vhoriz(MIPSOpcode op) override; + void Comp_VRot(MIPSOpcode op) override; + void Comp_VIdt(MIPSOpcode op) override; + void Comp_Vcmp(MIPSOpcode op) override; + void Comp_Vcmov(MIPSOpcode op) override; + void Comp_Viim(MIPSOpcode op) override; + void Comp_Vfim(MIPSOpcode op) override; + void Comp_VCrossQuat(MIPSOpcode op) override; + void Comp_Vsgn(MIPSOpcode op) override; + void Comp_Vocp(MIPSOpcode op) override; + void Comp_ColorConv(MIPSOpcode op) override; + void Comp_Vbfy(MIPSOpcode op) override; + + int Replace_fabsf(); + + // Not using a regular block cache. + JitBlockCache *GetBlockCache() { return nullptr; } + + void ClearCache(); + void InvalidateCache(); + void InvalidateCacheAt(u32 em_address, int length = 4); + + void EatPrefix() { js.EatPrefix(); } + + const u8 *GetDispatcher() const override { + return dispatcher; + } + + void LinkBlock(u8 *exitPoint, const u8 *checkedEntry) override; + void UnlinkBlock(u8 *checkedEntry, u32 originalAddress) override; + +private: + void FlushAll(); + void FlushPrefixV(); + + u32 GetCompilerPC(); + void CompileDelaySlot(); + void EatInstruction(MIPSOpcode op); + MIPSOpcode GetOffsetInstruction(int offset); + + void RestoreRoundingMode(bool force = false); + void ApplyRoundingMode(bool force = false); + void UpdateRoundingMode(); + + bool ReplaceJalTo(u32 dest); + + // Utility compilation functions + void BranchFPFlag(MIPSOpcode op, IRComparison cc, bool likely); + void BranchVFPUFlag(MIPSOpcode op, IRComparison cc, bool likely); + void BranchRSZeroComp(MIPSOpcode op, IRComparison cc, bool andLink, bool likely); + void BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely); + + // Utilities to reduce duplicated code + void CompImmLogic(MIPSGPReg rs, MIPSGPReg rt, u32 uimm, IROp op); + void CompType3(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt, IROp op, IROp constOp, bool symmetric = false); + void CompShiftImm(MIPSOpcode op, IROp shiftType, int sa); + void CompShiftVar(MIPSOpcode op, IROp shiftType, IROp shiftTypeConst); + + void ApplyPrefixST(u8 *vregs, u32 prefix, VectorSize sz); + void ApplyPrefixD(const u8 *vregs, VectorSize sz); + void GetVectorRegsPrefixS(u8 *regs, VectorSize sz, int vectorReg) { + _assert_(js.prefixSFlag & JitState::PREFIX_KNOWN); + GetVectorRegs(regs, sz, vectorReg); + ApplyPrefixST(regs, js.prefixS, sz); + } + void GetVectorRegsPrefixT(u8 *regs, VectorSize sz, int vectorReg) { + _assert_(js.prefixTFlag & JitState::PREFIX_KNOWN); + GetVectorRegs(regs, sz, vectorReg); + ApplyPrefixST(regs, js.prefixT, sz); + } + void GetVectorRegsPrefixD(u8 *regs, VectorSize sz, int vectorReg); + + // Utils + void Comp_ITypeMemLR(MIPSOpcode op, bool load); + + JitOptions jo; + JitState js; + + IRBlockCache blocks_; + + IRRegCache gpr; + // Arm64RegCacheFPU fpr; + + MIPSState *mips_; + + int dontLogBlocks; + int logBlocks; + + IRWriter ir; + + // where to write branch-likely trampolines + u32 blTrampolines_; + int blTrampolineCount_; + +public: + // Code pointers + const u8 *enterDispatcher; + + const u8 *outerLoop; + const u8 *outerLoopPCInSCRATCH1; + const u8 *dispatcherCheckCoreState; + const u8 *dispatcherPCInSCRATCH1; + const u8 *dispatcher; + const u8 *dispatcherNoCheck; + + const u8 *breakpointBailout; + + const u8 *saveStaticRegisters; + const u8 *loadStaticRegisters; + + const u8 *restoreRoundingMode; + const u8 *applyRoundingMode; + const u8 *updateRoundingMode; + + // Indexed by FPCR FZ:RN bits for convenience. Uses SCRATCH2. + const u8 *convertS0ToSCRATCH1[8]; +}; + +} // namespace MIPSComp + diff --git a/Core/MIPS/IR/IRRegCache.cpp b/Core/MIPS/IR/IRRegCache.cpp new file mode 100644 index 0000000000..7a31a463e4 --- /dev/null +++ b/Core/MIPS/IR/IRRegCache.cpp @@ -0,0 +1,46 @@ +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRInst.h" + +void IRRegCache::Dirty(MIPSGPReg rd) { + if (rd == 0) { + return; + } + if (reg_[rd].isImm) { + ir_->WriteSetConstant(rd, reg_[rd].immVal); + reg_[rd].isImm = false; + } +} + +void IRRegCache::MapIn(MIPSGPReg rd) { + Dirty(rd); +} + +void IRRegCache::MapInIn(MIPSGPReg rs, MIPSGPReg rt) { + Dirty(rs); + Dirty(rt); +} + +void IRRegCache::MapDirty(MIPSGPReg rd) { + Dirty(rd); +} + +void IRRegCache::MapDirtyIn(MIPSGPReg rd, MIPSGPReg rs) { + Dirty(rd); + Dirty(rs); +} + +void IRRegCache::MapDirtyInIn(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt) { + Dirty(rd); + Dirty(rs); + Dirty(rt); +} + +void IRRegCache::Start(IRWriter *ir) { + memset(®_, 0, sizeof(reg_)); + reg_[0].isImm = true; + ir_ = ir; +} + +void IRRegCache::FlushAll() { + +} diff --git a/Core/MIPS/IR/IRRegCache.h b/Core/MIPS/IR/IRRegCache.h new file mode 100644 index 0000000000..bf53e2a818 --- /dev/null +++ b/Core/MIPS/IR/IRRegCache.h @@ -0,0 +1,43 @@ +#pragma once + +// IRRegCache is only to perform pre-constant folding. This is worth it to get cleaner +// IR. + +#include "Common/CommonTypes.h" +#include "Core/MIPS/MIPS.h" + +enum { + TOTAL_MAPPABLE_MIPSREGS = 256, +}; + +struct RegIR { + bool isImm; + u32 immVal; +}; + +class IRWriter; + +class IRRegCache { +public: + void SetImm(MIPSGPReg r, u32 immVal) { + reg_[r].isImm = true; + reg_[r].immVal = immVal; + } + + bool IsImm(MIPSGPReg r) const { return reg_[r].isImm; } + u32 GetImm(MIPSGPReg r) const { return reg_[r].immVal; } + + void MapIn(MIPSGPReg rd); + void MapInIn(MIPSGPReg rs, MIPSGPReg rt); + void MapDirty(MIPSGPReg rd); + void MapDirtyIn(MIPSGPReg rd, MIPSGPReg rs); + void MapDirtyInIn(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt); + + void Start(IRWriter *ir); + void FlushAll(); + +private: + void Dirty(MIPSGPReg rd); + RegIR reg_[TOTAL_MAPPABLE_MIPSREGS]; + IRWriter *ir_; +}; diff --git a/Core/MIPS/JitCommon/JitCommon.cpp b/Core/MIPS/JitCommon/JitCommon.cpp index 91ed4c10d6..0b7fc19322 100644 --- a/Core/MIPS/JitCommon/JitCommon.cpp +++ b/Core/MIPS/JitCommon/JitCommon.cpp @@ -22,9 +22,11 @@ #include "Common/StringUtils.h" #include "Core/Util/DisArm64.h" +#include "Core/Config.h" #include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/MIPS/JitCommon/JitState.h" +#include "Core/MIPS/IR/IRJit.h" #if defined(ARM) #include "../ARM/ArmJit.h" @@ -45,17 +47,21 @@ namespace MIPSComp { } JitInterface *CreateNativeJit(MIPSState *mips) { + if (false && g_Config.iCpuCore == (int)CPUCore::CPU_JIT) { #if defined(ARM) - return new MIPSComp::ArmJit(mips); + return new MIPSComp::ArmJit(mips); #elif defined(ARM64) - return new MIPSComp::Arm64Jit(mips); + return new MIPSComp::IRJit(mips); #elif defined(_M_IX86) || defined(_M_X64) - return new MIPSComp::Jit(mips); + return new MIPSComp::Jit(mips); #elif defined(MIPS) - return new MIPSComp::MipsJit(mips); + return new MIPSComp::MipsJit(mips); #else - return new MIPSComp::FakeJit(mips); + return new MIPSComp::FakeJit(mips); #endif + } else if (true || g_Config.iCpuCore == (int)CPUCore::CPU_IRJIT) { + return new MIPSComp::IRJit(mips); + } } } diff --git a/Core/MIPS/MIPS.h b/Core/MIPS/MIPS.h index a24c2f3d8b..bbc9952c4d 100644 --- a/Core/MIPS/MIPS.h +++ b/Core/MIPS/MIPS.h @@ -166,6 +166,10 @@ public: float v[128]; u32 vi[128]; }; + // Used for temporary variables by IR Interpreter. + // Can be indexed through r[] using indices 192+. + u32 t[16]; + // Temps don't get flushed so we don't reserve space for them. // If vfpuCtrl (prefixes) get mysterious values, check the VFPU regcache code. u32 vfpuCtrl[16]; @@ -177,7 +181,7 @@ public: struct { u32 pc; - u32 lo; + u32 lo; // offset 192 + 16 + 16 + 1 + 1 u32 hi; u32 fcr31; //fpu control register