[PR #1379] x64_recomp: preserve ARM NZCV in _maybe_mkflags and ctx.eflags in _emit_flagctl #1409

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

📋 Pull Request Information

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

Base: main ← Head: fix/x64-recomp-flagctl-preserve


📝 Commits (1)

  • 5d91014 x64_recomp: preserve ARM NZCV in _maybe_mkflags and ctx.eflags in _emit_flagctl

📊 Changes

2 files changed (+240 additions, -9 deletions)

View changed files

➕ tests/test_x64_recomp_flagctl_mat.py (+229 -0)
📝 tools/x64_recomp.py (+11 -9)

📄 Description

Item ID: X64-RECOMP-SUB-MAT-FLAGCTL

Summary

Fixes two flag-materialization bugs in tools/x64_recomp.py:

  1. _maybe_mkflags inverted live ARM NZCV.C in-place for materialized SUB_KIND instructions:

    • Previously, _maybe_mkflags flipped bit 29 of ARM NZCV (eor_i(GTMP, GTMP, 0x20000000); msr_nzcv(GTMP)) before calling rt_mkflags(0) and never restored it — even though rt_mkflags already implements kind == 1 (SUB_KIND) to invert ARM borrow into ctx.eflags while preserving NZCV in x5.
    • Whenever any SUB_KIND instruction (cmp, sub, sbb, neg, cmps, scas) materialized flags for a downstream block and also fed a same-block carry consumer via arm_path, the same-block consumer assumed SUB_KIND ARM polarity (!CF) and got the inverted carry flag.
    • This broke the standard compiler 3-way unsigned compare (cmp a, b; jb less; je equal), where je in the fallthrough block forces cmp into self.mat: on main, (3 < 5) jumped to greater and (9 > 5) jumped to less.
    • Fix: pass kind = 2 if ins.mnem in LOGIC_KIND else (1 if ins.mnem in SUB_KIND else 0) to self._mkflags(kind) without touching ARM NZCV.
  2. _emit_flagctl (clc, stc, cmc) overwrote the entire ctx.eflags word:

    • Previously, _emit_flagctl ended with self._maybe_mkflags(ins), calling rt_mkflags(0) and rewriting SF, ZF, OF, PF, and AF in ctx.eflags from current NZCV and CTX_RES/CTX_A/CTX_B instead of updating only CF.
    • Fix: when ins.addr in self.mat, extract bit 29 (C) from ARM NZCV and patch only bit 0 (EF_CF) of ctx.eflags via bfi.

Tests (tests/test_x64_recomp_flagctl_mat.py)

  • 2 static tests: verifies _maybe_mkflags passes kind = 1 (movz w0, #1) without inverting GTMP/NZCV, and verifies materialized clc/stc/cmc patch EF_CF without emitting a second bl rt_mkflags.
  • 3 hardware execution suites:
    • 3-way unsigned cmp; jb; je across 7 (a, b) pairs (small, equal, signed-boundary 0x7FFFFFFF vs 0x80000000).
    • Materialized sub with same-block setc + cross-block sets, and sub; cmc; jmp +0 flag checks.
    • Cross-block clc/stc/cmc preserving OF/SF/ZF after sub, popf, and xor (LOGIC_KIND).

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone checks (no game files):
    • python3 -I tests/test_x64_recomp_flagctl_mat.py — 5 tests (2 static OK, 3 exec skipped without runner)
    • python3 -I tests/test_x64_recomp.py — OK
    • python3 -m compileall -q make_game.py tools tests — OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_flagctl_mat.py — OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Live Apple M5 hardware mutation proof on upstream/main (0f98d193c + #1174 runner without this fix):
    • 17 failures across all 5 test methods (every cmp; jb; je, sub; setc; jmp; sets, sub; cmc, and static assertion failed on main).
  • Native Apple M5 hardware execution (18-way scratch merge with #1174 and all open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_flagctl_mat.py: 5/5 passed (0 skipped)
    • All 17 test_x64_recomp_*.py suites combined: 128/128 passed (0 skipped)

AI-assisted: yes


🔄 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/1379 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-flagctl-preserve` --- ### 📝 Commits (1) - [`5d91014`](https://github.com/ubiquitarianism/leonida/commit/5d910148258790fa9750eb9cd642278bcdcf0130) x64_recomp: preserve ARM NZCV in _maybe_mkflags and ctx.eflags in _emit_flagctl ### 📊 Changes **2 files changed** (+240 additions, -9 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_flagctl_mat.py` (+229 -0) 📝 `tools/x64_recomp.py` (+11 -9) </details> ### 📄 Description Item ID: `X64-RECOMP-SUB-MAT-FLAGCTL` ### Summary Fixes two flag-materialization bugs in `tools/x64_recomp.py`: 1. **`_maybe_mkflags` inverted live ARM `NZCV.C` in-place for materialized `SUB_KIND` instructions**: - Previously, `_maybe_mkflags` flipped bit 29 of ARM `NZCV` (`eor_i(GTMP, GTMP, 0x20000000); msr_nzcv(GTMP)`) before calling `rt_mkflags(0)` and never restored it — even though `rt_mkflags` already implements `kind == 1` (`SUB_KIND`) to invert ARM borrow into `ctx.eflags` while preserving `NZCV` in `x5`. - Whenever any `SUB_KIND` instruction (`cmp`, `sub`, `sbb`, `neg`, `cmps`, `scas`) materialized flags for a downstream block **and** also fed a same-block carry consumer via `arm_path`, the same-block consumer assumed `SUB_KIND` ARM polarity (`!CF`) and got the **inverted** carry flag. - This broke the standard compiler 3-way unsigned compare (`cmp a, b; jb less; je equal`), where `je` in the fallthrough block forces `cmp` into `self.mat`: on `main`, `(3 < 5)` jumped to `greater` and `(9 > 5)` jumped to `less`. - **Fix**: pass `kind = 2 if ins.mnem in LOGIC_KIND else (1 if ins.mnem in SUB_KIND else 0)` to `self._mkflags(kind)` without touching ARM `NZCV`. 2. **`_emit_flagctl` (`clc`, `stc`, `cmc`) overwrote the entire `ctx.eflags` word**: - Previously, `_emit_flagctl` ended with `self._maybe_mkflags(ins)`, calling `rt_mkflags(0)` and rewriting `SF`, `ZF`, `OF`, `PF`, and `AF` in `ctx.eflags` from current `NZCV` and `CTX_RES`/`CTX_A`/`CTX_B` instead of updating only `CF`. - **Fix**: when `ins.addr in self.mat`, extract bit 29 (`C`) from ARM `NZCV` and patch only bit 0 (`EF_CF`) of `ctx.eflags` via `bfi`. ### Tests (`tests/test_x64_recomp_flagctl_mat.py`) - 2 static tests: verifies `_maybe_mkflags` passes `kind = 1` (`movz w0, #1`) without inverting `GTMP`/`NZCV`, and verifies materialized `clc`/`stc`/`cmc` patch `EF_CF` without emitting a second `bl rt_mkflags`. - 3 hardware execution suites: - 3-way unsigned `cmp; jb; je` across 7 `(a, b)` pairs (small, equal, signed-boundary `0x7FFFFFFF` vs `0x80000000`). - Materialized `sub` with same-block `setc` + cross-block `sets`, and `sub; cmc; jmp +0` flag checks. - Cross-block `clc`/`stc`/`cmc` preserving `OF`/`SF`/`ZF` after `sub`, `popf`, and `xor` (`LOGIC_KIND`). ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone checks (no game files)**: - `python3 -I tests/test_x64_recomp_flagctl_mat.py` — 5 tests (2 static OK, 3 exec skipped without runner) - `python3 -I tests/test_x64_recomp.py` — OK - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_flagctl_mat.py` — OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Live Apple M5 hardware mutation proof on `upstream/main` (`0f98d193c` + #1174 runner without this fix)**: - **17 failures** across all 5 test methods (every `cmp; jb; je`, `sub; setc; jmp; sets`, `sub; cmc`, and static assertion failed on `main`). - **Native Apple M5 hardware execution (18-way scratch merge with #1174 and all open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_flagctl_mat.py`: **5/5 passed** (0 skipped) - All 17 `test_x64_recomp_*.py` suites combined: **128/128 passed** (0 skipped) AI-assisted: yes --- <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#1409
No description provided.