diff --git a/.travis.sh b/.travis.sh index 4123e45729..60adacf9c2 100644 --- a/.travis.sh +++ b/.travis.sh @@ -1,5 +1,6 @@ #/bin/bash +export NDK_CCACHE=ccache NDK_VER=android-ndk-r10d download_extract() { @@ -85,7 +86,7 @@ travis_script() { # Compile PPSSPP if [ "$PPSSPP_BUILD_TYPE" = "Linux" ]; then if [ "$CXX" = "g++" ]; then - export CXX="g++-4.8" CC="gcc-4.8" + export CXX="ccache g++-4.8" CC="ccache gcc-4.8" fi if [ "$QT" = "TRUE" ]; then @@ -123,6 +124,8 @@ travis_script() { } travis_after_success() { + ccache -s + if [ "$PPSSPP_BUILD_TYPE" = "Linux" ]; then ./test.py fi diff --git a/.travis.yml b/.travis.yml index c4192ea5e3..c6dd38b16d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,20 +4,19 @@ sudo: required language: cpp -os: linux - dist: trusty -compiler: - - gcc +addons: + apt: + packages: + - build-essential + - libgl1-mesa-dev + - libglu1-mesa-dev + - cmake -env: - - PPSSPP_BUILD_TYPE=Linux - CMAKE=TRUE - - PPSSPP_BUILD_TYPE=Android - - PPSSPP_BUILD_TYPE=Blackberry - CMAKE=TRUE - - PPSSPP_BUILD_TYPE=Symbian +cache: + - apt + - ccache notifications: irc: @@ -30,15 +29,31 @@ notifications: matrix: include: - - compiler: clang + - os: linux + compiler: "gcc linux" env: PPSSPP_BUILD_TYPE=Linux CMAKE=TRUE - - compiler: gcc + - os: linux + compiler: "gcc android" + env: PPSSPP_BUILD_TYPE=Android + - os: linux + compiler: "gcc blackberry" + env: PPSSPP_BUILD_TYPE=Blackberry + CMAKE=TRUE + - os: linux + compiler: "gcc symbian" + env: PPSSPP_BUILD_TYPE=Symbian + - os: linux + compiler: "clang linux" + env: PPSSPP_BUILD_TYPE=Linux + CMAKE=TRUE + - os: linux + compiler: "gcc qt" env: PPSSPP_BUILD_TYPE=Linux QT=TRUE # Can't get iOS to work. # - os: osx -# compiler: clang +# compiler: "clang ios" # env: PPSSPP_BUILD_TYPE=iOS # CMAKE=TRUE diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d137ba551..7ce5de115b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1077,6 +1077,27 @@ include_directories(ext/xxhash) set(CoreExtra) set(CoreExtraLibs) + +set(CoreExtra ${CoreExtra} + Core/MIPS/IR/IRCompALU.cpp + Core/MIPS/IR/IRCompBranch.cpp + Core/MIPS/IR/IRCompFPU.cpp + Core/MIPS/IR/IRCompLoadStore.cpp + Core/MIPS/IR/IRCompVFPU.cpp + Core/MIPS/IR/IRFrontend.cpp + Core/MIPS/IR/IRFrontend.h + Core/MIPS/IR/IRInst.cpp + Core/MIPS/IR/IRInst.h + Core/MIPS/IR/IRInterpreter.cpp + Core/MIPS/IR/IRInterpreter.h + Core/MIPS/IR/IRJit.cpp + Core/MIPS/IR/IRJit.h + Core/MIPS/IR/IRPassSimplify.cpp + Core/MIPS/IR/IRPassSimplify.h + Core/MIPS/IR/IRRegCache.cpp + Core/MIPS/IR/IRRegCache.h + ) + if(ARM) set(CoreExtra ${CoreExtra} Core/MIPS/ARM/ArmAsm.cpp diff --git a/Common/CommonFuncs.h b/Common/CommonFuncs.h index c26b696afb..a533e2df6a 100644 --- a/Common/CommonFuncs.h +++ b/Common/CommonFuncs.h @@ -20,30 +20,27 @@ #include "base/compat.h" #include "CommonTypes.h" -#if defined(IOS) || defined(MIPS) -#include -#endif - template struct CompileTimeAssert; template<> struct CompileTimeAssert {}; -#ifndef _WIN32 +#if !defined(_WIN32) #include #include #if defined(_M_IX86) || defined(_M_X86) - #define Crash() {asm ("int $3");} +#define Crash() {asm ("int $3");} #else - #define Crash() {kill(getpid(), SIGINT);} +#include +#define Crash() {kill(getpid(), SIGINT);} #endif #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) inline u32 __rotl(u32 x, int shift) { - shift &= 31; - if (!shift) return x; - return (x << shift) | (x >> (32 - shift)); + shift &= 31; + if (!shift) return x; + return (x << shift) | (x >> (32 - shift)); } inline u64 __rotl64(u64 x, unsigned int shift){ diff --git a/Core/Config.cpp b/Core/Config.cpp index a5b4bb79d5..137194c475 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -282,9 +282,20 @@ static int DefaultNumWorkers() { return cpu_info.num_cores; } -static bool DefaultJit() { +// TODO: Default to IRJit on iOS when it's done. +static int DefaultCpuCore() { #ifdef IOS - return iosCanUseJit; + return iosCanUseJit ? CPU_CORE_JIT : CPU_CORE_INTERPRETER; +#elif defined(ARM) || defined(ARM64) || defined(_M_IX86) || defined(_M_X64) + return CPU_CORE_JIT; +#else + return CPU_CORE_INTERPRETER; +#endif +} + +static bool DefaultCodeGen() { +#ifdef IOS + return iosCanUseJit ? true : false; #elif defined(ARM) || defined(ARM64) || defined(_M_IX86) || defined(_M_X64) return true; #else @@ -353,7 +364,7 @@ static bool DefaultSasThread() { } static ConfigSetting cpuSettings[] = { - ReportedConfigSetting("Jit", &g_Config.bJit, &DefaultJit, true, true), + ReportedConfigSetting("CPUCore", &g_Config.iCpuCore, &DefaultCpuCore, true, true), ReportedConfigSetting("SeparateCPUThread", &g_Config.bSeparateCPUThread, false, true, true), ReportedConfigSetting("SeparateSASThread", &g_Config.bSeparateSASThread, &DefaultSasThread, true, true), ReportedConfigSetting("SeparateIOThread", &g_Config.bSeparateIOThread, true, true, true), @@ -463,7 +474,7 @@ static ConfigSetting graphicsSettings[] = { ReportedConfigSetting("VertexCache", &g_Config.bVertexCache, true, true, true), ReportedConfigSetting("TextureBackoffCache", &g_Config.bTextureBackoffCache, false, true, true), ReportedConfigSetting("TextureSecondaryCache", &g_Config.bTextureSecondaryCache, false, true, true), - ReportedConfigSetting("VertexDecJit", &g_Config.bVertexDecoderJit, &DefaultJit, false), + ReportedConfigSetting("VertexDecJit", &g_Config.bVertexDecoderJit, &DefaultCodeGen, false), #ifndef MOBILE_DEVICE ConfigSetting("FullScreen", &g_Config.bFullScreen, false), @@ -958,16 +969,16 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) { } // Override ppsspp.ini JIT value to prevent crashing - if (!DefaultJit() && g_Config.bJit) { + if (DefaultCpuCore() != CPU_CORE_JIT && g_Config.iCpuCore == CPU_CORE_JIT) { jitForcedOff = true; - g_Config.bJit = false; + g_Config.iCpuCore = CPU_CORE_INTERPRETER; } } void Config::Save() { if (jitForcedOff) { // if JIT has been forced off, we don't want to screw up the user's ppsspp.ini - g_Config.bJit = true; + g_Config.iCpuCore = CPU_CORE_JIT; } if (iniFilename_.size() && g_Config.bSaveSettings) { @@ -1036,7 +1047,7 @@ void Config::Save() { } if (jitForcedOff) { // force JIT off again just in case Config::Save() is called without exiting PPSSPP - g_Config.bJit = false; + g_Config.iCpuCore = CPU_CORE_INTERPRETER; } } diff --git a/Core/Config.h b/Core/Config.h index b2cfc1351e..6a2016997b 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -33,6 +33,12 @@ const int PSP_DEFAULT_FIRMWARE = 150; static const s8 VOLUME_OFF = 0; static const s8 VOLUME_MAX = 10; +enum CPUCore { + CPU_CORE_INTERPRETER = 0, + CPU_CORE_JIT = 1, + CPU_CORE_IRJIT = 2, +}; + enum { ROTATION_AUTO = 0, ROTATION_LOCKED_HORIZONTAL = 1, @@ -119,7 +125,7 @@ public: // Core bool bIgnoreBadMemAccess; bool bFastMemory; - bool bJit; + int iCpuCore; bool bCheckForNewVersion; bool bForceLagSync; bool bFuncReplacements; diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 83552556d0..60d9c7c66c 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -181,6 +181,18 @@ + + + + + + + + + + + + @@ -507,6 +519,12 @@ + + + + + + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index d4ba376f94..600070d1e8 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -67,6 +67,9 @@ {67687dba-8313-4442-b4eb-4be8c4867b65} + + {119ac973-e457-4025-9e1e-4fb34022ae23} + @@ -634,6 +637,42 @@ Core + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + @@ -1179,6 +1218,24 @@ Core + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + + + MIPS\IR + diff --git a/Core/CoreParameter.h b/Core/CoreParameter.h index ac33c91f58..c9351443c7 100644 --- a/Core/CoreParameter.h +++ b/Core/CoreParameter.h @@ -20,11 +20,7 @@ #include #include "Core/Compatibility.h" - -enum CPUCore { - CPU_INTERPRETER, - CPU_JIT, -}; +#include "Core/Config.h" enum GPUCore { GPUCORE_NULL, @@ -46,6 +42,7 @@ struct CoreParameter { CPUCore cpuCore; GPUCore gpuCore; + GraphicsContext *graphicsContext; // TODO: Find a better place. Thin3DContext *thin3d; bool enableSound; // there aren't multiple sound cores. diff --git a/Core/CoreTiming.cpp b/Core/CoreTiming.cpp index 8e816fcc45..61956cf1e3 100644 --- a/Core/CoreTiming.cpp +++ b/Core/CoreTiming.cpp @@ -570,9 +570,9 @@ void ForceCheck() int cyclesExecuted = slicelength - currentMIPS->downcount; globalTimer += cyclesExecuted; // This will cause us to check for new events immediately. - currentMIPS->downcount = 0; + currentMIPS->downcount = -1; // But let's not eat a bunch more time in Advance() because of this. - slicelength = 0; + slicelength = 1; } void Advance() diff --git a/Core/HLE/HLE.cpp b/Core/HLE/HLE.cpp index a4a448f574..2083b1ba24 100644 --- a/Core/HLE/HLE.cpp +++ b/Core/HLE/HLE.cpp @@ -70,6 +70,7 @@ static int delayedResultEvent = -1; static int hleAfterSyscall = HLE_AFTER_NOTHING; static const char *hleAfterSyscallReschedReason; static const HLEFunction *latestSyscall = nullptr; +static int idleOp; void hleDelayResultFinish(u64 userdata, int cycleslate) { @@ -93,6 +94,7 @@ void HLEInit() { RegisterAllModules(); delayedResultEvent = CoreTiming::RegisterEvent("HLEDelayedResult", hleDelayResultFinish); + idleOp = GetSyscallOp("FakeSysCalls", NID_IDLE); } void HLEDoState(PointerWrap &p) @@ -540,9 +542,8 @@ void CallSyscall(MIPSOpcode op) return; } - if (info->func) - { - if (op == GetSyscallOp("FakeSysCalls", NID_IDLE)) + if (info->func) { + if (op == idleOp) info->func(); else if (info->flags != 0) CallSyscallWithFlags(info); diff --git a/Core/HLE/ReplaceTables.cpp b/Core/HLE/ReplaceTables.cpp index 06b5392d9d..47a8b2d2ad 100644 --- a/Core/HLE/ReplaceTables.cpp +++ b/Core/HLE/ReplaceTables.cpp @@ -1109,7 +1109,7 @@ static int Hook_omertachinmokunookitethelegacy_download_frame() { return 0; } -#define JITFUNC(f) (&MIPSComp::JitInterface::f) +#define JITFUNC(f) (&MIPSComp::MIPSFrontendInterface::f) // Can either replace with C functions or functions emitted in Asm/ArmAsm. static const ReplacementTableEntry entries[] = { diff --git a/Core/HLE/ReplaceTables.h b/Core/HLE/ReplaceTables.h index 84f85b7eea..94ee26d69e 100644 --- a/Core/HLE/ReplaceTables.h +++ b/Core/HLE/ReplaceTables.h @@ -33,6 +33,8 @@ #pragma once +#include + #include "Common/CommonTypes.h" #include "Core/MIPS/JitCommon/JitCommon.h" diff --git a/Core/MIPS/ARM/ArmJit.cpp b/Core/MIPS/ARM/ArmJit.cpp index eaf0a97411..508a6c3b1f 100644 --- a/Core/MIPS/ARM/ArmJit.cpp +++ b/Core/MIPS/ARM/ArmJit.cpp @@ -677,4 +677,14 @@ void ArmJit::WriteSyscallExit() void ArmJit::Comp_DoNothing(MIPSOpcode op) { } +MIPSOpcode ArmJit::GetOriginalOp(MIPSOpcode op) { + JitBlockCache *bc = GetBlockCache(); + int block_num = bc->GetBlockNumberFromEmuHackOp(op, true); + if (block_num >= 0) { + return bc->GetOriginalFirstOp(block_num); + } else { + return op; + } } + +} // namespace diff --git a/Core/MIPS/ARM/ArmJit.h b/Core/MIPS/ARM/ArmJit.h index 07b70af968..7ec62e04c8 100644 --- a/Core/MIPS/ARM/ArmJit.h +++ b/Core/MIPS/ARM/ArmJit.h @@ -33,7 +33,7 @@ namespace MIPSComp { -class ArmJit : public ArmGen::ARMXCodeBlock, public JitInterface { +class ArmJit : public ArmGen::ARMXCodeBlock, public JitInterface, public MIPSFrontendInterface { public: ArmJit(MIPSState *mips); virtual ~ArmJit(); @@ -53,6 +53,7 @@ public: void Compile(u32 em_address) override; // Compiles a block at current MIPS PC bool DescribeCodePtr(const u8 *ptr, std::string &name) override; + MIPSOpcode GetOriginalOp(MIPSOpcode op) override; void Comp_RunBlock(MIPSOpcode op) override; void Comp_ReplacementFunc(MIPSOpcode op) override; diff --git a/Core/MIPS/ARM64/Arm64CompVFPU.cpp b/Core/MIPS/ARM64/Arm64CompVFPU.cpp index 91500507ae..3cd5f03d7b 100644 --- a/Core/MIPS/ARM64/Arm64CompVFPU.cpp +++ b/Core/MIPS/ARM64/Arm64CompVFPU.cpp @@ -1297,6 +1297,8 @@ namespace MIPSComp { } void Arm64Jit::Comp_Vi2x(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (!cpu_info.bNEON) { DISABLE; } diff --git a/Core/MIPS/ARM64/Arm64Jit.cpp b/Core/MIPS/ARM64/Arm64Jit.cpp index 427126e26e..a3f032ea5a 100644 --- a/Core/MIPS/ARM64/Arm64Jit.cpp +++ b/Core/MIPS/ARM64/Arm64Jit.cpp @@ -284,8 +284,6 @@ const u8 *Arm64Jit::DoJit(u32 em_address, JitBlock *b) { gpr.Start(analysis); fpr.Start(analysis); - int partialFlushOffset = 0; - js.numInstructions = 0; while (js.compiling) { gpr.SetCompilerPC(GetCompilerPC()); // Let it know for log messages @@ -636,4 +634,14 @@ void Arm64Jit::WriteSyscallExit() { void Arm64Jit::Comp_DoNothing(MIPSOpcode op) { } +MIPSOpcode Arm64Jit::GetOriginalOp(MIPSOpcode op) { + JitBlockCache *bc = GetBlockCache(); + int block_num = bc->GetBlockNumberFromEmuHackOp(op, true); + if (block_num >= 0) { + return bc->GetOriginalFirstOp(block_num); + } else { + return op; + } } + +} // namespace diff --git a/Core/MIPS/ARM64/Arm64Jit.h b/Core/MIPS/ARM64/Arm64Jit.h index e1c9cf5dec..4aec5fecca 100644 --- a/Core/MIPS/ARM64/Arm64Jit.h +++ b/Core/MIPS/ARM64/Arm64Jit.h @@ -33,7 +33,7 @@ namespace MIPSComp { -class Arm64Jit : public Arm64Gen::ARM64CodeBlock, public JitInterface { +class Arm64Jit : public Arm64Gen::ARM64CodeBlock, public JitInterface, public MIPSFrontendInterface { public: Arm64Jit(MIPSState *mips); virtual ~Arm64Jit(); @@ -54,6 +54,7 @@ public: const u8 *DoJit(u32 em_address, JitBlock *b); bool DescribeCodePtr(const u8 *ptr, std::string &name) override; + MIPSOpcode GetOriginalOp(MIPSOpcode op) override; void Comp_RunBlock(MIPSOpcode op) override; void Comp_ReplacementFunc(MIPSOpcode op) override; diff --git a/Core/MIPS/IR/IRAsm.cpp b/Core/MIPS/IR/IRAsm.cpp new file mode 100644 index 0000000000..f5d9c7ad31 --- /dev/null +++ b/Core/MIPS/IR/IRAsm.cpp @@ -0,0 +1,28 @@ +// Copyright (c) 2015- 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 "base/logging.h" + +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/System.h" +#include "Core/CoreTiming.h" +#include "Common/MemoryUtil.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/JitCommon/JitCommon.h" + + diff --git a/Core/MIPS/IR/IRBackendX86.cpp b/Core/MIPS/IR/IRBackendX86.cpp new file mode 100644 index 0000000000..388962d938 --- /dev/null +++ b/Core/MIPS/IR/IRBackendX86.cpp @@ -0,0 +1,622 @@ + + +#include "Common/x64Emitter.h" +#include "Core/MIPS/IR/IRInst.h" +#include "Core/MemMap.h" + +// Still need a register cache +struct Mapping { + Gen::OpArg dst; + Gen::OpArg src1; + Gen::OpArg src2; +}; + +class RegisterMap { +public: + Mapping Map(IRInst inst); +private: +}; + +Mapping RegisterMap::Map(IRInst inst) { + Mapping map; + return map; +} + + +class IRBackendX86 : public Gen::XCodeBlock { +public: + void Compile(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count); +}; + +void IRBackendX86::Compile(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count) { + RegisterMap regMap; + using namespace Gen; + + const IRInst *end = inst + count; + while (inst != end) { + Mapping map = regMap.Map(*inst); + switch (inst->op) { + /* + case IROp::Nop: + break; + case IROp::SetConst: + MOV(32, map.dst, map.src1); + break; + case IROp::SetConstF: + MOV(32, R(EAX), map.src1); + MOVD_xmm(map.dst, EAX); + break; + case IROp::Add: + if (map.src1.IsSimpleReg() && map.src2.IsSimpleReg()) { + LEA(32, map.dst.GetSimpleReg(), MRegSum(map.src1.GetSimpleReg(), map.src2.GetSimpleReg())); + break; + } + mips->r[inst->dest] = mips->r[inst->src1] + mips->r[inst->src2]; + break; + case IROp::Sub: + mips->r[inst->dest] = mips->r[inst->src1] - mips->r[inst->src2]; + break; + case IROp::And: + mips->r[inst->dest] = mips->r[inst->src1] & mips->r[inst->src2]; + break; + case IROp::Or: + mips->r[inst->dest] = mips->r[inst->src1] | mips->r[inst->src2]; + break; + case IROp::Xor: + mips->r[inst->dest] = mips->r[inst->src1] ^ mips->r[inst->src2]; + break; + case IROp::Mov: + mips->r[inst->dest] = mips->r[inst->src1]; + break; + case IROp::AddConst: + mips->r[inst->dest] = mips->r[inst->src1] + constPool[inst->src2]; + break; + case IROp::SubConst: + mips->r[inst->dest] = mips->r[inst->src1] - constPool[inst->src2]; + break; + case IROp::AndConst: + mips->r[inst->dest] = mips->r[inst->src1] & constPool[inst->src2]; + break; + case IROp::OrConst: + mips->r[inst->dest] = mips->r[inst->src1] | constPool[inst->src2]; + break; + case IROp::XorConst: + mips->r[inst->dest] = mips->r[inst->src1] ^ constPool[inst->src2]; + break; + case IROp::Neg: + mips->r[inst->dest] = -(s32)mips->r[inst->src1]; + break; + case IROp::Not: + mips->r[inst->dest] = ~mips->r[inst->src1]; + break; + case IROp::Ext8to32: + mips->r[inst->dest] = (s32)(s8)mips->r[inst->src1]; + break; + case IROp::Ext16to32: + mips->r[inst->dest] = (s32)(s16)mips->r[inst->src1]; + break; + + case IROp::Load8: + mips->r[inst->dest] = Memory::ReadUnchecked_U8(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load8Ext: + mips->r[inst->dest] = (s32)(s8)Memory::ReadUnchecked_U8(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load16: + mips->r[inst->dest] = Memory::ReadUnchecked_U16(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load16Ext: + mips->r[inst->dest] = (s32)(s16)Memory::ReadUnchecked_U16(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load32: + mips->r[inst->dest] = Memory::ReadUnchecked_U32(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::LoadFloat: + mips->f[inst->dest] = Memory::ReadUnchecked_Float(mips->r[inst->src1] + constPool[inst->src2]); + break; + + case IROp::Store8: + Memory::WriteUnchecked_U8(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Store16: + Memory::WriteUnchecked_U16(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Store32: + Memory::WriteUnchecked_U32(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::StoreFloat: + Memory::WriteUnchecked_Float(mips->f[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + + case IROp::LoadVec4: + { + u32 base = mips->r[inst->src1] + constPool[inst->src2]; +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_load_ps((const float *)Memory::GetPointerUnchecked(base))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = Memory::ReadUnchecked_Float(base + 4 * i); +#endif + break; + } + case IROp::StoreVec4: + { + u32 base = mips->r[inst->src1] + constPool[inst->src2]; +#if defined(_M_SSE) + _mm_store_ps((float *)Memory::GetPointerUnchecked(base), _mm_load_ps(&mips->f[inst->dest])); +#else + for (int i = 0; i < 4; i++) + Memory::WriteUnchecked_Float(mips->f[inst->dest + i], base + 4 * i); +#endif + break; + } + + case IROp::Vec4Init: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_load_ps(vec4InitValues[inst->src1])); +#else + memcpy(&mips->f[inst->dest + i], vec4InitValues[inst->src1], 4 * sizeof(float)); +#endif + break; + + case IROp::Vec4Shuffle: + { + // Can't use the SSE shuffle here because it takes an immediate. + // Backends with SSE support could use that though. + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + ((inst->src2 >> (i * 2)) & 3)]; + break; + } + + case IROp::Vec4Mov: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_load_ps(&mips->f[inst->src1])); +#else + memcpy(&mips->f[inst->dest], &mips->f[inst->src1], 4 * sizeof(float)); +#endif + break; + + case IROp::Vec4Add: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_add_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] + mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Sub: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_sub_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] - mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Mul: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_mul_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] * mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Div: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_div_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] / mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Scale: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_mul_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_set1_ps(mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] * mips->f[inst->src2 + i]; +#endif + break; + + case IROp::FCmpVfpuBit: + { + int op = inst->dest & 0xF; + int bit = inst->dest >> 4; + int result = 0; + switch (op) { + case VC_EQ: result = mips->f[inst->src1] == mips->f[inst->src2]; break; + case VC_NE: result = mips->f[inst->src1] != mips->f[inst->src2]; break; + case VC_LT: result = mips->f[inst->src1] < mips->f[inst->src2]; break; + case VC_LE: result = mips->f[inst->src1] <= mips->f[inst->src2]; break; + case VC_GT: result = mips->f[inst->src1] > mips->f[inst->src2]; break; + case VC_GE: result = mips->f[inst->src1] >= mips->f[inst->src2]; break; + case VC_EZ: result = mips->f[inst->src1] == 0.0f; break; + case VC_NZ: result = mips->f[inst->src1] != 0.0f; break; + case VC_TR: result = 1; break; + case VC_FL: result = 0; break; + default: + result = 0; + } + if (result != 0) { + mips->vfpuCtrl[VFPU_CTRL_CC] |= (1 << bit); + } else { + mips->vfpuCtrl[VFPU_CTRL_CC] &= ~(1 << bit); + } + } + break; + + case IROp::FCmpVfpuAggregate: + { + int mask = inst->dest; + u32 cc = mips->vfpuCtrl[VFPU_CTRL_CC]; + int a = (cc & mask) ? 0x10 : 0x00; + int b = (cc & mask) == mask ? 0x20 : 0x00; + mips->vfpuCtrl[VFPU_CTRL_CC] = (cc & ~0x30) | a | b;; + } + break; + + case IROp::FCmovVfpuCC: + if (((mips->vfpuCtrl[VFPU_CTRL_CC] >> (inst->src2 & 0x7f)) & 1) == (inst->src2 >> 7)) { + mips->f[inst->dest] = mips->f[inst->src1]; + } + break; + + // Not quickly implementable on all platforms, unfortunately. + case IROp::Vec4Dot: + { + float dot = mips->f[inst->src1] * mips->f[inst->src2]; + for (int i = 1; i < 4; i++) + dot += mips->f[inst->src1 + i] * mips->f[inst->src2 + i]; + mips->f[inst->dest] = dot; + break; + } + + case IROp::FSin: + mips->f[inst->dest] = vfpu_sin(mips->f[inst->src1]); + break; + case IROp::FCos: + mips->f[inst->dest] = vfpu_cos(mips->f[inst->src1]); + break; + case IROp::FRSqrt: + mips->f[inst->dest] = 1.0f / sqrtf(mips->f[inst->src1]); + break; + case IROp::FRecip: + mips->f[inst->dest] = 1.0f / mips->f[inst->src1]; + break; + case IROp::FAsin: + mips->f[inst->dest] = vfpu_asin(mips->f[inst->src1]); + break; + + case IROp::ShlImm: + mips->r[inst->dest] = mips->r[inst->src1] << (int)inst->src2; + break; + case IROp::ShrImm: + mips->r[inst->dest] = mips->r[inst->src1] >> (int)inst->src2; + break; + case IROp::SarImm: + mips->r[inst->dest] = (s32)mips->r[inst->src1] >> (int)inst->src2; + break; + case IROp::RorImm: + { + u32 x = mips->r[inst->src1]; + int sa = inst->src2; + mips->r[inst->dest] = (x >> sa) | (x << (32 - sa)); + } + break; + + case IROp::Shl: + mips->r[inst->dest] = mips->r[inst->src1] << (mips->r[inst->src2] & 31); + break; + case IROp::Shr: + mips->r[inst->dest] = mips->r[inst->src1] >> (mips->r[inst->src2] & 31); + break; + case IROp::Sar: + mips->r[inst->dest] = (s32)mips->r[inst->src1] >> (mips->r[inst->src2] & 31); + break; + case IROp::Ror: + { + u32 x = mips->r[inst->src1]; + int sa = mips->r[inst->src2] & 31; + mips->r[inst->dest] = (x >> sa) | (x << (32 - sa)); + } + break; + + case IROp::Clz: + { + int x = 31; + int count = 0; + int value = mips->r[inst->src1]; + while (x >= 0 && !(value & (1 << x))) { + count++; + x--; + } + mips->r[inst->dest] = count; + break; + } + + case IROp::Slt: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)mips->r[inst->src2]; + break; + + case IROp::SltU: + mips->r[inst->dest] = mips->r[inst->src1] < mips->r[inst->src2]; + break; + + case IROp::SltConst: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)constPool[inst->src2]; + break; + + case IROp::SltUConst: + mips->r[inst->dest] = mips->r[inst->src1] < constPool[inst->src2]; + break; + + case IROp::MovZ: + if (mips->r[inst->src1] == 0) + mips->r[inst->dest] = mips->r[inst->src2]; + break; + case IROp::MovNZ: + if (mips->r[inst->src1] != 0) + mips->r[inst->dest] = mips->r[inst->src2]; + break; + + case IROp::Max: + mips->r[inst->dest] = (s32)mips->r[inst->src1] > (s32)mips->r[inst->src2] ? mips->r[inst->src1] : mips->r[inst->src2]; + break; + case IROp::Min: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)mips->r[inst->src2] ? mips->r[inst->src1] : mips->r[inst->src2]; + break; + + case IROp::MtLo: + mips->lo = mips->r[inst->src1]; + break; + case IROp::MtHi: + mips->hi = mips->r[inst->src1]; + break; + case IROp::MfLo: + mips->r[inst->dest] = mips->lo; + break; + case IROp::MfHi: + mips->r[inst->dest] = mips->hi; + break; + + case IROp::Mult: + { + s64 result = (s64)(s32)mips->r[inst->src1] * (s64)(s32)mips->r[inst->src2]; + memcpy(&mips->lo, &result, 8); + break; + } + case IROp::MultU: + { + u64 result = (u64)mips->r[inst->src1] * (u64)mips->r[inst->src2]; + memcpy(&mips->lo, &result, 8); + break; + } + + case IROp::BSwap16: + { + u32 x = mips->r[inst->src1]; + mips->r[inst->dest] = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8); + break; + } + case IROp::BSwap32: + { + u32 x = mips->r[inst->src1]; + mips->r[inst->dest] = ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24); + break; + } + + case IROp::FAdd: + mips->f[inst->dest] = mips->f[inst->src1] + mips->f[inst->src2]; + break; + case IROp::FSub: + mips->f[inst->dest] = mips->f[inst->src1] - mips->f[inst->src2]; + break; + case IROp::FMul: + mips->f[inst->dest] = mips->f[inst->src1] * mips->f[inst->src2]; + break; + case IROp::FDiv: + mips->f[inst->dest] = mips->f[inst->src1] / mips->f[inst->src2]; + break; + case IROp::FMin: + mips->f[inst->dest] = std::min(mips->f[inst->src1], mips->f[inst->src2]); + break; + case IROp::FMax: + mips->f[inst->dest] = std::max(mips->f[inst->src1], mips->f[inst->src2]); + break; + + case IROp::FMov: + mips->f[inst->dest] = mips->f[inst->src1]; + break; + case IROp::FAbs: + mips->f[inst->dest] = fabsf(mips->f[inst->src1]); + break; + case IROp::FSqrt: + mips->f[inst->dest] = sqrtf(mips->f[inst->src1]); + break; + case IROp::FNeg: + mips->f[inst->dest] = -mips->f[inst->src1]; + break; + case IROp::FSat0_1: + mips->f[inst->dest] = clamp_value(mips->f[inst->src1], 0.0f, 1.0f); + break; + case IROp::FSatMinus1_1: + mips->f[inst->dest] = clamp_value(mips->f[inst->src1], -1.0f, 1.0f); + break; + + case IROp::FpCondToReg: + mips->r[inst->dest] = mips->fpcond; + break; + case IROp::VfpuCtrlToReg: + mips->r[inst->dest] = mips->vfpuCtrl[inst->src1]; + break; + case IROp::FRound: + mips->fs[inst->dest] = (int)floorf(mips->f[inst->src1] + 0.5f); + break; + case IROp::FTrunc: + { + float src = mips->f[inst->src1]; + if (src >= 0.0f) { + mips->fs[inst->dest] = (int)floorf(src); + // Overflow, but it was positive. + if (mips->fs[inst->dest] == -2147483648LL) { + mips->fs[inst->dest] = 2147483647LL; + } + } else { + // Overflow happens to be the right value anyway. + mips->fs[inst->dest] = (int)ceilf(src); + } + break; + } + case IROp::FCeil: + mips->fs[inst->dest] = (int)ceilf(mips->f[inst->src1]); + break; + case IROp::FFloor: + mips->fs[inst->dest] = (int)floorf(mips->f[inst->src1]); + break; + case IROp::FCmp: + switch (inst->dest) { + case IRFpCompareMode::False: + mips->fpcond = 0; + break; + case IRFpCompareMode::EqualOrdered: + case IRFpCompareMode::EqualUnordered: + mips->fpcond = mips->f[inst->src1] == mips->f[inst->src2]; + break; + case IRFpCompareMode::LessEqualOrdered: + case IRFpCompareMode::LessEqualUnordered: + mips->fpcond = mips->f[inst->src1] <= mips->f[inst->src2]; + break; + case IRFpCompareMode::LessOrdered: + case IRFpCompareMode::LessUnordered: + mips->fpcond = mips->f[inst->src1] < mips->f[inst->src2]; + break; + } + break; + + case IROp::FCvtSW: + mips->f[inst->dest] = (float)mips->fs[inst->src1]; + break; + case IROp::FCvtWS: + { + float src = mips->f[inst->src1]; + if (my_isnanorinf(src)) + { + mips->fs[inst->dest] = my_isinf(src) && src < 0.0f ? -2147483648LL : 2147483647LL; + break; + } + switch (mips->fcr31 & 3) + { + case 0: mips->fs[inst->dest] = (int)round_ieee_754(src); break; // RINT_0 + case 1: mips->fs[inst->dest] = (int)src; break; // CAST_1 + case 2: mips->fs[inst->dest] = (int)ceilf(src); break; // CEIL_2 + case 3: mips->fs[inst->dest] = (int)floorf(src); break; // FLOOR_3 + } + break; //cvt.w.s + } + + case IROp::ZeroFpCond: + mips->fpcond = 0; + break; + + case IROp::FMovFromGPR: + memcpy(&mips->f[inst->dest], &mips->r[inst->src1], 4); + break; + case IROp::FMovToGPR: + memcpy(&mips->r[inst->dest], &mips->f[inst->src1], 4); + break; + + case IROp::ExitToConst: + return constPool[inst->dest]; + + case IROp::ExitToReg: + return mips->r[inst->dest]; + + case IROp::ExitToConstIfEq: + if (mips->r[inst->src1] == mips->r[inst->src2]) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfNeq: + if (mips->r[inst->src1] != mips->r[inst->src2]) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfGtZ: + if ((s32)mips->r[inst->src1] > 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfGeZ: + if ((s32)mips->r[inst->src1] >= 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfLtZ: + if ((s32)mips->r[inst->src1] < 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfLeZ: + if ((s32)mips->r[inst->src1] <= 0) + return constPool[inst->dest]; + break; + + case IROp::Downcount: + mips->downcount -= (inst->src1) | ((inst->src2) << 8); + break; + + case IROp::SetPC: + mips->pc = mips->r[inst->src1]; + break; + + case IROp::SetPCConst: + mips->pc = constPool[inst->src1]; + break; + + case IROp::Syscall: + // SetPC was executed before. + { + MIPSOpcode op(constPool[inst->src1]); + CallSyscall(op); + return mips->pc; + } + + case IROp::Interpret: // SLOW fallback. Can be made faster. + { + MIPSOpcode op(constPool[inst->src1]); + MIPSInterpret(op); + break; + } + + case IROp::CallReplacement: + { + int funcIndex = constPool[inst->src1]; + const ReplacementTableEntry *f = GetReplacementFunc(funcIndex); + int cycles = f->replaceFunc(); + mips->downcount -= cycles; + break; + } + + case IROp::Break: + Crash(); + break; + + case IROp::SetCtrlVFPU: + mips->vfpuCtrl[inst->dest] = constPool[inst->src1]; + break; + + case IROp::SetCtrlVFPUReg: + mips->vfpuCtrl[inst->dest] = mips->r[inst->src1]; + break; + + case IROp::SetCtrlVFPUFReg: + memcpy(&mips->vfpuCtrl[inst->dest], &mips->f[inst->src1], 4); + break; + */ + default: + Crash(); + } + inst++; + } + + // If we got here, the block was badly constructed. + Crash(); +} diff --git a/Core/MIPS/IR/IRCompALU.cpp b/Core/MIPS/IR/IRCompALU.cpp new file mode 100644 index 0000000000..8ffa632aff --- /dev/null +++ b/Core/MIPS/IR/IRCompALU.cpp @@ -0,0 +1,376 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 + +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/IR/IRFrontend.h" +#include "Common/CPUDetect.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +namespace MIPSComp { + +void IRFrontend::Comp_IType(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + s32 simm = (s32)(s16)(op & 0xFFFF); // sign extension + u32 uimm = op & 0xFFFF; + u32 suimm = (u32)(s32)simm; + + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + + // noop, won't write to ZERO. + if (rt == 0) + return; + + switch (op >> 26) { + case 8: // same as addiu? + case 9: // R(rt) = R(rs) + simm; break; //addiu + ir.Write(IROp::AddConst, rt, rs, ir.AddConstant(simm)); + break; + + case 12: ir.Write(IROp::AndConst, rt, rs, ir.AddConstant(uimm)); break; + case 13: ir.Write(IROp::OrConst, rt, rs, ir.AddConstant(uimm)); break; + case 14: ir.Write(IROp::XorConst, rt, rs, ir.AddConstant(uimm)); break; + + case 10: // R(rt) = (s32)R(rs) < simm; break; //slti + ir.Write(IROp::SltConst, rt, rs, ir.AddConstant(simm)); + break; + + case 11: // R(rt) = R(rs) < suimm; break; //sltiu + ir.Write(IROp::SltUConst, rt, rs, ir.AddConstant(suimm)); + break; + + case 15: // R(rt) = uimm << 16; //lui + ir.WriteSetConstant(rt, uimm << 16); + break; + + default: + Comp_Generic(op); + break; + } +} + +void IRFrontend::Comp_RType2(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + + // Don't change $zr. + if (rd == 0) + return; + + switch (op & 63) { + case 22: //clz + ir.Write(IROp::Clz, rd, rs); + break; + case 23: //clo + ir.Write(IROp::Not, IRTEMP_0, rs); + ir.Write(IROp::Clz, rd, IRTEMP_0); + break; + default: + DISABLE; + } +} + +void IRFrontend::Comp_RType3(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + + // noop, won't write to ZERO. + if (rd == 0) + return; + + switch (op & 63) { + case 10: //if (!R(rt)) R(rd) = R(rs); break; //movz + ir.Write(IROp::MovZ, rd, rt, rs); + break; + case 11:// if (R(rt)) R(rd) = R(rs); break; //movn + ir.Write(IROp::MovNZ, rd, rt, rs); + break; + + case 32: //R(rd) = R(rs) + R(rt); break; //add + case 33: //R(rd) = R(rs) + R(rt); break; //addu + ir.Write(IROp::Add, rd, rs, rt); + break; + + case 34: //R(rd) = R(rs) - R(rt); break; //sub + case 35: //R(rd) = R(rs) - R(rt); break; //subu + ir.Write(IROp::Sub, rd, rs, rt); + break; + + case 36: //R(rd) = R(rs) & R(rt); break; //and + ir.Write(IROp::And, rd, rs, rt); + break; + case 37: //R(rd) = R(rs) | R(rt); break; //or + ir.Write(IROp::Or, rd, rs, rt); + break; + case 38: //R(rd) = R(rs) ^ R(rt); break; //xor/eor + ir.Write(IROp::Xor, rd, rs, rt); + break; + + case 39: // R(rd) = ~(R(rs) | R(rt)); break; //nor + if (rs == 0) { + ir.Write(IROp::Not, rd, rt); + } else if (rt == 0) { + ir.Write(IROp::Not, rd, rs); + } else { + ir.Write(IROp::Or, IRTEMP_0, rs, rt); + ir.Write(IROp::Not, rd, IRTEMP_0); + } + break; + + case 42: //R(rd) = (int)R(rs) < (int)R(rt); break; //slt + ir.Write(IROp::Slt, rd, rs, rt); + break; + + case 43: //R(rd) = R(rs) < R(rt); break; //sltu + ir.Write(IROp::SltU, rd, rs, rt); + break; + + case 44: //R(rd) = max(R(rs), R(rt); break; //max + ir.Write(IROp::Max, rd, rs, rt); + break; + + case 45: //R(rd) = min(R(rs), R(rt)); break; //min + ir.Write(IROp::Min, rd, rs, rt); + break; + + default: + Comp_Generic(op); + break; + } +} + +void IRFrontend::CompShiftImm(MIPSOpcode op, IROp shiftOpImm, int sa) { + MIPSGPReg rd = _RD; + MIPSGPReg rt = _RT; + ir.Write(shiftOpImm, rd, rt, sa); +} + +void IRFrontend::CompShiftVar(MIPSOpcode op, IROp shiftOp, IROp shiftOpImm) { + MIPSGPReg rd = _RD; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + ir.Write(IROp::AndConst, IRTEMP_0, rs, ir.AddConstant(31)); + ir.Write(shiftOp, rd, rt, IRTEMP_0); +} + +void IRFrontend::Comp_ShiftType(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + int fd = _FD; + int sa = _SA; + + // noop, won't write to ZERO. + if (rd == 0) + return; + + // WARNING : ROTR + switch (op & 0x3f) { + case 0: CompShiftImm(op, IROp::ShlImm, sa); break; //sll + case 2: CompShiftImm(op, (rs == 1 ? IROp::RorImm : IROp::ShrImm), sa); break; //srl + case 3: CompShiftImm(op, IROp::SarImm, sa); break; //sra + case 4: CompShiftVar(op, IROp::Shl, IROp::ShlImm); break; //sllv + case 6: CompShiftVar(op, (fd == 1 ? IROp::Ror : IROp::Shr), (fd == 1 ? IROp::RorImm : IROp::ShrImm)); break; //srlv + case 7: CompShiftVar(op, IROp::Sar, IROp::SarImm); break; //srav + default: + DISABLE; + break; + } +} + +void IRFrontend::Comp_Special3(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rs = _RS; + MIPSGPReg rt = _RT; + + int pos = _POS; + int size = _SIZE + 1; + u32 mask = 0xFFFFFFFFUL >> (32 - size); + + // Don't change $zr. + if (rt == 0) + return; + + switch (op & 0x3f) { + case 0x0: // ext + if (pos != 0) { + ir.Write(IROp::ShrImm, rt, rs, pos); + ir.Write(IROp::AndConst, rt, rt, ir.AddConstant(mask)); + } else { + ir.Write(IROp::AndConst, rt, rs, ir.AddConstant(mask)); + } + break; + + case 0x4: //ins + { + u32 sourcemask = mask >> pos; + u32 destmask = ~(sourcemask << pos); + ir.Write(IROp::AndConst, IRTEMP_0, rs, ir.AddConstant(sourcemask)); + if (pos != 0) { + ir.Write(IROp::ShlImm, IRTEMP_0, IRTEMP_0, pos); + } + ir.Write(IROp::AndConst, rt, rt, ir.AddConstant(destmask)); + ir.Write(IROp::Or, rt, rt, IRTEMP_0); + } + break; + } +} + + +void IRFrontend::Comp_Allegrex(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rt = _RT; + MIPSGPReg rd = _RD; + // Don't change $zr. + if (rd == 0) + return; + + switch ((op >> 6) & 31) { + case 16: // seb // R(rd) = (u32)(s32)(s8)(u8)R(rt); + ir.Write(IROp::Ext8to32, rd, rt); + break; + + case 24: // seh + ir.Write(IROp::Ext16to32, rd, rt); + break; + + case 20: //bitrev + default: + Comp_Generic(op); + return; + } +} + +void IRFrontend::Comp_Allegrex2(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rt = _RT; + MIPSGPReg rd = _RD; + // Don't change $zr. + if (rd == 0) + return; + + switch (op & 0x3ff) { + case 0xA0: //wsbh + ir.Write(IROp::BSwap16, rd, rt); + break; + case 0xE0: //wsbw + ir.Write(IROp::BSwap32, rd, rt); + break; + default: + Comp_Generic(op); + break; + } +} + +void IRFrontend::Comp_MulDivType(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + + switch (op & 63) { + case 16: // R(rd) = HI; //mfhi + if (rd != MIPS_REG_ZERO) { + ir.Write(IROp::MfHi, rd); + } + break; + + case 17: // HI = R(rs); //mthi + ir.Write(IROp::MtHi, 0, rs); + break; + + case 18: // R(rd) = LO; break; //mflo + if (rd != MIPS_REG_ZERO) { + ir.Write(IROp::MfLo, rd); + } + break; + + case 19: // LO = R(rs); break; //mtlo + ir.Write(IROp::MtLo, 0, rs); + break; + + case 24: //mult (the most popular one). lo,hi = signed mul (rs * rt) + ir.Write(IROp::Mult, 0, rs, rt); + break; + + case 25: //multu (2nd) lo,hi = unsigned mul (rs * rt) + ir.Write(IROp::MultU, 0, rs, rt); + break; + + case 26: //div + DISABLE; + ir.Write(IROp::Div, 0, rs, rt); + break; + + case 27: //divu + DISABLE; + ir.Write(IROp::DivU, 0, rs, rt); + break; + + case 28: //madd + DISABLE; + ir.Write(IROp::Madd, 0, rs, rt); + break; + + case 29: //maddu + DISABLE; + ir.Write(IROp::MaddU, 0, rs, rt); + break; + + case 46: // msub + DISABLE; + ir.Write(IROp::Msub, 0, rs, rt); + break; + + case 47: // msubu + DISABLE; + ir.Write(IROp::MsubU, 0, rs, rt); + break; + + default: + DISABLE; + } +} + +} diff --git a/Core/MIPS/IR/IRCompBranch.cpp b/Core/MIPS/IR/IRCompBranch.cpp new file mode 100644 index 0000000000..609b8f478f --- /dev/null +++ b/Core/MIPS/IR/IRCompBranch.cpp @@ -0,0 +1,400 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 "profiler/profiler.h" + +#include "Core/Reporting.h" +#include "Core/Config.h" +#include "Core/MemMap.h" +#include "Core/HLE/HLE.h" +#include "Core/HLE/HLETables.h" + +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSAnalyst.h" +#include "Core/MIPS/MIPSTables.h" + +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/JitCommon/JitBlockCache.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +#define LOOPOPTIMIZATION 0 + +#define MIPS_IS_BREAK(op) (((op) & 0xFC00003F) == 13) + +using namespace MIPSAnalyst; + +namespace MIPSComp +{ + +void IRFrontend::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in RSRTComp delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rt, rs); + + // Often, div/divu are followed by a likely "break" if the divisor was zero. + // Stalling is not really useful for us, so we optimize this out. + if (likely && offset == 4 && MIPS_IS_BREAK(delaySlotOp)) { + // Okay, let's not actually branch at all. We're done here. + EatInstruction(delaySlotOp); + // Let's not double-count the downcount, though. + js.downcountAmount--; + return; + } + + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + MIPSGPReg lhs = rs; + MIPSGPReg rhs = rt; + if (!delaySlotIsNice && !likely) { // if likely, we don't need this + if (rs != 0) { + ir.Write(IROp::Mov, IRTEMP_LHS, rs); + lhs = (MIPSGPReg)IRTEMP_LHS; + } + if (rt != 0) { + ir.Write(IROp::Mov, IRTEMP_RHS, rt); + rhs = (MIPSGPReg)IRTEMP_RHS; + } + } + + if (!likely) + CompileDelaySlot(); + + FlushAll(); + ir.Write(ComparisonToExit(cc), ir.AddConstant(GetCompilerPC() + 8), lhs, rhs); + // This makes the block "impure" :( + if (likely) + CompileDelaySlot(); + + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + + js.compiling = false; +} + +void IRFrontend::BranchRSZeroComp(MIPSOpcode op, IRComparison cc, bool andLink, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in RSZeroComp delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + MIPSGPReg rs = _RS; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rs); + + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + MIPSGPReg lhs = rs; + if (!delaySlotIsNice) { // if likely, we don't need this + ir.Write(IROp::Mov, IRTEMP_LHS, rs); + lhs = (MIPSGPReg)IRTEMP_LHS; + } + if (andLink) + ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 8); + + if (!likely) + CompileDelaySlot(); + + FlushAll(); + ir.Write(ComparisonToExit(cc), ir.AddConstant(GetCompilerPC() + 8), lhs); + if (likely) + CompileDelaySlot(); + // Taken + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + js.compiling = false; +} + +void IRFrontend::Comp_RelBranch(MIPSOpcode op) { + // The CC flags here should be opposite of the actual branch because they skip the branching action. + switch (op >> 26) { + case 4: BranchRSRTComp(op, IRComparison::NotEqual, false); break;//beq + case 5: BranchRSRTComp(op, IRComparison::Equal, false); break;//bne + + case 6: BranchRSZeroComp(op, IRComparison::Greater, false, false); break;//blez + case 7: BranchRSZeroComp(op, IRComparison::LessEqual, false, false); break;//bgtz + + case 20: BranchRSRTComp(op, IRComparison::NotEqual, true); break;//beql + case 21: BranchRSRTComp(op, IRComparison::Equal, true); break;//bnel + + case 22: BranchRSZeroComp(op, IRComparison::Greater, false, true); break;//blezl + case 23: BranchRSZeroComp(op, IRComparison::LessEqual, false, true); break;//bgtzl + + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } +} + +void IRFrontend::Comp_RelBranchRI(MIPSOpcode op) { + switch ((op >> 16) & 0x1F) { + case 0: BranchRSZeroComp(op, IRComparison::GreaterEqual, false, false); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltz + case 1: BranchRSZeroComp(op, IRComparison::Less, false, false); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgez + case 2: BranchRSZeroComp(op, IRComparison::GreaterEqual, false, true); break; //if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 8; break;//bltzl + case 3: BranchRSZeroComp(op, IRComparison::Less, false, true); break; //if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 8; break;//bgezl + case 16: BranchRSZeroComp(op, IRComparison::GreaterEqual, true, false); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else PC += 4; break;//bltzal + case 17: BranchRSZeroComp(op, IRComparison::Less, true, false); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else PC += 4; break;//bgezal + case 18: BranchRSZeroComp(op, IRComparison::GreaterEqual, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) < 0) DelayBranchTo(addr); else SkipLikely(); break;//bltzall + case 19: BranchRSZeroComp(op, IRComparison::Less, true, true); break; //R(MIPS_REG_RA) = PC + 8; if ((s32)R(rs) >= 0) DelayBranchTo(addr); else SkipLikely(); break;//bgezall + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } +} + +// If likely is set, discard the branch slot if NOT taken. +void IRFrontend::BranchFPFlag(MIPSOpcode op, IRComparison cc, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in FPFlag delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + u32 targetAddr = GetCompilerPC() + offset + 4; + + ir.Write(IROp::FpCondToReg, IRTEMP_LHS); + if (!likely) + CompileDelaySlot(); + + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + FlushAll(); + // Not taken + ir.Write(ComparisonToExit(cc), ir.AddConstant(GetCompilerPC() + 8), IRTEMP_LHS, 0); + // Taken + if (likely) + CompileDelaySlot(); + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + js.compiling = false; +} + +void IRFrontend::Comp_FPUBranch(MIPSOpcode op) { + switch((op >> 16) & 0x1f) { + case 0: BranchFPFlag(op, IRComparison::NotEqual, false); break; // bc1f + case 1: BranchFPFlag(op, IRComparison::Equal, false); break; // bc1t + case 2: BranchFPFlag(op, IRComparison::NotEqual, true); break; // bc1fl + case 3: BranchFPFlag(op, IRComparison::Equal, true); break; // bc1tl + default: + _dbg_assert_msg_(CPU, 0, "Trying to interpret instruction that can't be interpreted"); + break; + } +} + +// If likely is set, discard the branch slot if NOT taken. +void IRFrontend::BranchVFPUFlag(MIPSOpcode op, IRComparison cc, bool likely) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in VFPU delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + int offset = _IMM16 << 2; + u32 targetAddr = GetCompilerPC() + offset + 4; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + ir.Write(IROp::VfpuCtrlToReg, IRTEMP_LHS, VFPU_CTRL_CC); + + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + // Sometimes there's a VFPU branch in a delay slot (Disgaea 2: Dark Hero Days, Zettai Hero Project, La Pucelle) + // The behavior is undefined - the CPU may take the second branch even if the first one passes. + // However, it does consistently try each branch, which these games seem to expect. + bool delaySlotIsBranch = MIPSCodeUtils::IsVFPUBranch(delaySlotOp); + if (!likely) + CompileDelaySlot(); + + if (delaySlotIsBranch && (signed short)(delaySlotOp & 0xFFFF) != (signed short)(op & 0xFFFF) - 1) + ERROR_LOG_REPORT(JIT, "VFPU branch in VFPU delay slot at %08x with different target", GetCompilerPC()); + + int imm3 = (op >> 18) & 7; + + u32 notTakenTarget = GetCompilerPC() + (delaySlotIsBranch ? 4 : 8); + + ir.Write(IROp::AndConst, IRTEMP_LHS, IRTEMP_LHS, ir.AddConstant(1 << imm3)); + FlushAll(); + ir.Write(ComparisonToExit(cc), ir.AddConstant(notTakenTarget), IRTEMP_LHS, 0); + + if (likely) + CompileDelaySlot(); + + // Taken + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + js.compiling = false; +} + +void IRFrontend::Comp_VBranch(MIPSOpcode op) { + switch ((op >> 16) & 3) { + case 0: BranchVFPUFlag(op, IRComparison::NotEqual, false); break; // bvf + case 1: BranchVFPUFlag(op, IRComparison::Equal, false); break; // bvt + case 2: BranchVFPUFlag(op, IRComparison::NotEqual, true); break; // bvfl + case 3: BranchVFPUFlag(op, IRComparison::Equal, true); break; // bvtl + } +} + +void IRFrontend::Comp_Jump(MIPSOpcode op) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in Jump delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + + u32 off = _IMM26 << 2; + u32 targetAddr = (GetCompilerPC() & 0xF0000000) | off; + + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + // Might be a stubbed address or something? + if (!Memory::IsValidAddress(targetAddr)) { + if (js.nextExit == 0) { + ERROR_LOG_REPORT(JIT, "Jump to invalid address: %08x", targetAddr); + } else { + js.compiling = false; + } + // TODO: Mark this block dirty or something? May be indication it will be changed by imports. + return; + } + + switch (op >> 26) { + case 2: //j + CompileDelaySlot(); + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + break; + + case 3: //jal + ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 8); + CompileDelaySlot(); + FlushAll(); + ir.Write(IROp::ExitToConst, ir.AddConstant(targetAddr)); + break; + + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } + js.compiling = false; +} + +void IRFrontend::Comp_JumpReg(MIPSOpcode op) { + if (js.inDelaySlot) { + ERROR_LOG_REPORT(JIT, "Branch in JumpReg delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); + return; + } + MIPSGPReg rs = _RS; + MIPSGPReg rd = _RD; + bool andLink = (op & 0x3f) == 9 && rd != MIPS_REG_ZERO; + + MIPSOpcode delaySlotOp = GetOffsetInstruction(1); + bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rs); + if (andLink && rs == rd) + delaySlotIsNice = false; + + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + int destReg; + if (IsSyscall(delaySlotOp)) { + ir.Write(IROp::SetPC, 0, rs); + if (andLink) + ir.WriteSetConstant(rd, GetCompilerPC() + 8); + CompileDelaySlot(); + // Syscall (the delay slot) does FlushAll. + return; // Syscall (delay slot) wrote exit code. + } else if (delaySlotIsNice) { + if (andLink) + ir.WriteSetConstant(rd, GetCompilerPC() + 8); + CompileDelaySlot(); + destReg = rs; // Safe because FlushAll doesn't change any regs + FlushAll(); + } else { + // Bad delay slot. + ir.Write(IROp::Mov, IRTEMP_LHS, rs); + destReg = IRTEMP_LHS; + if (andLink) + ir.WriteSetConstant(rd, GetCompilerPC() + 8); + CompileDelaySlot(); + FlushAll(); + } + + switch (op & 0x3f) + { + case 8: //jr + break; + case 9: //jalr + break; + default: + _dbg_assert_msg_(CPU,0,"Trying to compile instruction that can't be compiled"); + break; + } + + ir.Write(IROp::ExitToReg, 0, destReg, 0); + js.compiling = false; +} + +void IRFrontend::Comp_Syscall(MIPSOpcode op) { + RestoreRoundingMode(); + + // Note: If we're in a delay slot, this is off by one compared to the interpreter. + int dcAmount = js.downcountAmount; + ir.Write(IROp::Downcount, 0, dcAmount & 0xFF, dcAmount >> 8); + js.downcountAmount = 0; + + FlushAll(); + + ir.Write(IROp::Syscall, 0, ir.AddConstant(op.encoding)); + + // TODO: This never happens because of Syscall exiting. + ApplyRoundingMode(); + js.compiling = false; +} + +void IRFrontend::Comp_Break(MIPSOpcode op) { + ir.Write(IROp::Break); + js.compiling = false; +} + +} // namespace Mipscomp diff --git a/Core/MIPS/IR/IRCompFPU.cpp b/Core/MIPS/IR/IRCompFPU.cpp new file mode 100644 index 0000000000..f046704ac7 --- /dev/null +++ b/Core/MIPS/IR/IRCompFPU.cpp @@ -0,0 +1,240 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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/Config.h" +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSTables.h" + +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Common/CPUDetect.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + + +// FPCR interesting bits: +// 24: FZ (flush-to-zero) +// 23:22: RMode (0 = nearest, 1 = +inf, 2 = -inf, 3 = zero) +// not much else is interesting for us, but should be preserved. +// To access: MRS Xt, FPCR ; MSR FPCR, Xt + + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +namespace MIPSComp { + +void IRFrontend::Comp_FPU3op(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int ft = _FT; + int fs = _FS; + int fd = _FD; + + switch (op & 0x3f) { + case 0: ir.Write(IROp::FAdd, fd, fs, ft); break; //F(fd) = F(fs) + F(ft); //add + case 1: ir.Write(IROp::FSub, fd, fs, ft); break; //F(fd) = F(fs) - F(ft); //sub + case 2: ir.Write(IROp::FMul, fd, fs, ft); break; //F(fd) = F(fs) * F(ft); //mul + case 3: ir.Write(IROp::FDiv, fd, fs, ft); break; //F(fd) = F(fs) / F(ft); //div + default: + DISABLE; + return; + } +} + +void IRFrontend::Comp_FPULS(MIPSOpcode op) { + CONDITIONAL_DISABLE; + s32 offset = _IMM16; + int ft = _FT; + MIPSGPReg rs = _RS; + + switch (op >> 26) { + case 49: //FI(ft) = Memory::Read_U32(addr); break; //lwc1 + ir.Write(IROp::LoadFloat, ft, rs, ir.AddConstant(offset)); + break; + + case 57: //Memory::Write_U32(FI(ft), addr); break; //swc1 + ir.Write(IROp::StoreFloat, ft, rs, ir.AddConstant(offset)); + break; + + default: + _dbg_assert_msg_(CPU, 0, "Trying to interpret FPULS instruction that can't be interpreted"); + break; + } +} + +void IRFrontend::Comp_FPUComp(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int opc = op & 0xF; + if (opc >= 8) opc -= 8; // alias + if (opc == 0) { // f, sf (signalling false) + ir.Write(IROp::ZeroFpCond); + return; + } + + int fs = _FS; + int ft = _FT; + IRFpCompareMode mode; + switch (opc) { + case 1: // un, ngle (unordered) + mode = IRFpCompareMode::NotEqualUnordered; + break; + case 2: // eq, seq (equal, ordered) + mode = IRFpCompareMode::EqualOrdered; + break; + case 3: // ueq, ngl (equal, unordered) + mode = IRFpCompareMode::EqualUnordered; + return; + case 4: // olt, lt (less than, ordered) + mode = IRFpCompareMode::LessOrdered; + break; + case 5: // ult, nge (less than, unordered) + mode = IRFpCompareMode::LessUnordered; + break; + case 6: // ole, le (less equal, ordered) + mode = IRFpCompareMode::LessEqualOrdered; + break; + case 7: // ule, ngt (less equal, unordered) + mode = IRFpCompareMode::LessEqualUnordered; + break; + default: + DISABLE; + return; + } + ir.Write(IROp::FCmp, (int)mode, fs, ft); +} + +void IRFrontend::Comp_FPU2op(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int fs = _FS; + int fd = _FD; + + switch (op & 0x3f) { + case 4: //F(fd) = sqrtf(F(fs)); break; //sqrt + ir.Write(IROp::FSqrt, fd, fs); + break; + case 5: //F(fd) = fabsf(F(fs)); break; //abs + ir.Write(IROp::FAbs, fd, fs); + break; + case 6: //F(fd) = F(fs); break; //mov + ir.Write(IROp::FMov, fd, fs); + break; + case 7: //F(fd) = -F(fs); break; //neg + ir.Write(IROp::FNeg, fd, fs); + break; + + case 12: //FsI(fd) = (int)floorf(F(fs)+0.5f); break; //round.w.s + { + ir.Write(IROp::FRound, fd, fs); + break; + } + + case 13: //FsI(fd) = Rto0(F(fs))); break; //trunc.w.s + { + ir.Write(IROp::FTrunc, fd, fs); + break; + } + + case 14://FsI(fd) = (int)ceilf (F(fs)); break; //ceil.w.s + { + ir.Write(IROp::FCeil, fd, fs); + break; + } + case 15: //FsI(fd) = (int)floorf(F(fs)); break; //floor.w.s + { + ir.Write(IROp::FFloor, fd, fs); + break; + } + + case 32: //F(fd) = (float)FsI(fs); break; //cvt.s.w + ir.Write(IROp::FCvtSW, fd, fs); + break; + + case 36: //FsI(fd) = (int) F(fs); break; //cvt.w.s + ir.Write(IROp::FCvtWS, fd, fs); + break; + + default: + DISABLE; + } +} + +void IRFrontend::Comp_mxc1(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int fs = _FS; + MIPSGPReg rt = _RT; + + switch ((op >> 21) & 0x1f) { + case 0: // R(rt) = FI(fs); break; //mfc1 + if (rt == MIPS_REG_ZERO) { + return; + } + ir.Write(IROp::FMovToGPR, rt, fs); + return; + + case 2: //cfc1 + if (rt == MIPS_REG_ZERO) { + return; + } + if (fs == 31) { + DISABLE; // TODO: Add a new op + } else if (fs == 0) { + ir.Write(IROp::SetConst, rt, ir.AddConstant(MIPSState::FCR0_VALUE)); + } else { + // Unsupported regs are always 0. + ir.Write(IROp::SetConst, rt, ir.AddConstant(0)); + } + return; + + case 4: //FI(fs) = R(rt); break; //mtc1 + ir.Write(IROp::FMovFromGPR, fs, rt); + return; + + case 6: //ctc1 + if (fs == 31) { + // Set rounding mode + DISABLE; + } else { + Comp_Generic(op); + } + return; + default: + DISABLE; + break; + } +} + +} // namespace MIPSComp diff --git a/Core/MIPS/IR/IRCompLoadStore.cpp b/Core/MIPS/IR/IRCompLoadStore.cpp new file mode 100644 index 0000000000..c57f1ec323 --- /dev/null +++ b/Core/MIPS/IR/IRCompLoadStore.cpp @@ -0,0 +1,159 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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/. + + +// Optimization ideas: +// +// It's common to see sequences of stores writing or reading to a contiguous set of +// addresses in function prologues/epilogues: +// sw s5, 104(sp) +// sw s4, 100(sp) +// sw s3, 96(sp) +// sw s2, 92(sp) +// sw s1, 88(sp) +// sw s0, 84(sp) +// sw ra, 108(sp) +// mov s4, a0 +// mov s3, a1 +// ... +// Such sequences could easily be detected and turned into nice contiguous +// sequences of ARM stores instead of the current 3 instructions per sw/lw. +// +// Also, if we kept track of the likely register content of a cached register, +// (pointer or data), we could avoid many BIC instructions. + + +#include "Core/MemMap.h" +#include "Core/Config.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSAnalyst.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/IR/IRRegCache.h" + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +namespace MIPSComp { + void IRFrontend::Comp_ITypeMemLR(MIPSOpcode op, bool load) { + DISABLE; + } + + void IRFrontend::Comp_ITypeMem(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int offset = (signed short)(op & 0xFFFF); + MIPSGPReg rt = _RT; + MIPSGPReg rs = _RS; + int o = op >> 26; + if (((op >> 29) & 1) == 0 && rt == MIPS_REG_ZERO) { + // Don't load anything into $zr + return; + } + + int addrReg = IRTEMP_0; + switch (o) { + // Load + case 35: + ir.Write(IROp::Load32, rt, rs, ir.AddConstant(offset)); + break; + case 37: + ir.Write(IROp::Load16, rt, rs, ir.AddConstant(offset)); + break; + case 33: + ir.Write(IROp::Load16Ext, rt, rs, ir.AddConstant(offset)); + break; + case 36: + ir.Write(IROp::Load8, rt, rs, ir.AddConstant(offset)); + break; + case 32: + ir.Write(IROp::Load8Ext, rt, rs, ir.AddConstant(offset)); + break; + // Store + case 43: + ir.Write(IROp::Store32, rt, rs, ir.AddConstant(offset)); + break; + case 41: + ir.Write(IROp::Store16, rt, rs, ir.AddConstant(offset)); + break; + case 40: + ir.Write(IROp::Store8, rt, rs, ir.AddConstant(offset)); + break; + + case 34: //lwl + case 38: //lwr + case 42: //swl + case 46: //swr + DISABLE; + break; + default: + Comp_Generic(op); + return; + } + } + + void IRFrontend::Comp_Cache(MIPSOpcode op) { +// int imm = (s16)(op & 0xFFFF); +// int rs = _RS; +// int addr = R(rs) + imm; + int func = (op >> 16) & 0x1F; + + // It appears that a cache line is 0x40 (64) bytes, loops in games + // issue the cache instruction at that interval. + + // These codes might be PSP-specific, they don't match regular MIPS cache codes very well + switch (func) { + // Icache + case 8: + // Invalidate the instruction cache at this address + DISABLE; + break; + // Dcache + case 24: + // "Create Dirty Exclusive" - for avoiding a cacheline fill before writing to it. + // Will cause garbage on the real machine so we just ignore it, the app will overwrite the cacheline. + break; + case 25: // Hit Invalidate - zaps the line if present in cache. Should not writeback???? scary. + // No need to do anything. + break; + case 27: // D-cube. Hit Writeback Invalidate. Tony Hawk Underground 2 + break; + case 30: // GTA LCS, a lot. Fill (prefetch). Tony Hawk Underground 2 + break; + + default: + DISABLE; + break; + } + } +} diff --git a/Core/MIPS/IR/IRCompVFPU.cpp b/Core/MIPS/IR/IRCompVFPU.cpp new file mode 100644 index 0000000000..1b484fbb60 --- /dev/null +++ b/Core/MIPS/IR/IRCompVFPU.cpp @@ -0,0 +1,1455 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 + +#include "math/math_util.h" + +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSTables.h" +#include "Core/MIPS/MIPSAnalyst.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Common/CPUDetect.h" +#include "Core/Config.h" +#include "Core/Reporting.h" + +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/IR/IRRegCache.h" + +// All functions should have CONDITIONAL_DISABLE, so we can narrow things down to a file quickly. +// Currently known non working ones should have DISABLE. + +// #define CONDITIONAL_DISABLE { Comp_Generic(op); return; } +#define CONDITIONAL_DISABLE ; +#define DISABLE { Comp_Generic(op); return; } + +#define _RS MIPS_GET_RS(op) +#define _RT MIPS_GET_RT(op) +#define _RD MIPS_GET_RD(op) +#define _FS MIPS_GET_FS(op) +#define _FT MIPS_GET_FT(op) +#define _FD MIPS_GET_FD(op) +#define _SA MIPS_GET_SA(op) +#define _POS ((op>> 6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) + +const int vfpuBase = 32; // skip the FP registers + +namespace MIPSComp { + static void ApplyVoffset(u8 regs[4], int count) { + for (int i = 0; i < count; i++) { + regs[i] = vfpuBase + voffset[regs[i]]; + } + } + + static bool IsConsecutive4(const u8 regs[4]) { + return regs[1] == regs[0] + 1 && + regs[2] == regs[1] + 1 && + regs[3] == regs[2] + 1; + } + + // Vector regs can overlap in all sorts of swizzled ways. + // This does allow a single overlap in sregs[i]. + static bool IsOverlapSafeAllowS(int dreg, int di, int sn, u8 sregs[], int tn = 0, u8 tregs[] = NULL) { + for (int i = 0; i < sn; ++i) { + if (sregs[i] == dreg && i != di) + return false; + } + for (int i = 0; i < tn; ++i) { + if (tregs[i] == dreg) + return false; + } + + // Hurray, no overlap, we can write directly. + return true; + } + + static bool IsOverlapSafe(int dreg, int sn, u8 sregs[], int tn = 0, u8 tregs[] = NULL) { + return IsOverlapSafeAllowS(dreg, -1, sn, sregs, tn, tregs); + } + + void IRFrontend::Comp_VPFX(MIPSOpcode op) { + CONDITIONAL_DISABLE; + int data = op & 0xFFFFF; + int regnum = (op >> 24) & 3; + switch (regnum) { + case 0: // S + js.prefixS = data; + js.prefixSFlag = JitState::PREFIX_KNOWN_DIRTY; + break; + case 1: // T + js.prefixT = data; + js.prefixTFlag = JitState::PREFIX_KNOWN_DIRTY; + break; + case 2: // D + js.prefixD = data; + js.prefixDFlag = JitState::PREFIX_KNOWN_DIRTY; + break; + default: + ERROR_LOG(CPU, "VPFX - bad regnum %i : data=%08x", regnum, data); + break; + } + } + + static void InitRegs(u8 *vregs, int reg) { + vregs[0] = reg; + vregs[1] = reg + 1; + vregs[2] = reg + 2; + vregs[3] = reg + 3; + } + + void IRFrontend::ApplyPrefixST(u8 *vregs, u32 prefix, VectorSize sz, int tempReg) { + if (prefix == 0xE4) + return; + + int n = GetNumVectorElements(sz); + u8 origV[4]; + static const float constantArray[8] = { 0.f, 1.f, 2.f, 0.5f, 3.f, 1.f / 3.f, 0.25f, 1.f / 6.f }; + + for (int i = 0; i < n; i++) + origV[i] = vregs[i]; + + // Some common vector prefixes + if (sz == V_Quad && IsConsecutive4(vregs)) { + if (prefix == 0xF00E4) { + InitRegs(vregs, tempReg); + ir.Write(IROp::Vec4Neg, vregs[0], origV[0]); + return; + } + if (prefix == 0x00FE4) { + InitRegs(vregs, tempReg); + ir.Write(IROp::Vec4Abs, vregs[0], origV[0]); + return; + } + // Pure shuffle + if (prefix == (prefix & 0xFF)) { + InitRegs(vregs, tempReg); + ir.Write(IROp::Vec4Shuffle, vregs[0], origV[0], prefix); + return; + } + } + + // Alright, fall back to the generic approach. + for (int i = 0; i < n; i++) { + int regnum = (prefix >> (i * 2)) & 3; + int abs = (prefix >> (8 + i)) & 1; + int negate = (prefix >> (16 + i)) & 1; + int constants = (prefix >> (12 + i)) & 1; + + // Unchanged, hurray. + if (!constants && regnum == i && !abs && !negate) + continue; + + // This puts the value into a temp reg, so we won't write the modified value back. + vregs[i] = tempReg + i; + if (!constants) { + // Prefix may say "z, z, z, z" but if this is a pair, we force to x. + // TODO: But some ops seem to use const 0 instead? + if (regnum >= n) { + WARN_LOG(CPU, "JIT: Invalid VFPU swizzle: %08x : %d / %d at PC = %08x (%s)", prefix, regnum, n, GetCompilerPC(), MIPSDisasmAt(GetCompilerPC())); + regnum = 0; + } + + if (abs) { + ir.Write(IROp::FAbs, vregs[i], origV[regnum]); + if (negate) + ir.Write(IROp::FNeg, vregs[i], vregs[i]); + } else { + if (negate) + ir.Write(IROp::FNeg, vregs[i], origV[regnum]); + else + ir.Write(IROp::FMov, vregs[i], origV[regnum]); + } + } else { + if (negate) { + ir.Write(IROp::SetConstF, vregs[i], ir.AddConstantFloat(-constantArray[regnum + (abs << 2)])); + } else { + ir.Write(IROp::SetConstF, vregs[i], ir.AddConstantFloat(constantArray[regnum + (abs << 2)])); + } + } + } + } + + void IRFrontend::GetVectorRegs(u8 regs[4], VectorSize N, int vectorReg) { + ::GetVectorRegs(regs, N, vectorReg); + ApplyVoffset(regs, N); + } + + void IRFrontend::GetMatrixRegs(u8 regs[16], MatrixSize N, int matrixReg) { + ::GetMatrixRegs(regs, N, matrixReg); + for (int i = 0; i < GetMatrixSide(N); i++) { + ApplyVoffset(regs + 4 * i, GetVectorSize(N)); + } + } + + void IRFrontend::GetVectorRegsPrefixS(u8 *regs, VectorSize sz, int vectorReg) { + _assert_(js.prefixSFlag & JitState::PREFIX_KNOWN); + GetVectorRegs(regs, sz, vectorReg); + ApplyPrefixST(regs, js.prefixS, sz, IRVTEMP_PFX_S); + } + void IRFrontend::GetVectorRegsPrefixT(u8 *regs, VectorSize sz, int vectorReg) { + _assert_(js.prefixTFlag & JitState::PREFIX_KNOWN); + GetVectorRegs(regs, sz, vectorReg); + ApplyPrefixST(regs, js.prefixT, sz, IRVTEMP_PFX_T); + } + + void IRFrontend::GetVectorRegsPrefixD(u8 *regs, VectorSize sz, int vectorReg) { + _assert_(js.prefixDFlag & JitState::PREFIX_KNOWN); + + GetVectorRegs(regs, sz, vectorReg); + int n = GetNumVectorElements(sz); + if (js.prefixD == 0) + return; + + for (int i = 0; i < n; i++) { + // Hopefully this is rare, we'll just write it into a dumping ground reg. + if (js.VfpuWriteMask(i)) + regs[i] = IRVTEMP_PFX_D + i; + } + } + + inline int GetDSat(int prefix, int i) { + return (prefix >> (i * 2)) & 3; + } + + // "D" prefix is really a post process. No need to allocate a temporary register (except + // dummies to simulate writemask, which is done in GetVectorRegsPrefixD + void IRFrontend::ApplyPrefixD(const u8 *vregs, VectorSize sz) { + _assert_(js.prefixDFlag & JitState::PREFIX_KNOWN); + if (!js.prefixD) + return; + + int n = GetNumVectorElements(sz); + for (int i = 0; i < n; i++) { + if (js.VfpuWriteMask(i)) + continue; + int sat = GetDSat(js.prefixD, i); + if (sat == 1) { + // clamped = x < 0 ? (x > 1 ? 1 : x) : x [0, 1] + ir.Write(IROp::FSat0_1, vregs[i], vregs[i]); + } else if (sat == 3) { + ir.Write(IROp::FSatMinus1_1, vregs[i], vregs[i]); + } + } + } + + void IRFrontend::Comp_SV(MIPSOpcode op) { + CONDITIONAL_DISABLE; + s32 offset = (signed short)(op & 0xFFFC); + int vt = ((op >> 16) & 0x1f) | ((op & 3) << 5); + MIPSGPReg rs = _RS; + switch (op >> 26) { + case 50: //lv.s + ir.Write(IROp::LoadFloat, vfpuBase + voffset[vt], rs, ir.AddConstant(offset)); + break; + + case 58: //sv.s + ir.Write(IROp::StoreFloat, vfpuBase + voffset[vt], rs, ir.AddConstant(offset)); + break; + + default: + DISABLE; + } + } + + void IRFrontend::Comp_SVQ(MIPSOpcode op) { + CONDITIONAL_DISABLE; + int imm = (signed short)(op & 0xFFFC); + int vt = (((op >> 16) & 0x1f)) | ((op & 1) << 5); + MIPSGPReg rs = _RS; + + u8 vregs[4]; + GetVectorRegs(vregs, V_Quad, vt); + + switch (op >> 26) { + case 54: //lv.q + if (IsConsecutive4(vregs)) { + ir.Write(IROp::LoadVec4, vregs[0], rs, ir.AddConstant(imm)); + } else { + // Let's not even bother with "vertical" loads for now. + ir.Write(IROp::LoadFloat, vregs[0], rs, ir.AddConstant(imm)); + ir.Write(IROp::LoadFloat, vregs[1], rs, ir.AddConstant(imm + 4)); + ir.Write(IROp::LoadFloat, vregs[2], rs, ir.AddConstant(imm + 8)); + ir.Write(IROp::LoadFloat, vregs[3], rs, ir.AddConstant(imm + 12)); + } + break; + + case 62: //sv.q + if (IsConsecutive4(vregs)) { + ir.Write(IROp::StoreVec4, vregs[0], rs, ir.AddConstant(imm)); + } else { + // Let's not even bother with "vertical" stores for now. + ir.Write(IROp::StoreFloat, vregs[0], rs, ir.AddConstant(imm)); + ir.Write(IROp::StoreFloat, vregs[1], rs, ir.AddConstant(imm + 4)); + ir.Write(IROp::StoreFloat, vregs[2], rs, ir.AddConstant(imm + 8)); + ir.Write(IROp::StoreFloat, vregs[3], rs, ir.AddConstant(imm + 12)); + } + break; + + default: + DISABLE; + break; + } + } + + void IRFrontend::Comp_VVectorInit(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + VectorSize sz = GetVecSize(op); + int type = (op >> 16) & 0xF; + int vd = _VD; + int n = GetNumVectorElements(sz); + u8 dregs[4]; + GetVectorRegsPrefixD(dregs, sz, vd); + + if (sz == V_Quad && IsConsecutive4(dregs)) { + ir.Write(IROp::Vec4Init, dregs[0], (int)(type == 6 ? Vec4Init::AllZERO : Vec4Init::AllONE)); + } else { + for (int i = 0; i < n; i++) { + ir.Write(IROp::SetConstF, dregs[i], ir.AddConstantFloat(type == 6 ? 0.0f : 1.0f)); + } + } + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_VIdt(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + int vd = _VD; + VectorSize sz = GetVecSize(op); + u8 dregs[4]; + GetVectorRegsPrefixD(dregs, sz, vd); + + if (sz == 4 && IsConsecutive4(dregs)) { + int row = vd & 3; + Vec4Init init = Vec4Init((int)Vec4Init::Set_1000 + row); + ir.Write(IROp::Vec4Init, dregs[0], (int)init); + } else { + switch (sz) { + case V_Pair: + ir.Write(IROp::SetConstF, dregs[0], ir.AddConstantFloat((vd & 1) == 0 ? 1.0f : 0.0f)); + ir.Write(IROp::SetConstF, dregs[1], ir.AddConstantFloat((vd & 1) == 1 ? 1.0f : 0.0f)); + break; + case V_Quad: + ir.Write(IROp::SetConstF, dregs[0], ir.AddConstantFloat((vd & 3) == 0 ? 1.0f : 0.0f)); + ir.Write(IROp::SetConstF, dregs[1], ir.AddConstantFloat((vd & 3) == 1 ? 1.0f : 0.0f)); + ir.Write(IROp::SetConstF, dregs[2], ir.AddConstantFloat((vd & 3) == 2 ? 1.0f : 0.0f)); + ir.Write(IROp::SetConstF, dregs[3], ir.AddConstantFloat((vd & 3) == 3 ? 1.0f : 0.0f)); + break; + default: + DISABLE; + } + } + + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_VMatrixInit(MIPSOpcode op) { + CONDITIONAL_DISABLE; + MatrixSize sz = GetMtxSize(op); + if (sz != M_4x4) { + DISABLE; + } + + // Not really about trying here, it will work if enabled. + VectorSize vsz = GetVectorSize(sz); + u8 vecs[4]; + int vd = _VD; + if (IsMatrixTransposed(vd)) { + // All outputs are transpositionally symmetric, so should be fine. + vd = TransposeMatrixReg(vd); + } + GetMatrixColumns(vd, M_4x4, vecs); + for (int i = 0; i < 4; i++) { + u8 vec[4]; + GetVectorRegs(vec, vsz, vecs[i]); + // As they are columns, they will be nicely consecutive. + Vec4Init init; + switch ((op >> 16) & 0xF) { + case 3: + init = Vec4Init((int)Vec4Init::Set_1000 + i); + break; + case 6: + init = Vec4Init::AllZERO; + break; + case 7: + init = Vec4Init::AllONE; + break; + default: + return; + } + ir.Write(IROp::Vec4Init, vec[0], (int)init); + } + return; + } + + void IRFrontend::Comp_VHdp(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + int vd = _VD; + int vs = _VS; + int vt = _VT; + VectorSize sz = GetVecSize(op); + + // TODO: Force read one of them into regs? probably not. + u8 sregs[4], tregs[4], dregs[1]; + GetVectorRegsPrefixS(sregs, sz, vs); + GetVectorRegsPrefixT(tregs, sz, vt); + GetVectorRegsPrefixD(dregs, V_Single, vd); + + ir.Write(IROp::FMul, IRVTEMP_0, sregs[0], tregs[0]); + + int n = GetNumVectorElements(sz); + for (int i = 1; i < n; i++) { + // sum += s[i]*t[i]; + if (i == n - 1) { + ir.Write(IROp::FAdd, IRVTEMP_0, IRVTEMP_0, tregs[i]); + } else { + ir.Write(IROp::FMul, IRVTEMP_0 + 1, sregs[i], tregs[i]); + ir.Write(IROp::FAdd, IRVTEMP_0, IRVTEMP_0, IRVTEMP_0 + 1); + } + } + + ir.Write(IROp::FMov, dregs[0], IRVTEMP_0); + ApplyPrefixD(dregs, V_Single); + } + + static const float MEMORY_ALIGNED16(vavg_table[4]) = { 1.0f, 1.0f / 2.0f, 1.0f / 3.0f, 1.0f / 4.0f }; + + void IRFrontend::Comp_Vhoriz(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + if (js.HasUnknownPrefix()) + DISABLE; + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], dregs[1]; + GetVectorRegsPrefixS(sregs, sz, _VS); + GetVectorRegsPrefixD(dregs, V_Single, _VD); + + // We have to start at +0.000 in case any values are -0.000. + ir.Write(IROp::SetConstF, IRVTEMP_0, ir.AddConstantFloat(0.0f)); + for (int i = 0; i < n; ++i) { + ir.Write(IROp::FAdd, IRVTEMP_0, IRVTEMP_0, sregs[i]); + } + + switch ((op >> 16) & 31) { + case 6: // vfad + ir.Write(IROp::FMov, dregs[0], IRVTEMP_0); + break; + case 7: // vavg + ir.Write(IROp::SetConstF, IRVTEMP_0 + 1, ir.AddConstantFloat(vavg_table[n - 1])); + ir.Write(IROp::FMul, dregs[0], IRVTEMP_0, IRVTEMP_0 + 1); + break; + } + + ApplyPrefixD(dregs, V_Single); + } + + void IRFrontend::Comp_VDot(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + int vd = _VD; + int vs = _VS; + int vt = _VT; + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + // TODO: Force read one of them into regs? probably not. + u8 sregs[4], tregs[4], dregs[1]; + GetVectorRegsPrefixS(sregs, sz, vs); + GetVectorRegsPrefixT(tregs, sz, vt); + GetVectorRegsPrefixD(dregs, V_Single, vd); + + if (sz == V_Quad && IsConsecutive4(sregs) && IsConsecutive4(tregs) && IsOverlapSafe(dregs[0], n, sregs, n, tregs)) { + ir.Write(IROp::Vec4Dot, dregs[0], sregs[0], tregs[0]); + ApplyPrefixD(dregs, V_Single); + return; + } + + int temp0 = IRVTEMP_0; + int temp1 = IRVTEMP_0 + 1; + ir.Write(IROp::FMul, temp0, sregs[0], tregs[0]); + for (int i = 1; i < n; i++) { + ir.Write(IROp::FMul, temp1, sregs[i], tregs[i]); + ir.Write(IROp::FAdd, i == (n - 1) ? dregs[0] : temp0, temp0, temp1); + } + ApplyPrefixD(dregs, V_Single); + } + + void IRFrontend::Comp_VecDo3(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) + DISABLE; + + // Check that we can support the ops, and prepare temporary values for ops that need it. + bool allowSIMD = true; + switch (op >> 26) { + case 24: //VFPU0 + switch ((op >> 23) & 7) { + case 0: // d[i] = s[i] + t[i]; break; //vadd + case 1: // d[i] = s[i] - t[i]; break; //vsub + case 7: // d[i] = s[i] / t[i]; break; //vdiv + break; + default: + DISABLE; + } + break; + case 25: //VFPU1 + switch ((op >> 23) & 7) { + case 0: // d[i] = s[i] * t[i]; break; //vmul + break; + default: + DISABLE; + } + break; + case 27: //VFPU3 + switch ((op >> 23) & 7) { + case 2: // vmin + case 3: // vmax + allowSIMD = false; + break; + case 6: // vsge + case 7: // vslt + allowSIMD = false; + break; + default: + DISABLE; + } + break; + default: + DISABLE; + break; + } + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], tregs[4], dregs[4]; + GetVectorRegsPrefixS(sregs, sz, _VS); + GetVectorRegsPrefixT(tregs, sz, _VT); + GetVectorRegsPrefixD(dregs, sz, _VD); + + u8 tempregs[4]; + bool usingTemps = false; + for (int i = 0; i < n; i++) { + if (!IsOverlapSafe(dregs[i], n, sregs, n, tregs)) { + tempregs[i] = IRVTEMP_0 + i; + usingTemps = true; + } else { + tempregs[i] = dregs[i]; + } + } + + if (allowSIMD && sz == V_Quad && !usingTemps && IsConsecutive4(dregs) && IsConsecutive4(sregs) && IsConsecutive4(tregs)) { + IROp opFunc = IROp::Nop; + switch (op >> 26) { + case 24: //VFPU0 + switch ((op >> 23) & 7) { + case 0: // d[i] = s[i] + t[i]; break; //vadd + opFunc = IROp::Vec4Add; + break; + case 1: // d[i] = s[i] - t[i]; break; //vsub + opFunc = IROp::Vec4Sub; + break; + case 7: // d[i] = s[i] / t[i]; break; //vdiv + opFunc = IROp::Vec4Div; + break; + } + break; + case 25: //VFPU1 + switch ((op >> 23) & 7) + { + case 0: // d[i] = s[i] * t[i]; break; //vmul + opFunc = IROp::Vec4Mul; + break; + } + break; + case 27: //VFPU3 + switch ((op >> 23) & 7) + { + case 2: // vmin + case 3: // vmax + case 6: // vsge + case 7: // vslt + DISABLE; + break; + } + break; + } + + if (opFunc != IROp::Nop) { + ir.Write(opFunc, dregs[0], sregs[0], tregs[0]); + } else { + DISABLE; + } + ApplyPrefixD(dregs, sz); + return; + } + + for (int i = 0; i < n; ++i) { + switch (op >> 26) { + case 24: //VFPU0 + switch ((op >> 23) & 7) { + case 0: // d[i] = s[i] + t[i]; break; //vadd + ir.Write(IROp::FAdd, tempregs[i], sregs[i], tregs[i]); + break; + case 1: // d[i] = s[i] - t[i]; break; //vsub + ir.Write(IROp::FSub, tempregs[i], sregs[i], tregs[i]); + break; + case 7: // d[i] = s[i] / t[i]; break; //vdiv + ir.Write(IROp::FDiv, tempregs[i], sregs[i], tregs[i]); + break; + } + break; + case 25: //VFPU1 + switch ((op >> 23) & 7) { + case 0: // d[i] = s[i] * t[i]; break; //vmul + ir.Write(IROp::FMul, tempregs[i], sregs[i], tregs[i]); + break; + } + break; + case 27: //VFPU3 + switch ((op >> 23) & 7) { + case 2: // vmin + ir.Write(IROp::FMin, tempregs[i], sregs[i], tregs[i]); + break; + case 3: // vmax + ir.Write(IROp::FMax, tempregs[i], sregs[i], tregs[i]); + break; + case 6: // vsge + case 7: // vslt + DISABLE; + break; + } + break; + } + } + + for (int i = 0; i < n; i++) { + if (dregs[i] != tempregs[i]) { + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + } + + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_VV2Op(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) + DISABLE; + + int vs = _VS; + int vd = _VD; + + // Pre-processing: Eliminate silly no-op VMOVs, common in Wipeout Pure + if (((op >> 16) & 0x1f) == 0 && vs == vd && js.HasNoPrefix()) { + return; + } + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], dregs[4]; + GetVectorRegsPrefixS(sregs, sz, vs); + GetVectorRegsPrefixD(dregs, sz, vd); + + bool usingTemps = false; + u8 tempregs[4]; + for (int i = 0; i < n; ++i) { + if (!IsOverlapSafe(dregs[i], n, sregs)) { + usingTemps = true; + tempregs[i] = IRVTEMP_0 + i; + } else { + tempregs[i] = dregs[i]; + } + } + + bool canSIMD = false; + // Some can be SIMD'd. + switch ((op >> 16) & 0x1f) { + case 0: // vmov + canSIMD = true; + break; + } + + if (canSIMD && !usingTemps && IsConsecutive4(sregs) && IsConsecutive4(dregs)) { + switch ((op >> 16) & 0x1f) { + case 0: // vmov + ir.Write(IROp::Vec4Mov, dregs[0], sregs[0]); + break; + } + ApplyPrefixD(dregs, sz); + return; + } + + for (int i = 0; i < n; ++i) { + switch ((op >> 16) & 0x1f) { + case 0: // d[i] = s[i]; break; //vmov + // Probably for swizzle. + ir.Write(IROp::FMov, tempregs[i], sregs[i]); + break; + case 1: // d[i] = fabsf(s[i]); break; //vabs + ir.Write(IROp::FAbs, tempregs[i], sregs[i]); + break; + case 2: // d[i] = -s[i]; break; //vneg + ir.Write(IROp::FNeg, tempregs[i], sregs[i]); + break; + case 4: // if (s[i] < 0) d[i] = 0; else {if(s[i] > 1.0f) d[i] = 1.0f; else d[i] = s[i];} break; // vsat0 + ir.Write(IROp::FSat0_1, tempregs[i], sregs[i]); + break; + case 5: // if (s[i] < -1.0f) d[i] = -1.0f; else {if(s[i] > 1.0f) d[i] = 1.0f; else d[i] = s[i];} break; // vsat1 + ir.Write(IROp::FSatMinus1_1, tempregs[i], sregs[i]); + break; + case 16: // d[i] = 1.0f / s[i]; break; //vrcp + ir.Write(IROp::FRecip, tempregs[i], sregs[i]); + break; + case 17: // d[i] = 1.0f / sqrtf(s[i]); break; //vrsq + ir.Write(IROp::FRSqrt, tempregs[i], sregs[i]); + break; + case 18: // d[i] = sinf((float)M_PI_2 * s[i]); break; //vsin + ir.Write(IROp::FSin, tempregs[i], sregs[i]); + break; + case 19: // d[i] = cosf((float)M_PI_2 * s[i]); break; //vcos + ir.Write(IROp::FCos, tempregs[i], sregs[i]); + break; + case 20: // d[i] = powf(2.0f, s[i]); break; //vexp2 + DISABLE; + break; + case 21: // d[i] = logf(s[i])/log(2.0f); break; //vlog2 + DISABLE; + break; + case 22: // d[i] = sqrtf(s[i]); break; //vsqrt + ir.Write(IROp::FSqrt, tempregs[i], sregs[i]); + break; + case 23: // d[i] = asinf(s[i]) / M_PI_2; break; //vasin + ir.Write(IROp::FAsin, tempregs[i], sregs[i]); + break; + case 24: // d[i] = -1.0f / s[i]; break; // vnrcp + ir.Write(IROp::FRecip, tempregs[i], sregs[i]); + ir.Write(IROp::FNeg, tempregs[i], tempregs[i]); + break; + case 26: // d[i] = -sinf((float)M_PI_2 * s[i]); break; // vnsin + ir.Write(IROp::FSin, tempregs[i], sregs[i]); + ir.Write(IROp::FNeg, tempregs[i], tempregs[i]); + break; + case 28: // d[i] = 1.0f / expf(s[i] * (float)M_LOG2E); break; // vrexp2 + default: + DISABLE; + break; + } + } + for (int i = 0; i < n; i++) { + if (dregs[i] != tempregs[i]) { + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + } + + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_Vi2f(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + int imm = (op >> 16) & 0x1f; + const float mult = 1.0f / (float)(1UL << imm); + + u8 sregs[4], dregs[4]; + GetVectorRegsPrefixS(sregs, sz, _VS); + GetVectorRegsPrefixD(dregs, sz, _VD); + + u8 tempregs[4]; + for (int i = 0; i < n; ++i) { + if (!IsOverlapSafe(dregs[i], n, sregs)) { + tempregs[i] = IRVTEMP_PFX_T + i; // Need IRVTEMP_0 for the scaling factor + } else { + tempregs[i] = dregs[i]; + } + } + if (mult != 1.0f) + ir.Write(IROp::SetConstF, IRVTEMP_0, ir.AddConstantFloat(mult)); + // TODO: Use the SCVTF with builtin scaling where possible. + for (int i = 0; i < n; i++) { + ir.Write(IROp::FCvtSW, tempregs[i], sregs[i]); + } + if (mult != 1.0f) { + for (int i = 0; i < n; i++) { + ir.Write(IROp::FMul, tempregs[i], tempregs[i], IRVTEMP_0); + } + } + + for (int i = 0; i < n; ++i) { + if (dregs[i] != tempregs[i]) { + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + } + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_Vh2f(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Vf2i(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Mftv(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int imm = op & 0xFF; + MIPSGPReg rt = _RT; + switch ((op >> 21) & 0x1f) { + case 3: //mfv / mfvc + // rt = 0, imm = 255 appears to be used as a CPU interlock by some games. + if (rt != MIPS_REG_ZERO) { + if (imm < 128) { //R(rt) = VI(imm); + ir.Write(IROp::FMovToGPR, rt, vfpuBase + voffset[imm]); + } else { + switch (imm - 128) { + case VFPU_CTRL_DPREFIX: + case VFPU_CTRL_SPREFIX: + case VFPU_CTRL_TPREFIX: + FlushPrefixV(); + break; + } + if (imm - 128 < 16) { + ir.Write(IROp::VfpuCtrlToReg, rt, imm - 128); + } else { + DISABLE; + } + } + } + break; + + case 7: // mtv + if (imm < 128) { + ir.Write(IROp::FMovFromGPR, vfpuBase + voffset[imm], rt); + } else if ((imm - 128) < 16) { + ir.Write(IROp::SetCtrlVFPU, imm - 128, rt); + } else { + DISABLE; + } + break; + + default: + DISABLE; + } + // This op is marked not to auto-eat prefix so we must do it manually. + EatPrefix(); + } + + // Good above + + void IRFrontend::Comp_Vmfvc(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int vs = _VS; + int imm = op & 0xFF; + if (imm >= 128 && imm < 128 + VFPU_CTRL_MAX) { + //if (imm - 128 == VFPU_CTRL_CC) { + // gpr.MapReg(MIPS_REG_VFPUCC, 0); + // fp.FMOV(fpr.V(vs), gpr.R(MIPS_REG_VFPUCC)); + // } else { + ir.Write(IROp::VfpuCtrlToReg, IRTEMP_0, imm - 128); + ir.Write(IROp::FMovFromGPR, vfpuBase + voffset[vs], IRTEMP_0); + } + } + + void IRFrontend::Comp_Vmtvc(MIPSOpcode op) { + CONDITIONAL_DISABLE; + int vs = _VS; + int imm = op & 0xFF; + if (imm >= 128 && imm < 128 + VFPU_CTRL_MAX) { + ir.Write(IROp::SetCtrlVFPUFReg, imm - 128, vfpuBase + voffset[vs]); + if (imm - 128 == VFPU_CTRL_SPREFIX) { + js.prefixSFlag = JitState::PREFIX_UNKNOWN; + } else if (imm - 128 == VFPU_CTRL_TPREFIX) { + js.prefixTFlag = JitState::PREFIX_UNKNOWN; + } else if (imm - 128 == VFPU_CTRL_DPREFIX) { + js.prefixDFlag = JitState::PREFIX_UNKNOWN; + } + } + } + + void IRFrontend::Comp_Vmmov(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + int vs = _VS; + int vd = _VD; + // This probably ignores prefixes for all sane intents and purposes. + if (vs == vd) { + // A lot of these no-op matrix moves in Wipeout... Just drop the instruction entirely. + return; + } + + MatrixSize sz = GetMtxSize(op); + if (sz != M_4x4) { + DISABLE; + } + int n = GetMatrixSide(sz); + + u8 sregs[16], dregs[16]; + GetMatrixRegs(sregs, sz, vs); + GetMatrixRegs(dregs, sz, vd); + + // Rough overlap check. + switch (GetMatrixOverlap(vs, vd, sz)) { + case OVERLAP_EQUAL: + // In-place transpose + DISABLE; + case OVERLAP_PARTIAL: + DISABLE; + case OVERLAP_NONE: + default: + break; + } + if (IsMatrixTransposed(vd) == IsMatrixTransposed(vs) && sz == M_4x4) { + // Untranspose both matrices + if (IsMatrixTransposed(vd)) { + vd = TransposeMatrixReg(vd); + vs = TransposeMatrixReg(vs); + } + // Get the columns + u8 scols[4], dcols[4]; + GetMatrixColumns(vs, sz, scols); + GetMatrixColumns(vd, sz, dcols); + for (int i = 0; i < 4; i++) { + u8 svec[4], dvec[4]; + GetVectorRegs(svec, GetVectorSize(sz), scols[i]); + GetVectorRegs(dvec, GetVectorSize(sz), dcols[i]); + ir.Write(IROp::Vec4Mov, dvec[0], svec[0]); + } + return; + } + for (int a = 0; a < n; a++) { + for (int b = 0; b < n; b++) { + ir.Write(IROp::FMov, dregs[a * 4 + b], sregs[a * 4 + b]); + } + } + } + + void IRFrontend::Comp_Vmscl(MIPSOpcode op) { + DISABLE; + + // TODO: Tricky, can transpose + } + + void IRFrontend::Comp_VScl(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + int vs = _VS; + int vd = _VD; + int vt = _VT; + u8 sregs[4], dregs[4], treg; + GetVectorRegsPrefixS(sregs, sz, vs); + // TODO: Prefixes seem strange... + GetVectorRegsPrefixT(&treg, V_Single, vt); + GetVectorRegsPrefixD(dregs, sz, vd); + + bool overlap = false; + // For prefixes to work, we just have to ensure that none of the output registers spill + // and that there's no overlap. + u8 tempregs[4]; + memcpy(tempregs, dregs, sizeof(tempregs)); + for (int i = 0; i < n; ++i) { + // Conservative, can be improved + if (treg == dregs[i] || !IsOverlapSafe(dregs[i], n, sregs)) { + // Need to use temp regs + tempregs[i] = IRVTEMP_0 + i; + overlap = true; + } + } + + if (n == 4 && IsConsecutive4(sregs) && IsConsecutive4(dregs)) { + if (!overlap || (vs == vd && IsOverlapSafe(treg, n, dregs))) { + ir.Write(IROp::Vec4Scale, dregs[0], sregs[0], treg); + ApplyPrefixD(dregs, sz); + return; + } + } + + for (int i = 0; i < n; i++) { + ir.Write(IROp::FMul, tempregs[i], sregs[i], treg); + } + + for (int i = 0; i < n; i++) { + // All must be mapped for prefixes to work. + if (dregs[i] != tempregs[i]) { + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + } + + ApplyPrefixD(dregs, sz); + } + + /* + // Capital = straight, lower case = transposed + // 8 possibilities: + ABC 2 + ABc missing + AbC 1 + Abc 1 + + aBC = ACB 2 + swap + aBc = AcB 1 + swap + abC = ACb missing + abc = Acb 1 + swap + + */ + + // This may or may not be a win when using the IR interpreter... + // Many more instructions to interpret. + void IRFrontend::Comp_Vmmul(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + MatrixSize sz = GetMtxSize(op); + int n = GetMatrixSide(sz); + + int vs = _VS; + int vd = _VD; + int vt = _VT; + MatrixOverlapType soverlap = GetMatrixOverlap(vs, vd, sz); + MatrixOverlapType toverlap = GetMatrixOverlap(vt, vd, sz); + + // A very common arrangment. Rearrange to something we can handle. + if (IsMatrixTransposed(vd)) { + // Matrix identity says (At * Bt) = (B * A)t + // D = S * T + // Dt = (S * T)t = (Tt * St) + vd = TransposeMatrixReg(vd); + std::swap(vs, vt); + } + + u8 sregs[16], tregs[16], dregs[16]; + GetMatrixRegs(sregs, sz, vs); + GetMatrixRegs(tregs, sz, vt); + GetMatrixRegs(dregs, sz, vd); + + if (soverlap || toverlap) { + DISABLE; + } + + // dregs are always consecutive, thanks to our transpose trick. + // However, not sure this is always worth it. + if (sz == M_4x4 && IsConsecutive4(dregs)) { + // TODO: The interpreter would like proper matrix ops better. Can generate those, and + // expand them like this as needed on "real" architectures. + int s0 = IRVTEMP_0; + int s1 = IRVTEMP_PFX_T; + if (!IsConsecutive4(sregs)) { + // METHOD 1: Handles AbC and Abc + for (int j = 0; j < 4; j++) { + ir.Write(IROp::Vec4Scale, s0, sregs[0], tregs[j * 4]); + for (int i = 1; i < 4; i++) { + ir.Write(IROp::Vec4Scale, s1, sregs[i], tregs[j * 4 + i]); + ir.Write(IROp::Vec4Add, s0, s0, s1); + } + ir.Write(IROp::Vec4Mov, dregs[j * 4], s0); + } + return; + } else if (IsConsecutive4(tregs)) { + // METHOD 2: Handles ABC only. Not efficient on CPUs that don't do fast dots. + // Dots only work if tregs are consecutive. + // TODO: Skip this and resort to method one and transpose the output? + for (int j = 0; j < 4; j++) { + for (int i = 0; i < 4; i++) { + ir.Write(IROp::Vec4Dot, s0 + i, sregs[i], tregs[j * 4]); + } + ir.Write(IROp::Vec4Mov, dregs[j * 4], s0); + } + return; + } else { + // ABc - s consecutive, t not. + // Tekken uses this. + // logBlocks = 1; + } + } + + // Fallback. Expands a LOT + int temp0 = IRVTEMP_0; + int temp1 = IRVTEMP_0 + 1; + for (int a = 0; a < n; a++) { + for (int b = 0; b < n; b++) { + ir.Write(IROp::FMul, temp0, sregs[b * 4], tregs[a * 4]); + for (int c = 1; c < n; c++) { + ir.Write(IROp::FMul, temp1, sregs[b * 4 + c], tregs[a * 4 + c]); + ir.Write(IROp::FAdd, (c == n - 1) ? dregs[a * 4 + b] : temp0, temp0, temp1); + } + } + } + } + + void IRFrontend::Comp_Vtfm(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + VectorSize sz = GetVecSize(op); + MatrixSize msz = GetMtxSize(op); + int n = GetNumVectorElements(sz); + int ins = (op >> 23) & 7; + + bool homogenous = false; + if (n == ins) { + n++; + sz = (VectorSize)((int)(sz)+1); + msz = (MatrixSize)((int)(msz)+1); + homogenous = true; + } + // Otherwise, n should already be ins + 1. + else if (n != ins + 1) { + DISABLE; + } + + u8 sregs[16], dregs[4], tregs[4]; + GetMatrixRegs(sregs, msz, _VS); + GetVectorRegs(tregs, sz, _VT); + GetVectorRegs(dregs, sz, _VD); + + // SIMD-optimized implementations - if sregs[0..3] is consecutive, the rest are too. + if (msz == M_4x4 && IsConsecutive4(sregs)) { + int s0 = IRVTEMP_0; + int s1 = IRVTEMP_PFX_T; + // For this algorithm, we don't care if tregs are consecutive or not, + // they are accessed one at a time. This handles homogenous transforms correctly, as well. + ir.Write(IROp::Vec4Scale, s0, sregs[0], tregs[0]); + for (int i = 1; i < 4; i++) { + if (!homogenous || (i != n - 1)) { + ir.Write(IROp::Vec4Scale, s1, sregs[i * 4], tregs[i]); + ir.Write(IROp::Vec4Add, s0, s0, s1); + } else { + ir.Write(IROp::Vec4Add, s0, s0, sregs[i * 4]); + } + } + if (IsConsecutive4(dregs)) { + ir.Write(IROp::Vec4Mov, dregs[0], s0); + } else { + for (int i = 0; i < 4; i++) { + ir.Write(IROp::FMov, dregs[i], s0 + i); + } + } + return; + } else if (msz == M_4x4 && !IsConsecutive4(sregs)) { + int s0 = IRVTEMP_0; + int s1 = IRVTEMP_PFX_S; + // Doesn't make complete sense to me why this works.... + ir.Write(IROp::Vec4Scale, s0, sregs[0], tregs[0]); + for (int i = 1; i < 4; i++) { + if (!homogenous || (i != n - 1)) { + ir.Write(IROp::Vec4Scale, s1, sregs[i], tregs[i]); + ir.Write(IROp::Vec4Add, s0, s0, s1); + } else { + ir.Write(IROp::Vec4Add, s0, s0, sregs[i]); + } + } + if (IsConsecutive4(dregs)) { + ir.Write(IROp::Vec4Mov, dregs[0], s0); + } else { + for (int i = 0; i < 4; i++) { + ir.Write(IROp::FMov, dregs[i], s0 + i); + } + } + return; + } + + // TODO: test overlap, optimize. + u8 tempregs[4]; + int s0 = IRVTEMP_0; + int temp1 = IRVTEMP_0 + 1; + for (int i = 0; i < n; i++) { + ir.Write(IROp::FMul, s0, sregs[i * 4], tregs[0]); + for (int k = 1; k < n; k++) { + if (!homogenous || k != n - 1) { + ir.Write(IROp::FMul, temp1, sregs[i * 4 + k], tregs[k]); + ir.Write(IROp::FAdd, s0, s0, temp1); + } else { + ir.Write(IROp::FAdd, s0, s0, sregs[i * 4 + k]); + } + } + int temp = IRVTEMP_PFX_T + i; + ir.Write(IROp::FMov, temp, s0); + tempregs[i] = temp; + } + for (int i = 0; i < n; i++) { + if (tempregs[i] != dregs[i]) + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + } + + void IRFrontend::Comp_VCrs(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_VDet(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Vi2x(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Vx2i(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_VCrossQuat(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + if (js.HasUnknownPrefix()) + DISABLE; + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], tregs[4], dregs[4]; + GetVectorRegs(sregs, sz, _VS); + GetVectorRegs(tregs, sz, _VT); + GetVectorRegs(dregs, sz, _VD); + + u8 tempregs[4]; + for (int i = 0; i < n; ++i) { + if (!IsOverlapSafe(dregs[i], n, sregs, n, tregs)) { + tempregs[i] = IRVTEMP_PFX_T + i; // using IRTEMP0 for other things + } else { + tempregs[i] = dregs[i]; + } + } + + if (sz == V_Triple) { + int temp0 = IRVTEMP_0; + int temp1 = IRVTEMP_0 + 1; + // Compute X + ir.Write(IROp::FMul, temp0, sregs[1], tregs[2]); + ir.Write(IROp::FMul, temp1, sregs[2], tregs[1]); + ir.Write(IROp::FSub, tempregs[0], temp0, temp1); + + // Compute Y + ir.Write(IROp::FMul, temp0, sregs[2], tregs[0]); + ir.Write(IROp::FMul, temp1, sregs[0], tregs[2]); + ir.Write(IROp::FSub, tempregs[1], temp0, temp1); + + // Compute Z + ir.Write(IROp::FMul, temp0, sregs[0], tregs[1]); + ir.Write(IROp::FMul, temp1, sregs[1], tregs[0]); + ir.Write(IROp::FSub, tempregs[2], temp0, temp1); + } else if (sz == V_Quad) { + DISABLE; + } + + for (int i = 0; i < n; i++) { + if (tempregs[i] != dregs[i]) + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + // No D prefix supported + } + + void IRFrontend::Comp_Vcmp(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) + DISABLE; + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], tregs[4]; + GetVectorRegsPrefixS(sregs, sz, _VS); + GetVectorRegsPrefixT(tregs, sz, _VT); + + VCondition cond = (VCondition)(op & 0xF); + int mask = 0; + for (int i = 0; i < n; i++) { + ir.Write(IROp::FCmpVfpuBit, cond | (i << 4), sregs[i], tregs[i]); + mask |= (1 << i); + } + ir.Write(IROp::FCmpVfpuAggregate, mask); + } + + void IRFrontend::Comp_Vcmov(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + // logBlocks = 1; + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], dregs[4]; + GetVectorRegsPrefixS(sregs, sz, _VS); + GetVectorRegsPrefixD(dregs, sz, _VD); + int tf = (op >> 19) & 1; + int imm3 = (op >> 16) & 7; + + for (int i = 0; i < n; ++i) { + // Simplification: Disable if overlap unsafe + if (!IsOverlapSafeAllowS(dregs[i], i, n, sregs)) { + DISABLE; + } + } + if (imm3 < 6) { + // Test one bit of CC. This bit decides whether none or all subregisters are copied. + for (int i = 0; i < n; i++) { + ir.Write(IROp::FCmovVfpuCC, dregs[i], sregs[i], (imm3) | ((!tf) << 7)); + } + } else { + // Look at the bottom four bits of CC to individually decide if the subregisters should be copied. + for (int i = 0; i < n; i++) { + ir.Write(IROp::FCmovVfpuCC, dregs[i], sregs[i], (i) | ((!tf) << 7)); + } + } + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_Viim(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) + DISABLE; + + s32 imm = (s32)(s16)(u16)(op & 0xFFFF); + u8 dreg; + GetVectorRegsPrefixD(&dreg, V_Single, _VT); + ir.Write(IROp::SetConstF, dreg, ir.AddConstantFloat((float)imm)); + ApplyPrefixD(&dreg, V_Single); + } + + void IRFrontend::Comp_Vfim(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) + DISABLE; + + FP16 half; + half.u = op & 0xFFFF; + FP32 fval = half_to_float_fast5(half); + + u8 dreg; + GetVectorRegsPrefixD(&dreg, V_Single, _VT); + ir.Write(IROp::SetConstF, dreg, ir.AddConstantFloat(fval.f)); + ApplyPrefixD(&dreg, V_Single); + } + + void IRFrontend::Comp_Vcst(MIPSOpcode op) { + CONDITIONAL_DISABLE; + + if (js.HasUnknownPrefix()) + DISABLE; + + int conNum = (op >> 16) & 0x1f; + int vd = _VD; + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 dregs[4]; + GetVectorRegsPrefixD(dregs, sz, _VD); + for (int i = 0; i < n; i++) { + ir.Write(IROp::SetConstF, dregs[i], ir.AddConstantFloat(cst_constants[conNum])); + } + ApplyPrefixD(dregs, sz); + } + + // Very heavily used by FF:CC. Should be replaced by a fast approximation instead of + // calling the math library. + void IRFrontend::Comp_VRot(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Vsgn(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Vocp(MIPSOpcode op) { + CONDITIONAL_DISABLE; + if (js.HasUnknownPrefix()) { + DISABLE; + } + + VectorSize sz = GetVecSize(op); + int n = GetNumVectorElements(sz); + + u8 sregs[4], dregs[4]; + // Actually, not sure that this instruction accepts an S prefix. We don't apply it in the + // interpreter. But whatever. + GetVectorRegsPrefixS(sregs, sz, _VS); + GetVectorRegsPrefixD(dregs, sz, _VD); + + u8 tempregs[4]; + for (int i = 0; i < n; ++i) { + if (!IsOverlapSafe(dregs[i], n, sregs)) { + tempregs[i] = IRVTEMP_PFX_T + i; // using IRTEMP0 for other things + } else { + tempregs[i] = dregs[i]; + } + } + + ir.Write(IROp::SetConstF, IRVTEMP_0, ir.AddConstantFloat(1.0f)); + for (int i = 0; i < n; ++i) { + ir.Write(IROp::FSub, tempregs[i], IRVTEMP_0, sregs[i]); + } + for (int i = 0; i < n; ++i) { + if (dregs[i] != tempregs[i]) { + ir.Write(IROp::FMov, dregs[i], tempregs[i]); + } + } + + ApplyPrefixD(dregs, sz); + } + + void IRFrontend::Comp_ColorConv(MIPSOpcode op) { + DISABLE; + } + + void IRFrontend::Comp_Vbfy(MIPSOpcode op) { + DISABLE; + } +} diff --git a/Core/MIPS/IR/IRFrontend.cpp b/Core/MIPS/IR/IRFrontend.cpp new file mode 100644 index 0000000000..6353816407 --- /dev/null +++ b/Core/MIPS/IR/IRFrontend.cpp @@ -0,0 +1,291 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 "base/logging.h" + +#include "Common/ChunkFile.h" +#include "Core/Reporting.h" +#include "Core/MemMap.h" + +#include "Core/MIPS/MIPSTables.h" +#include "Core/HLE/ReplaceTables.h" + +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRPassSimplify.h" +#include "Core/MIPS/IR/IRInterpreter.h" + +namespace MIPSComp { + +IRFrontend::IRFrontend(bool startDefaultPrefix) { + logBlocks = 0; + dontLogBlocks = 0; + js.startDefaultPrefix = true; + js.hasSetRounding = false; + // js.currentRoundingFunc = convertS0ToSCRATCH1[0]; +} + +void IRFrontend::DoState(PointerWrap &p) { + auto s = p.Section("Jit", 1, 2); + if (!s) + return; + + p.Do(js.startDefaultPrefix); + if (s >= 2) { + p.Do(js.hasSetRounding); + js.lastSetRounding = 0; + } else { + js.hasSetRounding = 1; + } +} + +void IRFrontend::FlushAll() { + FlushPrefixV(); +} + +void IRFrontend::FlushPrefixV() { + if ((js.prefixSFlag & JitState::PREFIX_DIRTY) != 0) { + ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_SPREFIX, ir.AddConstant(js.prefixS)); + js.prefixSFlag = (JitState::PrefixState) (js.prefixSFlag & ~JitState::PREFIX_DIRTY); + } + + if ((js.prefixTFlag & JitState::PREFIX_DIRTY) != 0) { + ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_TPREFIX, ir.AddConstant(js.prefixT)); + js.prefixTFlag = (JitState::PrefixState) (js.prefixTFlag & ~JitState::PREFIX_DIRTY); + } + + if ((js.prefixDFlag & JitState::PREFIX_DIRTY) != 0) { + ir.Write(IROp::SetCtrlVFPU, VFPU_CTRL_DPREFIX, ir.AddConstant(js.prefixD)); + js.prefixDFlag = (JitState::PrefixState) (js.prefixDFlag & ~JitState::PREFIX_DIRTY); + } +} + +void IRFrontend::EatInstruction(MIPSOpcode op) { + MIPSInfo info = MIPSGetInfo(op); + if (info & DELAYSLOT) { + ERROR_LOG_REPORT_ONCE(ateDelaySlot, JIT, "Ate a branch op."); + } + if (js.inDelaySlot) { + ERROR_LOG_REPORT_ONCE(ateInDelaySlot, JIT, "Ate an instruction inside a delay slot."); + } + + js.numInstructions++; + js.compilerPC += 4; + js.downcountAmount += MIPSGetInstructionCycleEstimate(op); +} + +void IRFrontend::CompileDelaySlot() { + js.inDelaySlot = true; + MIPSOpcode op = GetOffsetInstruction(1); + MIPSCompileOp(op, this); + js.inDelaySlot = false; +} + +bool IRFrontend::CheckRounding() { + bool cleanSlate = false; + if (js.hasSetRounding && !js.lastSetRounding) { + WARN_LOG(JIT, "Detected rounding mode usage, rebuilding jit with checks"); + // Won't loop, since hasSetRounding is only ever set to 1. + js.lastSetRounding = js.hasSetRounding; + cleanSlate = true; + } + + // Drat. The VFPU hit an uneaten prefix at the end of a block. + if (js.startDefaultPrefix && js.MayHavePrefix()) { + WARN_LOG(JIT, "An uneaten prefix at end of block"); + js.LogPrefix(); + + // Let's try that one more time. We won't get back here because we toggled the value. + js.startDefaultPrefix = false; + // TODO: Make sure this works. + // cleanSlate = true; + } + + return cleanSlate; +} + + +void IRFrontend::Comp_ReplacementFunc(MIPSOpcode op) { + int index = op.encoding & MIPS_EMUHACK_VALUE_MASK; + + const ReplacementTableEntry *entry = GetReplacementFunc(index); + if (!entry) { + ERROR_LOG(HLE, "Invalid replacement op %08x", op.encoding); + return; + } + + if (entry->flags & REPFLAG_DISABLED) { + MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this); + } else if (entry->replaceFunc) { + FlushAll(); + RestoreRoundingMode(); + ir.Write(IROp::SetPCConst, 0, ir.AddConstant(GetCompilerPC())); + ir.Write(IROp::CallReplacement, 0, ir.AddConstant(index)); + + if (entry->flags & (REPFLAG_HOOKENTER | REPFLAG_HOOKEXIT)) { + // Compile the original instruction at this address. We ignore cycles for hooks. + ApplyRoundingMode(); + MIPSCompileOp(Memory::Read_Instruction(GetCompilerPC(), true), this); + } else { + ApplyRoundingMode(); + ir.Write(IROp::Downcount, 0, js.downcountAmount & 0xFF, js.downcountAmount >> 8); + ir.Write(IROp::ExitToReg, 0, MIPS_REG_RA, 0); + js.compiling = false; + } + } else { + ERROR_LOG(HLE, "Replacement function %s has neither jit nor regular impl", entry->name); + } +} + +void IRFrontend::Comp_Generic(MIPSOpcode op) { + FlushAll(); + ir.Write(IROp::Interpret, 0, ir.AddConstant(op.encoding)); + const MIPSInfo info = MIPSGetInfo(op); + if ((info & IS_VFPU) != 0 && (info & VFPU_NO_PREFIX) == 0) { + // If it does eat them, it'll happen in MIPSCompileOp(). + if ((info & OUT_EAT_PREFIX) == 0) + js.PrefixUnknown(); + } +} + +// Destroys SCRATCH2 +void IRFrontend::RestoreRoundingMode(bool force) { + // If the game has never set an interesting rounding mode, we can safely skip this. + if (force || js.hasSetRounding) { + ir.Write(IROp::RestoreRoundingMode); + } +} + +// Destroys SCRATCH1 and SCRATCH2 +void IRFrontend::ApplyRoundingMode(bool force) { + // If the game has never set an interesting rounding mode, we can safely skip this. + if (force || js.hasSetRounding) { + ir.Write(IROp::ApplyRoundingMode); + } +} + +// Destroys SCRATCH1 and SCRATCH2 +void IRFrontend::UpdateRoundingMode() { + ir.Write(IROp::UpdateRoundingMode); +} + +void IRFrontend::Comp_DoNothing(MIPSOpcode op) { +} + +int IRFrontend::Replace_fabsf() { + Crash(); + return 0; +} + +u32 IRFrontend::GetCompilerPC() { + return js.compilerPC; +} + +MIPSOpcode IRFrontend::GetOffsetInstruction(int offset) { + return Memory::Read_Instruction(GetCompilerPC() + 4 * offset); +} + +void IRFrontend::DoJit(u32 em_address, std::vector &instructions, std::vector &constants) { + js.cancel = false; + js.blockStart = em_address; + js.compilerPC = em_address; + js.lastContinuedPC = 0; + js.initialBlockSize = 0; + js.nextExit = 0; + js.downcountAmount = 0; + js.curBlock = nullptr; + js.compiling = true; + js.inDelaySlot = false; + js.PrefixStart(); + ir.Clear(); + + js.numInstructions = 0; + while (js.compiling) { + MIPSOpcode inst = Memory::Read_Opcode_JIT(GetCompilerPC()); + js.downcountAmount += MIPSGetInstructionCycleEstimate(inst); + MIPSCompileOp(inst, this); + js.compilerPC += 4; + js.numInstructions++; + + if (ir.GetConstants().size() > 64) { + // Need to break the block + ir.Write(IROp::ExitToConst, ir.AddConstant(js.compilerPC)); + js.compiling = false; + } + } + + IRWriter simplified; + IRWriter *code = &ir; + if (true) { + static const IRPassFunc passes[] = { + &OptimizeFPMoves, + &PropagateConstants, + &PurgeTemps, + // &ThreeOpToTwoOp, + }; + if (IRApplyPasses(passes, ARRAY_SIZE(passes), ir, simplified)) + logBlocks = 1; + code = &simplified; + //if (ir.GetInstructions().size() >= 24) + // logBlocks = 1; + } + + instructions = code->GetInstructions(); + constants = code->GetConstants(); + + if (logBlocks > 0 && dontLogBlocks == 0) { + char temp2[256]; + ILOG("=============== mips %08x ===============", em_address); + for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) { + temp2[0] = 0; + MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, true); + ILOG("M: %08x %s", cpc, temp2); + } + } + + if (logBlocks > 0 && dontLogBlocks == 0) { + ILOG("=============== Original IR (%d instructions, %d const) ===============", (int)ir.GetInstructions().size(), (int)ir.GetConstants().size()); + for (size_t i = 0; i < ir.GetInstructions().size(); i++) { + char buf[256]; + DisassembleIR(buf, sizeof(buf), ir.GetInstructions()[i], &ir.GetConstants()[0]); + ILOG("%s", buf); + } + ILOG("=============== end ================="); + } + + if (logBlocks > 0 && dontLogBlocks == 0) { + ILOG("=============== IR (%d instructions, %d const) ===============", (int)code->GetInstructions().size(), (int)code->GetConstants().size()); + for (size_t i = 0; i < code->GetInstructions().size(); i++) { + char buf[256]; + DisassembleIR(buf, sizeof(buf), code->GetInstructions()[i], &code->GetConstants()[0]); + ILOG("%s", buf); + } + ILOG("=============== end ================="); + } + + if (logBlocks > 0) + logBlocks--; + if (dontLogBlocks > 0) + dontLogBlocks--; +} + +void IRFrontend::Comp_RunBlock(MIPSOpcode op) { + // This shouldn't be necessary, the dispatcher should catch us before we get here. + ERROR_LOG(JIT, "Comp_RunBlock should never be reached!"); +} + +} // namespace \ No newline at end of file diff --git a/Core/MIPS/IR/IRFrontend.h b/Core/MIPS/IR/IRFrontend.h new file mode 100644 index 0000000000..8ffdf5c904 --- /dev/null +++ b/Core/MIPS/IR/IRFrontend.h @@ -0,0 +1,139 @@ +#pragma once + +#include "Common/CommonTypes.h" +#include "Core/MIPS/JitCommon/JitCommon.h" +#include "Core/MIPS/JitCommon/JitState.h" +#include "Core/MIPS/MIPSVFPUUtils.h" +#include "Core/MIPS/IR/IRInst.h" + +namespace MIPSComp { + +class IRFrontend : public MIPSFrontendInterface { +public: + IRFrontend(bool startDefaultPrefix); + void Comp_Generic(MIPSOpcode op) override; + + void Comp_RunBlock(MIPSOpcode op) override; + void Comp_ReplacementFunc(MIPSOpcode op) override; + + // Ops + void Comp_ITypeMem(MIPSOpcode op) override; + void Comp_Cache(MIPSOpcode op) override; + + void Comp_RelBranch(MIPSOpcode op) override; + void Comp_RelBranchRI(MIPSOpcode op) override; + void Comp_FPUBranch(MIPSOpcode op) override; + void Comp_FPULS(MIPSOpcode op) override; + void Comp_FPUComp(MIPSOpcode op) override; + void Comp_Jump(MIPSOpcode op) override; + void Comp_JumpReg(MIPSOpcode op) override; + void Comp_Syscall(MIPSOpcode op) override; + void Comp_Break(MIPSOpcode op) override; + + void Comp_IType(MIPSOpcode op) override; + void Comp_RType2(MIPSOpcode op) override; + void Comp_RType3(MIPSOpcode op) override; + void Comp_ShiftType(MIPSOpcode op) override; + void Comp_Allegrex(MIPSOpcode op) override; + void Comp_Allegrex2(MIPSOpcode op) override; + void Comp_VBranch(MIPSOpcode op) override; + void Comp_MulDivType(MIPSOpcode op) override; + void Comp_Special3(MIPSOpcode op) override; + + void Comp_FPU3op(MIPSOpcode op) override; + void Comp_FPU2op(MIPSOpcode op) override; + void Comp_mxc1(MIPSOpcode op) override; + + void Comp_DoNothing(MIPSOpcode op) override; + + void Comp_SV(MIPSOpcode op) override; + void Comp_SVQ(MIPSOpcode op) override; + void Comp_VPFX(MIPSOpcode op) override; + void Comp_VVectorInit(MIPSOpcode op) override; + void Comp_VMatrixInit(MIPSOpcode op) override; + void Comp_VDot(MIPSOpcode op) override; + void Comp_VecDo3(MIPSOpcode op) override; + void Comp_VV2Op(MIPSOpcode op) override; + void Comp_Mftv(MIPSOpcode op) override; + void Comp_Vmfvc(MIPSOpcode op) override; + void Comp_Vmtvc(MIPSOpcode op) override; + void Comp_Vmmov(MIPSOpcode op) override; + void Comp_VScl(MIPSOpcode op) override; + void Comp_Vmmul(MIPSOpcode op) override; + void Comp_Vmscl(MIPSOpcode op) override; + void Comp_Vtfm(MIPSOpcode op) override; + void Comp_VHdp(MIPSOpcode op) override; + void Comp_VCrs(MIPSOpcode op) override; + void Comp_VDet(MIPSOpcode op) override; + void Comp_Vi2x(MIPSOpcode op) override; + void Comp_Vx2i(MIPSOpcode op) override; + void Comp_Vf2i(MIPSOpcode op) override; + void Comp_Vi2f(MIPSOpcode op) override; + void Comp_Vh2f(MIPSOpcode op) override; + void Comp_Vcst(MIPSOpcode op) override; + void Comp_Vhoriz(MIPSOpcode op) override; + void Comp_VRot(MIPSOpcode op) override; + void Comp_VIdt(MIPSOpcode op) override; + void Comp_Vcmp(MIPSOpcode op) override; + void Comp_Vcmov(MIPSOpcode op) override; + void Comp_Viim(MIPSOpcode op) override; + void Comp_Vfim(MIPSOpcode op) override; + void Comp_VCrossQuat(MIPSOpcode op) override; + void Comp_Vsgn(MIPSOpcode op) override; + void Comp_Vocp(MIPSOpcode op) override; + void Comp_ColorConv(MIPSOpcode op) override; + void Comp_Vbfy(MIPSOpcode op) override; + + int Replace_fabsf(); + void DoState(PointerWrap &p); + bool CheckRounding(); // returns true if we need a do-over + + void DoJit(u32 em_address, std::vector &instructions, std::vector &constants); + + void EatPrefix() override { + js.EatPrefix(); + } + +private: + void RestoreRoundingMode(bool force = false); + void ApplyRoundingMode(bool force = false); + void UpdateRoundingMode(); + + void FlushAll(); + void FlushPrefixV(); + + u32 GetCompilerPC(); + void CompileDelaySlot(); + void EatInstruction(MIPSOpcode op); + MIPSOpcode GetOffsetInstruction(int offset); + + // Utility compilation functions + void BranchFPFlag(MIPSOpcode op, IRComparison cc, bool likely); + void BranchVFPUFlag(MIPSOpcode op, IRComparison cc, bool likely); + void BranchRSZeroComp(MIPSOpcode op, IRComparison cc, bool andLink, bool likely); + void BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely); + + // Utilities to reduce duplicated code + void CompShiftImm(MIPSOpcode op, IROp shiftType, int sa); + void CompShiftVar(MIPSOpcode op, IROp shiftType, IROp shiftTypeConst); + + void ApplyPrefixST(u8 *vregs, u32 prefix, VectorSize sz, int tempReg); + void ApplyPrefixD(const u8 *vregs, VectorSize sz); + void GetVectorRegsPrefixS(u8 *regs, VectorSize sz, int vectorReg); + void GetVectorRegsPrefixT(u8 *regs, VectorSize sz, int vectorReg); + void GetVectorRegsPrefixD(u8 *regs, VectorSize sz, int vectorReg); + void GetVectorRegs(u8 regs[4], VectorSize N, int vectorReg); + void GetMatrixRegs(u8 regs[16], MatrixSize N, int matrixReg); + + // Utils + void Comp_ITypeMemLR(MIPSOpcode op, bool load); + + // State + JitState js; + IRWriter ir; + + int dontLogBlocks; + int logBlocks; +}; + +} // namespace \ No newline at end of file diff --git a/Core/MIPS/IR/IRInst.cpp b/Core/MIPS/IR/IRInst.cpp new file mode 100644 index 0000000000..dac93617f2 --- /dev/null +++ b/Core/MIPS/IR/IRInst.cpp @@ -0,0 +1,277 @@ +#include "Common/CommonFuncs.h" +#include "Core/MIPS/IR/IRInst.h" +#include "Core/MIPS/IR/IRPassSimplify.h" +#include "Core/MIPS/MIPSDebugInterface.h" + +static const IRMeta irMeta[] = { + { IROp::Nop, "Nop", "" }, + { IROp::SetConst, "SetConst", "GC" }, + { IROp::SetConstF, "SetConstF", "FC" }, + { IROp::Mov, "Mov", "GG" }, + { IROp::Add, "Add", "GGG" }, + { IROp::Sub, "Sub", "GGG" }, + { IROp::Neg, "Neg", "GG" }, + { IROp::Not, "Not", "GG" }, + { IROp::And, "And", "GGG" }, + { IROp::Or, "Or", "GGG" }, + { IROp::Xor, "Xor", "GGG" }, + { IROp::AddConst, "AddConst", "GGC" }, + { IROp::SubConst, "SubConst", "GGC" }, + { IROp::AndConst, "AndConst", "GGC" }, + { IROp::OrConst, "OrConst", "GGC" }, + { IROp::XorConst, "XorConst", "GGC" }, + { IROp::Shl, "Shl", "GGG" }, + { IROp::Shr, "Shr", "GGG" }, + { IROp::Sar, "Sar", "GGG" }, + { IROp::Ror, "Ror", "GGG" }, + { IROp::ShlImm, "ShlImm", "GGI" }, + { IROp::ShrImm, "ShrImm", "GGI" }, + { IROp::SarImm, "SarImm", "GGI" }, + { IROp::RorImm, "RorImm", "GGI" }, + { IROp::Slt, "Slt", "GGG" }, + { IROp::SltConst, "SltConst", "GGC" }, + { IROp::SltU, "SltU", "GGG" }, + { IROp::SltUConst, "SltUConst", "GGC" }, + { IROp::Clz, "Clz", "GG" }, + { IROp::MovZ, "MovZ", "GGG", IRFLAG_SRC3DST }, + { IROp::MovNZ, "MovNZ", "GGG", IRFLAG_SRC3DST }, + { IROp::Max, "Max", "GGG" }, + { IROp::Min, "Min", "GGG" }, + { IROp::BSwap16, "BSwap16", "GG" }, + { IROp::BSwap32, "BSwap32", "GG" }, + { IROp::Mult, "Mult", "_GG" }, + { IROp::MultU, "MultU", "_GG" }, + { IROp::Madd, "Madd", "_GG" }, + { IROp::MaddU, "MaddU", "_GG" }, + { IROp::Msub, "Msub", "_GG" }, + { IROp::MsubU, "MsubU", "_GG" }, + { IROp::Div, "Div", "_GG" }, + { IROp::DivU, "DivU", "_GG" }, + { IROp::MtLo, "MtLo", "_G" }, + { IROp::MtHi, "MtHi", "_G" }, + { IROp::MfLo, "MfLo", "G" }, + { IROp::MfHi, "MfHi", "G" }, + { IROp::Ext8to32, "Ext8to32", "GG" }, + { IROp::Ext16to32, "Ext16to32", "GG" }, + { IROp::Load8, "Load8", "GGC" }, + { IROp::Load8Ext, "Load8", "GGC" }, + { IROp::Load16, "Load16", "GGC" }, + { IROp::Load16Ext, "Load16Ext", "GGC" }, + { IROp::Load32, "Load32", "GGC" }, + { IROp::LoadFloat, "LoadFloat", "FGC" }, + { IROp::LoadVec4, "LoadVec4", "FGC" }, + { IROp::Store8, "Store8", "GGC", IRFLAG_SRC3 }, + { IROp::Store16, "Store16", "GGC", IRFLAG_SRC3 }, + { IROp::Store32, "Store32", "GGC", IRFLAG_SRC3 }, + { IROp::StoreFloat, "StoreFloat", "FGC", IRFLAG_SRC3 }, + { IROp::StoreVec4, "StoreVec4", "FGC", IRFLAG_SRC3 }, + { IROp::FAdd, "FAdd", "FFF" }, + { IROp::FSub, "FSub", "FFF" }, + { IROp::FMul, "FMul", "FFF" }, + { IROp::FDiv, "FDiv", "FFF" }, + { IROp::FMin, "FMin", "FFF" }, + { IROp::FMax, "FMax", "FFF" }, + { IROp::FMov, "FMov", "FF" }, + { IROp::FSqrt, "FSqrt", "FF" }, + { IROp::FSin, "FSin", "FF" }, + { IROp::FCos, "FCos", "FF" }, + { IROp::FSqrt, "FSqrt", "FF" }, + { IROp::FRSqrt, "FRSqrt", "FF" }, + { IROp::FRecip, "FRecip", "FF" }, + { IROp::FAsin, "FAsin", "FF" }, + { IROp::FNeg, "FNeg", "FF" }, + { IROp::FAbs, "FAbs", "FF" }, + { IROp::FRound, "FRound", "FF" }, + { IROp::FTrunc, "FTrunc", "FF" }, + { IROp::FCeil, "FCeil", "FF" }, + { IROp::FFloor, "FFloor", "FF" }, + { IROp::FCvtWS, "FCvtWS", "FF" }, + { IROp::FCvtSW, "FCvtSW", "FF" }, + { IROp::FCmp, "FCmp", "mFF" }, + { IROp::FSat0_1, "FSat(0 - 1)", "FF" }, + { IROp::FSatMinus1_1, "FSat(-1 - 1)", "FF" }, + { IROp::FMovFromGPR, "FMovFromGPR", "FG" }, + { IROp::FMovToGPR, "FMovToGPR", "GF" }, + { IROp::FpCondToReg, "FpCondToReg", "G" }, + { IROp::VfpuCtrlToReg, "VfpuCtrlToReg", "GI" }, + { IROp::SetCtrlVFPU, "SetCtrlVFPU", "TC" }, + { IROp::SetCtrlVFPUReg, "SetCtrlVFPUReg", "TC" }, + { IROp::SetCtrlVFPUFReg, "SetCtrlVFPUFReg", "TF" }, + { IROp::FCmovVfpuCC, "FCmovVfpuCC", "FFI" }, + { IROp::FCmpVfpuBit, "FCmpVfpuBit", "IFF" }, + { IROp::FCmpVfpuAggregate, "FCmpVfpuAggregate", ""}, + { IROp::Vec4Init, "Vec4Init", "Fv" }, + { IROp::Vec4Shuffle, "Vec4Shuffle", "FFs" }, + { IROp::Vec4Mov, "Vec4Mov", "FF" }, + { IROp::Vec4Add, "Vec4Add", "FFF" }, + { IROp::Vec4Sub, "Vec4Sub", "FFF" }, + { IROp::Vec4Div, "Vec4Div", "FFF" }, + { IROp::Vec4Mul, "Vec4Mul", "FFF" }, + { IROp::Vec4Scale, "Vec4Scale", "FFF" }, + { IROp::Vec4Dot, "Vec4Dot", "FFF" }, + { IROp::Vec4Neg, "Vec4Neg", "FF" }, + { IROp::Vec4Abs, "Vec4Abs", "FF" }, + + { IROp::Interpret, "Interpret", "_C" }, + { IROp::Downcount, "Downcount", "_II" }, + { IROp::ExitToConst, "Exit", "C", IRFLAG_EXIT }, + { IROp::ExitToConstIfEq, "ExitIfEq", "CGG", IRFLAG_EXIT }, + { IROp::ExitToConstIfNeq, "ExitIfNeq", "CGG", IRFLAG_EXIT }, + { IROp::ExitToConstIfGtZ, "ExitIfGtZ", "CG", IRFLAG_EXIT }, + { IROp::ExitToConstIfGeZ, "ExitIfGeZ", "CG", IRFLAG_EXIT }, + { IROp::ExitToConstIfLeZ, "ExitIfLeZ", "CG", IRFLAG_EXIT }, + { IROp::ExitToConstIfLtZ, "ExitIfLtZ", "CG", IRFLAG_EXIT }, + { IROp::ExitToReg, "ExitToReg", "_G", IRFLAG_EXIT }, + { IROp::Syscall, "Syscall", "_C", IRFLAG_EXIT }, + { IROp::Break, "Break", "", IRFLAG_EXIT}, + { IROp::SetPC, "SetPC", "_G" }, + { IROp::SetPCConst, "SetPC", "_C" }, + { IROp::CallReplacement, "CallRepl", "_C" }, +}; + +const IRMeta *metaIndex[256]; + +void InitIR() { + for (size_t i = 0; i < ARRAY_SIZE(irMeta); i++) { + metaIndex[(int)irMeta[i].op] = &irMeta[i]; + } +} + +void IRWriter::Write(IROp op, u8 dst, u8 src1, u8 src2) { + IRInst inst; + inst.op = op; + inst.dest = dst; + inst.src1 = src1; + inst.src2 = src2; + insts_.push_back(inst); +} + +void IRWriter::WriteSetConstant(u8 dst, u32 value) { + Write(IROp::SetConst, dst, AddConstant(value)); +} + +int IRWriter::AddConstant(u32 value) { + for (size_t i = 0; i < constPool_.size(); i++) { + if (constPool_[i] == value) + return (int)i; + } + constPool_.push_back(value); + if (constPool_.size() > 255) { + // Cannot have more than 256 constants in a block! + Crash(); + } + return (int)constPool_.size() - 1; +} + +int IRWriter::AddConstantFloat(float value) { + u32 val; + memcpy(&val, &value, 4); + return AddConstant(val); +} + +const char *GetGPRName(int r) { + if (r < 32) { + return currentDebugMIPS->GetRegName(0, r); + } + switch (r) { + case IRTEMP_0: return "irtemp0"; + case IRTEMP_1: return "irtemp1"; + case IRTEMP_LHS: return "irtemp_lhs"; + case IRTEMP_RHS: return "irtemp_rhs"; + default: return "(unk)"; + } +} + +void DisassembleParam(char *buf, int bufSize, u8 param, char type, const u32 *constPool) { + static const char *vfpuCtrlNames[VFPU_CTRL_MAX] = { + "SPFX", + "TPFX", + "DPFX", + "CC", + "INF4", + "RSV5", + "RSV6", + "REV", + "RCX0", + "RCX1", + "RCX2", + "RCX3", + "RCX4", + "RCX5", + "RCX6", + "RCX7", + }; + static const char *initVec4Names[8] = { + "[0 0 0 0]", + "[1 1 1 1]", + "[-1 -1 -1 -1]", + "[1 0 0 0]", + "[0 1 0 0]", + "[0 0 1 0]", + "[0 0 0 1]", + }; + static const char *xyzw = "xyzw"; + + switch (type) { + case 'G': + snprintf(buf, bufSize, "%s", GetGPRName(param)); + break; + case 'F': + if (param >= 32) { + snprintf(buf, bufSize, "v%d", param - 32); + } else { + snprintf(buf, bufSize, "f%d", param); + } + break; + case 'C': + snprintf(buf, bufSize, "%08x", constPool[param]); + break; + case 'I': + snprintf(buf, bufSize, "%02x", param); + break; + case 'm': + snprintf(buf, bufSize, "%d", param); + break; + case 'T': + snprintf(buf, bufSize, "%s", vfpuCtrlNames[param]); + break; + case 'v': + snprintf(buf, bufSize, "%s", initVec4Names[param]); + break; + case 's': + snprintf(buf, bufSize, "%c%c%c%c", xyzw[param & 3], xyzw[(param >> 2) & 3], xyzw[(param >> 4) & 3], xyzw[(param >> 6) & 3]); + break; + case '_': + case '\0': + buf[0] = 0; + break; + default: + snprintf(buf, bufSize, "?"); + break; + } +} + +const IRMeta *GetIRMeta(IROp op) { + return metaIndex[(int)op]; +} + +void DisassembleIR(char *buf, size_t bufsize, IRInst inst, const u32 *constPool) { + const IRMeta *meta = GetIRMeta(inst.op); + if (!meta) { + snprintf(buf, bufsize, "Unknown %d", (int)inst.op); + return; + } + char bufDst[16]; + char bufSrc1[16]; + char bufSrc2[16]; + DisassembleParam(bufDst, sizeof(bufDst) - 2, inst.dest, meta->types[0], constPool); + DisassembleParam(bufSrc1, sizeof(bufSrc1) - 2, inst.src1, meta->types[1], constPool); + DisassembleParam(bufSrc2, sizeof(bufSrc2), inst.src2, meta->types[2], constPool); + if (meta->types[1] && meta->types[0] != '_') { + strcat(bufDst, ", "); + } + if (meta->types[2] && meta->types[1] != '_') { + strcat(bufSrc1, ", "); + } + snprintf(buf, bufsize, "%s %s%s%s", meta->name, bufDst, bufSrc1, bufSrc2); +} diff --git a/Core/MIPS/IR/IRInst.h b/Core/MIPS/IR/IRInst.h new file mode 100644 index 0000000000..b1e17a6c45 --- /dev/null +++ b/Core/MIPS/IR/IRInst.h @@ -0,0 +1,365 @@ +#pragma once + +#include +#include + +#include "Common/CommonTypes.h" +#include "Core/MIPS/MIPS.h" + +#ifdef __SYMBIAN32__ +// Seems std::move() doesn't exist, so assuming it can't do moves at all. +namespace std { + template + const T &move(const T &x) { + return x; + } +}; +#endif + +// Basic IR +// +// This IR refers implicitly to the MIPS register set and is simple to interpret. +// To do real compiler things with it and do full-function compilation, it probably +// needs to be lifted to a higher IR first, before being lowered onto each target. +// But this gets rid of a lot of MIPS idiosyncrasies that makes it tricky, like +// delay slots, and is very suitable for translation into other IRs. Can of course +// even be directly JIT-ed, but the gains will probably be tiny over our older direct +// MIPS->target JITs. + +enum class IROp : u8 { + Nop, + + SetConst, + SetConstF, + + Mov, + + Add, + Sub, + Neg, + Not, + + And, + Or, + Xor, + + AddConst, + SubConst, + + AndConst, + OrConst, + XorConst, + + Shl, + Shr, + Sar, + Ror, + + // The shift is stored directly, not in the const table, so Imm instead of Const + ShlImm, + ShrImm, + SarImm, + RorImm, + + Slt, + SltConst, + SltU, + SltUConst, + + Clz, + + // Conditional moves + MovZ, + MovNZ, + + Max, + Min, + + // Byte swaps. All CPUs have native ones so worth keeping. + BSwap16, // Swaps both the high and low byte pairs. + BSwap32, + + // Weird Hi/Lo semantics preserved. Too annoying to do something more generic. + MtLo, + MtHi, + MfLo, + MfHi, + Mult, + MultU, + Madd, + MaddU, + Msub, + MsubU, + Div, + DivU, + + // These take a constant from the pool as an offset. + // Loads from a constant address can be represented by using r0. + Load8, + Load8Ext, + Load16, + Load16Ext, + Load32, + LoadFloat, + LoadVec4, + + Store8, + Store16, + Store32, + StoreFloat, + StoreVec4, + + Ext8to32, + Ext16to32, + + FAdd, + FSub, + FMul, + FDiv, + FMin, + FMax, + + FMov, + FSqrt, + FNeg, + FAbs, + + FRound, + FTrunc, + FCeil, + FFloor, + + FCvtWS, + FCvtSW, + + FMovFromGPR, + FMovToGPR, + + FSat0_1, + FSatMinus1_1, + + FpCondToReg, + VfpuCtrlToReg, + + ZeroFpCond, + FCmp, + + FCmovVfpuCC, + FCmpVfpuBit, + FCmpVfpuAggregate, + + // Rounding Mode + RestoreRoundingMode, + ApplyRoundingMode, + UpdateRoundingMode, + + SetCtrlVFPU, + SetCtrlVFPUReg, + SetCtrlVFPUFReg, + + // 4-wide instructions to assist SIMD. + // Can of course add a pass to break them up if a target does not + // support SIMD. + Vec4Init, + Vec4Shuffle, + Vec4Mov, + Vec4Add, + Vec4Sub, + Vec4Mul, + Vec4Div, + Vec4Scale, + Vec4Dot, + Vec4Neg, + Vec4Abs, + + // vx2i + Vec4ExpandU16ToU32Hi, + Vec4ExpandU8ToU32Hi, + Vec4ExpandS16ToS32Hi, + Vec4ExpandS8ToS32Hi, + + // Slow special functions. Used on singles. + FSin, + FCos, + FRSqrt, + FRecip, + FAsin, + + // Fake/System instructions + Interpret, + + // Emit this before you exits. Semantic is to set the downcount + // that will be used at the actual exit. + Downcount, // src1 + (src2<<8) + + // End-of-basic-block. + ExitToConst, // 0, const, downcount + ExitToReg, + ExitToConstIfEq, // const, reg1, reg2 + ExitToConstIfNeq, // const, reg1, reg2 + ExitToConstIfGtZ, // const, reg1, 0 + ExitToConstIfGeZ, // const, reg1, 0 + ExitToConstIfLtZ, // const, reg1, 0 + ExitToConstIfLeZ, // const, reg1, 0 + + ExitToConstIfFpTrue, + ExitToConstIfFpFalse, + + Syscall, + SetPC, // hack to make syscall returns work + SetPCConst, // hack to make replacement know PC + CallReplacement, + Break, +}; + +enum IRComparison { + Greater, + GreaterEqual, + Less, + LessEqual, + Equal, + NotEqual, + Bad, +}; + +// Some common vec4 constants. +enum class Vec4Init { + AllZERO, + AllONE, + AllMinusONE, + Set_1000, + Set_0100, + Set_0010, + Set_0001, +}; + +// Hm, unused +inline IRComparison Invert(IRComparison comp) { + switch (comp) { + case IRComparison::Equal: return IRComparison::NotEqual; + case IRComparison::NotEqual: return IRComparison::Equal; + case IRComparison::Greater: return IRComparison::LessEqual; + case IRComparison::GreaterEqual: return IRComparison::Less; + case IRComparison::Less: return IRComparison::GreaterEqual; + case IRComparison::LessEqual: return IRComparison::Greater; + default: + return IRComparison::Bad; + } +} + +inline IROp ComparisonToExit(IRComparison comp) { + switch (comp) { + case IRComparison::Equal: return IROp::ExitToConstIfEq; + case IRComparison::NotEqual: return IROp::ExitToConstIfNeq; + case IRComparison::Greater: return IROp::ExitToConstIfGtZ; + case IRComparison::GreaterEqual: return IROp::ExitToConstIfGeZ; + case IRComparison::Less: return IROp::ExitToConstIfLtZ; + case IRComparison::LessEqual: return IROp::ExitToConstIfLeZ; + default: + return IROp::Break; + } +} + +enum IRFpCompareMode { + False = 0, + NotEqualUnordered, + EqualOrdered, // eq, seq (equal, ordered) + EqualUnordered, // ueq, ngl (equal, unordered) + LessOrdered, // olt, lt (less than, ordered) + LessUnordered, // ult, nge (less than, unordered) + LessEqualOrdered, // ole, le (less equal, ordered) + LessEqualUnordered, // ule, ngt (less equal, unordered) +}; + +enum { + IRTEMP_0 = 192, + IRTEMP_1, + IRTEMP_LHS, // Reserved for use in branches + IRTEMP_RHS, // Reserved for use in branches + + IRVTEMP_PFX_S = 224 - 32, // Relative to the FP regs + IRVTEMP_PFX_T = 228 - 32, + IRVTEMP_PFX_D = 232 - 32, + IRVTEMP_0 = 236 - 32, + + // 16 float temps for vector S and T prefixes and things like that. + // IRVTEMP_0 = 208 - 64, // -64 to be relative to v[0] + + // Hacky way to get to other state + IRREG_VFPU_CTRL_BASE = 208, + IRREG_VFPU_CC = 211, + IRREG_LO = 242, // offset of lo in MIPSState / 4 + IRREG_HI = 243, + IRREG_FCR31 = 244, + IRREG_FPCOND = 245, +}; + +enum IRFlags { + // Uses src3, not dest. + IRFLAG_SRC3 = 0x0001, + // Uses src3 AND dest (i.e. mutates dest.) + IRFLAG_SRC3DST = 0x0002, + // Exit instruction (maybe conditional.) + IRFLAG_EXIT = 0x0004, +}; + +struct IRMeta { + IROp op; + const char *name; + const char types[4]; // GGG + u32 flags; +}; + +// 32 bits. +struct IRInst { + IROp op; + union { + u8 dest; + u8 src3; + }; + u8 src1; + u8 src2; +}; + +// Returns the new PC. +u32 IRInterpret(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count); + +// Each IR block gets a constant pool. +class IRWriter { +public: + IRWriter &operator =(const IRWriter &w) { + insts_ = w.insts_; + constPool_ = w.constPool_; + return *this; + } + IRWriter &operator =(IRWriter &&w) { + insts_ = std::move(w.insts_); + constPool_ = std::move(w.constPool_); + return *this; + } + + void Write(IROp op, u8 dst = 0, u8 src1 = 0, u8 src2 = 0); + void Write(IRInst inst) { + insts_.push_back(inst); + } + void WriteSetConstant(u8 dst, u32 value); + + int AddConstant(u32 value); + int AddConstantFloat(float value); + + void Clear() { + insts_.clear(); + constPool_.clear(); + } + + const std::vector &GetInstructions() const { return insts_; } + const std::vector &GetConstants() const { return constPool_; } + +private: + std::vector insts_; + std::vector constPool_; +}; + +const IRMeta *GetIRMeta(IROp op); +void DisassembleIR(char *buf, size_t bufsize, IRInst inst, const u32 *constPool); +void InitIR(); diff --git a/Core/MIPS/IR/IRInterpreter.cpp b/Core/MIPS/IR/IRInterpreter.cpp new file mode 100644 index 0000000000..c0bcdb91ca --- /dev/null +++ b/Core/MIPS/IR/IRInterpreter.cpp @@ -0,0 +1,638 @@ +#ifdef _M_SSE +#include +#endif + +#include +#include + +#include "Core/MemMap.h" +#include "Core/HLE/HLE.h" +#include "Core/HLE/ReplaceTables.h" +#include "Core/MIPS/MIPSTables.h" +#include "Core/MIPS/MIPSVFPUUtils.h" +#include "Core/System.h" +#include "Core/CoreTiming.h" + +#include "math/math_util.h" +#include "Common/CommonTypes.h" +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/IR/IRInst.h" +#include "Core/MIPS/IR/IRInterpreter.h" + +alignas(16) float vec4InitValues[8][4] = { + { 0.0f, 0.0f, 0.0f, 0.0f }, + { 1.0f, 1.0f, 1.0f, 1.0f }, + { -1.0f, -1.0f, -1.0f, -1.0f }, + { 1.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 1.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 1.0f, 0.0f }, + { 0.0f, 0.0f, 0.0f, 1.0f }, +}; + +u32 IRInterpret(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count) { + const IRInst *end = inst + count; + while (inst != end) { + switch (inst->op) { + case IROp::Nop: + _assert_(false); + break; + case IROp::SetConst: + mips->r[inst->dest] = constPool[inst->src1]; + break; + case IROp::SetConstF: + memcpy(&mips->f[inst->dest], &constPool[inst->src1], 4); + break; + case IROp::Add: + mips->r[inst->dest] = mips->r[inst->src1] + mips->r[inst->src2]; + break; + case IROp::Sub: + mips->r[inst->dest] = mips->r[inst->src1] - mips->r[inst->src2]; + break; + case IROp::And: + mips->r[inst->dest] = mips->r[inst->src1] & mips->r[inst->src2]; + break; + case IROp::Or: + mips->r[inst->dest] = mips->r[inst->src1] | mips->r[inst->src2]; + break; + case IROp::Xor: + mips->r[inst->dest] = mips->r[inst->src1] ^ mips->r[inst->src2]; + break; + case IROp::Mov: + mips->r[inst->dest] = mips->r[inst->src1]; + break; + case IROp::AddConst: + mips->r[inst->dest] = mips->r[inst->src1] + constPool[inst->src2]; + break; + case IROp::SubConst: + mips->r[inst->dest] = mips->r[inst->src1] - constPool[inst->src2]; + break; + case IROp::AndConst: + mips->r[inst->dest] = mips->r[inst->src1] & constPool[inst->src2]; + break; + case IROp::OrConst: + mips->r[inst->dest] = mips->r[inst->src1] | constPool[inst->src2]; + break; + case IROp::XorConst: + mips->r[inst->dest] = mips->r[inst->src1] ^ constPool[inst->src2]; + break; + case IROp::Neg: + mips->r[inst->dest] = -(s32)mips->r[inst->src1]; + break; + case IROp::Not: + mips->r[inst->dest] = ~mips->r[inst->src1]; + break; + case IROp::Ext8to32: + mips->r[inst->dest] = (s32)(s8)mips->r[inst->src1]; + break; + case IROp::Ext16to32: + mips->r[inst->dest] = (s32)(s16)mips->r[inst->src1]; + break; + + case IROp::Load8: + mips->r[inst->dest] = Memory::ReadUnchecked_U8(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load8Ext: + mips->r[inst->dest] = (s32)(s8)Memory::ReadUnchecked_U8(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load16: + mips->r[inst->dest] = Memory::ReadUnchecked_U16(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load16Ext: + mips->r[inst->dest] = (s32)(s16)Memory::ReadUnchecked_U16(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Load32: + mips->r[inst->dest] = Memory::ReadUnchecked_U32(mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::LoadFloat: + mips->f[inst->dest] = Memory::ReadUnchecked_Float(mips->r[inst->src1] + constPool[inst->src2]); + break; + + case IROp::Store8: + Memory::WriteUnchecked_U8(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Store16: + Memory::WriteUnchecked_U16(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::Store32: + Memory::WriteUnchecked_U32(mips->r[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + case IROp::StoreFloat: + Memory::WriteUnchecked_Float(mips->f[inst->src3], mips->r[inst->src1] + constPool[inst->src2]); + break; + + case IROp::LoadVec4: + { + u32 base = mips->r[inst->src1] + constPool[inst->src2]; +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_load_ps((const float *)Memory::GetPointerUnchecked(base))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = Memory::ReadUnchecked_Float(base + 4 * i); +#endif + break; + } + case IROp::StoreVec4: + { + u32 base = mips->r[inst->src1] + constPool[inst->src2]; +#if defined(_M_SSE) + _mm_store_ps((float *)Memory::GetPointerUnchecked(base), _mm_load_ps(&mips->f[inst->dest])); +#else + for (int i = 0; i < 4; i++) + Memory::WriteUnchecked_Float(mips->f[inst->dest + i], base + 4 * i); +#endif + break; + } + + case IROp::Vec4Init: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_load_ps(vec4InitValues[inst->src1])); +#else + memcpy(&mips->f[inst->dest], vec4InitValues[inst->src1], 4 * sizeof(float)); +#endif + break; + + case IROp::Vec4Shuffle: + { + // Can't use the SSE shuffle here because it takes an immediate. + // Backends with SSE support could use that though. + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + ((inst->src2 >> (i * 2)) & 3)]; + break; + } + + case IROp::Vec4Mov: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_load_ps(&mips->f[inst->src1])); +#else + memcpy(&mips->f[inst->dest], &mips->f[inst->src1], 4 * sizeof(float)); +#endif + break; + + case IROp::Vec4Add: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_add_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] + mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Sub: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_sub_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] - mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Mul: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_mul_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] * mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Div: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_div_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_load_ps(&mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] / mips->f[inst->src2 + i]; +#endif + break; + + case IROp::Vec4Scale: +#if defined(_M_SSE) + _mm_store_ps(&mips->f[inst->dest], _mm_mul_ps(_mm_load_ps(&mips->f[inst->src1]), _mm_set1_ps(mips->f[inst->src2]))); +#else + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = mips->f[inst->src1 + i] * mips->f[inst->src2]; +#endif + break; + + case IROp::Vec4Neg: + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = -mips->f[inst->src1 + i]; + break; + + case IROp::Vec4Abs: + for (int i = 0; i < 4; i++) + mips->f[inst->dest + i] = fabsf(mips->f[inst->src1 + i]); + break; + + case IROp::FCmpVfpuBit: + { + int op = inst->dest & 0xF; + int bit = inst->dest >> 4; + int result = 0; + switch (op) { + case VC_EQ: result = mips->f[inst->src1] == mips->f[inst->src2]; break; + case VC_NE: result = mips->f[inst->src1] != mips->f[inst->src2]; break; + case VC_LT: result = mips->f[inst->src1] < mips->f[inst->src2]; break; + case VC_LE: result = mips->f[inst->src1] <= mips->f[inst->src2]; break; + case VC_GT: result = mips->f[inst->src1] > mips->f[inst->src2]; break; + case VC_GE: result = mips->f[inst->src1] >= mips->f[inst->src2]; break; + case VC_EZ: result = mips->f[inst->src1] == 0.0f; break; + case VC_NZ: result = mips->f[inst->src1] != 0.0f; break; + case VC_EN: result = my_isnan(mips->f[inst->src1]); break; + case VC_NN: result = !my_isnan(mips->f[inst->src1]); break; + case VC_EI: result = my_isinf(mips->f[inst->src1]); break; + case VC_NI: result = !my_isinf(mips->f[inst->src1]); break; + case VC_ES: result = my_isnanorinf(mips->f[inst->src1]); break; + case VC_NS: result = !my_isnanorinf(mips->f[inst->src1]); break; + case VC_TR: result = 1; break; + case VC_FL: result = 0; break; + default: + result = 0; + } + if (result != 0) { + mips->vfpuCtrl[VFPU_CTRL_CC] |= (1 << bit); + } else { + mips->vfpuCtrl[VFPU_CTRL_CC] &= ~(1 << bit); + } + } + break; + + case IROp::FCmpVfpuAggregate: + { + u32 mask = inst->dest; + u32 cc = mips->vfpuCtrl[VFPU_CTRL_CC]; + int a = (cc & mask) ? 0x10 : 0x00; + int b = (cc & mask) == mask ? 0x20 : 0x00; + mips->vfpuCtrl[VFPU_CTRL_CC] = (cc & ~0x30) | a | b; + } + break; + + case IROp::FCmovVfpuCC: + if (((mips->vfpuCtrl[VFPU_CTRL_CC] >> (inst->src2 & 0xf)) & 1) == (inst->src2 >> 7)) { + mips->f[inst->dest] = mips->f[inst->src1]; + } + break; + + // Not quickly implementable on all platforms, unfortunately. + case IROp::Vec4Dot: + { + float dot = mips->f[inst->src1] * mips->f[inst->src2]; + for (int i = 1; i < 4; i++) + dot += mips->f[inst->src1 + i] * mips->f[inst->src2 + i]; + mips->f[inst->dest] = dot; + break; + } + + case IROp::FSin: + mips->f[inst->dest] = vfpu_sin(mips->f[inst->src1]); + break; + case IROp::FCos: + mips->f[inst->dest] = vfpu_cos(mips->f[inst->src1]); + break; + case IROp::FRSqrt: + mips->f[inst->dest] = 1.0f / sqrtf(mips->f[inst->src1]); + break; + case IROp::FRecip: + mips->f[inst->dest] = 1.0f / mips->f[inst->src1]; + break; + case IROp::FAsin: + mips->f[inst->dest] = vfpu_asin(mips->f[inst->src1]); + break; + + case IROp::ShlImm: + mips->r[inst->dest] = mips->r[inst->src1] << (int)inst->src2; + break; + case IROp::ShrImm: + mips->r[inst->dest] = mips->r[inst->src1] >> (int)inst->src2; + break; + case IROp::SarImm: + mips->r[inst->dest] = (s32)mips->r[inst->src1] >> (int)inst->src2; + break; + case IROp::RorImm: + { + u32 x = mips->r[inst->src1]; + int sa = inst->src2; + mips->r[inst->dest] = (x >> sa) | (x << (32 - sa)); + } + break; + + case IROp::Shl: + mips->r[inst->dest] = mips->r[inst->src1] << (mips->r[inst->src2] & 31); + break; + case IROp::Shr: + mips->r[inst->dest] = mips->r[inst->src1] >> (mips->r[inst->src2] & 31); + break; + case IROp::Sar: + mips->r[inst->dest] = (s32)mips->r[inst->src1] >> (mips->r[inst->src2] & 31); + break; + case IROp::Ror: + { + u32 x = mips->r[inst->src1]; + int sa = mips->r[inst->src2] & 31; + mips->r[inst->dest] = (x >> sa) | (x << (32 - sa)); + } + break; + + case IROp::Clz: + { + int x = 31; + int count = 0; + int value = mips->r[inst->src1]; + while (x >= 0 && !(value & (1 << x))) { + count++; + x--; + } + mips->r[inst->dest] = count; + break; + } + + case IROp::Slt: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)mips->r[inst->src2]; + break; + + case IROp::SltU: + mips->r[inst->dest] = mips->r[inst->src1] < mips->r[inst->src2]; + break; + + case IROp::SltConst: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)constPool[inst->src2]; + break; + + case IROp::SltUConst: + mips->r[inst->dest] = mips->r[inst->src1] < constPool[inst->src2]; + break; + + case IROp::MovZ: + if (mips->r[inst->src1] == 0) + mips->r[inst->dest] = mips->r[inst->src2]; + break; + case IROp::MovNZ: + if (mips->r[inst->src1] != 0) + mips->r[inst->dest] = mips->r[inst->src2]; + break; + + case IROp::Max: + mips->r[inst->dest] = (s32)mips->r[inst->src1] > (s32)mips->r[inst->src2] ? mips->r[inst->src1] : mips->r[inst->src2]; + break; + case IROp::Min: + mips->r[inst->dest] = (s32)mips->r[inst->src1] < (s32)mips->r[inst->src2] ? mips->r[inst->src1] : mips->r[inst->src2]; + break; + + case IROp::MtLo: + mips->lo = mips->r[inst->src1]; + break; + case IROp::MtHi: + mips->hi = mips->r[inst->src1]; + break; + case IROp::MfLo: + mips->r[inst->dest] = mips->lo; + break; + case IROp::MfHi: + mips->r[inst->dest] = mips->hi; + break; + + case IROp::Mult: + { + s64 result = (s64)(s32)mips->r[inst->src1] * (s64)(s32)mips->r[inst->src2]; + memcpy(&mips->lo, &result, 8); + break; + } + case IROp::MultU: + { + u64 result = (u64)mips->r[inst->src1] * (u64)mips->r[inst->src2]; + memcpy(&mips->lo, &result, 8); + break; + } + + case IROp::BSwap16: + { + u32 x = mips->r[inst->src1]; + mips->r[inst->dest] = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8); + break; + } + case IROp::BSwap32: + { + u32 x = mips->r[inst->src1]; + mips->r[inst->dest] = ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24); + break; + } + + case IROp::FAdd: + mips->f[inst->dest] = mips->f[inst->src1] + mips->f[inst->src2]; + break; + case IROp::FSub: + mips->f[inst->dest] = mips->f[inst->src1] - mips->f[inst->src2]; + break; + case IROp::FMul: + mips->f[inst->dest] = mips->f[inst->src1] * mips->f[inst->src2]; + break; + case IROp::FDiv: + mips->f[inst->dest] = mips->f[inst->src1] / mips->f[inst->src2]; + break; + case IROp::FMin: + mips->f[inst->dest] = std::min(mips->f[inst->src1], mips->f[inst->src2]); + break; + case IROp::FMax: + mips->f[inst->dest] = std::max(mips->f[inst->src1], mips->f[inst->src2]); + break; + + case IROp::FMov: + mips->f[inst->dest] = mips->f[inst->src1]; + break; + case IROp::FAbs: + mips->f[inst->dest] = fabsf(mips->f[inst->src1]); + break; + case IROp::FSqrt: + mips->f[inst->dest] = sqrtf(mips->f[inst->src1]); + break; + case IROp::FNeg: + mips->f[inst->dest] = -mips->f[inst->src1]; + break; + case IROp::FSat0_1: + mips->f[inst->dest] = clamp_value(mips->f[inst->src1], 0.0f, 1.0f); + break; + case IROp::FSatMinus1_1: + mips->f[inst->dest] = clamp_value(mips->f[inst->src1], -1.0f, 1.0f); + break; + + case IROp::FpCondToReg: + mips->r[inst->dest] = mips->fpcond; + break; + case IROp::VfpuCtrlToReg: + mips->r[inst->dest] = mips->vfpuCtrl[inst->src1]; + break; + case IROp::FRound: + mips->fs[inst->dest] = (int)floorf(mips->f[inst->src1] + 0.5f); + break; + case IROp::FTrunc: + { + float src = mips->f[inst->src1]; + if (src >= 0.0f) { + mips->fs[inst->dest] = (int)floorf(src); + // Overflow, but it was positive. + if (mips->fs[inst->dest] == -2147483648LL) { + mips->fs[inst->dest] = 2147483647LL; + } + } else { + // Overflow happens to be the right value anyway. + mips->fs[inst->dest] = (int)ceilf(src); + } + break; + } + case IROp::FCeil: + mips->fs[inst->dest] = (int)ceilf(mips->f[inst->src1]); + break; + case IROp::FFloor: + mips->fs[inst->dest] = (int)floorf(mips->f[inst->src1]); + break; + case IROp::FCmp: + switch (inst->dest) { + case IRFpCompareMode::False: + mips->fpcond = 0; + break; + case IRFpCompareMode::EqualOrdered: + case IRFpCompareMode::EqualUnordered: + mips->fpcond = mips->f[inst->src1] == mips->f[inst->src2]; + break; + case IRFpCompareMode::LessEqualOrdered: + case IRFpCompareMode::LessEqualUnordered: + mips->fpcond = mips->f[inst->src1] <= mips->f[inst->src2]; + break; + case IRFpCompareMode::LessOrdered: + case IRFpCompareMode::LessUnordered: + mips->fpcond = mips->f[inst->src1] < mips->f[inst->src2]; + break; + } + break; + + case IROp::FCvtSW: + mips->f[inst->dest] = (float)mips->fs[inst->src1]; + break; + case IROp::FCvtWS: + { + float src = mips->f[inst->src1]; + if (my_isnanorinf(src)) + { + mips->fs[inst->dest] = my_isinf(src) && src < 0.0f ? -2147483648LL : 2147483647LL; + break; + } + switch (mips->fcr31 & 3) + { + case 0: mips->fs[inst->dest] = (int)round_ieee_754(src); break; // RINT_0 + case 1: mips->fs[inst->dest] = (int)src; break; // CAST_1 + case 2: mips->fs[inst->dest] = (int)ceilf(src); break; // CEIL_2 + case 3: mips->fs[inst->dest] = (int)floorf(src); break; // FLOOR_3 + } + break; //cvt.w.s + } + + case IROp::ZeroFpCond: + mips->fpcond = 0; + break; + + case IROp::FMovFromGPR: + memcpy(&mips->f[inst->dest], &mips->r[inst->src1], 4); + break; + case IROp::FMovToGPR: + memcpy(&mips->r[inst->dest], &mips->f[inst->src1], 4); + break; + + case IROp::ExitToConst: + return constPool[inst->dest]; + + case IROp::ExitToReg: + return mips->r[inst->src1]; + + case IROp::ExitToConstIfEq: + if (mips->r[inst->src1] == mips->r[inst->src2]) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfNeq: + if (mips->r[inst->src1] != mips->r[inst->src2]) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfGtZ: + if ((s32)mips->r[inst->src1] > 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfGeZ: + if ((s32)mips->r[inst->src1] >= 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfLtZ: + if ((s32)mips->r[inst->src1] < 0) + return constPool[inst->dest]; + break; + case IROp::ExitToConstIfLeZ: + if ((s32)mips->r[inst->src1] <= 0) + return constPool[inst->dest]; + break; + + case IROp::Downcount: + mips->downcount -= (inst->src1) | ((inst->src2) << 8); + break; + + case IROp::SetPC: + mips->pc = mips->r[inst->src1]; + break; + + case IROp::SetPCConst: + mips->pc = constPool[inst->src1]; + break; + + case IROp::Syscall: + // SetPC was executed before. + { + MIPSOpcode op(constPool[inst->src1]); + CallSyscall(op); + if (coreState != CORE_RUNNING) + CoreTiming::ForceCheck(); + return mips->pc; + } + + case IROp::Interpret: // SLOW fallback. Can be made faster. + { + MIPSOpcode op(constPool[inst->src1]); + MIPSInterpret(op); + break; + } + + case IROp::CallReplacement: + { + int funcIndex = constPool[inst->src1]; + const ReplacementTableEntry *f = GetReplacementFunc(funcIndex); + int cycles = f->replaceFunc(); + mips->downcount -= cycles; + break; + } + + case IROp::Break: + Crash(); + break; + + case IROp::SetCtrlVFPU: + mips->vfpuCtrl[inst->dest] = constPool[inst->src1]; + break; + + case IROp::SetCtrlVFPUReg: + mips->vfpuCtrl[inst->dest] = mips->r[inst->src1]; + break; + + case IROp::SetCtrlVFPUFReg: + memcpy(&mips->vfpuCtrl[inst->dest], &mips->f[inst->src1], 4); + break; + + default: + Crash(); + } +#ifdef _DEBUG + if (mips->r[0] != 0) + Crash(); +#endif + inst++; + } + + // If we got here, the block was badly constructed. + Crash(); + return 0; +} diff --git a/Core/MIPS/IR/IRInterpreter.h b/Core/MIPS/IR/IRInterpreter.h new file mode 100644 index 0000000000..fe9f8e4ba9 --- /dev/null +++ b/Core/MIPS/IR/IRInterpreter.h @@ -0,0 +1,8 @@ +#pragma once + +#include "Common/CommonTypes.h" + +class MIPSState; +struct IRInst; + +u32 IRInterpret(MIPSState *mips, const IRInst *inst, const u32 *constPool, int count); diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp new file mode 100644 index 0000000000..fb49026855 --- /dev/null +++ b/Core/MIPS/IR/IRJit.cpp @@ -0,0 +1,170 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 "base/logging.h" +#include "profiler/profiler.h" +#include "Common/ChunkFile.h" +#include "Common/StringUtils.h" + +#include "Core/Reporting.h" +#include "Core/Config.h" +#include "Core/Core.h" +#include "Core/CoreTiming.h" +#include "Core/Debugger/SymbolMap.h" +#include "Core/MemMap.h" + +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSInt.h" +#include "Core/MIPS/MIPSTables.h" +#include "Core/HLE/sceKernelMemory.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/IR/IRPassSimplify.h" +#include "Core/MIPS/IR/IRInterpreter.h" +#include "Core/MIPS/JitCommon/JitCommon.h" + +namespace MIPSComp { + +IRJit::IRJit(MIPSState *mips) : mips_(mips), frontend_(mips->HasDefaultPrefix()) { + u32 size = 128 * 1024; + // blTrampolines_ = kernelMemory.Alloc(size, true, "trampoline"); + InitIR(); +} + +IRJit::~IRJit() { +} + +void IRJit::DoState(PointerWrap &p) { + frontend_.DoState(p); +} + +// This is here so the savestate matches between jit and non-jit. +void IRJit::DoDummyState(PointerWrap &p) { + auto s = p.Section("Jit", 1, 2); + if (!s) + return; + + bool dummy = false; + p.Do(dummy); + if (s >= 2) { + dummy = true; + p.Do(dummy); + } +} + +void IRJit::ClearCache() { + ILOG("IRJit: Clearing the cache!"); + blocks_.Clear(); +} + +void IRJit::InvalidateCache() { + blocks_.Clear(); +} + +void IRJit::InvalidateCacheAt(u32 em_address, int length) { + blocks_.InvalidateICache(em_address, length); +} + +void IRJit::Compile(u32 em_address) { + PROFILE_THIS_SCOPE("jitc"); + + int block_num = blocks_.AllocateBlock(em_address); + IRBlock *b = blocks_.GetBlock(block_num); + + std::vector instructions; + std::vector constants; + frontend_.DoJit(em_address, instructions, constants); + b->SetInstructions(instructions, constants); + b->Finalize(block_num); // Overwrites the first instruction + + if (frontend_.CheckRounding()) { + // Our assumptions are all wrong so it's clean-slate time. + ClearCache(); + Compile(em_address); + } +} + +void IRJit::RunLoopUntil(u64 globalticks) { + PROFILE_THIS_SCOPE("jit"); + + // ApplyRoundingMode(true); + // IR Dispatcher + + while (true) { + // RestoreRoundingMode(true); + CoreTiming::Advance(); + // ApplyRoundingMode(true); + if (coreState != 0) { + break; + } + while (mips_->downcount >= 0) { + u32 inst = Memory::ReadUnchecked_U32(mips_->pc); + u32 opcode = inst & 0xFF000000; + if (opcode == MIPS_EMUHACK_OPCODE) { + u32 data = inst & 0xFFFFFF; + IRBlock *block = blocks_.GetBlock(data); + mips_->pc = IRInterpret(mips_, block->GetInstructions(), block->GetConstants(), block->GetNumInstructions()); + } else { + // RestoreRoundingMode(true); + Compile(mips_->pc); + // ApplyRoundingMode(true); + } + } + } + + // RestoreRoundingMode(true); +} + +bool IRJit::DescribeCodePtr(const u8 *ptr, std::string &name) { + // Used in disassembly viewer. + return false; +} + +void IRJit::LinkBlock(u8 *exitPoint, const u8 *checkedEntry) { + Crash(); +} + +void IRJit::UnlinkBlock(u8 *checkedEntry, u32 originalAddress) { + Crash(); +} + +bool IRJit::ReplaceJalTo(u32 dest) { + Crash(); + return false; +} + +void IRBlockCache::Clear() { + blocks_.clear(); +} + +void IRBlockCache::InvalidateICache(u32 addess, u32 length) { + // TODO +} + +void IRBlock::Finalize(int number) { + origFirstOpcode_ = Memory::Read_Opcode_JIT(origAddr_); + MIPSOpcode opcode = MIPSOpcode(MIPS_EMUHACK_OPCODE | number); + Memory::Write_Opcode_JIT(origAddr_, opcode); +} + +MIPSOpcode IRJit::GetOriginalOp(MIPSOpcode op) { + IRBlock *b = blocks_.GetBlock(op.encoding & 0xFFFFFF); + return b->GetOriginalFirstOp(); +} + +} // namespace MIPSComp diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h new file mode 100644 index 0000000000..38f0df6a3e --- /dev/null +++ b/Core/MIPS/IR/IRJit.h @@ -0,0 +1,178 @@ +// 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 +// the Free Software Foundation, version 2.0 or later versions. + +// 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 + +#include + +#include "Common/Common.h" +#include "Common/CPUDetect.h" +#include "Core/MIPS/JitCommon/JitBlockCache.h" +#include "Core/MIPS/JitCommon/JitCommon.h" +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRInst.h" +#include "Core/MIPS/IR/IRFrontend.h" +#include "Core/MIPS/MIPSVFPUUtils.h" + +#ifndef offsetof +#include "stddef.h" +#endif + +namespace MIPSComp { + +// TODO : Use arena allocators. For now let's just malloc. +class IRBlock { +public: + IRBlock() : instr_(nullptr), const_(nullptr), numInstructions_(0), numConstants_(0), origAddr_(0) {} + IRBlock(u32 emAddr) : instr_(nullptr), const_(nullptr), origAddr_(emAddr), numInstructions_(0) {} + IRBlock(IRBlock &&b) { + instr_ = b.instr_; + const_ = b.const_; + numInstructions_ = b.numInstructions_; + numConstants_ = b.numConstants_; + origAddr_ = b.origAddr_; + origFirstOpcode_ = b.origFirstOpcode_; + b.instr_ = nullptr; + b.const_ = nullptr; + } + + IRBlock(const IRBlock &b) { + *this = b; + } + + IRBlock &operator=(const IRBlock &b) { + // No std::move on Symbian... But let's try not to use elsewhere. +#ifndef __SYMBIAN32__ + _assert_(false); +#endif + numInstructions_ = b.numInstructions_; + numConstants_ = b.numConstants_; + instr_ = new IRInst[numInstructions_]; + if (numInstructions_) { + memcpy(instr_, b.instr_, sizeof(IRInst) * numInstructions_); + } + const_ = new u32[numConstants_]; + if (numConstants_) { + memcpy(const_, b.const_, sizeof(u32) * numConstants_); + } + origAddr_ = b.origAddr_; + origFirstOpcode_ = b.origFirstOpcode_; + + return *this; + } + + ~IRBlock() { + delete[] instr_; + delete[] const_; + } + + void SetInstructions(const std::vector &inst, const std::vector &constants) { + instr_ = new IRInst[inst.size()]; + numInstructions_ = (u16)inst.size(); + if (!inst.empty()) { + memcpy(instr_, &inst[0], sizeof(IRInst) * inst.size()); + } + const_ = new u32[constants.size()]; + numConstants_ = (u16)constants.size(); + if (!constants.empty()) { + memcpy(const_, &constants[0], sizeof(u32) * constants.size()); + } + } + + const IRInst *GetInstructions() const { return instr_; } + const u32 *GetConstants() const { return const_; } + int GetNumInstructions() const { return numInstructions_; } + MIPSOpcode GetOriginalFirstOp() const { return origFirstOpcode_; } + + void Finalize(int number); + +private: + IRInst *instr_; + u32 *const_; + u16 numInstructions_; + u16 numConstants_; + u32 origAddr_; + MIPSOpcode origFirstOpcode_; +}; + +class IRBlockCache { +public: + void Clear(); + void InvalidateICache(u32 addess, u32 length); + int GetNumBlocks() const { return (int)blocks_.size(); } + int AllocateBlock(int emAddr) { + blocks_.push_back(IRBlock(emAddr)); + size_ = (int)blocks_.size(); + return (int)blocks_.size() - 1; + } + IRBlock *GetBlock(int i) { + if (i >= 0 && i < size_) { + return &blocks_[i]; + } else { + return nullptr; + } + } +private: + int size_; + std::vector blocks_; +}; + +class IRJit : public JitInterface { +public: + IRJit(MIPSState *mips); + virtual ~IRJit(); + + void DoState(PointerWrap &p) override; + void DoDummyState(PointerWrap &p) override; + + const JitOptions &GetJitOptions() { return jo; } + + void RunLoopUntil(u64 globalticks) override; + + void Compile(u32 em_address) override; // Compiles a block at current MIPS PC + + bool DescribeCodePtr(const u8 *ptr, std::string &name) override; + // Not using a regular block cache. + JitBlockCache *GetBlockCache() override { return nullptr; } + MIPSOpcode GetOriginalOp(MIPSOpcode op) override; + + void ClearCache(); + void InvalidateCache(); + void InvalidateCacheAt(u32 em_address, int length = 4); + + const u8 *GetDispatcher() const override { return nullptr; } + + void LinkBlock(u8 *exitPoint, const u8 *checkedEntry) override; + void UnlinkBlock(u8 *checkedEntry, u32 originalAddress) override; + +private: + bool ReplaceJalTo(u32 dest); + + JitOptions jo; + + IRFrontend frontend_; + IRBlockCache blocks_; + + MIPSState *mips_; + + // where to write branch-likely trampolines. not used atm + // u32 blTrampolines_; + // int blTrampolineCount_; +}; + +} // namespace MIPSComp + diff --git a/Core/MIPS/IR/IRPassSimplify.cpp b/Core/MIPS/IR/IRPassSimplify.cpp new file mode 100644 index 0000000000..6b29efc242 --- /dev/null +++ b/Core/MIPS/IR/IRPassSimplify.cpp @@ -0,0 +1,716 @@ +#include + +#include "Common/Log.h" +#include "Core/MIPS/IR/IRPassSimplify.h" +#include "Core/MIPS/IR/IRRegCache.h" + +void WriteInstWithConstants(const IRWriter &in, IRWriter &out, const u32 *constants, IRInst inst) { + // Remap constants to the new reality + const IRMeta *m = GetIRMeta(inst.op); + switch (m->types[0]) { + case 'C': + inst.dest = out.AddConstant(constants[inst.dest]); + break; + } + switch (m->types[1]) { + case 'C': + inst.src1 = out.AddConstant(constants[inst.src1]); + break; + } + switch (m->types[2]) { + case 'C': + inst.src2 = out.AddConstant(constants[inst.src2]); + break; + } + out.Write(inst); +} + +u32 Evaluate(u32 a, u32 b, IROp op) { + switch (op) { + case IROp::Add: case IROp::AddConst: return a + b; + case IROp::Sub: case IROp::SubConst: return a - b; + case IROp::And: case IROp::AndConst: return a & b; + case IROp::Or: case IROp::OrConst: return a | b; + case IROp::Xor: case IROp::XorConst: return a ^ b; + case IROp::Shr: case IROp::ShrImm: return a >> b; + case IROp::Sar: case IROp::SarImm: return (s32)a >> b; + case IROp::Ror: case IROp::RorImm: return (a >> b) | (a << (32 - b)); + case IROp::Shl: case IROp::ShlImm: return a << b; + case IROp::Slt: case IROp::SltConst: return ((s32)a < (s32)b); + case IROp::SltU: case IROp::SltUConst: return (a < b); + default: + return -1; + } +} + +u32 Evaluate(u32 a, IROp op) { + switch (op) { + case IROp::Not: return ~a; + case IROp::Neg: return -(s32)a; + case IROp::BSwap16: return ((a & 0xFF00FF00) >> 8) | ((a & 0x00FF00FF) << 8); + case IROp::BSwap32: return swap32(a); + case IROp::Ext8to32: return (u32)(s32)(s8)(u8)a; + case IROp::Ext16to32: return (u32)(s32)(s16)(u16)a; + case IROp::Clz: { + int x = 31; + int count = 0; + while (x >= 0 && !(a & (1 << x))) { + count++; + x--; + } + return count; + } + default: + return -1; + } +} + +IROp ArithToArithConst(IROp op) { + switch (op) { + case IROp::Add: return IROp::AddConst; + case IROp::Sub: return IROp::SubConst; + case IROp::And: return IROp::AndConst; + case IROp::Or: return IROp::OrConst; + case IROp::Xor: return IROp::XorConst; + case IROp::Slt: return IROp::SltConst; + case IROp::SltU: return IROp::SltUConst; + default: + return (IROp)-1; + } +} + +IROp ShiftToShiftImm(IROp op) { + switch (op) { + case IROp::Shl: return IROp::ShlImm; + case IROp::Shr: return IROp::ShrImm; + case IROp::Ror: return IROp::RorImm; + case IROp::Sar: return IROp::SarImm; + default: + return (IROp)-1; + } +} + +bool IRApplyPasses(const IRPassFunc *passes, size_t c, const IRWriter &in, IRWriter &out) { + if (c == 1) { + return passes[0](in, out); + } + + bool logBlocks = false; + + IRWriter temp[2]; + const IRWriter *nextIn = ∈ + IRWriter *nextOut = &temp[1]; + for (size_t i = 0; i < c - 1; ++i) { + if (passes[i](*nextIn, *nextOut)) { + logBlocks = true; + } + + temp[0] = std::move(temp[1]); + nextIn = &temp[0]; + } + + if (passes[c - 1](*nextIn, out)) { + logBlocks = true; + } + + return logBlocks; +} + +bool OptimizeFPMoves(const IRWriter &in, IRWriter &out) { + const u32 *constants = in.GetConstants().data(); + bool logBlocks = false; + IRInst prev; + prev.op = IROp::Nop; + prev.dest = 0; + prev.src1 = 0; + prev.src2 = 0; + for (int i = 0; i < (int)in.GetInstructions().size(); i++) { + IRInst inst = in.GetInstructions()[i]; + switch (inst.op) { + case IROp::FMovFromGPR: + //FMovToGPR a0, f12 + //FMovFromGPR f14, a0 + // to + //FMovToGPR a0, f12 + //FMov f14, f12 + if (prev.op == IROp::FMovToGPR && prev.dest == inst.src1) { + inst.op = IROp::FMov; + inst.src1 = prev.src1; + out.Write(inst); + } else { + out.Write(inst); + } + break; + + // This will need to scan forward or keep track of more information to be useful. + // Just doing one isn't. + /* + case IROp::LoadVec4: + // AddConst a0, sp, 0x30 + // LoadVec4 v16, a0, 0x0 + // to + // AddConst a0, sp, 0x30 + // LoadVec4 v16, sp, 0x30 + if (prev.op == IROp::AddConst && prev.dest == inst.src1 && prev.dest != prev.src1 && prev.src1 == MIPS_REG_SP) { + inst.src2 = out.AddConstant(constants[prev.src2] + constants[inst.src2]); + inst.src1 = prev.src1; + logBlocks = 1; + } else { + goto doDefault; + } + out.Write(inst); + break; + */ + default: + WriteInstWithConstants(in, out, constants, inst); + break; + } + prev = inst; + } + return logBlocks; +} + +// Might be useful later on x86. +bool ThreeOpToTwoOp(const IRWriter &in, IRWriter &out) { + bool logBlocks = false; + for (int i = 0; i < (int)in.GetInstructions().size(); i++) { + IRInst inst = in.GetInstructions()[i]; + const IRMeta *meta = GetIRMeta(inst.op); + switch (inst.op) { + case IROp::Sub: + case IROp::Slt: + case IROp::SltU: + case IROp::Add: + case IROp::And: + case IROp::Or: + case IROp::Xor: + if (inst.src1 != inst.dest && inst.src2 != inst.dest) { + out.Write(IROp::Mov, inst.dest, inst.src1); + out.Write(inst.op, inst.dest, inst.dest, inst.src2); + } else { + out.Write(inst); + } + break; + case IROp::FMul: + case IROp::FAdd: + if (inst.src1 != inst.dest && inst.src2 != inst.dest) { + out.Write(IROp::FMov, inst.dest, inst.src1); + out.Write(inst.op, inst.dest, inst.dest, inst.src2); + } else { + out.Write(inst); + } + break; + default: + out.Write(inst); + break; + } + } + // Can reuse the old constants array - not touching constants in this pass. + for (u32 value : in.GetConstants()) { + out.AddConstant(value); + } + return logBlocks; +} + +bool PropagateConstants(const IRWriter &in, IRWriter &out) { + IRRegCache gpr(&out); + + const u32 *constants = &in.GetConstants()[0]; + bool logBlocks = false; + for (int i = 0; i < (int)in.GetInstructions().size(); i++) { + IRInst inst = in.GetInstructions()[i]; + bool symmetric = true; + if (out.GetConstants().size() > 128) { + // Avoid causing a constant explosion. + goto doDefaultAndFlush; + } + switch (inst.op) { + case IROp::SetConst: + gpr.SetImm(inst.dest, constants[inst.src1]); + break; + case IROp::SetConstF: + goto doDefault; + + case IROp::Sub: + case IROp::Slt: + case IROp::SltU: + symmetric = false; // fallthrough + case IROp::Add: + case IROp::And: + case IROp::Or: + case IROp::Xor: + // Regularize, for the add/or check below. + if (symmetric && inst.src2 == inst.dest && inst.src1 != inst.src2) { + std::swap(inst.src1, inst.src2); + } + if (gpr.IsImm(inst.src1) && gpr.IsImm(inst.src2)) { + gpr.SetImm(inst.dest, Evaluate(gpr.GetImm(inst.src1), gpr.GetImm(inst.src2), inst.op)); + } else if (gpr.IsImm(inst.src2)) { + const u32 imm2 = gpr.GetImm(inst.src2); + gpr.MapDirtyIn(inst.dest, inst.src1); + if (imm2 == 0 && (inst.op == IROp::Add || inst.op == IROp::Or)) { + // Add / Or with zero is just a Mov. + if (inst.dest != inst.src1) + out.Write(IROp::Mov, inst.dest, inst.src1); + } else { + out.Write(ArithToArithConst(inst.op), inst.dest, inst.src1, out.AddConstant(imm2)); + } + } else if (symmetric && gpr.IsImm(inst.src1)) { + const u32 imm1 = gpr.GetImm(inst.src1); + gpr.MapDirtyIn(inst.dest, inst.src2); + if (imm1 == 0 && (inst.op == IROp::Add || inst.op == IROp::Or)) { + // Add / Or with zero is just a Mov. + if (inst.dest != inst.src2) + out.Write(IROp::Mov, inst.dest, inst.src2); + } else { + out.Write(ArithToArithConst(inst.op), inst.dest, inst.src2, out.AddConstant(imm1)); + } + } else { + gpr.MapDirtyInIn(inst.dest, inst.src1, inst.src2); + goto doDefault; + } + break; + + case IROp::Neg: + case IROp::Not: + case IROp::BSwap16: + case IROp::BSwap32: + case IROp::Ext8to32: + case IROp::Ext16to32: + case IROp::Clz: + if (gpr.IsImm(inst.src1)) { + gpr.SetImm(inst.dest, Evaluate(gpr.GetImm(inst.src1), inst.op)); + } else { + gpr.MapDirtyIn(inst.dest, inst.src1); + goto doDefault; + } + break; + + case IROp::AddConst: + case IROp::SubConst: + case IROp::AndConst: + case IROp::OrConst: + case IROp::XorConst: + case IROp::SltConst: + case IROp::SltUConst: + if (gpr.IsImm(inst.src1)) { + gpr.SetImm(inst.dest, Evaluate(gpr.GetImm(inst.src1), constants[inst.src2], inst.op)); + } else { + gpr.MapDirtyIn(inst.dest, inst.src1); + goto doDefault; + } + break; + + case IROp::Shl: + case IROp::Shr: + case IROp::Ror: + case IROp::Sar: + if (gpr.IsImm(inst.src1) && gpr.IsImm(inst.src2)) { + gpr.SetImm(inst.dest, Evaluate(gpr.GetImm(inst.src1), gpr.GetImm(inst.src2), inst.op)); + } else if (gpr.IsImm(inst.src2)) { + const u8 sa = gpr.GetImm(inst.src2) & 31; + gpr.MapDirtyIn(inst.dest, inst.src1); + if (sa == 0) { + if (inst.dest != inst.src1) + out.Write(IROp::Mov, inst.dest, inst.src1); + } else { + out.Write(ShiftToShiftImm(inst.op), inst.dest, inst.src1, sa); + } + } else { + gpr.MapDirtyInIn(inst.dest, inst.src1, inst.src2); + goto doDefault; + } + break; + + case IROp::ShlImm: + case IROp::ShrImm: + case IROp::RorImm: + case IROp::SarImm: + if (gpr.IsImm(inst.src1)) { + gpr.SetImm(inst.dest, Evaluate(gpr.GetImm(inst.src1), inst.src2, inst.op)); + } else { + gpr.MapDirtyIn(inst.dest, inst.src1); + goto doDefault; + } + break; + + case IROp::Mov: + if (inst.dest == inst.src1) { + // Nop + } else if (gpr.IsImm(inst.src1)) { + gpr.SetImm(inst.dest, gpr.GetImm(inst.src1)); + } else { + gpr.MapDirtyIn(inst.dest, inst.src1); + goto doDefault; + } + break; + + case IROp::Mult: + case IROp::MultU: + gpr.MapInIn(inst.src1, inst.src2); + goto doDefault; + + case IROp::MovZ: + case IROp::MovNZ: + gpr.MapInInIn(inst.dest, inst.src1, inst.src2); + goto doDefault; + + case IROp::Min: + case IROp::Max: + gpr.MapDirtyInIn(inst.dest, inst.src1, inst.src2); + goto doDefault; + + case IROp::FMovFromGPR: + if (gpr.IsImm(inst.src1)) { + out.Write(IROp::SetConstF, inst.dest, out.AddConstant(gpr.GetImm(inst.src1))); + } else { + gpr.MapIn(inst.src1); + goto doDefault; + } + break; + + case IROp::FMovToGPR: + gpr.MapDirty(inst.dest); + goto doDefault; + + case IROp::MfHi: + case IROp::MfLo: + gpr.MapDirty(inst.dest); + goto doDefault; + + case IROp::MtHi: + case IROp::MtLo: + gpr.MapIn(inst.src1); + goto doDefault; + + case IROp::Store8: + case IROp::Store16: + case IROp::Store32: + if (gpr.IsImm(inst.src1) && inst.src1 != inst.dest) { + gpr.MapIn(inst.dest); + out.Write(inst.op, inst.dest, 0, out.AddConstant(gpr.GetImm(inst.src1) + constants[inst.src2])); + } else { + gpr.MapInIn(inst.dest, inst.src1); + goto doDefault; + } + break; + case IROp::StoreFloat: + case IROp::StoreVec4: + if (gpr.IsImm(inst.src1)) { + out.Write(inst.op, inst.dest, 0, out.AddConstant(gpr.GetImm(inst.src1) + constants[inst.src2])); + } else { + gpr.MapIn(inst.src1); + goto doDefault; + } + break; + + case IROp::Load8: + case IROp::Load8Ext: + case IROp::Load16: + case IROp::Load16Ext: + case IROp::Load32: + if (gpr.IsImm(inst.src1) && inst.src1 != inst.dest) { + gpr.MapDirty(inst.dest); + out.Write(inst.op, inst.dest, 0, out.AddConstant(gpr.GetImm(inst.src1) + constants[inst.src2])); + } else { + gpr.MapDirtyIn(inst.dest, inst.src1); + goto doDefault; + } + break; + case IROp::LoadFloat: + case IROp::LoadVec4: + if (gpr.IsImm(inst.src1)) { + out.Write(inst.op, inst.dest, 0, out.AddConstant(gpr.GetImm(inst.src1) + constants[inst.src2])); + } else { + gpr.MapIn(inst.src1); + goto doDefault; + } + break; + + case IROp::Downcount: + case IROp::SetPCConst: + goto doDefault; + + case IROp::SetPC: + if (gpr.IsImm(inst.src1)) { + out.Write(IROp::SetPCConst, out.AddConstant(gpr.GetImm(inst.src1))); + } else { + gpr.MapIn(inst.src1); + goto doDefault; + } + break; + + // FP-only instructions don't need to flush immediates. + case IROp::FAdd: + case IROp::FMul: + // Regularize, to help x86 backends (add.s r0, r1, r0 -> add.s r0, r0, r1) + if (inst.src2 == inst.dest && inst.src1 != inst.src2) + std::swap(inst.src1, inst.src2); + out.Write(inst); + break; + + case IROp::FSub: + case IROp::FDiv: + case IROp::FNeg: + case IROp::FAbs: + case IROp::FMov: + case IROp::FRound: + case IROp::FTrunc: + case IROp::FCeil: + case IROp::FFloor: + case IROp::FCvtSW: + case IROp::FSin: + case IROp::FCos: + case IROp::FSqrt: + case IROp::FRSqrt: + case IROp::FRecip: + case IROp::FAsin: + out.Write(inst); + break; + + case IROp::SetCtrlVFPU: + goto doDefault; + + case IROp::FCvtWS: + // TODO: Actually, this should just use the currently set rounding mode. + // Move up with FCvtSW when that's implemented. + gpr.MapIn(IRREG_FCR31); + out.Write(inst); + break; + + case IROp::FpCondToReg: + if (gpr.IsImm(IRREG_FPCOND)) { + gpr.SetImm(inst.dest, gpr.GetImm(IRREG_FPCOND)); + } else { + gpr.MapDirtyIn(inst.dest, IRREG_FPCOND); + out.Write(inst); + } + break; + + case IROp::Vec4Init: + case IROp::Vec4Mov: + case IROp::Vec4Add: + case IROp::Vec4Sub: + case IROp::Vec4Mul: + case IROp::Vec4Div: + case IROp::Vec4Dot: + case IROp::Vec4Scale: + case IROp::Vec4Shuffle: + case IROp::Vec4Neg: + case IROp::Vec4Abs: + out.Write(inst); + break; + + case IROp::ZeroFpCond: + case IROp::FCmp: + gpr.MapDirty(IRREG_FPCOND); + goto doDefault; + + case IROp::RestoreRoundingMode: + case IROp::ApplyRoundingMode: + case IROp::UpdateRoundingMode: + goto doDefault; + + case IROp::VfpuCtrlToReg: + gpr.MapDirtyIn(inst.dest, IRREG_VFPU_CTRL_BASE + inst.src1); + goto doDefault; + + case IROp::CallReplacement: + case IROp::Break: + case IROp::Syscall: + case IROp::Interpret: + case IROp::ExitToConst: + case IROp::ExitToReg: + case IROp::ExitToConstIfEq: + case IROp::ExitToConstIfNeq: + case IROp::ExitToConstIfFpFalse: + case IROp::ExitToConstIfFpTrue: + case IROp::ExitToConstIfGeZ: + case IROp::ExitToConstIfGtZ: + case IROp::ExitToConstIfLeZ: + case IROp::ExitToConstIfLtZ: + default: + { + doDefaultAndFlush: + gpr.FlushAll(); + doDefault: + WriteInstWithConstants(in, out, constants, inst); + break; + } + } + } + return logBlocks; +} + +bool IRReadsFromGPR(const IRInst &inst, int reg) { + const IRMeta *m = GetIRMeta(inst.op); + + if (m->types[1] == 'G' && inst.src1 == reg) { + return true; + } + if (m->types[2] == 'G' && inst.src2 == reg) { + return true; + } + if ((m->flags & (IRFLAG_SRC3 | IRFLAG_SRC3DST)) != 0 && m->types[0] == 'G' && inst.src3 == reg) { + return true; + } + if (inst.op == IROp::Interpret || inst.op == IROp::CallReplacement) { + return true; + } + return false; +} + +int IRDestGPR(const IRInst &inst) { + const IRMeta *m = GetIRMeta(inst.op); + + if ((m->flags & IRFLAG_SRC3) == 0 && m->types[0] == 'G') { + return inst.dest; + } + return -1; +} + +bool PurgeTemps(const IRWriter &in, IRWriter &out) { + std::vector insts; + insts.reserve(in.GetInstructions().size()); + + struct Check { + Check(int r, int i, bool rbx) : reg(r), index(i), readByExit(rbx) { + } + + int reg; + int index; + bool readByExit; + }; + std::vector checks; + + bool logBlocks = false; + for (int i = 0, n = (int)in.GetInstructions().size(); i < n; i++) { + const IRInst &inst = in.GetInstructions()[i]; + const IRMeta *m = GetIRMeta(inst.op); + + for (Check &check : checks) { + if (check.reg == 0) { + continue; + } + + if (IRReadsFromGPR(inst, check.reg)) { + // Read from, so we can't optimize out. + check.reg = 0; + } else if (check.readByExit && (m->flags & IRFLAG_EXIT) != 0) { + check.reg = 0; + } else if (IRDestGPR(inst) == check.reg) { + // Clobbered, we can optimize out. + // This happens sometimes with temporaries used for constant addresses. + insts[check.index].op = IROp::Mov; + insts[check.index].dest = 0; + insts[check.index].src1 = 0; + check.reg = 0; + } + } + + int dest = IRDestGPR(inst); + switch (dest) { + case IRTEMP_0: + case IRTEMP_1: + case IRTEMP_LHS: + case IRTEMP_RHS: + // Unlike other ops, these don't need to persist between blocks. + // So we consider them not read unless proven read. + checks.push_back(Check(dest, i, false)); + break; + + default: + if (dest > IRTEMP_RHS) { + // These might sometimes be implicitly read/written by other instructions. + break; + } + checks.push_back(Check(dest, i, true)); + break; + + // Not a GPR output. + case 0: + case -1: + break; + } + + // TODO: VFPU temps? Especially for masked dregs. + + insts.push_back(inst); + } + + for (Check &check : checks) { + if (!check.readByExit && check.reg > 0) { + insts[check.index].op = IROp::Mov; + insts[check.index].dest = 0; + insts[check.index].src1 = 0; + } + } + + for (u32 value : in.GetConstants()) { + out.AddConstant(value); + } + for (const IRInst &inst : insts) { + if (inst.op != IROp::Mov || inst.dest != 0 || inst.src1 != 0) { + out.Write(inst); + } + } + + return logBlocks; +} + +bool ReduceLoads(const IRWriter &in, IRWriter &out) { + for (u32 value : in.GetConstants()) { + out.AddConstant(value); + } + + // This tells us to skip an AND op that has been optimized out. + // Maybe we could skip multiple, but that'd slow things down and is pretty uncommon. + int nextSkip = -1; + + bool logBlocks = false; + for (int i = 0, n = (int)in.GetInstructions().size(); i < n; i++) { + IRInst inst = in.GetInstructions()[i]; + + if (inst.op == IROp::Load32 || inst.op == IROp::Load16 || inst.op == IROp::Load16Ext) { + int dest = IRDestGPR(inst); + for (int j = i + 1; j < n; j++) { + const IRInst &laterInst = in.GetInstructions()[j]; + const IRMeta *m = GetIRMeta(laterInst.op); + + if ((m->flags & IRFLAG_EXIT) != 0) { + // Exit, so we can't do the optimization. + break; + } + if (IRReadsFromGPR(laterInst, dest)) { + if (IRDestGPR(laterInst) == dest && laterInst.op == IROp::AndConst) { + const u32 mask = in.GetConstants()[laterInst.src2]; + // Here we are, maybe we can reduce the load size based on the mask. + if ((mask & 0xffffff00) == 0) { + inst.op = IROp::Load8; + if (mask == 0xff) { + nextSkip = j; + } + } else if ((mask & 0xffff0000) == 0 && inst.op == IROp::Load32) { + inst.op = IROp::Load16; + if (mask == 0xffff) { + nextSkip = j; + } + } + } + // If it was read, we can't do the optimization. + break; + } + if (IRDestGPR(laterInst) == dest) { + // Someone else wrote, so we can't do the optimization. + break; + } + } + } + + if (i != nextSkip) { + out.Write(inst); + } + } + + return logBlocks; +} diff --git a/Core/MIPS/IR/IRPassSimplify.h b/Core/MIPS/IR/IRPassSimplify.h new file mode 100644 index 0000000000..80b979fbf8 --- /dev/null +++ b/Core/MIPS/IR/IRPassSimplify.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Core/MIPS/IR/IRInst.h" + +typedef bool (*IRPassFunc)(const IRWriter &in, IRWriter &out); +bool IRApplyPasses(const IRPassFunc *passes, size_t c, const IRWriter &in, IRWriter &out); + +// Block optimizer passes of varying usefulness. +bool PropagateConstants(const IRWriter &in, IRWriter &out); +bool PurgeTemps(const IRWriter &in, IRWriter &out); +bool ReduceLoads(const IRWriter &in, IRWriter &out); +bool ThreeOpToTwoOp(const IRWriter &in, IRWriter &out); +bool OptimizeFPMoves(const IRWriter &in, IRWriter &out); diff --git a/Core/MIPS/IR/IRRegCache.cpp b/Core/MIPS/IR/IRRegCache.cpp new file mode 100644 index 0000000000..63ae135878 --- /dev/null +++ b/Core/MIPS/IR/IRRegCache.cpp @@ -0,0 +1,68 @@ +#include +#include "Core/MIPS/IR/IRRegCache.h" +#include "Core/MIPS/IR/IRInst.h" + +void IRRegCache::Flush(int rd) { + if (rd == 0) { + return; + } + if (reg_[rd].isImm) { + ir_->WriteSetConstant(rd, reg_[rd].immVal); + reg_[rd].isImm = false; + } +} + +void IRRegCache::Discard(int rd) { + if (rd == 0) { + return; + } + reg_[rd].isImm = false; +} + +IRRegCache::IRRegCache(IRWriter *ir) : ir_(ir) { + memset(®_, 0, sizeof(reg_)); + reg_[0].isImm = true; + ir_ = ir; +} + +void IRRegCache::FlushAll() { + for (int i = 0; i < TOTAL_MAPPABLE_MIPSREGS; i++) { + //if (i < IRTEMP_0) + Flush(i); + } +} + +void IRRegCache::MapIn(int rd) { + Flush(rd); +} + +void IRRegCache::MapDirty(int rd) { + Discard(rd); +} + +void IRRegCache::MapInIn(int rs, int rt) { + Flush(rs); + Flush(rt); +} + +void IRRegCache::MapInInIn(int rd, int rs, int rt) { + Flush(rd); + Flush(rs); + Flush(rt); +} + +void IRRegCache::MapDirtyIn(int rd, int rs) { + if (rs != rd) { + Discard(rd); + } + Flush(rs); +} + +void IRRegCache::MapDirtyInIn(int rd, int rs, int rt) { + if (rs != rd && rt != rd) { + Discard(rd); + } + Flush(rs); + Flush(rt); +} + diff --git a/Core/MIPS/IR/IRRegCache.h b/Core/MIPS/IR/IRRegCache.h new file mode 100644 index 0000000000..9fcdab8b1e --- /dev/null +++ b/Core/MIPS/IR/IRRegCache.h @@ -0,0 +1,47 @@ +#pragma once + +// IRRegCache is only to perform pre-constant folding. This is worth it to get cleaner +// IR. + +#include "Common/CommonTypes.h" +#include "Core/MIPS/MIPS.h" + +enum { + TOTAL_MAPPABLE_MIPSREGS = 256, +}; + +struct RegIR { + bool isImm; + u32 immVal; +}; + +class IRWriter; + +// Transient +class IRRegCache { +public: + IRRegCache(IRWriter *ir); + + void SetImm(int r, u32 immVal) { + reg_[r].isImm = true; + reg_[r].immVal = immVal; + } + + bool IsImm(int r) const { return reg_[r].isImm; } + u32 GetImm(int r) const { return reg_[r].immVal; } + + void FlushAll(); + + void MapDirty(int rd); + void MapIn(int rd); + void MapInIn(int rs, int rt); + void MapInInIn(int rd, int rs, int rt); + void MapDirtyIn(int rd, int rs); + void MapDirtyInIn(int rd, int rs, int rt); + +private: + void Flush(int rd); + void Discard(int rd); + RegIR reg_[TOTAL_MAPPABLE_MIPSREGS]; + IRWriter *ir_; +}; diff --git a/Core/MIPS/JitCommon/JitCommon.cpp b/Core/MIPS/JitCommon/JitCommon.cpp index 91ed4c10d6..0a1acd8229 100644 --- a/Core/MIPS/JitCommon/JitCommon.cpp +++ b/Core/MIPS/JitCommon/JitCommon.cpp @@ -22,9 +22,11 @@ #include "Common/StringUtils.h" #include "Core/Util/DisArm64.h" +#include "Core/Config.h" #include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/MIPS/JitCommon/JitState.h" +#include "Core/MIPS/IR/IRJit.h" #if defined(ARM) #include "../ARM/ArmJit.h" diff --git a/Core/MIPS/JitCommon/JitCommon.h b/Core/MIPS/JitCommon/JitCommon.h index 9c440a6cdb..62a91a30c2 100644 --- a/Core/MIPS/JitCommon/JitCommon.h +++ b/Core/MIPS/JitCommon/JitCommon.h @@ -42,27 +42,12 @@ class MIPSState; namespace MIPSComp { void JitAt(); - class JitInterface { + class MIPSFrontendInterface { public: - virtual ~JitInterface() {} + virtual ~MIPSFrontendInterface() {} - virtual bool DescribeCodePtr(const u8 *ptr, std::string &name) = 0; - virtual const u8 *GetDispatcher() const = 0; - virtual JitBlockCache *GetBlockCache() = 0; - virtual void InvalidateCache() = 0; - virtual void InvalidateCacheAt(u32 em_address, int length = 4) = 0; - virtual void DoState(PointerWrap &p) = 0; - virtual void DoDummyState(PointerWrap &p) = 0; - virtual void RunLoopUntil(u64 globalticks) = 0; - virtual void Compile(u32 em_address) = 0; - virtual void ClearCache() = 0; virtual void EatPrefix() = 0; - // Block linking. This may need to work differently for whole-function JITs and stuff - // like that. - virtual void LinkBlock(u8 *exitPoint, const u8 *entryPoint) = 0; - virtual void UnlinkBlock(u8 *checkedEntry, u32 originalAddress) = 0; - virtual void Comp_Generic(MIPSOpcode op) = 0; virtual void Comp_RunBlock(MIPSOpcode op) = 0; virtual void Comp_ReplacementFunc(MIPSOpcode op) = 0; @@ -131,8 +116,30 @@ namespace MIPSComp { virtual int Replace_fabsf() = 0; }; - typedef void (JitInterface::*MIPSCompileFunc)(MIPSOpcode opcode); - typedef int (JitInterface::*MIPSReplaceFunc)(); + class JitInterface { + public: + virtual ~JitInterface() {} + + virtual bool DescribeCodePtr(const u8 *ptr, std::string &name) = 0; + virtual const u8 *GetDispatcher() const = 0; + virtual JitBlockCache *GetBlockCache() = 0; + virtual void InvalidateCache() = 0; + virtual void InvalidateCacheAt(u32 em_address, int length = 4) = 0; + virtual void DoState(PointerWrap &p) = 0; + virtual void DoDummyState(PointerWrap &p) = 0; + virtual void RunLoopUntil(u64 globalticks) = 0; + virtual void Compile(u32 em_address) = 0; + virtual void ClearCache() = 0; + virtual MIPSOpcode GetOriginalOp(MIPSOpcode op) = 0; + + // Block linking. This may need to work differently for whole-function JITs and stuff + // like that. + virtual void LinkBlock(u8 *exitPoint, const u8 *entryPoint) = 0; + virtual void UnlinkBlock(u8 *checkedEntry, u32 originalAddress) = 0; + }; + + typedef void (MIPSFrontendInterface::*MIPSCompileFunc)(MIPSOpcode opcode); + typedef int (MIPSFrontendInterface::*MIPSReplaceFunc)(); extern JitInterface *jit; diff --git a/Core/MIPS/MIPS.cpp b/Core/MIPS/MIPS.cpp index 1140b67914..0482f21211 100644 --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -27,6 +27,7 @@ #include "Core/MIPS/MIPSTables.h" #include "Core/MIPS/MIPSDebugInterface.h" #include "Core/MIPS/MIPSVFPUUtils.h" +#include "Core/MIPS/IR/IRJit.h" #include "Core/Reporting.h" #include "Core/System.h" #include "Core/HLE/sceDisplay.h" @@ -206,8 +207,10 @@ void MIPSState::Init() { // Initialize the VFPU random number generator with .. something? rng.Init(0x1337); - if (PSP_CoreParameter().cpuCore == CPU_JIT) { + if (PSP_CoreParameter().cpuCore == CPU_CORE_JIT) { MIPSComp::jit = MIPSComp::CreateNativeJit(this); + } else if (PSP_CoreParameter().cpuCore == CPU_CORE_IRJIT) { + MIPSComp::jit = new MIPSComp::IRJit(this); } else { MIPSComp::jit = nullptr; } @@ -224,14 +227,23 @@ void MIPSState::UpdateCore(CPUCore desired) { PSP_CoreParameter().cpuCore = desired; switch (PSP_CoreParameter().cpuCore) { - case CPU_JIT: + case CPU_CORE_JIT: INFO_LOG(CPU, "Switching to JIT"); - if (!MIPSComp::jit) { - MIPSComp::jit = MIPSComp::CreateNativeJit(this); + if (MIPSComp::jit) { + delete MIPSComp::jit; } + MIPSComp::jit = MIPSComp::CreateNativeJit(this); break; - case CPU_INTERPRETER: + case CPU_CORE_IRJIT: + INFO_LOG(CPU, "Switching to IRJIT"); + if (MIPSComp::jit) { + delete MIPSComp::jit; + } + MIPSComp::jit = new MIPSComp::IRJit(this); + break; + + case CPU_CORE_INTERPRETER: INFO_LOG(CPU, "Switching to interpreter"); delete MIPSComp::jit; MIPSComp::jit = 0; @@ -292,11 +304,12 @@ void MIPSState::SingleStep() { // returns 1 if reached ticks limit int MIPSState::RunLoopUntil(u64 globalTicks) { switch (PSP_CoreParameter().cpuCore) { - case CPU_JIT: + case CPU_CORE_JIT: + case CPU_CORE_IRJIT: MIPSComp::jit->RunLoopUntil(globalTicks); break; - case CPU_INTERPRETER: + case CPU_CORE_INTERPRETER: return MIPSInterpret_RunUntil(globalTicks); } return 1; diff --git a/Core/MIPS/MIPS.h b/Core/MIPS/MIPS.h index a24c2f3d8b..f51644fd76 100644 --- a/Core/MIPS/MIPS.h +++ b/Core/MIPS/MIPS.h @@ -86,6 +86,7 @@ enum MIPSGPReg { MIPS_REG_RA=31, // Not real regs, just for convenience/jit mapping. + // NOTE: These are not the same as the offsets the IR has to use! MIPS_REG_HI = 32, MIPS_REG_LO = 33, MIPS_REG_FPCOND = 34, @@ -155,7 +156,7 @@ public: void DoState(PointerWrap &p); - // MUST start with r and be followed by f! + // MUST start with r and be followed by f, v, and t! u32 r[32]; union { float f[32]; @@ -166,22 +167,29 @@ public: float v[128]; u32 vi[128]; }; - // Temps don't get flushed so we don't reserve space for them. + + // Register-allocated JIT Temps don't get flushed so we don't reserve space for them. + // However, the IR interpreter needs some temps that can stick around between ops. + // Can be indexed through r[] using indices 192+. + u32 t[16]; //192 + // If vfpuCtrl (prefixes) get mysterious values, check the VFPU regcache code. - u32 vfpuCtrl[16]; + u32 vfpuCtrl[16]; // 208 + + float vt[16]; //224 TODO: VFPU temp // ARM64 wants lo/hi to be aligned to 64 bits from the base of this struct. - u32 padLoHi; + u32 padLoHi; // 240 union { struct { - u32 pc; + u32 pc; //241 - u32 lo; - u32 hi; + u32 lo; //242 + u32 hi; //243 - u32 fcr31; //fpu control register - u32 fpcond; // cache the cond flag of fcr31 (& 1 << 23) + u32 fcr31; //244 fpu control register + u32 fpcond; //245 cache the cond flag of fcr31 (& 1 << 23) }; u32 other[6]; }; diff --git a/Core/MIPS/MIPS/MipsJit.h b/Core/MIPS/MIPS/MipsJit.h index 1fcb6faea6..8c5dbf9bd3 100644 --- a/Core/MIPS/MIPS/MipsJit.h +++ b/Core/MIPS/MIPS/MipsJit.h @@ -20,6 +20,7 @@ #include "Common/MipsEmitter.h" using namespace MIPSGen; +#include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/MIPS/JitCommon/JitState.h" #include "Core/MIPS/JitCommon/JitBlockCache.h" #include "../MIPSVFPUUtils.h" @@ -31,7 +32,7 @@ using namespace MIPSGen; namespace MIPSComp { -class MipsJit : public MIPSGen::MIPSCodeBlock, public JitInterface +class MipsJit : public MIPSGen::MIPSCodeBlock, public JitInterface, public MIPSFrontendInterface { public: MipsJit(MIPSState *mips); diff --git a/Core/MIPS/MIPSTables.cpp b/Core/MIPS/MIPSTables.cpp index 39038df6ae..76e260b377 100644 --- a/Core/MIPS/MIPSTables.cpp +++ b/Core/MIPS/MIPSTables.cpp @@ -28,6 +28,7 @@ #include "Core/CoreTiming.h" #include "Core/Reporting.h" #include "Core/Debugger/Breakpoints.h" +#include "base/logging.h" #include "JitCommon/JitCommon.h" @@ -81,7 +82,7 @@ struct MIPSInstruction { #define ENCODING(a) {a} #define INSTR(name, comp, dis, inter, flags) {Instruc, name, comp, dis, inter, MIPSInfo(flags)} -#define JITFUNC(f) (&JitInterface::f) +#define JITFUNC(f) (&MIPSFrontendInterface::f) using namespace MIPSDis; using namespace MIPSInt; @@ -911,7 +912,7 @@ const MIPSInstruction *MIPSGetInstruction(MIPSOpcode op) { return instr; } -void MIPSCompileOp(MIPSOpcode op, MIPSComp::JitInterface *jit) { +void MIPSCompileOp(MIPSOpcode op, MIPSComp::MIPSFrontendInterface *jit) { if (op == 0) return; const MIPSInstruction *instr = MIPSGetInstruction(op); @@ -922,7 +923,6 @@ void MIPSCompileOp(MIPSOpcode op, MIPSComp::JitInterface *jit) { } else { ERROR_LOG_REPORT(CPU,"MIPSCompileOp %08x failed",op.encoding); } - if (info & OUT_EAT_PREFIX) jit->EatPrefix(); } else { @@ -977,6 +977,7 @@ int MIPSInterpret_RunUntil(u64 globalTicks) while (coreState == CORE_RUNNING) { CoreTiming::Advance(); + u32 lastPC = 0; // NEVER stop in a delay slot! while (curMips->downcount >= 0 && coreState == CORE_RUNNING) @@ -1015,6 +1016,17 @@ int MIPSInterpret_RunUntil(u64 globalTicks) bool wasInDelaySlot = curMips->inDelaySlot; + /* + if (curMips->pc != lastPC + 4) { + if (blockCount > 0) { + MIPSState *mips_ = curMips; + fprintf(f, "BLOCK : %08x v0: %08x v1: %08x a0: %08x s0: %08x s4: %08x\n", mips_->pc, mips_->r[MIPS_REG_V0], mips_->r[MIPS_REG_V1], mips_->r[MIPS_REG_A0], mips_->r[MIPS_REG_S0], mips_->r[MIPS_REG_S4]); + fflush(f); + blockCount--; + } + } + lastPC = curMips->pc; + */ MIPSInterpret(op); if (curMips->inDelaySlot) diff --git a/Core/MIPS/MIPSTables.h b/Core/MIPS/MIPSTables.h index 39c8c06753..3987aa3e87 100644 --- a/Core/MIPS/MIPSTables.h +++ b/Core/MIPS/MIPSTables.h @@ -112,10 +112,10 @@ typedef void (CDECL *MIPSDisFunc)(MIPSOpcode opcode, char *out); typedef void (CDECL *MIPSInterpretFunc)(MIPSOpcode opcode); namespace MIPSComp { - class JitInterface; + class MIPSFrontendInterface; } -void MIPSCompileOp(MIPSOpcode op, MIPSComp::JitInterface *jit); +void MIPSCompileOp(MIPSOpcode op, MIPSComp::MIPSFrontendInterface *jit); void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, bool tabsToSpaces = false); MIPSInfo MIPSGetInfo(MIPSOpcode op); void MIPSInterpret(MIPSOpcode op); //only for those rare ones diff --git a/Core/MIPS/MIPSVFPUUtils.cpp b/Core/MIPS/MIPSVFPUUtils.cpp index 385926d5b4..cd9739be02 100644 --- a/Core/MIPS/MIPSVFPUUtils.cpp +++ b/Core/MIPS/MIPSVFPUUtils.cpp @@ -395,6 +395,10 @@ MatrixOverlapType GetMatrixOverlap(int mtx1, int mtx2, MatrixSize msize) { if (mtx1 == mtx2) return OVERLAP_EQUAL; + if (msize == M_4x4) { + return (mtx1 == mtx2) ? OVERLAP_EQUAL : OVERLAP_NONE; + } + u8 m1[16]; u8 m2[16]; GetMatrixRegs(m1, msize, mtx1); diff --git a/Core/MIPS/MIPSVFPUUtils.h b/Core/MIPS/MIPSVFPUUtils.h index bb8403217f..7f6ada0fa2 100644 --- a/Core/MIPS/MIPSVFPUUtils.h +++ b/Core/MIPS/MIPSVFPUUtils.h @@ -45,6 +45,10 @@ inline float vfpu_cos(float angle) { return cosf(angle); } +inline float vfpu_asin(float angle) { + return asinf(angle) / M_PI_2; +} + inline void vfpu_sincos(float angle, float &sine, float &cosine) { angle -= floorf(angle * 0.25f) * 4.f; angle *= (float)M_PI_2; @@ -127,7 +131,15 @@ int GetNumVectorElements(VectorSize sz); int GetMatrixSide(MatrixSize sz); const char *GetVectorNotation(int reg, VectorSize size); const char *GetMatrixNotation(int reg, MatrixSize size); - +inline bool IsMatrixTransposed(int matrixReg) { + return (matrixReg >> 5) & 1; +} +inline bool IsVectorColumn(int vectorReg) { + return !((vectorReg >> 5) & 1); +} +inline int TransposeMatrixReg(int matrixReg) { + return matrixReg ^ 0x20; +} int GetVectorOverlap(int reg1, VectorSize size1, int reg2, VectorSize size2); float Float16ToFloat32(unsigned short l); diff --git a/Core/MIPS/x86/Asm.cpp b/Core/MIPS/x86/Asm.cpp index 6d03ba5f6f..05eda2823d 100644 --- a/Core/MIPS/x86/Asm.cpp +++ b/Core/MIPS/x86/Asm.cpp @@ -40,7 +40,7 @@ namespace MIPSComp //TODO - make an option //#if _DEBUG -static bool enableDebug = false; +static bool enableDebug = false; //#else // bool enableDebug = false; diff --git a/Core/MIPS/x86/CompVFPU.cpp b/Core/MIPS/x86/CompVFPU.cpp index fcc51c3646..ec95760d57 100644 --- a/Core/MIPS/x86/CompVFPU.cpp +++ b/Core/MIPS/x86/CompVFPU.cpp @@ -101,8 +101,7 @@ void Jit::ApplyPrefixST(u8 *vregs, u32 prefix, VectorSize sz) { for (int i = 0; i < n; i++) origV[i] = vregs[i]; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int regnum = (prefix >> (i*2)) & 3; int abs = (prefix >> (8+i)) & 1; int negate = (prefix >> (16+i)) & 1; @@ -1802,8 +1801,7 @@ void Jit::Comp_Vf2i(MIPSOpcode op) { const double *mult = &mulTableVf2i[imm]; int setMXCSR = -1; - switch ((op >> 21) & 0x1f) - { + switch ((op >> 21) & 0x1f) { case 17: break; //z - truncate. Easy to support. case 16: @@ -2142,7 +2140,7 @@ void CosOnly(SinCosArg angle) { } void ASinScaled(SinCosArg angle) { - sincostemp[0] = asinf(angle) / M_PI_2; + sincostemp[0] = vfpu_asin(angle); } void SinCosNegSin(SinCosArg angle) { diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index 3ada3ad123..4bfce6814e 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -81,8 +81,7 @@ u32 JitBreakpoint() host->SetDebugMode(true); // There's probably a better place for this. - if (USE_JIT_MISSMAP) - { + if (USE_JIT_MISSMAP) { std::map notJitSorted; std::transform(notJitOps.begin(), notJitOps.end(), std::inserter(notJitSorted, notJitSorted.begin()), flip_pair); @@ -839,4 +838,14 @@ void Jit::CallProtectedFunction(const void *func, const OpArg &arg1, const u32 a void Jit::Comp_DoNothing(MIPSOpcode op) { } +MIPSOpcode Jit::GetOriginalOp(MIPSOpcode op) { + JitBlockCache *bc = GetBlockCache(); + int block_num = bc->GetBlockNumberFromEmuHackOp(op, true); + if (block_num >= 0) { + return bc->GetOriginalFirstOp(block_num); + } else { + return op; + } +} + } // namespace diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 68d160a958..4206f4d307 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -46,7 +46,7 @@ struct RegCacheState { FPURegCacheState fpr; }; -class Jit : public Gen::XCodeBlock, public JitInterface { +class Jit : public Gen::XCodeBlock, public JitInterface, public MIPSFrontendInterface { public: Jit(MIPSState *mips); virtual ~Jit(); @@ -161,6 +161,7 @@ public: void UpdateRoundingMode(); JitBlockCache *GetBlockCache() { return &blocks; } + MIPSOpcode GetOriginalOp(MIPSOpcode op) override; void ClearCache(); void InvalidateCache() override; diff --git a/Core/MemMap.cpp b/Core/MemMap.cpp index 557bc461c4..0d3b3817cc 100644 --- a/Core/MemMap.cpp +++ b/Core/MemMap.cpp @@ -426,26 +426,22 @@ __forceinline static Opcode Read_Instruction(u32 address, bool resolveReplacemen if (MIPS_IS_RUNBLOCK(inst.encoding) && MIPSComp::jit) { JitBlockCache *bc = MIPSComp::jit->GetBlockCache(); - int block_num = bc->GetBlockNumberFromEmuHackOp(inst, true); - if (block_num >= 0) { - inst = bc->GetOriginalFirstOp(block_num); - if (resolveReplacements && MIPS_IS_REPLACEMENT(inst)) { - u32 op; - if (GetReplacedOpAt(address, &op)) { - if (MIPS_IS_EMUHACK(op)) { - ERROR_LOG(HLE,"WTF 1"); - return Opcode(op); - } else { - return Opcode(op); - } + + inst = MIPSComp::jit->GetOriginalOp(inst); + if (resolveReplacements && MIPS_IS_REPLACEMENT(inst)) { + u32 op; + if (GetReplacedOpAt(address, &op)) { + if (MIPS_IS_EMUHACK(op)) { + ERROR_LOG(HLE,"WTF 1"); + return Opcode(op); } else { - ERROR_LOG(HLE, "Replacement, but no replacement op? %08x", inst.encoding); + return Opcode(op); } + } else { + ERROR_LOG(HLE, "Replacement, but no replacement op? %08x", inst.encoding); } - return inst; - } else { - return inst; } + return inst; } else if (resolveReplacements && MIPS_IS_REPLACEMENT(inst.encoding)) { u32 op; if (GetReplacedOpAt(address, &op)) { @@ -479,13 +475,7 @@ Opcode Read_Opcode_JIT(u32 address) { Opcode inst = Opcode(Read_U32(address)); if (MIPS_IS_RUNBLOCK(inst.encoding) && MIPSComp::jit) { - JitBlockCache *bc = MIPSComp::jit->GetBlockCache(); - int block_num = bc->GetBlockNumberFromEmuHackOp(inst, true); - if (block_num >= 0) { - return bc->GetOriginalFirstOp(block_num); - } else { - return inst; - } + return MIPSComp::jit->GetOriginalOp(inst); } else { return inst; } diff --git a/Core/MemMap.h b/Core/MemMap.h index c49a41d119..c2b4ad3b12 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -186,6 +186,14 @@ inline u32 ReadUnchecked_U32(const u32 address) { #endif } +inline float ReadUnchecked_Float(const u32 address) { +#ifdef _ARCH_32 + return *(float *)(base + (address & MEMVIEW32_MASK)); +#else + return *(float *)(base + address); +#endif +} + inline u16 ReadUnchecked_U16(const u32 address) { #ifdef _ARCH_32 return *(u16_le *)(base + (address & MEMVIEW32_MASK)); @@ -210,6 +218,14 @@ inline void WriteUnchecked_U32(u32 data, u32 address) { #endif } +inline void WriteUnchecked_Float(float data, u32 address) { +#ifdef _ARCH_32 + *(float *)(base + (address & MEMVIEW32_MASK)) = data; +#else + *(float *)(base + address) = data; +#endif +} + inline void WriteUnchecked_U16(u16 data, u32 address) { #ifdef _ARCH_32 *(u16_le *)(base + (address & MEMVIEW32_MASK)) = data; diff --git a/Core/MemMapFunctions.cpp b/Core/MemMapFunctions.cpp index 93029d65ff..7ffc80dc9a 100644 --- a/Core/MemMapFunctions.cpp +++ b/Core/MemMapFunctions.cpp @@ -87,7 +87,7 @@ inline void ReadFromHardware(T &var, const u32 address) { var = *((const T*)GetPointerUnchecked(address)); } else { // In jit, we only flush PC when bIgnoreBadMemAccess is off. - if (g_Config.bJit && g_Config.bIgnoreBadMemAccess) { + if (g_Config.iCpuCore == CPU_CORE_JIT && g_Config.bIgnoreBadMemAccess) { WARN_LOG(MEMMAP, "ReadFromHardware: Invalid address %08x", address); } else { WARN_LOG(MEMMAP, "ReadFromHardware: Invalid address %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); @@ -123,7 +123,7 @@ inline void WriteToHardware(u32 address, const T data) { *(T*)GetPointerUnchecked(address) = data; } else { // In jit, we only flush PC when bIgnoreBadMemAccess is off. - if (g_Config.bJit && g_Config.bIgnoreBadMemAccess) { + if (g_Config.iCpuCore == CPU_CORE_JIT && g_Config.bIgnoreBadMemAccess) { WARN_LOG(MEMMAP, "WriteToHardware: Invalid address %08x", address); } else { WARN_LOG(MEMMAP, "WriteToHardware: Invalid address %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index bf6f30eeed..0b4465144c 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -234,9 +234,14 @@ namespace SaveState if (MIPSComp::jit && p.mode == p.MODE_WRITE) { auto blockCache = MIPSComp::jit->GetBlockCache(); - auto savedBlocks = blockCache->SaveAndClearEmuHackOps(); + std::vector savedBlocks; + if (blockCache) { + savedBlocks = blockCache->SaveAndClearEmuHackOps(); + } Memory::DoState(p); - blockCache->RestoreSavedEmuHackOps(savedBlocks); + if (blockCache) { + blockCache->RestoreSavedEmuHackOps(savedBlocks); + } } else Memory::DoState(p); diff --git a/Qt/Core.pro b/Qt/Core.pro index 13827a029d..8a23d36e0e 100644 --- a/Qt/Core.pro +++ b/Qt/Core.pro @@ -38,6 +38,7 @@ SOURCES += $$P/Core/*.cpp \ # Core $$P/Core/HLE/*.cpp \ $$P/Core/HW/*.cpp \ $$P/Core/MIPS/*.cpp \ + $$P/Core/MIPS/IR/*.cpp \ $$P/Core/MIPS/JitCommon/*.cpp \ $$P/Core/Util/AudioFormat.cpp \ $$P/Core/Util/BlockAllocator.cpp \ @@ -56,6 +57,7 @@ HEADERS += $$P/Core/*.h \ $$P/Core/HLE/*.h \ $$P/Core/HW/*.h \ $$P/Core/MIPS/*.h \ + $$P/Core/MIPS/IR/*.h \ $$P/Core/MIPS/JitCommon/*.h \ $$P/Core/Util/AudioFormat.h \ $$P/Core/Util/BlockAllocator.h \ diff --git a/Qt/mainwindow.cpp b/Qt/mainwindow.cpp index 12862659c0..9ceca618ab 100644 --- a/Qt/mainwindow.cpp +++ b/Qt/mainwindow.cpp @@ -515,8 +515,6 @@ void MainWindow::createMenus() MenuTree* optionsMenu = new MenuTree(this, menuBar(), QT_TR_NOOP("&Options")); // - Core MenuTree* coreMenu = new MenuTree(this, optionsMenu, QT_TR_NOOP("&Core")); - coreMenu->add(new MenuAction(this, SLOT(dynarecAct()), QT_TR_NOOP("&CPU Dynarec"))) - ->addEventChecked(&g_Config.bJit); coreMenu->add(new MenuAction(this, SLOT(vertexDynarecAct()), QT_TR_NOOP("&Vertex Decoder Dynarec"))) ->addEventChecked(&g_Config.bVertexDecoderJit); coreMenu->add(new MenuAction(this, SLOT(fastmemAct()), QT_TR_NOOP("Fast &Memory (unstable)"))) diff --git a/Qt/mainwindow.h b/Qt/mainwindow.h index bf8bdf2a30..1428f4dea8 100644 --- a/Qt/mainwindow.h +++ b/Qt/mainwindow.h @@ -87,7 +87,6 @@ private slots: // Options // Core - void dynarecAct() { g_Config.bJit = !g_Config.bJit; } void vertexDynarecAct() { g_Config.bVertexDecoderJit = !g_Config.bVertexDecoderJit; } void fastmemAct() { g_Config.bFastMemory = !g_Config.bFastMemory; } void ignoreIllegalAct() { g_Config.bIgnoreBadMemAccess = !g_Config.bIgnoreBadMemAccess; } diff --git a/UI/DevScreens.cpp b/UI/DevScreens.cpp index 1b41243b61..42712bc111 100644 --- a/UI/DevScreens.cpp +++ b/UI/DevScreens.cpp @@ -669,6 +669,8 @@ UI::EventReturn JitCompareScreen::OnAddressChange(UI::EventParams &e) { return UI::EVENT_DONE; } JitBlockCache *blockCache = MIPSComp::jit->GetBlockCache(); + if (!blockCache) + return UI::EVENT_DONE; u32 addr; if (blockAddr_->GetText().size() > 8) return UI::EVENT_DONE; @@ -731,6 +733,9 @@ UI::EventReturn JitCompareScreen::OnBlockAddress(UI::EventParams &e) { } JitBlockCache *blockCache = MIPSComp::jit->GetBlockCache(); + if (!blockCache) + return UI::EVENT_DONE; + if (Memory::IsValidAddress(e.a)) { currentBlock_ = blockCache->GetBlockNumberFromStartAddress(e.a); } else { @@ -746,6 +751,9 @@ UI::EventReturn JitCompareScreen::OnRandomBlock(UI::EventParams &e) { } JitBlockCache *blockCache = MIPSComp::jit->GetBlockCache(); + if (!blockCache) + return UI::EVENT_DONE; + int numBlocks = blockCache->GetNumBlocks(); if (numBlocks > 0) { currentBlock_ = rand() % numBlocks; @@ -769,6 +777,9 @@ void JitCompareScreen::OnRandomBlock(int flag) { return; } JitBlockCache *blockCache = MIPSComp::jit->GetBlockCache(); + if (!blockCache) + return; + int numBlocks = blockCache->GetNumBlocks(); if (numBlocks > 0) { bool anyWanted = false; @@ -797,6 +808,8 @@ UI::EventReturn JitCompareScreen::OnCurrentBlock(UI::EventParams &e) { return UI::EVENT_DONE; } JitBlockCache *blockCache = MIPSComp::jit->GetBlockCache(); + if (!blockCache) + return UI::EVENT_DONE; std::vector blockNum; blockCache->GetBlockNumbersFromAddress(currentMIPS->pc, &blockNum); if (blockNum.size() > 0) { diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index dd89e1ae7d..06a3446eb6 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -101,7 +101,7 @@ void EmuScreen::bootGame(const std::string &filename) { invalid_ = true; CoreParameter coreParam; - coreParam.cpuCore = g_Config.bJit ? CPU_JIT : CPU_INTERPRETER; + coreParam.cpuCore = (CPUCore)g_Config.iCpuCore; coreParam.gpuCore = GPUCORE_GLES; switch (GetGPUBackend()) { case GPUBackend::OPENGL: @@ -282,7 +282,7 @@ void EmuScreen::sendMessage(const char *message, const char *value) { } else if (!strcmp(message, "clear jit")) { currentMIPS->ClearJitCache(); if (PSP_IsInited()) { - currentMIPS->UpdateCore(g_Config.bJit ? CPU_JIT : CPU_INTERPRETER); + currentMIPS->UpdateCore((CPUCore)g_Config.iCpuCore); } } else if (!strcmp(message, "window minimized")) { if (!strcmp(value, "true")) { diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 2a5828eaec..32c4a1f93b 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -1059,8 +1059,11 @@ void DeveloperToolsScreen::CreateViews() { } } #endif - if (canUseJit) { - list->Add(new CheckBox(&g_Config.bJit, sy->T("Dynarec", "Dynarec (JIT)")))->OnClick.Handle(this, &DeveloperToolsScreen::OnJitAffectingSetting); + + static const char *cpuCores[] = { "Interpreter", "Dynarec (JIT)", "IR Interpreter" }; + PopupMultiChoice *core = list->Add(new PopupMultiChoice(&g_Config.iCpuCore, gr->T("CPU Core"), cpuCores, 0, ARRAY_SIZE(cpuCores), sy->GetName(), screenManager())); + if (!canUseJit) { + core->HideChoice(1); } list->Add(new CheckBox(&g_Config.bShowDeveloperMenu, dev->T("Show Developer Menu"))); diff --git a/UI/MiscScreens.cpp b/UI/MiscScreens.cpp index e6f0cc28e6..c94c9e6df4 100644 --- a/UI/MiscScreens.cpp +++ b/UI/MiscScreens.cpp @@ -133,7 +133,7 @@ void HandleCommonMessages(const char *message, const char *value, ScreenManager MIPSComp::jit->ClearCache(); } if (PSP_IsInited()) { - currentMIPS->UpdateCore(g_Config.bJit ? CPU_JIT : CPU_INTERPRETER); + currentMIPS->UpdateCore((CPUCore)g_Config.iCpuCore); } } } diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 786e4e5126..9ca8045dd8 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -392,11 +392,15 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch gfxLog = true; break; case 'j': - g_Config.bJit = true; + g_Config.iCpuCore = CPU_CORE_JIT; g_Config.bSaveSettings = false; break; case 'i': - g_Config.bJit = false; + g_Config.iCpuCore = CPU_CORE_INTERPRETER; + g_Config.bSaveSettings = false; + break; + case 'r': + g_Config.iCpuCore = CPU_CORE_IRJIT; g_Config.bSaveSettings = false; break; case '-': diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 1cda4ed095..115b969972 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -157,6 +157,17 @@ EXEC_AND_LIB_FILES := \ $(SRC)/Core/MIPS/MIPSVFPUUtils.cpp.arm \ $(SRC)/Core/MIPS/MIPSCodeUtils.cpp.arm \ $(SRC)/Core/MIPS/MIPSDebugInterface.cpp \ + $(SRC)/Core/MIPS/IR/IRFrontend.cpp \ + $(SRC)/Core/MIPS/IR/IRJit.cpp \ + $(SRC)/Core/MIPS/IR/IRCompALU.cpp \ + $(SRC)/Core/MIPS/IR/IRCompBranch.cpp \ + $(SRC)/Core/MIPS/IR/IRCompFPU.cpp \ + $(SRC)/Core/MIPS/IR/IRCompLoadStore.cpp \ + $(SRC)/Core/MIPS/IR/IRCompVFPU.cpp \ + $(SRC)/Core/MIPS/IR/IRInst.cpp \ + $(SRC)/Core/MIPS/IR/IRInterpreter.cpp \ + $(SRC)/Core/MIPS/IR/IRPassSimplify.cpp \ + $(SRC)/Core/MIPS/IR/IRRegCache.cpp \ $(SRC)/UI/ui_atlas.cpp \ $(SRC)/UI/OnScreenDisplay.cpp \ $(SRC)/ext/libkirk/AES.c \ diff --git a/android/jni/TestRunner.cpp b/android/jni/TestRunner.cpp index cfc4e0354e..29ca2b2a0c 100644 --- a/android/jni/TestRunner.cpp +++ b/android/jni/TestRunner.cpp @@ -69,7 +69,7 @@ void RunTests() #endif CoreParameter coreParam; - coreParam.cpuCore = g_Config.bJit ? CPU_JIT : CPU_INTERPRETER; + coreParam.cpuCore = (CPUCore)g_Config.iCpuCore; coreParam.gpuCore = g_Config.bSoftwareRendering ? GPUCORE_SOFTWARE : GPUCORE_GLES; coreParam.enableSound = g_Config.bEnableSound; coreParam.graphicsContext = PSP_CoreParameter().graphicsContext; diff --git a/headless/Headless.cpp b/headless/Headless.cpp index 687ac1d875..f818da68ca 100644 --- a/headless/Headless.cpp +++ b/headless/Headless.cpp @@ -207,11 +207,11 @@ int main(int argc, const char* argv[]) #endif bool fullLog = false; - bool useJit = true; bool autoCompare = false; bool verbose = false; const char *stateToLoad = 0; GPUCore gpuCore = GPUCORE_NULL; + CPUCore cpuCore = CPU_CORE_JIT; std::vector testFilenames; const char *mountIso = 0; @@ -236,9 +236,11 @@ int main(int argc, const char* argv[]) else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--log")) fullLog = true; else if (!strcmp(argv[i], "-i")) - useJit = false; + cpuCore = CPU_CORE_INTERPRETER; else if (!strcmp(argv[i], "-j")) - useJit = true; + cpuCore = CPU_CORE_JIT; + else if (!strcmp(argv[i], "-ir")) + cpuCore = CPU_CORE_IRJIT; else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--compare")) autoCompare = true; else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")) @@ -311,7 +313,7 @@ int main(int argc, const char* argv[]) } CoreParameter coreParameter; - coreParameter.cpuCore = useJit ? CPU_JIT : CPU_INTERPRETER; + coreParameter.cpuCore = cpuCore; coreParameter.gpuCore = glWorking ? gpuCore : GPUCORE_NULL; coreParameter.graphicsContext = graphicsContext; coreParameter.enableSound = false; diff --git a/unittest/JitHarness.cpp b/unittest/JitHarness.cpp index b80fb04e6d..2467a27c0b 100644 --- a/unittest/JitHarness.cpp +++ b/unittest/JitHarness.cpp @@ -83,7 +83,7 @@ static void SetupJitHarness() { coreState = CORE_POWERUP; currentMIPS = &mipsr4k; Memory::g_MemorySize = Memory::RAM_NORMAL_SIZE; - PSP_CoreParameter().cpuCore = CPU_INTERPRETER; + PSP_CoreParameter().cpuCore = CPU_CORE_INTERPRETER; PSP_CoreParameter().unthrottle = true; Memory::Init(); @@ -169,7 +169,7 @@ bool TestJit() { double jit_speed = 0.0, interp_speed = 0.0; if (compileSuccess) { interp_speed = ExecCPUTest(); - mipsr4k.UpdateCore(CPU_JIT); + mipsr4k.UpdateCore(CPU_CORE_JIT); jit_speed = ExecCPUTest(); // Disassemble