cpu: FFREE and FFREEP in the x87 profile where the stack stays contiguous #1338

Open
opened 2026-11-26 22:59:16 +00:00 by github-bot · 6 comments
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-24 08:30 UTC · original

The x87 profile (tools/x87_lower.py) tracks a contiguous stack depth. It refuses FFREE, FFREEP, FINCSTP and FDECSTP as outside the profile. form_coverage --gaps x64_zen2 on e3193607 lists all four as unobserved. I'd like to take a small slice that keeps the contiguous-depth model.

Proposed acceptance, with depth d before the instruction (all others refused):

  • FFREEP ST(0) with d >= 1: tags ST(0) empty and pops. The depth becomes d - 1, the same as FSTP ST(0) without storing anything. At d = 0 it is refused, because the pop would change TOP while the depth stays 0.
  • FFREE ST(i) with i = d - 1 (the deepest occupied register): the depth becomes d - 1 and TOP stays the same. The remaining registers stay contiguous.
  • FFREE ST(i) with i >= d (an already empty register): no effect.
  • Refused:
    • FFREE ST(i) with i < d - 1, which would leave a hole the depth model cannot represent;
    • FFREEP ST(i) with i > 0;
    • FINCSTP and FDECSTP, which move TOP without changing tags.
  • Status word: C0 to C3 become undefined, as the SDM states for FFREE. In a function with FNSTSW, check_flags then keeps refusing readers of those bits. TOP stays known from the depth.

Fixture plan (source-built MASM, native vs translated, plus /O2):

  • Fill all eight registers, run ffree st(7), then load one more value. Natively this must not overflow (the result is not the indefinite NaN). It shows the freed slot is really free.
  • ffreep st(0) drops ST(0); the values below it are stored and compared.
  • FNSTSW AX after each form, with TOP compared natively.
  • Refusals: ffree st(1) at depth 3 (hole), ffreep st(0) at depth 0, ffreep st(2), fincstp, fdecstp.
  • Controls that must fail: one where FFREE does not lower the depth (the extra FLD then overflows the translated stack differently), and one where FFREEP does not pop.

Two questions:

  1. Is this scope useful, or would you rather wait for the exact-model work in #406?
  2. Should FFREE ST(i) with i >= d be accepted as a no-op, or refused to keep the profile narrow?
**@⁠legacy-code-archaeologist** on GitHub · 2026-11-24 08:30 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338) The x87 profile (`tools/x87_lower.py`) tracks a contiguous stack depth. It refuses FFREE, FFREEP, FINCSTP and FDECSTP as outside the profile. `form_coverage --gaps x64_zen2` on e3193607 lists all four as unobserved. I'd like to take a small slice that keeps the contiguous-depth model. **Proposed acceptance**, with depth `d` before the instruction (all others refused): - **FFREEP ST(0)** with `d >= 1`: tags ST(0) empty and pops. The depth becomes `d - 1`, the same as FSTP ST(0) without storing anything. At `d = 0` it is refused, because the pop would change TOP while the depth stays 0. - **FFREE ST(i)** with `i = d - 1` (the deepest occupied register): the depth becomes `d - 1` and TOP stays the same. The remaining registers stay contiguous. - **FFREE ST(i)** with `i >= d` (an already empty register): no effect. - **Refused:** - FFREE ST(i) with `i < d - 1`, which would leave a hole the depth model cannot represent; - FFREEP ST(i) with `i > 0`; - FINCSTP and FDECSTP, which move TOP without changing tags. - **Status word:** C0 to C3 become undefined, as the SDM states for FFREE. In a function with FNSTSW, `check_flags` then keeps refusing readers of those bits. TOP stays known from the depth. **Fixture plan** (source-built MASM, native vs translated, plus `/O2`): - Fill all eight registers, run `ffree st(7)`, then load one more value. Natively this must not overflow (the result is not the indefinite NaN). It shows the freed slot is really free. - `ffreep st(0)` drops ST(0); the values below it are stored and compared. - FNSTSW AX after each form, with TOP compared natively. - **Refusals:** `ffree st(1)` at depth 3 (hole), `ffreep st(0)` at depth 0, `ffreep st(2)`, `fincstp`, `fdecstp`. - **Controls that must fail:** one where FFREE does not lower the depth (the extra FLD then overflows the translated stack differently), and one where FFREEP does not pop. Two questions: 1. Is this scope useful, or would you rather wait for the exact-model work in #406? 2. Should FFREE ST(i) with `i >= d` be accepted as a no-op, or refused to keep the profile narrow?
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:59:16 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 08:39 UTC · original

Please start with FFREEP ST(0) on a known nonempty stack. An explicit refusal on empty depth is appropriate for this profile. Compare the remaining values and physical TOP and keep readers of undefined condition bits rejected. Leave FFREE for a follow-up. Freeing the deepest slot changes occupancy without moving TOP, so a model that derives TOP from depth would report the wrong status word even when its register values look right. Freeing an already empty slot can be considered with that follow-up and should preserve TOP and test any observable condition bits. A full-stack refill alone would miss this error.

**@⁠R0Y00** on GitHub · 2026-11-24 08:39 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7126392829) Please start with FFREEP ST(0) on a known nonempty stack. An explicit refusal on empty depth is appropriate for this profile. Compare the remaining values and physical TOP and keep readers of undefined condition bits rejected. Leave FFREE for a follow-up. Freeing the deepest slot changes occupancy without moving TOP, so a model that derives TOP from depth would report the wrong status word even when its register values look right. Freeing an already empty slot can be considered with that follow-up and should preserve TOP and test any observable condition bits. A full-stack refill alone would miss this error.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 09:28 UTC · original

#1361 is on main at b9c9f624. The Windows Release native/AOT fixture passed with the FFREEP and physical-TOP mutation controls. This covers FFREEP ST(0) on a known nonempty stack. Keeping this issue open for FFREE, where freeing a slot does not move TOP.

**@⁠R0Y00** on GitHub · 2026-11-24 09:28 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7127459057) #1361 is on main at b9c9f624. The Windows Release native/AOT fixture passed with the FFREEP and physical-TOP mutation controls. This covers FFREEP ST(0) on a known nonempty stack. Keeping this issue open for FFREE, where freeing a slot does not move TOP.
Author
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-24 10:07 UTC · original

Proposal for the FFREE follow-up. The two FFREE cases differ in how much of the model they need.

1. FFREE ST(i) with i >= depth (an already empty register). Occupancy and TOP stay as they are, so the depth model stays exact. I'd accept this now:

  • Effect: no stack change. C0 to C3 become undefined (SDM: FFREE leaves them undefined), so readers stay refused, C1 included.
  • Fixture: run ffree st(i) for every empty i at depths 0, 1 and 7. After each one, record the physical TOP (fnstsw ax; and ah, 38h) and the values still on the stack. At depth 7, ffree st(7) is followed by one more fld, which must not overflow natively (the loaded value, not the indefinite NaN).
  • Controls that must mismatch: one where FFREE pops, as if it were FFREEP, and one where it frees ST(0).

2. FFREE ST(depth - 1) (the deepest occupied register). TOP stays and the occupied range shrinks from the bottom. The profile would then need a second number next to the depth, the count of registers freed below the stack, so that TOP = (8 - depth - freed) mod 8 and pushes and pops keep TOP right.

The catch is the return. With freed > 0 and an empty stack, TOP is not 0. The profile requires TOP 0 at the entry and the return, and FINCSTP and FDECSTP are outside it. So this case could only be accepted together with FINCSTP/FDECSTP, or with a refusal at the return, which would leave almost nothing usable. I'd keep it refused for now, with a refusal test, and leave it to whoever takes FINCSTP and FDECSTP.

Refused in this slice:

  • FFREE ST(i) with i < depth (a hole, or the deepest register);
  • FINCSTP and FDECSTP.

Should I build case 1 like this?

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-24 10:07 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7128342308) Proposal for the FFREE follow-up. The two FFREE cases differ in how much of the model they need. **1. FFREE ST(i) with `i >= depth` (an already empty register).** Occupancy and TOP stay as they are, so the depth model stays exact. I'd accept this now: - Effect: no stack change. C0 to C3 become undefined (SDM: FFREE leaves them undefined), so readers stay refused, C1 included. - Fixture: run `ffree st(i)` for every empty `i` at depths 0, 1 and 7. After each one, record the physical TOP (`fnstsw ax; and ah, 38h`) and the values still on the stack. At depth 7, `ffree st(7)` is followed by one more `fld`, which must not overflow natively (the loaded value, not the indefinite NaN). - Controls that must mismatch: one where FFREE pops, as if it were FFREEP, and one where it frees ST(0). **2. FFREE ST(depth - 1) (the deepest occupied register).** TOP stays and the occupied range shrinks from the bottom. The profile would then need a second number next to the depth, the count of registers freed below the stack, so that `TOP = (8 - depth - freed) mod 8` and pushes and pops keep TOP right. The catch is the return. With `freed > 0` and an empty stack, TOP is not 0. The profile requires TOP 0 at the entry and the return, and FINCSTP and FDECSTP are outside it. So this case could only be accepted together with FINCSTP/FDECSTP, or with a refusal at the return, which would leave almost nothing usable. I'd keep it refused for now, with a refusal test, and leave it to whoever takes FINCSTP and FDECSTP. **Refused in this slice:** - FFREE ST(i) with `i < depth` (a hole, or the deepest register); - FINCSTP and FDECSTP. Should I build case 1 like this?
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 10:25 UTC · original

Please take the already-empty-slot case as the next slice. Require that the slot is statically known empty, preserve TOP and all occupied slots, and treat C0–C3 as unknown. Compare native and AOT at depths 0, 1 and 7, including the final legal push from depth 7 and the pop/wrong-slot controls. Keep occupied-slot FFREE and changes to TOP outside this patch. The issue stays open for those cases.

**@⁠R0Y00** on GitHub · 2026-11-24 10:25 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7128755325) Please take the already-empty-slot case as the next slice. Require that the slot is statically known empty, preserve TOP and all occupied slots, and treat C0–C3 as unknown. Compare native and AOT at depths 0, 1 and 7, including the final legal push from depth 7 and the pop/wrong-slot controls. Keep occupied-slot FFREE and changes to TOP outside this patch. The issue stays open for those cases.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 11:42 UTC · original

The already-empty FFREE slice landed in #1419 at 81446f94. Windows Release passed the native/AOT examination, add and move checks (3/3). The examination fixture tests depths 0, 1 and 7 and checks physical TOP, the last legal push and two mutation controls. Keeping this issue open for occupied-slot behavior, which needs a model of TOP and tags.

**@⁠R0Y00** on GitHub · 2026-11-24 11:42 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7130453351) The already-empty FFREE slice landed in #1419 at 81446f94. Windows Release passed the native/AOT examination, add and move checks (3/3). The examination fixture tests depths 0, 1 and 7 and checks physical TOP, the last legal push and two mutation controls. Keeping this issue open for occupied-slot behavior, which needs a model of TOP and tags.
Author
Owner

@⁠roansystemlabs on GitHub · 2026-11-25 04:56 UTC · original

I'd like to take the next occupied-slot piece of this issue, scoped so it does not need a TOP or tag model yet: FFREE ST(0) immediately followed by FINCSTP in the same block, the classic discard-top idiom.

Scope:

  • Accepted: the pair FFREE ST(0); FINCSTP at depth d >= 1, with no branch target between them. ST(0) is tagged empty and TOP moves up by one, so the stack stays contiguous at depth d - 1 and TOP is again 8 minus the depth, as after FSTP ST(0) without a store. C0 to C3 become unknown, so their readers stay rejected.
  • Still refused by name: FFREE of an occupied ST(i) without the FINCSTP, FFREE of an occupied ST(i) with i >= 1, FINCSTP or FDECSTP on their own, and a branch target between the two instructions.

Evidence: a source-built native/AOT comparison at depths 1, 2 and 8 that checks the remaining values and the physical TOP (fnstsw ax), plus the last legal push after the pair at depth 8; mutation controls that treat the pair as no pop or as two pops must fail; negative controls for FFREE ST(0) without FINCSTP, FINCSTP alone and a jump into the pair. Windows 11, MSVC, Release and Debug.

If you would rather have the full TOP and tag model here, or if someone already has this in progress, I'll step back and review.

AI-assisted.

**@⁠roansystemlabs** on GitHub · 2026-11-25 04:56 UTC · [original](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7153304825) I'd like to take the next occupied-slot piece of this issue, scoped so it does not need a TOP or tag model yet: FFREE ST(0) immediately followed by FINCSTP in the same block, the classic discard-top idiom. Scope: - Accepted: the pair FFREE ST(0); FINCSTP at depth d >= 1, with no branch target between them. ST(0) is tagged empty and TOP moves up by one, so the stack stays contiguous at depth d - 1 and TOP is again 8 minus the depth, as after FSTP ST(0) without a store. C0 to C3 become unknown, so their readers stay rejected. - Still refused by name: FFREE of an occupied ST(i) without the FINCSTP, FFREE of an occupied ST(i) with i >= 1, FINCSTP or FDECSTP on their own, and a branch target between the two instructions. Evidence: a source-built native/AOT comparison at depths 1, 2 and 8 that checks the remaining values and the physical TOP (fnstsw ax), plus the last legal push after the pair at depth 8; mutation controls that treat the pair as no pop or as two pops must fail; negative controls for FFREE ST(0) without FINCSTP, FINCSTP alone and a jump into the pair. Windows 11, MSVC, Release and Debug. If you would rather have the full TOP and tag model here, or if someone already has this in progress, I'll step back and review. AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/R0Y00--Xbox2Windows#1338
No description provided.