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

Merged
bosseurdinternet merged 2 commits from upstream-pr/309 into main 2026-11-21 22:25:44 +00:00
bosseurdinternet commented 2026-11-21 21:42:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T21:42:04Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6534749341 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/309). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T21:42:04Z. --- ## 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)
upstream-sync closed this pull request 2026-11-24 06:42:21 +00:00
R0Y00 commented 2026-11-24 06:48:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T21:56:49Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069830551 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/309#issuecomment-7069830551). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T21:56:49Z. --- 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.
bosseurdinternet commented 2026-11-24 06:48:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T21:59:19Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069859555 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/309#issuecomment-7069859555). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T21:59:19Z. --- 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 commented 2026-11-24 06:48:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T22:22:13Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070132601 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/309#issuecomment-7070132601). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T22:22:13Z. --- 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)._
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!304
No description provided.