git subrepo pull --force deps/lightrec
[pcsx_rearmed.git] / deps / lightrec / emitter.c
index fa74cc0..fd28935 100644 (file)
@@ -392,11 +392,34 @@ static void rec_alu_shiftv(struct lightrec_cstate *state, const struct block *bl
        lightrec_free_reg(reg_cache, rd);
 }
 
+static void rec_movi(struct lightrec_cstate *state,
+                    const struct block *block, u16 offset)
+{
+       struct regcache *reg_cache = state->reg_cache;
+       union code c = block->opcode_list[offset].c;
+       jit_state_t *_jit = block->_jit;
+       u16 flags = REG_EXT;
+       u8 rt;
+
+       if (!(c.i.imm & 0x8000))
+               flags |= REG_ZEXT;
+
+       rt = lightrec_alloc_reg_out(reg_cache, _jit, c.i.rt, flags);
+
+       jit_movi(rt, (s32)(s16) c.i.imm);
+
+       lightrec_free_reg(reg_cache, rt);
+}
+
 static void rec_ADDIU(struct lightrec_cstate *state,
                      const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_alu_imm(state, block, offset, jit_code_addi, false);
+
+       if (block->opcode_list[offset].c.i.rs)
+               rec_alu_imm(state, block, offset, jit_code_addi, false);
+       else
+               rec_movi(state, block, offset);
 }
 
 static void rec_ADDI(struct lightrec_cstate *state,
@@ -404,7 +427,7 @@ static void rec_ADDI(struct lightrec_cstate *state,
 {
        /* TODO: Handle the exception? */
        _jit_name(block->_jit, __func__);
-       rec_alu_imm(state, block, offset, jit_code_addi, false);
+       rec_ADDIU(state, block, offset);
 }
 
 static void rec_SLTIU(struct lightrec_cstate *state,
@@ -1022,22 +1045,31 @@ static void rec_io(struct lightrec_cstate *state,
        }
 }
 
+static u32 rec_ram_mask(struct lightrec_state *state)
+{
+       return (RAM_SIZE << (state->mirrors_mapped * 2)) - 1;
+}
+
 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 << (__WORDSIZE / 32 - 1);
+       s32 simm = (s32)imm << (1 - lut_is_32bit(state));
        s32 lut_offt = offsetof(struct lightrec_state, code_lut);
        bool no_mask = op->flags & LIGHTREC_NO_MASK;
-       bool add_imm = c.i.imm && invalidate && simm + lut_offt != (s16)(simm + lut_offt);
+       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;
        bool need_tmp2 = addr_offset || invalidate;
 
@@ -1071,7 +1103,17 @@ static void rec_store_memory(struct lightrec_cstate *cstate,
                addr_reg2 = addr_reg;
        }
 
-       jit_new_node_www(code, imm, addr_reg2, rt);
+       if (is_big_endian() && swap_code && c.i.rt) {
+               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) {
@@ -1082,17 +1124,22 @@ static void rec_store_memory(struct lightrec_cstate *cstate,
                        addr_reg = tmp2;
                }
 
-               if (__WORDSIZE == 64) {
+               if (!lut_is_32bit(state)) {
                        jit_lshi(tmp2, addr_reg, 1);
                        addr_reg = tmp2;
                }
 
-               if (__WORDSIZE == 64 || addr_reg != rs || c.i.rs != 0) {
+               if (addr_reg == rs && c.i.rs == 0) {
+                       addr_reg = LIGHTREC_REG_STATE;
+               } else {
                        jit_addr(tmp2, addr_reg, LIGHTREC_REG_STATE);
                        addr_reg = tmp2;
                }
 
-               jit_stxi(lut_offt, addr_reg, tmp3);
+               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);
        }
@@ -1107,29 +1154,32 @@ static void rec_store_memory(struct lightrec_cstate *cstate,
 static void rec_store_ram(struct lightrec_cstate *cstate,
                          const struct block *block,
                          u16 offset, jit_code_t code,
-                         bool invalidate)
+                         jit_code_t swap_code, bool invalidate)
 {
+       struct lightrec_state *state = cstate->state;
+
        _jit_note(block->_jit, __FILE__, __LINE__);
 
-       return rec_store_memory(cstate, block, offset, code,
-                               cstate->state->offset_ram,
-                               RAM_SIZE - 1, invalidate);
+       return rec_store_memory(cstate, block, offset, code, swap_code,
+                               state->offset_ram, rec_ram_mask(state),
+                               invalidate);
 }
 
 static void rec_store_scratch(struct lightrec_cstate *cstate,
-                             const struct block *block,
-                             u16 offset, jit_code_t code)
+                             const struct block *block, u16 offset,
+                             jit_code_t code, jit_code_t swap_code)
 {
        _jit_note(block->_jit, __FILE__, __LINE__);
 
-       return rec_store_memory(cstate, block, offset, code,
+       return rec_store_memory(cstate, block, offset, code, swap_code,
                                cstate->state->offset_scratch,
                                0x1fffffff, false);
 }
 
 static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate,
                                           const struct block *block,
-                                          u16 offset, jit_code_t code)
+                                          u16 offset, jit_code_t code,
+                                          jit_code_t swap_code)
 {
        struct lightrec_state *state = cstate->state;
        struct regcache *reg_cache = cstate->reg_cache;
@@ -1181,14 +1231,24 @@ static void rec_store_direct_no_invalidate(struct lightrec_cstate *cstate,
        }
 
        rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0);
-       jit_new_node_www(code, imm, tmp, rt);
+
+       if (is_big_endian() && swap_code && c.i.rt) {
+               tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
+
+               jit_new_node_ww(swap_code, tmp2, rt);
+               jit_new_node_www(code, imm, tmp, tmp2);
+
+               lightrec_free_reg(reg_cache, tmp2);
+       } else {
+               jit_new_node_www(code, imm, tmp, rt);
+       }
 
        lightrec_free_reg(reg_cache, rt);
        lightrec_free_reg(reg_cache, tmp);
 }
 
 static void rec_store_direct(struct lightrec_cstate *cstate, const struct block *block,
-                            u16 offset, jit_code_t code)
+                            u16 offset, jit_code_t code, jit_code_t swap_code)
 {
        struct lightrec_state *state = cstate->state;
        u32 ram_size = state->mirrors_mapped ? RAM_SIZE * 4 : RAM_SIZE;
@@ -1219,12 +1279,15 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block
 
        /* Compute the offset to the code LUT */
        jit_andi(tmp, tmp2, (RAM_SIZE - 1) & ~3);
-       if (__WORDSIZE == 64)
+       if (!lut_is_32bit(state))
                jit_lshi(tmp, tmp, 1);
        jit_addr(tmp, LIGHTREC_REG_STATE, tmp);
 
        /* Write NULL to the code LUT to invalidate any block that's there */
-       jit_stxi(offsetof(struct lightrec_state, code_lut), tmp, tmp3);
+       if (lut_is_32bit(state))
+               jit_stxi_i(offsetof(struct lightrec_state, code_lut), tmp, tmp3);
+       else
+               jit_stxi(offsetof(struct lightrec_state, code_lut), tmp, tmp3);
 
        if (state->offset_ram != state->offset_scratch) {
                jit_movi(tmp, state->offset_ram);
@@ -1247,14 +1310,25 @@ static void rec_store_direct(struct lightrec_cstate *cstate, const struct block
        lightrec_free_reg(reg_cache, tmp3);
 
        rt = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rt, 0);
-       jit_new_node_www(code, 0, tmp2, rt);
+
+       if (is_big_endian() && swap_code && c.i.rt) {
+               tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
+
+               jit_new_node_ww(swap_code, tmp, rt);
+               jit_new_node_www(code, 0, tmp2, tmp);
+
+               lightrec_free_reg(reg_cache, tmp);
+       } else {
+               jit_new_node_www(code, 0, tmp2, rt);
+       }
 
        lightrec_free_reg(reg_cache, rt);
        lightrec_free_reg(reg_cache, tmp2);
 }
 
 static void rec_store(struct lightrec_cstate *state,
-                     const struct block *block, u16 offset, jit_code_t code)
+                     const struct block *block, u16 offset,
+                     jit_code_t code, jit_code_t swap_code)
 {
        u16 flags = block->opcode_list[offset].flags;
        bool no_invalidate = (flags & LIGHTREC_NO_INVALIDATE) ||
@@ -1262,16 +1336,19 @@ static void rec_store(struct lightrec_cstate *state,
 
        switch (LIGHTREC_FLAGS_GET_IO_MODE(flags)) {
        case LIGHTREC_IO_RAM:
-               rec_store_ram(state, block, offset, code, !no_invalidate);
+               rec_store_ram(state, block, offset, code,
+                             swap_code, !no_invalidate);
                break;
        case LIGHTREC_IO_SCRATCH:
-               rec_store_scratch(state, block, offset, code);
+               rec_store_scratch(state, block, offset, code, swap_code);
                break;
        case LIGHTREC_IO_DIRECT:
-               if (no_invalidate)
-                       rec_store_direct_no_invalidate(state, block, offset, code);
-               else
-                       rec_store_direct(state, block, offset, code);
+               if (no_invalidate) {
+                       rec_store_direct_no_invalidate(state, block, offset,
+                                                      code, swap_code);
+               } else {
+                       rec_store_direct(state, block, offset, code, swap_code);
+               }
                break;
        default:
                rec_io(state, block, offset, true, false);
@@ -1283,14 +1360,15 @@ static void rec_SB(struct lightrec_cstate *state,
                   const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_store(state, block, offset, jit_code_stxi_c);
+       rec_store(state, block, offset, jit_code_stxi_c, 0);
 }
 
 static void rec_SH(struct lightrec_cstate *state,
                   const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_store(state, block, offset, jit_code_stxi_s);
+       rec_store(state, block, offset,
+                 jit_code_stxi_s, jit_code_bswapr_us);
 }
 
 static void rec_SW(struct lightrec_cstate *state,
@@ -1298,7 +1376,8 @@ static void rec_SW(struct lightrec_cstate *state,
 
 {
        _jit_name(block->_jit, __func__);
-       rec_store(state, block, offset, jit_code_stxi_i);
+       rec_store(state, block, offset,
+                 jit_code_stxi_i, jit_code_bswapr_ui);
 }
 
 static void rec_SWL(struct lightrec_cstate *state,
@@ -1323,15 +1402,17 @@ static void rec_SWC2(struct lightrec_cstate *state,
 }
 
 static void rec_load_memory(struct lightrec_cstate *cstate,
-                           const struct block *block,
-                           u16 offset, jit_code_t code, bool is_unsigned,
+                           const struct block *block, u16 offset,
+                           jit_code_t code, jit_code_t swap_code, bool is_unsigned,
                            uintptr_t addr_offset, u32 addr_mask)
 {
        struct regcache *reg_cache = cstate->reg_cache;
        struct opcode *op = &block->opcode_list[offset];
        jit_state_t *_jit = block->_jit;
        u8 rs, rt, addr_reg, flags = REG_EXT;
+       bool no_mask = op->flags & LIGHTREC_NO_MASK;
        union code c = op->c;
+       s16 imm;
 
        if (!c.i.rt)
                return;
@@ -1342,11 +1423,18 @@ 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, c.i.rt, flags);
 
-       if (!(op->flags & LIGHTREC_NO_MASK)) {
-               jit_andi(rt, rs, addr_mask);
+       if (!cstate->state->mirrors_mapped && c.i.imm && !no_mask) {
+               jit_addi(rt, rs, (s16)c.i.imm);
                addr_reg = rt;
+               imm = 0;
        } else {
                addr_reg = rs;
+               imm = (s16)c.i.imm;
+       }
+
+       if (!no_mask) {
+               jit_andi(rt, addr_reg, addr_mask);
+               addr_reg = rt;
        }
 
        if (addr_offset) {
@@ -1354,44 +1442,55 @@ static void rec_load_memory(struct lightrec_cstate *cstate,
                addr_reg = rt;
        }
 
-       jit_new_node_www(code, rt, addr_reg, (s16)c.i.imm);
+       jit_new_node_www(code, rt, addr_reg, imm);
+
+       if (is_big_endian() && swap_code) {
+               jit_new_node_ww(swap_code, rt, rt);
+
+               if (c.i.op == OP_LH)
+                       jit_extr_s(rt, rt);
+               else if (c.i.op == OP_LW && __WORDSIZE == 64)
+                       jit_extr_i(rt, rt);
+       }
 
        lightrec_free_reg(reg_cache, rs);
        lightrec_free_reg(reg_cache, rt);
 }
 
 static void rec_load_ram(struct lightrec_cstate *cstate,
-                        const struct block *block,
-                        u16 offset, jit_code_t code, bool is_unsigned)
+                        const struct block *block, u16 offset,
+                        jit_code_t code, jit_code_t swap_code, bool is_unsigned)
 {
        _jit_note(block->_jit, __FILE__, __LINE__);
 
-       rec_load_memory(cstate, block, offset, code, is_unsigned,
-                       cstate->state->offset_ram, RAM_SIZE - 1);
+       rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
+                       cstate->state->offset_ram, rec_ram_mask(cstate->state));
 }
 
 static void rec_load_bios(struct lightrec_cstate *cstate,
-                         const struct block *block,
-                         u16 offset, jit_code_t code, bool is_unsigned)
+                         const struct block *block, u16 offset,
+                         jit_code_t code, jit_code_t swap_code, bool is_unsigned)
 {
        _jit_note(block->_jit, __FILE__, __LINE__);
 
-       rec_load_memory(cstate, block, offset, code, is_unsigned,
+       rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
                        cstate->state->offset_bios, 0x1fffffff);
 }
 
 static void rec_load_scratch(struct lightrec_cstate *cstate,
-                            const struct block *block,
-                            u16 offset, jit_code_t code, bool is_unsigned)
+                            const struct block *block, u16 offset,
+                            jit_code_t code, jit_code_t swap_code, bool is_unsigned)
 {
        _jit_note(block->_jit, __FILE__, __LINE__);
 
-       rec_load_memory(cstate, block, offset, code, is_unsigned,
+       rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
                        cstate->state->offset_scratch, 0x1fffffff);
 }
 
-static void rec_load_direct(struct lightrec_cstate *cstate, const struct block *block,
-                           u16 offset, jit_code_t code, bool is_unsigned)
+static void rec_load_direct(struct lightrec_cstate *cstate,
+                           const struct block *block, u16 offset,
+                           jit_code_t code, jit_code_t swap_code,
+                           bool is_unsigned)
 {
        struct lightrec_state *state = cstate->state;
        struct regcache *reg_cache = cstate->reg_cache;
@@ -1483,28 +1582,38 @@ static void rec_load_direct(struct lightrec_cstate *cstate, const struct block *
 
        jit_new_node_www(code, rt, rt, imm);
 
+       if (is_big_endian() && swap_code) {
+               jit_new_node_ww(swap_code, rt, rt);
+
+               if (c.i.op == OP_LH)
+                       jit_extr_s(rt, rt);
+               else if (c.i.op == OP_LW && __WORDSIZE == 64)
+                       jit_extr_i(rt, rt);
+       }
+
        lightrec_free_reg(reg_cache, addr_reg);
        lightrec_free_reg(reg_cache, rt);
        lightrec_free_reg(reg_cache, tmp);
 }
 
 static void rec_load(struct lightrec_cstate *state, const struct block *block,
-                    u16 offset, jit_code_t code, bool is_unsigned)
+                    u16 offset, jit_code_t code, jit_code_t swap_code,
+                    bool is_unsigned)
 {
        u16 flags = block->opcode_list[offset].flags;
 
        switch (LIGHTREC_FLAGS_GET_IO_MODE(flags)) {
        case LIGHTREC_IO_RAM:
-               rec_load_ram(state, block, offset, code, is_unsigned);
+               rec_load_ram(state, block, offset, code, swap_code, is_unsigned);
                break;
        case LIGHTREC_IO_BIOS:
-               rec_load_bios(state, block, offset, code, is_unsigned);
+               rec_load_bios(state, block, offset, code, swap_code, is_unsigned);
                break;
        case LIGHTREC_IO_SCRATCH:
-               rec_load_scratch(state, block, offset, code, is_unsigned);
+               rec_load_scratch(state, block, offset, code, swap_code, is_unsigned);
                break;
        case LIGHTREC_IO_DIRECT:
-               rec_load_direct(state, block, offset, code, is_unsigned);
+               rec_load_direct(state, block, offset, code, swap_code, is_unsigned);
                break;
        default:
                rec_io(state, block, offset, false, true);
@@ -1515,25 +1624,25 @@ static void rec_load(struct lightrec_cstate *state, const struct block *block,
 static void rec_LB(struct lightrec_cstate *state, const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_load(state, block, offset, jit_code_ldxi_c, false);
+       rec_load(state, block, offset, jit_code_ldxi_c, 0, false);
 }
 
 static void rec_LBU(struct lightrec_cstate *state, const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_load(state, block, offset, jit_code_ldxi_uc, true);
+       rec_load(state, block, offset, jit_code_ldxi_uc, 0, true);
 }
 
 static void rec_LH(struct lightrec_cstate *state, const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_load(state, block, offset, jit_code_ldxi_s, false);
+       rec_load(state, block, offset, jit_code_ldxi_s, jit_code_bswapr_us, false);
 }
 
 static void rec_LHU(struct lightrec_cstate *state, const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_load(state, block, offset, jit_code_ldxi_us, true);
+       rec_load(state, block, offset, jit_code_ldxi_us, jit_code_bswapr_us, true);
 }
 
 static void rec_LWL(struct lightrec_cstate *state, const struct block *block, u16 offset)
@@ -1551,7 +1660,7 @@ static void rec_LWR(struct lightrec_cstate *state, const struct block *block, u1
 static void rec_LW(struct lightrec_cstate *state, const struct block *block, u16 offset)
 {
        _jit_name(block->_jit, __func__);
-       rec_load(state, block, offset, jit_code_ldxi_i, false);
+       rec_load(state, block, offset, jit_code_ldxi_i, jit_code_bswapr_ui, false);
 }
 
 static void rec_LWC2(struct lightrec_cstate *state, const struct block *block, u16 offset)
@@ -1759,6 +1868,26 @@ static void rec_cp0_CTC0(struct lightrec_cstate *state,
        rec_mtc0(state, block, offset);
 }
 
+static unsigned int cp2d_i_offset(u8 reg)
+{
+       return offsetof(struct lightrec_state, regs.cp2d[reg]);
+}
+
+static unsigned int cp2d_s_offset(u8 reg)
+{
+       return cp2d_i_offset(reg) + is_big_endian() * 2;
+}
+
+static unsigned int cp2c_i_offset(u8 reg)
+{
+       return offsetof(struct lightrec_state, regs.cp2c[reg]);
+}
+
+static unsigned int cp2c_s_offset(u8 reg)
+{
+       return cp2c_i_offset(reg) + is_big_endian() * 2;
+}
+
 static void rec_cp2_basic_MFC2(struct lightrec_cstate *state,
                               const struct block *block, u16 offset)
 {
@@ -1783,16 +1912,14 @@ static void rec_cp2_basic_MFC2(struct lightrec_cstate *state,
        case 9:
        case 10:
        case 11:
-               jit_ldxi_s(rt, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2d[reg]));
+               jit_ldxi_s(rt, LIGHTREC_REG_STATE, cp2d_s_offset(reg));
                break;
        case 7:
        case 16:
        case 17:
        case 18:
        case 19:
-               jit_ldxi_us(rt, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2d[reg]));
+               jit_ldxi_us(rt, LIGHTREC_REG_STATE, cp2d_s_offset(reg));
                break;
        case 28:
        case 29:
@@ -1803,8 +1930,7 @@ static void rec_cp2_basic_MFC2(struct lightrec_cstate *state,
                for (i = 0; i < 3; i++) {
                        out = i == 0 ? rt : tmp;
 
-                       jit_ldxi_s(tmp, LIGHTREC_REG_STATE,
-                                  offsetof(struct lightrec_state, regs.cp2d[9 + i]));
+                       jit_ldxi_s(tmp, LIGHTREC_REG_STATE, cp2d_s_offset(9 + i));
                        jit_movi(tmp2, 0x1f);
                        jit_rshi(out, tmp, 7);
 
@@ -1826,8 +1952,7 @@ static void rec_cp2_basic_MFC2(struct lightrec_cstate *state,
                lightrec_free_reg(reg_cache, tmp3);
                break;
        default:
-               jit_ldxi_i(rt, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2d[reg]));
+               jit_ldxi_i(rt, LIGHTREC_REG_STATE, cp2d_i_offset(reg));
                break;
        }
 
@@ -1853,13 +1978,11 @@ static void rec_cp2_basic_CFC2(struct lightrec_cstate *state,
        case 29:
        case 30:
                rt = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rt, REG_EXT);
-               jit_ldxi_s(rt, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2c[c.r.rd]));
+               jit_ldxi_s(rt, LIGHTREC_REG_STATE, cp2c_s_offset(c.r.rd));
                break;
        default:
                rt = lightrec_alloc_reg_out(reg_cache, _jit, c.r.rt, REG_ZEXT);
-               jit_ldxi_i(rt, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2c[c.r.rd]));
+               jit_ldxi_i(rt, LIGHTREC_REG_STATE, cp2c_i_offset(c.r.rd));
                break;
        }
 
@@ -1888,19 +2011,14 @@ static void rec_cp2_basic_MTC2(struct lightrec_cstate *state,
        switch (c.r.rd) {
        case 15:
                tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
-               jit_ldxi_i(tmp, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2d[13]));
+               jit_ldxi_i(tmp, LIGHTREC_REG_STATE, cp2d_i_offset(13));
 
                tmp2 = lightrec_alloc_reg_temp(reg_cache, _jit);
-               jit_ldxi_i(tmp2, LIGHTREC_REG_STATE,
-                          offsetof(struct lightrec_state, regs.cp2d[14]));
+               jit_ldxi_i(tmp2, LIGHTREC_REG_STATE, cp2d_i_offset(14));
 
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2d[12]),
-                          LIGHTREC_REG_STATE, tmp);
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2d[13]),
-                          LIGHTREC_REG_STATE, tmp2);
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2d[14]),
-                          LIGHTREC_REG_STATE, rt);
+               jit_stxi_i(cp2d_i_offset(12), LIGHTREC_REG_STATE, tmp);
+               jit_stxi_i(cp2d_i_offset(13), LIGHTREC_REG_STATE, tmp2);
+               jit_stxi_i(cp2d_i_offset(14), LIGHTREC_REG_STATE, rt);
 
                lightrec_free_reg(reg_cache, tmp);
                lightrec_free_reg(reg_cache, tmp2);
@@ -1910,18 +2028,15 @@ static void rec_cp2_basic_MTC2(struct lightrec_cstate *state,
 
                jit_lshi(tmp, rt, 7);
                jit_andi(tmp, tmp, 0xf80);
-               jit_stxi_s(offsetof(struct lightrec_state, regs.cp2d[9]),
-                          LIGHTREC_REG_STATE, tmp);
+               jit_stxi_s(cp2d_s_offset(9), LIGHTREC_REG_STATE, tmp);
 
                jit_lshi(tmp, rt, 2);
                jit_andi(tmp, tmp, 0xf80);
-               jit_stxi_s(offsetof(struct lightrec_state, regs.cp2d[10]),
-                          LIGHTREC_REG_STATE, tmp);
+               jit_stxi_s(cp2d_s_offset(10), LIGHTREC_REG_STATE, tmp);
 
                jit_rshi(tmp, rt, 3);
                jit_andi(tmp, tmp, 0xf80);
-               jit_stxi_s(offsetof(struct lightrec_state, regs.cp2d[11]),
-                          LIGHTREC_REG_STATE, tmp);
+               jit_stxi_s(cp2d_s_offset(11), LIGHTREC_REG_STATE, tmp);
 
                lightrec_free_reg(reg_cache, tmp);
                break;
@@ -1945,17 +2060,14 @@ static void rec_cp2_basic_MTC2(struct lightrec_cstate *state,
 
                jit_patch_at(to_loop, loop);
 
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2d[31]),
-                          LIGHTREC_REG_STATE, tmp2);
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2d[30]),
-                          LIGHTREC_REG_STATE, rt);
+               jit_stxi_i(cp2d_i_offset(31), LIGHTREC_REG_STATE, tmp2);
+               jit_stxi_i(cp2d_i_offset(30), LIGHTREC_REG_STATE, rt);
 
                lightrec_free_reg(reg_cache, tmp);
                lightrec_free_reg(reg_cache, tmp2);
                break;
        default:
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2d[c.r.rd]),
-                          LIGHTREC_REG_STATE, rt);
+               jit_stxi_i(cp2d_i_offset(c.r.rd), LIGHTREC_REG_STATE, rt);
                break;
        }
 
@@ -1982,8 +2094,7 @@ static void rec_cp2_basic_CTC2(struct lightrec_cstate *state,
        case 27:
        case 29:
        case 30:
-               jit_stxi_s(offsetof(struct lightrec_state, regs.cp2c[c.r.rd]),
-                          LIGHTREC_REG_STATE, rt);
+               jit_stxi_s(cp2c_s_offset(c.r.rd), LIGHTREC_REG_STATE, rt);
                break;
        case 31:
                tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
@@ -1996,16 +2107,14 @@ static void rec_cp2_basic_CTC2(struct lightrec_cstate *state,
                jit_andi(tmp2, rt, 0x7ffff000);
                jit_orr(tmp, tmp2, tmp);
 
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2c[31]),
-                          LIGHTREC_REG_STATE, tmp);
+               jit_stxi_i(cp2c_i_offset(31), LIGHTREC_REG_STATE, tmp);
 
                lightrec_free_reg(reg_cache, tmp);
                lightrec_free_reg(reg_cache, tmp2);
                break;
 
        default:
-               jit_stxi_i(offsetof(struct lightrec_state, regs.cp2c[c.r.rd]),
-                          LIGHTREC_REG_STATE, rt);
+               jit_stxi_i(cp2c_i_offset(c.r.rd), LIGHTREC_REG_STATE, rt);
        }
 
        lightrec_free_reg(reg_cache, rt);