mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 14:08:38 +00:00
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>
5.8 KiB
5.8 KiB
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
.textto arm64 ahead of time and ship a plain arm64 binary — no JIT entitlement, no sandbox fights..pdatagives exact function boundaries (the same tableRtlAddFunctionTableuses for SEH on the PC side), so the recompiler can enumerate code without guessing;.xdataunwinding 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.jitor 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→ hostx19–x27,x9–x15(GMAP);x28= guest ctx pointer,x16= dispatch target,x17/x30scratch/link,x0–x3temps. - Ctx block:
+0x00canonical x86 EFLAGS dword,+0x08last result,+0x10/+0x18last operands (for AF/PF reconstruction),+0x38the 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 throughrt_mkflagsand consumers read the ctx word. Sub-kind producers carry inverted C (arm borrow ≠ x86 borrow);popf/sahfreconstruct NZCV from the x86 word;rt_mkflagsrestores NZCV before returning so calls can't clobber it. - Control flow: direct branches/calls become arm
b;ret,jmp/call regand unknown targets load the guest pc intox16and go throughrt_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/sbbcarry 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
unhandledentry 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.pyis 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
recomp_census.pyon the real image: instruction histogram, coverage %, hazard list.- SIMD/SSE emitters (the largest hazard class on any modern binary), then
std/RCL-RCR. - SEH story: translate
.xdataprologue ops or run translated code inside a frame the host unwinder can walk — needed before C++throwworks. - arm64 userland runner (Mach-O +
mmapJIT-free) to replace the qemu harness on Apple silicon. - Vulkan backend under MoltenVK on macOS as the first graphics target; Metal backend later.