x64_recomp: fix 8/16-bit inc/dec, CF preservation, and inc/dec/neg AF (X64-RECOMP-INCDEC-WIDTH-CF) #1313
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1313"
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-24T21:43:27Z.
Item ID:
X64-RECOMP-INCDEC-WIDTH-CFAI-assisted: yes
Summary
Fixes five correctness bugs in
tools/x64_recomp.py(_emit_incdecand_emit_negnot):inc/decadded/subtracted1after shiftingT0left by 24/16:_emit_incdecshiftedT0left bysh = 32 - sz * 8, then emittedadds/subswith immediate1and shifted right bysh.inc aland 16-bitinc axcomplete no-ops (((v << 24) + 1) >> 24 == v) and brokeZFon 8/16-bitdec((1 << 24) - 1 == 0x00FFFFFF, leavingZ=0).1 << sh(movz T1, 1 << (sh - 16), hw=1, sf=0->0x01000000/0x00010000) when flags are live, and using plainadd/sub+uxtb/uxthwhen flags are dead.inc/decdestroyedCFviabfi(T3, GTMP, 29, 1)copying bit 0 instead of bit 29:BFIinserts the lowest bits (bit 0, always0inMRS NZCV) ofGTMPinto bit 29 ofT3, unconditionally zeroingCFwheneverkeep_cran.a.ubfx(GTMP, GTMP, 29, 1)beforea.bfi(T3, GTMP, 29, 1).keep_c = "C" in needwas alwaysFalseon same-blockCFconsumers:"C"is not inFLAG_WRITERS["inc"]/FLAG_WRITERS["dec"]("NZVPA"),self.needed[ins.addr]never contained"C", sostc; inc eax; jcoradd; inc; adcclobbered ARMC.add/subwhennot flaggyand always preserving ARMCaroundadds/subswhenflaggy.decinverted survivingCFviaSUB_KIND:_maybe_mkflagsflipped ARM bit 29 for everySUB_KINDinstruction includingdec, even thoughdecdoes not writeCF(inverting an upstreamstc/clc/cmpcarry bit)._emit_incdecby callingself._mkflags(0)directly onins.addr in self.matwhile saving/restoring bit 0 (EF_CF) ofctx.eflagsinGTMP(preserved acrossrt_mkflags).AFoperand stashing oninc/dec/neg, andnotrunning_stash_res/_maybe_mkflags:CTX_A/CTX_Bforinc/dec(orig,1) andneg(0,orig) whenever"A" in need or ins.addr in self.matsort_mkflagscomputes exactAF.notreturn immediately after_storeso it never touches flags or context flag slots.Merge compatibility
_emit_incdecand_emit_negnot(lines 1655–1698 onmain).#1174,#1202,#1205,#1211,#1213,#1279,#1296, and#1306.Verification
Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files):
python3 -I tests/test_x64_recomp.py— 32 tests (6 ok, 26 skipped without runner)python3 -I tests/test_x64_recomp_incdec.py -v— 8 tests (5 static ok, 3 exec skipped without runner)tools/x64_recomp.pytoupstream/maincauses 5/5 static test failures intests/test_x64_recomp_incdec.py.#1174,#1202,#1205,#1211,#1213,#1279,#1296,#1306— 0 merge conflicts):tests/test_x64_recomp_incdec.py— 8/8 OK (all 3 execution suites + 5 static tests, 0 skips)tests/test_x64_recomp.py— 46/46 OKtests/test_x64_recomp_sse.py— 16/16 OKtests/test_x64_recomp_bitops.py— 9/9 OKtests/test_x64_recomp_rcl_rcr.py— 5/5 OKtests/test_x64_recomp_std_str.py— 5/5 OKtests/test_x64_recomp_muldiv.py— 10/10 OKtests/test_x64_recomp_shifts.py— 9/9 OKtests/test_x64_recomp_atomics.py— 10/10 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_incdec.py— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_incdec.py— OKpython3 -I tests/test_ci_registration.py— 15/15 OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKImported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T21:43:38Z.
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 whoalin1 on 2026-11-24T23:24:40Z.
Independent verification (Devin, AI-assisted — CPU-only, no GPU).
Checked out head
7e38867detached. Read the diff in tools/x64_recomp.py:1 << (32 - width*8)instead of 1, then mask back — matches x86 semantics.Ran:
py -3.14 -I tests/test_x64_recomp_incdec.py→ 8 tests, OK (3 skipped: native-execution tests need Apple Silicon/QEMU, unavailable here)pytest tests/test_x64_recomp_incdec.py→ 5 passed, 3 skippedpy -3.14 -m compileall -q tools tests make_game.py→ cleanVerdict: static encoding/semantics checks pass; execution coverage is skipped on this Windows host, so flag behavior is verified by inspection + encoding tests only. No game files needed. Looks good to merge pending a run on a host with QEMU/ARM.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T00:52:33Z.
Independent run with QEMU, to add the executed half to the review above. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat61732aca1into the branch locally (907 commits behind, no conflicts). Head tested:7e3886751.Against a reference model: 202 of 1,032 checks are wrong on
main, 0 of 1,032 on this branch.The new test file has the wiring bug from #1409: as shipped under QEMU,
python -I tests/test_x64_recomp_incdec.pyis 8 run, 17 failures, because_runner's QEMU branch returnsxr.run_kernel(raw bytes) wherexr.run_result(the parsed value) is needed. WithRUNNERswapped forxr.run_resultin memory (no file changed): 8/8.Beyond the file, generated cases under QEMU, each compared against a small Python model:
inc,dec,neg,notonrax/eax/ax/al;0x7f,0x80,0xff,0x100, and at each width the largest positive value, the most negative value, their neighbours and all-ones;neg; and forinc/decthat CF survives from a precedingstc,clc, borrowingcmpand non-borrowingcmp. Fornot, that CF survivesstc/clc.mainOn
mainthe 8- and 16-bit forms are the bulk of it:inc alleaves the value unchanged (…a500stays…a500, want…a501), SF/OF/ZF follow from that, and CF is not preserved acrossinc/dec(afterstca followingsetcreads 0).I left 24 more checks out of both rows: the ones whose operand is 64-bit all-ones. Loading that with
mov rax, imm64hangs onmainand here alike (Asm.li(-1), fixed by #1339 and #1205), so they say nothing about this change.Counts.
tests/test_x64_recomp.py33/33 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 508 pass; the 6 that fail aremain's 5 plus the new file, for the wiring reason.Not tested: memory destinations, AF and PF, and the
lockprefix. No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:38:40Z.
Red/green: tests/test_x64_recomp_incdec.py fails 4 static emitter assertions on main (8/16-bit inc/dec delta, CF preservation, AF stash) and passes with the PR (cloud run on main
7daa4a4; execution tests skipped).Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
7daa4a4a5in GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)