CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/ARM64/Arm64RegCache.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include "Core/MIPS/MIPS.h"
21
#include "Common/Arm64Emitter.h"
22
23
namespace Arm64JitConstants {
24
25
const Arm64Gen::ARM64Reg DOWNCOUNTREG = Arm64Gen::W24;
26
const Arm64Gen::ARM64Reg FLAGTEMPREG = Arm64Gen::X25;
27
const Arm64Gen::ARM64Reg JITBASEREG = Arm64Gen::X26;
28
const Arm64Gen::ARM64Reg CTXREG = Arm64Gen::X27;
29
const Arm64Gen::ARM64Reg MEMBASEREG = Arm64Gen::X28;
30
const Arm64Gen::ARM64Reg SCRATCH1_64 = Arm64Gen::X16;
31
const Arm64Gen::ARM64Reg SCRATCH2_64 = Arm64Gen::X17;
32
const Arm64Gen::ARM64Reg SCRATCH1 = Arm64Gen::W16;
33
const Arm64Gen::ARM64Reg SCRATCH2 = Arm64Gen::W17;
34
35
enum {
36
TOTAL_MAPPABLE_MIPSREGS = 36,
37
};
38
39
enum RegMIPSLoc {
40
ML_IMM,
41
ML_ARMREG,
42
// In an arm reg, but an adjusted pointer (not pointerified - unaligned.)
43
ML_ARMREG_AS_PTR,
44
// In an arm reg, but also has a known immediate value.
45
ML_ARMREG_IMM,
46
ML_MEM,
47
};
48
49
// Initing is the default so the flag is reversed.
50
enum {
51
MAP_DIRTY = 1,
52
MAP_NOINIT = 2 | MAP_DIRTY,
53
};
54
55
} // namespace
56
57
namespace MIPSAnalyst {
58
struct AnalysisResults;
59
};
60
61
typedef int MIPSReg;
62
63
struct RegARM64 {
64
MIPSGPReg mipsReg; // if -1, no mipsreg attached.
65
bool isDirty; // Should the register be written back?
66
bool pointerified; // Has used movk to move the memory base into the top part of the reg. Note - still usable as 32-bit reg!
67
bool tempLocked; // Reserved for a temp register.
68
};
69
70
struct RegMIPS {
71
// Where is this MIPS register?
72
Arm64JitConstants::RegMIPSLoc loc;
73
// Data (both or only one may be used, depending on loc.)
74
u64 imm;
75
Arm64Gen::ARM64Reg reg; // reg index
76
bool spillLock; // if true, this register cannot be spilled.
77
bool isStatic; // if true, this register will not be written back to ram by the regcache
78
// If loc == ML_MEM, it's back in its location in the CPU context struct.
79
};
80
81
namespace MIPSComp {
82
struct JitOptions;
83
struct JitState;
84
}
85
86
class Arm64RegCache {
87
public:
88
Arm64RegCache(MIPSState *mipsState, MIPSComp::JitState *js, MIPSComp::JitOptions *jo);
89
~Arm64RegCache() {}
90
91
void Init(Arm64Gen::ARM64XEmitter *emitter);
92
void Start(MIPSAnalyst::AnalysisResults &stats);
93
94
// Protect the arm register containing a MIPS register from spilling, to ensure that
95
// it's being kept allocated.
96
void SpillLock(MIPSGPReg reg, MIPSGPReg reg2 = MIPS_REG_INVALID, MIPSGPReg reg3 = MIPS_REG_INVALID, MIPSGPReg reg4 = MIPS_REG_INVALID);
97
void ReleaseSpillLock(MIPSGPReg reg, MIPSGPReg reg2 = MIPS_REG_INVALID, MIPSGPReg reg3 = MIPS_REG_INVALID, MIPSGPReg reg4 = MIPS_REG_INVALID);
98
void ReleaseSpillLocksAndDiscardTemps();
99
100
void SetImm(MIPSGPReg reg, u64 immVal);
101
bool IsImm(MIPSGPReg reg) const;
102
bool IsPureImm(MIPSGPReg reg) const;
103
u64 GetImm(MIPSGPReg reg) const;
104
// Optimally set a register to an imm value (possibly using another register.)
105
void SetRegImm(Arm64Gen::ARM64Reg reg, u64 imm);
106
107
// May fail and return INVALID_REG if it needs flushing.
108
Arm64Gen::ARM64Reg TryMapTempImm(MIPSGPReg);
109
110
// Returns an ARM register containing the requested MIPS register.
111
Arm64Gen::ARM64Reg MapReg(MIPSGPReg reg, int mapFlags = 0);
112
Arm64Gen::ARM64Reg MapRegAsPointer(MIPSGPReg reg);
113
114
bool IsMapped(MIPSGPReg reg);
115
bool IsMappedAsPointer(MIPSGPReg reg);
116
bool IsInRAM(MIPSGPReg reg);
117
118
void MarkDirty(Arm64Gen::ARM64Reg reg);
119
void MapIn(MIPSGPReg rs);
120
void MapInIn(MIPSGPReg rd, MIPSGPReg rs);
121
void MapDirtyIn(MIPSGPReg rd, MIPSGPReg rs, bool avoidLoad = true);
122
void MapDirtyInIn(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt, bool avoidLoad = true);
123
void MapDirtyDirtyIn(MIPSGPReg rd1, MIPSGPReg rd2, MIPSGPReg rs, bool avoidLoad = true);
124
void MapDirtyDirtyInIn(MIPSGPReg rd1, MIPSGPReg rd2, MIPSGPReg rs, MIPSGPReg rt, bool avoidLoad = true);
125
void FlushArmReg(Arm64Gen::ARM64Reg r);
126
void FlushBeforeCall();
127
void FlushAll();
128
void FlushR(MIPSGPReg r);
129
void DiscardR(MIPSGPReg r);
130
131
Arm64Gen::ARM64Reg GetAndLockTempR();
132
133
Arm64Gen::ARM64Reg R(MIPSGPReg preg); // Returns a cached register, while checking that it's NOT mapped as a pointer
134
Arm64Gen::ARM64Reg RPtr(MIPSGPReg preg); // Returns a cached register, if it has been mapped as a pointer
135
136
void SetEmitter(Arm64Gen::ARM64XEmitter *emitter) { emit_ = emitter; }
137
138
// For better log output only.
139
void SetCompilerPC(u32 compilerPC) { compilerPC_ = compilerPC; }
140
141
int GetMipsRegOffset(MIPSGPReg r);
142
143
// These are called once on startup to generate functions, that you should then call.
144
void EmitLoadStaticRegisters();
145
void EmitSaveStaticRegisters();
146
147
private:
148
struct StaticAllocation {
149
MIPSGPReg mr;
150
Arm64Gen::ARM64Reg ar;
151
bool pointerified;
152
};
153
const StaticAllocation *GetStaticAllocations(int &count);
154
const Arm64Gen::ARM64Reg *GetMIPSAllocationOrder(int &count);
155
void MapRegTo(Arm64Gen::ARM64Reg reg, MIPSGPReg mipsReg, int mapFlags);
156
Arm64Gen::ARM64Reg AllocateReg();
157
Arm64Gen::ARM64Reg FindBestToSpill(bool unusedOnly, bool *clobbered);
158
Arm64Gen::ARM64Reg ARM64RegForFlush(MIPSGPReg r);
159
160
MIPSState *mips_;
161
Arm64Gen::ARM64XEmitter *emit_;
162
MIPSComp::JitState *js_;
163
MIPSComp::JitOptions *jo_;
164
u32 compilerPC_;
165
166
enum {
167
NUM_ARMREG = 32, // 31 actual registers, plus the zero/sp register which is not mappable.
168
NUM_MIPSREG = Arm64JitConstants::TOTAL_MAPPABLE_MIPSREGS,
169
};
170
171
RegARM64 ar[NUM_ARMREG];
172
RegMIPS mr[NUM_MIPSREG];
173
};
174
175