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 / ngas.om

684 lines · 10347 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/ngas.om: canonical ΩIR text on stdin to the binary on stdout (seed/ng/IR.md s10).
3note Owner: ng-ir lane. Build: cat seed/ng/ngtxt.om seed/ng/ngas.om | seed/out/omc > ngas
4note ngas reads only the text ngdis prints and checks syntax and numbering only (type, global and
5note function numbers in order, defined values numbered in order, the defined values an op row and
6note its ty call for, operand counts, const forms); every semantic rule is ngcheck's. Refusal:
7note "ngas: syntax error at line N" on stderr and exit 3; input or output over 16 MiB exit 23; more
8note than 65536 types, globals, functions, blocks in a function, or parameters of a function or block:
9note "ngas: limit at line N" (the line of the first one over) and exit 23 (IR.md s12);
10note unreadable input exit 27. Wire ids are written as the text gives them, in range or not.
11text w_msg "ngas: syntax error at line \n"
12text w_lim "ngas: limit at line "
13text w_tag "omega.ng.ir.v1"
14global w_in uchar 16777216
15global w_ob uchar 16777216
16global w_s ulong 8
17global w_k ulong 8
18global w_op uint 128
19note w_s: 0 cursor, 1 input end, 2 line number, 3 output length, 4 next value id
20note refuse with LIMIT when k items are already in the table (IR.md s12: at most 65536)
21fn as_lim k ulong to void
22if lt k 65536
23ret
24end
25r = write 2 w_lim 0 len w_lim
26put x_on 0 0
27tx_num get w_s 2
28tx_c 10
29r = write 2 x_out 0 get x_on 0
30exit 23
31end
32fn as_err to void
33r = write 2 w_msg 0 27
34put x_on 0 0
35tx_num get w_s 2
36tx_c 10
37r = write 2 x_out 0 get x_on 0
38exit 3
39end
40note ---- input ------------------------------------------------------------------------------------
41note the byte at the cursor plus d, or 256 past the end
42fn as_pk d ulong to ulong
43p = add get w_s 0 d
44if ge p get w_s 1
45ret 256
46end
47ret ulong get w_in p
48end
49fn as_eat c ulong to void
50if ne as_pk 0 c
51as_err
52end
53put w_s 0 add get w_s 0 1
54if eq c 10
55put w_s 2 add get w_s 2 1
56end
57end
58fn as_sp to void
59as_eat 32
60end
61fn as_eol to void
62as_eat 10
63end
64fn as_isd c ulong to bool
65ret and ge c 48 le c 57
66end
67note a decimal number without sign, leading zeros or overflow
68fn as_dec to ulong
69c = as_pk 0
70if not as_isd c
71as_err
72end
73if and eq c 48 as_isd as_pk 1
74as_err
75end
76v = ulong 0
77while as_isd as_pk 0 bound 21
78d = sub as_pk 0 48
79if gt v div sub 18446744073709551615 d 10
80as_err
81end
82v = add mul v 10 d
83put w_s 0 add get w_s 0 1
84end
85ret v
86end
87fn as_dlim m ulong to ulong
88v = as_dec
89if gt v m
90as_err
91end
92ret v
93end
94fn as_d32 to ulong
95ret as_dlim 4294967295
96end
97note the end of the token at the cursor (next space, newline, @ or input end)
98fn as_tend to ulong
99p = get w_s 0
100while lt p get w_s 1 bound 16777216
101c = get w_in p
102if or or eq c 32 eq c 10 eq c 64
103ret p
104end
105p = add p 1
106end
107ret p
108end
109note word i of x_kw as the whole token at the cursor; consumed when matched and take
110fn as_word i ulong take bool to bool
111p = get w_s 0
112e = as_tend
113n = ulong get x_kwl i
114if ne sub e p n
115ret false
116end
117s = ulong get x_kwo i
118for j ulong n bound 16
119if ne get w_in add p j get x_kw add s j
120ret false
121end
122end
123if take
124put w_s 0 e
125end
126ret true
127end
128fn as_kw i ulong to void
129if not as_word i true
130as_err
131end
132end
133note one of words lo..hi: its index minus lo
134fn as_wsel lo ulong hi ulong to ulong
135for i ulong add sub hi lo 1 bound 32
136if as_word add lo i true
137ret i
138end
139end
140as_err
141ret 0
142end
143note a type: a built-in name, or %tN for any N that has no name
144fn as_ty to ulong
145if eq as_pk 0 37
146as_sp2 37 116
147n = as_d32
148if le n 20
149ck_named n
150end
151ret n
152end
153p = get w_s 0
154e = as_tend
155for t ulong 21
156if as_tyis t p e
157put w_s 0 e
158ret t
159end
160end
161as_err
162ret 0
163end
164fn ck_named n ulong to void
165if ne get x_tnl n 1
166as_err
167end
168end
169fn as_tyis t ulong p ulong e ulong to bool
170n = ulong get x_tnl t
171if ne sub e p n
172ret false
173end
174s = ulong get x_tno t
175if eq get x_tyn s 45
176ret false
177end
178for j ulong n bound 8
179if ne get w_in add p j get x_tyn add s j
180ret false
181end
182end
183ret true
184end
185fn as_sp2 a ulong b ulong to void
186as_eat a
187as_eat b
188end
189fn as_hexd to ulong
190c = as_pk 0
191put w_s 0 add get w_s 0 1
192if as_isd c
193ret sub c 48
194end
195if and ge c 97 le c 102
196ret sub c 87
197end
198as_err
199ret 0
200end
201fn as_hexn n ulong to ulong
202v = ulong 0
203for j ulong n bound 16
204v = or shl v 4 as_hexd
205end
206ret v
207end
208note ---- output -------------------------------------------------------------------------------------
209fn as_b k ulong v ulong to void
210n = get w_s 3
211if gt add n k len w_ob
212exit 23
213end
214for j ulong k bound 8
215put w_ob add n j uchar and shr v mul j 8 255
216end
217put w_s 3 add n k
218end
219fn as_patch at ulong v ulong to void
220for j ulong 4
221put w_ob add at j uchar and shr v mul j 8 255
222end
223end
224fn as_here to ulong
225ret get w_s 3
226end
227note ---- module ------------------------------------------------------------------------------------
228fn as_head to void
229as_kw 29
230as_sp
231as_eat 49
232as_eol
233as_b 1 79
234as_b 1 83
235as_b 1 67
236as_b 1 49
237as_b 1 73
238as_b 1 82
239as_b 1 0
240as_b 1 9
241as_tagw
242as_b 2 0
243end
244fn as_tagw to void
245for j ulong len w_tag
246as_b 1 ulong get w_tag j
247end
248end
249fn as_types to void
250at = as_here
251as_b 4 0
252k = ulong 0
253while as_word 27 true bound 65537
254as_lim k
255as_type add k 32
256k = add k 1
257end
258as_patch at k
259end
260fn as_type t ulong to void
261as_sp
262as_sp2 37 116
263ck_eqn as_d32 t
264as_sp
265as_eat 61
266as_sp
267kd = add as_wsel 15 20 1
268as_b 1 kd
269sz = ulong 0
270if or eq kd 1 eq kd 2
271sp = as_tva
272sz = sp
273end
274if eq kd 3
275sz = as_tstr
276end
277if or eq kd 4 eq kd 5
278as_tref
279end
280if eq kd 6
281as_tfrg
282end
283as_eol
284tx_tset t kd sz
285end
286fn ck_eqn a ulong b ulong to void
287if ne a b
288as_err
289end
290end
291fn as_tva to ulong
292as_sp
293e = as_ty
294as_sp
295c = as_d32
296as_b 4 e
297as_b 4 c
298ret tx_mul c tx_size e
299end
300fn as_tstr to ulong
301as_sp
302sz = as_d32
303as_b 4 sz
304at = as_here
305as_b 4 0
306nf = ulong 0
307while eq as_pk 0 32 bound 16777216
308as_sp
309as_b 4 as_ty
310as_eat 64
311as_b 4 as_d32
312as_eat 58
313as_b 1 as_wsel 13 14
314nf = add nf 1
315end
316as_patch at nf
317ret sz
318end
319fn as_tref to void
320as_sp
321ac = as_wsel 3 4
322as_sp
323sp = as_wsel 5 9
324as_sp
325as_b 4 as_ty
326as_b 1 ac
327as_b 1 sp
328end
329fn as_tfrg to void
330as_sp
331as_b 1 as_wsel 10 12
332for j ulong 3
333as_sp
334as_b 2 as_dlim 65535
335end
336as_sp
337as_b 4 as_ty
338end
339fn as_globs to void
340at = as_here
341as_b 4 0
342k = ulong 0
343while as_word 6 true bound 65537
344as_lim k
345as_glob k
346k = add k 1
347end
348as_patch at k
349end
350fn as_glob g ulong to void
351as_sp
352as_sp2 64 103
353ck_eqn as_d32 g
354as_sp
355t = as_ty
356as_sp
357c = ulong 2
358if not as_word 32 true
359c = as_wsel 21 22
360end
361as_b 4 t
362as_b 1 c
363if eq c 1
364n = tx_size t
365if gt n 0
366as_sp
367end
368for j ulong n bound 16777216
369as_b 1 as_hexn 2
370end
371end
372as_eol
373end
374note ---- functions ----------------------------------------------------------------------------------
375fn as_funcs to void
376at = as_here
377as_b 4 0
378k = ulong 0
379while lt get w_s 0 get w_s 1 bound 65537
380as_lim k
381as_fn k
382k = add k 1
383end
384as_patch at k
385end
386fn as_fn f ulong to void
387k = as_wsel 0 2
388as_sp
389as_sp2 64 102
390ck_eqn as_d32 f
391as_sp
392as_kw 23
393as_sp
394fl = as_dlim 255
395if eq k 1
396as_kern
397end
398as_b 1 k
399as_b 1 fl
400at = as_here
401as_b 4 0
402n = ulong 0
403while and eq as_pk 0 32 eq as_pk 1 37 bound 65537
404as_lim n
405as_sp
406as_eat 37
407ck_eqn as_dec add n 1
408as_eat 58
409as_b 4 as_ty
410n = add n 1
411end
412as_patch at n
413as_sp
414as_sp2 45 62
415as_sp
416as_b 4 as_ty
417as_eol
418if eq k 1
419for j ulong 3
420as_b 4 get w_k j
421end
422as_b 1 get w_k 3
423end
424put w_s 4 add n 1
425as_blks
426end
427fn as_kern to void
428as_sp
429as_kw 24
430for j ulong 3
431as_sp
432put w_k j as_d32
433end
434as_sp
435as_kw 25
436as_sp
437put w_k 3 as_dlim 255
438end
439fn as_blks to void
440at = as_here
441as_b 4 0
442j = ulong 0
443while as_word 26 true bound 65537
444as_lim j
445as_blk j
446j = add j 1
447end
448as_kw 28
449as_eol
450as_patch at j
451end
452fn as_blk j ulong to void
453as_sp
454as_eat 94
455ck_eqn as_dec j
456at = as_here
457as_b 4 0
458n = ulong 0
459while eq as_pk 0 32 bound 65537
460as_lim n
461as_sp
462as_eat 37
463v = get w_s 4
464ck_eqn as_dec v
465put w_s 4 add v 1
466as_eat 58
467as_b 4 as_ty
468n = add n 1
469end
470as_eol
471as_patch at n
472at = as_here
473as_b 4 0
474n = 0
475while as_more bound 16777216
476as_op
477n = add n 1
478end
479as_patch at n
480end
481note another op line follows (not block, not end, not the input end)
482fn as_more to bool
483if ge get w_s 0 get w_s 1
484ret false
485end
486if as_word 26 false
487ret false
488end
489ret not as_word 28 false
490end
491fn as_op to void
492nd = ulong 0
493put w_k 4 0
494put w_k 5 0
495if eq as_pk 0 37
496as_eat 37
497put w_k 4 as_dec
498nd = 1
499if and eq as_pk 0 32 eq as_pk 1 37
500as_sp
501as_eat 37
502put w_k 5 as_dec
503nd = 2
504end
505as_sp
506as_eat 61
507as_sp
508end
509op = as_mnem
510as_b 1 op
511t = ulong 0
512if ne get x_rty op 0
513as_sp
514t = as_ty
515end
516as_b 4 t
517if ne get x_rim op 0
518as_sp
519as_b 8 as_imm op t
520end
521as_defs op t nd
522as_opnds op
523end
524fn as_defs op ulong t ulong nd ulong to void
525d = tx_ndef op t
526ck_eqn nd d
527v = get w_s 4
528if ge d 1
529ck_eqn get w_k 4 v
530end
531if eq d 2
532ck_eqn get w_k 5 add v 1
533end
534put w_s 4 add v d
535end
536fn as_mnem to ulong
537p = get w_s 0
538e = as_tend
539for op ulong 256
540if ne get x_rok op 0
541if as_rnis op p e
542put w_s 0 e
543ret op
544end
545end
546end
547as_err
548ret 0
549end
550fn as_rnis op ulong p ulong e ulong to bool
551n = ulong get x_rnl op
552if ne sub e p n
553ret false
554end
555s = ulong get x_rno op
556for j ulong n bound 16
557if ne get w_in add p j get x_rows add s j
558ret false
559end
560end
561ret true
562end
563fn as_imm op ulong t ulong to ulong
564if or eq op 52 and ge op 111 le op 116
565as_sp2 64 103
566ret as_dec
567end
568if ne op 1
569ret as_dec
570end
571f = tx_cf t
572if eq f 1
573ret sub 1 as_wsel 30 31
574end
575if eq f 4
576as_sp2 48 120
577ret as_hexn mul tx_sb t 2
578end
579if eq f 3
580ret as_sint t
581end
582v = as_dec
583if not tx_canon t v
584as_err
585end
586ret v
587end
588fn as_sint t ulong to ulong
589ng = eq as_pk 0 45
590if ng
591as_eat 45
592end
593v = as_dec
594if ng
595if or eq v 0 gt v 9223372036854775808
596as_err
597end
598v = sub 0 v
599else
600if ne shr v 63 0
601as_err
602end
603end
604if not tx_canon t v
605as_err
606end
607ret v
608end
609fn as_opnds op ulong to void
610c = ulong 0
611while eq as_pk 0 32 bound 129
612as_sp
613if ge c 128
614as_err
615end
616put w_op c uint as_opnd op c
617c = add c 1
618end
619as_eol
620fx = add tx_ntok op ulong get x_rfx op
621if ne get x_rva op 0
622if lt c fx
623as_err
624end
625as_b 4 sub c fx
626else
627ck_eqn c fx
628end
629for j ulong c bound 128
630as_b 4 ulong get w_op j
631end
632end
633fn as_opnd op ulong s ulong to ulong
634k = tx_slot op s
635if eq k 1
636as_eat 94
637ret as_d32
638end
639if eq k 2
640as_sp2 64 102
641ret as_d32
642end
643as_eat 37
644ret as_d32
645end
646fn main to int
647tx_init
648n = ulong 0
649while lt n len w_in bound 16777217
650k = read 0 w_in n sub len w_in n
651if lt k 0
652exit 27
653end
654if eq k 0
655break
656end
657n = add n ulong k
658end
659if eq n len w_in
660k = read 0 w_ob 0 1
661if lt k 0
662exit 27
663end
664if gt k 0
665exit 23
666end
667end
668put w_s 1 n
669put w_s 2 1
670as_head
671as_types
672as_globs
673as_funcs
674o = ulong 0
675m = get w_s 3
676while lt o m bound 16777217
677k = write 1 w_ob o sub m o
678if le k 0
679exit 27
680end
681o = add o ulong k
682end
683ret 0
684end