X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=deps%2Flightrec%2Femitter.c;h=3875e58ed052fe86f4afc1d3f722c6f531ea9984;hb=refs%2Fheads%2Flibretro;hp=1b4082b7075076f9b134aeaeabf72e80b424e94a;hpb=18cdd2ad8875d26bac6661dc3e2a58d357d2a463;p=pcsx_rearmed.git diff --git a/deps/lightrec/emitter.c b/deps/lightrec/emitter.c index 1b4082b7..8612119f 100644 --- a/deps/lightrec/emitter.c +++ b/deps/lightrec/emitter.c @@ -14,6 +14,8 @@ #include #include +#define LIGHTNING_UNALIGNED_32BIT 4 + typedef void (*lightrec_rec_func_t)(struct lightrec_cstate *, const struct block *, u16); /* Forward declarations */ @@ -28,13 +30,6 @@ static void rec_cp2_do_mfc2(struct lightrec_cstate *state, const struct block *block, u16 offset, u8 reg, u8 out_reg); -static void unknown_opcode(struct lightrec_cstate *state, const struct block *block, u16 offset) -{ - pr_warn("Unknown opcode: 0x%08x at PC 0x%08x\n", - block->opcode_list[offset].c.opcode, - block->pc + (offset << 2)); -} - static void lightrec_jump_to_fn(jit_state_t *_jit, void (*fn)(void)) { @@ -75,7 +70,8 @@ static void lightrec_emit_end_of_block(struct lightrec_cstate *state, jit_state_t *_jit = block->_jit; const struct opcode *op = &block->opcode_list[offset], *ds = get_delay_slot(block->opcode_list, offset); - u32 cycles = state->cycles + lightrec_cycles_of_opcode(op->c); + u32 cycles = state->cycles + lightrec_cycles_of_opcode(state->state, op->c); + bool has_ds = has_delay_slot(op->c); jit_note(__FILE__, __LINE__); @@ -94,9 +90,8 @@ static void lightrec_emit_end_of_block(struct lightrec_cstate *state, update_ra_register(reg_cache, _jit, ra_reg, block->pc, link); } - if (has_delay_slot(op->c) && - !op_flag_no_ds(op->flags) && !op_flag_local_branch(op->flags)) { - cycles += lightrec_cycles_of_opcode(ds->c); + if (has_ds && !op_flag_no_ds(op->flags) && !op_flag_local_branch(op->flags)) { + cycles += lightrec_cycles_of_opcode(state->state, ds->c); /* Recompile the delay slot */ if (ds->c.opcode) @@ -111,8 +106,8 @@ static void lightrec_emit_end_of_block(struct lightrec_cstate *state, pr_debug("EOB: %u cycles\n", cycles); } - if (op_flag_load_delay(ds->flags) - && opcode_is_load(ds->c) && !state->no_load_delay) { + if (has_ds && op_flag_load_delay(ds->flags) + && opcode_has_load_delay(ds->c) && !state->no_load_delay) { /* If the delay slot is a load opcode, its target register * will be written after the first opcode of the target is * executed. Handle this by jumping to a special section of @@ -124,6 +119,8 @@ static void lightrec_emit_end_of_block(struct lightrec_cstate *state, } else { lightrec_jump_to_eob(state, _jit); } + + lightrec_regcache_reset(reg_cache); } void lightrec_emit_jump_to_interpreter(struct lightrec_cstate *state, @@ -138,6 +135,10 @@ void lightrec_emit_jump_to_interpreter(struct lightrec_cstate *state, * PC (which might have an offset) in JIT_V0. */ lightrec_load_imm(reg_cache, _jit, JIT_V0, block->pc, block->pc + (offset << 2)); + if (lightrec_store_next_pc()) { + jit_stxi_i(lightrec_offset(next_pc), LIGHTREC_REG_STATE, JIT_V0); + } + jit_movi(JIT_V1, (uintptr_t)block); jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, state->cycles); @@ -154,6 +155,10 @@ static void lightrec_emit_eob(struct lightrec_cstate *state, lightrec_load_imm(reg_cache, _jit, JIT_V0, block->pc, block->pc + (offset << 2)); + if (lightrec_store_next_pc()) { + jit_stxi_i(lightrec_offset(next_pc), LIGHTREC_REG_STATE, JIT_V0); + } + jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, state->cycles); lightrec_jump_to_eob(state, _jit); @@ -244,7 +249,7 @@ static void rec_b(struct lightrec_cstate *state, const struct block *block, u16 *ds = get_delay_slot(block->opcode_list, offset); jit_node_t *addr; bool is_forward = (s16)op->i.imm >= 0; - int op_cycles = lightrec_cycles_of_opcode(op->c); + int op_cycles = lightrec_cycles_of_opcode(state->state, op->c); u32 target_offset, cycles = state->cycles + op_cycles; bool no_indirection = false; u32 next_pc; @@ -253,7 +258,7 @@ static void rec_b(struct lightrec_cstate *state, const struct block *block, u16 jit_note(__FILE__, __LINE__); if (!op_flag_no_ds(op->flags)) - cycles += lightrec_cycles_of_opcode(ds->c); + cycles += lightrec_cycles_of_opcode(state->state, ds->c); state->cycles = -op_cycles; @@ -417,6 +422,36 @@ static void rec_regimm_BGEZAL(struct lightrec_cstate *state, !op->i.rs, true); } +static void rec_alloc_rs_rd(struct regcache *reg_cache, + jit_state_t *_jit, + const struct opcode *op, + u8 rs, u8 rd, + u8 in_flags, u8 out_flags, + u8 *rs_out, u8 *rd_out) +{ + bool unload, discard; + u32 unload_flags; + + if (OPT_EARLY_UNLOAD) { + unload_flags = LIGHTREC_FLAGS_GET_RS(op->flags); + unload = unload_flags == LIGHTREC_REG_UNLOAD; + discard = unload_flags == LIGHTREC_REG_DISCARD; + } + + if (OPT_EARLY_UNLOAD && rs && rd != rs && (unload || discard)) { + rs = lightrec_alloc_reg_in(reg_cache, _jit, rs, in_flags); + lightrec_remap_reg(reg_cache, _jit, rs, rd, discard); + lightrec_set_reg_out_flags(reg_cache, rs, out_flags); + rd = rs; + } else { + rs = lightrec_alloc_reg_in(reg_cache, _jit, rs, in_flags); + rd = lightrec_alloc_reg_out(reg_cache, _jit, rd, out_flags); + } + + *rs_out = rs; + *rd_out = rd; +} + static void rec_alu_imm(struct lightrec_cstate *state, const struct block *block, u16 offset, jit_code_t code, bool slti) { @@ -429,8 +464,9 @@ static void rec_alu_imm(struct lightrec_cstate *state, const struct block *block out_flags |= REG_ZEXT; jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, REG_EXT); - rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, out_flags); + + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.i.rs, c.i.rt, REG_EXT, out_flags, &rs, &rt); jit_new_node_www(code, rt, rs, (s32)(s16) c.i.imm); @@ -447,10 +483,11 @@ static void rec_alu_special(struct lightrec_cstate *state, const struct block *b u8 rd, rt, rs; jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, REG_EXT); + rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, REG_EXT); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, - out_ext ? REG_EXT | REG_ZEXT : 0); + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.r.rs, c.r.rd, REG_EXT, + out_ext ? REG_EXT | REG_ZEXT : 0, &rs, &rd); jit_new_node_www(code, rd, rs, rt); @@ -468,17 +505,17 @@ static void rec_alu_shiftv(struct lightrec_cstate *state, const struct block *bl u8 rd, rt, rs, temp, flags = 0; jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0); if (code == jit_code_rshr) flags = REG_EXT; else if (code == jit_code_rshr_u) flags = REG_ZEXT; - rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, flags); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, flags); + rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0); + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.r.rt, c.r.rd, flags, flags, &rt, &rd); - if (rs != rd && rt != rd) { + if (rt != rd) { jit_andi(rd, rs, 0x1f); jit_new_node_www(code, rd, rt, rd); } else { @@ -500,14 +537,18 @@ static void rec_movi(struct lightrec_cstate *state, union code c = block->opcode_list[offset].c; jit_state_t *_jit = block->_jit; u16 flags = REG_EXT; + s32 value = (s32)(s16) c.i.imm; u8 rt; - if (!(c.i.imm & 0x8000)) + if (block->opcode_list[offset].flags & LIGHTREC_MOVI) + value += (s32)((u32)state->movi_temp[c.i.rt] << 16); + + if (value >= 0) flags |= REG_ZEXT; rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, flags); - jit_movi(rt, (s32)(s16) c.i.imm); + jit_movi(rt, value); lightrec_free_reg(reg_cache, rt); } @@ -515,9 +556,11 @@ static void rec_movi(struct lightrec_cstate *state, static void rec_ADDIU(struct lightrec_cstate *state, const struct block *block, u16 offset) { + const struct opcode *op = &block->opcode_list[offset]; + _jit_name(block->_jit, __func__); - if (block->opcode_list[offset].c.i.rs) + if (op->i.rs && !(op->flags & LIGHTREC_MOVI)) rec_alu_imm(state, block, offset, jit_code_addi, false); else rec_movi(state, block, offset); @@ -555,9 +598,9 @@ static void rec_ANDI(struct lightrec_cstate *state, _jit_name(block->_jit, __func__); jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0); - rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, - REG_EXT | REG_ZEXT); + + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.i.rs, c.i.rt, 0, REG_EXT | REG_ZEXT, &rs, &rt); /* PSX code uses ANDI 0xff / ANDI 0xffff a lot, which are basically * casts to uint8_t / uint16_t. */ @@ -581,8 +624,9 @@ static void rec_alu_or_xor(struct lightrec_cstate *state, const struct block *bl u8 rs, rt, flags; jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0); - rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, 0); + + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.i.rs, c.i.rt, 0, 0, &rs, &rt); flags = lightrec_get_reg_in_flags(reg_cache, rs); lightrec_set_reg_out_flags(reg_cache, rt, flags); @@ -597,8 +641,24 @@ static void rec_alu_or_xor(struct lightrec_cstate *state, const struct block *bl static void rec_ORI(struct lightrec_cstate *state, const struct block *block, u16 offset) { - _jit_name(block->_jit, __func__); - rec_alu_or_xor(state, block, offset, jit_code_ori); + const struct opcode *op = &block->opcode_list[offset]; + struct regcache *reg_cache = state->reg_cache; + jit_state_t *_jit = block->_jit; + s32 val; + u8 rt; + + _jit_name(_jit, __func__); + + if (op->flags & LIGHTREC_MOVI) { + rt = lightrec_alloc_reg_out(reg_cache, _jit, op->i.rt, REG_EXT); + + val = ((u32)state->movi_temp[op->i.rt] << 16) | op->i.imm; + jit_movi(rt, val); + + lightrec_free_reg(reg_cache, rt); + } else { + rec_alu_or_xor(state, block, offset, jit_code_ori); + } } static void rec_XORI(struct lightrec_cstate *state, @@ -616,6 +676,11 @@ static void rec_LUI(struct lightrec_cstate *state, jit_state_t *_jit = block->_jit; u8 rt, flags = REG_EXT; + if (block->opcode_list[offset].flags & LIGHTREC_MOVI) { + state->movi_temp[c.i.rt] = c.i.imm; + return; + } + jit_name(__func__); jit_note(__FILE__, __LINE__); @@ -669,9 +734,10 @@ static void rec_special_AND(struct lightrec_cstate *state, _jit_name(block->_jit, __func__); jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0); + rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, 0); + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.r.rs, c.r.rd, 0, 0, &rs, &rd); flags_rs = lightrec_get_reg_in_flags(reg_cache, rs); flags_rt = lightrec_get_reg_in_flags(reg_cache, rt); @@ -703,9 +769,10 @@ static void rec_special_or_nor(struct lightrec_cstate *state, u8 rd, rt, rs, flags_rs, flags_rt, flags_rd = 0; jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0); + rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, 0); + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.r.rs, c.r.rd, 0, 0, &rs, &rd); flags_rs = lightrec_get_reg_in_flags(reg_cache, rs); flags_rt = lightrec_get_reg_in_flags(reg_cache, rt); @@ -756,9 +823,10 @@ static void rec_special_XOR(struct lightrec_cstate *state, _jit_name(block->_jit, __func__); jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, 0); + rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, 0); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, 0); + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.r.rs, c.r.rd, 0, 0, &rs, &rd); flags_rs = lightrec_get_reg_in_flags(reg_cache, rs); flags_rt = lightrec_get_reg_in_flags(reg_cache, rt); @@ -819,7 +887,7 @@ static void rec_alu_shift(struct lightrec_cstate *state, const struct block *blo struct regcache *reg_cache = state->reg_cache; union code c = block->opcode_list[offset].c; jit_state_t *_jit = block->_jit; - u8 rd, rt, flags = 0; + u8 rd, rt, flags = 0, out_flags = 0; jit_note(__FILE__, __LINE__); @@ -828,13 +896,14 @@ static void rec_alu_shift(struct lightrec_cstate *state, const struct block *blo else if (code == jit_code_rshi_u) flags = REG_ZEXT; - rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, flags); - /* Input reg is zero-extended, if we SRL at least by one bit, we know * the output reg will be both zero-extended and sign-extended. */ + out_flags = flags; if (code == jit_code_rshi_u && c.r.imm) - flags |= REG_EXT; - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rd, flags); + out_flags |= REG_EXT; + + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.r.rt, c.r.rd, flags, out_flags, &rt, &rd); jit_new_node_www(code, rd, rt, c.r.imm); @@ -873,6 +942,8 @@ static void rec_alu_mult(struct lightrec_cstate *state, u8 reg_hi = get_mult_div_hi(c); jit_state_t *_jit = block->_jit; u8 lo, hi, rs, rt, rflags = 0; + bool no_lo = op_flag_no_lo(flags); + bool no_hi = op_flag_no_hi(flags); jit_note(__FILE__, __LINE__); @@ -884,44 +955,46 @@ static void rec_alu_mult(struct lightrec_cstate *state, rs = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rs, rflags); rt = lightrec_alloc_reg_in(reg_cache, _jit, c.r.rt, rflags); - if (!op_flag_no_lo(flags)) + if (!no_lo) lo = lightrec_alloc_reg_out(reg_cache, _jit, reg_lo, 0); - else if (__WORDSIZE == 32) - lo = lightrec_alloc_reg_temp(reg_cache, _jit); - if (!op_flag_no_hi(flags)) + if (!no_hi) hi = lightrec_alloc_reg_out(reg_cache, _jit, reg_hi, REG_EXT); if (__WORDSIZE == 32) { /* On 32-bit systems, do a 32*32->64 bit operation, or a 32*32->32 bit * operation if the MULT was detected a 32-bit only. */ - if (!op_flag_no_hi(flags)) { + if (no_lo) { if (is_signed) - jit_qmulr(lo, hi, rs, rt); + jit_hmulr(hi, rs, rt); else - jit_qmulr_u(lo, hi, rs, rt); - } else { + jit_hmulr_u(hi, rs, rt); + } else if (no_hi) { jit_mulr(lo, rs, rt); + } else if (is_signed) { + jit_qmulr(lo, hi, rs, rt); + } else { + jit_qmulr_u(lo, hi, rs, rt); } } else { /* On 64-bit systems, do a 64*64->64 bit operation. */ - if (op_flag_no_lo(flags)) { + if (no_lo) { jit_mulr(hi, rs, rt); jit_rshi(hi, hi, 32); } else { jit_mulr(lo, rs, rt); /* The 64-bit output value is in $lo, store the upper 32 bits in $hi */ - if (!op_flag_no_hi(flags)) + if (!no_hi) jit_rshi(hi, lo, 32); } } lightrec_free_reg(reg_cache, rs); lightrec_free_reg(reg_cache, rt); - if (!op_flag_no_lo(flags) || __WORDSIZE == 32) + if (!no_lo) lightrec_free_reg(reg_cache, lo); - if (!op_flag_no_hi(flags)) + if (!no_hi) lightrec_free_reg(reg_cache, hi); } @@ -1036,14 +1109,16 @@ static void rec_special_DIVU(struct lightrec_cstate *state, } static void rec_alu_mv_lo_hi(struct lightrec_cstate *state, - const struct block *block, u8 dst, u8 src) + const struct block *block, u16 offset, + u8 dst, u8 src) { struct regcache *reg_cache = state->reg_cache; jit_state_t *_jit = block->_jit; jit_note(__FILE__, __LINE__); - src = lightrec_alloc_reg_in(reg_cache, _jit, src, 0); - dst = lightrec_alloc_reg_out(reg_cache, _jit, dst, REG_EXT); + + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + src, dst, 0, REG_EXT, &src, &dst); jit_extr_i(dst, src); @@ -1057,7 +1132,7 @@ static void rec_special_MFHI(struct lightrec_cstate *state, union code c = block->opcode_list[offset].c; _jit_name(block->_jit, __func__); - rec_alu_mv_lo_hi(state, block, c.r.rd, REG_HI); + rec_alu_mv_lo_hi(state, block, offset, c.r.rd, REG_HI); } static void rec_special_MTHI(struct lightrec_cstate *state, @@ -1066,7 +1141,7 @@ static void rec_special_MTHI(struct lightrec_cstate *state, union code c = block->opcode_list[offset].c; _jit_name(block->_jit, __func__); - rec_alu_mv_lo_hi(state, block, REG_HI, c.r.rs); + rec_alu_mv_lo_hi(state, block, offset, REG_HI, c.r.rs); } static void rec_special_MFLO(struct lightrec_cstate *state, @@ -1075,7 +1150,7 @@ static void rec_special_MFLO(struct lightrec_cstate *state, union code c = block->opcode_list[offset].c; _jit_name(block->_jit, __func__); - rec_alu_mv_lo_hi(state, block, c.r.rd, REG_LO); + rec_alu_mv_lo_hi(state, block, offset, c.r.rd, REG_LO); } static void rec_special_MTLO(struct lightrec_cstate *state, @@ -1084,7 +1159,7 @@ static void rec_special_MTLO(struct lightrec_cstate *state, union code c = block->opcode_list[offset].c; _jit_name(block->_jit, __func__); - rec_alu_mv_lo_hi(state, block, REG_LO, c.r.rs); + rec_alu_mv_lo_hi(state, block, offset, REG_LO, c.r.rs); } static void call_to_c_wrapper(struct lightrec_cstate *state, @@ -1098,15 +1173,16 @@ static void call_to_c_wrapper(struct lightrec_cstate *state, /* Make sure JIT_R1 is not mapped; it will be used in the C wrapper. */ tmp2 = lightrec_alloc_reg(reg_cache, _jit, JIT_R1); + jit_movi(tmp2, (unsigned int)wrapper << (1 + __WORDSIZE / 32)); + tmp = lightrec_get_reg_with_value(reg_cache, - (intptr_t) state->state->wrappers_eps[wrapper]); + (intptr_t) state->state->c_wrapper); if (tmp < 0) { tmp = lightrec_alloc_reg_temp(reg_cache, _jit); - jit_ldxi(tmp, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, wrappers_eps[wrapper])); + jit_ldxi(tmp, LIGHTREC_REG_STATE, lightrec_offset(c_wrapper)); lightrec_temp_set_value(reg_cache, tmp, - (intptr_t) state->state->wrappers_eps[wrapper]); + (intptr_t) state->state->c_wrapper); } lightrec_free_reg(reg_cache, tmp2); @@ -1155,8 +1231,8 @@ static void rec_io(struct lightrec_cstate *state, * wrapper that it should write the REG_TEMP register instead of * the actual output register of the opcode. */ zero = lightrec_alloc_reg_in(reg_cache, _jit, 0, 0); - jit_stxi_c(offsetof(struct lightrec_state, in_delay_slot_n), - LIGHTREC_REG_STATE, zero); + jit_stxi_c(lightrec_offset(in_delay_slot_n), + LIGHTREC_REG_STATE, zero); lightrec_free_reg(reg_cache, zero); } @@ -1169,7 +1245,7 @@ static void rec_io(struct lightrec_cstate *state, } } -static u32 rec_ram_mask(struct lightrec_state *state) +static u32 rec_ram_mask(const struct lightrec_state *state) { return (RAM_SIZE << (state->mirrors_mapped * 2)) - 1; } @@ -1181,31 +1257,55 @@ static u32 rec_io_mask(const struct lightrec_state *state) return 0x1f800000 | GENMASK(31 - clz32(length - 1), 0); } +static void rec_add_offset(struct lightrec_cstate *cstate, + jit_state_t *_jit, u8 reg_out, u8 reg_in, + uintptr_t offset) +{ + struct regcache *reg_cache = cstate->reg_cache; + u8 reg_imm; + + reg_imm = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, offset); + jit_addr(reg_out, reg_in, reg_imm); + + lightrec_free_reg(reg_cache, reg_imm); +} + +static void rec_and_mask(struct lightrec_cstate *cstate, + jit_state_t *_jit, u8 reg_out, u8 reg_in, u32 mask) +{ + struct regcache *reg_cache = cstate->reg_cache; + u8 reg_imm; + + reg_imm = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, mask); + jit_andr(reg_out, reg_in, reg_imm); + + lightrec_free_reg(reg_cache, reg_imm); +} + static void rec_store_memory(struct lightrec_cstate *cstate, const struct block *block, u16 offset, jit_code_t code, - jit_code_t swap_code, - uintptr_t addr_offset, u32 addr_mask, - bool invalidate) + jit_code_t swap_code, uintptr_t addr_offset, + u32 addr_mask, bool invalidate) { const struct lightrec_state *state = cstate->state; struct regcache *reg_cache = cstate->reg_cache; struct opcode *op = &block->opcode_list[offset]; jit_state_t *_jit = block->_jit; union code c = op->c; - u8 rs, rt, tmp, tmp2, tmp3, addr_reg, addr_reg2; + u8 rs, rt, tmp = 0, tmp2 = 0, tmp3, addr_reg, addr_reg2; s16 imm = (s16)c.i.imm; s32 simm = (s32)imm << (1 - lut_is_32bit(state)); - s32 lut_offt = offsetof(struct lightrec_state, code_lut); + s32 lut_offt = lightrec_offset(code_lut); bool no_mask = op_flag_no_mask(op->flags); bool add_imm = c.i.imm && - ((!state->mirrors_mapped && !no_mask) || (invalidate && + (c.i.op == OP_META_SWU + || (!state->mirrors_mapped && !no_mask) || (invalidate && ((imm & 0x3) || simm + lut_offt != (s16)(simm + lut_offt)))); - bool need_tmp = !no_mask || addr_offset || add_imm || invalidate; + bool need_tmp = !no_mask || add_imm || invalidate; bool swc2 = c.i.op == OP_SWC2; u8 in_reg = swc2 ? REG_TEMP : c.i.rt; - rt = lightrec_alloc_reg_in(reg_cache, _jit, in_reg, 0); rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0); if (need_tmp) tmp = lightrec_alloc_reg_temp(reg_cache, _jit); @@ -1214,6 +1314,7 @@ static void rec_store_memory(struct lightrec_cstate *cstate, if (add_imm) { jit_addi(tmp, addr_reg, (s16)c.i.imm); + lightrec_free_reg(reg_cache, rs); addr_reg = tmp; imm = 0; } else if (simm) { @@ -1221,25 +1322,33 @@ static void rec_store_memory(struct lightrec_cstate *cstate, } if (!no_mask) { - jit_andi(tmp, addr_reg, addr_mask); + rec_and_mask(cstate, _jit, tmp, addr_reg, addr_mask); addr_reg = tmp; } if (addr_offset) { tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit); - jit_addi(tmp2, addr_reg, addr_offset); + rec_add_offset(cstate, _jit, tmp2, addr_reg, addr_offset); addr_reg2 = tmp2; } else { addr_reg2 = addr_reg; } + rt = lightrec_alloc_reg_in(reg_cache, _jit, in_reg, 0); + if (is_big_endian() && swap_code && in_reg) { tmp3 = lightrec_alloc_reg_temp(reg_cache, _jit); jit_new_node_ww(swap_code, tmp3, rt); - jit_new_node_www(code, imm, addr_reg2, tmp3); + + if (c.i.op == OP_META_SWU) + jit_unstr(addr_reg2, tmp3, LIGHTNING_UNALIGNED_32BIT); + else + jit_new_node_www(code, imm, addr_reg2, tmp3); lightrec_free_reg(reg_cache, tmp3); + } else if (c.i.op == OP_META_SWU) { + jit_unstr(addr_reg2, rt, LIGHTNING_UNALIGNED_32BIT); } else { jit_new_node_www(code, imm, addr_reg2, rt); } @@ -1286,7 +1395,7 @@ static void rec_store_ram(struct lightrec_cstate *cstate, u16 offset, jit_code_t code, jit_code_t swap_code, bool invalidate) { - struct lightrec_state *state = cstate->state; + const struct lightrec_state *state = cstate->state; _jit_note(block->_jit, __FILE__, __LINE__); @@ -1322,39 +1431,37 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate, u16 offset, jit_code_t code, jit_code_t swap_code) { - struct lightrec_state *state = cstate->state; + const struct lightrec_state *state = cstate->state; + u32 ram_size = state->mirrors_mapped ? RAM_SIZE * 4 : RAM_SIZE; struct regcache *reg_cache = cstate->reg_cache; union code c = block->opcode_list[offset].c; jit_state_t *_jit = block->_jit; jit_node_t *to_not_ram, *to_end; bool swc2 = c.i.op == OP_SWC2; - bool offset_ram_or_scratch = state->offset_ram || state->offset_scratch; - u8 tmp, tmp2, rs, rt, in_reg = swc2 ? REG_TEMP : c.i.rt; + u8 addr_reg, tmp, tmp2 = 0, rs, rt, in_reg = swc2 ? REG_TEMP : c.i.rt; s16 imm; jit_note(__FILE__, __LINE__); rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0); tmp = lightrec_alloc_reg_temp(reg_cache, _jit); - if (offset_ram_or_scratch) - tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit); - /* Convert to KUNSEG and avoid RAM mirrors */ - if (state->mirrors_mapped) { - imm = (s16)c.i.imm; - jit_andi(tmp, rs, 0x1f800000 | (4 * RAM_SIZE - 1)); - } else if (c.i.imm) { + if ((c.i.op == OP_META_SWU || !state->mirrors_mapped) && c.i.imm) { imm = 0; jit_addi(tmp, rs, (s16)c.i.imm); - jit_andi(tmp, tmp, 0x1f800000 | (RAM_SIZE - 1)); + addr_reg = tmp; } else { - imm = 0; - jit_andi(tmp, rs, 0x1f800000 | (RAM_SIZE - 1)); + imm = (s16)c.i.imm; + addr_reg = rs; } + rec_and_mask(cstate, _jit, tmp, addr_reg, 0x1f800000 | (ram_size - 1)); + lightrec_free_reg(reg_cache, rs); if (state->offset_ram != state->offset_scratch) { + tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit); + to_not_ram = jit_bmsi(tmp, BIT(28)); jit_movi(tmp2, state->offset_ram); @@ -1365,10 +1472,11 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate, jit_movi(tmp2, state->offset_scratch); jit_patch(to_end); } else if (state->offset_ram) { - jit_movi(tmp2, state->offset_ram); + tmp2 = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, + state->offset_ram); } - if (offset_ram_or_scratch) { + if (state->offset_ram || state->offset_scratch) { jit_addr(tmp, tmp, tmp2); lightrec_free_reg(reg_cache, tmp2); } @@ -1379,9 +1487,15 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate, tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit); jit_new_node_ww(swap_code, tmp2, rt); - jit_new_node_www(code, imm, tmp, tmp2); + + if (c.i.op == OP_META_SWU) + jit_unstr(tmp, tmp2, LIGHTNING_UNALIGNED_32BIT); + else + jit_new_node_www(code, imm, tmp, tmp2); lightrec_free_reg(reg_cache, tmp2); + } else if (c.i.op == OP_META_SWU) { + jit_unstr(tmp, rt, LIGHTNING_UNALIGNED_32BIT); } else { jit_new_node_www(code, imm, tmp, rt); } @@ -1393,15 +1507,17 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate, static void rec_store_direct(struct lightrec_cstate *cstate, const struct block *block, u16 offset, jit_code_t code, jit_code_t swap_code) { - struct lightrec_state *state = cstate->state; + const struct lightrec_state *state = cstate->state; u32 ram_size = state->mirrors_mapped ? RAM_SIZE * 4 : RAM_SIZE; struct regcache *reg_cache = cstate->reg_cache; union code c = block->opcode_list[offset].c; jit_state_t *_jit = block->_jit; jit_node_t *to_not_ram, *to_end; bool swc2 = c.i.op == OP_SWC2; - u8 tmp, tmp2, tmp3, masked_reg, rs, rt; + u8 addr_reg, tmp, tmp2, tmp3, rs, rt, reg_imm; u8 in_reg = swc2 ? REG_TEMP : c.i.rt; + u32 mask; + bool different_offsets = state->offset_ram != state->offset_scratch; jit_note(__FILE__, __LINE__); @@ -1412,28 +1528,30 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block /* Convert to KUNSEG and avoid RAM mirrors */ if (c.i.imm) { jit_addi(tmp2, rs, (s16)c.i.imm); - jit_andi(tmp2, tmp2, 0x1f800000 | (ram_size - 1)); + addr_reg = tmp2; } else { - jit_andi(tmp2, rs, 0x1f800000 | (ram_size - 1)); + addr_reg = rs; } + rec_and_mask(cstate, _jit, tmp2, addr_reg, 0x1f800000 | (ram_size - 1)); + lightrec_free_reg(reg_cache, rs); tmp = lightrec_alloc_reg_temp(reg_cache, _jit); - if (state->offset_ram != state->offset_scratch) { + mask = c.i.op == OP_SW ? RAM_SIZE - 1 : (RAM_SIZE - 1) & ~3; + reg_imm = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, mask); + + if (different_offsets) { to_not_ram = jit_bgti(tmp2, ram_size); - masked_reg = tmp2; + addr_reg = tmp2; } else { jit_lti_u(tmp, tmp2, ram_size); jit_movnr(tmp, tmp2, tmp); - masked_reg = tmp; + addr_reg = tmp; } /* Compute the offset to the code LUT */ - if (c.i.op == OP_SW) - jit_andi(tmp, masked_reg, RAM_SIZE - 1); - else - jit_andi(tmp, masked_reg, (RAM_SIZE - 1) & ~3); + jit_andr(tmp, addr_reg, reg_imm); if (!lut_is_32bit(state)) jit_lshi(tmp, tmp, 1); @@ -1441,11 +1559,22 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block /* Write NULL to the code LUT to invalidate any block that's there */ if (lut_is_32bit(state)) - jit_stxi_i(offsetof(struct lightrec_state, code_lut), tmp, tmp3); + jit_stxi_i(lightrec_offset(code_lut), tmp, tmp3); else - jit_stxi(offsetof(struct lightrec_state, code_lut), tmp, tmp3); + jit_stxi(lightrec_offset(code_lut), tmp, tmp3); - if (state->offset_ram != state->offset_scratch) { + if (c.i.op == OP_META_SWU) { + /* With a SWU opcode, we might have touched the following 32-bit + * word, so invalidate it as well */ + if (lut_is_32bit(state)) { + jit_stxi_i(lightrec_offset(code_lut) + 4, tmp, tmp3); + } else { + jit_stxi(lightrec_offset(code_lut) + sizeof(uintptr_t), + tmp, tmp3); + } + } + + if (different_offsets) { jit_movi(tmp, state->offset_ram); to_end = jit_b(); @@ -1455,7 +1584,7 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block if (state->offset_ram || state->offset_scratch) jit_movi(tmp, state->offset_scratch); - if (state->offset_ram != state->offset_scratch) + if (different_offsets) jit_patch(to_end); if (state->offset_ram || state->offset_scratch) @@ -1463,6 +1592,7 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block lightrec_free_reg(reg_cache, tmp); lightrec_free_reg(reg_cache, tmp3); + lightrec_free_reg(reg_cache, reg_imm); rt = lightrec_alloc_reg_in(reg_cache, _jit, in_reg, 0); @@ -1470,9 +1600,15 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block tmp = lightrec_alloc_reg_temp(reg_cache, _jit); jit_new_node_ww(swap_code, tmp, rt); - jit_new_node_www(code, 0, tmp2, tmp); + + if (c.i.op == OP_META_SWU) + jit_unstr(tmp2, tmp, LIGHTNING_UNALIGNED_32BIT); + else + jit_new_node_www(code, 0, tmp2, tmp); lightrec_free_reg(reg_cache, tmp); + } else if (c.i.op == OP_META_SWU) { + jit_unstr(tmp2, rt, LIGHTNING_UNALIGNED_32BIT); } else { jit_new_node_www(code, 0, tmp2, rt); } @@ -1488,7 +1624,7 @@ static void rec_store(struct lightrec_cstate *state, u32 flags = block->opcode_list[offset].flags; u32 mode = LIGHTREC_FLAGS_GET_IO_MODE(flags); bool no_invalidate = op_flag_no_invalidate(flags) || - state->state->invalidate_from_dma_only; + (state->state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY); union code c = block->opcode_list[offset].c; bool is_swc2 = c.i.op == OP_SWC2; @@ -1578,6 +1714,7 @@ static void rec_load_memory(struct lightrec_cstate *cstate, jit_code_t code, jit_code_t swap_code, bool is_unsigned, uintptr_t addr_offset, u32 addr_mask) { + struct lightrec_state *state = cstate->state; struct regcache *reg_cache = cstate->reg_cache; struct opcode *op = &block->opcode_list[offset]; bool load_delay = op_flag_load_delay(op->flags) && !cstate->no_load_delay; @@ -1600,7 +1737,8 @@ static void rec_load_memory(struct lightrec_cstate *cstate, rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0); rt = lightrec_alloc_reg_out(reg_cache, _jit, out_reg, flags); - if (!cstate->state->mirrors_mapped && c.i.imm && !no_mask) { + if ((op->i.op == OP_META_LWU && c.i.imm) + || (!state->mirrors_mapped && c.i.imm && !no_mask)) { jit_addi(rt, rs, (s16)c.i.imm); addr_reg = rt; imm = 0; @@ -1609,13 +1747,16 @@ static void rec_load_memory(struct lightrec_cstate *cstate, imm = (s16)c.i.imm; } + if (op->i.op == OP_META_LWU) + imm = LIGHTNING_UNALIGNED_32BIT; + if (!no_mask) { - jit_andi(rt, addr_reg, addr_mask); + rec_and_mask(cstate, _jit, rt, addr_reg, addr_mask); addr_reg = rt; } if (addr_offset) { - jit_addi(rt, addr_reg, addr_offset); + rec_add_offset(cstate, _jit, rt, addr_reg, addr_offset); addr_reg = rt; } @@ -1679,14 +1820,18 @@ static void rec_load_direct(struct lightrec_cstate *cstate, jit_code_t code, jit_code_t swap_code, bool is_unsigned) { - struct lightrec_state *state = cstate->state; + const struct lightrec_state *state = cstate->state; struct regcache *reg_cache = cstate->reg_cache; struct opcode *op = &block->opcode_list[offset]; bool load_delay = op_flag_load_delay(op->flags) && !cstate->no_load_delay; jit_state_t *_jit = block->_jit; jit_node_t *to_not_ram, *to_not_bios, *to_end, *to_end2; u8 tmp, rs, rt, out_reg, addr_reg, flags = REG_EXT; + bool different_offsets = state->offset_bios != state->offset_scratch; union code c = op->c; + s32 addr_mask; + u32 reg_imm; + s8 offt_reg; s16 imm; if (load_delay || c.i.op == OP_LWC2) @@ -1705,7 +1850,8 @@ static void rec_load_direct(struct lightrec_cstate *cstate, if ((state->offset_ram == state->offset_bios && state->offset_ram == state->offset_scratch && - state->mirrors_mapped) || !c.i.imm) { + state->mirrors_mapped && c.i.op != OP_META_LWU) + || !c.i.imm) { addr_reg = rs; imm = (s16)c.i.imm; } else { @@ -1717,21 +1863,43 @@ static void rec_load_direct(struct lightrec_cstate *cstate, lightrec_free_reg(reg_cache, rs); } + if (op->i.op == OP_META_LWU) + imm = LIGHTNING_UNALIGNED_32BIT; + tmp = lightrec_alloc_reg_temp(reg_cache, _jit); if (state->offset_ram == state->offset_bios && state->offset_ram == state->offset_scratch) { + if (!state->mirrors_mapped) + addr_mask = 0x1f800000 | (RAM_SIZE - 1); + else + addr_mask = 0x1fffffff; + + reg_imm = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, + addr_mask); if (!state->mirrors_mapped) { jit_andi(tmp, addr_reg, BIT(28)); jit_rshi_u(tmp, tmp, 28 - 22); - jit_ori(tmp, tmp, 0x1f800000 | (RAM_SIZE - 1)); + jit_orr(tmp, tmp, reg_imm); jit_andr(rt, addr_reg, tmp); } else { - jit_andi(rt, addr_reg, 0x1fffffff); + jit_andr(rt, addr_reg, reg_imm); } - if (state->offset_ram) - jit_movi(tmp, state->offset_ram); + lightrec_free_reg(reg_cache, reg_imm); + + if (state->offset_ram) { + offt_reg = lightrec_get_reg_with_value(reg_cache, + state->offset_ram); + if (offt_reg < 0) { + jit_movi(tmp, state->offset_ram); + lightrec_temp_set_value(reg_cache, tmp, + state->offset_ram); + } else { + lightrec_free_reg(reg_cache, tmp); + tmp = offt_reg; + } + } } else { to_not_ram = jit_bmsi(addr_reg, BIT(28)); @@ -1745,7 +1913,7 @@ static void rec_load_direct(struct lightrec_cstate *cstate, jit_patch(to_not_ram); - if (state->offset_bios != state->offset_scratch) + if (different_offsets) to_not_bios = jit_bmci(addr_reg, BIT(22)); /* Convert to KUNSEG */ @@ -1753,7 +1921,7 @@ static void rec_load_direct(struct lightrec_cstate *cstate, jit_movi(tmp, state->offset_bios); - if (state->offset_bios != state->offset_scratch) { + if (different_offsets) { to_end2 = jit_b(); jit_patch(to_not_bios); @@ -1875,9 +2043,9 @@ static void rec_LW(struct lightrec_cstate *state, const struct block *block, u16 rec_load(state, block, offset, code, jit_code_bswapr_ui, false); } -static void rec_break_syscall(struct lightrec_cstate *state, - const struct block *block, u16 offset, - u32 exit_code) +static void rec_exit_early(struct lightrec_cstate *state, + const struct block *block, u16 offset, + u32 exit_code, u32 pc) { struct regcache *reg_cache = state->reg_cache; jit_state_t *_jit = block->_jit; @@ -1888,38 +2056,35 @@ static void rec_break_syscall(struct lightrec_cstate *state, tmp = lightrec_alloc_reg_temp(reg_cache, _jit); jit_movi(tmp, exit_code); - jit_stxi_i(offsetof(struct lightrec_state, exit_flags), - LIGHTREC_REG_STATE, tmp); + jit_stxi_i(lightrec_offset(exit_flags), LIGHTREC_REG_STATE, tmp); - jit_ldxi_i(tmp, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, target_cycle)); + jit_ldxi_i(tmp, LIGHTREC_REG_STATE, lightrec_offset(target_cycle)); jit_subr(tmp, tmp, LIGHTREC_REG_CYCLE); jit_movi(LIGHTREC_REG_CYCLE, 0); - jit_stxi_i(offsetof(struct lightrec_state, target_cycle), - LIGHTREC_REG_STATE, tmp); - jit_stxi_i(offsetof(struct lightrec_state, current_cycle), - LIGHTREC_REG_STATE, tmp); + jit_stxi_i(lightrec_offset(target_cycle), LIGHTREC_REG_STATE, tmp); + jit_stxi_i(lightrec_offset(current_cycle), LIGHTREC_REG_STATE, tmp); lightrec_free_reg(reg_cache, tmp); - /* TODO: the return address should be "pc - 4" if we're a delay slot */ - lightrec_emit_end_of_block(state, block, offset, -1, - get_ds_pc(block, offset, 0), - 31, 0, true); + lightrec_emit_end_of_block(state, block, offset, -1, pc, 31, 0, true); } static void rec_special_SYSCALL(struct lightrec_cstate *state, const struct block *block, u16 offset) { _jit_name(block->_jit, __func__); - rec_break_syscall(state, block, offset, LIGHTREC_EXIT_SYSCALL); + + /* TODO: the return address should be "pc - 4" if we're a delay slot */ + rec_exit_early(state, block, offset, LIGHTREC_EXIT_SYSCALL, + get_ds_pc(block, offset, 0)); } static void rec_special_BREAK(struct lightrec_cstate *state, const struct block *block, u16 offset) { _jit_name(block->_jit, __func__); - rec_break_syscall(state, block, offset, LIGHTREC_EXIT_BREAK); + rec_exit_early(state, block, offset, LIGHTREC_EXIT_BREAK, + get_ds_pc(block, offset, 0)); } static void rec_mfc(struct lightrec_cstate *state, const struct block *block, u16 offset) @@ -1969,8 +2134,7 @@ rec_mfc0(struct lightrec_cstate *state, const struct block *block, u16 offset) rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, REG_EXT); - jit_ldxi_i(rt, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, regs.cp0[c.r.rd])); + jit_ldxi_i(rt, LIGHTREC_REG_STATE, lightrec_offset(regs.cp0[c.r.rd])); lightrec_free_reg(reg_cache, rt); } @@ -2021,15 +2185,12 @@ rec_mtc0(struct lightrec_cstate *state, const struct block *block, u16 offset) rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0); - if (c.r.rd != 13) { - jit_stxi_i(offsetof(struct lightrec_state, regs.cp0[c.r.rd]), - LIGHTREC_REG_STATE, rt); - } + if (c.r.rd != 13) + jit_stxi_i(lightrec_offset(regs.cp0[c.r.rd]), LIGHTREC_REG_STATE, rt); if (c.r.rd == 12 || c.r.rd == 13) { tmp = lightrec_alloc_reg_temp(reg_cache, _jit); - jit_ldxi_i(tmp, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, regs.cp0[13])); + jit_ldxi_i(tmp, LIGHTREC_REG_STATE, lightrec_offset(regs.cp0[13])); tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit); } @@ -2042,10 +2203,8 @@ rec_mtc0(struct lightrec_cstate *state, const struct block *block, u16 offset) jit_ori(tmp, tmp, 0x0300); jit_xori(tmp, tmp, 0x0300); jit_orr(tmp, tmp, tmp2); - jit_ldxi_i(tmp2, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, regs.cp0[12])); - jit_stxi_i(offsetof(struct lightrec_state, regs.cp0[13]), - LIGHTREC_REG_STATE, tmp); + jit_ldxi_i(tmp2, LIGHTREC_REG_STATE, lightrec_offset(regs.cp0[12])); + jit_stxi_i(lightrec_offset(regs.cp0[13]), LIGHTREC_REG_STATE, tmp); status = tmp2; } @@ -2073,14 +2232,11 @@ rec_mtc0(struct lightrec_cstate *state, const struct block *block, u16 offset) if (c.r.rd == 12 || c.r.rd == 13) { to_end = jit_beqi(tmp, 0); - jit_ldxi_i(tmp2, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, target_cycle)); + jit_ldxi_i(tmp2, LIGHTREC_REG_STATE, lightrec_offset(target_cycle)); jit_subr(tmp2, tmp2, LIGHTREC_REG_CYCLE); jit_movi(LIGHTREC_REG_CYCLE, 0); - jit_stxi_i(offsetof(struct lightrec_state, target_cycle), - LIGHTREC_REG_STATE, tmp2); - jit_stxi_i(offsetof(struct lightrec_state, current_cycle), - LIGHTREC_REG_STATE, tmp2); + jit_stxi_i(lightrec_offset(target_cycle), LIGHTREC_REG_STATE, tmp2); + jit_stxi_i(lightrec_offset(current_cycle), LIGHTREC_REG_STATE, tmp2); jit_patch(to_end); @@ -2088,7 +2244,7 @@ rec_mtc0(struct lightrec_cstate *state, const struct block *block, u16 offset) if (!op_flag_no_ds(block->opcode_list[offset].flags) && (c.r.rd == 12 || c.r.rd == 13)) { - state->cycles += lightrec_cycles_of_opcode(c); + state->cycles += lightrec_cycles_of_opcode(state->state, c); lightrec_emit_eob(state, block, offset + 1); } } @@ -2123,7 +2279,7 @@ static void rec_cp0_CTC0(struct lightrec_cstate *state, static unsigned int cp2d_i_offset(u8 reg) { - return offsetof(struct lightrec_state, regs.cp2d[reg]); + return lightrec_offset(regs.cp2d[reg]); } static unsigned int cp2d_s_offset(u8 reg) @@ -2133,7 +2289,7 @@ static unsigned int cp2d_s_offset(u8 reg) static unsigned int cp2c_i_offset(u8 reg) { - return offsetof(struct lightrec_state, regs.cp2c[reg]); + return lightrec_offset(regs.cp2c[reg]); } static unsigned int cp2c_s_offset(u8 reg) @@ -2415,8 +2571,7 @@ static void rec_cp0_RFE(struct lightrec_cstate *state, jit_note(__FILE__, __LINE__); status = lightrec_alloc_reg_temp(reg_cache, _jit); - jit_ldxi_i(status, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, regs.cp0[12])); + jit_ldxi_i(status, LIGHTREC_REG_STATE, lightrec_offset(regs.cp0[12])); tmp = lightrec_alloc_reg_temp(reg_cache, _jit); @@ -2426,10 +2581,8 @@ static void rec_cp0_RFE(struct lightrec_cstate *state, jit_andi(status, status, ~0xful); jit_orr(status, status, tmp); - jit_ldxi_i(tmp, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, regs.cp0[13])); - jit_stxi_i(offsetof(struct lightrec_state, regs.cp0[12]), - LIGHTREC_REG_STATE, status); + jit_ldxi_i(tmp, LIGHTREC_REG_STATE, lightrec_offset(regs.cp0[13])); + jit_stxi_i(lightrec_offset(regs.cp0[12]), LIGHTREC_REG_STATE, status); /* Exit dynarec in case there's a software interrupt. * exit_flags = !!(status & cause & 0x0300) & status; */ @@ -2437,8 +2590,7 @@ static void rec_cp0_RFE(struct lightrec_cstate *state, jit_andi(tmp, tmp, 0x0300); jit_nei(tmp, tmp, 0); jit_andr(tmp, tmp, status); - jit_stxi_i(offsetof(struct lightrec_state, exit_flags), - LIGHTREC_REG_STATE, tmp); + jit_stxi_i(lightrec_offset(exit_flags), LIGHTREC_REG_STATE, tmp); lightrec_free_reg(reg_cache, status); lightrec_free_reg(reg_cache, tmp); @@ -2464,24 +2616,34 @@ static void rec_meta_MOV(struct lightrec_cstate *state, union code c = op->c; jit_state_t *_jit = block->_jit; bool unload_rd; + bool unload_rs, discard_rs; u8 rs, rd; _jit_name(block->_jit, __func__); jit_note(__FILE__, __LINE__); + unload_rs = OPT_EARLY_UNLOAD + && LIGHTREC_FLAGS_GET_RS(op->flags) == LIGHTREC_REG_UNLOAD; + discard_rs = OPT_EARLY_UNLOAD + && LIGHTREC_FLAGS_GET_RS(op->flags) == LIGHTREC_REG_DISCARD; + + if ((unload_rs || discard_rs) && c.m.rs) { + /* If the source register is going to be unloaded or discarded, + * then we can simply mark its host register as now pointing to + * the destination register. */ + pr_debug("Remap %s to %s at offset 0x%x\n", + lightrec_reg_name(c.m.rs), lightrec_reg_name(c.m.rd), + offset << 2); + rs = lightrec_alloc_reg_in(reg_cache, _jit, c.m.rs, 0); + lightrec_remap_reg(reg_cache, _jit, rs, c.m.rd, discard_rs); + lightrec_free_reg(reg_cache, rs); + return; + } + unload_rd = OPT_EARLY_UNLOAD && LIGHTREC_FLAGS_GET_RD(op->flags) == LIGHTREC_REG_UNLOAD; - if (c.m.rs && !lightrec_reg_is_loaded(reg_cache, c.m.rs)) { - /* The source register is not yet loaded - we can load its value - * from the register cache directly into the target register. */ - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.m.rd, REG_EXT); - - jit_ldxi_i(rd, LIGHTREC_REG_STATE, - offsetof(struct lightrec_state, regs.gpr) + (c.m.rs << 2)); - - lightrec_free_reg(reg_cache, rd); - } else if (unload_rd) { + if (unload_rd) { /* If the destination register will be unloaded right after the * MOV meta-opcode, we don't actually need to write any host * register - we can just store the source register directly to @@ -2491,8 +2653,7 @@ static void rec_meta_MOV(struct lightrec_cstate *state, rs = lightrec_alloc_reg_in(reg_cache, _jit, c.m.rs, 0); - jit_stxi_i(offsetof(struct lightrec_state, regs.gpr) - + (c.m.rd << 2), LIGHTREC_REG_STATE, rs); + jit_stxi_i(lightrec_offset(regs.gpr) + (c.m.rd << 2), LIGHTREC_REG_STATE, rs); lightrec_free_reg(reg_cache, rs); } else { @@ -2524,8 +2685,8 @@ static void rec_meta_EXTC_EXTS(struct lightrec_cstate *state, _jit_name(block->_jit, __func__); jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.m.rs, 0); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.m.rd, REG_EXT); + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.m.rs, c.m.rd, 0, REG_EXT, &rs, &rd); if (c.m.op == OP_META_EXTC) jit_extr_c(rd, rs); @@ -2583,12 +2744,19 @@ static void rec_meta_MULT2(struct lightrec_cstate *state, hi = lightrec_alloc_reg_out(reg_cache, _jit, reg_hi, hiflags); - if (c.r.op >= 32) + if (c.r.op >= 32) { jit_lshi(hi, rs, c.r.op - 32); - else if (is_signed) - jit_rshi(hi, rs, 32 - c.r.op); - else - jit_rshi_u(hi, rs, 32 - c.r.op); + } else if (is_signed) { + if (c.r.op) + jit_rshi(hi, rs, 32 - c.r.op); + else + jit_rshi(hi, rs, 31); + } else { + if (c.r.op) + jit_rshi_u(hi, rs, 32 - c.r.op); + else + jit_movi(hi, 0); + } lightrec_free_reg(reg_cache, hi); } @@ -2609,8 +2777,9 @@ static void rec_meta_COM(struct lightrec_cstate *state, u8 rd, rs, flags; jit_note(__FILE__, __LINE__); - rs = lightrec_alloc_reg_in(reg_cache, _jit, c.m.rs, 0); - rd = lightrec_alloc_reg_out(reg_cache, _jit, c.m.rd, 0); + + rec_alloc_rs_rd(reg_cache, _jit, &block->opcode_list[offset], + c.m.rs, c.m.rd, 0, 0, &rs, &rd); flags = lightrec_get_reg_in_flags(reg_cache, rs); @@ -2623,6 +2792,36 @@ static void rec_meta_COM(struct lightrec_cstate *state, lightrec_free_reg(reg_cache, rd); } +static void rec_meta_LWU(struct lightrec_cstate *state, + const struct block *block, + u16 offset) +{ + jit_code_t code; + + if (is_big_endian() && __WORDSIZE == 64) + code = jit_code_unldr_u; + else + code = jit_code_unldr; + + _jit_name(block->_jit, __func__); + rec_load(state, block, offset, code, jit_code_bswapr_ui, false); +} + +static void rec_meta_SWU(struct lightrec_cstate *state, + const struct block *block, + u16 offset) +{ + _jit_name(block->_jit, __func__); + rec_store(state, block, offset, jit_code_unstr, jit_code_bswapr_ui); +} + +static void unknown_opcode(struct lightrec_cstate *state, + const struct block *block, u16 offset) +{ + rec_exit_early(state, block, offset, LIGHTREC_EXIT_UNKNOWN_OP, + block->pc + (offset << 2)); +} + static const lightrec_rec_func_t rec_standard[64] = { SET_DEFAULT_ELM(rec_standard, unknown_opcode), [OP_SPECIAL] = rec_SPECIAL, @@ -2661,6 +2860,8 @@ static const lightrec_rec_func_t rec_standard[64] = { [OP_META] = rec_META, [OP_META_MULT2] = rec_meta_MULT2, [OP_META_MULTU2] = rec_meta_MULT2, + [OP_META_LWU] = rec_meta_LWU, + [OP_META_SWU] = rec_meta_SWU, }; static const lightrec_rec_func_t rec_special[64] = {