Update lightrec 20220910 (#686)
[pcsx_rearmed.git] / deps / lightrec / lightrec.c
index ffa40f0..497cc68 100644 (file)
@@ -342,12 +342,14 @@ static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
        struct opcode *op;
        bool was_tagged;
        u16 offset = (u16)arg;
+       u16 old_flags;
 
        block = lightrec_find_block_from_lut(state->block_cache,
                                             arg >> 16, state->next_pc);
        if (unlikely(!block)) {
                pr_err("rw_generic: No block found in LUT for PC 0x%x offset 0x%x\n",
                         state->next_pc, offset);
+               lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
                return;
        }
 
@@ -357,11 +359,14 @@ static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
        lightrec_rw_helper(state, op->c, &op->flags, block);
 
        if (!was_tagged) {
-               pr_debug("Opcode of block at PC 0x%08x has been tagged - flag "
-                        "for recompilation\n", block->pc);
+               old_flags = block_set_flags(block, BLOCK_SHOULD_RECOMPILE);
 
-               block->flags |= BLOCK_SHOULD_RECOMPILE;
-               lut_write(state, lut_offset(block->pc), NULL);
+               if (!(old_flags & BLOCK_SHOULD_RECOMPILE)) {
+                       pr_debug("Opcode of block at PC 0x%08x has been tagged"
+                                " - flag for recompilation\n", block->pc);
+
+                       lut_write(state, lut_offset(block->pc), NULL);
+               }
        }
 }
 
@@ -418,12 +423,27 @@ static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg)
 
 u32 lightrec_mfc(struct lightrec_state *state, union code op)
 {
+       u32 val;
+
        if (op.i.op == OP_CP0)
                return state->regs.cp0[op.r.rd];
        else if (op.r.rs == OP_CP2_BASIC_MFC2)
                return lightrec_mfc2(state, op.r.rd);
-       else
-               return state->regs.cp2c[op.r.rd];
+
+       val = state->regs.cp2c[op.r.rd];
+
+       switch (op.r.rd) {
+       case 4:
+       case 12:
+       case 20:
+       case 26:
+       case 27:
+       case 29:
+       case 30:
+               return (u32)(s16)val;
+       default:
+               return val;
+       }
 }
 
 static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
@@ -586,31 +606,26 @@ static void lightrec_cp_cb(struct lightrec_state *state, u32 arg)
        lightrec_cp(state, (union code) arg);
 }
 
-static void lightrec_syscall_cb(struct lightrec_state *state)
-{
-       lightrec_set_exit_flags(state, LIGHTREC_EXIT_SYSCALL);
-}
-
-static void lightrec_break_cb(struct lightrec_state *state)
-{
-       lightrec_set_exit_flags(state, LIGHTREC_EXIT_BREAK);
-}
-
 static struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
 {
        struct block *block = lightrec_find_block(state->block_cache, pc);
+       u8 old_flags;
 
        if (block && lightrec_block_is_outdated(state, block)) {
                pr_debug("Block at PC 0x%08x is outdated!\n", block->pc);
 
-               /* Make sure the recompiler isn't processing the block we'll
-                * destroy */
-               if (ENABLE_THREADED_COMPILER)
-                       lightrec_recompiler_remove(state->rec, block);
+               old_flags = block_set_flags(block, BLOCK_IS_DEAD);
+               if (!(old_flags & BLOCK_IS_DEAD)) {
+                       /* Make sure the recompiler isn't processing the block
+                        * we'll destroy */
+                       if (ENABLE_THREADED_COMPILER)
+                               lightrec_recompiler_remove(state->rec, block);
+
+                       lightrec_unregister_block(state->block_cache, block);
+                       remove_from_code_lut(state->block_cache, block);
+                       lightrec_free_block(state, block);
+               }
 
-               lightrec_unregister_block(state->block_cache, block);
-               remove_from_code_lut(state->block_cache, block);
-               lightrec_free_block(state, block);
                block = NULL;
        }
 
@@ -645,19 +660,18 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
                if (unlikely(!block))
                        break;
 
-               if (OPT_REPLACE_MEMSET && (block->flags & BLOCK_IS_MEMSET)) {
+               if (OPT_REPLACE_MEMSET &&
+                   block_has_flag(block, BLOCK_IS_MEMSET)) {
                        func = state->memset_func;
                        break;
                }
 
-               should_recompile = block->flags & BLOCK_SHOULD_RECOMPILE &&
-                       !(block->flags & BLOCK_IS_DEAD);
+               should_recompile = block_has_flag(block, BLOCK_SHOULD_RECOMPILE) &&
+                       !block_has_flag(block, BLOCK_IS_DEAD);
 
                if (unlikely(should_recompile)) {
                        pr_debug("Block at PC 0x%08x should recompile\n", pc);
 
-                       lightrec_unregister(MEM_FOR_CODE, block->code_size);
-
                        if (ENABLE_THREADED_COMPILER) {
                                lightrec_recompiler_add(state->rec, block);
                        } else {
@@ -677,12 +691,12 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
                if (likely(func))
                        break;
 
-               if (unlikely(block->flags & BLOCK_NEVER_COMPILE)) {
+               if (unlikely(block_has_flag(block, BLOCK_NEVER_COMPILE))) {
                        pc = lightrec_emulate_block(state, block, pc);
 
                } else if (!ENABLE_THREADED_COMPILER) {
                        /* Block wasn't compiled yet - run the interpreter */
-                       if (block->flags & BLOCK_FULLY_TAGGED)
+                       if (block_has_flag(block, BLOCK_FULLY_TAGGED))
                                pr_debug("Block fully tagged, skipping first pass\n");
                        else if (ENABLE_FIRST_PASS && likely(!should_recompile))
                                pc = lightrec_emulate_block(state, block, pc);
@@ -693,6 +707,15 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
                                state->exit_flags = LIGHTREC_EXIT_NOMEM;
                                return NULL;
                        }
+               } else if (unlikely(block_has_flag(block, BLOCK_IS_DEAD))) {
+                       /*
+                        * If the block is dead but has never been compiled,
+                        * then its function pointer is NULL and we cannot
+                        * execute the block. In that case, reap all the dead
+                        * blocks now, and in the next loop we will create a
+                        * new block.
+                        */
+                       lightrec_reaper_reap(state->reaper);
                } else {
                        lightrec_recompiler_add(state->rec, block);
                }
@@ -706,16 +729,6 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
        return func;
 }
 
-static s32 c_function_wrapper(struct lightrec_state *state, s32 cycles_delta,
-                             void (*f)(struct lightrec_state *, u32), u32 arg)
-{
-       state->current_cycle = state->target_cycle - cycles_delta;
-
-       (*f)(state, arg);
-
-       return state->target_cycle - state->current_cycle;
-}
-
 static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
 {
        void *code;
@@ -821,9 +834,8 @@ static struct block * generate_wrapper(struct lightrec_state *state)
        struct block *block;
        jit_state_t *_jit;
        unsigned int i;
-       int stack_ptr;
-       jit_node_t *to_tramp, *to_fn_epilog;
        jit_node_t *addr[C_WRAPPERS_COUNT - 1];
+       jit_node_t *to_end[C_WRAPPERS_COUNT - 1];
 
        block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
        if (!block)
@@ -840,67 +852,69 @@ static struct block * generate_wrapper(struct lightrec_state *state)
        jit_prolog();
        jit_tramp(256);
 
-       /* Add entry points; separate them by opcodes that increment
-        * LIGHTREC_REG_STATE (since we cannot touch other registers).
-        * The difference will then tell us which C function to call. */
+       /* Add entry points */
        for (i = C_WRAPPERS_COUNT - 1; i > 0; i--) {
-               jit_addi(LIGHTREC_REG_STATE, LIGHTREC_REG_STATE, __WORDSIZE / 8);
+               jit_ldxi(JIT_R1, LIGHTREC_REG_STATE,
+                        offsetof(struct lightrec_state, c_wrappers[i]));
+               to_end[i - 1] = jit_b();
                addr[i - 1] = jit_indirect();
        }
 
+       jit_ldxi(JIT_R1, LIGHTREC_REG_STATE,
+                offsetof(struct lightrec_state, c_wrappers[0]));
+
+       for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
+               jit_patch(to_end[i]);
+
        jit_epilog();
        jit_prolog();
 
-       stack_ptr = jit_allocai(sizeof(uintptr_t) * NUM_TEMPS);
-
        /* Save all temporaries on stack */
-       for (i = 0; i < NUM_TEMPS; i++)
-               jit_stxi(stack_ptr + i * sizeof(uintptr_t), JIT_FP, JIT_R(i));
+       for (i = 0; i < NUM_TEMPS; i++) {
+               if (i + FIRST_TEMP != 1) {
+                       jit_stxi(offsetof(struct lightrec_state, wrapper_regs[i]),
+                                LIGHTREC_REG_STATE, JIT_R(i + FIRST_TEMP));
+               }
+       }
 
-       jit_getarg(JIT_R1, jit_arg());
+       jit_getarg(JIT_R2, jit_arg());
 
-       /* Jump to the trampoline */
-       to_tramp = jit_jmpi();
+       jit_prepare();
+       jit_pushargr(LIGHTREC_REG_STATE);
+       jit_pushargr(JIT_R2);
 
-       /* The trampoline will jump back here */
-       to_fn_epilog = jit_label();
+       jit_ldxi_ui(JIT_R2, LIGHTREC_REG_STATE,
+                   offsetof(struct lightrec_state, target_cycle));
 
-       /* Restore temporaries from stack */
-       for (i = 0; i < NUM_TEMPS; i++)
-               jit_ldxi(JIT_R(i), JIT_FP, stack_ptr + i * sizeof(uintptr_t));
+       /* state->current_cycle = state->target_cycle - delta; */
+       jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, LIGHTREC_REG_CYCLE);
+       jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
+                  LIGHTREC_REG_STATE, LIGHTREC_REG_CYCLE);
 
-       jit_ret();
-       jit_epilog();
+       /* Call the wrapper function */
+       jit_finishr(JIT_R1);
 
-       /* Trampoline entry point.
-        * The sole purpose of the trampoline is to cheese Lightning not to
-        * save/restore the callee-saved register LIGHTREC_REG_CYCLE, since we
-        * do want to return to the caller with this register modified. */
-       jit_prolog();
-       jit_tramp(256);
-       jit_patch(to_tramp);
-
-       /* Retrieve the wrapper function */
-       jit_ldxi(JIT_R0, LIGHTREC_REG_STATE,
-                offsetof(struct lightrec_state, c_wrappers));
-
-       /* Restore LIGHTREC_REG_STATE to its correct value */
-       jit_movi(LIGHTREC_REG_STATE, (uintptr_t) state);
+       /* delta = state->target_cycle - state->current_cycle */;
+       jit_ldxi_ui(LIGHTREC_REG_CYCLE, LIGHTREC_REG_STATE,
+                   offsetof(struct lightrec_state, current_cycle));
+       jit_ldxi_ui(JIT_R1, LIGHTREC_REG_STATE,
+                   offsetof(struct lightrec_state, target_cycle));
+       jit_subr(LIGHTREC_REG_CYCLE, JIT_R1, LIGHTREC_REG_CYCLE);
 
-       jit_prepare();
-       jit_pushargr(LIGHTREC_REG_STATE);
-       jit_pushargr(LIGHTREC_REG_CYCLE);
-       jit_pushargr(JIT_R0);
-       jit_pushargr(JIT_R1);
-       jit_finishi(c_function_wrapper);
-       jit_retval_i(LIGHTREC_REG_CYCLE);
+       /* Restore temporaries from stack */
+       for (i = 0; i < NUM_TEMPS; i++) {
+               if (i + FIRST_TEMP != 1) {
+                       jit_ldxi(JIT_R(i + FIRST_TEMP), LIGHTREC_REG_STATE,
+                                offsetof(struct lightrec_state, wrapper_regs[i]));
+               }
+       }
 
-       jit_patch_at(jit_jmpi(), to_fn_epilog);
+       jit_ret();
        jit_epilog();
 
        block->_jit = _jit;
        block->opcode_list = NULL;
-       block->flags = 0;
+       block->flags = BLOCK_NO_OPCODE_LIST;
        block->nb_ops = 0;
 
        block->function = lightrec_emit_code(state, block, _jit,
@@ -974,12 +988,12 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
        jit_prolog();
        jit_frame(256);
 
-       jit_getarg(JIT_R0, jit_arg());
+       jit_getarg(JIT_V1, jit_arg());
        jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
 
        /* Force all callee-saved registers to be pushed on the stack */
        for (i = 0; i < NUM_REGS; i++)
-               jit_movr(JIT_V(i), JIT_V(i));
+               jit_movr(JIT_V(i + FIRST_REG), JIT_V(i + FIRST_REG));
 
        /* Pass lightrec_state structure to blocks, using the last callee-saved
         * register that Lightning provides */
@@ -988,13 +1002,15 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
        loop = jit_label();
 
        /* Call the block's code */
-       jit_jmpr(JIT_R0);
+       jit_jmpr(JIT_V1);
 
        if (OPT_REPLACE_MEMSET) {
                /* Blocks will jump here when they need to call
                 * lightrec_memset() */
                addr3 = jit_indirect();
 
+               jit_movr(JIT_V1, LIGHTREC_REG_CYCLE);
+
                jit_prepare();
                jit_pushargr(LIGHTREC_REG_STATE);
                jit_finishi(lightrec_memset);
@@ -1002,8 +1018,8 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
                jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
                            offsetof(struct lightrec_state, regs.gpr[31]));
 
-               jit_retval(JIT_R0);
-               jit_subr(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, JIT_R0);
+               jit_retval(LIGHTREC_REG_CYCLE);
+               jit_subr(LIGHTREC_REG_CYCLE, JIT_V1, LIGHTREC_REG_CYCLE);
        }
 
        /* The block will jump here, with the number of cycles remaining in
@@ -1018,25 +1034,30 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
        to_end = jit_blei(LIGHTREC_REG_CYCLE, 0);
 
        /* Convert next PC to KUNSEG and avoid mirrors */
-       jit_andi(JIT_R0, JIT_V0, 0x10000000 | (RAM_SIZE - 1));
-       jit_rshi_u(JIT_R1, JIT_R0, 28);
+       jit_andi(JIT_V1, JIT_V0, 0x10000000 | (RAM_SIZE - 1));
+       jit_rshi_u(JIT_R1, JIT_V1, 28);
        jit_andi(JIT_R2, JIT_V0, BIOS_SIZE - 1);
        jit_addi(JIT_R2, JIT_R2, RAM_SIZE);
-       jit_movnr(JIT_R0, JIT_R2, JIT_R1);
+       jit_movnr(JIT_V1, JIT_R2, JIT_R1);
 
        /* If possible, use the code LUT */
        if (!lut_is_32bit(state))
-               jit_lshi(JIT_R0, JIT_R0, 1);
-       jit_addr(JIT_R0, JIT_R0, LIGHTREC_REG_STATE);
+               jit_lshi(JIT_V1, JIT_V1, 1);
+       jit_addr(JIT_V1, JIT_V1, LIGHTREC_REG_STATE);
 
        offset = offsetof(struct lightrec_state, code_lut);
        if (lut_is_32bit(state))
-               jit_ldxi_ui(JIT_R0, JIT_R0, offset);
+               jit_ldxi_ui(JIT_V1, JIT_V1, offset);
        else
-               jit_ldxi(JIT_R0, JIT_R0, offset);
+               jit_ldxi(JIT_V1, JIT_V1, offset);
 
        /* If we get non-NULL, loop */
-       jit_patch_at(jit_bnei(JIT_R0, 0), loop);
+       jit_patch_at(jit_bnei(JIT_V1, 0), loop);
+
+       /* The code LUT will be set to this address when the block at the target
+        * PC has been preprocessed but not yet compiled by the threaded
+        * recompiler */
+       addr = jit_indirect();
 
        /* Slow path: call C function get_next_block_func() */
 
@@ -1044,22 +1065,22 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
                /* We may call the interpreter - update state->current_cycle */
                jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
                           offsetof(struct lightrec_state, target_cycle));
-               jit_subr(JIT_R1, JIT_R2, LIGHTREC_REG_CYCLE);
+               jit_subr(JIT_V1, JIT_R2, LIGHTREC_REG_CYCLE);
                jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
-                          LIGHTREC_REG_STATE, JIT_R1);
+                          LIGHTREC_REG_STATE, JIT_V1);
        }
 
-       /* The code LUT will be set to this address when the block at the target
-        * PC has been preprocessed but not yet compiled by the threaded
-        * recompiler */
-       addr = jit_indirect();
-
-       /* Get the next block */
        jit_prepare();
        jit_pushargr(LIGHTREC_REG_STATE);
        jit_pushargr(JIT_V0);
+
+       /* Save the cycles register if needed */
+       if (!(ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES))
+               jit_movr(JIT_V0, LIGHTREC_REG_CYCLE);
+
+       /* Get the next block */
        jit_finishi(&get_next_block_func);
-       jit_retval(JIT_R0);
+       jit_retval(JIT_V1);
 
        if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
                /* The interpreter may have updated state->current_cycle and
@@ -1069,10 +1090,12 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
                jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
                           offsetof(struct lightrec_state, target_cycle));
                jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, JIT_R1);
+       } else {
+               jit_movr(LIGHTREC_REG_CYCLE, JIT_V0);
        }
 
        /* If we get non-NULL, loop */
-       jit_patch_at(jit_bnei(JIT_R0, 0), loop);
+       jit_patch_at(jit_bnei(JIT_V1, 0), loop);
 
        /* When exiting, the recompiled code will jump to that address */
        jit_note(__FILE__, __LINE__);
@@ -1083,7 +1106,7 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
 
        block->_jit = _jit;
        block->opcode_list = NULL;
-       block->flags = 0;
+       block->flags = BLOCK_NO_OPCODE_LIST;
        block->nb_ops = 0;
 
        block->function = lightrec_emit_code(state, block, _jit,
@@ -1127,11 +1150,13 @@ unsigned int lightrec_cycles_of_opcode(union code code)
        return 2;
 }
 
-void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block)
+void lightrec_free_opcode_list(struct lightrec_state *state, struct opcode *ops)
 {
+       struct opcode_list *list = container_of(ops, struct opcode_list, ops);
+
        lightrec_free(state, MEM_FOR_IR,
-                     sizeof(*block->opcode_list) * block->nb_ops,
-                     block->opcode_list);
+                     sizeof(*list) + list->nb_ops * sizeof(struct opcode),
+                     list);
 }
 
 static unsigned int lightrec_get_mips_block_len(const u32 *src)
@@ -1153,25 +1178,28 @@ static unsigned int lightrec_get_mips_block_len(const u32 *src)
 static struct opcode * lightrec_disassemble(struct lightrec_state *state,
                                            const u32 *src, unsigned int *len)
 {
-       struct opcode *list;
+       struct opcode_list *list;
        unsigned int i, length;
 
        length = lightrec_get_mips_block_len(src);
 
-       list = lightrec_malloc(state, MEM_FOR_IR, sizeof(*list) * length);
+       list = lightrec_malloc(state, MEM_FOR_IR,
+                              sizeof(*list) + sizeof(struct opcode) * length);
        if (!list) {
                pr_err("Unable to allocate memory\n");
                return NULL;
        }
 
+       list->nb_ops = (u16) length;
+
        for (i = 0; i < length; i++) {
-               list[i].opcode = LE32TOH(src[i]);
-               list[i].flags = 0;
+               list->ops[i].opcode = LE32TOH(src[i]);
+               list->ops[i].flags = 0;
        }
 
        *len = length * sizeof(u32);
 
-       return list;
+       return list->ops;
 }
 
 static struct block * lightrec_precompile_block(struct lightrec_state *state,
@@ -1179,11 +1207,12 @@ static struct block * lightrec_precompile_block(struct lightrec_state *state,
 {
        struct opcode *list;
        struct block *block;
-       void *host;
+       void *host, *addr;
        const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg(pc));
        const u32 *code = (u32 *) host;
        unsigned int length;
        bool fully_tagged;
+       u8 block_flags = 0;
 
        if (!map)
                return NULL;
@@ -1209,9 +1238,6 @@ static struct block * lightrec_precompile_block(struct lightrec_state *state,
        block->flags = 0;
        block->code_size = 0;
        block->precompile_date = state->current_cycle;
-#if ENABLE_THREADED_COMPILER
-       block->op_list_freed = (atomic_flag)ATOMIC_FLAG_INIT;
-#endif
        block->nb_ops = length / sizeof(u32);
 
        lightrec_optimize(state, block);
@@ -1230,17 +1256,23 @@ static struct block * lightrec_precompile_block(struct lightrec_state *state,
        /* If the first opcode is an 'impossible' branch, never compile the
         * block */
        if (should_emulate(block->opcode_list))
-               block->flags |= BLOCK_NEVER_COMPILE;
+               block_flags |= BLOCK_NEVER_COMPILE;
 
        fully_tagged = lightrec_block_is_fully_tagged(block);
        if (fully_tagged)
-               block->flags |= BLOCK_FULLY_TAGGED;
+               block_flags |= BLOCK_FULLY_TAGGED;
 
-       if (OPT_REPLACE_MEMSET && (block->flags & BLOCK_IS_MEMSET))
-               lut_write(state, lut_offset(pc), state->memset_func);
+       if (block_flags)
+               block_set_flags(block, block_flags);
 
        block->hash = lightrec_calculate_block_hash(block);
 
+       if (OPT_REPLACE_MEMSET && block_has_flag(block, BLOCK_IS_MEMSET))
+               addr = state->memset_func;
+       else
+               addr = state->get_next_block;
+       lut_write(state, lut_offset(pc), addr);
+
        pr_debug("Recompile count: %u\n", state->nb_precompile++);
 
        return block;
@@ -1310,24 +1342,31 @@ static void lightrec_reap_function(struct lightrec_state *state, void *data)
        lightrec_free_function(state, data);
 }
 
+static void lightrec_reap_opcode_list(struct lightrec_state *state, void *data)
+{
+       lightrec_free_opcode_list(state, data);
+}
+
 int lightrec_compile_block(struct lightrec_cstate *cstate,
                           struct block *block)
 {
        struct lightrec_state *state = cstate->state;
        struct lightrec_branch_target *target;
-       bool op_list_freed = false, fully_tagged = false;
+       bool fully_tagged = false;
        struct block *block2;
        struct opcode *elm;
        jit_state_t *_jit, *oldjit;
        jit_node_t *start_of_block;
        bool skip_next = false;
        void *old_fn, *new_fn;
+       size_t old_code_size;
        unsigned int i, j;
+       u8 old_flags;
        u32 offset;
 
        fully_tagged = lightrec_block_is_fully_tagged(block);
        if (fully_tagged)
-               block->flags |= BLOCK_FULLY_TAGGED;
+               block_set_flags(block, BLOCK_FULLY_TAGGED);
 
        _jit = jit_new_state();
        if (!_jit)
@@ -1335,11 +1374,11 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
 
        oldjit = block->_jit;
        old_fn = block->function;
+       old_code_size = block->code_size;
        block->_jit = _jit;
 
        lightrec_regcache_reset(cstate->reg_cache);
        cstate->cycles = 0;
-       cstate->nb_branches = 0;
        cstate->nb_local_branches = 0;
        cstate->nb_targets = 0;
 
@@ -1377,9 +1416,6 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                cstate->cycles += lightrec_cycles_of_opcode(elm->c);
        }
 
-       for (i = 0; i < cstate->nb_branches; i++)
-               jit_patch(cstate->branches[i]);
-
        for (i = 0; i < cstate->nb_local_branches; i++) {
                struct lightrec_branch *branch = &cstate->local_branches[i];
 
@@ -1403,7 +1439,6 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                        pr_err("Unable to find branch target\n");
        }
 
-       jit_patch_abs(jit_jmpi(), state->eob_wrapper_func);
        jit_ret();
        jit_epilog();
 
@@ -1412,22 +1447,24 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                if (!ENABLE_THREADED_COMPILER)
                        pr_err("Unable to compile block!\n");
                block->_jit = oldjit;
+               jit_clear_state();
                _jit_destroy_state(_jit);
                return -ENOMEM;
        }
 
+       /* Pause the reaper, because lightrec_reset_lut_offset() may try to set
+        * the old block->function pointer to the code LUT. */
+       if (ENABLE_THREADED_COMPILER)
+               lightrec_reaper_pause(state->reaper);
+
        block->function = new_fn;
-       block->flags &= ~BLOCK_SHOULD_RECOMPILE;
+       block_clear_flags(block, BLOCK_SHOULD_RECOMPILE);
 
        /* Add compiled function to the LUT */
        lut_write(state, lut_offset(block->pc), block->function);
 
-       if (ENABLE_THREADED_COMPILER) {
-               /* Since we might try to reap the same block multiple times,
-                * we need the reaper to wait until everything has been
-                * submitted, so that the duplicate entries can be dropped. */
-               lightrec_reaper_pause(state->reaper);
-       }
+       if (ENABLE_THREADED_COMPILER)
+               lightrec_reaper_continue(state->reaper);
 
        /* Detect old blocks that have been covered by the new one */
        for (i = 0; i < cstate->nb_targets; i++) {
@@ -1437,6 +1474,13 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                        continue;
 
                offset = block->pc + target->offset * sizeof(u32);
+
+               /* Pause the reaper while we search for the block until we set
+                * the BLOCK_IS_DEAD flag, otherwise the block may be removed
+                * under our feet. */
+               if (ENABLE_THREADED_COMPILER)
+                       lightrec_reaper_pause(state->reaper);
+
                block2 = lightrec_find_block(state->block_cache, offset);
                if (block2) {
                        /* No need to check if block2 is compilable - it must
@@ -1444,12 +1488,16 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
 
                        /* Set the "block dead" flag to prevent the dynarec from
                         * recompiling this block */
-                       block2->flags |= BLOCK_IS_DEAD;
+                       old_flags = block_set_flags(block2, BLOCK_IS_DEAD);
+               }
+
+               if (ENABLE_THREADED_COMPILER) {
+                       lightrec_reaper_continue(state->reaper);
 
                        /* If block2 was pending for compilation, cancel it.
                         * If it's being compiled right now, wait until it
                         * finishes. */
-                       if (ENABLE_THREADED_COMPILER)
+                       if (block2)
                                lightrec_recompiler_remove(state->rec, block2);
                }
 
@@ -1464,20 +1512,17 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                                 "0x%08x\n", block2->pc, block->pc);
 
                        /* Finally, reap the block. */
-                       if (ENABLE_THREADED_COMPILER) {
+                       if (!ENABLE_THREADED_COMPILER) {
+                               lightrec_unregister_block(state->block_cache, block2);
+                               lightrec_free_block(state, block2);
+                       } else if (!(old_flags & BLOCK_IS_DEAD)) {
                                lightrec_reaper_add(state->reaper,
                                                    lightrec_reap_block,
                                                    block2);
-                       } else {
-                               lightrec_unregister_block(state->block_cache, block2);
-                               lightrec_free_block(state, block2);
                        }
                }
        }
 
-       if (ENABLE_THREADED_COMPILER)
-               lightrec_reaper_continue(state->reaper);
-
        if (ENABLE_DISASSEMBLER) {
                pr_debug("Compiling block at PC: 0x%08x\n", block->pc);
                jit_disassemble();
@@ -1485,15 +1530,20 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
 
        jit_clear_state();
 
-#if ENABLE_THREADED_COMPILER
        if (fully_tagged)
-               op_list_freed = atomic_flag_test_and_set(&block->op_list_freed);
-#endif
-       if (fully_tagged && !op_list_freed) {
+               old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
+
+       if (fully_tagged && !(old_flags & BLOCK_NO_OPCODE_LIST)) {
                pr_debug("Block PC 0x%08x is fully tagged"
                         " - free opcode list\n", block->pc);
-               lightrec_free_opcode_list(state, block);
-               block->opcode_list = NULL;
+
+               if (ENABLE_THREADED_COMPILER) {
+                       lightrec_reaper_add(state->reaper,
+                                           lightrec_reap_opcode_list,
+                                           block->opcode_list);
+               } else {
+                       lightrec_free_opcode_list(state, block->opcode_list);
+               }
        }
 
        if (oldjit) {
@@ -1509,6 +1559,8 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                        _jit_destroy_state(oldjit);
                        lightrec_free_function(state, old_fn);
                }
+
+               lightrec_unregister(MEM_FOR_CODE, old_code_size);
        }
 
        return 0;
@@ -1561,20 +1613,24 @@ u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
        return state->next_pc;
 }
 
-u32 lightrec_execute_one(struct lightrec_state *state, u32 pc)
+u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc,
+                            u32 target_cycle)
 {
-       return lightrec_execute(state, pc, state->current_cycle);
-}
-
-u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc)
-{
-       struct block *block = lightrec_get_block(state, pc);
-       if (!block)
-               return 0;
+       struct block *block;
 
        state->exit_flags = LIGHTREC_EXIT_NORMAL;
+       state->target_cycle = target_cycle;
+
+       do {
+               block = lightrec_get_block(state, pc);
+               if (!block)
+                       break;
+
+               pc = lightrec_emulate_block(state, block, pc);
 
-       pc = lightrec_emulate_block(state, block, pc);
+               if (ENABLE_THREADED_COMPILER)
+                       lightrec_reaper_reap(state->reaper);
+       } while (state->current_cycle < state->target_cycle);
 
        if (LOG_LEVEL >= INFO_L)
                lightrec_print_info(state);
@@ -1584,9 +1640,13 @@ u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc)
 
 void lightrec_free_block(struct lightrec_state *state, struct block *block)
 {
+       u8 old_flags;
+
        lightrec_unregister(MEM_FOR_MIPS_CODE, block->nb_ops * sizeof(u32));
-       if (block->opcode_list)
-               lightrec_free_opcode_list(state, block);
+       old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
+
+       if (!(old_flags & BLOCK_NO_OPCODE_LIST))
+               lightrec_free_opcode_list(state, block->opcode_list);
        if (block->_jit)
                _jit_destroy_state(block->_jit);
        if (block->function) {
@@ -1705,8 +1765,6 @@ struct lightrec_state * lightrec_init(char *argv0,
        state->c_wrappers[C_WRAPPER_RW_GENERIC] = lightrec_rw_generic_cb;
        state->c_wrappers[C_WRAPPER_MTC] = lightrec_mtc_cb;
        state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;
-       state->c_wrappers[C_WRAPPER_SYSCALL] = lightrec_syscall_cb;
-       state->c_wrappers[C_WRAPPER_BREAK] = lightrec_break_cb;
 
        map = &state->maps[PSX_MAP_BIOS];
        state->offset_bios = (uintptr_t)map->address - map->pc;
@@ -1714,6 +1772,9 @@ struct lightrec_state * lightrec_init(char *argv0,
        map = &state->maps[PSX_MAP_SCRATCH_PAD];
        state->offset_scratch = (uintptr_t)map->address - map->pc;
 
+       map = &state->maps[PSX_MAP_HW_REGISTERS];
+       state->offset_io = (uintptr_t)map->address - map->pc;
+
        map = &state->maps[PSX_MAP_KERNEL_USER_RAM];
        state->offset_ram = (uintptr_t)map->address - map->pc;
 
@@ -1725,6 +1786,7 @@ struct lightrec_state * lightrec_init(char *argv0,
        if (state->offset_bios == 0 &&
            state->offset_scratch == 0 &&
            state->offset_ram == 0 &&
+           state->offset_io == 0 &&
            state->mirrors_mapped) {
                pr_info("Memory map is perfect. Emitted code will be best.\n");
        } else {