CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/MIPS/ARM/ArmJit.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "ppsspp_config.h"18#if PPSSPP_ARCH(ARM)1920#include "Common/Profiler/Profiler.h"2122#include "Common/Log.h"23#include "Common/Serialize/Serializer.h"24#include "Common/Serialize/SerializeFuncs.h"2526#include "Core/Reporting.h"27#include "Core/Config.h"28#include "Core/Core.h"29#include "Core/CoreTiming.h"30#include "Core/Debugger/Breakpoints.h"31#include "Core/Debugger/SymbolMap.h"32#include "Core/MemMap.h"3334#include "Core/MIPS/MIPS.h"35#include "Core/MIPS/MIPSAnalyst.h"36#include "Core/MIPS/MIPSCodeUtils.h"37#include "Core/MIPS/MIPSInt.h"38#include "Core/MIPS/MIPSTables.h"39#include "Core/HLE/ReplaceTables.h"40#include "Core/MIPS/ARM/ArmRegCache.h"41#include "Core/MIPS/ARM/ArmRegCacheFPU.h"4243#include "ArmRegCache.h"44#include "ArmJit.h"45#include "CPUDetect.h"4647#include "ext/disarm.h"4849using namespace ArmJitConstants;5051void DisassembleArm(const u8 *data, int size) {52char temp[256];53for (int i = 0; i < size; i += 4) {54const u32 *codePtr = (const u32 *)(data + i);55u32 inst = codePtr[0];56u32 next = (i < size - 4) ? codePtr[1] : 0;57// MAGIC SPECIAL CASE for MOVW/MOVT readability!58if ((inst & 0x0FF00000) == 0x03000000 && (next & 0x0FF00000) == 0x03400000) {59u32 low = ((inst & 0x000F0000) >> 4) | (inst & 0x0FFF);60u32 hi = ((next & 0x000F0000) >> 4) | (next & 0x0FFF);61int reg0 = (inst & 0x0000F000) >> 12;62int reg1 = (next & 0x0000F000) >> 12;63if (reg0 == reg1) {64snprintf(temp, sizeof(temp), "%08x MOV32 %s, %04x%04x", (u32)inst, ArmRegName(reg0), hi, low);65INFO_LOG(Log::JIT, "A: %s", temp);66i += 4;67continue;68}69}70ArmDis((u32)codePtr, inst, temp, sizeof(temp), true);71INFO_LOG(Log::JIT, "A: %s", temp);72}73}7475static u32 JitBreakpoint(uint32_t addr) {76// Should we skip this breakpoint?77if (CBreakPoints::CheckSkipFirst() == currentMIPS->pc || CBreakPoints::CheckSkipFirst() == addr)78return 0;7980BreakAction result = CBreakPoints::ExecBreakPoint(addr);81if ((result & BREAK_ACTION_PAUSE) == 0)82return 0;8384return 1;85}8687static u32 JitMemCheck(u32 pc) {88if (CBreakPoints::CheckSkipFirst() == currentMIPS->pc)89return 0;9091// Note: pc may be the delay slot.92const auto op = Memory::Read_Instruction(pc, true);93s32 offset = SignExtend16ToS32(op & 0xFFFF);94if (MIPSGetInfo(op) & IS_VFPU)95offset &= 0xFFFC;96u32 addr = currentMIPS->r[MIPS_GET_RS(op)] + offset;9798CBreakPoints::ExecOpMemCheck(addr, pc);99return coreState == CORE_RUNNING || coreState == CORE_NEXTFRAME ? 0 : 1;100}101102namespace MIPSComp103{104using namespace ArmGen;105using namespace ArmJitConstants;106107ArmJit::ArmJit(MIPSState *mipsState) : blocks(mipsState, this), gpr(mipsState, &js, &jo), fpr(mipsState, &js, &jo), mips_(mipsState) {108logBlocks = 0;109dontLogBlocks = 0;110blocks.Init();111gpr.SetEmitter(this);112fpr.SetEmitter(this);113AllocCodeSpace(1024 * 1024 * 16); // 32MB is the absolute max because that's what an ARM branch instruction can reach, backwards and forwards.114GenerateFixedCode();115116INFO_LOG(Log::JIT, "ARM JIT initialized: %lld MB of code space", (long long)(GetSpaceLeft() / (1024 * 1024)));117118js.startDefaultPrefix = mips_->HasDefaultPrefix();119120// The debugger sets this so that "go" on a breakpoint will actually... go.121// But if they reset, we can end up hitting it by mistake, since it's based on PC and ticks.122CBreakPoints::SetSkipFirst(0);123}124125ArmJit::~ArmJit() {126}127128void ArmJit::DoState(PointerWrap &p)129{130auto s = p.Section("Jit", 1, 2);131if (!s)132return;133134if (p.mode == PointerWrap::MODE_READ && !js.startDefaultPrefix) {135WARN_LOG(Log::CPU, "Jit: An uneaten prefix was previously detected. Jitting in unknown-prefix mode.");136}137Do(p, js.startDefaultPrefix);138if (s >= 2) {139Do(p, js.hasSetRounding);140if (p.mode == PointerWrap::MODE_READ) {141js.lastSetRounding = 0;142}143} else {144js.hasSetRounding = 1;145}146147// The debugger sets this so that "go" on a breakpoint will actually... go.148// But if they reset, we can end up hitting it by mistake, since it's based on PC and ticks.149CBreakPoints::SetSkipFirst(0);150}151152void ArmJit::UpdateFCR31() {153}154155void ArmJit::FlushAll()156{157gpr.FlushAll();158fpr.FlushAll();159FlushPrefixV();160}161162void ArmJit::FlushPrefixV() {163if (js.startDefaultPrefix && !js.blockWrotePrefixes && js.HasNoPrefix()) {164// They started default, we never modified in memory, and they're default now.165// No reason to modify memory. This is common at end of blocks. Just clear dirty.166js.prefixSFlag = (JitState::PrefixState)(js.prefixSFlag & ~JitState::PREFIX_DIRTY);167js.prefixTFlag = (JitState::PrefixState)(js.prefixTFlag & ~JitState::PREFIX_DIRTY);168js.prefixDFlag = (JitState::PrefixState)(js.prefixDFlag & ~JitState::PREFIX_DIRTY);169return;170}171172if ((js.prefixSFlag & JitState::PREFIX_DIRTY) != 0) {173gpr.SetRegImm(SCRATCHREG1, js.prefixS);174STR(SCRATCHREG1, CTXREG, offsetof(MIPSState, vfpuCtrl[VFPU_CTRL_SPREFIX]));175js.prefixSFlag = (JitState::PrefixState) (js.prefixSFlag & ~JitState::PREFIX_DIRTY);176}177178if ((js.prefixTFlag & JitState::PREFIX_DIRTY) != 0) {179gpr.SetRegImm(SCRATCHREG1, js.prefixT);180STR(SCRATCHREG1, CTXREG, offsetof(MIPSState, vfpuCtrl[VFPU_CTRL_TPREFIX]));181js.prefixTFlag = (JitState::PrefixState) (js.prefixTFlag & ~JitState::PREFIX_DIRTY);182}183184if ((js.prefixDFlag & JitState::PREFIX_DIRTY) != 0) {185gpr.SetRegImm(SCRATCHREG1, js.prefixD);186STR(SCRATCHREG1, CTXREG, offsetof(MIPSState, vfpuCtrl[VFPU_CTRL_DPREFIX]));187js.prefixDFlag = (JitState::PrefixState) (js.prefixDFlag & ~JitState::PREFIX_DIRTY);188}189190// If we got here, we must've written prefixes to memory in this block.191js.blockWrotePrefixes = true;192}193194void ArmJit::ClearCache()195{196blocks.Clear();197ClearCodeSpace(0);198GenerateFixedCode();199}200201void ArmJit::InvalidateCacheAt(u32 em_address, int length) {202if (blocks.RangeMayHaveEmuHacks(em_address, em_address + length)) {203blocks.InvalidateICache(em_address, length);204}205}206207void ArmJit::EatInstruction(MIPSOpcode op) {208MIPSInfo info = MIPSGetInfo(op);209if (info & DELAYSLOT) {210ERROR_LOG_REPORT_ONCE(ateDelaySlot, Log::JIT, "Ate a branch op.");211}212if (js.inDelaySlot) {213ERROR_LOG_REPORT_ONCE(ateInDelaySlot, Log::JIT, "Ate an instruction inside a delay slot.");214}215216CheckJitBreakpoint(GetCompilerPC() + 4, 0);217js.numInstructions++;218js.compilerPC += 4;219js.downcountAmount += MIPSGetInstructionCycleEstimate(op);220}221222void ArmJit::CompileDelaySlot(int flags) {223// Need to offset the downcount which was already incremented for the branch + delay slot.224CheckJitBreakpoint(GetCompilerPC() + 4, -2);225226// preserve flag around the delay slot! Maybe this is not always necessary on ARM where227// we can (mostly) control whether we set the flag or not. Of course, if someone puts an slt in to the228// delay slot, we're screwed.229if (flags & DELAYSLOT_SAFE)230MRS(R8); // Save flags register. R8 is preserved through function calls and is not allocated.231232js.inDelaySlot = true;233MIPSOpcode op = GetOffsetInstruction(1);234MIPSCompileOp(op, this);235js.inDelaySlot = false;236237if (flags & DELAYSLOT_FLUSH)238FlushAll();239if (flags & DELAYSLOT_SAFE)240_MSR(true, false, R8); // Restore flags register241}242243void ArmJit::Compile(u32 em_address) {244PROFILE_THIS_SCOPE("jitc");245246// INFO_LOG(Log::JIT, "Compiling at %08x", em_address);247248if (GetSpaceLeft() < 0x10000 || blocks.IsFull()) {249ClearCache();250}251252BeginWrite(JitBlockCache::MAX_BLOCK_INSTRUCTIONS * 16);253254int block_num = blocks.AllocateBlock(em_address);255JitBlock *b = blocks.GetBlock(block_num);256DoJit(em_address, b);257_assert_msg_(b->originalAddress == em_address, "original %08x != em_address %08x (block %d)", b->originalAddress, em_address, b->blockNum);258blocks.FinalizeBlock(block_num, jo.enableBlocklink);259260EndWrite();261262bool cleanSlate = false;263264if (js.hasSetRounding && !js.lastSetRounding) {265WARN_LOG(Log::JIT, "Detected rounding mode usage, rebuilding jit with checks");266// Won't loop, since hasSetRounding is only ever set to 1.267js.lastSetRounding = js.hasSetRounding;268cleanSlate = true;269}270271// Drat. The VFPU hit an uneaten prefix at the end of a block.272if (js.startDefaultPrefix && js.MayHavePrefix()) {273WARN_LOG_REPORT(Log::JIT, "An uneaten prefix at end of block: %08x", GetCompilerPC() - 4);274js.LogPrefix();275276// Let's try that one more time. We won't get back here because we toggled the value.277js.startDefaultPrefix = false;278cleanSlate = true;279}280281if (cleanSlate) {282// Our assumptions are all wrong so it's clean-slate time.283ClearCache();284Compile(em_address);285}286}287288void ArmJit::RunLoopUntil(u64 globalticks) {289PROFILE_THIS_SCOPE("jit");290((void (*)())enterDispatcher)();291}292293u32 ArmJit::GetCompilerPC() {294return js.compilerPC;295}296297MIPSOpcode ArmJit::GetOffsetInstruction(int offset) {298return Memory::Read_Instruction(GetCompilerPC() + 4 * offset);299}300301const u8 *ArmJit::DoJit(u32 em_address, JitBlock *b)302{303js.cancel = false;304js.blockStart = em_address;305js.compilerPC = em_address;306js.lastContinuedPC = 0;307js.initialBlockSize = 0;308js.nextExit = 0;309js.downcountAmount = 0;310js.curBlock = b;311js.compiling = true;312js.inDelaySlot = false;313js.blockWrotePrefixes = false;314js.PrefixStart();315316// We add a downcount flag check before the block, used when entering from a linked block.317// The last block decremented downcounter, and the flag should still be available.318// Got three variants here of where we position the code, needs detailed benchmarking.319320FixupBranch bail;321if (jo.useBackJump) {322// Moves the MOVI2R and B *before* checkedEntry, and just branch backwards there.323// Speedup seems to be zero unfortunately but I guess it may vary from device to device.324// Not intrusive so keeping it around here to experiment with, may help on ARMv6 due to325// large/slow construction of 32-bit immediates?326JumpTarget backJump = GetCodePtr();327gpr.SetRegImm(R0, js.blockStart);328B((const void *)outerLoopPCInR0);329b->checkedEntry = GetCodePtr();330SetCC(CC_LT);331B(backJump);332SetCC(CC_AL);333} else if (jo.useForwardJump) {334b->checkedEntry = GetCodePtr();335SetCC(CC_LT);336bail = B();337SetCC(CC_AL);338} else {339b->checkedEntry = GetCodePtr();340SetCC(CC_LT);341gpr.SetRegImm(R0, js.blockStart);342B((const void *)outerLoopPCInR0);343SetCC(CC_AL);344}345346b->normalEntry = GetCodePtr();347// TODO: this needs work348MIPSAnalyst::AnalysisResults analysis; // = MIPSAnalyst::Analyze(em_address);349350gpr.Start(analysis);351fpr.Start(analysis);352353js.numInstructions = 0;354while (js.compiling)355{356gpr.SetCompilerPC(GetCompilerPC()); // Let it know for log messages357// Jit breakpoints are quite fast, so let's do them in release too.358CheckJitBreakpoint(GetCompilerPC(), 0);359360MIPSOpcode inst = Memory::Read_Opcode_JIT(GetCompilerPC());361//MIPSInfo info = MIPSGetInfo(inst);362//if (info & IS_VFPU) {363// logBlocks = 1;364//}365366js.downcountAmount += MIPSGetInstructionCycleEstimate(inst);367368MIPSCompileOp(inst, this);369370js.compilerPC += 4;371js.numInstructions++;372373if (jo.Disabled(JitDisable::REGALLOC_GPR)) {374gpr.FlushAll();375}376if (jo.Disabled(JitDisable::REGALLOC_FPR)) {377fpr.FlushAll();378FlushPrefixV();379}380381// Safety check, in case we get a bunch of really large jit ops without a lot of branching.382if (GetSpaceLeft() < 0x800 || js.numInstructions >= JitBlockCache::MAX_BLOCK_INSTRUCTIONS)383{384FlushAll();385WriteExit(GetCompilerPC(), js.nextExit++);386js.compiling = false;387}388}389390if (jo.useForwardJump) {391SetJumpTarget(bail);392gpr.SetRegImm(R0, js.blockStart);393B((const void *)outerLoopPCInR0);394}395396FlushLitPool();397398char temp[256];399if (logBlocks > 0 && dontLogBlocks == 0) {400INFO_LOG(Log::JIT, "=============== mips ===============");401for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) {402MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp, sizeof(temp), true);403INFO_LOG(Log::JIT, "M: %08x %s", cpc, temp);404}405}406407b->codeSize = GetCodePtr() - b->normalEntry;408409if (logBlocks > 0 && dontLogBlocks == 0) {410INFO_LOG(Log::JIT, "=============== ARM ===============");411DisassembleArm(b->normalEntry, GetCodePtr() - b->normalEntry);412}413if (logBlocks > 0)414logBlocks--;415if (dontLogBlocks > 0)416dontLogBlocks--;417418// Don't forget to zap the newly written instructions in the instruction cache!419FlushIcache();420421if (js.lastContinuedPC == 0)422b->originalSize = js.numInstructions;423else424{425// We continued at least once. Add the last proxy and set the originalSize correctly.426blocks.ProxyBlock(js.blockStart, js.lastContinuedPC, (GetCompilerPC() - js.lastContinuedPC) / sizeof(u32), GetCodePtr());427b->originalSize = js.initialBlockSize;428}429return b->normalEntry;430}431432void ArmJit::AddContinuedBlock(u32 dest)433{434// The first block is the root block. When we continue, we create proxy blocks after that.435if (js.lastContinuedPC == 0)436js.initialBlockSize = js.numInstructions;437else438blocks.ProxyBlock(js.blockStart, js.lastContinuedPC, (GetCompilerPC() - js.lastContinuedPC) / sizeof(u32), GetCodePtr());439js.lastContinuedPC = dest;440}441442bool ArmJit::DescribeCodePtr(const u8 *ptr, std::string &name)443{444// TODO: Not used by anything yet (except the modified VerySleepy on Windows)445return false;446}447448void ArmJit::Comp_RunBlock(MIPSOpcode op)449{450// This shouldn't be necessary, the dispatcher should catch us before we get here.451ERROR_LOG(Log::JIT, "Comp_RunBlock should never be reached!");452}453454void ArmJit::LinkBlock(u8 *exitPoint, const u8 *checkedEntry) {455if (PlatformIsWXExclusive()) {456ProtectMemoryPages(exitPoint, 32, MEM_PROT_READ | MEM_PROT_WRITE);457}458459ARMXEmitter emit(exitPoint);460u32 op = *((const u32 *)emit.GetCodePointer());461bool prelinked = (op & 0xFF000000) == 0xEA000000;462// Jump directly to the block, yay.463emit.B(checkedEntry);464465if (!prelinked) {466do {467op = *((const u32 *)emit.GetCodePointer());468// Overwrite whatever is here with a breakpoint.469emit.BKPT(1);470// Stop after overwriting the next unconditional branch or BKPT.471// It can be a BKPT if we unlinked, and are now linking a different one.472} while ((op & 0xFF000000) != 0xEA000000 && (op & 0xFFF000F0) != 0xE1200070);473}474emit.FlushIcache();475if (PlatformIsWXExclusive()) {476ProtectMemoryPages(exitPoint, 32, MEM_PROT_READ | MEM_PROT_EXEC);477}478}479480void ArmJit::UnlinkBlock(u8 *checkedEntry, u32 originalAddress) {481if (PlatformIsWXExclusive()) {482ProtectMemoryPages(checkedEntry, 16, MEM_PROT_READ | MEM_PROT_WRITE);483}484// Send anyone who tries to run this block back to the dispatcher.485// Not entirely ideal, but .. pretty good.486// I hope there's enough space...487// checkedEntry is the only "linked" entrance so it's enough to overwrite that.488ARMXEmitter emit(checkedEntry);489emit.MOVI2R(R0, originalAddress);490emit.STR(R0, CTXREG, offsetof(MIPSState, pc));491emit.B(MIPSComp::jit->GetDispatcher());492emit.FlushIcache();493if (PlatformIsWXExclusive()) {494ProtectMemoryPages(checkedEntry, 16, MEM_PROT_READ | MEM_PROT_EXEC);495}496}497498bool ArmJit::ReplaceJalTo(u32 dest) {499#if PPSSPP_ARCH(ARM)500const ReplacementTableEntry *entry = nullptr;501u32 funcSize = 0;502if (!CanReplaceJalTo(dest, &entry, &funcSize)) {503return false;504}505506// Warning - this might be bad if the code at the destination changes...507if (entry->flags & REPFLAG_ALLOWINLINE) {508// Jackpot! Just do it, no flushing. The code will be entirely inlined.509510// First, compile the delay slot. It's unconditional so no issues.511CompileDelaySlot(DELAYSLOT_NICE);512// Technically, we should write the unused return address to RA, but meh.513MIPSReplaceFunc repl = entry->jitReplaceFunc;514int cycles = (this->*repl)();515js.downcountAmount += cycles;516} else {517gpr.SetImm(MIPS_REG_RA, GetCompilerPC() + 8);518CompileDelaySlot(DELAYSLOT_NICE);519FlushAll();520SaveDowncount();521RestoreRoundingMode();522523if (BLInRange((const void *)(entry->replaceFunc))) {524BL((const void *)(entry->replaceFunc));525} else {526MOVI2R(R0, (uintptr_t)entry->replaceFunc);527BL(R0);528}529ApplyRoundingMode();530RestoreDowncount();531532WriteDownCountR(R0);533}534535js.compilerPC += 4;536// No writing exits, keep going!537538if (CBreakPoints::HasMemChecks()) {539// We could modify coreState, so we need to write PC and check.540// Otherwise, PC may end up on the jal. We add 4 to skip the delay slot.541FlushAll();542WriteExit(GetCompilerPC() + 4, js.nextExit++);543js.compiling = false;544}545546// Add a trigger so that if the inlined code changes, we invalidate this block.547blocks.ProxyBlock(js.blockStart, dest, funcSize / sizeof(u32), GetCodePtr());548#endif549return true;550}551552void ArmJit::Comp_ReplacementFunc(MIPSOpcode op)553{554// We get here if we execute the first instruction of a replaced function. This means555// that we do need to return to RA.556557// Inlined function calls (caught in jal) are handled differently.558559int index = op.encoding & MIPS_EMUHACK_VALUE_MASK;560561const ReplacementTableEntry *entry = GetReplacementFunc(index);562if (!entry) {563ERROR_LOG_REPORT_ONCE(replFunc, Log::HLE, "Invalid replacement op %08x at %08x", op.encoding, js.compilerPC);564return;565}566567u32 funcSize = g_symbolMap->GetFunctionSize(GetCompilerPC());568bool disabled = (entry->flags & REPFLAG_DISABLED) != 0;569if (!disabled && funcSize != SymbolMap::INVALID_ADDRESS && funcSize > sizeof(u32)) {570// We don't need to disable hooks, the code will still run.571if ((entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) == 0) {572// Any breakpoint at the func entry was already tripped, so we can still run the replacement.573// That's a common case - just to see how often the replacement hits.574disabled = CBreakPoints::RangeContainsBreakPoint(GetCompilerPC() + sizeof(u32), funcSize - sizeof(u32));575}576}577578if (disabled) {579MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this);580} else if (entry->jitReplaceFunc) {581MIPSReplaceFunc repl = entry->jitReplaceFunc;582int cycles = (this->*repl)();583584if (entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) {585// Compile the original instruction at this address. We ignore cycles for hooks.586MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this);587} else {588FlushAll();589// Flushed, so R1 is safe.590LDR(R1, CTXREG, MIPS_REG_RA * 4);591js.downcountAmount += cycles;592WriteExitDestInR(R1);593js.compiling = false;594}595} else if (entry->replaceFunc) {596FlushAll();597SaveDowncount();598RestoreRoundingMode();599gpr.SetRegImm(SCRATCHREG1, GetCompilerPC());600MovToPC(SCRATCHREG1);601602// Standard function call, nothing fancy.603// The function returns the number of cycles it took in EAX.604if (BLInRange((const void *)(entry->replaceFunc))) {605BL((const void *)(entry->replaceFunc));606} else {607MOVI2R(R0, (uintptr_t)entry->replaceFunc);608BL(R0);609}610611if (entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) {612// Compile the original instruction at this address. We ignore cycles for hooks.613ApplyRoundingMode();614RestoreDowncount();615MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this);616} else {617ApplyRoundingMode();618RestoreDowncount();619620CMPI2R(R0, 0, SCRATCHREG2);621FixupBranch positive = B_CC(CC_GE);622623RSB(R0, R0, Operand2(0));624MovFromPC(R1);625FixupBranch done = B();626627SetJumpTarget(positive);628LDR(R1, CTXREG, MIPS_REG_RA * 4);629630SetJumpTarget(done);631WriteDownCountR(R0);632WriteExitDestInR(R1);633js.compiling = false;634}635} else {636ERROR_LOG(Log::HLE, "Replacement function %s has neither jit nor regular impl", entry->name);637}638}639640void ArmJit::Comp_Generic(MIPSOpcode op)641{642FlushAll();643MIPSInterpretFunc func = MIPSGetInterpretFunc(op);644if (func)645{646SaveDowncount();647// TODO: Perhaps keep the rounding mode for interp?648RestoreRoundingMode();649gpr.SetRegImm(SCRATCHREG1, GetCompilerPC());650MovToPC(SCRATCHREG1);651gpr.SetRegImm(R0, op.encoding);652QuickCallFunction(R1, (void *)func);653ApplyRoundingMode();654RestoreDowncount();655}656657const MIPSInfo info = MIPSGetInfo(op);658if ((info & IS_VFPU) != 0 && (info & VFPU_NO_PREFIX) == 0)659{660// If it does eat them, it'll happen in MIPSCompileOp().661if ((info & OUT_EAT_PREFIX) == 0)662js.PrefixUnknown();663664// Even if DISABLE'd, we want to set this flag so we overwrite.665if ((info & OUT_VFPU_PREFIX) != 0)666js.blockWrotePrefixes = true;667}668}669670void ArmJit::MovFromPC(ARMReg r) {671LDR(r, CTXREG, offsetof(MIPSState, pc));672}673674void ArmJit::MovToPC(ARMReg r) {675STR(r, CTXREG, offsetof(MIPSState, pc));676}677678void ArmJit::SaveDowncount() {679if (jo.downcountInRegister)680STR(DOWNCOUNTREG, CTXREG, offsetof(MIPSState, downcount));681}682683void ArmJit::RestoreDowncount() {684if (jo.downcountInRegister)685LDR(DOWNCOUNTREG, CTXREG, offsetof(MIPSState, downcount));686}687688void ArmJit::WriteDownCount(int offset) {689if (jo.downcountInRegister) {690int theDowncount = js.downcountAmount + offset;691Operand2 op2;692if (TryMakeOperand2(theDowncount, op2)) {693SUBS(DOWNCOUNTREG, DOWNCOUNTREG, op2);694} else {695// Should be fine to use R2 here, flushed the regcache anyway.696// If js.downcountAmount can be expressed as an Imm8, we don't need this anyway.697gpr.SetRegImm(R2, theDowncount);698SUBS(DOWNCOUNTREG, DOWNCOUNTREG, R2);699}700} else {701int theDowncount = js.downcountAmount + offset;702LDR(SCRATCHREG2, CTXREG, offsetof(MIPSState, downcount));703Operand2 op2;704if (TryMakeOperand2(theDowncount, op2)) {705SUBS(SCRATCHREG2, SCRATCHREG2, op2);706} else {707// Should be fine to use R2 here, flushed the regcache anyway.708// If js.downcountAmount can be expressed as an Imm8, we don't need this anyway.709gpr.SetRegImm(R2, theDowncount);710SUBS(SCRATCHREG2, SCRATCHREG2, R2);711}712STR(SCRATCHREG2, CTXREG, offsetof(MIPSState, downcount));713}714}715716// Abuses R2717void ArmJit::WriteDownCountR(ARMReg reg) {718if (jo.downcountInRegister) {719SUBS(DOWNCOUNTREG, DOWNCOUNTREG, reg);720} else {721LDR(R2, CTXREG, offsetof(MIPSState, downcount));722SUBS(R2, R2, reg);723STR(R2, CTXREG, offsetof(MIPSState, downcount));724}725}726727// Destroys SCRATCHREG2. Does not destroy SCRATCHREG1.728void ArmJit::RestoreRoundingMode(bool force) {729// If the game has never set an interesting rounding mode, we can safely skip this.730if (force || js.hasSetRounding) {731QuickCallFunction(R1, restoreRoundingMode);732}733}734735// Does not destroy R0 (SCRATCHREG1). Destroys R14 (SCRATCHREG2).736void ArmJit::ApplyRoundingMode(bool force) {737// If the game has never set an interesting rounding mode, we can safely skip this.738if (force || js.hasSetRounding) {739QuickCallFunction(R1, applyRoundingMode);740}741}742743// Does (must!) not destroy R0 (SCRATCHREG1). Destroys R14 (SCRATCHREG2).744void ArmJit::UpdateRoundingMode(u32 fcr31) {745// We must set js.hasSetRounding at compile time, or this block will use the wrong rounding mode.746// The fcr31 parameter is -1 when not known at compile time, so we just assume it was changed.747if (fcr31 & 0x01000003) {748js.hasSetRounding = true;749}750}751752// IDEA - could have a WriteDualExit that takes two destinations and two condition flags,753// and just have conditional that set PC "twice". This only works when we fall back to dispatcher754// though, as we need to have the SUBS flag set in the end. So with block linking in the mix,755// I don't think this gives us that much benefit.756void ArmJit::WriteExit(u32 destination, int exit_num)757{758// NOTE: Can't blindly check for bad destination addresses here, sometimes exits with bad destinations are written intentionally (like breaks).759_assert_msg_(exit_num < MAX_JIT_BLOCK_EXITS, "Expected a valid exit_num. dest=%08x", destination);760761WriteDownCount();762//If nobody has taken care of this yet (this can be removed when all branches are done)763JitBlock *b = js.curBlock;764b->exitAddress[exit_num] = destination;765b->exitPtrs[exit_num] = GetWritableCodePtr();766767// Link opportunity!768int block = blocks.GetBlockNumberFromStartAddress(destination);769if (block >= 0 && jo.enableBlocklink) {770// It exists! Joy of joy!771B(blocks.GetBlock(block)->checkedEntry);772b->linkStatus[exit_num] = true;773} else {774gpr.SetRegImm(R0, destination);775B((const void *)dispatcherPCInR0);776}777}778779void ArmJit::WriteExitDestInR(ARMReg Reg)780{781// TODO: If not fast memory, check for invalid address in reg and trigger exception.782MovToPC(Reg);783WriteDownCount();784// TODO: shouldn't need an indirect branch here...785B((const void *)dispatcher);786}787788void ArmJit::WriteSyscallExit()789{790WriteDownCount();791B((const void *)dispatcherCheckCoreState);792}793794bool ArmJit::CheckJitBreakpoint(u32 addr, int downcountOffset) {795if (CBreakPoints::IsAddressBreakPoint(addr)) {796MRS(R8);797FlushAll();798MOVI2R(SCRATCHREG1, GetCompilerPC());799MovToPC(SCRATCHREG1);800SaveDowncount();801RestoreRoundingMode();802MOVI2R(R0, addr);803QuickCallFunction(SCRATCHREG2, &JitBreakpoint);804805// If 0, the conditional breakpoint wasn't taken.806CMPI2R(R0, 0, SCRATCHREG2);807FixupBranch skip = B_CC(CC_EQ);808WriteDownCount(downcountOffset);809ApplyRoundingMode();810RestoreDowncount();811B((const void *)dispatcherCheckCoreState);812SetJumpTarget(skip);813814ApplyRoundingMode();815_MSR(true, false, R8);816return true;817}818819return false;820}821822bool ArmJit::CheckMemoryBreakpoint(int instructionOffset) {823if (CBreakPoints::HasMemChecks()) {824int off = instructionOffset + (js.inDelaySlot ? 1 : 0);825826MRS(R8);827FlushAll();828SaveDowncount();829RestoreRoundingMode();830MOVI2R(R0, GetCompilerPC());831MovToPC(R0);832if (off != 0)833ADDI2R(R0, R0, off * 4, SCRATCHREG2);834QuickCallFunction(SCRATCHREG2, &JitMemCheck);835836// If 0, the breakpoint wasn't tripped.837CMPI2R(R0, 0, SCRATCHREG2);838FixupBranch skip = B_CC(CC_EQ);839WriteDownCount(-1 - off);840ApplyRoundingMode();841RestoreDowncount();842B((const void *)dispatcherCheckCoreState);843SetJumpTarget(skip);844845ApplyRoundingMode();846_MSR(true, false, R8);847return true;848}849850return false;851}852853void ArmJit::Comp_DoNothing(MIPSOpcode op) { }854855MIPSOpcode ArmJit::GetOriginalOp(MIPSOpcode op) {856JitBlockCache *bc = GetBlockCache();857int block_num = bc->GetBlockNumberFromEmuHackOp(op, true);858if (block_num >= 0) {859return bc->GetOriginalFirstOp(block_num);860} else {861return op;862}863}864865} // namespace866867#endif // PPSSPP_ARCH(ARM)868869870