From e271e43ec560e34d7a9476825f246785a12f7a4f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 16 Jul 2023 18:54:59 -0700 Subject: [PATCH] riscv: Initial staffolding for IR based jit. --- CMakeLists.txt | 2 + Core/Core.vcxproj | 2 + Core/Core.vcxproj.filters | 9 ++++ Core/MIPS/IR/IRJit.h | 4 +- Core/MIPS/JitCommon/JitCommon.cpp | 4 ++ Core/MIPS/RiscV/RiscVJit.cpp | 79 +++++++++++++++++++++++++++++++ Core/MIPS/RiscV/RiscVJit.h | 53 +++++++++++++++++++++ 7 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Core/MIPS/RiscV/RiscVJit.cpp create mode 100644 Core/MIPS/RiscV/RiscVJit.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 452b74f1a8..9ba6ab7773 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1609,6 +1609,8 @@ list(APPEND CoreExtra ) list(APPEND CoreExtra + Core/MIPS/RiscV/RiscVJit.cpp + Core/MIPS/RiscV/RiscVJit.h GPU/Common/VertexDecoderRiscV.cpp ) diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 5a347d4006..ddbaa5a692 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -593,6 +593,7 @@ + @@ -1161,6 +1162,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index 49c2a10c7b..d3efd2f156 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -91,6 +91,9 @@ {678fa299-0ff7-4983-982d-2da47b52e238} + + {067e3128-3aaf-4ed1-b19e-bab11606abe7} + @@ -1204,6 +1207,9 @@ Core + + MIPS\RiscV + @@ -1950,6 +1956,9 @@ Core + + MIPS\RiscV + diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h index 199c1f95e1..756a584ddf 100644 --- a/Core/MIPS/IR/IRJit.h +++ b/Core/MIPS/IR/IRJit.h @@ -170,8 +170,8 @@ public: void LinkBlock(u8 *exitPoint, const u8 *checkedEntry) override; void UnlinkBlock(u8 *checkedEntry, u32 originalAddress) override; -private: - bool CompileBlock(u32 em_address, std::vector &instructions, u32 &mipsBytes, bool preload); +protected: + virtual bool CompileBlock(u32 em_address, std::vector &instructions, u32 &mipsBytes, bool preload); JitOptions jo; diff --git a/Core/MIPS/JitCommon/JitCommon.cpp b/Core/MIPS/JitCommon/JitCommon.cpp index 20a8fb6e74..4156732d5d 100644 --- a/Core/MIPS/JitCommon/JitCommon.cpp +++ b/Core/MIPS/JitCommon/JitCommon.cpp @@ -45,6 +45,8 @@ #include "../x86/Jit.h" #elif PPSSPP_ARCH(MIPS) #include "../MIPS/MipsJit.h" +#elif PPSSPP_ARCH(RISCV64) +#include "../RiscV/RiscVJit.h" #else #include "../fake/FakeJit.h" #endif @@ -108,6 +110,8 @@ namespace MIPSComp { return new MIPSComp::Jit(mipsState); #elif PPSSPP_ARCH(MIPS) return new MIPSComp::MipsJit(mipsState); +#elif PPSSPP_ARCH(RISCV64) + return new MIPSComp::RiscVJit(mipsState); #else return new MIPSComp::FakeJit(mipsState); #endif diff --git a/Core/MIPS/RiscV/RiscVJit.cpp b/Core/MIPS/RiscV/RiscVJit.cpp new file mode 100644 index 0000000000..a4045947d8 --- /dev/null +++ b/Core/MIPS/RiscV/RiscVJit.cpp @@ -0,0 +1,79 @@ +// Copyright (c) 2023- 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/MIPS/RiscV/RiscVJit.h" +#include "Common/Profiler/Profiler.h" + +namespace MIPSComp { + +RiscVJit::RiscVJit(MIPSState *mipsState) : IRJit(mipsState) { + AllocCodeSpace(1024 * 1024 * 16); + + // TODO: gpr, fpr, GenerateFixedCode(jo); +} + +void RiscVJit::RunLoopUntil(u64 globalticks) { + PROFILE_THIS_SCOPE("jit"); + ((void (*)())enterDispatcher_)(); +} + +bool RiscVJit::CompileBlock(u32 em_address, std::vector &instructions, u32 &mipsBytes, bool preload) { + if (!IRJit::CompileBlock(em_address, instructions, mipsBytes, preload)) + return false; + + // TODO: Compile native. + return true; +} + +bool RiscVJit::DescribeCodePtr(const u8 *ptr, std::string &name) { + // TODO: Describe for debugging / profiling. + return false; +} + +bool RiscVJit::CodeInRange(const u8 *ptr) const { + return IsInSpace(ptr); +} + +bool RiscVJit::IsAtDispatchFetch(const u8 *ptr) const { + // TODO + return false; +} + +const u8 *RiscVJit::GetDispatcher() const { + // TODO + return nullptr; +} + +const u8 *RiscVJit::GetCrashHandler() const { + // TODO: Implement a crash handler + return nullptr; +} + +void RiscVJit::ClearCache() { + IRJit::ClearCache(); + + ClearCodeSpace(jitStartOffset_); + FlushIcacheSection(region + jitStartOffset_, region + region_size - jitStartOffset_); +} + +void RiscVJit::UpdateFCR31() { + IRJit::UpdateFCR31(); + + // TODO: Handle rounding modes. +} + +} // namespace MIPSComp diff --git a/Core/MIPS/RiscV/RiscVJit.h b/Core/MIPS/RiscV/RiscVJit.h new file mode 100644 index 0000000000..b98fa7d183 --- /dev/null +++ b/Core/MIPS/RiscV/RiscVJit.h @@ -0,0 +1,53 @@ +// Copyright (c) 2023- 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 "Common/RiscVEmitter.h" +#include "Core/MIPS/IR/IRJit.h" + +namespace MIPSComp { + +class RiscVJit : public RiscVGen::RiscVCodeBlock, public IRJit { +public: + RiscVJit(MIPSState *mipsState); + + void RunLoopUntil(u64 globalticks) override; + + bool DescribeCodePtr(const u8 *ptr, std::string &name) override; + bool CodeInRange(const u8 *ptr) const override; + bool IsAtDispatchFetch(const u8 *ptr) const override; + const u8 *GetDispatcher() const override; + const u8 *GetCrashHandler() const override; + + void ClearCache() override; + void UpdateFCR31() override; + + // TODO: GetBlockCacheDebugInterface, block linking? + +protected: + bool CompileBlock(u32 em_address, std::vector &instructions, u32 &mipsBytes, bool preload) override; + +private: + void GenerateFixedCode(const JitOptions &jo); + + const u8 *enterDispatcher_ = nullptr; + + int jitStartOffset_ = 0; +}; + +} // namespace MIPSComp