You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
x64_recomp: clear logic OF & high eflags in rt_mkflags, fix parity/ctx_jcc NZCV & T0 clobber (X64-RECOMP-PARITY-LOGIC-EFLAGS) - #1401
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/maincdb003ea1 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)
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.
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.
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).
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Item ID:
X64-RECOMP-PARITY-LOGIC-EFLAGSSummary
Fixes four flag-materialization and condition-testing bugs in
tools/x64_recomp.py:rt_mkflags(kind == 2,LOGIC_KIND) leaked staleOF: ARM64ands/tstleavesNZCV.Vunchanged from prior arithmetic, andrt_mkflagscopiedNZCV.VintoEF_OF(bit 11) even whenkind == 2(and/or/xor/test), corrupting cross-blockjo/jno/jl/jge/jle/jg. Now clearsOF = 0forkind == 2, matchingCF = 0.rt_mkflagswrote only 32 bits ofCTX_EFLAGS:a.str(2, CTX_REG, CTX_EFLAGS, size=4)left stale upper 32 bits written bypopfq(size=8), leaking them into subsequentpushfq. Changed tosize=8._parity_jccignoredCTX_EFLAGSafterpopf/sahf/materialized paths and clobbered ARMNZCV: previously always folded staleCTX_RESand rantst_i(T0, 1), corruptingjp/setp/cmovpafterpopf/cross-block paths AND destroying live ARMNZCVfor any subsequent same-blocksetcc/cmov/jcc. Now readsEF_PF(bit 2) fromCTX_EFLAGSwhenprod is None,prod.mnem in ("popf", "sahf"), orprod.addr in self.mat, and usestbz/tbnz(never modifyingNZCV)._parity_jccand_ctx_jccclobberedT0(holding_emit_cmov's loaded source operand) and_ctx_jcc_regclobberedNZCV: everycmovp/cmovnpand every cross-blockcmovccloadedsrcintoT0, then called_parity_jcc/_ctx_jccwhich overwroteT0withctx.eflagsor foldedctx.resbeforea.mov(g, T0, sf). Switching_parity_jcc/_ctx_jccto scratchT2and usingtbz/tbnzfor single-bit conditions in_ctx_jcc_regpreserves bothT0and live ARMNZCV.Tests & Verification
python3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_parity_logic_eflags.py— OKpython3 -I tests/test_x64_recomp_parity_logic_eflags.py -v— 2 static OK (5 exec skip cleanly without#1174runner)python3 -I tests/test_x64_recomp.py— 6 static OK (26 exec skipped)python3 -I tests/test_text_write_line_endings.py— 3/3 OKtests/test_x64_recomp_parity_logic_eflags.pyon unpatchedupstream/main+#1174): 5 failures (FAILED (failures=5)).upstream/maincdb003ea1with#1174+ all 19 other openx64_recompPR branches):tests/test_x64_recomp_parity_logic_eflags.py: 7/7 OK (0 skipped) on native Apple M5 arm64tests/test_x64_recomp_*.pysuites combined: 145/145 OK (0 skipped)