[PR #1321] x64_recomp: fix cwd/REX.W conv, ret imm16, indirect call [rsp], and jrcxz/loop (X64-RECOMP-CONV-CALL-RET-LOOP) #1352

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

📋 Pull Request Information

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

Base: main ← Head: fix/x64-recomp-conv-call-ret-loop


📝 Commits (2)

  • fa76977 x64_recomp: fix cwd/REX.W conv, ret imm16, indirect call [rsp], and jrcxz/loop
  • 082b42d tests: match any scratch register loading ctx.eflags in cross-block loope check

📊 Changes

2 files changed (+301 additions, -16 deletions)

View changed files

➕ tests/test_x64_recomp_control_conv.py (+271 -0)
📝 tools/x64_recomp.py (+30 -16)

📄 Description

Item ID: X64-RECOMP-CONV-CALL-RET-LOOP
AI-assisted: yes

Summary

Fixes five correctness and crash bugs in tools/x64_recomp.py (_emit_conv, _emit_branch, _emit_call, _emit_ret):

  1. _emit_conv (66 99 cwd copied AX into DX instead of sign-extending AX):
    • a.sxth(T0, GMAP[0], sf=0); a.bfi(GMAP[2], T0, 0, 16) wrote the low 16 bits of AX directly into DX instead of its sign bit (0x0000 / 0xFFFF). Fixed by inserting a.asr(T0, T0, 16, sf=0) before bfi.
    • Fixed prefix precedence so REX.W takes priority over 0x66 (66 48 98 -> cdqe, 66 48 99 -> cqo).
  2. _emit_ret (C2 iw ret imm16 crashed with IndexError and failed for imm16 >= 4096):
    • Line 2173 indexed ins.ops[1] instead of ins.ops[0][1], raising IndexError: tuple index out of range on every ret imm16.
    • Passed imm16 directly to ARM64 ADD imm12 (0..4095), raising AssertionError for imm16 >= 4096. Fixed using a.li(T0, imm); a.add_r(GMAP[4], GMAP[4], T0) when imm >= 4096.
  3. _emit_call (indirect call [rsp+disp] / call rsp decremented rsp before loading the target):
    • Pushing the return address (str_pre(T0, GMAP[4], -8)) before self._load(ins, op, ADDR_T) caused call [rsp] to read the pushed return address and call [rsp+8] to read 8 bytes too low.
    • Fixed by loading ADDR_T before pushing the return address.
  4. _emit_branch (jrcxz vs 67 jecxz and loop/loope/loopne):
    • E3 (jrcxz) hardcoded sf=0, ignoring the upper 32 bits of rcx in 64-bit mode; fixed to sf = 0 if ins.addrsize else 1.
    • loop/loope/loopne now respect 0x67 (ins.addrsize) for 32-bit ecx vs 64-bit rcx, and loope/loopne check self.arm_path so cross-block ZF reads ctx.eflags via _ctx_jcc instead of stale ARM Z.

Merge compatibility

  • Keeps edits strictly inside _emit_conv (lines 1855–1872) and _emit_branch/_emit_call/_emit_ret (lines 2140–2175 on main).
  • Verified 0 merge conflicts in a 10-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306, and #1313.

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_control_conv.py -v — 9 tests (5 static ok, 4 exec skipped without runner)
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main causes 8 failures across all 5 static tests in tests/test_x64_recomp_control_conv.py.
  • Native Apple M5 execution (scratch 10-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306, #1313 — 0 merge conflicts):
    • tests/test_x64_recomp_control_conv.py — 9/9 OK (all 4 execution suites + 5 static tests, 0 skips)
    • tests/test_x64_recomp.py — 46/46 OK
    • tests/test_x64_recomp_sse.py — 16/16 OK
    • tests/test_x64_recomp_bitops.py — 9/9 OK
    • tests/test_x64_recomp_rcl_rcr.py — 5/5 OK
    • tests/test_x64_recomp_std_str.py — 5/5 OK
    • tests/test_x64_recomp_muldiv.py — 10/10 OK
    • tests/test_x64_recomp_shifts.py — 9/9 OK
    • tests/test_x64_recomp_atomics.py — 10/10 OK
    • tests/test_x64_recomp_incdec.py — 8/8 OK
    • Total: 127/127 native arm64 tests passing.
  • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_control_conv.py — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_control_conv.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/1321 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-conv-call-ret-loop` --- ### 📝 Commits (2) - [`fa76977`](https://github.com/ubiquitarianism/leonida/commit/fa7697749fcbc51795686fab6a3ac6d76b8ba20c) x64_recomp: fix cwd/REX.W conv, ret imm16, indirect call [rsp], and jrcxz/loop - [`082b42d`](https://github.com/ubiquitarianism/leonida/commit/082b42d8b44128cbc85679b979e12856d0c0de23) tests: match any scratch register loading ctx.eflags in cross-block loope check ### 📊 Changes **2 files changed** (+301 additions, -16 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_control_conv.py` (+271 -0) 📝 `tools/x64_recomp.py` (+30 -16) </details> ### 📄 Description Item ID: `X64-RECOMP-CONV-CALL-RET-LOOP` AI-assisted: yes ## Summary Fixes five correctness and crash bugs in `tools/x64_recomp.py` (`_emit_conv`, `_emit_branch`, `_emit_call`, `_emit_ret`): 1. **`_emit_conv` (`66 99` `cwd` copied `AX` into `DX` instead of sign-extending `AX`)**: - `a.sxth(T0, GMAP[0], sf=0); a.bfi(GMAP[2], T0, 0, 16)` wrote the low 16 bits of `AX` directly into `DX` instead of its sign bit (`0x0000` / `0xFFFF`). Fixed by inserting `a.asr(T0, T0, 16, sf=0)` before `bfi`. - Fixed prefix precedence so `REX.W` takes priority over `0x66` (`66 48 98` -> `cdqe`, `66 48 99` -> `cqo`). 2. **`_emit_ret` (`C2 iw` `ret imm16` crashed with `IndexError` and failed for `imm16 >= 4096`)**: - Line 2173 indexed `ins.ops[1]` instead of `ins.ops[0][1]`, raising `IndexError: tuple index out of range` on every `ret imm16`. - Passed `imm16` directly to ARM64 `ADD imm12` (`0..4095`), raising `AssertionError` for `imm16 >= 4096`. Fixed using `a.li(T0, imm); a.add_r(GMAP[4], GMAP[4], T0)` when `imm >= 4096`. 3. **`_emit_call` (indirect `call [rsp+disp]` / `call rsp` decremented `rsp` before loading the target)**: - Pushing the return address (`str_pre(T0, GMAP[4], -8)`) before `self._load(ins, op, ADDR_T)` caused `call [rsp]` to read the pushed return address and `call [rsp+8]` to read 8 bytes too low. - Fixed by loading `ADDR_T` before pushing the return address. 4. **`_emit_branch` (`jrcxz` vs `67 jecxz` and `loop`/`loope`/`loopne`)**: - `E3` (`jrcxz`) hardcoded `sf=0`, ignoring the upper 32 bits of `rcx` in 64-bit mode; fixed to `sf = 0 if ins.addrsize else 1`. - `loop`/`loope`/`loopne` now respect `0x67` (`ins.addrsize`) for 32-bit `ecx` vs 64-bit `rcx`, and `loope`/`loopne` check `self.arm_path` so cross-block `ZF` reads `ctx.eflags` via `_ctx_jcc` instead of stale ARM `Z`. ## Merge compatibility - Keeps edits strictly inside `_emit_conv` (lines 1855–1872) and `_emit_branch`/`_emit_call`/`_emit_ret` (lines 2140–2175 on `main`). - Verified **0 merge conflicts** in a 10-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306`, and `#1313`. ## 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_control_conv.py -v` — 9 tests (5 static ok, 4 exec skipped without runner) - Mutation proof: reverting `tools/x64_recomp.py` to `upstream/main` causes **8 failures across all 5 static tests** in `tests/test_x64_recomp_control_conv.py`. - Native Apple M5 execution (scratch 10-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306`, `#1313` — 0 merge conflicts): - `tests/test_x64_recomp_control_conv.py` — **9/9 OK** (all 4 execution suites + 5 static tests, 0 skips) - `tests/test_x64_recomp.py` — **46/46 OK** - `tests/test_x64_recomp_sse.py` — **16/16 OK** - `tests/test_x64_recomp_bitops.py` — **9/9 OK** - `tests/test_x64_recomp_rcl_rcr.py` — **5/5 OK** - `tests/test_x64_recomp_std_str.py` — **5/5 OK** - `tests/test_x64_recomp_muldiv.py` — **10/10 OK** - `tests/test_x64_recomp_shifts.py` — **9/9 OK** - `tests/test_x64_recomp_atomics.py` — **10/10 OK** - `tests/test_x64_recomp_incdec.py` — **8/8 OK** - Total: **127/127 native arm64 tests passing**. - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_control_conv.py` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_control_conv.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#1352
No description provided.