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

Open
opened 2026-11-26 20:20:35 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1213
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-std-str


📝 Commits (1)

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

📊 Changes

2 files changed (+235 additions, -22 deletions)

View changed files

➕ tests/test_x64_recomp_std_str.py (+184 -0)
📝 tools/x64_recomp.py (+51 -22)

📄 Description

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 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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1213 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-std-str` --- ### 📝 Commits (1) - [`944e675`](https://github.com/ubiquitarianism/leonida/commit/944e675b6683d34e760fc6de7289b91e26c6bca5) x64_recomp: std/cld direction flag, reverse string ops, and lods emitter (X64-RECOMP-STD-STR) ### 📊 Changes **2 files changed** (+235 additions, -22 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_std_str.py` (+184 -0) 📝 `tools/x64_recomp.py` (+51 -22) </details> ### 📄 Description 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 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1247
No description provided.