Files
mandy/docs/mobile.md
Nolan Cotter a2070dfa10 tools: x86-64 -> arm64 recompiler groundwork for the mobile path
tools/x64_recomp.py is the start of the static recompiler the arm64 port
needs: an x86-64 decoder (prefixes/REX, ModRM+SIB, RIP-relative, hi8
regs, immediate forms, groups 1-5/8/15/1A/11) and an arm64 assembler
with label fixups. tools/recomp_census.py counts how much of a PE's
.pdata-declared code the decoder covers. docs/mobile.md records the
route (AOT lift of .text to arm64, macOS first, MoltenVK or SPIRV-Cross
for graphics) and what still blocks iOS.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-23 13:47:00 -07:00

5.8 KiB
Raw Blame History

Mobile / arm64 port notes

The PC port works because the Xbox Series CPU is x86-64, the same ISA a PC runs; runtime/ maps the PE image and executes it natively. On Apple silicon that stops working for exactly one reason: the guest is x86-64 and the host is arm64. Everything else (PE loading, import shims, D3D12→graphics, assets) is a port of layers that already exist. This note records the design and the current state of the x86-64 → arm64 groundwork.

Route

  • Static/AOT recompilation is the only clean iOS path: lift the exe's .text to arm64 ahead of time and ship a plain arm64 binary — no JIT entitlement, no sandbox fights. .pdata gives exact function boundaries (the same table RtlAddFunctionTable uses for SEH on the PC side), so the recompiler can enumerate code without guessing; .xdata unwinding has to be re-emitted as arm64 unwind info or handled in the translated prologue/epilogue, which is the genuinely hard part.
  • Dynamic translation (FEX/Box64-style) is the fallback on Android, where JIT and Vulkan are unrestricted; on iOS it needs com.apple.developer.cs.jit or workarounds and is not the plan.
  • arm64 macOS first, iOS second. Same ISA, same Metal, same Mach kernel, but no entitlement fights and debuggable. If "mobile" matters more than "iOS", Android is dramatically easier: native Vulkan (the whole GPU layer ports) plus unrestricted JIT.
  • Graphics on Apple: MoltenVK over the existing Vulkan backend for a first pixel (no conservative raster / geometry shaders in Metal — needs fallbacks), or SPIRV-Cross → MSL for a native Metal backend longer term.

What exists today

Piece Where State
x86-64 decoder tools/x64_recomp.py disasm/decode_one prefixes/REX, ModRM+SIB, RIP-relative and moffs addressing, hi8 regs (ah/ch/dh/bh), Iv/Iz/J immediates incl. imm64, groups 1–5/8/15/1A/11, VEX stubs
arm64 assembler tools/x64_recomp.py Asm integer/logical/bitfield/shift/multiply/load-store/branch/system encoders, label fixups, li() with pad4 for self-referential constant materialization
Translator tools/x64_recomp.py FunctionTranslator linear-sweep over decoded insns; lazy flag analysis picks arm-NZCV when one producer covers a consumer, else materializes canonical x86 EFLAGS in the ctx block; unsupported insns emit brk and land in stats["unhandled"]
Dispatch runtime tools/x64_recomp.py emit_rt rt_dispatch (guest pc → host pc through a generated table), rt_mkflags (NZCV+operands → canonical x86 EFLAGS; preserves NZCV), rt_print (semihosting result output)
Verification tools/x64_recomp.py build_test_kernel/run_kernel flat kernel image → qemu-system-aarch64 -M virt -cpu cortex-a53 -kernel -semihosting; guest ret off the sentinel stack exits and prints R=xxxxxxxx (guest edi)
PE census tools/recomp_census.py walks every .pdata function, decodes + translates it, reports coverage %, hazard mnemonics, indirect control flow, unwind-info flags
Tests tests/test_x64_recomp.py, tests/test_recomp_census.py hand-built byte fixtures only; exec tests skip when qemu is absent

Translator contract (as implemented)

  • Guest regs rax–r15 → host x19–x27, x9–x15 (GMAP); x28 = guest ctx pointer, x16 = dispatch target, x17/x30 scratch/link, x0–x3 temps.
  • Ctx block: +0x00 canonical x86 EFLAGS dword, +0x08 last result, +0x10/+0x18 last operands (for AF/PF reconstruction), +0x38 the semihosting exit block in test images.
  • Flag model: arithmetic runs on arm NZCV directly; a conditional reads arm NZCV only when one flag-writing producer covers every flag it needs (_flag_analysis, _producer_ok); otherwise producers materialize x86 EFLAGS through rt_mkflags and consumers read the ctx word. Sub-kind producers carry inverted C (arm borrow ≠ x86 borrow); popf/sahf reconstruct NZCV from the x86 word; rt_mkflags restores NZCV before returning so calls can't clobber it.
  • Control flow: direct branches/calls become arm b; ret, jmp/call reg and unknown targets load the guest pc into x16 and go through rt_dispatch; guest pc 0 is the exit sentinel.

Status and honest limits

  • Hand-written fixtures run end-to-end under qemu: arithmetic, loops, taken/not-taken signed and unsigned branches, setcc/cmov, adc/sbb carry chains, shifts with CF, movsx, high-byte regs, call/ret/leave/pushf/popf, rep movsb, repne scasb, moffs/LEA memory addressing, and indirect jumps through the pc table — all hand-assembled, no game bytes.
  • Not covered yet (each is an unhandled entry at translate time, not silent wrongness): SIMD/SSE beyond decode, x87, rcl/rcr, std (DF=1), system/privileged insns, int/syscall, far transfers. Real binaries are x87/SIMD-heavy, so coverage on the engine image will be far below the fixture suite until those lands — recomp_census.py is the tool that will measure it.
  • The engine image itself is still not in the repository (the exe obtained so far is a packaging probe — docs/boot.md), so nothing here has run against real code. First real milestone is the census over a genuine PE, then a same-behaviour run of a real function under qemu against a native x86-64 run.

Next steps

  1. recomp_census.py on the real image: instruction histogram, coverage %, hazard list.
  2. SIMD/SSE emitters (the largest hazard class on any modern binary), then std/RCL-RCR.
  3. SEH story: translate .xdata prologue ops or run translated code inside a frame the host unwinder can walk — needed before C++ throw works.
  4. arm64 userland runner (Mach-O + mmap JIT-free) to replace the qemu harness on Apple silicon.
  5. Vulkan backend under MoltenVK on macOS as the first graphics target; Metal backend later.