x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY) #2150
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!2150
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2150/head"
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?
@abnalstlll-byte on GitHub · 2026-11-25 23:55 UTC · original
abnalstlll-byte:fix/x64-recomp-shift-zero-count-carry→main· +148 −11 · 3 files · 3 commits · merged 2026-11-26 00:39 UTCSummary
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)
In x86-64, shifts and rotates (shl, shr, sar, rol, ror) leave all flags untouched when the count is zero. In tools/x64_recomp.py: 1. Immediate count 0 shifts were classified with flags_w = FLAG_WRITERS[mnem], causing FunctionTranslator to treat them as flag producers in arm_path, while emitting 0 instructions. Subsequent condition checks (e.g. setc/jcc) therefore checked ADD_KIND condition 'cs' against the preceding SUB_KIND instruction (which set C == 0 on borrow), inverting the carry flag. 2. Variable count shifts (cl) skipped execution on cbz(T1, skip) when cl == 0, leaving ARM NZCV in the preceding SUB_KIND instruction's polarity (C == 0 for borrow), while downstream consumers expected ADD_KIND polarity (C == 1 for borrow), inverting the carry flag. Fix: 1. In _classify(), mark shifts and rotates with immediate count 0 (including masked mod 32/64 zero counts) with flags_w = '' and flags_r = '' so they are not treated as flag writers. 2. In _flag_analysis(), track the nearest reaching carry writer before variable shifts in the basic block (shift_prev_c). 3. In _emit_shift(), on the cl == 0 path when carry is needed: - If the preceding carry producer was in SUB_KIND, invert NZCV bit 29 (C := !C) to convert subtraction borrow polarity into ADD_KIND carry polarity. - If there was no preceding carry producer in the block, restore CF from CTX_EFLAGS into NZCV bit 29. 4. Move _maybe_mkflags(ins) after the skip label so that materialized flag blocks preserve valid flags on both zero and non-zero count paths. 5. Add unit tests in tests/test_x64_recomp.py. AI-assisted (Claude Code)@qhukz13 on GitHub · 2026-11-26 00:36 UTC · original
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).
@aslan2142 on GitHub · 2026-11-26 00:39 UTC · original
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).