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 / gpu / cuda-crack / check / reencode.om

357 lines · 6197 bytes Raw Blame History
1note check/reencode.om: rebuild a kernel's machine code from the isa/sm121-forms.txt tables alone.
2note stdin: the table text, a line "E", then the kernel bytes (16 bytes per instruction).
3note First the D records are checked against the F records: each F id equals its position, each field
4note number 0..6 appears at most once per form, operand fields of one form never overlap, their union
5note is exactly the form's mask, and a field with width > 0 is exactly the bits start .. start+width-1.
6note For each instruction: find the first form whose fixed bits equal the instruction's bits outside
7note the form's operand fields (words 0..2), then write fixed bits or the operand-field bits, and the
8note control word 3 unchanged. Unmatched instructions are written as zero.
9note stdout: the rebuilt bytes. stderr: "N U", instructions read and instructions no form explains.
10note exit 0 all matched; 2 read error or input too big; 3 write error; 4 unmatched; 5 bad length; 6 no E line;
11note 7 the F and D records disagree
12
13global inb uchar 8388608
14global ip ulong 1
15global tf ulong 32768
16global nt ulong 1
17global ow ulong 4
18global obuf uchar 4194304
19global dg uchar 32
20global lb uchar 64
21global ln ulong 1
22global du ulong 12288
23global dm ulong 3
24global ex ulong 3
25global bad ulong 1
26global dmax ulong 1
27global df ulong 4096
28
29note mark the table inconsistent
30fn flag to void
31put bad 0 1
32end
33
34fn rdall to long
35t = ulong 0
36while lt t len inb bound 1000000
37k = read 0 inb t sub len inb t
38if lt k 0
39ret k
40end
41if eq k 0
42ret long t
43end
44t = add t ulong k
45end
46ret neg long 27
47end
48
49note the next decimal number at ip, skipping spaces
50fn num to ulong
51p = get ip 0
52while eq get inb p 32 bound 1000
53p = add p 1
54end
55v = ulong 0
56while and ge get inb p 48 le get inb p 57 bound 24
57v = add mul v 10 sub ulong get inb p 48
58p = add p 1
59end
60put ip 0 p
61ret v
62end
63
64fn skipline n ulong to void
65p = get ip 0
66while and lt p n ne get inb p 10 bound 8388608
67p = add p 1
68end
69put ip 0 add p 1
70end
71
72note one F record: id op sel fix0 fix1 fix2 msk0 msk1 msk2 ctl, kept as op fix0..2 msk0..2
73fn readf to void
74put ip 0 add get ip 0 1
75id = num
76j = get nt 0
77if ne id j
78flag
79end
80b = mul j 8
81put tf b num
82sel = num
83for k ulong 6
84put tf add b add k 1 num
85end
86if lt j 4095
87put nt 0 add j 1
88end
89end
90
91note OR the D record's mask (in dm) into form id's field union; overlapping fields are an error
92fn dunion id ulong to void
93b = mul id 3
94for k ulong 3
95m = get dm k
96o = get du add b k
97if ne and o m 0
98flag
99end
100put du add b k or o m
101end
102end
103
104note each field number 0..6 appears at most once per form
105fn dfield id ulong fd ulong to void
106if lt fd 7
107bit = shl ulong 1 fd
108o = get df id
109if ne and o bit 0
110flag
111end
112put df id or o bit
113else
114flag
115end
116end
117
118note the mask of wd consecutive instruction bits from bit st, into ex
119fn span st ulong wd ulong to void
120for k ulong 3
121put ex k 0
122end
123for i ulong wd
124c = add st i
125if lt c 96
126w = div c 32
127put ex w or get ex w shl ulong 1 and c 31
128else
129flag
130end
131end
132end
133
134note a linear field's mask must be exactly its span
135fn dspan st ulong wd ulong to void
136span st wd
137for k ulong 3
138if ne get ex k get dm k
139flag
140end
141end
142end
143
144note one D record: id field m0 m1 m2 start width
145fn readd to void
146put ip 0 add get ip 0 1
147id = num
148fd = num
149for k ulong 3
150put dm k num
151end
152st = num
153wd = num
154if lt id 4096
155dunion id
156dfield id fd
157if gt id get dmax 0
158put dmax 0 id
159end
160else
161flag
162end
163if gt wd 96
164flag
165else
166if gt wd 0
167dspan st wd
168end
169end
170end
171
172note form j's field union must equal its mask
173fn dform j ulong to void
174for k ulong 3
175u = get du add mul j 3 k
176m = get tf add mul j 8 add k 4
177if ne u m
178flag
179end
180end
181end
182
183note every D record names a listed form, and each form's field union equals its mask
184fn dcheck to void
185n = get nt 0
186if ge get dmax 0 n
187flag
188end
189for j ulong n
190dform j
191end
192end
193
194note parse records up to the E line; the offset of the kernel bytes, or 0 when no E line
195fn table n ulong to ulong
196put ip 0 0
197while lt get ip 0 n bound 1000000
198c = get inb get ip 0
199if eq c 69
200skipline n
201ret get ip 0
202end
203if eq c 70
204readf
205end
206if eq c 68
207readd
208end
209skipline n
210end
211ret 0
212end
213
214fn rdw q ulong to ulong
215a = ulong get inb q
216b = shl ulong get inb add q 1 8
217c = shl ulong get inb add q 2 16
218d = shl ulong get inb add q 3 24
219ret or or a b or c d
220end
221
222note 1 when form j explains the instruction at q
223fn fits j ulong q ulong to bool
224b = mul j 8
225ok = true
226for k ulong 3
227x = rdw add q mul k 4
228m = get tf add b add k 4
229if ne and x xor m 4294967295 get tf add b add k 1
230ok = false
231end
232end
233ret ok
234end
235
236note the first form that explains the instruction at q, or nt when none does
237fn find q ulong to ulong
238r = get nt 0
239for j ulong get nt 0
240if and eq r get nt 0 fits j q
241r = j
242end
243end
244ret r
245end
246
247fn putw o ulong x ulong to void
248for j ulong 4
249put obuf add o j uchar and shr x mul j 8 255
250end
251end
252
253note word k of form b's instruction: fixed bits, or the input's bits inside the operand fields
254fn rword b ulong q ulong k ulong to ulong
255x = rdw add q mul k 4
256m = get tf add b add k 4
257f = get tf add b add k 1
258ret or f and x m
259end
260
261note rebuild instruction i (input at q) from form j into obuf
262fn rebuild i ulong q ulong j ulong to void
263b = mul j 8
264o = mul i 16
265for k ulong 3
266y = rword b q k
267putw add o mul k 4 y
268end
269putw add o 12 rdw add q 12
270end
271
272fn pch c ulong to void
273p = get ln 0
274put lb p uchar c
275put ln 0 add p 1
276end
277
278fn pnum v ulong to void
279n = ulong 0
280x = v
281while gt x 0 bound 32
282q = div x 10
283put dg n uchar add 48 sub x mul q 10
284x = q
285n = add n 1
286end
287if eq n 0
288pch 48
289end
290k = n
291while gt k 0 bound 32
292k = sub k 1
293pch ulong get dg k
294end
295end
296
297fn wrall n ulong to long
298d = ulong 0
299while lt d n bound 1000000
300w = write 1 obuf d sub n d
301if le w 0
302ret w
303end
304d = add d ulong w
305end
306ret long d
307end
308
309note "N U" on stderr: instructions read, instructions no form explains
310fn report n ulong u ulong to void
311put ln 0 0
312pnum n
313pch 32
314pnum u
315pch 10
316w = write 2 lb 0 get ln 0
317end
318
319fn main to int
320r = rdall
321if lt r 0
322ret 2
323end
324n = ulong r
325ks = table n
326if eq ks 0
327ret 6
328end
329dcheck
330if ne get bad 0 0
331ret 7
332end
333kl = sub n ks
334if ne and kl 15 0
335ret 5
336end
337ni = div kl 16
338u = ulong 0
339for i ulong ni
340q = add ks mul i 16
341j = find q
342if lt j get nt 0
343rebuild i q j
344else
345u = add u 1
346end
347end
348report ni u
349w = wrall kl
350if ne w long kl
351ret 3
352end
353if ne u 0
354ret 4
355end
356ret 0
357end