mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 22:00:52 +01:00
[PR #2150] [MERGED] x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY) #2154
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2154
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/2150
Author: @abnalstlll-byte
Created: 11/26/2026
Status: ✅ Merged
Merged: 11/26/2026
Merged by: @aslan2142
Base:
main← Head:fix/x64-recomp-shift-zero-count-carry📝 Commits (3)
5ce5769x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY)fb3169fMerge main into fix/x64-recomp-shift-zero-count-carry (#2150)4a1c850x64_recomp: treat a shift by cl as a writer that may leave the flags alone (#2150 review)📊 Changes
3 files changed (+148 additions, -11 deletions)
View changed files
📝
docs/mobile.md(+1 -1)📝
tests/test_x64_recomp.py(+106 -0)📝
tools/x64_recomp.py(+41 -10)📄 Description
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)
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.