x64_recomp: fix short-form xchg (0x91..0x97) and movnti (0F C3) widths/emission (X64-RECOMP-SHORT-XCHG-MOVNTI) #1428

Open
github-bot wants to merge 1 commit from github-bot/gh-1428 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 00:02 UTC · original
IlyassRS3:fix/x64-recomp-short-xchg-movnti → main · +191 −0 · 2 files · 1 commits · open

Item ID: X64-RECOMP-SHORT-XCHG-MOVNTI
AI-assisted: yes

Summary

Fixes two bugs in tools/x64_recomp.py:

  1. Short-form xchg rAX, r (0x91..0x97) decoded as Oq 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 — making xchg eax, r / xchg ax, r / xchg rax, r swap r with itself and never touch RAX, while also ignoring 32-bit zero-extension and 16-bit upper-48-bit preservation.
    • Normalizing xchg short-form opspec ("Oq Oq" or "Oq Rq" from #847) to "Ov Av" in decode_one before _finish() gives the exact opcode register (O, REX.B-extended) and fixed accumulator (A = r0, never REX.B-extended) at operand width v (2/4/8 bytes), with zero merge conflict against #847 (X64-RECOMP-PRIMARY-90).
  2. movnti (0F C3) width and emission:
    • OF[0xC3] had opspec "Ev Gv", so 66 0F C3 decoded as 16-bit even though x86-64 movnti ignores 0x66 and only has 32-bit (0F C3) and 64-bit (48 0F C3) memory-store forms.
    • decode_one now sets "Eq Gq" if rexw else "Ed Gd" for movnti, and _emit_mov emits memory movnti stores directly (and rejects non-memory mod=3 forms).

Verification (macOS Apple M5, arm64, Python 3.14; no game files)

  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_xchg_movnti.py — OK
  • python3 -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 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Live Apple M5 arm64 mutation proof (unpatched upstream/main 364ab4eab + #1174 runner + new test file): Ran 11 tests — FAILED (failures=10, errors=1) (all 11 decode + native execution tests fail without this fix).
  • 24-way merge on Apple M5 arm64 (#1174 + all 22 prior x64_recomp PR branches + this PR): 0 conflicts, all 24 tests/test_x64_recomp*.py suites (208/208 tests OK, 0 skips).
  • External PR #847 conflict check: merged pull/847/head on top of the 24-way merge with 0 conflicts; verified 41 90, 66 41 90, 49 90, and 66 48 91 all decode with exact operand widths.
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1428) `IlyassRS3:fix/x64-recomp-short-xchg-movnti` → `main` · +191 −0 · 2 files · 1 commits · open Item ID: `X64-RECOMP-SHORT-XCHG-MOVNTI` AI-assisted: yes ### Summary Fixes two bugs in `tools/x64_recomp.py`: 1. **Short-form `xchg rAX, r` (`0x91..0x97`) decoded as `Oq 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 — making `xchg eax, r` / `xchg ax, r` / `xchg rax, r` swap `r` with itself and never touch `RAX`, while also ignoring 32-bit zero-extension and 16-bit upper-48-bit preservation. - Normalizing `xchg` short-form opspec (`"Oq Oq"` or `"Oq Rq"` from #847) to `"Ov Av"` in `decode_one` before `_finish()` gives the exact opcode register (`O`, REX.B-extended) and fixed accumulator (`A` = `r0`, never REX.B-extended) at operand width `v` (2/4/8 bytes), with zero merge conflict against #847 (`X64-RECOMP-PRIMARY-90`). 2. **`movnti` (`0F C3`) width and emission**: - `OF[0xC3]` had opspec `"Ev Gv"`, so `66 0F C3` decoded as 16-bit even though x86-64 `movnti` ignores `0x66` and only has 32-bit (`0F C3`) and 64-bit (`48 0F C3`) memory-store forms. - `decode_one` now sets `"Eq Gq" if rexw else "Ed Gd"` for `movnti`, and `_emit_mov` emits memory `movnti` stores directly (and rejects non-memory `mod=3` forms). ### Verification (macOS Apple M5, arm64, Python 3.14; no game files) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_xchg_movnti.py` — OK - `python3 -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 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Live Apple M5 arm64 mutation proof** (unpatched `upstream/main` `364ab4eab` + #1174 runner + new test file): `Ran 11 tests — FAILED (failures=10, errors=1)` (all 11 decode + native execution tests fail without this fix). - **24-way merge on Apple M5 arm64** (#1174 + all 22 prior `x64_recomp` PR branches + this PR): **0 conflicts**, all 24 `tests/test_x64_recomp*.py` suites (**208/208 tests OK, 0 skips**). - **External PR #847 conflict check**: merged `pull/847/head` on top of the 24-way merge with **0 conflicts**; verified `41 90`, `66 41 90`, `49 90`, and `66 48 91` all decode with exact operand widths.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:51:04 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 00:03 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 00:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1428#pullrequestreview-6049448457) · **reviewed** 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.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-25 22:37 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 1bf5eeafd into 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 main after this branch was cut, and its own exec tests never run.

1. Merged with current main, tests/test_x64_recomp.py goes 33/33 to 32/33. DecodeTests.test_opcode_90_nop_pause_and_rex_xchg (from #847, merged as be7478f5b) expects 41 90 to decode with 8-byte operands:

- ('xchg', (('r', 8, 4, False), ('r', 0, 4)), 2, False)      <- this branch
+ ('xchg', (('r', 8, 8, False), ('r', 0, 8, False)), 2, False)  <- main's test

This branch has the x86 answer: 41 90 has no REX.W, so it is xchg r8d, eax. It is main's expectation that is off, and main executes it that way too (table below). So the test needs updating in whichever lands second; after a merge of main here 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_EXEC requires xr.CAN_RUN_MACHO, which only exists with #1174. The tests call xr.run_result, which works under QEMU on main today. As shipped: 11 run, 5 skipped. With the class skip lifted in memory (no file changed): 11/11 pass under QEMU. Suggested gate: runnable when CAN_RUN_MACHO or QEMU is found, as tests/test_x64_recomp.py does with HAS_QEMU.

Under QEMU, rax=aaaabbbb11112222, other register=ccccdddd33334444, reading rax afterwards:

bytes want main this branch
91 (xchg ecx,eax) 0000000033334444 aaaabbbb11112222 (no swap) 0000000033334444
48 91 (xchg rcx,rax) ccccdddd33334444 aaaabbbb11112222 (no swap) ccccdddd33334444
41 90 (xchg r8d,eax) 0000000033334444 ccccdddd33334444 (64-bit swap) 0000000033334444
41 90, then r8 0000000011112222 aaaabbbb11112222 0000000011112222
66 41 90 (xchg r8w,ax) aaaabbbb11114444 ccccdddd33334444 aaaabbbb11114444
49 90 (xchg r8,rax) ccccdddd33334444 ccccdddd33334444 ccccdddd33334444
90 unchanged unchanged unchanged

90 and f3 90 still decode as nop and pause.

Counts. tests/test_recomp_census.py 11/11. Full standalone suite: 512 run, 507 pass; the 5 that fail are main's 4 plus test_x64_recomp.py for the reason in point 1.

No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 22:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1428#issuecomment-7176583376) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `1bf5eeafd` into 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 `main` after this branch was cut, and its own exec tests never run. **1. Merged with current `main`, `tests/test_x64_recomp.py` goes 33/33 to 32/33.** `DecodeTests.test_opcode_90_nop_pause_and_rex_xchg` (from #847, merged as `be7478f5b`) expects `41 90` to decode with 8-byte operands: ``` - ('xchg', (('r', 8, 4, False), ('r', 0, 4)), 2, False) <- this branch + ('xchg', (('r', 8, 8, False), ('r', 0, 8, False)), 2, False) <- main's test ``` This branch has the x86 answer: `41 90` has no REX.W, so it is `xchg r8d, eax`. It is `main`'s expectation that is off, and `main` executes it that way too (table below). So the test needs updating in whichever lands second; after a merge of `main` here 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_EXEC` requires `xr.CAN_RUN_MACHO`, which only exists with #1174. The tests call `xr.run_result`, which works under QEMU on `main` today. As shipped: 11 run, 5 skipped. With the class skip lifted in memory (no file changed): **11/11 pass under QEMU.** Suggested gate: runnable when `CAN_RUN_MACHO` or QEMU is found, as `tests/test_x64_recomp.py` does with `HAS_QEMU`. **Under QEMU**, rax=`aaaabbbb11112222`, other register=`ccccdddd33334444`, reading rax afterwards: | bytes | want | `main` | this branch | |---|---|---|---| | `91` (xchg ecx,eax) | `0000000033334444` | `aaaabbbb11112222` (no swap) | `0000000033334444` | | `48 91` (xchg rcx,rax) | `ccccdddd33334444` | `aaaabbbb11112222` (no swap) | `ccccdddd33334444` | | `41 90` (xchg r8d,eax) | `0000000033334444` | `ccccdddd33334444` (64-bit swap) | `0000000033334444` | | `41 90`, then r8 | `0000000011112222` | `aaaabbbb11112222` | `0000000011112222` | | `66 41 90` (xchg r8w,ax) | `aaaabbbb11114444` | `ccccdddd33334444` | `aaaabbbb11114444` | | `49 90` (xchg r8,rax) | `ccccdddd33334444` | `ccccdddd33334444` | `ccccdddd33334444` | | `90` | unchanged | unchanged | unchanged | `90` and `f3 90` still decode as `nop` and `pause`. **Counts.** `tests/test_recomp_census.py` 11/11. Full standalone suite: 512 run, 507 pass; the 5 that fail are `main`'s 4 plus `test_x64_recomp.py` for the reason in point 1. No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1428/head:github-bot/gh-1428'
git switch 'github-bot/gh-1428'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1428'
git switch 'github-bot/gh-1428'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1428'
git switch 'github-bot/gh-1428'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1428'
git switch 'main'
git merge --squash 'github-bot/gh-1428'
git switch 'main'
git merge --ff-only 'github-bot/gh-1428'
git switch 'main'
git merge 'github-bot/gh-1428'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.