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 / evidence / SECURITY-673 / pure3 / mut4.om

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