x64_recomp: preserve ARM NZCV in _maybe_mkflags and ctx.eflags in _emit_flagctl #1379
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1379"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T22:56:37Z.
Item ID:
X64-RECOMP-SUB-MAT-FLAGCTLSummary
Fixes two flag-materialization bugs in
tools/x64_recomp.py:_maybe_mkflagsinverted live ARMNZCV.Cin-place for materializedSUB_KINDinstructions:_maybe_mkflagsflipped bit 29 of ARMNZCV(eor_i(GTMP, GTMP, 0x20000000); msr_nzcv(GTMP)) before callingrt_mkflags(0)and never restored it — even thoughrt_mkflagsalready implementskind == 1(SUB_KIND) to invert ARM borrow intoctx.eflagswhile preservingNZCVinx5.SUB_KINDinstruction (cmp,sub,sbb,neg,cmps,scas) materialized flags for a downstream block and also fed a same-block carry consumer viaarm_path, the same-block consumer assumedSUB_KINDARM polarity (!CF) and got the inverted carry flag.cmp a, b; jb less; je equal), wherejein the fallthrough block forcescmpintoself.mat: onmain,(3 < 5)jumped togreaterand(9 > 5)jumped toless.kind = 2 if ins.mnem in LOGIC_KIND else (1 if ins.mnem in SUB_KIND else 0)toself._mkflags(kind)without touching ARMNZCV._emit_flagctl(clc,stc,cmc) overwrote the entirectx.eflagsword:_emit_flagctlended withself._maybe_mkflags(ins), callingrt_mkflags(0)and rewritingSF,ZF,OF,PF, andAFinctx.eflagsfrom currentNZCVandCTX_RES/CTX_A/CTX_Binstead of updating onlyCF.ins.addr in self.mat, extract bit 29 (C) from ARMNZCVand patch only bit 0 (EF_CF) ofctx.eflagsviabfi.Tests (
tests/test_x64_recomp_flagctl_mat.py)_maybe_mkflagspasseskind = 1(movz w0, #1) without invertingGTMP/NZCV, and verifies materializedclc/stc/cmcpatchEF_CFwithout emitting a secondbl rt_mkflags.cmp; jb; jeacross 7(a, b)pairs (small, equal, signed-boundary0x7FFFFFFFvs0x80000000).subwith same-blocksetc+ cross-blocksets, andsub; cmc; jmp +0flag checks.clc/stc/cmcpreservingOF/SF/ZFaftersub,popf, andxor(LOGIC_KIND).Verification
python3 -I tests/test_x64_recomp_flagctl_mat.py— 5 tests (2 static OK, 3 exec skipped without runner)python3 -I tests/test_x64_recomp.py— OKpython3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_flagctl_mat.py— OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKupstream/main(0f98d193c+ #1174 runner without this fix):cmp; jb; je,sub; setc; jmp; sets,sub; cmc, and static assertion failed onmain).x64_recompPRs, 0 conflicts):tests/test_x64_recomp_flagctl_mat.py: 5/5 passed (0 skipped)test_x64_recomp_*.pysuites combined: 128/128 passed (0 skipped)AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T22:56:47Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T23:15:47Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainatffcfe68f1into the branch locally (830 commits behind, no conflicts). Head tested:5d9101482.The fix is real, and the bug it fixes on
mainis a serious one. The new test file has the wiring bug from #1409, so as shipped it cannot show that.As shipped under QEMU,
python -I tests/test_x64_recomp_flagctl_mat.py: 5 run, 14 failures, all of the shapeb'R=0000000a\n' != 10._runner's QEMU branch returnsxr.run_kernel(raw bytes) wherexr.run_result(parsed value) is needed. Suggested fix: usexr.run_resultin both branches.With
RUNNERswapped forxr.run_resultin memory (no file changed), the same five tests:main(tools/x64_recomp.pyfromffcfe68f1, this file's tests)On
main, the three-way unsigned comparecmp a, b; jb less; je equaltakes the wrong arm in all seven cases the test tries:main7fffffff,8000000080000000,8000000080000000,7fffffffThe
sub; setcandsub; cmccross-block cases are wrong onmainin 6 of the subtests and right here, and the two staticclc/stcchecks fail onmainand pass here.Counts.
tests/test_x64_recomp.py33/33 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 512 run, 507 pass; the 5 that fail aremain's 4 plus the new file, for the wiring reason above.I ran the pull request's own cases and did not write additional ones for this change. No game files used. AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:34:10Z.
Red/green: classified READY by dada-lotF (red on main, green on head merged with main); head
5d91014825unchanged since 2026-11-24, before that run.Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
d48a957bbin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)