Source/Packages

Accelerator.SM90.InstructionEncoding

packages/hardware/architectures/nvidia-sm90/src/Accelerator/SM90/InstructionEncoding.alpha

500 lines73 declarations29.3 KiBSHA-256 6893fe9ca611

Complete file · line 304

InstructionEncoding.alpha

Definition view
1module Accelerator.SM90.InstructionEncoding
2
3import Accelerator.SM86.Control
4import Accelerator.SM86.Instruction
5import Accelerator.SM86.Types
6import Accelerator.SM90.Instruction
7import Accelerator.SM90.UniformOperands
8import Accelerator.SM90.Word
9import Std.Natural
10
11-- The sm_90 encoder and decoder of Accelerator.SM90.Instruction.
12--
13-- The encoder lays each operand into its field from the typed instruction
14-- and REFUSES an operand its field cannot hold.  The decoder reads the
15-- fields back and then re-encodes them: it accepts a word only when that
16-- reproduces every one of its 128 bits, so a word with a stray modifier, a
17-- reserved bit or an unknown size code is refused as non-canonical instead
18-- of being read as something it is not.  Hence encode (decode w) = w for
19-- every word the decoder accepts, by construction; the converse,
20-- decode (encode x) = x, is what the round-trip laws of
21-- tests/Proof/SM90InstructionEncodingLaws.alpha pin instance by instance.
22--
23-- Field layouts (bit numbers of the 128-bit word; nvdisasm -hex's first
24-- 64-bit value is bits 0..63):
25--
26--   LDG    opcode 0x981 | Rd 16..23 | Ra 24..31 | UR 32..37 (URZ, 63) |
27--          offset 40..62 | .E 72 | size 73..75 | fixed 0x0c1e0000 at 64
28--   STG    opcode 0x986 | Ra 24..31 | Rv 32..39 | offset 40..62 |
29--          UR 64..69 (URZ) | .E 72 | size 73..75 | fixed 0x0c100000 at 64
30--   (LDG and STG: bits 77..79 set for .STRONG.GPU, clear for the weak
31--   form -- ptxas -arch=sm_90a's ld/st.relaxed.gpu, scratchpad probe
32--   reference/oracles/nvidia-sm90-ptxas/global-branch/strong.sass)
33--   LDGSTS opcode 0xfae | Rs 16..23 | Rg 24..31 | source offset 32..43 |
34--          shared offset 44..63 (both signed) | UR 64..69 (URZ) |
35--          0x0b900c40 at 64
36--   BRA    opcode 0x947 | displacement bits 2..9 at 16..23 and 10..57 at
37--          34..81 | fixed 0x03800000 at 64 (bits 87..89)
38--   REDG   opcode 0x9a6 | Ra 24..31 | Rv 32..39 | offset 40..62 |
39--          UR 64..69 (URZ) | descriptor flag 71 (clear) | .E 72 | fixed
40--          0x0c10f33f at 64 (.ADD.F32.FTZ.RN.STRONG.GPU)
41--
42-- with the guard at 12..15 and the scheduling control at 105..125 for all
43-- (Accelerator.SM90.Word).  The size code is 4 for 32 bits, 5 for 64 and 6
44-- for 128 (measured: ptxas -arch=sm_90a, reference/oracles/
45-- nvidia-sm90-ptxas/global-branch/global.sass).  The descriptor flag,
46-- bit 76, is clear in all three global forms: that is the raw form.
47
48family SM90Encoding : Type 0
49constructor SM90Encoded
50field unrestricted sm90EncodedWord : SM90Word
51constructor SM90EncodingRefused
52field unrestricted sm90EncodingRefusal : SM90EncodingRefusal
53end-family
54
55family SM90Decoding : Type 0
56constructor SM90Decoded
57field unrestricted sm90DecodedInstruction : SM90Instruction
58constructor SM90DecodingRefused
59field unrestricted sm90DecodingRefusal : SM90EncodingRefusal
60end-family
61
62def sm90Refuse = (lambda unrestricted reason : SM90EncodingRefusal .
63  constructor SM90Encoding SM90EncodingRefused reason)
64
65def sm90Emit = (lambda unrestricted low : Nat . unrestricted high : Nat .
66  constructor SM90Encoding SM90Encoded (sm90Word low high))
67
68-- Refuse with `reason` unless `ok` (a 0/1 flag), else continue.
69def sm90Require = (lambda unrestricted ok : Nat . unrestricted reason : SM90EncodingRefusal .
70  unrestricted continue : SM90Encoding .
71    sm90Choose SM90Encoding ok continue (sm90Refuse reason))
72
73-- The control fits its field.  The encoder admits every control that
74-- fits, as the hardware's decoder does: nvdisasm's grammar is stricter (it
75-- rejects a store or BRA that names a write scoreboard, a BRA with a read
76-- scoreboard, and a yield with stall 0 or 15 -- reference/oracles/
77-- nvidia-sm90-ptxas/global-branch/README.md), but the RTX 3070 runs such
78-- words from the SM86 encoders, so refusing them here would change what
79-- an SM86 program means rather than how it is encoded.  Whether a control
80-- is sound is the scoreboard model's question (Accelerator.SM86.Operands
81-- and the checked machine models), not the encoder's.
82def sm90ControlFits = (lambda unrestricted control : SM86Control .
83  nat-less-than (sm90ControlBits control) sm90ControlSpan)
84
85-- ---------------------------------------------------------------------------
86-- Opcodes and fixed bits
87
88def sm90OpcodeLoadGlobal : Nat = 0x981
89def sm90OpcodeStoreGlobal : Nat = 0x986
90def sm90OpcodeCopyGlobalToShared : Nat = 0xfae
91def sm90OpcodeBranch : Nat = 0x947
92def sm90OpcodeReduceGlobal : Nat = 0x9a6
93
94-- URZ is owned by UniformOperands; a raw load has no descriptor or offset.
95
96def sm90DestinationPlace : Nat = (compile-time (sm90Place 16))
97def sm90SourcePlace : Nat = (compile-time (sm90Place 24))
98def sm90SecondSourcePlace : Nat = (compile-time (sm90Place 32))
99def sm90GlobalOffsetPlace : Nat = (compile-time (sm90Place 40))
100-- the global offset's non-negative values (the 24-bit immediate is signed)
101def sm90GlobalOffsetLimit : Nat = (compile-time (sm90Place 23))
102-- LDGSTS: the source offset at 32 and the shared offset at 44 (bits
103-- 44..63) are signed; their non-negative halves are below 2^11 and 2^19.
104-- nvdisasm -b SM90 reads a shared offset of 0xfffff as -0x1
105-- (reference/oracles/nvidia-sm90-ptxas/global-branch/README.md), so the
106-- encoder refuses 2^19 and above instead of addressing below the base.
107def sm90CopySourceOffsetLimit : Nat = (compile-time (sm90Place 11))
108def sm90CopySharedOffsetPlace : Nat = (compile-time (sm90Place 44))
109def sm90CopySharedOffsetLimit : Nat = (compile-time (sm90Place 19))
110
111-- The size code, bits 73..75 (high bits 9..11).
112def sm90SizePlace : Nat = 512
113def sm90Size32 : Nat = 4
114def sm90Size64 : Nat = 5
115def sm90Size128 : Nat = 6
116
117-- High bits 0..31 of each form less its size code: LDG.E and STG.E as ptxas
118-- writes them for sm_90a (0x0c1e1900, 0x0c101904) with the descriptor flag
119-- (bit 76, 0x1000) cleared and, for STG, the uniform field (bits 64..69)
120-- naming URZ in place of UR4.  Bit 72 (0x100) is .E, the 64-bit address.
121def sm90LoadGlobalFixed : Nat = 0x0c1e0100
122def sm90StoreGlobalFixed : Nat = 0x0c10013f
123-- LDGSTS.E.BYPASS.128 as ptxas writes it (0x0b901c44) with bit 76 clear
124-- and URZ at 64..69; bit 70 (0x40) is kept as ptxas sets it.
125def sm90CopyGlobalToSharedFixed : Nat = 0x0b900c7f
126-- REDG.E.ADD.F32.FTZ.RN.STRONG.GPU as ptxas writes it (0x0c10f384,
127-- red.sass) with RED's descriptor flag (bit 71, 0x80) cleared and URZ at
128-- 64..69 in place of UR4
129def sm90ReduceGlobalAddFloat32Fixed : Nat = 0x0c10f33f
130-- BRA's bits 87..89 as ptxas writes every BRA (0x03800000 at 64)
131def sm90BranchFixed : Nat = 0x03800000
132
133-- a register group of `width` registers (1, 2, 4) starting at `register`:
134-- one register is any (RZ included: STG [Ra.64], RZ stores zero); a pair or
135-- quad is aligned to its width and ends below RZ
136def sm90RegisterGroupAdmitted = (lambda unrestricted width : Nat . unrestricted register : SM86Register .
137  let unrestricted n = (sm90RegisterNumber register) in
138  naturalOr (naturalEqual width 1)
139    (pipe n (nat-modulo width) naturalIsZero (naturalAnd (naturalLessOrEqual (nat-add n width) 255))))
140def sm90LoadRegisters = (lambda unrestricted width : (family SM90GlobalLoadWidth) .
141  (eliminate SM90GlobalLoadWidth (lambda unrestricted current : (family SM90GlobalLoadWidth) . Nat) width
142    (branch SM90GlobalLoad32 . 1) (branch SM90GlobalLoad128 . 4) (branch SM90GlobalLoad32Strong . 1)
143    (branch SM90GlobalLoad128Strong . 4)
144    (branch SM90GlobalLoad128ReadOnly . 4)))
145def sm90StoreRegisters = (lambda unrestricted width : SM90GlobalStoreWidth .
146  eliminate SM90GlobalStoreWidth (lambda unrestricted current : SM90GlobalStoreWidth . Nat) width
147    (branch SM90GlobalStore32 . 1) (branch SM90GlobalStore64 . 2) (branch SM90GlobalStore128 . 4)
148    (branch SM90GlobalStore32Strong . 1))
149-- the strength bits (77..79, high bits 13..15): 7 strong at the GPU's
150-- scope, 0 weak
151def sm90StrengthPlace : Nat = 8192
152def sm90StrongGPU : Nat = 7
153def sm90LoadStrength = (lambda unrestricted width : (family SM90GlobalLoadWidth) .
154  (eliminate SM90GlobalLoadWidth (lambda unrestricted current : (family SM90GlobalLoadWidth) . Nat) width
155    (branch SM90GlobalLoad32 . 0) (branch SM90GlobalLoad128 . 0) (branch SM90GlobalLoad32Strong . sm90StrongGPU)
156    (branch SM90GlobalLoad128Strong . sm90StrongGPU)
157    (branch SM90GlobalLoad128ReadOnly . 4)))
158def sm90StoreStrength = (lambda unrestricted width : SM90GlobalStoreWidth .
159  eliminate SM90GlobalStoreWidth (lambda unrestricted current : SM90GlobalStoreWidth . Nat) width
160    (branch SM90GlobalStore32 . 0) (branch SM90GlobalStore64 . 0) (branch SM90GlobalStore128 . 0)
161    (branch SM90GlobalStore32Strong . sm90StrongGPU))
162
163def sm90LoadSize = (lambda unrestricted width : (family SM90GlobalLoadWidth) .
164  (eliminate SM90GlobalLoadWidth (lambda unrestricted current : (family SM90GlobalLoadWidth) . Nat) width
165    (branch SM90GlobalLoad32 . sm90Size32)
166    (branch SM90GlobalLoad128 . sm90Size128)
167    (branch SM90GlobalLoad32Strong . sm90Size32)
168    (branch SM90GlobalLoad128Strong . sm90Size128)
169    (branch SM90GlobalLoad128ReadOnly . sm90Size128)))
170
171def sm90StoreSize = (lambda unrestricted width : SM90GlobalStoreWidth .
172  eliminate SM90GlobalStoreWidth (lambda unrestricted current : SM90GlobalStoreWidth . Nat) width
173    (branch SM90GlobalStore32 . sm90Size32)
174    (branch SM90GlobalStore64 . sm90Size64)
175    (branch SM90GlobalStore128 . sm90Size128)
176    (branch SM90GlobalStore32Strong . sm90Size32))
177
178-- Branch displacement fields: bits 2..9 at 16 (8 bits); bits 10..39 at 34
179-- (30 bits, the low half's top); bits 40..57 at 64 (18 bits).
180def sm90BranchLowPlace : Nat = (compile-time (sm90Place 16))
181def sm90BranchMiddlePlace : Nat = (compile-time (sm90Place 34))
182def sm90BranchMiddleSpan : Nat = (compile-time (sm90Place 30))
183def sm90BranchHighSpan : Nat = (compile-time (sm90Place 18))
184def sm90BranchHighUnit : Nat = (compile-time (sm90Place 40))
185-- the displacement's two's complement is 58 bits; the forward half below
186def sm90BranchModulus : Nat = (compile-time (sm90Place 58))
187def sm90BranchForwardLimit : Nat = (compile-time (sm90Place 57))
188
189-- ---------------------------------------------------------------------------
190-- Encoders
191
192-- ptxas 12.9.86 maps L1::no_allocate to SASS LDG.NA bit 86.
193-- This cache hint neither changes loaded values nor supplies a memory fence.
194def sm90LoadAllocationBits = (lambda unrestricted allocation : (family SM90LoadAllocation) .
195  (eliminate SM90LoadAllocation (lambda unrestricted current : (family SM90LoadAllocation) . Nat) allocation
196    (branch SM90LoadAllocate . 0) (branch SM90LoadNoAllocate . 0x400000)))
197
198def sm90EncodeLoadGlobal =
199  (lambda unrestricted guard : (family SM86InstructionGuard) .
200  (lambda unrestricted width : (family SM90GlobalLoadWidth) .
201  (lambda unrestricted allocation : (family SM90LoadAllocation) .
202  (lambda unrestricted destination : (family SM86Register) .
203  (lambda unrestricted address : (family SM86Register) .
204  (lambda unrestricted offset : Nat .
205  (lambda unrestricted control : (family SM86Control) .
206    (sm90Require (sm90RegisterGroupAdmitted 2 address) (constructor SM90EncodingRefusal SM90RefusalAddressUnaligned)
207    (sm90Require (sm90RegisterGroupAdmitted (sm90LoadRegisters width) destination) (constructor SM90EncodingRefusal SM90RefusalDataUnaligned)
208    (sm90Require (nat-less-than offset sm90GlobalOffsetLimit) (constructor SM90EncodingRefusal SM90RefusalGlobalOffsetOutOfRange)
209    (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange)
210      (sm90Emit
211        (nat-add (sm90HeaderLow sm90OpcodeLoadGlobal guard)
212        (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace)
213        (nat-add (sm90Put (sm90RegisterNumber address) sm90SourcePlace)
214        (nat-add (sm90Put sm90UniformZero sm90SecondSourcePlace)
215          (sm90Put offset sm90GlobalOffsetPlace)))))
216        (nat-add (sm90HeaderHigh control)
217          (nat-add (nat-add sm90LoadGlobalFixed (sm90LoadAllocationBits allocation)) (nat-add (sm90Put (sm90LoadSize width) sm90SizePlace)
218            (sm90Put (sm90LoadStrength width) sm90StrengthPlace))))))))))))))))
219
220-- PTX atom.relaxed.gpu.global.add.u32, ptxas 12.9.86. Fixed ADD,
221-- unsigned 32-bit operand, PT status destination and explicit descriptor.
222-- Only zero immediate offset is admitted; callers form the full address.
223def sm90EncodeFetchAddGlobal32 = (lambda unrestricted guard : (family SM86InstructionGuard) .
224(lambda unrestricted destination : (family SM86Register) . (lambda unrestricted address : (family SM86Register) .
225(lambda unrestricted value : (family SM86Register) . (lambda unrestricted descriptor : (family SM90UniformRegister) .
226(lambda unrestricted offset : Nat . (lambda unrestricted control : (family SM86Control) .
227(let unrestricted uniform = (sm90UniformRegisterNumberOf descriptor) in
228(sm90Require (sm90RegisterGroupAdmitted 2 address) (constructor SM90EncodingRefusal SM90RefusalAddressUnaligned)
229(sm90Require (naturalAnd (naturalLess uniform 62) (naturalIsZero (nat-modulo uniform 2))) (constructor SM90EncodingRefusal SM90RefusalAddressUnaligned)
230(sm90Require (naturalIsZero offset) (constructor SM90EncodingRefusal SM90RefusalGlobalOffsetOutOfRange)
231(sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange)
232(sm90Emit (nat-add (sm90HeaderLow 0x9a8 guard)
233(nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace)
234(nat-add (sm90Put (sm90RegisterNumber address) sm90SourcePlace)
235(nat-add (sm90Put (sm90RegisterNumber value) sm90SecondSourcePlace) (sm90Put offset sm90GlobalOffsetPlace)))))
236(nat-add (sm90HeaderHigh control) (nat-add 0x081ee1c0 uniform)))))))))))))))
237
238def sm90EncodeStoreGlobal =
239  (lambda unrestricted guard : SM86InstructionGuard .
240  unrestricted width : SM90GlobalStoreWidth .
241  unrestricted address : SM86Register .
242  unrestricted value : SM86Register .
243  unrestricted offset : Nat .
244  unrestricted control : SM86Control .
245    then (sm90Require (sm90RegisterGroupAdmitted 2 address) SM90RefusalAddressUnaligned)
246    (sm90Require (sm90RegisterGroupAdmitted (sm90StoreRegisters width) value) SM90RefusalDataUnaligned)
247    (sm90Require (nat-less-than offset sm90GlobalOffsetLimit) SM90RefusalGlobalOffsetOutOfRange)
248    (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange)
249      (sm90Emit
250        (nest nat-add (sm90HeaderLow sm90OpcodeStoreGlobal guard)
251        (sm90Put (sm90RegisterNumber address) sm90SourcePlace)
252        (sm90Put (sm90RegisterNumber value) sm90SecondSourcePlace)
253          (sm90Put offset sm90GlobalOffsetPlace)))
254        (nest nat-add (sm90HeaderHigh control)
255          sm90StoreGlobalFixed (sm90Put (sm90StoreSize width) sm90SizePlace))
256            (sm90Put (sm90StoreStrength width) sm90StrengthPlace))
257
258def sm90EncodeReduceGlobalAddFloat32 =
259  (lambda unrestricted guard : SM86InstructionGuard .
260  unrestricted address : SM86Register .
261  unrestricted value : SM86Register .
262  unrestricted offset : Nat .
263  unrestricted control : SM86Control .
264    then (sm90Require (sm90RegisterGroupAdmitted 2 address) SM90RefusalAddressUnaligned)
265    (sm90Require (nat-less-than offset sm90GlobalOffsetLimit) SM90RefusalGlobalOffsetOutOfRange)
266    (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange)
267      (sm90Emit
268        (nest nat-add (sm90HeaderLow sm90OpcodeReduceGlobal guard)
269        (sm90Put (sm90RegisterNumber address) sm90SourcePlace)
270        (sm90Put (sm90RegisterNumber value) sm90SecondSourcePlace)
271          (sm90Put offset sm90GlobalOffsetPlace)))
272        (nat-add (sm90HeaderHigh control)) sm90ReduceGlobalAddFloat32Fixed)
273
274def sm90EncodeCopyGlobalToShared =
275  (lambda unrestricted guard : SM86InstructionGuard .
276  unrestricted address : SM86Register .
277  unrestricted offset : Nat .
278  unrestricted source : SM86Register .
279  unrestricted sourceOffset : Nat .
280  unrestricted control : SM86Control .
281    then (sm90Require (sm90RegisterGroupAdmitted 2 source) SM90RefusalAddressUnaligned)
282    (sm90Require
283      (naturalAnd (nat-less-than sourceOffset sm90CopySourceOffsetLimit) (nat-less-than offset sm90CopySharedOffsetLimit))
284      SM90RefusalCopyOffsetOutOfRange)
285    (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange)
286      (sm90Emit
287        (nest nat-add (sm90HeaderLow sm90OpcodeCopyGlobalToShared guard)
288        (sm90Put (sm90RegisterNumber address) sm90DestinationPlace)
289        (sm90Put (sm90RegisterNumber source) sm90SourcePlace)
290        (sm90Put sourceOffset sm90SecondSourcePlace)
291          (sm90Put offset sm90CopySharedOffsetPlace)))
292        (nat-add (sm90HeaderHigh control)) sm90CopyGlobalToSharedFixed)
293
294-- The displacement's 58-bit two's complement, or the modulus itself when
295-- it is out of range (no displacement encodes to it).
296def sm90BranchComplement = (lambda unrestricted target : SM90BranchTarget .
297  eliminate SM90BranchTarget (lambda unrestricted current : SM90BranchTarget . Nat) target
298    (branch SM90BranchForward bytes .
299      naturalSelect (nat-less-than bytes sm90BranchForwardLimit) bytes sm90BranchModulus)
300    (branch SM90BranchBackward bytes .
301      naturalSelect (naturalAnd (nat-less-than 0 bytes) (naturalLessOrEqual bytes sm90BranchForwardLimit))
302        (nat-subtract sm90BranchModulus bytes) sm90BranchModulus))
303
304def sm90BranchBytes = (lambda unrestricted target : SM90BranchTarget .
305  eliminate SM90BranchTarget (lambda unrestricted current : SM90BranchTarget . Nat) target
306    (branch SM90BranchForward bytes . bytes)
307    (branch SM90BranchBackward bytes . bytes))
308
309def sm90EncodeBranch =
310  (lambda unrestricted guard : SM86InstructionGuard .
311  unrestricted target : SM90BranchTarget .
312  unrestricted control : SM86Control .
313    let unrestricted complement = (sm90BranchComplement target) in
314    then (sm90Require (pipe target sm90BranchBytes (nat-modulo 4) (naturalEqual 0)) SM90RefusalBranchUnaligned)
315    (sm90Require (nat-less-than complement sm90BranchModulus) SM90RefusalBranchOutOfRange)
316    (sm90Require (sm90ControlFits control) SM90RefusalControlOutOfRange)
317      (sm90Emit
318        (nest nat-add (sm90HeaderLow sm90OpcodeBranch guard)
319        (pipe complement (nat-divide 4) (nat-modulo sm90Span8) (sm90Put sm90BranchLowPlace))
320          (pipe complement (nat-divide 1024) (nat-modulo sm90BranchMiddleSpan) (sm90Put sm90BranchMiddlePlace))))
321        (nest nat-add (sm90HeaderHigh control)
322        sm90BranchFixed)
323          (nat-divide complement sm90BranchHighUnit))
324
325-- Observable independent ptxas/nvdisasm probes establish these fields.
326-- Immediate PRMT: 0x816; register-selected PRMT: 0x216. The right
327-- operand is in bits64..71; all default-mode bits72..104 are zero.
328def sm90SelectorValue = (lambda unrestricted selector : (family SM90ByteSelector) .
329 (eliminate SM90ByteSelector (lambda unrestricted current : (family SM90ByteSelector) . Nat) selector
330 (branch SM90ByteSelectorImmediate value . value)
331 (branch SM90ByteSelectorRegister register . (sm90RegisterNumber register))))
332def sm90SelectorFits = (lambda unrestricted selector : (family SM90ByteSelector) .
333 (eliminate SM90ByteSelector (lambda unrestricted current : (family SM90ByteSelector) . Nat) selector
334 (branch SM90ByteSelectorImmediate value . (naturalLess value 65536))
335 (branch SM90ByteSelectorRegister register . 1)))
336def sm90SelectorOpcode = (lambda unrestricted selector : (family SM90ByteSelector) .
337 (eliminate SM90ByteSelector (lambda unrestricted current : (family SM90ByteSelector) . Nat) selector
338 (branch SM90ByteSelectorImmediate value . 0x816)
339 (branch SM90ByteSelectorRegister register . 0x216)))
340def sm90EncodePermute = (lambda unrestricted guard : (family SM86InstructionGuard) .
341 (lambda unrestricted destination : (family SM86Register) . (lambda unrestricted left : (family SM86Register) .
342 (lambda unrestricted right : (family SM86Register) . (lambda unrestricted selector : (family SM90ByteSelector) .
343 (lambda unrestricted control : (family SM86Control) .
344 (sm90Require (sm90SelectorFits selector) (constructor SM90EncodingRefusal SM90RefusalSelectorOutOfRange)
345 (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange)
346 (sm90Emit
347 (nat-add (sm90HeaderLow (sm90SelectorOpcode selector) guard)
348 (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace)
349 (nat-add (sm90Put (sm90RegisterNumber left) sm90SourcePlace)
350 (sm90Put (sm90SelectorValue selector) sm90SecondSourcePlace))))
351 (nat-add (sm90HeaderHigh control) (sm90RegisterNumber right)))))))))))
352def sm90EncodeLogicThree = (lambda unrestricted guard : (family SM86InstructionGuard) .
353 (lambda unrestricted destination : (family SM86Register) . (lambda unrestricted first : (family SM86Register) .
354 (lambda unrestricted second : (family SM86Register) . (lambda unrestricted third : (family SM86Register) .
355 (lambda unrestricted truth : Byte . (lambda unrestricted control : (family SM86Control) .
356 (sm90Require (sm90ControlFits control) (constructor SM90EncodingRefusal SM90RefusalControlOutOfRange)
357 (sm90Emit
358 (nat-add (sm90HeaderLow 0x212 guard)
359 (nat-add (sm90Put (sm90RegisterNumber destination) sm90DestinationPlace)
360 (nat-add (sm90Put (sm90RegisterNumber first) sm90SourcePlace)
361 (sm90Put (sm90RegisterNumber second) sm90SecondSourcePlace))))
362 (nat-add (sm90HeaderHigh control) (nat-add 0x078e0000
363 (nat-add (sm90RegisterNumber third) (nat-multiply (byte-to-nat truth) 256)))))))))))))
364
365def sm90EncodeInstruction = (lambda unrestricted instruction : (family SM90Instruction) .
366  (eliminate SM90Instruction (lambda unrestricted current : (family SM90Instruction) . (family SM90Encoding)) instruction
367    (branch SM90LoadGlobal guard width allocation destination address offset control .
368      (sm90EncodeLoadGlobal guard width allocation destination address offset control))
369    (branch SM90FetchAddGlobal32 guard destination address value descriptor offset control .
370      (sm90EncodeFetchAddGlobal32 guard destination address value descriptor offset control))
371    (branch SM90StoreGlobal guard width address value offset control .
372      (sm90EncodeStoreGlobal guard width address value offset control))
373    (branch SM90CopyGlobalToShared guard address offset source sourceOffset control .
374      (sm90EncodeCopyGlobalToShared guard address offset source sourceOffset control))
375    (branch SM90Branch guard target control .
376      (sm90EncodeBranch guard target control))
377    (branch SM90ReduceGlobalAddFloat32 guard address value offset control .
378      (sm90EncodeReduceGlobalAddFloat32 guard address value offset control))
379    (branch SM90PermuteBytes guard destination left right selector control .
380      (sm90EncodePermute guard destination left right selector control))
381    (branch SM90LogicThree guard destination first second third truth control .
382      (sm90EncodeLogicThree guard destination first second third truth control))))
383
384-- ---------------------------------------------------------------------------
385-- Decoder
386
387-- Accept `candidate` only when it encodes back to `word`, bit for bit.
388def sm90DecodeCanonical = (lambda unrestricted word : SM90Word .
389  unrestricted candidate : SM90Instruction .
390    eliminate SM90Encoding (lambda unrestricted current : SM90Encoding . SM90Decoding)
391      (sm90EncodeInstruction candidate)
392      (branch SM90Encoded encoded .
393        sm90Choose SM90Decoding (sm90WordEqual encoded word)
394          (constructor SM90Decoding SM90Decoded candidate)
395          (constructor SM90Decoding SM90DecodingRefused SM90RefusalNotCanonical))
396      (branch SM90EncodingRefused reason .
397        constructor SM90Decoding SM90DecodingRefused SM90RefusalNotCanonical))
398
399-- (a strong word of another size, or other strength bits, re-encodes to
400-- a different word: the canonical check refuses it)
401def sm90LoadWidthOf = (lambda unrestricted size : Nat . (lambda unrestricted strength : Nat .
402  (sm90Choose (family SM90GlobalLoadWidth) (naturalEqual size sm90Size128)
403    (sm90Choose (family SM90GlobalLoadWidth) (naturalIsZero strength)
404      (constructor SM90GlobalLoadWidth SM90GlobalLoad128)
405      (sm90Choose (family SM90GlobalLoadWidth) (naturalEqual strength 4)
406        (constructor SM90GlobalLoadWidth SM90GlobalLoad128ReadOnly)
407        (constructor SM90GlobalLoadWidth SM90GlobalLoad128Strong)))
408    (sm90Choose (family SM90GlobalLoadWidth) (naturalIsZero strength)
409      (constructor SM90GlobalLoadWidth SM90GlobalLoad32) (constructor SM90GlobalLoadWidth SM90GlobalLoad32Strong)))))
410
411def sm90StoreWidthOf = (lambda unrestricted size : Nat . unrestricted strength : Nat .
412  sm90Choose SM90GlobalStoreWidth (naturalEqual size sm90Size128)
413    SM90GlobalStore128
414    (sm90Choose SM90GlobalStoreWidth (naturalEqual size sm90Size64)
415      SM90GlobalStore64
416      (sm90Choose SM90GlobalStoreWidth (naturalIsZero strength)
417        SM90GlobalStore32 SM90GlobalStore32Strong)))
418
419def sm90BranchTargetOf = (lambda unrestricted complement : Nat .
420  sm90Choose SM90BranchTarget (nat-less-than complement sm90BranchForwardLimit)
421    (constructor SM90BranchTarget SM90BranchForward complement)
422    (constructor SM90BranchTarget SM90BranchBackward (nat-subtract sm90BranchModulus complement)))
423
424def sm90DecodeMemoryWord = (lambda unrestricted word : (family SM90Word) .
425  (let unrestricted low = (sm90WordLowOf word) in
426  (let unrestricted high = (sm90WordHighOf word) in
427  (let unrestricted opcode = (sm90OpcodeOf word) in
428  (let unrestricted guard = (sm90GuardOfWord word) in
429  (let unrestricted control = (sm90ControlOfWord word) in
430  (let unrestricted size = (sm90Field high sm90SizePlace 8) in
431  (let unrestricted strength = (sm90Field high sm90StrengthPlace 8) in
432  (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeLoadGlobal)
433    (sm90DecodeCanonical word
434      (constructor SM90Instruction SM90LoadGlobal guard (sm90LoadWidthOf size strength)
435        (sm90Choose (family SM90LoadAllocation) (sm90Field high 0x400000 2)
436          (constructor SM90LoadAllocation SM90LoadNoAllocate) (constructor SM90LoadAllocation SM90LoadAllocate))
437        (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8))
438        (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
439        (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit)
440        control))
441  (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeStoreGlobal)
442    (sm90DecodeCanonical word
443      (constructor SM90Instruction SM90StoreGlobal guard (sm90StoreWidthOf size strength)
444        (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
445        (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8))
446        (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit)
447        control))
448  (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeCopyGlobalToShared)
449    (sm90DecodeCanonical word
450      (constructor SM90Instruction SM90CopyGlobalToShared guard
451        (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8))
452        (sm90Field low sm90CopySharedOffsetPlace sm90CopySharedOffsetLimit)
453        (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
454        (sm90Field low sm90SecondSourcePlace sm90CopySourceOffsetLimit)
455        control))
456  (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeBranch)
457    (sm90DecodeCanonical word
458      (constructor SM90Instruction SM90Branch guard
459        (sm90BranchTargetOf
460          (nat-add (nat-multiply 4 (sm90Field low sm90BranchLowPlace sm90Span8))
461          (nat-add (nat-multiply 1024 (sm90Field low sm90BranchMiddlePlace sm90BranchMiddleSpan))
462            (nat-multiply sm90BranchHighUnit (nat-modulo high sm90BranchHighSpan)))))
463        control))
464  (sm90Choose (family SM90Decoding) (naturalEqual opcode sm90OpcodeReduceGlobal)
465    (sm90DecodeCanonical word
466      (constructor SM90Instruction SM90ReduceGlobalAddFloat32 guard
467        (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
468        (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8))
469        (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit)
470        control))
471    (constructor SM90Decoding SM90DecodingRefused (constructor SM90EncodingRefusal SM90RefusalUnknownOpcode)))))))))))))))
472
473def sm90DecodeWord = (lambda unrestricted word : (family SM90Word) .
474 (let unrestricted low = (sm90WordLowOf word) in
475 (let unrestricted high = (sm90WordHighOf word) in
476 (let unrestricted opcode = (sm90OpcodeOf word) in
477 (let unrestricted guard = (sm90GuardOfWord word) in
478 (let unrestricted control = (sm90ControlOfWord word) in
479 (sm90Choose (family SM90Decoding) (naturalEqual opcode 0x9a8)
480 (sm90DecodeCanonical word (constructor SM90Instruction SM90FetchAddGlobal32 guard
481 (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8))
482 (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
483 (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8))
484 (sm90UniformRegister (nat-modulo high 64))
485 (sm90Field low sm90GlobalOffsetPlace sm90GlobalOffsetLimit) control))
486 (sm90Choose (family SM90Decoding) (naturalOr (naturalEqual opcode 0x816) (naturalEqual opcode 0x216))
487 (sm90DecodeCanonical word (constructor SM90Instruction SM90PermuteBytes guard
488 (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8))
489 (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
490 (sm90RegisterOf (nat-modulo high sm90Span8))
491 (sm90Choose (family SM90ByteSelector) (naturalEqual opcode 0x816)
492 (constructor SM90ByteSelector SM90ByteSelectorImmediate (sm90Field low sm90SecondSourcePlace sm90Span32))
493 (constructor SM90ByteSelector SM90ByteSelectorRegister (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8)))) control))
494 (sm90Choose (family SM90Decoding) (naturalEqual opcode 0x212)
495 (sm90DecodeCanonical word (constructor SM90Instruction SM90LogicThree guard
496 (sm90RegisterOf (sm90Field low sm90DestinationPlace sm90Span8))
497 (sm90RegisterOf (sm90Field low sm90SourcePlace sm90Span8))
498 (sm90RegisterOf (sm90Field low sm90SecondSourcePlace sm90Span8))
499 (sm90RegisterOf (nat-modulo high sm90Span8)) (nat-to-byte (sm90Field high 256 256)) control))
500 (sm90DecodeMemoryWord word))))))))))

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.