cpu: accept FFREE ST(0) followed by FINCSTP as a pop in the x87 profile #2211

Merged
roansystemlabs merged 1 commit from upstream-pr/2219 into main 2026-11-25 06:56:44 +00:00
roansystemlabs commented 2026-11-25 05:43:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-25T05:43:16Z.


Refs #1338.

Change

The x87 profile (tools/x87_lower.py) now accepts FFREE ST(0) directly followed by FINCSTP on a nonempty stack, the slice proposed in #1338 (https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7153304825). It does not need a TOP or tag model: FFREE tags ST(0) empty and FINCSTP moves TOP up by one, so the pair pops ST(0) like FFREEP ST(0) without a store. The stack stays contiguous at depth d - 1 and TOP is again 8 minus the depth.

  • Accepted: the pair at depth d >= 1, when no branch in the function targets its FINCSTP. Both halves lower to no code; the depth after the FINCSTP is d - 1. C0, C2 and C3 become undefined and FINCSTP clears C1; the profile treats all four as unknown, so their readers stay refused.
  • Still refused: FFREE of an occupied ST(0) without the FINCSTP right after it (also with another instruction between them), FFREE of an occupied ST(i) for i >= 1 (also when a FINCSTP follows), FINCSTP on its own, FDECSTP, the pair on an empty stack, and a branch to the FINCSTP. A branch to the FFREE of the pair is accepted.
  • Unchanged: the existing fincstp and fdecstp rejection cases in cpu_aot_x87_div, cpu_aot_x87_mul, cpu_aot_x87_moves, cpu_aot_x87_sub, cpu_aot_x87_fnstcw and cpu_aot_x87_examine keep their message (unsupported x87 operation), and so do the ffree st(0) cases of cpu_aot_x87_add and cpu_aot_x87_moves (frees an occupied register).

#1663 and #2098 also change tools/x87_lower.py; this branch merges without conflicts with the current heads of both and with main at f7e08a5e (git merge-tree).

Verification

cpu_aot_x87_examine gains the export ffree_fincstp (tests/cpu/x87_examine.asm):

  • the input, then the pair at depth 1;
  • the input and 1.0, then the pair at depth 2 (drops 1.0);
  • 1.0, five 0.0 and pi, then the pair at depth 8 (drops pi), then one more fld1, the last legal push;
  • the physical TOP after each pair and after the push (fnstsw ax, and ah, 38h, store AH);
  • from depth 8 it stores the pushed 1.0, the 0.0 below it, the 1.0 below the zeros and the input at the bottom, for all 34 m80 encodings of the fixture.

Controls (must mismatch):

  • nopop: the status read after each pair reports TOP as if the pair had not popped. 34/952 mismatches.
  • twopop: the same read reports TOP as if the pair had popped twice. 34/952 mismatches.

A lowering that kept the depth after the pair, or dropped it by two, does not translate the export at all (a later load overflows or a store underflows; checked with a patched lowerer outside this branch), so these two controls change the status reads of the emitted code instead.

New refusals in check_rejections: a reader of C0 after fld1; ftst; ffree st(0); fincstp, a reader of C1 after the pair, the pair on an empty stack, ffree st(1); fincstp, ffree st(0); fnop; fincstp, fincstp; ffree st(0), and a je to the FINCSTP (the same code with the je to the FFREE is accepted).

Windows 11 Home 25H2 (build 26200), Intel Core i7-10700, MSVC 19.44.35230, commit ae25887f on a4ee28a8:

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

Result:

  • 952 cases over 9 exports matched the model in the native and translated DLLs. The native run gave TOP 0, 7, 1 and 0. The push from depth 7 did not overflow, and the stored values matched.
  • Controls normal/unsigned/positive/legacy/ffreep/fftop/ffree/freetop/fstsw/nopop/twopop gave 5/136/7/34/33/68/34/136/34/34/34 mismatches. freetop went from 102 to 136 because ffree_fincstp also reads TOP 7 at depth 1.
  • 2 sequences were accepted and 23 refused.
  • The release preset gives the same result.

Fail-first: with tools/x87_lower.py from a4ee28a8 and this branch's tests, the dev build fails while emitting the fixture (ffree st(0) frees an occupied register at the first pair), and cpu_aot_x87_examine fails because check_rejections refuses the accepted branch case with the same message.

Other runs:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_x87_' and the same with --preset release: 21/21 each.
  • ctest --preset dev --no-tests=error -R '^cpu_' -E '_hardware$' after a full dev build: 0 failures out of 246, of which 3 are capability skips (77) (cpu_aot_switch_fsgsbase_rdpid, cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha).
  • git diff --check is clean.

Not run: the Linux and macOS jobs (this change has no pure-Python check), and the _hardware tests.

Evidence and follow-up

The fixture is source-built MASM in this repository (MIT); no external data.

The occupied-slot cases that need a model of TOP and tags stay open in #1338: FFREE of an occupied register without the FINCSTP (deepest register or a hole), and FINCSTP or FDECSTP on their own.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6598070409 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2219). Originally posted by [roansystemlabs](https://github.com/roansystemlabs) on 2026-11-25T05:43:16Z. --- Refs #1338. ## Change The x87 profile (`tools/x87_lower.py`) now accepts **FFREE ST(0) directly followed by FINCSTP** on a nonempty stack, the slice proposed in #1338 (https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7153304825). It does not need a TOP or tag model: FFREE tags ST(0) empty and FINCSTP moves TOP up by one, so the pair pops ST(0) like FFREEP ST(0) without a store. The stack stays contiguous at depth d - 1 and TOP is again 8 minus the depth. - Accepted: the pair at depth d >= 1, when no branch in the function targets its FINCSTP. Both halves lower to no code; the depth after the FINCSTP is d - 1. C0, C2 and C3 become undefined and FINCSTP clears C1; the profile treats all four as unknown, so their readers stay refused. - Still refused: FFREE of an occupied ST(0) without the FINCSTP right after it (also with another instruction between them), FFREE of an occupied ST(i) for i >= 1 (also when a FINCSTP follows), FINCSTP on its own, FDECSTP, the pair on an empty stack, and a branch to the FINCSTP. A branch to the FFREE of the pair is accepted. - Unchanged: the existing `fincstp` and `fdecstp` rejection cases in `cpu_aot_x87_div`, `cpu_aot_x87_mul`, `cpu_aot_x87_moves`, `cpu_aot_x87_sub`, `cpu_aot_x87_fnstcw` and `cpu_aot_x87_examine` keep their message (`unsupported x87 operation`), and so do the `ffree st(0)` cases of `cpu_aot_x87_add` and `cpu_aot_x87_moves` (`frees an occupied register`). #1663 and #2098 also change `tools/x87_lower.py`; this branch merges without conflicts with the current heads of both and with `main` at `f7e08a5e` (`git merge-tree`). ## Verification `cpu_aot_x87_examine` gains the export `ffree_fincstp` (`tests/cpu/x87_examine.asm`): - the input, then the pair at **depth 1**; - the input and 1.0, then the pair at **depth 2** (drops 1.0); - 1.0, five 0.0 and pi, then the pair at **depth 8** (drops pi), then one more `fld1`, the last legal push; - the physical TOP after each pair and after the push (`fnstsw ax`, `and ah, 38h`, store AH); - from depth 8 it stores the pushed 1.0, the 0.0 below it, the 1.0 below the zeros and the input at the bottom, for all 34 m80 encodings of the fixture. Controls (must mismatch): - `nopop`: the status read after each pair reports TOP as if the pair had not popped. 34/952 mismatches. - `twopop`: the same read reports TOP as if the pair had popped twice. 34/952 mismatches. A lowering that kept the depth after the pair, or dropped it by two, does not translate the export at all (a later load overflows or a store underflows; checked with a patched lowerer outside this branch), so these two controls change the status reads of the emitted code instead. New refusals in `check_rejections`: a reader of C0 after `fld1; ftst; ffree st(0); fincstp`, a reader of C1 after the pair, the pair on an empty stack, `ffree st(1); fincstp`, `ffree st(0); fnop; fincstp`, `fincstp; ffree st(0)`, and a `je` to the FINCSTP (the same code with the `je` to the FFREE is accepted). Windows 11 Home 25H2 (build 26200), Intel Core i7-10700, MSVC 19.44.35230, commit `ae25887f` on `a4ee28a8`: ``` ctest --preset dev --no-tests=error -R '^cpu_aot_x87_examine$' -V ``` Result: - 952 cases over 9 exports matched the model in the native and translated DLLs. The native run gave TOP 0, 7, 1 and 0. The push from depth 7 did not overflow, and the stored values matched. - Controls normal/unsigned/positive/legacy/ffreep/fftop/ffree/freetop/fstsw/nopop/twopop gave 5/136/7/34/33/68/34/136/34/34/34 mismatches. `freetop` went from 102 to 136 because `ffree_fincstp` also reads TOP 7 at depth 1. - 2 sequences were accepted and 23 refused. - The `release` preset gives the same result. Fail-first: with `tools/x87_lower.py` from `a4ee28a8` and this branch's tests, the dev build fails while emitting the fixture (`ffree st(0) frees an occupied register` at the first pair), and `cpu_aot_x87_examine` fails because `check_rejections` refuses the accepted branch case with the same message. Other runs: - `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'` and the same with `--preset release`: 21/21 each. - `ctest --preset dev --no-tests=error -R '^cpu_' -E '_hardware$'` after a full dev build: 0 failures out of 246, of which 3 are capability skips (77) (`cpu_aot_switch_fsgsbase_rdpid`, `cpu_aot_fences_hints_clwb`, `cpu_aot_crypto_sha`). - `git diff --check` is clean. Not run: the Linux and macOS jobs (this change has no pure-Python check), and the `_hardware` tests. ## Evidence and follow-up The fixture is source-built MASM in this repository (MIT); no external data. The occupied-slot cases that need a model of TOP and tags stay open in #1338: FFREE of an occupied register without the FINCSTP (deepest register or a hole), and FINCSTP or FDECSTP on their own. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
tedin7 commented 2026-11-25 06:01:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T05:46:02Z.

Review: COMMENTED


Review of head ae25887f169ff3b6d413f487250c5a47fb9d48dc (comment only, no approval).

Verdict

No P1, no P2. The lowering matches the stated semantics: FFREE ST(0) tags ST(0) empty and FINCSTP moves TOP up by one, so the pair pops one slot with no store. Depth goes d to d-1, and the pair on an empty stack is an underflow.

What I checked

  • Diff scope: 6 files, +120/-19 (git diff --stat origin/main...HEAD). Title is neutral, the PR is not a draft and is MERGEABLE, and there is nothing non-clean-room.
  • _discard_pairs / depths: the pair is detected only when the FINCSTP is the next instruction. The branch guard pairs[target] != i.ip lets the FFREE fall through into its own FINCSTP and refuses any other edge into it. A branch to the FFREE is still accepted.
  • _statement passes paired=True for every FFREE ST(0) and every FINCSTP. This is safe because depths() has already rejected the unpaired forms, and an unpaired FINCSTP still hits the old unsupported x87 operation path.
  • Docs (docs/experiments.md) and the CMakeLists.txt variant list (nopop twopop) match the new test.

Commands run locally (Linux)

  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 408 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean.
  • check_rejections() from tests/cpu/x87_examine.py (needs iced-x86 and pefile): accepted 2 and rejected 23 sequences, passes.

Mutations (reverted with git checkout -- .)

  • Disable the branch-into-pair guard in depths -> AssertionError: translation accepted a branch into ffree st(0), fincstp. Killed.
  • FINCSTP of the pair returns depth instead of depth - 1 -> x87 stack not empty at return. Killed.
  • Let the pair be detected with FNOP in place of FINCSTP -> the ffree st(0), fnop, fincstp case fails on its message. Killed.

Not run

  • The native-vs-translation comparison of ffree_fincstp (952 cases, controls nopop and twopop each needing at least one mismatch) needs the Windows harness. I did not run it here. At review time the CI "Build and test on Windows" and gfx1013 jobs were still pending, so that part is unverified by me.

Nits (non-blocking)

  • tools/x87_lower.py (_statement): the paired argument is True for any FFREE ST(0) or FINCSTP. A one-line comment saying it relies on depths() having already filtered would help later edits. The existing comment mostly says this.
  • The PR notes overlap with #1663 and #2098 in tools/x87_lower.py. A rebase is likely, and X87_OPS and _effect are the hot spots.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6052458765 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2219#pullrequestreview-6052458765). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T05:46:02Z. **Review:** COMMENTED --- Review of head `ae25887f169ff3b6d413f487250c5a47fb9d48dc` (comment only, no approval). ## Verdict No P1, no P2. The lowering matches the stated semantics: FFREE ST(0) tags ST(0) empty and FINCSTP moves TOP up by one, so the pair pops one slot with no store. Depth goes d to d-1, and the pair on an empty stack is an underflow. ## What I checked - Diff scope: 6 files, +120/-19 (`git diff --stat origin/main...HEAD`). Title is neutral, the PR is not a draft and is MERGEABLE, and there is nothing non-clean-room. - `_discard_pairs` / `depths`: the pair is detected only when the FINCSTP is the next instruction. The branch guard `pairs[target] != i.ip` lets the FFREE fall through into its own FINCSTP and refuses any other edge into it. A branch to the FFREE is still accepted. - `_statement` passes `paired=True` for every FFREE ST(0) and every FINCSTP. This is safe because `depths()` has already rejected the unpaired forms, and an unpaired FINCSTP still hits the old `unsupported x87 operation` path. - Docs (`docs/experiments.md`) and the `CMakeLists.txt` variant list (`nopop twopop`) match the new test. ## Commands run locally (Linux) - `python3 tests/docs_links_check.py` -> `Checked 18 Markdown files and 408 links.` - `python3 tests/bidi_controls_check.py` -> `no bidirectional controls.` - `git diff --check origin/main...HEAD` -> clean. - `check_rejections()` from `tests/cpu/x87_examine.py` (needs iced-x86 and pefile): `accepted 2 and rejected 23 sequences`, passes. ## Mutations (reverted with `git checkout -- .`) - Disable the branch-into-pair guard in `depths` -> `AssertionError: translation accepted a branch into ffree st(0), fincstp`. Killed. - FINCSTP of the pair returns `depth` instead of `depth - 1` -> `x87 stack not empty at return`. Killed. - Let the pair be detected with FNOP in place of FINCSTP -> the `ffree st(0), fnop, fincstp` case fails on its message. Killed. ## Not run - The native-vs-translation comparison of `ffree_fincstp` (952 cases, controls `nopop` and `twopop` each needing at least one mismatch) needs the Windows harness. I did not run it here. At review time the CI "Build and test on Windows" and gfx1013 jobs were still pending, so that part is unverified by me. ## Nits (non-blocking) - `tools/x87_lower.py` (`_statement`): the `paired` argument is `True` for any FFREE ST(0) or FINCSTP. A one-line comment saying it relies on `depths()` having already filtered would help later edits. The existing comment mostly says this. - The PR notes overlap with #1663 and #2098 in `tools/x87_lower.py`. A rebase is likely, and `X87_OPS` and `_effect` are the hot spots. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 07:01:03 +00:00
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!2211
No description provided.