Skip to content

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

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
quietfold:fix/x64-recomp-parity-logic-eflags
Nov 28, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
quietfold:fix/x64-recomp-parity-logic-eflags

Conversation

@quietfold

Copy link
Copy Markdown
Contributor

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)

Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Red/green: classified READY by dada-lotF (red on main, green on head merged with main); head df07ec2 unchanged since 2026-11-24, before that run.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957 in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

@MoonEyes
MoonEyes merged commit 1782974 into ProjectLeonida:main Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants