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.
call in ir_codegen.c already respects hook_direct_calls before emitting direct
calls, and module_a64.c propagates the option. Any future direct-call shortcut must preserve hook precedence for
both AOT and JIT code. Do not bypass an active hook because an original guest target is known.BLR target
and emits a
direct call to it, with the lookup as the fallback. This covers C++ virtual calls, and it is static, with no writable
code. A runtime cache like __stubs_sh is a later option. Its entry must update atomically, for example by packing a
32-bit target offset and a 32-bit function index into one 64-bit word.
Resolve hook ownership first, and invalidate or bypass this shortcut when a hook changes the destination. Coordinate
the representation with the expanded hook system rather than adding a second target cache.ir_codegen.c, ir_profile.c.CPU.disp at it so the runtime keeps writing slots as it does now. The table's address and the code range become
link-time constants, so a lookup from AOT code is one subtraction, one bound and alignment check, and one load. JIT
units and NRO images keep the lookup through CPU.cpu.h, ir_helpers.c, src/nx/loader/module_abi.h, guest_jit.c.native_bench.snx_lookup checks, or one sparse table over the
whole address space.cpu.h, guest_jit.c, src/nx/hle/services/ldr.c.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).
jit_service.c.HOST_MACHINE is 0 on these hosts, so the loader refuses what the JIT would compile, and a miss ends the
game (code_loader.c, rtapi.c).elf_object.c, link_elf.c), and set HOST_MACHINE.code_loader.c, elf_object.c.ir_codegen.c, guest_jit.c, src/nx/runtime/driver/recompile.c.Made from docs/layers/cpu/plan.md at 69237645142c. GPL-3.0-or-later.