cpu: accept FFREE of a statically empty register in the x87 profile #1419

Manually merged
github merged 1 commit from gh-pr/1419/head into gh-pr/1419/base 2026-11-26 23:05:36 +00:00
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-24 11:08 UTC · original
legacy-code-archaeologist:cpu/x87-ffree-empty → main · +81 −16 · 8 files · 1 commits · merged 2026-11-24 11:33 UTC

Refs #1338

What

The x87 profile (tools/x87_lower.py) now accepts FFREE ST(i) when ST(i) is statically known to be empty, that is when i is at least the depth. This is the slice approved in #1338.

  • Occupancy, TOP and every occupied slot stay unchanged. C0 to C3 become undefined, and readers of them stay refused.
  • FFREE of an occupied register is refused with "frees an occupied register". That covers the deepest register (TOP would stay while the stack shrinks) and any other (a hole).
  • Unchanged by this PR: FFREEP ST(i) for i > 0, FINCSTP and FDECSTP stay outside the profile, and TOP only changes as it did before.

Existing tests changed (please check)

tests/cpu/x87_add.py and tests/cpu/x87_moves.py each had the rejection case ffree st(1) after one fld. ST(1) is empty at depth 1, so this PR accepts that sequence. Both cases now use ffree st(0) on the occupied ST(0) and still expect a refusal, now with the "frees an occupied register" message.

Evidence

cpu_aot_x87_examine gains the export ffree_empty:

  • It runs ffree st(0) and ffree st(7) at depth 0.
  • It runs ffree st(1), st(4) and st(7) at depth 1.
  • At depth 7 it runs ffree st(7), then the final legal push to depth 8.
  • After each group it records the physical TOP (fnstsw ax, and ah, 38h, store AH).
  • From depth 8 it stores the pushed 0.0, the 1.0 below it, and the input at the bottom, for all 34 m80 encodings of the fixture.

The check fails in each of these cases:

  • Controls (must mismatch):
    • ffree: ffree st(7) frees ST(0) instead. 34/884 mismatches.
    • freetop: FFREE moves TOP as a pop does, so the status word at depth 1 reads TOP 0. 68/884 mismatches; the literal also matches the depth-1 status read of ffreep_top.
  • New refusals:
    • a reader of C0 after fld1; ftst; ffree st(1) (the same sequence without FFREE is accepted);
    • ffree st(0) and ffree st(1) on occupied registers.

Windows 11 Pro 26100.9168, Intel i7-6820HQ, MSVC 19.52, commit c6571ee9 on 153bb5f6:

ctest --preset dev --no-tests=error -R '^cpu_aot_x87_examine$' -V

Result:

  • 884 cases over 7 exports matched the model in the native and translated DLLs. The native run gave TOP 0, 7 and 0. The push from depth 7 did not overflow, and the values were unchanged.
  • Controls normal/unsigned/positive/legacy/ffreep/fftop/ffree/freetop gave 5/136/7/34/33/34/34/68 mismatches.
  • 15 sequences were refused.
  • cpu_aot_x87_add and cpu_aot_x87_moves pass.
  • The release preset gives the same result.

Other runs:

  • Before the rebase, ctest --preset dev -E '_hardware$': 630/630.
  • Release -R '^cpu_aot_x87_': 21/21.
  • tools/form_coverage.py --gaps x64_zen2 no longer lists ffree.

🤖 Generated with Claude Code

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-24 11:08 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1419) `legacy-code-archaeologist:cpu/x87-ffree-empty` → `main` · +81 −16 · 8 files · 1 commits · merged 2026-11-24 11:33 UTC Refs #1338 ## What The x87 profile (`tools/x87_lower.py`) now accepts **FFREE ST(i) when ST(i) is statically known to be empty**, that is when `i` is at least the depth. This is the slice approved in #1338. - Occupancy, TOP and every occupied slot stay unchanged. C0 to C3 become undefined, and readers of them stay refused. - FFREE of an occupied register is refused with "frees an occupied register". That covers the deepest register (TOP would stay while the stack shrinks) and any other (a hole). - Unchanged by this PR: FFREEP ST(i) for i > 0, FINCSTP and FDECSTP stay outside the profile, and TOP only changes as it did before. ## Existing tests changed (please check) `tests/cpu/x87_add.py` and `tests/cpu/x87_moves.py` each had the rejection case `ffree st(1)` after one `fld`. ST(1) is empty at depth 1, so this PR accepts that sequence. Both cases now use `ffree st(0)` on the occupied ST(0) and still expect a refusal, now with the "frees an occupied register" message. ## Evidence `cpu_aot_x87_examine` gains the export `ffree_empty`: - It runs `ffree st(0)` and `ffree st(7)` at depth 0. - It runs `ffree st(1)`, `st(4)` and `st(7)` at depth 1. - At depth 7 it runs `ffree st(7)`, then the final legal push to depth 8. - After each group it records the physical TOP (`fnstsw ax`, `and ah, 38h`, store AH). - From depth 8 it stores the pushed 0.0, the 1.0 below it, and the input at the bottom, for all 34 m80 encodings of the fixture. The check fails in each of these cases: - **Controls (must mismatch):** - `ffree`: `ffree st(7)` frees ST(0) instead. 34/884 mismatches. - `freetop`: FFREE moves TOP as a pop does, so the status word at depth 1 reads TOP 0. 68/884 mismatches; the literal also matches the depth-1 status read of `ffreep_top`. - **New refusals:** - a reader of C0 after `fld1; ftst; ffree st(1)` (the same sequence without FFREE is accepted); - `ffree st(0)` and `ffree st(1)` on occupied registers. Windows 11 Pro 26100.9168, Intel i7-6820HQ, MSVC 19.52, commit c6571ee9 on 153bb5f6: ``` ctest --preset dev --no-tests=error -R '^cpu_aot_x87_examine$' -V ``` **Result:** - 884 cases over 7 exports matched the model in the native and translated DLLs. The native run gave TOP 0, 7 and 0. The push from depth 7 did not overflow, and the values were unchanged. - Controls normal/unsigned/positive/legacy/ffreep/fftop/ffree/freetop gave 5/136/7/34/33/34/34/68 mismatches. - 15 sequences were refused. - `cpu_aot_x87_add` and `cpu_aot_x87_moves` pass. - The `release` preset gives the same result. Other runs: - Before the rebase, `ctest --preset dev -E '_hardware$'`: 630/630. - Release `-R '^cpu_aot_x87_'`: 21/21. - `tools/form_coverage.py --gaps x64_zen2` no longer lists `ffree`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FFREE ST(i) is accepted when ST(i) is known to be empty at translation
time (i at least the depth). It changes neither occupancy nor TOP, and
C0 to C3 become undefined. FFREE of an occupied register stays outside
the profile: freeing the deepest one keeps TOP while the stack shrinks,
and freeing another leaves a hole.

cpu_aot_x87_examine gains ffree_empty: FFREE of empty registers at depths
0, 1 and 7, a final legal push from depth 7, TOP after each group (FNSTSW
AX, AH masked to TOP) and the stored values. Two controls must mismatch
(FFREE that frees ST(0) instead, and a TOP moved as by a pop), and a
reader of C0 after FFREE must be refused.

The rejection cases "ffree st(1)" at depth 1 in x87_add.py and
x87_moves.py freed an empty register and are accepted now; they use the
occupied ffree st(0) instead and still expect a refusal.

Windows 11 26100, ctest --preset dev --no-tests=error
-R '^cpu_aot_x87_examine$' -V: 884 cases over 7 exports matched in the
native and translated DLLs (TOP 0, 7, 0); controls ffree/freetop 34/68
mismatches; 15 sequences rejected. Release preset passes the same.

Refs #1338

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:05:34 +00:00
github manually merged commit c6571ee981 into gh-pr/1419/base 2026-11-26 23:05:36 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.