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 / omc / pinned.om

3986 lines · 68349 bytes Raw Blame History
1note omc: the Omega compiler written in Omega (the om4 subset). Reads Omegese on stdin, writes a runnable
2note AArch64 Linux program (or bare function words) on stdout. Built first by om4; its codegen matches om4
3note byte for byte on the original subset (seed chain rung 5, ledger L408). Native host operations
4note extend that subset; om4 still builds this compiler, and the compiler rebuilds itself identically.
5note All compiler state lives in fixed global pools (decision 24: no heap). Every working value is a ulong
6note (raw 64 bits) so the language's checked conversions never fire on om4's untyped-register logic.
7
8note ---- input, output, token ----
9global inp uchar 8388608
10global inlen ulong 1
11global cur ulong 1
12global line ulong 1
13global tk ulong 1
14global tw ulong 1
15global tl ulong 1
16global ts ulong 1
17global tbig ulong 1
18global out uchar 8388608
19global oc ulong 1
20
21note ---- name table (1024 names in scope): packed name, register, type, length, kind, address ----
22global nm_name ulong 1024
23global nm_reg uchar 1024
24global nm_type uchar 1024
25global nm_len ulong 1024
26global nm_kind uchar 1024
27global nm_addr ulong 1024
28global nn ulong 1
29
30note ---- function table (512): name, address (byte offset in out), return type, parameter count,
31note per-parameter type/length/kind (6 each), name length, name offset in the input ----
32global fn_name ulong 512
33global fn_addr ulong 512
34global fn_ret uchar 512
35global fn_np uchar 512
36global fn_pt uchar 3072
37global fn_pl ulong 3072
38global fn_pk uchar 3072
39global fn_nmlen ulong 512
40global fn_off ulong 512
41global nf ulong 1
42global lastfn ulong 1
43global scanning ulong 1
44global ncalls ulong 1
45global callsite ulong 65536
46global callfrom ulong 65536
47global callto ulong 65536
48global settled uchar 512
49
50
51note ---- compiler state ----
52global npar ulong 1
53global held ulong 1
54global nslot ulong 1
55global curret ulong 1
56global progmode ulong 1
57global nglob ulong 1
58global hostargs ulong 1
59global stkbytes ulong 1
60global dead ulong 1
61global poolb ulong 1
62
63note ======== leaf helpers ========
64
65note poolc: the i-th register of the pool x0..x6, x8..x15, x17 (x7 is the runtime, x16 scratch)
66fn poolc i ulong to ulong
67if lt i 7
68ret i
69end
70if lt i 15
71ret add i 1
72end
73ret 17
74end
75
76note ---- error reporting ----
77global msg uchar 64
78global mc ulong 1
79
80fn mputb b uchar to void
81m = get mc 0
82put msg m b
83put mc 0 add m 1
84end
85
86fn mputn v ulong to void
87d = new uchar 20 0
88i = ulong 0
89x = v
90put d 0 uchar add rem x 10 48
91x = div x 10
92i = add i 1
93while gt x 0
94put d i uchar add rem x 10 48
95x = div x 10
96i = add i 1
97end
98j = i
99while gt j 0
100j = sub j 1
101mputb get d j
102end
103end
104
105note fail: write "omc: line N: error E" to stderr (fd 2) and exit with the code
106fn fail code ulong to void
107put mc 0 0
108mputb 111
109mputb 109
110mputb 99
111mputb 58
112mputb 32
113mputb 108
114mputb 105
115mputb 110
116mputb 101
117mputb 32
118mputn get line 0
119mputb 58
120mputb 32
121mputb 101
122mputb 114
123mputb 114
124mputb 111
125mputb 114
126mputb 32
127mputn code
128mputb 10
129w = write 2 msg 0 get mc 0
130exit int code
131end
132
133note emitb / emit: one byte / one 32-bit word (little-endian) to the output; error 14 when it is full
134note (om4 EMIT, EMITB: the 8 MiB output area)
135fn emitb b uchar to void
136o = get oc 0
137if ge o 8388608
138fail 14
139end
140put out o b
141put oc 0 add o 1
142end
143
144fn emit w ulong to void
145if ge get oc 0 8388608
146fail 14
147end
148emitb uchar and w 255
149emitb uchar and shr w 8 255
150emitb uchar and shr w 16 255
151emitb uchar and shr w 24 255
152end
153
154note tok_num: the word at [ts,cur) is [-]decimal, at most 20 digits, from -2^63 to 2^64 - 1 (error 6
155note bad number, 8 out of range). tw = its raw 64 bits (a negative one as two's complement); tbig = 1
156note when it is 2^63 or more (only a ulong holds it), else 0. As om4's TK_NUM.
157fn tok_num to void
158p = get ts 0
159e = get cur 0
160minus = false
161if eq get inp p 45
162minus = true
163p = add p 1
164end
165ndig = sub e p
166if eq ndig 0
167fail 6
168end
169if gt ndig 20
170fail 6
171end
172v = ulong 0
173lim = ulong 1844674407370955161
174i = p
175while lt i e
176d = sub ulong get inp i 48
177if gt d 9
178fail 6
179end
180if gt v lim
181fail 8
182end
183if and eq v lim gt d 5
184fail 8
185end
186v = add mul v 10 d
187i = add i 1
188end
189half = shl ulong 1 63
190put tbig 0 0
191if minus
192if gt v half
193fail 8
194end
195put tw 0 sub ulong 0 v
196else
197if ge v half
198put tbig 0 1
199end
200put tw 0 v
201end
202put tk 0 2
203end
204
205note tok_text: a text literal starting at the opening quote. tw = first byte offset, tl = source length
206note (escapes not yet decoded). Ends at an unescaped '"' on the same line, followed by a blank/end. Err 22.
207note (Loops over the input are two loops, the inner under 1000000 passes: om4 has no pass limit.)
208fn tok_text to void
209put cur 0 add get cur 0 1
210put tw 0 get cur 0
211go = true
212while go
213n = ulong 0
214while and go lt n 999999
215if ge get cur 0 get inlen 0
216fail 22
217end
218b = get inp get cur 0
219if eq b 10
220fail 22
221end
222if eq b 34
223go = false
224else
225put cur 0 add get cur 0 1
226if eq b 92
227if ge get cur 0 get inlen 0
228fail 22
229end
230if eq get inp get cur 0 10
231fail 22
232end
233put cur 0 add get cur 0 1
234end
235end
236n = add n 1
237end
238end
239put tl 0 sub get cur 0 get tw 0
240put cur 0 add get cur 0 1
241put tk 0 4
242if lt get cur 0 get inlen 0
243if gt get inp get cur 0 32
244fail 22
245end
246end
247end
248
249note tok2: a word at cur with first byte c; packs 8 bytes into tw, sets tl and ts. A '"' is a text; a
250note first byte '-' or a digit is a number.
251fn tok2 c uchar to void
252if eq c 34
253tok_text
254ret
255end
256put ts 0 get cur 0
257w = ulong 0
258sh = ulong 0
259n = ulong 0
260go = true
261while go
262m = ulong 0
263while and go lt m 999999
264if ge get cur 0 get inlen 0
265go = false
266else
267b = get inp get cur 0
268if le b 32
269go = false
270else
271if lt n 8
272w = or w shl ulong b sh
273sh = add sh 8
274end
275n = add n 1
276put cur 0 add get cur 0 1
277end
278end
279m = add m 1
280end
281end
282put tw 0 w
283put tl 0 n
284f = get inp get ts 0
285isnum = false
286if eq f 45
287isnum = true
288end
289if and ge f 48 le f 57
290isnum = true
291end
292if isnum
293tok_num
294else
295put tk 0 1
296end
297end
298
299note tok: next word, skipping blanks (space, tab, carriage return); newline is end of line (kind 0) and
300note bumps the line number; another byte below 32 is error 16; the end of the input is kind 3. om4 TOK.
301fn tok to void
302go = true
303while go
304n = ulong 0
305while and go lt n 999999
306if ge get cur 0 get inlen 0
307go = false
308else
309c = get inp get cur 0
310if or or eq c 32 eq c 9 eq c 13
311put cur 0 add get cur 0 1
312else
313if eq c 10
314put cur 0 add get cur 0 1
315put line 0 add get line 0 1
316put tk 0 0
317ret
318end
319if lt c 32
320fail 16
321end
322tok2 c
323ret
324end
325end
326n = add n 1
327end
328end
329put tk 0 3
330end
331
332note ======== keyword lookup (original om4 KWTAB plus hand-maintained native host words 100..134) ========
333note genkw.awk describes the original om4 table only; retain the native host extension on regeneration.
334fn kw w ulong to ulong
335if eq w 6584163
336ret 132
337end
338packed = or ulong 114 shl ulong 101 8
339packed = or packed shl ulong 97 16
340packed = or packed shl ulong 100 24
341packed = or packed shl ulong 108 32
342packed = or packed shl ulong 110 40
343packed = or packed shl ulong 107 48
344if eq w packed
345ret 133
346end
347packed = or ulong 115 shl ulong 111 8
348packed = or packed shl ulong 99 16
349packed = or packed shl ulong 107 24
350packed = or packed shl ulong 111 32
351packed = or packed shl ulong 112 40
352packed = or packed shl ulong 116 48
353if eq w packed
354ret 134
355end
356if eq w 1819242338
357ret 1
358end
359if eq w 491260502901
360ret 2
361end
362if eq w 14453
363ret 2
364end
365if eq w 128034844210037
366ret 3
367end
368if eq w 3551605
369ret 3
370end
371if eq w 1953393013
372ret 4
373end
374if eq w 3289973
375ret 4
376end
377if eq w 444234427509
378ret 5
379end
380if eq w 3421813
381ret 5
382end
383if eq w 1702132066
384ret 6
385end
386if eq w 14441
387ret 6
388end
389if eq w 500136110195
390ret 7
391end
392if eq w 3551593
393ret 7
394end
395if eq w 7630441
396ret 8
397end
398if eq w 3289961
399ret 8
400end
401if eq w 1735290732
402ret 9
403end
404if eq w 3421801
405ret 9
406end
407if eq w 6579297
408ret 33
409end
410if eq w 6452595
411ret 34
412end
413if eq w 7107949
414ret 35
415end
416if eq w 7760228
417ret 36
418end
419if eq w 7169394
420ret 37
421end
422if eq w 6581857
423ret 38
424end
425if eq w 29295
426ret 39
427end
428if eq w 7499640
429ret 40
430end
431if eq w 7104627
432ret 41
433end
434if eq w 7497843
435ret 42
436end
437if eq w 29029
438ret 43
439end
440if eq w 25966
441ret 44
442end
443if eq w 29804
444ret 45
445end
446if eq w 25964
447ret 46
448end
449if eq w 29799
450ret 47
451end
452if eq w 25959
453ret 48
454end
455if eq w 6776174
456ret 49
457end
458if eq w 7630702
459ret 50
460end
461if eq w 7628135
462ret 51
463end
464if eq w 7234924
465ret 52
466end
467if eq w 1684104562
468ret 53
469end
470if eq w 435744764535
471ret 54
472end
473if eq w 1702195828
474ret 55
475end
476if eq w 435728179558
477ret 56
478end
479if eq w 2003134838
480ret 57
481end
482if eq w 1953064037
483ret 58
484end
485if eq w 28262
486ret 64
487end
488if eq w 28532
489ret 65
490end
491if eq w 6581861
492ret 66
493end
494if eq w 26217
495ret 67
496end
497if eq w 1702063205
498ret 68
499end
500if eq w 435610544247
501ret 69
502end
503if eq w 431349919586
504ret 70
505end
506if eq w 7499622
507ret 71
508end
509if eq w 7628146
510ret 72
511end
512if eq w 61
513ret 73
514end
515if eq w 40
516ret 74
517end
518if eq w 41
519ret 75
520end
521if eq w 1684631414
522ret 76
523end
524if eq w 119165519096935
525ret 78
526end
527if eq w 1954047348
528ret 79
529end
530if eq w 7632240
531ret 80
532end
533if eq w 1701147238
534ret 81
535end
536if eq w 1953069157
537ret 82
538end
539if eq w 1702129518
540ret 83
541end
542if eq w 7824750
543ret 84
544end
545if eq w 461195539042
546ret 77
547end
548if eq w 1885434484
549ret 77
550end
551if eq w 7239535
552ret 77
553end
554if eq w 7308604897168158579
555ret 131
556end
557if eq w 126918101919090
558ret 130
559end
560if eq w 7453301735350958181
561ret 124
562end
563if eq w 32481142782190962
564ret 125
565end
566if eq w 491394461042
567ret 126
568end
569if eq w 28263429247363435
570ret 127
571end
572if eq w 32480013273033072
573ret 128
574end
575if eq w 110404021020007
576ret 129
577end
578if eq w 122498715312503
579ret 123
580end
581if eq w 32497639802299751
582ret 113
583end
584if eq w 32760384594014051
585ret 114
586end
587if eq w 1684957538
588ret 115
589end
590if eq w 28263429381382519
591ret 116
592end
593if eq w 499851228006
594ret 117
595end
596if eq w 1986225522
597ret 118
598end
599if eq w 1684956531
600ret 119
601end
602if eq w 32491051338263667
603ret 120
604end
605if eq w 128017462948449
606ret 121
607end
608if eq w 28254586010038881
609ret 122
610end
611if eq w 111546465018726
612ret 100
613end
614if eq w 427055215462
615ret 101
616end
617if eq w 110429739705190
618ret 102
619end
620if eq w 461262648166
621ret 103
622end
623if eq w 33607047151054707
624ret 104
625end
626if eq w 121382024472940
627ret 105
628end
629if eq w 128026086171489
630ret 106
631end
632if eq w 1802661734
633ret 107
634end
635if eq w 431215375716
636ret 108
637end
638if eq w 126918153105261
639ret 109
640end
641if eq w 474148269926
642ret 110
643end
644if eq w 118121959288437
645ret 111
646end
647if eq w 32204027264196973
648ret 112
649end
650ret 0
651end
652
653note ======== om4's small tables, as functions ========
654note twid: width in bits by type 0..9 (bool 1; uchar ushort uint ulong; byte short int long)
655fn twid t ulong to ulong
656if eq t 1
657ret 1
658end
659if or eq t 2 eq t 6
660ret 8
661end
662if or eq t 3 eq t 7
663ret 16
664end
665if or eq t 4 eq t 8
666ret 32
667end
668if or eq t 5 eq t 9
669ret 64
670end
671ret 0
672end
673
674note tlog: log2 of the element size by type
675fn tlog t ulong to ulong
676if or eq t 3 eq t 7
677ret 1
678end
679if or eq t 4 eq t 8
680ret 2
681end
682if or eq t 5 eq t 9
683ret 3
684end
685ret 0
686end
687
688note regidx: the pool index of register r (the inverse of poolc; 255 for x7 and x16)
689fn regidx r ulong to ulong
690if lt r 7
691ret r
692end
693if eq r 7
694ret 255
695end
696if lt r 16
697ret sub r 1
698end
699if eq r 16
700ret 255
701end
702ret 15
703end
704
705note conds: the condition for eq ne lt le gt ge (i = 2 (op - 43) + 1 when signed): EQ 0, NE 1, LO 3 / LT 11,
706note LS 9 / LE 13, HI 8 / GT 12, HS 2 / GE 10 (Arm ARM Table C1-1). The opposite condition is cond xor 1.
707fn conds i ulong to ulong
708if lt i 2
709ret 0
710end
711if lt i 4
712ret 1
713end
714if eq i 4
715ret 3
716end
717if eq i 5
718ret 11
719end
720if eq i 6
721ret 9
722end
723if eq i 7
724ret 13
725end
726if eq i 8
727ret 8
728end
729if eq i 9
730ret 12
731end
732if eq i 10
733ret 2
734end
735ret 10
736end
737
738note ldop / stop: register-offset load / store by element type (om4 LDOP, STOP tables)
739fn ldop t ulong to ulong
740note LDRB 0x38606800, LDRH 0x78607800, LDR w 0xb8607800, LDR x 0xf8607800, LDRSB 0x38a06800,
741note LDRSH 0x78a07800, LDRSW 0xb8a07800
742if eq t 2
743ret 945842176
744end
745if eq t 3
746ret 2019588096
747end
748if eq t 4
749ret 3093329920
750end
751if or eq t 5 eq t 9
752ret 4167071744
753end
754if eq t 6
755ret 950036480
756end
757if eq t 7
758ret 2023782400
759end
760if eq t 8
761ret 3097524224
762end
763ret 0
764end
765
766fn stop t ulong to ulong
767note STRB 0x38206800, STRH 0x78207800, STR w 0xb8207800, STR x 0xf8207800
768if or eq t 2 eq t 6
769ret 941647872
770end
771if or eq t 3 eq t 7
772ret 2015393792
773end
774if or eq t 4 eq t 8
775ret 3089135616
776end
777if or eq t 5 eq t 9
778ret 4162877440
779end
780ret 0
781end
782
783note ---- trap list (8192 sites per function): output offset of the branch, trap code ----
784global tsite ulong 8192
785global tcode ulong 8192
786global nt ulong 1
787note ---- block stack (512): kind (1 if, 2 else, 3 while, 4 for), branch to patch, loop head, loop
788note register, its type, names and held pool slots to restore at end, then-part ended (for else) ----
789global bkind ulong 512
790global bbr ulong 512
791global bhead ulong 512
792global breg ulong 512
793global btyp ulong 512
794global bnames ulong 512
795global bheld ulong 512
796global bdead ulong 512
797global nb ulong 1
798global curfn ulong 1
799
800note ======== names and functions ========
801note findname: packed name -> its index + 1 among the names in scope (0 none). om4 FINDNAME.
802fn findname w ulong to ulong
803i = ulong 0
804n = get nn 0
805while lt i n
806if eq get nm_name i w
807ret add i 1
808end
809i = add i 1
810end
811ret 0
812end
813
814note findfn: packed name w (first 8 bytes), length l (any value up to 8 for a short name), start s in the
815note input (read only for a name over 8 bytes) -> function index + 1 (0 none). om4 FINDFN.
816fn findfn w ulong l ulong s ulong to ulong
817j = ulong 0
818while lt j get nf 0
819if eq get fn_name j w
820fl = get fn_nmlen j
821if le l 8
822if le fl 8
823ret add j 1
824end
825else
826if eq fl l
827o = get fn_off j
828k = ulong 8
829same = true
830while and same lt k l
831a = get inp add o k
832if ne a get inp add s k
833same = false
834end
835k = add k 1
836end
837if same
838ret add j 1
839end
840end
841end
842end
843j = add j 1
844end
845ret 0
846end
847
848note newname: the name must not be in use (a name in scope when 8 bytes or fewer, or a function): error 10
849fn newname w ulong l ulong s ulong to void
850if le l 8
851if ne findname w 0
852fail 10
853end
854end
855if ne findfn w l s 0
856fail 10
857end
858end
859
860note addname: packed name, register, type, length, kind, address. om4 ADDNAME.
861fn addname w ulong r ulong t ulong ln ulong k ulong a ulong to void
862n = get nn 0
863if ge n 1024
864fail 14
865end
866put nm_name n w
867put nm_reg n uchar r
868put nm_type n uchar t
869put nm_len n ln
870put nm_kind n uchar k
871put nm_addr n a
872put nn 0 add n 1
873end
874
875note ======== registers and words ========
876note alloc: the next free register of the pool (after the parameters); error 12 when none is left
877fn alloc to ulong
878i = add get npar 0 get nslot 0
879if ge i 16
880fail 12
881end
882put nslot 0 add get nslot 0 1
883ret poolc i
884end
885
886note destreg: d, or a new temporary when d is 255
887fn destreg d ulong to ulong
888if eq d 255
889ret alloc
890end
891ret d
892end
893
894note e3: emit op | d | n << 5 | m << 16
895fn e3 op ulong d ulong n ulong m ulong to void
896emit or or or op d shl n 5 shl m 16
897end
898
899note trapsite: note the place and code of a branch to the trap stub, then emit it. trapc: B.cond, as om4.
900fn trapsite w ulong code ulong to void
901n = get nt 0
902if ge n 8192
903fail 14
904end
905put tsite n get oc 0
906put tcode n code
907put nt 0 add n 1
908emit w
909end
910
911fn trapc cc ulong code ulong to void
912trapsite or 1409286144 cc code
913end
914
915fn rdword at ulong to ulong
916a = ulong get out at
917b = shl ulong get out add at 1 8
918c = shl ulong get out add at 2 16
919d = shl ulong get out add at 3 24
920ret or or or a b c d
921end
922
923fn wrword at ulong w ulong to void
924put out at uchar and w 255
925put out add at 1 uchar and shr w 8 255
926put out add at 2 uchar and shr w 16 255
927put out add at 3 uchar and shr w 24 255
928end
929
930note patch19: the word distance from a B.cond / CBZ / CBNZ at `at` to `target`, into bits 5..23
931fn patch19 at ulong target ulong to void
932d = and shr sub target at 2 524287
933wrword at or rdword at shl d 5
934end
935
936note patch26: the word distance from a B at `at` to `target`, into bits 0..25
937fn patch26 at ulong target ulong to void
938d = and shr sub target at 2 67108863
939wrword at or rdword at d
940end
941
942note back: emit B to an earlier place (C6.2.35)
943fn back addr ulong to void
944emit or 335544320 and shr sub addr get oc 0 2 67108863
945end
946
947note matconst: put a 64-bit value in register r: MOVZ (C6.2.285) or, for a value with the top bit set,
948note MOVN (C6.2.284) with the first halfword that differs from the fill, then MOVK (C6.2.283) for each
949note other such halfword. om4 MATCONST.
950fn matconst r ulong v ulong to void
951fill = ulong 0
952mop = ulong 3531603968
953if ne shr v 63 0
954fill = ulong 65535
955mop = ulong 2457862144
956end
957started = false
958hw = ulong 0
959while lt hw 4
960h = and shr v shl hw 4 65535
961if ne h fill
962if started
963emit or or or 4068474880 shl h 5 shl hw 21 r
964else
965emit or or or mop shl xor h fill 5 shl hw 21 r
966started = true
967end
968end
969hw = add hw 1
970end
971if not started
972emit or mop r
973end
974end
975
976note canon: u8/u16/u32 UBFM Xr, Xr, #0, #w-1 (C6.2.487); i8/i16/i32 SBFM (C6.2.355); else nothing
977fn canon r ulong t ulong to void
978if lt t 2
979ret
980end
981if eq t 5
982ret
983end
984if eq t 9
985ret
986end
987base = ulong 3544186880
988if gt t 5
989base = ulong 2470445056
990end
991emit or or or base shl sub twid t 1 10 r shl r 5
992end
993
994note chkext: CMP Xr, Wr, SXTB/SXTH/SXTW (SUBS extended, C6.2.463), then B.NE trap code
995fn chkext r ulong t ulong code ulong to void
996emit or or or 3944742943 shl sub t 2 13 shl r 16 shl r 5
997trapc 1 code
998end
999
1000note range: error 8 unless v is a value of type t; big = the number was written as 2^63 or more
1001fn range v ulong t ulong big ulong to void
1002w = twid t
1003if eq w 64
1004if eq t 9
1005if ne big 0
1006fail 8
1007end
1008ret
1009end
1010if ne big 0
1011ret
1012end
1013if ne shr v 63 0
1014fail 8
1015end
1016ret
1017end
1018if ne big 0
1019fail 8
1020end
1021lim = shl ulong 1 w
1022if lt t 6
1023if ge v lim
1024fail 8
1025end
1026ret
1027end
1028half = shr lim 1
1029if ge add v half lim
1030fail 8
1031end
1032end
1033
1034note cset: CSET Xr, cond = CSINC Xr, XZR, XZR, opposite cond (C6.2.141)
1035fn cset r ulong cc ulong to void
1036emit or or 2594113504 r shl xor cc 1 12
1037end
1038
1039note force: put a value (kind 1 register, 3 number, 4 flags) in register r; a bare number is error 11
1040fn force k ulong v ulong t ulong r ulong to void
1041if eq k 1
1042if ne v r
1043emit or or 2852127712 r shl v 16
1044end
1045ret
1046end
1047if eq k 3
1048matconst r v
1049ret
1050end
1051if eq k 4
1052cset r v
1053ret
1054end
1055fail 11
1056end
1057
1058note matreg: a register holding the value
1059fn matreg k ulong v ulong t ulong to ulong
1060if eq k 1
1061ret v
1062end
1063r = alloc
1064force k v t r
1065ret r
1066end
1067
1068note spop: ADD (C6.2.5) / SUB (C6.2.457) SP, SP by a multiple of 16 below 2^24: low 12 bits, then the
1069note high 12 bits LSL 12; nothing for a zero part
1070fn spop base ulong bytes ulong to void
1071lo = and bytes 4095
1072if ne lo 0
1073emit or or base 1023 shl lo 10
1074end
1075hi = and shr bytes 12 4095
1076if ne hi 0
1077emit or or base 4195327 shl hi 10
1078end
1079end
1080
1081fn spadd bytes ulong to void
1082spop 2432696320 bytes
1083end
1084
1085fn spsub bytes ulong to void
1086spop 3506438144 bytes
1087end
1088
1089note stkchk: program mode only: CMP SP, X28 (C6.2.463); B.LO trap 6
1090fn stkchk to void
1091if ne get progmode 0 0
1092emit 3946603519
1093trapc 3 6
1094end
1095end
1096
1097note epi: give back the stack of the local arrays, then RET (C6.2.338)
1098fn epi to void
1099spadd get stkbytes 0
1100emit 3596551104
1101end
1102
1103fn dead1 to void
1104put dead 0 1
1105end
1106
1107note pushb / pushnh: open a block (om4 PUSHB, split in two because a call takes at most 6 values)
1108fn pushb br ulong k ulong head ulong reg ulong ty ulong to void
1109n = get nb 0
1110if ge n 512
1111fail 14
1112end
1113put bkind n k
1114put bbr n br
1115put bhead n head
1116put breg n reg
1117put btyp n ty
1118put bdead n 0
1119put nb 0 add n 1
1120end
1121
1122fn pushnh names ulong hd ulong to void
1123n = sub get nb 0 1
1124put bnames n names
1125put bheld n hd
1126end
1127
1128note ======== small readers ========
1129fn elemtype t ulong to void
1130if lt t 2
1131fail 9
1132end
1133end
1134
1135note arrlen: an array length 1 .. 16777216 (error 8 below, 23 above, 23 when written as 2^63 or more)
1136fn arrlen v ulong to void
1137if ne get tbig 0 0
1138fail 23
1139end
1140if or eq v 0 ne shr v 63 0
1141fail 8
1142end
1143if gt v 16777216
1144fail 23
1145end
1146end
1147
1148note typeword: read a type name -> 1..9
1149fn typeword to ulong
1150tok
1151k = get tk 0
1152if eq k 0
1153fail 4
1154end
1155if ne k 1
1156fail 16
1157end
1158if gt get tl 0 8
1159fail 16
1160end
1161c = kw get tw 0
1162if eq c 76
1163fail 9
1164end
1165if gt sub c 1 8
1166fail 16
1167end
1168ret c
1169end
1170
1171note eol: the line must end here
1172fn eol to void
1173tok
1174k = get tk 0
1175if and ne k 0 ne k 3
1176fail 5
1177end
1178end
1179
1180note arrname: read an array name -> its index + 1
1181fn arrname to ulong
1182tok
1183k = get tk 0
1184if eq k 0
1185fail 4
1186end
1187if eq k 3
1188fail 4
1189end
1190if ne k 1
1191fail 16
1192end
1193if gt get tl 0 8
1194fail 13
1195end
1196e = findname get tw 0
1197if eq e 0
1198fail 3
1199end
1200if eq get nm_kind sub e 1 0
1201fail 7
1202end
1203ret e
1204end
1205
1206note topname: read a new name for a global or a text -> its packed word
1207fn topname to ulong
1208tok
1209if ne get tk 0 1
1210fail 16
1211end
1212if gt get tl 0 8
1213fail 13
1214end
1215w = get tw 0
1216if ne kw w 0
1217fail 16
1218end
1219newname w get tl 0 get ts 0
1220ret w
1221end
1222
1223note ---- value results (om4 x0 kind 1 register / 2 bare number / 3 typed number / 4 flags, x1 register,
1224note number or condition, x2 type). fr: 32 cells per value nesting depth 0..1024: 0..5 the two operands of
1225note a binary op (kind, value, type each), 8..25 a call's arguments (kind, value, array name each) ----
1226global rk ulong 1
1227global rv ulong 1
1228global rt ulong 1
1229global fr ulong 32800
1230global depth ulong 1
1231
1232note ======== BINOP (om4 BINOP and its BO_* parts): two values already read, types settled ========
1233note bo_w3: emit fx | rd | ra << 5 | (imm12 << 10, or rb << 16)
1234fn bo_w3 fx ulong rd ulong ra ulong rb ulong imm ulong to void
1235if ne imm 0
1236emit or or or fx rd shl ra 5 shl rb 10
1237else
1238emit or or or fx rd shl ra 5 shl rb 16
1239end
1240end
1241
1242note bo_fix: after a 64-bit op on rd: u8/u16/u32 wrap (canon), i8/i16/i32 overflow check (trap 1)
1243fn bo_fix rd ulong ty ulong to void
1244if lt ty 6
1245canon rd ty
1246ret
1247end
1248if eq ty 9
1249ret
1250end
1251chkext rd ty 1
1252end
1253
1254note bo_schk: a shift amount in a register must be below the width: CMP Xb, #w; B.HS trap 7
1255fn bo_schk ty ulong rb ulong to void
1256emit or or 4043309087 shl twid ty 10 shl rb 5
1257trapc 2 7
1258end
1259
1260note add, sub: u32 the 32-bit form; i64 ADDS/SUBS + B.VS trap 1; others 64-bit then bo_fix (om4 BO_ADD)
1261fn bo_add op ulong ty ulong ra ulong rb ulong imm ulong rd ulong to void
1262fx = ulong 2332033024
1263if eq op 34
1264fx = ulong 3405774848
1265end
1266if ne imm 0
1267fx = ulong 2432696320
1268if eq op 34
1269fx = ulong 3506438144
1270end
1271end
1272if eq ty 4
1273bo_w3 xor fx 2147483648 rd ra rb imm
1274ret
1275end
1276if eq ty 9
1277bo_w3 or fx 536870912 rd ra rb imm
1278trapc 6 1
1279ret
1280end
1281bo_w3 fx rd ra rb imm
1282bo_fix rd ty
1283end
1284
1285note mul: i64 SMULH t, MUL, CMP t, rd, ASR #63, B.NE trap 1 (om4 BO_MUL)
1286fn bo_mul ty ulong ra ulong rb ulong rd ulong to void
1287if eq ty 9
1288t = alloc
1289e3 2604694528 t ra rb
1290bo_w3 2600500224 rd ra rb 0
1291e3 3951098911 0 t rd
1292trapc 1 1
1293ret
1294end
1295if eq ty 4
1296bo_w3 453016576 rd ra rb 0
1297ret
1298end
1299bo_w3 2600500224 rd ra rb 0
1300bo_fix rd ty
1301end
1302
1303note div, rem (om4 BO_DIV, BO_SDIV): CBZ Xb trap 2 unless fl bit 1 (b a number other than 0); UDIV or
1304note SDIV; rem: t = a / b, rd = a - t * b (MSUB); i64 div: CMN Xb, #1; B.NE +3; NEGS XZR, Xa; B.VS trap 1,
1305note unless fl bit 2 (b a number other than -1)
1306fn bo_div op ulong ty ulong ra ulong rb ulong rd ulong fl ulong to void
1307if eq and fl 1 0
1308trapsite or 3019898880 rb 2
1309end
1310if eq op 37
1311t = alloc
1312if ge ty 6
1313e3 2596277248 t ra rb
1314else
1315e3 2596276224 t ra rb
1316end
1317e3 or 2600501248 shl ra 10 rd t rb
1318ret
1319end
1320if lt ty 6
1321bo_w3 2596276224 rd ra rb 0
1322ret
1323end
1324if eq ty 9
1325if eq and fl 2 0
1326emit or 2969568287 shl rb 5
1327emit 1409286241
1328emit or 3942646783 shl ra 16
1329trapc 6 1
1330end
1331end
1332bo_w3 2596277248 rd ra rb 0
1333if ne ty 9
1334bo_fix rd ty
1335end
1336end
1337
1338note and, or, xor (om4 BO_LOG)
1339fn bo_log op ulong ra ulong rb ulong rd ulong to void
1340fx = ulong 2315255808
1341if eq op 39
1342fx = ulong 2852126720
1343end
1344if eq op 40
1345fx = ulong 3388997632
1346end
1347bo_w3 fx rd ra rb 0
1348end
1349
1350note bo_lsl: LSL Xd, Xn, #s as UBFM Xd, Xn, #(64-s) mod 64, #63-s
1351fn bo_lsl ra ulong s ulong rd ulong to void
1352r = shl and sub 64 s 63 16
1353r = or r shl sub 63 s 10
1354r = or r shl ra 5
1355emit or or 3544186880 r rd
1356end
1357
1358note shl (om4 BO_SHL, BS_64): by a number s: UBFM Xd, Xn, #(64-s) mod 64, #63-s; by a register: the amount
1359note check, LSLV; i64: t = a << s, X16 = t >> s arithmetic, CMP X16, Xa, B.NE trap 1, MOV Xd, Xt
1360fn bo_shl ty ulong ra ulong rb ulong imm ulong rd ulong to void
1361if eq ty 9
1362t = alloc
1363if ne imm 0
1364bo_lsl ra rb t
1365emit or or 2470509584 shl rb 16 shl t 5
1366else
1367bo_schk ty rb
1368e3 2596282368 t ra rb
1369e3 2596284432 0 t rb
1370end
1371emit or 3942646303 shl ra 16
1372trapc 1 1
1373emit or or 2852127712 rd shl t 16
1374ret
1375end
1376if ne imm 0
1377bo_lsl ra rb rd
1378bo_fix rd ty
1379ret
1380end
1381bo_schk ty rb
1382if eq ty 4
1383bo_w3 448798720 rd ra rb 0
1384ret
1385end
1386bo_w3 2596282368 rd ra rb 0
1387bo_fix rd ty
1388end
1389
1390note shr: unsigned LSR, signed ASR; by a number UBFM / SBFM Xd, Xn, #s, #63; by a register the amount check
1391note then LSRV / ASRV (om4 BO_SHR)
1392fn bo_shr ty ulong ra ulong rb ulong imm ulong rd ulong to void
1393if ne imm 0
1394fi = ulong 3544251392
1395if ge ty 6
1396fi = ulong 2470509568
1397end
1398emit or or or fi shl rb 16 shl ra 5 rd
1399ret
1400end
1401bo_schk ty rb
1402fx = ulong 2596283392
1403if ge ty 6
1404fx = ulong 2596284416
1405end
1406bo_w3 fx rd ra rb 0
1407end
1408
1409note comparisons: CMP Xa, #imm or CMP Xa, Xb; the result is the flags and a condition (om4 BO_CMP)
1410fn bo_cmp op ulong ty ulong ra ulong rb ulong imm ulong to void
1411if ne imm 0
1412emit or or 4043309087 shl rb 10 shl ra 5
1413else
1414emit or or 3942645791 shl rb 16 shl ra 5
1415end
1416i = mul sub op 43 2
1417if ge ty 6
1418i = add i 1
1419end
1420put rk 0 4
1421put rv 0 conds i
1422put rt 0 1
1423end
1424
1425note binop: op 33..48, destination (255 none), ix the fr slot of a (kind, number/register, type) then b.
1426note Result in rk/rv/rt. om4 BINOP.
1427fn binop op ulong dst ulong ix ulong to void
1428ty = get fr add ix 2
1429ra = matreg get fr ix get fr add ix 1 ty
1430imm = ulong 0
1431bk = get fr add ix 3
1432bn = get fr add ix 4
1433rb = ulong 0
1434useimm = false
1435if eq bk 3
1436if or eq op 41 eq op 42
1437if lt bn twid ty
1438useimm = true
1439end
1440else
1441if or lt op 35 ge op 43
1442if le bn 4095
1443useimm = true
1444end
1445end
1446end
1447end
1448if useimm
1449rb = bn
1450imm = ulong 1
1451else
1452rb = matreg bk bn get fr add ix 5
1453end
1454if ge op 43
1455bo_cmp op ty ra rb imm
1456ret
1457end
1458if eq ty 1
1459if gt sub op 38 2
1460fail 7
1461end
1462end
1463rd = destreg dst
1464if lt op 35
1465bo_add op ty ra rb imm rd
1466else
1467if eq op 35
1468bo_mul ty ra rb rd
1469else
1470if lt op 38
1471fl = ulong 0
1472if and eq bk 3 ne bn 0
1473fl = or fl 1
1474end
1475if and eq bk 3 ne bn 18446744073709551615
1476fl = or fl 2
1477end
1478bo_div op ty ra rb rd fl
1479else
1480if lt op 41
1481bo_log op ra rb rd
1482else
1483if eq op 41
1484bo_shl ty ra rb imm rd
1485else
1486bo_shr ty ra rb imm rd
1487end
1488end
1489end
1490end
1491end
1492put rk 0 1
1493put rv 0 rd
1494put rt 0 ty
1495end
1496
1497note ======== calls: registers saved around a call and loaded back ========
1498global svn ulong 1
1499global sva ulong 1
1500note save: n = registers in use (parameters + pool slots), A = 8 (n + 1) rounded up to 16: SUB SP, SP, #A
1501note (C6.2.457); the stack check; STR Xr, [SP, #8j] for each (C6.2.415); STR X30, [SP, #8n]. Out: svn n,
1502note sva A. om4 SAVE.
1503fn save to void
1504n = add get npar 0 get nslot 0
1505a = shl shr add n 2 1 4
1506put svn 0 n
1507put sva 0 a
1508spsub a
1509stkchk
1510j = ulong 0
1511while lt j n
1512emit or or 4177527776 poolc j shl j 10
1513j = add j 1
1514end
1515emit or 4177527806 shl n 10
1516end
1517
1518note loadarg: register t from an argument: kind 1 or 5 a register v, read back from its save slot (LDR Xt,
1519note [SP, #8 slot], C6.2.216); kind 3 or 6 the number or address v. om4 LOADARG.
1520fn loadarg t ulong k ulong v ulong to void
1521if or eq k 3 eq k 6
1522matconst t v
1523ret
1524end
1525emit or or 4181722080 t shl regidx v 10
1526end
1527
1528note basearg: register t = the base of array name e (index + 1): its register from the save area, or its
1529note address. om4 BASEARG.
1530fn basearg t ulong e ulong to void
1531i = sub e 1
1532if ge get nm_kind i 4
1533matconst t get nm_addr i
1534ret
1535end
1536loadarg t 1 ulong get nm_reg i
1537end
1538
1539note restore: d destination (255: a new temporary at pool slot `slot`), slot the pool slot before the
1540note arguments, n saved count, a save bytes, res 1 if there is a result in X0. MOV Xd, X0 (C6.2.282); LDR
1541note each register in use before the arguments except Xd (C6.2.216); LDR X30; ADD SP, SP, #A (C6.2.5).
1542note Returns Xd (255 when no result). om4 RESTORE.
1543fn restore d ulong slot ulong n ulong a ulong res ulong to ulong
1544put nslot 0 slot
1545rd = ulong 255
1546if ne res 0
1547rd = d
1548if eq d 255
1549rd = alloc
1550end
1551if ne rd 0
1552emit or 2852127712 rd
1553end
1554end
1555m = add get npar 0 slot
1556j = ulong 0
1557while lt j m
1558r = poolc j
1559if ne r rd
1560emit or or 4181722080 r shl j 10
1561end
1562j = add j 1
1563end
1564emit or 4181722110 shl n 10
1565spadd a
1566ret rd
1567end
1568
1569note ca_bl: BL to function f (index + 1), C6.2.43
1570fn ca_bl f ulong to void
1571n = get ncalls 0
1572if ge n 65536
1573fail 14
1574end
1575put callsite n get oc 0
1576put callfrom n get curfn 0
1577put callto n f
1578put ncalls 0 add n 1
1579
1580emit or 2483027968 and shr sub get fn_addr sub f 1 get oc 0 2 67108863
1581end
1582
1583note ======== element addresses and array arguments ========
1584global ea_b ulong 1
1585global ea_i ulong 1
1586note elemaddr: array name e (index + 1), index value (kind, number or register, type). A number index must
1587note be inside the array (error 21) and needs no check; a register index: CMP Xi, #len (C6.2.96) or CMP Xi,
1588note Xt (C6.2.97), B.HS trap 3 (unsigned, so a negative signed index traps too). Out: ea_b base register
1589note (the array's register, or a new one holding its address), ea_i index register. om4 ELEMADDR.
1590fn elemaddr e ulong k ulong v ulong t ulong to void
1591if eq t 1
1592fail 7
1593end
1594i = sub e 1
1595ln = get nm_len i
1596x = v
1597if eq k 3
1598if ge v ln
1599fail 21
1600end
1601x = alloc
1602matconst x v
1603else
1604if le ln 4095
1605emit or or 4043309087 shl ln 10 shl v 5
1606else
1607y = alloc
1608matconst y ln
1609emit or or 3942645791 shl y 16 shl v 5
1610end
1611trapc 2 3
1612end
1613put ea_i 0 x
1614if ge get nm_kind i 4
1615b = alloc
1616matconst b get nm_addr i
1617put ea_b 0 b
1618else
1619put ea_b 0 ulong get nm_reg i
1620end
1621end
1622
1623note aa_word: the next word of an array argument must be a name of at most 8 bytes
1624fn aa_word to void
1625tok
1626k = get tk 0
1627if eq k 0
1628fail 4
1629end
1630if ne k 1
1631fail 16
1632end
1633if gt get tl 0 8
1634fail 13
1635end
1636end
1637
1638note arrarg: an array argument for parameter pi of function f (index + 1): [view | edit] NAME. Its type and
1639note length must equal the parameter's; an edit parameter needs a writable array (local or edit, error 20;
1640note a view passed to it is error 20, OMEGESE.md 9.4); a global is never passed (error 19). Out: rk 5
1641note (register rv) or 6 (address rv, a text), rt the name entry. om4 ARRARG.
1642fn arrarg f ulong pi ulong to void
1643md = ulong 0
1644aa_word
1645w = get tw 0
1646c = kw w
1647if ne c 0
1648if or lt c 57 gt c 58
1649fail 16
1650end
1651md = c
1652aa_word
1653w = get tw 0
1654end
1655e = findname w
1656if eq e 0
1657fail 3
1658end
1659i = sub e 1
1660k = ulong get nm_kind i
1661if eq k 0
1662fail 7
1663end
1664if eq k 4
1665fail 19
1666end
1667p = add mul sub f 1 6 pi
1668if eq get fn_pk p 3
1669if eq md 57
1670fail 20
1671end
1672if and ne k 1 ne k 3
1673fail 20
1674end
1675else
1676if eq md 58
1677fail 7
1678end
1679end
1680if ne get nm_type i get fn_pt p
1681fail 7
1682end
1683if ne get nm_len i get fn_pl p
1684fail 7
1685end
1686put rt 0 e
1687if eq k 5
1688put rk 0 6
1689put rv 0 get nm_addr i
1690else
1691put rk 0 5
1692put rv 0 ulong get nm_reg i
1693end
1694end
1695
1696note ======== the parts of the value reader that read no further value ========
1697fn vdec to void
1698put depth 0 sub get depth 0 1
1699end
1700
1701note vdone: a value's result, and one nesting level less (om4 VT_RET)
1702fn vdone k ulong v ulong t ulong to void
1703put rk 0 k
1704put rv 0 v
1705put rt 0 t
1706vdec
1707end
1708
1709note vt_un: neg / not of the value in rk/rv/rt (a bare number already refused). neg: NEG Xd, Xm (SUB from
1710note XZR, C6.2.458; the 32-bit form for u32), i64 NEGS + B.VS trap 1 (C6.2.465), then the wrap or the
1711note sign check; not: bool EOR Xd, Xn, #1 (C6.2.155), else MVN (ORN from XZR, C6.2.300), then the wrap.
1712note om4 VU1 .. VU_E.
1713fn vt_un c ulong dst ulong to void
1714ty = get rt 0
1715ra = matreg get rk 0 get rv 0 ty
1716rd = destreg dst
1717if eq c 50
1718if eq ty 1
1719e3 3527409664 rd ra 0
1720else
1721if eq ty 4
1722e3 706741216 rd 0 ra
1723else
1724e3 2854224864 rd 0 ra
1725if lt ty 6
1726canon rd ty
1727end
1728end
1729end
1730else
1731if eq ty 1
1732fail 7
1733end
1734if eq ty 9
1735e3 3942646752 rd 0 ra
1736trapc 6 1
1737else
1738if eq ty 4
1739e3 1258292192 rd 0 ra
1740else
1741e3 3405775840 rd 0 ra
1742if lt ty 6
1743canon rd ty
1744else
1745chkext rd ty 1
1746end
1747end
1748end
1749end
1750put rk 0 1
1751put rv 0 rd
1752put rt 0 ty
1753end
1754
1755note vt_conv: the value in rk/rv/rt converted to type tt (neither bool, error 9). Out of range when any bit
1756note at or above s is set: LSR Xt, Xa, #s (UBFM, C6.2.487); CBNZ Xt trap 5 (C6.2.77); signed to a narrower
1757note signed type: the sign-extension check, trap 5. om4 VTT1 .. VC_OK.
1758fn vt_conv tt ulong to void
1759ft = get rt 0
1760ra = matreg get rk 0 get rv 0 ft
1761if eq ft 1
1762fail 9
1763end
1764if eq tt 1
1765fail 9
1766end
1767wf = twid ft
1768wt = twid tt
1769s = ulong 99
1770if lt ft 6
1771if ge tt 6
1772if ge wf wt
1773s = sub wt 1
1774end
1775else
1776if gt wf wt
1777s = wt
1778end
1779end
1780else
1781if lt tt 6
1782s = wt
1783if gt s 63
1784s = ulong 63
1785end
1786else
1787if gt wf wt
1788chkext ra tt 5
1789end
1790end
1791end
1792if ne s 99
1793t = alloc
1794emit or or or 3544251392 t shl s 16 shl ra 5
1795trapsite or 3036676096 t 5
1796end
1797put rk 0 1
1798put rv 0 ra
1799put rt 0 tt
1800end
1801
1802note vt_get: get A i with the index value in rk/rv/rt: the element address, then the load of the element
1803note type, register offset (LDOP table). om4 VT_GET.
1804fn vt_get e ulong dst ulong to void
1805elemaddr e get rk 0 get rv 0 get rt 0
1806rd = destreg dst
1807ty = ulong get nm_type sub e 1
1808w = or ldop ty rd
1809emit or or w shl get ea_b 0 5 shl get ea_i 0 16
1810put rk 0 1
1811put rv 0 rd
1812put rt 0 ty
1813end
1814
1815note vs_chk: read / write bounds: n <= len A and off <= len A - n, else trap 3 (CMP C6.2.97, B.HI C6.2.34,
1816note SUB C6.2.458). om4 VT_SYS.
1817fn vs_chk e ulong ro ulong rn ulong to void
1818t = alloc
1819matconst t get nm_len sub e 1
1820emit or or 3942645791 shl rn 5 shl t 16
1821trapc 8 3
1822e3 3405774848 t t rn
1823emit or or 3942645791 shl ro 5 shl t 16
1824trapc 8 3
1825end
1826
1827note vs_call: save the registers in use, X0 = fd, X1 = A + off (ADD through X16, C6.2.6), X2 = n, X8 = 63
1828note (read) or 64 (write) (MOVZ C6.2.285), SVC #0 (C6.2.466). om4 VT_SYS.
1829fn vs_call c ulong fk ulong fv ulong e ulong ro ulong rn ulong to void
1830save
1831loadarg 0 fk fv
1832basearg 1 e
1833loadarg 16 1 ro
1834emit 2333081633
1835loadarg 2 1 rn
1836emit or 3531603976 shl add c 10 5
1837emit 3556769793
1838end
1839
1840note ca_scal: a scalar argument just read (rk/rv/rt) for parameter p: flags into a register; its type
1841note must be the parameter's; stored at fr[q..q+2]. om4 CA1 .. CA_PUT.
1842fn ca_scal p ulong q ulong to void
1843if eq get rk 0 4
1844put rv 0 matreg 4 get rv 0 get rt 0
1845put rk 0 1
1846put rt 0 1
1847end
1848if ne get rt 0 ulong get fn_pt p
1849fail 7
1850end
1851put fr q get rk 0
1852put fr add q 1 get rv 0
1853put fr add q 2 0
1854end
1855
1856note ca_arr: an array argument i of function f, call frame cb: one array passed twice is error 19.
1857note om4 CA_ARR .. CA_PUT.
1858fn ca_arr f ulong i ulong cb ulong to void
1859arrarg f i
1860j = ulong 0
1861while lt j i
1862if eq get fr add add cb mul j 3 2 get rt 0
1863fail 19
1864end
1865j = add j 1
1866end
1867q = add cb mul i 3
1868put fr q get rk 0
1869put fr add q 1 get rv 0
1870put fr add q 2 get rt 0
1871end
1872
1873note ca_end: all arguments read: save, the arguments into X0.., BL, restore. Frame cb: 3 per argument,
1874note then cb+18 the parameter count, cb+19 the pool slot before the arguments. om4 CA_DONE .. CA5.
1875fn ca_end f ulong dst ulong cb ulong to void
1876save
1877np = get fr add cb 18
1878i = ulong 0
1879while lt i np
1880q = add cb mul i 3
1881loadarg i get fr q get fr add q 1
1882i = add i 1
1883end
1884ca_bl f
1885hr = ulong 1
1886if eq get fn_ret sub f 1 10
1887hr = ulong 0
1888end
1889n = get svn 0
1890a = get sva 0
1891sl = get fr add cb 19
1892r = restore dst sl n a hr
1893put rk 0 1
1894put rv 0 r
1895put rt 0 ulong get fn_ret sub f 1
1896end
1897
1898note vs_fd, vs_arr, vs_off, vs_end: the steps of read / write between the values it reads, with the fd
1899note (kind, value), the offset register and the pool slot before kept in fr[x+8..x+11] (om4's VT_SYS
1900note frame slots 4, 5, 7 and 14)
1901fn vs_fd x ulong to void
1902if ne get rt 0 8
1903fail 7
1904end
1905put fr add x 9 get rk 0
1906put fr add x 10 get rv 0
1907end
1908
1909fn vs_arr c ulong e ulong to void
1910if eq c 53
1911k = get nm_kind sub e 1
1912if or eq k 2 eq k 5
1913fail 20
1914end
1915end
1916end
1917
1918fn vs_off x ulong to void
1919if ne get rt 0 5
1920fail 7
1921end
1922put fr add x 11 matreg get rk 0 get rv 0 get rt 0
1923end
1924
1925fn vs_end c ulong e ulong dst ulong x ulong to void
1926if ne get rt 0 5
1927fail 7
1928end
1929rn = matreg get rk 0 get rv 0 get rt 0
1930ro = get fr add x 11
1931vs_chk e ro rn
1932fk = get fr add x 9
1933fv = get fr add x 10
1934vs_call c fk fv e ro rn
1935sl = get fr add x 8
1936n = get svn 0
1937a = get sva 0
1938r = restore dst sl n a 1
1939vdone 1 r 9
1940end
1941
1942note Native Linux ARM64 host operations. Encoding forms are those in seed/rung2/table.s2.
1943note Syscall numbers and layouts: Linux asm-generic/unistd.h, fcntl.h and time.h.
1944note No raw address or arbitrary syscall is exposed to an Omega program.
1945fn hargc c ulong to ulong
1946if or or eq c 107 eq c 109 eq c 129
1947ret 0
1948end
1949if or eq c 100 eq c 101
1950ret 1
1951end
1952if or or or eq c 102 eq c 105 eq c 106 eq c 127
1953ret 2
1954end
1955ret 3
1956end
1957fn hsysno c ulong to ulong
1958if eq c 127
1959ret 129
1960end
1961if eq c 129
1962ret 172
1963end
1964if eq c 100
1965ret 57
1966end
1967if eq c 101
1968ret 82
1969end
1970if eq c 102
1971ret 52
1972end
1973if eq c 103
1974ret 62
1975end
1976if eq c 104
1977ret 198
1978end
1979if eq c 105
1980ret 201
1981end
1982if eq c 106
1983ret 242
1984end
1985if eq c 107
1986ret 220
1987end
1988ret 24
1989end
1990fn harg x ulong i ulong ty ulong to void
1991rdval 0 ty 255 0
1992if ne get rt 0 ty
1993fail 7
1994end
1995q = add add x 8 mul i 2
1996put fr q get rk 0
1997put fr add q 1 get rv 0
1998end
1999fn hload x ulong i ulong reg ulong to void
2000q = add add x 8 mul i 2
2001loadarg reg get fr q get fr add q 1
2002end
2003fn hsys n ulong to void
2004matconst 8 n
2005emit 3556769793
2006end
2007fn hreturn dst ulong x ulong to void
2008sl = get fr add x 7
2009n = get svn 0
2010a = get sva 0
2011r = restore dst sl n a 1
2012vdone 1 r 9
2013end
2014fn hscalar c ulong dst ulong x ulong to void
2015np = hargc c
2016for i ulong np
2017ty = ulong 8
2018if and eq c 103 eq i 1
2019ty = ulong 9
2020end
2021harg x i ty
2022end
2023save
2024for i ulong np
2025hload x i i
2026end
2027if eq c 106
2028hload x 1 3
2029matconst 1 0
2030matconst 2 0
2031end
2032if eq c 107
2033matconst 0 17
2034for i ulong 5
2035matconst add i 1 0
2036end
2037end
2038hsys hsysno c
2039hreturn dst x
2040end
2041fn hclock c ulong dst ulong x ulong to void
2042save
2043spsub 16
2044stkchk
2045matconst 0 1
2046if eq c 130
2047matconst 0 0
2048end
2049emit 2432697313
2050hsys 113
2051emit 4043309087
2052bad = get oc 0
2053emit 1409286155
2054emit 4181722081
2055emit 4181723106
2056note Reject overflow before seconds are multiplied; clock result is a signed nanosecond count.
2057matconst 3 9223372036
2058e3 3942645760 31 1 3
2059trapc 8 1
2060matconst 3 1000000000
2061e3 2600500224 1 1 3
2062e3 2868903936 0 1 2
2063trapc 6 1
2064patch19 bad get oc 0
2065spadd 16
2066hreturn dst x
2067end
2068fn harray x ulong writable ulong to ulong
2069e = arrname
2070if ne get nm_type sub e 1 2
2071fail 7
2072end
2073if ne writable 0
2074vs_arr 53 e
2075end
2076put fr add x 20 e
2077harg x 0 5
2078harg x 1 5
2079ro = matreg get fr add x 8 get fr add x 9 5
2080rn = matreg get fr add x 10 get fr add x 11 5
2081put fr add x 21 ro
2082put fr add x 22 rn
2083vs_chk e ro rn
2084ret e
2085end
2086note Copy a checked path to a private 4096-byte stack buffer. Reject embedded zero bytes.
2087note Public paths remain length-delimited; only the kernel-facing private copy has a terminator.
2088fn hpathcp x ulong to void
2089e = get fr add x 20
2090save
2091basearg 4 e
2092loadarg 16 1 get fr add x 21
2093e3 2332033024 4 4 16
2094loadarg 5 1 get fr add x 22
2095matconst 6 4095
2096e3 3942645760 31 5 6
2097trapc 8 3
2098end
2099fn hcopy to void
2100spsub 4096
2101stkchk
2102emit 2432697313
2103head = get oc 0
2104emit 4043309247
2105done = get oc 0
2106emit 1409286144
2107emit 960495750
2108emit 4043309279
2109trapc 0 3
2110emit 956301350
2111emit 2432697476
2112emit 2432697377
2113emit 3506439333
2114back head
2115patch19 done get oc 0
2116emit 956301375
2117emit 2432697313
2118matconst 0 18446744073709551516
2119end
2120fn hpath c ulong dst ulong x ulong to void
2121e = harray x 0
2122if eq c 110
2123harg x 2 8
2124harg x 3 8
2125end
2126if eq c 112
2127harg x 2 8
2128end
2129hpathcp x
2130if eq c 110
2131hload x 2 2
2132hload x 3 3
2133end
2134if eq c 111
2135matconst 2 0
2136end
2137if eq c 126
2138matconst 2 512
2139end
2140if eq c 112
2141hload x 2 2
2142end
2143hcopy
2144nr = ulong 56
2145if or eq c 111 eq c 126
2146nr = ulong 35
2147end
2148if eq c 112
2149nr = ulong 34
2150end
2151hsys nr
2152spadd 4096
2153hreturn dst x
2154end
2155fn hbuffer c ulong dst ulong x ulong to void
2156harg x 4 8
2157wr = ulong 0
2158if or or or or eq c 113 eq c 116 eq c 117 eq c 118 eq c 131
2159wr = ulong 1
2160end
2161e = harray x wr
2162if or or eq c 116 eq c 118 eq c 119
2163harg x 2 8
2164end
2165if or eq c 116 eq c 117
2166need = ulong 4
2167if eq c 117
2168need = ulong 128
2169end
2170tmp = alloc
2171matconst tmp need
2172e3 3942645760 31 get fr add x 22 tmp
2173trapc 3 3
2174end
2175save
2176hload x 4 0
2177basearg 1 e
2178loadarg 16 1 get fr add x 21
2179e3 2332033024 1 1 16
2180loadarg 2 1 get fr add x 22
2181nr = ulong 61
2182if eq c 114
2183nr = ulong 203
2184end
2185if eq c 115
2186nr = ulong 200
2187end
2188if eq c 116
2189nr = ulong 260
2190hload x 2 2
2191matconst 3 0
2192end
2193if eq c 117
2194nr = ulong 80
2195end
2196if or eq c 118 eq c 119
2197nr = ulong 207
2198hload x 2 3
2199matconst 4 0
2200matconst 5 0
2201if eq c 119
2202nr = ulong 206
2203matconst 16 16384
2204e3 2852126720 3 3 16
2205end
2206end
2207
2208if eq c 131
2209note getsockname uses a private 32-bit socklen. Success returns the full address length.
2210spsub 16
2211stkchk
2212emit 3103785954
2213emit 2432697314
2214hsys 204
2215emit 4043309087
2216failed = get oc 0
2217emit 1409286155
2218emit 3107980256
2219patch19 failed get oc 0
2220spadd 16
2221else
2222hsys nr
2223end
2224hreturn dst x
2225end
2226fn hsleep dst ulong x ulong to void
2227harg x 0 9
2228save
2229hload x 0 2
2230emit 4043309151
2231trapc 11 3
2232spsub 16
2233stkchk
2234matconst 3 1000000000
2235e3 2596276224 4 2 3
2236note MSUB x5,x4,x3,x2 gives the subsecond remainder.
2237emit 2600700037
2238emit 4177527780
2239emit 4177528805
2240emit 2432697312
2241matconst 1 0
2242hsys 101
2243spadd 16
2244hreturn dst x
2245end
2246fn hargs c ulong dst ulong x ulong to void
2247if eq c 121
2248save
2249emit 4181721952
2250hreturn dst x
2251ret
2252end
2253harg x 4 5
2254e = harray x 1
2255save
2256hload x 4 0
2257emit 4181721953
2258e3 3942645760 31 0 1
2259trapc 2 3
2260emit 2432697344
2261matconst 16 8
2262e3 2600500224 0 0 16
2263e3 2332033024 3 27 0
2264emit 4181721187
2265e3 2852126720 4 31 3
2266matconst 5 0
2267scan = get oc 0
2268emit 960495750
2269emit 4043309279
2270done = get oc 0
2271emit 1409286144
2272emit 2432697476
2273emit 2432697509
2274matconst 16 1048576
2275e3 3942645760 31 5 16
2276trapc 2 3
2277back scan
2278patch19 done get oc 0
2279loadarg 2 1 get fr add x 22
2280e3 3942645760 31 5 2
2281small = get oc 0
2282emit 1409286152
2283basearg 0 e
2284loadarg 16 1 get fr add x 21
2285e3 2332033024 0 0 16
2286e3 2852126720 2 31 5
2287copy = get oc 0
2288emit 4043309247
2289endcp = get oc 0
2290emit 1409286144
2291emit 960495718
2292emit 956301318
2293emit 2432697443
2294emit 2432697344
2295emit 3506439333
2296back copy
2297patch19 endcp get oc 0
2298e3 2852126720 0 31 2
2299endbr = get oc 0
2300emit 335544320
2301patch19 small get oc 0
2302matconst 0 18446744073709551511
2303patch26 endbr get oc 0
2304hreturn dst x
2305end
2306
2307note waitio(fd int, events int, timeout_ns long): revents, 0 timeout, or negative errno.
2308note ppoll uses one private pollfd and one private timespec. Negative timeout is infinite.
2309fn hwaitio dst ulong x ulong to void
2310harg x 0 8
2311harg x 1 8
2312harg x 2 9
2313save
2314hload x 0 0
2315hload x 1 1
2316hload x 2 2
2317note pollfd.events is 16 bits; refuse truncation of the public int argument.
2318emit 4043309119
2319trapc 11 3
2320matconst 16 65535
2321e3 3942645760 31 1 16
2322trapc 8 3
2323spsub 32
2324stkchk
2325emit 4177527807
2326emit 3103785952
2327emit 2030046177
2328matconst 3 1000000000
2329emit 4043309151
2330forever = get oc 0
2331emit 1409286155
2332e3 2596276224 4 2 3
2333emit 2600700037
2334emit 4177528804
2335emit 4177529829
2336emit 2432705506
2337jump = get oc 0
2338emit 335544320
2339patch19 forever get oc 0
2340matconst 2 0
2341patch26 jump get oc 0
2342emit 2432697312
2343matconst 1 1
2344matconst 3 0
2345matconst 4 8
2346hsys 73
2347emit 4043309087
2348failed = get oc 0
2349emit 1409286157
2350emit 2034241504
2351patch19 failed get oc 0
2352spadd 32
2353hreturn dst x
2354end
2355
2356note A packed argument list is u16 count followed by count (u16 length, bytes) records.
2357note Byte order is little-endian. The packet is at most 4096 bytes and 64 records.
2358note The decoder constructs kernel pointers only in private stack memory.
2359fn hpack c ulong dst ulong x ulong to void
2360e = harray x 0
2361if eq c 125
2362harg x 2 8
2363end
2364save
2365basearg 4 e
2366loadarg 16 1 get fr add x 21
2367e3 2332033024 4 4 16
2368loadarg 5 1 get fr add x 22
2369matconst 16 4096
2370e3 3942645760 31 5 16
2371trapc 8 3
2372emit 4043311295
2373trapc 3 3
2374if eq c 125
2375hload x 2 14
2376end
2377spsub 8192
2378stkchk
2379emit 2034237574
2380emit 2432698500
2381emit 3506440357
2382emit 4043309279
2383trapc 0 3
2384matconst 16 64
2385e3 3942645760 31 6 16
2386trapc 8 3
2387if eq c 125
2388emit 4043311327
2389trapc 1 3
2390end
2391matconst 9 0
2392emit 2432697322
2393emit 2436892651
2394outer = get oc 0
2395e3 3942645760 31 9 6
2396done = get oc 0
2397emit 1409286146
2398emit 4043311295
2399trapc 3 3
2400emit 2034237580
2401emit 2432698500
2402emit 3506440357
2403e3 3942645760 31 12 5
2404trapc 8 3
2405note A program path and both rename paths must be nonempty.
2406if eq c 124
2407emit 4043309375
2408notfirst = get oc 0
2409emit 1409286145
2410emit 4043309471
2411trapc 0 3
2412patch19 notfirst get oc 0
2413else
2414emit 4043309471
2415trapc 0 3
2416end
2417emit 4177527146
2418emit 2432704875
2419inner = get oc 0
2420emit 4043309471
2421endstr = get oc 0
2422emit 1409286144
2423emit 960495757
2424emit 4043309503
2425trapc 0 3
2426emit 956301645
2427emit 2432697476
2428emit 2432697674
2429emit 3506439333
2430emit 3506439564
2431back inner
2432patch19 endstr get oc 0
2433emit 956301663
2434emit 2432697674
2435emit 2432697641
2436back outer
2437patch19 done get oc 0
2438emit 4043309247
2439trapc 1 3
2440emit 4177527167
2441if eq c 124
2442emit 2432697312
2443emit 2436892641
2444e3 2852126720 2 31 11
2445hsys 221
2446else
2447emit 2436892656
2448emit 4181721601
2449emit 4181722627
2450matconst 0 18446744073709551516
2451matconst 2 18446744073709551516
2452e3 2852126720 4 31 14
2453hsys 276
2454end
2455spadd 8192
2456hreturn dst x
2457end
2458fn hpipe dst ulong x ulong to void
2459e = harray x 1
2460harg x 2 8
2461tmp = alloc
2462matconst tmp 8
2463e3 3942645760 31 get fr add x 22 tmp
2464trapc 3 3
2465save
2466basearg 0 e
2467loadarg 16 1 get fr add x 21
2468e3 2332033024 0 0 16
2469hload x 2 1
2470hsys 59
2471hreturn dst x
2472end
2473
2474fn hcwd dst ulong x ulong to void
2475e = harray x 1
2476save
2477basearg 0 e
2478loadarg 16 1 get fr add x 21
2479e3 2332033024 0 0 16
2480loadarg 1 1 get fr add x 22
2481hsys 17
2482hreturn dst x
2483end
2484fn hreadlnk dst ulong x ulong to void
2485e = harray x 0
2486put fr add x 23 e
2487put fr add x 24 get fr add x 21
2488put fr add x 25 get fr add x 22
2489e = harray x 1
2490put fr add x 26 e
2491put fr add x 27 get fr add x 21
2492put fr add x 28 get fr add x 22
2493put fr add x 20 get fr add x 23
2494put fr add x 21 get fr add x 24
2495put fr add x 22 get fr add x 25
2496hpathcp x
2497note readlnk refuses an empty path before the kernel call.
2498emit 4043309247
2499trapc 0 3
2500basearg 2 get fr add x 26
2501loadarg 16 1 get fr add x 27
2502e3 2332033024 2 2 16
2503loadarg 3 1 get fr add x 28
2504hcopy
2505hsys 78
2506spadd 4096
2507hreturn dst x
2508end
2509fn hsockopt dst ulong x ulong to void
2510harg x 2 8
2511harg x 3 8
2512harg x 4 8
2513e = harray x 0
2514save
2515hload x 2 0
2516hload x 3 1
2517hload x 4 2
2518basearg 3 e
2519loadarg 16 1 get fr add x 21
2520e3 2332033024 3 3 16
2521loadarg 4 1 get fr add x 22
2522hsys 208
2523hreturn dst x
2524end
2525
2526fn hostval c ulong dst ulong to void
2527if eq get progmode 0 0
2528fail 9
2529end
2530x = mul get depth 0 32
2531put fr add x 7 get nslot 0
2532if eq c 132
2533hcwd dst x
2534ret
2535end
2536if eq c 133
2537hreadlnk dst x
2538ret
2539end
2540if eq c 134
2541hsockopt dst x
2542ret
2543end
2544if or eq c 109 eq c 130
2545hclock c dst x
2546ret
2547end
2548if or and ge c 113 le c 119 eq c 131
2549hbuffer c dst x
2550ret
2551end
2552if eq c 120
2553hsleep dst x
2554ret
2555end
2556if eq c 123
2557hwaitio dst x
2558ret
2559end
2560if or eq c 124 eq c 125
2561hpack c dst x
2562ret
2563end
2564if eq c 126
2565hpath c dst x
2566ret
2567end
2568if or eq c 127 eq c 129
2569hscalar c dst x
2570ret
2571end
2572if eq c 128
2573hpipe dst x
2574ret
2575end
2576if ge c 121
2577put hostargs 0 1
2578hargs c dst x
2579ret
2580end
2581if ge c 110
2582hpath c dst x
2583ret
2584end
2585hscalar c dst x
2586end
2587
2588note ======== rdval: read one value (om4 VAL, VALTOK, CALL and the VT_ parts, one self-recursive
2589note function because a function calls only earlier functions or itself) ========
2590note mode 0: read the first word; 1: the first word is the current token; 2: a call of function fe (index
2591note + 1), its arguments still to read. exp the expected type (0 none), dst the destination register (255
2592note none). Out: rk kind (1 register, 2 bare number, 3 typed number, 4 flags), rv register / number /
2593note condition, rt type (for a bare number: 1 when written as 2^63 or more). Only the outermost op writes the
2594note destination. Every value nests one level deeper (at most 1024, error 14); a call does not.
2595fn rdval mode ulong exp ulong dst ulong fe ulong to void
2596if eq mode 2
2597cb = add mul get depth 0 32 8
2598put fr add cb 18 ulong get fn_np sub fe 1
2599put fr add cb 19 get nslot 0
2600if eq get progmode 0 0
2601if eq get curfn 0 fe
2602fail 9
2603end
2604end
2605i = ulong 0
2606while lt i get fr add cb 18
2607p = add mul sub fe 1 6 i
2608if eq get fn_pk p 0
2609rdval 0 ulong get fn_pt p 255 0
2610ca_scal p add cb mul i 3
2611else
2612ca_arr fe i cb
2613end
2614i = add i 1
2615end
2616ca_end fe dst cb
2617ret
2618end
2619if eq mode 0
2620tok
2621end
2622put depth 0 add get depth 0 1
2623if gt get depth 0 1024
2624fail 14
2625end
2626if or eq get tk 0 0 eq get tk 0 3
2627fail 4
2628end
2629if eq get tk 0 4
2630fail 16
2631end
2632if eq get tk 0 2
2633if eq exp 0
2634vdone 2 get tw 0 get tbig 0
2635ret
2636end
2637range get tw 0 exp get tbig 0
2638vdone 3 get tw 0 exp
2639ret
2640end
2641if gt get tl 0 8
2642f = findfn get tw 0 get tl 0 get ts 0
2643if eq f 0
2644fail 13
2645end
2646if eq get fn_ret sub f 1 10
2647fail 7
2648end
2649rdval 2 0 dst f
2650vdec
2651ret
2652end
2653c = kw get tw 0
2654if eq c 0
2655e = findname get tw 0
2656if ne e 0
2657if ne get nm_kind sub e 1 0
2658fail 7
2659end
2660vdone 1 ulong get nm_reg sub e 1 ulong get nm_type sub e 1
2661ret
2662end
2663f = findfn get tw 0 0 0
2664if eq f 0
2665fail 3
2666end
2667if eq get fn_ret sub f 1 10
2668fail 7
2669end
2670rdval 2 0 dst f
2671vdec
2672ret
2673end
2674if le sub c 1 8
2675tok
2676if eq get tk 0 2
2677range get tw 0 c get tbig 0
2678vdone 3 get tw 0 c
2679ret
2680end
2681rdval 1 0 255 0
2682if eq get rk 0 2
2683fail 11
2684end
2685vt_conv c
2686vdec
2687ret
2688end
2689if eq c 74
2690rdval 0 exp dst 0
2691tok
2692if ne get tk 0 1
2693fail 16
2694end
2695if gt get tl 0 8
2696fail 16
2697end
2698if ne kw get tw 0 75
2699fail 16
2700end
2701vdec
2702ret
2703end
2704if eq c 77
2705fail 9
2706end
2707if and ge c 100 le c 134
2708hostval c dst
2709ret
2710end
2711if eq c 51
2712e = arrname
2713rdval 0 5 255 0
2714vt_get e dst
2715vdec
2716ret
2717end
2718if eq c 52
2719e = arrname
2720vdone 3 get nm_len sub e 1 5
2721ret
2722end
2723if or eq c 53 eq c 54
2724x = mul get depth 0 32
2725put fr add x 8 get nslot 0
2726rdval 0 8 255 0
2727vs_fd x
2728e = arrname
2729vs_arr c e
2730rdval 0 5 255 0
2731vs_off x
2732rdval 0 5 255 0
2733vs_end c e dst x
2734ret
2735end
2736if eq c 55
2737vdone 3 1 1
2738ret
2739end
2740if eq c 56
2741vdone 3 0 1
2742ret
2743end
2744if gt sub c 33 17
2745fail 16
2746end
2747if ge c 49
2748rdval 0 exp 255 0
2749if and eq c 50 eq get rk 0 4
2750put rv 0 xor get rv 0 1
2751vdec
2752ret
2753end
2754if eq get rk 0 2
2755fail 11
2756end
2757vt_un c dst
2758vdec
2759ret
2760end
2761rdval 0 0 255 0
2762ak = get rk 0
2763av = get rv 0
2764at = get rt 0
2765if eq ak 4
2766av = matreg 4 av at
2767ak = ulong 1
2768at = ulong 1
2769end
2770rdval 0 0 255 0
2771bk = get rk 0
2772bv = get rv 0
2773bt = get rt 0
2774if eq bk 4
2775bv = matreg 4 bv bt
2776bk = ulong 1
2777bt = ulong 1
2778end
2779if eq ak 2
2780if eq bk 2
2781fail 11
2782end
2783range av bt at
2784at = bt
2785ak = ulong 3
2786end
2787if eq bk 2
2788range bv at bt
2789bt = at
2790bk = ulong 3
2791end
2792if and ne c 41 ne c 42
2793if ne at bt
2794fail 7
2795end
2796end
2797x = mul get depth 0 32
2798put fr x ak
2799put fr add x 1 av
2800put fr add x 2 at
2801put fr add x 3 bk
2802put fr add x 4 bv
2803put fr add x 5 bt
2804binop c dst x
2805vdec
2806end
2807
2808note cond: read a condition (a bool value); emit a branch taken when it is false: B.cond with the opposite
2809note condition (C6.2.34), or CBZ Xr (C6.2.78). Returns that branch's place. om4 COND.
2810fn cond to ulong
2811rdval 0 1 255 0
2812if ne get rt 0 1
2813fail 7
2814end
2815if eq get rk 0 4
2816at = get oc 0
2817emit or 1409286144 xor get rv 0 1
2818ret at
2819end
2820r = matreg get rk 0 get rv 0 get rt 0
2821at = get oc 0
2822emit or 3019898880 r
2823ret at
2824end
2825
2826note ======== statements, one per line (om4 STMT and the S_ parts) ========
2827note skipnl: note ...: the rest of the line is not read (up to the newline, which tok reads). om4 S_NOTE.
2828fn skipnl to void
2829go = true
2830while go
2831n = ulong 0
2832while and go lt n 999999
2833if ge get cur 0 get inlen 0
2834go = false
2835else
2836if eq get inp get cur 0 10
2837go = false
2838else
2839put cur 0 add get cur 0 1
2840end
2841end
2842n = add n 1
2843end
2844end
2845end
2846
2847note s_long: a word over 8 characters at the start of a line: only a call of a void function. om4 S_LONG.
2848fn s_long to void
2849if eq get curret 0 0
2850fail 13
2851end
2852if ne get dead 0 0
2853fail 17
2854end
2855f = findfn get tw 0 get tl 0 get ts 0
2856if eq f 0
2857fail 13
2858end
2859if ne get fn_ret sub f 1 10
2860fail 7
2861end
2862rdval 2 0 255 f
2863eol
2864end
2865
2866note blo: B.LO back to an earlier place (C6.2.34)
2867fn blo at ulong to void
2868d = and shr sub at get oc 0 2 524287
2869emit or 1409286147 shl d 5
2870end
2871
2872note sa_arr: x = new T N v: N elements of type T on the stack, each set to v; only in the function's
2873note outermost block, so the stack held by local arrays is known at every ret. SUB SP (C6.2.457); program
2874note mode: the stack check; MOV Xb, SP (ADD immediate, C6.2.5); then the fill loop: STR v, [Xb, Xi, LSL];
2875note ADD Xi, Xi, #1; CMP Xi, #N or Xn; B.LO back (C6.2.34). om4 SA_ARR.
2876fn sa_arr w ulong to void
2877if ne get nb 0 0
2878fail 9
2879end
2880t = typeword
2881elemtype t
2882tok
2883if ne get tk 0 2
2884fail 16
2885end
2886n = get tw 0
2887arrlen n
2888by = shl shr add shl n tlog t 15 4 4
2889s = add get stkbytes 0 by
2890if gt s 4194304
2891fail 23
2892end
2893put stkbytes 0 s
2894r = alloc
2895put held 0 get nslot 0
2896rdval 0 t 255 0
2897if ne get rt 0 t
2898fail 7
2899end
2900v = matreg get rk 0 get rv 0 get rt 0
2901spsub by
2902stkchk
2903emit or 2432697312 r
2904i = alloc
2905emit or 3531603968 i
2906m = ulong 0
2907if gt n 4095
2908m = alloc
2909matconst m n
2910end
2911lp = get oc 0
2912emit or or or stop t v shl r 5 shl i 16
2913emit or or 2432697344 i shl i 5
2914if le n 4095
2915emit or or 4043309087 shl n 10 shl i 5
2916else
2917emit or or 3942645791 shl m 16 shl i 5
2918end
2919blo lp
2920addname w r t n 1 0
2921end
2922
2923note s_assign: name [=] value (an existing scalar, or a new one), or name = new T N v. om4 S_ASSIGN.
2924fn s_assign w ulong to void
2925tok
2926if eq get tk 0 1
2927if le get tl 0 8
2928if eq kw get tw 0 73
2929tok
2930end
2931end
2932end
2933e = findname w
2934if ne e 0
2935i = sub e 1
2936if ne get nm_kind i 0
2937fail 7
2938end
2939r = ulong get nm_reg i
2940t = ulong get nm_type i
2941rdval 1 t r 0
2942if ne get rt 0 t
2943fail 7
2944end
2945force get rk 0 get rv 0 get rt 0 r
2946eol
2947ret
2948end
2949if ne findfn w 0 0 0
2950fail 10
2951end
2952if eq get tk 0 1
2953if le get tl 0 8
2954if eq kw get tw 0 84
2955sa_arr w
2956eol
2957ret
2958end
2959end
2960end
2961r = alloc
2962put held 0 get nslot 0
2963rdval 1 0 r 0
2964if eq get rk 0 2
2965fail 11
2966end
2967t = get rt 0
2968force get rk 0 get rv 0 t r
2969addname w r t 0 0 0
2970eol
2971end
2972
2973note s_name: a name at the start of a line: a call of a void function, or name [=] value. om4 S_NAME.
2974fn s_name w ulong to void
2975if ne findname w 0
2976s_assign w
2977ret
2978end
2979f = findfn w get tl 0 0
2980if eq f 0
2981s_assign w
2982ret
2983end
2984if ne get fn_ret sub f 1 10
2985fail 7
2986end
2987rdval 2 0 255 f
2988eol
2989end
2990
2991note fn_p: the type of parameter np (name pw) of function f (index): T, or view / edit T N (an array:
2992note kind 2 / 3, element type, length 1 .. 2^24). om4 FN_P .. FN_ADD.
2993fn fn_p f ulong np ulong pw ulong to void
2994tok
2995if ne get tk 0 1
2996fail 16
2997end
2998if gt get tl 0 8
2999fail 16
3000end
3001c = kw get tw 0
3002t = ulong 0
3003ln = ulong 0
3004k = ulong 0
3005if or eq c 57 eq c 58
3006k = sub c 55
3007t = typeword
3008elemtype t
3009tok
3010if eq get tk 0 0
3011fail 4
3012end
3013if ne get tk 0 2
3014fail 16
3015end
3016ln = get tw 0
3017arrlen ln
3018else
3019if gt sub c 1 8
3020fail 16
3021end
3022t = c
3023end
3024addname pw poolc np t ln k 0
3025p = add mul f 6 np
3026put fn_pt p uchar t
3027put fn_pl p ln
3028put fn_pk p uchar k
3029end
3030
3031note fn_to: to T | to void at the end of a fn line; the function's state starts. om4 FN_TO .. FN_T2.
3032fn fn_to f ulong np ulong to void
3033tok
3034if eq get tk 0 0
3035fail 4
3036end
3037if ne get tk 0 1
3038fail 16
3039end
3040if gt get tl 0 8
3041fail 16
3042end
3043c = kw get tw 0
3044r = c
3045if eq c 76
3046r = ulong 10
3047else
3048if gt sub c 1 8
3049fail 16
3050end
3051end
3052put curret 0 r
3053put fn_ret f uchar r
3054put fn_np f uchar np
3055put stkbytes 0 0
3056put dead 0 0
3057put npar 0 np
3058put nslot 0 0
3059put held 0 0
3060put nb 0 0
3061put nt 0 0
3062eol
3063end
3064
3065note s_fn: fn name (param type | param view T N | param edit T N)* to type | void. om4 S_FN.
3066fn s_fn to void
3067if ne get curret 0 0
3068fail 15
3069end
3070tok
3071if ne get tk 0 1
3072fail 16
3073end
3074if gt get tl 0 63
3075fail 13
3076end
3077w = get tw 0
3078if ne kw w 0
3079fail 16
3080end
3081l = get tl 0
3082s = get ts 0
3083f = ulong 0
3084if eq get scanning 0 1
3085newname w l s
3086f = get nf 0
3087if ge f 512
3088fail 14
3089end
3090put nf 0 add f 1
3091put curfn 0 add f 1
3092put lastfn 0 add f 1
3093put fn_name f w
3094put fn_addr f get oc 0
3095put fn_ret f 0
3096put fn_np f 0
3097put fn_nmlen f l
3098put fn_off f s
3099else
3100f = sub findfn w l s 1
3101put curfn 0 add f 1
3102put lastfn 0 add f 1
3103put fn_addr f get oc 0
3104end
3105put nn 0 get nglob 0
3106np = ulong 0
3107go = true
3108while go
3109tok
3110if eq get tk 0 0
3111fail 4
3112end
3113if ne get tk 0 1
3114fail 16
3115end
3116if gt get tl 0 8
3117fail 13
3118end
3119pw = get tw 0
3120c = kw pw
3121if eq c 65
3122go = false
3123else
3124if ne c 0
3125fail 16
3126end
3127newname pw get tl 0 get ts 0
3128if ge np 6
3129fail 9
3130end
3131fn_p f np pw
3132np = add np 1
3133end
3134end
3135fn_to f np
3136if eq get scanning 0 1
3137put curret 0 0
3138put nn 0 0
3139end
3140end
3141
3142note s_ret: ret [value]: the value into X0, give back the local arrays' stack, RET. om4 S_RET.
3143fn s_ret to void
3144r = get curret 0
3145if eq r 10
3146eol
3147epi
3148dead1
3149ret
3150end
3151rdval 0 r 0 0
3152if ne get rt 0 r
3153fail 7
3154end
3155force get rk 0 get rv 0 get rt 0 0
3156epi
3157dead1
3158eol
3159end
3160
3161note s_exit: exit value (int): Linux exit (system call 93: MOVZ X8, #93, C6.2.285; SVC #0, C6.2.466); the
3162note path ends. om4 S_EXIT.
3163fn s_exit to void
3164rdval 0 8 255 0
3165if ne get rt 0 8
3166fail 7
3167end
3168force get rk 0 get rv 0 get rt 0 0
3169emit 3531606952
3170emit 3556769793
3171dead1
3172eol
3173end
3174
3175note stel: the checked store of v (type t) at ea_b / ea_i (the STOP table: register offset, scaled)
3176fn stel t ulong v ulong to void
3177b = shl get ea_b 0 5
3178i = shl get ea_i 0 16
3179emit or or or stop t v b i
3180end
3181
3182note s_put: put A i v: index, then value, then the checked store (STOP table). om4 S_PUT.
3183fn s_put to void
3184e = arrname
3185k = get nm_kind sub e 1
3186if or eq k 2 eq k 5
3187fail 20
3188end
3189rdval 0 5 255 0
3190ik = get rk 0
3191iv = get rv 0
3192it = get rt 0
3193t = ulong get nm_type sub e 1
3194rdval 0 t 255 0
3195if ne get rt 0 t
3196fail 7
3197end
3198v = matreg get rk 0 get rv 0 get rt 0
3199elemaddr e ik iv it
3200stel t v
3201eol
3202end
3203
3204note s_global: global NAME T N: a fixed pool of N elements, zero at start, in the read/write part of the
3205note program image after the 8 MiB stack pool (address 0x10800000 + pool bytes so far); at most 1 GiB of
3206note pools (error 23). om4 S_GLOBAL.
3207fn s_global to void
3208w = topname
3209t = typeword
3210elemtype t
3211tok
3212if ne get tk 0 2
3213fail 16
3214end
3215n = get tw 0
3216arrlen n
3217by = shl shr add shl n tlog t 15 4 4
3218pb = get poolb 0
3219a = add 276824064 pb
3220s = add pb by
3221if gt s 1073741824
3222fail 23
3223end
3224put poolb 0 s
3225addname w 0 t n 4 a
3226put nglob 0 get nn 0
3227eol
3228end
3229
3230note s_text: text NAME "...": read-only bytes in the program image (escapes \n \t \\ \", any other is error
3231note 22), padded to a word, with their length; address 0x400000 + its place. om4 S_TEXT.
3232fn s_text to void
3233w = topname
3234tok
3235if ne get tk 0 4
3236fail 16
3237end
3238a = add get oc 0 4194304
3239p = get tw 0
3240e = add p get tl 0
3241n = ulong 0
3242go = true
3243while go
3244m = ulong 0
3245while and go lt m 999999
3246if ge p e
3247go = false
3248else
3249b = get inp p
3250p = add p 1
3251if eq b 92
3252b = get inp p
3253p = add p 1
3254if eq b 110
3255b = uchar 10
3256else
3257if eq b 116
3258b = uchar 9
3259else
3260if and ne b 92 ne b 34
3261fail 22
3262end
3263end
3264end
3265end
3266emitb b
3267n = add n 1
3268end
3269m = add m 1
3270end
3271end
3272while ne and get oc 0 3 0
3273emitb 0
3274end
3275addname w 0 2 n 5 a
3276put nglob 0 get nn 0
3277eol
3278end
3279
3280note s_if: if cond: the false branch is patched at else / end. om4 S_IF.
3281fn s_if to void
3282at = cond
3283pushb at 1 0 0 0
3284pushnh get nn 0 get held 0
3285eol
3286end
3287
3288note s_else: jump over the else part (B, C6.2.35); the if's false branch lands after that jump. om4 S_ELSE.
3289fn s_else to void
3290n = get nb 0
3291if eq n 0
3292fail 15
3293end
3294i = sub n 1
3295if ne get bkind i 1
3296fail 15
3297end
3298b = get oc 0
3299emit 335544320
3300patch19 get bbr i get oc 0
3301put bkind i 2
3302put bbr i b
3303put nn 0 get bnames i
3304put held 0 get bheld i
3305put bdead i get dead 0
3306put dead 0 0
3307eol
3308end
3309
3310note lpcnt: the hidden counter check of a loop: CMP Xc, #N (C6.2.96) or CMP Xc, Xn (C6.2.97); B.HS trap
3311note 4; ADD Xc, Xc, #1 (C6.2.5). om4 SW_NOB .. SW_G and the same in S_FOR.
3312fn lpcnt c ulong n ulong to void
3313if le n 4095
3314emit or or 4043309087 shl n 10 shl c 5
3315else
3316m = alloc
3317matconst m n
3318emit or or 3942645791 shl c 5 shl m 16
3319end
3320trapc 2 4
3321emit or or 2432697344 c shl c 5
3322end
3323
3324note s_while: while cond [bound N]: hidden counter = 0; head: cond false -> exit; counter >= N -> trap 4;
3325note counter = counter + 1; body; B head. N is 1 .. 999999 (error 8), 1000000 with no bound. om4 S_WHILE.
3326fn s_while to void
3327sn = get nn 0
3328sh = get held 0
3329c = alloc
3330put held 0 get nslot 0
3331emit or 3531603968 c
3332hd = get oc 0
3333br = cond
3334n = ulong 1000000
3335tok
3336k = get tk 0
3337if and ne k 0 ne k 3
3338if ne k 1
3339fail 5
3340end
3341if gt get tl 0 8
3342fail 5
3343end
3344if ne kw get tw 0 70
3345fail 5
3346end
3347tok
3348if eq get tk 0 0
3349fail 4
3350end
3351if ne get tk 0 2
3352fail 16
3353end
3354if ge sub get tw 0 1 999999
3355fail 8
3356end
3357n = get tw 0
3358eol
3359end
3360lpcnt c n
3361pushb br 3 hd c 0
3362pushnh sn sh
3363end
3364
3365note sf_br: B.HS (unsigned) or B.GE (signed) to the loop exit, C6.2.34 (distance filled at end); returns
3366note its place. om4 SF_BR.
3367fn sf_br t ulong to ulong
3368at = get oc 0
3369if lt t 6
3370emit 1409286146
3371else
3372emit 1409286154
3373end
3374ret at
3375end
3376
3377note sf_num: for i T K with K a number: i = 0 (MOVZ, C6.2.285); head: CMP Xi, #K or Xk; B.HS/B.GE exit.
3378note om4 SF_NUM .. SF_PUSH.
3379fn sf_num w ulong t ulong kk ulong sn ulong sh ulong to void
3380eol
3381i = alloc
3382put held 0 get nslot 0
3383addname w i t 0 0 0
3384emit or 3531603968 i
3385hd = get oc 0
3386if le kk 4095
3387emit or or 4043309087 shl kk 10 shl i 5
3388else
3389k = alloc
3390matconst k kk
3391emit or or 3942645791 shl i 5 shl k 16
3392end
3393br = sf_br t
3394pushb br 4 hd i t
3395pushnh sn sh
3396end
3397
3398note sf_val: for i T K with K a value of type T in register kr: i = 0; the hidden counter c = 0; head: CMP
3399note Xi, Xk; B.HS/B.GE exit; the counter guard of a while with no bound. om4 SF_VAL.
3400fn sf_val w ulong t ulong kr ulong sn ulong sh ulong to void
3401eol
3402i = alloc
3403put held 0 get nslot 0
3404addname w i t 0 0 0
3405emit or 3531603968 i
3406c = alloc
3407put held 0 get nslot 0
3408emit or 3531603968 c
3409hd = get oc 0
3410emit or or 3942645791 shl kr 16 shl i 5
3411br = sf_br t
3412m = alloc
3413matconst m 1000000
3414emit or or 3942645791 shl c 5 shl m 16
3415trapc 2 4
3416emit or or 2432697344 c shl c 5
3417pushb br 4 hd i t
3418pushnh sn sh
3419end
3420
3421note s_for: for i T K: i = 0; head: not i < K -> exit; body; latch i = add i 1; B head. K a number, or a
3422note value of type T: a name is read at every pass, another value once before the loop. om4 S_FOR.
3423fn s_for to void
3424sn = get nn 0
3425sh = get held 0
3426tok
3427if eq get tk 0 0
3428fail 4
3429end
3430if ne get tk 0 1
3431fail 16
3432end
3433if gt get tl 0 8
3434fail 13
3435end
3436w = get tw 0
3437if ne kw w 0
3438fail 16
3439end
3440newname w get tl 0 get ts 0
3441t = typeword
3442if eq t 1
3443fail 7
3444end
3445tok
3446if eq get tk 0 0
3447fail 4
3448end
3449if eq get tk 0 2
3450range get tw 0 t get tbig 0
3451sf_num w t get tw 0 sn sh
3452ret
3453end
3454rdval 1 t 255 0
3455if ne get rt 0 t
3456fail 7
3457end
3458if eq get rk 0 3
3459sf_num w t get rv 0 sn sh
3460ret
3461end
3462kr = matreg get rk 0 get rv 0 get rt 0
3463sf_val w t kr sn sh
3464end
3465
3466note finish: end of a function: a function that needs a value must not reach its end (error 18), a void
3467note one returns; then the trap stubs: shared tail, function mode MOV X0, X7; LDR X16, [X7, #16]; BR X16
3468note (C6.2.282, C6.2.216, C6.2.46: OscRt.trap); program mode ADD X0, X1, #100; MOVZ X8, #93; SVC #0 (exit
3469note 100 + code). Per trap code used: MOVZ X1, #code (C6.2.285); B tail. om4 FINISH.
3470fn finish to void
3471eol
3472if eq get dead 0 0
3473if ne get curret 0 10
3474fail 18
3475end
3476epi
3477end
3478nt0 = get nt 0
3479if ne nt0 0
3480tail = get oc 0
3481if eq get progmode 0 0
3482emit 2852586464
3483emit 4181723376
3484emit 3592356352
3485else
3486emit 2432798752
3487emit 3531606952
3488emit 3556769793
3489end
3490code = ulong 1
3491while le code 14
3492st = ulong 0
3493j = ulong 0
3494while lt j nt0
3495if eq get tcode j code
3496if eq st 0
3497st = get oc 0
3498emit or 3531603969 shl code 5
3499back tail
3500end
3501patch19 get tsite j st
3502end
3503j = add j 1
3504end
3505code = add code 1
3506end
3507end
3508put curret 0 0
3509put nn 0 get nglob 0
3510put curfn 0 0
3511end
3512
3513note s_end: close the innermost block (if: patch its branch; else: patch its jump; while: B head; for: i =
3514note add i 1, B head), or the function. om4 S_END.
3515fn s_end to void
3516n = get nb 0
3517if eq n 0
3518finish
3519ret
3520end
3521i = sub n 1
3522put nb 0 i
3523k = get bkind i
3524if eq k 2
3525put dead 0 and get dead 0 get bdead i
3526else
3527put dead 0 0
3528end
3529if eq k 4
3530put fr 0 1
3531put fr 1 get breg i
3532put fr 2 get btyp i
3533put fr 3 3
3534put fr 4 1
3535put fr 5 get btyp i
3536binop 33 get breg i 0
3537end
3538if ge k 3
3539back get bhead i
3540end
3541if eq k 2
3542patch26 get bbr i get oc 0
3543else
3544patch19 get bbr i get oc 0
3545end
3546put nn 0 get bnames i
3547put held 0 get bheld i
3548eol
3549end
3550
3551note ======== the end of the input: the output ========
3552note writeout: the output from byte st to the end, to stdout (error 2), then exit 0. om4 WRITEOUT.
3553fn writeout st ulong to void
3554p = st
3555go = true
3556while go
3557m = ulong 0
3558while and go lt m 999999
3559if ge p get oc 0
3560go = false
3561else
3562n = write 1 out p sub get oc 0 p
3563if le n 0
3564fail 2
3565end
3566p = add p ulong n
3567end
3568m = add m 1
3569end
3570end
3571exit int 0
3572end
3573
3574note elfhdr: the 176-byte file header (om4 ELFHDR, 44 words): ELF64 little-endian, executable, AArch64,
3575note entry 0x4000b0 (the word after the header), 2 program headers: code read+execute from 0x400000, and
3576note the read/write pools from 0x10000000. Only the words that are not 0 (the output starts at 0).
3577fn elfhdr to void
3578wrword 0 1179403647
3579wrword 4 65794
3580wrword 16 11993090
3581wrword 20 1
3582wrword 24 4194480
3583wrword 32 64
3584wrword 52 3670080
3585wrword 56 2
3586wrword 64 1
3587wrword 68 5
3588wrword 80 4194304
3589wrword 88 4194304
3590wrword 112 4096
3591wrword 120 1
3592wrword 124 6
3593wrword 136 268435456
3594wrword 144 268435456
3595wrword 168 4096
3596end
3597
3598note eof_prog: program mode: start code = MOVZ X28, #0x1000, LSL 16 (stack pool floor); MOVZ X16, #0x1080,
3599note LSL 16; MOV SP, X16 (ADD immediate, C6.2.5); BL main (C6.2.43); [MOVZ X0, #0 for a void main]; MOVZ X8,
3600note #93; SVC #0. The entry word after the header is a B to it; the header gets the file and pool sizes.
3601note om4 EOF_PROG.
3602fn eof_prog to void
3603f = findfn 1852399981 4 0
3604if eq f 0
3605fail 16
3606end
3607if ne get fn_np sub f 1 0
3608fail 9
3609end
3610wrword 176 or 335544320 and shr sub get oc 0 176 2 67108863
3611if ne get hostargs 0 0
3612note Preserve Linux's initial argument stack privately before installing Omega's bounded stack.
3613emit 2432697339
3614end
3615emit 3533832220
3616emit 3533836304
3617emit 2432696863
3618emit or 2483027968 and shr sub get fn_addr sub f 1 get oc 0 2 67108863
3619if eq get fn_ret sub f 1 10
3620emit 3531603968
3621end
3622emit 3531606952
3623emit 3556769793
3624elfhdr
3625wrword 96 get oc 0
3626wrword 104 get oc 0
3627wrword 160 add get poolb 0 8388608
3628writeout 0
3629end
3630
3631note s_eof: end of input: no open function (error 15), some function (16); function mode: no globals or
3632note texts (9); one function: its bare words; more: the entry word, a B to the last function. om4 S_EOF.
3633note Resolve every call after all addresses are known. In function mode remove leaves of the call
3634note graph; any functions left after nf rounds form or reach a cycle (error 9).
3635fn resolve to void
3636n = get ncalls 0
3637i = ulong 0
3638while lt i n
3639s = get callsite i
3640f = sub get callto i 1
3641wrword s or 2483027968 and shr sub get fn_addr f s 2 67108863
3642i = add i 1
3643end
3644if eq get progmode 0 0
3645pass = ulong 0
3646while lt pass get nf 0
3647f = ulong 0
3648while lt f get nf 0
3649if eq get settled f 0
3650ok = true
3651i = ulong 0
3652while and ok lt i n
3653if eq get callfrom i add f 1
3654if eq get settled sub get callto i 1 0
3655ok = false
3656end
3657end
3658i = add i 1
3659end
3660if ok
3661put settled f 1
3662end
3663end
3664f = add f 1
3665end
3666pass = add pass 1
3667end
3668f = ulong 0
3669while lt f get nf 0
3670if eq get settled f 0
3671fail 9
3672end
3673f = add f 1
3674end
3675end
3676end
3677
3678fn s_eof to void
3679
3680if ne get curret 0 0
3681fail 15
3682end
3683if eq get nf 0 0
3684fail 16
3685end
3686resolve
3687if ne get progmode 0 0
3688eof_prog
3689end
3690if ne get nglob 0 0
3691fail 9
3692end
3693if eq get nf 0 1
3694writeout 180
3695end
3696a = get fn_addr sub get lastfn 0 1
3697wrword 176 or 335544320 and shr sub a 176 2 67108863
3698writeout 176
3699end
3700
3701note stmt: one line. Returns 1 at the end of the input. om4 STMT.
3702fn stmt to ulong
3703put nslot 0 get held 0
3704tok
3705k = get tk 0
3706if eq k 0
3707ret 0
3708end
3709if eq k 3
3710s_eof
3711ret 1
3712end
3713if ne k 1
3714fail 16
3715end
3716if gt get tl 0 8
3717s_long
3718ret 0
3719end
3720w = get tw 0
3721c = kw w
3722if eq c 83
3723skipnl
3724ret 0
3725end
3726if eq c 64
3727s_fn
3728ret 0
3729end
3730if eq get curret 0 0
3731if eq c 78
3732s_global
3733ret 0
3734end
3735if eq c 79
3736s_text
3737ret 0
3738end
3739fail 16
3740end
3741if eq c 66
3742s_end
3743ret 0
3744end
3745if eq c 68
3746s_else
3747ret 0
3748end
3749if ne get dead 0 0
3750fail 17
3751end
3752if eq c 0
3753s_name w
3754ret 0
3755end
3756if eq c 67
3757s_if
3758ret 0
3759end
3760if eq c 69
3761s_while
3762ret 0
3763end
3764if eq c 71
3765s_for
3766ret 0
3767end
3768if eq c 72
3769s_ret
3770ret 0
3771end
3772if eq c 80
3773s_put
3774ret 0
3775end
3776if eq c 82
3777s_exit
3778ret 0
3779end
3780if or or or eq c 77 eq c 78 eq c 79 eq c 81
3781fail 9
3782end
3783fail 16
3784ret 0
3785end
3786
3787note ======== program mode: some line is `fn main` (blanks, fn, blanks, main, then a blank or the end) ========
3788note skipbl: the first place at or after q that is not a space or tab (or the end)
3789fn skipbl q ulong to ulong
3790e = get inlen 0
3791p = q
3792go = true
3793while go
3794m = ulong 0
3795while and go lt m 999999
3796if ge p e
3797go = false
3798else
3799b = get inp p
3800if or eq b 32 eq b 9
3801p = add p 1
3802else
3803go = false
3804end
3805end
3806m = add m 1
3807end
3808end
3809ret p
3810end
3811
3812note skipln: the place after the next newline at or after q (or the end)
3813fn skipln q ulong to ulong
3814e = get inlen 0
3815p = q
3816go = true
3817while go
3818m = ulong 0
3819while and go lt m 999999
3820if ge p e
3821go = false
3822else
3823b = get inp p
3824p = add p 1
3825if eq b 10
3826go = false
3827end
3828end
3829m = add m 1
3830end
3831end
3832ret p
3833end
3834
3835note ps_at: the line's first byte that is not blank is at p: 1 when the line is `fn main`, 2 when the input
3836note ends while deciding, else 0. om4 PS_LINE .. PS_YES.
3837fn ps_at p ulong to ulong
3838e = get inlen 0
3839if ne get inp p 102
3840ret 0
3841end
3842q = add p 1
3843if ge q e
3844ret 2
3845end
3846if ne get inp q 110
3847ret 0
3848end
3849r = add q 1
3850q = skipbl r
3851if ge q e
3852ret 2
3853end
3854if eq q r
3855ret 0
3856end
3857if gt add q 4 e
3858ret 0
3859end
3860if ne get inp q 109
3861ret 0
3862end
3863if ne get inp add q 1 97
3864ret 0
3865end
3866if ne get inp add q 2 105
3867ret 0
3868end
3869if ne get inp add q 3 110
3870ret 0
3871end
3872if ge add q 4 e
3873ret 1
3874end
3875if gt get inp add q 4 32
3876ret 0
3877end
3878ret 1
3879end
3880
3881fn prescan to void
3882e = get inlen 0
3883p = ulong 0
3884go = true
3885while go
3886m = ulong 0
3887while and go lt m 999999
3888p = skipbl p
3889if ge p e
3890go = false
3891else
3892r = ps_at p
3893if eq r 1
3894put progmode 0 1
3895go = false
3896end
3897if eq r 2
3898go = false
3899end
3900if go
3901p = skipln p
3902if ge p e
3903go = false
3904end
3905end
3906end
3907m = add m 1
3908end
3909end
3910end
3911
3912note First collect signatures. Only fn headers are parsed here; other lines are read in the body pass.
3913fn namescan to void
3914put scanning 0 1
3915put line 0 1
3916go = true
3917while go
3918n = ulong 0
3919while and go lt n 999999
3920tok
3921if eq get tk 0 3
3922go = false
3923else
3924if ne get tk 0 0
3925if and eq get tk 0 1 eq get tw 0 28262
3926s_fn
3927else
3928skipnl
3929end
3930end
3931end
3932n = add n 1
3933end
3934end
3935put scanning 0 0
3936put cur 0 0
3937put line 0 1
3938put nn 0 0
3939put curfn 0 0
3940put curret 0 0
3941put npar 0 0
3942put nslot 0 0
3943put held 0 0
3944end
3945
3946note main: read all of stdin
3947note (8 MiB at most, error 14; a read error is 1), find the mode, then one
3948note statement per line from output byte 180 (om4 START .. STMT)
3949fn main to int
3950off = ulong 0
3951go = true
3952while go
3953m = ulong 0
3954while and go lt m 999999
3955if ge off len inp
3956fail 14
3957end
3958n = read 0 inp off sub len inp off
3959if eq n 0
3960go = false
3961else
3962if lt n 0
3963fail 1
3964end
3965off = add off ulong n
3966end
3967m = add m 1
3968end
3969end
3970put inlen 0 off
3971prescan
3972namescan
3973put oc 0 180
3974put line 0 1
3975done = false
3976while not done
3977m = ulong 0
3978while and not done lt m 999999
3979if ne stmt 0
3980done = true
3981end
3982m = add m 1
3983end
3984end
3985ret 0
3986end