x64_recomp: std/cld direction flag, reverse string ops, and lods emitter (X64-RECOMP-STD-STR) #1213
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1213"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T20:24:34Z.
Claimed in the coordination issue as: X64-RECOMP-STD-STR (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143502030)
What and why
Completes the
std(DF=1) gap listed in Next Step #2 ofdocs/mobile.md(along with the missinglodsemitter and cross-blockscas/cmpsflag materialization) intools/x64_recomp.py(independent of#1174,#1202,#1205, and#1211, and 3-way-merges cleanly with all openx64_recompPRs):CTX_DF = 0x20):cldclearsctx.dfandstdsetsctx.df = 1without touchingNZCV;pushfincludes bit 10 (DF) andpopfrestoresctx.dffrom bit 10._str_step): computes+sz(whenDF=0) or-sz(whenDF=1) viasz - (df << sz.bit_length())in 3 instructions without modifyingNZCV, so live flags survive acrossstd,cld,movs,stos,lods, and zero-countrep scas/cmps.movs/stos/scas/cmpsand newlods: supportslodsb/lodsw/lodsd/lodsq(preserving upper bytes ofraxfor 8/16-bit and zero-extending for 32-bit) and overlapping backwardrep movs, backwardrep stos, backwardrepne scas, and backwardrepe cmps.scas/cmps: stashes the unshifted difference (_stash_res) and calls_maybe_mkflagswhen at least one iteration executes, so cross-blockjcc/setccafterscas/cmpssees up-to-dateCTX_EFLAGSwhilercx == 0stays a no-op.tests/test_x64_recomp_std_str.py): verifies translation plus 4 end-to-end execution suites covering overlapping backwardrep movsb, backwardrep stosd+ forward/backwardlodsb/lodsd, backwardrepne scasb/repe cmpsbwith cross-block flags,std; pushf; cld; popfDF round-trip, andNZCVpreservation.Tested
main:tests/test_x64_recomp_std_str.py,tests/test_ci_registration.py,tests/test_text_write_line_endings.py,compileall,make_game.py --help,check_no_game_data.py: all passed.#1174,#1202,#1205, and#1211in a scratch clone):tests/test_x64_recomp_std_str.py5/5 passed,tests/test_x64_recomp_rcl_rcr.py5/5 passed,tests/test_x64_recomp_bitops.py9/9 passed,tests/test_x64_recomp_sse.py16/16 passed,tests/test_x64_recomp.py46/46 passed (81/81 total).git apply --check) against openx64_recompPRs#847,#950,#1120,#1121, and clean 3-way merge with#1174,#1202,#1205, and#1211.GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T20:24:47Z.
Review: COMMENTED
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.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T22:58:12Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat21d3662adinto the branch locally (911 commits behind, no conflicts). Head tested:944e675b6.Short version: the emitter is right in everything I ran. One of its tests cannot pass under this QEMU, for a reason that is on
main, not in this pull request.python -I tests/test_x64_recomp_std_str.pyunder QEMU: 5 run, 4 pass, 1 error.test_std_overlapping_backward_rep_movsbhits the 15 srun_kerneltimeout.The hang is its last guest instruction,
8b 7b 02(mov edi, [rbx+2]), an unaligned 4-byte read. On plainmain, with no string instruction involved, every unaligned access hangs the QEMU harness here:mov rbx, scratchmainmov edi, [rbx+0],mov edi, [rbx+4]mov edi, [rbx+2],mov edi, [rbx+1]movzx edi, word [rbx+1],mov rdi, [rbx+1]mov [rbx+2], eaxMy reading of the cause, which I have not proven:
build_test_kernel's boot stub never turns the MMU on, so data accesses are Device memory, where an unaligned access is an alignment fault, and this QEMU version raises it; there is no vector table, so the guest spins. It would explain why this passes on the native Mach-O runner and on older QEMU. I will raise it separately; it is not for this pull request to fix. A test that wants to pass on QEMU today can read aligned dwords.The same copy, read back aligned, is correct on this branch under QEMU:
std; rep movsb×4,ABCDat s+0, rsi=s+3, rdi=s+542414241424142410000444300004443ffffffff,00000001Also right under QEMU:
std; movsb(single),std; rep stosb×4,std; lodsb×2 with rsi stepping down, forwardrep movsbstill forward (and still corrupting an overlapping copy, as x86 does),std; pushfqshows bit 10 set, andpush 0x402; popfq; pushfqround-trips DF. Onmain,stdis unhandled, so none of these ran before.Counts.
tests/test_x64_recomp.py33/33 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 512 run, 507 pass; the 5 that fail aremain's 4 plus this file's one timeout.I did not test
repe cmps/repne scasbackward, or 16- and 64-bitlods. No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T23:06:02Z.
Follow-up to my comment above: the harness hang is now #2143 (X64-RECOMP-QEMU-UNALIGNED), a boot-stub change that turns the MMU on. With #2143 merged into this branch locally (no conflicts),
python -I tests/test_x64_recomp_std_str.pyis 5/5 under QEMU 11.1.3, includingtest_std_overlapping_backward_rep_movsb, andtests/test_x64_recomp.pyis 34/34. Nothing in this pull request needs to change for it. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:19:13Z.
#1213 | head
944e675b66(merged cleanly with mainadfb3821a2) |python -I tests/test_x64_recomp_std_str.py -v+ 29-form bare-metal kernel &_str_stepARM64 encoding probe +python -I tests/test_x64_recomp.py+python -I tests/test_recomp_census.py| red on main:FAILED (failures=1, skipped=4)(stdandlodsraisedUnhandledInsnintest_std_cld_lods_and_rep_strings_translate_cleanly) | green on PR+main:5 ran, OK (skipped=4)(test_x64_recomp_std_str.py; QEMU/Mach-O exec tests skipped on Windows x64 withoutqemu-system-aarch64),29/298/16/32/64-bitlods/stos/movs/scas/cmps+std/cld+pushf/popfbare-metal kernel translation probe and all 4_str_stepshift encodings (SUB X2, X2, X3, LSL #1..4) verified,45 ran, OK (skipped=32)(test_x64_recomp.py), and11 ran, OK(test_recomp_census.py) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yesAudited
944e675b66(tools/x64_recomp.pyandtests/test_x64_recomp_std_str.py) merged cleanly ontomain(adfb3821a2) on Windows 11. Confirmed_str_stepcomputes+sz/-szwithout touchingNZCV,pushf/popfround-tripDF(bit 10) atCTX_DF(+0x20),_emit_scas_cmpsnow stashesCTX_RESand materializes cross-block flags via_maybe_mkflags(fixing missingPF/CTX_EFLAGSupdates onmain), and a combined 3-way merge of#1213+#1306+#1329ontomain(adfb3821a2) merges cleanly with zero conflicts and passes all fivex64_recomp/recomp_censustest suites. Also verifiedscripts/check_no_game_data.py.Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T00:44:34Z.
Independent run of #1213 (python -I, no build, no game; GPU not used).
944e675b66829530dbd0(PR merged into it locally withgit merge --no-ff, clean,tools/x64_recomp.pyand the new test only)qemu-system-aarch64on this machine and not Apple Silicon, so the QEMU/native execution tests are skipped (4 of 5 in the new file).Commands (one process each, from the repo root):
python -I tests/test_x64_recomp_std_str.py, the 16 othertests/test_x64_recomp*.py/test_recomp_census.pyfiles that exist on main, plustest_ci_registration.py,test_text_write_line_endings.py,test_tool_stdout_encoding.py,test_check_no_game_data.py.tools/x64_recomp.pyreverted to main and the new test kept: FAILED (failures=1, skipped=4). The one failing test is the translation test,std,cldandlodsare reported as unhandled instructions. This is a new-feature red (instructions missing on main). The 4 skipped tests are the ones that execute the result; their behaviour was not exercised here.test_x64_recomp.py32 run OK (skipped=26) andtest_recomp_census.py7 OK (the head branch is based on an older main, the other x64 test files do not exist there).test_x64_recomp.py45 run, OK, skipped=32, both merged and on main).test_tool_stdout_encoding.pyfails identically on plain main and merged (pre-existing, 4 programs outside this PR).Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T00:54:14Z.
Merged after enlight3d's red/green check (check) (RTX 3090, driver 621.40, Windows 11) and a local re-check here. Merged after local re-verification: this PR merged onto
main5d7ac1ee5without conflict, and its test file(s) (tests/test_x64_recomp_std_str.py) pass there. Earlier today the same tests failed onmainwithout the PR (red/green) and a bundle with other open PRs showed no new failures.check_no_game_data.pyis clean. Windows 11, Python 3.14; no GPU needed for these tests.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.