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

Merged
denn0x merged 1 commit from upstream-pr/403 into main 2026-11-22 03:20:37 +00:00
denn0x commented 2026-11-22 00:27:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T00:27:32Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536325052 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/403). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T00:27:32Z. --- ## 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). ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_rcx_branches$' -V ``` ```text 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 #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](https://github.com/denn0x/Xbox2Windows/actions/runs/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 - [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:51 +00:00
denn0x commented 2026-11-24 06:48:18 +00:00 (Migrated from github.com)

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


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072696948 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/403#issuecomment-7072696948). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T02:08:01Z. --- 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)._
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!397
No description provided.