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

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

📋 Pull Request Information

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

Base: main ← Head: fix/x64-recomp-signext-imm


📝 Commits (1)

  • c641b98 x64_recomp: sign-extend ALU/CMP/TEST/MOV immediates, fix Asm.li(-1), and handle movnti

📊 Changes

2 files changed (+203 additions, -12 deletions)

View changed files

➕ tests/test_x64_recomp_signext_imm.py (+195 -0)
📝 tools/x64_recomp.py (+8 -12)

📄 Description

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

🔄 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/1339 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-signext-imm` --- ### 📝 Commits (1) - [`c641b98`](https://github.com/ubiquitarianism/leonida/commit/c641b983213307a3f924473f33d1b9b7ab0b5a01) x64_recomp: sign-extend ALU/CMP/TEST/MOV immediates, fix Asm.li(-1), and handle movnti ### 📊 Changes **2 files changed** (+203 additions, -12 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_signext_imm.py` (+195 -0) 📝 `tools/x64_recomp.py` (+8 -12) </details> ### 📄 Description 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 --- <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#1369
No description provided.