NFSU.cc / Devlog

Static Recompiler Optimizations: Dead x86 Flag Pruning, CHK_SP Coalescing, and Fast x87

Naive x86-to-C static recompilation spends most of its CPU budget updating EFLAGS nobody reads and bounds-checking ESP on every push and pop. Here is how tools/recomp/lift.py eliminates both while staying verified by difftest.py.

Static Recompilationx86WebAssemblyCompilersDifftest
Commits: 8444027 · 69f51fb · fc8ef2e View raw Markdown (.md)

The cost of faithful x86 lifting

pc/Speed.exe (3.18 MB, 2003 PC) contains 13,764 functions lifted ahead of time into C by tools/recomp/lift.py and compiled to web/out/speed.wasm (42.31 MB uncompressed, ~11.1 MB brotli). In early builds, every arithmetic instruction computed CF, ZF, SF, OF, and PF, and every stack/memory access emitted a runtime bounds check.

1. Liveness-based dead guest flag pruning (8444027)

In x86 code, the vast majority of flag writes are overwritten by the next add, sub, xor, or inc before any conditional branch (jcc), cmov, setcc, or adc/sbb ever reads them.

2. CHK_IMG and straight-line CHK_SP coalescing (69f51fb, fc8ef2e)

Guest memory safety in host/rt.h prevents a bug in lifted x86 from corrupting the Emscripten host heap, but checking every individual stack slot inside a function prologue/epilogue is wasteful:

3. RN_FAST_X87 verified against RN_STRICT_X87 (69f51fb)

x87 floating-point stack emulation is a classic hotspot in 2003 PC racing physics (physics.md, accel.md). host/rt.h now splits x87 execution into:

Files in repo: tools/recomp/lift.py · tools/recomp/build.py · tools/recomp/abk.py · tools/recomp/difftest.py · host/rt.h