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

Merged
quietfold merged 1 commit from upstream-pr/1339 into main 2026-11-28 19:39:29 +00:00
quietfold commented 2026-11-24 22:01:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T22:01:18Z.


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
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5481057819 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1339). Originally posted by [quietfold](https://github.com/quietfold) on 2026-11-24T22:01:18Z. --- 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
copilot-pull-request-reviewer[bot] commented 2026-11-24 22:01:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T22:01:31Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6048102921 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1339#pullrequestreview-6048102921). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-24T22:01:31Z. **Review:** COMMENTED --- 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.
whoalin1 commented 2026-11-24 23:26:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by whoalin1 on 2026-11-24T23:26:13Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7147397666 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1339#issuecomment-7147397666). Originally posted by [whoalin1](https://github.com/whoalin1) on 2026-11-24T23:26:13Z. --- 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.
kevincommits commented 2026-11-25 22:46:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T22:46:38Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7176774535 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1339#issuecomment-7176774535). Originally posted by [kevincommits](https://github.com/kevincommits) on 2026-11-25T22:46:38Z. --- 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.
MoonEyes commented 2026-11-28 19:39:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:39:25Z.


Red/green: tests/test_x64_recomp_signext_imm.py fails 8 assertions on main (and rsp,-16 / cmp rax,-1 / mov qword [mem],-1 emit truncated immediates, Asm.li(-1) breaks) and passes with the PR (cloud run on main 7daa4a4).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main 7daa4a4a5 in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7244707922 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1339#issuecomment-7244707922). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T19:39:25Z. --- Red/green: tests/test_x64_recomp_signext_imm.py fails 8 assertions on main (and rsp,-16 / cmp rax,-1 / mov qword [mem],-1 emit truncated immediates, Asm.li(-1) breaks) and passes with the PR (cloud run on main 7daa4a4). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main 7daa4a4a5 in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1339
No description provided.