x64_recomp: preserve ARM NZCV in _maybe_mkflags and ctx.eflags in _emit_flagctl #1379

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

@⁠IlyassRS3 on GitHub · 2026-11-24 22:56 UTC · original
IlyassRS3:fix/x64-recomp-flagctl-preserve → main · +240 −9 · 2 files · 1 commits · open

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

**@⁠IlyassRS3** on GitHub · 2026-11-24 22:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1379) `IlyassRS3:fix/x64-recomp-flagctl-preserve` → `main` · +240 −9 · 2 files · 1 commits · open 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:49:26 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 22:56 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 22:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1379#pullrequestreview-6048705781) · **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-25 23:15 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 ffcfe68f1 into the branch locally (830 commits behind, no conflicts). Head tested: 5d9101482.

The fix is real, and the bug it fixes on main is a serious one. The new test file has the wiring bug from #1409, so as shipped it cannot show that.

As shipped under QEMU, python -I tests/test_x64_recomp_flagctl_mat.py: 5 run, 14 failures, all of the shape b'R=0000000a\n' != 10. _runner's QEMU branch returns xr.run_kernel (raw bytes) where xr.run_result (parsed value) is needed. Suggested fix: use xr.run_result in both branches.

With RUNNER swapped for xr.run_result in memory (no file changed), the same five tests:

tool under test result under QEMU
this branch 5/5 pass
main (tools/x64_recomp.py from ffcfe68f1, this file's tests) 15 failures

On main, the three-way unsigned compare cmp a, b; jb less; je equal takes the wrong arm in all seven cases the test tries:

a, b want main
3, 5 less greater
5, 5 equal less
9, 5 greater less
0, 1 less greater
7fffffff, 80000000 less greater
80000000, 80000000 equal less
80000000, 7fffffff greater less

The sub; setc and sub; cmc cross-block cases are wrong on main in 6 of the subtests and right here, and the two static clc/stc checks fail on main and pass here.

Counts. tests/test_x64_recomp.py 33/33 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 512 run, 507 pass; the 5 that fail are main's 4 plus the new file, for the wiring reason above.

I ran the pull request's own cases and did not write additional ones for this change. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 23:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1379#issuecomment-7177329781) 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 `ffcfe68f1` into the branch locally (830 commits behind, no conflicts). Head tested: `5d9101482`. **The fix is real, and the bug it fixes on `main` is a serious one. The new test file has the wiring bug from #1409, so as shipped it cannot show that.** As shipped under QEMU, `python -I tests/test_x64_recomp_flagctl_mat.py`: 5 run, 14 failures, all of the shape `b'R=0000000a\n' != 10`. `_runner`'s QEMU branch returns `xr.run_kernel` (raw bytes) where `xr.run_result` (parsed value) is needed. Suggested fix: use `xr.run_result` in both branches. With `RUNNER` swapped for `xr.run_result` in memory (no file changed), the same five tests: | tool under test | result under QEMU | |---|---| | this branch | 5/5 pass | | `main` (`tools/x64_recomp.py` from `ffcfe68f1`, this file's tests) | 15 failures | On `main`, the three-way unsigned compare `cmp a, b; jb less; je equal` takes the wrong arm in all seven cases the test tries: | a, b | want | `main` | |---|---|---| | 3, 5 | less | greater | | 5, 5 | equal | less | | 9, 5 | greater | less | | 0, 1 | less | greater | | `7fffffff`, `80000000` | less | greater | | `80000000`, `80000000` | equal | less | | `80000000`, `7fffffff` | greater | less | The `sub; setc` and `sub; cmc` cross-block cases are wrong on `main` in 6 of the subtests and right here, and the two static `clc`/`stc` checks fail on `main` and pass here. **Counts.** `tests/test_x64_recomp.py` 33/33 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 512 run, 507 pass; the 5 that fail are `main`'s 4 plus the new file, for the wiring reason above. I ran the pull request's own cases and did not write additional ones for this change. 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/1379/head:github-bot/gh-1379'
git switch 'github-bot/gh-1379'

Merge

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