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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:fix/x64-recomp-parity-logic-eflags
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:fix/x64-recomp-parity-logic-eflags

Conversation

@IlyassRS3

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.

@kevincommits

kevincommits commented Nov 26, 2026 •

Copy link
Copy Markdown

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 x64_recomp: give REX.W precedence over 0x66 operand-size override (X64-RECOMP-REXW-OVER-66) #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 x64_recomp: the QEMU harness maps guest RAM as Normal memory, so unaligned accesses work (X64-RECOMP-QEMU-UNALIGNED) #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; x64_recomp: SSE/SSE2 scalar floating-point and XMM move/bitwise emitters (X64-RECOMP-SSE-SCALAR) #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.

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