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=e1a176c354a2def5dd25f0e81a0b42562186ab2d;hpb=d1c67a8c131d618949fc3dedabf393ebeb7d195b;p=pcsx_rearmed.git diff --git a/deps/lightrec/emitter.c b/deps/lightrec/emitter.c index e1a176c3..f84f049f 100644 --- a/deps/lightrec/emitter.c +++ b/deps/lightrec/emitter.c @@ -3,6 +3,7 @@ * Copyright (C) 2014-2021 Paul Cercueil */ +#include "arch.h" #include "blockcache.h" #include "debug.h" #include "disassembler.h" @@ -103,7 +104,7 @@ static void lightrec_emit_end_of_block(struct lightrec_cstate *state, if (cycles && update_cycles) { jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, cycles); - pr_debug("EOB: %u cycles\n", cycles); + pr_debug("EOB: %"PRIu32" cycles\n", cycles); } if (has_ds && op_flag_load_delay(ds->flags) @@ -136,8 +137,7 @@ void lightrec_emit_jump_to_interpreter(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(offsetof(struct lightrec_state, next_pc), - LIGHTREC_REG_STATE, JIT_V0); + jit_stxi_i(lightrec_offset(next_pc), LIGHTREC_REG_STATE, JIT_V0); } jit_movi(JIT_V1, (uintptr_t)block); @@ -157,8 +157,7 @@ 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(offsetof(struct lightrec_state, next_pc), - LIGHTREC_REG_STATE, JIT_V0); + jit_stxi_i(lightrec_offset(next_pc), LIGHTREC_REG_STATE, JIT_V0); } jit_subi(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, state->cycles); @@ -249,11 +248,11 @@ static void rec_b(struct lightrec_cstate *state, const struct block *block, u16 struct lightrec_branch *branch; const struct opcode *op = &block->opcode_list[offset], *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(state->state, op->c); u32 target_offset, cycles = state->cycles + op_cycles; bool no_indirection = false; + jit_node_t *addr = NULL; u32 next_pc; u8 rs, rt; @@ -310,7 +309,7 @@ static void rec_b(struct lightrec_cstate *state, const struct block *block, u16 target_offset = offset + 1 + (s16)op->i.imm - !!op_flag_no_ds(op->flags); - pr_debug("Adding local branch to offset 0x%x\n", + pr_debug("Adding local branch to offset 0x%"PRIx32"\n", target_offset << 2); branch = &state->local_branches[ state->nb_local_branches++]; @@ -943,7 +942,7 @@ static void rec_alu_mult(struct lightrec_cstate *state, u8 reg_lo = get_mult_div_lo(c); u8 reg_hi = get_mult_div_hi(c); jit_state_t *_jit = block->_jit; - u8 lo, hi, rs, rt, rflags = 0; + u8 lo, hi = 0, rs, rt, rflags = 0; bool no_lo = op_flag_no_lo(flags); bool no_hi = op_flag_no_hi(flags); @@ -1175,15 +1174,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); @@ -1232,8 +1232,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); } @@ -1277,10 +1277,16 @@ static void rec_and_mask(struct lightrec_cstate *cstate, 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); + if (arch_has_fast_mask() + && (is_low_mask(mask) || is_high_mask(mask))) { + jit_andi(reg_out, reg_in, mask); + } else { + 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); + lightrec_free_reg(reg_cache, reg_imm); + } } static void rec_store_memory(struct lightrec_cstate *cstate, @@ -1294,10 +1300,10 @@ static void rec_store_memory(struct lightrec_cstate *cstate, struct opcode *op = &block->opcode_list[offset]; jit_state_t *_jit = block->_jit; union code c = op->c; - u8 rs, rt, tmp = 0, tmp2 = 0, tmp3, addr_reg, addr_reg2; + u8 rs, rt, tmp = 0, tmp2 = 0, tmp3, addr_reg, addr_reg2, src_reg; 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 && (c.i.op == OP_META_SWU @@ -1336,25 +1342,23 @@ static void rec_store_memory(struct lightrec_cstate *cstate, } rt = lightrec_alloc_reg_in(reg_cache, _jit, in_reg, 0); + src_reg = rt; if (is_big_endian() && swap_code && in_reg) { tmp3 = lightrec_alloc_reg_temp(reg_cache, _jit); jit_new_node_ww(swap_code, tmp3, rt); - 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); + lightrec_free_reg(reg_cache, rt); + src_reg = tmp3; } - lightrec_free_reg(reg_cache, rt); + if (c.i.op == OP_META_SWU) + jit_unstr(addr_reg2, src_reg, LIGHTNING_UNALIGNED_32BIT); + else + jit_new_node_www(code, imm, addr_reg2, src_reg); + + lightrec_free_reg(reg_cache, src_reg); if (invalidate) { tmp3 = lightrec_alloc_reg_in(reg_cache, _jit, 0, 0); @@ -1439,7 +1443,7 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate, jit_state_t *_jit = block->_jit; jit_node_t *to_not_ram, *to_end; bool swc2 = c.i.op == OP_SWC2; - u8 addr_reg, tmp, tmp2 = 0, rs, rt, in_reg = swc2 ? REG_TEMP : c.i.rt; + u8 addr_reg, tmp, tmp2 = 0, rs, rt, src_reg, in_reg = swc2 ? REG_TEMP : c.i.rt; s16 imm; jit_note(__FILE__, __LINE__); @@ -1483,25 +1487,23 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate, } rt = lightrec_alloc_reg_in(reg_cache, _jit, in_reg, 0); + src_reg = rt; if (is_big_endian() && swap_code && in_reg) { tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit); jit_new_node_ww(swap_code, tmp2, rt); + src_reg = 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); + lightrec_free_reg(reg_cache, rt); } - lightrec_free_reg(reg_cache, rt); + if (c.i.op == OP_META_SWU) + jit_unstr(tmp, src_reg, LIGHTNING_UNALIGNED_32BIT); + else + jit_new_node_www(code, imm, tmp, src_reg); + + lightrec_free_reg(reg_cache, src_reg); lightrec_free_reg(reg_cache, tmp); } @@ -1515,7 +1517,7 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block jit_state_t *_jit = block->_jit; jit_node_t *to_not_ram, *to_end; bool swc2 = c.i.op == OP_SWC2; - u8 addr_reg, tmp, tmp2, tmp3, rs, rt, reg_imm; + u8 src_reg, 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; @@ -1560,19 +1562,18 @@ 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 (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(offsetof(struct lightrec_state, code_lut) + 4, - tmp, tmp3); + jit_stxi_i(lightrec_offset(code_lut) + 4, tmp, tmp3); } else { - jit_stxi(offsetof(struct lightrec_state, code_lut) - + sizeof(uintptr_t), tmp, tmp3); + jit_stxi(lightrec_offset(code_lut) + sizeof(uintptr_t), + tmp, tmp3); } } @@ -1597,25 +1598,23 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block lightrec_free_reg(reg_cache, reg_imm); rt = lightrec_alloc_reg_in(reg_cache, _jit, in_reg, 0); + src_reg = rt; if (is_big_endian() && swap_code && in_reg) { tmp = lightrec_alloc_reg_temp(reg_cache, _jit); jit_new_node_ww(swap_code, tmp, rt); + src_reg = 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); + lightrec_free_reg(reg_cache, rt); } - lightrec_free_reg(reg_cache, rt); + if (c.i.op == OP_META_SWU) + jit_unstr(tmp2, src_reg, LIGHTNING_UNALIGNED_32BIT); + else + jit_new_node_www(code, 0, tmp2, src_reg); + + lightrec_free_reg(reg_cache, src_reg); lightrec_free_reg(reg_cache, tmp2); } @@ -1877,19 +1876,19 @@ static void rec_load_direct(struct lightrec_cstate *cstate, else addr_mask = 0x1fffffff; - reg_imm = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, - addr_mask); if (!state->mirrors_mapped) { + reg_imm = lightrec_alloc_reg_temp_with_value(reg_cache, _jit, + addr_mask); jit_andi(tmp, addr_reg, BIT(28)); jit_rshi_u(tmp, tmp, 28 - 22); jit_orr(tmp, tmp, reg_imm); jit_andr(rt, addr_reg, tmp); + + lightrec_free_reg(reg_cache, reg_imm); } else { - jit_andr(rt, addr_reg, reg_imm); + rec_and_mask(cstate, _jit, rt, addr_reg, addr_mask); } - lightrec_free_reg(reg_cache, reg_imm); - if (state->offset_ram) { offt_reg = lightrec_get_reg_with_value(reg_cache, state->offset_ram); @@ -2058,17 +2057,13 @@ static void rec_exit_early(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); @@ -2140,8 +2135,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); } @@ -2192,15 +2186,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); } @@ -2213,10 +2204,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; } @@ -2244,14 +2233,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); @@ -2294,7 +2280,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) @@ -2304,7 +2290,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) @@ -2586,8 +2572,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); @@ -2597,10 +2582,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; */ @@ -2608,8 +2591,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); @@ -2662,16 +2644,7 @@ static void rec_meta_MOV(struct lightrec_cstate *state, 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 @@ -2681,8 +2654,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 {