Skip to content

cpu: lower x64 string instructions in the shared lifter - #368

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-string-instructions
Nov 22, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-string-instructions

Conversation

@denn0x

@denn0x denn0x commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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 gpu: skip the title-address write fixture where array atomics are inexact #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).

@denn0x

denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

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

Lower MOVS, STOS, LODS, SCAS and CMPS in all four widths, bare and under
REP, REPE and REPNE, together with CLD and STD. Dispatch is by iced Code
because the SSE2 scalar MOVSD and CMPSD share the string mnemonics.

Each iteration moves or compares one element and steps RSI and RDI by
the width, downward when DF is set, so overlapping copies replicate as
on the hardware. RCX is tested before each element and ZF after it, and
a zero count leaves pointers, memory and flags unchanged. The 67h forms,
REPNE on MOVS, STOS and LODS, and FS or GS sources are rejected.

cpu_aot_string_ops builds 62 exports from MASM source and compares
39984 cases of memory, RAX, RSI, RDI, RCX, ZF and CF across the native
DLL, the translation and a Python reference. Three mutated translations
(memmove for REP MOVS, swapped CMPS operands, STD ignored) must mismatch.

AI-assisted (Claude Code).
@denn0x
denn0x force-pushed the cpu/lower-string-instructions branch from 791dbcb to 5b2fd02 Compare November 22, 2026 02:45
@denn0x

denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

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

@R0Y00
R0Y00 merged commit 1ec51e0 into R0Y00:main Nov 22, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants