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/ARM/ArmRegCache.cpp
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
#include "ppsspp_config.h"
19
#if PPSSPP_ARCH(ARM)
20
21
#include "Core/MemMap.h"
22
#include "Core/MIPS/ARM/ArmRegCache.h"
23
#include "Core/MIPS/ARM/ArmJit.h"
24
#include "Core/MIPS/MIPSAnalyst.h"
25
#include "Core/Reporting.h"
26
#include "Common/ArmEmitter.h"
27
28
#ifndef offsetof
29
#include "stddef.h"
30
#endif
31
32
using namespace ArmGen;
33
using namespace ArmJitConstants;
34
35
ArmRegCache::ArmRegCache(MIPSState *mipsState, MIPSComp::JitState *js, MIPSComp::JitOptions *jo) : mips_(mipsState), js_(js), jo_(jo) {
36
}
37
38
void ArmRegCache::Init(ARMXEmitter *emitter) {
39
emit_ = emitter;
40
}
41
42
void ArmRegCache::Start(MIPSAnalyst::AnalysisResults &stats) {
43
for (int i = 0; i < NUM_ARMREG; i++) {
44
ar[i].mipsReg = MIPS_REG_INVALID;
45
ar[i].isDirty = false;
46
}
47
for (int i = 0; i < NUM_MIPSREG; i++) {
48
mr[i].loc = ML_MEM;
49
mr[i].reg = INVALID_REG;
50
mr[i].imm = -1;
51
mr[i].spillLock = false;
52
}
53
}
54
55
const ARMReg *ArmRegCache::GetMIPSAllocationOrder(int &count) {
56
// Note that R0 is reserved as scratch for now.
57
// R12 is also potentially usable.
58
// R4-R7 are registers we could use for static allocation or downcount.
59
// R8 is used to preserve flags in nasty branches.
60
// R9 and upwards are reserved for jit basics.
61
// R14 (LR) is used as a scratch reg (overwritten on calls/return.)
62
if (jo_->downcountInRegister) {
63
static const ARMReg allocationOrder[] = {
64
R1, R2, R3, R4, R5, R6, R12,
65
};
66
count = sizeof(allocationOrder) / sizeof(const int);
67
return allocationOrder;
68
} else {
69
static const ARMReg allocationOrder2[] = {
70
R1, R2, R3, R4, R5, R6, R7, R12,
71
};
72
count = sizeof(allocationOrder2) / sizeof(const int);
73
return allocationOrder2;
74
}
75
}
76
77
void ArmRegCache::FlushBeforeCall() {
78
// R4-R11 are preserved. Others need flushing.
79
FlushArmReg(R1);
80
FlushArmReg(R2);
81
FlushArmReg(R3);
82
FlushArmReg(R12);
83
}
84
85
ARMReg ArmRegCache::MapRegAsPointer(MIPSGPReg mipsReg) { // read-only, non-dirty.
86
// If already mapped as a pointer, bail.
87
if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
88
return mr[mipsReg].reg;
89
}
90
// First, make sure the register is already mapped.
91
MapReg(mipsReg, 0);
92
// If it's dirty, flush it.
93
ARMReg armReg = mr[mipsReg].reg;
94
if (ar[armReg].isDirty) {
95
emit_->STR(armReg, CTXREG, GetMipsRegOffset(ar[armReg].mipsReg));
96
}
97
// Convert to a pointer by adding the base and clearing off the top bits.
98
// If SP, we can probably avoid the top bit clear, let's play with that later.
99
emit_->BIC(armReg, armReg, Operand2(0xC0, 4)); // &= 0x3FFFFFFF
100
emit_->ADD(armReg, MEMBASEREG, armReg);
101
ar[armReg].isDirty = false;
102
ar[armReg].mipsReg = mipsReg;
103
mr[mipsReg].loc = ML_ARMREG_AS_PTR;
104
return armReg;
105
}
106
107
bool ArmRegCache::IsMappedAsPointer(MIPSGPReg mipsReg) {
108
return mr[mipsReg].loc == ML_ARMREG_AS_PTR;
109
}
110
111
bool ArmRegCache::IsMapped(MIPSGPReg mipsReg) {
112
return mr[mipsReg].loc == ML_ARMREG;
113
}
114
115
void ArmRegCache::SetRegImm(ARMReg reg, u32 imm) {
116
// If we can do it with a simple Operand2, let's do that.
117
Operand2 op2;
118
bool inverse;
119
if (TryMakeOperand2_AllowInverse(imm, op2, &inverse)) {
120
if (!inverse)
121
emit_->MOV(reg, op2);
122
else
123
emit_->MVN(reg, op2);
124
return;
125
}
126
127
// Okay, so it's a bit more complex. Let's see if we have any useful regs with imm values.
128
for (int i = 0; i < NUM_MIPSREG; i++) {
129
const auto &mreg = mr[i];
130
if (mreg.loc != ML_ARMREG_IMM)
131
continue;
132
133
if (mreg.imm - imm < 256) {
134
emit_->SUB(reg, mreg.reg, mreg.imm - imm);
135
return;
136
}
137
if (imm - mreg.imm < 256) {
138
emit_->ADD(reg, mreg.reg, imm - mreg.imm);
139
return;
140
}
141
// This could be common when using an address.
142
if ((mreg.imm & 0x3FFFFFFF) == imm) {
143
emit_->BIC(reg, mreg.reg, Operand2(0xC0, 4)); // &= 0x3FFFFFFF
144
return;
145
}
146
// TODO: All sorts of things are possible here, shifted adds, ands/ors, etc.
147
}
148
149
// No luck. Let's go with a regular load.
150
emit_->MOVI2R(reg, imm);
151
}
152
153
void ArmRegCache::MapRegTo(ARMReg reg, MIPSGPReg mipsReg, int mapFlags) {
154
ar[reg].isDirty = (mapFlags & MAP_DIRTY) ? true : false;
155
if ((mapFlags & MAP_NOINIT) != MAP_NOINIT) {
156
if (mipsReg == MIPS_REG_ZERO) {
157
// If we get a request to load the zero register, at least we won't spend
158
// time on a memory access...
159
// TODO: EOR?
160
emit_->MOV(reg, 0);
161
162
// This way, if we SetImm() it, we'll keep it.
163
mr[mipsReg].loc = ML_ARMREG_IMM;
164
mr[mipsReg].imm = 0;
165
} else {
166
switch (mr[mipsReg].loc) {
167
case ML_MEM:
168
emit_->LDR(reg, CTXREG, GetMipsRegOffset(mipsReg));
169
mr[mipsReg].loc = ML_ARMREG;
170
break;
171
case ML_IMM:
172
SetRegImm(reg, mr[mipsReg].imm);
173
ar[reg].isDirty = true; // IMM is always dirty.
174
175
// If we are mapping dirty, it means we're gonna overwrite.
176
// So the imm value is no longer valid.
177
if (mapFlags & MAP_DIRTY)
178
mr[mipsReg].loc = ML_ARMREG;
179
else
180
mr[mipsReg].loc = ML_ARMREG_IMM;
181
break;
182
default:
183
mr[mipsReg].loc = ML_ARMREG;
184
break;
185
}
186
}
187
} else {
188
mr[mipsReg].loc = ML_ARMREG;
189
}
190
ar[reg].mipsReg = mipsReg;
191
mr[mipsReg].reg = reg;
192
}
193
194
ARMReg ArmRegCache::FindBestToSpill(bool unusedOnly, bool *clobbered) {
195
int allocCount;
196
const ARMReg *allocOrder = GetMIPSAllocationOrder(allocCount);
197
198
static const int UNUSED_LOOKAHEAD_OPS = 30;
199
200
*clobbered = false;
201
for (int i = 0; i < allocCount; i++) {
202
ARMReg reg = allocOrder[i];
203
if (ar[reg].mipsReg != MIPS_REG_INVALID && mr[ar[reg].mipsReg].spillLock)
204
continue;
205
206
// Awesome, a clobbered reg. Let's use it.
207
if (MIPSAnalyst::IsRegisterClobbered(ar[reg].mipsReg, compilerPC_, UNUSED_LOOKAHEAD_OPS)) {
208
*clobbered = true;
209
return reg;
210
}
211
212
// Not awesome. A used reg. Let's try to avoid spilling.
213
if (unusedOnly && MIPSAnalyst::IsRegisterUsed(ar[reg].mipsReg, compilerPC_, UNUSED_LOOKAHEAD_OPS)) {
214
continue;
215
}
216
217
return reg;
218
}
219
220
return INVALID_REG;
221
}
222
223
// TODO: Somewhat smarter spilling - currently simply spills the first available, should do
224
// round robin or FIFO or something.
225
ARMReg ArmRegCache::MapReg(MIPSGPReg mipsReg, int mapFlags) {
226
// Let's see if it's already mapped. If so we just need to update the dirty flag.
227
// We don't need to check for ML_NOINIT because we assume that anyone who maps
228
// with that flag immediately writes a "known" value to the register.
229
if (mr[mipsReg].loc == ML_ARMREG || mr[mipsReg].loc == ML_ARMREG_IMM) {
230
ARMReg armReg = mr[mipsReg].reg;
231
if (ar[armReg].mipsReg != mipsReg) {
232
ERROR_LOG_REPORT(Log::JIT, "Register mapping out of sync! %i", mipsReg);
233
}
234
if (mapFlags & MAP_DIRTY) {
235
// Mapping dirty means the old imm value is invalid.
236
mr[mipsReg].loc = ML_ARMREG;
237
ar[armReg].isDirty = true;
238
}
239
return (ARMReg)mr[mipsReg].reg;
240
} else if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
241
// Was mapped as pointer, now we want it mapped as a value, presumably to
242
// add or subtract stuff to it. Later we could allow such things but for now
243
// let's just convert back to a register value by reloading from the backing storage.
244
ARMReg armReg = mr[mipsReg].reg;
245
if ((mapFlags & MAP_NOINIT) != MAP_NOINIT) {
246
emit_->LDR(armReg, CTXREG, GetMipsRegOffset(mipsReg));
247
}
248
mr[mipsReg].loc = ML_ARMREG;
249
if (mapFlags & MAP_DIRTY) {
250
ar[armReg].isDirty = true;
251
}
252
return (ARMReg)mr[mipsReg].reg;
253
}
254
255
// Okay, not mapped, so we need to allocate an ARM register.
256
257
int allocCount;
258
const ARMReg *allocOrder = GetMIPSAllocationOrder(allocCount);
259
260
ARMReg desiredReg = INVALID_REG;
261
// Try to "statically" allocate the first 6 regs after v0.
262
int desiredOrder = allocCount - (6 - (mipsReg - (int)MIPS_REG_V0));
263
if (desiredOrder >= 0 && desiredOrder < allocCount)
264
desiredReg = allocOrder[desiredOrder];
265
266
if (desiredReg != INVALID_REG) {
267
if (ar[desiredReg].mipsReg == MIPS_REG_INVALID) {
268
// With this placement, we may be able to optimize flush.
269
MapRegTo(desiredReg, mipsReg, mapFlags);
270
return desiredReg;
271
}
272
}
273
274
allocate:
275
for (int i = 0; i < allocCount; i++) {
276
ARMReg reg = allocOrder[i];
277
278
if (ar[reg].mipsReg == MIPS_REG_INVALID) {
279
// That means it's free. Grab it, and load the value into it (if requested).
280
MapRegTo(reg, mipsReg, mapFlags);
281
return reg;
282
}
283
}
284
285
// Still nothing. Let's spill a reg and goto 10.
286
// TODO: Use age or something to choose which register to spill?
287
// TODO: Spill dirty regs first? or opposite?
288
bool clobbered;
289
ARMReg bestToSpill = FindBestToSpill(true, &clobbered);
290
if (bestToSpill == INVALID_REG) {
291
bestToSpill = FindBestToSpill(false, &clobbered);
292
}
293
294
if (bestToSpill != INVALID_REG) {
295
// ERROR_LOG(Log::JIT, "Out of registers at PC %08x - spills register %i.", mips_->pc, bestToSpill);
296
// TODO: Broken somehow in Dante's Inferno, but most games work. Bad flags in MIPSTables somewhere?
297
if (clobbered) {
298
DiscardR(ar[bestToSpill].mipsReg);
299
} else {
300
FlushArmReg(bestToSpill);
301
}
302
goto allocate;
303
}
304
305
// Uh oh, we have all of them spilllocked....
306
ERROR_LOG_REPORT(Log::JIT, "Out of spillable registers at PC %08x!!!", mips_->pc);
307
return INVALID_REG;
308
}
309
310
void ArmRegCache::MapInIn(MIPSGPReg rd, MIPSGPReg rs) {
311
SpillLock(rd, rs);
312
MapReg(rd);
313
MapReg(rs);
314
ReleaseSpillLocks();
315
}
316
317
void ArmRegCache::MapDirtyIn(MIPSGPReg rd, MIPSGPReg rs, bool avoidLoad) {
318
SpillLock(rd, rs);
319
bool load = !avoidLoad || rd == rs;
320
MapReg(rd, load ? MAP_DIRTY : MAP_NOINIT);
321
MapReg(rs);
322
ReleaseSpillLocks();
323
}
324
325
void ArmRegCache::MapDirtyInIn(MIPSGPReg rd, MIPSGPReg rs, MIPSGPReg rt, bool avoidLoad) {
326
SpillLock(rd, rs, rt);
327
bool load = !avoidLoad || (rd == rs || rd == rt);
328
MapReg(rd, load ? MAP_DIRTY : MAP_NOINIT);
329
MapReg(rt);
330
MapReg(rs);
331
ReleaseSpillLocks();
332
}
333
334
void ArmRegCache::MapDirtyDirtyIn(MIPSGPReg rd1, MIPSGPReg rd2, MIPSGPReg rs, bool avoidLoad) {
335
SpillLock(rd1, rd2, rs);
336
bool load1 = !avoidLoad || rd1 == rs;
337
bool load2 = !avoidLoad || rd2 == rs;
338
MapReg(rd1, load1 ? MAP_DIRTY : MAP_NOINIT);
339
MapReg(rd2, load2 ? MAP_DIRTY : MAP_NOINIT);
340
MapReg(rs);
341
ReleaseSpillLocks();
342
}
343
344
void ArmRegCache::MapDirtyDirtyInIn(MIPSGPReg rd1, MIPSGPReg rd2, MIPSGPReg rs, MIPSGPReg rt, bool avoidLoad) {
345
SpillLock(rd1, rd2, rs, rt);
346
bool load1 = !avoidLoad || (rd1 == rs || rd1 == rt);
347
bool load2 = !avoidLoad || (rd2 == rs || rd2 == rt);
348
MapReg(rd1, load1 ? MAP_DIRTY : MAP_NOINIT);
349
MapReg(rd2, load2 ? MAP_DIRTY : MAP_NOINIT);
350
MapReg(rt);
351
MapReg(rs);
352
ReleaseSpillLocks();
353
}
354
355
void ArmRegCache::FlushArmReg(ARMReg r) {
356
if (ar[r].mipsReg == MIPS_REG_INVALID) {
357
// Nothing to do, reg not mapped.
358
if (ar[r].isDirty) {
359
ERROR_LOG_REPORT(Log::JIT, "Dirty but no mipsreg?");
360
}
361
return;
362
}
363
if (ar[r].mipsReg != MIPS_REG_INVALID) {
364
auto &mreg = mr[ar[r].mipsReg];
365
if (mreg.loc == ML_ARMREG_IMM || ar[r].mipsReg == MIPS_REG_ZERO) {
366
// We know its immedate value, no need to STR now.
367
mreg.loc = ML_IMM;
368
mreg.reg = INVALID_REG;
369
} else {
370
if (ar[r].isDirty && mreg.loc == ML_ARMREG)
371
emit_->STR(r, CTXREG, GetMipsRegOffset(ar[r].mipsReg));
372
mreg.loc = ML_MEM;
373
mreg.reg = INVALID_REG;
374
mreg.imm = 0;
375
}
376
}
377
ar[r].isDirty = false;
378
ar[r].mipsReg = MIPS_REG_INVALID;
379
}
380
381
void ArmRegCache::DiscardR(MIPSGPReg mipsReg) {
382
const RegMIPSLoc prevLoc = mr[mipsReg].loc;
383
if (prevLoc == ML_ARMREG || prevLoc == ML_ARMREG_AS_PTR || prevLoc == ML_ARMREG_IMM) {
384
ARMReg armReg = mr[mipsReg].reg;
385
ar[armReg].isDirty = false;
386
ar[armReg].mipsReg = MIPS_REG_INVALID;
387
mr[mipsReg].reg = INVALID_REG;
388
if (mipsReg == MIPS_REG_ZERO) {
389
mr[mipsReg].loc = ML_IMM;
390
} else {
391
mr[mipsReg].loc = ML_MEM;
392
}
393
mr[mipsReg].imm = 0;
394
}
395
if (prevLoc == ML_IMM && mipsReg != MIPS_REG_ZERO) {
396
mr[mipsReg].loc = ML_MEM;
397
mr[mipsReg].imm = 0;
398
}
399
}
400
401
void ArmRegCache::FlushR(MIPSGPReg r) {
402
switch (mr[r].loc) {
403
case ML_IMM:
404
// IMM is always "dirty".
405
if (r != MIPS_REG_ZERO) {
406
SetRegImm(SCRATCHREG1, mr[r].imm);
407
emit_->STR(SCRATCHREG1, CTXREG, GetMipsRegOffset(r));
408
}
409
break;
410
411
case ML_ARMREG:
412
case ML_ARMREG_IMM:
413
if (mr[r].reg == INVALID_REG) {
414
ERROR_LOG_REPORT(Log::JIT, "FlushR: MipsReg %d had bad ArmReg", r);
415
}
416
if (ar[mr[r].reg].isDirty) {
417
if (r != MIPS_REG_ZERO) {
418
emit_->STR((ARMReg)mr[r].reg, CTXREG, GetMipsRegOffset(r));
419
}
420
ar[mr[r].reg].isDirty = false;
421
}
422
ar[mr[r].reg].mipsReg = MIPS_REG_INVALID;
423
break;
424
425
case ML_ARMREG_AS_PTR:
426
// Never dirty.
427
if (ar[mr[r].reg].isDirty) {
428
ERROR_LOG_REPORT(Log::JIT, "ARMREG_AS_PTR cannot be dirty (yet)");
429
}
430
ar[mr[r].reg].mipsReg = MIPS_REG_INVALID;
431
break;
432
433
case ML_MEM:
434
// Already there, nothing to do.
435
break;
436
437
default:
438
ERROR_LOG_REPORT(Log::JIT, "FlushR: MipsReg %d with invalid location %d", r, mr[r].loc);
439
break;
440
}
441
if (r == MIPS_REG_ZERO) {
442
mr[r].loc = ML_IMM;
443
} else {
444
mr[r].loc = ML_MEM;
445
}
446
mr[r].reg = INVALID_REG;
447
mr[r].imm = 0;
448
}
449
450
// Note: if allowFlushImm is set, this also flushes imms while checking the sequence.
451
int ArmRegCache::FlushGetSequential(MIPSGPReg startMipsReg, bool allowFlushImm) {
452
// Only start a sequence on a dirty armreg.
453
// TODO: Could also start with an imm?
454
const auto &startMipsInfo = mr[startMipsReg];
455
if ((startMipsInfo.loc != ML_ARMREG && startMipsInfo.loc != ML_ARMREG_IMM) || startMipsInfo.reg == INVALID_REG || !ar[startMipsInfo.reg].isDirty) {
456
return 0;
457
}
458
459
int allocCount;
460
const ARMReg *allocOrder = GetMIPSAllocationOrder(allocCount);
461
462
int c = 1;
463
// The sequence needs to have ascending arm regs for STMIA.
464
int lastArmReg = startMipsInfo.reg;
465
// Can't use HI/LO, only regs in the main r[] array.
466
for (int r = (int)startMipsReg + 1; r < 32; ++r) {
467
if ((mr[r].loc == ML_ARMREG || mr[r].loc == ML_ARMREG_IMM) && mr[r].reg != INVALID_REG) {
468
if ((int)mr[r].reg > lastArmReg && ar[mr[r].reg].isDirty) {
469
++c;
470
lastArmReg = mr[r].reg;
471
continue;
472
}
473
// If we're not allowed to flush imms, don't even consider them.
474
} else if (allowFlushImm && mr[r].loc == ML_IMM && MIPSGPReg(r) != MIPS_REG_ZERO) {
475
// Okay, let's search for a free (and later) reg to put this imm into.
476
bool found = false;
477
for (int j = 0; j < allocCount; ++j) {
478
ARMReg immReg = allocOrder[j];
479
if ((int)immReg > lastArmReg && ar[immReg].mipsReg == MIPS_REG_INVALID) {
480
++c;
481
lastArmReg = immReg;
482
483
// Even if the sequence fails, we'll need it in a reg anyway, might as well be this one.
484
MapRegTo(immReg, MIPSGPReg(r), 0);
485
found = true;
486
break;
487
}
488
}
489
if (found) {
490
continue;
491
}
492
}
493
494
// If it didn't hit a continue above, the chain is over.
495
// There's no way to skip a slot with STMIA.
496
break;
497
}
498
499
return c;
500
}
501
502
void ArmRegCache::FlushAll() {
503
// ADD + STMIA is probably better than STR + STR, so let's merge 2 into a STMIA.
504
const int minSequential = 2;
505
506
// Let's try to put things in order and use STMIA.
507
// First we have to save imms. We have to use a separate loop because otherwise
508
// we would overwrite existing regs, and other code assumes FlushAll() won't do that.
509
for (int i = 0; i < NUM_MIPSREG; i++) {
510
MIPSGPReg mipsReg = MIPSGPReg(i);
511
512
// This happens to also flush imms to regs as much as possible.
513
int c = FlushGetSequential(mipsReg, true);
514
if (c >= minSequential) {
515
// Skip the next c (adjust down 1 because the loop increments.)
516
i += c - 1;
517
}
518
}
519
520
// Okay, now the real deal: this time NOT flushing imms.
521
for (int i = 0; i < NUM_MIPSREG; i++) {
522
MIPSGPReg mipsReg = MIPSGPReg(i);
523
524
int c = FlushGetSequential(mipsReg, false);
525
if (c >= minSequential) {
526
u16 regs = 0;
527
for (int j = 0; j < c; ++j) {
528
regs |= 1 << mr[i + j].reg;
529
}
530
531
emit_->ADD(SCRATCHREG1, CTXREG, GetMipsRegOffset(mipsReg));
532
emit_->STMBitmask(SCRATCHREG1, true, false, false, regs);
533
534
// Okay, those are all done now, discard them.
535
for (int j = 0; j < c; ++j) {
536
DiscardR(MIPSGPReg(i + j));
537
}
538
// Skip the next c (adjust down 1 because the loop increments.)
539
i += c - 1;
540
} else {
541
FlushR(mipsReg);
542
}
543
}
544
// Sanity check
545
for (int i = 0; i < NUM_ARMREG; i++) {
546
if (ar[i].mipsReg != MIPS_REG_INVALID) {
547
ERROR_LOG_REPORT(Log::JIT, "Flush fail: ar[%i].mipsReg=%i", i, ar[i].mipsReg);
548
}
549
}
550
}
551
552
void ArmRegCache::SetImm(MIPSGPReg r, u32 immVal) {
553
if (r == MIPS_REG_ZERO && immVal != 0) {
554
ERROR_LOG_REPORT(Log::JIT, "Trying to set immediate %08x to r0 at %08x", immVal, compilerPC_);
555
return;
556
}
557
558
if (mr[r].loc == ML_ARMREG_IMM && mr[r].imm == immVal) {
559
// Already have that value, let's keep it in the reg.
560
return;
561
}
562
// Zap existing value if cached in a reg
563
if (mr[r].reg != INVALID_REG) {
564
ar[mr[r].reg].mipsReg = MIPS_REG_INVALID;
565
ar[mr[r].reg].isDirty = false;
566
}
567
mr[r].loc = ML_IMM;
568
mr[r].imm = immVal;
569
mr[r].reg = INVALID_REG;
570
}
571
572
bool ArmRegCache::IsImm(MIPSGPReg r) const {
573
if (r == MIPS_REG_ZERO) return true;
574
return mr[r].loc == ML_IMM || mr[r].loc == ML_ARMREG_IMM;
575
}
576
577
u32 ArmRegCache::GetImm(MIPSGPReg r) const {
578
if (r == MIPS_REG_ZERO) return 0;
579
if (mr[r].loc != ML_IMM && mr[r].loc != ML_ARMREG_IMM) {
580
ERROR_LOG_REPORT(Log::JIT, "Trying to get imm from non-imm register %i", r);
581
}
582
return mr[r].imm;
583
}
584
585
int ArmRegCache::GetMipsRegOffset(MIPSGPReg r) {
586
if (r < 32)
587
return r * 4;
588
switch (r) {
589
case MIPS_REG_HI:
590
return offsetof(MIPSState, hi);
591
case MIPS_REG_LO:
592
return offsetof(MIPSState, lo);
593
case MIPS_REG_FPCOND:
594
return offsetof(MIPSState, fpcond);
595
case MIPS_REG_VFPUCC:
596
return offsetof(MIPSState, vfpuCtrl[VFPU_CTRL_CC]);
597
default:
598
ERROR_LOG_REPORT(Log::JIT, "bad mips register %i", r);
599
return 0; // or what?
600
}
601
}
602
603
void ArmRegCache::SpillLock(MIPSGPReg r1, MIPSGPReg r2, MIPSGPReg r3, MIPSGPReg r4) {
604
mr[r1].spillLock = true;
605
if (r2 != MIPS_REG_INVALID) mr[r2].spillLock = true;
606
if (r3 != MIPS_REG_INVALID) mr[r3].spillLock = true;
607
if (r4 != MIPS_REG_INVALID) mr[r4].spillLock = true;
608
}
609
610
void ArmRegCache::ReleaseSpillLocks() {
611
for (int i = 0; i < NUM_MIPSREG; i++) {
612
mr[i].spillLock = false;
613
}
614
}
615
616
void ArmRegCache::ReleaseSpillLock(MIPSGPReg reg) {
617
mr[reg].spillLock = false;
618
}
619
620
ARMReg ArmRegCache::R(MIPSGPReg mipsReg) {
621
if (mr[mipsReg].loc == ML_ARMREG || mr[mipsReg].loc == ML_ARMREG_IMM) {
622
return (ARMReg)mr[mipsReg].reg;
623
} else {
624
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in arm reg. compilerPC = %08x", mipsReg, compilerPC_);
625
return INVALID_REG; // BAAAD
626
}
627
}
628
629
ARMReg ArmRegCache::RPtr(MIPSGPReg mipsReg) {
630
if (mr[mipsReg].loc == ML_ARMREG_AS_PTR) {
631
return (ARMReg)mr[mipsReg].reg;
632
} else {
633
ERROR_LOG_REPORT(Log::JIT, "Reg %i not in arm reg as pointer. compilerPC = %08x", mipsReg, compilerPC_);
634
return INVALID_REG; // BAAAD
635
}
636
}
637
638
#endif // PPSSPP_ARCH(ARM)
639
640