2012-11-01 15:19:01 +00:00
|
|
|
// 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2013-12-30 09:17:11 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2013-01-25 22:09:11 +00:00
|
|
|
#include "Common/Thunk.h"
|
2013-12-30 09:17:11 +00:00
|
|
|
#include "Core/MIPS/x86/Asm.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-26 03:25:14 +00:00
|
|
|
#if defined(ARM)
|
|
|
|
#error DO NOT BUILD X86 JIT ON ARM
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-25 22:09:11 +00:00
|
|
|
#include "Common/x64Emitter.h"
|
2013-04-26 21:58:20 +00:00
|
|
|
#include "Core/MIPS/JitCommon/JitBlockCache.h"
|
2013-11-08 17:51:52 +00:00
|
|
|
#include "Core/MIPS/JitCommon/JitState.h"
|
2014-05-04 22:59:58 +00:00
|
|
|
#include "Core/MIPS/x86/JitSafeMem.h"
|
2013-12-30 09:17:11 +00:00
|
|
|
#include "Core/MIPS/x86/RegCache.h"
|
|
|
|
#include "Core/MIPS/x86/RegCacheFPU.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-03-15 18:32:57 +00:00
|
|
|
class PointerWrap;
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
namespace MIPSComp
|
|
|
|
{
|
|
|
|
|
2013-06-29 18:22:58 +00:00
|
|
|
// This is called when Jit hits a breakpoint. Returns 1 when hit.
|
|
|
|
u32 JitBreakpoint();
|
2013-01-19 04:58:29 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
struct JitOptions
|
|
|
|
{
|
2013-11-11 03:38:42 +00:00
|
|
|
JitOptions();
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
bool enableBlocklink;
|
2013-08-16 08:05:52 +00:00
|
|
|
bool immBranches;
|
2013-08-16 08:07:11 +00:00
|
|
|
bool continueBranches;
|
2013-11-11 04:29:30 +00:00
|
|
|
bool continueJumps;
|
2013-08-16 08:07:11 +00:00
|
|
|
int continueMaxInstructions;
|
2014-11-18 04:37:27 +00:00
|
|
|
bool enableVFPUSIMD;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-08-16 06:13:40 +00:00
|
|
|
// TODO: Hmm, humongous.
|
|
|
|
struct RegCacheState {
|
|
|
|
GPRRegCacheState gpr;
|
|
|
|
FPURegCacheState fpr;
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
class Jit : public Gen::XCodeBlock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Jit(MIPSState *mips);
|
2013-12-10 12:06:57 +00:00
|
|
|
virtual ~Jit();
|
|
|
|
|
2013-02-18 09:14:57 +00:00
|
|
|
void DoState(PointerWrap &p);
|
2013-03-08 16:49:21 +00:00
|
|
|
static void DoDummyState(PointerWrap &p);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Compiled ops should ignore delay slots
|
|
|
|
// the compiler will take care of them by itself
|
|
|
|
// OR NOT
|
2013-08-24 21:43:49 +00:00
|
|
|
void Comp_Generic(MIPSOpcode op);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
void RunLoopUntil(u64 globalticks);
|
|
|
|
|
|
|
|
void Compile(u32 em_address); // Compiles a block at current MIPS PC
|
|
|
|
const u8 *DoJit(u32 em_address, JitBlock *b);
|
|
|
|
|
2013-12-19 07:57:39 +00:00
|
|
|
bool DescribeCodePtr(const u8 *ptr, std::string &name);
|
2013-12-01 02:21:47 +00:00
|
|
|
|
2013-08-24 21:43:49 +00:00
|
|
|
void Comp_RunBlock(MIPSOpcode op);
|
2013-11-30 19:57:44 +00:00
|
|
|
void Comp_ReplacementFunc(MIPSOpcode op);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Ops
|
2013-08-24 21:43:49 +00:00
|
|
|
void Comp_ITypeMem(MIPSOpcode op);
|
2013-11-10 20:23:39 +00:00
|
|
|
void Comp_Cache(MIPSOpcode op);
|
2013-08-24 21:43:49 +00:00
|
|
|
|
|
|
|
void Comp_RelBranch(MIPSOpcode op);
|
|
|
|
void Comp_RelBranchRI(MIPSOpcode op);
|
|
|
|
void Comp_FPUBranch(MIPSOpcode op);
|
|
|
|
void Comp_FPULS(MIPSOpcode op);
|
|
|
|
void Comp_FPUComp(MIPSOpcode op);
|
|
|
|
void Comp_Jump(MIPSOpcode op);
|
|
|
|
void Comp_JumpReg(MIPSOpcode op);
|
|
|
|
void Comp_Syscall(MIPSOpcode op);
|
|
|
|
void Comp_Break(MIPSOpcode op);
|
|
|
|
|
|
|
|
void Comp_IType(MIPSOpcode op);
|
|
|
|
void Comp_RType2(MIPSOpcode op);
|
|
|
|
void Comp_RType3(MIPSOpcode op);
|
|
|
|
void Comp_ShiftType(MIPSOpcode op);
|
|
|
|
void Comp_Allegrex(MIPSOpcode op);
|
|
|
|
void Comp_Allegrex2(MIPSOpcode op);
|
|
|
|
void Comp_VBranch(MIPSOpcode op);
|
|
|
|
void Comp_MulDivType(MIPSOpcode op);
|
|
|
|
void Comp_Special3(MIPSOpcode op);
|
|
|
|
|
|
|
|
void Comp_FPU3op(MIPSOpcode op);
|
|
|
|
void Comp_FPU2op(MIPSOpcode op);
|
|
|
|
void Comp_mxc1(MIPSOpcode op);
|
|
|
|
|
|
|
|
void Comp_SV(MIPSOpcode op);
|
|
|
|
void Comp_SVQ(MIPSOpcode op);
|
|
|
|
void Comp_VPFX(MIPSOpcode op);
|
|
|
|
void Comp_VVectorInit(MIPSOpcode op);
|
|
|
|
void Comp_VMatrixInit(MIPSOpcode op);
|
|
|
|
void Comp_VDot(MIPSOpcode op);
|
|
|
|
void Comp_VecDo3(MIPSOpcode op);
|
|
|
|
void Comp_VV2Op(MIPSOpcode op);
|
|
|
|
void Comp_Mftv(MIPSOpcode op);
|
2014-09-02 06:13:07 +00:00
|
|
|
void Comp_Vmfvc(MIPSOpcode op);
|
2013-08-24 21:43:49 +00:00
|
|
|
void Comp_Vmtvc(MIPSOpcode op);
|
|
|
|
void Comp_Vmmov(MIPSOpcode op);
|
|
|
|
void Comp_VScl(MIPSOpcode op);
|
|
|
|
void Comp_Vmmul(MIPSOpcode op);
|
|
|
|
void Comp_Vmscl(MIPSOpcode op);
|
|
|
|
void Comp_Vtfm(MIPSOpcode op);
|
|
|
|
void Comp_VHdp(MIPSOpcode op);
|
|
|
|
void Comp_VCrs(MIPSOpcode op);
|
|
|
|
void Comp_VDet(MIPSOpcode op);
|
|
|
|
void Comp_Vi2x(MIPSOpcode op);
|
|
|
|
void Comp_Vx2i(MIPSOpcode op);
|
|
|
|
void Comp_Vf2i(MIPSOpcode op);
|
|
|
|
void Comp_Vi2f(MIPSOpcode op);
|
2013-09-28 10:30:28 +00:00
|
|
|
void Comp_Vh2f(MIPSOpcode op);
|
2013-08-24 21:43:49 +00:00
|
|
|
void Comp_Vcst(MIPSOpcode op);
|
|
|
|
void Comp_Vhoriz(MIPSOpcode op);
|
|
|
|
void Comp_VRot(MIPSOpcode op);
|
|
|
|
void Comp_VIdt(MIPSOpcode op);
|
|
|
|
void Comp_Vcmp(MIPSOpcode op);
|
|
|
|
void Comp_Vcmov(MIPSOpcode op);
|
|
|
|
void Comp_Viim(MIPSOpcode op);
|
|
|
|
void Comp_Vfim(MIPSOpcode op);
|
|
|
|
void Comp_VCrossQuat(MIPSOpcode op);
|
2013-11-07 13:34:08 +00:00
|
|
|
void Comp_Vsgn(MIPSOpcode op);
|
2013-11-19 13:24:56 +00:00
|
|
|
void Comp_Vocp(MIPSOpcode op);
|
2013-08-24 21:43:49 +00:00
|
|
|
|
|
|
|
void Comp_DoNothing(MIPSOpcode op);
|
2013-02-10 11:14:55 +00:00
|
|
|
|
2013-12-17 22:40:27 +00:00
|
|
|
int Replace_fabsf();
|
2013-12-21 11:36:30 +00:00
|
|
|
int Replace_dl_write_matrix();
|
2013-12-17 22:40:27 +00:00
|
|
|
|
2013-01-26 00:33:32 +00:00
|
|
|
void ApplyPrefixST(u8 *vregs, u32 prefix, VectorSize sz);
|
2013-02-18 07:15:16 +00:00
|
|
|
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);
|
2013-02-17 23:50:31 +00:00
|
|
|
void EatPrefix() { js.EatPrefix(); }
|
2013-01-25 18:50:30 +00:00
|
|
|
|
2014-10-12 18:34:26 +00:00
|
|
|
void RestoreRoundingMode(bool force = false, XEmitter *emitter = NULL);
|
|
|
|
void ApplyRoundingMode(bool force = false, XEmitter *emitter = NULL);
|
|
|
|
void UpdateRoundingMode(XEmitter *emitter = NULL);
|
2014-08-22 16:48:00 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
JitBlockCache *GetBlockCache() { return &blocks; }
|
|
|
|
AsmRoutineManager &Asm() { return asm_; }
|
2012-12-28 23:09:17 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void ClearCache();
|
2014-06-19 08:08:45 +00:00
|
|
|
void InvalidateCache();
|
2014-07-05 23:25:16 +00:00
|
|
|
inline void InvalidateCacheAt(u32 em_address, int length = 4) {
|
|
|
|
if (blocks.RangeMayHaveEmuHacks(em_address, em_address + length)) {
|
|
|
|
blocks.InvalidateICache(em_address, length);
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 15:27:23 +00:00
|
|
|
|
2012-12-28 23:09:17 +00:00
|
|
|
private:
|
2013-08-16 06:13:40 +00:00
|
|
|
void GetStateAndFlushAll(RegCacheState &state);
|
|
|
|
void RestoreState(const RegCacheState state);
|
2012-11-01 15:19:01 +00:00
|
|
|
void FlushAll();
|
2013-02-15 09:12:43 +00:00
|
|
|
void FlushPrefixV();
|
2013-01-22 06:57:53 +00:00
|
|
|
void WriteDowncount(int offset = 0);
|
2013-12-18 15:27:23 +00:00
|
|
|
bool ReplaceJalTo(u32 dest);
|
2013-02-02 21:12:34 +00:00
|
|
|
// See CompileDelaySlotFlags for flags.
|
2013-08-16 08:07:11 +00:00
|
|
|
void CompileDelaySlot(int flags, RegCacheState *state = NULL);
|
|
|
|
void CompileDelaySlot(int flags, RegCacheState &state) {
|
|
|
|
CompileDelaySlot(flags, &state);
|
|
|
|
}
|
2013-08-24 21:43:49 +00:00
|
|
|
void EatInstruction(MIPSOpcode op);
|
2014-10-13 00:15:31 +00:00
|
|
|
void AddContinuedBlock(u32 dest);
|
2013-02-02 21:12:34 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void WriteExit(u32 destination, int exit_num);
|
2013-12-17 22:40:27 +00:00
|
|
|
void WriteExitDestInReg(X64Reg reg);
|
|
|
|
void WriteExitDestInEAX() { WriteExitDestInReg(EAX); }
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// void WriteRfiExitDestInEAX();
|
|
|
|
void WriteSyscallExit();
|
2013-01-22 03:41:12 +00:00
|
|
|
bool CheckJitBreakpoint(u32 addr, int downcountOffset);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Utility compilation functions
|
2013-08-24 21:43:49 +00:00
|
|
|
void BranchFPFlag(MIPSOpcode op, Gen::CCFlags cc, bool likely);
|
|
|
|
void BranchVFPUFlag(MIPSOpcode op, Gen::CCFlags cc, bool likely);
|
|
|
|
void BranchRSZeroComp(MIPSOpcode op, Gen::CCFlags cc, bool andLink, bool likely);
|
|
|
|
void BranchRSRTComp(MIPSOpcode op, Gen::CCFlags cc, bool likely);
|
|
|
|
void BranchLog(MIPSOpcode op);
|
|
|
|
void BranchLogExit(MIPSOpcode op, u32 dest, bool useEAX);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Utilities to reduce duplicated code
|
2013-08-24 21:43:49 +00:00
|
|
|
void CompImmLogic(MIPSOpcode op, void (XEmitter::*arith)(int, const OpArg &, const OpArg &));
|
2014-07-27 19:33:29 +00:00
|
|
|
void CompTriArith(MIPSOpcode op, void (XEmitter::*arith)(int, const OpArg &, const OpArg &), u32 (*doImm)(const u32, const u32), bool invertResult = false);
|
2013-08-24 21:43:49 +00:00
|
|
|
void CompShiftImm(MIPSOpcode op, void (XEmitter::*shift)(int, OpArg, OpArg), u32 (*doImm)(const u32, const u32));
|
|
|
|
void CompShiftVar(MIPSOpcode op, void (XEmitter::*shift)(int, OpArg, OpArg), u32 (*doImm)(const u32, const u32));
|
2014-01-18 17:57:13 +00:00
|
|
|
void CompITypeMemRead(MIPSOpcode op, u32 bits, void (XEmitter::*mov)(int, int, X64Reg, OpArg), const void *safeFunc);
|
|
|
|
template <typename T>
|
|
|
|
void CompITypeMemRead(MIPSOpcode op, u32 bits, void (XEmitter::*mov)(int, int, X64Reg, OpArg), T (*safeFunc)(u32 addr)) {
|
|
|
|
CompITypeMemRead(op, bits, mov, (const void *)safeFunc);
|
|
|
|
}
|
|
|
|
void CompITypeMemWrite(MIPSOpcode op, u32 bits, const void *safeFunc);
|
|
|
|
template <typename T>
|
|
|
|
void CompITypeMemWrite(MIPSOpcode op, u32 bits, void (*safeFunc)(T val, u32 addr)) {
|
|
|
|
CompITypeMemWrite(op, bits, (const void *)safeFunc);
|
|
|
|
}
|
2013-08-24 21:43:49 +00:00
|
|
|
void CompITypeMemUnpairedLR(MIPSOpcode op, bool isStore);
|
|
|
|
void CompITypeMemUnpairedLRInner(MIPSOpcode op, X64Reg shiftReg);
|
2013-11-12 08:45:28 +00:00
|
|
|
void CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool delaySlotIsNice, bool likely, bool andLink);
|
2014-10-12 19:37:54 +00:00
|
|
|
void CompBranchExit(bool taken, u32 targetAddr, u32 notTakenAddr, bool delaySlotIsNice, bool likely, bool andLink);
|
2014-11-09 16:15:39 +00:00
|
|
|
static CCFlags FlipCCFlag(CCFlags flag);
|
|
|
|
static CCFlags SwapCCFlag(CCFlags flag);
|
2013-08-24 21:43:49 +00:00
|
|
|
|
|
|
|
void CompFPTriArith(MIPSOpcode op, void (XEmitter::*arith)(X64Reg reg, OpArg), bool orderMatters);
|
2013-02-13 09:51:49 +00:00
|
|
|
void CompFPComp(int lhs, int rhs, u8 compare, bool allowNaN = false);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-01-18 17:57:13 +00:00
|
|
|
void CallProtectedFunction(const void *func, const OpArg &arg1);
|
|
|
|
void CallProtectedFunction(const void *func, const OpArg &arg1, const OpArg &arg2);
|
|
|
|
void CallProtectedFunction(const void *func, const u32 arg1, const u32 arg2, const u32 arg3);
|
|
|
|
void CallProtectedFunction(const void *func, const OpArg &arg1, const u32 arg2, const u32 arg3);
|
|
|
|
|
|
|
|
template <typename Tr, typename T1>
|
|
|
|
void CallProtectedFunction(Tr (*func)(T1), const OpArg &arg1) {
|
|
|
|
CallProtectedFunction((const void *)func, arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Tr, typename T1, typename T2>
|
|
|
|
void CallProtectedFunction(Tr (*func)(T1, T2), const OpArg &arg1, const OpArg &arg2) {
|
|
|
|
CallProtectedFunction((const void *)func, arg1, arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Tr, typename T1, typename T2, typename T3>
|
|
|
|
void CallProtectedFunction(Tr (*func)(T1, T2, T3), const u32 arg1, const u32 arg2, const u32 arg3) {
|
|
|
|
CallProtectedFunction((const void *)func, arg1, arg2, arg3);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Tr, typename T1, typename T2, typename T3>
|
|
|
|
void CallProtectedFunction(Tr (*func)(T1, T2, T3), const OpArg &arg1, const u32 arg2, const u32 arg3) {
|
|
|
|
CallProtectedFunction((const void *)func, arg1, arg2, arg3);
|
|
|
|
}
|
2013-07-06 07:54:53 +00:00
|
|
|
|
2013-11-11 06:50:23 +00:00
|
|
|
bool PredictTakeBranch(u32 targetAddr, bool likely);
|
2014-10-13 02:01:04 +00:00
|
|
|
bool CanContinueBranch(u32 targetAddr) {
|
2013-08-16 08:07:11 +00:00
|
|
|
if (!jo.continueBranches || js.numInstructions >= jo.continueMaxInstructions) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Need at least 2 exits left over.
|
2013-11-11 06:50:23 +00:00
|
|
|
if (js.nextExit >= MAX_JIT_BLOCK_EXITS - 2) {
|
2013-08-16 08:07:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-13 02:01:04 +00:00
|
|
|
// Sometimes we predict wrong and get into impossible conditions where games have jumps to 0.
|
|
|
|
if (!targetAddr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool CanContinueJump(u32 targetAddr) {
|
|
|
|
if (!jo.continueJumps || js.numInstructions >= jo.continueMaxInstructions) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!targetAddr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool CanContinueImmBranch(u32 targetAddr) {
|
|
|
|
if (!jo.immBranches || js.numInstructions >= jo.continueMaxInstructions) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-16 08:07:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
JitBlockCache blocks;
|
|
|
|
JitOptions jo;
|
|
|
|
JitState js;
|
|
|
|
|
|
|
|
GPRRegCache gpr;
|
|
|
|
FPURegCache fpr;
|
|
|
|
|
|
|
|
AsmRoutineManager asm_;
|
2013-01-19 18:49:19 +00:00
|
|
|
ThunkManager thunks;
|
2014-05-04 22:59:58 +00:00
|
|
|
JitSafeMemFuncs safeMemFuncs;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
MIPSState *mips_;
|
2013-01-26 16:42:34 +00:00
|
|
|
|
|
|
|
friend class JitSafeMem;
|
2014-05-04 22:59:58 +00:00
|
|
|
friend class JitSafeMemFuncs;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-08-24 21:43:49 +00:00
|
|
|
typedef void (Jit::*MIPSCompileFunc)(MIPSOpcode opcode);
|
2013-12-17 22:40:27 +00:00
|
|
|
typedef int (Jit::*MIPSReplaceFunc)();
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
} // namespace MIPSComp
|
|
|
|
|