cpu: lower the ZF and CF forms of FCMOVcc in the x87 profile #470

Merged
denn0x merged 4 commits from upstream-pr/476 into main 2026-11-22 07:15:11 +00:00
denn0x commented 2026-11-22 05:55:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T05:55:30Z.


Change

Refs #406. This follows #471 (fscale). It adds the six conditional moves that read only ZF and CF to the opt-in x87 profile in lower(..., x87=True): fcmovb, fcmove, fcmovbe, fcmovnb, fcmovne and fcmovnbe with st, st(i).

Semantics. Each form copies st(i) into st(0) when its condition holds and leaves the stack depth unchanged.

Form Moves when
fcmovb CF
fcmove ZF
fcmovbe CF or ZF
fcmovnb not CF
fcmovne not ZF
fcmovnbe neither CF nor ZF
  • Flags: read from the integer lowering's carry and zero, so they come from integer instructions or from fcomi (#456).
  • Flag precondition: in a function with a move, each status flag that any instruction reads must be written by an earlier instruction on every path from the entry. lower() starts both flags as false and has no input for the caller's flags, so a function that could observe them is rejected (see below).
  • The copy: moves the whole 80-bit encoding, as fld st(i) does. A signaling NaN stays signaling, and unsupported encodings are copied unchanged. The native runs confirm this (control quiet below).

Out of scope: fcmovu and fcmovnu read PF, which the integer lowering does not model, so they stay rejected.

Lowering (tools/x87_lower.py). if (carry || zero) x87_slot[top] = x87_slot[top - k]; with the form's condition. An st(i) at or beyond the stack depth is an underflow. No helper is added to tools/x87_soft.h.

Defined flags. defined_flags() is a forward pass over the function that computes which of the six status flags are defined before each instruction. In a function with a move, translation rejects every instruction that reads a flag that is not defined there. The check covers every reader, not only the moves, because adc, sbb, rcl, rcr and cmc pass an undefined CF on into the flags and registers they write. A later writer of ZF, such as test, could then carry the caller's CF to a move. The pass tracks all six status flags, so a reader of OF or another flag that the integer lowering may model later is covered too. DF is not tracked, because the Windows x64 calling convention clears it.

  • Writes come from iced's flag information: written, set or cleared, minus the flags it reports undefined (ZF after imul, for example).
  • A join keeps only the flags defined on all of its incoming paths.
  • A call leaves every status flag undefined.
  • A CL-count shift and a rep compare may change nothing, so they keep only the flags already defined.
  • An 8- or 16-bit shl, shr or sar by the operand width or more leaves CF undefined, and a 16-bit shld or shrd by more than 16 leaves both undefined.

The analysis runs only for functions that contain a conditional move. No other lowering changes, so the integer readers of functions without a move keep their current behavior.

Fixture

tests/cpu/x87_fcmov.asm and .def are generated by tests/cpu/x87_fcmov.py asm, and emit checks that every decoded x87 instruction is the form its sequence names. The .asm registers its test on its first line (; coverage: source cpu_aot_x87_fcmov). The 16 exports cover:

  • each form after mov eax, [rcx+48]; cmp eax, [rcx+52], moving st(1);
  • fcmovb moving st(2) while st(1) must stay unchanged;
  • the maximum (fcomi; fcmovb) and minimum (fcomi; fcmovnbe) of two values, as compilers emit them;
  • two moves under the same flags (fcmovne st(1), then fcmove st(2));
  • fcmove st, st(0);
  • fcmovnb moving st(7) at depth 8, with all eight values stored as m64;
  • readers of the CF of cmp before the move: mov eax, 0FFFFFFFFh; adc eax, 0; fcmove and mov eax, 0; sbb eax, 0; fcmove (ZF from that CF), cmc; fcmovb, and mov eax, 0; rcl eax, 1; test eax, eax; fcmovne (that CF carried through EAX).

Every export stores every value, so a translation that moves the wrong register or changes another slot mismatches.

Seven more exports are native only. Each moves on the caller's flags:

  • fcmovb and fcmove right after two loads, with no flag writer;
  • mov eax, 0FFFFFFFFh; adc eax, 0; fcmove and mov eax, 0; sbb eax, 0; fcmove, whose ZF comes from the caller's CF;
  • adc eax, 0 or rcl eax, 1 from EAX = 0, then test eax, eax; fcmovne, which carry the caller's CF through EAX to a defined ZF;
  • cmc; fcmovb, which inverts the caller's CF.

They run through x87_call_flags, a new entry of tests/cpu/x87_harness.asm. It is x87_call with the caller's CF and ZF loaded by sahf just before the call, so the native code sees the flags its caller set. For 40 value pairs and all four (ZF, CF) pairs, each export followed the caller's flag in all 1120 runs. Translation must reject each export at its first reader of the undefined flag: the move, or the adc, sbb, rcl or cmc. Before this rule, translation accepted the five indirect exports.

Model. The model sets ZF and CF from the dwords or from the comparison model of cpu_aot_x87_compare, follows EAX, ZF and CF through mov, adc, sbb, rcl, test and cmc, evaluates each condition itself and copies raw encodings. It shares no code with tools/x87_soft.h. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.

Cases. 13290 cases:

  • for the two-value cmp exports, including the four readers of CF, every seventh pair of the 34 special encodings with dword pairs that compare below, equal and above, and random values with dword pairs that include the signed and unsigned edges;
  • for the three-value exports (fcmovb_st2, fcmovne_fcmove), 450 random triples of the special encodings, each with one of the below, equal and above dword pairs;
  • for the fcomi exports, every pair of the special encodings and random values;
  • for the depth-8 export, random m64 values; the compared dwords are the halves of the seventh.

In 5807 of the cases a move changes the output block.

Negative controls. Eight mutated translations must each mismatch:

Control Mutation Mismatches
invert fcmovb moves when CF is clear 3448/13290
beonly fcmovbe reads only CF 185/13290
nbeor fcmovnbe moves when either flag is clear 960/13290
neverne fcmovne never moves 1055/13290
always every move ignores its condition 5851/13290
quiet the copy quiets a signaling NaN 276/13290
cmcdrop cmc is dropped 696/13290
nocarryin adc and sbb ignore the incoming CF 510/13290

Rejections. Translation rejects 14 sequences with the expected diagnostic:

  • fcmovu and fcmovnu;
  • fcmovb st, st(1) with one value, fcmove st, st(2) with two values, and fcmovnbe on an empty stack;
  • a move whose flag is undefined on some path. The cases are a move before any flag writer, a join with one undefined predecessor (jrcxz around the cmp), fcmovbe after stc (ZF undefined), fcmovnb after a call, fcmove after imul (ZF undefined), fcmovb after shl eax, cl with no earlier writer, fcmovb after shl ax, 16, and a cmc or a seto before the cmp that defines the move's flags. The integer lowering would also reject seto, but the flag rule comes first.

Eight sequences whose flags are written on every path must still translate. Four are moves: after clc, after a join where both paths write ZF, after xor eax, eax, and after fcomi. The other four are readers of CF after an explicit producer: clc before adc, stc before sbb, cmp before cmc, and clc before rcl and test.

Changes to existing tests: the shared native harness tests/cpu/x87_harness.asm gains the x87_call_flags entry. The other entries are unchanged.

Verification

Tested at 5c0151e, the merge of main at 933cbad (after #484) into 4d7bc7c. The merge brought in #287's BMI2 lowering in tools/ffx_version_query.py without conflicts. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).

cmake --build --preset dev --target cpu_x87_fcmov_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_fcmov$' -V
Test timeout computed to be: 10000000
13290 cases over 16 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
cases in which a conditional move changed the output block: 5807 of 13290
processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
x87_call kept a live caller ST(0) value and control word around all 16 native and translated exports
native fcmovb_entry, fcmove_entry, adc_entry, sbb_entry, adc_test_entry, rcl_test_entry, cmc_entry followed the caller's CF or ZF (set by SAHF before the call) in all 1120 runs over 40 value pairs and the four flag pairs; translation rejects each at its first reader of the flag, which no instruction defines on every path from the entry: fcmovb_entry at fcmovb (CF); fcmove_entry at fcmove (ZF); adc_entry at adc (CF); sbb_entry at sbb (CF); adc_test_entry at adc (CF); rcl_test_entry at rcl (CF); cmc_entry at cmc (CF)
control invert: 3448/13290 mismatches
control beonly: 185/13290 mismatches
control nbeor: 960/13290 mismatches
control neverne: 1055/13290 mismatches
control always: 5851/13290 mismatches
control quiet: 276/13290 mismatches
control cmcdrop: 696/13290 mismatches
control nocarryin: 510/13290 mismatches
rejected 14 sequences outside the profile: fcmovu st, st(1); fcmovnu st, st(1); fcmovb st, st(1) with one value; fcmove st, st(2) with two values; fcmovnbe on an empty stack; fcmovb before any flag writer; fcmove after a join with one undefined predecessor; fcmovbe after stc, which leaves ZF undefined; fcmovnb after a call; fcmove after imul, which leaves ZF undefined; fcmovb after shl eax, cl with no earlier writer; fcmovb after shl ax, 16; cmc before a cmp and fcmovb; seto before a cmp and fcmovb
translated 8 sequences whose flags are written on every path: fcmovb after clc; fcmove after a join where both paths write ZF; fcmove after xor eax, eax; fcmovnbe after fcomi; fcmove after clc and adc; fcmove after stc and sbb; fcmovb after cmp and cmc; fcmovne after clc, rcl and test
  • x87 regressions: all thirteen x87 tests pass at 5c0151e (ctest --preset dev --no-tests=error -R '^cpu_aot_x87_').
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml at 5c0151e gives 205 passed, 1 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 208. The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, which fails the same way on main. This change touches no GPU code.
  • Pure-Python checks: tests/form_coverage_check.py, tests/docs_links_check.py and tests/bidi_controls_check.py pass at 5c0151e.
  • Fork CI: the Windows build-and-test, Windows /O2 build-and-test, Linux Python and gfx1013 LLVM jobs passed at 5c0151e in the fork's Windows checks run 44997062998. Both Windows jobs ran and passed all thirteen x87 tests (173 passed and 2 capability skips, bvh_wave64_warp and wave_shuffle_warp, out of 175 in each).

Coverage. python tools/form_coverage.py out/build/dev/test-results.xml gives x64 180 of 948 forms, and 173 without cpu_aot_x87_fcmov. The added forms are the six conditional moves and sbb, which no other fixture covers and which fcmove_sbb translates. These totals are at 5c0151e and include every fixture on main at 933cbad.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part.
  • fcmovu and fcmovnu, which wait for PF in the integer lowering.

Overlap

I trial-merged this branch with the 84 other open PRs. Each one conflicts in the same files against main and against this branch: the 11 that merge cleanly into main also merge cleanly here, and this branch adds no conflict to the others. The changes stay inside tools/x87_lower.py, the new fixture, the new x87_call_flags entry of the native harness, a CMake block after the cpu_aot_x87_scale test, and the x87 section of docs/experiments.md. If #335 adds OF to the integer lowering, the moves are unaffected because none of them reads it, and the flag rule already covers a reader of OF in a function with a move.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the six forms, the signaling-NaN measurement, the controls and the reproduction command.

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 (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6539064532 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T05:55:30Z. --- ## Change Refs #406. This follows #471 (`fscale`). It adds the six conditional moves that read only ZF and CF to the opt-in x87 profile in `lower(..., x87=True)`: `fcmovb`, `fcmove`, `fcmovbe`, `fcmovnb`, `fcmovne` and `fcmovnbe` with `st, st(i)`. **Semantics.** Each form copies `st(i)` into `st(0)` when its condition holds and leaves the stack depth unchanged. | Form | Moves when | |---|---| | `fcmovb` | CF | | `fcmove` | ZF | | `fcmovbe` | CF or ZF | | `fcmovnb` | not CF | | `fcmovne` | not ZF | | `fcmovnbe` | neither CF nor ZF | - **Flags:** read from the integer lowering's `carry` and `zero`, so they come from integer instructions or from `fcomi` (#456). - **Flag precondition:** in a function with a move, each status flag that any instruction reads must be written by an earlier instruction on every path from the entry. `lower()` starts both flags as false and has no input for the caller's flags, so a function that could observe them is rejected (see below). - **The copy:** moves the whole 80-bit encoding, as `fld st(i)` does. A signaling NaN stays signaling, and unsupported encodings are copied unchanged. The native runs confirm this (control `quiet` below). **Out of scope:** `fcmovu` and `fcmovnu` read PF, which the integer lowering does not model, so they stay rejected. **Lowering (`tools/x87_lower.py`).** `if (carry || zero) x87_slot[top] = x87_slot[top - k];` with the form's condition. An `st(i)` at or beyond the stack depth is an underflow. No helper is added to `tools/x87_soft.h`. **Defined flags.** `defined_flags()` is a forward pass over the function that computes which of the six status flags are defined before each instruction. In a function with a move, translation rejects every instruction that reads a flag that is not defined there. The check covers every reader, not only the moves, because `adc`, `sbb`, `rcl`, `rcr` and `cmc` pass an undefined CF on into the flags and registers they write. A later writer of ZF, such as `test`, could then carry the caller's CF to a move. The pass tracks all six status flags, so a reader of OF or another flag that the integer lowering may model later is covered too. `DF` is not tracked, because the Windows x64 calling convention clears it. - Writes come from iced's flag information: written, set or cleared, minus the flags it reports undefined (ZF after `imul`, for example). - A join keeps only the flags defined on all of its incoming paths. - A call leaves every status flag undefined. - A CL-count shift and a `rep` compare may change nothing, so they keep only the flags already defined. - An 8- or 16-bit `shl`, `shr` or `sar` by the operand width or more leaves CF undefined, and a 16-bit `shld` or `shrd` by more than 16 leaves both undefined. The analysis runs only for functions that contain a conditional move. No other lowering changes, so the integer readers of functions without a move keep their current behavior. ## Fixture `tests/cpu/x87_fcmov.asm` and `.def` are generated by `tests/cpu/x87_fcmov.py asm`, and `emit` checks that every decoded x87 instruction is the form its sequence names. The `.asm` registers its test on its first line (`; coverage: source cpu_aot_x87_fcmov`). The 16 exports cover: - each form after `mov eax, [rcx+48]; cmp eax, [rcx+52]`, moving `st(1)`; - `fcmovb` moving `st(2)` while `st(1)` must stay unchanged; - the maximum (`fcomi; fcmovb`) and minimum (`fcomi; fcmovnbe`) of two values, as compilers emit them; - two moves under the same flags (`fcmovne st(1)`, then `fcmove st(2)`); - `fcmove st, st(0)`; - `fcmovnb` moving `st(7)` at depth 8, with all eight values stored as m64; - readers of the CF of `cmp` before the move: `mov eax, 0FFFFFFFFh; adc eax, 0; fcmove` and `mov eax, 0; sbb eax, 0; fcmove` (ZF from that CF), `cmc; fcmovb`, and `mov eax, 0; rcl eax, 1; test eax, eax; fcmovne` (that CF carried through EAX). Every export stores every value, so a translation that moves the wrong register or changes another slot mismatches. Seven more exports are native only. Each moves on the caller's flags: - `fcmovb` and `fcmove` right after two loads, with no flag writer; - `mov eax, 0FFFFFFFFh; adc eax, 0; fcmove` and `mov eax, 0; sbb eax, 0; fcmove`, whose ZF comes from the caller's CF; - `adc eax, 0` or `rcl eax, 1` from EAX = 0, then `test eax, eax; fcmovne`, which carry the caller's CF through EAX to a defined ZF; - `cmc; fcmovb`, which inverts the caller's CF. They run through `x87_call_flags`, a new entry of `tests/cpu/x87_harness.asm`. It is `x87_call` with the caller's CF and ZF loaded by `sahf` just before the call, so the native code sees the flags its caller set. For 40 value pairs and all four (ZF, CF) pairs, each export followed the caller's flag in all 1120 runs. Translation must reject each export at its first reader of the undefined flag: the move, or the `adc`, `sbb`, `rcl` or `cmc`. Before this rule, translation accepted the five indirect exports. **Model.** The model sets ZF and CF from the dwords or from the comparison model of `cpu_aot_x87_compare`, follows EAX, ZF and CF through `mov`, `adc`, `sbb`, `rcl`, `test` and `cmc`, evaluates each condition itself and copies raw encodings. It shares no code with `tools/x87_soft.h`. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. **Cases.** 13290 cases: - for the two-value `cmp` exports, including the four readers of CF, every seventh pair of the 34 special encodings with dword pairs that compare below, equal and above, and random values with dword pairs that include the signed and unsigned edges; - for the three-value exports (`fcmovb_st2`, `fcmovne_fcmove`), 450 random triples of the special encodings, each with one of the below, equal and above dword pairs; - for the `fcomi` exports, every pair of the special encodings and random values; - for the depth-8 export, random m64 values; the compared dwords are the halves of the seventh. In 5807 of the cases a move changes the output block. **Negative controls.** Eight mutated translations must each mismatch: | Control | Mutation | Mismatches | |---|---|---| | `invert` | `fcmovb` moves when CF is clear | 3448/13290 | | `beonly` | `fcmovbe` reads only CF | 185/13290 | | `nbeor` | `fcmovnbe` moves when either flag is clear | 960/13290 | | `neverne` | `fcmovne` never moves | 1055/13290 | | `always` | every move ignores its condition | 5851/13290 | | `quiet` | the copy quiets a signaling NaN | 276/13290 | | `cmcdrop` | `cmc` is dropped | 696/13290 | | `nocarryin` | `adc` and `sbb` ignore the incoming CF | 510/13290 | **Rejections.** Translation rejects 14 sequences with the expected diagnostic: - `fcmovu` and `fcmovnu`; - `fcmovb st, st(1)` with one value, `fcmove st, st(2)` with two values, and `fcmovnbe` on an empty stack; - a move whose flag is undefined on some path. The cases are a move before any flag writer, a join with one undefined predecessor (`jrcxz` around the `cmp`), `fcmovbe` after `stc` (ZF undefined), `fcmovnb` after a call, `fcmove` after `imul` (ZF undefined), `fcmovb` after `shl eax, cl` with no earlier writer, `fcmovb` after `shl ax, 16`, and a `cmc` or a `seto` before the `cmp` that defines the move's flags. The integer lowering would also reject `seto`, but the flag rule comes first. Eight sequences whose flags are written on every path must still translate. Four are moves: after `clc`, after a join where both paths write ZF, after `xor eax, eax`, and after `fcomi`. The other four are readers of CF after an explicit producer: `clc` before `adc`, `stc` before `sbb`, `cmp` before `cmc`, and `clc` before `rcl` and `test`. **Changes to existing tests:** the shared native harness `tests/cpu/x87_harness.asm` gains the `x87_call_flags` entry. The other entries are unchanged. ## Verification Tested at `5c0151e`, the merge of `main` at `933cbad` (after #484) into `4d7bc7c`. The merge brought in #287's BMI2 lowering in `tools/ffx_version_query.py` without conflicts. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (`AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD`). ```powershell cmake --build --preset dev --target cpu_x87_fcmov_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_x87_fcmov$' -V ``` ```text Test timeout computed to be: 10000000 13290 cases over 16 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack cases in which a conditional move changed the output block: 5807 of 13290 processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD x87_call kept a live caller ST(0) value and control word around all 16 native and translated exports native fcmovb_entry, fcmove_entry, adc_entry, sbb_entry, adc_test_entry, rcl_test_entry, cmc_entry followed the caller's CF or ZF (set by SAHF before the call) in all 1120 runs over 40 value pairs and the four flag pairs; translation rejects each at its first reader of the flag, which no instruction defines on every path from the entry: fcmovb_entry at fcmovb (CF); fcmove_entry at fcmove (ZF); adc_entry at adc (CF); sbb_entry at sbb (CF); adc_test_entry at adc (CF); rcl_test_entry at rcl (CF); cmc_entry at cmc (CF) control invert: 3448/13290 mismatches control beonly: 185/13290 mismatches control nbeor: 960/13290 mismatches control neverne: 1055/13290 mismatches control always: 5851/13290 mismatches control quiet: 276/13290 mismatches control cmcdrop: 696/13290 mismatches control nocarryin: 510/13290 mismatches rejected 14 sequences outside the profile: fcmovu st, st(1); fcmovnu st, st(1); fcmovb st, st(1) with one value; fcmove st, st(2) with two values; fcmovnbe on an empty stack; fcmovb before any flag writer; fcmove after a join with one undefined predecessor; fcmovbe after stc, which leaves ZF undefined; fcmovnb after a call; fcmove after imul, which leaves ZF undefined; fcmovb after shl eax, cl with no earlier writer; fcmovb after shl ax, 16; cmc before a cmp and fcmovb; seto before a cmp and fcmovb translated 8 sequences whose flags are written on every path: fcmovb after clc; fcmove after a join where both paths write ZF; fcmove after xor eax, eax; fcmovnbe after fcomi; fcmove after clc and adc; fcmove after stc and sbb; fcmovb after cmp and cmc; fcmovne after clc, rcl and test ``` - **x87 regressions:** all thirteen x87 tests pass at `5c0151e` (`ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'`). - **Full suite:** `ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xml` at `5c0151e` gives 205 passed, 1 failed and 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) out of 208. The run includes the GPU tests. The failure is `gpu_alias_lookup_writes_hardware`, the RDNA 4 array-atomics probe recorded in #369, which fails the same way on `main`. This change touches no GPU code. - **Pure-Python checks:** `tests/form_coverage_check.py`, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass at `5c0151e`. - **Fork CI:** the Windows build-and-test, Windows `/O2` build-and-test, Linux Python and gfx1013 LLVM jobs passed at `5c0151e` in the fork's `Windows checks` run [44997062998](https://github.com/denn0x/Xbox2Windows/actions/runs/44997062998). Both Windows jobs ran and passed all thirteen x87 tests (173 passed and 2 capability skips, `bvh_wave64_warp` and `wave_shuffle_warp`, out of 175 in each). **Coverage.** `python tools/form_coverage.py out/build/dev/test-results.xml` gives x64 **180 of 948** forms, and 173 without `cpu_aot_x87_fcmov`. The added forms are the six conditional moves and `sbb`, which no other fixture covers and which `fcmove_sbb` translates. These totals are at `5c0151e` and include every fixture on `main` at `933cbad`. **Not measured:** - Intel CPUs. All measurements are on one AMD Zen 4 part. - `fcmovu` and `fcmovnu`, which wait for PF in the integer lowering. ## Overlap I trial-merged this branch with the 84 other open PRs. Each one conflicts in the same files against `main` and against this branch: the 11 that merge cleanly into `main` also merge cleanly here, and this branch adds no conflict to the others. The changes stay inside `tools/x87_lower.py`, the new fixture, the new `x87_call_flags` entry of the native harness, a CMake block after the `cpu_aot_x87_scale` test, and the x87 section of `docs/experiments.md`. If #335 adds OF to the integer lowering, the moves are unaffected because none of them reads it, and the flag rule already covers a reader of OF in a function with a move. ## Evidence The fixture is synthetic and original to this repository (MIT). `docs/experiments.md` documents the six forms, the signaling-NaN measurement, the controls and the reproduction command. ## 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 (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:43:15 +00:00
R0Y00 commented 2026-11-24 06:48:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:06:50Z.


The six predicates and full-width copies check out for the CMP/FCOMI cases. There is a missing precondition: lower() initializes carry and zero to false, so two FLDs followed by FCMOVB before any flag writer silently consume that initializer. On native x64 the move can observe either incoming CF. Please reject an FCMOV whose required flags are not defined on every incoming path (including a join with one undefined predecessor), or explicitly bind those flags as inputs if separately agreed. A caller setting CF with STC versus CLC before the same leaf distinguishes the cases; keep the existing defined-producer controls.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075134944 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476#issuecomment-7075134944). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T06:06:50Z. --- The six predicates and full-width copies check out for the CMP/FCOMI cases. There is a missing precondition: lower() initializes carry and zero to false, so two FLDs followed by FCMOVB before any flag writer silently consume that initializer. On native x64 the move can observe either incoming CF. Please reject an FCMOV whose required flags are not defined on every incoming path (including a join with one undefined predecessor), or explicitly bind those flags as inputs if separately agreed. A caller setting CF with STC versus CLC before the same leaf distinguishes the cases; keep the existing defined-producer controls.
R0Y00 commented 2026-11-24 06:48:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:21:15Z.


The new entry and join checks address the direct cases. One path at b1d485b still accepts incoming CF: two FLDs, CMC, then FCMOVB. iced reports CF in both rflags_read and rflags_written for CMC (verified with byte F5). Consequently _flags_after() marks CF defined even though CMC only inverts the unbound incoming bit. STC versus CLC in the caller still changes the native result. Please carry that dependency through CMC and the supported carry-consuming writers such as ADC/SBB and rotates through carry. Add a rejection for entry-CMC-FCMOVB and retain a positive case with CLC or STC before CMC.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075276839 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476#issuecomment-7075276839). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T06:21:15Z. --- The new entry and join checks address the direct cases. One path at b1d485b still accepts incoming CF: two FLDs, CMC, then FCMOVB. iced reports CF in both rflags_read and rflags_written for CMC (verified with byte F5). Consequently _flags_after() marks CF defined even though CMC only inverts the unbound incoming bit. STC versus CLC in the caller still changes the native result. Please carry that dependency through CMC and the supported carry-consuming writers such as ADC/SBB and rotates through carry. Add a rejection for entry-CMC-FCMOVB and retain a positive case with CLC or STC before CMC.
denn0x commented 2026-11-24 06:48:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T06:36:01Z.


Thanks, fixed in b1d485b.

  • Rejection: translation now rejects an FCMOV that reads a flag not written on every path from the entry. defined_flags() in tools/x87_lower.py is a forward pass over ZF and CF. It takes writes from iced's flag information, minus the flags iced reports undefined, and intersects at joins. Calls leave both flags undefined. CL-count shifts and rep compares define no new flag, and 8- and 16-bit shifts by the operand width or more leave CF undefined. The diagnostic names the flag: fcmovb st,st(1) at 0x1004 reads CF, which no instruction defines on every path from the entry. No flag is bound as an input.
  • STC vs CLC: the native harness has a new entry, x87_call_flags, that loads the caller's CF and ZF with sahf just before the call. Two native-only exports run fcmovb and fcmove right after two loads. For 40 value pairs under all four (ZF, CF) pairs, each move followed the caller's flag in all 320 runs, and translation rejects both exports.
  • New rejection cases:
    • a move before any writer;
    • a join with one undefined predecessor (jrcxz around the cmp);
    • fcmovbe after stc (ZF undefined);
    • fcmovnb after a call;
    • fcmove after imul (ZF undefined);
    • fcmovb after shl eax, cl with no earlier writer;
    • fcmovb after shl ax, 16.
  • Still translated: four sequences whose flags are written on every path: after clc, after a join where both paths write ZF, after xor eax, eax, and after fcomi.
  • Unchanged: the 10098 cases and the six defined-producer controls give the same counts.

The body has the updated output. Fork CI run 44993139598 passed at b1d485b.

The integer flag readers (jcc, setcc, cmovcc, adc/sbb, rcl/rcr) consume the same initializer: je as a function's first instruction lowers to if (zero) goto .... That is outside this PR. The same pass could gate them in a separate one if you want it.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075419659 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476#issuecomment-7075419659). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T06:36:01Z. --- Thanks, fixed in `b1d485b`. - **Rejection:** translation now rejects an FCMOV that reads a flag not written on every path from the entry. `defined_flags()` in `tools/x87_lower.py` is a forward pass over ZF and CF. It takes writes from iced's flag information, minus the flags iced reports undefined, and intersects at joins. Calls leave both flags undefined. CL-count shifts and `rep` compares define no new flag, and 8- and 16-bit shifts by the operand width or more leave CF undefined. The diagnostic names the flag: `fcmovb st,st(1) at 0x1004 reads CF, which no instruction defines on every path from the entry`. No flag is bound as an input. - **STC vs CLC:** the native harness has a new entry, `x87_call_flags`, that loads the caller's CF and ZF with `sahf` just before the call. Two native-only exports run `fcmovb` and `fcmove` right after two loads. For 40 value pairs under all four (ZF, CF) pairs, each move followed the caller's flag in all 320 runs, and translation rejects both exports. - **New rejection cases:** - a move before any writer; - a join with one undefined predecessor (`jrcxz` around the `cmp`); - `fcmovbe` after `stc` (ZF undefined); - `fcmovnb` after a call; - `fcmove` after `imul` (ZF undefined); - `fcmovb` after `shl eax, cl` with no earlier writer; - `fcmovb` after `shl ax, 16`. - **Still translated:** four sequences whose flags are written on every path: after `clc`, after a join where both paths write ZF, after `xor eax, eax`, and after `fcomi`. - **Unchanged:** the 10098 cases and the six defined-producer controls give the same counts. The body has the updated output. Fork CI run [44993139598](https://github.com/denn0x/Xbox2Windows/actions/runs/44993139598) passed at `b1d485b`. The integer flag readers (`jcc`, `setcc`, `cmovcc`, `adc`/`sbb`, `rcl`/`rcr`) consume the same initializer: `je` as a function's first instruction lowers to `if (zero) goto ...`. That is outside this PR. The same pass could gate them in a separate one if you want it. _AI-assisted (Claude Code)._
R0Y00 commented 2026-11-24 06:48:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:57:35Z.


At b1d485b the direct reads and joins are covered. One case still lets the new FCMOV consume a caller flag indirectly: mov eax, 0FFFFFFFFh; adc eax, 0; fcmove st, st(1) after two distinct FLDs. Incoming CF=0 gives EAX=FFFFFFFFh and ZF=0; CF=1 gives EAX=0 and ZF=1. _flags_after marks ADC's ZF as defined solely because iced lists it as written. Please exercise the two native caller-CF values with your harness and reject this FCMOV when the producer consumes undefined carry. The same dependency applies to SBB and carry rotates for flags they produce. An explicit CLC/STC before ADC should still pass. This can stay scoped to the x87 profile.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075627484 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476#issuecomment-7075627484). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T06:57:35Z. --- At b1d485b the direct reads and joins are covered. One case still lets the new FCMOV consume a caller flag indirectly: `mov eax, 0FFFFFFFFh; adc eax, 0; fcmove st, st(1)` after two distinct FLDs. Incoming CF=0 gives EAX=FFFFFFFFh and ZF=0; CF=1 gives EAX=0 and ZF=1. `_flags_after` marks ADC's ZF as defined solely because iced lists it as written. Please exercise the two native caller-CF values with your harness and reject this FCMOV when the producer consumes undefined carry. The same dependency applies to SBB and carry rotates for flags they produce. An explicit CLC/STC before ADC should still pass. This can stay scoped to the x87 profile.
FZNull commented 2026-11-24 06:48:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by FZNull on 2026-11-22T07:05:38Z.


Independent verification on a second machine. Trial-merged this PR onto main 5a2c7822 (merge commit aec5bc8, clean, no conflicts) and ran the x87 suite.

Environment: Windows 11 build 26300.9974, AMD Ryzen 5 5500 (AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD — Zen 3, a different part from the Zen 4 used above), MSVC 19.50.35726, Windows SDK 10.0.26100.0, CMake 4.5.1 + Ninja 1.13.2, DXC 1.9.2609.5, Python 3.13.12.

Build: full build 899/899 targets, exit 0.

Tests:

ctest --test-dir out/build/verify-476 --no-tests=error -R 'x87' -V

13/13 x87 tests pass, 72.69 s total. cpu_aot_x87_fcmov reports:

10098 cases over 12 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
cases in which a conditional move changed the output block: 4415 of 10098
x87_call kept a live caller ST(0) value and control word around all 12 native and translated exports
control invert: 2752/10098 mismatches
control beonly: 185/10098 mismatches
control nbeor: 960/10098 mismatches
control neverne: 800/10098 mismatches
control always: 4459/10098 mismatches
control quiet: 210/10098 mismatches

All six negative controls and the rejection counts match the PR body exactly. Control run cpu_aot_wide_arithmetic also passes (2929 inputs, fault and rejection controls OK). No failures, no skips. The PR also still merges cleanly onto the later main 933cbad per git merge-tree (not built).

Verdict: verified on an AMD Zen 3 machine; every measured figure matches the PR. Reviewed at merge commit aec5bc8 (PR head b1d485b on main 5a2c7822).

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075707753 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476#issuecomment-7075707753). Originally posted by [FZNull](https://github.com/FZNull) on 2026-11-22T07:05:38Z. --- Independent verification on a second machine. Trial-merged this PR onto main `5a2c7822` (merge commit `aec5bc8`, clean, no conflicts) and ran the x87 suite. **Environment:** Windows 11 build 26300.9974, AMD Ryzen 5 5500 (`AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD` — Zen 3, a different part from the Zen 4 used above), MSVC 19.50.35726, Windows SDK 10.0.26100.0, CMake 4.5.1 + Ninja 1.13.2, DXC 1.9.2609.5, Python 3.13.12. **Build:** full build 899/899 targets, exit 0. **Tests:** ```powershell ctest --test-dir out/build/verify-476 --no-tests=error -R 'x87' -V ``` 13/13 x87 tests pass, 72.69 s total. `cpu_aot_x87_fcmov` reports: ```text 10098 cases over 12 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack cases in which a conditional move changed the output block: 4415 of 10098 x87_call kept a live caller ST(0) value and control word around all 12 native and translated exports control invert: 2752/10098 mismatches control beonly: 185/10098 mismatches control nbeor: 960/10098 mismatches control neverne: 800/10098 mismatches control always: 4459/10098 mismatches control quiet: 210/10098 mismatches ``` All six negative controls and the rejection counts match the PR body exactly. Control run `cpu_aot_wide_arithmetic` also passes (2929 inputs, fault and rejection controls OK). No failures, no skips. The PR also still merges cleanly onto the later main `933cbad` per `git merge-tree` (not built). Verdict: verified on an AMD Zen 3 machine; every measured figure matches the PR. Reviewed at merge commit `aec5bc8` (PR head `b1d485b` on main `5a2c7822`). _AI-assisted._
denn0x commented 2026-11-24 06:48:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T07:17:56Z.


Thanks, fixed in 4d7bc7c. The branch also merges main (5c0151e). In a function that contains a conditional move, translation now rejects every instruction that reads a status flag that is not defined on every path from the entry, not only the move.

Why every reader. adc, sbb, rcl, rcr and cmc pass an undefined CF on into the flags they write, as in your example. They also pass it into registers: mov eax, 0; adc eax, 0; test eax, eax; fcmovne sees a ZF that test defines, but the value comes from the caller's CF. Rejecting the first reader of the undefined flag covers both paths. The pass now tracks all six status flags rather than only ZF and CF, so the rule still holds if the integer lowering later reads OF (#335) or another flag. DF is not tracked, because the Windows x64 calling convention clears it.

Native runs. Five new native-only exports run through x87_call_flags under all four (ZF, CF) pairs. Together with the two earlier ones, each export followed the caller's flag in all 1120 runs over 40 value pairs. Translation rejects each export at its first reader of the undefined flag. The previous head accepted all five new exports.

Export Sequence Rejected at
adc_entry your example adc (CF)
sbb_entry mov eax, 0; sbb eax, 0; fcmove sbb (CF)
adc_test_entry mov eax, 0; adc eax, 0; test eax, eax; fcmovne adc (CF)
rcl_test_entry mov eax, 0; rcl eax, 1; test eax, eax; fcmovne rcl (CF)
cmc_entry cmc; fcmovb cmc (CF)

Translated readers. Four new exports read the CF of the fixture's cmp before the move. They are the adc and sbb sequences above, cmc; fcmovb, and the rcl/test one. Each is compared with native on 798 cases. Two new mutations must mismatch: a dropped cmc (696 mismatches) and adc/sbb without the incoming carry (510). The test now has 13290 cases over 16 exports. The six existing controls still mismatch at their new counts, which are in the updated body.

Static cases.

  • Newly rejected: a cmc before the defining cmp, and a seto before the defining cmp. The integer lowering would also reject seto, but the flag rule comes first.
  • Still translated: clc before adc, stc before sbb, cmp before cmc, and clc before rcl and test.

The scope stays inside the x87 profile: functions without a conditional move are unchanged.

The body has the updated output, controls and verification. The full suite at 5c0151e gives 205 passed, the #369 probe failing as on main, and 2 capability skips out of 208. Fork CI run 44997062998 passed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075831005 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/476#issuecomment-7075831005). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-22T07:17:56Z. --- Thanks, fixed in `4d7bc7c`. The branch also merges `main` (`5c0151e`). In a function that contains a conditional move, translation now rejects every instruction that reads a status flag that is not defined on every path from the entry, not only the move. **Why every reader.** `adc`, `sbb`, `rcl`, `rcr` and `cmc` pass an undefined CF on into the flags they write, as in your example. They also pass it into registers: `mov eax, 0; adc eax, 0; test eax, eax; fcmovne` sees a ZF that `test` defines, but the value comes from the caller's CF. Rejecting the first reader of the undefined flag covers both paths. The pass now tracks all six status flags rather than only ZF and CF, so the rule still holds if the integer lowering later reads OF (#335) or another flag. `DF` is not tracked, because the Windows x64 calling convention clears it. **Native runs.** Five new native-only exports run through `x87_call_flags` under all four (ZF, CF) pairs. Together with the two earlier ones, each export followed the caller's flag in all 1120 runs over 40 value pairs. Translation rejects each export at its first reader of the undefined flag. The previous head accepted all five new exports. | Export | Sequence | Rejected at | |---|---|---| | `adc_entry` | your example | `adc` (CF) | | `sbb_entry` | `mov eax, 0; sbb eax, 0; fcmove` | `sbb` (CF) | | `adc_test_entry` | `mov eax, 0; adc eax, 0; test eax, eax; fcmovne` | `adc` (CF) | | `rcl_test_entry` | `mov eax, 0; rcl eax, 1; test eax, eax; fcmovne` | `rcl` (CF) | | `cmc_entry` | `cmc; fcmovb` | `cmc` (CF) | **Translated readers.** Four new exports read the CF of the fixture's `cmp` before the move. They are the `adc` and `sbb` sequences above, `cmc; fcmovb`, and the `rcl`/`test` one. Each is compared with native on 798 cases. Two new mutations must mismatch: a dropped `cmc` (696 mismatches) and `adc`/`sbb` without the incoming carry (510). The test now has 13290 cases over 16 exports. The six existing controls still mismatch at their new counts, which are in the updated body. **Static cases.** - Newly rejected: a `cmc` before the defining `cmp`, and a `seto` before the defining `cmp`. The integer lowering would also reject `seto`, but the flag rule comes first. - Still translated: `clc` before `adc`, `stc` before `sbb`, `cmp` before `cmc`, and `clc` before `rcl` and `test`. The scope stays inside the x87 profile: functions without a conditional move are unchanged. The body has the updated output, controls and verification. The full suite at `5c0151e` gives 205 passed, the #369 probe failing as on `main`, and 2 capability skips out of 208. Fork CI run [44997062998](https://github.com/denn0x/Xbox2Windows/actions/runs/44997062998) passed. _AI-assisted (Claude Code)._
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!470
No description provided.