x64_recomp: give REX.W precedence over 0x66 operand-size override (X64-RECOMP-REXW-OVER-66) #1409

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

@⁠IlyassRS3 on GitHub · 2026-11-24 23:37 UTC · original
IlyassRS3:fix/x64-recomp-rexw-over-66 → main · +138 −4 · 2 files · 1 commits · open

Item ID: X64-RECOMP-REXW-OVER-66

Summary

Fixes REX.W vs 0x66 operand-size override precedence in tools/x64_recomp.py:

  • _vsize(opsize, rexw): checks if rexw: return 8 before if opsize: return 2 per Intel SDM Vol. 2A §2.2.1.2. Previously 66 48 ... decoded as 16-bit instead of 64-bit, causing 66 48 B8 <imm64> (movabs) to read only 2 immediate bytes and desynchronize subsequent instructions.
  • decode_one state["opsize"]: sets state["opsize"] = 0 if rexw else opsize while leaving ins.opsize = opsize intact for mandatory-66 prefix decoders (e.g. SSE2). This ensures Iz immediates (66 48 81, 66 48 68, 66 48 69, etc.) read 4 bytes instead of 2 and string ops (66 48 A5) use 8-byte element width.

Tests & Verification

  • AI-assisted: yes
  • Host / hardware: macOS (Apple M5, arm64)
  • Standalone checks on upstream/main (3a0af1f15):
    • 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_rexw_66.py — OK
    • python3 -I tests/test_x64_recomp_rexw_66.py -v — 2 static OK (1 exec skips cleanly without #1174 runner)
    • python3 -I tests/test_x64_recomp.py — 6 static OK (26 exec skipped)
    • 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 mutation proof (tests/test_x64_recomp_rexw_66.py on unpatched upstream/main + #1174): 2 failures, 1 error.
  • Native Apple M5 execution (23-way scratch merge on upstream/main 3a0af1f15 with #1174 + all 21 other open x64_recomp PR branches):
    • 0 merge conflicts across all 23 branches
    • tests/test_x64_recomp_rexw_66.py: 3/3 OK (0 skipped) on native Apple M5 arm64
    • All 22 tests/test_x64_recomp_*.py suites combined: 151/151 OK (0 skipped)
**@⁠IlyassRS3** on GitHub · 2026-11-24 23:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1409) `IlyassRS3:fix/x64-recomp-rexw-over-66` → `main` · +138 −4 · 2 files · 1 commits · open Item ID: `X64-RECOMP-REXW-OVER-66` ## Summary Fixes `REX.W` vs `0x66` operand-size override precedence in `tools/x64_recomp.py`: - **`_vsize(opsize, rexw)`**: checks `if rexw: return 8` before `if opsize: return 2` per Intel SDM Vol. 2A §2.2.1.2. Previously `66 48 ...` decoded as 16-bit instead of 64-bit, causing `66 48 B8 <imm64>` (`movabs`) to read only 2 immediate bytes and desynchronize subsequent instructions. - **`decode_one` `state["opsize"]`**: sets `state["opsize"] = 0 if rexw else opsize` while leaving `ins.opsize = opsize` intact for mandatory-`66` prefix decoders (e.g. SSE2). This ensures `Iz` immediates (`66 48 81`, `66 48 68`, `66 48 69`, etc.) read 4 bytes instead of 2 and string ops (`66 48 A5`) use 8-byte element width. ## Tests & Verification - **AI-assisted**: yes - **Host / hardware**: macOS (Apple M5, arm64) - Standalone checks on `upstream/main` (`3a0af1f15`): - `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_rexw_66.py` — OK - `python3 -I tests/test_x64_recomp_rexw_66.py -v` — 2 static OK (1 exec skips cleanly without `#1174` runner) - `python3 -I tests/test_x64_recomp.py` — 6 static OK (26 exec skipped) - `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 mutation proof** (`tests/test_x64_recomp_rexw_66.py` on unpatched `upstream/main` + `#1174`): **2 failures, 1 error**. - **Native Apple M5 execution (23-way scratch merge on `upstream/main` `3a0af1f15` with `#1174` + all 21 other open `x64_recomp` PR branches)**: - 0 merge conflicts across all 23 branches - `tests/test_x64_recomp_rexw_66.py`: **3/3 OK (0 skipped)** on native Apple M5 arm64 - All 22 `tests/test_x64_recomp_*.py` suites combined: **151/151 OK (0 skipped)**
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:25 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 23:37 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-24 23:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1409#pullrequestreview-6049161806) · **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:21 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 392dda8f0 into the branch locally (674 commits behind, no conflicts). Head tested: d35c8665d.

The decoder change does what it says, but two things need fixing.

1. The new exec test errors wherever QEMU is installed. python -I tests/test_x64_recomp_rexw_66.py: 3 run, 1 error.

test_exec_66_rexw_movabs_add_mov_push_bswap
TypeError: unsupported operand type(s) for &: 'bytes' and 'int'

_find_runner's QEMU branch returns xr.run_kernel, which gives back (raw output bytes, translator), not the parsed value. xr.run_result is the one that parses R=xxxxxxxx. On this branch CAN_RUN_MACHO does not exist (it comes with #1174), so the QEMU branch is the only one that can be taken: the test is skipped without QEMU and errors with it. Suggested fix: use xr.run_result in both branches.

With that one substitution made locally, all four cases in the test give the asserted values under QEMU (1122334412345679, 0000000400000001, 0807060504030201, ffffffffffedcbaa), so the assertions themselves are right.

2. 66 48 98 and 66 48 99 still take the 16-bit path. _emit_conv tests ins.opsize before ins.rexw, and ins.opsize deliberately stays 1.

bytes want got under QEMU
48 98 (cdqe), eax=80000000 rax=ffffffff80000000 ffffffff80000000
66 48 98 the same 0000000080000000
48 99 (cqo), rax=8000000000000000 rdx=ffffffffffffffff ffffffffffffffff
66 48 99 the same 0000000000000000

Suggested fix: test ins.rexw first in both arms of _emit_conv.

What does work. These gave the wrong value on main and the right one here, each run under QEMU: 66 48 ff c0 (inc carries past bit 32), 66 48 f7 d8 (neg), 66 48 c1 e0 24 (shl by 36), 66 48 0f af c1 (imul), 66 48 05 imm32, 66 48 29 c8 (sub with borrow), 66 48 8d 04 08 (lea), 66 48 63 c1 (movsxd). Lengths now match for 66 48 c7 /0, 66 48 a9, 66 48 69, 66 48 05 (8, 7, 8, 7 bytes; main read a 2-byte immediate). 66 48 0f 6e, 0f 7e, 0f 3a 16, 0f 3a 22 decode the same as on main, so the mandatory-prefix forms were not disturbed.

Counts. tests/test_x64_recomp.py 33/33 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 511 run, 506 pass; the 5 that fail are main's 4 plus this new file.

Not this pull request, seen on main too: 48 c7 c0 01 00 00 80 gives fffffffe80000001 and 48 91 does not exchange. #1339 and #1428 look like the owners of those.

No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 22:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1409#issuecomment-7176261648) 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 `392dda8f0` into the branch locally (674 commits behind, no conflicts). Head tested: `d35c8665d`. The decoder change does what it says, but two things need fixing. **1. The new exec test errors wherever QEMU is installed.** `python -I tests/test_x64_recomp_rexw_66.py`: 3 run, 1 error. ``` test_exec_66_rexw_movabs_add_mov_push_bswap TypeError: unsupported operand type(s) for &: 'bytes' and 'int' ``` `_find_runner`'s QEMU branch returns `xr.run_kernel`, which gives back `(raw output bytes, translator)`, not the parsed value. `xr.run_result` is the one that parses `R=xxxxxxxx`. On this branch `CAN_RUN_MACHO` does not exist (it comes with #1174), so the QEMU branch is the only one that can be taken: the test is skipped without QEMU and errors with it. Suggested fix: use `xr.run_result` in both branches. With that one substitution made locally, all four cases in the test give the asserted values under QEMU (`1122334412345679`, `0000000400000001`, `0807060504030201`, `ffffffffffedcbaa`), so the assertions themselves are right. **2. `66 48 98` and `66 48 99` still take the 16-bit path.** `_emit_conv` tests `ins.opsize` before `ins.rexw`, and `ins.opsize` deliberately stays 1. | bytes | want | got under QEMU | |---|---|---| | `48 98` (cdqe), eax=`80000000` | rax=`ffffffff80000000` | `ffffffff80000000` | | `66 48 98` | the same | `0000000080000000` | | `48 99` (cqo), rax=`8000000000000000` | rdx=`ffffffffffffffff` | `ffffffffffffffff` | | `66 48 99` | the same | `0000000000000000` | Suggested fix: test `ins.rexw` first in both arms of `_emit_conv`. **What does work.** These gave the wrong value on `main` and the right one here, each run under QEMU: `66 48 ff c0` (inc carries past bit 32), `66 48 f7 d8` (neg), `66 48 c1 e0 24` (shl by 36), `66 48 0f af c1` (imul), `66 48 05 imm32`, `66 48 29 c8` (sub with borrow), `66 48 8d 04 08` (lea), `66 48 63 c1` (movsxd). Lengths now match for `66 48 c7 /0`, `66 48 a9`, `66 48 69`, `66 48 05` (8, 7, 8, 7 bytes; `main` read a 2-byte immediate). `66 48 0f 6e`, `0f 7e`, `0f 3a 16`, `0f 3a 22` decode the same as on `main`, so the mandatory-prefix forms were not disturbed. **Counts.** `tests/test_x64_recomp.py` 33/33 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 511 run, 506 pass; the 5 that fail are `main`'s 4 plus this new file. Not this pull request, seen on `main` too: `48 c7 c0 01 00 00 80` gives `fffffffe80000001` and `48 91` does not exchange. #1339 and #1428 look like the owners of those. 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/1409/head:github-bot/gh-1409'
git switch 'github-bot/gh-1409'

Merge

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