diff --git a/CMakeLists.txt b/CMakeLists.txt index 655d8f7e13..7ce5de115b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1084,6 +1084,8 @@ set(CoreExtra ${CoreExtra} Core/MIPS/IR/IRCompFPU.cpp Core/MIPS/IR/IRCompLoadStore.cpp Core/MIPS/IR/IRCompVFPU.cpp + Core/MIPS/IR/IRFrontend.cpp + Core/MIPS/IR/IRFrontend.h Core/MIPS/IR/IRInst.cpp Core/MIPS/IR/IRInst.h Core/MIPS/IR/IRInterpreter.cpp diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 561d83d2d3..60d9c7c66c 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -187,6 +187,7 @@ + @@ -518,6 +519,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index 0fc92ec2fa..600070d1e8 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -670,6 +670,9 @@ MIPS\IR + + MIPS\IR + @@ -1230,6 +1233,9 @@ MIPS\IR + + MIPS\IR + diff --git a/Core/MIPS/IR/IRFrontend.cpp b/Core/MIPS/IR/IRFrontend.cpp new file mode 100644 index 0000000000..94a7ebe105 --- /dev/null +++ b/Core/MIPS/IR/IRFrontend.cpp @@ -0,0 +1,288 @@ +// 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 "Common/ChunkFile.h" +#include "Core/Reporting.h" +#include "Core/MemMap.h" + +#include "Core/MIPS/MIPSTables.h" +#include "Core/HLE/ReplaceTables.h" + +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRPassSimplify.h" +#include "Core/MIPS/IR/IRInterpreter.h" + +namespace MIPSComp { + +IRFrontend::IRFrontend(bool startDefaultPrefix) { + logBlocks = 0; + dontLogBlocks = 0; + js.startDefaultPrefix = startDefaultPrefix; + // js.currentRoundingFunc = convertS0ToSCRATCH1[0]; +} + +void IRFrontend::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; + } +} + +void IRFrontend::FlushAll() { + FlushPrefixV(); +} + +void IRFrontend::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 IRFrontend::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 IRFrontend::CompileDelaySlot() { + js.inDelaySlot = true; + MIPSOpcode op = GetOffsetInstruction(1); + MIPSCompileOp(op, this); + js.inDelaySlot = false; +} + +bool IRFrontend::CheckRounding() { + 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"); + js.LogPrefix(); + + // Let's try that one more time. We won't get back here because we toggled the value. + js.startDefaultPrefix = false; + // TODO: Make sure this works. + // cleanSlate = true; + } + + return cleanSlate; +} + + +void IRFrontend::Comp_ReplacementFunc(MIPSOpcode op) { + int index = op.encoding & MIPS_EMUHACK_VALUE_MASK; + + const ReplacementTableEntry *entry = GetReplacementFunc(index); + if (!entry) { + ERROR_LOG(HLE, "Invalid replacement op %08x", op.encoding); + return; + } + + if (entry->flags & REPFLAG_DISABLED) { + MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this); + } else if (entry->replaceFunc) { + FlushAll(); + RestoreRoundingMode(); + ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC())); + ir.Write(IROp::CallReplacement, 0, ir.AddConstant(index)); + + if (entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) { + // Compile the original instruction at this address. We ignore cycles for hooks. + ApplyRoundingMode(); + MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this); + } else { + ApplyRoundingMode(); + ir.Write(IROp::Downcount, 0, js.downcountAmount & 0xFF, js.downcountAmount >> 8); + ir.Write(IROp::ExitToReg, MIPS_REG_RA, 0, 0); + js.compiling = false; + } + } else { + ERROR_LOG(HLE, "Replacement function %s has neither jit nor regular impl", entry->name); + } +} + +void IRFrontend::Comp_Generic(MIPSOpcode op) { + FlushAll(); + ir.Write(IROp::Interpret, 0, 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 IRFrontend::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 IRFrontend::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 IRFrontend::UpdateRoundingMode() { + ir.Write(IROp::UpdateRoundingMode); +} + +void IRFrontend::Comp_DoNothing(MIPSOpcode op) { +} + +int IRFrontend::Replace_fabsf() { + Crash(); + return 0; +} + +u32 IRFrontend::GetCompilerPC() { + return js.compilerPC; +} + +MIPSOpcode IRFrontend::GetOffsetInstruction(int offset) { + return Memory::Read_Instruction(GetCompilerPC() + 4 * offset); +} + +void IRFrontend::DoJit(u32 em_address, IRBlock *b) { + js.cancel = false; + js.blockStart = em_address; + js.compilerPC = em_address; + 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(); + + 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++; + + if (ir.GetConstants().size() > 64) { + // Need to break the block + ir.Write(IROp::ExitToConst, ir.AddConstant(js.compilerPC)); + js.compiling = false; + } + } + + IRWriter simplified; + IRWriter *code = &ir; + if (true) { + static const IRPassFunc passes[] = { + &PropagateConstants, + }; + if (IRApplyPasses(passes, ARRAY_SIZE(passes), ir, simplified)) + logBlocks = 1; + code = &simplified; + if (ir.GetInstructions().size() >= 24) + logBlocks = 1; + } + + b->SetInstructions(code->GetInstructions(), code->GetConstants()); + + if (logBlocks > 0 && dontLogBlocks == 0) { + char temp2[256]; + ILOG("=============== mips %08x ===============", em_address); + for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) { + temp2[0] = 0; + MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, true); + ILOG("M: %08x %s", cpc, temp2); + } + } + + if (logBlocks > 0 && dontLogBlocks == 0) { + ILOG("=============== Original IR (%d instructions, %d const) ===============", (int)ir.GetInstructions().size(), (int)ir.GetConstants().size()); + for (int i = 0; i < ir.GetInstructions().size(); i++) { + char buf[256]; + DisassembleIR(buf, sizeof(buf), ir.GetInstructions()[i], ir.GetConstants().data()); + ILOG("%s", buf); + } + ILOG("=============== end ================="); + } + + if (logBlocks > 0 && dontLogBlocks == 0) { + ILOG("=============== IR (%d instructions, %d const) ===============", (int)code->GetInstructions().size(), (int)code->GetConstants().size()); + for (int i = 0; i < code->GetInstructions().size(); i++) { + char buf[256]; + DisassembleIR(buf, sizeof(buf), code->GetInstructions()[i], code->GetConstants().data()); + ILOG("%s", buf); + } + ILOG("=============== end ================="); + } + + if (logBlocks > 0) + logBlocks--; + if (dontLogBlocks > 0) + dontLogBlocks--; +} + +void IRFrontend::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!"); +} + + +} // namespace \ No newline at end of file diff --git a/Core/MIPS/IR/IRFrontend.h b/Core/MIPS/IR/IRFrontend.h new file mode 100644 index 0000000000..c5854dffdf --- /dev/null +++ b/Core/MIPS/IR/IRFrontend.h @@ -0,0 +1,144 @@ +#pragma once + +#include "Common/CommonTypes.h" +#include "Core/MIPS/JitCommon/JitCommon.h" +#include "Core/MIPS/JitCommon/JitState.h" +#include "Core/MIPS/MIPSVFPUUtils.h" +#include "Core/MIPS/IR/IRInst.h" + +namespace MIPSComp { + +class IRBlock; + +class IRFrontend : public MIPSFrontendInterface { +public: + IRFrontend(bool startDefaultPrefix); + void Comp_Generic(MIPSOpcode op) 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(); + void DoState(PointerWrap &p); + bool CheckRounding(); // returns true if we need a do-over + void DoJit(u32 em_address, IRBlock *b); + +private: + void RestoreRoundingMode(bool force = false); + void ApplyRoundingMode(bool force = false); + void UpdateRoundingMode(); + + void EatPrefix() { js.EatPrefix(); } + + void FlushAll(); + void FlushPrefixV(); + + u32 GetCompilerPC(); + void CompileDelaySlot(); + void EatInstruction(MIPSOpcode op); + MIPSOpcode GetOffsetInstruction(int offset); + + // 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 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); + + // State + JitState js; + IRWriter ir; + + int dontLogBlocks; + int logBlocks; +}; + +} // namespace \ No newline at end of file diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp index 9ed4f2eb70..94ab72bfd9 100644 --- a/Core/MIPS/IR/IRJit.cpp +++ b/Core/MIPS/IR/IRJit.cpp @@ -18,7 +18,6 @@ #include "base/logging.h" #include "profiler/profiler.h" #include "Common/ChunkFile.h" -#include "Common/CPUDetect.h" #include "Common/StringUtils.h" #include "Core/Reporting.h" @@ -32,7 +31,6 @@ #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" @@ -51,27 +49,6 @@ IRJit::IRJit(MIPSState *mips) : mips_(mips), frontend_(mips->HasDefaultPrefix()) IRJit::~IRJit() { } -IRFrontend::IRFrontend(bool startDefaultPrefix) { - logBlocks = 0; - dontLogBlocks = 0; - js.startDefaultPrefix = startDefaultPrefix; - // js.currentRoundingFunc = convertS0ToSCRATCH1[0]; -} - -void IRFrontend::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; - } -} - void IRJit::DoState(PointerWrap &p) { frontend_.DoState(p); } @@ -90,27 +67,6 @@ void IRJit::DoDummyState(PointerWrap &p) { } } -void IRFrontend::FlushAll() { - FlushPrefixV(); -} - -void IRFrontend::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("IRJit: Clearing the cache!"); blocks_.Clear(); @@ -124,50 +80,6 @@ void IRJit::InvalidateCacheAt(u32 em_address, int length) { blocks_.InvalidateICache(em_address, length); } -void IRFrontend::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 IRFrontend::CompileDelaySlot() { - js.inDelaySlot = true; - MIPSOpcode op = GetOffsetInstruction(1); - MIPSCompileOp(op, this); - js.inDelaySlot = false; -} - -bool IRFrontend::CheckRounding() { - 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"); - js.LogPrefix(); - - // Let's try that one more time. We won't get back here because we toggled the value. - js.startDefaultPrefix = false; - // TODO: Make sure this works. - // cleanSlate = true; - } - - return cleanSlate; -} - void IRJit::Compile(u32 em_address) { PROFILE_THIS_SCOPE("jitc"); @@ -214,104 +126,11 @@ void IRJit::RunLoopUntil(u64 globalticks) { // RestoreRoundingMode(true); } -u32 IRFrontend::GetCompilerPC() { - return js.compilerPC; -} - -MIPSOpcode IRFrontend::GetOffsetInstruction(int offset) { - return Memory::Read_Instruction(GetCompilerPC() + 4 * offset); -} - -void IRFrontend::DoJit(u32 em_address, IRBlock *b) { - js.cancel = false; - js.blockStart = em_address; - js.compilerPC = em_address; - 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(); - - 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++; - - if (ir.GetConstants().size() > 64) { - // Need to break the block - ir.Write(IROp::ExitToConst, ir.AddConstant(js.compilerPC)); - js.compiling = false; - } - } - - IRWriter simplified; - IRWriter *code = &ir; - if (true) { - static const IRPassFunc passes[] = { - &PropagateConstants, - }; - if (IRApplyPasses(passes, ARRAY_SIZE(passes), ir, simplified)) - logBlocks = 1; - code = &simplified; - if (ir.GetInstructions().size() >= 24) - logBlocks = 1; - } - - b->SetInstructions(code->GetInstructions(), code->GetConstants()); - - if (logBlocks > 0 && dontLogBlocks == 0) { - char temp2[256]; - ILOG("=============== mips %08x ===============", em_address); - for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) { - temp2[0] = 0; - MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, true); - ILOG("M: %08x %s", cpc, temp2); - } - } - - if (logBlocks > 0 && dontLogBlocks == 0) { - ILOG("=============== Original IR (%d instructions, %d const) ===============", (int)ir.GetInstructions().size(), (int)ir.GetConstants().size()); - for (int i = 0; i < ir.GetInstructions().size(); i++) { - char buf[256]; - DisassembleIR(buf, sizeof(buf), ir.GetInstructions()[i], ir.GetConstants().data()); - ILOG("%s", buf); - } - ILOG("=============== end ================="); - } - - if (logBlocks > 0 && dontLogBlocks == 0) { - ILOG("=============== IR (%d instructions, %d const) ===============", (int)code->GetInstructions().size(), (int)code->GetConstants().size()); - for (int i = 0; i < code->GetInstructions().size(); i++) { - char buf[256]; - DisassembleIR(buf, sizeof(buf), code->GetInstructions()[i], code->GetConstants().data()); - ILOG("%s", buf); - } - ILOG("=============== end ================="); - } - - if (logBlocks > 0) - logBlocks--; - if (dontLogBlocks > 0) - dontLogBlocks--; -} - bool IRJit::DescribeCodePtr(const u8 *ptr, std::string &name) { // Used in disassembly viewer. return false; } -void IRFrontend::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(); } @@ -325,78 +144,6 @@ bool IRJit::ReplaceJalTo(u32 dest) { return false; } -void IRFrontend::Comp_ReplacementFunc(MIPSOpcode op) { - int index = op.encoding & MIPS_EMUHACK_VALUE_MASK; - - const ReplacementTableEntry *entry = GetReplacementFunc(index); - if (!entry) { - ERROR_LOG(HLE, "Invalid replacement op %08x", op.encoding); - return; - } - - if (entry->flags & REPFLAG_DISABLED) { - MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this); - } else if (entry->replaceFunc) { - FlushAll(); - RestoreRoundingMode(); - ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC())); - ir.Write(IROp::CallReplacement, 0, ir.AddConstant(index)); - - if (entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) { - // Compile the original instruction at this address. We ignore cycles for hooks. - ApplyRoundingMode(); - MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this); - } else { - ApplyRoundingMode(); - ir.Write(IROp::Downcount, 0, js.downcountAmount & 0xFF, js.downcountAmount >> 8); - ir.Write(IROp::ExitToReg, MIPS_REG_RA, 0, 0); - js.compiling = false; - } - } else { - ERROR_LOG(HLE, "Replacement function %s has neither jit nor regular impl", entry->name); - } -} - -void IRFrontend::Comp_Generic(MIPSOpcode op) { - FlushAll(); - ir.Write(IROp::Interpret, 0, 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 IRFrontend::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 IRFrontend::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 IRFrontend::UpdateRoundingMode() { - ir.Write(IROp::UpdateRoundingMode); -} - -void IRFrontend::Comp_DoNothing(MIPSOpcode op) { -} - -int IRFrontend::Replace_fabsf() { - Crash(); - return 0; -} - void IRBlockCache::Clear() { blocks_.clear(); } diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h index 3947136ea9..aa026b0bd8 100644 --- a/Core/MIPS/IR/IRJit.h +++ b/Core/MIPS/IR/IRJit.h @@ -20,11 +20,11 @@ #include #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/IR/IRFrontend.h" #include "Core/MIPS/MIPSVFPUUtils.h" #ifndef offsetof @@ -101,137 +101,6 @@ private: std::vector blocks_; }; -class IRFrontend : public MIPSFrontendInterface { -public: - IRFrontend(bool startDefaultPrefix); - void Comp_Generic(MIPSOpcode op) 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(); - void DoState(PointerWrap &p); - bool CheckRounding(); // returns true if we need a do-over - void DoJit(u32 em_address, IRBlock *b); - -private: - void RestoreRoundingMode(bool force = false); - void ApplyRoundingMode(bool force = false); - void UpdateRoundingMode(); - - void EatPrefix() { js.EatPrefix(); } - - void FlushAll(); - void FlushPrefixV(); - - u32 GetCompilerPC(); - void CompileDelaySlot(); - void EatInstruction(MIPSOpcode op); - MIPSOpcode GetOffsetInstruction(int offset); - - // 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 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); - - // State - JitState js; - IRWriter ir; - - int dontLogBlocks; - int logBlocks; -}; - class IRJit : public JitInterface { public: IRJit(MIPSState *mips); diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 92a10e800c..115b969972 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -157,6 +157,7 @@ EXEC_AND_LIB_FILES := \ $(SRC)/Core/MIPS/MIPSVFPUUtils.cpp.arm \ $(SRC)/Core/MIPS/MIPSCodeUtils.cpp.arm \ $(SRC)/Core/MIPS/MIPSDebugInterface.cpp \ + $(SRC)/Core/MIPS/IR/IRFrontend.cpp \ $(SRC)/Core/MIPS/IR/IRJit.cpp \ $(SRC)/Core/MIPS/IR/IRCompALU.cpp \ $(SRC)/Core/MIPS/IR/IRCompBranch.cpp \