cpu: accept a fourth integer argument in R9 as an explicit per-entry choice #309
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!309
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/309/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCChange
Builds on #285 (merged). Reworked after review: the fourth argument is an explicit per-entry choice, and the default is unchanged.
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 withundefined register r9, and a write followed by a read is accepted, withr9as a local. The generated C++ of every function translated this way is identical to main's.name:4, in the export list of the CLI, oftranslate()and ofcpu_translation(). R9 is then defined at entry and the generated function has a fourthword r9parameter.name:3is the explicit default. Any other count is rejected withunsupported argument count.lift(pe, name, arguments=3)takes the count directly, andFunction.argumentsrecords the entry's argument registers; the entry state, the local declarations and the signature follow it.name:4. The CLI and CMake already pass one comma-separated list, so the suffix needs no CMake change.@Nwould collide with stdcall decoration. An export whose own name contains:cannot be named this way.Shared interfaces: the export-list syntax,
lift's new keyword,Function.arguments(the module constantARGUMENTSis replaced byARGUMENT_REGISTERSandDEFAULT_ARGUMENTS), andtests/cpu/register_probe.asm, which gainsprobe4;probeandclobber_controlare unchanged.Verification
Local runs used the same compiler build as CI (MSVC 14.52.37025:
cl,link,ml64) under Wine 11.0 on Linux, ate144565on main6ee2406. 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 translatesr9_mix:4,r9_loop:4,no_r9:4,r9_scratch. Four-argument entries are called throughprobe4and the three-argument entry throughprobe; 7,719 calls per side match a Python reference on return value, guarded trace, RBX and RSI. The test requires the count recorded on eachFunctionto equal the count its list item names, and the built source to equal the translation of that list.narrow.asm'sbyte_merge, which writes R9 before reading it, keeps(word rcx, word rdx, word r8)withr9a local, and 249 pairs throughprobematch the native export and thecpu_aot_narrow_returnsreference;r9_mixrejected in three-argument mode withundefined register r9, and the same export accepted asr9_mix:4with a four-parameter signature;r9_scratch, which writes R9 and then reads it, accepted in three-argument mode, andr9_scratch:3gives the same translation;no_r9:4gives identical results for every fourth argument;5andxrejected; R9 patched to R10 rejected withundefined register r10.switch_check.py,switch_addr32_check.py,entry_order_check.pyandnarrow_check.pyare unchanged, andcpu_aot_jump_tables,cpu_aot_switch_addr32,cpu_aot_entry_orderandcpu_aot_narrow_returnspass.cpu_*tests: same results as main. Under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail on main too because Wine's loader differs from Windows. The Windows job is the reference for those./O2from this repository's C++ sources plus one plain C++ file, and the/O1and/Odbuilds: the same 642, 639 and 15 functions translate, all 1,296 have identical C++, and the shared header is identical.r9_mixis accepted without a suffix andr9_scratchrecords four arguments, which the controls above reject./O2and 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;probe4stack offsets) each fail the test or the build. Not caught by any test: R11 also defined at entry;/W4 /WXagain, asdocs/experiments.mdstates.pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.
Evidence and follow-up
tests/cpu/fourth_arg.asmandprobe4are original MASM assembled in the build. No binaries were added.fourth_argis classified intools/form_coverage.py.CMakeLists.txt,docs/experiments.mdandtools/form_coverage.py. I will rebase whichever lands second.AI-assisted (Claude Code).
🤖 Generated with 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]>@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.
@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 22:22 UTC · original
Reworked as requested, in a new commit
e144565on top of the first one, rebased on6ee2406.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,/O1and/Od, all 1,296 have identical C++, and the shared header is identical.Explicit per-entry choice.
name:4in the export list (CLI,translate(),cpu_translation()) gives that entry a fourth integer argument in R9;name:3is the explicit default; other counts are rejected.Function.argumentsrecords it, and the entry state, locals and signature follow it. The fixture buildsr9_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 throughprobe, 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: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).