cpu: lower x64 string instructions in the shared lifter #362

Merged
denn0x merged 1 commit from upstream-pr/368 into main 2026-11-22 03:26:15 +00:00
denn0x commented 2026-11-21 23:37:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-21T23:37:41Z.


Change

Advances CPU translation coverage. The x64 string instructions are an independent problem: none of movs, stos, lods, scas, cmps, cld or std lowers today, and no open PR or issue claims them. There is no prerequisite PR. The shared interface is lower() in tools/ffx_version_query.py and CPU_FIXTURES in tools/form_coverage.py, which #287, #289, #291, #326, #328, #331 and #335 also extend. This change adds one elif branch, a separate string_op() helper and a conditional bool direction declaration on its own line. It leaves the zero/carry declaration and the existing branches untouched, so whichever of these lands later needs only a short merge.

Lowering.

  • Dispatch: by iced Code, not mnemonic. The SSE2 scalar movsd and cmpsd share their mnemonics and stay unsupported.
  • Element loop: each iteration loads and stores (or compares) one element, then steps RSI and RDI by the width, downward when DF is set. An overlapping rep movsb with RDI = RSI + 1 therefore replicates the first byte as on the hardware.
  • Repeat semantics: REP, REPE and REPNE test RCX before each element. REPE and REPNE then test ZF after the decrement. A zero count leaves pointers, memory and flags unchanged.
  • Compare order: cmps computes [RSI] − [RDI] and scas computes the accumulator − [RDI]. Like the rest of lower(), only ZF and CF are modelled. Whichever change later adds OF, SF or PF to lower() should extend scas and cmps too.
  • Accumulator widths: lodsb and lodsw merge into RAX; lodsd zero-extends.
  • Direction flag: cld and std set a direction flag that starts clear. The x64 software conventions expect DF clear on function exit and on entry to runtime and system calls.
  • Rejected:
    • 67h forms, which walk ESI, EDI and ECX;
    • REPNE on movs, stos and lods;
    • FS and GS sources, through the existing prefix check.

Fixture

tests/cpu/string_ops.asm builds 62 exports:

  • every width of each instruction bare;
  • movs, stos and lods under REP;
  • scas and cmps under REPE and REPNE;
  • 14 variants under STD, including one with STD then CLD.

Each export takes destination, source, count and a state block. It sets RDI, RSI, RCX and RAX from them and sets the flags from a preset: CF only, ZF only, or neither. It reports ZF, CF, RAX, RSI, RDI and RCX. RSI and RDI are callee-saved and kept in R10 and R11.

tests/cpu/string_ops.py check runs 39984 cases. Each one goes to the native DLL, the translation and a Python reference, which executes one element at a time.

  • Buffer: 1 KiB of a period-16 pattern with planted breaks and a run of 0x5a, between two 1 KiB guards. Memory is compared over the guards too.
  • Overlaps: 1 byte and 3 bytes in both directions, so the dword and qword forms overlap within an element.
  • Counts: 0, 1, 2, 5, 17 and 40, with each case kept only when the reference stays in the buffer.
  • Exit paths: every REPE and REPNE export covers a zero count, an exit on the first element, an exit on a later element and exhaustion of the count, with both CF results.

Negative controls. Three translations of the same image are mutated after lowering, and each must mismatch:

Control Mutation Mismatches
memmove REP movs becomes one whole-range std::memmove 1143/39984
swapped cmps operands swapped 5814/39984
forward std ignored 7551/39984

The check also confirms each rejection by its diagnostic, using synthetic bytes: 67 f3 a4, 67 ae, f2 a4, f2 48 ab, f2 ac, 64 a4, and the SSE2 movsd xmm0, xmm1 and cmpeqsd. On main, lower() rejects the same bytes, for example f3 a4 c3 with unsupported query operation at 0x1000: rep movsb, so emit cannot translate the fixture.

Verification

Tested at a70c9ec on 9bdbb20. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (Zen 4).

ctest --preset dev --no-tests=error -R '^cpu_aot_string_ops$' -V
95: 39984 cases over 62 exports (memory, RAX, RSI, RDI, RCX, ZF/CF) matched the reference in native and translated DLLs
95: control memmove: 1143/39984 mismatches
95: control swapped: 5814/39984 mismatches
95: control forward: 7551/39984 mismatches
95: rejected 67h string addressing, REPNE on MOVS/STOS/LODS, an FS source and the SSE2 MOVSD/CMPSD forms
1/1 Test #95: cpu_aot_string_ops ...............   Passed    0.99 sec
  • Full suite: ctest --preset dev --no-tests=error --output-junit <repo>/out/test-results.xml gives 144 passed, 1 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp, as on main on this machine). The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, which #332 changed on main just before this rebase. This change touches no GPU code. On the RX 9070 XT (driver 32.0.32015.7029, debug layer enabled), the probe prints array_atomics ... expected=64 actual=256,256,256,256, while the fixture's own add/sub and other checks report mismatches=0. I'll follow that up separately. Before the rebase, on bc24dfd, the same suite gave 145 passed.
  • CPU tests: ctest --preset dev --no-tests=error -R '^cpu_' gives 43/43 passed, including the existing cpu_lower_prefixes encodings.
  • Pure-Python checks: tests/form_coverage_check.py and tests/docs_links_check.py pass.
  • Fork CI: the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at a70c9ec in the fork's Windows checks run 44962440518.

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 77 of 948 forms. The same JUnit file without cpu_aot_string_ops gives 55. The 22 added forms are movs, stos, lods, scas and cmps in four widths, plus cld and std. The reference lists movsd and cmpsd once each, under Integer. A future SSE2 fixture that uses scalar movsd would therefore count the same form.

Measured, not established: the fixture checks architectural results on one host CPU. It does not cover RCX wrap at 2^64, faults part-way through a repeated instruction, the restart behaviour after an interrupt, or the flags this lowering does not model (OF, SF, AF, PF).

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the lowering, the fixture and its controls.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535857819 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/368). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-21T23:37:41Z. --- ## Change Advances CPU translation coverage. The x64 string instructions are an independent problem: none of `movs`, `stos`, `lods`, `scas`, `cmps`, `cld` or `std` lowers today, and no open PR or issue claims them. There is no prerequisite PR. The shared interface is `lower()` in `tools/ffx_version_query.py` and `CPU_FIXTURES` in `tools/form_coverage.py`, which #287, #289, #291, #326, #328, #331 and #335 also extend. This change adds one `elif` branch, a separate `string_op()` helper and a conditional `bool direction` declaration on its own line. It leaves the `zero`/`carry` declaration and the existing branches untouched, so whichever of these lands later needs only a short merge. **Lowering.** - **Dispatch:** by iced `Code`, not mnemonic. The SSE2 scalar `movsd` and `cmpsd` share their mnemonics and stay unsupported. - **Element loop:** each iteration loads and stores (or compares) one element, then steps RSI and RDI by the width, downward when DF is set. An overlapping `rep movsb` with RDI = RSI + 1 therefore replicates the first byte as on the hardware. - **Repeat semantics:** REP, REPE and REPNE test RCX before each element. REPE and REPNE then test ZF after the decrement. A zero count leaves pointers, memory and flags unchanged. - **Compare order:** `cmps` computes [RSI] − [RDI] and `scas` computes the accumulator − [RDI]. Like the rest of `lower()`, only ZF and CF are modelled. Whichever change later adds OF, SF or PF to `lower()` should extend `scas` and `cmps` too. - **Accumulator widths:** `lodsb` and `lodsw` merge into RAX; `lodsd` zero-extends. - **Direction flag:** `cld` and `std` set a `direction` flag that starts clear. The [x64 software conventions](https://learn.microsoft.com/en-us/cpp/build/x64-software-conventions#x64-register-usage) expect DF clear on function exit and on entry to runtime and system calls. - **Rejected:** - 67h forms, which walk ESI, EDI and ECX; - REPNE on `movs`, `stos` and `lods`; - FS and GS sources, through the existing prefix check. ## Fixture `tests/cpu/string_ops.asm` builds 62 exports: - every width of each instruction bare; - `movs`, `stos` and `lods` under REP; - `scas` and `cmps` under REPE and REPNE; - 14 variants under STD, including one with STD then CLD. Each export takes destination, source, count and a state block. It sets RDI, RSI, RCX and RAX from them and sets the flags from a preset: CF only, ZF only, or neither. It reports ZF, CF, RAX, RSI, RDI and RCX. RSI and RDI are callee-saved and kept in R10 and R11. `tests/cpu/string_ops.py check` runs 39984 cases. Each one goes to the native DLL, the translation and a Python reference, which executes one element at a time. - **Buffer:** 1 KiB of a period-16 pattern with planted breaks and a run of `0x5a`, between two 1 KiB guards. Memory is compared over the guards too. - **Overlaps:** 1 byte and 3 bytes in both directions, so the dword and qword forms overlap within an element. - **Counts:** 0, 1, 2, 5, 17 and 40, with each case kept only when the reference stays in the buffer. - **Exit paths:** every REPE and REPNE export covers a zero count, an exit on the first element, an exit on a later element and exhaustion of the count, with both CF results. **Negative controls.** Three translations of the same image are mutated after lowering, and each must mismatch: | Control | Mutation | Mismatches | |---|---|---| | `memmove` | REP `movs` becomes one whole-range `std::memmove` | 1143/39984 | | `swapped` | `cmps` operands swapped | 5814/39984 | | `forward` | `std` ignored | 7551/39984 | The check also confirms each rejection by its diagnostic, using synthetic bytes: `67 f3 a4`, `67 ae`, `f2 a4`, `f2 48 ab`, `f2 ac`, `64 a4`, and the SSE2 `movsd xmm0, xmm1` and `cmpeqsd`. On `main`, `lower()` rejects the same bytes, for example `f3 a4 c3` with `unsupported query operation at 0x1000: rep movsb`, so `emit` cannot translate the fixture. ## Verification Tested at `a70c9ec` on `9bdbb20`. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (Zen 4). ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_string_ops$' -V ``` ```text 95: 39984 cases over 62 exports (memory, RAX, RSI, RDI, RCX, ZF/CF) matched the reference in native and translated DLLs 95: control memmove: 1143/39984 mismatches 95: control swapped: 5814/39984 mismatches 95: control forward: 7551/39984 mismatches 95: rejected 67h string addressing, REPNE on MOVS/STOS/LODS, an FS source and the SSE2 MOVSD/CMPSD forms 1/1 Test #95: cpu_aot_string_ops ............... Passed 0.99 sec ``` - **Full suite:** `ctest --preset dev --no-tests=error --output-junit <repo>/out/test-results.xml` gives 144 passed, 1 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`, as on `main` on this machine). The run includes the GPU tests. The failure is `gpu_alias_lookup_writes_hardware`, which #332 changed on `main` just before this rebase. This change touches no GPU code. On the RX 9070 XT (driver 32.0.32015.7029, debug layer enabled), the probe prints `array_atomics ... expected=64 actual=256,256,256,256`, while the fixture's own add/sub and other checks report `mismatches=0`. I'll follow that up separately. Before the rebase, on `bc24dfd`, the same suite gave 145 passed. - **CPU tests:** `ctest --preset dev --no-tests=error -R '^cpu_'` gives 43/43 passed, including the existing `cpu_lower_prefixes` encodings. - **Pure-Python checks:** `tests/form_coverage_check.py` and `tests/docs_links_check.py` pass. - **Fork CI:** the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at `a70c9ec` in the fork's `Windows checks` run [44962440518](https://github.com/denn0x/Xbox2Windows/actions/runs/44962440518). **Coverage.** `python tools/form_coverage.py out/test-results.xml` gives x64 **77 of 948** forms. The same JUnit file without `cpu_aot_string_ops` gives 55. The 22 added forms are `movs`, `stos`, `lods`, `scas` and `cmps` in four widths, plus `cld` and `std`. The reference lists `movsd` and `cmpsd` once each, under Integer. A future SSE2 fixture that uses scalar `movsd` would therefore count the same form. **Measured, not established:** the fixture checks architectural results on one host CPU. It does not cover RCX wrap at 2^64, faults part-way through a repeated instruction, the restart behaviour after an interrupt, or the flags this lowering does not model (OF, SF, AF, PF). ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the lowering, the fixture and its controls. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:42:40 +00:00
denn0x commented 2026-11-24 06:48:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T02:08:00Z.


Rebased onto c2fc84c as 791dbcb, resolving the lower() signature conflict with #422 that I mentioned there. string_op() stays above lower(), and the signature keeps x87=False. The other files merged unchanged. ctest --preset dev --no-tests=error -R '^cpu_' -j 8 gives 46 of 46 passed, including cpu_aot_string_ops and both x87 tests.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072696605 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/368#issuecomment-7072696605). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T02:08:00Z. --- Rebased onto `c2fc84c` as `791dbcb`, resolving the `lower()` signature conflict with #422 that I mentioned there. `string_op()` stays above `lower()`, and the signature keeps `x87=False`. The other files merged unchanged. `ctest --preset dev --no-tests=error -R '^cpu_' -j 8` gives 46 of 46 passed, including `cpu_aot_string_ops` and both x87 tests. _AI-assisted (Claude Code)._
denn0x commented 2026-11-24 06:48:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T02:49:11Z.


Rebased again onto 1606f7b as 5b2fd02. The conflict was additive: #291's rotates_shifts fixture now sits at the same spot in CMakeLists.txt and CPU_FIXTURES, and both entries are kept. ctest --preset dev --no-tests=error -R '^cpu_' -j 8 gives 50 of 50 passed, including cpu_aot_string_ops and cpu_aot_rotates_shifts.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073145615 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/368#issuecomment-7073145615). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T02:49:11Z. --- Rebased again onto `1606f7b` as `5b2fd02`. The conflict was additive: #291's `rotates_shifts` fixture now sits at the same spot in `CMakeLists.txt` and `CPU_FIXTURES`, and both entries are kept. `ctest --preset dev --no-tests=error -R '^cpu_' -j 8` gives 50 of 50 passed, including `cpu_aot_string_ops` and `cpu_aot_rotates_shifts`. _AI-assisted (Claude Code)._
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
R0Y00/Xbox2Windows!362
No description provided.