x86

1791 definitions in 26 files, 0 with an encoding diagram. src/isa/x86

Files

FileDefinitionsOtherWhat
hostall.lisp01a build CPU with every feature, for machine-independent output.
package.lisp00the ISA.X86 package: the x86-64 host description.
state.lisp018the x86-64 machine state the semantics bodies read and write.
flags.lisp05x86's old spellings of core forms (deprecated aliases).
fp.lisp023x86's floating-point rules around core's IEEE primitives (SSE, AVX, FMA).
defx86.lisp02the x86-64 instruction form: encoding, operands, semantics.
hostcpu.lisp00the CPU a build is for: which x86-64 forms it can run.
gen/gp.lisp8580x86-64 general-purpose instruction forms
gen/sse.lisp2780x86-64 legacy SSE forms
gen/avx.lisp6550x86-64 VEX (AVX, AVX2, BMI) forms
gen/unread.lisp01x86-64 rows of the SDM not read, with why
enc.lisp014the x86-64 emitter and decoder, from the forms' encoding words.
tables.lisp00the encoding table of every form defined before it.
cost/xclass.lisp01ucas's x86-64 instruction classes (generated, do not edit).
cost.lisp017what each x86-64 form costs under the selected cost model.
cost/glc.lisp00x86-64 cost model glc, Intel Golden Cove P-core as in Sapphire Rapids (generated, do not edit).
cost/rpl.lisp00x86-64 cost model rpl, Intel Raptor Lake / Alder Lake P-core (Golden Cove/Raptor Cove) (generated, do not edit).
cost/skl.lisp00x86-64 cost model skl, Intel Skylake/Kaby/Coffee Lake client core (this host's i5-9400 is one) (generated, do not edit).
cost/skx.lisp00x86-64 cost model skx, Intel Skylake-SP/X server core (generated, do not edit).
cost/x86v2.lisp00x86-64 cost model x86v2, generic x86-64-v2 (SSE4.2, POPCNT) (generated, do not edit).
cost/x86v3.lisp00x86-64 cost model x86v3, generic x86-64-v3 (AVX2, FMA, BMI1/2, LZCNT, MOVBE) (generated, do not edit).
cost/x86v4.lisp00x86-64 cost model x86v4, generic x86-64-v4 (AVX-512 F/BW/CD/DQ/VL) (generated, do not edit).
cost/znver1.lisp00x86-64 cost model znver1, AMD Zen+ (Ryzen 5 3550H, Picasso) (generated, do not edit).
cost/znver2.lisp00x86-64 cost model znver2, AMD Zen 2 (Ryzen 5 3600, Steam Deck's Custom APU 0932) (generated, do not edit).
cost/znver3.lisp00x86-64 cost model znver3, AMD Zen 3 (Ryzen 7 5825U, Cezanne) (generated, do not edit).
cost/znver4.lisp00x86-64 cost model znver4, AMD Zen 4 (generated, do not edit).

README

src/isa/x86: x86-64

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:

FileWhat
package.lispISA.X86
state.lispx86: the state a semantics body reads and writes (16 GPRs, 16 xmm/ymm, the six flags, a fault, mval)
flags.lispspellings core doesn't have yet: x86-borrow-out, x86-sub-overflow, x86-even-parity, x86-lane, x86-vmap256
fp.lispfloating 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.lispthe defx86 form, its registry, the opcode column read into an encoding word
gen/gp.lispthe general-purpose forms (generated)
gen/sse.lispthe legacy-encoded vector forms (generated)
gen/avx.lispthe VEX forms: AVX, AVX2, BMI1/2, FMA (generated)
gen/unread.lisp*x86-unread*: every form or row not read, with why (generated)
enc.lispx86-emit, x86-emit-mem, x86-decode over the encoding words
tables.lisp*x86-enc*: the encoding word of every form, by index
cost/xclass.lispucas's instruction classes (generated by tools/costgen)
cost/<model>.lispone cost model per µarch: znver1..znver4, skl, skx, glc, rpl, x86v2..x86v4 (generated); the build loads XSPE_COST_CPU (default znver4)
cost.lispx86-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.

Costs

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.

The form

(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)))

Conventions

How the pseudocode is read (tools/sdmgen/lisp.c)

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.

Errata

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.

#PageWhat the text saysWhat is meantHandled by
E-1DIVthe remainder AH := AX MOD SRC after AL has changed AXthe remainder of the original dividendDIV.sdm
E-2IDIVas E-1, and a range check on constants read as unsignedsigned comparisons; the 8- and 16-bit forms left unreadIDIV.sdm
E-4, E-18CMPXCHGflags 'according to the comparison', none made; accumulator a wordCMP's flags; AL/AX/EAX/RAXCMPXCHG.sdm
E-7CMOVccCMOVG r64's modes V/N.E.valid in 64-bit modeforms.c
E-9IMULthe product's signedness in a commenta signed productcode.c reads the comment
E-10MOVprose (segment loads) after DEST := SRC;the GPR rows' first lineMOV.sdm
E-13CMCEFLAGS.CF[bit 0] := NOT EFLAGS.CF[bit 0]CF := NOT CFCMC.sdm
E-16SETccrows without /r or /0a ModRM byte, /0forms.c, defx86.lisp
E-17CWD/CDQ/CQODX := SignExtend(AX)the sign half of the extensionCWD_CDQ_CQO.sdm
S-0(3.1)the notation chapter's example CMC pagenot a pagepages.c
S-1..S-4RCL/RCR/ROL/RORfour instructions' code run together; LSB(SRC); a lost superscript; MSB − 1each under its instructionRCL_RCR_ROL_ROR.sdm
S-5SAL/SAR/SHL/SHR, SARXa division's signedness in a commentsigned for SARcode.c, lisp.c
S-7PINSRB/D/Q, PEXTRB/D/Q, PINSRW(pdftotext) rows run togetherone row eachthe three .sdm
S-8CVTDQ2PS, ROUNDPS(pdftotext) rows misreadthe legacy rowsCVTDQ2PS.sdm, ROUNDPS.sdm
S-9PAVGB66 0F E0, /rno commaforms.c
S-10PMOVSX/ZX66 0f 38 20capitalsforms.c
S-11GF2P8AFFINEQB66 0F3A CE /r /ib66 0F 3A CE /r ibGF2P8AFFINEQB.sdm; forms.c, defx86.lisp read 0F38 too
S-12GF2P8AFFINEQB, VSM4*a CPUID cell of two linesboththe .sdm
S-13SHLD/SHRD(pdftotext) MRC Valid in one cellOp/En, then the modeforms.c
L-1POPCNTa C For (...) { ... } loopDEST := BitCount(SRC)POPCNT.sdm
L-2, L-3MOVBEELES IF; DEST[31:23] := TEMP[7:0]ELSE IF; DEST[31:24]MOVBE.sdm
L-4XCHGthe MR form's ModRM:reg (r), the RM form's (w)/(r)both operands read and writtenXCHG.sdm
L-5PMAXS*, PMINS*, PCMPGT*, PMADDWD, PMULHW, PMULDQ, PSRA*, PABS*, PSIGN*untyped lanessigned integers@signed overrides
L-6MOVLPDVEX row B's destination ModRM:r/m (r)ModRM:reg (w)MOVLPD.sdm
L-7BT/BTS/BTR/BTCBit(BitBase, BitOffset)the offset MOD OperandSize (the Description; register bit bases)BT*.sdm
L-8PMINUBthe 128-bit section's DEST[15:0] := SRC[7:0]DEST[7:0]not mended: the form stays unread (the lane run differs)
L-9VMASKMOVPS/PD, VPERM2F128two operands in one Op/En fieldrows unread
L-10CMPtemp := 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 itlisp.c (a sum's temporary is read back wrapped)
L-11HADDPD, HADDPSthe sums written high element + low elementthe CPU takes the low element as the first source (the NaN it propagates; x86_fpdiff)HADDPD.sdm, HADDPS.sdm
L-12SQRTSSthe legacy version's SQRT(SRC2[31:0])its source, SRCSQRTSS.sdm
L-13VFMADDSUB*PD, VFMSUBADD*PDthe VEX.256 versions' last RoundFPControl_MXCSR( unclosedclosedVFMADDSUB132PD.sdm, VFMSUBADD132PD.sdm
L-14VFMADD132SDDEST[63:0] := MAXVL-1:128RoundFPControl_MXCSR(...), DEST[127:63] := DEST[127:63]no MAXVL-1:128; DEST[127:64]VFMADD132SD.sdm
L-15VFMSUB*PS, VFNMADD*PS, VFNMSUB*PSVEX loops of MAXNUM := 2 / 4 elements of 32 bits4 / 8 (the CPU, x86_fpdiff)the three .sdm
L-16CVTDQ2PS, CVTDQ2PD, CVTPD2PS, CVTPS2DQ, CVTPS2PD, CVTTPD2DQ, CVTTPS2DQVEX slices closed by a parenthesis, SRC[127:96), SRC[127z:96)SRC[127:96])the seven .sdm
L-18ROUNDPS, ROUNDPDRoundToInteger(SRC[95:64]], ...); ROUNDPS(128-bit Legacy SSE Version)one bracket; a headerROUNDPS.sdm, ROUNDPD.sdm
L-19COMISD, COMISS, UCOMISD, UCOMISSRESULT := OrderedCompare(DEST[63:0] <> SRC[63:0]) { CASE (RESULT) OF UNORDERED: ZF,PF,CF := 111; ... }the compare of two operands, then each flagthe 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).

Coverage and TODO

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:

The normalized vocabulary (docs/norm.md)


Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.