x64_recomp: sign-extend ALU/CMP/TEST/MOV immediates, fix Asm.li(-1), and handle movnti (X64-RECOMP-SIGNEXT-IMM) #1339

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

@⁠IlyassRS3 on GitHub · 2026-11-24 22:01 UTC · original
IlyassRS3:fix/x64-recomp-signext-imm → main · +203 −12 · 2 files · 1 commits · open

Item ID: X64-RECOMP-SIGNEXT-IMM
AI-assisted: yes

Summary

Fixes silent zero-extension of negative immediates across _emit_alu, _emit_cmp, and _emit_mov, fixes Asm.li(-1) crash, and handles movnti in tools/x64_recomp.py:

  1. ALU, CMP, and TEST truncated signed imm8/imm32 to the immediate's encoded width:
    • _finish and _finish_group already decode 83 /r ib, 81 /r id, 05..3D id, F7 /0 id, and A9 id as signed Python integers, but _emit_alu and _emit_cmp called self._load(ins, src, T1) without as_size=sz.
    • _load masked op[1] with (1 << (src[2] * 8)) - 1, so:
      • and rsp, -16 (48 83 e4 f0, standard stack alignment) loaded +0xF0 instead of 0xFFFFFFFFFFFFFFF0, zeroing bits 8..63 of rsp.
      • cmp rax, -1 (48 83 f8 ff / 48 81 f8 ff ff ff ff / 48 3d ff ff ff ff) compared against +0xFF or +0xFFFFFFFF instead of -1.
      • test rax, -1 (48 f7 c0 ff ff ff ff / 48 a9 ff ff ff ff) masked out bits 32..63.
      • add/sub/or/xor with negative imm8/imm32 added/subtracted +0xFF or +0xFFFFFFFF.
    • Fixed by passing as_size=sz to self._load(ins, src, T1, as_size=sz) in _emit_alu and _emit_cmp, and stashing CTX_A/CTX_B when ins.addr in self.mat as well as "A" in need.
  2. mov qword ptr [mem], imm32 (48 C7 /0) and movnti (0F C3):
    • _emit_mov's special case for C7 /0 sign-extension only checked dst[0] == "r", so mov qword ptr [rsp], -1 stored 0x00000000FFFFFFFF instead of 0xFFFFFFFFFFFFFFFF. Passing as_size=self._op_size(dst) to _load fixes both register and memory destinations and removes the redundant branch.
    • Included "movnti" alongside "mov" so 32/64-bit non-temporal stores translate cleanly instead of raising UnhandledInsn("movnti").
  3. Asm.li(rd, -1):
    • Added elif not nzc: self.movn(rd, 0) (identical to #1205 and #1329) so materializing 0xFFFFFFFFFFFFFFFF (for cmp rax, -1, mov [rsp], -1, push -1, etc.) emits MOVN instead of crashing with IndexError.

Merge compatibility

  • Verified 0 merge conflicts in a 12-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306, #1313, #1321, and #1329.

Verification

Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files):

  • python3 -I tests/test_x64_recomp.py — 32 tests (6 ok, 26 skipped without runner)
  • python3 -I tests/test_x64_recomp_signext_imm.py -v — 8 tests (4 static ok, 4 exec skipped without runner)
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main causes 8 failures across all 4 static tests in tests/test_x64_recomp_signext_imm.py.
  • Native Apple M5 execution (scratch 12-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306, #1313, #1321, #1329 — 0 merge conflicts):
    • tests/test_x64_recomp_signext_imm.py — 8/8 OK (all 4 execution suites + 4 static tests, 0 skips)
    • All 11 other test_x64_recomp*.py suites — 134/134 OK
    • Total: 142/142 native arm64 tests passing.
  • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_signext_imm.py — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_signext_imm.py — OK
  • python3 -I tests/test_ci_registration.py — 15/15 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
**@⁠IlyassRS3** on GitHub · 2026-11-24 22:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1339) `IlyassRS3:fix/x64-recomp-signext-imm` → `main` · +203 −12 · 2 files · 1 commits · open Item ID: `X64-RECOMP-SIGNEXT-IMM` AI-assisted: yes ## Summary Fixes silent zero-extension of negative immediates across `_emit_alu`, `_emit_cmp`, and `_emit_mov`, fixes `Asm.li(-1)` crash, and handles `movnti` in `tools/x64_recomp.py`: 1. **ALU, CMP, and TEST truncated signed `imm8`/`imm32` to the immediate's encoded width**: - `_finish` and `_finish_group` already decode `83 /r ib`, `81 /r id`, `05..3D id`, `F7 /0 id`, and `A9 id` as signed Python integers, but `_emit_alu` and `_emit_cmp` called `self._load(ins, src, T1)` without `as_size=sz`. - `_load` masked `op[1]` with `(1 << (src[2] * 8)) - 1`, so: - `and rsp, -16` (`48 83 e4 f0`, standard stack alignment) loaded `+0xF0` instead of `0xFFFFFFFFFFFFFFF0`, zeroing bits 8..63 of `rsp`. - `cmp rax, -1` (`48 83 f8 ff` / `48 81 f8 ff ff ff ff` / `48 3d ff ff ff ff`) compared against `+0xFF` or `+0xFFFFFFFF` instead of `-1`. - `test rax, -1` (`48 f7 c0 ff ff ff ff` / `48 a9 ff ff ff ff`) masked out bits 32..63. - `add`/`sub`/`or`/`xor` with negative `imm8`/`imm32` added/subtracted `+0xFF` or `+0xFFFFFFFF`. - Fixed by passing `as_size=sz` to `self._load(ins, src, T1, as_size=sz)` in `_emit_alu` and `_emit_cmp`, and stashing `CTX_A`/`CTX_B` when `ins.addr in self.mat` as well as `"A" in need`. 2. **`mov qword ptr [mem], imm32` (`48 C7 /0`) and `movnti` (`0F C3`)**: - `_emit_mov`'s special case for `C7 /0` sign-extension only checked `dst[0] == "r"`, so `mov qword ptr [rsp], -1` stored `0x00000000FFFFFFFF` instead of `0xFFFFFFFFFFFFFFFF`. Passing `as_size=self._op_size(dst)` to `_load` fixes both register and memory destinations and removes the redundant branch. - Included `"movnti"` alongside `"mov"` so 32/64-bit non-temporal stores translate cleanly instead of raising `UnhandledInsn("movnti")`. 3. **`Asm.li(rd, -1)`**: - Added `elif not nzc: self.movn(rd, 0)` (identical to `#1205` and `#1329`) so materializing `0xFFFFFFFFFFFFFFFF` (for `cmp rax, -1`, `mov [rsp], -1`, `push -1`, etc.) emits `MOVN` instead of crashing with `IndexError`. ## Merge compatibility - Verified **0 merge conflicts** in a 12-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306`, `#1313`, `#1321`, and `#1329`. ## Verification Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files): - `python3 -I tests/test_x64_recomp.py` — 32 tests (6 ok, 26 skipped without runner) - `python3 -I tests/test_x64_recomp_signext_imm.py -v` — 8 tests (4 static ok, 4 exec skipped without runner) - Mutation proof: reverting `tools/x64_recomp.py` to `upstream/main` causes **8 failures across all 4 static tests** in `tests/test_x64_recomp_signext_imm.py`. - Native Apple M5 execution (scratch 12-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306`, `#1313`, `#1321`, `#1329` — 0 merge conflicts): - `tests/test_x64_recomp_signext_imm.py` — **8/8 OK** (all 4 execution suites + 4 static tests, 0 skips) - All 11 other `test_x64_recomp*.py` suites — **134/134 OK** - Total: **142/142 native arm64 tests passing**. - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_signext_imm.py` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_signext_imm.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:47:57 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 22:01 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 22:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1339#pullrequestreview-6048102921) · **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

@⁠whoalin1 on GitHub · 2026-11-24 23:26 UTC · original

Independent verification (Devin, AI-assisted — CPU-only, no GPU).

Checked out head c641b98 detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_signext_imm.py.

Semantics reviewed:

  • ALU/CMP/TEST/MOV immediate loads now pass as_size=operand size so imm is masked at the right width; verified that for 8/16-bit ops the mask-then-widen sequence is equivalent to x86 sign-extension (truncated high bits are shifted out by the lsl anyway), and for 64-bit dst the C7 /0 imm32 sign-extension is preserved via the generic mov path.
  • The removed mov special-case (C7 /0) is correctly subsumed: dst osz=8 + imm now loads with as_size=8 keeping the sign-extended imm32.
  • movnti now shares the mov path — loses the non-temporal hint (perf only, not correctness); acceptable but worth noting.
  • AF stash conditions widened to include ins.addr in self.mat, matching the pattern used elsewhere.
  • Asm.li(-1) movn fix is identical to the one in #1329 — whichever lands second will conflict trivially.

Ran:

  • py -3.14 -I tests/test_x64_recomp_signext_imm.py → OK (4 passed + subtests, 4 skipped: native-exec needs Apple Silicon/QEMU)
  • pytest tests/test_x64_recomp_signext_imm.py → 4 passed, 4 skipped
  • py -3.14 -m compileall -q tools tests make_game.py → clean

Verdict: correct on inspection; tests pass. Execution tests skipped on this host. Base is main — overlaps #1329 on the li() hunk.

**@⁠whoalin1** on GitHub · 2026-11-24 23:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1339#issuecomment-7147397666) Independent verification (Devin, AI-assisted — CPU-only, no GPU). Checked out head c641b98 detached. Diff: tools/x64_recomp.py + tests/test_x64_recomp_signext_imm.py. Semantics reviewed: - ALU/CMP/TEST/MOV immediate loads now pass as_size=operand size so imm is masked at the right width; verified that for 8/16-bit ops the mask-then-widen sequence is equivalent to x86 sign-extension (truncated high bits are shifted out by the lsl anyway), and for 64-bit dst the C7 /0 imm32 sign-extension is preserved via the generic mov path. - The removed mov special-case (C7 /0) is correctly subsumed: dst osz=8 + imm now loads with as_size=8 keeping the sign-extended imm32. - movnti now shares the mov path — loses the non-temporal hint (perf only, not correctness); acceptable but worth noting. - AF stash conditions widened to include ins.addr in self.mat, matching the pattern used elsewhere. - Asm.li(-1) movn fix is identical to the one in #1329 — whichever lands second will conflict trivially. Ran: - `py -3.14 -I tests/test_x64_recomp_signext_imm.py` → OK (4 passed + subtests, 4 skipped: native-exec needs Apple Silicon/QEMU) - `pytest tests/test_x64_recomp_signext_imm.py` → 4 passed, 4 skipped - `py -3.14 -m compileall -q tools tests make_game.py` → clean Verdict: correct on inspection; tests pass. Execution tests skipped on this host. Base is main — overlaps #1329 on the li() hunk.
Author
Owner

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

Independent run with QEMU, to add the executed half to the review above. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 5fab93967 into the branch locally (838 commits behind, no conflicts). Head tested: c641b9832.

The exec tests fail wherever QEMU is installed: 8 run, 19 failures, all of the shape b'R=00000001\n' != 1. It is the wiring bug described in #1409: _runner's QEMU branch returns xr.run_kernel (raw output bytes) where xr.run_result (the parsed value) is needed. The raw outputs already show the right numbers. With RUNNER swapped for xr.run_result in memory (no file changed): 8/8 pass under QEMU.

Extra cases under QEMU, reading rax afterwards:

sequence want main this branch
48 c7 c0 01 00 00 80 (mov rax, imm32) ffffffff80000001 fffffffe80000001 ffffffff80000001
48 c7 c0 ff ff ff ff (mov rax, -1) ffffffffffffffff fffffffeffffffff ffffffffffffffff
48 83 e0 f0 (and rax, -16), rax=1111111112345678 1111111112345670 0000000000000070 1111111112345670
83 c8 ff (or eax, -1), high half set 00000000ffffffff 00000000000000ff 00000000ffffffff

Unchanged and right on both: 04 ff (add al, -1), 48 05 ff ff ff 7f, 3c ff; sete, 48 6b c0 fe (imul by -2), b8 ff ff ff ff, and 66 c7 44 24 f8 fe ff read back with movzx.

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 the new file, for the reason above.

One thing to coordinate: this adds movnti to _emit_mov, and #1428 adds it too, differently. Whichever lands second will need to reconcile them. I did not test the two together.

No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 22:46 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1339#issuecomment-7176774535) Independent run with QEMU, to add the executed half to the review above. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `5fab93967` into the branch locally (838 commits behind, no conflicts). Head tested: `c641b9832`. **The exec tests fail wherever QEMU is installed: 8 run, 19 failures, all of the shape `b'R=00000001\n' != 1`.** It is the wiring bug described in #1409: `_runner`'s QEMU branch returns `xr.run_kernel` (raw output bytes) where `xr.run_result` (the parsed value) is needed. The raw outputs already show the right numbers. With `RUNNER` swapped for `xr.run_result` in memory (no file changed): **8/8 pass under QEMU.** Extra cases under QEMU, reading rax afterwards: | sequence | want | `main` | this branch | |---|---|---|---| | `48 c7 c0 01 00 00 80` (mov rax, imm32) | `ffffffff80000001` | `fffffffe80000001` | `ffffffff80000001` | | `48 c7 c0 ff ff ff ff` (mov rax, -1) | `ffffffffffffffff` | `fffffffeffffffff` | `ffffffffffffffff` | | `48 83 e0 f0` (and rax, -16), rax=`1111111112345678` | `1111111112345670` | `0000000000000070` | `1111111112345670` | | `83 c8 ff` (or eax, -1), high half set | `00000000ffffffff` | `00000000000000ff` | `00000000ffffffff` | Unchanged and right on both: `04 ff` (add al, -1), `48 05 ff ff ff 7f`, `3c ff; sete`, `48 6b c0 fe` (imul by -2), `b8 ff ff ff ff`, and `66 c7 44 24 f8 fe ff` read back with `movzx`. **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 the new file, for the reason above. One thing to coordinate: this adds `movnti` to `_emit_mov`, and #1428 adds it too, differently. Whichever lands second will need to reconcile them. I did not test the two together. 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/1339/head:github-bot/gh-1339'
git switch 'github-bot/gh-1339'

Merge

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