cpu: accept a fourth integer argument in R9 as an explicit per-entry choice #309

Manually merged
github merged 2 commits from gh-pr/309/head into gh-pr/309/base 2026-11-26 21:42:02 +00:00
Owner

@⁠bosseurdinternet on GitHub · 2026-11-21 21:42 UTC · original
bosseurdinternet:cpu/fourth-argument → main · +323 −9 · 9 files · 2 commits · merged 2026-11-21 22:25 UTC

Change

Builds on #285 (merged). Reworked after review: the fourth argument is an explicit per-entry choice, and the default is unchanged.

  • Default: three arguments, as on main. An entry listed by name takes word f(word rcx, word rdx, word r8). R9 is undefined at entry and is a scratch register: a read before a write is rejected with undefined register r9, and a write followed by a read is accepted, with r9 as a local. The generated C++ of every function translated this way is identical to main's.
  • Explicit fourth argument. Whoever invokes the translator can list an entry as name:4, in the export list of the CLI, of translate() and of cpu_translation(). R9 is then defined at entry and the generated function has a fourth word r9 parameter. name:3 is the explicit default. Any other count is rejected with unsupported argument count. lift(pe, name, arguments=3) takes the count directly, and Function.arguments records the entry's argument registers; the entry state, the local declarations and the signature follow it.
  • Why name:4. The CLI and CMake already pass one comma-separated list, so the suffix needs no CMake change. @N would collide with stdcall decoration. An export whose own name contains : cannot be named this way.
  • What the count is. It is the invoker's choice, not derived from the machine code, and the host must declare and pass the same number of arguments as the entry was translated with. Stack arguments (fifth and later) and floating-point arguments stay unsupported.

Shared interfaces: the export-list syntax, lift's new keyword, Function.arguments (the module constant ARGUMENTS is replaced by ARGUMENT_REGISTERS and DEFAULT_ARGUMENTS), and tests/cpu/register_probe.asm, which gains probe4; probe and clobber_control are unchanged.

Verification

Local runs used the same compiler build as CI (MSVC 14.52.37025: cl, link, ml64) under Wine 11.0 on Linux, at e144565 on main 6ee2406. I reran the items below on this head unless a line says otherwise.

  • ctest --no-tests=error -R '^cpu_aot_fourth_argument$' -V: passed. The build translates r9_mix:4,r9_loop:4,no_r9:4,r9_scratch. Four-argument entries are called through probe4 and the three-argument entry through probe; 7,719 calls per side match a Python reference on return value, guarded trace, RBX and RSI. The test requires the count recorded on each Function to equal the count its list item names, and the built source to equal the translation of that list.
  • Controls printed by the test, one line each:
    • an existing three-argument entry: narrow.asm's byte_merge, which writes R9 before reading it, keeps (word rcx, word rdx, word r8) with r9 a local, and 249 pairs through probe match the native export and the cpu_aot_narrow_returns reference;
    • r9_mix rejected in three-argument mode with undefined register r9, and the same export accepted as r9_mix:4 with a four-parameter signature;
    • r9_scratch, which writes R9 and then reads it, accepted in three-argument mode, and r9_scratch:3 gives the same translation;
    • no_r9:4 gives identical results for every fourth argument;
    • counts 5 and x rejected; R9 patched to R10 rejected with undefined register r10.
  • Existing callers. switch_check.py, switch_addr32_check.py, entry_order_check.py and narrow_check.py are unchanged, and cpu_aot_jump_tables, cpu_aot_switch_addr32, cpu_aot_entry_order and cpu_aot_narrow_returns pass.
  • Other cpu_* tests: same results as main. Under Wine, cpu_aot_import_call, cpu_aot_ordinal_import, cpu_native_reloc_free and cpu_native_tls fail on main too because Wine's loader differs from Windows. The Windows job is the reference for those.
  • Corpus, default mode against main, with no allowed difference. 1,119 functions compiled with MSVC /O2 from this repository's C++ sources plus one plain C++ file, and the /O1 and /Od builds: the same 642, 639 and 15 functions translate, all 1,296 have identical C++, and the shared header is identical.
  • Manual mutant, not in CI: with the default changed to four, r9_mix is accepted without a suffix and r9_scratch records four arguments, which the controls above reject.
  • Run during development, not rerun on this head:
    • with every corpus entry tried in four-argument mode, 9 more functions translate at /O2 and at /O1. Their mangled names show four integer-class parameters each. This is an upper bound that assumes the right count was chosen for each entry;
    • differential execution of those nine in four-argument mode, on a few inputs each, found no mismatch;
    • further mutants (the choice ignored; the entry state, signature or locals not following it; a count of 5 accepted; an unchecked suffix; R10 defined at entry; three wrong probe4 stack offsets) each fail the test or the build. Not caught by any test: R11 also defined at entry;
    • the switch fixture's translations compile under clang-cl /W4 /WX again, as docs/experiments.md states.
  • pyflakes, tests/docs_links_check.py and tests/form_coverage_check.py passed.

Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.

Evidence and follow-up

  • tests/cpu/fourth_arg.asm and probe4 are original MASM assembled in the build. No binaries were added. fourth_arg is classified in tools/form_coverage.py.
  • Limits of the corpus numbers: 831 of the 1,119 functions are MSVC STL instantiations, and this is desktop code built from this repository, not a title.
  • #321 adds lines next to this PR's in CMakeLists.txt, docs/experiments.md and tools/form_coverage.py. I will rebase whichever lands second.
  • Jump-table translator notes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠bosseurdinternet** on GitHub · 2026-11-21 21:42 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/309) `bosseurdinternet:cpu/fourth-argument` → `main` · +323 −9 · 9 files · 2 commits · merged 2026-11-21 22:25 UTC ## Change Builds on #285 (merged). Reworked after review: the fourth argument is an explicit per-entry choice, and the default is unchanged. - **Default: three arguments, as on main.** An entry listed by name takes `word f(word rcx, word rdx, word r8)`. R9 is undefined at entry and is a scratch register: a read before a write is rejected with `undefined register r9`, and a write followed by a read is accepted, with `r9` as a local. The generated C++ of every function translated this way is identical to main's. - **Explicit fourth argument.** Whoever invokes the translator can list an entry as `name:4`, in the export list of the CLI, of `translate()` and of `cpu_translation()`. R9 is then defined at entry and the generated function has a fourth `word r9` parameter. `name:3` is the explicit default. Any other count is rejected with `unsupported argument count`. `lift(pe, name, arguments=3)` takes the count directly, and `Function.arguments` records the entry's argument registers; the entry state, the local declarations and the signature follow it. - **Why `name:4`.** The CLI and CMake already pass one comma-separated list, so the suffix needs no CMake change. `@N` would collide with stdcall decoration. An export whose own name contains `:` cannot be named this way. - **What the count is.** It is the invoker's choice, not derived from the machine code, and the host must declare and pass the same number of arguments as the entry was translated with. Stack arguments (fifth and later) and floating-point arguments stay unsupported. Shared interfaces: the export-list syntax, `lift`'s new keyword, `Function.arguments` (the module constant `ARGUMENTS` is replaced by `ARGUMENT_REGISTERS` and `DEFAULT_ARGUMENTS`), and `tests/cpu/register_probe.asm`, which gains `probe4`; `probe` and `clobber_control` are unchanged. ## Verification Local runs used the same compiler build as CI (MSVC 14.52.37025: `cl`, `link`, `ml64`) under Wine 11.0 on Linux, at `e144565` on main `6ee2406`. I reran the items below on this head unless a line says otherwise. - **`ctest --no-tests=error -R '^cpu_aot_fourth_argument$' -V`: passed.** The build translates `r9_mix:4,r9_loop:4,no_r9:4,r9_scratch`. Four-argument entries are called through `probe4` and the three-argument entry through `probe`; 7,719 calls per side match a Python reference on return value, guarded trace, RBX and RSI. The test requires the count recorded on each `Function` to equal the count its list item names, and the built source to equal the translation of that list. - **Controls printed by the test, one line each:** - an existing three-argument entry: `narrow.asm`'s `byte_merge`, which writes R9 before reading it, keeps `(word rcx, word rdx, word r8)` with `r9` a local, and 249 pairs through `probe` match the native export and the `cpu_aot_narrow_returns` reference; - `r9_mix` rejected in three-argument mode with `undefined register r9`, and the same export accepted as `r9_mix:4` with a four-parameter signature; - `r9_scratch`, which writes R9 and then reads it, accepted in three-argument mode, and `r9_scratch:3` gives the same translation; - `no_r9:4` gives identical results for every fourth argument; - counts `5` and `x` rejected; R9 patched to R10 rejected with `undefined register r10`. - **Existing callers.** `switch_check.py`, `switch_addr32_check.py`, `entry_order_check.py` and `narrow_check.py` are unchanged, and `cpu_aot_jump_tables`, `cpu_aot_switch_addr32`, `cpu_aot_entry_order` and `cpu_aot_narrow_returns` pass. - **Other `cpu_*` tests:** same results as main. Under Wine, `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_native_reloc_free` and `cpu_native_tls` fail on main too because Wine's loader differs from Windows. The Windows job is the reference for those. - **Corpus, default mode against main, with no allowed difference.** 1,119 functions compiled with MSVC `/O2` from this repository's C++ sources plus one plain C++ file, and the `/O1` and `/Od` builds: the same 642, 639 and 15 functions translate, all 1,296 have identical C++, and the shared header is identical. - **Manual mutant, not in CI:** with the default changed to four, `r9_mix` is accepted without a suffix and `r9_scratch` records four arguments, which the controls above reject. - **Run during development, not rerun on this head:** - with every corpus entry tried in four-argument mode, 9 more functions translate at `/O2` and at `/O1`. Their mangled names show four integer-class parameters each. This is an upper bound that assumes the right count was chosen for each entry; - differential execution of those nine in four-argument mode, on a few inputs each, found no mismatch; - further mutants (the choice ignored; the entry state, signature or locals not following it; a count of 5 accepted; an unchecked suffix; R10 defined at entry; three wrong `probe4` stack offsets) each fail the test or the build. Not caught by any test: R11 also defined at entry; - the switch fixture's translations compile under clang-cl `/W4 /WX` again, as `docs/experiments.md` states. - `pyflakes`, `tests/docs_links_check.py` and `tests/form_coverage_check.py` passed. Not run locally: the D3D12 tests and anything that needs Windows loader behaviour. ## Evidence and follow-up - `tests/cpu/fourth_arg.asm` and `probe4` are original MASM assembled in the build. No binaries were added. `fourth_arg` is classified in `tools/form_coverage.py`. - Limits of the corpus numbers: 831 of the 1,119 functions are MSVC STL instantiations, and this is desktop code built from this repository, not a title. - #321 adds lines next to this PR's in `CMakeLists.txt`, `docs/experiments.md` and `tools/form_coverage.py`. I will rebase whichever lands second. - [Jump-table translator notes](https://github.com/R0Y00/Xbox2Windows/blob/main/docs/experiments.md#switch-dispatch-through-jump-tables) _AI-assisted (Claude Code)._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tools/cpu_switch_aot.py took RCX, RDX and R8 as arguments and refused any
function that reads R9 before writing it ("undefined register r9"). The
Windows x64 convention passes the fourth integer argument in R9.

ARGUMENTS now holds RCX, RDX, R8 and R9. The entry state of the definedness
pass, the local declarations and the emitted signature already derive from
that tuple, so every generated function takes a fourth word parameter r9,
and a function that used R9 as scratch no longer declares it as a local.
The machine code does not show how many parameters the source declares: a
function that reads R9 without a fourth integer parameter is accepted and
reads what its caller left there, as was already the case for RCX, RDX and
R8. Stack arguments and floating-point arguments stay unsupported.

The register probe gains probe4, which passes its fifth argument in R9.
cpu_aot_fourth_argument translates four exports of fourth_arg.asm (R9 in
arithmetic and stored, R9D as a loop counter behind CMP/JA, TEST/JZ and
DEC/JNZ, no R9 at all, R9 written before it is read) and compares native
and translated calls with a Python reference over the direct-call input
pairs crossed with ten fourth arguments. The two exports that never read the
incoming R9 must give identical results for every fourth argument, and the
LEA index patched from R9 to R10 must be rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of #309: keep the three-argument signature and its entry
definedness as the default, and make the four-argument ABI an explicit
choice for one entry, matched by the host declaration and probe.

An entry takes RCX, RDX and R8 unless the export list names it as
"name:4"; "name:3" is the explicit default and any other count is
rejected with "unsupported argument count". The suffix works unchanged
through the CLI's comma-separated list, translate()/translate_pe() and
the CMake cpu_translation() list; lift() takes the count as a keyword.
Function.arguments records the argument registers of each entry, and the
entry state of the definedness pass, the local declarations and the
signature follow it. On a three-argument entry R9 is undefined at entry
again: a read before a write is rejected, and a write followed by a read
is accepted with r9 declared as a local. The generated C++ for every
function translated without a suffix is the same as on main.

cpu_aot_fourth_argument now translates r9_mix:4,r9_loop:4,no_r9:4 and
r9_scratch (three arguments), checks that the built source is that
translation, and calls each entry through the probe that declares its
argument count: probe4 for four, probe for three. New printed controls:
narrow.asm's byte_merge keeps its three-parameter translation and still
matches through probe; r9_mix is rejected with "undefined register r9"
in three-argument mode and accepted as r9_mix:4; the counts 5 and x are
rejected. The R10 rejection control stays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:42:00 +00:00
github manually merged commit e1445650c1 into gh-pr/309/base 2026-11-26 21:42:02 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 21:56 UTC · original

Please keep the existing three-argument signature and its entry definedness as the default. Make the four-argument ABI an explicit per-entry choice, matched by the host declaration and probe. On a three-argument entry R9 is a scratch register: reject a read before definition and allow a write followed by a read. Verify an existing three-argument caller, and show the same R9-first-read fixture rejected in three-argument mode and accepted with the explicit fourth argument.

**@⁠R0Y00** on GitHub · 2026-11-21 21:56 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/309#issuecomment-7069830551) Please keep the existing three-argument signature and its entry definedness as the default. Make the four-argument ABI an explicit per-entry choice, matched by the host declaration and probe. On a three-argument entry R9 is a scratch register: reject a read before definition and allow a write followed by a read. Verify an existing three-argument caller, and show the same R9-first-read fixture rejected in three-argument mode and accepted with the explicit fourth argument.
Author
Owner

@⁠bosseurdinternet on GitHub · 2026-11-21 21:59 UTC · original

Understood. I am reworking it that way: the three-argument signature and entry definedness stay the default, so R9 is a scratch register there (a read before definition rejected, a write followed by a read accepted), and the four-argument ABI becomes an explicit per-entry choice matched by the host declaration and the probe. The update will come as a new commit on this branch with the controls you list: an existing three-argument caller verified, and the same R9-first-read export rejected in three-argument mode and accepted with the explicit fourth argument. With the default restored, the generated C++ of every function main already translates should be identical again; I will report that check.

AI-assisted (Claude Code).

**@⁠bosseurdinternet** on GitHub · 2026-11-21 21:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/309#issuecomment-7069859555) Understood. I am reworking it that way: the three-argument signature and entry definedness stay the default, so R9 is a scratch register there (a read before definition rejected, a write followed by a read accepted), and the four-argument ABI becomes an explicit per-entry choice matched by the host declaration and the probe. The update will come as a new commit on this branch with the controls you list: an existing three-argument caller verified, and the same R9-first-read export rejected in three-argument mode and accepted with the explicit fourth argument. With the default restored, the generated C++ of every function main already translates should be identical again; I will report that check. _AI-assisted (Claude Code)._
Author
Owner

@⁠bosseurdinternet on GitHub · 2026-11-21 22:22 UTC · original

Reworked as requested, in a new commit e144565 on top of the first one, rebased on 6ee2406.

Default unchanged. An entry listed by name keeps word f(word rcx, word rdx, word r8) and R9 undefined at entry. Compared with main and with no allowed difference, the same 642, 639 and 15 corpus functions translate at /O2, /O1 and /Od, all 1,296 have identical C++, and the shared header is identical.

Explicit per-entry choice. name:4 in the export list (CLI, translate(), cpu_translation()) gives that entry a fourth integer argument in R9; name:3 is the explicit default; other counts are rejected. Function.arguments records it, and the entry state, locals and signature follow it. The fixture builds r9_mix:4,r9_loop:4,no_r9:4,r9_scratch, so both modes are in one image.

Matched by the host declaration and probe. Four-argument entries are called through probe4, the three-argument entry through probe, and the test requires the recorded count to equal the one the list item names.

The controls you listed, from cpu_aot_fourth_argument -V:

control r9_scratch (three arguments, probe): writes R9 before reading it, r9 is a local, 249 pairs match, native and translated
control narrow.asm byte_merge (existing entry, three arguments by default, writes R9 before reading it): signature (word rcx, word rdx, word r8) with r9 a local, 249 pairs through probe match native and the cpu_aot_narrow_returns reference
rejected r9_mix (three arguments by default): undefined register r9 at RVA 0x1000
accepted r9_mix:4 (explicit fourth argument): signature (word rcx, word rdx, word r8, word r9)
r9_scratch:3 and r9_scratch give the same translation
rejected r9_mix:5 (invalid argument count): unsupported argument count 5 for export r9_mix: expected 3 or 4
rejected r9_mix:x (invalid argument count): unsupported argument count 'x' for export r9_mix: expected 3 or 4
rejected r9_mix:4 (lea rax,[rcx+r9*2] -> lea rax,[rcx+r10*2]): undefined register r10 at RVA 0x1000

The existing three-argument tests (switch_check.py, switch_addr32_check.py, entry_order_check.py, narrow_check.py) are unchanged and pass. The title and description are updated; the description says which checks I reran on this head and which were run during development.

AI-assisted (Claude Code).

**@⁠bosseurdinternet** on GitHub · 2026-11-21 22:22 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/309#issuecomment-7070132601) Reworked as requested, in a new commit `e144565` on top of the first one, rebased on `6ee2406`. **Default unchanged.** An entry listed by name keeps `word f(word rcx, word rdx, word r8)` and R9 undefined at entry. Compared with main and with no allowed difference, the same 642, 639 and 15 corpus functions translate at `/O2`, `/O1` and `/Od`, all 1,296 have identical C++, and the shared header is identical. **Explicit per-entry choice.** `name:4` in the export list (CLI, `translate()`, `cpu_translation()`) gives that entry a fourth integer argument in R9; `name:3` is the explicit default; other counts are rejected. `Function.arguments` records it, and the entry state, locals and signature follow it. The fixture builds `r9_mix:4,r9_loop:4,no_r9:4,r9_scratch`, so both modes are in one image. **Matched by the host declaration and probe.** Four-argument entries are called through `probe4`, the three-argument entry through `probe`, and the test requires the recorded count to equal the one the list item names. **The controls you listed**, from `cpu_aot_fourth_argument -V`: ```text control r9_scratch (three arguments, probe): writes R9 before reading it, r9 is a local, 249 pairs match, native and translated control narrow.asm byte_merge (existing entry, three arguments by default, writes R9 before reading it): signature (word rcx, word rdx, word r8) with r9 a local, 249 pairs through probe match native and the cpu_aot_narrow_returns reference rejected r9_mix (three arguments by default): undefined register r9 at RVA 0x1000 accepted r9_mix:4 (explicit fourth argument): signature (word rcx, word rdx, word r8, word r9) r9_scratch:3 and r9_scratch give the same translation rejected r9_mix:5 (invalid argument count): unsupported argument count 5 for export r9_mix: expected 3 or 4 rejected r9_mix:x (invalid argument count): unsupported argument count 'x' for export r9_mix: expected 3 or 4 rejected r9_mix:4 (lea rax,[rcx+r9*2] -> lea rax,[rcx+r10*2]): undefined register r10 at RVA 0x1000 ``` The existing three-argument tests (`switch_check.py`, `switch_addr32_check.py`, `entry_order_check.py`, `narrow_check.py`) are unchanged and pass. The title and description are updated; the description says which checks I reran on this head and which were run during development. _AI-assisted (Claude Code)._
This discussion has been locked. Commenting is limited to contributors.
No description provided.