Files
ppsspp/Core/MIPS/IR/IRJit.h

210 lines
5.9 KiB
C
Raw Permalink Normal View History

2016-05-06 23:45:37 +02:00
// 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
2016-05-08 22:23:51 +02:00
#include <cstring>
#include <unordered_map>
2016-05-08 22:23:51 +02:00
#include "Common/CommonTypes.h"
2016-05-06 23:45:37 +02:00
#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"
2016-05-06 23:45:37 +02:00
#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() {}
IRBlock(u32 emAddr) : origAddr_(emAddr) {}
2016-05-07 17:37:19 +02:00
IRBlock(IRBlock &&b) {
instr_ = b.instr_;
hash_ = b.hash_;
2016-05-07 17:37:19 +02:00
origAddr_ = b.origAddr_;
origSize_ = b.origSize_;
2016-05-07 21:00:30 +02:00
origFirstOpcode_ = b.origFirstOpcode_;
targetOffset_ = b.targetOffset_;
numInstructions_ = b.numInstructions_;
2016-05-07 17:37:19 +02:00
b.instr_ = nullptr;
}
2016-05-06 23:45:37 +02:00
~IRBlock() {
delete[] instr_;
}
void SetInstructions(const std::vector<IRInst> &inst) {
2016-05-06 23:45:37 +02:00
instr_ = new IRInst[inst.size()];
numInstructions_ = (u16)inst.size();
if (!inst.empty()) {
memcpy(instr_, &inst[0], sizeof(IRInst) * inst.size());
}
2016-05-06 23:45:37 +02:00
}
2016-05-07 17:37:19 +02:00
const IRInst *GetInstructions() const { return instr_; }
int GetNumInstructions() const { return numInstructions_; }
MIPSOpcode GetOriginalFirstOp() const { return origFirstOpcode_; }
bool HasOriginalFirstOp() const;
bool RestoreOriginalFirstOp(int number);
bool IsValid() const { return origAddr_ != 0 && origFirstOpcode_.encoding != 0x68FFFFFF; }
2016-07-01 17:27:24 -07:00
void SetOriginalSize(u32 size) {
origSize_ = size;
}
void SetTargetOffset(int offset) {
targetOffset_ = offset;
}
int GetTargetOffset() const {
return targetOffset_;
}
void UpdateHash() {
hash_ = CalculateHash();
}
bool HashMatches() const {
return origAddr_ && hash_ == CalculateHash();
}
bool OverlapsRange(u32 addr, u32 size) const;
2016-05-07 17:37:19 +02:00
void GetRange(u32 &start, u32 &size) const {
start = origAddr_;
size = origSize_;
}
u32 GetOriginalStart() const {
return origAddr_;
}
2016-05-07 17:37:19 +02:00
void Finalize(int number);
void Destroy(int number);
2016-05-07 17:37:19 +02:00
2016-05-06 23:45:37 +02:00
private:
u64 CalculateHash() const;
IRInst *instr_ = nullptr;
u64 hash_ = 0;
u32 origAddr_ = 0;
u32 origSize_ = 0;
MIPSOpcode origFirstOpcode_ = MIPSOpcode(0x68FFFFFF);
int targetOffset_ = -1;
u16 numInstructions_ = 0;
2016-05-06 23:45:37 +02:00
};
class IRBlockCache : public JitBlockCacheDebugInterface {
2016-05-06 23:45:37 +02:00
public:
IRBlockCache() {}
2016-05-06 23:45:37 +02:00
void Clear();
std::vector<int> FindInvalidatedBlockNumbers(u32 address, u32 length);
void FinalizeBlock(int i, bool preload = false);
int GetNumBlocks() const override { return (int)blocks_.size(); }
2016-05-06 23:45:37 +02:00
int AllocateBlock(int emAddr) {
2016-05-08 14:06:42 -07:00
blocks_.push_back(IRBlock(emAddr));
2016-05-06 23:45:37 +02:00
return (int)blocks_.size() - 1;
}
IRBlock *GetBlock(int i) {
if (i >= 0 && i < (int)blocks_.size()) {
2016-05-08 14:06:42 -07:00
return &blocks_[i];
2016-05-07 21:00:30 +02:00
} else {
return nullptr;
}
2016-05-06 23:45:37 +02:00
}
const IRBlock *GetBlock(int i) const {
if (i >= 0 && i < (int)blocks_.size()) {
return &blocks_[i];
} else {
return nullptr;
}
}
int FindPreloadBlock(u32 em_address);
int FindByCookie(int cookie);
std::vector<u32> SaveAndClearEmuHackOps();
void RestoreSavedEmuHackOps(std::vector<u32> saved);
JitBlockDebugInfo GetBlockDebugInfo(int blockNum) const override;
void ComputeStats(BlockCacheStats &bcStats) const override;
int GetBlockNumberFromStartAddress(u32 em_address, bool realBlocksOnly = true) const override;
2016-05-06 23:45:37 +02:00
private:
u32 AddressToPage(u32 addr) const;
2016-05-06 23:45:37 +02:00
std::vector<IRBlock> blocks_;
std::unordered_map<u32, std::vector<int>> byPage_;
2016-05-06 23:45:37 +02:00
};
class IRJit : public JitInterface {
public:
IRJit(MIPSState *mipsState);
~IRJit();
void DoState(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
void CompileFunction(u32 start_address, u32 length) override;
bool DescribeCodePtr(const u8 *ptr, std::string &name) override;
// Not using a regular block cache.
JitBlockCache *GetBlockCache() override { return nullptr; }
JitBlockCacheDebugInterface *GetBlockCacheDebugInterface() override { return &blocks_; }
MIPSOpcode GetOriginalOp(MIPSOpcode op) override;
std::vector<u32> SaveAndClearEmuHackOps() override { return blocks_.SaveAndClearEmuHackOps(); }
void RestoreSavedEmuHackOps(std::vector<u32> saved) override { blocks_.RestoreSavedEmuHackOps(saved); }
2016-04-24 10:57:56 -07:00
void ClearCache() override;
void InvalidateCacheAt(u32 em_address, int length = 4) override;
void UpdateFCR31() override;
bool CodeInRange(const u8 *ptr) const override {
return false;
}
const u8 *GetDispatcher() const override { return nullptr; }
const u8 *GetCrashHandler() const override { return nullptr; }
void LinkBlock(u8 *exitPoint, const u8 *checkedEntry) override;
void UnlinkBlock(u8 *checkedEntry, u32 originalAddress) override;
protected:
bool CompileBlock(u32 em_address, std::vector<IRInst> &instructions, u32 &mipsBytes, bool preload);
virtual bool CompileTargetBlock(IRBlock *block, int block_num, bool preload) { return true; }
virtual void FinalizeTargetBlock(IRBlock *block, int block_num) {}
JitOptions jo;
IRFrontend frontend_;
2016-05-06 23:45:37 +02:00
IRBlockCache blocks_;
MIPSState *mips_;
// where to write branch-likely trampolines. not used atm
// u32 blTrampolines_;
// int blTrampolineCount_;
2016-05-06 23:45:37 +02:00
};
} // namespace MIPSComp