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

Open
opened 2026-11-24 08:30:46 +00:00 by legacy-code-archaeologist · 6 comments
legacy-code-archaeologist commented 2026-11-24 08:30:46 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T08:30:46Z.


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?
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6577754041 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-24T08:30:46Z. --- 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?
R0Y00 commented 2026-11-24 09:04:46 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T08:39:02Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7126392829 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7126392829). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T08:39:02Z. --- 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 commented 2026-11-24 10:02:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T09:28:19Z.


#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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127459057 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7127459057). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T09:28:19Z. --- #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.
legacy-code-archaeologist commented 2026-11-24 11:02:58 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T10:07:15Z.


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?

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7128342308 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7128342308). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-24T10:07:15Z. --- 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?
R0Y00 commented 2026-11-24 11:03:00 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T10:25:12Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7128755325 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7128755325). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T10:25:12Z. --- 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 commented 2026-11-24 12:03:18 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T11:42:57Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7130453351 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7130453351). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T11:42:57Z. --- 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.
roansystemlabs commented 2026-11-25 05:04:58 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-25T04:56:39Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7153304825 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1338#issuecomment-7153304825). Originally posted by [roansystemlabs](https://github.com/roansystemlabs) on 2026-11-25T04:56:39Z. --- 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.
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#1331
No description provided.