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

Open
github-bot wants to merge 2 commits from github-bot/gh-1321 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 21:50 UTC · original
IlyassRS3:fix/x64-recomp-conv-call-ret-loop → main · +301 −16 · 2 files · 2 commits · open

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
**@⁠IlyassRS3** on GitHub · 2026-11-24 21:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1321) `IlyassRS3:fix/x64-recomp-conv-call-ret-loop` → `main` · +301 −16 · 2 files · 2 commits · open 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:47:23 +00:00
Author
Owner

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

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

Checked out head fa76977 detached. Diff touches tools/x64_recomp.py + new tests/test_x64_recomp_control_conv.py.

Semantics reviewed:

  • cbtw/cwtd: REX.W now checked before opsize — correct precedence (cdqe/cqo win over 66h operand-size forms); adds missing sxth+asr pair for the cwde-with-EDX path.
  • jcxz/loop/loope/loopne: sf now follows addrsize (67h → 16-bit RCX) instead of always 32-bit; loop decrement uses same width. loope/loopne now only emit a direct b.cond when the flag producer actually writes Z, otherwise fall back to ctx flag read — sound fix.
  • Indirect call: return address is now pushed AFTER resolving the target operand, fixing call [rsp] reading the just-pushed return address.
  • ret imm16: masked to 16 bits, uses li+add_r for imm >= 4096 — correct.

Ran:

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

Verdict: looks correct; encoding tests pass, execution tests skipped on this host (no QEMU/ARM). Base is main — independent of the other x64_recomp PRs by the same author despite overlapping files.

**@⁠whoalin1** on GitHub · 2026-11-24 23:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1321#issuecomment-7147373514) Independent verification (Devin, AI-assisted — CPU-only, no GPU). Checked out head fa76977 detached. Diff touches tools/x64_recomp.py + new tests/test_x64_recomp_control_conv.py. Semantics reviewed: - cbtw/cwtd: REX.W now checked before opsize — correct precedence (cdqe/cqo win over 66h operand-size forms); adds missing sxth+asr pair for the cwde-with-EDX path. - jcxz/loop/loope/loopne: sf now follows addrsize (67h → 16-bit RCX) instead of always 32-bit; loop decrement uses same width. loope/loopne now only emit a direct b.cond when the flag producer actually writes Z, otherwise fall back to ctx flag read — sound fix. - Indirect call: return address is now pushed AFTER resolving the target operand, fixing call [rsp] reading the just-pushed return address. - ret imm16: masked to 16 bits, uses li+add_r for imm >= 4096 — correct. Ran: - `py -3.14 -I tests/test_x64_recomp_control_conv.py` → OK (5 passed + subtests, 4 skipped: native-exec tests need Apple Silicon/QEMU) - `pytest tests/test_x64_recomp_control_conv.py` → 5 passed, 4 skipped - `py -3.14 -m compileall -q tools tests make_game.py` → clean Verdict: looks correct; encoding tests pass, execution tests skipped on this host (no QEMU/ARM). Base is main — independent of the other x64_recomp PRs by the same author despite overlapping files.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 03:49 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 a73a57e13 into the branch locally (969 commits behind, no conflicts). Head tested: 082b42d8b.

Against a reference model this branch is right in all 119 checks. main gets 28 of the same 119 wrong, and each fix named in the title shows up in that difference.

  • The test file, step by step (nothing changed on disk; patched in memory or merged locally):

    • as shipped under QEMU: 9 run, 3 failures, 1 error. The failures are the wiring bug from #1409 (xr.run_kernel where xr.run_result is needed);
    • with RUNNER swapped for xr.run_result: 2 errors left, both QEMU timeouts;
    • one is a 64-bit all-ones constant in the cwd/cqo test, which hangs in Asm.li on main (fixed in #1339, #1205, #1329). With that branch patched in memory: 1 error left, test_ret_imm16_small_and_large_stack_cleanup;
    • that last one is not this PR. Under run_kernel on main, a guest ret hangs whenever the harness's pc table lands at 4 mod 8, which depends only on how many arm64 words the test translates to (details and a table in #2143). In my generated cases below, ret 8, ret 0x10 and ret 0xff8 hang for that reason while ret 0, ret 0x1000 and ret 0x2000 pass;
    • with #2143 merged in locally as well: 9/9.
  • Beyond the file, generated cases under QEMU against a small Python model:

    • cbw, cwde, cdqe, cwd, cdq, cqo, plus 66 48 98 and 66 48 99, six rax values each, full rax and full rdx (96 checks);
    • jrcxz and 67 jecxz with rcx = 0, 1, 1<<32, (1<<32)+5, 1<<63 (10);
    • loop and 67 loop iteration counts for 1, 2, 5, the latter with junk in the upper half of rcx (6);
    • ret imm16 restoring rsp for 0, 8, 0x10, 0xff8, 0x1000, 0x2000 (6), and the call [rsp] / call [rsp+8] sequence from the file (1).
    tool checks wrong what is wrong
    this branch + #2143 119 0
    this branch alone 119 3 ret 8, ret 0x10, ret 0xff8: the harness hang above, not the emitter
    main 119 28 66 48 98 ×6 and 66 48 99 ×6 (treated as cbw/cwd), cwd ×4 (copies ax into dx), jrcxz ×2 (tests ecx only), 67 loop ×3 (timeout), ret imm16 ×6 (timeout), call [rsp] ×1 (returns 0x20, want 0x40)
  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the reasons above.

Not tested or not judged: loope/loopne beyond the file's own cases, the upper half of rcx after a 67-prefixed loop, ret imm16 above 0x2000, and indirect calls through other addressing modes. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 03:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1321#issuecomment-7182058610) 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 `a73a57e13` into the branch locally (969 commits behind, no conflicts). Head tested: `082b42d8b`. **Against a reference model this branch is right in all 119 checks. `main` gets 28 of the same 119 wrong, and each fix named in the title shows up in that difference.** - The test file, step by step (nothing changed on disk; patched in memory or merged locally): - as shipped under QEMU: 9 run, 3 failures, 1 error. The failures are the wiring bug from #1409 (`xr.run_kernel` where `xr.run_result` is needed); - with `RUNNER` swapped for `xr.run_result`: 2 errors left, both QEMU timeouts; - one is a 64-bit all-ones constant in the `cwd`/`cqo` test, which hangs in `Asm.li` on `main` (fixed in #1339, #1205, #1329). With that branch patched in memory: 1 error left, `test_ret_imm16_small_and_large_stack_cleanup`; - that last one is not this PR. Under `run_kernel` on `main`, a guest `ret` hangs whenever the harness's pc table lands at 4 mod 8, which depends only on how many arm64 words the test translates to (details and a table in #2143). In my generated cases below, `ret 8`, `ret 0x10` and `ret 0xff8` hang for that reason while `ret 0`, `ret 0x1000` and `ret 0x2000` pass; - with #2143 merged in locally as well: **9/9**. - Beyond the file, generated cases under QEMU against a small Python model: - `cbw`, `cwde`, `cdqe`, `cwd`, `cdq`, `cqo`, plus `66 48 98` and `66 48 99`, six `rax` values each, full `rax` and full `rdx` (96 checks); - `jrcxz` and `67 jecxz` with `rcx` = 0, 1, `1<<32`, `(1<<32)+5`, `1<<63` (10); - `loop` and `67 loop` iteration counts for 1, 2, 5, the latter with junk in the upper half of `rcx` (6); - `ret imm16` restoring `rsp` for 0, 8, 0x10, 0xff8, 0x1000, 0x2000 (6), and the `call [rsp]` / `call [rsp+8]` sequence from the file (1). | tool | checks | wrong | what is wrong | |---|---|---|---| | this branch + #2143 | 119 | 0 | | | this branch alone | 119 | 3 | `ret 8`, `ret 0x10`, `ret 0xff8`: the harness hang above, not the emitter | | `main` | 119 | 28 | `66 48 98` ×6 and `66 48 99` ×6 (treated as `cbw`/`cwd`), `cwd` ×4 (copies `ax` into `dx`), `jrcxz` ×2 (tests `ecx` only), `67 loop` ×3 (timeout), `ret imm16` ×6 (timeout), `call [rsp]` ×1 (returns 0x20, want 0x40) | - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the reasons above. Not tested or not judged: `loope`/`loopne` beyond the file's own cases, the upper half of `rcx` after a `67`-prefixed `loop`, `ret imm16` above 0x2000, and indirect calls through other addressing modes. 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/1321/head:github-bot/gh-1321'
git switch 'github-bot/gh-1321'

Merge

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