x64_recomp: clear logic OF & high eflags in rt_mkflags, fix parity/ctx_jcc NZCV & T0 clobber (X64-RECOMP-PARITY-LOGIC-EFLAGS) #1401

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

@⁠IlyassRS3 on GitHub · 2026-11-24 23:29 UTC · original
IlyassRS3:fix/x64-recomp-parity-logic-eflags → main · +300 −15 · 2 files · 1 commits · open

Item ID: X64-RECOMP-PARITY-LOGIC-EFLAGS

Summary

Fixes four flag-materialization and condition-testing bugs in tools/x64_recomp.py:

  • rt_mkflags (kind == 2, LOGIC_KIND) leaked stale OF: ARM64 ands/tst leaves NZCV.V unchanged from prior arithmetic, and rt_mkflags copied NZCV.V into EF_OF (bit 11) even when kind == 2 (and/or/xor/test), corrupting cross-block jo/jno/jl/jge/jle/jg. Now clears OF = 0 for kind == 2, matching CF = 0.
  • rt_mkflags wrote only 32 bits of CTX_EFLAGS: a.str(2, CTX_REG, CTX_EFLAGS, size=4) left stale upper 32 bits written by popfq (size=8), leaking them into subsequent pushfq. Changed to size=8.
  • _parity_jcc ignored CTX_EFLAGS after popf/sahf/materialized paths and clobbered ARM NZCV: previously always folded stale CTX_RES and ran tst_i(T0, 1), corrupting jp/setp/cmovp after popf/cross-block paths AND destroying live ARM NZCV for any subsequent same-block setcc/cmov/jcc. Now reads EF_PF (bit 2) from CTX_EFLAGS when prod is None, prod.mnem in ("popf", "sahf"), or prod.addr in self.mat, and uses tbz/tbnz (never modifying NZCV).
  • _parity_jcc and _ctx_jcc clobbered T0 (holding _emit_cmov's loaded source operand) and _ctx_jcc_reg clobbered NZCV: every cmovp/cmovnp and every cross-block cmovcc loaded src into T0, then called _parity_jcc/_ctx_jcc which overwrote T0 with ctx.eflags or folded ctx.res before a.mov(g, T0, sf). Switching _parity_jcc/_ctx_jcc to scratch T2 and using tbz/tbnz for single-bit conditions in _ctx_jcc_reg preserves both T0 and live ARM NZCV.

Tests & Verification

  • AI-assisted: yes
  • Host / hardware: macOS (Apple M5, arm64)
  • Standalone checks:
    • 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_parity_logic_eflags.py — OK
    • python3 -I tests/test_x64_recomp_parity_logic_eflags.py -v — 2 static OK (5 exec skip cleanly without #1174 runner)
    • python3 -I tests/test_x64_recomp.py — 6 static OK (26 exec skipped)
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Live Apple M5 mutation proof (tests/test_x64_recomp_parity_logic_eflags.py on unpatched upstream/main + #1174): 5 failures (FAILED (failures=5)).
  • Native Apple M5 execution (21-way scratch merge on upstream/main cdb003ea1 with #1174 + all 19 other open x64_recomp PR branches):
    • 0 merge conflicts across all 21 branches
    • tests/test_x64_recomp_parity_logic_eflags.py: 7/7 OK (0 skipped) on native Apple M5 arm64
    • All 20 tests/test_x64_recomp_*.py suites combined: 145/145 OK (0 skipped)
**@⁠IlyassRS3** on GitHub · 2026-11-24 23:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1401) `IlyassRS3:fix/x64-recomp-parity-logic-eflags` → `main` · +300 −15 · 2 files · 1 commits · open Item ID: `X64-RECOMP-PARITY-LOGIC-EFLAGS` ## Summary Fixes four flag-materialization and condition-testing bugs in `tools/x64_recomp.py`: - **`rt_mkflags` (`kind == 2`, `LOGIC_KIND`) leaked stale `OF`**: ARM64 `ands`/`tst` leaves `NZCV.V` unchanged from prior arithmetic, and `rt_mkflags` copied `NZCV.V` into `EF_OF` (bit 11) even when `kind == 2` (`and`/`or`/`xor`/`test`), corrupting cross-block `jo`/`jno`/`jl`/`jge`/`jle`/`jg`. Now clears `OF = 0` for `kind == 2`, matching `CF = 0`. - **`rt_mkflags` wrote only 32 bits of `CTX_EFLAGS`**: `a.str(2, CTX_REG, CTX_EFLAGS, size=4)` left stale upper 32 bits written by `popfq` (`size=8`), leaking them into subsequent `pushfq`. Changed to `size=8`. - **`_parity_jcc` ignored `CTX_EFLAGS` after `popf`/`sahf`/materialized paths and clobbered ARM `NZCV`**: previously always folded stale `CTX_RES` and ran `tst_i(T0, 1)`, corrupting `jp`/`setp`/`cmovp` after `popf`/cross-block paths AND destroying live ARM `NZCV` for any subsequent same-block `setcc`/`cmov`/`jcc`. Now reads `EF_PF` (bit 2) from `CTX_EFLAGS` when `prod is None`, `prod.mnem in ("popf", "sahf")`, or `prod.addr in self.mat`, and uses `tbz`/`tbnz` (never modifying `NZCV`). - **`_parity_jcc` and `_ctx_jcc` clobbered `T0` (holding `_emit_cmov`'s loaded source operand) and `_ctx_jcc_reg` clobbered `NZCV`**: every `cmovp`/`cmovnp` and every cross-block `cmovcc` loaded `src` into `T0`, then called `_parity_jcc`/`_ctx_jcc` which overwrote `T0` with `ctx.eflags` or folded `ctx.res` before `a.mov(g, T0, sf)`. Switching `_parity_jcc`/`_ctx_jcc` to scratch `T2` and using `tbz`/`tbnz` for single-bit conditions in `_ctx_jcc_reg` preserves both `T0` and live ARM `NZCV`. ## Tests & Verification - **AI-assisted**: yes - **Host / hardware**: macOS (Apple M5, arm64) - Standalone checks: - `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_parity_logic_eflags.py` — OK - `python3 -I tests/test_x64_recomp_parity_logic_eflags.py -v` — 2 static OK (5 exec skip cleanly without `#1174` runner) - `python3 -I tests/test_x64_recomp.py` — 6 static OK (26 exec skipped) - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Live Apple M5 mutation proof** (`tests/test_x64_recomp_parity_logic_eflags.py` on unpatched `upstream/main` + `#1174`): **5 failures** (`FAILED (failures=5)`). - **Native Apple M5 execution (21-way scratch merge on `upstream/main` `cdb003ea1` with `#1174` + all 19 other open `x64_recomp` PR branches)**: - 0 merge conflicts across all 21 branches - `tests/test_x64_recomp_parity_logic_eflags.py`: **7/7 OK (0 skipped)** on native Apple M5 arm64 - All 20 `tests/test_x64_recomp_*.py` suites combined: **145/145 OK (0 skipped)**
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:08 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 23:29 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 23:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1401#pullrequestreview-6049069751) · **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 04: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 a73a57e13 into the branch locally (902 commits behind, no conflicts). Head tested: df07ec2bd.

Against a reference model main gets 70 of 212 condition-code checks wrong. This branch fixes 62 of them; the 8 it leaves are a separate bug that #1202 already fixes.

  • Test file. As shipped under QEMU, python -I tests/test_x64_recomp_parity_logic_eflags.py is 7 run, 5 failures, from the wiring bug in #1409 (xr.run_kernel where xr.run_result is needed). With RUNNER swapped for xr.run_result in memory (no file changed): 7/7.

  • Beyond the file, generated cases under QEMU against a small Python model:

    • six producers (cmp, test, add, sub, and, xor) by seven operand pairs, each in the same block and after a jmp +0, preceded by an overflowing add so a stale OF would show. Then setp, sete, sets, setb, setl, seto, setnp, setge in that order, parity first, so anything the parity check clobbers shows in the ones after it (84 checks);
    • cmovcc edi, edx for all 16 conditions after cmp on four operand pairs, same block and after a jmp +0 (128 checks).
    tool checks wrong what is wrong
    this branch (measured with #2143 merged in locally) 212 8 cmovbe/cmova after a block boundary, 4 pairs each (timeout)
    main 212 70 same-block flags after a setp: cmp 7, sub 7, add 7, xor 5, and 2, test 2; cmovcc across a block boundary: 28 (plus the same 8 be/a); cmovcc in the same block: 4
  • The 8 left over are not this PR. On main, _ctx_jcc_reg handles be/a with tst_i(w, EF_CF | EF_ZF); 0x41 is not an arm64 bitmask immediate, so the translator records setcc@…: 0x41 not a bitmask imm as unhandled and the code traps at run time. That affects every jbe/ja/setbe/seta/cmovbe/cmova that reads flags from ctx.eflags. This branch keeps that tst_i; #1202 replaces it with movz + tst_r. The two PRs touch neighbouring lines of _ctx_jcc_reg, so whichever lands second needs a small rebase.

  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the wiring reason.

Not tested: sahf as a producer, AF, the rt_mkflags upper-32-bit clear beyond the file's own case, and 8/16/64-bit producers. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 04:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1401#issuecomment-7182440937) 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 `a73a57e13` into the branch locally (902 commits behind, no conflicts). Head tested: `df07ec2bd`. **Against a reference model `main` gets 70 of 212 condition-code checks wrong. This branch fixes 62 of them; the 8 it leaves are a separate bug that #1202 already fixes.** - **Test file.** As shipped under QEMU, `python -I tests/test_x64_recomp_parity_logic_eflags.py` is 7 run, 5 failures, from the wiring bug in #1409 (`xr.run_kernel` where `xr.run_result` is needed). With `RUNNER` swapped for `xr.run_result` in memory (no file changed): **7/7**. - Beyond the file, generated cases under QEMU against a small Python model: - six producers (`cmp`, `test`, `add`, `sub`, `and`, `xor`) by seven operand pairs, each in the same block and after a `jmp +0`, preceded by an overflowing `add` so a stale OF would show. Then `setp, sete, sets, setb, setl, seto, setnp, setge` in that order, parity first, so anything the parity check clobbers shows in the ones after it (84 checks); - `cmovcc edi, edx` for all 16 conditions after `cmp` on four operand pairs, same block and after a `jmp +0` (128 checks). | tool | checks | wrong | what is wrong | |---|---|---|---| | this branch (measured with #2143 merged in locally) | 212 | 8 | `cmovbe`/`cmova` after a block boundary, 4 pairs each (timeout) | | `main` | 212 | 70 | same-block flags after a `setp`: `cmp` 7, `sub` 7, `add` 7, `xor` 5, `and` 2, `test` 2; `cmovcc` across a block boundary: 28 (plus the same 8 `be`/`a`); `cmovcc` in the same block: 4 | - **The 8 left over are not this PR.** On `main`, `_ctx_jcc_reg` handles `be`/`a` with `tst_i(w, EF_CF | EF_ZF)`; `0x41` is not an arm64 bitmask immediate, so the translator records `setcc@…: 0x41 not a bitmask imm` as unhandled and the code traps at run time. That affects every `jbe`/`ja`/`setbe`/`seta`/`cmovbe`/`cmova` that reads flags from `ctx.eflags`. This branch keeps that `tst_i`; #1202 replaces it with `movz` + `tst_r`. The two PRs touch neighbouring lines of `_ctx_jcc_reg`, so whichever lands second needs a small rebase. - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the wiring reason. Not tested: `sahf` as a producer, AF, the `rt_mkflags` upper-32-bit clear beyond the file's own case, and 8/16/64-bit producers. 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/1401/head:github-bot/gh-1401'
git switch 'github-bot/gh-1401'

Merge

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