module Accelerator.SM90.InstructionEncoding import Accelerator.SM86.Control import Accelerator.SM86.Instruction import Accelerator.SM86.Types import Accelerator.SM90.Instruction import Accelerator.SM90.UniformOperands import Accelerator.SM90.Word import Std.Natural -- The sm_90 encoder and decoder of Accelerator.SM90.Instruction. -- -- The encoder lays each operand into its field from the typed instruction -- and REFUSES an operand its field cannot hold. The decoder reads the -- fields back and then re-encodes them: it accepts a word only when that -- reproduces every one of its 128 bits, so a word with a stray modifier, a -- reserved bit or an unknown size code is refused as non-canonical instead -- of being read as something it is not. Hence encode (decode w) = w for -- every word the decoder accepts, by construction; the converse, -- decode (encode x) = x, is what the round-trip laws of -- tests/Proof/SM90InstructionEncodingLaws.alpha pin instance by instance. -- -- Field layouts (bit numbers of the 128-bit word; nvdisasm -hex's first -- 64-bit value is bits 0..63): -- -- LDG opcode 0x981 | Rd 16..23 | Ra 24..31 | UR 32..37 (URZ, 63) | -- offset 40..62 | .E 72 | size 73..75 | fixed 0x0c1e0000 at 64 -- STG opcode 0x986 | Ra 24..31 | Rv 32..39 | offset 40..62 | -- UR 64..69 (URZ) | .E 72 | size 73..75 | fixed 0x0c100000 at 64 -- (LDG and STG: bits 77..79 set for .STRONG.GPU, clear for the weak -- form -- ptxas -arch=sm_90a's ld/st.relaxed.gpu, scratchpad probe -- reference/oracles/nvidia-sm90-ptxas/global-branch/strong.sass) -- LDGSTS opcode 0xfae | Rs 16..23 | Rg 24..31 | source offset 32..43 | -- shared offset 44..63 (both signed) | UR 64..69 (URZ) | -- 0x0b900c40 at 64 -- BRA opcode 0x947 | displacement bits 2..9 at 16..23 and 10..57 at -- 34..81 | fixed 0x03800000 at 64 (bits 87..89) -- REDG opcode 0x9a6 | Ra 24..31 | Rv 32..39 | offset 40..62 | -- UR 64..69 (URZ) | descriptor flag 71 (clear) | .E 72 | fixed -- 0x0c10f33f at 64 (.ADD.F32.FTZ.RN.STRONG.GPU) -- -- with the guard at 12..15 and the scheduling control at 105..125 for all -- (Accelerator.SM90.Word). The size code is 4 for 32 bits, 5 for 64 and 6 -- for 128 (measured: ptxas -arch=sm_90a, reference/oracles/ -- nvidia-sm90-ptxas/global-branch/global.sass). The descriptor flag, -- bit 76, is clear in all three global forms: that is the raw form. family SM90Encoding : Type 0 constructor SM90Encoded field unrestricted sm90EncodedWord : SM90Word constructor SM90EncodingRefused field unrestricted sm90EncodingRefusal : SM90EncodingRefusal end-family family SM90Decoding : Type 0 constructor SM90Decoded field unrestricted sm90DecodedInstruction : SM90Instruction constructor SM90DecodingRefused field unrestricted sm90DecodingRefusal : SM90EncodingRefusal end-family def sm90Refuse = (lambda unrestricted reason : SM90EncodingRefusal . constructor SM90Encoding SM90EncodingRefused reason) def sm90Emit = (lambda unrestricted low : Nat . unrestricted high : Nat . constructor SM90Encoding SM90Encoded (sm90Word low high)) -- Refuse with `reason` unless `ok` (a 0/1 flag), else continue. def sm90Require = (lambda unrestricted ok : Nat . unrestricted reason : SM90EncodingRefusal . unrestricted continue : SM90Encoding . sm90Choose SM90Encoding ok continue (sm90Refuse reason)) -- The control fits its field. The encoder admits every control that -- fits, as the hardware's decoder does: nvdisasm's grammar is stricter (it -- rejects a store or BRA that names a write scoreboard, a BRA with a read -- scoreboard, and a yield with stall 0 or 15 -- reference/oracles/ -- nvidia-sm90-ptxas/global-branch/README.md), but the RTX 3070 runs such -- words from the SM86 encoders, so refusing them here would change what -- an SM86 program means rather than how it is encoded. Whether a control -- is sound is the scoreboard model's question (Accelerator.SM86.Operands -- and the checked machine models), not the encoder's. def sm90ControlFits = (lambda unrestricted control : SM86Control . nat-less-than (sm90ControlBits control) sm90ControlSpan) -- --------------------------------------------------------------------------- -- Opcodes and fixed bits def sm90OpcodeLoadGlobal : Nat = 0x981 def sm90OpcodeStoreGlobal : Nat = 0x986 def sm90OpcodeCopyGlobalToShared : Nat = 0xfae def sm90OpcodeBranch : Nat = 0x947 def sm90OpcodeReduceGlobal : Nat = 0x9a6 -- URZ is owned by UniformOperands; a raw load has no descriptor or offset. def sm90DestinationPlace : Nat = (compile-time (sm90Place 16)) def sm90SourcePlace : Nat = (compile-time (sm90Place 24)) def sm90SecondSourcePlace : Nat = (compile-time (sm90Place 32)) def sm90GlobalOffsetPlace : Nat = (compile-time (sm90Place 40)) -- the global offset's non-negative values (the 24-bit immediate is signed) def sm90GlobalOffsetLimit : Nat = (compile-time (sm90Place 23)) -- LDGSTS: the source offset at 32 and the shared offset at 44 (bits -- 44..63) are signed; their non-negative halves are below 2^11 and 2^19. -- nvdisasm -b SM90 reads a shared offset of 0xfffff as -0x1 -- (reference/oracles/nvidia-sm90-ptxas/global-branch/README.md), so the -- encoder refuses 2^19 and above instead of addressing below the base. def sm90CopySourceOffsetLimit : Nat = (compile-time (sm90Place 11)) def sm90CopySharedOffsetPlace : Nat = (compile-time (sm90Place 44)) def sm90CopySharedOffsetLimit : Nat = (compile-time (sm90Place 19)) -- The size code, bits 73..75 (high bits 9..11). def sm90SizePlace : Nat = 512 def sm90Size32 : Nat = 4 def sm90Size64 : Nat = 5 def sm90Size128 : Nat = 6 -- High bits 0..31 of each form less its size code: LDG.E and STG.E as ptxas -- writes them for sm_90a (0x0c1e1900, 0x0c101904) with the descriptor flag -- (bit 76, 0x1000) cleared and, for STG, the uniform field (bits 64..69) -- naming URZ in place of UR4. Bit 72 (0x100) is .E, the 64-bit address. def sm90LoadGlobalFixed : Nat = 0x0c1e0100 def sm90StoreGlobalFixed : Nat = 0x0c10013f -- LDGSTS.E.BYPASS.128 as ptxas writes it (0x0b901c44) with bit 76 clear -- and URZ at 64..69; bit 70 (0x40) is kept as ptxas sets it. def sm90CopyGlobalToSharedFixed : Nat = 0x0b900c7f -- REDG.E.ADD.F32.FTZ.RN.STRONG.GPU as ptxas writes it (0x0c10f384, -- red.sass) with RED's descriptor flag (bit 71, 0x80) cleared and URZ at -- 64..69 in place of UR4 def sm90ReduceGlobalAddFloat32Fixed : Nat = 0x0c10f33f -- BRA's bits 87..89 as ptxas writes every BRA (0x03800000 at 64) def sm90BranchFixed : Nat = 0x03800000 -- a register group of `width` registers (1, 2, 4) starting at `register`: -- one register is any (RZ included: STG [Ra.64], RZ stores zero); a pair or -- quad is aligned to its width and ends below RZ def sm90RegisterGroupAdmitted = (lambda unrestricted width : Nat . unrestricted register : SM86Register . let unrestricted n = (sm90RegisterNumber register) in naturalOr (naturalEqual width 1) (pipe n (nat-modulo width) naturalIsZero (naturalAnd (naturalLessOrEqual (nat-add n width) 255)))) def sm90LoadRegisters = (lambda unrestricted width : (family SM90GlobalLoadWidth) . (eliminate SM90GlobalLoadWidth (lambda unrestricted current : (family SM90GlobalLoadWidth) . Nat) width (branch SM90GlobalLoad32 . 1) (branch SM90GlobalLoad128 . 4) (branch SM90GlobalLoad32Strong . 1) (branch SM90GlobalLoad128Strong . 4) (branch SM90GlobalLoad128ReadOnly . 4))) def sm90StoreRegisters = (lambda unrestricted width : SM90GlobalStoreWidth . eliminate SM90GlobalStoreWidth (lambda unrestricted current : SM90GlobalStoreWidth . Nat) width (branch SM90GlobalStore32 . 1) (branch SM90GlobalStore64 . 2) (branch SM90GlobalStore128 . 4) (branch SM90GlobalStore32Strong . 1)) -- the strength bits (77..79, high bits 13..15): 7 strong at the GPU's -- scope, 0 weak def sm90StrengthPlace : Nat = 8192 def sm90StrongGPU : Nat = 7 def sm90LoadStrength = (lambda unrestricted width : (family SM90GlobalLoadWidth) . (eliminate SM90GlobalLoadWidth (lambda unrestricted current : (family SM90GlobalLoadWidth) . Nat) width (branch SM90GlobalLoad32 . 0) (branch SM90GlobalLoad128 . 0) (branch SM90GlobalLoad32Strong . sm90StrongGPU) (branch SM90GlobalLoad128Strong . sm90StrongGPU) (branch SM90GlobalLoad128ReadOnly . 4))) def sm90StoreStrength = (lambda unrestricted width : SM90GlobalStoreWidth . eliminate SM90GlobalStoreWidth (lambda unrestricted current : SM90GlobalStoreWidth . Nat) width (branch SM90GlobalStore32 . 0) (branch SM90GlobalStore64 . 0) (branch SM90GlobalStore128 . 0) (branch SM90GlobalStore32Strong . sm90StrongGPU)) def sm90LoadSize = (lambda unrestricted width : (family SM90GlobalLoadWidth) . (eliminate SM90GlobalLoadWidth (lambda unrestricted current : (family SM90GlobalLoadWidth) . Nat) width (branch SM90GlobalLoad32 . sm90Size32) (branch SM90GlobalLoad128 . sm90Size128) (branch SM90GlobalLoad32Strong . sm90Size32) (branch SM90GlobalLoad128Strong . sm90Size128) (branch SM90GlobalLoad128ReadOnly . sm90Size128))) def sm90StoreSize = (lambda unrestricted width : SM90GlobalStoreWidth . eliminate SM90GlobalStoreWidth (lambda unrestricted current : SM90GlobalStoreWidth . Nat) width (branch SM90GlobalStore32 . sm90Size32) (branch SM90GlobalStore64 . sm90Size64) (branch SM90GlobalStore128 . sm90Size128) (branch SM90GlobalStore32Strong . sm90Size32)) -- Branch displacement fields: bits 2..9 at 16 (8 bits); bits 10..39 at 34 -- (30 bits, the low half's top); bits 40..57 at 64 (18 bits). def sm90BranchLowPlace : Nat = (compile-time (sm90Place 16)) def sm90BranchMiddlePlace : Nat = (compile-time (sm90Place 34)) def sm90BranchMiddleSpan : Nat = (compile-time (sm90Place 30)) def sm90BranchHighSpan : Nat = (compile-time (sm90Place 18)) def sm90BranchHighUnit : Nat = (compile-time (sm90Place 40)) -- the displacement's two's complement is 58 bits; the forward half below def sm90BranchModulus : Nat = (compile-time (sm90Place 58)) def sm90BranchForwardLimit : Nat = (compile-time (sm90Place 57)) -- --------------------------------------------------------------------------- -- Encoders -- ptxas 12.9.86 maps L1::no_allocate to SASS LDG.NA bit 86. -- This cache hint neither changes loaded values nor supplies a memory fence. def sm90LoadAllocationBits = (lambda unrestricted allocation : (family SM90LoadAllocation) . (eliminate SM90LoadAllocation (lambda unrestricted current : (family SM90LoadAllocation) . Nat) allocation (branch SM90LoadAllocate . 0) (branch SM90LoadNoAllocate . 0x400000))) def sm90EncodeLoadGlobal = (lambda unrestricted guard : (family SM86InstructionGuard) . (lambda unrestricted width : (family SM90GlobalLoadWidth) . (lambda unrestricted allocation : (family SM90LoadAllocation) . (lambda unrestricted destination : (family SM86Register) . (lambda unrestricted address : (family SM86Register) . (lambda unrestricted offset : Nat . (lambda unrestricted control : (family SM86Control) . (sm90Require (sm90RegisterGroupAdmitted 2 address) (constructor SM90EncodingRefusal SM90RefusalAddressUnaligned) (sm90Require (sm90RegisterGroupAdmitted (sm90LoadRegisters width) destination) (constructor SM90EncodingRefusal SM90RefusalDataUnaligned) (sm90Require (nat-less-than offset sm90GlobalOffsetLimit) (constructor SM90EncodingRefusal SM90RefusalGlobalOffsetOutOfRange) (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange) (sm90Emit (nat-add (sm90HeaderLow sm90OpcodeLoadGlobal guard) (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace) (nat-add (sm90Put (sm90RegisterNumber address) sm90SourcePlace) (nat-add (sm90Put sm90UniformZero sm90SecondSourcePlace) (sm90Put offset sm90GlobalOffsetPlace))))) (nat-add (sm90HeaderHigh control) (nat-add (nat-add sm90LoadGlobalFixed (sm90LoadAllocationBits allocation)) (nat-add (sm90Put (sm90LoadSize width) sm90SizePlace) (sm90Put (sm90LoadStrength width) sm90StrengthPlace)))))))))))))))) -- PTX atom.relaxed.gpu.global.add.u32, ptxas 12.9.86. Fixed ADD, -- unsigned 32-bit operand, PT status destination and explicit descriptor. -- Only zero immediate offset is admitted; callers form the full address. def sm90EncodeFetchAddGlobal32 = (lambda unrestricted guard : (family SM86InstructionGuard) . (lambda unrestricted destination : (family SM86Register) . (lambda unrestricted address : (family SM86Register) . (lambda unrestricted value : (family SM86Register) . (lambda unrestricted descriptor : (family SM90UniformRegister) . (lambda unrestricted offset : Nat . (lambda unrestricted control : (family SM86Control) . (let unrestricted uniform = (sm90UniformRegisterNumberOf descriptor) in (sm90Require (sm90RegisterGroupAdmitted 2 address) (constructor SM90EncodingRefusal SM90RefusalAddressUnaligned) (sm90Require (naturalAnd (naturalLess uniform 62) (naturalIsZero (nat-modulo uniform 2))) (constructor SM90EncodingRefusal SM90RefusalAddressUnaligned) (sm90Require (naturalIsZero offset) (constructor SM90EncodingRefusal SM90RefusalGlobalOffsetOutOfRange) (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange) (sm90Emit (nat-add (sm90HeaderLow 0x9a8 guard) (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace) (nat-add (sm90Put (sm90RegisterNumber address) sm90SourcePlace) (nat-add (sm90Put (sm90RegisterNumber value) sm90SecondSourcePlace) (sm90Put offset sm90GlobalOffsetPlace))))) (nat-add (sm90HeaderHigh control) (nat-add 0x081ee1c0 uniform))))))))))))))) def sm90EncodeStoreGlobal = (lambda unrestricted guard : SM86InstructionGuard . unrestricted width : SM90GlobalStoreWidth . unrestricted address : SM86Register . unrestricted value : SM86Register . unrestricted offset : Nat . unrestricted control : SM86Control . then (sm90Require (sm90RegisterGroupAdmitted 2 address) SM90RefusalAddressUnaligned) (sm90Require (sm90RegisterGroupAdmitted (sm90StoreRegisters width) value) SM90RefusalDataUnaligned) (sm90Require (nat-less-than offset sm90GlobalOffsetLimit) SM90RefusalGlobalOffsetOutOfRange) (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange) (sm90Emit (nest nat-add (sm90HeaderLow sm90OpcodeStoreGlobal guard) (sm90Put (sm90RegisterNumber address) sm90SourcePlace) (sm90Put (sm90RegisterNumber value) sm90SecondSourcePlace) (sm90Put offset sm90GlobalOffsetPlace))) (nest nat-add (sm90HeaderHigh control) sm90StoreGlobalFixed (sm90Put (sm90StoreSize width) sm90SizePlace)) (sm90Put (sm90StoreStrength width) sm90StrengthPlace)) def sm90EncodeReduceGlobalAddFloat32 = (lambda unrestricted guard : SM86InstructionGuard . unrestricted address : SM86Register . unrestricted value : SM86Register . unrestricted offset : Nat . unrestricted control : SM86Control . then (sm90Require (sm90RegisterGroupAdmitted 2 address) SM90RefusalAddressUnaligned) (sm90Require (nat-less-than offset sm90GlobalOffsetLimit) SM90RefusalGlobalOffsetOutOfRange) (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange) (sm90Emit (nest nat-add (sm90HeaderLow sm90OpcodeReduceGlobal guard) (sm90Put (sm90RegisterNumber address) sm90SourcePlace) (sm90Put (sm90RegisterNumber value) sm90SecondSourcePlace) (sm90Put offset sm90GlobalOffsetPlace))) (nat-add (sm90HeaderHigh control)) sm90ReduceGlobalAddFloat32Fixed) def sm90EncodeCopyGlobalToShared = (lambda unrestricted guard : SM86InstructionGuard . unrestricted address : SM86Register . unrestricted offset : Nat . unrestricted source : SM86Register . unrestricted sourceOffset : Nat . unrestricted control : SM86Control . then (sm90Require (sm90RegisterGroupAdmitted 2 source) SM90RefusalAddressUnaligned) (sm90Require (naturalAnd (nat-less-than sourceOffset sm90CopySourceOffsetLimit) (nat-less-than offset sm90CopySharedOffsetLimit)) SM90RefusalCopyOffsetOutOfRange) (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange) (sm90Emit (nest nat-add (sm90HeaderLow sm90OpcodeCopyGlobalToShared guard) (sm90Put (sm90RegisterNumber address) sm90DestinationPlace) (sm90Put (sm90RegisterNumber source) sm90SourcePlace) (sm90Put sourceOffset sm90SecondSourcePlace) (sm90Put offset sm90CopySharedOffsetPlace))) (nat-add (sm90HeaderHigh control)) sm90CopyGlobalToSharedFixed) -- The displacement's 58-bit two's complement, or the modulus itself when -- it is out of range (no displacement encodes to it). def sm90BranchComplement = (lambda unrestricted target : SM90BranchTarget . eliminate SM90BranchTarget (lambda unrestricted current : SM90BranchTarget . Nat) target (branch SM90BranchForward bytes . naturalSelect (nat-less-than bytes sm90BranchForwardLimit) bytes sm90BranchModulus) (branch SM90BranchBackward bytes . naturalSelect (naturalAnd (nat-less-than 0 bytes) (naturalLessOrEqual bytes sm90BranchForwardLimit)) (nat-subtract sm90BranchModulus bytes) sm90BranchModulus)) def sm90BranchBytes = (lambda unrestricted target : SM90BranchTarget . eliminate SM90BranchTarget (lambda unrestricted current : SM90BranchTarget . Nat) target (branch SM90BranchForward bytes . bytes) (branch SM90BranchBackward bytes . bytes)) def sm90EncodeBranch = (lambda unrestricted guard : SM86InstructionGuard . unrestricted target : SM90BranchTarget . unrestricted control : SM86Control . let unrestricted complement = (sm90BranchComplement target) in then (sm90Require (pipe target sm90BranchBytes (nat-modulo 4) (naturalEqual 0)) SM90RefusalBranchUnaligned) (sm90Require (nat-less-than complement sm90BranchModulus) SM90RefusalBranchOutOfRange) (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange) (sm90Emit (nest nat-add (sm90HeaderLow sm90OpcodeBranch guard) (pipe complement (nat-divide 4) (nat-modulo sm90Span8) (sm90Put sm90BranchLowPlace)) (pipe complement (nat-divide 1024) (nat-modulo sm90BranchMiddleSpan) (sm90Put sm90BranchMiddlePlace)))) (nest nat-add (sm90HeaderHigh control) sm90BranchFixed) (nat-divide complement sm90BranchHighUnit)) -- Observable independent ptxas/nvdisasm probes establish these fields. -- Immediate PRMT: 0x816; register-selected PRMT: 0x216. The right -- operand is in bits64..71; all default-mode bits72..104 are zero. def sm90SelectorValue = (lambda unrestricted selector : (family SM90ByteSelector) . (eliminate SM90ByteSelector (lambda unrestricted current : (family SM90ByteSelector) . Nat) selector (branch SM90ByteSelectorImmediate value . value) (branch SM90ByteSelectorRegister register . (sm90RegisterNumber register)))) def sm90SelectorFits = (lambda unrestricted selector : (family SM90ByteSelector) . (eliminate SM90ByteSelector (lambda unrestricted current : (family SM90ByteSelector) . Nat) selector (branch SM90ByteSelectorImmediate value . (naturalLess value 65536)) (branch SM90ByteSelectorRegister register . 1))) def sm90SelectorOpcode = (lambda unrestricted selector : (family SM90ByteSelector) . (eliminate SM90ByteSelector (lambda unrestricted current : (family SM90ByteSelector) . Nat) selector (branch SM90ByteSelectorImmediate value . 0x816) (branch SM90ByteSelectorRegister register . 0x216))) def sm90EncodePermute = (lambda unrestricted guard : (family SM86InstructionGuard) . (lambda unrestricted destination : (family SM86Register) . (lambda unrestricted left : (family SM86Register) . (lambda unrestricted right : (family SM86Register) . (lambda unrestricted selector : (family SM90ByteSelector) . (lambda unrestricted control : (family SM86Control) . (sm90Require (sm90SelectorFits selector) (constructor SM90EncodingRefusal SM90RefusalSelectorOutOfRange) (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange) (sm90Emit (nat-add (sm90HeaderLow (sm90SelectorOpcode selector) guard) (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace) (nat-add (sm90Put (sm90RegisterNumber left) sm90SourcePlace) (sm90Put (sm90SelectorValue selector) sm90SecondSourcePlace)))) (nat-add (sm90HeaderHigh control) (sm90RegisterNumber right))))))))))) def sm90EncodeLogicThree = (lambda unrestricted guard : (family SM86InstructionGuard) . (lambda unrestricted destination : (family SM86Register) . (lambda unrestricted first : (family SM86Register) . (lambda unrestricted second : (family SM86Register) . (lambda unrestricted third : (family SM86Register) . (lambda unrestricted truth : Byte . (lambda unrestricted control : (family SM86Control) . (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange) (sm90Emit (nat-add (sm90HeaderLow 0x212 guard) (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace) (nat-add (sm90Put (sm90RegisterNumber first) sm90SourcePlace) (sm90Put (sm90RegisterNumber second) sm90SecondSourcePlace)))) (nat-add (sm90HeaderHigh control) (nat-add 0x078e0000 (nat-add (sm90RegisterNumber third) (nat-multiply (byte-to-nat truth) 256))))))))))))) def sm90EncodeInstruction = (lambda unrestricted instruction : (family SM90Instruction) . (eliminate SM90Instruction (lambda unrestricted current : (family SM90Instruction) . (family SM90Encoding)) instruction (branch SM90LoadGlobal guard width allocation destination address offset control . (sm90EncodeLoadGlobal guard width allocation destination address offset control)) (branch SM90FetchAddGlobal32 guard destination address value descriptor offset control . (sm90EncodeFetchAddGlobal32 guard destination address value descriptor offset control)) (branch SM90StoreGlobal guard width address value offset control . (sm90EncodeStoreGlobal guard width address value offset control)) (branch SM90CopyGlobalToShared guard address offset source sourceOffset control . (sm90EncodeCopyGlobalToShared guard address offset source sourceOffset control)) (branch SM90Branch guard target control . (sm90EncodeBranch guard target control)) (branch SM90ReduceGlobalAddFloat32 guard address value offset control . (sm90EncodeReduceGlobalAddFloat32 guard address value offset control)) (branch SM90PermuteBytes guard destination left right selector control . (sm90EncodePermute guard destination left right selector control)) (branch SM90LogicThree guard destination first second third truth control . (sm90EncodeLogicThree guard destination first second third truth control)))) -- --------------------------------------------------------------------------- -- Decoder -- Accept `candidate` only when it encodes back to `word`, bit for bit. def sm90DecodeCanonical = (lambda unrestricted word : SM90Word . unrestricted candidate : SM90Instruction . eliminate SM90Encoding (lambda unrestricted current : SM90Encoding . SM90Decoding) (sm90EncodeInstruction candidate) (branch SM90Encoded encoded . sm90Choose SM90Decoding (sm90WordEqual encoded word) (constructor SM90Decoding SM90Decoded candidate) (constructor SM90Decoding SM90DecodingRefused SM90RefusalNotCanonical)) (branch SM90EncodingRefused reason . constructor SM90Decoding SM90DecodingRefused SM90RefusalNotCanonical)) -- (a strong word of another size, or other strength bits, re-encodes to -- a different word: the canonical check refuses it) def sm90LoadWidthOf = (lambda unrestricted size : Nat . (lambda unrestricted strength : Nat . (sm90Choose (family SM90GlobalLoadWidth) (naturalEqual size sm90Size128) (sm90Choose (family SM90GlobalLoadWidth) (naturalIsZero strength) (constructor SM90GlobalLoadWidth SM90GlobalLoad128) (sm90Choose (family SM90GlobalLoadWidth) (naturalEqual strength 4) (constructor SM90GlobalLoadWidth SM90GlobalLoad128ReadOnly) (constructor SM90GlobalLoadWidth SM90GlobalLoad128Strong))) (sm90Choose (family SM90GlobalLoadWidth) (naturalIsZero strength) (constructor SM90GlobalLoadWidth SM90GlobalLoad32) (constructor SM90GlobalLoadWidth SM90GlobalLoad32Strong))))) def sm90StoreWidthOf = (lambda unrestricted size : Nat . unrestricted strength : Nat . sm90Choose SM90GlobalStoreWidth (naturalEqual size sm90Size128) SM90GlobalStore128 (sm90Choose SM90GlobalStoreWidth (naturalEqual size sm90Size64) SM90GlobalStore64 (sm90Choose SM90GlobalStoreWidth (naturalIsZero strength) SM90GlobalStore32 SM90GlobalStore32Strong))) def sm90BranchTargetOf = (lambda unrestricted complement : Nat . sm90Choose SM90BranchTarget (nat-less-than complement sm90BranchForwardLimit) (constructor SM90BranchTarget SM90BranchForward complement) (constructor SM90BranchTarget SM90BranchBackward (nat-subtract sm90BranchModulus complement))) def sm90DecodeMemoryWord = (lambda unrestricted word : (family SM90Word) . (let unrestricted low = (sm90WordLowOf word) in (let unrestricted high = (sm90WordHighOf word) in (let unrestricted opcode = (sm90OpcodeOf word) in (let unrestricted guard = (sm90GuardOfWord word) in (let unrestricted control = (sm90ControlOfWord word) in (let unrestricted size = (sm90Field high sm90SizePlace 8) in (let unrestricted strength = (sm90Field high sm90StrengthPlace 8) in (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeLoadGlobal) (sm90DecodeCanonical word (constructor SM90Instruction SM90LoadGlobal guard (sm90LoadWidthOf size strength) (sm90Choose (family SM90LoadAllocation) (sm90Field high 0x400000 2) (constructor SM90LoadAllocation SM90LoadNoAllocate) (constructor SM90LoadAllocation SM90LoadAllocate)) (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit) control)) (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeStoreGlobal) (sm90DecodeCanonical word (constructor SM90Instruction SM90StoreGlobal guard (sm90StoreWidthOf size strength) (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8)) (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit) control)) (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeCopyGlobalToShared) (sm90DecodeCanonical word (constructor SM90Instruction SM90CopyGlobalToShared guard (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8)) (sm90Field low sm90CopySharedOffsetPlace sm90CopySharedOffsetLimit) (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90Field low sm90SecondSourcePlace sm90CopySourceOffsetLimit) control)) (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeBranch) (sm90DecodeCanonical word (constructor SM90Instruction SM90Branch guard (sm90BranchTargetOf (nat-add (nat-multiply 4 (sm90Field low sm90BranchLowPlace sm90Span8)) (nat-add (nat-multiply 1024 (sm90Field low sm90BranchMiddlePlace sm90BranchMiddleSpan)) (nat-multiply sm90BranchHighUnit (nat-modulo high sm90BranchHighSpan))))) control)) (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeReduceGlobal) (sm90DecodeCanonical word (constructor SM90Instruction SM90ReduceGlobalAddFloat32 guard (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8)) (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit) control)) (constructor SM90Decoding SM90DecodingRefused (constructor SM90EncodingRefusal SM90RefusalUnknownOpcode))))))))))))))) def sm90DecodeWord = (lambda unrestricted word : (family SM90Word) . (let unrestricted low = (sm90WordLowOf word) in (let unrestricted high = (sm90WordHighOf word) in (let unrestricted opcode = (sm90OpcodeOf word) in (let unrestricted guard = (sm90GuardOfWord word) in (let unrestricted control = (sm90ControlOfWord word) in (sm90Choose (family SM90Decoding) (naturalEqual opcode 0x9a8) (sm90DecodeCanonical word (constructor SM90Instruction SM90FetchAddGlobal32 guard (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8)) (sm90UniformRegister (nat-modulo high 64)) (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit) control)) (sm90Choose (family SM90Decoding) (naturalOr (naturalEqual opcode 0x816) (naturalEqual opcode 0x216)) (sm90DecodeCanonical word (constructor SM90Instruction SM90PermuteBytes guard (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90RegisterOf (nat-modulo high sm90Span8)) (sm90Choose (family SM90ByteSelector) (naturalEqual opcode 0x816) (constructor SM90ByteSelector SM90ByteSelectorImmediate (sm90Field low sm90SecondSourcePlace sm90Span32)) (constructor SM90ByteSelector SM90ByteSelectorRegister (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8)))) control)) (sm90Choose (family SM90Decoding) (naturalEqual opcode 0x212) (sm90DecodeCanonical word (constructor SM90Instruction SM90LogicThree guard (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8)) (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8)) (sm90RegisterOf (nat-modulo high sm90Span8)) (nat-to-byte (sm90Field high 256 256)) control)) (sm90DecodeMemoryWord word))))))))))