#include <stdbool.h>
#include <stddef.h>
#include <string.h>
-#if ENABLE_TINYMM
-#include <tinymm.h>
-#endif
#define GENMASK(h, l) \
(((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
case OP_LW:
if (op.i.rt)
state->regs.gpr[op.i.rt] = ret;
- default: /* fall-through */
+ fallthrough;
+ default:
break;
}
}
clamp_s32(gteir3 >> 7, 0, 0x1f) << 10;
case 15:
reg = 14;
- default: /* fall-through */
+ fallthrough;
+ default:
return state->regs.cp2d[reg];
}
}
return;
case 30:
state->regs.cp2d[31] = count_leading_bits((s32) data);
- default: /* fall-through */
+ fallthrough;
+ default:
state->regs.cp2d[reg] = data;
break;
}
break;
case 31:
data = (data & 0x7ffff000) | !!(data & 0x7f87e000) << 31;
- default: /* fall-through */
+ fallthrough;
+ default:
break;
}
struct block *block;
bool should_recompile;
void *func;
+ int err;
for (;;) {
- func = lut_read(state, pc);
+ func = lut_read(state, lut_offset(pc));
if (func && func != state->get_next_block)
break;
lightrec_unregister(MEM_FOR_CODE, block->code_size);
- if (ENABLE_THREADED_COMPILER)
+ if (ENABLE_THREADED_COMPILER) {
lightrec_recompiler_add(state->rec, block);
- else
- lightrec_compile_block(state->cstate, block);
+ } else {
+ err = lightrec_compile_block(state->cstate, block);
+ if (err) {
+ state->exit_flags = LIGHTREC_EXIT_NOMEM;
+ return NULL;
+ }
+ }
}
if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
pc = lightrec_emulate_block(state, block, pc);
/* Then compile it using the profiled data */
- lightrec_compile_block(state->cstate, block);
+ err = lightrec_compile_block(state->cstate, block);
+ if (err) {
+ state->exit_flags = LIGHTREC_EXIT_NOMEM;
+ return NULL;
+ }
} else {
lightrec_recompiler_add(state->rec, block);
}
return state->target_cycle - state->current_cycle;
}
+static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
+{
+ void *code;
+
+ if (ENABLE_THREADED_COMPILER)
+ lightrec_code_alloc_lock(state);
+
+ code = tlsf_malloc(state->tlsf, size);
+
+ if (ENABLE_THREADED_COMPILER)
+ lightrec_code_alloc_unlock(state);
+
+ return code;
+}
+
+static void lightrec_realloc_code(struct lightrec_state *state,
+ void *ptr, size_t size)
+{
+ /* NOTE: 'size' MUST be smaller than the size specified during
+ * the allocation. */
+
+ if (ENABLE_THREADED_COMPILER)
+ lightrec_code_alloc_lock(state);
+
+ tlsf_realloc(state->tlsf, ptr, size);
+
+ if (ENABLE_THREADED_COMPILER)
+ lightrec_code_alloc_unlock(state);
+}
+
+static void lightrec_free_code(struct lightrec_state *state, void *ptr)
+{
+ if (ENABLE_THREADED_COMPILER)
+ lightrec_code_alloc_lock(state);
+
+ tlsf_free(state->tlsf, ptr);
+
+ if (ENABLE_THREADED_COMPILER)
+ lightrec_code_alloc_unlock(state);
+}
+
static void * lightrec_emit_code(struct lightrec_state *state,
+ const struct block *block,
jit_state_t *_jit, unsigned int *size)
{
bool has_code_buffer = ENABLE_CODE_BUFFER && state->tlsf;
if (has_code_buffer) {
jit_get_code(&code_size);
- code = tlsf_malloc(state->tlsf, (size_t) code_size);
- if (!code)
- return NULL;
+ code = lightrec_alloc_code(state, (size_t) code_size);
+
+ if (!code) {
+ if (ENABLE_THREADED_COMPILER) {
+ /* If we're using the threaded compiler, return
+ * an allocation error here. The threaded
+ * compiler will then empty its job queue and
+ * request a code flush using the reaper. */
+ return NULL;
+ }
+
+ /* Remove outdated blocks, and try again */
+ lightrec_remove_outdated_blocks(state->block_cache, block);
+
+ pr_debug("Re-try to alloc %zu bytes...\n", code_size);
+
+ code = lightrec_alloc_code(state, code_size);
+ if (!code) {
+ pr_err("Could not alloc even after removing old blocks!\n");
+ return NULL;
+ }
+ }
jit_set_code(code, code_size);
}
lightrec_register(MEM_FOR_CODE, new_code_size);
if (has_code_buffer) {
- tlsf_realloc(state->tlsf, code, new_code_size);
+ lightrec_realloc_code(state, code, (size_t) new_code_size);
pr_debug("Creating code block at address 0x%" PRIxPTR ", "
"code size: %" PRIuPTR " new: %" PRIuPTR "\n",
block->flags = 0;
block->nb_ops = 0;
- block->function = lightrec_emit_code(state, _jit,
+ block->function = lightrec_emit_code(state, block, _jit,
&block->code_size);
if (!block->function)
goto err_free_block;
block->flags = 0;
block->nb_ops = 0;
- block->function = lightrec_emit_code(state, _jit,
+ block->function = lightrec_emit_code(state, block, _jit,
&block->code_size);
if (!block->function)
goto err_free_block;
block->next = NULL;
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
case OP_SWC2:
if (!LIGHTREC_FLAGS_GET_IO_MODE(op->flags))
return false;
- default: /* fall-through */
+ fallthrough;
+ default:
continue;
}
}
{
if (ENABLE_CODE_BUFFER && state->tlsf) {
pr_debug("Freeing code block at 0x%" PRIxPTR "\n", (uintptr_t) fn);
- tlsf_free(state->tlsf, fn);
+ lightrec_free_code(state, fn);
}
}
jit_state_t *_jit, *oldjit;
jit_node_t *start_of_block;
bool skip_next = false;
- void *old_fn;
+ void *old_fn, *new_fn;
unsigned int i, j;
u32 offset;
jit_ret();
jit_epilog();
- block->function = lightrec_emit_code(state, _jit,
- &block->code_size);
- if (!block->function) {
- pr_err("Unable to compile block!\n");
+ new_fn = lightrec_emit_code(state, block, _jit, &block->code_size);
+ if (!new_fn) {
+ if (!ENABLE_THREADED_COMPILER)
+ pr_err("Unable to compile block!\n");
+ block->_jit = oldjit;
+ _jit_destroy_state(_jit);
+ return -ENOMEM;
}
+ block->function = new_fn;
block->flags &= ~BLOCK_SHOULD_RECOMPILE;
/* Add compiled function to the LUT */
lightrec_free_opcode_list(state, block);
if (block->_jit)
_jit_destroy_state(block->_jit);
- lightrec_free_function(state, block->function);
- lightrec_unregister(MEM_FOR_CODE, block->code_size);
+ if (block->function) {
+ lightrec_free_function(state, block->function);
+ lightrec_unregister(MEM_FOR_CODE, block->code_size);
+ }
lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
}
size_t nb,
const struct lightrec_ops *ops)
{
- const struct lightrec_mem_map *codebuf_map;
+ const struct lightrec_mem_map *codebuf_map = &map[PSX_MAP_CODE_BUFFER];
struct lightrec_state *state;
uintptr_t addr;
void *tlsf = NULL;
return NULL;
}
- if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER) {
- codebuf_map = &map[PSX_MAP_CODE_BUFFER];
-
+ if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER
+ && codebuf_map->address) {
tlsf = tlsf_create_with_pool(codebuf_map->address,
codebuf_map->length);
if (!tlsf) {
state->tlsf = tlsf;
state->with_32bit_lut = with_32bit_lut;
-#if ENABLE_TINYMM
- state->tinymm = tinymm_init(malloc, free, 4096);
- if (!state->tinymm)
- goto err_free_state;
-#endif
-
state->block_cache = lightrec_blockcache_init(state);
if (!state->block_cache)
- goto err_free_tinymm;
+ goto err_free_state;
if (ENABLE_THREADED_COMPILER) {
state->rec = lightrec_recompiler_init(state);
lightrec_free_cstate(state->cstate);
err_free_block_cache:
lightrec_free_block_cache(state->block_cache);
-err_free_tinymm:
-#if ENABLE_TINYMM
- tinymm_shutdown(state->tinymm);
err_free_state:
-#endif
lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
lut_elm_size(state) * CODE_LUT_SIZE);
free(state);
if (ENABLE_CODE_BUFFER && state->tlsf)
tlsf_destroy(state->tlsf);
-#if ENABLE_TINYMM
- tinymm_shutdown(state->tinymm);
-#endif
lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
lut_elm_size(state) * CODE_LUT_SIZE);
free(state);
void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len)
{
u32 kaddr = kunseg(addr & ~0x3);
- const struct lightrec_mem_map *map = lightrec_get_map(state, NULL, kaddr);
-
- if (map) {
- if (map != &state->maps[PSX_MAP_KERNEL_USER_RAM])
- return;
+ enum psx_map idx = lightrec_get_map_idx(state, kaddr);
+ switch (idx) {
+ case PSX_MAP_MIRROR1:
+ case PSX_MAP_MIRROR2:
+ case PSX_MAP_MIRROR3:
/* Handle mirrors */
- kaddr &= (state->maps[PSX_MAP_KERNEL_USER_RAM].length - 1);
-
- lightrec_invalidate_map(state, map, kaddr, len);
+ kaddr &= RAM_SIZE - 1;
+ fallthrough;
+ case PSX_MAP_KERNEL_USER_RAM:
+ break;
+ default:
+ return;
}
+
+ memset(lut_address(state, lut_offset(kaddr)), 0,
+ ((len + 3) / 4) * lut_elm_size(state));
}
void lightrec_invalidate_all(struct lightrec_state *state)