diff --git a/CMakeLists.txt b/CMakeLists.txt index e893dd0677..3df14fef8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -298,6 +298,11 @@ elseif(X86) Common/x64Analyzer.h Common/x64Emitter.cpp Common/x64Emitter.h) +elseif(MIPS) + set(CommonExtra ${CommonExtra} + Common/FakeCPUDetect.cpp + Common/MipsEmitter.cpp + Common/MipsEmitter.h) else() set(CommonExtra ${CommonExtra} Common/FakeCPUDetect.cpp @@ -1069,6 +1074,12 @@ elseif(X86) Core/MIPS/x86/RegCacheFPU.h GPU/Common/VertexDecoderX86.cpp ext/disarm.cpp) +elseif(MIPS) + set(CoreExtra ${CoreExtra} + Core/MIPS/MIPS/MipsJit.cpp + Core/MIPS/MIPS/MipsJit.h + GPU/Common/VertexDecoderFake.cpp + ext/disarm.cpp) else() set(CoreExtra ${CoreExtra} Core/MIPS/fake/FakeJit.cpp diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj index 058134de48..4b552d4fc0 100644 --- a/Common/Common.vcxproj +++ b/Common/Common.vcxproj @@ -189,6 +189,7 @@ + @@ -226,6 +227,7 @@ + @@ -258,4 +260,4 @@ - + \ No newline at end of file diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters index 7de082ca92..3d7f89228f 100644 --- a/Common/Common.vcxproj.filters +++ b/Common/Common.vcxproj.filters @@ -37,6 +37,7 @@ Crypto + @@ -67,6 +68,7 @@ + diff --git a/Common/MipsEmitter.cpp b/Common/MipsEmitter.cpp new file mode 100644 index 0000000000..b190aee592 --- /dev/null +++ b/Common/MipsEmitter.cpp @@ -0,0 +1,448 @@ +// Copyright (c) 2014- 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 +#include +#include +#include +#include +#include + +#include "MemoryUtil.h" +#include "MipsEmitter.h" +#include "CPUDetect.h" + +namespace MIPSGen { +void MIPSEmitter::SetCodePtr(u8 *ptr) { + code_ = ptr; + lastCacheFlushEnd_ = ptr; +} + +void MIPSEmitter::ReserveCodeSpace(u32 bytes) { + for (u32 i = 0; i < bytes / 4; ++i) { + BREAK(0); + } +} + +const u8 *MIPSEmitter::AlignCode16() { + ReserveCodeSpace((-(intptr_t)code_) & 15); + return code_; +} + +const u8 *MIPSEmitter::AlignCodePage() { + // TODO: Assuming code pages ought to be 4K? + ReserveCodeSpace((-(intptr_t)code_) & 4095); + return code_; +} + +const u8 *MIPSEmitter::GetCodePtr() const { + return code_; +} + +u8 *MIPSEmitter::GetWritableCodePtr() { + return code_; +} + +void MIPSEmitter::FlushIcache() { + FlushIcacheSection(lastCacheFlushEnd_, code_); + lastCacheFlushEnd_ = code_; +} + +void MIPSEmitter::FlushIcacheSection(u8 *start, u8 *end) { +#if defined(MIPS) +#ifdef __clang__ + __clear_cache(start, end); +#else + __builtin___clear_cache(start, end); +#endif +#endif +} + +void MIPSEmitter::BREAK(u32 code) { + // 000000 iiiiiiiiiiiiiiiiiiii 001101 + _dbg_assert_msg_(JIT, code <= 0xfffff, "Bad emitter arguments"); + Write32Fields(26, 0x00, 6, code & 0xfffff, 0, 0x0d); +} + +FixupBranch MIPSEmitter::J() { + // 000010 iiiiiiiiiiiiiiiiiiiiiiiiii (fix up) + FixupBranch b = MakeFixupBranch(BRANCH_26); + Write32Fields(26, 0x02); + return b; +} + +void MIPSEmitter::J(const void *func) { + SetJumpTarget(J(), func); +} + +FixupBranch MIPSEmitter::JAL() { + // 000011 iiiiiiiiiiiiiiiiiiiiiiiiii (fix up) + FixupBranch b = MakeFixupBranch(BRANCH_26); + Write32Fields(26, 0x03); + return b; +} + +void MIPSEmitter::JAL(const void *func) { + SetJumpTarget(JAL(), func); +} + +void MIPSEmitter::JR(MIPSReg rs) { + // 000000 sssss xxxxxxxxxx hint- 001000 (hint must be 0.) + _dbg_assert_msg_(JIT, rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 0, 0x08); +} + +void MIPSEmitter::JALR(MIPSReg rd, MIPSReg rs) { + // 000000 sssss xxxxx ddddd hint- 001001 (hint must be 0.) + _dbg_assert_msg_(JIT, rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 11, rd, 0, 0x09); +} + +FixupBranch MIPSEmitter::BEQ(MIPSReg rs, MIPSReg rt) { + // 000100 sssss ttttt iiiiiiiiiiiiiii (fix up) + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + FixupBranch b = MakeFixupBranch(BRANCH_16); + Write32Fields(26, 0x04, 21, rs, 16, rt); + return b; +} + +void MIPSEmitter::BEQ(MIPSReg rs, MIPSReg rt, const void *func) { + SetJumpTarget(BEQ(rs, rt), func); +} + +FixupBranch MIPSEmitter::BNE(MIPSReg rs, MIPSReg rt) { + // 000101 sssss ttttt iiiiiiiiiiiiiii (fix up) + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + FixupBranch b = MakeFixupBranch(BRANCH_16); + Write32Fields(26, 0x05, 21, rs, 16, rt); + return b; +} + +void MIPSEmitter::BNE(MIPSReg rs, MIPSReg rt, const void *func) { + SetJumpTarget(BNE(rs, rt), func); +} + +FixupBranch MIPSEmitter::BLEZ(MIPSReg rs) { + // 000110 sssss xxxxx iiiiiiiiiiiiiii (fix up) + _dbg_assert_msg_(JIT, rs < F_BASE, "Bad emitter arguments"); + FixupBranch b = MakeFixupBranch(BRANCH_16); + Write32Fields(26, 0x06, 21, rs); + return b; +} + +void MIPSEmitter::BLEZ(MIPSReg rs, const void *func) { + SetJumpTarget(BLEZ(rs), func); +} + +FixupBranch MIPSEmitter::BGTZ(MIPSReg rs) { + // 000111 sssss xxxxx iiiiiiiiiiiiiii (fix up) + _dbg_assert_msg_(JIT, rs < F_BASE, "Bad emitter arguments"); + FixupBranch b = MakeFixupBranch(BRANCH_16); + Write32Fields(26, 0x07, 21, rs); + return b; +} + +void MIPSEmitter::BGTZ(MIPSReg rs, const void *func) { + SetJumpTarget(BGTZ(rs), func); +} + +void MIPSEmitter::SetJumpTarget(const FixupBranch &branch) { + SetJumpTarget(branch, code_); +} + +bool MIPSEmitter::BInRange(const void *func) { + return BInRange(code_, func); +} + +bool MIPSEmitter::JInRange(const void *func) { + return JInRange(code_, func); +} + +void MIPSEmitter::SetJumpTarget(const FixupBranch &branch, const void *dst) { + const intptr_t srcp = (intptr_t)branch.ptr; + const intptr_t dstp = (intptr_t)dst; + u32 *fixup = (u32 *)branch.ptr; + + _dbg_assert_msg_(JIT, (dstp & 3) == 0, "Destination should be aligned"); + + if (branch.type == BRANCH_16) { + // The distance is encoded as words from the delay slot. + ptrdiff_t distance = (dstp - srcp - 4) >> 2; + _dbg_assert_msg_(JIT, BInRange(branch.ptr, dst), "Destination is too far away (%p -> %p)", branch.ptr, dst); + *fixup = (*fixup & 0xffff0000) | (distance & 0x0000ffff); + } else { + // Absolute, easy. + _dbg_assert_msg_(JIT, JInRange(branch.ptr, dst), "Destination is too far away (%p -> %p)", branch.ptr, dst); + *fixup = (*fixup & 0xfc000000) | ((dstp >> 2) & 0x03ffffff); + } +} + +bool MIPSEmitter::BInRange(const void *src, const void *dst) { + const intptr_t srcp = (intptr_t)src; + const intptr_t dstp = (intptr_t)dst; + + // The distance is encoded as words from the delay slot. + ptrdiff_t distance = (dstp - srcp - 4) >> 2; + return distance >= -0x8000 && distance < 0x8000; +} + +bool MIPSEmitter::JInRange(const void *src, const void *dst) { + const intptr_t srcp = (intptr_t)src; + const intptr_t dstp = (intptr_t)dst; + + return (srcp - (srcp & 0x0fffffff)) == (dstp - (dstp & 0x0fffffff)); +} + +void MIPSEmitter::QuickCallFunction(MIPSReg scratchreg, const void *func) { + _dbg_assert_msg_(JIT, scratchreg < F_BASE, "Bad emitter arguments"); + if (JInRange(func)) { + JAL(func); + } else { + // This may never happen. + MOVP2R(scratchreg, func); + JALR(scratchreg); + } +} + +FixupBranch MIPSEmitter::MakeFixupBranch(FixupBranchType type) { + FixupBranch b; + b.ptr = code_; + b.type = type; + return b; +} + +void MIPSEmitter::LB(MIPSReg value, MIPSReg base, s16 offset) { + // 100000 sssss ttttt iiiiiiiiiiiiiiii - rs = base, rt = value + _dbg_assert_msg_(JIT, value < F_BASE && base < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x20, 21, base, 16, value, 0, (u16)offset); +} + +void MIPSEmitter::LW(MIPSReg value, MIPSReg base, s16 offset) { + // 100011 sssss ttttt iiiiiiiiiiiiiiii - rs = base, rt = value + _dbg_assert_msg_(JIT, value < F_BASE && base < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x23, 21, base, 16, value, 0, (u16)offset); +} + +void MIPSEmitter::SB(MIPSReg value, MIPSReg base, s16 offset) { + // 101000 sssss ttttt iiiiiiiiiiiiiiii - rs = base, rt = value + _dbg_assert_msg_(JIT, value < F_BASE && base < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x28, 21, base, 16, value, 0, (u16)offset); +} + +void MIPSEmitter::SW(MIPSReg value, MIPSReg base, s16 offset) { + // 101011 sssss ttttt iiiiiiiiiiiiiiii - rs = base, rt = value + _dbg_assert_msg_(JIT, value < F_BASE && base < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x2b, 21, base, 16, value, 0, (u16)offset); +} + +void MIPSEmitter::SLL(MIPSReg rd, MIPSReg rt, u8 sa) { + // 000000 xxxxx ttttt ddddd aaaaa 000000 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && sa <= 0x1f, "Bad emitter arguments"); + Write32Fields(26, 0x00, 16, rt, 11, rd, 6, sa & 0x1f, 0, 0x00); +} + +void MIPSEmitter::SRL(MIPSReg rd, MIPSReg rt, u8 sa) { + // 000000 xxxxx ttttt ddddd aaaaa 000010 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && sa <= 0x1f, "Bad emitter arguments"); + Write32Fields(26, 0x00, 16, rt, 11, rd, 6, sa & 0x1f, 0, 0x02); +} + +void MIPSEmitter::SRA(MIPSReg rd, MIPSReg rt, u8 sa) { + // 000000 xxxxx ttttt ddddd aaaaa 000011 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && sa <= 0x1f, "Bad emitter arguments"); + Write32Fields(26, 0x00, 16, rt, 11, rd, 6, sa & 0x1f, 0, 0x03); +} + +void MIPSEmitter::SLLV(MIPSReg rd, MIPSReg rt, MIPSReg rs) { + // 000000 sssss ttttt ddddd xxxxx 000100 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x04); +} + +void MIPSEmitter::SRLV(MIPSReg rd, MIPSReg rt, MIPSReg rs) { + // 000000 sssss ttttt ddddd xxxxx 000110 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x06); +} + +void MIPSEmitter::SRAV(MIPSReg rd, MIPSReg rt, MIPSReg rs) { + // 000000 sssss ttttt ddddd xxxxx 000111 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x07); +} + +void MIPSEmitter::SLT(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd xxxxx 101010 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x2a); +} + +void MIPSEmitter::SLTU(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd xxxxx 101011 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x2b); +} + +void MIPSEmitter::SLTI(MIPSReg rt, MIPSReg rs, s16 imm) { + // 001010 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x0a, 21, rs, 16, rt, 0, (u16)imm); +} + +void MIPSEmitter::SLTIU(MIPSReg rt, MIPSReg rs, s16 imm) { + // 001011 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x0b, 21, rs, 16, rt, 0, (u16)imm); +} + +void MIPSEmitter::ADDU(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd 00000100001 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x21); +} + +void MIPSEmitter::SUBU(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd 00000100011 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x23); +} + +void MIPSEmitter::ADDIU(MIPSReg rt, MIPSReg rs, s16 imm) { + // 001001 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x09, 21, rs, 16, rt, 0, (u16)imm); +} + +void MIPSEmitter::AND(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd 00000100100 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x24); +} + +void MIPSEmitter::OR(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd 00000100101 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x25); +} + +void MIPSEmitter::XOR(MIPSReg rd, MIPSReg rs, MIPSReg rt) { + // 000000 sssss ttttt ddddd 00000100110 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && rs < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x00, 21, rs, 16, rt, 11, rd, 0, 0x26); +} + +void MIPSEmitter::ANDI(MIPSReg rt, MIPSReg rs, s16 imm) { + // 001100 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x0c, 21, rs, 16, rt, 0, (u16)imm); +} + +void MIPSEmitter::ORI(MIPSReg rt, MIPSReg rs, s16 imm) { + // 001101 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x0d, 21, rs, 16, rt, 0, (u16)imm); +} + +void MIPSEmitter::XORI(MIPSReg rt, MIPSReg rs, s16 imm) { + // 001110 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rs < F_BASE && rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x0e, 21, rs, 16, rt, 0, (u16)imm); +} + +void MIPSEmitter::LUI(MIPSReg rt, s16 imm) { + // 001111 sssss ttttt iiiiiiiiiiiiiiii + _dbg_assert_msg_(JIT, rt < F_BASE, "Bad emitter arguments"); + Write32Fields(26, 0x0f, 21, rt, 0, (u16)imm); +} + +void MIPSEmitter::DSLL(MIPSReg rd, MIPSReg rt, u8 sa) { + // 000000 xxxxx ttttt ddddd aaaaa 111000 DSLL + // 000000 xxxxx ttttt ddddd aaaaa 111100 DSLL32 + _dbg_assert_msg_(JIT, rd < F_BASE && rt < F_BASE && sa <= 0x3f, "Bad emitter arguments"); + // TODO: Assert MIPS64. + if (sa >= 32) { + Write32Fields(26, 0x00, 16, rt, 11, rd, 6, (sa - 32) & 0x1f, 0, 0x3c); + } else { + Write32Fields(26, 0x00, 16, rt, 11, rd, 6, sa & 0x1f, 0, 0x38); + } +} + +void MIPSEmitter::MOVI2R(MIPSReg reg, u64 imm) { + _dbg_assert_msg_(JIT, reg < F_BASE, "Bad emitter arguments"); + // TODO: Assert MIPS64. + + // Probably better to use a literal pool and load. + LUI(reg, imm >> 48); + ORI(reg, reg, (imm >> 32) & 0x0000ffff); + DSLL(reg, reg, 16); + ORI(reg, reg, (imm >> 16) & 0x0000ffff); + DSLL(reg, reg, 16); + ORI(reg, reg, (imm >> 0) & 0x0000ffff); +} + +void MIPSEmitter::MOVI2R(MIPSReg reg, u32 imm) { + _dbg_assert_msg_(JIT, reg < F_BASE, "Bad emitter arguments"); + + if ((imm & 0xffff0000) != 0) { +#if 0 + // TODO: CPUDetect MIPS64. Ideally allow emitter to emit MIPS32 on x64. + ORI(reg, R_ZERO, imm >> 16); + DSLL(reg, reg, 16); + ORI(reg, reg, imm & 0x0000ffff); +#else + LUI(reg, imm >> 16); + ORI(reg, reg, imm & 0x0000ffff); +#endif + } else { + ORI(reg, R_ZERO, imm & 0x0000ffff); + } +} + +void MIPSCodeBlock::AllocCodeSpace(int size) { + region_size = size; + region = (u8 *)AllocateExecutableMemory(region_size); + SetCodePtr(region); +} + +// Always clear code space with breakpoints, so that if someone accidentally executes +// uninitialized, it just breaks into the debugger. +void MIPSCodeBlock::ClearCodeSpace() { + // Set BREAK instructions on all of it. + u32 *region32 = (u32 *)region; + for (u32 i = 0; i < region_size / 4; ++i) { + *region32++ = 0x0000000d; + } + ResetCodePtr(); +} + +void MIPSCodeBlock::FreeCodeSpace() { + FreeMemoryPages(region, region_size); + region = NULL; + region_size = 0; +} + +void MIPSCodeBlock::WriteProtect() { + WriteProtectMemory(region, region_size, true); +} + +void MIPSCodeBlock::UnWriteProtect() { + UnWriteProtectMemory(region, region_size, false); +} + +} \ No newline at end of file diff --git a/Common/MipsEmitter.h b/Common/MipsEmitter.h new file mode 100644 index 0000000000..fec9393d0e --- /dev/null +++ b/Common/MipsEmitter.h @@ -0,0 +1,290 @@ +// Copyright (c) 2014- 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/. + +// WARNING - THIS LIBRARY IS NOT THREAD SAFE!!! (cargo culted but probably true) + +#pragma once + +#include +#include + +#include "Common.h" +#include "MsgHandler.h" + +namespace MIPSGen { + +enum MIPSReg { + R_ZERO = 0, + R_AT, + V0, V1, + + A0 = 4, A1 = 5, A2 = 6, A3 = 7, A4 = 8, A5 = 9, A6 = 10, A7 = 11, + // Alternate names depending on ABI. + T0 = 8, T1 = 9, T2 = 10, T3 = 11, + + T4, T5, T6, T7, + S0, S1, S2, S3, S4, S5, S6, S7, + T8, T9, + K0, K1, + R_GP, R_SP, R_FP, + R_RA, + + F_BASE = 32, + F0 = 32, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, + F16, F17, F18, F19, F20, F21, F22, F23, F24, F25, F26, F27, F28, F29, F30, F31, + + INVALID_REG = 0xFFFFFFFF +}; + +enum { + // All 32 except: ZERO, K0/K1 (kernel), RA. The rest are only convention. + NUMGPRs = 32 - 1 - 2 - 1, + NUMFPRs = 32, +}; + +enum FixupBranchType { + // 16-bit immediate jump/branch (to pc + (simm16 + 1 ops).) + BRANCH_16, + // 26-bit immediate jump/branch (to pc's 4 high bits + imm * 4.) + BRANCH_26, +}; + +// Beware of delay slots. +struct FixupBranch { + u8 *ptr; + FixupBranchType type; +}; + +class MIPSEmitter { +public: + MIPSEmitter() : code_(0), lastCacheFlushEnd_(0) { + } + MIPSEmitter(u8 *code_ptr) : code_(code_ptr), lastCacheFlushEnd_(code_ptr) { + SetCodePtr(code_ptr); + } + virtual ~MIPSEmitter() { + } + + void SetCodePtr(u8 *ptr); + void ReserveCodeSpace(u32 bytes); + const u8 *AlignCode16(); + const u8 *AlignCodePage(); + const u8 *GetCodePtr() const; + u8 *GetWritableCodePtr(); + void FlushIcache(); + void FlushIcacheSection(u8 *start, u8 *end); + + // 20 bits valid in code. + void BREAK(u32 code); + + void NOP() { + SLL(R_ZERO, R_ZERO, 0); + } + + FixupBranch J(); + void J(const void *func); + FixupBranch JAL(); + void JAL(const void *func); + void JR(MIPSReg rs); + void JRRA() { + JR(R_RA); + } + void JALR(MIPSReg rd, MIPSReg rs); + void JALR(MIPSReg rs) { + JALR(R_RA, rs); + } + + inline FixupBranch B() { + return BEQ(R_ZERO, R_ZERO); + } + inline void B(const void *func) { + return BEQ(R_ZERO, R_ZERO, func); + } + FixupBranch BEQ(MIPSReg rs, MIPSReg rt); + void BEQ(MIPSReg rs, MIPSReg rt, const void *func); + FixupBranch BNE(MIPSReg rs, MIPSReg rt); + void BNE(MIPSReg rs, MIPSReg rt, const void *func); + inline FixupBranch BEQZ(MIPSReg rs) { + return BEQ(rs, R_ZERO); + } + inline void BEQZ(MIPSReg rs, const void *func) { + return BEQ(rs, R_ZERO, func); + } + inline FixupBranch BNEZ(MIPSReg rs) { + return BNE(rs, R_ZERO); + } + inline void BNEZ(MIPSReg rs, const void *func) { + return BNE(rs, R_ZERO, func); + } + FixupBranch BLEZ(MIPSReg rs); + void BLEZ(MIPSReg rs, const void *func); + FixupBranch BGTZ(MIPSReg rs); + void BGTZ(MIPSReg rs, const void *func); + + void SetJumpTarget(const FixupBranch &branch); + bool BInRange(const void *func); + bool JInRange(const void *func); + + // R_AT is the stereotypical scratch reg, but it is not likely to be used. + void QuickCallFunction(MIPSReg scratchreg, const void *func); + template void QuickCallFunction(MIPSReg scratchreg, T func) { + QuickCallFunction(scratchreg, (const void *)func); + } + + void LB(MIPSReg dest, MIPSReg base, s16 offset); + void LW(MIPSReg dest, MIPSReg base, s16 offset); + void SB(MIPSReg value, MIPSReg base, s16 offset); + void SW(MIPSReg value, MIPSReg base, s16 offset); + + void SLL(MIPSReg rd, MIPSReg rt, u8 sa); + void SRL(MIPSReg rd, MIPSReg rt, u8 sa); + void SRA(MIPSReg rd, MIPSReg rt, u8 sa); + void SLLV(MIPSReg rd, MIPSReg rt, MIPSReg rs); + void SRLV(MIPSReg rd, MIPSReg rt, MIPSReg rs); + void SRAV(MIPSReg rd, MIPSReg rt, MIPSReg rs); + + void SLT(MIPSReg rd, MIPSReg rt, MIPSReg rs); + void SLTU(MIPSReg rd, MIPSReg rt, MIPSReg rs); + void SLTI(MIPSReg rd, MIPSReg rt, s16 imm); + // Note: very importantly, *sign* extends imm before an unsigned compare. + void SLTIU(MIPSReg rt, MIPSReg rs, s16 imm); + + // ADD/SUB/ADDI intentionally omitted. They are just versions that trap. + void ADDU(MIPSReg rd, MIPSReg rs, MIPSReg rt); + void SUBU(MIPSReg rd, MIPSReg rs, MIPSReg rt); + void ADDIU(MIPSReg rt, MIPSReg rs, s16 imm); + void SUBIU(MIPSReg rt, MIPSReg rs, s16 imm) { + ADDIU(rt, rs, -imm); + } + + void AND(MIPSReg rd, MIPSReg rs, MIPSReg rt); + void OR(MIPSReg rd, MIPSReg rs, MIPSReg rt); + void XOR(MIPSReg rd, MIPSReg rs, MIPSReg rt); + void ANDI(MIPSReg rt, MIPSReg rs, s16 imm); + void ORI(MIPSReg rt, MIPSReg rs, s16 imm); + void XORI(MIPSReg rt, MIPSReg rs, s16 imm); + + // Clears the lower bits. On MIPS64, the result is sign extended. + void LUI(MIPSReg rt, s16 imm); + + // MIPS64 only. Transparently uses DSLL32 to shift 32-63 bits. + void DSLL(MIPSReg rd, MIPSReg rt, u8 sa); + + void MOVI2R(MIPSReg reg, u64 val); + void MOVI2R(MIPSReg reg, s64 val) { + MOVI2R(reg, (u64)val); + } + void MOVI2R(MIPSReg reg, u32 val); + void MOVI2R(MIPSReg reg, s32 val) { + MOVI2R(reg, (u32)val); + } + template void MOVP2R(MIPSReg reg, T *val) { + MOVI2R(reg, (intptr_t)(const void *)val); + } + +protected: + inline void Write32(u32 value) { + *code32_++ = value; + } + + // Less parenthesis. + inline void Write32Fields(u8 pos1, u32 v1) { + *code32_++ = (v1 << pos1); + } + inline void Write32Fields(u8 pos1, u32 v1, u8 pos2, u32 v2) { + *code32_++ = (v1 << pos1) | (v2 << pos2); + } + inline void Write32Fields(u8 pos1, u32 v1, u8 pos2, u32 v2, u8 pos3, u32 v3) { + *code32_++ = (v1 << pos1) | (v2 << pos2) | (v3 << pos3); + } + inline void Write32Fields(u8 pos1, u32 v1, u8 pos2, u32 v2, u8 pos3, u32 v3, u8 pos4, u32 v4) { + *code32_++ = (v1 << pos1) | (v2 << pos2) | (v3 << pos3) | (v4 << pos4); + } + inline void Write32Fields(u8 pos1, u32 v1, u8 pos2, u32 v2, u8 pos3, u32 v3, u8 pos4, u32 v4, u8 pos5, u32 v5) { + *code32_++ = (v1 << pos1) | (v2 << pos2) | (v3 << pos3) | (v4 << pos5) | (v5 << pos5); + } + inline void Write32Fields(u8 pos1, u32 v1, u8 pos2, u32 v2, u8 pos3, u32 v3, u8 pos4, u32 v4, u8 pos5, u32 v5, u8 pos6, u32 v6) { + *code32_++ = (v1 << pos1) | (v2 << pos2) | (v3 << pos3) | (v4 << pos5) | (v5 << pos5) | (v6 << pos6); + } + + static void SetJumpTarget(const FixupBranch &branch, const void *dst); + static bool BInRange(const void *src, const void *dst); + static bool JInRange(const void *src, const void *dst); + FixupBranch MakeFixupBranch(FixupBranchType type); + +private: + union { + u8 *code_; + u32 *code32_; + }; + u8 *lastCacheFlushEnd_; +}; + +// Everything that needs to generate machine code should inherit from this. +// You get memory management for free, plus, you can use all the LUI etc functions without +// having to prefix them with gen-> or something similar. +class MIPSCodeBlock : public MIPSEmitter { +public: + MIPSCodeBlock() : region(nullptr), region_size(0) { + } + virtual ~MIPSCodeBlock() { + if (region) { + FreeCodeSpace(); + } + } + + // Call this before you generate any code. + void AllocCodeSpace(int size); + + // Always clear code space with breakpoints, so that if someone accidentally executes + // uninitialized, it just breaks into the debugger. + void ClearCodeSpace(); + + // Call this when shutting down. Don't rely on the destructor, even though it'll do the job. + void FreeCodeSpace(); + + bool IsInSpace(const u8 *ptr) const { + return ptr >= region && ptr < region + region_size; + } + + // Can possibly be undone. Will write protect the entire code region. + // Start over if you need to change the code, though (call FreeCodeSpace(), AllocCodeSpace().) + void WriteProtect(); + void UnWriteProtect(); + + void ResetCodePtr() { + SetCodePtr(region); + } + + size_t GetSpaceLeft() const { + return region_size - (GetCodePtr() - region); + } + + u8 *GetBasePtr() { + return region; + } + + size_t GetOffset(const u8 *ptr) const { + return ptr - region; + } + +protected: + u8 *region; + size_t region_size; +}; + +}; diff --git a/Core/MIPS/JitCommon/JitBlockCache.h b/Core/MIPS/JitCommon/JitBlockCache.h index 8ca96f62ed..09953b611e 100644 --- a/Core/MIPS/JitCommon/JitBlockCache.h +++ b/Core/MIPS/JitCommon/JitBlockCache.h @@ -49,6 +49,11 @@ typedef Gen::XCodeBlock CodeBlock; namespace PpcGen { class PPCXEmitter; } using namespace PpcGen; typedef PpcGen::PPCXCodeBlock CodeBlock; +#elif defined(MIPS) +#include "Common/MipsEmitter.h" +namespace MIPSGen { class MIPSXEmitter; } +using namespace MIPSGen; +typedef MIPSGen::MIPSXCodeBlock CodeBlock; #else #warning "Unsupported arch!" #include "Common/FakeEmitter.h" diff --git a/Core/MIPS/JitCommon/JitCommon.h b/Core/MIPS/JitCommon/JitCommon.h index a36620382c..06f19a13a9 100644 --- a/Core/MIPS/JitCommon/JitCommon.h +++ b/Core/MIPS/JitCommon/JitCommon.h @@ -34,6 +34,8 @@ struct JitBlock; #include "../ARM/ArmJit.h" #elif defined(_M_IX86) || defined(_M_X64) #include "../x86/Jit.h" +#elif defined(MIPS) +#include "../MIPS/MipsJit.h" #else #include "../fake/FakeJit.h" #endif diff --git a/Core/MIPS/MIPS.cpp b/Core/MIPS/MIPS.cpp index af602d8cc8..006af47a3c 100644 --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -38,6 +38,8 @@ #include "ARM/ArmJit.h" #elif defined(_M_IX86) || defined(_M_X64) #include "x86/Jit.h" +#elif defined(MIPS) +#include "MIPS/MipsJit.h" #else #include "fake/FakeJit.h" #endif diff --git a/Core/MIPS/MIPS/MipsJit.cpp b/Core/MIPS/MIPS/MipsJit.cpp new file mode 100644 index 0000000000..96d7f54d3a --- /dev/null +++ b/Core/MIPS/MIPS/MipsJit.cpp @@ -0,0 +1,360 @@ +// 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/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/ReplaceTables.h" + +#include "MipsJit.h" +#include "CPUDetect.h" + +void DisassembleMIPS(const u8 *data, int size) { +} + +namespace MIPSComp +{ + +MIPSJitOptions::MIPSJitOptions() { + enableBlocklink = true; + immBranches = false; + continueBranches = false; + continueJumps = false; + continueMaxInstructions = 300; +} + +Jit::Jit(MIPSState *mips) : blocks(mips, this), mips_(mips) +{ + logBlocks = 0; + dontLogBlocks = 0; + blocks.Init(); + AllocCodeSpace(1024 * 1024 * 16); + js.startDefaultPrefix = mips_->HasDefaultPrefix(); +} + +void Jit::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; + } +} + +// This is here so the savestate matches between jit and non-jit. +void Jit::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 Jit::FlushAll() +{ + //gpr.FlushAll(); + //fpr.FlushAll(); + FlushPrefixV(); +} + +void Jit::FlushPrefixV() +{ +} + +void Jit::ClearCache() +{ + blocks.Clear(); + ClearCodeSpace(); + //GenerateFixedCode(); +} + +void Jit::InvalidateCache() +{ + blocks.Clear(); +} + +void Jit::InvalidateCacheAt(u32 em_address, int length) +{ + blocks.InvalidateICache(em_address, length); +} + +void Jit::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 Jit::CompileDelaySlot(int flags) +{ + //if (flags & DELAYSLOT_SAFE) + // Save flags here + + js.inDelaySlot = true; + MIPSOpcode op = Memory::Read_Opcode_JIT(js.compilerPC + 4); + MIPSCompileOp(op); + js.inDelaySlot = false; + + if (flags & DELAYSLOT_FLUSH) + FlushAll(); + //if (flags & DELAYSLOT_SAFE) + // Restore flags here +} + + +void Jit::Compile(u32 em_address) { + if (GetSpaceLeft() < 0x10000 || blocks.IsFull()) { + ClearCache(); + } + int block_num = blocks.AllocateBlock(em_address); + JitBlock *b = blocks.GetBlock(block_num); + DoJit(em_address, b); + blocks.FinalizeBlock(block_num, jo.enableBlocklink); + + 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; + } + + if (cleanSlate) { + // Our assumptions are all wrong so it's clean-slate time. + ClearCache(); + Compile(em_address); + } +} + +void Jit::RunLoopUntil(u64 globalticks) +{ + ((void (*)())enterCode)(); +} + +const u8 *Jit::DoJit(u32 em_address, JitBlock *b) +{ + js.cancel = false; + js.blockStart = js.compilerPC = mips_->pc; + js.lastContinuedPC = 0; + js.initialBlockSize = 0; + js.nextExit = 0; + js.downcountAmount = 0; + js.curBlock = b; + js.compiling = true; + js.inDelaySlot = false; + js.PrefixStart(); + b->normalEntry = GetCodePtr(); + js.numInstructions = 0; + while (js.compiling) + { + MIPSOpcode inst = Memory::Read_Opcode_JIT(js.compilerPC); + js.downcountAmount += MIPSGetInstructionCycleEstimate(inst); + + MIPSCompileOp(inst); + + js.compilerPC += 4; + js.numInstructions++; + + // Safety check, in case we get a bunch of really large jit ops without a lot of branching. + if (GetSpaceLeft() < 0x800 || js.numInstructions >= JitBlockCache::MAX_BLOCK_INSTRUCTIONS) + { + FlushAll(); + WriteExit(js.compilerPC, js.nextExit++); + js.compiling = false; + } + } + + b->codeSize = GetCodePtr() - b->normalEntry; + + // Don't forget to zap the newly written instructions in the instruction cache! + FlushIcache(); + + if (js.lastContinuedPC == 0) + b->originalSize = js.numInstructions; + else + { + // We continued at least once. Add the last proxy and set the originalSize correctly. + blocks.ProxyBlock(js.blockStart, js.lastContinuedPC, (js.compilerPC - js.lastContinuedPC) / sizeof(u32), GetCodePtr()); + b->originalSize = js.initialBlockSize; + } + + return b->normalEntry; +} + +void Jit::AddContinuedBlock(u32 dest) +{ + // The first block is the root block. When we continue, we create proxy blocks after that. + if (js.lastContinuedPC == 0) + js.initialBlockSize = js.numInstructions; + else + blocks.ProxyBlock(js.blockStart, js.lastContinuedPC, (js.compilerPC - js.lastContinuedPC) / sizeof(u32), GetCodePtr()); + js.lastContinuedPC = dest; +} + +bool Jit::DescribeCodePtr(const u8 *ptr, std::string &name) +{ + // TODO: Not used by anything yet. + return false; +} + +void Jit::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!"); +} + +bool Jit::ReplaceJalTo(u32 dest) { + return true; +} + +void Jit::Comp_ReplacementFunc(MIPSOpcode op) +{ +} + +void Jit::Comp_Generic(MIPSOpcode op) +{ + FlushAll(); + MIPSInterpretFunc func = MIPSGetInterpretFunc(op); + if (func) + { + //SaveDowncount(); + RestoreRoundingMode(); + // Move Imm32(js.compilerPC) in to M(&mips_->pc) + QuickCallFunction(V1, (void *)func); + ApplyRoundingMode(); + //RestoreDowncount(); + } + + 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(); + } +} + +void Jit::MovFromPC(MIPSReg r) { +} + +void Jit::MovToPC(MIPSReg r) { +} + +void Jit::SaveDowncount() { +} + +void Jit::RestoreDowncount() { +} + +void Jit::WriteDownCount(int offset) { +} + +void Jit::WriteDownCountR(MIPSReg reg) { +} + +void Jit::RestoreRoundingMode(bool force) { +} + +void Jit::ApplyRoundingMode(bool force) { +} + +void Jit::UpdateRoundingMode() { +} + +void Jit::WriteExit(u32 destination, int exit_num) +{ + //WriteDownCount(); + JitBlock *b = js.curBlock; + b->exitAddress[exit_num] = destination; + b->exitPtrs[exit_num] = GetWritableCodePtr(); + + // Link opportunity! + int block = blocks.GetBlockNumberFromStartAddress(destination); + if (block >= 0 && jo.enableBlocklink) { + // It exists! Joy of joy! + B(blocks.GetBlock(block)->checkedEntry); + b->linkStatus[exit_num] = true; + } else { + //gpr.SetRegImm(V0, destination); + //B((const void *)dispatcherPCInV0); + } +} + +void Jit::WriteExitDestInR(MIPSReg Reg) +{ + MovToPC(Reg); + //WriteDownCount(); + // TODO: shouldn't need an indirect branch here... + B((const void *)dispatcher); +} + +void Jit::WriteSyscallExit() +{ + //WriteDownCount(); + B((const void *)dispatcherCheckCoreState); +} + +#define _RS ((op>>21) & 0x1F) +#define _RT ((op>>16) & 0x1F) +#define _RD ((op>>11) & 0x1F) +#define _FS ((op>>11) & 0x1F) +#define _FT ((op>>16) & 0x1F) +#define _FD ((op>>6) & 0x1F) +#define _POS ((op>>6) & 0x1F) +#define _SIZE ((op>>11) & 0x1F) + +//memory regions: +// +// 08-0A +// 48-4A +// 04-05 +// 44-45 +// mov eax, addrreg + // shr eax, 28 +// mov eax, [table+eax] +// mov dreg, [eax+offreg] + +} diff --git a/Core/MIPS/MIPS/MipsJit.h b/Core/MIPS/MIPS/MipsJit.h new file mode 100644 index 0000000000..13d5b4bdfb --- /dev/null +++ b/Core/MIPS/MIPS/MipsJit.h @@ -0,0 +1,194 @@ +// 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 "Core/MIPS/JitCommon/JitState.h" +#include "Core/MIPS/JitCommon/JitBlockCache.h" +#include "../MIPSVFPUUtils.h" + +#ifndef offsetof +#include "stddef.h" +#endif + +namespace MIPSComp +{ + +struct MIPSJitOptions +{ + MIPSJitOptions(); + + bool enableBlocklink; + bool immBranches; + bool continueBranches; + bool continueJumps; + int continueMaxInstructions; +}; + +class Jit : public MIPSGen::MIPSXCodeBlock +{ +public: + Jit(MIPSState *mips); + + void DoState(PointerWrap &p); + static void DoDummyState(PointerWrap &p); + + // Compiled ops should ignore delay slots + // the compiler will take care of them by itself + // OR NOT + void Comp_Generic(MIPSOpcode op); + + void RunLoopUntil(u64 globalticks); + + void Compile(u32 em_address); // Compiles a block at current MIPS PC + const u8 *DoJit(u32 em_address, JitBlock *b); + + bool DescribeCodePtr(const u8 *ptr, std::string &name); + + void CompileDelaySlot(int flags); + void EatInstruction(MIPSOpcode op); + void AddContinuedBlock(u32 dest); + + void Comp_RunBlock(MIPSOpcode op); + void Comp_ReplacementFunc(MIPSOpcode op); + + // Ops + void Comp_ITypeMem(MIPSOpcode op) {} + void Comp_Cache(MIPSOpcode op) {} + + void Comp_RelBranch(MIPSOpcode op) {} + void Comp_RelBranchRI(MIPSOpcode op) {} + void Comp_FPUBranch(MIPSOpcode op) {} + void Comp_FPULS(MIPSOpcode op) {} + void Comp_FPUComp(MIPSOpcode op) {} + void Comp_Jump(MIPSOpcode op) {} + void Comp_JumpReg(MIPSOpcode op) {} + void Comp_Syscall(MIPSOpcode op) {} + void Comp_Break(MIPSOpcode op) {} + + void Comp_IType(MIPSOpcode op) {} + void Comp_RType2(MIPSOpcode op) {} + void Comp_RType3(MIPSOpcode op) {} + void Comp_ShiftType(MIPSOpcode op) {} + void Comp_Allegrex(MIPSOpcode op) {} + void Comp_Allegrex2(MIPSOpcode op) {} + void Comp_VBranch(MIPSOpcode op) {} + void Comp_MulDivType(MIPSOpcode op) {} + void Comp_Special3(MIPSOpcode op) {} + + void Comp_FPU3op(MIPSOpcode op) {} + void Comp_FPU2op(MIPSOpcode op) {} + void Comp_mxc1(MIPSOpcode op) {} + + void Comp_DoNothing(MIPSOpcode op) {} + + void Comp_SV(MIPSOpcode op) {} + void Comp_SVQ(MIPSOpcode op) {} + void Comp_VPFX(MIPSOpcode op) {} + void Comp_VVectorInit(MIPSOpcode op) {} + void Comp_VMatrixInit(MIPSOpcode op) {} + void Comp_VDot(MIPSOpcode op) {} + void Comp_VecDo3(MIPSOpcode op) {} + void Comp_VV2Op(MIPSOpcode op) {} + void Comp_Mftv(MIPSOpcode op) {} + void Comp_Vmfvc(MIPSOpcode op) {} + void Comp_Vmtvc(MIPSOpcode op) {} + void Comp_Vmmov(MIPSOpcode op) {} + void Comp_VScl(MIPSOpcode op) {} + void Comp_Vmmul(MIPSOpcode op) {} + void Comp_Vmscl(MIPSOpcode op) {} + void Comp_Vtfm(MIPSOpcode op) {} + void Comp_VHdp(MIPSOpcode op) {} + void Comp_VCrs(MIPSOpcode op) {} + void Comp_VDet(MIPSOpcode op) {} + void Comp_Vi2x(MIPSOpcode op) {} + void Comp_Vx2i(MIPSOpcode op) {} + void Comp_Vf2i(MIPSOpcode op) {} + void Comp_Vi2f(MIPSOpcode op) {} + void Comp_Vh2f(MIPSOpcode op) {} + void Comp_Vcst(MIPSOpcode op) {} + void Comp_Vhoriz(MIPSOpcode op) {} + void Comp_VRot(MIPSOpcode op) {} + void Comp_VIdt(MIPSOpcode op) {} + void Comp_Vcmp(MIPSOpcode op) {} + void Comp_Vcmov(MIPSOpcode op) {} + void Comp_Viim(MIPSOpcode op) {} + void Comp_Vfim(MIPSOpcode op) {} + void Comp_VCrossQuat(MIPSOpcode op) {} + void Comp_Vsgn(MIPSOpcode op) {} + void Comp_Vocp(MIPSOpcode op) {} + + int Replace_fabsf() { return 0; } + + JitBlockCache *GetBlockCache() { return &blocks; } + + void ClearCache(); + void InvalidateCache(); + void InvalidateCacheAt(u32 em_address, int length = 4); + + void EatPrefix() { js.EatPrefix(); } + +private: + void GenerateFixedCode(); + void FlushAll(); + void FlushPrefixV(); + + void WriteDownCount(int offset = 0); + void WriteDownCountR(MIPSReg reg); + void RestoreRoundingMode(bool force = false); + void ApplyRoundingMode(bool force = false); + void UpdateRoundingMode(); + void MovFromPC(MIPSReg r); + void MovToPC(MIPSReg r); + + bool ReplaceJalTo(u32 dest); + + void SaveDowncount(); + void RestoreDowncount(); + + void WriteExit(u32 destination, int exit_num); + void WriteExitDestInR(MIPSReg Reg); + void WriteSyscallExit(); + + JitBlockCache blocks; + MIPSJitOptions jo; + JitState js; + + MIPSState *mips_; + + int dontLogBlocks; + int logBlocks; + +public: + // Code pointers + const u8 *enterCode; + + const u8 *outerLoop; + const u8 *outerLoopPCInR0; + const u8 *dispatcherCheckCoreState; + const u8 *dispatcherPCInR0; + const u8 *dispatcher; + const u8 *dispatcherNoCheck; + + const u8 *breakpointBailout; +}; + +typedef void (Jit::*MIPSCompileFunc)(MIPSOpcode opcode); +typedef int (Jit::*MIPSReplaceFunc)(); + +} // namespace MIPSComp + diff --git a/Core/MIPS/fake/FakeJit.cpp b/Core/MIPS/fake/FakeJit.cpp index ec443239db..792069b601 100644 --- a/Core/MIPS/fake/FakeJit.cpp +++ b/Core/MIPS/fake/FakeJit.cpp @@ -40,10 +40,6 @@ namespace MIPSComp FakeJitOptions::FakeJitOptions() { enableBlocklink = true; - downcountInRegister = true; - useBackJump = false; - useForwardJump = false; - cachePointers = true; immBranches = false; continueBranches = false; continueJumps = false; @@ -89,6 +85,8 @@ void Jit::DoDummyState(PointerWrap &p) void Jit::FlushAll() { + //gpr.FlushAll(); + //fpr.FlushAll(); FlushPrefixV(); } @@ -100,6 +98,7 @@ void Jit::ClearCache() { blocks.Clear(); ClearCodeSpace(); + //GenerateFixedCode(); } void Jit::InvalidateCache() @@ -113,6 +112,17 @@ void Jit::InvalidateCacheAt(u32 em_address, int length) } void Jit::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 Jit::CompileDelaySlot(int flags) diff --git a/Core/MIPS/fake/FakeJit.h b/Core/MIPS/fake/FakeJit.h index 6d61d91c77..d0014e11c5 100644 --- a/Core/MIPS/fake/FakeJit.h +++ b/Core/MIPS/fake/FakeJit.h @@ -32,12 +32,7 @@ struct FakeJitOptions { FakeJitOptions(); - bool useNEONVFPU; bool enableBlocklink; - bool downcountInRegister; - bool useBackJump; - bool useForwardJump; - bool cachePointers; bool immBranches; bool continueBranches; bool continueJumps; @@ -169,24 +164,6 @@ private: void WriteExitDestInR(FakeReg Reg); void WriteSyscallExit(); - // Utility compilation functions - void BranchFPFlag(MIPSOpcode op, FakeGen::CCFlags cc, bool likely); - void BranchVFPUFlag(MIPSOpcode op, FakeGen::CCFlags cc, bool likely); - void BranchRSZeroComp(MIPSOpcode op, FakeGen::CCFlags cc, bool andLink, bool likely); - void BranchRSRTComp(MIPSOpcode op, FakeGen::CCFlags cc, bool likely); - - // Utilities to reduce duplicated code - void CompImmLogic(MIPSGPReg rs, MIPSGPReg rt, u32 uimm, void (FakeXEmitter::*arith)(FakeReg dst, FakeReg src, Operand2 op2), bool (FakeXEmitter::*tryArithI2R)(FakeReg dst, FakeReg src, u32 val), u32 (*eval)(u32 a, u32 b)); - void CompType3(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt, void (FakeXEmitter::*arithOp2)(FakeReg dst, FakeReg rm, Operand2 rn), bool (FakeXEmitter::*tryArithI2R)(FakeReg dst, FakeReg rm, u32 val), u32 (*eval)(u32 a, u32 b), bool symmetric = false); - - void CompShiftImm(MIPSOpcode op, FakeGen::ShiftType shiftType, int sa); - void CompShiftVar(MIPSOpcode op, FakeGen::ShiftType shiftType); - - // Utils - void SetR0ToEffectiveAddress(MIPSGPReg rs, s16 offset); - void SetCCAndR0ForSafeAddress(MIPSGPReg rs, s16 offset, FakeReg tempReg, bool reverse = false); - void Comp_ITypeMemLR(MIPSOpcode op, bool load); - JitBlockCache blocks; FakeJitOptions jo; JitState js; diff --git a/GPU/Common/VertexDecoderCommon.h b/GPU/Common/VertexDecoderCommon.h index e80a840bfe..183624a5f0 100644 --- a/GPU/Common/VertexDecoderCommon.h +++ b/GPU/Common/VertexDecoderCommon.h @@ -27,6 +27,8 @@ #include "Common/ArmEmitter.h" #elif defined(_M_IX86) || defined(_M_X64) #include "Common/x64Emitter.h" +#elif defined(MIPS) +#include "Common/MipsEmitter.h" #else #include "Common/FakeEmitter.h" #endif @@ -579,6 +581,8 @@ public: class VertexDecoderJitCache : public ArmGen::ARMXCodeBlock { #elif defined(_M_IX86) || defined(_M_X64) class VertexDecoderJitCache : public Gen::XCodeBlock { +#elif defined(MIPS) +class VertexDecoderJitCache : public MIPSGen::MIPSXCodeBlock { #else class VertexDecoderJitCache : public FakeGen::FakeXCodeBlock { #endif diff --git a/GPU/Common/VertexDecoderFake.cpp b/GPU/Common/VertexDecoderFake.cpp index 7cd200d0f0..aa8e0c3934 100644 --- a/GPU/Common/VertexDecoderFake.cpp +++ b/GPU/Common/VertexDecoderFake.cpp @@ -26,7 +26,11 @@ static const float by128 = 1.0f / 128.0f; static const float by16384 = 1.0f / 16384.0f; static const float by32768 = 1.0f / 32768.0f; +#ifdef MIPS +using namespace MIPSGen; +#else using namespace FakeGen; +#endif static const JitLookup jitLookup[] = { {&VertexDecoder::Step_WeightsU8, &VertexDecoderJitCache::Jit_WeightsU8},