[PR #1313] x64_recomp: fix 8/16-bit inc/dec, CF preservation, and inc/dec/neg AF (X64-RECOMP-INCDEC-WIDTH-CF) #1343

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

📋 Pull Request Information

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

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


📝 Commits (1)

  • 7e38867 x64_recomp: fix 8/16-bit inc/dec, CF preservation, and inc/dec/neg AF

📊 Changes

2 files changed (+340 additions, -18 deletions)

View changed files

➕ tests/test_x64_recomp_incdec.py (+294 -0)
📝 tools/x64_recomp.py (+46 -18)

📄 Description

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

Summary

Fixes five correctness bugs in tools/x64_recomp.py (_emit_incdec and _emit_negnot):

  1. 8-bit and 16-bit inc/dec added/subtracted 1 after shifting T0 left by 24/16:
    • _emit_incdec shifted T0 left by sh = 32 - sz * 8, then emitted adds/subs with immediate 1 and shifted right by sh.
    • This made 8-bit inc al and 16-bit inc ax complete no-ops (((v << 24) + 1) >> 24 == v) and broke ZF on 8/16-bit dec ((1 << 24) - 1 == 0x00FFFFFF, leaving Z=0).
    • Fixed by adding/subtracting 1 << sh (movz T1, 1 << (sh - 16), hw=1, sf=0 -> 0x01000000 / 0x00010000) when flags are live, and using plain add/sub + uxtb/uxth when flags are dead.
  2. inc/dec destroyed CF via bfi(T3, GTMP, 29, 1) copying bit 0 instead of bit 29:
    • BFI inserts the lowest bits (bit 0, always 0 in MRS NZCV) of GTMP into bit 29 of T3, unconditionally zeroing CF whenever keep_c ran.
    • Fixed by extracting bit 29 with a.ubfx(GTMP, GTMP, 29, 1) before a.bfi(T3, GTMP, 29, 1).
  3. keep_c = "C" in need was always False on same-block CF consumers:
    • Because "C" is not in FLAG_WRITERS["inc"]/FLAG_WRITERS["dec"] ("NZVPA"), self.needed[ins.addr] never contained "C", so stc; inc eax; jc or add; inc; adc clobbered ARM C.
    • Fixed by using non-flag-setting add/sub when not flaggy and always preserving ARM C around adds/subs when flaggy.
  4. Materialized dec inverted surviving CF via SUB_KIND:
    • _maybe_mkflags flipped ARM bit 29 for every SUB_KIND instruction including dec, even though dec does not write CF (inverting an upstream stc/clc/cmp carry bit).
    • Fixed in _emit_incdec by calling self._mkflags(0) directly on ins.addr in self.mat while saving/restoring bit 0 (EF_CF) of ctx.eflags in GTMP (preserved across rt_mkflags).
  5. Missing AF operand stashing on inc/dec/neg, and not running _stash_res/_maybe_mkflags:
    • Stashed CTX_A/CTX_B for inc/dec (orig, 1) and neg (0, orig) whenever "A" in need or ins.addr in self.mat so rt_mkflags computes exact AF.
    • Made not return immediately after _store so it never touches flags or context flag slots.

Merge compatibility

  • Keeps edits strictly inside _emit_incdec and _emit_negnot (lines 1655–1698 on main).
  • Verified 0 merge conflicts in a 9-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, and #1306.

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_incdec.py -v — 8 tests (5 static ok, 3 exec skipped without runner)
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main causes 5/5 static test failures in tests/test_x64_recomp_incdec.py.
  • Native Apple M5 execution (scratch 9-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296, #1306 — 0 merge conflicts):
    • tests/test_x64_recomp_incdec.py — 8/8 OK (all 3 execution suites + 5 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
    • tests/test_x64_recomp_shifts.py — 9/9 OK
    • tests/test_x64_recomp_atomics.py — 10/10 OK
    • Total: 118/118 native arm64 tests passing.
  • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_incdec.py — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_incdec.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/1313 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-incdec-width-cf` --- ### 📝 Commits (1) - [`7e38867`](https://github.com/ubiquitarianism/leonida/commit/7e38867514b3ce9c528f1f951197a6cdd6eb3087) x64_recomp: fix 8/16-bit inc/dec, CF preservation, and inc/dec/neg AF ### 📊 Changes **2 files changed** (+340 additions, -18 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_incdec.py` (+294 -0) 📝 `tools/x64_recomp.py` (+46 -18) </details> ### 📄 Description Item ID: `X64-RECOMP-INCDEC-WIDTH-CF` AI-assisted: yes ## Summary Fixes five correctness bugs in `tools/x64_recomp.py` (`_emit_incdec` and `_emit_negnot`): 1. **8-bit and 16-bit `inc`/`dec` added/subtracted `1` after shifting `T0` left by 24/16**: - `_emit_incdec` shifted `T0` left by `sh = 32 - sz * 8`, then emitted `adds`/`subs` with immediate `1` and shifted right by `sh`. - This made 8-bit `inc al` and 16-bit `inc ax` complete no-ops (`((v << 24) + 1) >> 24 == v`) and broke `ZF` on 8/16-bit `dec` (`(1 << 24) - 1 == 0x00FFFFFF`, leaving `Z=0`). - Fixed by adding/subtracting `1 << sh` (`movz T1, 1 << (sh - 16), hw=1, sf=0` -> `0x01000000` / `0x00010000`) when flags are live, and using plain `add`/`sub` + `uxtb`/`uxth` when flags are dead. 2. **`inc`/`dec` destroyed `CF` via `bfi(T3, GTMP, 29, 1)` copying bit 0 instead of bit 29**: - `BFI` inserts the lowest bits (bit 0, always `0` in `MRS NZCV`) of `GTMP` into bit 29 of `T3`, unconditionally zeroing `CF` whenever `keep_c` ran. - Fixed by extracting bit 29 with `a.ubfx(GTMP, GTMP, 29, 1)` before `a.bfi(T3, GTMP, 29, 1)`. 3. **`keep_c = "C" in need` was always `False` on same-block `CF` consumers**: - Because `"C"` is not in `FLAG_WRITERS["inc"]`/`FLAG_WRITERS["dec"]` (`"NZVPA"`), `self.needed[ins.addr]` never contained `"C"`, so `stc; inc eax; jc` or `add; inc; adc` clobbered ARM `C`. - Fixed by using non-flag-setting `add`/`sub` when `not flaggy` and always preserving ARM `C` around `adds`/`subs` when `flaggy`. 4. **Materialized `dec` inverted surviving `CF` via `SUB_KIND`**: - `_maybe_mkflags` flipped ARM bit 29 for every `SUB_KIND` instruction including `dec`, even though `dec` does not write `CF` (inverting an upstream `stc`/`clc`/`cmp` carry bit). - Fixed in `_emit_incdec` by calling `self._mkflags(0)` directly on `ins.addr in self.mat` while saving/restoring bit 0 (`EF_CF`) of `ctx.eflags` in `GTMP` (preserved across `rt_mkflags`). 5. **Missing `AF` operand stashing on `inc`/`dec`/`neg`, and `not` running `_stash_res`/`_maybe_mkflags`**: - Stashed `CTX_A`/`CTX_B` for `inc`/`dec` (`orig`, `1`) and `neg` (`0`, `orig`) whenever `"A" in need or ins.addr in self.mat` so `rt_mkflags` computes exact `AF`. - Made `not` return immediately after `_store` so it never touches flags or context flag slots. ## Merge compatibility - Keeps edits strictly inside `_emit_incdec` and `_emit_negnot` (lines 1655–1698 on `main`). - Verified **0 merge conflicts** in a 9-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, and `#1306`. ## 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_incdec.py -v` — 8 tests (5 static ok, 3 exec skipped without runner) - Mutation proof: reverting `tools/x64_recomp.py` to `upstream/main` causes **5/5 static test failures** in `tests/test_x64_recomp_incdec.py`. - Native Apple M5 execution (scratch 9-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296`, `#1306` — 0 merge conflicts): - `tests/test_x64_recomp_incdec.py` — **8/8 OK** (all 3 execution suites + 5 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** - `tests/test_x64_recomp_shifts.py` — **9/9 OK** - `tests/test_x64_recomp_atomics.py` — **10/10 OK** - Total: **118/118 native arm64 tests passing**. - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_incdec.py` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_incdec.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#1343
No description provided.