[PR #2150] [MERGED] x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY) #2154

Closed
opened 2026-11-26 21:20:13 +01:00 by 4ld0 · 0 comments
Owner

📋 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)

  • 5ce5769 x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY)
  • fb3169f Merge main into fix/x64-recomp-shift-zero-count-carry (#2150)
  • 4a1c850 x64_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. In tools/x64_recomp.py:

  1. Immediate count 0 shifts were unconditionally classified with flags_w = FLAG_WRITERS[mnem], causing FunctionTranslator to treat them as flag producers in arm_path, while emitting no instructions (if cval == 0: return). Downstream condition checks (setc, jcc, adc, sbb) therefore used ADD_KIND condition cs against the preceding SUB_KIND instruction (which set ARM C == 0 on borrow), causing inverted carry flag evaluation.
  2. Variable count shifts (cl) skipped execution on cbz(T1, skip) when cl == 0, leaving ARM NZCV in the preceding SUB_KIND instruction's borrow polarity (C == 0 for borrow), while downstream consumers expected ADD_KIND polarity (C == 1 for borrow).

Solution

  1. Immediate Zero Shifts: In _classify(), check if ins.ops[1] is immediate and its masked count (mod 32 for <64-bit, mod 64 for 64-bit) is 0. If so, clear flags_w = "" and flags_r = "". The instruction is not recorded as a flag writer, allowing downstream readers to directly see the true reaching flag producer in arm_path.
  2. Preceding Writer Tracking: In _flag_analysis(), track shift_prev_c, recording the nearest carry writer preceding variable shifts within the basic block.
  3. ARM Carry Normalization on Zero Count: In _emit_shift(), on the cl == 0 path when need_c is true:
    • If the preceding carry producer was in SUB_KIND, invert ARM NZCV bit 29 (eor_i(GTMP, GTMP, 0x20000000)) before jumping to skip. This converts subtraction borrow polarity (C == 0 for borrow) to ADD_KIND polarity (C == 1 for borrow), perfectly satisfying downstream consumers.
    • If there was no preceding carry producer in the block, restore EF_CF from CTX_EFLAGS into NZCV bit 29.
    • If the preceding producer was already in ADD_KIND, ARM C already has the correct polarity.
  4. Materialization: Place _maybe_mkflags(ins) after the skip label so that basic blocks with materialized flags correctly capture the preserved flags when cl == 0.
  5. Testing: Added unit tests in tests/test_x64_recomp.py covering 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.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2150 **Author:** [@abnalstlll-byte](https://github.com/abnalstlll-byte) **Created:** 11/26/2026 **Status:** ✅ Merged **Merged:** 11/26/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/x64-recomp-shift-zero-count-carry` --- ### 📝 Commits (3) - [`5ce5769`](https://github.com/ubiquitarianism/leonida/commit/5ce5769d48980b8d89baa30a423256de9ce8dcf0) x64_recomp: preserve carry flag across zero-count shifts (X64-RECOMP-SHIFT-ZERO-COUNT-CARRY) - [`fb3169f`](https://github.com/ubiquitarianism/leonida/commit/fb3169f76854c64fca5343d6afeb19bc9e41b31b) Merge main into fix/x64-recomp-shift-zero-count-carry (#2150) - [`4a1c850`](https://github.com/ubiquitarianism/leonida/commit/4a1c850a7ae26db84f116cca606c77834b1445b7) x64_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) <details> <summary>View changed files</summary> 📝 `docs/mobile.md` (+1 -1) 📝 `tests/test_x64_recomp.py` (+106 -0) 📝 `tools/x64_recomp.py` (+41 -10) </details> ### 📄 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. In `tools/x64_recomp.py`: 1. Immediate count 0 shifts were unconditionally classified with `flags_w = FLAG_WRITERS[mnem]`, causing `FunctionTranslator` to treat them as flag producers in `arm_path`, while emitting no instructions (`if cval == 0: return`). Downstream condition checks (`setc`, `jcc`, `adc`, `sbb`) therefore used ADD_KIND condition `cs` against the preceding SUB_KIND instruction (which set ARM `C == 0` on borrow), causing inverted carry flag evaluation. 2. Variable count shifts (`cl`) skipped execution on `cbz(T1, skip)` when `cl == 0`, leaving ARM NZCV in the preceding SUB_KIND instruction's borrow polarity (`C == 0` for borrow), while downstream consumers expected ADD_KIND polarity (`C == 1` for borrow). ## Solution 1. **Immediate Zero Shifts**: In `_classify()`, check if `ins.ops[1]` is immediate and its masked count (mod 32 for <64-bit, mod 64 for 64-bit) is 0. If so, clear `flags_w = ""` and `flags_r = ""`. The instruction is not recorded as a flag writer, allowing downstream readers to directly see the true reaching flag producer in `arm_path`. 2. **Preceding Writer Tracking**: In `_flag_analysis()`, track `shift_prev_c`, recording the nearest carry writer preceding variable shifts within the basic block. 3. **ARM Carry Normalization on Zero Count**: In `_emit_shift()`, on the `cl == 0` path when `need_c` is true: - If the preceding carry producer was in `SUB_KIND`, invert ARM NZCV bit 29 (`eor_i(GTMP, GTMP, 0x20000000)`) before jumping to `skip`. This converts subtraction borrow polarity (`C == 0` for borrow) to ADD_KIND polarity (`C == 1` for borrow), perfectly satisfying downstream consumers. - If there was no preceding carry producer in the block, restore `EF_CF` from `CTX_EFLAGS` into NZCV bit 29. - If the preceding producer was already in `ADD_KIND`, ARM `C` already has the correct polarity. 4. **Materialization**: Place `_maybe_mkflags(ins)` after the `skip` label so that basic blocks with materialized flags correctly capture the preserved flags when `cl == 0`. 5. **Testing**: Added unit tests in `tests/test_x64_recomp.py` covering 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:20:13 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2154
No description provided.