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

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

📋 Pull Request Information

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

Base: main ← Head: fix/x64-recomp-parity-logic-eflags


📝 Commits (1)

  • df07ec2 x64_recomp: clear logic OF & high eflags in rt_mkflags, fix parity/ctx_jcc NZCV & T0 clobber

📊 Changes

2 files changed (+300 additions, -15 deletions)

View changed files

➕ tests/test_x64_recomp_parity_logic_eflags.py (+279 -0)
📝 tools/x64_recomp.py (+21 -15)

📄 Description

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)

🔄 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/1401 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/x64-recomp-parity-logic-eflags` --- ### 📝 Commits (1) - [`df07ec2`](https://github.com/ubiquitarianism/leonida/commit/df07ec2bd0cbf5463a23e2d669ebeaf82bd5bd55) x64_recomp: clear logic OF & high eflags in rt_mkflags, fix parity/ctx_jcc NZCV & T0 clobber ### 📊 Changes **2 files changed** (+300 additions, -15 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_parity_logic_eflags.py` (+279 -0) 📝 `tools/x64_recomp.py` (+21 -15) </details> ### 📄 Description 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)** --- <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#1430
No description provided.