Rosetta 2 part by part, applied to ScarletNX

The parts follow the outline that attesor's README gives Rosetta 2: translation ahead of time, the JIT, instruction set translation, system call translation and runtime support, with its translation challenges (registers, memory order, vector instructions and calling conventions). The facts about Rosetta itself come from Project Champollion and Dougall Johnson's post, because attesor's own claims need a second source (see Sources).

Each work item has an ID from its part (A for translation ahead of time, L for links, J for the JIT, I for instruction translation, M for memory, S for system calls, R for runtime support, D for delivery, T for tools). It gives the change, where it goes, its effect on SNX_ABI, the proof it needs, a size (S, M, L or XL) and a priority (P0 first). A change to the contract between generated code and the runtime raises SNX_ABI and recompiles every game (docs/dev/architecture.md, "Module ABI"). A change that only alters the recompiler's output for the same contract needs no bump, and old modules keep running until they are recompiled. A new recompile setting must join the JIT cache key, as volatile_memory does (src/common/cpu/recompiler/jit/jit_cache_key.c).

1. Translation ahead of time, and its cache

Rosetta 2. Dougall Johnson describes Rosetta translating the entire text segment up front, before the program runs. Champollion shows the mechanics (part 1, "Roles of oahd and oahd-helper", and part 2). At exec time the runtime hands the file to the oahd daemon, which computes a SHA-256 over the file's contents and its path and looks for a matching .aot file under /var/db/oah. On a miss, oahd-helper translates the whole binary into a Mach-O file with a new LC_AOT_METADATA load command (number 0xcacaca01). The x86-64 system libraries are translated once into a shared aot_shared_cache, about 2.4 GB in Champollion's analysis, mapped into every process.

ScarletNX now. The launcher's Load Switch NSP/XCI/NRO extracts and recompiles a game before its first run, and the driver recompiles a game whose <id>.abi stamp is stale when it is played. The x86-64 back end's units are held in memory while the process runs, under the SHA-256 of what each is made from (src/common/cpu/recompiler/x64/x64_cache.c), close to Rosetta's content key; nothing is kept on the disk. The module records each executable's build ID and segment layout, and the runtime refuses an executable that changed (snx_module_mods in src/nx/loader/module_fallback_llvm.c, src/nx/runtime/loader.c). Discovery (src/common/cpu/recompiler/codegen.c, src/nx/loader/loader.c) starts from the dynamic symbols, the addends of relative relocations, the .eh_frame ranges (swept word by word), hints from earlier runs and ADR or ADRP+ADD address loads, then follows branches. Jump table cases are not followed. Units group sorted functions up to the unit_instructions setting, 90000 instructions by default and 20000 on phones (module_a64.c). A cold recompile of a game of 255 units takes about 740 s.

Rosetta translates only what it can find too, but its JIT catches the rest on every host. ScarletNX has no JIT on RISC-V, LoongArch or Intel Macs, so the completeness of discovery is what decides whether a game runs there.

A1 Recover jump tables

A2 Sweep for functions nothing references

Custom RTTI and method-table discovery. Search loaded executable data for the runtime's custom type records and follow their validated links to method tables. Establish each supported layout from its SDK/runtime access code or documented format. Custom RTTI need not follow the standard C++ ABI, and a type record may contain no method addresses itself. Do not assume every word in a record is a function pointer.

The current relative-relocation scan already seeds code pointers, including some vtable methods and callbacks (relptrs in loader.c, consumed by snx_codegen_analyze in codegen.c). Extend that coverage with explicit metadata discovery rather than duplicating it. Resolve entries using the format's actual absolute or relative addressing and the owning module's load base. Validate record bounds, counts and links before following them, then require method targets to be aligned and inside a loaded executable code range. Deduplicate them with existing symbol, relocation, unwind and hint seeds, retaining their metadata origin for diagnostics.

Feed validated method addresses, including adjustment thunks, into the existing discovery worklist even when no direct branch references them. Respect existing function ranges and internal-entry handling rather than splitting a function from a table reference alone. Run this metadata pass before speculative code sweeping and apply it when NRO metadata becomes available through its existing discovery path. Unknown formats retain normal discovery and dispatch.

Use synthetic fixtures for a method reachable only through RTTI, shared tables, relative entries where supported, duplicate targets, thunks and malformed or data-only lookalikes. Report newly discovered methods separately from relocation seeds and the scan cost. Compare cold AOT misses before and after. This is function discovery, not automatic devirtualization or permission to bypass hooks. Extracted RTTI records stay local.

A3 Stable units

A5 Optional profile-guided translation

Rosetta translates each instruction once with almost no optimization. ScarletNX runs LLVM, which can use a profile.


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