2012-11-01 16:19:01 +01: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 23:01:49 +01:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 16:19:01 +01: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/.
|
|
|
|
|
|
|
|
#include "../../Core.h"
|
|
|
|
#include "../../CoreTiming.h"
|
|
|
|
#include "../MIPS.h"
|
|
|
|
#include "../MIPSCodeUtils.h"
|
|
|
|
#include "../MIPSInt.h"
|
|
|
|
#include "../MIPSTables.h"
|
|
|
|
|
2013-01-07 22:33:09 +01:00
|
|
|
#include "ArmRegCache.h"
|
|
|
|
#include "ArmJit.h"
|
2012-11-01 16:19:01 +01:00
|
|
|
|
2013-01-08 13:49:52 +01:00
|
|
|
#include "../../ext/disarm.h"
|
2012-11-01 16:19:01 +01:00
|
|
|
|
2013-01-08 13:49:52 +01:00
|
|
|
namespace MIPSComp
|
2012-11-01 16:19:01 +01:00
|
|
|
{
|
|
|
|
|
2012-11-23 19:41:35 +01:00
|
|
|
Jit::Jit(MIPSState *mips) : blocks(mips), gpr(mips), mips_(mips)
|
2012-11-01 16:19:01 +01:00
|
|
|
{
|
|
|
|
blocks.Init();
|
|
|
|
asm_.Init(mips, this);
|
|
|
|
gpr.SetEmitter(this);
|
2012-11-23 19:41:35 +01:00
|
|
|
//fpr.SetEmitter(this);
|
2012-11-01 16:19:01 +01:00
|
|
|
AllocCodeSpace(1024 * 1024 * 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Jit::FlushAll()
|
|
|
|
{
|
2013-01-07 22:33:09 +01:00
|
|
|
gpr.FlushAll();
|
2012-11-23 19:41:35 +01:00
|
|
|
//fpr.Flush(FLUSH_ALL);
|
2012-11-01 16:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::ClearCache()
|
|
|
|
{
|
|
|
|
blocks.Clear();
|
|
|
|
ClearCodeSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *codeCache;
|
|
|
|
#define CACHESIZE 16384*1024
|
|
|
|
void Jit::CompileAt(u32 addr)
|
|
|
|
{
|
|
|
|
u32 op = Memory::Read_Instruction(addr);
|
|
|
|
MIPSCompileOp(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::Compile(u32 em_address)
|
|
|
|
{
|
|
|
|
if (GetSpaceLeft() < 0x10000 || blocks.IsFull())
|
|
|
|
{
|
|
|
|
ClearCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
int block_num = blocks.AllocateBlock(em_address);
|
2013-01-07 22:33:09 +01:00
|
|
|
ArmJitBlock *b = blocks.GetBlock(block_num);
|
2012-11-01 16:19:01 +01:00
|
|
|
blocks.FinalizeBlock(block_num, jo.enableBlocklink, DoJit(em_address, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::RunLoopUntil(u64 globalticks)
|
|
|
|
{
|
|
|
|
// TODO: copy globalticks somewhere
|
|
|
|
((void (*)())asm_.enterCode)();
|
2013-01-08 00:26:42 +01:00
|
|
|
INFO_LOG(DYNA_REC, "Left asm code");
|
2012-11-01 16:19:01 +01:00
|
|
|
}
|
|
|
|
|
2013-01-08 13:49:52 +01:00
|
|
|
void Hullo(int a, int b, int c, int d) {
|
|
|
|
INFO_LOG(DYNA_REC, "Hullo %08x %08x %08x %08x", a, b, c, d);
|
|
|
|
}
|
|
|
|
|
2013-01-08 17:03:17 +01:00
|
|
|
static void DisassembleArm(const u8 *data, int size) {
|
|
|
|
char temp[256];
|
|
|
|
for (int i = 0; i < size; i += 4) {
|
|
|
|
const u32 *codePtr = (const u32 *)(data + i);
|
|
|
|
u32 inst = codePtr[0];
|
|
|
|
u32 next = (i < size - 4) ? codePtr[1] : 0;
|
|
|
|
// MAGIC SPECIAL CASE for MOVW/MOVT readability!
|
|
|
|
if ((inst & 0x0FF00000) == 0x03000000 && (next & 0x0FF00000) == 0x03400000) {
|
|
|
|
u32 low = ((inst & 0x000F0000) >> 4) | (inst & 0x0FFF);
|
|
|
|
u32 hi = ((next & 0x000F0000) >> 4) | (next & 0x0FFF);
|
|
|
|
int reg0 = (inst & 0x0000F000) >> 12;
|
|
|
|
int reg1 = (next & 0x0000F000) >> 12;
|
|
|
|
if (reg0 == reg1) {
|
|
|
|
sprintf(temp, "%08x MOV32? %s, %04x%04x", (u32)inst, ArmRegName(reg0), hi, low);
|
|
|
|
INFO_LOG(DYNA_REC, "A: %s", temp);
|
|
|
|
i += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArmDis((u32)codePtr, inst, temp);
|
|
|
|
INFO_LOG(DYNA_REC, "A: %s", temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:33:09 +01:00
|
|
|
const u8 *Jit::DoJit(u32 em_address, ArmJitBlock *b)
|
2012-11-01 16:19:01 +01:00
|
|
|
{
|
|
|
|
js.cancel = false;
|
|
|
|
js.blockStart = js.compilerPC = mips_->pc;
|
|
|
|
js.downcountAmount = 0;
|
|
|
|
js.curBlock = b;
|
|
|
|
js.compiling = true;
|
|
|
|
js.inDelaySlot = false;
|
|
|
|
|
2013-01-08 13:49:52 +01:00
|
|
|
// We add a check before the block, used when entering from a linked block.
|
|
|
|
b->checkedEntry = GetCodePtr();
|
|
|
|
// Downcount flag check. The last block decremented downcounter, and the flag should still be available.
|
|
|
|
SetCC(CC_LT);
|
|
|
|
ARMABI_MOVI2R(R0, js.blockStart);
|
|
|
|
MovToPC(R0);
|
|
|
|
ARMABI_MOVI2R(R0, (u32)asm_.outerLoop);
|
|
|
|
B(R0);
|
|
|
|
SetCC(CC_AL);
|
|
|
|
|
|
|
|
b->normalEntry = GetCodePtr();
|
2012-11-01 16:19:01 +01:00
|
|
|
// TODO: this needs work
|
|
|
|
MIPSAnalyst::AnalysisResults analysis; // = MIPSAnalyst::Analyze(em_address);
|
|
|
|
|
2012-11-23 19:41:35 +01:00
|
|
|
gpr.Start(analysis);
|
2012-11-01 16:19:01 +01:00
|
|
|
|
|
|
|
int numInstructions = 0;
|
|
|
|
int cycles = 0;
|
2013-01-08 13:49:52 +01:00
|
|
|
#define LOGASM
|
|
|
|
#ifdef LOGASM
|
|
|
|
char temp[256];
|
|
|
|
#endif
|
2012-11-01 16:19:01 +01:00
|
|
|
while (js.compiling)
|
|
|
|
{
|
|
|
|
u32 inst = Memory::Read_Instruction(js.compilerPC);
|
2013-01-08 13:49:52 +01:00
|
|
|
#ifdef LOGASM
|
|
|
|
MIPSDisAsm(inst, js.compilerPC, temp, true);
|
|
|
|
INFO_LOG(DYNA_REC, "M: %08x %s", js.compilerPC, temp);
|
|
|
|
#endif
|
2012-11-01 16:19:01 +01:00
|
|
|
js.downcountAmount += MIPSGetInstructionCycleEstimate(inst);
|
|
|
|
|
|
|
|
MIPSCompileOp(inst);
|
|
|
|
|
|
|
|
js.compilerPC += 4;
|
|
|
|
numInstructions++;
|
|
|
|
}
|
2013-01-08 13:49:52 +01:00
|
|
|
#ifdef LOGASM
|
|
|
|
MIPSDisAsm(Memory::Read_Instruction(js.compilerPC), js.compilerPC, temp, true);
|
2013-01-08 17:03:17 +01:00
|
|
|
INFO_LOG(DYNA_REC, "M: %08x %s", js.compilerPC, temp);
|
2013-01-08 13:49:52 +01:00
|
|
|
#endif
|
|
|
|
|
2012-11-01 16:19:01 +01:00
|
|
|
b->codeSize = GetCodePtr() - b->normalEntry;
|
2013-01-08 17:03:17 +01:00
|
|
|
|
2013-01-08 13:49:52 +01:00
|
|
|
#ifdef LOGASM
|
2013-01-08 17:03:17 +01:00
|
|
|
DisassembleArm(b->checkedEntry, GetCodePtr() - b->checkedEntry);
|
2013-01-08 13:49:52 +01:00
|
|
|
#endif
|
|
|
|
|
2012-11-01 16:19:01 +01:00
|
|
|
AlignCode16();
|
|
|
|
b->originalSize = numInstructions;
|
|
|
|
return b->normalEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::Comp_RunBlock(u32 op)
|
|
|
|
{
|
|
|
|
// This shouldn't be necessary, the dispatcher should catch us before we get here.
|
|
|
|
ERROR_LOG(DYNA_REC, "Comp_RunBlock");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::Comp_Generic(u32 op)
|
|
|
|
{
|
|
|
|
FlushAll();
|
|
|
|
MIPSInterpretFunc func = MIPSGetInterpretFunc(op);
|
|
|
|
if (func)
|
|
|
|
{
|
2013-01-08 13:49:52 +01:00
|
|
|
ARMABI_MOVI2R(R0, js.compilerPC);
|
|
|
|
MovToPC(R0);
|
|
|
|
ARMABI_CallFunctionC((void *)func, op);
|
2012-11-01 16:19:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 13:49:52 +01:00
|
|
|
void Jit::MovFromPC(ARMReg r) {
|
2013-01-08 14:29:03 +01:00
|
|
|
LDR(r, R10, offsetof(MIPSState, pc));
|
2013-01-08 13:49:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::MovToPC(ARMReg r) {
|
2013-01-08 14:29:03 +01:00
|
|
|
STR(R10, r, offsetof(MIPSState, pc));
|
2013-01-08 13:49:52 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 19:41:35 +01:00
|
|
|
void Jit::DoDownCount()
|
|
|
|
{
|
2013-01-07 22:33:09 +01:00
|
|
|
ARMABI_MOVI2R(R0, Mem(&CoreTiming::downcount));
|
|
|
|
LDR(R1, R0);
|
2012-11-23 19:41:35 +01:00
|
|
|
if(js.downcountAmount < 255) // We can enlarge this if we used rotations
|
|
|
|
{
|
2013-01-07 22:33:09 +01:00
|
|
|
SUBS(R1, R1, js.downcountAmount);
|
|
|
|
STR(R0, R1);
|
|
|
|
} else {
|
|
|
|
// Should be fine to use R2 here, flushed the regcache anyway.
|
|
|
|
// If js.downcountAmount can be expressed as an Imm8, we don't need this anyway.
|
|
|
|
ARMABI_MOVI2R(R2, js.downcountAmount);
|
|
|
|
SUBS(R1, R1, R2);
|
|
|
|
STR(R0, R1);
|
2012-11-23 19:41:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::WriteExitDestInR(ARMReg Reg)
|
2012-11-01 16:19:01 +01:00
|
|
|
{
|
2013-01-08 13:49:52 +01:00
|
|
|
MovToPC(Reg);
|
2012-11-23 19:41:35 +01:00
|
|
|
DoDownCount();
|
2013-01-08 13:49:52 +01:00
|
|
|
// TODO: shouldn't need an indirect branch here...
|
2013-01-07 22:33:09 +01:00
|
|
|
ARMABI_MOVI2R(R0, (u32)asm_.dispatcher);
|
|
|
|
B(R0);
|
2012-11-23 19:41:35 +01:00
|
|
|
}
|
2012-11-01 16:19:01 +01:00
|
|
|
|
2012-11-23 19:41:35 +01:00
|
|
|
void Jit::WriteExit(u32 destination, int exit_num)
|
|
|
|
{
|
|
|
|
DoDownCount();
|
2012-11-01 16:19:01 +01:00
|
|
|
//If nobody has taken care of this yet (this can be removed when all branches are done)
|
2013-01-07 22:33:09 +01:00
|
|
|
ArmJitBlock *b = js.curBlock;
|
2012-11-01 16:19:01 +01:00
|
|
|
b->exitAddress[exit_num] = destination;
|
|
|
|
b->exitPtrs[exit_num] = GetWritableCodePtr();
|
|
|
|
|
|
|
|
// Link opportunity!
|
|
|
|
int block = blocks.GetBlockNumberFromStartAddress(destination);
|
|
|
|
if (block >= 0 && jo.enableBlocklink)
|
|
|
|
{
|
|
|
|
// It exists! Joy of joy!
|
2012-11-23 19:41:35 +01:00
|
|
|
B(blocks.GetBlock(block)->checkedEntry);
|
2012-11-01 16:19:01 +01:00
|
|
|
b->linkStatus[exit_num] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-08 13:49:52 +01:00
|
|
|
ARMABI_MOVI2R(R0, destination);
|
|
|
|
MovToPC(R0);
|
2013-01-07 22:33:09 +01:00
|
|
|
ARMABI_MOVI2R(R0, (u32)asm_.dispatcher);
|
|
|
|
B(R0);
|
2012-11-01 16:19:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Jit::WriteSyscallExit()
|
|
|
|
{
|
2012-11-23 19:41:35 +01:00
|
|
|
DoDownCount();
|
2013-01-08 17:03:17 +01:00
|
|
|
ARMABI_MOVI2R(R0, (u32)asm_.dispatcherCheckCoreState);
|
|
|
|
B(R0);
|
2012-11-01 16:19:01 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 19:41:35 +01:00
|
|
|
|
2012-11-01 16:19:01 +01:00
|
|
|
#define _RS ((op>>21) & 0x1F)
|
|
|
|
#define _RT ((op>>16) & 0x1F)
|
|
|
|
#define _RD ((op>>11) & 0x1F)
|
|
|
|
#define _FS ((op>>11) & 0x1F)
|
|
|
|
#define _FT ((op>>16) & 0x1F)
|
|
|
|
#define _FD ((op>>6) & 0x1F)
|
|
|
|
#define _POS ((op>>6) & 0x1F)
|
|
|
|
#define _SIZE ((op>>11) & 0x1F)
|
|
|
|
|
|
|
|
//memory regions:
|
|
|
|
//
|
|
|
|
// 08-0A
|
|
|
|
// 48-4A
|
|
|
|
// 04-05
|
|
|
|
// 44-45
|
|
|
|
// mov eax, addrreg
|
|
|
|
// shr eax, 28
|
|
|
|
// mov eax, [table+eax]
|
|
|
|
// mov dreg, [eax+offreg]
|
|
|
|
|
|
|
|
}
|