Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,25 @@ if(EXISTS "${INTAKE_PYTHON}")
add_test(NAME cpu_aot_x87_sub COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/cpu/x87_sub.py"
check "$<TARGET_FILE:cpu_x87_harness>" "$<TARGET_FILE:cpu_x87_sub>"
"$<TARGET_FILE:cpu_x87_sub_lifted>" ${x87_sub_control_files})
cpu_asm_fixture(cpu_x87_moves x87_moves)
# The lifted DLL and seven mutated controls that the check requires to mismatch.
set(x87_moves_control_files "")
foreach(variant IN ITEMS lifted chssub absnan fxchnone fldtop fstpdrop fstwrite copyquiet)
set(x87_moves_fixture "${CMAKE_CURRENT_BINARY_DIR}/cpu_x87_moves_${variant}.cpp")
add_custom_command(OUTPUT "${x87_moves_fixture}"
COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/cpu/x87_moves.py"
emit "$<TARGET_FILE:cpu_x87_moves>" "${x87_moves_fixture}" ${variant}
DEPENDS cpu_x87_moves tests/cpu/x87_moves.py tests/cpu/x87_sub.py tests/cpu/x87_div.py
tests/cpu/x87_mul.py tests/cpu/x87_add.py tools/ffx_version_query.py tools/x87_lower.py
tools/x87_soft.h tools/cpu_ranges.py tools/cpu_leaf_aot.py VERBATIM)
add_library(cpu_x87_moves_${variant} SHARED "${x87_moves_fixture}")
if(NOT variant STREQUAL "lifted")
list(APPEND x87_moves_control_files "$<TARGET_FILE:cpu_x87_moves_${variant}>")
endif()
endforeach()
add_test(NAME cpu_aot_x87_moves COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/cpu/x87_moves.py"
check "$<TARGET_FILE:cpu_x87_harness>" "$<TARGET_FILE:cpu_x87_moves>"
"$<TARGET_FILE:cpu_x87_moves_lifted>" ${x87_moves_control_files})
cpu_asm_fixture(cpu_create create_context)
set(create_fixture "${CMAKE_CURRENT_BINARY_DIR}/cpu_create_lifted.cpp")
add_custom_command(OUTPUT "${create_fixture}"
Expand Down
6 changes: 4 additions & 2 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ The local Bink DLL imports ordinal 1 from XAudio2_9.dll. A read-only check confi

## x87 in the shared lowering

`lower(..., x87=True)` in `tools/ffx_version_query.py` accepts an opt-in x87 leaf-function profile. The default (`x87=False`) still rejects every x87 instruction, so existing callers are unchanged. The profile accepts `fld` and `fstp` with an m32, m64 or m80 memory operand, and five forms of each of `fadd`, `fsub`, `fsubr`, `fmul`, `fdiv` and `fdivr`: an m32 or m64 operand, `st, st(i)`, `st(i), st`, and the popping form (`faddp`, `fsubp`, `fsubrp`, `fmulp`, `fdivp`, `fdivrp`) with `st(i), st` for `i` from 1 to 7. A popping form with `st(0)` as the destination is rejected. A function enters with control word 0x027F (53-bit precision, round to nearest-even, all exceptions masked), TOP 0, all eight registers empty and a clear status word, and it must return with the stack empty. The stack depth before each instruction is fixed at translation time (`tools/x87_lower.py`), so each register becomes a C++ slot and no TOP exists at run time. Translation rejects any other x87 instruction, including control-word, status-word, environment and `wait` access; a ninth push; a pop or `faddp` without enough values; depths that differ where control flow joins; an x87 instruction that is not reachable from the entry through direct edges; and a nonempty stack at a return, a call, an indirect branch or an external edge.
`lower(..., x87=True)` in `tools/ffx_version_query.py` accepts an opt-in x87 leaf-function profile. The default (`x87=False`) still rejects every x87 instruction, so existing callers are unchanged. The profile accepts `fld` and `fstp` with an m32, m64 or m80 memory operand, `fst` with an m32 or m64 operand, `fld st(i)`, `fst st(i)`, `fstp st(i)`, `fxch st(i)`, `fchs` and `fabs`, and five forms of each of `fadd`, `fsub`, `fsubr`, `fmul`, `fdiv` and `fdivr`: an m32 or m64 operand, `st, st(i)`, `st(i), st`, and the popping form (`faddp`, `fsubp`, `fsubrp`, `fmulp`, `fdivp`, `fdivrp`) with `st(i), st` for `i` from 1 to 7. A popping form with `st(0)` as the destination is rejected. A function enters with control word 0x027F (53-bit precision, round to nearest-even, all exceptions masked), TOP 0, all eight registers empty and a clear status word, and it must return with the stack empty. The stack depth before each instruction is fixed at translation time (`tools/x87_lower.py`), so each register becomes a C++ slot and no TOP exists at run time. Translation rejects any other x87 instruction, including control-word, status-word, environment and `wait` access; a ninth push; a pop, an `st(i)` operand or `faddp` without enough values; depths that differ where control flow joins; an x87 instruction that is not reachable from the entry through direct edges; and a nonempty stack at a return, a call, an indirect branch or an external edge.

The helpers in `tools/x87_soft.h` compute in integers and do not use host floating point. A register holds the full 80-bit encoding. m80 loads and stores copy all 80 bits, including NaN payloads, denormals, pseudo-denormals and unnormals. m32 and m64 loads convert exactly and quiet a signaling NaN. A sum rounds to 53 significand bits within the extended exponent range; a result below that range is denormalized first and then rounded at the same bit positions. The sum rounds again when it is stored to m32 or m64. NaN operands follow the x87 column of Intel SDM Vol. 1, Table 4-7: a QNaN beats an SNaN, and otherwise the larger significand wins. When two NaNs have equal significands, the positive one wins in either operand order. This tie rule was measured, not taken from the table. Infinities of opposite sign, unnormals, pseudo-infinities and pseudo-NaNs give the indefinite. An exact zero sum of nonzero addends is +0, and -0 + -0 is -0. The status word, exceptions and the other rounding modes are not modelled. Their absence is safe only because the profile rejects every instruction that could observe or change them.

Expand All @@ -536,7 +536,9 @@ The helpers in `tools/x87_soft.h` compute in integers and do not use host floati

`cpu_aot_x87_div` checks the division forms in the same way, with 17 exports and 23939 cases. `fdivr` and `fdivrp` divide the source by the destination. Each export's decoded x87 instructions must match the forms its sequence names, so an assembler that exchanged the `fdiv` and `fdivr` register encodings would fail before the run. The helper divides the 64-bit significands into a 64-bit quotient and folds a nonzero remainder into a sticky bit. That is enough to round to 53 bits, and the result goes through the same rounding as the sum and the product. Zero by zero and infinity by infinity give the indefinite. A finite nonzero value by zero gives a signed infinity (the masked divide-by-zero response). A finite value by infinity, and zero by a finite value, give a signed zero. The cases cover every pair of the 34 special encodings and every special m80 value against the m32 and m64 special encodings, in both orders. They also cover exact quotients halfway between 53-bit neighbours, quotients whose 64-bit significand ends exactly on a tie so that only the remainder decides, `x / x` through `fdiv st, st(0)`, quotients beyond the binary64 range, the extended overflow and underflow thresholds, denormal results, two three-value chains through `fdivp st(1), st`, `fdivp st(2), st` and `fdivrp st(1), st`, and random operands. Seven mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, an ignored remainder, the indefinite for a finite value by zero, `fdivr` in the `fdiv` order, an unsigned zero quotient, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 8785, 14373, 253, 602, 6829, 551 and 54 mismatches. Translation still rejects `fdivp st(0), st`, `fdivrp st(0), st`, `fidiv`, `fidivr`, `fsqrt`, `fprem`, `fiadd` and divisions without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_div$' -V`.

`cpu_aot_x87_sub` checks `fsub`, `fsubr`, `fsubp`, `fsubrp` and the `fadd` forms besides `faddp` in the same way, with 22 exports and 35023 cases. `fsubr` and `fsubrp` subtract the destination from the source. The helper computes `a - b` as `a + (-b)`, but it selects a NaN or invalid operand before negating, so a NaN result keeps its own sign. `x - x` is +0, and -0 - +0 is -0. The cases reuse the addition pairs of `cpu_aot_x87_add`, and again with the second operand negated. They also cover `x - x` and `x + x` through `st(0)`, m32 and m64 operands against the special encodings, `float` and `double` differences, a three-value chain through `fsubp st(2), st` and `fsubrp st(1), st`, and random operands. Seven mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, negating a NaN subtrahend, `fsubr` in the `fsub` order, -0 for an exact cancellation, a dropped sticky bit, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 15680, 24289, 2022, 7753, 777, 108 and 81 mismatches. Translation still rejects `fsubp st(0), st`, `fsubrp st(0), st`, `fisub`, `fisubr`, `fchs`, `fabs`, `fcom` and arithmetic without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_sub$' -V`.
`cpu_aot_x87_sub` checks `fsub`, `fsubr`, `fsubp`, `fsubrp` and the `fadd` forms besides `faddp` in the same way, with 22 exports and 35023 cases. `fsubr` and `fsubrp` subtract the destination from the source. The helper computes `a - b` as `a + (-b)`, but it selects a NaN or invalid operand before negating, so a NaN result keeps its own sign. `x - x` is +0, and -0 - +0 is -0. The cases reuse the addition pairs of `cpu_aot_x87_add`, and again with the second operand negated. They also cover `x - x` and `x + x` through `st(0)`, m32 and m64 operands against the special encodings, `float` and `double` differences, a three-value chain through `fsubp st(2), st` and `fsubrp st(1), st`, and random operands. Seven mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, negating a NaN subtrahend, `fsubr` in the `fsub` order, -0 for an exact cancellation, a dropped sticky bit, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 15680, 24289, 2022, 7753, 777, 108 and 81 mismatches. Translation still rejects `fsubp st(0), st`, `fsubrp st(0), st`, `fisub`, `fisubr`, `frndint`, `fscale`, `fcom` and arithmetic without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_sub$' -V`.

`cpu_aot_x87_moves` checks the register data movement in the same way, with 26 exports and 20224 cases. `fld st(i)` pushes a copy of `st(i)`, `fst st(i)` copies `st(0)` into `st(i)`, and `fstp st(i)` does the same and then pops. `fxch st(i)` exchanges `st(0)` and `st(i)`. These copy the full 80-bit encoding, so a signaling NaN stays signaling and an unnormal keeps its bits. `fchs` and `fabs` change only the sign bit, of every encoding, including zeros, NaNs, unnormals and pseudo-denormals. `fst` to m32 or m64 rounds and stores `st(0)` like `fstp` and leaves the register unchanged. The model moves raw encodings and shares no code with `tools/x87_soft.h`. The exports cover `fld st(0)`, `st(1)`, `st(2)` and `st(6)` at depth 7, `fst st(1)` and `st(2)`, `fstp st(0)`, `st(1)` and `st(2)`, `fxch st(0)`, `st(1)`, `st(2)` and `st(7)` at depth 8, and `fchs` and `fabs` stored to m80, m64 and m32. They also cover an `fst` to m64 and one to m32 before the final `fstp`, and movement combined with arithmetic: `a + (-b)`, `|a| * |b|` and a subtraction after `fxch`. The cases run every pair of the 34 special encodings through the two-value exports and 300 triples of them through the three-value exports, plus random values and 300 blocks of eight m64 values. Seven mutated translations must each mismatch: `fchs` as `0 - x`, `fabs` that leaves -0 and negative NaNs unchanged, `fxch` that does nothing, `fld st(i)` that always copies `st(0)`, `fstp st(i)` that only pops, `fst` to m32 or m64 that also writes the rounded value back to `st(0)`, and register copies that quiet a signaling NaN. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 969, 225, 3116, 2212, 1922, 1357 and 267 mismatches. Translation rejects an `st(i)` operand beyond the stack depth, a push at depth 8, `fchs` and `fabs` on an empty stack, `fist`, `fld1`, `ffree` and `fdecstp`. It also rejects the undocumented alias encodings of `fstp st(i)` (`DF D0+i`, `DF D8+i`) and `fxch st(i)` (`DD C8+i`, `DF C8+i`), which assemblers do not emit and the fixture does not measure. Only `st(0)` to `st(2)`, `fld st(6)` and `fxch st(7)` are exercised as register operands. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_moves$' -V`.

## Mapping an image without LoadLibrary

Expand Down
2 changes: 1 addition & 1 deletion tests/cpu/x87_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def attempt(hex_bytes, *, x87=True, calls=None, exits=None):
fld, fstp = "d9 01", "dd 19" # fld dword ptr [rcx]; fstp qword ptr [rdx]
rejected = {
"the profile off (default)": (attempt(f"{fld} {fstp} c3", x87=False), "unsupported query operation"),
"fld st(1)": (attempt(f"{fld} d9 c1 de c1 de c1 {fstp} c3"), "unsupported x87 operand"),
"fild m16": (attempt(f"{fld} df 01 de c1 {fstp} c3"), "unsupported x87 operation"),
"fcom st(1)": (attempt(f"{fld} {fld} d8 d1 de c1 {fstp} c3"), "fcom"),
"fnstsw ax": (attempt(f"{fld} df e0 {fstp} c3"), "unsupported x87 operation"),
"fldcw": (attempt(f"d9 29 {fld} {fstp} c3"), "fldcw"),
Expand Down
278 changes: 278 additions & 0 deletions tests/cpu/x87_moves.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
; Generated by tests/cpu/x87_moves.py asm; edit SEQUENCES there and regenerate.
; Source-owned differential fixture for the register data movement of the x87 leaf-function
; profile. Each export reads an input block at RCX, writes an output block at RDX and returns with
; the x87 stack empty.
.code

fld_st0 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld st(0)
faddp st(1), st
fstp tbyte ptr [rdx+0]
ret
fld_st0 ENDP

fld_st1 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fld st(1)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
fstp tbyte ptr [rdx+32]
ret
fld_st1 ENDP

fld_st2 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fld tbyte ptr [rcx+32]
fld st(2)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
fstp tbyte ptr [rdx+32]
fstp tbyte ptr [rdx+48]
ret
fld_st2 ENDP

fld_st6_depth8 PROC FRAME
.endprolog
fld qword ptr [rcx+0]
fld qword ptr [rcx+8]
fld qword ptr [rcx+16]
fld qword ptr [rcx+24]
fld qword ptr [rcx+32]
fld qword ptr [rcx+40]
fld qword ptr [rcx+48]
fld st(6)
fstp qword ptr [rdx+0]
fstp qword ptr [rdx+8]
fstp qword ptr [rdx+16]
fstp qword ptr [rdx+24]
fstp qword ptr [rdx+32]
fstp qword ptr [rdx+40]
fstp qword ptr [rdx+48]
fstp qword ptr [rdx+56]
ret
fld_st6_depth8 ENDP

fst_st1 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fst st(1)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
ret
fst_st1 ENDP

fst_st2 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fld tbyte ptr [rcx+32]
fst st(2)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
fstp tbyte ptr [rdx+32]
ret
fst_st2 ENDP

fstp_st1 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fstp st(1)
fstp tbyte ptr [rdx+0]
ret
fstp_st1 ENDP

fstp_st0 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fstp st(0)
fstp tbyte ptr [rdx+0]
ret
fstp_st0 ENDP

fstp_st2 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fld tbyte ptr [rcx+32]
fstp st(2)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
ret
fstp_st2 ENDP

fxch_st0 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fxch st(0)
fstp tbyte ptr [rdx+0]
ret
fxch_st0 ENDP

fxch_st1 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fxch st(1)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
ret
fxch_st1 ENDP

fxch_st2 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fld tbyte ptr [rcx+32]
fxch st(2)
fstp tbyte ptr [rdx+0]
fstp tbyte ptr [rdx+16]
fstp tbyte ptr [rdx+32]
ret
fxch_st2 ENDP

fxch_st7_depth8 PROC FRAME
.endprolog
fld qword ptr [rcx+0]
fld qword ptr [rcx+8]
fld qword ptr [rcx+16]
fld qword ptr [rcx+24]
fld qword ptr [rcx+32]
fld qword ptr [rcx+40]
fld qword ptr [rcx+48]
fld qword ptr [rcx+56]
fxch st(7)
fstp qword ptr [rdx+0]
fstp qword ptr [rdx+8]
fstp qword ptr [rdx+16]
fstp qword ptr [rdx+24]
fstp qword ptr [rdx+32]
fstp qword ptr [rdx+40]
fstp qword ptr [rdx+48]
fstp qword ptr [rdx+56]
ret
fxch_st7_depth8 ENDP

fchs_m80 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fchs
fstp tbyte ptr [rdx+0]
ret
fchs_m80 ENDP

fchs_m64 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fchs
fstp qword ptr [rdx+0]
ret
fchs_m64 ENDP

fchs_m32 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fchs
fstp dword ptr [rdx+0]
ret
fchs_m32 ENDP

fabs_m80 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fabs
fstp tbyte ptr [rdx+0]
ret
fabs_m80 ENDP

fabs_m64 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fabs
fstp qword ptr [rdx+0]
ret
fabs_m64 ENDP

fabs_m32 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fabs
fstp dword ptr [rdx+0]
ret
fabs_m32 ENDP

chs_abs_chs PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fchs
fabs
fchs
fstp tbyte ptr [rdx+0]
ret
chs_abs_chs ENDP

fst_m32 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fst dword ptr [rdx+0]
fstp tbyte ptr [rdx+16]
ret
fst_m32 ENDP

fst_m64 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fst qword ptr [rdx+0]
fstp tbyte ptr [rdx+16]
ret
fst_m64 ENDP

fst_m64_m32 PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fst qword ptr [rdx+0]
fst dword ptr [rdx+8]
fstp tbyte ptr [rdx+16]
ret
fst_m64_m32 ENDP

chs_add PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fchs
faddp st(1), st
fstp tbyte ptr [rdx+0]
ret
chs_add ENDP

abs_mul PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fabs
fld tbyte ptr [rcx+16]
fabs
fmulp st(1), st
fstp tbyte ptr [rdx+0]
ret
abs_mul ENDP

fxch_sub PROC FRAME
.endprolog
fld tbyte ptr [rcx+0]
fld tbyte ptr [rcx+16]
fxch st(1)
fsubp st(1), st
fstp tbyte ptr [rdx+0]
ret
fxch_sub ENDP

END
27 changes: 27 additions & 0 deletions tests/cpu/x87_moves.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
EXPORTS
fld_st0
fld_st1
fld_st2
fld_st6_depth8
fst_st1
fst_st2
fstp_st1
fstp_st0
fstp_st2
fxch_st0
fxch_st1
fxch_st2
fxch_st7_depth8
fchs_m80
fchs_m64
fchs_m32
fabs_m80
fabs_m64
fabs_m32
chs_abs_chs
fst_m32
fst_m64
fst_m64_m32
chs_add
abs_mul
fxch_sub
Loading