Skip to content

cpu: lower forward MOVS and STOS string instructions with REP - #370

Closed
denn0x wants to merge 1 commit into
R0Y00:mainfrom
denn0x:cpu/string-moves
Closed

denn0x wants to merge 1 commit into
R0Y00:mainfrom
denn0x:cpu/string-moves

Conversation

@denn0x

@denn0x denn0x commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Change

This advances integer-lifter coverage for CPU code: compilers emit rep movs and rep stos for inline memcpy and memset and for struct copies. It touches only the shared lower dispatch in tools/ffx_version_query.py. The new branch keys on MEMORY_ESRDI operands, so it cannot catch the SSE movsd. No open PR claims string instructions (#326 is movsx, #364 is pmovsx/pmovzx), and no prerequisite PR is needed.

  • movsb, movsw, movsd, movsq, stosb, stosw, stosd, stosq, with and without rep:
    • Elements are copied one at a time, from low addresses to high, as the hardware does. An overlapping copy whose destination is one element above its source therefore repeats the first element. It does not act like memmove.
    • rep counts RCX down to zero. RSI and RDI advance by the element size. Flags are unchanged.
  • cld: lowers to nothing, because the Windows x64 ABI enters with DF clear and only forward strings are lowered.
  • Still rejected:
    • std (backward strings);
    • repne on a move or store;
    • 32-bit address-size forms (67 prefix, ESI/EDI);
    • FS/GS sources (the existing prefix check);
    • cmps, scas, lods;
    • the SSE scalar movsd, which shares the mnemonic but has register operands.

Tests

  • tests/cpu/string_ops.asm: ten MASM exports.
    • The rep forms of all eight instructions, plus one movsq and one stosd without rep.
    • Each takes (dst, src_or_value, count) and returns ((final RCX << 20) + RSI advance) << 20 + RDI advance, so the counter and both pointer advances are observed as well as memory.
  • tests/cpu/string_ops.py: compares the native DLL, the translated DLL and an independent sequential Python model, on the full 512-byte buffer and the return value.
    • Moves: counts 0–9 and 17, with disjoint, forward-overlapping (destination = source + element) and backward-overlapping ranges.
    • Stores: the same counts, with four fill values including all-ones and mixed high bytes.
    • That gives 385 cases. Each of the 36 forward-overlap cases is asserted to differ from a memmove of the same range, so a block-copy lowering cannot pass.
    • Rejections: repne movsb, rep movsb and stosb with a 67 prefix, FS movsb, std, repe cmpsb, lodsb, repne scasb and SSE movsd xmm0, xmm1, each with its exact diagnostic.
  • Coverage: tools/form_coverage.py maps string_ops to cpu_aot_string_ops, and x64 coverage goes from 55 of 948 forms on 9bdbb20 to 64 at bd00df2 (the eight string forms plus cld), measured with tools/form_coverage.py over the JUnit of ctest --preset dev -R '^cpu_'.

Verification

Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13.14 with iced-x86 1.21.0. Commit bd00df2 (rebased on 9bdbb20).

cmake --build --preset dev --target cpu_string_ops_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_string_ops$' -V
95: 385 string move/store cases (36 forward-overlap) matched native and translated DLLs in memory, RCX, RSI and RDI
95: REPNE, 32-bit addressing, FS, STD, CMPS, LODS, SCAS and SSE MOVSD were rejected
1/1 Test #95: cpu_aot_string_ops ...............   Passed    0.18 sec

Mutation check (local, reverted): lowering rep movs as a single memmove fails at the first forward-overlap case:

AssertionError: lifted rep_movsb(dst=129, src=0x80, count=2) = 0x200002, expected 0x200002; first differing byte 130

Full suite (ctest --preset dev --no-tests=error), rebased on 9bdbb20:

Before the rebase, on bc24dfd, the suite gave 145 passed and the same two skips, with no failures. All 43 cpu_ tests pass at bd00df2. tests/form_coverage_check.py passes.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • It includes a negative control (the memmove discrimination, the rejections and the mutation check).
  • docs/experiments.md is updated.
  • No game files, dumps or binaries.

AI-assisted (Claude Code).

The integer lifter now lowers movsb/movsw/movsd/movsq and
stosb/stosw/stosd/stosq, with and without rep, plus cld. Elements are
copied one at a time from low to high addresses, so a forward-overlapping
copy repeats its first element as the hardware does instead of acting like
memmove. rep counts RCX down to zero; RSI and RDI advance by the element
size. cld lowers to nothing because the Windows x64 ABI enters with DF
clear. std, repne, 32-bit address-size forms, FS/GS sources, cmps, scas,
lods and the SSE movsd (same mnemonic, register operands) stay rejected.

cpu_aot_string_ops builds ten MASM exports and compares memory, final RCX
and the RSI/RDI advances against the native DLL and a sequential reference
(385 cases, 36 forward-overlap cases asserted to differ from memmove).

AI-assisted (Claude Code).
@denn0x

denn0x commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #368, which lowers the same forward movs/stos forms and also covers lods, scas, cmps and std. Sorry for the noise.

@denn0x denn0x closed this Nov 21, 2026
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.

1 participant