[PR #1296] x64_recomp: fix 64-bit shift CF and narrow shift/rotate semantics (X64-RECOMP-SHIFT-WIDTH-CF) #1327

Open
opened 2026-11-26 20:20:52 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1296
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: fix/x64-recomp-shift-width-cf


📝 Commits (1)

  • 75d6d34 x64_recomp: fix 64-bit shift CF and narrow shift/rotate semantics

📊 Changes

2 files changed (+480 additions, -36 deletions)

View changed files

➕ tests/test_x64_recomp_shifts.py (+368 -0)
📝 tools/x64_recomp.py (+112 -36)

📄 Description

Item ID: X64-RECOMP-SHIFT-WIDTH-CF
AI-assisted: yes

Summary

Fixes six correctness bugs in tools/x64_recomp.py (_emit_shift, _shift_cf_imm, _shift_cf_var):

  1. 64-bit shl/shr/sar/ror CF extraction used sf=0:
    • _shift_cf_imm passed sf=0 to a.ubfx(T3, ..., bit, 1, sf=0), emitting illegal 32-bit UBFM encodings (immr/imms >= 32) for any 64-bit shift/rotate extracting bit >= 32 (shl rax, 1, shr rax, 40, sar rax, 40, ror rax, 1, etc.).
    • _shift_cf_var passed sf=0 to lsrv/lsr, silently truncating the 64-bit shift amount mod 32 and reading only the low 32 bits of T2/T0.
    • Fixed by passing sf = 1 if width == 64 else 0.
  2. 8-bit and 16-bit sar shifted zero-extended T0:
    • _load zero-extends 8/16-bit operands to 32 bits, so sar al, ... / sar ax, ... shifted in 0s instead of the sign bit for negative bytes/words (e.g. sar al, 1 on 0x80 returned 0x40 instead of 0xC0).
    • Fixed by sign-extending T0 via sxtb/sxth before asr/asrv and masking back with uxtb/uxth.
  3. 8-bit and 16-bit rol/ror rotated as 32-bit values and emitted illegal extr for cval % width == 0:
    • Rotating an 8/16-bit zero-extended operand via 32-bit ror/rorv shifted zeros into the byte/word, and cval % width or width produced an invalid extr(..., 0) / ror(..., width) when cval was a multiple of width (e.g. rol al, 8, ror ax, 16).
    • Fixed by replicating the narrow operand across 32 bits (orr_r shift 8/16) before rotating and masking back with uxtb/uxth, and skipping the rotate instruction on immediate cval % width == 0 while still computing CF.
  4. 8-bit and 16-bit shl/shr/sar computed SF/ZF at 32-bit width and zeroed CF on cval == width:
    • Testing the 32-bit zero-extended T0 missed SF (bit 7/15) and missed ZF when shl shifted non-zero bits into bits 8..31. Fixed by masking T0 to width, then sign-extending into T3 via sxtb/sxth before tst_r.
    • On shl/shr with cval == width, x86 sets CF to the last bit shifted out (orig[0] / orig[width-1]) rather than 0; on sar with cval >= width, CF receives the sign bit (orig[width-1]). Fixed in _shift_cf_imm (cval > width and min(cval - 1, width - 1)) and _shift_cf_var (asrv for sar).
  5. rol/ror clobbered N/Z with tst_r and clobbered PF/ctx.eflags on materialization:
    • On x86-64, rol/ror update CF (and OF when count == 1) without modifying SF, ZF, AF, or PF.
    • Stopped emitting tst_r and _stash_res for rol/ror, and when materialized (ins.addr in self.mat), patch only bit 0 (EF_CF) of ctx.eflags directly instead of calling rt_mkflags.

Merge compatibility

  • Keeps edits strictly inside _emit_shift, _shift_cf_imm, and _shift_cf_var (preserving a 3-line buffer around lines 1882–1885 and _emit_mul) so it merges with 0 conflicts alongside #1174, #1202, #1205, #1211, #1213, and #1279.

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_shifts.py -v — 9 tests (4 static ok, 5 exec skipped without runner)
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main causes 10 failures across all 4 static tests in tests/test_x64_recomp_shifts.py.
  • Native Apple M5 execution (scratch 7-way merge with #1174, #1202, #1205, #1211, #1213, #1279 — 0 merge conflicts):
    • tests/test_x64_recomp_shifts.py — 9/9 OK (all 5 execution suites + 4 static tests, 0 skips)
    • tests/test_x64_recomp.py — 46/46 OK
    • tests/test_x64_recomp_sse.py — 16/16 OK
    • tests/test_x64_recomp_bitops.py — 9/9 OK
    • tests/test_x64_recomp_rcl_rcr.py — 5/5 OK
    • tests/test_x64_recomp_std_str.py — 5/5 OK
    • tests/test_x64_recomp_muldiv.py — 10/10 OK
    • Total: 100/100 native arm64 tests passing.
  • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_shifts.py — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_shifts.py — OK
  • python3 -I tests/test_ci_registration.py — 15/15 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK

🔄 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/1296 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-shift-width-cf` --- ### 📝 Commits (1) - [`75d6d34`](https://github.com/ubiquitarianism/leonida/commit/75d6d346918cd72691eb3eb24168dc9a477afc3b) x64_recomp: fix 64-bit shift CF and narrow shift/rotate semantics ### 📊 Changes **2 files changed** (+480 additions, -36 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_shifts.py` (+368 -0) 📝 `tools/x64_recomp.py` (+112 -36) </details> ### 📄 Description Item ID: `X64-RECOMP-SHIFT-WIDTH-CF` AI-assisted: yes ## Summary Fixes six correctness bugs in `tools/x64_recomp.py` (`_emit_shift`, `_shift_cf_imm`, `_shift_cf_var`): 1. **64-bit `shl`/`shr`/`sar`/`ror` CF extraction used `sf=0`**: - `_shift_cf_imm` passed `sf=0` to `a.ubfx(T3, ..., bit, 1, sf=0)`, emitting **illegal 32-bit `UBFM`** encodings (`immr`/`imms >= 32`) for any 64-bit shift/rotate extracting bit `>= 32` (`shl rax, 1`, `shr rax, 40`, `sar rax, 40`, `ror rax, 1`, etc.). - `_shift_cf_var` passed `sf=0` to `lsrv`/`lsr`, silently truncating the 64-bit shift amount mod 32 and reading only the low 32 bits of `T2`/`T0`. - Fixed by passing `sf = 1 if width == 64 else 0`. 2. **8-bit and 16-bit `sar` shifted zero-extended `T0`**: - `_load` zero-extends 8/16-bit operands to 32 bits, so `sar al, ...` / `sar ax, ...` shifted in 0s instead of the sign bit for negative bytes/words (e.g. `sar al, 1` on `0x80` returned `0x40` instead of `0xC0`). - Fixed by sign-extending `T0` via `sxtb`/`sxth` before `asr`/`asrv` and masking back with `uxtb`/`uxth`. 3. **8-bit and 16-bit `rol`/`ror` rotated as 32-bit values and emitted illegal `extr` for `cval % width == 0`**: - Rotating an 8/16-bit zero-extended operand via 32-bit `ror`/`rorv` shifted zeros into the byte/word, and `cval % width or width` produced an invalid `extr(..., 0)` / `ror(..., width)` when `cval` was a multiple of `width` (e.g. `rol al, 8`, `ror ax, 16`). - Fixed by replicating the narrow operand across 32 bits (`orr_r` shift 8/16) before rotating and masking back with `uxtb`/`uxth`, and skipping the rotate instruction on immediate `cval % width == 0` while still computing `CF`. 4. **8-bit and 16-bit `shl`/`shr`/`sar` computed `SF`/`ZF` at 32-bit width and zeroed `CF` on `cval == width`**: - Testing the 32-bit zero-extended `T0` missed `SF` (bit 7/15) and missed `ZF` when `shl` shifted non-zero bits into bits 8..31. Fixed by masking `T0` to `width`, then sign-extending into `T3` via `sxtb`/`sxth` before `tst_r`. - On `shl`/`shr` with `cval == width`, x86 sets `CF` to the last bit shifted out (`orig[0]` / `orig[width-1]`) rather than `0`; on `sar` with `cval >= width`, `CF` receives the sign bit (`orig[width-1]`). Fixed in `_shift_cf_imm` (`cval > width` and `min(cval - 1, width - 1)`) and `_shift_cf_var` (`asrv` for `sar`). 5. **`rol`/`ror` clobbered `N`/`Z` with `tst_r` and clobbered `PF`/`ctx.eflags` on materialization**: - On x86-64, `rol`/`ror` update `CF` (and `OF` when count == 1) without modifying `SF`, `ZF`, `AF`, or `PF`. - Stopped emitting `tst_r` and `_stash_res` for `rol`/`ror`, and when materialized (`ins.addr in self.mat`), patch only bit 0 (`EF_CF`) of `ctx.eflags` directly instead of calling `rt_mkflags`. ## Merge compatibility - Keeps edits strictly inside `_emit_shift`, `_shift_cf_imm`, and `_shift_cf_var` (preserving a 3-line buffer around lines 1882–1885 and `_emit_mul`) so it merges with **0 conflicts** alongside `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, and `#1279`. ## 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_shifts.py -v` — 9 tests (4 static ok, 5 exec skipped without runner) - Mutation proof: reverting `tools/x64_recomp.py` to `upstream/main` causes **10 failures across all 4 static tests** in `tests/test_x64_recomp_shifts.py`. - Native Apple M5 execution (scratch 7-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279` — 0 merge conflicts): - `tests/test_x64_recomp_shifts.py` — **9/9 OK** (all 5 execution suites + 4 static tests, 0 skips) - `tests/test_x64_recomp.py` — **46/46 OK** - `tests/test_x64_recomp_sse.py` — **16/16 OK** - `tests/test_x64_recomp_bitops.py` — **9/9 OK** - `tests/test_x64_recomp_rcl_rcr.py` — **5/5 OK** - `tests/test_x64_recomp_std_str.py` — **5/5 OK** - `tests/test_x64_recomp_muldiv.py` — **10/10 OK** - Total: **100/100 native arm64 tests passing**. - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_shifts.py` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_shifts.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#1327
No description provided.