CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/MIPS/JitCommon/JitBlockCache.h
Views: 1401
// Copyright (c) 2012- PPSSPP Project / Dolphin Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <cstdint>20#include <map>21#include <unordered_map>22#include <vector>23#include <string>2425#include "ppsspp_config.h"26#include "Common/CommonTypes.h"27#include "Common/CodeBlock.h"28#include "Core/MIPS/MIPS.h"2930#if PPSSPP_ARCH(ARM) || PPSSPP_ARCH(ARM64)31const int MAX_JIT_BLOCK_EXITS = 4;32#else33const int MAX_JIT_BLOCK_EXITS = 8;34#endif35constexpr bool JIT_USE_COMPILEDHASH = true;3637struct BlockCacheStats {38int numBlocks;39float avgBloat; // In code bytes, not instructions!40float minBloat;41u32 minBloatBlock;42float maxBloat;43u32 maxBloatBlock;44};4546enum class DestroyType {47DESTROY,48INVALIDATE,49// Skips jit unlink, since it'll be poisoned anyway.50CLEAR,51};5253// Define this in order to get VTune profile support for the Jit generated code.54// Add the VTune include/lib directories to the project directories to get this to build.55// #define USE_VTUNE5657// We should be careful not to access these block structures during runtime as they are large.58// Fine to mess with them at block compile time though.59struct JitBlock {60bool ContainsAddress(u32 em_address) const;6162const u8 *checkedEntry; // const, we have to translate to writable.63const u8 *normalEntry;6465u8 *exitPtrs[MAX_JIT_BLOCK_EXITS]; // to be able to rewrite the exit jump66u32 exitAddress[MAX_JIT_BLOCK_EXITS]; // 0xFFFFFFFF == unknown6768u32 originalAddress;69MIPSOpcode originalFirstOpcode; //to be able to restore70uint64_t compiledHash;71u16 codeSize;72u16 originalSize;73u16 blockNum;7475bool invalid;76bool linkStatus[MAX_JIT_BLOCK_EXITS];7778#ifdef USE_VTUNE79char blockName[32];80#endif8182// By having a pointer, we avoid a constructor/destructor being generated and dog slow83// performance in debug.84std::vector<u32> *proxyFor;8586bool IsPureProxy() const {87return originalFirstOpcode.encoding == 0x68FF0000;88}89void SetPureProxy() {90// Magic number that won't be a real opcode.91originalFirstOpcode.encoding = 0x68FF0000;92}93};9495typedef void (*CompiledCode)();9697struct JitBlockDebugInfo {98uint32_t originalAddress;99std::vector<std::string> origDisasm;100std::vector<std::string> irDisasm; // if any101std::vector<std::string> targetDisasm;102};103104struct JitBlockMeta {105bool valid;106uint32_t addr;107uint32_t sizeInBytes;108};109110struct JitBlockProfileStats {111int64_t executions;112int64_t totalNanos;113};114115class JitBlockCacheDebugInterface {116public:117virtual int GetNumBlocks() const = 0;118virtual int GetBlockNumberFromStartAddress(u32 em_address, bool realBlocksOnly = true) const = 0;119virtual JitBlockDebugInfo GetBlockDebugInfo(int blockNum) const = 0; // Expensive120virtual JitBlockMeta GetBlockMeta(int blockNum) const = 0;121virtual JitBlockProfileStats GetBlockProfileStats(int blockNum) const = 0;122virtual void ComputeStats(BlockCacheStats &bcStats) const = 0;123virtual bool IsValidBlock(int blockNum) const = 0;124virtual bool SupportsProfiling() const { return false; }125126virtual ~JitBlockCacheDebugInterface() {}127};128129class JitBlockCache : public JitBlockCacheDebugInterface {130public:131JitBlockCache(MIPSState *mipsState, CodeBlockCommon *codeBlock);132~JitBlockCache();133134int AllocateBlock(u32 em_address);135// When a proxy block is invalidated, the block located at the rootAddress is invalidated too.136void ProxyBlock(u32 rootAddress, u32 startAddress, u32 size, const u8 *codePtr);137void FinalizeBlock(int block_num, bool block_link);138139void Clear();140void Init();141void Shutdown();142void Reset();143144bool IsFull() const;145void ComputeStats(BlockCacheStats &bcStats) const override;146147// Code Cache148JitBlock *GetBlock(int block_num);149const JitBlock *GetBlock(int block_num) const;150151// Fast way to get a block. Only works on the first source-cpu instruction of a block.152int GetBlockNumberFromStartAddress(u32 em_address, bool realBlocksOnly = true) const override;153154// slower, but can get numbers from within blocks, not just the first instruction.155// WARNING! WILL NOT WORK WITH JIT INLINING ENABLED (not yet a feature but will be soon)156// Returns a list of block numbers - only one block can start at a particular address, but they CAN overlap.157// This one is slow so should only be used for one-shots from the debugger UI, not for anything during runtime.158void GetBlockNumbersFromAddress(u32 em_address, std::vector<int> *block_numbers);159// Similar to above, but only the first matching address.160int GetBlockNumberFromAddress(u32 em_address);161int GetBlockNumberFromEmuHackOp(MIPSOpcode inst, bool ignoreBad = false) const;162163u32 GetAddressFromBlockPtr(const u8 *ptr) const;164165MIPSOpcode GetOriginalFirstOp(int block_num);166167bool RangeMayHaveEmuHacks(u32 start, u32 end) const;168169// DOES NOT WORK CORRECTLY WITH JIT INLINING170void InvalidateICache(u32 address, const u32 length);171void InvalidateChangedBlocks();172void DestroyBlock(int block_num, DestroyType type);173174// No jit operations may be run between these calls.175// Meant to be used to make memory safe for savestates, memcpy, etc.176std::vector<u32> SaveAndClearEmuHackOps();177void RestoreSavedEmuHackOps(const std::vector<u32> &saved);178179int GetNumBlocks() const override { return num_blocks_; }180bool IsValidBlock(int blockNum) const override { return blockNum >= 0 && blockNum < num_blocks_ && !blocks_[blockNum].invalid; }181JitBlockMeta GetBlockMeta(int blockNum) const override {182JitBlockMeta meta{};183if (IsValidBlock(blockNum)) {184meta.valid = true;185meta.addr = blocks_[blockNum].originalAddress;186meta.sizeInBytes = blocks_[blockNum].originalSize;187}188return meta;189}190JitBlockProfileStats GetBlockProfileStats(int blockNum) const override {191return JitBlockProfileStats{};192}193194static int GetBlockExitSize();195196JitBlockDebugInfo GetBlockDebugInfo(int blockNum) const override;197198enum {199MAX_BLOCK_INSTRUCTIONS = 0x4000,200};201202private:203void LinkBlockExits(int i);204void LinkBlock(int i);205void UnlinkBlock(int i);206207void AddBlockMap(int block_num);208void RemoveBlockMap(int block_num);209210MIPSOpcode GetEmuHackOpForBlock(int block_num) const;211212CodeBlockCommon *codeBlock_;213JitBlock *blocks_ = nullptr;214std::unordered_multimap<u32, int> proxyBlockMap_;215216int num_blocks_ = 0;217std::unordered_multimap<u32, int> links_to_;218std::map<std::pair<u32,u32>, u32> block_map_; // (end_addr, start_addr) -> number219220enum {221JITBLOCK_RANGE_SCRATCH = 0,222JITBLOCK_RANGE_RAMBOTTOM = 1,223JITBLOCK_RANGE_RAMTOP = 2,224JITBLOCK_RANGE_COUNT = 3,225};226std::pair<u32, u32> blockMemRanges_[3];227228enum {229MAX_NUM_BLOCKS = 65536*2230};231};232233234235