x64_recomp: fix cwd/REX.W conv, ret imm16, indirect call [rsp], and jrcxz/loop (X64-RECOMP-CONV-CALL-RET-LOOP) #1321
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1321"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T21:50:57Z.
Item ID:
X64-RECOMP-CONV-CALL-RET-LOOPAI-assisted: yes
Summary
Fixes five correctness and crash bugs in
tools/x64_recomp.py(_emit_conv,_emit_branch,_emit_call,_emit_ret):_emit_conv(66 99cwdcopiedAXintoDXinstead of sign-extendingAX):a.sxth(T0, GMAP[0], sf=0); a.bfi(GMAP[2], T0, 0, 16)wrote the low 16 bits ofAXdirectly intoDXinstead of its sign bit (0x0000/0xFFFF). Fixed by insertinga.asr(T0, T0, 16, sf=0)beforebfi.REX.Wtakes priority over0x66(66 48 98->cdqe,66 48 99->cqo)._emit_ret(C2 iwret imm16crashed withIndexErrorand failed forimm16 >= 4096):ins.ops[1]instead ofins.ops[0][1], raisingIndexError: tuple index out of rangeon everyret imm16.imm16directly to ARM64ADD imm12(0..4095), raisingAssertionErrorforimm16 >= 4096. Fixed usinga.li(T0, imm); a.add_r(GMAP[4], GMAP[4], T0)whenimm >= 4096._emit_call(indirectcall [rsp+disp]/call rspdecrementedrspbefore loading the target):str_pre(T0, GMAP[4], -8)) beforeself._load(ins, op, ADDR_T)causedcall [rsp]to read the pushed return address andcall [rsp+8]to read 8 bytes too low.ADDR_Tbefore pushing the return address._emit_branch(jrcxzvs67 jecxzandloop/loope/loopne):E3(jrcxz) hardcodedsf=0, ignoring the upper 32 bits ofrcxin 64-bit mode; fixed tosf = 0 if ins.addrsize else 1.loop/loope/loopnenow respect0x67(ins.addrsize) for 32-bitecxvs 64-bitrcx, andloope/loopnecheckself.arm_pathso cross-blockZFreadsctx.eflagsvia_ctx_jccinstead of stale ARMZ.Merge compatibility
_emit_conv(lines 1855–1872) and_emit_branch/_emit_call/_emit_ret(lines 2140–2175 onmain).#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)tools/x64_recomp.pytoupstream/maincauses 8 failures across all 5 static tests intests/test_x64_recomp_control_conv.py.#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 OKtests/test_x64_recomp_sse.py— 16/16 OKtests/test_x64_recomp_bitops.py— 9/9 OKtests/test_x64_recomp_rcl_rcr.py— 5/5 OKtests/test_x64_recomp_std_str.py— 5/5 OKtests/test_x64_recomp_muldiv.py— 10/10 OKtests/test_x64_recomp_shifts.py— 9/9 OKtests/test_x64_recomp_atomics.py— 10/10 OKtests/test_x64_recomp_incdec.py— 8/8 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_control_conv.py— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_control_conv.py— OKpython3 -I tests/test_ci_registration.py— 15/15 OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKImported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T21:51:07Z.
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.
Imported from ubiquitarianism/leonida. Originally posted by whoalin1 on 2026-11-24T23:24:58Z.
Independent verification (Devin, AI-assisted — CPU-only, no GPU).
Checked out head
fa76977detached. Diff touches tools/x64_recomp.py + new tests/test_x64_recomp_control_conv.py.Semantics reviewed:
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 skippedpy -3.14 -m compileall -q tools tests make_game.py→ cleanVerdict: 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.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T03:49:44Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainata73a57e13into the branch locally (969 commits behind, no conflicts). Head tested:082b42d8b.Against a reference model this branch is right in all 119 checks.
maingets 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):
xr.run_kernelwherexr.run_resultis needed);RUNNERswapped forxr.run_result: 2 errors left, both QEMU timeouts;cwd/cqotest, which hangs inAsm.lionmain(fixed in #1339, #1205, #1329). With that branch patched in memory: 1 error left,test_ret_imm16_small_and_large_stack_cleanup;run_kernelonmain, a guestrethangs 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 0x10andret 0xff8hang for that reason whileret 0,ret 0x1000andret 0x2000pass;Beyond the file, generated cases under QEMU against a small Python model:
cbw,cwde,cdqe,cwd,cdq,cqo, plus66 48 98and66 48 99, sixraxvalues each, fullraxand fullrdx(96 checks);jrcxzand67 jecxzwithrcx= 0, 1,1<<32,(1<<32)+5,1<<63(10);loopand67 loopiteration counts for 1, 2, 5, the latter with junk in the upper half ofrcx(6);ret imm16restoringrspfor 0, 8, 0x10, 0xff8, 0x1000, 0x2000 (6), and thecall [rsp]/call [rsp+8]sequence from the file (1).ret 8,ret 0x10,ret 0xff8: the harness hang above, not the emittermain66 48 98×6 and66 48 99×6 (treated ascbw/cwd),cwd×4 (copiesaxintodx),jrcxz×2 (testsecxonly),67 loop×3 (timeout),ret imm16×6 (timeout),call [rsp]×1 (returns 0x20, want 0x40)Counts.
tests/test_x64_recomp.py44/44 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 512 pass; the 2 that fail aremain'stest_mod_pack_output_aliasand the new file, for the reasons above.Not tested or not judged:
loope/loopnebeyond the file's own cases, the upper half ofrcxafter a67-prefixedloop,ret imm16above 0x2000, and indirect calls through other addressing modes. No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:39:03Z.
Red/green: tests/test_x64_recomp_control_conv.py fails 8 static emitter assertions on main (cwd/REX.W, ret imm16, call [rsp], jrcxz width) and passes with the PR (cloud run on main
7daa4a4; execution tests skipped).Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
7daa4a4a5in GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)