x64_recomp: handle 16-bit operand-size override (66) in push and pop (X64-RECOMP-PUSH-POP-16) #1408

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

@⁠IlyassRS3 on GitHub · 2026-11-24 23:34 UTC · original
IlyassRS3:fix/x64-recomp-push-pop-16 → main · +189 −4 · 2 files · 1 commits · open

Item ID: X64-RECOMP-PUSH-POP-16

Summary

Fixes 16-bit operand-size override (66 prefix without REX.W) in _emit_pushpop in tools/x64_recomp.py:

  • push (66 50..57, 66 41 50..57, 66 68 iw, 66 6A ib, 66 FF /6): loads 2 bytes (or sign-extended imm8 masked to 16 bits) and emits str_pre(T0, rsp, -2, size=2) (STRH W0, [X23, #-2]!) instead of always decrementing RSP by 8 and writing 8 bytes.
  • pop (66 58..5F, 66 41 58..5F, 66 8F /0): emits ldr_post(T0, rsp, 2, size=2) (LDRH W0, [X23], #2), inserts only the low 16 bits via bfi(..., 0, 16) for register destinations (preserving upper 48 bits), and stores exactly 2 bytes via str(..., size=2) for memory destinations (after the post-increment of RSP, matching Intel SDM for pop word ptr [rsp+disp]).
  • 66 + REX.W precedence (66 48 50, 66 48 58): REX.W overrides 66, preserving full 64-bit push/pop (±8).

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_push_pop_16.py — OK
    • python3 -I tests/test_x64_recomp_push_pop_16.py -v — 1 static OK (2 exec skip 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_push_pop_16.py on unpatched upstream/main + #1174): 2 failures, 1 error.
  • Native Apple M5 execution (22-way scratch merge on upstream/main 3a0af1f15 with #1174 + all 20 other open x64_recomp PR branches):
    • 0 merge conflicts across all 22 branches
    • tests/test_x64_recomp_push_pop_16.py: 3/3 OK (0 skipped) on native Apple M5 arm64
    • All 21 tests/test_x64_recomp_*.py suites combined: 148/148 OK (0 skipped)
**@⁠IlyassRS3** on GitHub · 2026-11-24 23:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1408) `IlyassRS3:fix/x64-recomp-push-pop-16` → `main` · +189 −4 · 2 files · 1 commits · open Item ID: `X64-RECOMP-PUSH-POP-16` ## Summary Fixes 16-bit operand-size override (`66` prefix without `REX.W`) in `_emit_pushpop` in `tools/x64_recomp.py`: - **`push` (`66 50..57`, `66 41 50..57`, `66 68 iw`, `66 6A ib`, `66 FF /6`)**: loads 2 bytes (or sign-extended `imm8` masked to 16 bits) and emits `str_pre(T0, rsp, -2, size=2)` (`STRH W0, [X23, #-2]!`) instead of always decrementing `RSP` by 8 and writing 8 bytes. - **`pop` (`66 58..5F`, `66 41 58..5F`, `66 8F /0`)**: emits `ldr_post(T0, rsp, 2, size=2)` (`LDRH W0, [X23], #2`), inserts only the low 16 bits via `bfi(..., 0, 16)` for register destinations (preserving upper 48 bits), and stores exactly 2 bytes via `str(..., size=2)` for memory destinations (after the post-increment of `RSP`, matching Intel SDM for `pop word ptr [rsp+disp]`). - **`66` + `REX.W` precedence (`66 48 50`, `66 48 58`)**: `REX.W` overrides `66`, preserving full 64-bit `push`/`pop` (`±8`). ## 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_push_pop_16.py` — OK - `python3 -I tests/test_x64_recomp_push_pop_16.py -v` — 1 static OK (2 exec skip 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_push_pop_16.py` on unpatched `upstream/main` + `#1174`): **2 failures, 1 error**. - **Native Apple M5 execution (22-way scratch merge on `upstream/main` `3a0af1f15` with `#1174` + all 20 other open `x64_recomp` PR branches)**: - 0 merge conflicts across all 22 branches - `tests/test_x64_recomp_push_pop_16.py`: **3/3 OK (0 skipped)** on native Apple M5 arm64 - All 21 `tests/test_x64_recomp_*.py` suites combined: **148/148 OK (0 skipped)**
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:24 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 23:35 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:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1408#pullrequestreview-6049131613) · **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:28 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 d601736ce into the branch locally (702 commits behind, no conflicts). Head tested: c6d1639d4.

The emitter change is right as far as I could test it. The new test file has one wiring bug.

The exec tests error wherever QEMU is installed. python -I tests/test_x64_recomp_push_pop_16.py: 3 run, 2 errors, both

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

It is the same cause as in #1409: _find_runner's QEMU branch returns xr.run_kernel, which gives back (raw output bytes, translator). xr.run_result is the one that parses R=xxxxxxxx. CAN_RUN_MACHO only exists with #1174, so on this branch the tests are skipped without QEMU and error with it. Suggested fix: use xr.run_result in both branches.

With RUNNER swapped for xr.run_result (in memory, no file changed): 3/3 pass under QEMU. So every assertion in the file holds, including the pop word ptr [rsp+4] post-increment case and the 66 48 50 / 66 48 58 REX.W case.

Extra cases, each run under QEMU on main and on this branch:

sequence want main this branch
push rax; pushw 0xBEEF; pop word [rsp]; pop rax, rax=1122334455667788 112233445566beef ffffffffffffbeef 112233445566beef
66 50, then rsp delta 2 QEMU run fails 2
xor rax,rax; pushw -2; pop ax 000000000000fffe fffffffffffffffe 000000000000fffe
66 54; 66 58 (push sp, pop ax), ax == old sp yes yes yes
66 54; 66 5c (push sp, pop sp), rsp unchanged yes yes yes

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

I found nothing wrong in _emit_pushpop itself. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 22:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1408#issuecomment-7176397711) 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 `d601736ce` into the branch locally (702 commits behind, no conflicts). Head tested: `c6d1639d4`. The emitter change is right as far as I could test it. The new test file has one wiring bug. **The exec tests error wherever QEMU is installed.** `python -I tests/test_x64_recomp_push_pop_16.py`: 3 run, 2 errors, both ``` TypeError: unsupported operand type(s) for &: 'bytes' and 'int' ``` It is the same cause as in #1409: `_find_runner`'s QEMU branch returns `xr.run_kernel`, which gives back `(raw output bytes, translator)`. `xr.run_result` is the one that parses `R=xxxxxxxx`. `CAN_RUN_MACHO` only exists with #1174, so on this branch the tests are skipped without QEMU and error with it. Suggested fix: use `xr.run_result` in both branches. **With `RUNNER` swapped for `xr.run_result` (in memory, no file changed): 3/3 pass under QEMU.** So every assertion in the file holds, including the `pop word ptr [rsp+4]` post-increment case and the `66 48 50` / `66 48 58` REX.W case. Extra cases, each run under QEMU on `main` and on this branch: | sequence | want | `main` | this branch | |---|---|---|---| | `push rax; pushw 0xBEEF; pop word [rsp]; pop rax`, rax=`1122334455667788` | `112233445566beef` | `ffffffffffffbeef` | `112233445566beef` | | `66 50`, then rsp delta | 2 | QEMU run fails | 2 | | `xor rax,rax; pushw -2; pop ax` | `000000000000fffe` | `fffffffffffffffe` | `000000000000fffe` | | `66 54; 66 58` (push sp, pop ax), ax == old sp | yes | yes | yes | | `66 54; 66 5c` (push sp, pop sp), rsp unchanged | yes | yes | yes | **Counts.** `tests/test_x64_recomp.py` 33/33 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 512 run, 507 pass; the 5 that fail are `main`'s 4 plus this new file, for the reason above. I found nothing wrong in `_emit_pushpop` itself. 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/1408/head:github-bot/gh-1408'
git switch 'github-bot/gh-1408'

Merge

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