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-01-25 22:09:11 +00:00
|
|
|
#include "Globals.h"
|
|
|
|
#include "Common/Thunk.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "Asm.h"
|
|
|
|
|
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"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "JitCache.h"
|
|
|
|
#include "RegCache.h"
|
2013-01-25 22:09:11 +00:00
|
|
|
#include "RegCacheFPU.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
namespace MIPSComp
|
|
|
|
{
|
|
|
|
|
2013-01-19 04:58:29 +00:00
|
|
|
// This is called when Jit hits a breakpoint.
|
|
|
|
void JitBreakpoint();
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
struct JitOptions
|
|
|
|
{
|
|
|
|
JitOptions()
|
|
|
|
{
|
2013-01-11 23:44:18 +00:00
|
|
|
enableBlocklink = true;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool enableBlocklink;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JitState
|
|
|
|
{
|
2013-02-15 09:12:43 +00:00
|
|
|
enum PrefixState
|
|
|
|
{
|
|
|
|
PREFIX_UNKNOWN = 0x00,
|
|
|
|
PREFIX_KNOWN = 0x01,
|
|
|
|
PREFIX_DIRTY = 0x10,
|
|
|
|
PREFIX_KNOWN_DIRTY = 0x11,
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 compilerPC;
|
|
|
|
u32 blockStart;
|
|
|
|
bool cancel;
|
|
|
|
bool inDelaySlot;
|
|
|
|
int downcountAmount;
|
2013-02-02 21:12:34 +00:00
|
|
|
int numInstructions;
|
2012-11-01 15:19:01 +00:00
|
|
|
bool compiling; // TODO: get rid of this in favor of using analysis results to determine end of block
|
|
|
|
JitBlock *curBlock;
|
2013-01-26 00:33:32 +00:00
|
|
|
|
|
|
|
// VFPU prefix magic
|
2013-02-18 09:14:57 +00:00
|
|
|
bool startDefaultPrefix;
|
2013-01-26 00:33:32 +00:00
|
|
|
u32 prefixS;
|
|
|
|
u32 prefixT;
|
|
|
|
u32 prefixD;
|
2013-02-15 09:12:43 +00:00
|
|
|
PrefixState prefixSFlag;
|
|
|
|
PrefixState prefixTFlag;
|
|
|
|
PrefixState prefixDFlag;
|
2013-01-26 00:33:32 +00:00
|
|
|
void PrefixStart() {
|
2013-02-18 09:14:57 +00:00
|
|
|
if (startDefaultPrefix) {
|
|
|
|
EatPrefix();
|
|
|
|
} else {
|
|
|
|
PrefixUnknown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void PrefixUnknown() {
|
2013-02-15 09:12:43 +00:00
|
|
|
prefixSFlag = PREFIX_UNKNOWN;
|
|
|
|
prefixTFlag = PREFIX_UNKNOWN;
|
|
|
|
prefixDFlag = PREFIX_UNKNOWN;
|
2013-01-26 00:33:32 +00:00
|
|
|
}
|
2013-02-15 07:47:03 +00:00
|
|
|
bool MayHavePrefix() const {
|
2013-02-18 06:37:56 +00:00
|
|
|
if (HasUnknownPrefix()) {
|
2013-02-15 07:47:03 +00:00
|
|
|
return true;
|
|
|
|
} else if (prefixS != 0xE4 || prefixT != 0xE4 || prefixD != 0) {
|
|
|
|
return true;
|
2013-02-18 06:46:12 +00:00
|
|
|
} else if (VfpuWriteMask() != 0) {
|
2013-02-15 07:47:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-18 06:37:56 +00:00
|
|
|
bool HasUnknownPrefix() const {
|
|
|
|
if (!(prefixSFlag & PREFIX_KNOWN) || !(prefixTFlag & PREFIX_KNOWN) || !(prefixDFlag & PREFIX_KNOWN)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-26 00:33:32 +00:00
|
|
|
void EatPrefix() {
|
2013-02-15 09:12:43 +00:00
|
|
|
if ((prefixSFlag & PREFIX_KNOWN) == 0 || prefixS != 0xE4) {
|
|
|
|
prefixSFlag = PREFIX_KNOWN_DIRTY;
|
|
|
|
prefixS = 0xE4;
|
|
|
|
}
|
|
|
|
if ((prefixTFlag & PREFIX_KNOWN) == 0 || prefixT != 0xE4) {
|
|
|
|
prefixTFlag = PREFIX_KNOWN_DIRTY;
|
|
|
|
prefixT = 0xE4;
|
|
|
|
}
|
2013-02-18 06:46:12 +00:00
|
|
|
if ((prefixDFlag & PREFIX_KNOWN) == 0 || prefixD != 0x0 || VfpuWriteMask() != 0) {
|
2013-02-15 09:12:43 +00:00
|
|
|
prefixDFlag = PREFIX_KNOWN_DIRTY;
|
|
|
|
prefixD = 0x0;
|
|
|
|
}
|
2013-01-26 00:33:32 +00:00
|
|
|
}
|
2013-02-18 06:46:12 +00:00
|
|
|
u8 VfpuWriteMask() const {
|
|
|
|
_assert_(prefixDFlag & JitState::PREFIX_KNOWN);
|
|
|
|
return (prefixD >> 8) & 0xF;
|
|
|
|
}
|
|
|
|
bool VfpuWriteMask(int i) const {
|
|
|
|
_assert_(prefixDFlag & JitState::PREFIX_KNOWN);
|
|
|
|
return (prefixD >> (8 + i)) & 1;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-01-24 09:56:47 +00:00
|
|
|
enum CompileDelaySlotFlags
|
|
|
|
{
|
|
|
|
// Easy, nothing extra.
|
|
|
|
DELAYSLOT_NICE = 0,
|
|
|
|
// Flush registers after delay slot.
|
|
|
|
DELAYSLOT_FLUSH = 1,
|
|
|
|
// Preserve flags.
|
|
|
|
DELAYSLOT_SAFE = 2,
|
|
|
|
// Flush registers after and preserve flags.
|
|
|
|
DELAYSLOT_SAFE_FLUSH = DELAYSLOT_FLUSH | DELAYSLOT_SAFE,
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
class Jit : public Gen::XCodeBlock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Jit(MIPSState *mips);
|
2013-02-18 09:14:57 +00:00
|
|
|
void DoState(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
|
|
|
|
void Comp_Generic(u32 op);
|
|
|
|
|
|
|
|
void RunLoopUntil(u64 globalticks);
|
|
|
|
|
|
|
|
void Compile(u32 em_address); // Compiles a block at current MIPS PC
|
|
|
|
const u8 *DoJit(u32 em_address, JitBlock *b);
|
|
|
|
|
|
|
|
void CompileAt(u32 addr);
|
|
|
|
void Comp_RunBlock(u32 op);
|
|
|
|
|
|
|
|
// Ops
|
|
|
|
void Comp_ITypeMem(u32 op);
|
|
|
|
|
|
|
|
void Comp_RelBranch(u32 op);
|
|
|
|
void Comp_RelBranchRI(u32 op);
|
|
|
|
void Comp_FPUBranch(u32 op);
|
|
|
|
void Comp_FPULS(u32 op);
|
2013-02-10 11:14:55 +00:00
|
|
|
void Comp_FPUComp(u32 op);
|
2012-11-01 15:19:01 +00:00
|
|
|
void Comp_Jump(u32 op);
|
|
|
|
void Comp_JumpReg(u32 op);
|
|
|
|
void Comp_Syscall(u32 op);
|
2013-02-01 08:49:14 +00:00
|
|
|
void Comp_Break(u32 op);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
void Comp_IType(u32 op);
|
|
|
|
void Comp_RType3(u32 op);
|
|
|
|
void Comp_ShiftType(u32 op);
|
|
|
|
void Comp_Allegrex(u32 op);
|
|
|
|
void Comp_VBranch(u32 op);
|
2013-01-28 23:48:42 +00:00
|
|
|
void Comp_MulDivType(u32 op);
|
2013-02-10 11:14:55 +00:00
|
|
|
void Comp_Special3(u32 op);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
void Comp_FPU3op(u32 op);
|
|
|
|
void Comp_FPU2op(u32 op);
|
|
|
|
void Comp_mxc1(u32 op);
|
|
|
|
|
2013-02-10 11:14:55 +00:00
|
|
|
void Comp_SV(u32 op);
|
2013-01-25 18:50:30 +00:00
|
|
|
void Comp_SVQ(u32 op);
|
2013-01-26 00:33:32 +00:00
|
|
|
void Comp_VPFX(u32 op);
|
|
|
|
void Comp_VDot(u32 op);
|
2013-02-15 08:29:40 +00:00
|
|
|
void Comp_VecDo3(u32 op);
|
2013-02-19 04:02:58 +00:00
|
|
|
void Comp_VV2Op(u32 op);
|
2013-02-10 11:14:55 +00:00
|
|
|
void Comp_Mftv(u32 op);
|
2013-02-14 08:25:32 +00:00
|
|
|
void Comp_Vmtvc(u32 op);
|
2013-02-10 11:14:55 +00:00
|
|
|
|
|
|
|
void Comp_DoNothing(u32 op);
|
|
|
|
|
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
|
|
|
|
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();
|
2013-01-18 18:43:40 +00:00
|
|
|
void ClearCacheAt(u32 em_address);
|
2012-12-28 23:09:17 +00:00
|
|
|
private:
|
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);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-02-02 21:12:34 +00:00
|
|
|
// See CompileDelaySlotFlags for flags.
|
|
|
|
void CompileDelaySlot(int flags);
|
|
|
|
void EatInstruction(u32 op);
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void WriteExit(u32 destination, int exit_num);
|
|
|
|
void WriteExitDestInEAX();
|
|
|
|
// 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
|
|
|
|
void BranchFPFlag(u32 op, Gen::CCFlags cc, bool likely);
|
|
|
|
void BranchVFPUFlag(u32 op, Gen::CCFlags cc, bool likely);
|
2013-01-22 16:04:01 +00:00
|
|
|
void BranchRSZeroComp(u32 op, Gen::CCFlags cc, bool andLink, bool likely);
|
2012-11-01 15:19:01 +00:00
|
|
|
void BranchRSRTComp(u32 op, Gen::CCFlags cc, bool likely);
|
2013-01-21 02:48:54 +00:00
|
|
|
void BranchLog(u32 op);
|
|
|
|
void BranchLogExit(u32 op, u32 dest, bool useEAX);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Utilities to reduce duplicated code
|
|
|
|
void CompImmLogic(u32 op, void (XEmitter::*arith)(int, const OpArg &, const OpArg &));
|
2013-01-25 08:02:03 +00:00
|
|
|
void CompTriArith(u32 op, void (XEmitter::*arith)(int, const OpArg &, const OpArg &), u32 (*doImm)(const u32, const u32));
|
2012-11-01 15:19:01 +00:00
|
|
|
void CompShiftImm(u32 op, void (XEmitter::*shift)(int, OpArg, OpArg));
|
|
|
|
void CompShiftVar(u32 op, void (XEmitter::*shift)(int, OpArg, OpArg));
|
2013-01-20 00:02:56 +00:00
|
|
|
void CompITypeMemRead(u32 op, u32 bits, void (XEmitter::*mov)(int, int, X64Reg, OpArg), void *safeFunc);
|
|
|
|
void CompITypeMemWrite(u32 op, u32 bits, void *safeFunc);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
void CompFPTriArith(u32 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
|
|
|
|
|
|
|
JitBlockCache blocks;
|
|
|
|
JitOptions jo;
|
|
|
|
JitState js;
|
|
|
|
|
|
|
|
GPRRegCache gpr;
|
|
|
|
FPURegCache fpr;
|
|
|
|
|
|
|
|
AsmRoutineManager asm_;
|
2013-01-19 18:49:19 +00:00
|
|
|
ThunkManager thunks;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
MIPSState *mips_;
|
2013-01-26 16:42:34 +00:00
|
|
|
|
|
|
|
class JitSafeMem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JitSafeMem(Jit *jit, int raddr, s32 offset);
|
|
|
|
|
|
|
|
// Emit code necessary for a memory write, returns true if MOV to dest is needed.
|
|
|
|
bool PrepareWrite(OpArg &dest);
|
|
|
|
// Emit code proceeding a slow write call, returns true if slow write is needed.
|
|
|
|
bool PrepareSlowWrite();
|
|
|
|
// Emit a slow write from src.
|
2013-01-26 18:07:05 +00:00
|
|
|
void DoSlowWrite(void *safeFunc, const OpArg src, int suboffset = 0);
|
2013-01-26 16:42:34 +00:00
|
|
|
|
|
|
|
// Emit code necessary for a memory read, returns true if MOV from src is needed.
|
2013-01-26 17:23:43 +00:00
|
|
|
bool PrepareRead(OpArg &src);
|
2013-01-26 16:42:34 +00:00
|
|
|
// Emit code for a slow read call, and returns true if result is in EAX.
|
|
|
|
bool PrepareSlowRead(void *safeFunc);
|
2013-01-26 18:07:05 +00:00
|
|
|
|
2013-01-27 07:08:19 +00:00
|
|
|
// Cleans up final code for the memory access.
|
|
|
|
void Finish();
|
|
|
|
|
|
|
|
// Use this before anything else if you're gonna use the below.
|
|
|
|
void SetFar();
|
2013-01-26 18:07:05 +00:00
|
|
|
// WARNING: Only works for non-GPR. Do not use for reads into GPR.
|
|
|
|
OpArg NextFastAddress(int suboffset);
|
|
|
|
// WARNING: Only works for non-GPR. Do not use for reads into GPR.
|
|
|
|
void NextSlowRead(void *safeFunc, int suboffset);
|
2013-01-26 16:42:34 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
OpArg PrepareMemoryOpArg();
|
|
|
|
void PrepareSlowAccess();
|
|
|
|
|
|
|
|
Jit *jit_;
|
|
|
|
int raddr_;
|
|
|
|
s32 offset_;
|
|
|
|
bool needsCheck_;
|
|
|
|
bool needsSkip_;
|
2013-01-27 07:08:19 +00:00
|
|
|
bool far_;
|
2013-01-27 07:18:50 +00:00
|
|
|
u32 iaddr_;
|
2013-01-26 16:42:34 +00:00
|
|
|
X64Reg xaddr_;
|
|
|
|
FixupBranch tooLow_, tooHigh_, skip_;
|
|
|
|
const u8 *safe_;
|
|
|
|
};
|
|
|
|
friend class JitSafeMem;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (Jit::*MIPSCompileFunc)(u32 opcode);
|
|
|
|
|
|
|
|
} // namespace MIPSComp
|
|
|
|
|