Rosetta 2. A call to another image goes through __stubs_sh, which holds pairs of an x86 target and its translated target (Champollion part 1, "X86_64 address resolution and lazy binding"). The stub compares the live lazy pointer with the stored x86 target and branches straight to the translated code when they match. A mismatch goes to resolve_x64_addr, which can call the JIT and then writes the new pair back. Other indirect targets resolve through a lookup of function starts and otherwise unreferenced blocks, with the JIT behind it, for example for switch statements (Dougall Johnson).

ScarletNX now. The lookup is a dense table with one slot for each guest instruction word (CPU.disp, src/common/cpu/aarch64/cpu.h). It is constant-time, at a memory cost of twice the code size on a 64-bit host. snx_lookup loads code_lo, code_hi and disp from the CPU structure, then the slot, in about twelve instructions with two conditional branches (build_lookup in src/common/cpu/recompiler/llvm/ir_helpers.c). A game's executables call each other through PLT stubs that load a GOT entry and BR to it. The loader does not read DT_JMPREL (loader.c), so the recompiler does not know those targets, and every call to an import runs the stub's BR through the lookup. Code a game maps at run time (NROs through ldr:ro) lies outside CPU.disp, so each call into it from outside its unit goes through rt->miss, then variant_resolve, then the JIT's slot (src/nx/runtime/guest_jit.c, docs/platforms/nx/nro.md).

On each host. All of this is IR, so it applies to every host alike. Direct calls in a large module pass through the veneers that AArch64 Mach-O modules get past 128 MiB and the stubs that PowerPC64 modules get past 32 MiB (docs/dev/architecture.md). Those still cost less than a lookup.

L1 Standalone PLT binding retired in favor of hook expansion

L2 Guarded direct calls for hot indirect targets

L3 A module that owns its dispatch table

L4 Dispatch for NRO code

3. The JIT for code the AOT pass did not see

Rosetta 2. The JIT translates code the AOT pass never saw, such as code a program writes at run time and switch targets the lookup does not cover. Dougall Johnson notes that it runs relatively rarely. Champollion part 1 follows the translate function in the runtime.

ScarletNX now. A pc with no code goes to rt_miss (rtapi.c). The thread leaves its core and waits until its whole batch is compiled: up to 256 pcs plus a 20000-instruction discovery budget, at jit_opt (O2 by default, O0 on phones), on up to four threads at nice 10 (jit_service.c, jit_service.h, settings.txt). There is no interpreter. ScarletNX keeps what the JIT compiled in <game>.jitcache and records each missed pc as a hint for the next recompile, which Rosetta's documents do not describe. Code a mod patches reaches the runtime through IC IVAU, and every AOT function then checks a stale byte on entry and after each call returns (stale_check and check_return in ir_codegen.c, and "Code the game patches" in docs/dev/architecture.md).

On each host. x86-64 and AArch64 hosts have the JIT, and so does PowerPC64. iOS has it only with a debugger attached (docs/hosts/apple.md), and RISC-V, LoongArch and Intel Macs have none (HOST_MACHINE in code_loader.c). Phones compile at O0 because other levels break there (guest_jit.c).

J2 Priority for batches a thread waits on

J3 A runtime JIT on RISC-V and LoongArch

J4 Stale checks only where code can change


Made from docs/layers/cpu/plan.md at 69237645142c. GPL-3.0-or-later.