Path: blob/master/Core/MIPS/LoongArch64/LoongArch64Asm.cpp
3188 views
// Copyright (c) 2023- 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 "Common/Log.h"18#include "Core/CoreTiming.h"19#include "Core/MemMap.h"20#include "Core/MIPS/LoongArch64/LoongArch64Jit.h"21#include "Core/MIPS/LoongArch64/LoongArch64RegCache.h"22#include "Core/MIPS/JitCommon/JitCommon.h"23#include "Core/MIPS/JitCommon/JitState.h"24#include "Core/Core.h"2526namespace MIPSComp {2728using namespace LoongArch64Gen;29using namespace LoongArch64JitConstants;3031static const bool enableDebug = false;32static const bool enableDisasm = false;3334static void ShowPC(u32 downcount, void *membase, void *jitbase) {35static int count = 0;36if (currentMIPS) {37ERROR_LOG(Log::JIT, "[%08x] ShowPC Downcount : %08x %d %p %p", currentMIPS->pc, downcount, count, membase, jitbase);38} else {39ERROR_LOG(Log::JIT, "Universe corrupt?");40}41count++;42}4344void LoongArch64JitBackend::GenerateFixedCode(MIPSState *mipsState) {45// This will be used as a writable scratch area, always 32-bit accessible.46const u8 *start = AlignCodePage();47if (DebugProfilerEnabled()) {48ProtectMemoryPages(start, GetMemoryProtectPageSize(), MEM_PROT_READ | MEM_PROT_WRITE);49hooks_.profilerPC = (uint32_t *)GetWritableCodePtr();50*hooks_.profilerPC = 0;51hooks_.profilerStatus = (IRProfilerStatus *)GetWritableCodePtr() + 1;52*hooks_.profilerStatus = IRProfilerStatus::NOT_RUNNING;53SetCodePointer(GetCodePtr() + sizeof(uint32_t) * 2, GetWritableCodePtr() + sizeof(uint32_t) * 2);54}5556const u8 *disasmStart = AlignCodePage();57BeginWrite(GetMemoryProtectPageSize());58if (jo.useStaticAlloc) {59saveStaticRegisters_ = AlignCode16();60ST_W(DOWNCOUNTREG, CTXREG, offsetof(MIPSState, downcount));61regs_.EmitSaveStaticRegisters();62RET();6364loadStaticRegisters_ = AlignCode16();65regs_.EmitLoadStaticRegisters();66LD_W(DOWNCOUNTREG, CTXREG, offsetof(MIPSState, downcount));67RET();68} else {69saveStaticRegisters_ = nullptr;70loadStaticRegisters_ = nullptr;71}7273applyRoundingMode_ = AlignCode16();74{75// LoongArch64 does not have any flush to zero capability, so leaving it off76LD_WU(SCRATCH2, CTXREG, offsetof(MIPSState, fcr31));77// We have to do this because otherwise, FMUL will output inaccurate results,78// which cause some game going into infinite loop, for example PATAPON.79ANDI(SCRATCH2, SCRATCH2, 3);80SLLI_D(SCRATCH2, SCRATCH2, 8);8182// We can skip if the rounding mode is nearest (0) and flush is not set.83// (as restoreRoundingMode cleared it out anyway)84FixupBranch skip = BEQZ(SCRATCH2);8586// MIPS Rounding Mode: LoongArch6487// 0: Round nearest 0 RNE88// 1: Round to zero 1 RZ89// 2: Round up (ceil) 2 RP90// 3: Round down (floor) 3 RM91MOVGR2FCSR(FCSR3, SCRATCH2);9293SetJumpTarget(skip);94RET();95}9697hooks_.enterDispatcher = (IRNativeFuncNoArg)AlignCode16();9899// Start by saving some regs on the stack. There are 11 GPs and 8 FPs we want.100// Note: we leave R_SP as, well, SP, so it doesn't need to be saved.101static constexpr LoongArch64Reg regs_to_save[]{ R_RA, R22, R23, R24, R25, R26, R27, R28, R29, R30, R31 };102// TODO: Maybe we shouldn't regalloc all of these? Is it worth it?103static constexpr LoongArch64Reg regs_to_save_fp[]{ F24, F25, F26, F27, F28, F29, F30, F31 };104int saveSize = (64 / 8) * (int)(ARRAY_SIZE(regs_to_save) + ARRAY_SIZE(regs_to_save_fp));105if (saveSize & 0xF)106saveSize += 8;107_assert_msg_((saveSize & 0xF) == 0, "Stack must be kept aligned");108int saveOffset = 0;109ADDI_D(R_SP, R_SP, -saveSize);110for (LoongArch64Reg r : regs_to_save) {111ST_D(r, R_SP, saveOffset);112saveOffset += 64 / 8;113}114for (LoongArch64Reg r : regs_to_save_fp) {115FST_D(r, R_SP, saveOffset);116saveOffset += 64 / 8;117}118_assert_(saveOffset <= saveSize);119120// Fixed registers, these are always kept when in Jit context.121LI(MEMBASEREG, Memory::base);122LI(CTXREG, mipsState);123LI(JITBASEREG, GetBasePtr() - MIPS_EMUHACK_OPCODE);124125LoadStaticRegisters();126WriteDebugProfilerStatus(IRProfilerStatus::IN_JIT);127MovFromPC(SCRATCH1);128WriteDebugPC(SCRATCH1);129outerLoopPCInSCRATCH1_ = GetCodePtr();130MovToPC(SCRATCH1);131outerLoop_ = GetCodePtr();132// Advance can change the downcount (or thread), so must save/restore around it.133SaveStaticRegisters();134RestoreRoundingMode(true);135WriteDebugProfilerStatus(IRProfilerStatus::TIMER_ADVANCE);136QuickCallFunction(&CoreTiming::Advance, R20);137WriteDebugProfilerStatus(IRProfilerStatus::IN_JIT);138ApplyRoundingMode(true);139LoadStaticRegisters();140141dispatcherCheckCoreState_ = GetCodePtr();142LI(SCRATCH1, &coreState);143LD_W(SCRATCH1, SCRATCH1, 0);144FixupBranch badCoreState = BNEZ(SCRATCH1);145146// We just checked coreState, so go to advance if downcount is negative.147BLT(DOWNCOUNTREG, R_ZERO, outerLoop_);148FixupBranch skipToRealDispatch = B();149150dispatcherPCInSCRATCH1_ = GetCodePtr();151MovToPC(SCRATCH1);152153hooks_.dispatcher = GetCodePtr();154FixupBranch bail = BLT(DOWNCOUNTREG, R_ZERO);155SetJumpTarget(skipToRealDispatch);156157dispatcherNoCheck_ = GetCodePtr();158159// Debug160if (enableDebug) {161MOVE(R4, DOWNCOUNTREG);162MOVE(R5, MEMBASEREG);163MOVE(R6, JITBASEREG);164QuickCallFunction(&ShowPC, R20);165}166167LD_WU(SCRATCH1, CTXREG, offsetof(MIPSState, pc));168WriteDebugPC(SCRATCH1);169#ifdef MASKED_PSP_MEMORY170LI(SCRATCH2, 0x3FFFFFFF);171AND(SCRATCH1, SCRATCH1, SCRATCH2);172#endif173ADD_D(SCRATCH1, SCRATCH1, MEMBASEREG);174hooks_.dispatchFetch = GetCodePtr();175LD_WU(SCRATCH1, SCRATCH1, 0);176SRLI_D(SCRATCH2, SCRATCH1, 24);177// We're in other words comparing to the top 8 bits of MIPS_EMUHACK_OPCODE by subtracting.178ADDI_D(SCRATCH2, SCRATCH2, -(MIPS_EMUHACK_OPCODE >> 24));179FixupBranch needsCompile = BNEZ(SCRATCH2);180// No need to mask, JITBASEREG has already accounted for the upper bits.181ADD_D(SCRATCH1, JITBASEREG, SCRATCH1);182JR(SCRATCH1);183SetJumpTarget(needsCompile);184185// No block found, let's jit. We don't need to save static regs, they're all callee saved.186RestoreRoundingMode(true);187WriteDebugProfilerStatus(IRProfilerStatus::COMPILING);188QuickCallFunction(&MIPSComp::JitAt, R20);189WriteDebugProfilerStatus(IRProfilerStatus::IN_JIT);190ApplyRoundingMode(true);191192// Try again, the block index should be set now.193B(dispatcherNoCheck_);194195SetJumpTarget(bail);196197LI(SCRATCH1, &coreState);198LD_W(SCRATCH1, SCRATCH1, 0);199BEQZ(SCRATCH1, outerLoop_);200201const uint8_t *quitLoop = GetCodePtr();202SetJumpTarget(badCoreState);203204WriteDebugProfilerStatus(IRProfilerStatus::NOT_RUNNING);205SaveStaticRegisters();206RestoreRoundingMode(true);207208saveOffset = 0;209for (LoongArch64Reg r : regs_to_save) {210LD_D(r, R_SP, saveOffset);211saveOffset += 64 / 8;212}213for (LoongArch64Reg r : regs_to_save_fp) {214FLD_D(r, R_SP, saveOffset);215saveOffset += 64 / 8;216}217ADDI_D(R_SP, R_SP, saveSize);218219RET();220221hooks_.crashHandler = GetCodePtr();222LI(SCRATCH1, &coreState);223LI(SCRATCH2, CORE_RUNTIME_ERROR);224ST_W(SCRATCH2, SCRATCH1, 0);225B(quitLoop);226227// Leave this at the end, add more stuff above.228if (enableDisasm) {229#if PPSSPP_ARCH(LOONGARCH64)230std::vector<std::string> lines = DisassembleLA64(start, GetCodePtr() - start);231for (auto s : lines) {232INFO_LOG(Log::JIT, "%s", s.c_str());233}234#endif235}236237// Let's spare the pre-generated code from unprotect-reprotect.238AlignCodePage();239jitStartOffset_ = (int)(GetCodePtr() - start);240// Don't forget to zap the instruction cache! This must stay at the end of this function.241FlushIcache();242EndWrite();243}244245} // namespace MIPSComp246247248