1791 definitions in 26 files, 0 with an encoding diagram. src/isa/x86
| File | Definitions | Other | What |
|---|---|---|---|
| hostall.lisp | 0 | 1 | a build CPU with every feature, for machine-independent output. |
| package.lisp | 0 | 0 | the ISA.X86 package: the x86-64 host description. |
| state.lisp | 0 | 18 | the x86-64 machine state the semantics bodies read and write. |
| flags.lisp | 0 | 5 | x86's old spellings of core forms (deprecated aliases). |
| fp.lisp | 0 | 23 | x86's floating-point rules around core's IEEE primitives (SSE, AVX, FMA). |
| defx86.lisp | 0 | 2 | the x86-64 instruction form: encoding, operands, semantics. |
| hostcpu.lisp | 0 | 0 | the CPU a build is for: which x86-64 forms it can run. |
| gen/gp.lisp | 858 | 0 | x86-64 general-purpose instruction forms |
| gen/sse.lisp | 278 | 0 | x86-64 legacy SSE forms |
| gen/avx.lisp | 655 | 0 | x86-64 VEX (AVX, AVX2, BMI) forms |
| gen/unread.lisp | 0 | 1 | x86-64 rows of the SDM not read, with why |
| enc.lisp | 0 | 14 | the x86-64 emitter and decoder, from the forms' encoding words. |
| tables.lisp | 0 | 0 | the encoding table of every form defined before it. |
| cost/xclass.lisp | 0 | 1 | ucas's x86-64 instruction classes (generated, do not edit). |
| cost.lisp | 0 | 17 | what each x86-64 form costs under the selected cost model. |
| cost/glc.lisp | 0 | 0 | x86-64 cost model glc, Intel Golden Cove P-core as in Sapphire Rapids (generated, do not edit). |
| cost/rpl.lisp | 0 | 0 | x86-64 cost model rpl, Intel Raptor Lake / Alder Lake P-core (Golden Cove/Raptor Cove) (generated, do not edit). |
| cost/skl.lisp | 0 | 0 | x86-64 cost model skl, Intel Skylake/Kaby/Coffee Lake client core (this host's i5-9400 is one) (generated, do not edit). |
| cost/skx.lisp | 0 | 0 | x86-64 cost model skx, Intel Skylake-SP/X server core (generated, do not edit). |
| cost/x86v2.lisp | 0 | 0 | x86-64 cost model x86v2, generic x86-64-v2 (SSE4.2, POPCNT) (generated, do not edit). |
| cost/x86v3.lisp | 0 | 0 | x86-64 cost model x86v3, generic x86-64-v3 (AVX2, FMA, BMI1/2, LZCNT, MOVBE) (generated, do not edit). |
| cost/x86v4.lisp | 0 | 0 | x86-64 cost model x86v4, generic x86-64-v4 (AVX-512 F/BW/CD/DQ/VL) (generated, do not edit). |
| cost/znver1.lisp | 0 | 0 | x86-64 cost model znver1, AMD Zen+ (Ryzen 5 3550H, Picasso) (generated, do not edit). |
| cost/znver2.lisp | 0 | 0 | x86-64 cost model znver2, AMD Zen 2 (Ryzen 5 3600, Steam Deck's Custom APU 0932) (generated, do not edit). |
| cost/znver3.lisp | 0 | 0 | x86-64 cost model znver3, AMD Zen 3 (Ryzen 7 5825U, Cezanne) (generated, do not edit). |
| cost/znver4.lisp | 0 | 0 | x86-64 cost model znver4, AMD Zen 4 (generated, do not edit). |
Package ISA.X86. x86-64 is the CPU host of today's builds: the PS3's
PowerPC code is translated to it (docs/isel.md). Like every ISA of
src/isa, it is described from its specification so that it can play
either role; as a host it is never emulated: each instruction form is described, with its encoding and its
semantics in the core vocabulary (docs/lisp.md), so that ctil can match guest
semantics against host semantics and emit the bytes.
The forms are not written by hand. tools/sdmgen (C99, run only with
-DXSPE_REGEN=ON) reads the Intel SDM, Volume 2 (325383-093US, pinned in
cpmfile.json) and writes gen/*.lisp, which is committed:
| File | What |
|---|---|
package.lisp | ISA.X86 |
state.lisp | x86: the state a semantics body reads and writes (16 GPRs, 16 xmm/ymm, the six flags, a fault, mval) |
flags.lisp | spellings core doesn't have yet: x86-borrow-out, x86-sub-overflow, x86-even-parity, x86-lane, x86-vmap256 |
fp.lisp | floating point: core's IEEE primitives under MXCSR's rules (x86-fp, x86-fp-fma, x86-fp-min, x86-fp-max, x86-fp-cvt, x86-fp-from-int, x86-fp-to-int, x86-fp-cmp, x86-fp-compare, x86-fp-round, x86-fp-end) |
defx86.lisp | the defx86 form, its registry, the opcode column read into an encoding word |
gen/gp.lisp | the general-purpose forms (generated) |
gen/sse.lisp | the legacy-encoded vector forms (generated) |
gen/avx.lisp | the VEX forms: AVX, AVX2, BMI1/2, FMA (generated) |
gen/unread.lisp | *x86-unread*: every form or row not read, with why (generated) |
enc.lisp | x86-emit, x86-emit-mem, x86-decode over the encoding words |
tables.lisp | *x86-enc*: the encoding word of every form, by index |
cost/xclass.lisp | ucas's instruction classes (generated by tools/costgen) |
cost/<model>.lisp | one cost model per µarch: znver1..znver4, skl, skx, glc, rpl, x86v2..x86v4 (generated); the build loads XSPE_COST_CPU (default znver4) |
cost.lisp | x86-form-cost: each form's class (mnemonic table, operand rules) and price (docs/cost.md) |
Regenerate with cmake -S . -B build -DXSPE_REGEN=ON && cmake --build build
--target x86-regen (CPM_SOURCE_CACHE may point at a cache that already holds
the PDF). The test x86_regen (REGEN mode only) regenerates into the build
tree and requires the committed files to be identical.
The matcher prices forms with ucas's x86-64 tables (tools/costgen compiles
ucas's src/x86data.c in; docs/cost.md):
(x86-form-cost 'imul.r64.r/m64) ; -> entry (lat 3, rtp 1, uops 1, "ALU1"), :imul, :x
(x86-form-cost-units 'vpshufb.ymm.ymm.ymm/m256 'znver2) ; -> 501
*x86-form-classes* gives each mnemonic ucas's class (ucas has no such
table, only descriptions). A few operand rules refine the class: a shift by
CL, a one-operand IMUL, a 64-bit DIV, a vector shift by an immediate, MOVD to
or from a GPR, a store. A form with no class costs ucas's :libm estimate.
(defx86 add.r/m64.r64 "REX.W + 01 /r"
((dest :rm gpr 64 :rw :mem 64) (src :reg gpr 64 :r))
(:asm "ADD r/m64, r64") (:page "ADD") (:opsize 64)
(psetq dest (wrap 64 (+ dest src))
cf (carry-out 64 dest src)
af (carry-out 4 dest src)
of (add-overflow 64 dest src))
(setq pf (x86-even-parity 8 dest) zf (b2i (= dest 0)) sf (bit-at dest 63)))
. (xmm2/m128 is xmm/m128); a second form of the
same spelling gets ~2.defx86.lisp reads it at compile time into one encoding word
(prefixes 66/F2/F3, REX.W, VEX L/pp/map/W/WIG/LIG, opcode, /r or /digit,
+r, immediates, a fixed byte after the opcode, whether the r/m is memory or
register only, whether an 8-bit register operand needs a REX). The emitter and
the decoder both read that word: one datum, both directions. A 16-bit form's
66 prefix, which the column leaves out, comes from :opsize 16.(name role class width access . options). Role: :reg
(ModRM.reg), :rm (ModRM.r/m), :vvvv, :imm, :is4 (a register in
imm8[7:4]), :opreg (+r), :fixed (an implicit register, :fixed n, or the
constant 1). Class: gpr, vec (128 xmm, 256 ymm), imm, rel, mem
(memory only), one. :mem w: the r/m may be w bits of memory. :sext w:
an immediate sign extended to the operand size (Vol. 2A, 2.2.1.5). Names
follow the SDM's: dest, src (two operands), src1, src2, src3.(:asm ...), (:page ...), (:opsize n), (:vlen n),
(:cpuid :avx2 ...), (:undefined f...) (flags, or values, the SDM leaves
undefined in some case at least: the body keeps their old value),
(:flags-unread f...) (flags whose Flags Affected prose isn't read: the body
doesn't set them), (:unread "why") (no semantics: the Operation isn't
read; there is no function).(x86/<name> cpu dest-n src-n ...) of
the register numbers or immediates of the operands that are encoded. Each
operand's name is bound to its value at its width, each register the body
names (rax ... r15) to its 64 bits, each flag it names (cf pf af zf sf
of) to its bit; what the body sets is written back at the end: a 32-bit GPR
write zero extends, an 8- or 16-bit one keeps the rest (Vol. 1, 3.4.1.1); a
VEX vector write zeroes the bits above its width, a legacy SSE one keeps them.
(raise :divide-error) faults (core's raise, the kinds of state.lisp's
defexceptions; x86-raise is a deprecated alias): nothing is written back.carry-out,
add-overflow, (carry-out 4 ...); of a difference borrow-out,
sub-overflow, (borrow-out 4 ...); computed in the same psetq as
the result, from the operands. SF, ZF, PF are read from the result at the end
of the body (even-parity 8 is PF). Flags the prose clears or sets are
set at the end.DEST[31:0]. Core's
lane is big-endian (lane 0 most significant). x86-lane takes x86's
numbering: for a 128-bit vector (x86-lane w v i) = (lane w v (- (/ 128 w)
1 i)). A statement list that applies the same function to every lane of a
whole vector is written vmap (x86-vmap256 for a ymm, core's vmap on
each half), which doesn't depend on lane numbering. Other lane statements
are dpbs at x86's (LSB-0) bit positions.m64, m128) is the state's mval. The emitter
does encode memory operands (x86-emit-mem).fp.lisp: (x86-fp fadd :f64 (a b)) is
fadd in MXCSR.RC's rounding mode, sources read under DAZ, an underflowing
result flushed under FTZ, a NaN source giving the first NaN source quieted
and an invalid operation the QNaN indefinite, and the exceptions (core's
fflags, tininess after rounding, plus the denormal operand flag) gathered
in fpx. x86-fp-fma is the FMA pages' fused rounding (ffma, signs of
the product or the addend inverted for FMSUB/FNMADD/FNMSUB), x86-fp-min
and x86-fp-max the MIN/MAX procedures of MINSD's page written out with
fzero-p, fnan-p, flt. A body that has one binds mxcsr and fpx and ends with
x86-fp-end: the flags into MXCSR, #XM for an unmasked one.
x86f-float is true of such a body (isel leaves floats to the derivation
prover, docs/meta.md "Floats"). The test x86_fpdiff runs every read float
form against the building machine's CPU (tests/isa/x86/fpdiff.c: its C
intrinsics on random operands under a random MXCSR): equal results and
MXCSR.@signed in its override (L-5).Per form, the Operation section (the part of it whose header names the form's
mnemonic and encoding: VPADDB (VEX.256 Encoded Instruction)) is parsed
(code.c, ported from ScarletNX's sdm_code.c) and partially evaluated:
what the form fixes (OperandSize, the mnemonic, REX.W, VEX.L, 64-bit mode) is
decided, so IF OperandSize = 64 keeps one branch and a FOR of constant
bounds is unrolled; what depends on operand values becomes Lisp. Values are
exact integers; an assignment wraps to its destination's width. A procedure
the page defines (LOGICAL_LEFT_SHIFT_WORDS(SRC, COUNT_SRC)) is inlined where
it is called. Two loop shapes are put in closed form, in the canonical
spellings: the shift and rotate loops (SAL/SHR/SAR/ROL/ROR, SARX/SHLX/SHRX:
while count != 0 ... value * 2 ... count - 1) become ash/rotl/rotr by the
count, with what the last step sets (CF) taken from the value one step short;
the bit-scan loops (BSF, BSR, LZCNT, TZCNT) become ctz/clz. Any other loop
the values decide is a loop. Nothing is guessed: a name, function, statement or
condition the reader doesn't know makes the form :unread with why, and so
does an elided repetition ((* Repeat ... *)) it can't expand.
The overrides are tools/sdmgen/sdm/*.sdm: a page of only the sections it
replaces (@opcodes, @encoding, @operation, @flags, or @signed), each
saying which erratum it is. E-n are vibe86's numbers, S-n ScarletNX's (its
tools/sailgen/sdm/README.md; the overrides marked so are ported from it),
L-n found here.
| # | Page | What the text says | What is meant | Handled by |
|---|---|---|---|---|
| E-1 | DIV | the remainder AH := AX MOD SRC after AL has changed AX | the remainder of the original dividend | DIV.sdm |
| E-2 | IDIV | as E-1, and a range check on constants read as unsigned | signed comparisons; the 8- and 16-bit forms left unread | IDIV.sdm |
| E-4, E-18 | CMPXCHG | flags 'according to the comparison', none made; accumulator a word | CMP's flags; AL/AX/EAX/RAX | CMPXCHG.sdm |
| E-7 | CMOVcc | CMOVG r64's modes V/N.E. | valid in 64-bit mode | forms.c |
| E-9 | IMUL | the product's signedness in a comment | a signed product | code.c reads the comment |
| E-10 | MOV | prose (segment loads) after DEST := SRC; | the GPR rows' first line | MOV.sdm |
| E-13 | CMC | EFLAGS.CF[bit 0] := NOT EFLAGS.CF[bit 0] | CF := NOT CF | CMC.sdm |
| E-16 | SETcc | rows without /r or /0 | a ModRM byte, /0 | forms.c, defx86.lisp |
| E-17 | CWD/CDQ/CQO | DX := SignExtend(AX) | the sign half of the extension | CWD_CDQ_CQO.sdm |
| S-0 | (3.1) | the notation chapter's example CMC page | not a page | pages.c |
| S-1..S-4 | RCL/RCR/ROL/ROR | four instructions' code run together; LSB(SRC); a lost superscript; MSB − 1 | each under its instruction | RCL_RCR_ROL_ROR.sdm |
| S-5 | SAL/SAR/SHL/SHR, SARX | a division's signedness in a comment | signed for SAR | code.c, lisp.c |
| S-7 | PINSRB/D/Q, PEXTRB/D/Q, PINSRW | (pdftotext) rows run together | one row each | the three .sdm |
| S-8 | CVTDQ2PS, ROUNDPS | (pdftotext) rows misread | the legacy rows | CVTDQ2PS.sdm, ROUNDPS.sdm |
| S-9 | PAVGB | 66 0F E0, /r | no comma | forms.c |
| S-10 | PMOVSX/ZX | 66 0f 38 20 | capitals | forms.c |
| S-11 | GF2P8AFFINEQB | 66 0F3A CE /r /ib | 66 0F 3A CE /r ib | GF2P8AFFINEQB.sdm; forms.c, defx86.lisp read 0F38 too |
| S-12 | GF2P8AFFINEQB, VSM4* | a CPUID cell of two lines | both | the .sdm |
| S-13 | SHLD/SHRD | (pdftotext) MRC Valid in one cell | Op/En, then the mode | forms.c |
| L-1 | POPCNT | a C For (...) { ... } loop | DEST := BitCount(SRC) | POPCNT.sdm |
| L-2, L-3 | MOVBE | ELES IF; DEST[31:23] := TEMP[7:0] | ELSE IF; DEST[31:24] | MOVBE.sdm |
| L-4 | XCHG | the MR form's ModRM:reg (r), the RM form's (w)/(r) | both operands read and written | XCHG.sdm |
| L-5 | PMAXS*, PMINS*, PCMPGT*, PMADDWD, PMULHW, PMULDQ, PSRA*, PABS*, PSIGN* | untyped lanes | signed integers | @signed overrides |
| L-6 | MOVLPD | VEX row B's destination ModRM:r/m (r) | ModRM:reg (w) | MOVLPD.sdm |
| L-7 | BT/BTS/BTR/BTC | Bit(BitBase, BitOffset) | the offset MOD OperandSize (the Description; register bit bases) | BT*.sdm |
| L-8 | PMINUB | the 128-bit section's DEST[15:0] := SRC[7:0] | DEST[7:0] | not mended: the form stays unread (the lane run differs) |
| L-9 | VMASKMOVPS/PD, VPERM2F128 | two operands in one Op/En field | rows unread | |
| L-10 | CMP | temp := SRC1 − SignExtend(SRC2), a variable of no width, then flags "as SUB" | the difference at the operand size: ZF, SF, PF of temp wrapped to it | lisp.c (a sum's temporary is read back wrapped) |
| L-11 | HADDPD, HADDPS | the sums written high element + low element | the CPU takes the low element as the first source (the NaN it propagates; x86_fpdiff) | HADDPD.sdm, HADDPS.sdm |
| L-12 | SQRTSS | the legacy version's SQRT(SRC2[31:0]) | its source, SRC | SQRTSS.sdm |
| L-13 | VFMADDSUB*PD, VFMSUBADD*PD | the VEX.256 versions' last RoundFPControl_MXCSR( unclosed | closed | VFMADDSUB132PD.sdm, VFMSUBADD132PD.sdm |
| L-14 | VFMADD132SD | DEST[63:0] := MAXVL-1:128RoundFPControl_MXCSR(...), DEST[127:63] := DEST[127:63] | no MAXVL-1:128; DEST[127:64] | VFMADD132SD.sdm |
| L-15 | VFMSUB*PS, VFNMADD*PS, VFNMSUB*PS | VEX loops of MAXNUM := 2 / 4 elements of 32 bits | 4 / 8 (the CPU, x86_fpdiff) | the three .sdm |
| L-16 | CVTDQ2PS, CVTDQ2PD, CVTPD2PS, CVTPS2DQ, CVTPS2PD, CVTTPD2DQ, CVTTPS2DQ | VEX slices closed by a parenthesis, SRC[127:96), SRC[127z:96) | SRC[127:96]) | the seven .sdm |
| L-18 | ROUNDPS, ROUNDPD | RoundToInteger(SRC[95:64]], ...); ROUNDPS(128-bit Legacy SSE Version) | one bracket; a header | ROUNDPS.sdm, ROUNDPD.sdm |
| L-19 | COMISD, COMISS, UCOMISD, UCOMISS | RESULT := OrderedCompare(DEST[63:0] <> SRC[63:0]) { CASE (RESULT) OF UNORDERED: ZF,PF,CF := 111; ... } | the compare of two operands, then each flag | the four .sdm |
Floating-point pages (a title that says Floating-Point, a precision, or
Fused ... Multiply): + - * / of two 32- or 64-bit lanes are x86-fp with
fadd fsub fmul fdiv, SQRT is fsqrt, MIN/MAX the pages' procedures
as x86-fp-min/x86-fp-max, RoundFPControl_MXCSR(A*B + C) (and -,
-(A*B)) one x86-fp-fma; the FMA pages' For i = 0 to MAXNUM-1 { ... }
loops are read as FOR ... ROF, their VEX.128/VEX.256 as the form's
length, their operands as DEST, SRC2, SRC3. Convert_<From>_To_<To>[_Truncate]
is x86-fp-cvt, x86-fp-from-int or x86-fp-to-int (Integer: the
general-purpose operand's size, else 32 bits); CMPSD's DEST[63:0] OP3
SRC[63:0] is x86-fp-cmp of the predicate imm8[2:0] (OP5: imm8[4:0]) whose
table the page's CASE (COMPARISON PREDICATE) gives; OrderedCompare and
UnorderedCompare are x86-fp-compare; RoundToInteger(x, ROUND_CONTROL)
is x86-fp-round. A legacy float form of two registers and an immediate
whose section says SRC1 and SRC2 (CMPPD's) means its destination and its
r/m. A header may be (V)CVTSD2SI (the legacy form and its VEX one), and a
condition's And is AND (CVTSI2SD's 64-Bit Mode And OperandSize = 64).
A line NAME(ARG) defines a procedure only when its arguments are names
(CVTDQ2PD's call continued on a line of its own is none).
Found while reading but handled in the generator: one-line ELSE x;
without FI (PBLENDVB), DOWN TO (SHLD), m := 0, k := 0; (PDEP),
CASE ... of in lower case (VPERM2I128), ELSEIF (RORX), CF:DEST pairs
(ADCX).
gen/*.lisp ends with its counts; *x86-unread* lists the rest. EVEX
(AVX-512), MMX, x87, system and segment operands, VSIB, far pointers and
branches (rel: control flow is the recompiler's, not a semantics) are rows
not read as forms. Next:
LOGICAL_RIGHT_SHIFT_QWORDS_256b);EffectiveAddress), MOVS/STOS and other memory forms;APPROXIMATE), the half-precision conversions (VCVTPH2PS's
vCvt_h2s, VCVTPS2PH), VCVTNEPS2BF16, MOVMSKPD/PS.state.lisp declares the register files with defregs (gpr, xmm,
ymmh, rflags with its six status fields, mxcsr with its fields), the
faults with defexceptions (kinds by meaning, codes = the vector + 1),
memory with defmem x86 :order :le :addr-width 64. The six flag slots are
one rflags slot; x86-cf ... x86-of, x86-flag, x86-set-flag are
readers for code outside a body (tests).defx86's function has the state type in scope (with-isa (x86)); a
body's flag variables read (reg-field rflags cf) and are written back
by one set-reg-fields; GPR writes are set-reg-ext (:zero for 32 and
64 bits, :keep for 8 and 16).borrow-out, sub-overflow, even-parity,
(raise :kind), and (undefined 1) for a flag the SDM leaves undefined
((:undefined ...) stays as documentation); before, the body kept the
old value.(defmodel host :family x86 :isa x86 :role :host ...)
(cmake/hostcpu.cmake, hostall.lisp); defhostcpu and x86-host-has-p
are deprecated aliases.Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.