1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
6 #include "blockcache.h"
8 #include "disassembler.h"
10 #include "lightning-wrapper.h"
11 #include "optimizer.h"
17 typedef void (*lightrec_rec_func_t)(struct lightrec_cstate *, const struct block *, u16);
19 /* Forward declarations */
20 static void rec_SPECIAL(struct lightrec_cstate *state, const struct block *block, u16 offset);
21 static void rec_REGIMM(struct lightrec_cstate *state, const struct block *block, u16 offset);
22 static void rec_CP0(struct lightrec_cstate *state, const struct block *block, u16 offset);
23 static void rec_CP2(struct lightrec_cstate *state, const struct block *block, u16 offset);
25 static void unknown_opcode(struct lightrec_cstate *state, const struct block *block, u16 offset)
27 pr_warn("Unknown opcode: 0x%08x at PC 0x%08x\n",
28 block->opcode_list[offset].c.opcode,
29 block->pc + (offset << 2));
33 lightrec_jump_to_eob(struct lightrec_cstate *state, jit_state_t *_jit)
35 /* Prevent jit_jmpi() from using our cycles register as a temporary */
36 jit_live(LIGHTREC_REG_CYCLE);
38 jit_patch_abs(jit_jmpi(), state->state->eob_wrapper_func);
41 static void lightrec_emit_end_of_block(struct lightrec_cstate *state,
42 const struct block *block, u16 offset,
43 s8 reg_new_pc, u32 imm, u8 ra_reg,
44 u32 link, bool update_cycles)
46 struct regcache *reg_cache = state->reg_cache;
47 jit_state_t *_jit = block->_jit;
48 const struct opcode *op = &block->opcode_list[offset],
49 *next = &block->opcode_list[offset + 1];
50 u32 cycles = state->cycles + lightrec_cycles_of_opcode(op->c);
52 jit_note(__FILE__, __LINE__);
55 /* Update the $ra register */
56 u8 link_reg = lightrec_alloc_reg_out(reg_cache, _jit, ra_reg, 0);
57 jit_movi(link_reg, link);
58 lightrec_free_reg(reg_cache, link_reg);
62 reg_new_pc = lightrec_alloc_reg(reg_cache, _jit, JIT_V0);
63 lightrec_lock_reg(reg_cache, _jit, reg_new_pc);
65 jit_movi(reg_new_pc, imm);
68 if (has_delay_slot(op->c) &&
69 !op_flag_no_ds(op->flags) && !op_flag_local_branch(op->flags)) {
70 cycles += lightrec_cycles_of_opcode(next->c);
72 /* Recompile the delay slot */
74 lightrec_rec_opcode(state, block, offset + 1);
77 /* Clean the remaining registers */
78 lightrec_clean_regs(reg_cache, _jit);
80 jit_movr(JIT_V0, reg_new_pc);
82 if (cycles && update_cycles) {
83 jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, cycles);
84 pr_debug("EOB: %u cycles\n", cycles);
87 lightrec_jump_to_eob(state, _jit);
90 void lightrec_emit_eob(struct lightrec_cstate *state, const struct block *block,
91 u16 offset, bool after_op)
93 struct regcache *reg_cache = state->reg_cache;
94 jit_state_t *_jit = block->_jit;
95 union code c = block->opcode_list[offset].c;
96 u32 cycles = state->cycles;
99 cycles += lightrec_cycles_of_opcode(c);
101 lightrec_clean_regs(reg_cache, _jit);
103 jit_movi(JIT_V0, block->pc + (offset << 2));
104 jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, cycles);
106 lightrec_jump_to_eob(state, _jit);
109 static u8 get_jr_jalr_reg(struct lightrec_cstate *state, const struct block *block, u16 offset)
111 struct regcache *reg_cache = state->reg_cache;
112 jit_state_t *_jit = block->_jit;
113 const struct opcode *op = &block->opcode_list[offset];
116 rs = lightrec_request_reg_in(reg_cache, _jit, op->r.rs, JIT_V0);
117 lightrec_lock_reg(reg_cache, _jit, rs);
122 static void rec_special_JR(struct lightrec_cstate *state, const struct block *block, u16 offset)
124 u8 rs = get_jr_jalr_reg(state, block, offset);
126 _jit_name(block->_jit, __func__);
127 lightrec_emit_end_of_block(state, block, offset, rs, 0, 31, 0, true);
130 static void rec_special_JALR(struct lightrec_cstate *state, const struct block *block, u16 offset)
132 u8 rs = get_jr_jalr_reg(state, block, offset);
133 union code c = block->opcode_list[offset].c;
135 _jit_name(block->_jit, __func__);
136 lightrec_emit_end_of_block(state, block, offset, rs, 0, c.r.rd,
137 get_branch_pc(block, offset, 2), true);
140 static void rec_J(struct lightrec_cstate *state, const struct block *block, u16 offset)
142 union code c = block->opcode_list[offset].c;
144 _jit_name(block->_jit, __func__);
145 lightrec_emit_end_of_block(state, block, offset, -1,
146 (block->pc & 0xf0000000) | (c.j.imm << 2),
150 static void rec_JAL(struct lightrec_cstate *state, const struct block *block, u16 offset)
152 union code c = block->opcode_list[offset].c;
154 _jit_name(block->_jit, __func__);
155 lightrec_emit_end_of_block(state, block, offset, -1,
156 (block->pc & 0xf0000000) | (c.j.imm << 2),
157 31, get_branch_pc(block, offset, 2), true);
160 static void lightrec_do_early_unload(struct lightrec_cstate *state,
161 const struct block *block, u16 offset)
163 struct regcache *reg_cache = state->reg_cache;
164 const struct opcode *op = &block->opcode_list[offset];
165 jit_state_t *_jit = block->_jit;
171 { op->r.rd, LIGHTREC_FLAGS_GET_RD(op->flags), },
172 { op->i.rt, LIGHTREC_FLAGS_GET_RT(op->flags), },
173 { op->i.rs, LIGHTREC_FLAGS_GET_RS(op->flags), },
176 for (i = 0; i < ARRAY_SIZE(reg_ops); i++) {
177 reg = reg_ops[i].reg;
179 switch (reg_ops[i].op) {
180 case LIGHTREC_REG_UNLOAD:
181 lightrec_clean_reg_if_loaded(reg_cache, _jit, reg, true);
184 case LIGHTREC_REG_DISCARD:
185 lightrec_discard_reg_if_loaded(reg_cache, reg);
188 case LIGHTREC_REG_CLEAN:
189 lightrec_clean_reg_if_loaded(reg_cache, _jit, reg, false);
197 static void rec_b(struct lightrec_cstate *state, const struct block *block, u16 offset,
198 jit_code_t code, jit_code_t code2, u32 link, bool unconditional, bool bz)
200 struct regcache *reg_cache = state->reg_cache;
201 struct native_register *regs_backup;
202 jit_state_t *_jit = block->_jit;
203 struct lightrec_branch *branch;
204 const struct opcode *op = &block->opcode_list[offset],
205 *next = &block->opcode_list[offset + 1];
208 bool is_forward = (s16)op->i.imm >= -1;
209 int op_cycles = lightrec_cycles_of_opcode(op->c);
210 u32 target_offset, cycles = state->cycles + op_cycles;
211 bool no_indirection = false;
214 jit_note(__FILE__, __LINE__);
216 if (!op_flag_no_ds(op->flags))
217 cycles += lightrec_cycles_of_opcode(next->c);
219 state->cycles = -op_cycles;
221 if (!unconditional) {
222 rs = lightrec_alloc_reg_in(reg_cache, _jit, op->i.rs, REG_EXT);
223 rt = bz ? 0 : lightrec_alloc_reg_in(reg_cache,
224 _jit, op->i.rt, REG_EXT);
226 /* Unload dead registers before evaluating the branch */
227 if (OPT_EARLY_UNLOAD)
228 lightrec_do_early_unload(state, block, offset);
230 if (op_flag_local_branch(op->flags) &&
231 (op_flag_no_ds(op->flags) || !next->opcode) &&
232 is_forward && !lightrec_has_dirty_regs(reg_cache))
233 no_indirection = true;
236 pr_debug("Using no indirection for branch at offset 0x%hx\n", offset << 2);
240 jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, cycles);
242 if (!unconditional) {
243 /* Generate the branch opcode */
245 addr = jit_new_node_pww(code, NULL, rs, rt);
247 lightrec_free_regs(reg_cache);
248 regs_backup = lightrec_regcache_enter_branch(reg_cache);
251 if (op_flag_local_branch(op->flags)) {
252 /* Recompile the delay slot */
253 if (next && next->opcode && !op_flag_no_ds(op->flags))
254 lightrec_rec_opcode(state, block, offset + 1);
257 /* Update the $ra register */
258 link_reg = lightrec_alloc_reg_out(reg_cache, _jit, 31, 0);
259 jit_movi(link_reg, link);
260 lightrec_free_reg(reg_cache, link_reg);
263 /* Clean remaining registers */
264 lightrec_clean_regs(reg_cache, _jit);
266 target_offset = offset + 1 + (s16)op->i.imm
267 - !!op_flag_no_ds(op->flags);
268 pr_debug("Adding local branch to offset 0x%x\n",
270 branch = &state->local_branches[
271 state->nb_local_branches++];
273 branch->target = target_offset;
276 branch->branch = jit_new_node_pww(code2, NULL, rs, rt);
278 branch->branch = jit_b();
280 branch->branch = jit_bgti(LIGHTREC_REG_CYCLE, 0);
283 if (!op_flag_local_branch(op->flags) || !is_forward) {
284 next_pc = get_branch_pc(block, offset, 1 + (s16)op->i.imm);
285 lightrec_emit_end_of_block(state, block, offset, -1, next_pc,
289 if (!unconditional) {
293 lightrec_regcache_leave_branch(reg_cache, regs_backup);
296 /* Update the $ra register */
297 link_reg = lightrec_alloc_reg_out(reg_cache, _jit,
299 jit_movi(link_reg, (s32)link);
300 lightrec_free_reg(reg_cache, link_reg);
303 if (!op_flag_no_ds(op->flags) && next->opcode)
304 lightrec_rec_opcode(state, block, offset + 1);
308 static void rec_BNE(struct lightrec_cstate *state,
309 const struct block *block, u16 offset)
311 union code c = block->opcode_list[offset].c;
313 _jit_name(block->_jit, __func__);
316 rec_b(state, block, offset, jit_code_beqi, jit_code_bnei, 0, false, true);
318 rec_b(state, block, offset, jit_code_beqr, jit_code_bner, 0, false, false);
321 static void rec_BEQ(struct lightrec_cstate *state,
322 const struct block *block, u16 offset)
324 union code c = block->opcode_list[offset].c;
326 _jit_name(block->_jit, __func__);
329 rec_b(state, block, offset, jit_code_bnei, jit_code_beqi, 0, c.i.rs == 0, true);
331 rec_b(state, block, offset, jit_code_bner, jit_code_beqr, 0, c.i.rs == c.i.rt, false);
334 static void rec_BLEZ(struct lightrec_cstate *state,
335 const struct block *block, u16 offset)
337 union code c = block->opcode_list[offset].c;
339 _jit_name(block->_jit, __func__);
340 rec_b(state, block, offset, jit_code_bgti, jit_code_blei, 0, c.i.rs == 0, true);
343 static void rec_BGTZ(struct lightrec_cstate *state,
344 const struct block *block, u16 offset)
346 _jit_name(block->_jit, __func__);
347 rec_b(state, block, offset, jit_code_blei, jit_code_bgti, 0, false, true);
350 static void rec_regimm_BLTZ(struct lightrec_cstate *state,
351 const struct block *block, u16 offset)
353 _jit_name(block->_jit, __func__);
354 rec_b(state, block, offset, jit_code_bgei, jit_code_blti, 0, false, true);
357 static void rec_regimm_BLTZAL(struct lightrec_cstate *state,
358 const struct block *block, u16 offset)
360 _jit_name(block->_jit, __func__);
361 rec_b(state, block, offset, jit_code_bgei, jit_code_blti,
362 get_branch_pc(block, offset, 2), false, true);
365 static void rec_regimm_BGEZ(struct lightrec_cstate *state,
366 const struct block *block, u16 offset)
368 union code c = block->opcode_list[offset].c;
370 _jit_name(block->_jit, __func__);
371 rec_b(state, block, offset, jit_code_blti, jit_code_bgei, 0, !c.i.rs, true);
374 static void rec_regimm_BGEZAL(struct lightrec_cstate *state,
375 const struct block *block, u16 offset)
377 const struct opcode *op = &block->opcode_list[offset];
378 _jit_name(block->_jit, __func__);
379 rec_b(state, block, offset, jit_code_blti, jit_code_bgei,
380 get_branch_pc(block, offset, 2),
384 static void rec_alu_imm(struct lightrec_cstate *state, const struct block *block,
385 u16 offset, jit_code_t code, bool slti)
387 struct regcache *reg_cache = state->reg_cache;
388 union code c = block->opcode_list[offset].c;
389 jit_state_t *_jit = block->_jit;
390 u8 rs, rt, out_flags = REG_EXT;
393 out_flags |= REG_ZEXT;
395 jit_note(__FILE__, __LINE__);
396 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, REG_EXT);
397 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, out_flags);
399 jit_new_node_www(code, rt, rs, (s32)(s16) c.i.imm);
401 lightrec_free_reg(reg_cache, rs);
402 lightrec_free_reg(reg_cache, rt);
405 static void rec_alu_special(struct lightrec_cstate *state, const struct block *block,
406 u16 offset, jit_code_t code, bool out_ext)
408 struct regcache *reg_cache = state->reg_cache;
409 union code c = block->opcode_list[offset].c;
410 jit_state_t *_jit = block->_jit;
413 jit_note(__FILE__, __LINE__);
414 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, REG_EXT);
415 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, REG_EXT);
416 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd,
417 out_ext ? REG_EXT | REG_ZEXT : 0);
419 jit_new_node_www(code, rd, rs, rt);
421 lightrec_free_reg(reg_cache, rs);
422 lightrec_free_reg(reg_cache, rt);
423 lightrec_free_reg(reg_cache, rd);
426 static void rec_alu_shiftv(struct lightrec_cstate *state, const struct block *block,
427 u16 offset, jit_code_t code)
429 struct regcache *reg_cache = state->reg_cache;
430 union code c = block->opcode_list[offset].c;
431 jit_state_t *_jit = block->_jit;
432 u8 rd, rt, rs, temp, flags = 0;
434 jit_note(__FILE__, __LINE__);
435 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0);
437 if (code == jit_code_rshr)
439 else if (code == jit_code_rshr_u)
442 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, flags);
443 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, flags);
445 if (rs != rd && rt != rd) {
446 jit_andi(rd, rs, 0x1f);
447 jit_new_node_www(code, rd, rt, rd);
449 temp = lightrec_alloc_reg_temp(reg_cache, _jit);
450 jit_andi(temp, rs, 0x1f);
451 jit_new_node_www(code, rd, rt, temp);
452 lightrec_free_reg(reg_cache, temp);
455 lightrec_free_reg(reg_cache, rs);
456 lightrec_free_reg(reg_cache, rt);
457 lightrec_free_reg(reg_cache, rd);
460 static void rec_movi(struct lightrec_cstate *state,
461 const struct block *block, u16 offset)
463 struct regcache *reg_cache = state->reg_cache;
464 union code c = block->opcode_list[offset].c;
465 jit_state_t *_jit = block->_jit;
469 if (!(c.i.imm & 0x8000))
472 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, flags);
474 jit_movi(rt, (s32)(s16) c.i.imm);
476 lightrec_free_reg(reg_cache, rt);
479 static void rec_ADDIU(struct lightrec_cstate *state,
480 const struct block *block, u16 offset)
482 _jit_name(block->_jit, __func__);
484 if (block->opcode_list[offset].c.i.rs)
485 rec_alu_imm(state, block, offset, jit_code_addi, false);
487 rec_movi(state, block, offset);
490 static void rec_ADDI(struct lightrec_cstate *state,
491 const struct block *block, u16 offset)
493 /* TODO: Handle the exception? */
494 _jit_name(block->_jit, __func__);
495 rec_ADDIU(state, block, offset);
498 static void rec_SLTIU(struct lightrec_cstate *state,
499 const struct block *block, u16 offset)
501 _jit_name(block->_jit, __func__);
502 rec_alu_imm(state, block, offset, jit_code_lti_u, true);
505 static void rec_SLTI(struct lightrec_cstate *state,
506 const struct block *block, u16 offset)
508 _jit_name(block->_jit, __func__);
509 rec_alu_imm(state, block, offset, jit_code_lti, true);
512 static void rec_ANDI(struct lightrec_cstate *state,
513 const struct block *block, u16 offset)
515 struct regcache *reg_cache = state->reg_cache;
516 union code c = block->opcode_list[offset].c;
517 jit_state_t *_jit = block->_jit;
520 _jit_name(block->_jit, __func__);
521 jit_note(__FILE__, __LINE__);
522 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
523 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt,
526 /* PSX code uses ANDI 0xff / ANDI 0xffff a lot, which are basically
527 * casts to uint8_t / uint16_t. */
530 else if (c.i.imm == 0xffff)
533 jit_andi(rt, rs, (u32)(u16) c.i.imm);
535 lightrec_free_reg(reg_cache, rs);
536 lightrec_free_reg(reg_cache, rt);
539 static void rec_alu_or_xor(struct lightrec_cstate *state, const struct block *block,
540 u16 offset, jit_code_t code)
542 struct regcache *reg_cache = state->reg_cache;
543 union code c = block->opcode_list[offset].c;
544 jit_state_t *_jit = block->_jit;
547 jit_note(__FILE__, __LINE__);
548 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
549 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, 0);
551 flags = lightrec_get_reg_in_flags(reg_cache, rs);
552 lightrec_set_reg_out_flags(reg_cache, rt, flags);
554 jit_new_node_www(code, rt, rs, (u32)(u16) c.i.imm);
556 lightrec_free_reg(reg_cache, rs);
557 lightrec_free_reg(reg_cache, rt);
561 static void rec_ORI(struct lightrec_cstate *state,
562 const struct block *block, u16 offset)
564 _jit_name(block->_jit, __func__);
565 rec_alu_or_xor(state, block, offset, jit_code_ori);
568 static void rec_XORI(struct lightrec_cstate *state,
569 const struct block *block, u16 offset)
571 _jit_name(block->_jit, __func__);
572 rec_alu_or_xor(state, block, offset, jit_code_xori);
575 static void rec_LUI(struct lightrec_cstate *state,
576 const struct block *block, u16 offset)
578 struct regcache *reg_cache = state->reg_cache;
579 union code c = block->opcode_list[offset].c;
580 jit_state_t *_jit = block->_jit;
581 u8 rt, flags = REG_EXT;
584 jit_note(__FILE__, __LINE__);
586 if (!(c.i.imm & BIT(15)))
589 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, flags);
591 jit_movi(rt, (s32)(c.i.imm << 16));
593 lightrec_free_reg(reg_cache, rt);
596 static void rec_special_ADDU(struct lightrec_cstate *state,
597 const struct block *block, u16 offset)
599 _jit_name(block->_jit, __func__);
600 rec_alu_special(state, block, offset, jit_code_addr, false);
603 static void rec_special_ADD(struct lightrec_cstate *state,
604 const struct block *block, u16 offset)
606 /* TODO: Handle the exception? */
607 _jit_name(block->_jit, __func__);
608 rec_alu_special(state, block, offset, jit_code_addr, false);
611 static void rec_special_SUBU(struct lightrec_cstate *state,
612 const struct block *block, u16 offset)
614 _jit_name(block->_jit, __func__);
615 rec_alu_special(state, block, offset, jit_code_subr, false);
618 static void rec_special_SUB(struct lightrec_cstate *state,
619 const struct block *block, u16 offset)
621 /* TODO: Handle the exception? */
622 _jit_name(block->_jit, __func__);
623 rec_alu_special(state, block, offset, jit_code_subr, false);
626 static void rec_special_AND(struct lightrec_cstate *state,
627 const struct block *block, u16 offset)
629 struct regcache *reg_cache = state->reg_cache;
630 union code c = block->opcode_list[offset].c;
631 jit_state_t *_jit = block->_jit;
632 u8 rd, rt, rs, flags_rs, flags_rt, flags_rd;
634 _jit_name(block->_jit, __func__);
635 jit_note(__FILE__, __LINE__);
636 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0);
637 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0);
638 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, 0);
640 flags_rs = lightrec_get_reg_in_flags(reg_cache, rs);
641 flags_rt = lightrec_get_reg_in_flags(reg_cache, rt);
643 /* Z(rd) = Z(rs) | Z(rt) */
644 flags_rd = REG_ZEXT & (flags_rs | flags_rt);
646 /* E(rd) = (E(rt) & Z(rt)) | (E(rs) & Z(rs)) | (E(rs) & E(rt)) */
647 if (((flags_rs & REG_EXT) && (flags_rt & REG_ZEXT)) ||
648 ((flags_rt & REG_EXT) && (flags_rs & REG_ZEXT)) ||
649 (REG_EXT & flags_rs & flags_rt))
652 lightrec_set_reg_out_flags(reg_cache, rd, flags_rd);
654 jit_andr(rd, rs, rt);
656 lightrec_free_reg(reg_cache, rs);
657 lightrec_free_reg(reg_cache, rt);
658 lightrec_free_reg(reg_cache, rd);
661 static void rec_special_or_nor(struct lightrec_cstate *state,
662 const struct block *block, u16 offset, bool nor)
664 struct regcache *reg_cache = state->reg_cache;
665 union code c = block->opcode_list[offset].c;
666 jit_state_t *_jit = block->_jit;
667 u8 rd, rt, rs, flags_rs, flags_rt, flags_rd = 0;
669 jit_note(__FILE__, __LINE__);
670 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0);
671 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0);
672 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, 0);
674 flags_rs = lightrec_get_reg_in_flags(reg_cache, rs);
675 flags_rt = lightrec_get_reg_in_flags(reg_cache, rt);
677 /* or: Z(rd) = Z(rs) & Z(rt)
680 flags_rd = REG_ZEXT & flags_rs & flags_rt;
682 /* E(rd) = (E(rs) & E(rt)) | (E(rt) & !Z(rt)) | (E(rs) & !Z(rs)) */
683 if ((REG_EXT & flags_rs & flags_rt) ||
684 ((flags_rt & (REG_EXT | REG_ZEXT)) == REG_EXT) ||
685 ((flags_rs & (REG_EXT | REG_ZEXT)) == REG_EXT))
688 lightrec_set_reg_out_flags(reg_cache, rd, flags_rd);
695 lightrec_free_reg(reg_cache, rs);
696 lightrec_free_reg(reg_cache, rt);
697 lightrec_free_reg(reg_cache, rd);
700 static void rec_special_OR(struct lightrec_cstate *state,
701 const struct block *block, u16 offset)
703 _jit_name(block->_jit, __func__);
704 rec_special_or_nor(state, block, offset, false);
707 static void rec_special_NOR(struct lightrec_cstate *state,
708 const struct block *block, u16 offset)
710 _jit_name(block->_jit, __func__);
711 rec_special_or_nor(state, block, offset, true);
714 static void rec_special_XOR(struct lightrec_cstate *state,
715 const struct block *block, u16 offset)
717 struct regcache *reg_cache = state->reg_cache;
718 union code c = block->opcode_list[offset].c;
719 jit_state_t *_jit = block->_jit;
720 u8 rd, rt, rs, flags_rs, flags_rt, flags_rd;
722 _jit_name(block->_jit, __func__);
724 jit_note(__FILE__, __LINE__);
725 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0);
726 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0);
727 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, 0);
729 flags_rs = lightrec_get_reg_in_flags(reg_cache, rs);
730 flags_rt = lightrec_get_reg_in_flags(reg_cache, rt);
732 /* Z(rd) = Z(rs) & Z(rt) */
733 flags_rd = REG_ZEXT & flags_rs & flags_rt;
735 /* E(rd) = E(rs) & E(rt) */
736 flags_rd |= REG_EXT & flags_rs & flags_rt;
738 lightrec_set_reg_out_flags(reg_cache, rd, flags_rd);
740 jit_xorr(rd, rs, rt);
742 lightrec_free_reg(reg_cache, rs);
743 lightrec_free_reg(reg_cache, rt);
744 lightrec_free_reg(reg_cache, rd);
747 static void rec_special_SLTU(struct lightrec_cstate *state,
748 const struct block *block, u16 offset)
750 _jit_name(block->_jit, __func__);
751 rec_alu_special(state, block, offset, jit_code_ltr_u, true);
754 static void rec_special_SLT(struct lightrec_cstate *state,
755 const struct block *block, u16 offset)
757 _jit_name(block->_jit, __func__);
758 rec_alu_special(state, block, offset, jit_code_ltr, true);
761 static void rec_special_SLLV(struct lightrec_cstate *state,
762 const struct block *block, u16 offset)
764 _jit_name(block->_jit, __func__);
765 rec_alu_shiftv(state, block, offset, jit_code_lshr);
768 static void rec_special_SRLV(struct lightrec_cstate *state,
769 const struct block *block, u16 offset)
771 _jit_name(block->_jit, __func__);
772 rec_alu_shiftv(state, block, offset, jit_code_rshr_u);
775 static void rec_special_SRAV(struct lightrec_cstate *state,
776 const struct block *block, u16 offset)
778 _jit_name(block->_jit, __func__);
779 rec_alu_shiftv(state, block, offset, jit_code_rshr);
782 static void rec_alu_shift(struct lightrec_cstate *state, const struct block *block,
783 u16 offset, jit_code_t code)
785 struct regcache *reg_cache = state->reg_cache;
786 union code c = block->opcode_list[offset].c;
787 jit_state_t *_jit = block->_jit;
788 u8 rd, rt, flags = 0;
790 jit_note(__FILE__, __LINE__);
792 if (code == jit_code_rshi)
794 else if (code == jit_code_rshi_u)
797 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, flags);
799 /* Input reg is zero-extended, if we SRL at least by one bit, we know
800 * the output reg will be both zero-extended and sign-extended. */
801 if (code == jit_code_rshi_u && c.r.imm)
803 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, flags);
805 jit_new_node_www(code, rd, rt, c.r.imm);
807 lightrec_free_reg(reg_cache, rt);
808 lightrec_free_reg(reg_cache, rd);
811 static void rec_special_SLL(struct lightrec_cstate *state,
812 const struct block *block, u16 offset)
814 _jit_name(block->_jit, __func__);
815 rec_alu_shift(state, block, offset, jit_code_lshi);
818 static void rec_special_SRL(struct lightrec_cstate *state,
819 const struct block *block, u16 offset)
821 _jit_name(block->_jit, __func__);
822 rec_alu_shift(state, block, offset, jit_code_rshi_u);
825 static void rec_special_SRA(struct lightrec_cstate *state,
826 const struct block *block, u16 offset)
828 _jit_name(block->_jit, __func__);
829 rec_alu_shift(state, block, offset, jit_code_rshi);
832 static void rec_alu_mult(struct lightrec_cstate *state,
833 const struct block *block, u16 offset, bool is_signed)
835 struct regcache *reg_cache = state->reg_cache;
836 union code c = block->opcode_list[offset].c;
837 u32 flags = block->opcode_list[offset].flags;
838 u8 reg_lo = get_mult_div_lo(c);
839 u8 reg_hi = get_mult_div_hi(c);
840 jit_state_t *_jit = block->_jit;
841 u8 lo, hi, rs, rt, rflags = 0;
843 jit_note(__FILE__, __LINE__);
850 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, rflags);
851 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, rflags);
853 if (!op_flag_no_lo(flags))
854 lo = lightrec_alloc_reg_out(reg_cache, _jit, reg_lo, 0);
855 else if (__WORDSIZE == 32)
856 lo = lightrec_alloc_reg_temp(reg_cache, _jit);
858 if (!op_flag_no_hi(flags))
859 hi = lightrec_alloc_reg_out(reg_cache, _jit, reg_hi, REG_EXT);
861 if (__WORDSIZE == 32) {
862 /* On 32-bit systems, do a 32*32->64 bit operation, or a 32*32->32 bit
863 * operation if the MULT was detected a 32-bit only. */
864 if (!op_flag_no_hi(flags)) {
866 jit_qmulr(lo, hi, rs, rt);
868 jit_qmulr_u(lo, hi, rs, rt);
870 jit_mulr(lo, rs, rt);
873 /* On 64-bit systems, do a 64*64->64 bit operation. */
874 if (op_flag_no_lo(flags)) {
875 jit_mulr(hi, rs, rt);
876 jit_rshi(hi, hi, 32);
878 jit_mulr(lo, rs, rt);
880 /* The 64-bit output value is in $lo, store the upper 32 bits in $hi */
881 if (!op_flag_no_hi(flags))
882 jit_rshi(hi, lo, 32);
886 lightrec_free_reg(reg_cache, rs);
887 lightrec_free_reg(reg_cache, rt);
888 if (!op_flag_no_lo(flags) || __WORDSIZE == 32)
889 lightrec_free_reg(reg_cache, lo);
890 if (!op_flag_no_hi(flags))
891 lightrec_free_reg(reg_cache, hi);
894 static void rec_alu_div(struct lightrec_cstate *state,
895 const struct block *block, u16 offset, bool is_signed)
897 struct regcache *reg_cache = state->reg_cache;
898 union code c = block->opcode_list[offset].c;
899 u32 flags = block->opcode_list[offset].flags;
900 bool no_check = op_flag_no_div_check(flags);
901 u8 reg_lo = get_mult_div_lo(c);
902 u8 reg_hi = get_mult_div_hi(c);
903 jit_state_t *_jit = block->_jit;
904 jit_node_t *branch, *to_end;
905 u8 lo = 0, hi = 0, rs, rt, rflags = 0;
907 jit_note(__FILE__, __LINE__);
914 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, rflags);
915 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, rflags);
917 if (!op_flag_no_lo(flags))
918 lo = lightrec_alloc_reg_out(reg_cache, _jit, reg_lo, 0);
920 if (!op_flag_no_hi(flags))
921 hi = lightrec_alloc_reg_out(reg_cache, _jit, reg_hi, 0);
923 /* Jump to special handler if dividing by zero */
925 branch = jit_beqi(rt, 0);
927 if (op_flag_no_lo(flags)) {
929 jit_remr(hi, rs, rt);
931 jit_remr_u(hi, rs, rt);
932 } else if (op_flag_no_hi(flags)) {
934 jit_divr(lo, rs, rt);
936 jit_divr_u(lo, rs, rt);
939 jit_qdivr(lo, hi, rs, rt);
941 jit_qdivr_u(lo, hi, rs, rt);
945 /* Jump above the div-by-zero handler */
950 if (!op_flag_no_lo(flags)) {
956 jit_movi(lo, 0xffffffff);
960 if (!op_flag_no_hi(flags))
966 lightrec_free_reg(reg_cache, rs);
967 lightrec_free_reg(reg_cache, rt);
969 if (!op_flag_no_lo(flags))
970 lightrec_free_reg(reg_cache, lo);
972 if (!op_flag_no_hi(flags))
973 lightrec_free_reg(reg_cache, hi);
976 static void rec_special_MULT(struct lightrec_cstate *state,
977 const struct block *block, u16 offset)
979 _jit_name(block->_jit, __func__);
980 rec_alu_mult(state, block, offset, true);
983 static void rec_special_MULTU(struct lightrec_cstate *state,
984 const struct block *block, u16 offset)
986 _jit_name(block->_jit, __func__);
987 rec_alu_mult(state, block, offset, false);
990 static void rec_special_DIV(struct lightrec_cstate *state,
991 const struct block *block, u16 offset)
993 _jit_name(block->_jit, __func__);
994 rec_alu_div(state, block, offset, true);
997 static void rec_special_DIVU(struct lightrec_cstate *state,
998 const struct block *block, u16 offset)
1000 _jit_name(block->_jit, __func__);
1001 rec_alu_div(state, block, offset, false);
1004 static void rec_alu_mv_lo_hi(struct lightrec_cstate *state,
1005 const struct block *block, u8 dst, u8 src)
1007 struct regcache *reg_cache = state->reg_cache;
1008 jit_state_t *_jit = block->_jit;
1010 jit_note(__FILE__, __LINE__);
1011 src = lightrec_alloc_reg_in(reg_cache, _jit, src, 0);
1012 dst = lightrec_alloc_reg_out(reg_cache, _jit, dst, REG_EXT);
1014 jit_extr_i(dst, src);
1016 lightrec_free_reg(reg_cache, src);
1017 lightrec_free_reg(reg_cache, dst);
1020 static void rec_special_MFHI(struct lightrec_cstate *state,
1021 const struct block *block, u16 offset)
1023 union code c = block->opcode_list[offset].c;
1025 _jit_name(block->_jit, __func__);
1026 rec_alu_mv_lo_hi(state, block, c.r.rd, REG_HI);
1029 static void rec_special_MTHI(struct lightrec_cstate *state,
1030 const struct block *block, u16 offset)
1032 union code c = block->opcode_list[offset].c;
1034 _jit_name(block->_jit, __func__);
1035 rec_alu_mv_lo_hi(state, block, REG_HI, c.r.rs);
1038 static void rec_special_MFLO(struct lightrec_cstate *state,
1039 const struct block *block, u16 offset)
1041 union code c = block->opcode_list[offset].c;
1043 _jit_name(block->_jit, __func__);
1044 rec_alu_mv_lo_hi(state, block, c.r.rd, REG_LO);
1047 static void rec_special_MTLO(struct lightrec_cstate *state,
1048 const struct block *block, u16 offset)
1050 union code c = block->opcode_list[offset].c;
1052 _jit_name(block->_jit, __func__);
1053 rec_alu_mv_lo_hi(state, block, REG_LO, c.r.rs);
1056 static void call_to_c_wrapper(struct lightrec_cstate *state,
1057 const struct block *block, u32 arg,
1058 enum c_wrappers wrapper)
1060 struct regcache *reg_cache = state->reg_cache;
1061 jit_state_t *_jit = block->_jit;
1064 /* Make sure JIT_R1 is not mapped; it will be used in the C wrapper. */
1065 tmp2 = lightrec_alloc_reg(reg_cache, _jit, JIT_R1);
1067 tmp = lightrec_get_reg_with_value(reg_cache,
1068 (intptr_t) state->state->wrappers_eps[wrapper]);
1070 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1071 jit_ldxi(tmp, LIGHTREC_REG_STATE,
1072 offsetof(struct lightrec_state, wrappers_eps[wrapper]));
1074 lightrec_temp_set_value(reg_cache, tmp,
1075 (intptr_t) state->state->wrappers_eps[wrapper]);
1078 lightrec_free_reg(reg_cache, tmp2);
1081 /* On MIPS, register t9 is always used as the target register for JALR.
1082 * Therefore if it does not contain the target address we must
1085 lightrec_unload_reg(reg_cache, _jit, _T9);
1091 lightrec_regcache_mark_live(reg_cache, _jit);
1094 lightrec_free_reg(reg_cache, tmp);
1095 lightrec_regcache_mark_live(reg_cache, _jit);
1098 static void rec_io(struct lightrec_cstate *state,
1099 const struct block *block, u16 offset,
1100 bool load_rt, bool read_rt)
1102 struct regcache *reg_cache = state->reg_cache;
1103 jit_state_t *_jit = block->_jit;
1104 union code c = block->opcode_list[offset].c;
1105 u32 flags = block->opcode_list[offset].flags;
1106 bool is_tagged = LIGHTREC_FLAGS_GET_IO_MODE(flags);
1109 jit_note(__FILE__, __LINE__);
1111 lightrec_clean_reg_if_loaded(reg_cache, _jit, c.i.rs, false);
1113 if (read_rt && likely(c.i.rt))
1114 lightrec_clean_reg_if_loaded(reg_cache, _jit, c.i.rt, true);
1116 lightrec_clean_reg_if_loaded(reg_cache, _jit, c.i.rt, false);
1119 call_to_c_wrapper(state, block, c.opcode, C_WRAPPER_RW);
1121 lut_entry = lightrec_get_lut_entry(block);
1122 call_to_c_wrapper(state, block, (lut_entry << 16) | offset,
1123 C_WRAPPER_RW_GENERIC);
1127 static u32 rec_ram_mask(struct lightrec_state *state)
1129 return (RAM_SIZE << (state->mirrors_mapped * 2)) - 1;
1132 static void rec_store_memory(struct lightrec_cstate *cstate,
1133 const struct block *block,
1134 u16 offset, jit_code_t code,
1135 jit_code_t swap_code,
1136 uintptr_t addr_offset, u32 addr_mask,
1139 const struct lightrec_state *state = cstate->state;
1140 struct regcache *reg_cache = cstate->reg_cache;
1141 struct opcode *op = &block->opcode_list[offset];
1142 jit_state_t *_jit = block->_jit;
1143 union code c = op->c;
1144 u8 rs, rt, tmp, tmp2, tmp3, addr_reg, addr_reg2;
1145 s16 imm = (s16)c.i.imm;
1146 s32 simm = (s32)imm << (1 - lut_is_32bit(state));
1147 s32 lut_offt = offsetof(struct lightrec_state, code_lut);
1148 bool no_mask = op_flag_no_mask(op->flags);
1149 bool add_imm = c.i.imm &&
1150 ((!state->mirrors_mapped && !no_mask) || (invalidate &&
1151 ((imm & 0x3) || simm + lut_offt != (s16)(simm + lut_offt))));
1152 bool need_tmp = !no_mask || addr_offset || add_imm || invalidate;
1154 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0);
1155 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
1157 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1162 jit_addi(tmp, addr_reg, (s16)c.i.imm);
1170 jit_andi(tmp, addr_reg, addr_mask);
1175 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
1176 jit_addi(tmp2, addr_reg, addr_offset);
1179 addr_reg2 = addr_reg;
1182 if (is_big_endian() && swap_code && c.i.rt) {
1183 tmp3 = lightrec_alloc_reg_temp(reg_cache, _jit);
1185 jit_new_node_ww(swap_code, tmp3, rt);
1186 jit_new_node_www(code, imm, addr_reg2, tmp3);
1188 lightrec_free_reg(reg_cache, tmp3);
1190 jit_new_node_www(code, imm, addr_reg2, rt);
1193 lightrec_free_reg(reg_cache, rt);
1196 tmp3 = lightrec_alloc_reg_in(reg_cache, _jit, 0, 0);
1198 if (c.i.op != OP_SW) {
1199 jit_andi(tmp, addr_reg, ~3);
1203 if (!lut_is_32bit(state)) {
1204 jit_lshi(tmp, addr_reg, 1);
1208 if (addr_reg == rs && c.i.rs == 0) {
1209 addr_reg = LIGHTREC_REG_STATE;
1211 jit_addr(tmp, addr_reg, LIGHTREC_REG_STATE);
1215 if (lut_is_32bit(state))
1216 jit_stxi_i(lut_offt, addr_reg, tmp3);
1218 jit_stxi(lut_offt, addr_reg, tmp3);
1220 lightrec_free_reg(reg_cache, tmp3);
1224 lightrec_free_reg(reg_cache, tmp2);
1226 lightrec_free_reg(reg_cache, tmp);
1227 lightrec_free_reg(reg_cache, rs);
1230 static void rec_store_ram(struct lightrec_cstate *cstate,
1231 const struct block *block,
1232 u16 offset, jit_code_t code,
1233 jit_code_t swap_code, bool invalidate)
1235 struct lightrec_state *state = cstate->state;
1237 _jit_note(block->_jit, __FILE__, __LINE__);
1239 return rec_store_memory(cstate, block, offset, code, swap_code,
1240 state->offset_ram, rec_ram_mask(state),
1244 static void rec_store_scratch(struct lightrec_cstate *cstate,
1245 const struct block *block, u16 offset,
1246 jit_code_t code, jit_code_t swap_code)
1248 _jit_note(block->_jit, __FILE__, __LINE__);
1250 return rec_store_memory(cstate, block, offset, code, swap_code,
1251 cstate->state->offset_scratch,
1255 static void rec_store_io(struct lightrec_cstate *cstate,
1256 const struct block *block, u16 offset,
1257 jit_code_t code, jit_code_t swap_code)
1259 _jit_note(block->_jit, __FILE__, __LINE__);
1261 return rec_store_memory(cstate, block, offset, code, swap_code,
1262 cstate->state->offset_io,
1266 static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate,
1267 const struct block *block,
1268 u16 offset, jit_code_t code,
1269 jit_code_t swap_code)
1271 struct lightrec_state *state = cstate->state;
1272 struct regcache *reg_cache = cstate->reg_cache;
1273 union code c = block->opcode_list[offset].c;
1274 jit_state_t *_jit = block->_jit;
1275 jit_node_t *to_not_ram, *to_end;
1276 u8 tmp, tmp2, rs, rt;
1279 jit_note(__FILE__, __LINE__);
1280 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
1281 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1283 if (state->offset_ram || state->offset_scratch)
1284 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
1286 /* Convert to KUNSEG and avoid RAM mirrors */
1287 if (state->mirrors_mapped) {
1289 jit_andi(tmp, rs, 0x1f800000 | (4 * RAM_SIZE - 1));
1290 } else if (c.i.imm) {
1292 jit_addi(tmp, rs, (s16)c.i.imm);
1293 jit_andi(tmp, tmp, 0x1f800000 | (RAM_SIZE - 1));
1296 jit_andi(tmp, rs, 0x1f800000 | (RAM_SIZE - 1));
1299 lightrec_free_reg(reg_cache, rs);
1301 if (state->offset_ram != state->offset_scratch) {
1302 to_not_ram = jit_bmsi(tmp, BIT(28));
1304 jit_movi(tmp2, state->offset_ram);
1307 jit_patch(to_not_ram);
1309 jit_movi(tmp2, state->offset_scratch);
1311 } else if (state->offset_ram) {
1312 jit_movi(tmp2, state->offset_ram);
1315 if (state->offset_ram || state->offset_scratch) {
1316 jit_addr(tmp, tmp, tmp2);
1317 lightrec_free_reg(reg_cache, tmp2);
1320 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0);
1322 if (is_big_endian() && swap_code && c.i.rt) {
1323 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
1325 jit_new_node_ww(swap_code, tmp2, rt);
1326 jit_new_node_www(code, imm, tmp, tmp2);
1328 lightrec_free_reg(reg_cache, tmp2);
1330 jit_new_node_www(code, imm, tmp, rt);
1333 lightrec_free_reg(reg_cache, rt);
1334 lightrec_free_reg(reg_cache, tmp);
1337 static void rec_store_direct(struct lightrec_cstate *cstate, const struct block *block,
1338 u16 offset, jit_code_t code, jit_code_t swap_code)
1340 struct lightrec_state *state = cstate->state;
1341 u32 ram_size = state->mirrors_mapped ? RAM_SIZE * 4 : RAM_SIZE;
1342 struct regcache *reg_cache = cstate->reg_cache;
1343 union code c = block->opcode_list[offset].c;
1344 jit_state_t *_jit = block->_jit;
1345 jit_node_t *to_not_ram, *to_end;
1346 u8 tmp, tmp2, tmp3, rs, rt;
1348 jit_note(__FILE__, __LINE__);
1350 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
1351 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
1352 tmp3 = lightrec_alloc_reg_in(reg_cache, _jit, 0, 0);
1354 /* Convert to KUNSEG and avoid RAM mirrors */
1356 jit_addi(tmp2, rs, (s16)c.i.imm);
1357 jit_andi(tmp2, tmp2, 0x1f800000 | (ram_size - 1));
1359 jit_andi(tmp2, rs, 0x1f800000 | (ram_size - 1));
1362 lightrec_free_reg(reg_cache, rs);
1363 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1365 to_not_ram = jit_bgti(tmp2, ram_size);
1367 /* Compute the offset to the code LUT */
1368 jit_andi(tmp, tmp2, (RAM_SIZE - 1) & ~3);
1369 if (!lut_is_32bit(state))
1370 jit_lshi(tmp, tmp, 1);
1371 jit_addr(tmp, LIGHTREC_REG_STATE, tmp);
1373 /* Write NULL to the code LUT to invalidate any block that's there */
1374 if (lut_is_32bit(state))
1375 jit_stxi_i(offsetof(struct lightrec_state, code_lut), tmp, tmp3);
1377 jit_stxi(offsetof(struct lightrec_state, code_lut), tmp, tmp3);
1379 if (state->offset_ram != state->offset_scratch) {
1380 jit_movi(tmp, state->offset_ram);
1385 jit_patch(to_not_ram);
1387 if (state->offset_ram || state->offset_scratch)
1388 jit_movi(tmp, state->offset_scratch);
1390 if (state->offset_ram != state->offset_scratch)
1393 if (state->offset_ram || state->offset_scratch)
1394 jit_addr(tmp2, tmp2, tmp);
1396 lightrec_free_reg(reg_cache, tmp);
1397 lightrec_free_reg(reg_cache, tmp3);
1399 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0);
1401 if (is_big_endian() && swap_code && c.i.rt) {
1402 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1404 jit_new_node_ww(swap_code, tmp, rt);
1405 jit_new_node_www(code, 0, tmp2, tmp);
1407 lightrec_free_reg(reg_cache, tmp);
1409 jit_new_node_www(code, 0, tmp2, rt);
1412 lightrec_free_reg(reg_cache, rt);
1413 lightrec_free_reg(reg_cache, tmp2);
1416 static void rec_store(struct lightrec_cstate *state,
1417 const struct block *block, u16 offset,
1418 jit_code_t code, jit_code_t swap_code)
1420 u32 flags = block->opcode_list[offset].flags;
1421 bool no_invalidate = op_flag_no_invalidate(flags) ||
1422 state->state->invalidate_from_dma_only;
1424 switch (LIGHTREC_FLAGS_GET_IO_MODE(flags)) {
1425 case LIGHTREC_IO_RAM:
1426 rec_store_ram(state, block, offset, code,
1427 swap_code, !no_invalidate);
1429 case LIGHTREC_IO_SCRATCH:
1430 rec_store_scratch(state, block, offset, code, swap_code);
1432 case LIGHTREC_IO_DIRECT:
1433 if (no_invalidate) {
1434 rec_store_direct_no_invalidate(state, block, offset,
1437 rec_store_direct(state, block, offset, code, swap_code);
1440 case LIGHTREC_IO_DIRECT_HW:
1441 rec_store_io(state, block, offset, code, swap_code);
1444 rec_io(state, block, offset, true, false);
1449 static void rec_SB(struct lightrec_cstate *state,
1450 const struct block *block, u16 offset)
1452 _jit_name(block->_jit, __func__);
1453 rec_store(state, block, offset, jit_code_stxi_c, 0);
1456 static void rec_SH(struct lightrec_cstate *state,
1457 const struct block *block, u16 offset)
1459 _jit_name(block->_jit, __func__);
1460 rec_store(state, block, offset,
1461 jit_code_stxi_s, jit_code_bswapr_us);
1464 static void rec_SW(struct lightrec_cstate *state,
1465 const struct block *block, u16 offset)
1468 _jit_name(block->_jit, __func__);
1469 rec_store(state, block, offset,
1470 jit_code_stxi_i, jit_code_bswapr_ui);
1473 static void rec_SWL(struct lightrec_cstate *state,
1474 const struct block *block, u16 offset)
1476 _jit_name(block->_jit, __func__);
1477 rec_io(state, block, offset, true, false);
1480 static void rec_SWR(struct lightrec_cstate *state,
1481 const struct block *block, u16 offset)
1483 _jit_name(block->_jit, __func__);
1484 rec_io(state, block, offset, true, false);
1487 static void rec_SWC2(struct lightrec_cstate *state,
1488 const struct block *block, u16 offset)
1490 _jit_name(block->_jit, __func__);
1491 rec_io(state, block, offset, false, false);
1494 static void rec_load_memory(struct lightrec_cstate *cstate,
1495 const struct block *block, u16 offset,
1496 jit_code_t code, jit_code_t swap_code, bool is_unsigned,
1497 uintptr_t addr_offset, u32 addr_mask)
1499 struct regcache *reg_cache = cstate->reg_cache;
1500 struct opcode *op = &block->opcode_list[offset];
1501 jit_state_t *_jit = block->_jit;
1502 u8 rs, rt, addr_reg, flags = REG_EXT;
1503 bool no_mask = op_flag_no_mask(op->flags);
1504 union code c = op->c;
1513 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
1514 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, flags);
1516 if (!cstate->state->mirrors_mapped && c.i.imm && !no_mask) {
1517 jit_addi(rt, rs, (s16)c.i.imm);
1526 jit_andi(rt, addr_reg, addr_mask);
1531 jit_addi(rt, addr_reg, addr_offset);
1535 jit_new_node_www(code, rt, addr_reg, imm);
1537 if (is_big_endian() && swap_code) {
1538 jit_new_node_ww(swap_code, rt, rt);
1540 if (c.i.op == OP_LH)
1542 else if (c.i.op == OP_LW && __WORDSIZE == 64)
1546 lightrec_free_reg(reg_cache, rs);
1547 lightrec_free_reg(reg_cache, rt);
1550 static void rec_load_ram(struct lightrec_cstate *cstate,
1551 const struct block *block, u16 offset,
1552 jit_code_t code, jit_code_t swap_code, bool is_unsigned)
1554 _jit_note(block->_jit, __FILE__, __LINE__);
1556 rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
1557 cstate->state->offset_ram, rec_ram_mask(cstate->state));
1560 static void rec_load_bios(struct lightrec_cstate *cstate,
1561 const struct block *block, u16 offset,
1562 jit_code_t code, jit_code_t swap_code, bool is_unsigned)
1564 _jit_note(block->_jit, __FILE__, __LINE__);
1566 rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
1567 cstate->state->offset_bios, 0x1fffffff);
1570 static void rec_load_scratch(struct lightrec_cstate *cstate,
1571 const struct block *block, u16 offset,
1572 jit_code_t code, jit_code_t swap_code, bool is_unsigned)
1574 _jit_note(block->_jit, __FILE__, __LINE__);
1576 rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
1577 cstate->state->offset_scratch, 0x1fffffff);
1580 static void rec_load_io(struct lightrec_cstate *cstate,
1581 const struct block *block, u16 offset,
1582 jit_code_t code, jit_code_t swap_code, bool is_unsigned)
1584 _jit_note(block->_jit, __FILE__, __LINE__);
1586 rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
1587 cstate->state->offset_io, 0x1fffffff);
1590 static void rec_load_direct(struct lightrec_cstate *cstate,
1591 const struct block *block, u16 offset,
1592 jit_code_t code, jit_code_t swap_code,
1595 struct lightrec_state *state = cstate->state;
1596 struct regcache *reg_cache = cstate->reg_cache;
1597 union code c = block->opcode_list[offset].c;
1598 jit_state_t *_jit = block->_jit;
1599 jit_node_t *to_not_ram, *to_not_bios, *to_end, *to_end2;
1600 u8 tmp, rs, rt, addr_reg, flags = REG_EXT;
1609 jit_note(__FILE__, __LINE__);
1610 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
1611 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, flags);
1613 if ((state->offset_ram == state->offset_bios &&
1614 state->offset_ram == state->offset_scratch &&
1615 state->mirrors_mapped) || !c.i.imm) {
1619 jit_addi(rt, rs, (s16)c.i.imm);
1623 if (c.i.rs != c.i.rt)
1624 lightrec_free_reg(reg_cache, rs);
1627 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1629 if (state->offset_ram == state->offset_bios &&
1630 state->offset_ram == state->offset_scratch) {
1631 if (!state->mirrors_mapped) {
1632 jit_andi(tmp, addr_reg, BIT(28));
1633 jit_rshi_u(tmp, tmp, 28 - 22);
1634 jit_ori(tmp, tmp, 0x1f800000 | (RAM_SIZE - 1));
1635 jit_andr(rt, addr_reg, tmp);
1637 jit_andi(rt, addr_reg, 0x1fffffff);
1640 if (state->offset_ram)
1641 jit_movi(tmp, state->offset_ram);
1643 to_not_ram = jit_bmsi(addr_reg, BIT(28));
1645 /* Convert to KUNSEG and avoid RAM mirrors */
1646 jit_andi(rt, addr_reg, RAM_SIZE - 1);
1648 if (state->offset_ram)
1649 jit_movi(tmp, state->offset_ram);
1653 jit_patch(to_not_ram);
1655 if (state->offset_bios != state->offset_scratch)
1656 to_not_bios = jit_bmci(addr_reg, BIT(22));
1658 /* Convert to KUNSEG */
1659 jit_andi(rt, addr_reg, 0x1fc00000 | (BIOS_SIZE - 1));
1661 jit_movi(tmp, state->offset_bios);
1663 if (state->offset_bios != state->offset_scratch) {
1666 jit_patch(to_not_bios);
1668 /* Convert to KUNSEG */
1669 jit_andi(rt, addr_reg, 0x1f800fff);
1671 if (state->offset_scratch)
1672 jit_movi(tmp, state->offset_scratch);
1680 if (state->offset_ram || state->offset_bios || state->offset_scratch)
1681 jit_addr(rt, rt, tmp);
1683 jit_new_node_www(code, rt, rt, imm);
1685 if (is_big_endian() && swap_code) {
1686 jit_new_node_ww(swap_code, rt, rt);
1688 if (c.i.op == OP_LH)
1690 else if (c.i.op == OP_LW && __WORDSIZE == 64)
1694 lightrec_free_reg(reg_cache, addr_reg);
1695 lightrec_free_reg(reg_cache, rt);
1696 lightrec_free_reg(reg_cache, tmp);
1699 static void rec_load(struct lightrec_cstate *state, const struct block *block,
1700 u16 offset, jit_code_t code, jit_code_t swap_code,
1703 u32 flags = block->opcode_list[offset].flags;
1705 switch (LIGHTREC_FLAGS_GET_IO_MODE(flags)) {
1706 case LIGHTREC_IO_RAM:
1707 rec_load_ram(state, block, offset, code, swap_code, is_unsigned);
1709 case LIGHTREC_IO_BIOS:
1710 rec_load_bios(state, block, offset, code, swap_code, is_unsigned);
1712 case LIGHTREC_IO_SCRATCH:
1713 rec_load_scratch(state, block, offset, code, swap_code, is_unsigned);
1715 case LIGHTREC_IO_DIRECT_HW:
1716 rec_load_io(state, block, offset, code, swap_code, is_unsigned);
1718 case LIGHTREC_IO_DIRECT:
1719 rec_load_direct(state, block, offset, code, swap_code, is_unsigned);
1722 rec_io(state, block, offset, false, true);
1727 static void rec_LB(struct lightrec_cstate *state, const struct block *block, u16 offset)
1729 _jit_name(block->_jit, __func__);
1730 rec_load(state, block, offset, jit_code_ldxi_c, 0, false);
1733 static void rec_LBU(struct lightrec_cstate *state, const struct block *block, u16 offset)
1735 _jit_name(block->_jit, __func__);
1736 rec_load(state, block, offset, jit_code_ldxi_uc, 0, true);
1739 static void rec_LH(struct lightrec_cstate *state, const struct block *block, u16 offset)
1741 jit_code_t code = is_big_endian() ? jit_code_ldxi_us : jit_code_ldxi_s;
1743 _jit_name(block->_jit, __func__);
1744 rec_load(state, block, offset, code, jit_code_bswapr_us, false);
1747 static void rec_LHU(struct lightrec_cstate *state, const struct block *block, u16 offset)
1749 _jit_name(block->_jit, __func__);
1750 rec_load(state, block, offset, jit_code_ldxi_us, jit_code_bswapr_us, true);
1753 static void rec_LWL(struct lightrec_cstate *state, const struct block *block, u16 offset)
1755 _jit_name(block->_jit, __func__);
1756 rec_io(state, block, offset, true, true);
1759 static void rec_LWR(struct lightrec_cstate *state, const struct block *block, u16 offset)
1761 _jit_name(block->_jit, __func__);
1762 rec_io(state, block, offset, true, true);
1765 static void rec_LW(struct lightrec_cstate *state, const struct block *block, u16 offset)
1769 if (is_big_endian() && __WORDSIZE == 64)
1770 code = jit_code_ldxi_ui;
1772 code = jit_code_ldxi_i;
1774 _jit_name(block->_jit, __func__);
1775 rec_load(state, block, offset, code, jit_code_bswapr_ui, false);
1778 static void rec_LWC2(struct lightrec_cstate *state, const struct block *block, u16 offset)
1780 _jit_name(block->_jit, __func__);
1781 rec_io(state, block, offset, false, false);
1784 static void rec_break_syscall(struct lightrec_cstate *state,
1785 const struct block *block, u16 offset,
1788 struct regcache *reg_cache = state->reg_cache;
1789 jit_state_t *_jit = block->_jit;
1792 _jit_note(block->_jit, __FILE__, __LINE__);
1794 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1796 jit_movi(tmp, exit_code);
1797 jit_stxi_i(offsetof(struct lightrec_state, exit_flags),
1798 LIGHTREC_REG_STATE, tmp);
1800 lightrec_free_reg(reg_cache, tmp);
1802 /* TODO: the return address should be "pc - 4" if we're a delay slot */
1803 lightrec_emit_end_of_block(state, block, offset, -1,
1804 get_ds_pc(block, offset, 0),
1808 static void rec_special_SYSCALL(struct lightrec_cstate *state,
1809 const struct block *block, u16 offset)
1811 _jit_name(block->_jit, __func__);
1812 rec_break_syscall(state, block, offset, LIGHTREC_EXIT_SYSCALL);
1815 static void rec_special_BREAK(struct lightrec_cstate *state,
1816 const struct block *block, u16 offset)
1818 _jit_name(block->_jit, __func__);
1819 rec_break_syscall(state, block, offset, LIGHTREC_EXIT_BREAK);
1822 static void rec_mtc(struct lightrec_cstate *state, const struct block *block, u16 offset)
1824 struct regcache *reg_cache = state->reg_cache;
1825 union code c = block->opcode_list[offset].c;
1826 jit_state_t *_jit = block->_jit;
1828 jit_note(__FILE__, __LINE__);
1829 lightrec_clean_reg_if_loaded(reg_cache, _jit, c.i.rs, false);
1830 lightrec_clean_reg_if_loaded(reg_cache, _jit, c.i.rt, false);
1832 call_to_c_wrapper(state, block, c.opcode, C_WRAPPER_MTC);
1834 if (c.i.op == OP_CP0 &&
1835 !op_flag_no_ds(block->opcode_list[offset].flags) &&
1836 (c.r.rd == 12 || c.r.rd == 13))
1837 lightrec_emit_end_of_block(state, block, offset, -1,
1838 get_ds_pc(block, offset, 1),
1843 rec_mfc0(struct lightrec_cstate *state, const struct block *block, u16 offset)
1845 struct regcache *reg_cache = state->reg_cache;
1846 union code c = block->opcode_list[offset].c;
1847 jit_state_t *_jit = block->_jit;
1850 jit_note(__FILE__, __LINE__);
1852 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, REG_EXT);
1854 jit_ldxi_i(rt, LIGHTREC_REG_STATE,
1855 offsetof(struct lightrec_state, regs.cp0[c.r.rd]));
1857 lightrec_free_reg(reg_cache, rt);
1860 static bool block_in_bios(const struct lightrec_cstate *state,
1861 const struct block *block)
1863 const struct lightrec_mem_map *bios = &state->state->maps[PSX_MAP_BIOS];
1864 u32 pc = kunseg(block->pc);
1866 return pc >= bios->pc && pc < bios->pc + bios->length;
1870 rec_mtc0(struct lightrec_cstate *state, const struct block *block, u16 offset)
1872 struct regcache *reg_cache = state->reg_cache;
1873 const union code c = block->opcode_list[offset].c;
1874 jit_state_t *_jit = block->_jit;
1875 u8 rt, tmp = 0, tmp2, status;
1877 jit_note(__FILE__, __LINE__);
1885 /* Those registers are read-only */
1891 if (block_in_bios(state, block) && c.r.rd == 12) {
1892 /* If we are running code from the BIOS, handle writes to the
1893 * Status register in C. BIOS code may toggle bit 16 which will
1894 * map/unmap the RAM, while game code cannot do that. */
1895 rec_mtc(state, block, offset);
1899 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0);
1902 jit_stxi_i(offsetof(struct lightrec_state, regs.cp0[c.r.rd]),
1903 LIGHTREC_REG_STATE, rt);
1906 if (c.r.rd == 12 || c.r.rd == 13) {
1907 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
1908 jit_ldxi_i(tmp, LIGHTREC_REG_STATE,
1909 offsetof(struct lightrec_state, regs.cp0[13]));
1911 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
1916 } else if (c.r.rd == 13) {
1917 /* Cause = (Cause & ~0x0300) | (value & 0x0300) */
1918 jit_andi(tmp2, rt, 0x0300);
1919 jit_ori(tmp, tmp, 0x0300);
1920 jit_xori(tmp, tmp, 0x0300);
1921 jit_orr(tmp, tmp, tmp2);
1922 jit_ldxi_i(tmp2, LIGHTREC_REG_STATE,
1923 offsetof(struct lightrec_state, regs.cp0[12]));
1924 jit_stxi_i(offsetof(struct lightrec_state, regs.cp0[13]),
1925 LIGHTREC_REG_STATE, tmp);
1929 if (c.r.rd == 12 || c.r.rd == 13) {
1930 /* Exit dynarec in case there's a software interrupt.
1931 * exit_flags = !!(status & tmp & 0x0300) & status; */
1932 jit_andr(tmp, tmp, status);
1933 jit_andi(tmp, tmp, 0x0300);
1934 jit_nei(tmp, tmp, 0);
1935 jit_andr(tmp, tmp, status);
1939 /* Exit dynarec in case we unmask a hardware interrupt.
1940 * exit_flags = !(~status & 0x401) */
1942 jit_comr(tmp2, status);
1943 jit_andi(tmp2, tmp2, 0x401);
1944 jit_eqi(tmp2, tmp2, 0);
1945 jit_orr(tmp, tmp, tmp2);
1948 if (c.r.rd == 12 || c.r.rd == 13) {
1949 jit_stxi_i(offsetof(struct lightrec_state, exit_flags),
1950 LIGHTREC_REG_STATE, tmp);
1952 lightrec_free_reg(reg_cache, tmp);
1953 lightrec_free_reg(reg_cache, tmp2);
1956 lightrec_free_reg(reg_cache, rt);
1958 if (!op_flag_no_ds(block->opcode_list[offset].flags) &&
1959 (c.r.rd == 12 || c.r.rd == 13))
1960 lightrec_emit_eob(state, block, offset + 1, true);
1963 static void rec_cp0_MFC0(struct lightrec_cstate *state,
1964 const struct block *block, u16 offset)
1966 _jit_name(block->_jit, __func__);
1967 rec_mfc0(state, block, offset);
1970 static void rec_cp0_CFC0(struct lightrec_cstate *state,
1971 const struct block *block, u16 offset)
1973 _jit_name(block->_jit, __func__);
1974 rec_mfc0(state, block, offset);
1977 static void rec_cp0_MTC0(struct lightrec_cstate *state,
1978 const struct block *block, u16 offset)
1980 _jit_name(block->_jit, __func__);
1981 rec_mtc0(state, block, offset);
1984 static void rec_cp0_CTC0(struct lightrec_cstate *state,
1985 const struct block *block, u16 offset)
1987 _jit_name(block->_jit, __func__);
1988 rec_mtc0(state, block, offset);
1991 static unsigned int cp2d_i_offset(u8 reg)
1993 return offsetof(struct lightrec_state, regs.cp2d[reg]);
1996 static unsigned int cp2d_s_offset(u8 reg)
1998 return cp2d_i_offset(reg) + is_big_endian() * 2;
2001 static unsigned int cp2c_i_offset(u8 reg)
2003 return offsetof(struct lightrec_state, regs.cp2c[reg]);
2006 static unsigned int cp2c_s_offset(u8 reg)
2008 return cp2c_i_offset(reg) + is_big_endian() * 2;
2011 static void rec_cp2_basic_MFC2(struct lightrec_cstate *state,
2012 const struct block *block, u16 offset)
2014 struct regcache *reg_cache = state->reg_cache;
2015 const union code c = block->opcode_list[offset].c;
2016 jit_state_t *_jit = block->_jit;
2017 const u32 zext_regs = 0x300f0080;
2018 u8 rt, tmp, tmp2, tmp3, out, flags;
2019 u8 reg = c.r.rd == 15 ? 14 : c.r.rd;
2022 _jit_name(block->_jit, __func__);
2024 flags = (zext_regs & BIT(reg)) ? REG_ZEXT : REG_EXT;
2025 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rt, flags);
2035 jit_ldxi_s(rt, LIGHTREC_REG_STATE, cp2d_s_offset(reg));
2042 jit_ldxi_us(rt, LIGHTREC_REG_STATE, cp2d_s_offset(reg));
2046 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
2047 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
2048 tmp3 = lightrec_alloc_reg_temp(reg_cache, _jit);
2050 for (i = 0; i < 3; i++) {
2051 out = i == 0 ? rt : tmp;
2053 jit_ldxi_s(tmp, LIGHTREC_REG_STATE, cp2d_s_offset(9 + i));
2054 jit_movi(tmp2, 0x1f);
2055 jit_rshi(out, tmp, 7);
2057 jit_ltr(tmp3, tmp2, out);
2058 jit_movnr(out, tmp2, tmp3);
2060 jit_gei(tmp2, out, 0);
2061 jit_movzr(out, tmp2, tmp2);
2064 jit_lshi(tmp, tmp, 5 * i);
2065 jit_orr(rt, rt, tmp);
2070 lightrec_free_reg(reg_cache, tmp);
2071 lightrec_free_reg(reg_cache, tmp2);
2072 lightrec_free_reg(reg_cache, tmp3);
2075 jit_ldxi_i(rt, LIGHTREC_REG_STATE, cp2d_i_offset(reg));
2079 lightrec_free_reg(reg_cache, rt);
2082 static void rec_cp2_basic_CFC2(struct lightrec_cstate *state,
2083 const struct block *block, u16 offset)
2085 struct regcache *reg_cache = state->reg_cache;
2086 const union code c = block->opcode_list[offset].c;
2087 jit_state_t *_jit = block->_jit;
2090 _jit_name(block->_jit, __func__);
2100 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rt, REG_EXT);
2101 jit_ldxi_s(rt, LIGHTREC_REG_STATE, cp2c_s_offset(c.r.rd));
2104 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rt, REG_ZEXT);
2105 jit_ldxi_i(rt, LIGHTREC_REG_STATE, cp2c_i_offset(c.r.rd));
2109 lightrec_free_reg(reg_cache, rt);
2112 static void rec_cp2_basic_MTC2(struct lightrec_cstate *state,
2113 const struct block *block, u16 offset)
2115 struct regcache *reg_cache = state->reg_cache;
2116 const union code c = block->opcode_list[offset].c;
2117 jit_state_t *_jit = block->_jit;
2118 jit_node_t *loop, *to_loop;
2119 u8 rt, tmp, tmp2, flags = 0;
2121 _jit_name(block->_jit, __func__);
2129 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, flags);
2133 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
2134 jit_ldxi_i(tmp, LIGHTREC_REG_STATE, cp2d_i_offset(13));
2136 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
2137 jit_ldxi_i(tmp2, LIGHTREC_REG_STATE, cp2d_i_offset(14));
2139 jit_stxi_i(cp2d_i_offset(12), LIGHTREC_REG_STATE, tmp);
2140 jit_stxi_i(cp2d_i_offset(13), LIGHTREC_REG_STATE, tmp2);
2141 jit_stxi_i(cp2d_i_offset(14), LIGHTREC_REG_STATE, rt);
2143 lightrec_free_reg(reg_cache, tmp);
2144 lightrec_free_reg(reg_cache, tmp2);
2147 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
2149 jit_lshi(tmp, rt, 7);
2150 jit_andi(tmp, tmp, 0xf80);
2151 jit_stxi_s(cp2d_s_offset(9), LIGHTREC_REG_STATE, tmp);
2153 jit_lshi(tmp, rt, 2);
2154 jit_andi(tmp, tmp, 0xf80);
2155 jit_stxi_s(cp2d_s_offset(10), LIGHTREC_REG_STATE, tmp);
2157 jit_rshi(tmp, rt, 3);
2158 jit_andi(tmp, tmp, 0xf80);
2159 jit_stxi_s(cp2d_s_offset(11), LIGHTREC_REG_STATE, tmp);
2161 lightrec_free_reg(reg_cache, tmp);
2164 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
2165 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
2167 /* if (rt < 0) rt = ~rt; */
2168 jit_rshi(tmp, rt, 31);
2169 jit_xorr(tmp, rt, tmp);
2171 /* We know the sign bit is 0. Left-shift by 1 to start the algorithm */
2172 jit_lshi(tmp, tmp, 1);
2175 /* Decrement tmp2 and right-shift the value by 1 until it equals zero */
2177 jit_subi(tmp2, tmp2, 1);
2178 jit_rshi_u(tmp, tmp, 1);
2179 to_loop = jit_bnei(tmp, 0);
2181 jit_patch_at(to_loop, loop);
2183 jit_stxi_i(cp2d_i_offset(31), LIGHTREC_REG_STATE, tmp2);
2184 jit_stxi_i(cp2d_i_offset(30), LIGHTREC_REG_STATE, rt);
2186 lightrec_free_reg(reg_cache, tmp);
2187 lightrec_free_reg(reg_cache, tmp2);
2190 jit_stxi_i(cp2d_i_offset(c.r.rd), LIGHTREC_REG_STATE, rt);
2194 lightrec_free_reg(reg_cache, rt);
2197 static void rec_cp2_basic_CTC2(struct lightrec_cstate *state,
2198 const struct block *block, u16 offset)
2200 struct regcache *reg_cache = state->reg_cache;
2201 const union code c = block->opcode_list[offset].c;
2202 jit_state_t *_jit = block->_jit;
2205 _jit_name(block->_jit, __func__);
2207 rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0);
2217 jit_stxi_s(cp2c_s_offset(c.r.rd), LIGHTREC_REG_STATE, rt);
2220 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
2221 tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
2223 jit_andi(tmp, rt, 0x7f87e000);
2224 jit_nei(tmp, tmp, 0);
2225 jit_lshi(tmp, tmp, 31);
2227 jit_andi(tmp2, rt, 0x7ffff000);
2228 jit_orr(tmp, tmp2, tmp);
2230 jit_stxi_i(cp2c_i_offset(31), LIGHTREC_REG_STATE, tmp);
2232 lightrec_free_reg(reg_cache, tmp);
2233 lightrec_free_reg(reg_cache, tmp2);
2237 jit_stxi_i(cp2c_i_offset(c.r.rd), LIGHTREC_REG_STATE, rt);
2240 lightrec_free_reg(reg_cache, rt);
2243 static void rec_cp0_RFE(struct lightrec_cstate *state,
2244 const struct block *block, u16 offset)
2246 struct regcache *reg_cache = state->reg_cache;
2247 jit_state_t *_jit = block->_jit;
2251 jit_note(__FILE__, __LINE__);
2253 status = lightrec_alloc_reg_temp(reg_cache, _jit);
2254 jit_ldxi_i(status, LIGHTREC_REG_STATE,
2255 offsetof(struct lightrec_state, regs.cp0[12]));
2257 tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
2259 /* status = ((status >> 2) & 0xf) | status & ~0xf; */
2260 jit_rshi(tmp, status, 2);
2261 jit_andi(tmp, tmp, 0xf);
2262 jit_andi(status, status, ~0xful);
2263 jit_orr(status, status, tmp);
2265 jit_ldxi_i(tmp, LIGHTREC_REG_STATE,
2266 offsetof(struct lightrec_state, regs.cp0[13]));
2267 jit_stxi_i(offsetof(struct lightrec_state, regs.cp0[12]),
2268 LIGHTREC_REG_STATE, status);
2270 /* Exit dynarec in case there's a software interrupt.
2271 * exit_flags = !!(status & cause & 0x0300) & status; */
2272 jit_andr(tmp, tmp, status);
2273 jit_andi(tmp, tmp, 0x0300);
2274 jit_nei(tmp, tmp, 0);
2275 jit_andr(tmp, tmp, status);
2276 jit_stxi_i(offsetof(struct lightrec_state, exit_flags),
2277 LIGHTREC_REG_STATE, tmp);
2279 lightrec_free_reg(reg_cache, status);
2280 lightrec_free_reg(reg_cache, tmp);
2283 static void rec_CP(struct lightrec_cstate *state,
2284 const struct block *block, u16 offset)
2286 union code c = block->opcode_list[offset].c;
2287 jit_state_t *_jit = block->_jit;
2290 jit_note(__FILE__, __LINE__);
2292 call_to_c_wrapper(state, block, c.opcode, C_WRAPPER_CP);
2295 static void rec_meta_MOV(struct lightrec_cstate *state,
2296 const struct block *block, u16 offset)
2298 struct regcache *reg_cache = state->reg_cache;
2299 union code c = block->opcode_list[offset].c;
2300 jit_state_t *_jit = block->_jit;
2303 _jit_name(block->_jit, __func__);
2304 jit_note(__FILE__, __LINE__);
2306 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0);
2307 rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, REG_EXT);
2315 lightrec_free_reg(reg_cache, rs);
2316 lightrec_free_reg(reg_cache, rd);
2319 static void rec_meta_EXTC_EXTS(struct lightrec_cstate *state,
2320 const struct block *block,
2323 struct regcache *reg_cache = state->reg_cache;
2324 union code c = block->opcode_list[offset].c;
2325 jit_state_t *_jit = block->_jit;
2328 _jit_name(block->_jit, __func__);
2329 jit_note(__FILE__, __LINE__);
2331 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
2332 rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, REG_EXT);
2334 if (c.i.op == OP_META_EXTC)
2339 lightrec_free_reg(reg_cache, rs);
2340 lightrec_free_reg(reg_cache, rt);
2343 static void rec_meta_MULT2(struct lightrec_cstate *state,
2344 const struct block *block,
2347 struct regcache *reg_cache = state->reg_cache;
2348 union code c = block->opcode_list[offset].c;
2349 jit_state_t *_jit = block->_jit;
2350 u8 reg_lo = get_mult_div_lo(c);
2351 u8 reg_hi = get_mult_div_hi(c);
2352 u32 flags = block->opcode_list[offset].flags;
2353 bool is_signed = c.i.op == OP_META_MULT2;
2354 u8 rs, lo, hi, rflags = 0, hiflags = 0;
2356 if (!op_flag_no_hi(flags) && c.r.op < 32) {
2357 rflags = is_signed ? REG_EXT : REG_ZEXT;
2358 hiflags = is_signed ? REG_EXT : (REG_EXT | REG_ZEXT);
2361 _jit_name(block->_jit, __func__);
2362 jit_note(__FILE__, __LINE__);
2364 rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, rflags);
2366 if (!op_flag_no_lo(flags)) {
2367 lo = lightrec_alloc_reg_out(reg_cache, _jit, reg_lo, 0);
2370 jit_lshi(lo, rs, c.r.op);
2374 lightrec_free_reg(reg_cache, lo);
2377 if (!op_flag_no_hi(flags)) {
2378 hi = lightrec_alloc_reg_out(reg_cache, _jit, reg_hi, hiflags);
2381 jit_lshi(hi, rs, c.r.op - 32);
2383 jit_rshi(hi, rs, 32 - c.r.op);
2385 jit_rshi_u(hi, rs, 32 - c.r.op);
2387 lightrec_free_reg(reg_cache, hi);
2390 lightrec_free_reg(reg_cache, rs);
2392 _jit_name(block->_jit, __func__);
2393 jit_note(__FILE__, __LINE__);
2396 static const lightrec_rec_func_t rec_standard[64] = {
2397 SET_DEFAULT_ELM(rec_standard, unknown_opcode),
2398 [OP_SPECIAL] = rec_SPECIAL,
2399 [OP_REGIMM] = rec_REGIMM,
2404 [OP_BLEZ] = rec_BLEZ,
2405 [OP_BGTZ] = rec_BGTZ,
2406 [OP_ADDI] = rec_ADDI,
2407 [OP_ADDIU] = rec_ADDIU,
2408 [OP_SLTI] = rec_SLTI,
2409 [OP_SLTIU] = rec_SLTIU,
2410 [OP_ANDI] = rec_ANDI,
2412 [OP_XORI] = rec_XORI,
2428 [OP_LWC2] = rec_LWC2,
2429 [OP_SWC2] = rec_SWC2,
2431 [OP_META_MOV] = rec_meta_MOV,
2432 [OP_META_EXTC] = rec_meta_EXTC_EXTS,
2433 [OP_META_EXTS] = rec_meta_EXTC_EXTS,
2434 [OP_META_MULT2] = rec_meta_MULT2,
2435 [OP_META_MULTU2] = rec_meta_MULT2,
2438 static const lightrec_rec_func_t rec_special[64] = {
2439 SET_DEFAULT_ELM(rec_special, unknown_opcode),
2440 [OP_SPECIAL_SLL] = rec_special_SLL,
2441 [OP_SPECIAL_SRL] = rec_special_SRL,
2442 [OP_SPECIAL_SRA] = rec_special_SRA,
2443 [OP_SPECIAL_SLLV] = rec_special_SLLV,
2444 [OP_SPECIAL_SRLV] = rec_special_SRLV,
2445 [OP_SPECIAL_SRAV] = rec_special_SRAV,
2446 [OP_SPECIAL_JR] = rec_special_JR,
2447 [OP_SPECIAL_JALR] = rec_special_JALR,
2448 [OP_SPECIAL_SYSCALL] = rec_special_SYSCALL,
2449 [OP_SPECIAL_BREAK] = rec_special_BREAK,
2450 [OP_SPECIAL_MFHI] = rec_special_MFHI,
2451 [OP_SPECIAL_MTHI] = rec_special_MTHI,
2452 [OP_SPECIAL_MFLO] = rec_special_MFLO,
2453 [OP_SPECIAL_MTLO] = rec_special_MTLO,
2454 [OP_SPECIAL_MULT] = rec_special_MULT,
2455 [OP_SPECIAL_MULTU] = rec_special_MULTU,
2456 [OP_SPECIAL_DIV] = rec_special_DIV,
2457 [OP_SPECIAL_DIVU] = rec_special_DIVU,
2458 [OP_SPECIAL_ADD] = rec_special_ADD,
2459 [OP_SPECIAL_ADDU] = rec_special_ADDU,
2460 [OP_SPECIAL_SUB] = rec_special_SUB,
2461 [OP_SPECIAL_SUBU] = rec_special_SUBU,
2462 [OP_SPECIAL_AND] = rec_special_AND,
2463 [OP_SPECIAL_OR] = rec_special_OR,
2464 [OP_SPECIAL_XOR] = rec_special_XOR,
2465 [OP_SPECIAL_NOR] = rec_special_NOR,
2466 [OP_SPECIAL_SLT] = rec_special_SLT,
2467 [OP_SPECIAL_SLTU] = rec_special_SLTU,
2470 static const lightrec_rec_func_t rec_regimm[64] = {
2471 SET_DEFAULT_ELM(rec_regimm, unknown_opcode),
2472 [OP_REGIMM_BLTZ] = rec_regimm_BLTZ,
2473 [OP_REGIMM_BGEZ] = rec_regimm_BGEZ,
2474 [OP_REGIMM_BLTZAL] = rec_regimm_BLTZAL,
2475 [OP_REGIMM_BGEZAL] = rec_regimm_BGEZAL,
2478 static const lightrec_rec_func_t rec_cp0[64] = {
2479 SET_DEFAULT_ELM(rec_cp0, rec_CP),
2480 [OP_CP0_MFC0] = rec_cp0_MFC0,
2481 [OP_CP0_CFC0] = rec_cp0_CFC0,
2482 [OP_CP0_MTC0] = rec_cp0_MTC0,
2483 [OP_CP0_CTC0] = rec_cp0_CTC0,
2484 [OP_CP0_RFE] = rec_cp0_RFE,
2487 static const lightrec_rec_func_t rec_cp2_basic[64] = {
2488 SET_DEFAULT_ELM(rec_cp2_basic, rec_CP),
2489 [OP_CP2_BASIC_MFC2] = rec_cp2_basic_MFC2,
2490 [OP_CP2_BASIC_CFC2] = rec_cp2_basic_CFC2,
2491 [OP_CP2_BASIC_MTC2] = rec_cp2_basic_MTC2,
2492 [OP_CP2_BASIC_CTC2] = rec_cp2_basic_CTC2,
2495 static void rec_SPECIAL(struct lightrec_cstate *state,
2496 const struct block *block, u16 offset)
2498 union code c = block->opcode_list[offset].c;
2499 lightrec_rec_func_t f = rec_special[c.r.op];
2501 if (!HAS_DEFAULT_ELM && unlikely(!f))
2502 unknown_opcode(state, block, offset);
2504 (*f)(state, block, offset);
2507 static void rec_REGIMM(struct lightrec_cstate *state,
2508 const struct block *block, u16 offset)
2510 union code c = block->opcode_list[offset].c;
2511 lightrec_rec_func_t f = rec_regimm[c.r.rt];
2513 if (!HAS_DEFAULT_ELM && unlikely(!f))
2514 unknown_opcode(state, block, offset);
2516 (*f)(state, block, offset);
2519 static void rec_CP0(struct lightrec_cstate *state,
2520 const struct block *block, u16 offset)
2522 union code c = block->opcode_list[offset].c;
2523 lightrec_rec_func_t f = rec_cp0[c.r.rs];
2525 if (!HAS_DEFAULT_ELM && unlikely(!f))
2526 rec_CP(state, block, offset);
2528 (*f)(state, block, offset);
2531 static void rec_CP2(struct lightrec_cstate *state,
2532 const struct block *block, u16 offset)
2534 union code c = block->opcode_list[offset].c;
2536 if (c.r.op == OP_CP2_BASIC) {
2537 lightrec_rec_func_t f = rec_cp2_basic[c.r.rs];
2539 if (HAS_DEFAULT_ELM || likely(f)) {
2540 (*f)(state, block, offset);
2545 rec_CP(state, block, offset);
2548 void lightrec_rec_opcode(struct lightrec_cstate *state,
2549 const struct block *block, u16 offset)
2551 struct regcache *reg_cache = state->reg_cache;
2552 struct lightrec_branch_target *target;
2553 const struct opcode *op = &block->opcode_list[offset];
2554 jit_state_t *_jit = block->_jit;
2555 lightrec_rec_func_t f;
2558 if (op_flag_sync(op->flags)) {
2560 jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, state->cycles);
2563 lightrec_storeback_regs(reg_cache, _jit);
2564 lightrec_regcache_reset(reg_cache);
2566 pr_debug("Adding branch target at offset 0x%x\n", offset << 2);
2567 target = &state->targets[state->nb_targets++];
2568 target->offset = offset;
2569 target->label = jit_indirect();
2572 if (likely(op->opcode)) {
2573 f = rec_standard[op->i.op];
2575 if (!HAS_DEFAULT_ELM && unlikely(!f))
2576 unknown_opcode(state, block, offset);
2578 (*f)(state, block, offset);
2581 if (OPT_EARLY_UNLOAD) {
2582 unload_offset = offset +
2583 (has_delay_slot(op->c) && !op_flag_no_ds(op->flags));
2585 lightrec_do_early_unload(state, block, unload_offset);