x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY) #2150
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2150"
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 abnalstlll-byte on 2026-11-25T23:55:01Z.
Summary
Fixes carry flag inversion when an x86-64 shift or rotate instruction has a count of zero (
X64-RECOMP-SHIFT-ZERO-COUNT-CARRY).In x86-64, shifts and rotates (
shl,shr,sar,rol,ror) leave all arithmetic flags untouched when the masked count is zero. Intools/x64_recomp.py:flags_w = FLAG_WRITERS[mnem], causingFunctionTranslatorto treat them as flag producers inarm_path, while emitting no instructions (if cval == 0: return). Downstream condition checks (setc,jcc,adc,sbb) therefore used ADD_KIND conditioncsagainst the preceding SUB_KIND instruction (which set ARMC == 0on borrow), causing inverted carry flag evaluation.cl) skipped execution oncbz(T1, skip)whencl == 0, leaving ARM NZCV in the preceding SUB_KIND instruction's borrow polarity (C == 0for borrow), while downstream consumers expected ADD_KIND polarity (C == 1for borrow).Solution
_classify(), check ifins.ops[1]is immediate and its masked count (mod 32 for <64-bit, mod 64 for 64-bit) is 0. If so, clearflags_w = ""andflags_r = "". The instruction is not recorded as a flag writer, allowing downstream readers to directly see the true reaching flag producer inarm_path._flag_analysis(), trackshift_prev_c, recording the nearest carry writer preceding variable shifts within the basic block._emit_shift(), on thecl == 0path whenneed_cis true:SUB_KIND, invert ARM NZCV bit 29 (eor_i(GTMP, GTMP, 0x20000000)) before jumping toskip. This converts subtraction borrow polarity (C == 0for borrow) to ADD_KIND polarity (C == 1for borrow), perfectly satisfying downstream consumers.EF_CFfromCTX_EFLAGSinto NZCV bit 29.ADD_KIND, ARMCalready has the correct polarity._maybe_mkflags(ins)after theskiplabel so that basic blocks with materialized flags correctly capture the preserved flags whencl == 0.tests/test_x64_recomp.pycovering decode flags, reaching producer selection, arm64 translation words, and QEMU execution cases.Verification
python -I tests/test_x64_recomp.py: 41 tests pass (0 failures, 0 errors).python -m compileall -q make_game.py tools tests: Clean.python -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py: Clean (0 violations).python -I tests/test_text_write_line_endings.py: 3 tests pass.AI-assisted (Claude Code)
Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-26T00:36:24Z.
Code review at
5ce5769d4(Linux;tests/test_x64_recomp.pyhere runs 12 of its 41 tests and skips the 29 that needqemu-system-aarch64, so the new end-to-end cases did not run on my side).The immediate-zero case is right (no flag writer, so readers see the real producer), and so are the two variable-count paths you handle. One producer kind is still open on the
cl == 0path:and/or/xor/test,_arm_cond(tools/x64_recomp.py:1452-1457) does not read ARM C and V at all: x86 clears CF and OF there, sob/oare never taken andl/gemap tomi/pl. After a variable shift, though, the consumer's producer is the shift, anADD_KINDmnemonic, sobmaps tocs,otovsandltolt, all read from the ARM flags. Whencl == 0those are whatever the logic op left in C and V, which is not necessarily 0 (ARM's flag-setting logical ops leave V alone and C as the shifter left it). Sotest eax, eax; shl ebx, cl; jb/setc/adc/jlwithcl == 0can take the wrong branch.prev_cfinds the logic op (it hasCinflags_w),prev_c_in_subis false and it is notNone, so it goes to the plaincbzpath.prev_c.mnem in LOGIC_KIND, clear C and V in NZCV on the zero-count path (CF = OF = 0 as x86 left them), next to the SUB_KIND inversion. A case liketest_var_shift_sub_kind_producer_inversionwithtestbefore the shift would pin it.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-26T00:39:18Z.
Merged, thanks! x86 changes no flag for a shift or rotate whose masked count is 0, and the recompiler now follows that.
setc/jcsees the real producer.cl, changed by us (4a1c850a7): patching ARM's carry at the shift left one case wrong. With the carry from an earlier block,cmp r8d, 2(r8d = 1);jmp;mov cl, 0;shl eax, cl;setcread 0 instead of 1, because liveness treated the shift as always writing and the earlier writer never stored its word._flag_analysisnow treats a shift byclas a writer that may leave the flags alone: the flags stay live across it, the writer before it stores its word too, and a consumer behind it reads the ctx word. Withcl = 0the shift is skipped whole.docs/mobile.mdsays so.test_x64_recomp.py, including qemu runs of the carry from an earlier block withcl = 0andcl = 1and of a carry a logic op cleared; the first fails on the previous head. The docs and line-ending tests pass.main61732aca1+4a1c850a7: the semantic suite passes (495 files, 6,333 tests, 0 files failed);test_x64_recomppass; the 15 docs and writer tests pass;check_no_game_data.pyis clean on the 3 changed files.AI-assisted (Claude Code, Claude Opus 5.5).