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

Open
github-bot wants to merge 1 commit from github-bot/gh-1296 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 21:25 UTC · original
IlyassRS3:fix/x64-recomp-shift-width-cf → main · +480 −36 · 2 files · 1 commits · open

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
**@⁠IlyassRS3** on GitHub · 2026-11-24 21:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1296) `IlyassRS3:fix/x64-recomp-shift-width-cf` → `main` · +480 −36 · 2 files · 1 commits · open 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:46:25 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 21:26 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 21:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1296#pullrequestreview-6047679229) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 00:34 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 61732aca1 into the branch locally (925 commits behind, no conflicts). Head tested: 75d6d3469.

Against a reference model: 509 of 2,496 checks are wrong on main, 0 of 2,496 on this branch.

  • The new test file has the wiring bug from #1409: as shipped under QEMU, python -I tests/test_x64_recomp_shifts.py is 9 run, 64 failures, because _runner's QEMU branch returns xr.run_kernel (raw bytes) where xr.run_result (the parsed value) is needed. With RUNNER swapped for xr.run_result in memory (no file changed): 9/9.

  • Beyond the file, generated cases under QEMU, each compared against a small Python model of the x86 rules:

    • shl, shr, sar, rol, ror on rax/eax/ax/al;
    • the by-1 (D0/D1), imm8 (C0/C1) and cl (D2/D3) forms;
    • counts 1, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, so the 5- and 6-bit count masks, counts equal to and above the operand width, and 8/16-bit rotates by a multiple of the width;
    • checked per case: the full 64-bit register (so the untouched upper bits, and the zero-extension of a 32-bit write), CF, and ZF and SF for the three shifts. Where the masked count is 0 the check is that a preceding stc survives.
    tool checks wrong
    main (61732aca1) 2,496 509
    this branch 2,496 0
  • Counts. tests/test_x64_recomp.py 33/33 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 508 pass; the 6 that fail are main's 5 plus the new file, for the wiring reason.

  • Overlap. #1211 (rcl/rcr) adds a branch at the top of the same _emit_shift; the two will need reconciling by whichever lands second. I did not test them together.

Not tested: memory destinations, OF, PF and AF, and CF for shift counts above the operand width (the model skips it as undefined). The zero-count-after-cmp case from my finding in #1608 is separate and unchanged by this. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 00:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1296#issuecomment-7178904689) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `61732aca1` into the branch locally (925 commits behind, no conflicts). Head tested: `75d6d3469`. **Against a reference model: 509 of 2,496 checks are wrong on `main`, 0 of 2,496 on this branch.** - The new test file has the wiring bug from #1409: as shipped under QEMU, `python -I tests/test_x64_recomp_shifts.py` is 9 run, 64 failures, because `_runner`'s QEMU branch returns `xr.run_kernel` (raw bytes) where `xr.run_result` (the parsed value) is needed. With `RUNNER` swapped for `xr.run_result` in memory (no file changed): 9/9. - Beyond the file, generated cases under QEMU, each compared against a small Python model of the x86 rules: - `shl`, `shr`, `sar`, `rol`, `ror` on `rax`/`eax`/`ax`/`al`; - the by-1 (`D0`/`D1`), `imm8` (`C0`/`C1`) and `cl` (`D2`/`D3`) forms; - counts 1, 3, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, so the 5- and 6-bit count masks, counts equal to and above the operand width, and 8/16-bit rotates by a multiple of the width; - checked per case: the full 64-bit register (so the untouched upper bits, and the zero-extension of a 32-bit write), CF, and ZF and SF for the three shifts. Where the masked count is 0 the check is that a preceding `stc` survives. | tool | checks | wrong | |---|---|---| | `main` (`61732aca1`) | 2,496 | 509 | | this branch | 2,496 | 0 | - **Counts.** `tests/test_x64_recomp.py` 33/33 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 508 pass; the 6 that fail are `main`'s 5 plus the new file, for the wiring reason. - **Overlap.** #1211 (rcl/rcr) adds a branch at the top of the same `_emit_shift`; the two will need reconciling by whichever lands second. I did not test them together. Not tested: memory destinations, OF, PF and AF, and CF for shift counts above the operand width (the model skips it as undefined). The zero-count-after-`cmp` case from my finding in #1608 is separate and unchanged by this. No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1296/head:github-bot/gh-1296'
git switch 'github-bot/gh-1296'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1296'
git switch 'github-bot/gh-1296'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1296'
git switch 'github-bot/gh-1296'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1296'
git switch 'main'
git merge --squash 'github-bot/gh-1296'
git switch 'main'
git merge --ff-only 'github-bot/gh-1296'
git switch 'main'
git merge 'github-bot/gh-1296'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.