cpu: translate 8- and 16-bit register writes, void returns and ret 0 in the jump-table translator #272

Closed
opened 2026-11-21 20:07:57 +00:00 by bosseurdinternet · 0 comments
bosseurdinternet commented 2026-11-21 20:07:57 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T20:07:57Z.


Problem

tools/cpu_switch_aot.py accepts only 32- and 64-bit register writes and requires RAX to be fully defined at every RET. It therefore refuses:

  • a function that returns nothing (undefined register rax);
  • a function that returns a bool (mov al, 1; ret: unsupported partial register write);
  • ret 0 encoded as C2 00 00 (unsupported control flow);
  • any 8- or 16-bit register destination.

Measured on 1,119 functions compiled with MSVC 14.52 /O2 from this repository's own C++ sources plus one plain C++ file written for the measurement: cpu_switch_aot.lift lowers 478 today. As a first refusal, ret 0 accounts for 52 functions and a narrow register write for 49. Limits of that measurement: 831 of the 1,119 functions are MSVC STL instantiations, only the first refusal of each function is counted, and this is desktop code.

Proposal

I am working on this now and intend to send one PR.

  • Lifter.write merges 8- and 16-bit low-register destinations into the full register. AH, BH, CH and DH stay rejected. 32- and 64-bit writes are unchanged, so the C++ generated for functions that already lower stays identical.
  • The must-defined pass tracks how many low bytes of each register are defined (0, 1, 2 or 8) and whether the register may hold a guest address. A narrow write to a partly defined register does not make it readable at a wider width.
  • RET no longer requires RAX. Each translated function reports the number of low bytes of RAX defined at every return (0 for a void function, 1 for a bool). A return whose RAX may hold a guest address stays rejected.
  • ret 0 is accepted; any other ret N stays rejected.

Shared interfaces this changes: the read and write effects returned by Lifter.statement, Lifter.write, and the must-defined pass at the end of lift().

Overlap. #173 (SETcc and CMOVcc, by @Vonsek) changes the same lift() loop and statement(). The behaviours do not overlap. #173 was opened first; if it lands first I will rebase onto it. One interaction worth knowing: with byte-wide definedness, a SETcc into a byte register would not need the full register to be defined beforehand.

An in-memory prototype of the design lowers 582 of the same 1,119 functions with no function lost. About 55 of the 104 gained are empty void functions and 13 are two-instruction bool functions, so roughly 36 are substantive. These are prototype numbers; the PR will report measured ones.

Alternatives

  • Only void returns and ret 0, without narrow writes: smaller, but a bool return stays refused. I can split the PR this way if preferred.
  • Treating a narrow write as defining the whole register: accepts more, but lets undefined upper bytes reach wider reads.

Verification

A new source-built MASM fixture and CTest entry, separate from switch.asm so it does not collide with #173:

  • differential comparison with the native exports through the existing register probe, comparing RAX only up to the reported width (the x64 calling convention leaves unused bits of the value returned in RAX undefined);
  • rejection controls printed in the test output: a high-byte destination, a wide read after a narrow write, ret 8, a join of paths with different defined widths, and a returned guest address;
  • before and after counts on the corpus above, with a check that no previously lowered function is lost and that their generated C++ is unchanged.

Checklist

  • I checked for an existing issue or pull request for this change.
  • I did not include game files, dumps, distribution links or credentials.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6533794465 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/276). Originally posted by [bosseurdinternet](https://github.com/bosseurdinternet) on 2026-11-21T20:07:57Z. --- ### Problem `tools/cpu_switch_aot.py` accepts only 32- and 64-bit register writes and requires RAX to be fully defined at every `RET`. It therefore refuses: - a function that returns nothing (`undefined register rax`); - a function that returns a bool (`mov al, 1; ret`: `unsupported partial register write`); - `ret 0` encoded as `C2 00 00` (`unsupported control flow`); - any 8- or 16-bit register destination. Measured on 1,119 functions compiled with MSVC 14.52 `/O2` from this repository's own C++ sources plus one plain C++ file written for the measurement: `cpu_switch_aot.lift` lowers 478 today. As a first refusal, `ret 0` accounts for 52 functions and a narrow register write for 49. Limits of that measurement: 831 of the 1,119 functions are MSVC STL instantiations, only the first refusal of each function is counted, and this is desktop code. ### Proposal I am working on this now and intend to send one PR. - `Lifter.write` merges 8- and 16-bit low-register destinations into the full register. AH, BH, CH and DH stay rejected. 32- and 64-bit writes are unchanged, so the C++ generated for functions that already lower stays identical. - The must-defined pass tracks how many low bytes of each register are defined (0, 1, 2 or 8) and whether the register may hold a guest address. A narrow write to a partly defined register does not make it readable at a wider width. - `RET` no longer requires RAX. Each translated function reports the number of low bytes of RAX defined at every return (0 for a void function, 1 for a bool). A return whose RAX may hold a guest address stays rejected. - `ret 0` is accepted; any other `ret N` stays rejected. Shared interfaces this changes: the read and write effects returned by `Lifter.statement`, `Lifter.write`, and the must-defined pass at the end of `lift()`. **Overlap.** #173 (SETcc and CMOVcc, by @Vonsek) changes the same `lift()` loop and `statement()`. The behaviours do not overlap. #173 was opened first; if it lands first I will rebase onto it. One interaction worth knowing: with byte-wide definedness, a SETcc into a byte register would not need the full register to be defined beforehand. An in-memory prototype of the design lowers 582 of the same 1,119 functions with no function lost. About 55 of the 104 gained are empty void functions and 13 are two-instruction bool functions, so roughly 36 are substantive. These are prototype numbers; the PR will report measured ones. ### Alternatives - Only void returns and `ret 0`, without narrow writes: smaller, but a bool return stays refused. I can split the PR this way if preferred. - Treating a narrow write as defining the whole register: accepts more, but lets undefined upper bytes reach wider reads. ### Verification A new source-built MASM fixture and CTest entry, separate from `switch.asm` so it does not collide with #173: - differential comparison with the native exports through the existing register probe, comparing RAX only up to the reported width (the x64 calling convention leaves unused bits of the value returned in RAX undefined); - rejection controls printed in the test output: a high-byte destination, a wide read after a narrow write, `ret 8`, a join of paths with different defined widths, and a returned guest address; - before and after counts on the corpus above, with a check that no previously lowered function is lost and that their generated C++ is unchanged. ### Checklist - [x] I checked for an existing issue or pull request for this change. - [x] I did not include game files, dumps, distribution links or credentials. _AI-assisted (Claude Code)._
Sign in to join this conversation.
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#272
No description provided.