+ C_WRAPPER_RW_GENERIC);
+ }
+}
+
+static u32 rec_ram_mask(struct lightrec_state *state)
+{
+ return (RAM_SIZE << (state->mirrors_mapped * 2)) - 1;
+}
+
+static u32 rec_io_mask(const struct lightrec_state *state)
+{
+ u32 length = state->maps[PSX_MAP_HW_REGISTERS].length;
+
+ return GENMASK(31 - clz32(length - 1), 0);
+}
+
+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)
+{
+ 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;
+ s16 imm = (s16)c.i.imm;
+ s32 simm = (s32)imm << (1 - lut_is_32bit(state));
+ s32 lut_offt = offsetof(struct lightrec_state, code_lut);
+ bool no_mask = op_flag_no_mask(op->flags);
+ bool add_imm = c.i.imm &&
+ ((!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 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);
+
+ addr_reg = rs;
+
+ if (add_imm) {
+ jit_addi(tmp, addr_reg, (s16)c.i.imm);
+ addr_reg = tmp;
+ imm = 0;
+ } else if (simm) {
+ lut_offt += simm;
+ }
+
+ if (!no_mask) {
+ jit_andi(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);
+ addr_reg2 = tmp2;
+ } else {
+ addr_reg2 = addr_reg;
+ }
+
+ 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);
+
+ lightrec_free_reg(reg_cache, tmp3);
+ } else {
+ jit_new_node_www(code, imm, addr_reg2, rt);
+ }
+
+ lightrec_free_reg(reg_cache, rt);
+
+ if (invalidate) {
+ tmp3 = lightrec_alloc_reg_in(reg_cache, _jit, 0, 0);
+
+ if (c.i.op != OP_SW) {
+ jit_andi(tmp, addr_reg, ~3);
+ addr_reg = tmp;
+ }
+
+ if (!lut_is_32bit(state)) {
+ jit_lshi(tmp, addr_reg, 1);
+ addr_reg = tmp;
+ }
+
+ if (addr_reg == rs && c.i.rs == 0) {
+ addr_reg = LIGHTREC_REG_STATE;
+ } else {
+ jit_add_state(tmp, addr_reg);
+ addr_reg = tmp;
+ }
+
+ if (lut_is_32bit(state))
+ jit_stxi_i(lut_offt, addr_reg, tmp3);
+ else
+ jit_stxi(lut_offt, addr_reg, tmp3);
+
+ lightrec_free_reg(reg_cache, tmp3);