x64_recomp: sign-extend ALU/CMP/TEST/MOV immediates, fix Asm.li(-1), and handle movnti (X64-RECOMP-SIGNEXT-IMM) #1339
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!1339
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/x64-recomp-signext-imm"
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?
Pull request successfully merged and closed
Item ID:
X64-RECOMP-SIGNEXT-IMMAI-assisted: yes
Summary
Fixes silent zero-extension of negative immediates across
_emit_alu,_emit_cmp, and_emit_mov, fixesAsm.li(-1)crash, and handlesmovntiintools/x64_recomp.py:imm8/imm32to the immediate's encoded width:_finishand_finish_groupalready decode83 /r ib,81 /r id,05..3D id,F7 /0 id, andA9 idas signed Python integers, but_emit_aluand_emit_cmpcalledself._load(ins, src, T1)withoutas_size=sz._loadmaskedop[1]with(1 << (src[2] * 8)) - 1, so:and rsp, -16(48 83 e4 f0, standard stack alignment) loaded+0xF0instead of0xFFFFFFFFFFFFFFF0, zeroing bits 8..63 ofrsp.cmp rax, -1(48 83 f8 ff/48 81 f8 ff ff ff ff/48 3d ff ff ff ff) compared against+0xFFor+0xFFFFFFFFinstead 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/xorwith negativeimm8/imm32added/subtracted+0xFFor+0xFFFFFFFF.as_size=sztoself._load(ins, src, T1, as_size=sz)in_emit_aluand_emit_cmp, and stashingCTX_A/CTX_Bwhenins.addr in self.matas well as"A" in need.mov qword ptr [mem], imm32(48 C7 /0) andmovnti(0F C3):_emit_mov's special case forC7 /0sign-extension only checkeddst[0] == "r", somov qword ptr [rsp], -1stored0x00000000FFFFFFFFinstead of0xFFFFFFFFFFFFFFFF. Passingas_size=self._op_size(dst)to_loadfixes both register and memory destinations and removes the redundant branch."movnti"alongside"mov"so 32/64-bit non-temporal stores translate cleanly instead of raisingUnhandledInsn("movnti").Asm.li(rd, -1):elif not nzc: self.movn(rd, 0)(identical to#1205and#1329) so materializing0xFFFFFFFFFFFFFFFF(forcmp rax, -1,mov [rsp], -1,push -1, etc.) emitsMOVNinstead of crashing withIndexError.Merge compatibility
#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)tools/x64_recomp.pytoupstream/maincauses 8 failures across all 4 static tests intests/test_x64_recomp_signext_imm.py.#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)test_x64_recomp*.pysuites — 134/134 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_signext_imm.py— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_signext_imm.py— OKpython3 -I tests/test_ci_registration.py— 15/15 OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKIndependent 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:
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 skippedpy -3.14 -m compileall -q tools tests make_game.py→ cleanVerdict: correct on inspection; tests pass. Execution tests skipped on this host. Base is main — overlaps #1329 on the li() hunk.
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
mainat5fab93967into 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 returnsxr.run_kernel(raw output bytes) wherexr.run_result(the parsed value) is needed. The raw outputs already show the right numbers. WithRUNNERswapped forxr.run_resultin memory (no file changed): 8/8 pass under QEMU.Extra cases under QEMU, reading rax afterwards:
main48 c7 c0 01 00 00 80(mov rax, imm32)ffffffff80000001fffffffe80000001ffffffff8000000148 c7 c0 ff ff ff ff(mov rax, -1)fffffffffffffffffffffffeffffffffffffffffffffffff48 83 e0 f0(and rax, -16), rax=111111111234567811111111123456700000000000000070111111111234567083 c8 ff(or eax, -1), high half set00000000ffffffff00000000000000ff00000000ffffffffUnchanged 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, and66 c7 44 24 f8 fe ffread back withmovzx.Counts.
tests/test_x64_recomp.py33/33 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 512 run, 507 pass; the 5 that fail aremain's 4 plus the new file, for the reason above.One thing to coordinate: this adds
movntito_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.
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)