interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / ngtxt.om

294 lines · 8180 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/ngtxt.om: shared part of the ΩIR text tools ngas.om and ngdis.om (seed/ng/IR.md s10).
3note Owner: ng-ir lane. Program mode, no fn main; globals x_*, functions tx_*. Built as
4note cat seed/ng/ngtxt.om seed/ng/ngas.om | omc and cat seed/ng/ngtxt.om seed/ng/ngdis.om | omc
5note Neither tool shares code with seed/ng/src/ir.om or ngcheck.om: the text tools are the third
6note independent decoder of the wire format, and the gate compares all three.
7note Row table "OP NAME TK IMM FIX VAR TY" (IR.md s7.12; TK 0 P 1 E 2 R 3 T; TY 0 none 1 r 2 o 3 p).
8text x_rows "1 const 0 1 0 0 1\n2 select 0 0 3 0 1\n10 add 0 0 2 0 1\n11 sub 0 0 2 0 1\n12 mul 0 0 2 0 1\n13 div 0 0 2 0 1\n14 rem 0 0 2 0 1\n15 neg 0 0 1 0 1\n16 and 0 0 2 0 1\n17 or 0 0 2 0 1\n18 xor 0 0 2 0 1\n19 not 0 0 1 0 1\n20 shl 0 0 2 0 1\n21 shr 0 0 2 0 1\n22 rotl 0 0 2 0 1\n23 rotr 0 0 2 0 1\n24 min 0 0 2 0 1\n25 max 0 0 2 0 1\n26 fsqrt 0 0 1 0 1\n27 fabs 0 0 1 0 1\n28 fmath 0 1 1 0 1\n30 eq 0 0 2 0 1\n31 ne 0 0 2 0 1\n32 lt 0 0 2 0 1\n33 le 0 0 2 0 1\n34 gt 0 0 2 0 1\n35 ge 0 0 2 0 1\n36 conv 0 1 1 0 1\n37 bitcast 0 0 1 0 1\n40 gpu.id 0 1 0 0 1\n41 shared 0 0 0 0 1\n42 load 2 0 2 0 1\n43 store 1 0 3 0 0\n44 sync 1 0 0 0 0\n45 shfl 0 1 2 0 1\n46 fma 0 0 3 0 1\n47 atom 1 1 3 0 1\n48 atomcas 1 1 4 0 1\n50 alloca 1 0 0 0 1\n51 fill 1 0 2 0 0\n52 gaddr 0 1 0 0 1\n53 len 0 0 1 0 1\n54 slice 2 0 3 0 1\n55 fload 2 1 1 0 1\n56 fstore 1 1 2 0 0\n57 fref 0 1 1 0 1\n58 vload 2 0 2 0 1\n59 vstore 1 0 3 0 0\n60 br 3 0 1 1 0\n61 cbr 3 0 3 0 0\n62 ret 3 0 0 0 0\n63 retv 3 0 1 0 0\n64 call 1 0 1 1 2\n65 trap 3 1 0 0 0\n66 exit 3 0 1 0 0\n67 launch 1 0 4 1 0\n70 chk.add 1 1 2 0 0\n71 chk.sub 1 1 2 0 0\n72 chk.mul 1 1 2 0 0\n73 chk.neg 1 1 1 0 0\n74 chk.divz 1 1 1 0 0\n75 chk.divovf 1 1 2 0 0\n76 chk.shl 1 1 2 0 0\n77 chk.shift 1 1 1 0 0\n78 chk.conv 1 1 1 0 3\n79 chk.bounds 1 1 3 0 0\n80 chk.slice 1 1 3 0 0\n81 chk.fuel 1 1 2 0 0\n82 chk.true 1 1 1 0 0\n90 fact.true 0 1 1 0 0\n91 fact.range 0 1 3 0 0\n92 fact.inbounds 0 1 3 0 0\n93 fact.slice 0 1 3 0 0\n100 vsplat 0 0 1 0 1\n101 vextract 0 1 1 0 1\n102 vinsert 0 1 2 0 1\n103 vreduce 0 1 1 0 1\n104 mmald 2 1 4 0 1\n105 mmast 1 1 5 0 0\n106 mma 0 1 3 0 1\n110 host 1 1 0 1 2\n111 pool.grow 1 1 1 0 1\n112 pool.len 2 1 0 0 1\n113 pool.load 2 1 1 0 1\n114 pool.store 1 1 2 0 0\n115 pool.read 1 1 3 0 1\n116 pool.write 1 1 3 0 1\n"
9note built-in type names by id (IR.md s4.1); "-" marks an id without a name (printed %tN)
10text x_tyn "void bool u8 u16 u32 u64 i8 i16 i32 i64 - - - f32 f64 - f16 bf16 e4m3 e5m2 tok "
11note words: 0 fn 1 kernel 2 export 3 view 4 edit 5 host 6 global 7 shared 8 constant 9 private
12note 10 a 11 b 12 c 13 le 14 be 15 vec 16 array 17 struct 18 ref 19 slice 20 frag 21 zero 22 data
13note 23 flags 24 threads 25 subgroup 26 block 27 type 28 end 29 omegair 30 true 31 false
14text x_kw "fn kernel export view edit host global shared constant private a b c le be vec array struct ref slice frag zero data flags threads subgroup block type end omegair true false pool "
15global x_rok uchar 256
16global x_rtk uchar 256
17global x_rim uchar 256
18global x_rfx uchar 256
19global x_rva uchar 256
20global x_rty uchar 256
21global x_rno uint 256
22global x_rnl uint 256
23global x_kwo uint 64
24global x_kwl uint 64
25global x_tno uint 32
26global x_tnl uint 32
27global x_tk uchar 65568
28global x_tsz ulong 65568
29global x_nt ulong 1
30global x_out uchar 1048576
31global x_on ulong 1
32global x_dg uchar 24
33note ---- init ---------------------------------------------------------------------------------------
34fn tx_init to void
35tx_rinit
36w = ulong 0
37s = ulong 0
38for i ulong len x_kw
39if eq get x_kw i 32
40put x_kwo w uint s
41put x_kwl w uint sub i s
42w = add w 1
43s = add i 1
44end
45end
46w = 0
47s = 0
48for i ulong len x_tyn
49if eq get x_tyn i 32
50put x_tno w uint s
51put x_tnl w uint sub i s
52w = add w 1
53s = add i 1
54end
55end
56end
57note decimal at p in x_rows (no sign); the next position goes to x_on (reset by tx_init callers)
58fn tx_rdec p ulong to ulong
59i = p
60v = ulong 0
61while lt i len x_rows bound 12
62c = get x_rows i
63if or lt c 48 gt c 57
64break
65end
66v = add mul v 10 ulong sub c 48
67i = add i 1
68end
69put x_on 0 i
70ret v
71end
72fn tx_rinit to void
73p = ulong 0
74while lt p len x_rows bound 256
75op = tx_rdec p
76s = add get x_on 0 1
77e = s
78while ne get x_rows e 32 bound 32
79e = add e 1
80end
81put x_rok op 1
82put x_rno op uint s
83put x_rnl op uint sub e s
84p = add e 1
85put x_rtk op uchar tx_rdec p
86p = add get x_on 0 1
87put x_rim op uchar tx_rdec p
88p = add get x_on 0 1
89put x_rfx op uchar tx_rdec p
90p = add get x_on 0 1
91put x_rva op uchar tx_rdec p
92p = add get x_on 0 1
93put x_rty op uchar tx_rdec p
94p = add get x_on 0 1
95end
96put x_on 0 0
97end
98note ---- rows ----------------------------------------------------------------------------------------
99note 1 when op has a token operand at slot 0 (E, R and T ops other than br)
100fn tx_ntok op ulong to ulong
101if or eq get x_rtk op 0 eq op 60
102ret 0
103end
104ret 1
105end
106note operand slot kind: 0 value, 1 block, 2 function
107fn tx_slot op ulong s ulong to ulong
108if and eq op 60 eq s 0
109ret 1
110end
111if and eq op 61 or eq s 2 eq s 3
112ret 1
113end
114if and eq s 1 or eq op 64 eq op 67
115ret 2
116end
117ret 0
118end
119note values op defines with result type t: its token (E ops), then its result (ty r or o, t != 0)
120fn tx_ndef op ulong t ulong to ulong
121n = ulong 0
122if eq get x_rtk op 1
123n = 1
124end
125c = get x_rty op
126if and ne t 0 or eq c 1 eq c 2
127n = add n 1
128end
129ret n
130end
131note ---- types --------------------------------------------------------------------------------------
132fn tx_sb t ulong to ulong
133if or eq t 1 eq t 2
134ret 1
135end
136if or eq t 6 or eq t 18 eq t 19
137ret 1
138end
139if or eq t 3 or eq t 7 or eq t 16 eq t 17
140ret 2
141end
142if or eq t 4 or eq t 8 eq t 13
143ret 4
144end
145if or eq t 5 or eq t 9 eq t 14
146ret 8
147end
148ret 0
149end
150note byte size of type t for global data (scalars, and table VEC, ARRAY, STRUCT); 0 otherwise
151fn tx_size t ulong to ulong
152if lt t 32
153ret tx_sb t
154end
155if ge t add 32 get x_nt 0
156ret 0
157end
158ret get x_tsz t
159end
160fn tx_mul a ulong b ulong to ulong
161x = ulong 1099511627776
162if and ne a 0 gt b div x a
163ret x
164end
165ret mul a b
166end
167note record table entry t (kind k) with size sz, as the next entry
168fn tx_tset t ulong k ulong sz ulong to void
169put x_tk t uchar k
170put x_tsz t sz
171put x_nt 0 sub t 31
172end
173note const formats: 1 bool, 2 unsigned decimal, 3 signed decimal, 4 hex bits, 0 plain decimal
174fn tx_cf t ulong to ulong
175if eq t 1
176ret 1
177end
178if and ge t 2 le t 5
179ret 2
180end
181if and ge t 6 le t 9
182ret 3
183end
184if ne tx_sb t 0
185ret 4
186end
187ret 0
188end
189note canonical const bits of a scalar type (IR.md s7.2)
190fn tx_canon t ulong v ulong to bool
191f = tx_cf t
192if eq f 0
193ret true
194end
195if eq f 1
196ret le v 1
197end
198w = mul tx_sb t 8
199if eq w 64
200ret true
201end
202if eq f 3
203h = shl ulong 1 sub w 1
204ret lt add v h shl ulong 1 w
205end
206ret lt v shl ulong 1 w
207end
208note ---- output -------------------------------------------------------------------------------------
209fn tx_flush to void
210n = get x_on 0
211o = ulong 0
212while lt o n bound 1048577
213k = write 1 x_out o sub n o
214if le k 0
215exit 27
216end
217o = add o ulong k
218end
219put x_on 0 0
220end
221fn tx_c b ulong to void
222n = get x_on 0
223if ge n len x_out
224tx_flush
225n = 0
226end
227put x_out n uchar b
228put x_on 0 add n 1
229end
230fn tx_num v ulong to void
231x = v
232i = ulong 24
233for j ulong 21
234i = sub i 1
235put x_dg i uchar add 48 rem x 10
236x = div x 10
237if eq x 0
238break
239end
240end
241for j ulong sub 24 i bound 24
242tx_c ulong get x_dg add i j
243end
244end
245fn tx_hx d ulong to void
246if lt d 10
247tx_c add d 48
248else
249tx_c add d 87
250end
251end
252note n hex digits of the low bits of v, most significant first
253fn tx_hexn v ulong n ulong to void
254for j ulong n bound 16
255tx_hx and shr v mul sub sub n 1 j 4 15
256end
257end
258fn tx_kw i ulong to void
259s = ulong get x_kwo i
260for j ulong ulong get x_kwl i bound 16
261tx_c ulong get x_kw add s j
262end
263end
264fn tx_sp to void
265tx_c 32
266end
267fn tx_nl to void
268tx_c 10
269end
270fn tx_rname op ulong to void
271s = ulong get x_rno op
272for j ulong ulong get x_rnl op bound 16
273tx_c ulong get x_rows add s j
274end
275end
276note the built-in name of t, or %tN
277fn tx_ty t ulong to void
278if le t 20
279if ne get x_tnl t 1
280s = ulong get x_tno t
281for j ulong ulong get x_tnl t bound 8
282tx_c ulong get x_tyn add s j
283end
284ret
285end
286end
287tx_c 37
288tx_c 116
289tx_num t
290end
291fn tx_val v ulong to void
292tx_c 37
293tx_num v
294end