Skip to content

cpu: lower LOOP, LOOPE, LOOPNE, JRCXZ and their ECX forms - #403

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

R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-rcx-branches

Conversation

@denn0x

@denn0x denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Change

Advances CPU translation coverage. The RCX-counted branches are an independent problem: lower() rejects loop, loope, loopne, jrcxz and jecxz 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. #287, #289, #291, #326, #328, #331, #335, #368, #379 and #386 also extend these. This change adds one elif branch at an anchor those PRs do not use. It also adds the same Code as C import as #368, which merges as an identical change. A trial merge of #368, #379 and #386 into this branch is clean, and the combined tree passes all 46 cpu_ tests.

Lowering.

  • Forms: loop, loope and loopne lower to a decrement and a conditional goto. jrcxz lowers to a test of RCX.
  • ECX forms: the 67h forms count in ECX (iced LOOP_REL8_64_ECX, LOOPE_REL8_64_ECX, LOOPNE_REL8_64_ECX, JECXZ_REL8_64). Their decrement is a 32-bit write, which clears the upper half of RCX. The native fixture confirms this on cases with noise in the upper half.
  • Flags: none of these instructions changes a flag. loope and loopne test ZF after the decrement.
  • Targets: branch targets must lie in the translated interval, as for the existing conditional jumps.

Fixture

tests/cpu/rcx_branches.asm builds eight exports. emit asserts that each one contains the expected iced Code, so a misassembled db 67h prefix cannot pass silently.

  • loop_count, loopd_count: these count iterations with lea, which leaves flags alone, after one of four ZF/CF presets. The presets must survive loop. Counts are 1 to 40 and 1000. For loopd, RCX also carries upper halves of 0, 0xabcd and 0xffffffff.
  • loope_scan, loopne_scan, looped_scan, loopned_scan: byte scans guarded by jrcxz or jecxz. They cover counts 0 to 24, seven start offsets and five values. Over the cases, they exit on the first element, on a later one and at count exhaustion. The ECX scans also get upper-half noise.
  • jrcxz_probe, jecxz_probe: counts that differ only in the upper half of RCX, with each flag preset.

tests/cpu/rcx_branches.py check compares RAX, RCX, the final pointer offset and ZF/CF for 7712 cases. Each case runs against the native DLL, the translation and a Python reference.

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

Control Mutation Mismatches
noecx the ECX decrement keeps the upper half of RCX, and jecxz tests all of RCX 3836/7712
nozf loope and loopne ignore ZF 4432/7712
jrcxz jrcxz and jecxz inverted 7056/7712

The scan buffer ends in a zero byte. A control that skips the zero-count guard therefore still stops inside the buffer.

A first version of noecx made the ECX loops count in all of RCX. On the upper-half cases that runs for about 2^47 iterations, and the test hung, so the control now keeps the loop on ECX.

On main, lower() rejects e2 fe c3 with unsupported query operation at 0x1000: loop and e3 fe c3 with ... jrcxz.

Verification

Tested at 4d377a8 on 4f6c39d. 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_rcx_branches$' -V
90: 7712 cases over 8 exports (RAX, RCX, pointer, ZF/CF) matched the reference in native and translated DLLs
90: control noecx: 3836/7712 mismatches
90: control nozf: 4432/7712 mismatches
90: control jrcxz: 7056/7712 mismatches
1/1 Test #90: cpu_aot_rcx_branches .............   Passed    0.24 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). The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe tracked in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #369, which fails the same way on main. This change touches no GPU code.
  • 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 4d377a8 in the fork's Windows checks run 44966447068.

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 60 of 948 forms, 55 without cpu_aot_rcx_branches. The five added forms are loop, loope, loopne, jrcxz and jecxz.

Not measured: an operand-size (66h) prefix on these branches. iced decodes 66 e2 as the plain 64-bit form, and the lowering, like the existing conditional jumps, does not treat it differently. I did not measure a prefixed branch.

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

Lower the RCX-counted branches in the shared lifter as a decrement and a
conditional goto. None of them changes a flag; LOOPE and LOOPNE test ZF
after the decrement, and JRCXZ only tests the count. The 67h forms
(loopd, looped, loopned, jecxz) count in ECX, and their decrement is a
32-bit write that clears the upper half of RCX, which the native fixture
confirms.

cpu_aot_rcx_branches builds eight exports from MASM source and compares
7712 cases of RAX, RCX, the final pointer and ZF/CF across the native
DLL, the translation and a Python reference. Three mutated translations
(the ECX forms keeping the upper half of RCX, LOOPE/LOOPNE ignoring ZF,
and JRCXZ inverted) must mismatch.

AI-assisted (Claude Code).
@denn0x
denn0x force-pushed the cpu/lower-rcx-branches branch from 4d377a8 to ed260fa Compare November 22, 2026 02:07
@denn0x

denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto c2fc84c as ed260fa, resolving the docs/experiments.md overlap with #415 that I mentioned in both PRs. The RCX-branch paragraphs now follow the XLATB paragraph after the address-size text, with no change to either text. ctest --preset dev --no-tests=error -R '^cpu_' -j 8 gives 46 of 46 passed, including cpu_aot_rcx_branches and cpu_aot_xlatb. tests/docs_links_check.py passes.

AI-assisted (Claude Code).

@R0Y00
R0Y00 merged commit a40fa0e 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