x64_recomp: std/cld direction flag, reverse string ops, and lods emitter (X64-RECOMP-STD-STR) #1213

Open
github-bot wants to merge 1 commit from github-bot/gh-1213 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 20:24 UTC · original
IlyassRS3:feat/x64-recomp-std-str → main · +235 −22 · 2 files · 1 commits · open

Claimed in the coordination issue as: X64-RECOMP-STD-STR (#14)

What and why

Completes the std (DF=1) gap listed in Next Step #2 of docs/mobile.md (along with the missing lods emitter and cross-block scas/cmps flag materialization) in tools/x64_recomp.py (independent of #1174, #1202, #1205, and #1211, and 3-way-merges cleanly with all open x64_recomp PRs):

  • Direction flag (CTX_DF = 0x20): cld clears ctx.df and std sets ctx.df = 1 without touching NZCV; pushf includes bit 10 (DF) and popf restores ctx.df from bit 10.
  • NZCV-preserving step calculation (_str_step): computes +sz (when DF=0) or -sz (when DF=1) via sz - (df << sz.bit_length()) in 3 instructions without modifying NZCV, so live flags survive across std, cld, movs, stos, lods, and zero-count rep scas/cmps.
  • Reverse-direction movs/stos/scas/cmps and new lods: supports lodsb/lodsw/lodsd/lodsq (preserving upper bytes of rax for 8/16-bit and zero-extending for 32-bit) and overlapping backward rep movs, backward rep stos, backward repne scas, and backward repe cmps.
  • Materialized flags for scas/cmps: stashes the unshifted difference (_stash_res) and calls _maybe_mkflags when at least one iteration executes, so cross-block jcc/setcc after scas/cmps sees up-to-date CTX_EFLAGS while rcx == 0 stays a no-op.
  • Tests (tests/test_x64_recomp_std_str.py): verifies translation plus 4 end-to-end execution suites covering overlapping backward rep movsb, backward rep stosd + forward/backward lodsb/lodsd, backward repne scasb / repe cmpsb with cross-block flags, std; pushf; cld; popf DF round-trip, and NZCV preservation.

Tested

  • Standalone on 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.
  • Native arm64 hardware execution on Apple M5 / macOS 27.2.1 (Python 3.14, cleanly 5-way 3-way-merged with #1174, #1202, #1205, and #1211 in a scratch clone): tests/test_x64_recomp_std_str.py 5/5 passed, tests/test_x64_recomp_rcl_rcr.py 5/5 passed, tests/test_x64_recomp_bitops.py 9/9 passed, tests/test_x64_recomp_sse.py 16/16 passed, tests/test_x64_recomp.py 46/46 passed (81/81 total).
  • Verified zero conflicts (git apply --check) against open x64_recomp PRs #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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠IlyassRS3** on GitHub · 2026-11-24 20:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1213) `IlyassRS3:feat/x64-recomp-std-str` → `main` · +235 −22 · 2 files · 1 commits · open Claimed in the coordination issue as: X64-RECOMP-STD-STR (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Completes the `std` (`DF=1`) gap listed in Next Step #2 of `docs/mobile.md` (along with the missing `lods` emitter and cross-block `scas`/`cmps` flag materialization) in `tools/x64_recomp.py` (independent of `#1174`, `#1202`, `#1205`, and `#1211`, and 3-way-merges cleanly with all open `x64_recomp` PRs): - **Direction flag (`CTX_DF = 0x20`)**: `cld` clears `ctx.df` and `std` sets `ctx.df = 1` without touching `NZCV`; `pushf` includes bit 10 (`DF`) and `popf` restores `ctx.df` from bit 10. - **NZCV-preserving step calculation (`_str_step`)**: computes `+sz` (when `DF=0`) or `-sz` (when `DF=1`) via `sz - (df << sz.bit_length())` in 3 instructions without modifying `NZCV`, so live flags survive across `std`, `cld`, `movs`, `stos`, `lods`, and zero-count `rep scas`/`cmps`. - **Reverse-direction `movs`/`stos`/`scas`/`cmps` and new `lods`**: supports `lodsb`/`lodsw`/`lodsd`/`lodsq` (preserving upper bytes of `rax` for 8/16-bit and zero-extending for 32-bit) and overlapping backward `rep movs`, backward `rep stos`, backward `repne scas`, and backward `repe cmps`. - **Materialized flags for `scas`/`cmps`**: stashes the unshifted difference (`_stash_res`) and calls `_maybe_mkflags` when at least one iteration executes, so cross-block `jcc`/`setcc` after `scas`/`cmps` sees up-to-date `CTX_EFLAGS` while `rcx == 0` stays a no-op. - **Tests (`tests/test_x64_recomp_std_str.py`)**: verifies translation plus 4 end-to-end execution suites covering overlapping backward `rep movsb`, backward `rep stosd` + forward/backward `lodsb`/`lodsd`, backward `repne scasb` / `repe cmpsb` with cross-block flags, `std; pushf; cld; popf` DF round-trip, and `NZCV` preservation. ### Tested - Standalone on `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. - Native arm64 hardware execution on Apple M5 / macOS 27.2.1 (Python 3.14, cleanly 5-way 3-way-merged with `#1174`, `#1202`, `#1205`, and `#1211` in a scratch clone): `tests/test_x64_recomp_std_str.py` **5/5 passed**, `tests/test_x64_recomp_rcl_rcr.py` **5/5 passed**, `tests/test_x64_recomp_bitops.py` **9/9 passed**, `tests/test_x64_recomp_sse.py` **16/16 passed**, `tests/test_x64_recomp.py` **46/46 passed** (81/81 total). - Verified zero conflicts (`git apply --check`) against open `x64_recomp` PRs `#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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:43:05 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 20:24 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 20:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1213#pullrequestreview-6046794587) · **reviewed** 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.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-25 22:58 UTC · original

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 21d3662ad into 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.py under QEMU: 5 run, 4 pass, 1 error. test_std_overlapping_backward_rep_movsb hits the 15 s run_kernel timeout.

The hang is its last guest instruction, 8b 7b 02 (mov edi, [rbx+2]), an unaligned 4-byte read. On plain main, with no string instruction involved, every unaligned access hangs the QEMU harness here:

guest code after mov rbx, scratch main this branch
mov edi, [rbx+0], mov edi, [rbx+4] right value right value
mov edi, [rbx+2], mov edi, [rbx+1] timeout timeout
movzx edi, word [rbx+1], mov rdi, [rbx+1] timeout timeout
mov [rbx+2], eax timeout timeout

My 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:

check after std; rep movsb ×4, ABCD at s+0, rsi=s+3, rdi=s+5 want got
dword at s+0 42414241 42414241
dword at s+4 00004443 00004443
rsi − s, rdi − s −1, 1 ffffffff, 00000001

Also right under QEMU: std; movsb (single), std; rep stosb ×4, std; lodsb ×2 with rsi stepping down, forward rep movsb still forward (and still corrupting an overlapping copy, as x86 does), std; pushfq shows bit 10 set, and push 0x402; popfq; pushfq round-trips DF. On main, std is unhandled, so none of these ran before.

Counts. tests/test_x64_recomp.py 33/33 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 512 run, 507 pass; the 5 that fail are main's 4 plus this file's one timeout.

I did not test repe cmps / repne scas backward, or 16- and 64-bit lods. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 22:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1213#issuecomment-7176995159) 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 `21d3662ad` into 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.py` under QEMU: 5 run, 4 pass, 1 error. `test_std_overlapping_backward_rep_movsb` hits the 15 s `run_kernel` timeout. The hang is its last guest instruction, `8b 7b 02` (`mov edi, [rbx+2]`), an unaligned 4-byte read. On plain `main`, with no string instruction involved, every unaligned access hangs the QEMU harness here: | guest code after `mov rbx, scratch` | `main` | this branch | |---|---|---| | `mov edi, [rbx+0]`, `mov edi, [rbx+4]` | right value | right value | | `mov edi, [rbx+2]`, `mov edi, [rbx+1]` | timeout | timeout | | `movzx edi, word [rbx+1]`, `mov rdi, [rbx+1]` | timeout | timeout | | `mov [rbx+2], eax` | timeout | timeout | My 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:** | check after `std; rep movsb` ×4, `ABCD` at s+0, rsi=s+3, rdi=s+5 | want | got | |---|---|---| | dword at s+0 | `42414241` | `42414241` | | dword at s+4 | `00004443` | `00004443` | | rsi − s, rdi − s | −1, 1 | `ffffffff`, `00000001` | Also right under QEMU: `std; movsb` (single), `std; rep stosb` ×4, `std; lodsb` ×2 with rsi stepping down, forward `rep movsb` still forward (and still corrupting an overlapping copy, as x86 does), `std; pushfq` shows bit 10 set, and `push 0x402; popfq; pushfq` round-trips DF. On `main`, `std` is unhandled, so none of these ran before. **Counts.** `tests/test_x64_recomp.py` 33/33 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 512 run, 507 pass; the 5 that fail are `main`'s 4 plus this file's one timeout. I did not test `repe cmps` / `repne scas` backward, or 16- and 64-bit `lods`. No game files used. AI-assisted.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-25 23:06 UTC · original

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.py is 5/5 under QEMU 11.1.3, including test_std_overlapping_backward_rep_movsb, and tests/test_x64_recomp.py is 34/34. Nothing in this pull request needs to change for it. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 23:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1213#issuecomment-7177145961) 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.py` is 5/5 under QEMU 11.1.3, including `test_std_overlapping_backward_rep_movsb`, and `tests/test_x64_recomp.py` is 34/34. Nothing in this pull request needs to change for it. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1213/head:github-bot/gh-1213'
git switch 'github-bot/gh-1213'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1213'
git switch 'github-bot/gh-1213'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1213'
git switch 'github-bot/gh-1213'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1213'
git switch 'main'
git merge --squash 'github-bot/gh-1213'
git switch 'main'
git merge --ff-only 'github-bot/gh-1213'
git switch 'main'
git merge 'github-bot/gh-1213'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.