lightrec: Increase size of hw registers area
[pcsx_rearmed.git] / deps / lightrec / regcache.c
CommitLineData
98fa08a5 1// SPDX-License-Identifier: LGPL-2.1-or-later
d16005f8 2/*
98fa08a5 3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
d16005f8
PC
4 */
5
6#include "debug.h"
7#include "memmanager.h"
98fa08a5 8#include "lightning-wrapper.h"
d16005f8
PC
9#include "regcache.h"
10
d16005f8
PC
11#include <stdbool.h>
12#include <stddef.h>
13
0e720fb1 14#define REG_PC (offsetof(struct lightrec_state, curr_pc) / sizeof(u32))
9259d748 15
ba3814c1
PC
16enum reg_priority {
17 REG_IS_TEMP,
18 REG_IS_TEMP_VALUE,
19 REG_IS_ZERO,
20 REG_IS_LOADED,
21 REG_IS_DIRTY,
22
23 REG_NB_PRIORITIES,
24};
25
d16005f8 26struct native_register {
ba3814c1 27 bool used, output, extend, extended,
98fa08a5 28 zero_extend, zero_extended, locked;
9259d748 29 s16 emulated_register;
ba3814c1
PC
30 intptr_t value;
31 enum reg_priority prio;
d16005f8
PC
32};
33
34struct regcache {
35 struct lightrec_state *state;
36 struct native_register lightrec_regs[NUM_REGS + NUM_TEMPS];
37};
38
39static const char * mips_regs[] = {
40 "zero",
41 "at",
42 "v0", "v1",
43 "a0", "a1", "a2", "a3",
44 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
45 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
46 "t8", "t9",
47 "k0", "k1",
48 "gp", "sp", "fp", "ra",
49 "lo", "hi",
50};
51
cb72ea13
PC
52/* Forward declaration(s) */
53static void clean_reg(jit_state_t *_jit,
54 struct native_register *nreg, u8 jit_reg, bool clean);
55
d16005f8
PC
56const char * lightrec_reg_name(u8 reg)
57{
58 return mips_regs[reg];
59}
60
98fa08a5
PC
61static inline bool lightrec_reg_is_zero(u8 jit_reg)
62{
63#if defined(__mips__) || defined(__alpha__) || defined(__riscv)
64 if (jit_reg == _ZERO)
65 return true;
66#endif
67 return false;
68}
69
9259d748 70static inline s8 lightrec_get_hardwired_reg(u16 reg)
98fa08a5
PC
71{
72#if defined(__mips__) || defined(__alpha__) || defined(__riscv)
73 if (reg == 0)
74 return _ZERO;
75#endif
76 return -1;
77}
78
d16005f8
PC
79static inline u8 lightrec_reg_number(const struct regcache *cache,
80 const struct native_register *nreg)
81{
82 return (u8) (((uintptr_t) nreg - (uintptr_t) cache->lightrec_regs)
83 / sizeof(*nreg));
84}
85
86static inline u8 lightrec_reg_to_lightning(const struct regcache *cache,
87 const struct native_register *nreg)
88{
89 u8 offset = lightrec_reg_number(cache, nreg);
ba3814c1
PC
90
91 if (offset < NUM_REGS)
92 return JIT_V(FIRST_REG + offset);
93 else
94 return JIT_R(FIRST_TEMP + offset - NUM_REGS);
d16005f8
PC
95}
96
97static inline struct native_register * lightning_reg_to_lightrec(
98 struct regcache *cache, u8 reg)
99{
100 if ((JIT_V0 > JIT_R0 && reg >= JIT_V0) ||
101 (JIT_V0 < JIT_R0 && reg < JIT_R0)) {
102 if (JIT_V1 > JIT_V0)
ba3814c1 103 return &cache->lightrec_regs[reg - JIT_V(FIRST_REG)];
d16005f8 104 else
ba3814c1 105 return &cache->lightrec_regs[JIT_V(FIRST_REG) - reg];
d16005f8
PC
106 } else {
107 if (JIT_R1 > JIT_R0)
ba3814c1 108 return &cache->lightrec_regs[NUM_REGS + reg - JIT_R(FIRST_TEMP)];
d16005f8 109 else
ba3814c1 110 return &cache->lightrec_regs[NUM_REGS + JIT_R(FIRST_TEMP) - reg];
d16005f8
PC
111 }
112}
113
98fa08a5
PC
114u8 lightrec_get_reg_in_flags(struct regcache *cache, u8 jit_reg)
115{
116 struct native_register *reg;
117 u8 flags = 0;
118
119 if (lightrec_reg_is_zero(jit_reg))
120 return REG_EXT | REG_ZEXT;
121
122 reg = lightning_reg_to_lightrec(cache, jit_reg);
123 if (reg->extended)
124 flags |= REG_EXT;
125 if (reg->zero_extended)
126 flags |= REG_ZEXT;
127
128 return flags;
129}
130
131void lightrec_set_reg_out_flags(struct regcache *cache, u8 jit_reg, u8 flags)
132{
133 struct native_register *reg;
134
135 if (!lightrec_reg_is_zero(jit_reg)) {
136 reg = lightning_reg_to_lightrec(cache, jit_reg);
137 reg->extend = flags & REG_EXT;
138 reg->zero_extend = flags & REG_ZEXT;
139 }
140}
141
d16005f8
PC
142static struct native_register * alloc_temp(struct regcache *cache)
143{
ba3814c1
PC
144 struct native_register *elm, *nreg = NULL;
145 enum reg_priority best = REG_NB_PRIORITIES;
d16005f8
PC
146 unsigned int i;
147
148 /* We search the register list in reverse order. As temporaries are
149 * meant to be used only in the emitter functions, they can be mapped to
150 * caller-saved registers, as they won't have to be saved back to
151 * memory. */
152 for (i = ARRAY_SIZE(cache->lightrec_regs); i; i--) {
ba3814c1 153 elm = &cache->lightrec_regs[i - 1];
d16005f8 154
9259d748 155 if (!elm->used && !elm->locked && elm->prio < best) {
ba3814c1
PC
156 nreg = elm;
157 best = elm->prio;
158
159 if (best == REG_IS_TEMP)
160 break;
161 }
d16005f8
PC
162 }
163
ba3814c1 164 return nreg;
d16005f8
PC
165}
166
167static struct native_register * find_mapped_reg(struct regcache *cache,
9259d748 168 u16 reg, bool out)
d16005f8
PC
169{
170 unsigned int i;
171
172 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++) {
173 struct native_register *nreg = &cache->lightrec_regs[i];
ba3814c1
PC
174 if ((nreg->prio >= REG_IS_ZERO) &&
175 nreg->emulated_register == reg &&
176 (!out || !nreg->locked))
d16005f8
PC
177 return nreg;
178 }
179
180 return NULL;
181}
182
183static struct native_register * alloc_in_out(struct regcache *cache,
9259d748 184 u16 reg, bool out)
d16005f8 185{
ba3814c1
PC
186 struct native_register *elm, *nreg = NULL;
187 enum reg_priority best = REG_NB_PRIORITIES;
d16005f8
PC
188 unsigned int i;
189
190 /* Try to find if the register is already mapped somewhere */
191 nreg = find_mapped_reg(cache, reg, out);
192 if (nreg)
193 return nreg;
194
ba3814c1 195 nreg = NULL;
d16005f8 196
d16005f8 197 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++) {
ba3814c1 198 elm = &cache->lightrec_regs[i];
d16005f8 199
9259d748 200 if (!elm->used && !elm->locked && elm->prio < best) {
ba3814c1
PC
201 nreg = elm;
202 best = elm->prio;
203
204 if (best == REG_IS_TEMP)
205 break;
206 }
d16005f8
PC
207 }
208
ba3814c1 209 return nreg;
d16005f8
PC
210}
211
212static void lightrec_discard_nreg(struct native_register *nreg)
213{
214 nreg->extended = false;
98fa08a5 215 nreg->zero_extended = false;
d16005f8 216 nreg->output = false;
d16005f8
PC
217 nreg->used = false;
218 nreg->locked = false;
219 nreg->emulated_register = -1;
ba3814c1 220 nreg->prio = 0;
d16005f8
PC
221}
222
223static void lightrec_unload_nreg(struct regcache *cache, jit_state_t *_jit,
224 struct native_register *nreg, u8 jit_reg)
225{
cb72ea13 226 clean_reg(_jit, nreg, jit_reg, false);
d16005f8
PC
227 lightrec_discard_nreg(nreg);
228}
229
230void lightrec_unload_reg(struct regcache *cache, jit_state_t *_jit, u8 jit_reg)
231{
98fa08a5
PC
232 if (lightrec_reg_is_zero(jit_reg))
233 return;
234
d16005f8
PC
235 lightrec_unload_nreg(cache, _jit,
236 lightning_reg_to_lightrec(cache, jit_reg), jit_reg);
237}
238
d16005f8
PC
239u8 lightrec_alloc_reg(struct regcache *cache, jit_state_t *_jit, u8 jit_reg)
240{
98fa08a5 241 struct native_register *reg;
d16005f8 242
98fa08a5
PC
243 if (lightrec_reg_is_zero(jit_reg))
244 return jit_reg;
245
246 reg = lightning_reg_to_lightrec(cache, jit_reg);
d16005f8
PC
247 lightrec_unload_nreg(cache, _jit, reg, jit_reg);
248
249 reg->used = true;
ba3814c1 250 reg->prio = REG_IS_LOADED;
d16005f8
PC
251 return jit_reg;
252}
253
254u8 lightrec_alloc_reg_temp(struct regcache *cache, jit_state_t *_jit)
255{
256 u8 jit_reg;
257 struct native_register *nreg = alloc_temp(cache);
258 if (!nreg) {
259 /* No free register, no dirty register to free. */
260 pr_err("No more registers! Abandon ship!\n");
261 return 0;
262 }
263
264 jit_reg = lightrec_reg_to_lightning(cache, nreg);
265 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
266
ba3814c1 267 nreg->prio = REG_IS_TEMP;
d16005f8
PC
268 nreg->used = true;
269 return jit_reg;
270}
271
ba3814c1
PC
272s8 lightrec_get_reg_with_value(struct regcache *cache, intptr_t value)
273{
274 struct native_register *nreg;
275 unsigned int i;
276
277 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++) {
278 nreg = &cache->lightrec_regs[i];
279
280 if (nreg->prio == REG_IS_TEMP_VALUE && nreg->value == value) {
281 nreg->used = true;
282 return lightrec_reg_to_lightning(cache, nreg);
283 }
284 }
285
286 return -1;
287}
288
289void lightrec_temp_set_value(struct regcache *cache, u8 jit_reg, intptr_t value)
290{
291 struct native_register *nreg;
292
293 nreg = lightning_reg_to_lightrec(cache, jit_reg);
294
295 nreg->prio = REG_IS_TEMP_VALUE;
296 nreg->value = value;
297}
298
98fa08a5 299u8 lightrec_alloc_reg_out(struct regcache *cache, jit_state_t *_jit,
9259d748 300 u16 reg, u8 flags)
d16005f8 301{
98fa08a5 302 struct native_register *nreg;
d16005f8 303 u8 jit_reg;
98fa08a5
PC
304 s8 hw_reg;
305
306 hw_reg = lightrec_get_hardwired_reg(reg);
307 if (hw_reg >= 0)
308 return (u8) hw_reg;
309
310 nreg = alloc_in_out(cache, reg, true);
d16005f8
PC
311 if (!nreg) {
312 /* No free register, no dirty register to free. */
313 pr_err("No more registers! Abandon ship!\n");
314 return 0;
315 }
316
317 jit_reg = lightrec_reg_to_lightning(cache, nreg);
318
319 /* If we get a dirty register that doesn't correspond to the one
320 * we're requesting, store back the old value */
321 if (nreg->emulated_register != reg)
322 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
323
d16005f8
PC
324 nreg->used = true;
325 nreg->output = true;
326 nreg->emulated_register = reg;
98fa08a5
PC
327 nreg->extend = flags & REG_EXT;
328 nreg->zero_extend = flags & REG_ZEXT;
ba3814c1 329 nreg->prio = reg ? REG_IS_LOADED : REG_IS_ZERO;
d16005f8
PC
330 return jit_reg;
331}
332
98fa08a5 333u8 lightrec_alloc_reg_in(struct regcache *cache, jit_state_t *_jit,
9259d748 334 u16 reg, u8 flags)
d16005f8 335{
98fa08a5 336 struct native_register *nreg;
d16005f8
PC
337 u8 jit_reg;
338 bool reg_changed;
98fa08a5
PC
339 s8 hw_reg;
340
341 hw_reg = lightrec_get_hardwired_reg(reg);
342 if (hw_reg >= 0)
343 return (u8) hw_reg;
344
345 nreg = alloc_in_out(cache, reg, false);
d16005f8
PC
346 if (!nreg) {
347 /* No free register, no dirty register to free. */
348 pr_err("No more registers! Abandon ship!\n");
349 return 0;
350 }
351
352 jit_reg = lightrec_reg_to_lightning(cache, nreg);
353
354 /* If we get a dirty register that doesn't correspond to the one
355 * we're requesting, store back the old value */
356 reg_changed = nreg->emulated_register != reg;
357 if (reg_changed)
358 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
359
ba3814c1 360 if (nreg->prio < REG_IS_LOADED && reg != 0) {
98fa08a5 361 s16 offset = offsetof(struct lightrec_state, regs.gpr)
d16005f8
PC
362 + (reg << 2);
363
98fa08a5
PC
364 nreg->zero_extended = flags & REG_ZEXT;
365 nreg->extended = !nreg->zero_extended;
366
d16005f8 367 /* Load previous value from register cache */
98fa08a5
PC
368 if (nreg->zero_extended)
369 jit_ldxi_ui(jit_reg, LIGHTREC_REG_STATE, offset);
370 else
371 jit_ldxi_i(jit_reg, LIGHTREC_REG_STATE, offset);
372
ba3814c1 373 nreg->prio = REG_IS_LOADED;
d16005f8
PC
374 }
375
376 /* Clear register r0 before use */
ba3814c1 377 if (reg == 0 && nreg->prio != REG_IS_ZERO) {
d16005f8
PC
378 jit_movi(jit_reg, 0);
379 nreg->extended = true;
98fa08a5 380 nreg->zero_extended = true;
ba3814c1 381 nreg->prio = REG_IS_ZERO;
d16005f8
PC
382 }
383
384 nreg->used = true;
385 nreg->output = false;
386 nreg->emulated_register = reg;
d16005f8 387
98fa08a5
PC
388 if ((flags & REG_EXT) && !nreg->extended &&
389 (!nreg->zero_extended || !(flags & REG_ZEXT))) {
d16005f8 390 nreg->extended = true;
98fa08a5 391 nreg->zero_extended = false;
d16005f8 392 jit_extr_i(jit_reg, jit_reg);
98fa08a5
PC
393 } else if (!(flags & REG_EXT) && (flags & REG_ZEXT) &&
394 !nreg->zero_extended) {
395 nreg->zero_extended = true;
396 nreg->extended = false;
397 jit_extr_ui(jit_reg, jit_reg);
d16005f8 398 }
d16005f8
PC
399
400 return jit_reg;
401}
402
9259d748 403static bool reg_pc_is_mapped(struct regcache *cache)
d16005f8 404{
9259d748
PC
405 struct native_register *nreg = lightning_reg_to_lightrec(cache, JIT_V0);
406
407 return nreg->prio == REG_IS_LOADED && nreg->emulated_register == REG_PC;
408}
409
410void lightrec_load_imm(struct regcache *cache,
411 jit_state_t *_jit, u8 jit_reg, u32 pc, u32 imm)
412{
413 s32 delta = imm - pc;
414
415 if (!reg_pc_is_mapped(cache) || !can_sign_extend(delta, 16))
416 jit_movi(jit_reg, imm);
417 else if (jit_reg != JIT_V0 || delta)
418 jit_addi(jit_reg, JIT_V0, delta);
419}
420
421void lightrec_load_next_pc_imm(struct regcache *cache,
422 jit_state_t *_jit, u32 pc, u32 imm)
423{
424 struct native_register *nreg = lightning_reg_to_lightrec(cache, JIT_V0);
0e720fb1
PC
425 u8 reg = JIT_V0;
426
427 if (lightrec_store_next_pc())
428 reg = lightrec_alloc_reg_temp(cache, _jit);
9259d748
PC
429
430 if (reg_pc_is_mapped(cache)) {
431 /* JIT_V0 contains next PC - so we can overwrite it */
0e720fb1 432 lightrec_load_imm(cache, _jit, reg, pc, imm);
9259d748
PC
433 } else {
434 /* JIT_V0 contains something else - invalidate it */
0e720fb1
PC
435 if (reg == JIT_V0)
436 lightrec_unload_reg(cache, _jit, JIT_V0);
9259d748 437
0e720fb1 438 jit_movi(reg, imm);
9259d748
PC
439 }
440
0e720fb1
PC
441 if (lightrec_store_next_pc()) {
442 jit_stxi_i(offsetof(struct lightrec_state, next_pc),
443 LIGHTREC_REG_STATE, reg);
444 lightrec_free_reg(cache, reg);
445 } else {
446 nreg->prio = REG_IS_LOADED;
447 nreg->emulated_register = -1;
448 nreg->locked = true;
449 }
9259d748
PC
450}
451
452void lightrec_load_next_pc(struct regcache *cache, jit_state_t *_jit, u8 reg)
453{
454 struct native_register *nreg_v0, *nreg;
d16005f8 455 u16 offset;
9259d748
PC
456 u8 jit_reg;
457
0e720fb1
PC
458 if (lightrec_store_next_pc()) {
459 jit_reg = lightrec_alloc_reg_in(cache, _jit, reg, 0);
460 offset = offsetof(struct lightrec_state, next_pc);
461 jit_stxi_i(offset, LIGHTREC_REG_STATE, jit_reg);
462 lightrec_free_reg(cache, jit_reg);
463
464 return;
465 }
466
9259d748
PC
467 /* Invalidate JIT_V0 if it is not mapped to 'reg' */
468 nreg_v0 = lightning_reg_to_lightrec(cache, JIT_V0);
469 if (nreg_v0->prio >= REG_IS_LOADED && nreg_v0->emulated_register != reg)
470 lightrec_unload_nreg(cache, _jit, nreg_v0, JIT_V0);
d16005f8
PC
471
472 nreg = find_mapped_reg(cache, reg, false);
9259d748
PC
473 if (!nreg) {
474 /* Not mapped - load the value from the register cache */
d16005f8 475
9259d748
PC
476 offset = offsetof(struct lightrec_state, regs.gpr) + (reg << 2);
477 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE, offset);
d16005f8 478
9259d748
PC
479 nreg_v0->prio = REG_IS_LOADED;
480 nreg_v0->emulated_register = reg;
d16005f8 481
9259d748
PC
482 } else if (nreg == nreg_v0) {
483 /* The target register 'reg' is mapped to JIT_V0 */
d16005f8 484
9259d748
PC
485 if (!nreg->zero_extended)
486 jit_extr_ui(JIT_V0, JIT_V0);
487
488 } else {
489 /* The target register 'reg' is mapped elsewhere. In that case,
490 * move the register's value to JIT_V0 and re-map it in the
491 * register cache. We can then safely discard the original
492 * mapped register (even if it was dirty). */
493
494 jit_reg = lightrec_reg_to_lightning(cache, nreg);
495 if (nreg->zero_extended)
496 jit_movr(JIT_V0, jit_reg);
497 else
498 jit_extr_ui(JIT_V0, jit_reg);
499
500 *nreg_v0 = *nreg;
501 lightrec_discard_nreg(nreg);
502 }
503
0e720fb1
PC
504 if (lightrec_store_next_pc()) {
505 jit_stxi_i(offsetof(struct lightrec_state, next_pc),
506 LIGHTREC_REG_STATE, JIT_V0);
507 } else {
508 lightrec_clean_reg(cache, _jit, JIT_V0);
9259d748 509
0e720fb1
PC
510 nreg_v0->zero_extended = true;
511 nreg_v0->locked = true;
512 }
d16005f8
PC
513}
514
515static void free_reg(struct native_register *nreg)
516{
517 /* Set output registers as dirty */
518 if (nreg->used && nreg->output && nreg->emulated_register > 0)
ba3814c1 519 nreg->prio = REG_IS_DIRTY;
98fa08a5 520 if (nreg->output) {
d16005f8 521 nreg->extended = nreg->extend;
98fa08a5
PC
522 nreg->zero_extended = nreg->zero_extend;
523 }
d16005f8
PC
524 nreg->used = false;
525}
526
527void lightrec_free_reg(struct regcache *cache, u8 jit_reg)
528{
98fa08a5
PC
529 if (!lightrec_reg_is_zero(jit_reg))
530 free_reg(lightning_reg_to_lightrec(cache, jit_reg));
d16005f8
PC
531}
532
533void lightrec_free_regs(struct regcache *cache)
534{
535 unsigned int i;
536
537 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++)
538 free_reg(&cache->lightrec_regs[i]);
539}
540
541static void clean_reg(jit_state_t *_jit,
542 struct native_register *nreg, u8 jit_reg, bool clean)
543{
cb72ea13 544 /* If we get a dirty register, store back the old value */
ba3814c1 545 if (nreg->prio == REG_IS_DIRTY) {
98fa08a5 546 s16 offset = offsetof(struct lightrec_state, regs.gpr)
d16005f8
PC
547 + (nreg->emulated_register << 2);
548
549 jit_stxi_i(offset, LIGHTREC_REG_STATE, jit_reg);
ba3814c1
PC
550
551 if (clean) {
552 if (nreg->emulated_register == 0)
553 nreg->prio = REG_IS_ZERO;
554 else
555 nreg->prio = REG_IS_LOADED;
556 }
d16005f8
PC
557 }
558}
559
560static void clean_regs(struct regcache *cache, jit_state_t *_jit, bool clean)
561{
562 unsigned int i;
563
ba3814c1
PC
564 for (i = 0; i < NUM_REGS; i++) {
565 clean_reg(_jit, &cache->lightrec_regs[i],
566 JIT_V(FIRST_REG + i), clean);
567 }
d16005f8
PC
568 for (i = 0; i < NUM_TEMPS; i++) {
569 clean_reg(_jit, &cache->lightrec_regs[i + NUM_REGS],
ba3814c1 570 JIT_R(FIRST_TEMP + i), clean);
d16005f8
PC
571 }
572}
573
574void lightrec_storeback_regs(struct regcache *cache, jit_state_t *_jit)
575{
576 clean_regs(cache, _jit, false);
577}
578
579void lightrec_clean_regs(struct regcache *cache, jit_state_t *_jit)
580{
581 clean_regs(cache, _jit, true);
582}
583
ba3814c1
PC
584bool lightrec_has_dirty_regs(struct regcache *cache)
585{
586 unsigned int i;
587
588 for (i = 0; i < NUM_REGS + NUM_TEMPS; i++)
589 if (cache->lightrec_regs[i].prio == REG_IS_DIRTY)
590 return true;
591
592 return false;
593}
594
d16005f8
PC
595void lightrec_clean_reg(struct regcache *cache, jit_state_t *_jit, u8 jit_reg)
596{
98fa08a5
PC
597 struct native_register *reg;
598
599 if (!lightrec_reg_is_zero(jit_reg)) {
600 reg = lightning_reg_to_lightrec(cache, jit_reg);
601 clean_reg(_jit, reg, jit_reg, true);
602 }
d16005f8
PC
603}
604
cb72ea13
PC
605bool lightrec_reg_is_loaded(struct regcache *cache, u16 reg)
606{
607 return !!find_mapped_reg(cache, reg, false);
608}
609
d16005f8 610void lightrec_clean_reg_if_loaded(struct regcache *cache, jit_state_t *_jit,
9259d748 611 u16 reg, bool unload)
d16005f8
PC
612{
613 struct native_register *nreg;
614 u8 jit_reg;
615
616 nreg = find_mapped_reg(cache, reg, false);
617 if (nreg) {
618 jit_reg = lightrec_reg_to_lightning(cache, nreg);
619
620 if (unload)
621 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
622 else
623 clean_reg(_jit, nreg, jit_reg, true);
624 }
625}
626
9259d748 627void lightrec_discard_reg_if_loaded(struct regcache *cache, u16 reg)
03535202
PC
628{
629 struct native_register *nreg;
630
631 nreg = find_mapped_reg(cache, reg, false);
632 if (nreg)
633 lightrec_discard_nreg(nreg);
634}
635
d16005f8
PC
636struct native_register * lightrec_regcache_enter_branch(struct regcache *cache)
637{
638 struct native_register *backup;
639
640 backup = lightrec_malloc(cache->state, MEM_FOR_LIGHTREC,
641 sizeof(cache->lightrec_regs));
642 memcpy(backup, &cache->lightrec_regs, sizeof(cache->lightrec_regs));
643
644 return backup;
645}
646
647void lightrec_regcache_leave_branch(struct regcache *cache,
648 struct native_register *regs)
649{
650 memcpy(&cache->lightrec_regs, regs, sizeof(cache->lightrec_regs));
651 lightrec_free(cache->state, MEM_FOR_LIGHTREC,
652 sizeof(cache->lightrec_regs), regs);
653}
654
655void lightrec_regcache_reset(struct regcache *cache)
656{
657 memset(&cache->lightrec_regs, 0, sizeof(cache->lightrec_regs));
658}
659
0e720fb1 660void lightrec_preload_pc(struct regcache *cache, jit_state_t *_jit)
9259d748
PC
661{
662 struct native_register *nreg;
663
664 /* The block's PC is loaded in JIT_V0 at the start of the block */
665 nreg = lightning_reg_to_lightrec(cache, JIT_V0);
666 nreg->emulated_register = REG_PC;
667 nreg->prio = REG_IS_LOADED;
668 nreg->zero_extended = true;
0e720fb1
PC
669
670 jit_live(JIT_V0);
9259d748
PC
671}
672
d16005f8
PC
673struct regcache * lightrec_regcache_init(struct lightrec_state *state)
674{
675 struct regcache *cache;
676
677 cache = lightrec_calloc(state, MEM_FOR_LIGHTREC, sizeof(*cache));
678 if (!cache)
679 return NULL;
680
681 cache->state = state;
682
683 return cache;
684}
685
686void lightrec_free_regcache(struct regcache *cache)
687{
688 return lightrec_free(cache->state, MEM_FOR_LIGHTREC,
689 sizeof(*cache), cache);
690}
691
692void lightrec_regcache_mark_live(struct regcache *cache, jit_state_t *_jit)
693{
694 struct native_register *nreg;
695 unsigned int i;
696
697#ifdef _WIN32
698 /* FIXME: GNU Lightning on Windows seems to use our mapped registers as
699 * temporaries. Until the actual bug is found and fixed, unconditionally
700 * mark our registers as live here. */
701 for (i = 0; i < NUM_REGS; i++) {
702 nreg = &cache->lightrec_regs[i];
703
ba3814c1
PC
704 if (nreg->used || nreg->prio > REG_IS_TEMP)
705 jit_live(JIT_V(FIRST_REG + i));
d16005f8
PC
706 }
707#endif
708
709 for (i = 0; i < NUM_TEMPS; i++) {
710 nreg = &cache->lightrec_regs[NUM_REGS + i];
711
ba3814c1
PC
712 if (nreg->used || nreg->prio > REG_IS_TEMP)
713 jit_live(JIT_R(FIRST_TEMP + i));
d16005f8 714 }
ba3814c1
PC
715
716 jit_live(LIGHTREC_REG_STATE);
717 jit_live(LIGHTREC_REG_CYCLE);
d16005f8 718}