x64_recomp: fix short-form xchg (0x91..0x97) and movnti (0F C3) widths/emission (X64-RECOMP-SHORT-XCHG-MOVNTI) #1428
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1428"
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-25T00:02:48Z.
Item ID:
X64-RECOMP-SHORT-XCHG-MOVNTIAI-assisted: yes
Summary
Fixes two bugs in
tools/x64_recomp.py:xchg rAX, r(0x91..0x97) decoded asOq Oq(a 64-bit self-swap no-op):PRIMARY[0x91..0x97]had opspec"Oq Oq", which decoded both operands as the opcode register(opcode & 7) | (rexb << 3)at fixed 64-bit width — makingxchg eax, r/xchg ax, r/xchg rax, rswaprwith itself and never touchRAX, while also ignoring 32-bit zero-extension and 16-bit upper-48-bit preservation.xchgshort-form opspec ("Oq Oq"or"Oq Rq"from #847) to"Ov Av"indecode_onebefore_finish()gives the exact opcode register (O, REX.B-extended) and fixed accumulator (A=r0, never REX.B-extended) at operand widthv(2/4/8 bytes), with zero merge conflict against #847 (X64-RECOMP-PRIMARY-90).movnti(0F C3) width and emission:OF[0xC3]had opspec"Ev Gv", so66 0F C3decoded as 16-bit even though x86-64movntiignores0x66and only has 32-bit (0F C3) and 64-bit (48 0F C3) memory-store forms.decode_onenow sets"Eq Gq" if rexw else "Ed Gd"formovnti, and_emit_movemits memorymovntistores directly (and rejects non-memorymod=3forms).Verification (macOS Apple M5, arm64, Python 3.14; no game files)
python3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_xchg_movnti.py— OKpython3 -I tests/test_x64_recomp_xchg_movnti.py(standalone) — 6 decode/translate tests OK (5 exec tests skipped without #1174 runner)python3 -I tests/test_text_write_line_endings.py— 3/3 OKpython3 -I tests/test_ci_registration.py— 16/16 OKupstream/main364ab4eab+ #1174 runner + new test file):Ran 11 tests — FAILED (failures=10, errors=1)(all 11 decode + native execution tests fail without this fix).x64_recompPR branches + this PR): 0 conflicts, all 24tests/test_x64_recomp*.pysuites (208/208 tests OK, 0 skips).pull/847/headon top of the 24-way merge with 0 conflicts; verified41 90,66 41 90,49 90, and66 48 91all decode with exact operand widths.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T00:03:00Z.
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-25T22:37:03Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat1bf5eeafdinto the branch locally (713 commits behind, no textual conflicts). Head tested:41db94513.The change is right on every case I ran, but it now disagrees with a test that landed on
mainafter this branch was cut, and its own exec tests never run.1. Merged with current
main,tests/test_x64_recomp.pygoes 33/33 to 32/33.DecodeTests.test_opcode_90_nop_pause_and_rex_xchg(from #847, merged asbe7478f5b) expects41 90to decode with 8-byte operands:This branch has the x86 answer:
41 90has no REX.W, so it isxchg r8d, eax. It ismain's expectation that is off, andmainexecutes it that way too (table below). So the test needs updating in whichever lands second; after a merge ofmainhere that is one assertion:(("r", 8, 4, False), ("r", 0, 4)).2. The five exec tests are skipped everywhere on this branch, QEMU included.
CAN_EXECrequiresxr.CAN_RUN_MACHO, which only exists with #1174. The tests callxr.run_result, which works under QEMU onmaintoday. As shipped: 11 run, 5 skipped. With the class skip lifted in memory (no file changed): 11/11 pass under QEMU. Suggested gate: runnable whenCAN_RUN_MACHOor QEMU is found, astests/test_x64_recomp.pydoes withHAS_QEMU.Under QEMU, rax=
aaaabbbb11112222, other register=ccccdddd33334444, reading rax afterwards:main91(xchg ecx,eax)0000000033334444aaaabbbb11112222(no swap)000000003333444448 91(xchg rcx,rax)ccccdddd33334444aaaabbbb11112222(no swap)ccccdddd3333444441 90(xchg r8d,eax)0000000033334444ccccdddd33334444(64-bit swap)000000003333444441 90, then r80000000011112222aaaabbbb11112222000000001111222266 41 90(xchg r8w,ax)aaaabbbb11114444ccccdddd33334444aaaabbbb1111444449 90(xchg r8,rax)ccccdddd33334444ccccdddd33334444ccccdddd333344449090andf3 90still decode asnopandpause.Counts.
tests/test_recomp_census.py11/11. Full standalone suite: 512 run, 507 pass; the 5 that fail aremain's 4 plustest_x64_recomp.pyfor the reason in point 1.No game files used. AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T22:29:14Z.
Native Windows 11 Verification &
mainMerge-Interaction Catch (41db945135)Verified
41db9451357e52ba9cab7fbde4749d03adfa2e7amerged ontoorigin/main(adfb3821a23612263c1c5b195453ed01c4c114db,git merge-tree --write-treerc=0) on native Windows 11 (26200):tests/test_x64_recomp_xchg_movnti.py):origin/mainadfb3821a):FAILED (failures=5, skipped=5)out of11tests (0x91..0x97short-formxchgdecoded as 64-bit"Oq Rq"instead of"Ov Av"and0F C3movntiwas unhandled).origin/main + 41db945135):python -I tests/test_x64_recomp_xchg_movnti.pypassed (11 ran, OK (skipped=5)— 16/32/64-bitxchg,REX.Br8..r15with unextendedeax/ax/rax, andmovnti32/64-bit + ignored0x66prefix).tests/test_x64_recomp.pyon currentmain:python -I tests/test_x64_recomp.pyonorigin/main + 41db945135fails 1 existing assertion attests/test_x64_recomp.py:88-90(DecodeTests.test_opcode_90_nop_pause_and_rex_xchg):mainencoded the old pre-fix 64-bit operand size for41 90(xchg r8d, eax, whereREX.W == 0so the architectural operand size is 32-bit4bytes, whereas49 90is 64-bitxchg r8, rax). Updatingtests/test_x64_recomp.py:88-90to expect(('r', 8, 4, False), ('r', 0, 4))for41 90(and optionally adding49 90for the 64-bit case) will maketest_x64_recomp.pypass cleanly on merge.AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T00:17:29Z.
Tested
41db94513merged (no commit) withmain829530dbd: Python 3.12.4, Windows 11 10.0.26300, CPU only.check_no_game_data.pyis clean.tests/test_x64_recomp_xchg_movnti.py, 11 tests pass (5 QEMU tests skipped).tools/x64_recomp.pyback tomain, the 5 encoding checks fail (e.g.(('r', 9, 8, False), ('r', 9, 8, False)) != (('r', 9, 4, False), ('r', 0, 4))). The QEMU run itself is not checked here.No blocker from my side.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T11:52:01Z.
Tested green here earlier on this same head (see my comment above). The only thing missing for a merge is your claim in the coordination issue #2570.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T14:18:39Z.
Ready for testing: merges cleanly with
main3535eb5c4. Not inmainyet:git cherryfinds 1 commit(s) not there, and 1 of the 126 lines it adds are already present. No duplicate found: its claimX64-RECOMP-SHORT-XCHG-MOVNTIappears in no other open pull request and in nomaincommit.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T15:51:58Z.
RUN report for #1428 (X64-RECOMP-SHORT-XCHG-MOVNTI)
Heads: PR head
41db94513(base364ab4eab); origin/mainc22a0a732, merged locally withgit merge --no-ff(clean, 2 files).Kind: Python (
tools/x64_recomp.pyplus a new test file).Commands (one process per file):
python -I -B tests/<file>fortest_x64_recomp_xchg_movnti.py, everytests/test_x64_recomp*.py(19 files on current main),test_text_write_line_endings.py,test_ci_registration.py.Results:
tools/x64_recomp.pyreverted to the PR base, new test kept; behavioural, there is no new API):test_x64_recomp_xchg_movnti.pyRan 11, FAILED (failures=6, skipped=5).test_x64_recomp_xchg_movnti.pyRan 11, OK (skipped=5);test_text_write_line_endings.py3 OK;test_ci_registration.py16 OK;test_x64_recomp.py(older at this base) 32 OK.native arm64 Mach-O runner unavailable); they cannot run on Windows x64. The description's 10 failures plus 1 error need that runner (macOS arm64).test_x64_recomp*.pyfiles, the new file (11 OK, 5 skipped),test_text_write_line_endings.py(3 OK) andtest_ci_registration.py(18 OK) pass, buttests/test_x64_recomp.pyfails one test:DecodeTests.test_opcode_90_nop_pause_and_rex_xchg. It passes on plain mainc22a0a732(47 tests OK, 34 skipped) and fails only with the PR merged.41 90expects("xchg", (("r", 8, 8, False), ("r", 0, 8, False)), 2, False), the old 8-byte width. With the PR it decodes as(("r", 8, 4, False), ("r", 0, 4)), i.e. 32-bitxchg r8d, eax, which is the correct x86-64 form of41 90and the width the PR intends. So the stale expectation intests/test_x64_recomp.pyneeds updating in this PR (it merges without textual conflict).Machine: Windows 11 Pro x64, RTX 3090 24 GB, NVIDIA 621.40, Python 3.13.12 (
python -I).Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T17:08:53Z.
Correction (my earlier verdict here was incomplete): not ready to merge.
Tested
41db94513merged (no commit) withmain4364a4fa6: Python 3.12, Windows 11 10.0.26300, CPU only.python -I tests/test_x64_recomp_xchg_movnti.pygives 11 OK (5 skipped) with the PR, andFAILED (failures=5, skipped=5)withtools/x64_recomp.pyback tomain.x64_recomptest files,DecodeTests.test_opcode_90_nop_pause_and_rex_xchgpasses onmainand fails on the merge:41 90now decodes asxchg r8d, eax(32-bit, REX.B without REX.W), while the test still expects 8 bytes. See the comment from TheGeogeo's review above. The expectation needs updating in this PR.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T17:11:42Z.
Tested
41db94513merged (no commit) with main4364a4fa6: Python 3.12.4, Windows 11 10.0.26300, CPU only.Not green once merged: an existing test on main fails with this change.
tests/test_x64_recomp_xchg_movnti.pyruns 11 tests: OK, 5 skipped (no arm64 runner here).tools/x64_recomp.pyback to main, 5 of them fail (the shortxchgwidths for 16, 32 and 64 bits, REX.B withrax, and themovntiwidths).tests/test_x64_recomp.pyfailsDecodeTests.test_opcode_90_nop_pause_and_rex_xchgon the merge (it passes on main).41 90, the merge decodes('xchg', (('r', 8, 4, False), ('r', 0, 4)), 2, False).('xchg', (('r', 8, 8, False), ('r', 0, 8, False)), 2, False).41 90is REX.B without REX.W, soxchg r8d, eaxat 32 bits. The fix is to update that expectation intest_x64_recomp.pyin this PR.tests/test_x64_recomp*.pyfiles give the same results on main and on the merge.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:41:26Z.
Review: APPROVED
Reviewed the diff against
main:tools/x64_recomp.py:616-619cleanly fixes short-formxchg(Ov Av, avoiding REX.B extendingrAX) andmovnti(Eq Gq/Ed Gd, ignoring0x66), andtools/x64_recomp.py:1804-1808emits memory-onlymovntistores, covered bytests/test_x64_recomp_xchg_movnti.py. Clean-room verified. Approving; awaiting a green CI run before merge.(AI-assisted review.)