1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
7 #include "memmanager.h"
8 #include "lightning-wrapper.h"
14 #define REG_PC (offsetof(struct lightrec_state, curr_pc) / sizeof(u32))
26 struct native_register {
27 bool used, output, extend, extended,
28 zero_extend, zero_extended, locked;
29 s16 emulated_register;
31 enum reg_priority prio;
35 struct lightrec_state *state;
36 struct native_register lightrec_regs[NUM_REGS + NUM_TEMPS];
39 static const char * mips_regs[] = {
43 "a0", "a1", "a2", "a3",
44 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
45 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
48 "gp", "sp", "fp", "ra",
52 /* Forward declaration(s) */
53 static void clean_reg(jit_state_t *_jit,
54 struct native_register *nreg, u8 jit_reg, bool clean);
56 const char * lightrec_reg_name(u8 reg)
58 return mips_regs[reg];
61 static inline bool lightrec_reg_is_zero(u8 jit_reg)
63 #if defined(__mips__) || defined(__alpha__) || defined(__riscv)
70 static inline s8 lightrec_get_hardwired_reg(u16 reg)
72 #if defined(__mips__) || defined(__alpha__) || defined(__riscv)
79 static inline u8 lightrec_reg_number(const struct regcache *cache,
80 const struct native_register *nreg)
82 return (u8) (((uintptr_t) nreg - (uintptr_t) cache->lightrec_regs)
86 static inline u8 lightrec_reg_to_lightning(const struct regcache *cache,
87 const struct native_register *nreg)
89 u8 offset = lightrec_reg_number(cache, nreg);
91 if (offset < NUM_REGS)
92 return JIT_V(FIRST_REG + offset);
94 return JIT_R(FIRST_TEMP + offset - NUM_REGS);
97 static inline struct native_register * lightning_reg_to_lightrec(
98 struct regcache *cache, u8 reg)
100 if ((JIT_V0 > JIT_R0 && reg >= JIT_V0) ||
101 (JIT_V0 < JIT_R0 && reg < JIT_R0)) {
103 return &cache->lightrec_regs[reg - JIT_V(FIRST_REG)];
105 return &cache->lightrec_regs[JIT_V(FIRST_REG) - reg];
108 return &cache->lightrec_regs[NUM_REGS + reg - JIT_R(FIRST_TEMP)];
110 return &cache->lightrec_regs[NUM_REGS + JIT_R(FIRST_TEMP) - reg];
114 u8 lightrec_get_reg_in_flags(struct regcache *cache, u8 jit_reg)
116 struct native_register *reg;
119 if (lightrec_reg_is_zero(jit_reg))
120 return REG_EXT | REG_ZEXT;
122 reg = lightning_reg_to_lightrec(cache, jit_reg);
125 if (reg->zero_extended)
131 void lightrec_set_reg_out_flags(struct regcache *cache, u8 jit_reg, u8 flags)
133 struct native_register *reg;
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;
142 static struct native_register * alloc_temp(struct regcache *cache)
144 struct native_register *elm, *nreg = NULL;
145 enum reg_priority best = REG_NB_PRIORITIES;
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
152 for (i = ARRAY_SIZE(cache->lightrec_regs); i; i--) {
153 elm = &cache->lightrec_regs[i - 1];
155 if (!elm->used && !elm->locked && elm->prio < best) {
159 if (best == REG_IS_TEMP)
167 static struct native_register * find_mapped_reg(struct regcache *cache,
172 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++) {
173 struct native_register *nreg = &cache->lightrec_regs[i];
174 if ((nreg->prio >= REG_IS_ZERO) &&
175 nreg->emulated_register == reg &&
176 (!out || !nreg->locked))
183 static struct native_register * alloc_in_out(struct regcache *cache,
186 struct native_register *elm, *nreg = NULL;
187 enum reg_priority best = REG_NB_PRIORITIES;
190 /* Try to find if the register is already mapped somewhere */
191 nreg = find_mapped_reg(cache, reg, out);
197 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++) {
198 elm = &cache->lightrec_regs[i];
200 if (!elm->used && !elm->locked && elm->prio < best) {
204 if (best == REG_IS_TEMP)
212 static void lightrec_discard_nreg(struct native_register *nreg)
214 nreg->extended = false;
215 nreg->zero_extended = false;
216 nreg->output = false;
218 nreg->locked = false;
219 nreg->emulated_register = -1;
223 static void lightrec_unload_nreg(struct regcache *cache, jit_state_t *_jit,
224 struct native_register *nreg, u8 jit_reg)
226 clean_reg(_jit, nreg, jit_reg, false);
227 lightrec_discard_nreg(nreg);
230 void lightrec_unload_reg(struct regcache *cache, jit_state_t *_jit, u8 jit_reg)
232 if (lightrec_reg_is_zero(jit_reg))
235 lightrec_unload_nreg(cache, _jit,
236 lightning_reg_to_lightrec(cache, jit_reg), jit_reg);
239 u8 lightrec_alloc_reg(struct regcache *cache, jit_state_t *_jit, u8 jit_reg)
241 struct native_register *reg;
243 if (lightrec_reg_is_zero(jit_reg))
246 reg = lightning_reg_to_lightrec(cache, jit_reg);
247 lightrec_unload_nreg(cache, _jit, reg, jit_reg);
250 reg->prio = REG_IS_LOADED;
254 u8 lightrec_alloc_reg_temp(struct regcache *cache, jit_state_t *_jit)
257 struct native_register *nreg = alloc_temp(cache);
259 /* No free register, no dirty register to free. */
260 pr_err("No more registers! Abandon ship!\n");
264 jit_reg = lightrec_reg_to_lightning(cache, nreg);
265 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
267 nreg->prio = REG_IS_TEMP;
272 s8 lightrec_get_reg_with_value(struct regcache *cache, intptr_t value)
274 struct native_register *nreg;
277 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++) {
278 nreg = &cache->lightrec_regs[i];
280 if (nreg->prio == REG_IS_TEMP_VALUE && nreg->value == value) {
282 return lightrec_reg_to_lightning(cache, nreg);
289 void lightrec_temp_set_value(struct regcache *cache, u8 jit_reg, intptr_t value)
291 struct native_register *nreg;
293 nreg = lightning_reg_to_lightrec(cache, jit_reg);
295 nreg->prio = REG_IS_TEMP_VALUE;
299 u8 lightrec_alloc_reg_out(struct regcache *cache, jit_state_t *_jit,
302 struct native_register *nreg;
306 hw_reg = lightrec_get_hardwired_reg(reg);
310 nreg = alloc_in_out(cache, reg, true);
312 /* No free register, no dirty register to free. */
313 pr_err("No more registers! Abandon ship!\n");
317 jit_reg = lightrec_reg_to_lightning(cache, nreg);
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);
326 nreg->emulated_register = reg;
327 nreg->extend = flags & REG_EXT;
328 nreg->zero_extend = flags & REG_ZEXT;
329 nreg->prio = reg ? REG_IS_LOADED : REG_IS_ZERO;
333 u8 lightrec_alloc_reg_in(struct regcache *cache, jit_state_t *_jit,
336 struct native_register *nreg;
341 hw_reg = lightrec_get_hardwired_reg(reg);
345 nreg = alloc_in_out(cache, reg, false);
347 /* No free register, no dirty register to free. */
348 pr_err("No more registers! Abandon ship!\n");
352 jit_reg = lightrec_reg_to_lightning(cache, nreg);
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;
358 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
360 if (nreg->prio < REG_IS_LOADED && reg != 0) {
361 s16 offset = offsetof(struct lightrec_state, regs.gpr)
364 nreg->zero_extended = flags & REG_ZEXT;
365 nreg->extended = !nreg->zero_extended;
367 /* Load previous value from register cache */
368 if (nreg->zero_extended)
369 jit_ldxi_ui(jit_reg, LIGHTREC_REG_STATE, offset);
371 jit_ldxi_i(jit_reg, LIGHTREC_REG_STATE, offset);
373 nreg->prio = REG_IS_LOADED;
376 /* Clear register r0 before use */
377 if (reg == 0 && nreg->prio != REG_IS_ZERO) {
378 jit_movi(jit_reg, 0);
379 nreg->extended = true;
380 nreg->zero_extended = true;
381 nreg->prio = REG_IS_ZERO;
385 nreg->output = false;
386 nreg->emulated_register = reg;
388 if ((flags & REG_EXT) && !nreg->extended &&
389 (!nreg->zero_extended || !(flags & REG_ZEXT))) {
390 nreg->extended = true;
391 nreg->zero_extended = false;
392 jit_extr_i(jit_reg, jit_reg);
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);
403 static bool reg_pc_is_mapped(struct regcache *cache)
405 struct native_register *nreg = lightning_reg_to_lightrec(cache, JIT_V0);
407 return nreg->prio == REG_IS_LOADED && nreg->emulated_register == REG_PC;
410 void lightrec_load_imm(struct regcache *cache,
411 jit_state_t *_jit, u8 jit_reg, u32 pc, u32 imm)
413 s32 delta = imm - pc;
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);
421 void lightrec_load_next_pc_imm(struct regcache *cache,
422 jit_state_t *_jit, u32 pc, u32 imm)
424 struct native_register *nreg = lightning_reg_to_lightrec(cache, JIT_V0);
427 if (lightrec_store_next_pc())
428 reg = lightrec_alloc_reg_temp(cache, _jit);
430 if (reg_pc_is_mapped(cache)) {
431 /* JIT_V0 contains next PC - so we can overwrite it */
432 lightrec_load_imm(cache, _jit, reg, pc, imm);
434 /* JIT_V0 contains something else - invalidate it */
436 lightrec_unload_reg(cache, _jit, JIT_V0);
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);
446 nreg->prio = REG_IS_LOADED;
447 nreg->emulated_register = -1;
452 void lightrec_load_next_pc(struct regcache *cache, jit_state_t *_jit, u8 reg)
454 struct native_register *nreg_v0, *nreg;
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);
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);
472 nreg = find_mapped_reg(cache, reg, false);
474 /* Not mapped - load the value from the register cache */
476 offset = offsetof(struct lightrec_state, regs.gpr) + (reg << 2);
477 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE, offset);
479 nreg_v0->prio = REG_IS_LOADED;
480 nreg_v0->emulated_register = reg;
482 } else if (nreg == nreg_v0) {
483 /* The target register 'reg' is mapped to JIT_V0 */
485 if (!nreg->zero_extended)
486 jit_extr_ui(JIT_V0, JIT_V0);
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). */
494 jit_reg = lightrec_reg_to_lightning(cache, nreg);
495 if (nreg->zero_extended)
496 jit_movr(JIT_V0, jit_reg);
498 jit_extr_ui(JIT_V0, jit_reg);
501 lightrec_discard_nreg(nreg);
504 if (lightrec_store_next_pc()) {
505 jit_stxi_i(offsetof(struct lightrec_state, next_pc),
506 LIGHTREC_REG_STATE, JIT_V0);
508 lightrec_clean_reg(cache, _jit, JIT_V0);
510 nreg_v0->zero_extended = true;
511 nreg_v0->locked = true;
515 static void free_reg(struct native_register *nreg)
517 /* Set output registers as dirty */
518 if (nreg->used && nreg->output && nreg->emulated_register > 0)
519 nreg->prio = REG_IS_DIRTY;
521 nreg->extended = nreg->extend;
522 nreg->zero_extended = nreg->zero_extend;
527 void lightrec_free_reg(struct regcache *cache, u8 jit_reg)
529 if (!lightrec_reg_is_zero(jit_reg))
530 free_reg(lightning_reg_to_lightrec(cache, jit_reg));
533 void lightrec_free_regs(struct regcache *cache)
537 for (i = 0; i < ARRAY_SIZE(cache->lightrec_regs); i++)
538 free_reg(&cache->lightrec_regs[i]);
541 static void clean_reg(jit_state_t *_jit,
542 struct native_register *nreg, u8 jit_reg, bool clean)
544 /* If we get a dirty register, store back the old value */
545 if (nreg->prio == REG_IS_DIRTY) {
546 s16 offset = offsetof(struct lightrec_state, regs.gpr)
547 + (nreg->emulated_register << 2);
549 jit_stxi_i(offset, LIGHTREC_REG_STATE, jit_reg);
552 if (nreg->emulated_register == 0)
553 nreg->prio = REG_IS_ZERO;
555 nreg->prio = REG_IS_LOADED;
560 static void clean_regs(struct regcache *cache, jit_state_t *_jit, bool clean)
564 for (i = 0; i < NUM_REGS; i++) {
565 clean_reg(_jit, &cache->lightrec_regs[i],
566 JIT_V(FIRST_REG + i), clean);
568 for (i = 0; i < NUM_TEMPS; i++) {
569 clean_reg(_jit, &cache->lightrec_regs[i + NUM_REGS],
570 JIT_R(FIRST_TEMP + i), clean);
574 void lightrec_storeback_regs(struct regcache *cache, jit_state_t *_jit)
576 clean_regs(cache, _jit, false);
579 void lightrec_clean_regs(struct regcache *cache, jit_state_t *_jit)
581 clean_regs(cache, _jit, true);
584 bool lightrec_has_dirty_regs(struct regcache *cache)
588 for (i = 0; i < NUM_REGS + NUM_TEMPS; i++)
589 if (cache->lightrec_regs[i].prio == REG_IS_DIRTY)
595 void lightrec_clean_reg(struct regcache *cache, jit_state_t *_jit, u8 jit_reg)
597 struct native_register *reg;
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);
605 bool lightrec_reg_is_loaded(struct regcache *cache, u16 reg)
607 return !!find_mapped_reg(cache, reg, false);
610 void lightrec_clean_reg_if_loaded(struct regcache *cache, jit_state_t *_jit,
611 u16 reg, bool unload)
613 struct native_register *nreg;
616 nreg = find_mapped_reg(cache, reg, false);
618 jit_reg = lightrec_reg_to_lightning(cache, nreg);
621 lightrec_unload_nreg(cache, _jit, nreg, jit_reg);
623 clean_reg(_jit, nreg, jit_reg, true);
627 void lightrec_discard_reg_if_loaded(struct regcache *cache, u16 reg)
629 struct native_register *nreg;
631 nreg = find_mapped_reg(cache, reg, false);
633 lightrec_discard_nreg(nreg);
636 struct native_register * lightrec_regcache_enter_branch(struct regcache *cache)
638 struct native_register *backup;
640 backup = lightrec_malloc(cache->state, MEM_FOR_LIGHTREC,
641 sizeof(cache->lightrec_regs));
642 memcpy(backup, &cache->lightrec_regs, sizeof(cache->lightrec_regs));
647 void lightrec_regcache_leave_branch(struct regcache *cache,
648 struct native_register *regs)
650 memcpy(&cache->lightrec_regs, regs, sizeof(cache->lightrec_regs));
651 lightrec_free(cache->state, MEM_FOR_LIGHTREC,
652 sizeof(cache->lightrec_regs), regs);
655 void lightrec_regcache_reset(struct regcache *cache)
657 memset(&cache->lightrec_regs, 0, sizeof(cache->lightrec_regs));
660 void lightrec_preload_pc(struct regcache *cache, jit_state_t *_jit)
662 struct native_register *nreg;
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;
673 struct regcache * lightrec_regcache_init(struct lightrec_state *state)
675 struct regcache *cache;
677 cache = lightrec_calloc(state, MEM_FOR_LIGHTREC, sizeof(*cache));
681 cache->state = state;
686 void lightrec_free_regcache(struct regcache *cache)
688 return lightrec_free(cache->state, MEM_FOR_LIGHTREC,
689 sizeof(*cache), cache);
692 void lightrec_regcache_mark_live(struct regcache *cache, jit_state_t *_jit)
694 struct native_register *nreg;
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];
704 if (nreg->used || nreg->prio > REG_IS_TEMP)
705 jit_live(JIT_V(FIRST_REG + i));
709 for (i = 0; i < NUM_TEMPS; i++) {
710 nreg = &cache->lightrec_regs[NUM_REGS + i];
712 if (nreg->used || nreg->prio > REG_IS_TEMP)
713 jit_live(JIT_R(FIRST_TEMP + i));
716 jit_live(LIGHTREC_REG_STATE);
717 jit_live(LIGHTREC_REG_CYCLE);