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

Open
opened 2026-11-26 20:21:19 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1428
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/x64-recomp-short-xchg-movnti


📝 Commits (1)

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

📊 Changes

2 files changed (+191 additions, -0 deletions)

View changed files

➕ tests/test_x64_recomp_xchg_movnti.py (+182 -0)
📝 tools/x64_recomp.py (+9 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1428 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-short-xchg-movnti` --- ### 📝 Commits (1) - [`41db945`](https://github.com/ubiquitarianism/leonida/commit/41db9451357e52ba9cab7fbde4749d03adfa2e7a) x64_recomp: fix short-form xchg (0x91..0x97) and movnti (0F C3) widths/emission (X64-RECOMP-SHORT-XCHG-MOVNTI) ### 📊 Changes **2 files changed** (+191 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_xchg_movnti.py` (+182 -0) 📝 `tools/x64_recomp.py` (+9 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1458
No description provided.