Files
mandy/docs/mobile.md
Jan Trencansky 4a1c850a7a x64_recomp: treat a shift by cl as a writer that may leave the flags alone (#2150 review)
x86 changes no flag for a shift or rotate whose masked count is 0. The
first version patched ARM's carry when cl is 0, taking CF from ctx when no
carry writer came before the shift in its block. But flag liveness treated
the shift as always writing, so a writer in an earlier block never stored
its word, and ctx held a stale CF: cmp r8d, 2 (r8d = 1); jmp; mov cl, 0;
shl eax, cl; setc read 0 instead of 1.

_flag_analysis now treats a shift by cl as a conditional writer: liveness
does not kill the flags it writes, the writer before it stores its word as
well, and a consumer behind it reads the ctx word. With cl = 0 the shift
is skipped whole, its flag store included, so ctx keeps the earlier word.
The ARM-carry patching and shift_prev_c go; the immediate-count-0 change
stays.

Tests: the analysis checks of the patching are replaced by checks of the
new rule, and three execution cases are added (a carry from an earlier
block with cl = 0 and cl = 1, and one a logic op cleared). The first one
fails on the previous head.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 19:37:25 -05:00

6.0 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; a shift or rotate by cl may leave the flags alone (x86 changes none for a count of 0), so a consumer behind one reads the ctx word and the writer before it stores its word too; 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.