interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / seed / omc / omc.om

19192 lines · 337225 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 irmode ulong 1
12global cur ulong 1
13global line ulong 1
14global tk ulong 1
15global tw ulong 1
16global tl ulong 1
17global ts ulong 1
18global tbig ulong 1
19global out uchar 16777216
20global oc ulong 1
21
22note ---- OM-2 profile (spec/omegese-om2.md, OMEGESE.md 9.13). om2 is 1 in an OM-2 unit (its first line
23note that is not a note or blank is exactly om2, om2 diag or om2 fmt); om2m 0 compile, 1 diag, 2 fmt;
24note om2ln/om2pos the line number and offset of that line; fmtm 1 while a unit is formatted; lbase and
25note sbase shift reported lines and bytes back to the original input in fmt mode; tka/tkb the byte span of
26note the last token; dexp what was being read (0 word 1 value 2 type 3 name 4 number 5 end 6 newline);
27note dsev 0 derived, 2 unknown (an undeclared function or struct), 3 error ----
28global om2 ulong 1
29global om2m ulong 1
30global om2ln ulong 1
31global om2pos ulong 1
32global fmtm ulong 1
33global chkmode ulong 1
34global chkln ulong 1
35global lbase ulong 1
36global sbase ulong 1
37global tka ulong 1
38global tkb ulong 1
39global dexp ulong 1
40global dsev ulong 1
41
42note ---- executable contracts: OM-2 debug by default, release erases only validated pure predicates.
43note Function prefixes are replayed at returns with their declaration scope, including inline frames.
44global crel ulong 1
45global cpure ulong 1
46global cvalid ulong 1
47global clim ulong 1
48global crkind ulong 1
49global crval ulong 1
50global crtype ulong 1
51global cfpos ulong 8
52global cfline ulong 8
53global cflim ulong 8
54global cfhas ulong 8
55global csnap ulong 8
56global ckind ulong 1
57
58note ---- OM-2 section 4 (unroll, inline, folding, bounds, sites). fn_inl/fn_body/fn_bln/fn_pn: a function's
59note inline mark, its body offset and line, its parameter names. fstart: where the function being compiled
60note starts (65536 words at most in OM-2). nmlo: names below it and at or above nglob are hidden while an
61note inlined body is compiled (0 otherwise). fzone: open unrolled bodies and inline frames (folding is
62note done only when it is above 0). inl and il_*: the inline frames (at most 4). ilp/nilp: the B words of
63note an inlined ret, patched at its end. lastb/tgoc: the word after the last inlined ret's B and the last
64note place a branch was patched to. u_*: the unrolled loop of each block. fok/fval/fty: a fold result.
65note sitebuf/nsite: the omc-site records of om2 diag.
66global fn_inl uchar 4096
67global fn_body ulong 4096
68global fn_bln ulong 4096
69global fn_pn ulong 24576
70global fstart ulong 1
71global nmlo ulong 1
72global fzone ulong 1
73global inl ulong 1
74global il_cur ulong 8
75global il_line ulong 8
76global il_ret ulong 8
77global il_nn ulong 8
78global il_lo ulong 8
79global il_held ulong 8
80global il_ns ulong 8
81global il_res ulong 8
82global il_fn ulong 8
83global il_p0 ulong 8
84global ilp ulong 4096
85global nilp ulong 1
86global lastb ulong 1
87global tgoc ulong 1
88global u_cur ulong 512
89global u_line ulong 512
90global u_it ulong 512
91global u_k ulong 512
92global u_nn ulong 512
93global u_held ulong 512
94note fuel (E14, E16): the expansion fuel used by the function being compiled: an unrolled loop costs K when
95note it starts, an inline expansion 1, at most 65536 in all (error 14)
96global fuel ulong 1
97global fok ulong 1
98global fval ulong 1
99global fty ulong 1
100global sitebuf uchar 8388608
101global nsite ulong 1
102global ie_from ulong 16384
103global ie_to ulong 16384
104global nie ulong 1
105global reach uchar 4096
106
107note ---- name table (8192 names in scope): packed name, register, type, length, kind, address ----
108global nm_name ulong 8192
109global nm_reg uchar 8192
110global nm_type uchar 8192
111global nm_len ulong 8192
112global nm_kind uchar 8192
113global nm_addr ulong 8192
114global nm_fv uchar 8192
115global nn ulong 1
116note ---- floats in v registers: kind 9 is a float held in v(16 + pool index of x register rv), the twin of
117note pool register rv; vsl marks each pool slot whose value lives in its twin (an f32/f64 local name, a
118note kind 9 value), saved and loaded as D around a call. fpw asks the next value read for kind 9 (1: a new
119note twin, 2 + r: the twin of r); fpo hands that request to the part that makes the value. A float local
120note name (nm_fv 1) lives in its twin; its x register holds a copy only while a reader needs one. ----
121global vsl uchar 16
122global fpw ulong 1
123global fpo ulong 1
124note fwv: rdval's fpw request at each nesting depth (kept here, not in a register of rdval)
125global fwv ulong 1026
126
127note ---- function table (4096): name, address (byte offset in out), return type, parameter count,
128note per-parameter type/length/kind (6 each), name length, name offset in the input ----
129global fn_name ulong 4096
130global fn_addr ulong 4096
131global fn_ret uchar 4096
132global fn_np uchar 4096
133global fn_pt uchar 24576
134global fn_pl ulong 24576
135global fn_pk uchar 24576
136global fn_nmlen ulong 4096
137global fn_off ulong 4096
138note x86 Linux retains the parser's sixteen logical slots in each native frame.
139note No A64 instruction may pass emit once the explicit target is selected (error 25).
140global xtarget ulong 1
141global xnaddr ulong 1
142global xasite ulong 65536
143global xavalue ulong 65536
144note x86efi (xtarget 2): exit-site jumps patched to the shared firmware return, the PE bss address,
145note and the error/done branch lists of one efi operation (closed by xe 2).
146global xexsite ulong 65536
147global xnexit ulong 1
148global xbss ulong 1
149global xenerr ulong 1
150global xerrpos ulong 256
151global xerrcode ulong 256
152global xenend ulong 1
153global xendpos ulong 256
154global nf ulong 1
155global lastfn ulong 1
156global scanning ulong 1
157global ncalls ulong 1
158global callsite ulong 524288
159global callfrom ulong 524288
160global callto ulong 524288
161global settled uchar 4096
162
163
164note ---- compiler state ----
165global npar ulong 1
166global held ulong 1
167global nslot ulong 1
168global curret ulong 1
169global progmode ulong 1
170global nglob ulong 1
171global hostargs ulong 1
172global maptab ulong 1
173global fdtab ulong 1
174global stkbytes ulong 1
175global dead ulong 1
176global poolb ulong 1
177
178note ---- structs (16) and fields (256). R6. nm_type 16+index, kind 6 own, 7 edit, 8 view ----
179global st_name ulong 16
180global st_nf uchar 16
181global st_cells uchar 16
182global st_foff uchar 16
183global nst ulong 1
184global sf_name ulong 256
185global sf_type uchar 256
186global sf_len uchar 256
187global sf_off uchar 256
188global nsf ulong 1
189global svk ulong 16
190global svv ulong 16
191global svt ulong 16
192
193note ---- OM-2 section 5 external layouts (16) and their fields (256): name, SIZE, first field, field count,
194note the cursor and line after the block's end (the body pass jumps there). Field: name, type, COUNT (0 a
195note scalar), byte offset, 1 big-endian. lymap marks the bytes taken while a layout is read. nm_lay /
196note fn_play: the layout (index + 1) of a layout parameter name / parameter slot, else 0 ----
197global ly_name ulong 16
198global ly_size ulong 16
199global ly_fo ulong 16
200global ly_nf ulong 16
201global ly_ec ulong 16
202global ly_el ulong 16
203global nly ulong 1
204global lf_name ulong 256
205global lf_type uchar 256
206global lf_cnt ulong 256
207global lf_off ulong 256
208global lf_big uchar 256
209global nlf ulong 1
210global lymap uchar 4096
211global nm_lay uchar 8192
212global fn_play uchar 24576
213
214note ---- OM-2 section 5 fresh scratch (erratum E8). fn_pfr: 1 for a `fresh` parameter slot. nm_fr: the
215note written-set slot (index + 1) a name's elements are tracked in, else 0. fz_n slots in use by the
216note function being compiled (16), fz_len their N, fz_cur the elements definitely written on the path
217note being compiled (bit i for element i); fz_ent / fz_thn (16 per block): the sets when the block opened
218note and at its else. il_fz: fz_n when an inline frame opened. dobx 1: error 14 reports obligation=init ----
219global fn_pfr uchar 24576
220global nm_fr uchar 8192
221global fz_n ulong 1
222global fz_len ulong 16
223global fz_cur ulong 16
224global fz_ent ulong 8192
225global fz_thn ulong 8192
226global il_fz ulong 8
227global dobx ulong 1
228
229note ---- OM-2 section 5 abstract types (16; type 64 + index): name, its length, representation type (4 uint,
230note 5 ulong), the preserving operations listed (1 rotr, 2 rotl, 4 and, 8 or, 16 xor), 1 when `holds NAME
231note dup32` is declared. aw_*: the word reader of the exact-constructor test (place, packed word, length,
232note newlines crossed before it) ----
233global ab_name ulong 16
234global ab_len ulong 16
235global ab_base uchar 16
236global ab_ops ulong 16
237global ab_holds uchar 16
238global nab ulong 1
239global aw_p ulong 1
240global aw_w ulong 1
241global aw_l ulong 1
242global aw_nl ulong 1
243
244note ---- OM-2 section 6 unsized views (spec 4 slots, 6, erratum E2). An unsized `view T *` / `edit T *`
245note name has length 0 in nm_len and its run-time element count in register nm_lr. Parameters take the
246note registers x0..x5 by slots: an unsized array two (pointer, then count), every other parameter one
247note (pwid, pslot); psl counts the slots of the fn line being read (6 at most, error 9). ul_k / ul_v: the
248note count (kind 1 register / 3 number) of argument i of the call at value depth d, at d * 6 + i ----
249global nm_lr uchar 8192
250global psl ulong 1
251global ul_k ulong 6156
252global ul_v ulong 6156
253note ---- OM-2 section 6 multi-entry units: nexp `export fn` lines (32 at most, function mode only), fn_exp 1
254note for an exported function, expf 1 while an `export fn` line is read. With any export, words 0 .. E-1
255note of the code are B to each export in source order and the code starts at byte 176 + 4E ----
256global nexp ulong 1
257global expf ulong 1
258global fn_exp uchar 4096
259
260note ======== leaf helpers ========
261
262note poolc: the i-th register of the pool x0..x6, x8..x15, x17 (x7 is the runtime, x16 scratch)
263fn poolc i ulong to ulong
264if lt i 7
265ret i
266end
267if lt i 15
268ret add i 1
269end
270ret 17
271end
272
273note pwid: the parameter slots of fn table entry p: 2 for an unsized array (pointer, count), else 1
274fn pwid p ulong to ulong
275k = ulong get fn_pk p
276if and or eq k 2 eq k 3 eq get fn_pl p 0
277ret 2
278end
279ret 1
280end
281
282note unsz: 1 when name entry i is an unsized array (nm_len 0, its count in register nm_lr), else 0. A text
283note (kind 5) of length 0 (`text e ""`) is sized: its count is the number 0 and it has no count register.
284fn unsz i ulong to ulong
285if and eq get nm_len i 0 ne get nm_kind i 5
286ret 1
287end
288ret 0
289end
290
291note pslot: the first slot (register) of parameter i of function f (index)
292fn pslot f ulong i ulong to ulong
293s = ulong 0
294j = ulong 0
295while lt j i
296s = add s pwid add mul f 6 j
297j = add j 1
298end
299ret s
300end
301
302note ---- error reporting ----
303global msg uchar 65536
304global mc ulong 1
305
306note mflush: complete a diagnostic/query stdout chunk or report compiler output error 2.
307fn mflush to void
308off = ulong 0
309n = get mc 0
310while lt off n
311w = write 1 msg off sub n off
312if le w 0
313exit int 2
314end
315off = add off ulong w
316end
317put mc 0 0
318end
319
320fn mputb b uchar to void
321m = get mc 0
322put msg m b
323put mc 0 add m 1
324end
325
326fn mputn v ulong to void
327d = new uchar 20 0
328i = ulong 0
329x = v
330put d 0 uchar add rem x 10 48
331x = div x 10
332i = add i 1
333while gt x 0
334put d i uchar add rem x 10 48
335x = div x 10
336i = add i 1
337end
338j = i
339while gt j 0
340j = sub j 1
341mputb get d j
342end
343end
344
345note fail: write "omc: line N: error E" to stderr (fd 2) and exit with the code
346fn fail code ulong to void
347put mc 0 0
348mputb 111
349mputb 109
350mputb 99
351mputb 58
352mputb 32
353mputb 108
354mputb 105
355mputb 110
356mputb 101
357mputb 32
358mputn add get line 0 get lbase 0
359mputb 58
360mputb 32
361mputb 101
362mputb 114
363mputb 114
364mputb 111
365mputb 114
366mputb 32
367mputn code
368mputb 10
369w = write 2 msg 0 get mc 0
370if ne get om2 0 0
371if eq get om2m 0 1
372drec code
373end
374end
375if ne get chkmode 0 0
376diagrec code
377end
378exit int code
379end
380
381note mputw: the first n bytes (at most 8) of the little-endian packed word w
382fn mputw w ulong n ulong to void
383i = ulong 0
384x = w
385while lt i n
386mputb uchar and x 255
387x = shr x 8
388i = add i 1
389end
390end
391
392note dcol: the 1-based column of byte a in its line
393fn dcol a ulong to ulong
394j = a
395go = true
396while go
397m = ulong 0
398while and go lt m 999999
399if eq j 0
400go = false
401else
402if eq get inp sub j 1 10
403go = false
404else
405j = sub j 1
406end
407end
408m = add m 1
409end
410end
411ret add sub a j 1
412end
413
414note dsev_w: sev= incomplete when the failure is on the end-of-input token (or inside a text literal cut by
415note the end), unknown for an undeclared function or struct, else error
416fn dsev_w code ulong to void
417s = get dsev 0
418if eq s 0
419if eq get tk 0 3
420s = ulong 1
421end
422if and eq code 22 ge get cur 0 get inlen 0
423s = ulong 1
424end
425end
426if eq s 1
427mputw 7308339910638005865 8
428mputw 25972 2
429ret
430end
431if eq s 2
432mputw 31093567915781749 7
433ret
434end
435mputw 491496043109 5
436end
437
438note dexp_w: expect= the class of token being read (end for an open function at the end of the input)
439fn dexp_w code ulong to void
440e = get dexp 0
441if eq code 15
442e = ulong 5
443end
444if or eq code 6 eq code 8
445e = ulong 4
446end
447if eq e 1
448mputw 435761734006 5
449ret
450end
451if eq e 2
452mputw 1701869940 4
453ret
454end
455if eq e 3
456mputw 1701667182 4
457ret
458end
459if eq e 4
460mputw 125779768604014 6
461ret
462end
463if eq e 5
464mputw 6581861 3
465ret
466end
467if eq e 6
468mputw 28550371718161774 7
469ret
470end
471mputw 1685221239 4
472end
473
474note dobl_w: obligation= from the code: 27 init, 28 ctor, 29 layout, 21 bounds, 14 growth, 12 regs, else -
475fn dobl_w code ulong to void
476if eq code 30
477mputw 8386654075301556067 8
478ret
479end
480if eq code 27
481mputw 1953066601 4
482ret
483end
484if eq code 28
485mputw 1919906915 4
486ret
487end
488if eq code 29
489mputw 128047730221420 6
490ret
491end
492if eq code 21
493mputw 126875187113826 6
494ret
495end
496if and eq code 14 ne get dobx 0 0
497mputw 1953066601 4
498ret
499end
500if eq code 14
501mputw 114849429287527 6
502ret
503end
504if eq code 12
505mputw 1936156018 4
506ret
507end
508mputb 45
509end
510
511note mputh: one byte as two lowercase hex digits
512fn mputh b ulong to void
513hi = shr b 4
514lo = and b 15
515if lt hi 10
516mputb uchar add hi 48
517else
518mputb uchar add hi 87
519end
520if lt lo 10
521mputb uchar add lo 48
522else
523mputb uchar add lo 87
524end
525end
526
527note drec: the om2 diag machine record after the human line (spec 1): code, sev, line (the human line),
528note col, the half-open byte span of the token being read, expect, found (at most 32 bytes, hex),
529note obligation, profile
530fn drec code ulong to void
531put mc 0 0
532mputw 7449351137626647919 8
533mputw 4424052638528581920 8
534mputn code
535mputw 263979365152 5
536dsev_w code
537mputw 67505853393952 6
538mputn add get line 0 get lbase 0
539a = get tka 0
540b = get tkb 0
541if le b a
542b = a
543if gt get cur 0 a
544b = get cur 0
545end
546end
547if gt b get inlen 0
548b = get inlen 0
549end
550mputw 263812244256 5
551mputn dcol a
552mputw 67544290456352 6
553mputn add a get sbase 0
554mputb 45
555mputn add b get sbase 0
556mputw 4428273620941825312 8
557dexp_w code
558mputw 17280399158765088 7
559if eq a b
560mputb 45
561end
562i = a
563while and lt i b lt sub i a 32
564mputh ulong get inp i
565i = add i 1
566end
567mputw 8386097683628191520 8
568mputw 1030647657 4
569dobl_w code
570mputw 7308332183992823840 8
571mputw 11210624683837 6
572w = write 2 msg 0 get mc 0
573end
574
575note emitb / emit: one byte / one 32-bit word (little-endian) to the output; error 14 when it is full
576note AArch64 retains its 8 MiB output ceiling; x86 byte sequences use the full output pool.
577fn emitb b uchar to void
578o = get oc 0
579if or ge o len out and eq get xtarget 0 0 ge o 8388608
580fail 14
581end
582note Charge x86's variable-width encoding to the same OM-2 emitted-storage budget as emit.
583if and ne get xtarget 0 0 ne get om2 0 0
584if ne get curret 0 0
585if ge sub o get fstart 0 262144
586fail 14
587end
588end
589end
590put out o b
591put oc 0 add o 1
592end
593
594fn emit w ulong to void
595if ne get xtarget 0 0
596fail 25
597end
598if ge get oc 0 8388608
599fail 14
600end
601if ne get om2 0 0
602if ne get curret 0 0
603if ge sub get oc 0 get fstart 0 262144
604fail 14
605end
606end
607end
608emitb uchar and w 255
609emitb uchar and shr w 8 255
610emitb uchar and shr w 16 255
611emitb uchar and shr w 24 255
612end
613
614note tok_num: the word at [ts,cur) is [-]decimal, at most 20 digits, from -2^63 to 2^64 - 1 (error 6
615note bad number, 8 out of range). tw = its raw 64 bits (a negative one as two's complement); tbig = 1
616note when it is 2^63 or more (only a ulong holds it), else 0. As om4's TK_NUM.
617note The float-marker scan walks the word in chunks of at most 999999 bytes: a single loop's hidden
618note 1000000-pass guard trapped 4 on a decimal word of 1000001 digits, which must refuse with error 6 (#671).
619fn tok_num to void
620if ne get om2 0 0
621if ne tok_hex 0
622ret
623end
624end
625p = get ts 0
626e = get cur 0
627fl = false
628i = p
629while lt i e
630c = sub e i
631if gt c 999999
632c = ulong 999999
633end
634for k ulong c
635b = get inp add i k
636if or or or eq b 46 eq b 101 eq b 69 eq b 102
637fl = true
638end
639end
640i = add i c
641end
642if fl
643put tk 0 5
644ret
645end
646minus = false
647if eq get inp p 45
648minus = true
649p = add p 1
650end
651ndig = sub e p
652if eq ndig 0
653nfail 6
654end
655if gt ndig 20
656nfail 6
657end
658v = ulong 0
659lim = ulong 1844674407370955161
660i = p
661while lt i e
662d = sub ulong get inp i 48
663if gt d 9
664nfail 6
665end
666if gt v lim
667nfail 8
668end
669if and eq v lim gt d 5
670nfail 8
671end
672v = add mul v 10 d
673i = add i 1
674end
675half = shl ulong 1 63
676put tbig 0 0
677if minus
678if gt v half
679nfail 8
680end
681put tw 0 sub ulong 0 v
682else
683if ge v half
684put tbig 0 1
685end
686put tw 0 v
687end
688put tk 0 2
689end
690
691note tok_text: a text literal starting at the opening quote. tw = first byte offset, tl = source length
692note (escapes not yet decoded). Ends at an unescaped '"' on the same line, followed by a blank/end. Err 22.
693note (Loops over the input are two loops, the inner under 1000000 passes: om4 has no pass limit.)
694fn tok_text to void
695put cur 0 add get cur 0 1
696put tw 0 get cur 0
697go = true
698while go
699n = ulong 0
700while and go lt n 999999
701if ge get cur 0 get inlen 0
702fail 22
703end
704b = get inp get cur 0
705if eq b 10
706fail 22
707end
708if eq b 34
709go = false
710else
711put cur 0 add get cur 0 1
712if eq b 92
713if ge get cur 0 get inlen 0
714fail 22
715end
716if eq get inp get cur 0 10
717fail 22
718end
719put cur 0 add get cur 0 1
720end
721end
722n = add n 1
723end
724end
725put tl 0 sub get cur 0 get tw 0
726put cur 0 add get cur 0 1
727put tk 0 4
728if lt get cur 0 get inlen 0
729if gt get inp get cur 0 32
730fail 22
731end
732end
733end
734
735note tok2: a word at cur with first byte c; packs 8 bytes into tw, sets tl and ts. A '"' is a text; a
736note first byte '-' or a digit is a number.
737fn tok2 c uchar to void
738if eq c 34
739tok_text
740ret
741end
742put ts 0 get cur 0
743if or eq c 40 eq c 41
744put tw 0 ulong c
745put tl 0 1
746put cur 0 add get cur 0 1
747put tk 0 1
748ret
749end
750w = ulong 0
751sh = ulong 0
752n = ulong 0
753go = true
754while go
755m = ulong 0
756while and go lt m 999999
757if ge get cur 0 get inlen 0
758go = false
759else
760b = get inp get cur 0
761if le b 32
762go = false
763else
764if or eq b 40 eq b 41
765go = false
766else
767if lt n 8
768w = or w shl ulong b sh
769sh = add sh 8
770end
771n = add n 1
772put cur 0 add get cur 0 1
773end
774end
775end
776m = add m 1
777end
778end
779put tw 0 w
780put tl 0 n
781f = get inp get ts 0
782isnum = false
783if eq f 45
784isnum = true
785end
786if and ge f 48 le f 57
787isnum = true
788end
789if isnum
790tok_num
791else
792put tk 0 1
793end
794end
795
796note tok: next word, skipping blanks (space, tab, carriage return); newline is end of line (kind 0) and
797note bumps the line number; another byte below 32 is error 16; the end of the input is kind 3. om4 TOK.
798fn tok to void
799go = true
800while go
801n = ulong 0
802while and go lt n 999999
803if ge get cur 0 get inlen 0
804go = false
805else
806c = get inp get cur 0
807if or or eq c 32 eq c 9 eq c 13
808put cur 0 add get cur 0 1
809else
810if eq c 10
811put tka 0 get cur 0
812put tkb 0 add get cur 0 1
813put cur 0 add get cur 0 1
814put line 0 add get line 0 1
815put tk 0 0
816ret
817end
818put tka 0 get cur 0
819put tkb 0 add get cur 0 1
820if lt c 32
821fail 16
822end
823put tka 0 get cur 0
824put tkb 0 get cur 0
825tok2 c
826put tkb 0 get cur 0
827ret
828end
829end
830n = add n 1
831end
832end
833put tka 0 get inlen 0
834put tkb 0 get inlen 0
835put tk 0 3
836end
837
838note ======== keyword lookup (original om4 KWTAB plus native host words 100..177, flock 197, execpure 198 and execenv 233) ========
839note genkw.awk describes the original om4 table only; retain the native host extension on regeneration.
840note efi (178) is a keyword only under `target x86efi`; gate18 checks that scoped entry separately.
841fn kw w ulong to ulong
842if and eq w 6907493 eq get xtarget 0 2
843ret 178
844end
845if eq w 7310034270740117605
846ret 198
847end
848if eq w 6584163
849ret 132
850end
851packed = or ulong 114 shl ulong 101 8
852packed = or packed shl ulong 97 16
853packed = or packed shl ulong 100 24
854packed = or packed shl ulong 108 32
855packed = or packed shl ulong 110 40
856packed = or packed shl ulong 107 48
857if eq w packed
858ret 133
859end
860packed = or ulong 115 shl ulong 111 8
861packed = or packed shl ulong 99 16
862packed = or packed shl ulong 107 24
863packed = or packed shl ulong 111 32
864packed = or packed shl ulong 112 40
865packed = or packed shl ulong 116 48
866if eq w packed
867ret 134
868end
869if eq w 120325193752946
870ret 135
871end
872if eq w 128017463930469
873ret 136
874end
875if eq w 28254586011020901
876ret 137
877end
878if eq w 491394459747
879ret 138
880end
881if eq w 109326067987558
882ret 139
883end
884if eq w 1952543859
885ret 140
886end
887if eq w 1802398060
888ret 141
889end
890if eq w 118122009688435
891ret 142
892end
893if eq w 431366170723
894ret 143
895end
896if eq w 32496501618073958
897ret 144
898end
899if eq w 110404104906087
900ret 145
901end
902if eq w 28263450605282663
903ret 146
904end
905if eq w 110403870025063
906ret 147
907end
908if eq w 28263390475740519
909ret 148
910end
911if eq w 499851228012
912ret 149
913end
914if eq w 7310575174812004980
915ret 150
916end
917if eq w 474450192483
918ret 151
919end
920if eq w 500152823651
921ret 152
922end
923if eq w 125796981695334
924ret 153
925end
926if eq w 28263429314995559
927ret 154
928end
929if eq w 461497265525
930ret 155
931end
932if eq w 109326135157862
933ret 156
934end
935if eq w 431215175021
936ret 157
937end
938if eq w 110404071351667
939ret 158
940end
941if eq w 28263390660289895
942ret 159
943end
944if eq w 28263390660289907
945ret 160
946end
947if eq w 431130767984
948ret 161
949end
950if eq w 111550659721072
951ret 162
952end
953if eq w 110404071351655
954ret 163
955end
956if eq w 126883592042867
957ret 164
958end
959if eq w 7959953343641905267
960ret 165
961end
962if eq w 119247140709732
963ret 166
964end
965if eq w 131277528260973
966ret 167
967end
968if eq w 31647734493634925
969ret 168
970end
971if eq w 28254586010624365
972ret 169
973end
974if eq w 7310584035480199533
975ret 170
976end
977if eq w 30518463021081956
978ret 171
979end
980if eq w 119247140124277
981ret 172
982end
983if eq w 32198529639276909
984ret 173
985end
986if eq w 127978875937133
987ret 174
988end
989if eq w 127979077263725
990ret 175
991end
992if eq w 28266723420430701
993ret 176
994end
995if eq w 28268896573219181
996ret 177
997end
998if eq w 30527267904513654
999ret 228
1000end
1001if eq w 31632320040101494
1002ret 229
1003end
1004if eq w 8606486010553525878
1005ret 230
1006end
1007if eq w 28260147925448293
1008ret 231
1009end
1010if eq w 31932469534615158
1011ret 232
1012end
1013if eq w 461229747302
1014ret 197
1015end
1016if eq w 1819242338
1017ret 1
1018end
1019if eq w 491260502901
1020ret 2
1021end
1022if eq w 14453
1023ret 2
1024end
1025if eq w 128034844210037
1026ret 3
1027end
1028if eq w 3551605
1029ret 3
1030end
1031if eq w 1953393013
1032ret 4
1033end
1034if eq w 3289973
1035ret 4
1036end
1037if eq w 444234427509
1038ret 5
1039end
1040if eq w 3421813
1041ret 5
1042end
1043if eq w 1702132066
1044ret 6
1045end
1046if eq w 14441
1047ret 6
1048end
1049if eq w 500136110195
1050ret 7
1051end
1052if eq w 3551593
1053ret 7
1054end
1055if eq w 7630441
1056ret 8
1057end
1058if eq w 3289961
1059ret 8
1060end
1061if eq w 1735290732
1062ret 9
1063end
1064if eq w 3421801
1065ret 9
1066end
1067if eq w 6579297
1068ret 33
1069end
1070if eq w 6452595
1071ret 34
1072end
1073if eq w 7107949
1074ret 35
1075end
1076if eq w 7760228
1077ret 36
1078end
1079if eq w 7169394
1080ret 37
1081end
1082if eq w 6581857
1083ret 38
1084end
1085if eq w 29295
1086ret 39
1087end
1088if eq w 7499640
1089ret 40
1090end
1091if eq w 7104627
1092ret 41
1093end
1094if eq w 7497843
1095ret 42
1096end
1097if eq w 29029
1098ret 43
1099end
1100if eq w 25966
1101ret 44
1102end
1103if eq w 29804
1104ret 45
1105end
1106if eq w 25964
1107ret 46
1108end
1109if eq w 29799
1110ret 47
1111end
1112if eq w 25959
1113ret 48
1114end
1115if eq w 6776174
1116ret 49
1117end
1118if eq w 7630702
1119ret 50
1120end
1121if eq w 7628135
1122ret 51
1123end
1124if eq w 7234924
1125ret 52
1126end
1127if eq w 1684104562
1128ret 53
1129end
1130if eq w 435744764535
1131ret 54
1132end
1133if eq w 1702195828
1134ret 55
1135end
1136if eq w 435728179558
1137ret 56
1138end
1139if eq w 2003134838
1140ret 57
1141end
1142if eq w 1953064037
1143ret 58
1144end
1145if eq w 28262
1146ret 64
1147end
1148if eq w 28532
1149ret 65
1150end
1151if eq w 6581861
1152ret 66
1153end
1154if eq w 26217
1155ret 67
1156end
1157if eq w 1702063205
1158ret 68
1159end
1160if eq w 435610544247
1161ret 69
1162end
1163if eq w 431349919586
1164ret 70
1165end
1166if eq w 7499622
1167ret 71
1168end
1169if eq w 7628146
1170ret 72
1171end
1172if eq w 61
1173ret 73
1174end
1175if eq w 40
1176ret 74
1177end
1178if eq w 41
1179ret 75
1180end
1181if eq w 1684631414
1182ret 76
1183end
1184if eq w 119165519096935
1185ret 78
1186end
1187if eq w 1954047348
1188ret 79
1189end
1190if eq w 7632240
1191ret 80
1192end
1193if eq w 1701147238
1194ret 81
1195end
1196if eq w 1953069157
1197ret 82
1198end
1199if eq w 1702129518
1200ret 83
1201end
1202if eq w 7824750
1203ret 84
1204end
1205if eq w 461195539042
1206ret 86
1207end
1208if eq w 1885434484
1209ret 87
1210end
1211if eq w 7239535
1212ret 77
1213end
1214if eq w 7308604897168158579
1215ret 131
1216end
1217if eq w 126918101919090
1218ret 130
1219end
1220if eq w 7453301735350958181
1221ret 124
1222end
1223if eq w 32481142782190962
1224ret 125
1225end
1226if eq w 491394461042
1227ret 126
1228end
1229if eq w 28263429247363435
1230ret 127
1231end
1232if eq w 32480013273033072
1233ret 128
1234end
1235if eq w 110404021020007
1236ret 129
1237end
1238if eq w 122498715312503
1239ret 123
1240end
1241if eq w 32497639802299751
1242ret 113
1243end
1244if eq w 32760384594014051
1245ret 114
1246end
1247if eq w 1684957538
1248ret 115
1249end
1250if eq w 28263429381382519
1251ret 116
1252end
1253if eq w 499851228006
1254ret 117
1255end
1256if eq w 1986225522
1257ret 118
1258end
1259if eq w 1684956531
1260ret 119
1261end
1262if eq w 32491051338263667
1263ret 120
1264end
1265if eq w 128017462948449
1266ret 121
1267end
1268if eq w 28254586010038881
1269ret 122
1270end
1271if eq w 111546465018726
1272ret 100
1273end
1274if eq w 427055215462
1275ret 101
1276end
1277if eq w 110429739705190
1278ret 102
1279end
1280if eq w 461262648166
1281ret 103
1282end
1283if eq w 33607047151054707
1284ret 104
1285end
1286if eq w 121382024472940
1287ret 105
1288end
1289if eq w 128026086171489
1290ret 106
1291end
1292if eq w 1802661734
1293ret 107
1294end
1295if eq w 431215375716
1296ret 108
1297end
1298if eq w 126918153105261
1299ret 109
1300end
1301if eq w 474148269926
1302ret 110
1303end
1304if eq w 118121959288437
1305ret 111
1306end
1307if eq w 32204027264196973
1308ret 112
1309end
1310if eq w 127970521019507
1311ret 85
1312end
1313if eq w 3289958
1314ret 88
1315end
1316if eq w 3421798
1317ret 89
1318end
1319if eq w 32777959416031586
1320ret 90
1321end
1322if eq w 500136244070
1323ret 91
1324end
1325if eq w 1935827302
1326ret 92
1327end
1328if eq w 1852403046
1329ret 93
1330end
1331if eq w 2019650918
1332ret 94
1333end
1334if eq w 112628629924211
1335ret 95
1336end
1337if eq w 7381246293360141935
1338ret 96
1339end
1340if eq w 7310870969309884259
1341ret 97
1342end
1343if eq w 7234314147887146352
1344ret 220
1345end
1346if eq w 32480013390212976
1347ret 221
1348end
1349if eq w 29118836274783603
1350ret 222
1351end
1352if eq w 7959665
1353ret 223
1354end
1355if eq w 7305804402314801251
1356ret 199
1357end
1358if eq w 8674333540260081763
1359ret 200
1360end
1361if eq w 7308604897285727600
1362ret 224
1363end
1364if eq w 6384998
1365ret 227
1366end
1367if eq w 33335428990204005
1368ret 233
1369end
1370if eq get om2 0 0
1371ret 0
1372end
1373ret kw2 w
1374end
1375
1376note ======== OM-2 words (spec/omegese-om2.md): reserved only in an OM-2 unit. rotr 59, rotl 60 are
1377note binary operators; dit 225 and scrub 226 are program-mode value words (OMEGESE 9.13.1); count 179 is a
1378note value word (cnt_word); the rest are statement and declaration words of later sections. ========
1379fn kw2 w ulong to ulong
1380if eq w 1920233330
1381ret 59
1382end
1383if eq w 1819570034
1384ret 60
1385end
1386if eq w 119212982038133
1387ret 181
1388end
1389if eq w 111524889521769
1390ret 182
1391end
1392if eq w 448612627046
1393ret 183
1394end
1395if eq w 8386654075301880417
1396ret 184
1397end
1398if eq w 1818322291
1399ret 185
1400end
1401if eq w 1852141679
1402ret 186
1403end
1404if eq w 495606067048
1405ret 187
1406end
1407if eq w 128047730221420
1408ret 188
1409end
1410if eq w 431315315046
1411ret 189
1412end
1413if eq w 29793
1414ret 190
1415end
1416if eq w 111516484659564
1417ret 191
1418end
1419if eq w 6777186
1420ret 192
1421end
1422if eq w 1953653104
1423ret 193
1424end
1425if eq w 7632239
1426ret 194
1427end
1428if eq w 128034844735589
1429ret 195
1430end
1431if eq w 7565423
1432ret 196
1433end
1434if eq w 7629156
1435ret 225
1436end
1437if eq w 422877225843
1438ret 226
1439end
1440if eq w 500069396323
1441ret 179
1442end
1443ret 0
1444end
1445
1446note nfail: a bad number (diag expect=number); unkfail: an undeclared function or struct (sev=unknown)
1447fn nfail code ulong to void
1448put dexp 0 4
1449fail code
1450end
1451
1452fn unkfail code ulong to void
1453put dsev 0 2
1454put dexp 0 3
1455fail code
1456end
1457
1458note lineis: the n bytes at q are the packed word w and the line ends right after them
1459fn lineis q ulong w ulong n ulong to ulong
1460e = get inlen 0
1461if gt add q n e
1462ret 0
1463end
1464i = ulong 0
1465x = w
1466while lt i n
1467if ne ulong get inp add q i and x 255
1468ret 0
1469end
1470x = shr x 8
1471i = add i 1
1472end
1473r = add q n
1474if lt r e
1475if ne get inp r 10
1476ret 0
1477end
1478end
1479ret 1
1480end
1481
1482note isnote: the word at p is note (the lexer ends a word at a blank, a parenthesis or the end)
1483fn isnote p ulong to ulong
1484e = get inlen 0
1485if gt add p 4 e
1486ret 0
1487end
1488i = ulong 0
1489x = ulong 1702129518
1490while lt i 4
1491if ne ulong get inp add p i and x 255
1492ret 0
1493end
1494x = shr x 8
1495i = add i 1
1496end
1497r = add p 4
1498if lt r e
1499b = get inp r
1500if and gt b 32 and ne b 40 ne b 41
1501ret 0
1502end
1503end
1504ret 1
1505end
1506
1507fn chk_mode q ulong to ulong
1508if ne lineis q 461229090915 5 0
1509ret 1
1510end
1511if ne lineis q 1954047342 4 0
1512ret 2
1513end
1514ret 0
1515end
1516
1517fn chk_lead to void
1518e = get inlen 0
1519p = ulong 0
1520ln = ulong 1
1521go = true
1522while go
1523m = ulong 0
1524while and go lt m 999999
1525q = skipbl p
1526if ge q e
1527go = false
1528else
1529if eq get inp q 10
1530p = add q 1
1531ln = add ln 1
1532else
1533if ne isnote q 0
1534p = skipln q
1535ln = add ln 1
1536else
1537k = chk_mode q
1538if ne k 0
1539put chkmode 0 k
1540put chkln 0 ln
1541put sbase 0 skipln q
1542end
1543go = false
1544end
1545end
1546end
1547m = add m 1
1548end
1549end
1550end
1551
1552note om2lead: skip blank lines and note lines; when the next line is exactly om2 (1), om2 diag (2) or
1553note om2 fmt (3) the unit is OM-2: om2, om2m, om2ln, om2pos. Anything else leaves the unit OM-1.
1554fn om2lead to void
1555e = get inlen 0
1556p = ulong 0
1557ln = ulong 1
1558if ne get chkmode 0 0
1559p = get sbase 0
1560ln = add get chkln 0 1
1561end
1562go = true
1563while go
1564m = ulong 0
1565while and go lt m 999999
1566q = skipbl p
1567if ge q e
1568go = false
1569else
1570if eq get inp q 10
1571p = add q 1
1572ln = add ln 1
1573else
1574if ne isnote q 0
1575p = skipln q
1576ln = add ln 1
1577else
1578r = ulong 0
1579if eq q p
1580if ne lineis q 3304815 3 0
1581r = ulong 1
1582end
1583if ne lineis q 7449351137405332847 8 0
1584r = ulong 2
1585end
1586if ne lineis q 32771382692703599 7 0
1587r = ulong 3
1588end
1589if eq r 0
1590r = cp_mode q
1591end
1592end
1593if ne r 0
1594put om2 0 1
1595put om2m 0 sub r 1
1596put om2ln 0 ln
1597put om2pos 0 q
1598end
1599go = false
1600end
1601end
1602end
1603m = add m 1
1604end
1605end
1606end
1607
1608note ---- om2 fmt (spec 2, errata E4, E6): only when the input's first line, at byte 0, is om2 fmt (an om2
1609note fmt line elsewhere is error 16). The rest (the bytes after the first LF) is compiled as a unit of
1610note its own (OM-1 or OM-2 by its own first line) and, when it is accepted, its canonical text is written
1611note instead of the artifact. A CR anywhere and blank lines at the end are error 6. Lines and spans are
1612note reported against the whole input.
1613global fst ulong 1
1614global fpend ulong 1
1615global fbo ulong 1
1616global fprp ulong 1
1617global flso ulong 1
1618
1619note fmttail: blank lines at the end of the rest (n bytes) are error 6, at the first such line
1620fn fmttail n ulong to void
1621lnb = ulong 0
1622lnl = ulong 1
1623ln = ulong 1
1624i = ulong 0
1625go = true
1626while go
1627m = ulong 0
1628while and go lt m 999999
1629if ge i n
1630go = false
1631else
1632b = get inp i
1633if eq b 10
1634ln = add ln 1
1635else
1636if and ne b 32 ne b 9
1637lnb = add i 1
1638lnl = ln
1639end
1640end
1641i = add i 1
1642end
1643m = add m 1
1644end
1645end
1646bad = false
1647if eq lnb 0
1648if gt n 0
1649bad = true
1650lnl = ulong 0
1651end
1652end
1653k = lnb
1654fl = n
1655go = true
1656while go
1657m = ulong 0
1658while and go lt m 999999
1659if ge k n
1660go = false
1661else
1662if eq get inp k 10
1663fl = k
1664go = false
1665end
1666k = add k 1
1667end
1668m = add m 1
1669end
1670end
1671if lt add fl 1 n
1672bad = true
1673end
1674if bad
1675put line 0 add lnl 1
1676put tka 0 lnb
1677put tkb 0 lnb
1678fail 6
1679end
1680end
1681
1682fn fmtprep to void
1683if ne get om2pos 0 0
1684put line 0 get om2ln 0
1685put tka 0 get om2pos 0
1686put tkb 0 get om2pos 0
1687fail 16
1688end
1689e = get inlen 0
1690ln = ulong 1
1691i = ulong 0
1692go = true
1693while go
1694m = ulong 0
1695while and go lt m 999999
1696if ge i e
1697go = false
1698else
1699b = get inp i
1700if eq b 13
1701put line 0 ln
1702put tka 0 i
1703put tkb 0 add i 1
1704fail 6
1705end
1706if eq b 10
1707ln = add ln 1
1708end
1709i = add i 1
1710end
1711m = add m 1
1712end
1713end
1714r = skipln get om2pos 0
1715put lbase 0 get om2ln 0
1716put sbase 0 r
1717n = sub e r
1718j = ulong 0
1719go = true
1720while go
1721m = ulong 0
1722while and go lt m 999999
1723if ge j n
1724go = false
1725else
1726put inp j get inp add r j
1727j = add j 1
1728end
1729m = add m 1
1730end
1731end
1732put inlen 0 n
1733fmttail n
1734put om2 0 0
1735put om2m 0 0
1736put om2ln 0 0
1737put fmtm 0 1
1738om2lead
1739if eq get om2m 0 2
1740put line 0 get om2ln 0
1741put tka 0 get om2pos 0
1742put tkb 0 get om2pos 0
1743fail 16
1744end
1745end
1746
1747note fmtbyte: one input byte of the canonical text. fst: 0 line start, 1 indentation (kept, dropped
1748note again on a blank line), 2 note line (copied), 3 between words, 4 in a word, 5 in a text literal, 6
1749note after a backslash in a text literal. A run of blanks between words (fpend, starting at output byte
1750note fbo) is written as it comes, then becomes one space unless a parenthesis is next to it (fprp: the
1751note word before it was one); at the end of the line it is dropped.
1752fn fmtbyte p ulong to void
1753b = get inp p
1754s = get fst 0
1755if eq b 10
1756if lt s 2
1757put oc 0 get flso 0
1758else
1759if ne get fpend 0 0
1760put oc 0 get fbo 0
1761end
1762end
1763emitb 10
1764put fst 0 0
1765put fpend 0 0
1766put fprp 0 0
1767put flso 0 get oc 0
1768ret
1769end
1770if eq s 2
1771emitb b
1772ret
1773end
1774if eq s 5
1775emitb b
1776if eq b 92
1777put fst 0 6
1778end
1779if eq b 34
1780put fst 0 3
1781put fprp 0 0
1782end
1783ret
1784end
1785if eq s 6
1786emitb b
1787put fst 0 5
1788ret
1789end
1790blank = or eq b 32 eq b 9
1791if lt s 2
1792if blank
1793emitb b
1794put fst 0 1
1795ret
1796end
1797if ne isnote p 0
1798emitb b
1799put fst 0 2
1800ret
1801end
1802put fst 0 3
1803put fpend 0 0
1804end
1805if blank
1806if eq get fpend 0 0
1807put fbo 0 get oc 0
1808put fpend 0 1
1809end
1810emitb b
1811put fst 0 3
1812ret
1813end
1814paren = or eq b 40 eq b 41
1815if and eq get fst 0 4 not paren
1816emitb b
1817ret
1818end
1819if ne get fpend 0 0
1820if and eq get fprp 0 0 not paren
1821put oc 0 get fbo 0
1822emitb 32
1823end
1824end
1825put fpend 0 0
1826emitb b
1827if eq b 34
1828put fst 0 5
1829put fprp 0 0
1830ret
1831end
1832if paren
1833put fst 0 3
1834put fprp 0 1
1835else
1836put fst 0 4
1837put fprp 0 0
1838end
1839end
1840
1841fn fmtout to void
1842put oc 0 0
1843put fst 0 0
1844put fpend 0 0
1845put fprp 0 0
1846put flso 0 0
1847e = get inlen 0
1848p = ulong 0
1849go = true
1850while go
1851m = ulong 0
1852while and go lt m 999999
1853if ge p e
1854go = false
1855else
1856fmtbyte p
1857p = add p 1
1858end
1859m = add m 1
1860end
1861end
1862if ge get fst 0 2
1863if ne get fpend 0 0
1864put oc 0 get fbo 0
1865end
1866emitb 10
1867end
1868put fmtm 0 0
1869put om2m 0 0
1870writeout 0
1871end
1872
1873note tok_hex (OM-2): 0x and 1..16 digits 0-9 a-f is the unsigned value (tk 2, tbig as tok_num). A - before
1874note 0x, 0X, no digits, more than 16 digits, an uppercase or any other character is error 6. 0 when the
1875note word is not a hex literal.
1876fn tok_hex to ulong
1877p = get ts 0
1878e = get cur 0
1879if eq get inp p 45
1880q = add p 1
1881if lt add q 1 e
1882if eq get inp q 48
1883c = get inp add q 1
1884if or eq c 120 eq c 88
1885nfail 6
1886end
1887end
1888end
1889ret 0
1890end
1891if ge add p 1 e
1892ret 0
1893end
1894if ne get inp p 48
1895ret 0
1896end
1897c = get inp add p 1
1898if eq c 88
1899nfail 6
1900end
1901if ne c 120
1902ret 0
1903end
1904n = sub e add p 2
1905if eq n 0
1906nfail 6
1907end
1908if gt n 16
1909nfail 6
1910end
1911v = ulong 0
1912i = add p 2
1913while lt i e
1914d = ulong get inp i
1915if and ge d 48 le d 57
1916v = or shl v 4 sub d 48
1917else
1918if and ge d 97 le d 102
1919v = or shl v 4 sub d 87
1920else
1921nfail 6
1922end
1923end
1924i = add i 1
1925end
1926put tbig 0 shr v 63
1927put tw 0 v
1928put tk 0 2
1929ret 1
1930end
1931
1932note ---- float literals: a token is the decimal value fsgn * fnd * 10^fp10, fsuf 0 none / 1 f32 / 2 f64.
1933note fg_* hold the working state of the one literal being converted (see fparse/fbits/fnorm/fgen).
1934global fsgn ulong 1
1935global fnd ulong 1
1936global fp10 long 1
1937global fsuf ulong 1
1938global fg_fl ulong 1
1939global fg_seen ulong 1
1940global fg_ex long 1
1941global fg_esign long 1
1942global fg_ed ulong 1
1943global fg_ty ulong 1
1944global fg_e long 1
1945global fg_q ulong 1
1946global fg_m ulong 1
1947global fg_stk ulong 1
1948global fbn ulong 80
1949
1950note ======== float literals: decimal text to IEEE-754 binary32/binary64 ========
1951note The literal is fnd * 10^fp10 = A / D with A = fnd * 10^max(fp10,0) and D = 10^max(-fp10,0), both
1952note held exactly as 40 32-bit limbs in fbn (A at 0, D at 40). The smaller of the two is shifted to the
1953note other's bit length, then restoring division yields the first 64 quotient bits (fg_m, leading bit
1954note set) and a remainder whose nonzero-ness is the sticky flag (fg_stk); froundsh rounds that exact
1955note truncation once to nearest even and fpack packs it, subnormals included (#1024: the old converter
1956note scaled one factor of ten at a time and dropped bits at every step). fp10 < -343 means a value
1957note below (2^63-1)*10^-344 < 2^-1075, signed zero; fp10 > 308 means at least 10^309, above the
1958note binary64 maximum (error 8). So A and D never exceed 1141 bits.
1959
1960note blen: bits needed for x (0 for zero)
1961fn blen x ulong to ulong
1962n = ulong 0
1963while ne x 0
1964x = shr x 1
1965n = add n 1
1966end
1967ret n
1968end
1969
1970note fparse: read the literal text inp[pos, pos+len) into fsgn, fnd, fp10, fsuf (0 none, 1 f32, 2 f64).
1971note Grammar: [-]digits[.digits][(e|E)[+|-]digits][f32|f64]. At most 2^63-1 significant digits
1972note (error 8), a dot with no digit after it is error 6, |exponent| <= 999 (error 8).
1973note The digit scans here and in fexp run as nested 999999-pass loops: a literal of more than 1000000
1974note zero digits trapped 4 in a single loop's hidden 1000000-pass guard (#965).
1975fn fparse pos ulong ln ulong to void
1976p = pos
1977e = add pos ln
1978put fsgn 0 0
1979put fsuf 0 0
1980put fnd 0 0
1981put fg_fl 0 0
1982put fg_seen 0 0
1983if eq get inp p 45
1984put fsgn 0 1
1985p = add p 1
1986end
1987go = true
1988while go
1989pos = ulong 0
1990while and go lt pos 999999
1991if ge p e
1992go = false
1993else
1994d = sub ulong get inp p 48
1995if le d 9
1996if gt get fnd 0 922337203685477580
1997fail 8
1998end
1999if eq get fnd 0 922337203685477580
2000if gt d 7
2001fail 8
2002end
2003end
2004put fnd 0 add mul get fnd 0 10 d
2005if ne get fg_seen 0 0
2006put fg_fl 0 add get fg_fl 0 1
2007end
2008p = add p 1
2009else
2010go = false
2011if eq get inp p 46
2012if eq get fg_seen 0 0
2013put fg_seen 0 1
2014p = add p 1
2015go = true
2016end
2017end
2018end
2019end
2020pos = add pos 1
2021end
2022end
2023if and ne get fg_seen 0 0 eq get fg_fl 0 0
2024fail 6
2025end
2026p = fexp p e
2027p = fsufx p e
2028if ne p e
2029fail 6
2030end
2031x = mul get fg_esign 0 get fg_ex 0
2032put fp10 0 sub x long get fg_fl 0
2033end
2034
2035note fexp: the exponent of the literal, if present: fg_ex, fg_esign, fg_ed. Returns the new position.
2036fn fexp p ulong e ulong to ulong
2037put fg_ex 0 0
2038put fg_esign 0 1
2039if and lt p e or eq get inp p 101 eq get inp p 69
2040p = add p 1
2041if lt p e
2042if or eq get inp p 43 eq get inp p 45
2043if eq get inp p 45
2044put fg_esign 0 -1
2045end
2046p = add p 1
2047end
2048end
2049put fg_ed 0 0
2050go = true
2051while go
2052m = ulong 0
2053while and go lt m 999999
2054if ge p e
2055go = false
2056else
2057d = sub ulong get inp p 48
2058if gt d 9
2059go = false
2060else
2061put fg_ex 0 mul get fg_ex 0 10
2062put fg_ex 0 add get fg_ex 0 long d
2063if gt get fg_ex 0 999
2064fail 8
2065end
2066put fg_ed 0 add get fg_ed 0 1
2067p = add p 1
2068end
2069end
2070m = add m 1
2071end
2072end
2073if eq get fg_ed 0 0
2074fail 6
2075end
2076end
2077ret p
2078end
2079
2080note fsufx: the optional f32/f64 suffix: fsuf 1 or 2. Returns the new position.
2081fn fsufx p ulong e ulong to ulong
2082if lt p e
2083r = sub e p
2084if ne r 3
2085fail 6
2086end
2087if ne get inp p 102
2088fail 6
2089end
2090if and eq get inp add p 1 51 eq get inp add p 2 50
2091put fsuf 0 1
2092else
2093if and eq get inp add p 1 54 eq get inp add p 2 52
2094put fsuf 0 2
2095else
2096fail 6
2097end
2098end
2099p = add p 3
2100end
2101ret p
2102end
2103
2104note fbmul: limbs fbn[o, o+40) times k (k < 2^32, so each limb step fits 64 bits); the product must fit
2105note in 40 limbs. Returns its bit length, so fbmul o 1 only measures.
2106fn fbmul o ulong k ulong to ulong
2107c = ulong 0
2108t = ulong 0
2109i = ulong 0
2110while lt i 40
2111j = add o i
2112v = add mul get fbn j k c
2113put fbn j and v 4294967295
2114c = shr v 32
2115if ne and v 4294967295 0
2116t = add mul i 32 blen and v 4294967295
2117end
2118i = add i 1
2119end
2120ret t
2121end
2122
2123note fbpow: fbn[o, o+40) times b^n (b 2 or 10), in factors that stay below 2^32.
2124fn fbpow o ulong b ulong n ulong to void
2125while gt n 0
2126m = ulong 1
2127c = ulong 0
2128while and lt c n le mul m b 4294967295
2129m = mul m b
2130c = add c 1
2131end
2132t = fbmul o m
2133n = sub n c
2134end
2135end
2136
2137note fbsub: one restoring-division step. If A >= D then A = A - D and 1, else A is kept and 0.
2138fn fbsub to ulong
2139r = ulong 2
2140i = ulong 40
2141while and gt i 0 eq r 2
2142i = sub i 1
2143a = get fbn i
2144d = get fbn add i 40
2145if lt a d
2146r = ulong 0
2147end
2148if gt a d
2149r = ulong 1
2150end
2151end
2152if eq r 0
2153ret 0
2154end
2155b = ulong 0
2156i = ulong 0
2157while lt i 40
2158v = sub sub get fbn i get fbn add i 40 b
2159put fbn i and v 4294967295
2160b = shr v 63
2161i = add i 1
2162end
2163ret 1
2164end
2165
2166note froundsh: round fg_m right by sh bits to fg_q, nearest even; the guard and every lower bit
2167note (plus fg_stk) decide. sh may be 64 (then q is zero and bit 63 is the guard).
2168fn froundsh sh ulong to void
2169m = get fg_m 0
2170q = ulong 0
2171guard = ulong 0
2172lost = ulong 0
2173if eq sh 0
2174q = m
2175else
2176if ge sh 64
2177guard = shr m 63
2178lost = and m 9223372036854775807
2179else
2180q = shr m sh
2181guard = and shr m sub sh 1 1
2182if gt sub sh 1 0
2183lost = and m sub shl 1 sub sh 1 1
2184end
2185end
2186end
2187sticky = or get fg_stk 0 lost
2188if ne guard 0
2189if or ne sticky 0 eq and q 1 1
2190q = add q 1
2191end
2192end
2193put fg_q 0 q
2194end
2195
2196note fpack: fg_m/fg_e to IEEE bits of fg_ty, including subnormals; a literal that rounds to infinity
2197note is refused (error 8).
2198fn fpack to ulong
2199p = ulong 53
2200bias = long 1023
2201maxe = long 2047
2202emin = long -1022
2203mb = ulong 52
2204w = ulong 64
2205if eq get fg_ty 0 10
2206p = ulong 24
2207bias = long 127
2208maxe = long 255
2209emin = long -126
2210mb = ulong 23
2211w = ulong 32
2212end
2213e = add get fg_e 0 63
2214if ge e emin
2215sh = sub 64 p
2216froundsh sh
2217q = get fg_q 0
2218if eq q shl 1 p
2219q = shr q 1
2220e = add e 1
2221end
2222E = add e bias
2223if ge E maxe
2224fail 8
2225end
2226q = and q sub shl 1 sub p 1 1
2227q = or q shl ulong E mb
2228q = or q shl get fsgn 0 sub w 1
2229ret q
2230end
2231k = ulong sub emin e
2232sh = add sub 64 p k
2233if gt sh 64
2234ret shl get fsgn 0 sub w 1
2235end
2236froundsh sh
2237q = get fg_q 0
2238q = or q shl get fsgn 0 sub w 1
2239ret q
2240end
2241
2242note fbits: the parsed literal as IEEE bits of type ty (10 f32, 11 f64). Zero keeps the sign (-0.0).
2243fn fbits ty ulong to ulong
2244put fg_ty 0 ty
2245p = get fp10 0
2246if or eq get fnd 0 0 lt p -343
2247w = ulong 64
2248if eq ty 10
2249w = ulong 32
2250end
2251b = ulong 0
2252if ne get fsgn 0 0
2253b = shl ulong 1 sub w 1
2254end
2255ret b
2256end
2257if gt p 308
2258fail 8
2259end
2260i = ulong 0
2261while lt i 80
2262put fbn i 0
2263i = add i 1
2264end
2265put fbn 0 and get fnd 0 4294967295
2266put fbn 1 shr get fnd 0 32
2267put fbn 40 1
2268if gt p 0
2269fbpow 0 10 ulong p
2270else
2271fbpow 40 10 ulong sub 0 p
2272end
2273a = fbmul 0 1
2274d = fbmul 40 1
2275e = long 0
2276if lt a d
2277fbpow 0 2 sub d a
2278e = sub 0 long sub d a
2279else
2280fbpow 40 2 sub a d
2281e = long sub a d
2282end
2283q = ulong 0
2284while lt q 9223372036854775808
2285q = or shl q 1 fbsub
2286a = fbmul 0 2
2287e = sub e 1
2288end
2289put fg_m 0 q
2290put fg_e 0 add e 1
2291put fg_stk 0 fbmul 0 1
2292ret fpack
2293end
2294
2295note fsizeof: sizeof T, or sizeof S for a struct: bytes. foffset: offsetof S f: the field's byte offset
2296note in the struct's 8-byte-cell layout. Both are compile-time constants of type ulong.
2297fn fsizeof dst ulong to void
2298sid = peek_struct
2299if ne sid 0
2300si = sub sid 1
2301n = ulong get st_cells si
2302vdone 3 mul n 8 5
2303ret
2304end
2305t = typeword
2306if ge t 64
2307fail 7
2308end
2309v = shl 1 tlog t
2310vdone 3 v 5
2311end
2312
2313fn foffset dst ulong to void
2314tok
2315if ne get tk 0 1
2316fail 16
2317end
2318if gt get tl 0 8
2319fail 13
2320end
2321sid = findstruct get tw 0
2322if eq sid 0
2323unkfail 3
2324end
2325tok
2326if ne get tk 0 1
2327fail 16
2328end
2329if gt get tl 0 8
2330fail 13
2331end
2332f = findfield sub sid 1 get tw 0
2333if eq f 0
2334fail 3
2335end
2336off = ulong get sf_off sub f 1
2337vdone 3 mul off 8 5
2338end
2339
2340note fspan: a deferred literal stored as pos*16777216+len. Input stays below 2^23 bytes, so neither part
2341note overflows its field; the old pos*64+len read a literal of 64 or more characters from the wrong span (#965).
2342fn fspan ty ulong packed ulong to ulong
2343fparse div packed 16777216 rem packed 16777216
2344ret fbits ty
2345end
2346
2347fn isfloat t ulong to ulong
2348if or eq t 10 eq t 11
2349ret 1
2350end
2351ret 0
2352end
2353
2354note ======== om4's small tables, as functions ========
2355note twid: width in bits by type 0..9 (bool 1; uchar ushort uint ulong; byte short int long)
2356fn twid t ulong to ulong
2357if eq t 13
2358ret 64
2359end
2360if eq t 10
2361ret 32
2362end
2363if eq t 11
2364ret 64
2365end
2366if eq t 1
2367ret 1
2368end
2369if or eq t 2 eq t 6
2370ret 8
2371end
2372if or eq t 3 eq t 7
2373ret 16
2374end
2375if or eq t 4 eq t 8
2376ret 32
2377end
2378if or eq t 5 eq t 9
2379ret 64
2380end
2381ret 0
2382end
2383
2384note tlog: log2 of the element size by type
2385fn tlog t ulong to ulong
2386if eq t 13
2387ret 3
2388end
2389if eq t 10
2390ret 2
2391end
2392if eq t 11
2393ret 3
2394end
2395if or eq t 3 eq t 7
2396ret 1
2397end
2398if or eq t 4 eq t 8
2399ret 2
2400end
2401if or eq t 5 eq t 9
2402ret 3
2403end
2404ret 0
2405end
2406
2407note regidx: the pool index of register r (the inverse of poolc; 255 for x7 and x16)
2408fn regidx r ulong to ulong
2409if lt r 7
2410ret r
2411end
2412if eq r 7
2413ret 255
2414end
2415if lt r 16
2416ret sub r 1
2417end
2418if eq r 16
2419ret 255
2420end
2421ret 15
2422end
2423
2424note 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,
2425note LS 9 / LE 13, HI 8 / GT 12, HS 2 / GE 10 (Arm ARM Table C1-1). The opposite condition is cond xor 1.
2426fn conds i ulong to ulong
2427if lt i 2
2428ret 0
2429end
2430if lt i 4
2431ret 1
2432end
2433if eq i 4
2434ret 3
2435end
2436if eq i 5
2437ret 11
2438end
2439if eq i 6
2440ret 9
2441end
2442if eq i 7
2443ret 13
2444end
2445if eq i 8
2446ret 8
2447end
2448if eq i 9
2449ret 12
2450end
2451if eq i 10
2452ret 2
2453end
2454ret 10
2455end
2456
2457note ldop / stop: register-offset load / store by element type (om4 LDOP, STOP tables)
2458fn ldop t ulong to ulong
2459note LDRB 0x38606800, LDRH 0x78607800, LDR w 0xb8607800, LDR x 0xf8607800, LDRSB 0x38a06800,
2460note LDRSH 0x78a07800, LDRSW 0xb8a07800
2461if eq t 2
2462ret 945842176
2463end
2464if eq t 3
2465ret 2019588096
2466end
2467if eq t 4
2468ret 3093329920
2469end
2470if or or eq t 5 eq t 9 eq t 13
2471ret 4167071744
2472end
2473if eq t 10
2474ret 3093329920
2475end
2476if eq t 11
2477ret 4167071744
2478end
2479if eq t 6
2480ret 950036480
2481end
2482if eq t 7
2483ret 2023782400
2484end
2485if eq t 8
2486ret 3097524224
2487end
2488ret 0
2489end
2490
2491fn stop t ulong to ulong
2492note STRB 0x38206800, STRH 0x78207800, STR w 0xb8207800, STR x 0xf8207800
2493if or eq t 2 eq t 6
2494ret 941647872
2495end
2496if or eq t 3 eq t 7
2497ret 2015393792
2498end
2499if or eq t 4 eq t 8
2500ret 3089135616
2501end
2502if or or eq t 5 eq t 9 eq t 13
2503ret 4162877440
2504end
2505if eq t 10
2506ret 3089135616
2507end
2508if eq t 11
2509ret 4162877440
2510end
2511ret 0
2512end
2513
2514note ---- trap list (8192 sites per function): output offset of the branch, trap code ----
2515global tsite ulong 8192
2516global tcode ulong 8192
2517global nt ulong 1
2518note ---- block stack (512): kind (1 if, 2 else, 3 while, 4 for), branch to patch, loop head, loop
2519note register, its type, names and held pool slots to restore at end, then-part ended (for else) ----
2520global bkind ulong 512
2521global bbr ulong 512
2522global bhead ulong 512
2523global breg ulong 512
2524global btyp ulong 512
2525global bnames ulong 512
2526global bheld ulong 512
2527global bdead ulong 512
2528global nb ulong 1
2529global curfn ulong 1
2530note ---- break fixups (1024): output offset of the branch, block index of the loop it leaves ----
2531global brkpos ulong 1024
2532global brkblk ulong 1024
2533global nbrk ulong 1
2534global cpos ulong 1024
2535global cblk ulong 1024
2536global ncont ulong 1
2537fn dcat_w code ulong to void
2538if or or eq code 4 eq code 6 eq code 16
2539mputw 9104236865280339 7
2540ret
2541end
2542if or or eq code 7 eq code 11 eq code 19
2543mputw 138601847124 5
2544ret
2545end
2546if or eq code 17 eq code 18
2547mputw 138903768134 5
2548ret
2549end
2550if or or eq code 3 eq code 10 eq code 13
2551mputw 138601644366 5
2552ret
2553end
2554if or or or eq code 12 eq code 14 eq code 23 eq code 24
2555mputw 6436850320626696515 8
2556mputb 32
2557ret
2558end
2559if or eq code 8 eq code 21
2560mputw 35481921143122 6
2561ret
2562end
2563if or or or eq code 27 eq code 28 eq code 29 eq code 30
2564mputw 6071768544483102531 8
2565mputb 32
2566ret
2567end
2568mputw 9104236865280339 7
2569end
2570
2571fn dty_w t ulong to void
2572if eq t 1
2573mputw 1819242338 4
2574ret
2575end
2576if eq t 2
2577mputw 491260502901 5
2578ret
2579end
2580if eq t 3
2581mputw 128034844210037 6
2582ret
2583end
2584if eq t 4
2585mputw 1953393013 4
2586ret
2587end
2588if eq t 5
2589mputw 444234427509 5
2590ret
2591end
2592if eq t 6
2593mputw 1702132066 4
2594ret
2595end
2596if eq t 7
2597mputw 500136110195 5
2598ret
2599end
2600if eq t 8
2601mputw 7630441 3
2602ret
2603end
2604if eq t 9
2605mputw 1735290732 4
2606ret
2607end
2608if eq t 10
2609mputw 3289958 3
2610ret
2611end
2612if eq t 11
2613mputw 3421798 3
2614ret
2615end
2616if eq t 12
2617mputw 1684631414 4
2618ret
2619end
2620if eq t 13
2621mputb 113
2622mputb 116
2623mputb 121
2624ret
2625end
2626mputw 2003134838 4
2627end
2628
2629fn dname_kind i ulong to void
2630k = ulong get nm_kind i
2631if eq k 4
2632mputw 119165519096935 6
2633ret
2634end
2635if eq k 5
2636mputw 1954047348 4
2637ret
2638end
2639if eq k 9
2640mputw 500152823651 5
2641ret
2642end
2643if or eq k 2 eq k 8
2644mputw 2003134838 4
2645ret
2646end
2647if or eq k 3 eq k 7
2648mputw 1953064037 4
2649ret
2650end
2651if ne get curfn 0 0
2652f = sub get curfn 0 1
2653np = ulong get fn_np f
2654endp = add get nglob 0 np
2655if and ge i get nglob 0 lt i endp
2656mputw 469786321264 5
2657ret
2658end
2659end
2660mputw 465490374508 5
2661end
2662
2663fn mput_name w ulong to void
2664x = w
2665go = true
2666while go
2667if eq x 0
2668go = false
2669else
2670b = and x 255
2671if eq b 0
2672go = false
2673else
2674mputb uchar b
2675x = shr x 8
2676end
2677end
2678end
2679end
2680
2681
2682fn mput_tok a ulong b ulong to void
2683if eq a b
2684if eq get tk 0 3
2685mputw 6713189 3
2686ret
2687end
2688mputb 45
2689ret
2690end
2691i = a
2692while and lt i b lt sub i a 16
2693c = ulong get inp i
2694if and gt c 32 lt c 127
2695mputb uchar c
2696else
2697mputh c
2698end
2699i = add i 1
2700end
2701end
2702
2703fn diagrec code ulong to void
2704put mc 0 0
2705mputw 7594244768023276911 8
2706mputw 11207551510369 6
2707if eq get om2 0 0
2708mputw 3239279 3
2709mputb 32
2710else
2711mputw 3304815 3
2712mputb 32
2713end
2714dcat_w code
2715a = get tka 0
2716b = get tkb 0
2717if le b a
2718b = a
2719if gt get cur 0 a
2720b = get cur 0
2721end
2722end
2723if gt b get inlen 0
2724b = get inlen 0
2725end
2726sa = a
2727sb = b
2728if ge sa get sbase 0
2729sa = sub sa get sbase 0
2730else
2731sa = ulong 0
2732end
2733if ge sb get sbase 0
2734sb = sub sb get sbase 0
2735else
2736sb = ulong 0
2737end
2738mputw 263844884595 5
2739mputn sa
2740mputb 45
2741mputn sb
2742mputb 32
2743s = get dsev 0
2744if eq s 0
2745if eq get tk 0 3
2746s = ulong 1
2747end
2748if and eq code 22 ge get cur 0 get inlen 0
2749s = ulong 1
2750end
2751end
2752if eq s 1
2753mputw 4993454379819552329 8
2754mputw 2114900 3
2755else
2756if eq s 2
2757mputw 2327894055524716117 8
2758else
2759mputw 35511885252934 6
2760end
2761end
2762note Repairability is independent of prefix finality; only capacity failures are unrepairable.
2763if or or or eq code 12 eq code 14 eq code 23 eq code 24
2764mputw 5929342197426179669 8
2765mputw 138601579073 4
2766else
2767mputw 4774187554571306322 8
2768mputw 2114892 2
2769end
2770mputw 67505686012704 6
2771mputn code
2772mputw 67505853393952 6
2773hl = get line 0
2774if ne get chkmode 0 0
2775if gt hl get chkln 0
2776hl = sub hl get chkln 0
2777else
2778hl = ulong 1
2779end
2780end
2781mputn hl
2782mputw 4428273620941825312 8
2783dexp_w code
2784mputw 17280399158765088 7
2785mput_tok a b
2786mputb 10
2787mflush
2788end
2789
2790fn emit_query to void
2791put mc 0 0
2792mputw 8462595071175650671 8
2793mputw 2869133187838565 7
2794mputw 9071406607197043 7
2795if eq get curfn 0 0
2796mputw 7810779306727075700 8
2797mputb 10
2798mputw 7166760226508402531 8
2799mputw 7142830393916027764 8
2800mputw 8028905624785088111 8
2801mputw 8392569455039504738 8
2802mputw 2338603462594687776 8
2803mputw 44651802478 5
2804else
2805mputw 7957695015192261990 8
2806mputb 10
2807mputw 7166760226508402531 8
2808mputw 2338605647353508724 8
2809mputw 7308332192711599721 8
2810mputw 7021802451731703328 8
2811mputw 8079585727842951280 8
2812mputw 9117624560546933 7
2813mputw 1702129518 4
2814in_loop = ulong 0
2815j = get nb 0
2816while and eq in_loop 0 gt j 0
2817j = sub j 1
2818if and ge get bkind j 3 le get bkind j 4
2819in_loop = ulong 1
2820end
2821end
2822if ne in_loop 0
2823mputw 7142827075067601440 8
2824mputw 749134030103146095 8
2825else
2826mputb 10
2827end
2828end
2829mputw 2879017064423790 7
2830mflush
2831i = ulong 0
2832while lt i get nn 0
2833if ne get nm_name i 0
2834mput_name get nm_name i
2835mputb 32
2836dty_w ulong get nm_type i
2837mputb 32
2838dname_kind i
2839mputb 10
2840mflush
2841end
2842i = add i 1
2843end
2844mputw 7957695015192261990 8
2845mputw 670323 3
2846mflush
2847f = ulong 0
2848while lt f get nf 0
2849mputw 2125414 3
2850note Single caller: spell the function name here rather than spend a bootstrap function slot.
2851o = get fn_off f
2852l = get fn_nmlen f
2853if and ne o 0 gt l 0
2854i = ulong 0
2855while lt i l
2856mputb get inp add o i
2857i = add i 1
2858end
2859else
2860mput_name get fn_name f
2861end
2862mputw 544175136 4
2863dty_w ulong get fn_ret f
2864np = ulong get fn_np f
2865p = ulong 0
2866while lt p np
2867mputb 32
2868dty_w ulong get fn_pt add mul f 6 p
2869p = add p 1
2870end
2871mputb 10
2872mflush
2873f = add f 1
2874end
2875mflush
2876exit int 0
2877end
2878note ======== names and functions ========
2879note findname: packed name -> its index + 1 among the names in scope (0 none). om4 FINDNAME.
2880fn findname w ulong to ulong
2881i = ulong 0
2882n = get nn 0
2883if ne get cpure 0 0
2884n = get clim 0
2885end
2886if eq i get nglob 0
2887if gt get nmlo 0 i
2888i = get nmlo 0
2889end
2890end
2891while lt i n
2892if eq get nm_name i w
2893ret add i 1
2894end
2895i = add i 1
2896if eq i get nglob 0
2897if gt get nmlo 0 i
2898i = get nmlo 0
2899end
2900end
2901end
2902ret 0
2903end
2904
2905note findfn: packed name w (first 8 bytes), length l (any value up to 8 for a short name), start s in the
2906note input (read only for a name over 8 bytes) -> function index + 1 (0 none). om4 FINDFN.
2907fn findfn w ulong l ulong s ulong to ulong
2908j = ulong 0
2909while lt j get nf 0
2910if eq get fn_name j w
2911fl = get fn_nmlen j
2912if le l 8
2913if le fl 8
2914ret add j 1
2915end
2916else
2917if eq fl l
2918o = get fn_off j
2919k = ulong 8
2920same = true
2921while and same lt k l
2922a = get inp add o k
2923if ne a get inp add s k
2924same = false
2925end
2926k = add k 1
2927end
2928if same
2929ret add j 1
2930end
2931end
2932end
2933end
2934j = add j 1
2935end
2936ret 0
2937end
2938
2939note newname: the name must not be in use (a name in scope when 8 bytes or fewer, or a function): error 10
2940fn newname w ulong l ulong s ulong to void
2941if ne findstruct w 0
2942fail 10
2943end
2944if le l 8
2945if ne findname w 0
2946fail 10
2947end
2948if ne findlay w 0
2949fail 10
2950end
2951if ne findab w 0
2952fail 10
2953end
2954end
2955if ne findfn w l s 0
2956fail 10
2957end
2958end
2959
2960note addname: packed name, register, type, length, kind, address. om4 ADDNAME.
2961fn addname w ulong r ulong t ulong ln ulong k ulong a ulong to void
2962n = get nn 0
2963if ge n 8192
2964fail 14
2965end
2966put nm_name n w
2967put nm_reg n uchar r
2968put nm_type n uchar t
2969put nm_len n ln
2970put nm_kind n uchar k
2971put nm_addr n a
2972put nm_fv n 0
2973put nm_lay n 0
2974put nm_fr n 0
2975put nm_lr n 0
2976put nn 0 add n 1
2977end
2978
2979note ======== registers and words ========
2980note alloc: the next free register of the pool (after the parameters); error 12 when none is left
2981fn alloc to ulong
2982i = add get npar 0 get nslot 0
2983if ge i 16
2984fail 12
2985end
2986put vsl i 0
2987put nslot 0 add get nslot 0 1
2988ret poolc i
2989end
2990
2991note destreg: d, or a new temporary when d is 255
2992fn destreg d ulong to ulong
2993if eq d 255
2994ret alloc
2995end
2996ret d
2997end
2998
2999note e3: emit op | d | n << 5 | m << 16
3000fn e3 op ulong d ulong n ulong m ulong to void
3001emit or or or op d shl n 5 shl m 16
3002end
3003
3004note trapsite: note the place and code of a branch to the trap stub, then emit it. trapc: B.cond, as om4.
3005fn trapsite w ulong code ulong to void
3006n = get nt 0
3007if ge n 8192
3008fail 14
3009end
3010put tsite n get oc 0
3011put tcode n code
3012put nt 0 add n 1
3013emit w
3014if eq get om2m 0 1
3015siterec
3016end
3017end
3018
3019fn trapc cc ulong code ulong to void
3020if ne get xtarget 0 0
3021xtrap cc code
3022ret
3023end
3024trapsite or 1409286144 cc code
3025end
3026
3027fn rdword at ulong to ulong
3028a = ulong get out at
3029b = shl ulong get out add at 1 8
3030c = shl ulong get out add at 2 16
3031d = shl ulong get out add at 3 24
3032ret or or or a b c d
3033end
3034
3035fn wrword at ulong w ulong to void
3036put out at uchar and w 255
3037put out add at 1 uchar and shr w 8 255
3038put out add at 2 uchar and shr w 16 255
3039put out add at 3 uchar and shr w 24 255
3040end
3041
3042note bdisp: the checked word distance from a branch at `at` to `target` for its signed w-bit word field:
3043note w 19 for B.cond / CBZ / CBNZ (C6.2.34, C6.2.78, C6.2.77), w 26 for B (C6.2.35) and BL. The
3044note target must be word aligned and at most 2^(w-1) - 1 words forward or 2^(w-1) words back (w 19: 1048572
3045note bytes forward, 1048576 back; w 26: 134217724 forward, 134217728 back), else error 24 before any
3046note artifact is accepted. Returns the distance masked to w bits.
3047fn bdisp at ulong target ulong w ulong to ulong
3048diff = sub target at
3049if ne and diff 3 0
3050fail 24
3051end
3052span = shl ulong 1 add w 1
3053if and gt diff sub span 4 lt diff sub ulong 0 span
3054fail 24
3055end
3056ret and shr diff 2 sub shl ulong 1 w 1
3057end
3058
3059note patch19: the word distance from a B.cond / CBZ / CBNZ at `at` to `target`, into bits 5..23
3060fn patch19 at ulong target ulong to void
3061if ne get xtarget 0 0
3062xpatch at target
3063if eq target get oc 0
3064put tgoc 0 target
3065end
3066ret
3067end
3068d = bdisp at target 19
3069wrword at or rdword at shl d 5
3070if eq target get oc 0
3071put tgoc 0 target
3072end
3073end
3074
3075note patch26: the word distance from a B at `at` to `target`, into bits 0..25, checked by bdisp. The AArch64
3076note output stops at 8 MiB = 2^23 bytes (emit, error 14), inside the 2^27-byte reach, so no legal program
3077note reaches error 24 here; the check keeps a wrong B from ever being written if that ceiling changes.
3078fn patch26 at ulong target ulong to void
3079if ne get xtarget 0 0
3080xpatch at target
3081if eq target get oc 0
3082put tgoc 0 target
3083end
3084ret
3085end
3086d = bdisp at target 26
3087wrword at or rdword at d
3088if eq target get oc 0
3089put tgoc 0 target
3090end
3091end
3092
3093note back: emit B to an earlier place (C6.2.35)
3094fn back addr ulong to void
3095if ne get xtarget 0 0
3096p = xbranch 14
3097xpatch p addr
3098ret
3099end
3100emit or 335544320 bdisp get oc 0 addr 26
3101end
3102
3103note matconst: put a 64-bit value in register r: MOVZ (C6.2.285) or, for a value with the top bit set,
3104note MOVN (C6.2.284) with the first halfword that differs from the fill, then MOVK (C6.2.283) for each
3105note other such halfword. om4 MATCONST.
3106fn matconst r ulong v ulong to void
3107if ne get xtarget 0 0
3108ximm 0 v
3109xstore 0 r
3110ret
3111end
3112fill = ulong 0
3113mop = ulong 3531603968
3114if ne shr v 63 0
3115fill = ulong 65535
3116mop = ulong 2457862144
3117end
3118started = false
3119hw = ulong 0
3120while lt hw 4
3121h = and shr v shl hw 4 65535
3122if ne h fill
3123if started
3124emit or or or 4068474880 shl h 5 shl hw 21 r
3125else
3126emit or or or mop shl xor h fill 5 shl hw 21 r
3127started = true
3128end
3129end
3130hw = add hw 1
3131end
3132if not started
3133emit or mop r
3134end
3135end
3136
3137note canon: u8/u16/u32 UBFM Xr, Xr, #0, #w-1 (C6.2.487); i8/i16/i32 SBFM (C6.2.355); else nothing
3138fn canon r ulong t ulong to void
3139canonto r r t
3140end
3141
3142note canonto: canon of Xn written to Xd (the same UBFM / SBFM with Rd = d, Rn = n)
3143fn canonto d ulong n ulong t ulong to void
3144if ne get xtarget 0 0
3145xload 0 n
3146xcanrax t
3147xstore 0 d
3148ret
3149end
3150if lt t 2
3151ret
3152end
3153if eq t 5
3154ret
3155end
3156if eq t 9
3157ret
3158end
3159base = ulong 3544186880
3160if gt t 5
3161base = ulong 2470445056
3162end
3163emit or or or base shl sub twid t 1 10 d shl n 5
3164end
3165
3166note chkext: CMP Xr, Wr, SXTB/SXTH/SXTW (SUBS extended, C6.2.463), then B.NE trap code
3167fn chkext r ulong t ulong code ulong to void
3168if ne get xtarget 0 0
3169xext r t code
3170ret
3171end
3172emit or or or 3944742943 shl sub t 2 13 shl r 16 shl r 5
3173trapc 1 code
3174end
3175
3176note range: error 8 unless v is a value of type t; big = the number was written as 2^63 or more
3177fn range v ulong t ulong big ulong to void
3178w = twid t
3179if eq w 64
3180if eq t 9
3181if ne big 0
3182fail 8
3183end
3184ret
3185end
3186if ne big 0
3187ret
3188end
3189if ne shr v 63 0
3190fail 8
3191end
3192ret
3193end
3194if ne big 0
3195fail 8
3196end
3197lim = shl ulong 1 w
3198if lt t 6
3199if ge v lim
3200fail 8
3201end
3202ret
3203end
3204half = shr lim 1
3205if ge add v half lim
3206fail 8
3207end
3208end
3209
3210note cset: CSET Xr, cond = CSINC Xr, XZR, XZR, opposite cond (C6.2.141)
3211fn cset r ulong cc ulong to void
3212if ne get xtarget 0 0
3213xb 15 add 144 xcc cc 192
3214xb 72 15 182
3215emitb 192
3216xstore 0 r
3217ret
3218end
3219emit or or 2594113504 r shl xor cc 1 12
3220end
3221
3222note force: put a value (kind 1 register, 3 number, 4 flags, 9 float in a twin: FMOV Xr, Dn) in register
3223note r; a bare number is error 11
3224fn force k ulong v ulong t ulong r ulong to void
3225if ne get xtarget 0 0
3226if eq k 1
3227xload 0 v
3228xstore 0 r
3229ret
3230end
3231end
3232if eq k 1
3233if ne v r
3234emit or or 2852127712 r shl v 16
3235end
3236ret
3237end
3238if or eq k 3 eq k 7
3239matconst r v
3240ret
3241end
3242if eq k 4
3243cset r v
3244ret
3245end
3246if eq k 9
3247fv2x t vtw v r
3248ret
3249end
3250fail 11
3251end
3252
3253note matreg: a register holding the value
3254fn matreg k ulong v ulong t ulong to ulong
3255if eq k 1
3256ret v
3257end
3258if eq k 9
3259fv2x t vtw v v
3260ret v
3261end
3262r = alloc
3263force k v t r
3264ret r
3265end
3266
3267note spop: ADD (C6.2.5) / SUB (C6.2.457) SP, SP by a multiple of 16 below 2^24: low 12 bits, then the
3268note high 12 bits LSL 12; nothing for a zero part
3269fn spop base ulong bytes ulong to void
3270lo = and bytes 4095
3271if ne lo 0
3272emit or or base 1023 shl lo 10
3273end
3274hi = and shr bytes 12 4095
3275if ne hi 0
3276emit or or base 4195327 shl hi 10
3277end
3278end
3279
3280fn spadd bytes ulong to void
3281if ne get xtarget 0 0
3282xsp 0 bytes
3283ret
3284end
3285spop 2432696320 bytes
3286end
3287
3288fn spsub bytes ulong to void
3289if ne get xtarget 0 0
3290xsp 1 bytes
3291ret
3292end
3293spop 3506438144 bytes
3294end
3295
3296note stkchk: program mode only: CMP SP, X28 (C6.2.463); B.LO trap 6
3297fn stkchk to void
3298if ne get xtarget 0 0
3299ret
3300end
3301if ne get progmode 0 0
3302emit 3946603519
3303trapc 3 6
3304end
3305end
3306
3307note epi: give back the stack of the local arrays, then RET (C6.2.338)
3308fn epi to void
3309if ne get xtarget 0 0
3310xload 0 0
3311emitb 201
3312emitb 195
3313ret
3314end
3315spadd get stkbytes 0
3316emit 3596551104
3317end
3318
3319fn dead1 to void
3320put dead 0 1
3321end
3322
3323note pushb / pushnh: open a block (om4 PUSHB, split in two because a call takes at most 6 values)
3324fn pushb br ulong k ulong head ulong reg ulong ty ulong to void
3325n = get nb 0
3326if ge n 512
3327fail 14
3328end
3329put bkind n k
3330put bbr n br
3331put bhead n head
3332put breg n reg
3333put btyp n ty
3334put bdead n 0
3335put nb 0 add n 1
3336note fz_push / fz_else / fz_join: the sets when block n opens; at its else (the then sets kept, the
3337note opening sets back); at its end (kind k: 1 if, 2 else, 3 while, 4 for)
3338j = ulong 0
3339while lt j get fz_n 0
3340put fz_ent add mul n 16 j get fz_cur j
3341j = add j 1
3342end
3343end
3344
3345fn pushnh names ulong hd ulong to void
3346n = sub get nb 0 1
3347put bnames n names
3348put bheld n hd
3349end
3350
3351note ======== small readers ========
3352fn elemtype t ulong to void
3353if lt t 2
3354fail 9
3355end
3356if ge t 64
3357fail 7
3358end
3359end
3360
3361note arrlen: an array length 1 .. 16777216 (error 8 below, 23 above, 23 when written as 2^63 or more)
3362fn arrlen v ulong to void
3363if ne get tbig 0 0
3364fail 23
3365end
3366if or eq v 0 ne shr v 63 0
3367fail 8
3368end
3369if gt v 16777216
3370fail 23
3371end
3372end
3373
3374note lenval: an array length, loop bound or trap code token: a number, or an integer constant name (kind 9).
3375note A name that is not an integer constant is error 16. When the line ends it is error eolc: 16 for a pool,
3376note a new array or a parameter written without its length (OMEGESE s.7, as om4), 4 for trap and bound.
3377fn lenval eolc ulong to ulong
3378tok
3379k = get tk 0
3380if eq k 0
3381fail eolc
3382end
3383if eq k 2
3384ret get tw 0
3385end
3386if eq k 1
3387if le get tl 0 8
3388e = findname get tw 0
3389if ne e 0
3390if eq get nm_kind sub e 1 9
3391t = ulong get nm_type sub e 1
3392if ge t 10
3393fail 16
3394end
3395put tbig 0 0
3396ret get nm_addr sub e 1
3397end
3398end
3399end
3400end
3401fail 16
3402ret 0
3403end
3404
3405note typeword: read a type name -> 1..9
3406fn typeword to ulong
3407put dexp 0 2
3408tok
3409k = get tk 0
3410if eq k 0
3411fail 4
3412end
3413if ne k 1
3414fail 16
3415end
3416if gt get tl 0 8
3417fail 16
3418end
3419c = kw get tw 0
3420if eq c 0
3421a = findab get tw 0
3422if ne a 0
3423ret add a 63
3424end
3425unkfail 16
3426end
3427if eq c 76
3428fail 9
3429end
3430if or eq c 88 eq c 89
3431if ne get irmode 0 0
3432fail 9
3433end
3434end
3435if eq c 88
3436ret 10
3437end
3438if eq c 89
3439ret 11
3440end
3441if eq c 223
3442ret 13
3443end
3444if gt sub c 1 8
3445fail 16
3446end
3447ret c
3448end
3449
3450note eol: the line must end here
3451fn eol to void
3452put dexp 0 6
3453tok
3454k = get tk 0
3455if and ne k 0 ne k 3
3456fail 5
3457end
3458end
3459
3460note arrname: read an array name -> its index + 1
3461fn arrname to ulong
3462put dexp 0 3
3463tok
3464k = get tk 0
3465if eq k 0
3466fail 4
3467end
3468if eq k 3
3469fail 4
3470end
3471if ne k 1
3472fail 16
3473end
3474if gt get tl 0 8
3475fail 13
3476end
3477if eq kw get tw 0 193
3478fail 9
3479end
3480e = findname get tw 0
3481if eq e 0
3482fail 3
3483end
3484if eq get nm_kind sub e 1 0
3485fail 7
3486end
3487ret e
3488end
3489
3490note topname: read a new name for a global or a text -> its packed word
3491fn topname to ulong
3492tok
3493if ne get tk 0 1
3494fail 16
3495end
3496if gt get tl 0 8
3497fail 13
3498end
3499w = get tw 0
3500if ne kw w 0
3501fail 16
3502end
3503newname w get tl 0 get ts 0
3504ret w
3505end
3506
3507note ---- value results (om4 x0 kind 1 register / 2 bare number / 3 typed number / 4 flags, x1 register,
3508note number or condition, x2 type). fr: 32 cells per value nesting depth 0..1024: 0..5 the two operands of
3509note a binary op (kind, value, type each), 8..25 a call's arguments (kind, value, array name each) ----
3510global rk ulong 1
3511global rv ulong 1
3512global rt ulong 1
3513global fr ulong 32800
3514global depth ulong 1
3515
3516note ======== BINOP (om4 BINOP and its BO_* parts): two values already read, types settled ========
3517note bo_w3: emit fx | rd | ra << 5 | (imm12 << 10, or rb << 16)
3518fn bo_w3 fx ulong rd ulong ra ulong rb ulong imm ulong to void
3519if ne imm 0
3520emit or or or fx rd shl ra 5 shl rb 10
3521else
3522emit or or or fx rd shl ra 5 shl rb 16
3523end
3524end
3525
3526note bo_fix: after a 64-bit op on rd: u8/u16/u32 wrap (canon), i8/i16/i32 overflow check (trap 1)
3527fn bo_fix rd ulong ty ulong to void
3528if lt ty 6
3529canon rd ty
3530ret
3531end
3532if eq ty 9
3533ret
3534end
3535chkext rd ty 1
3536end
3537
3538note bo_schk: a shift amount in a register must be below the width: CMP Xb, #w; B.HS trap 7
3539fn bo_schk ty ulong rb ulong to void
3540if ne get xtarget 0 0
3541xload 0 rb
3542ximm 1 twid ty
3543xrr 57 0 1
3544xtrap 2 7
3545ret
3546end
3547emit or or 4043309087 shl twid ty 10 shl rb 5
3548trapc 2 7
3549end
3550
3551note add, sub: u32 the 32-bit form; i64 ADDS/SUBS + B.VS trap 1; others 64-bit then bo_fix (om4 BO_ADD)
3552fn bo_add op ulong ty ulong ra ulong rb ulong imm ulong rd ulong to void
3553if ne get xtarget 0 0
3554xarith op ty ra rb imm rd
3555ret
3556end
3557fx = ulong 2332033024
3558if eq op 34
3559fx = ulong 3405774848
3560end
3561if ne imm 0
3562fx = ulong 2432696320
3563if eq op 34
3564fx = ulong 3506438144
3565end
3566end
3567if eq ty 4
3568bo_w3 xor fx 2147483648 rd ra rb imm
3569ret
3570end
3571if eq ty 9
3572bo_w3 or fx 536870912 rd ra rb imm
3573trapc 6 1
3574ret
3575end
3576if eq ty 13
3577bo_w3 or fx 536870912 rd ra rb imm
3578if eq op 34
3579trapc 3 1
3580else
3581trapc 2 1
3582end
3583ret
3584end
3585bo_w3 fx rd ra rb imm
3586bo_fix rd ty
3587end
3588
3589note mul: i64 SMULH t, MUL, CMP t, rd, ASR #63, B.NE trap 1 (om4 BO_MUL)
3590fn bo_mul ty ulong ra ulong rb ulong rd ulong to void
3591if ne get xtarget 0 0
3592xarith 35 ty ra rb 0 rd
3593ret
3594end
3595if eq ty 9
3596t = alloc
3597e3 2604694528 t ra rb
3598bo_w3 2600500224 rd ra rb 0
3599e3 3951098911 0 t rd
3600trapc 1 1
3601ret
3602end
3603if eq ty 13
3604t = alloc
3605e3 2613083136 t ra rb
3606bo_w3 2600500224 rd ra rb 0
3607emit or 4043309087 shl t 5
3608trapc 1 1
3609ret
3610end
3611if eq ty 4
3612bo_w3 453016576 rd ra rb 0
3613ret
3614end
3615bo_w3 2600500224 rd ra rb 0
3616bo_fix rd ty
3617end
3618
3619note div, rem (om4 BO_DIV, BO_SDIV): CBZ Xb trap 2 unless fl bit 1 (b a number other than 0); UDIV or
3620note 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,
3621note unless fl bit 2 (b a number other than -1)
3622fn bo_div op ulong ty ulong ra ulong rb ulong rd ulong fl ulong to void
3623if ne get xtarget 0 0
3624xdiv op ty ra rb rd fl
3625ret
3626end
3627if eq and fl 1 0
3628trapsite or 3019898880 rb 2
3629end
3630if eq op 37
3631t = alloc
3632if and ge ty 6 le ty 9
3633e3 2596277248 t ra rb
3634else
3635e3 2596276224 t ra rb
3636end
3637e3 or 2600501248 shl ra 10 rd t rb
3638ret
3639end
3640if or lt ty 6 eq ty 13
3641bo_w3 2596276224 rd ra rb 0
3642ret
3643end
3644if eq ty 9
3645if eq and fl 2 0
3646emit or 2969568287 shl rb 5
3647emit 1409286241
3648emit or 3942646783 shl ra 16
3649trapc 6 1
3650end
3651end
3652bo_w3 2596277248 rd ra rb 0
3653if ne ty 9
3654bo_fix rd ty
3655end
3656end
3657
3658note and, or, xor (om4 BO_LOG)
3659fn bo_log op ulong ra ulong rb ulong rd ulong to void
3660if ne get xtarget 0 0
3661xarith op 5 ra rb 0 rd
3662ret
3663end
3664fx = ulong 2315255808
3665if eq op 39
3666fx = ulong 2852126720
3667end
3668if eq op 40
3669fx = ulong 3388997632
3670end
3671bo_w3 fx rd ra rb 0
3672end
3673
3674note bo_lsl: LSL Xd, Xn, #s as UBFM Xd, Xn, #(64-s) mod 64, #63-s
3675fn bo_lsl ra ulong s ulong rd ulong to void
3676if ne get xtarget 0 0
3677xshift 41 5 ra s 1 rd
3678ret
3679end
3680r = shl and sub 64 s 63 16
3681r = or r shl sub 63 s 10
3682r = or r shl ra 5
3683emit or or 3544186880 r rd
3684end
3685
3686note shl (om4 BO_SHL, BS_64): by a number s: UBFM Xd, Xn, #(64-s) mod 64, #63-s; by a register: the amount
3687note check, LSLV; i64: t = a << s, X16 = t >> s arithmetic, CMP X16, Xa, B.NE trap 1, MOV Xd, Xt
3688fn bo_shl ty ulong ra ulong rb ulong imm ulong rd ulong to void
3689if ne get xtarget 0 0
3690xshift 41 ty ra rb imm rd
3691ret
3692end
3693if eq ty 9
3694t = alloc
3695if ne imm 0
3696bo_lsl ra rb t
3697emit or or 2470509584 shl rb 16 shl t 5
3698else
3699bo_schk ty rb
3700e3 2596282368 t ra rb
3701e3 2596284432 0 t rb
3702end
3703emit or 3942646303 shl ra 16
3704trapc 1 1
3705emit or or 2852127712 rd shl t 16
3706ret
3707end
3708if ne imm 0
3709bo_lsl ra rb rd
3710bo_fix rd ty
3711ret
3712end
3713bo_schk ty rb
3714if eq ty 4
3715bo_w3 448798720 rd ra rb 0
3716ret
3717end
3718bo_w3 2596282368 rd ra rb 0
3719bo_fix rd ty
3720end
3721
3722note shr: unsigned LSR, signed ASR; by a number UBFM / SBFM Xd, Xn, #s, #63; by a register the amount check
3723note then LSRV / ASRV (om4 BO_SHR)
3724fn bo_shr ty ulong ra ulong rb ulong imm ulong rd ulong to void
3725if ne get xtarget 0 0
3726xshift 42 ty ra rb imm rd
3727ret
3728end
3729if ne imm 0
3730fi = ulong 3544251392
3731if ge ty 6
3732fi = ulong 2470509568
3733end
3734emit or or or fi shl rb 16 shl ra 5 rd
3735ret
3736end
3737bo_schk ty rb
3738fx = ulong 2596283392
3739if ge ty 6
3740fx = ulong 2596284416
3741end
3742bo_w3 fx rd ra rb 0
3743end
3744
3745note comparisons: CMP Xa, #imm or CMP Xa, Xb; the result is the flags and a condition (om4 BO_CMP)
3746fn bo_cmp op ulong ty ulong ra ulong rb ulong imm ulong to void
3747if ne get xtarget 0 0
3748xcmp ra rb imm
3749else
3750if ne imm 0
3751emit or or 4043309087 shl rb 10 shl ra 5
3752else
3753emit or or 3942645791 shl rb 16 shl ra 5
3754end
3755end
3756i = mul sub op 43 2
3757if and ge ty 6 le ty 9
3758i = add i 1
3759end
3760put rk 0 4
3761put rv 0 conds i
3762put rt 0 1
3763end
3764
3765note Baseline SSE2, Intel SDM MOVD/MOVQ, scalar SS/SD, UCOMIS, CVTSI and CVTT instructions.
3766note WolframLanguageEvaluator: {2^31,2^63,2^22,2^51} -> {2147483648,9223372036854775808,4194304,2251799813685248}.
3767note WolframLanguageEvaluator: FromDigits[#,16]& /@ {"7F800000","7FF0000000000000","7FC00000","7FF8000000000000"}
3768note -> {2139095040,9218868437227405312,2143289344,9221120237041090560}.
3769fn xfmask ty ulong kind ulong to ulong
3770if eq ty 10
3771if eq kind 0
3772ret 2147483648
3773end
3774if eq kind 1
3775ret 2147483647
3776end
3777if eq kind 2
3778ret 4194304
3779end
3780if eq kind 3
3781ret 2139095040
3782end
3783ret 2143289344
3784end
3785if eq kind 0
3786ret 9223372036854775808
3787end
3788if eq kind 1
3789ret 9223372036854775807
3790end
3791if eq kind 2
3792ret 2251799813685248
3793end
3794if eq kind 3
3795ret 9218868437227405312
3796end
3797ret 9221120237041090560
3798end
3799
3800note MOVD clears EAX's upper bits on extraction; no float sign extension.
3801fn xfmov ty ulong op ulong vr ulong to void
3802emitb 102
3803if eq ty 11
3804emitb 72
3805end
3806xb 15 op add 192 shl vr 3
3807end
3808
3809fn xfsse ty ulong op ulong mr ulong to void
3810if eq ty 10
3811emitb 243
3812else
3813emitb 242
3814end
3815xb 15 op mr
3816end
3817
3818fn xfucom ty ulong mr ulong to void
3819if eq ty 11
3820emitb 102
3821end
3822xb 15 46 mr
3823end
3824
3825note Raw Intel condition for parity: existing xbranch takes the A64 condition vocabulary.
3826fn xfbranch cc ulong to ulong
3827p = get oc 0
3828emitb 15
3829emitb uchar add 128 cc
3830xword 0
3831ret p
3832end
3833
3834note Leaves the raw value in RAX, magnitude in RCX, and unsigned magnitude-versus-infinity flags.
3835fn xfclass ty ulong r ulong to void
3836xload 0 r
3837ximm 1 xfmask ty 1
3838xrr 33 1 0
3839ximm 2 xfmask ty 3
3840xrr 57 1 2
3841end
3842
3843note Arm FPProcessNaNs priority: first signaling NaN, second signaling NaN, then first quiet NaN.
3844note minNum/maxNum additionally return the numeric argument when paired with one quiet NaN.
3845note Return the selected-result jump for the caller to patch past its normal arithmetic.
3846fn xfnans ty ulong a ulong b ulong num ulong d ulong to ulong
3847note Emit shared result stubs first so each case patches immediately instead of retaining its own fixup.
3848skip = xbranch 14
3849quiet = get oc 0
3850ximm 1 xfmask ty 2
3851xrr 9 0 1
3852store = get oc 0
3853xstore 0 d
3854d = xbranch 14
3855xpatch skip get oc 0
3856xfclass ty a
3857skip = xbranch 8
3858xfclass ty b
3859bn = xbranch 8
3860none = xbranch 14
3861xpatch skip get oc 0
3862ximm 1 xfmask ty 2
3863xrr 133 0 1
3864p = xbranch 0
3865xpatch p quiet
3866xfclass ty b
3867skip = xbranch 9
3868ximm 1 xfmask ty 2
3869xrr 133 0 1
3870p = xbranch 0
3871xpatch p quiet
3872xload 0 a
3873p = xbranch 14
3874xpatch p store
3875xpatch skip get oc 0
3876if ne num 0
3877xload 0 b
3878else
3879xload 0 a
3880end
3881p = xbranch 14
3882xpatch p store
3883xpatch bn get oc 0
3884if ne num 0
3885ximm 1 xfmask ty 2
3886xrr 133 0 1
3887p = xbranch 0
3888xpatch p quiet
3889xload 0 a
3890p = xbranch 14
3891xpatch p store
3892else
3893p = xbranch 14
3894xpatch p quiet
3895end
3896xpatch none get oc 0
3897ret d
3898end
3899
3900note SSE invalid arithmetic produces a negative indefinite NaN; Arm's default NaN is positive.
3901note Operand NaNs have already bypassed arithmetic, retaining their sign and payload.
3902fn xfdefnan ty ulong d ulong to void
3903xfclass ty d
3904ok = xbranch 9
3905ximm 0 xfmask ty 4
3906xstore 0 d
3907xpatch ok get oc 0
3908end
3909
3910fn xfbin op ulong ty ulong a ulong b ulong d ulong to void
3911nanend = xfnans ty a b 0 d
3912f2v ty a 0
3913f2v ty b 1
3914code = ulong 89
3915if eq op 33
3916code = ulong 88
3917end
3918if eq op 34
3919code = ulong 92
3920end
3921if eq op 36
3922code = ulong 94
3923end
3924xfsse ty code 193
3925fv2x ty 0 d
3926xfdefnan ty d
3927xpatch nanend get oc 0
3928end
3929
3930fn xfminmax c ulong ty ulong a ulong b ulong d ulong to void
3931nanend = xfnans ty a b 1 d
3932f2v ty a 0
3933f2v ty b 1
3934xfucom ty 193
3935cc = ulong 3
3936if eq c 94
3937cc = ulong 8
3938end
3939takea = xbranch cc
3940if eq c 94
3941cc = ulong 3
3942else
3943cc = ulong 8
3944end
3945takeb = xbranch cc
3946note Equal finite operands have equal bits except signed zero: OR chooses -0, AND chooses +0.
3947xload 0 a
3948xload 1 b
3949if eq c 94
3950xrr 33 0 1
3951else
3952xrr 9 0 1
3953end
3954xstore 0 d
3955equalend = xbranch 14
3956xpatch takea get oc 0
3957xload 0 a
3958xstore 0 d
3959aend = xbranch 14
3960xpatch takeb get oc 0
3961xload 0 b
3962xstore 0 d
3963xpatch equalend get oc 0
3964xpatch aend get oc 0
3965xpatch nanend get oc 0
3966end
3967
3968note Normalize the predicate to TEST flags; callers retain the existing rk=4, rv=NE contract.
3969fn xfcmp op ulong ty ulong a ulong b ulong to void
3970f2v ty a 0
3971f2v ty b 1
3972xfucom ty 193
3973unord = xfbranch 10
3974cc = ulong 4
3975if eq op 44
3976cc = ulong 5
3977end
3978if eq op 45
3979cc = ulong 2
3980end
3981if eq op 46
3982cc = ulong 6
3983end
3984if eq op 47
3985cc = ulong 7
3986end
3987if eq op 48
3988cc = ulong 3
3989end
3990xb 15 add 144 cc 192
3991done = xbranch 14
3992xpatch unord get oc 0
3993if eq op 44
3994ximm 0 1
3995else
3996ximm 0 0
3997end
3998xpatch done get oc 0
3999xb 72 15 182
4000emitb 192
4001xrr 133 0 0
4002put rk 0 4
4003put rv 0 1
4004put rt 0 1
4005end
4006
4007fn xfsito ty ulong to void
4008if eq ty 10
4009emitb 243
4010else
4011emitb 242
4012end
4013emitb 72
4014xb 15 42 192
4015end
4016
4017fn xfint ft ulong tt ulong a ulong d ulong to void
4018xload 0 a
4019if eq ft 5
4020xrr 133 0 0
4021small = xbranch 10
4022note Sticky halve before signed conversion preserves nearest-even rounding for the unsigned high half.
4023xrr 137 2 0
4024xb 72 209 234
4025ximm 1 1
4026xrr 33 0 1
4027xrr 9 0 2
4028xfsito tt
4029xfsse tt 88 192
4030done = xbranch 14
4031xpatch small get oc 0
4032xfsito tt
4033xpatch done get oc 0
4034else
4035xfsito tt
4036end
4037fv2x tt 0 d
4038end
4039
4040fn xfconst bits ulong to void
4041ximm 0 bits
4042xfmov 11 110 1
4043end
4044
4045fn xfcvtt to void
4046emitb 242
4047emitb 72
4048xb 15 44 192
4049end
4050
4051
4052note Widen f32 exactly, guard NaN and the destination's own bounds, then CVTT toward zero.
4053note WolframLanguageEvaluator: Table[BitShiftLeft[1023+k,52],{k,{7,8,15,16,31,32,63,64}}]
4054note -> {4638707616191610880,4643211215818981376,4674736413210574848,4679240012837945344,4746794007248502784,4751297606875873280,4890909195324358656,4895412794951729152}.
4055fn xftoint ft ulong tt ulong d ulong to void
4056n = twid tt
4057sg = and ge tt 6 le tt 9
4058if eq ft 10
4059xfsse 10 90 192
4060end
4061xfucom 11 192
4062nan = xfbranch 10
4063if sg
4064n = sub n 1
4065end
4066xfconst shl add ulong 1023 n 52
4067xfucom 11 193
4068above = xbranch 2
4069if sg
4070xfconst or 9223372036854775808 shl add ulong 1023 n 52
4071else
4072xfconst 0
4073end
4074xfucom 11 193
4075below = xbranch 9
4076if and not sg eq n 64
4077xfconst 4890909195324358656
4078xfucom 11 193
4079small = xbranch 3
4080xfsse 11 92 193
4081xfcvtt
4082ximm 1 9223372036854775808
4083xrr 9 0 1
4084done = xbranch 14
4085xpatch small get oc 0
4086xfcvtt
4087xpatch done get oc 0
4088else
4089xfcvtt
4090end
4091done = xbranch 14
4092xpatch nan get oc 0
4093ximm 0 0
4094nan = xbranch 14
4095xpatch above get oc 0
4096maxv = ulong 18446744073709551615
4097if sg
4098maxv = sub shl ulong 1 n 1
4099else
4100if lt n 64
4101maxv = sub shl ulong 1 n 1
4102end
4103end
4104ximm 0 maxv
4105above = xbranch 14
4106xpatch below get oc 0
4107if sg
4108ximm 0 sub ulong 0 shl ulong 1 n
4109else
4110ximm 0 0
4111end
4112xpatch done get oc 0
4113xpatch nan get oc 0
4114xpatch above get oc 0
4115xstore 0 d
4116end
4117
4118fn xfconv ft ulong tt ulong a ulong to ulong
4119if or eq ft 1 eq tt 1
4120fail 9
4121end
4122if or eq ft 13 eq tt 13
4123fail 7
4124end
4125if eq ft tt
4126ret a
4127end
4128d = alloc
4129if ge ft 10
4130f2v ft a 0
4131if ge tt 10
4132xfsse ft 90 192
4133fv2x tt 0 d
4134else
4135xftoint ft tt d
4136end
4137else
4138xfint ft tt a d
4139end
4140ret d
4141end
4142
4143note xfbinop: binop's float arm on x86: both operands in x registers, a compare or one SSE operation.
4144fn xfbinop op ulong dst ulong ix ulong ty ulong to void
4145ra = matreg get fr ix get fr add ix 1 ty
4146rb = matreg get fr add ix 3 get fr add ix 4 ty
4147if ge op 43
4148xfcmp op ty ra rb
4149ret
4150end
4151if or or lt op 33 gt op 36 ge op 41
4152fail 7
4153end
4154rd = destreg dst
4155xfbin op ty ra rb rd
4156put rk 0 1
4157put rv 0 rd
4158put rt 0 ty
4159end
4160
4161note xfun1 / xfmm: fintr's and fminmax's x86 arms; the operand is in rk/rv (fminmax: the first in ak/av).
4162fn xfun1 c ulong t ulong dst ulong to void
4163ra = matreg get rk 0 get rv 0 t
4164rd = destreg dst
4165if eq c 91
4166nanend = xfnans t ra ra 0 rd
4167f2v t ra 0
4168xfsse t 81 192
4169fv2x t 0 rd
4170xfdefnan t rd
4171xpatch nanend get oc 0
4172else
4173xload 0 ra
4174ximm 1 xfmask t 1
4175xrr 33 0 1
4176xstore 0 rd
4177end
4178put rk 0 1
4179put rv 0 rd
4180put rt 0 t
4181end
4182
4183fn xfmm c ulong at ulong ak ulong av ulong dst ulong to void
4184ra = matreg ak av at
4185rb = matreg get rk 0 get rv 0 at
4186rd = destreg dst
4187xfminmax c at ra rb rd
4188put rk 0 1
4189put rv 0 rd
4190put rt 0 at
4191end
4192
4193note ---- floating point. A float value is its IEEE bit pattern: in an x register (kind 1, 7 a constant),
4194note or in a v register (kind 9, the twin of a pool register; float local names live there). Arithmetic
4195note runs one scalar FP instruction (round to nearest even, no fused op) on v registers: an x operand moves
4196note to v0/v1 first; a result wanted in x moves back from v1/v2, one wanted as kind 9 stays in its twin.
4197note f32 uses the S form (low 32 bits), f64 the D form.
4198fn f2v ty ulong xr ulong vr ulong to void
4199if ne get xtarget 0 0
4200xload 0 xr
4201xfmov ty 110 vr
4202ret
4203end
4204if eq ty 10
4205emit or or 505872384 shl xr 5 vr
4206else
4207emit or or 2657550336 shl xr 5 vr
4208end
4209end
4210
4211fn fv2x ty ulong vr ulong xr ulong to void
4212if ne get xtarget 0 0
4213xfmov ty 126 vr
4214xstore 0 xr
4215ret
4216end
4217if eq ty 10
4218emit or or 505806848 shl vr 5 xr
4219else
4220emit or or 2657484800 shl vr 5 xr
4221end
4222end
4223
4224note vtw: the twin of pool register r, v(16 + its pool index): v16..v31, caller-saved in AAPCS64
4225fn vtw r ulong to ulong
4226ret add 16 regidx r
4227end
4228
4229note fv8d / fv8s: the FMOV (scalar, immediate) imm8 of f64 / f32 bits b, or 256 when there is none. imm8 is
4230note the sign, then bits 54..48 (f64) / 25..19 (f32). Encodable only when the fraction bits below those are
4231note zero and exponent bits 61..54 (f64) / 29..25 (f32) all equal NOT bit 62 / 30 (VFPExpandImm).
4232fn fv8d b ulong to ulong
4233if ne and b 281474976710655 0
4234ret 256
4235end
4236e = and shr b 54 255
4237if and ne e 0 ne e 255
4238ret 256
4239end
4240if eq and shr b 62 1 and e 1
4241ret 256
4242end
4243s = shl shr b 63 7
4244s = or s shl and e 1 6
4245ret or s and shr b 48 63
4246end
4247
4248fn fv8s b ulong to ulong
4249if ne and b 524287 0
4250ret 256
4251end
4252e = and shr b 25 31
4253if and ne e 0 ne e 31
4254ret 256
4255end
4256if eq and shr b 30 1 and e 1
4257ret 256
4258end
4259s = shl and shr b 31 1 7
4260s = or s shl and e 1 6
4261ret or s and shr b 19 63
4262end
4263
4264note fvk: float bits b of type ty into v register vr: 0 FMOV from XZR (C7.2.131); a value with an 8-bit
4265note form FMOV (scalar, immediate, C7.2.132); any other MOVZ/MOVK into X16, then FMOV from X16.
4266fn fvk ty ulong b ulong vr ulong to void
4267if eq b 0
4268f2v ty 31 vr
4269ret
4270end
4271if eq ty 10
4272im = fv8s b
4273if lt im 256
4274emit or or 505417728 shl im 13 vr
4275ret
4276end
4277else
4278im = fv8d b
4279if lt im 256
4280emit or or 509612032 shl im 13 vr
4281ret
4282end
4283end
4284matconst 16 b
4285f2v ty 16 vr
4286end
4287
4288note fvop: the v register holding float operand (kind k, value v, type t): its twin for kind 9, else v
4289note register s, filled from the constant or from the operand's x register.
4290fn fvop k ulong v ulong t ulong s ulong to ulong
4291if eq k 9
4292ret vtw v
4293end
4294if eq k 7
4295fvk t v s
4296ret s
4297end
4298r = matreg k v t
4299f2v t r s
4300ret s
4301end
4302
4303note fvdst: the pool register whose twin takes a kind 9 result for request o (fpw): 1 a new one, its slot
4304note marked as living in v; 2 + r the twin of r (a float local name).
4305fn fvdst o ulong to ulong
4306if eq o 1
4307r = alloc
4308put vsl regidx r uchar 1
4309ret r
4310end
4311ret sub o 2
4312end
4313
4314note fvset: float value (kind k, value v, type t) into the twin of r: FMOV Dd, Dn (C7.2.130), the constant,
4315note or FMOV from its x register. Nothing when it is already there.
4316fn fvset k ulong v ulong t ulong r ulong to void
4317d = vtw r
4318if eq k 9
4319if ne v r
4320if eq t 10
4321emit or or 505430016 shl vtw v 5 d
4322else
4323emit or or 509624320 shl vtw v 5 d
4324end
4325end
4326ret
4327end
4328if eq k 7
4329fvk t v d
4330ret
4331end
4332x = matreg k v t
4333f2v t x d
4334end
4335
4336note fp_binop: one FP add / sub / mul / div of v registers va, vb into vd
4337fn fp_binop op ulong ty ulong va ulong vb ulong vd ulong to void
4338base = ulong 509609984
4339if eq ty 10
4340base = ulong 505415680
4341end
4342if eq op 33
4343if eq ty 10
4344base = ulong 505423872
4345else
4346base = ulong 509618176
4347end
4348else
4349if eq op 34
4350if eq ty 10
4351base = ulong 505427968
4352else
4353base = ulong 509622272
4354end
4355else
4356if eq op 36
4357if eq ty 10
4358base = ulong 505419776
4359else
4360base = ulong 509614080
4361end
4362end
4363end
4364end
4365emit or or or base shl vb 16 shl va 5 vd
4366end
4367
4368fn fp_neg ty ulong va ulong vd ulong to void
4369if eq ty 10
4370emit or or ulong 505495552 shl va 5 vd
4371else
4372emit or or ulong 509689856 shl va 5 vd
4373end
4374end
4375
4376note fp_un1: sqrt (c 91) or abs of v register va into vd
4377fn fp_un1 c ulong ty ulong va ulong vd ulong to void
4378if eq c 91
4379if eq ty 10
4380emit or or ulong 505528320 shl va 5 vd
4381else
4382emit or or ulong 509722624 shl va 5 vd
4383end
4384else
4385if eq ty 10
4386emit or or ulong 505462784 shl va 5 vd
4387else
4388emit or or ulong 509657088 shl va 5 vd
4389end
4390end
4391end
4392
4393note fp_minmax: FMINNM/FMAXNM, the IEEE minNum/maxNum rule: one quiet NaN returns the other operand,
4394note and -0.0 orders below +0.0.
4395fn fp_minmax c ulong ty ulong va ulong vb ulong vd ulong to void
4396base = ulong 509638656
4397if eq c 94
4398base = ulong 509634560
4399end
4400if eq ty 10
4401base = ulong 505444352
4402if eq c 94
4403base = ulong 505440256
4404end
4405end
4406emit or or or base shl vb 16 shl va 5 vd
4407end
4408
4409note fintr: sqrt or abs of one float value; fminmax: min or max of two values of one float type.
4410note Both refuse a bare integer operand and an unresolved literal, as everywhere else. A request o (fpo)
4411note makes the result in a twin (kind 9); otherwise it moves to x register destreg dst (kind 1).
4412fn fintr c ulong dst ulong to void
4413o = get fpo 0
4414put fpo 0 0
4415if ne get irmode 0 0
4416fail 9
4417end
4418put fpw 0 1
4419rdval 0 0 255 0
4420if eq get rk 0 2
4421fail 7
4422end
4423if eq get rk 0 6
4424fail 11
4425end
4426t = get rt 0
4427if eq isfloat t 0
4428fail 7
4429end
4430if ne get xtarget 0 0
4431xfun1 c t dst
4432vdec
4433ret
4434end
4435va = fvop get rk 0 get rv 0 t 0
4436if eq o 0
4437rd = destreg dst
4438fp_un1 c t va 1
4439fv2x t 1 rd
4440put rk 0 1
4441put rv 0 rd
4442else
4443r = fvdst o
4444fp_un1 c t va vtw r
4445put rk 0 9
4446put rv 0 r
4447end
4448put rt 0 t
4449vdec
4450end
4451
4452fn fminmax c ulong dst ulong to void
4453o = get fpo 0
4454put fpo 0 0
4455if ne get irmode 0 0
4456fail 9
4457end
4458put fpw 0 1
4459rdval 0 0 255 0
4460if or eq get rk 0 2 eq get rk 0 6
4461fail 11
4462end
4463at = get rt 0
4464ak = get rk 0
4465av = get rv 0
4466put fpw 0 1
4467rdval 0 0 255 0
4468if or eq get rk 0 2 eq get rk 0 6
4469fail 11
4470end
4471if or eq isfloat at 0 eq isfloat get rt 0 0
4472fail 7
4473end
4474if ne at get rt 0
4475fail 7
4476end
4477if ne get xtarget 0 0
4478xfmm c at ak av dst
4479vdec
4480ret
4481end
4482va = fvop ak av at 0
4483vb = fvop get rk 0 get rv 0 at 1
4484if eq o 0
4485rd = destreg dst
4486fp_minmax c at va vb 2
4487fv2x at 2 rd
4488put rk 0 1
4489put rv 0 rd
4490else
4491r = fvdst o
4492fp_minmax c at va vb vtw r
4493put rk 0 9
4494put rv 0 r
4495end
4496put rt 0 at
4497vdec
4498end
4499
4500note FMADD (scalar), ARM C7: one rounded multiply-add, not FMUL followed by FADD.
4501note The addend is Ra, bits 14..10; multiplicands Rn, Rm are bits 9..5, 20..16.
4502fn ffma dst ulong to void
4503o = get fpo 0
4504put fpo 0 0
4505put fpw 0 1
4506rdval 0 0 255 0
4507if eq get rk 0 6
4508fail 11
4509end
4510at = get rt 0
4511if eq isfloat at 0
4512fail 7
4513end
4514ak = get rk 0
4515av = get rv 0
4516put fpw 0 1
4517rdval 0 0 255 0
4518if eq get rk 0 6
4519fail 11
4520end
4521if ne get rt 0 at
4522fail 7
4523end
4524bk = get rk 0
4525bv = get rv 0
4526put fpw 0 1
4527rdval 0 0 255 0
4528if eq get rk 0 6
4529fail 11
4530end
4531if ne get rt 0 at
4532fail 7
4533end
4534note x86's baseline is SSE2, not FMA3. No unfused fallback is permitted. The operands are typed
4535note first, so an ill-typed fma gets the same error 7 or 11 on every target; only a well-typed one
4536note is refused here.
4537if or ne get irmode 0 0 ne get xtarget 0 0
4538fail 9
4539end
4540va = fvop ak av at 0
4541vb = fvop bk bv at 1
4542vc = fvop get rk 0 get rv 0 at 2
4543r = ulong 0
4544vd = ulong 3
4545if eq o 0
4546r = destreg dst
4547else
4548r = fvdst o
4549vd = vtw r
4550end
4551b = ulong 520093696
4552if eq at 11
4553b = 524288000
4554end
4555b = or b shl vb 16
4556b = or b shl vc 10
4557b = or b shl va 5
4558emit or b vd
4559if eq o 0
4560fv2x at vd r
4561put rk 0 1
4562else
4563put rk 0 9
4564end
4565put rv 0 r
4566put rt 0 at
4567vdec
4568end
4569
4570note fcond: the condition for a float compare op: EQ, NE, MI (lt), LS (le), GT, GE. MI/LS/GT/GE are
4571note false for NaN, so unordered comparisons come out as the IEEE rules require.
4572fn fcond op ulong to ulong
4573i = sub op 43
4574if eq i 0
4575ret 0
4576end
4577if eq i 1
4578ret 1
4579end
4580if eq i 2
4581ret 4
4582end
4583if eq i 3
4584ret 9
4585end
4586if eq i 4
4587ret 12
4588end
4589ret 10
4590end
4591
4592note fp_cmp: FCMP Sn, Sm / Dn, Dm (scalar) of v registers va, vb: 0x1E202000 / 0x1E602000 with Rm and Rn
4593note zero, so both fields come from va and vb alone (#861: the S word had Rm bit 0 set, and an even vb
4594note read the next twin).
4595fn fp_cmp op ulong ty ulong va ulong vb ulong to void
4596if eq ty 10
4597emit or or 505421824 shl vb 16 shl va 5
4598else
4599emit or or 509616128 shl vb 16 shl va 5
4600end
4601put rk 0 4
4602put rv 0 fcond op
4603put rt 0 1
4604end
4605
4606note fp_conv: one scalar conversion (int<->float, f32<->f64) into a new x register. Same type is a copy.
4607note Returns the register. A float result is made in v(rd), or in v0 when rd is x17 (v17 is a twin).
4608fn fp_conv ft ulong tt ulong ra ulong to ulong
4609if ne get xtarget 0 0
4610ret xfconv ft tt ra
4611end
4612if eq ft tt
4613ret ra
4614end
4615if eq ft 1
4616fail 9
4617end
4618if eq tt 1
4619fail 9
4620end
4621rd = alloc
4622if eq isfloat tt 0
4623f2v ft ra 0
4624fp_toint ft tt rd
4625ret rd
4626end
4627vs = rd
4628if ge rd 16
4629vs = ulong 0
4630end
4631fp_tov ft tt ra vs
4632fv2x tt vs rd
4633ret rd
4634end
4635
4636note fp_tov: x register ra of type ft (an integer, or the other float type, moved to v0 first) converted
4637note to float type tt in v register vd: SCVTF/UCVTF (C7.2.236/C7.2.355) by signedness, FCVT (C7.2.69).
4638fn fp_tov ft ulong tt ulong ra ulong vd ulong to void
4639if ne isfloat ft 0
4640f2v ft ra 0
4641if eq ft 10
4642emit or 505593856 vd
4643else
4644emit or 509755392 vd
4645end
4646ret
4647end
4648sgn = false
4649if ge ft 6
4650sgn = true
4651end
4652if eq tt 10
4653if eq twid ft 32
4654if sgn
4655emit or or 505544704 shl ra 5 vd
4656else
4657emit or or 505610240 shl ra 5 vd
4658end
4659else
4660if sgn
4661emit or or 2653028352 shl ra 5 vd
4662else
4663emit or or 2653093888 shl ra 5 vd
4664end
4665end
4666else
4667if eq twid ft 32
4668if sgn
4669emit or or 509739008 shl ra 5 vd
4670else
4671emit or or 509804544 shl ra 5 vd
4672end
4673else
4674if sgn
4675emit or or 2657222656 shl ra 5 vd
4676else
4677emit or or 2657288192 shl ra 5 vd
4678end
4679end
4680end
4681end
4682
4683
4684note fp_toint: float (v0) to integer rd: truncate toward zero, saturate to the destination's own range, NaN
4685note to 0 (OMEGESE 9.9; spec/one-layer-format.md 10.3). 32/64-bit: FCVTZS/FCVTZU (C7.2.90/C7.2.94) by
4686note signedness, int sign-extended (SXTW). 8/16-bit: the scalar SIMD FCVTZ[SU] in v0, then saturating
4687note narrows (SQXTN/UQXTN, C7.2.311/C7.2.374) to the width, FMOV W (C7.2.130), then SXTB/SXTH for signed.
4688fn fp_toint ft ulong tt ulong rd ulong to void
4689if ne get xtarget 0 0
4690xftoint ft tt rd
4691ret
4692end
4693w = twid tt
4694sg = ge tt 6
4695if eq w 64
4696if sg
4697if eq ft 10
4698emit or 2654470144 rd
4699else
4700emit or 2658664448 rd
4701end
4702else
4703if eq ft 10
4704emit or 2654535680 rd
4705else
4706emit or 2658729984 rd
4707end
4708end
4709ret
4710end
4711if eq w 32
4712if sg
4713if eq ft 10
4714emit or 506986496 rd
4715else
4716emit or 511180800 rd
4717end
4718emit or or 2470476800 rd shl rd 5
4719else
4720if eq ft 10
4721emit or 507052032 rd
4722else
4723emit or 511246336 rd
4724end
4725end
4726ret
4727end
4728fp_narrow ft sg w
4729emit or 505806848 rd
4730if sg
4731if eq w 8
4732emit or or 2470452224 rd shl rd 5
4733else
4734emit or or 2470460416 rd shl rd 5
4735end
4736end
4737end
4738
4739note fp_narrow: convert v0 in place to a saturated integer of width w (8 or 16) in its low bits.
4740fn fp_narrow ft ulong sg bool w ulong to void
4741if eq ft 10
4742if sg
4743emit 1587656704
4744emit 1583433728
4745else
4746emit 2124527616
4747emit 2120304640
4748end
4749else
4750if sg
4751emit 1591851008
4752emit 1587628032
4753emit 1583433728
4754else
4755emit 2128721920
4756emit 2124498944
4757emit 2120304640
4758end
4759end
4760if eq w 8
4761if sg
4762emit 1579239424
4763else
4764emit 2116110336
4765end
4766end
4767end
4768
4769note ---- rotate (OM-2, spec 3): rotr 59 / rotl 60 of an unsigned uchar..ulong by the same type. uint and
4770note ulong with a number below the width: ROR (immediate) = EXTR Rd, Rn, Rn, #s (C6.2.347, C6.2.160);
4771note otherwise the shift amount check (trap 7) then RORV (C6.2.349); rotl by n is ror by w - n (a number)
4772note or by NEG n (C6.2.295) since RORV takes the amount modulo the width. uchar/ushort first replicate the
4773note value through W16 with ORR (shifted register, LSL, C6.2.302), rotate the 32-bit pattern, then canon.
4774fn rotrep ty ulong ra ulong to void
4775if eq ty 3
4776emit or or 704659472 shl ra 16 shl ra 5
4777ret
4778end
4779emit or or 704651280 shl ra 16 shl ra 5
4780emit 705708560
4781end
4782
4783fn rotimm ty ulong ra ulong s ulong rd ulong to void
4784if ne get xtarget 0 0
4785xrot ty ra s 1 rd
4786ret
4787end
4788if eq ty 5
4789emit or or or 2478833664 shl ra 16 shl s 10 or shl ra 5 rd
4790ret
4791end
4792if eq ty 4
4793emit or or or 327155712 shl ra 16 shl s 10 or shl ra 5 rd
4794ret
4795end
4796rotrep ty ra
4797emit or or 328204800 shl s 10 rd
4798canon rd ty
4799end
4800
4801fn rotreg ty ulong ra ulong rr ulong rd ulong to void
4802if ne get xtarget 0 0
4803xrot ty ra rr 0 rd
4804ret
4805end
4806if eq ty 5
4807emit or or 2596285440 shl rr 16 or shl ra 5 rd
4808ret
4809end
4810if eq ty 4
4811emit or or 448801792 shl rr 16 or shl ra 5 rd
4812ret
4813end
4814rotrep ty ra
4815emit or or 448802304 shl rr 16 rd
4816canon rd ty
4817end
4818
4819fn binrot op ulong dst ulong ix ulong to void
4820ty = get fr add ix 2
4821if or lt ty 2 gt ty 5
4822fail 7
4823end
4824ra = matreg get fr ix get fr add ix 1 ty
4825bk = get fr add ix 3
4826bn = get fr add ix 4
4827w = twid ty
4828rb = ulong 0
4829useimm = false
4830if and eq bk 3 lt bn w
4831useimm = true
4832rb = bn
4833else
4834rb = matreg bk bn get fr add ix 5
4835bo_schk ty rb
4836end
4837rd = destreg dst
4838if useimm
4839s = rb
4840if and eq op 60 ne s 0
4841s = sub w s
4842end
4843rotimm ty ra s rd
4844else
4845rr = rb
4846if eq op 60
4847rr = alloc
4848if ne get xtarget 0 0
4849xload 0 rb
4850xb 72 247 216
4851xstore 0 rr
4852else
4853emit or or 3405775840 shl rb 16 rr
4854end
4855end
4856rotreg ty ra rr rd
4857end
4858put rk 0 1
4859put rv 0 rd
4860put rt 0 ty
4861end
4862
4863note binop: op 33..48, destination (255 none), ix the fr slot of a (kind, number/register, type) then b.
4864note Result in rk/rv/rt. om4 BINOP. A float op reads its operands as v registers; with a request (fpo)
4865note the result stays in a twin (kind 9), else it moves to x (kind 1).
4866fn binop op ulong dst ulong ix ulong to void
4867if ne get fzone 0 0
4868if and eq get fr ix 3 eq get fr add ix 3 3
4869foldbin op get fr add ix 2 get fr add ix 1 get fr add ix 4
4870if ne get fok 0 0
4871put rk 0 3
4872put rv 0 get fval 0
4873put rt 0 get fty 0
4874ret
4875end
4876end
4877end
4878if or eq op 59 eq op 60
4879binrot op dst ix
4880ret
4881end
4882ty = get fr add ix 2
4883if ne isfloat ty 0
4884if ne get xtarget 0 0
4885xfbinop op dst ix ty
4886ret
4887end
4888o = get fpo 0
4889put fpo 0 0
4890va = fvop get fr ix get fr add ix 1 ty 0
4891vb = fvop get fr add ix 3 get fr add ix 4 ty 1
4892if ge op 43
4893fp_cmp op ty va vb
4894ret
4895end
4896if or or lt op 33 gt op 36 ge op 41
4897fail 7
4898end
4899if eq o 0
4900rd = destreg dst
4901fp_binop op ty va vb 2
4902fv2x ty 2 rd
4903put rk 0 1
4904put rv 0 rd
4905else
4906r = fvdst o
4907fp_binop op ty va vb vtw r
4908put rk 0 9
4909put rv 0 r
4910end
4911put rt 0 ty
4912ret
4913end
4914ra = matreg get fr ix get fr add ix 1 ty
4915if eq ty 13
4916if and ge op 38 le op 42
4917fail 7
4918end
4919end
4920imm = ulong 0
4921bk = get fr add ix 3
4922bn = get fr add ix 4
4923rb = ulong 0
4924useimm = false
4925if eq bk 3
4926if or eq op 41 eq op 42
4927if lt bn twid ty
4928useimm = true
4929end
4930else
4931if or lt op 35 ge op 43
4932if le bn 4095
4933useimm = true
4934end
4935end
4936end
4937end
4938if useimm
4939rb = bn
4940imm = ulong 1
4941else
4942rb = matreg bk bn get fr add ix 5
4943end
4944if ge op 43
4945bo_cmp op ty ra rb imm
4946ret
4947end
4948if eq ty 1
4949if gt sub op 38 2
4950fail 7
4951end
4952end
4953rd = destreg dst
4954if lt op 35
4955bo_add op ty ra rb imm rd
4956else
4957if eq op 35
4958bo_mul ty ra rb rd
4959else
4960if lt op 38
4961fl = ulong 0
4962if and eq bk 3 ne bn 0
4963fl = or fl 1
4964end
4965if and eq bk 3 ne bn 18446744073709551615
4966fl = or fl 2
4967end
4968bo_div op ty ra rb rd fl
4969else
4970if lt op 41
4971bo_log op ra rb rd
4972else
4973if eq op 41
4974bo_shl ty ra rb imm rd
4975else
4976bo_shr ty ra rb imm rd
4977end
4978end
4979end
4980end
4981end
4982put rk 0 1
4983put rv 0 rd
4984put rt 0 ty
4985end
4986
4987note ======== calls: registers saved around a call and loaded back ========
4988global svn ulong 1
4989global sva ulong 1
4990note save: n = registers in use (parameters + pool slots), A = 8 (n + 1) rounded up to 16: SUB SP, SP, #A
4991note (C6.2.457); the stack check; STR Xr, [SP, #8j] for each (C6.2.415), or STR Dj+16 (SIMD&FP, C7.2.331)
4992note for a slot living in its twin (vsl); STR X30, [SP, #8n]. Out: svn n, sva A. om4 SAVE.
4993fn save to void
4994if ne get xtarget 0 0
4995put svn 0 add get npar 0 get nslot 0
4996put sva 0 256
4997xsp 1 256
4998j = ulong 0
4999while lt j 18
5000xload 0 j
5001xmem 137 0 4 mul j 8
5002j = add j 1
5003end
5004note Reserve the callee's return address, frame pointer and slot area before CALL.
5005xcheck 272
5006ret
5007end
5008n = add get npar 0 get nslot 0
5009a = shl shr add n 2 1 4
5010put svn 0 n
5011put sva 0 a
5012spsub a
5013stkchk
5014j = ulong 0
5015while lt j n
5016if ne get vsl j 0
5017emit or or 4244636640 add j 16 shl j 10
5018else
5019emit or or 4177527776 poolc j shl j 10
5020end
5021j = add j 1
5022end
5023emit or 4177527806 shl n 10
5024end
5025
5026note loadarg: register t from an argument: kind 1, 5 or 9 a register v, read back from its save slot (LDR
5027note Xt, [SP, #8 slot], C6.2.216; a kind 9 slot holds the twin's D bits, the same bit pattern); kind 3, 6
5028note or 7 the number, address or float bits v. om4 LOADARG.
5029fn loadarg t ulong k ulong v ulong to void
5030if ne get xtarget 0 0
5031xarg t k v
5032ret
5033end
5034if or or eq k 3 eq k 6 eq k 7
5035matconst t v
5036ret
5037end
5038emit or or 4181722080 t shl regidx v 10
5039end
5040
5041note basearg: register t = the base of array name e (index + 1): its register from the save area, or its
5042note address. om4 BASEARG.
5043fn basearg t ulong e ulong to void
5044i = sub e 1
5045if ge get nm_kind i 4
5046mataddr t get nm_addr i
5047ret
5048end
5049loadarg t 1 ulong get nm_reg i
5050end
5051
5052note restore: d destination (255: a new temporary at pool slot `slot`), slot the pool slot before the
5053note arguments, n saved count, a save bytes, res 1 if there is a result in X0. MOV Xd, X0 (C6.2.282); LDR
5054note each register in use before the arguments except Xd (C6.2.216), or LDR Dj+16 (C7.2.191) instead for a
5055note slot living in its twin; LDR X30; ADD SP, SP, #A (C6.2.5). Returns Xd (255 when no result). om4
5056note RESTORE.
5057fn restore d ulong slot ulong n ulong a ulong res ulong to ulong
5058if ne get xtarget 0 0
5059ret xrestore d slot res
5060end
5061put nslot 0 slot
5062rd = ulong 255
5063if ne res 0
5064rd = d
5065if eq d 255
5066rd = alloc
5067end
5068if ne rd 0
5069emit or 2852127712 rd
5070end
5071end
5072m = add get npar 0 slot
5073j = ulong 0
5074while lt j m
5075r = poolc j
5076if ne get vsl j 0
5077emit or or 4248830944 add j 16 shl j 10
5078else
5079if ne r rd
5080emit or or 4181722080 r shl j 10
5081end
5082end
5083j = add j 1
5084end
5085emit or 4181722110 shl n 10
5086spadd a
5087ret rd
5088end
5089
5090note ca_bl: BL to function f (index + 1), C6.2.43
5091fn ca_bl f ulong to void
5092n = get ncalls 0
5093if ge n 524288
5094fail 14
5095end
5096put callsite n get oc 0
5097put callfrom n get curfn 0
5098put callto n f
5099put ncalls 0 add n 1
5100
5101if ne get xtarget 0 0
5102emitb 232
5103xword 0
5104else
5105emit or 2483027968 bdisp get oc 0 get fn_addr sub f 1 26
5106end
5107end
5108
5109note ======== element addresses and array arguments ========
5110global ea_b ulong 1
5111global ea_i ulong 1
5112global ea_k ulong 1
5113
5114note eaem: the load or store word base (LDOP / STOP register-offset form) of register rt at ea_b / ea_i.
5115note ea_i 255 (OM-2, a number index k = ea_k up to 4095): the unsigned-offset form instead, same size, V
5116note (bit 26, the SIMD&FP forms) and opc bits, imm12 = k scaled by the element size (LDR/STR (immediate),
5117note C6.2.216 and its family; C7.2.191/C7.2.331).
5118fn eaem wd ulong rg ulong to void
5119b = get ea_b 0
5120if eq get ea_i 0 255
5121emit or or or or and wd 3300917248 956301312 shl get ea_k 0 10 shl b 5 rg
5122ret
5123end
5124emit or or or wd rg shl b 5 shl get ea_i 0 16
5125end
5126note elemaddr: array name e (index + 1), index value (kind, number or register, type). A number index must
5127note be inside the array (error 21) and needs no check; a register index: CMP Xi, #len (C6.2.96) or CMP Xi,
5128note Xt (C6.2.97), B.HS trap 3 (unsigned, so a negative signed index traps too). Out: ea_b base register
5129note (the array's register, or a new one holding its address), ea_i index register. om4 ELEMADDR.
5130note OM-2 unsized array (unsz): every index, a number too, is checked against its count register nm_lr
5131note (CMP Xi, Xn; B.HS trap 3); none is eliminated (spec 4, 6).
5132fn elemaddr e ulong k ulong v ulong t ulong to void
5133if ne get xtarget 0 0
5134xelem e k v t
5135ret
5136end
5137if or or eq t 1 eq t 10 eq t 11
5138fail 7
5139end
5140i = sub e 1
5141ln = get nm_len i
5142x = v
5143if ne unsz i 0
5144x = matreg k v t
5145emit or or 3942645791 shl ulong get nm_lr i 16 shl x 5
5146trapc 2 3
5147else
5148if eq k 3
5149if ge v ln
5150fail 21
5151end
5152if and ne get om2 0 0 le v 4095
5153x = ulong 255
5154put ea_k 0 v
5155else
5156x = alloc
5157matconst x v
5158end
5159else
5160if le ln 4095
5161emit or or 4043309087 shl ln 10 shl v 5
5162else
5163y = alloc
5164matconst y ln
5165emit or or 3942645791 shl y 16 shl v 5
5166end
5167trapc 2 3
5168end
5169end
5170put ea_i 0 x
5171if ge get nm_kind i 4
5172b = alloc
5173matconst b get nm_addr i
5174put ea_b 0 b
5175else
5176put ea_b 0 ulong get nm_reg i
5177end
5178end
5179
5180note aa_word: the next word of an array argument must be a name of at most 8 bytes
5181fn aa_word to void
5182tok
5183k = get tk 0
5184if eq k 0
5185fail 4
5186end
5187if ne k 1
5188fail 16
5189end
5190if gt get tl 0 8
5191fail 13
5192end
5193end
5194
5195note arrarg: an array argument for parameter pi of function f (index + 1): [view | edit] NAME. Its type and
5196note length must equal the parameter's; an edit parameter needs a writable array (local or edit, error 20;
5197note a view passed to it is error 20, OMEGESE.md 9.4); a global is never passed (error 19). Out: rk 5
5198note (register rv) or 6 (address rv, a text), rt the name entry. om4 ARRARG.
5199note OM-2: an unsized parameter (length 0) takes an array of any length (erratum E2), whose count goes to
5200note ul_k / ul_v at depth * 6 + pi: its count register, or its length as a number. `part` is read by arrpart.
5201fn arrarg f ulong pi ulong to void
5202md = ulong 0
5203aa_word
5204w = get tw 0
5205c = kw w
5206if ne c 0
5207if eq c 193
5208arrpart f pi 0
5209ret
5210end
5211if or lt c 57 gt c 58
5212fail 16
5213end
5214md = c
5215aa_word
5216w = get tw 0
5217if eq kw w 193
5218arrpart f pi md
5219ret
5220end
5221end
5222e = findname w
5223if eq e 0
5224fail 3
5225end
5226i = sub e 1
5227k = ulong get nm_kind i
5228if eq k 0
5229fail 7
5230end
5231if ge k 6
5232fail 7
5233end
5234if eq k 4
5235fail 19
5236end
5237p = add mul sub f 1 6 pi
5238if eq get fn_pk p 3
5239if eq md 57
5240fail 20
5241end
5242if and ne k 1 ne k 3
5243fail 20
5244end
5245else
5246if eq md 58
5247fail 7
5248end
5249end
5250if ne get nm_type i get fn_pt p
5251fail 7
5252end
5253ux = add mul get depth 0 6 pi
5254if eq get fn_pl p 0
5255if ne unsz i 0
5256put ul_k ux 1
5257put ul_v ux ulong get nm_lr i
5258else
5259put ul_k ux 3
5260put ul_v ux get nm_len i
5261end
5262else
5263if ne get nm_len i get fn_pl p
5264fail 7
5265end
5266end
5267put rt 0 e
5268if eq k 5
5269put rk 0 6
5270put rv 0 get nm_addr i
5271else
5272put rk 0 5
5273put rv 0 ulong get nm_reg i
5274end
5275end
5276
5277note arrpart (OM-2, spec 6): `part A o k` as argument pi of function f (index + 1); md 57 / 58 when `view` /
5278note `edit` was written before it. A is a name (a global is error 19, a fresh array 27); o, then k, are
5279note ulong values. Checks, in this order: o <= len A (CMP Xo, Xlen; B.HI trap 3), then k <= len A - o (SUB,
5280note CMP, B.HI trap 3); the wrapped o + k is never formed. The part is a view of A + o * size with count k:
5281note an edit when A is writable (local or edit) and no `view` was written, else a view. It goes only to an
5282note unsized parameter (error 7). Out as arrarg; rt is A, so for error 19 the part counts as its base.
5283fn arrpart f ulong pi ulong md ulong to void
5284p = add mul sub f 1 6 pi
5285e = arrname
5286i = sub e 1
5287k = ulong get nm_kind i
5288if ge k 6
5289fail 7
5290end
5291if eq k 4
5292fail 19
5293end
5294fz_arg e
5295if eq get fn_pk p 3
5296if eq md 57
5297fail 20
5298end
5299if and ne k 1 ne k 3
5300fail 20
5301end
5302else
5303if eq md 58
5304fail 7
5305end
5306end
5307if ne ulong get nm_type i ulong get fn_pt p
5308fail 7
5309end
5310if ne get fn_pl p 0
5311fail 7
5312end
5313rdval 0 5 255 0
5314k = get rt 0
5315if and ne k 5 ne k 13
5316fail 7
5317end
5318ro = matreg get rk 0 get rv 0 k
5319rdval 0 5 255 0
5320k = get rt 0
5321if and ne k 5 ne k 13
5322fail 7
5323end
5324rn = matreg get rk 0 get rv 0 k
5325d = partck i ro rn
5326ux = add mul get depth 0 6 pi
5327put ul_k ux 1
5328put ul_v ux rn
5329put rt 0 e
5330put rk 0 5
5331put rv 0 d
5332end
5333
5334note partck: the two checks of part (name index i, o in ro, k in rn), then the part's base register
5335fn partck i ulong ro ulong rn ulong to ulong
5336rl = ulong get nm_lr i
5337if eq unsz i 0
5338rl = alloc
5339matconst rl get nm_len i
5340end
5341if ne get xtarget 0 0
5342xcmp ro rl 0
5343xtrap 8 3
5344d = alloc
5345xarith 34 5 rl ro 0 d
5346xcmp rn d 0
5347xtrap 8 3
5348b = ulong get nm_reg i
5349if eq get nm_kind i 5
5350b = alloc
5351mataddr b get nm_addr i
5352end
5353xload 0 ro
5354xb 72 193 224
5355emitb uchar tlog ulong get nm_type i
5356xload 1 b
5357xrr 1 0 1
5358xstore 0 d
5359ret d
5360end
5361emit or or 3942645791 shl rl 16 shl ro 5
5362trapc 8 3
5363d = alloc
5364e3 3405774848 d rl ro
5365emit or or 3942645791 shl d 16 shl rn 5
5366trapc 8 3
5367b = ulong get nm_reg i
5368if eq get nm_kind i 5
5369b = alloc
5370matconst b get nm_addr i
5371end
5372emit or or or or 2332033024 shl ro 16 shl tlog ulong get nm_type i 10 shl b 5 d
5373ret d
5374end
5375
5376note ======== the parts of the value reader that read no further value ========
5377fn vdec to void
5378if and ne get cpure 0 0 ge get rt 0 10
5379fail 30
5380end
5381put depth 0 sub get depth 0 1
5382end
5383
5384note vdone: a value's result, and one nesting level less (om4 VT_RET)
5385fn vdone k ulong v ulong t ulong to void
5386put rk 0 k
5387put rv 0 v
5388put rt 0 t
5389vdec
5390end
5391
5392note fwreq: hand this depth's fpw request to the part that makes the value (fpo)
5393fn fwreq to void
5394put fpo 0 get fwv get depth 0
5395end
5396
5397note vt_un: neg / not of the value in rk/rv/rt (a bare number already refused). neg: NEG Xd, Xm (SUB from
5398note XZR, C6.2.458; the 32-bit form for u32), i64 NEGS + B.VS trap 1 (C6.2.465), then the wrap or the
5399note sign check; not: bool EOR Xd, Xn, #1 (C6.2.155), else MVN (ORN from XZR, C6.2.300), then the wrap.
5400note om4 VU1 .. VU_E. Float neg: FNEG (C7.2.144) of the operand's v register; with a request (fpo) the
5401note result stays in a twin (kind 9).
5402fn vt_un c ulong dst ulong to void
5403o = get fpo 0
5404put fpo 0 0
5405if ne get fzone 0 0
5406if eq get rk 0 3
5407foldun c get rt 0 get rv 0
5408if ne get fok 0 0
5409put rv 0 get fval 0
5410ret
5411end
5412end
5413end
5414if ne get xtarget 0 0
5415xunary c dst
5416ret
5417end
5418ty = get rt 0
5419if or eq ty 10 eq ty 11
5420if eq c 50
5421fail 7
5422end
5423va = fvop get rk 0 get rv 0 ty 0
5424if eq o 0
5425rd = destreg dst
5426fp_neg ty va 1
5427fv2x ty 1 rd
5428put rk 0 1
5429put rv 0 rd
5430else
5431r = fvdst o
5432fp_neg ty va vtw r
5433put rk 0 9
5434put rv 0 r
5435end
5436put rt 0 ty
5437ret
5438end
5439ra = matreg get rk 0 get rv 0 ty
5440rd = destreg dst
5441if eq c 50
5442if eq ty 13
5443fail 7
5444end
5445if eq ty 1
5446e3 3527409664 rd ra 0
5447else
5448if eq ty 4
5449e3 706741216 rd 0 ra
5450else
5451e3 2854224864 rd 0 ra
5452if lt ty 6
5453canon rd ty
5454end
5455end
5456end
5457else
5458if or eq ty 1 eq ty 13
5459fail 7
5460end
5461if eq ty 9
5462e3 3942646752 rd 0 ra
5463trapc 6 1
5464else
5465if eq ty 4
5466e3 1258292192 rd 0 ra
5467else
5468e3 3405775840 rd 0 ra
5469if lt ty 6
5470canon rd ty
5471else
5472chkext rd ty 1
5473end
5474end
5475end
5476end
5477put rk 0 1
5478put rv 0 rd
5479put rt 0 ty
5480end
5481
5482note vt_conv: the value in rk/rv/rt converted to type tt (neither bool, error 9). Out of range when any bit
5483note 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
5484note signed type: the sign-extension check, trap 5. om4 VTT1 .. VC_OK. To a float with a request (fpo): the
5485note conversion is made in a twin (kind 9); the same float type is left as it is.
5486fn vt_conv tt ulong to void
5487if ne get xtarget 0 0
5488xconv tt
5489ret
5490end
5491o = get fpo 0
5492put fpo 0 0
5493ft = get rt 0
5494if or or or eq ft 10 eq ft 11 eq tt 10 eq tt 11
5495if or eq ft 13 eq tt 13
5496fail 7
5497end
5498if eq ft 1
5499fail 9
5500end
5501if eq tt 1
5502fail 9
5503end
5504if and ne o 0 ne isfloat tt 0
5505if eq ft tt
5506ret
5507end
5508ra = matreg get rk 0 get rv 0 ft
5509r = fvdst o
5510fp_tov ft tt ra vtw r
5511put rk 0 9
5512put rv 0 r
5513put rt 0 tt
5514ret
5515end
5516ra = matreg get rk 0 get rv 0 ft
5517rd = fp_conv ft tt ra
5518put rk 0 1
5519put rv 0 rd
5520put rt 0 tt
5521ret
5522end
5523ra = matreg get rk 0 get rv 0 ft
5524if eq ft 1
5525fail 9
5526end
5527if eq tt 1
5528fail 9
5529end
5530wf = twid ft
5531wt = twid tt
5532s = ulong 99
5533sgf = and ge ft 6 le ft 9
5534sgt = and ge tt 6 le tt 9
5535if eq sgf 0
5536if ne sgt 0
5537if ge wf wt
5538s = sub wt 1
5539end
5540else
5541if gt wf wt
5542s = wt
5543end
5544end
5545else
5546if eq sgt 0
5547s = wt
5548if gt s 63
5549s = ulong 63
5550end
5551else
5552if gt wf wt
5553chkext ra tt 5
5554end
5555end
5556end
5557if ne s 99
5558t = alloc
5559emit or or or 3544251392 t shl s 16 shl ra 5
5560trapsite or 3036676096 t 5
5561end
5562put rk 0 1
5563put rv 0 ra
5564put rt 0 tt
5565end
5566
5567note vt_get: get A i with the index value in rk/rv/rt: the element address, then the load of the element
5568note type, register offset (LDOP table). om4 VT_GET. A float element with a request (fpo) loads straight
5569note into a twin: the same word with V (bit 26), LDR St/Dt (C7.2.192) (kind 9).
5570fn vt_get e ulong dst ulong to void
5571if ne get xtarget 0 0
5572elemaddr e get rk 0 get rv 0 get rt 0
5573st_ld get ea_b 0 get ea_i 0 ulong get nm_type sub e 1 dst
5574ret
5575end
5576o = get fpo 0
5577put fpo 0 0
5578elemaddr e get rk 0 get rv 0 get rt 0
5579ty = ulong get nm_type sub e 1
5580if and ne o 0 ne isfloat ty 0
5581r = fvdst o
5582eaem or ldop ty 67108864 vtw r
5583put rk 0 9
5584put rv 0 r
5585put rt 0 ty
5586ret
5587end
5588rd = destreg dst
5589eaem ldop ty rd
5590put rk 0 1
5591put rv 0 rd
5592put rt 0 ty
5593end
5594
5595note 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,
5596note SUB C6.2.458). om4 VT_SYS.
5597fn vs_chk e ulong ro ulong rn ulong to void
5598if ne get xtarget 0 0
5599if ne get nm_type sub e 1 2
5600fail 7
5601end
5602xload 1 rn
5603if ne unsz sub e 1 0
5604xload 0 ulong get nm_lr sub e 1
5605else
5606ximm 0 get nm_len sub e 1
5607end
5608xrr 57 1 0
5609xtrap 8 3
5610xrr 41 0 1
5611xload 1 ro
5612xrr 57 1 0
5613xtrap 8 3
5614ret
5615end
5616if ne get nm_type sub e 1 2
5617fail 7
5618end
5619t = alloc
5620l = ulong get nm_lr sub e 1
5621if eq unsz sub e 1 0
5622l = t
5623matconst t get nm_len sub e 1
5624end
5625emit or or 3942645791 shl rn 5 shl l 16
5626trapc 8 3
5627e3 3405774848 t l rn
5628emit or or 3942645791 shl ro 5 shl t 16
5629trapc 8 3
5630end
5631
5632note vs_call: save the registers in use, X0 = fd, X1 = A + off (ADD through X16, C6.2.6), X2 = n, X8 = 63
5633note (read) or 64 (write) (MOVZ C6.2.285), SVC #0 (C6.2.466). om4 VT_SYS.
5634fn vs_call c ulong fk ulong fv ulong e ulong ro ulong rn ulong to void
5635if ne get xtarget 0 0
5636xio c fk fv e ro rn
5637ret
5638end
5639save
5640loadarg 0 fk fv
5641basearg 1 e
5642loadarg 16 1 ro
5643emit 2333081633
5644loadarg 2 1 rn
5645emit or 3531603976 shl add c 10 5
5646emit 3556769793
5647end
5648
5649note ca_scal: a scalar argument just read (rk/rv/rt) for parameter p: flags into a register; its type
5650note must be the parameter's; stored at fr[q..q+2]. om4 CA1 .. CA_PUT.
5651fn ca_scal p ulong q ulong to void
5652if eq get rk 0 4
5653put rv 0 matreg 4 get rv 0 get rt 0
5654put rk 0 1
5655put rt 0 1
5656end
5657if ne get rt 0 ulong get fn_pt p
5658fail 7
5659end
5660put fr q get rk 0
5661put fr add q 1 get rv 0
5662put fr add q 2 0
5663end
5664
5665note ca_arr: an array argument i of function f, call frame cb: one array passed twice is error 19.
5666note om4 CA_ARR .. CA_PUT.
5667fn ca_arr f ulong i ulong cb ulong to void
5668arrarg f i
5669j = ulong 0
5670while lt j i
5671if eq get fr add add cb mul j 3 2 get rt 0
5672fail 19
5673end
5674j = add j 1
5675end
5676q = add cb mul i 3
5677put fr q get rk 0
5678put fr add q 1 get rv 0
5679put fr add q 2 get rt 0
5680end
5681
5682note ca_end: all arguments read: save, the arguments into X0.., BL, restore. Frame cb: 3 per argument,
5683note then cb+18 the parameter count, cb+19 the pool slot before the arguments. om4 CA_DONE .. CA5.
5684fn ca_end f ulong dst ulong cb ulong to void
5685save
5686np = get fr add cb 18
5687i = ulong 0
5688while lt i np
5689q = add cb mul i 3
5690s = pslot sub f 1 i
5691loadarg s get fr q get fr add q 1
5692if eq pwid add mul sub f 1 6 i 2
5693ux = add mul div cb 32 6 i
5694loadarg add s 1 get ul_k ux get ul_v ux
5695end
5696i = add i 1
5697end
5698ca_bl f
5699hr = ulong 1
5700if eq get fn_ret sub f 1 12
5701hr = ulong 0
5702end
5703n = get svn 0
5704a = get sva 0
5705sl = get fr add cb 19
5706r = restore dst sl n a hr
5707put rk 0 1
5708put rv 0 r
5709put rt 0 ulong get fn_ret sub f 1
5710end
5711
5712note vs_fd, vs_arr, vs_off, vs_end: the steps of read / write between the values it reads, with the fd
5713note (kind, value), the offset register and the pool slot before kept in fr[x+8..x+11] (om4's VT_SYS
5714note frame slots 4, 5, 7 and 14)
5715fn vs_fd x ulong to void
5716if ne get rt 0 8
5717fail 7
5718end
5719put fr add x 9 get rk 0
5720put fr add x 10 get rv 0
5721end
5722
5723fn vs_arr c ulong e ulong to void
5724fz_arg e
5725if ge get nm_kind sub e 1 6
5726fail 7
5727end
5728if eq c 53
5729k = get nm_kind sub e 1
5730if or eq k 2 eq k 5
5731fail 20
5732end
5733end
5734end
5735
5736fn vs_off x ulong to void
5737if ne get rt 0 5
5738fail 7
5739end
5740put fr add x 11 matreg get rk 0 get rv 0 get rt 0
5741end
5742
5743fn vs_end c ulong e ulong dst ulong x ulong to void
5744if ne get rt 0 5
5745fail 7
5746end
5747rn = matreg get rk 0 get rv 0 get rt 0
5748ro = get fr add x 11
5749vs_chk e ro rn
5750fk = get fr add x 9
5751fv = get fr add x 10
5752vs_call c fk fv e ro rn
5753sl = get fr add x 8
5754n = get svn 0
5755a = get sva 0
5756r = restore dst sl n a 1
5757vdone 1 r 9
5758end
5759
5760note cnt_word: OM-2 `count A off n v`, a ulong: how many of the n uchar elements of A from off equal v.
5761note A is an array name (a local, global, text, or a view or edit parameter, sized or unsized); off and n
5762note are ulong, v is uchar. The four values are read left to right; then the slice is checked exactly as
5763note read and write check theirs (vs_chk: a non-uchar A is error 7; n <= len A and off <= len A - n, else
5764note trap 3), and only then is any element read, so an out-of-bounds slice traps 3 whatever v is and an
5765note in-bounds one never traps. A tracked fresh array (error 27, fz_arg), a struct or layout (error 7) and
5766note a contract clause (error 30, cp_token) are refused; an IR unit is never OM-2, so count is no word there
5767note (error 3), and the irmode guard (error 9, as dit) only keeps it so. Code (AArch64): P = A + off
5768note (ADD, C6.2.6), M = n (MOV = ORR), V0 = v in all 16 byte lanes (DUP), S = 0 (MOVZ, C6.2.285); then
5769note while M >= 16 (CMP, C6.2.96; B.LO, C6.2.34): a block of K = min(M >> 4, 255) chunks (LSR = UBFM,
5770note C6.2.487; CMP; B.LS; MOVZ X16, #255; V1 = 0 by MOVI, C7.2.247), each chunk LD1 {V2.16B}, [P], #16
5771note (post-index), CMEQ V2, V2, V0 (0xff where equal), SUB V1.16B, V1, V2 (+1 per equal lane; 255 chunks
5772note keep each lane <= 255), SUB M, M, #16, SUBS X16, X16, #1, B.NE; then UADDLV H2, V1.16B (the 16 lanes,
5773note at most 4080), UMOV W16, V2.H[0], ADD S, S, X16, B back (C6.2.35). The last M < 16 bytes one at a
5774note time: CBZ M (C6.2.78), UMOV WK, V0.B[0], LDRB W16, [P], #1 (post-index), CMP W16, WK, CINC S, S, EQ
5775note (CSINC), SUBS M, M, #1, B.NE. Every word is a fixed part from the instruction's Arm ARM encoding ORed
5776note with register fields; each fixed part was checked against llvm-mc 18 at two or more register
5777note choices. P, M, S and K are new temporaries, so no name's register changes; X16 is scratch;
5778note V0-V2 carry nothing between operations (see scrub). The result moves to the destination after the
5779note temporaries are given back.
5780fn cnt_word dst ulong to void
5781if ne get irmode 0 0
5782fail 9
5783end
5784x = mul get depth 0 32
5785sl = get nslot 0
5786put fr add x 8 sl
5787e = arrname
5788fz_arg e
5789if ge get nm_kind sub e 1 6
5790fail 7
5791end
5792rdval 0 5 255 0
5793vs_off x
5794rdval 0 5 255 0
5795if ne get rt 0 5
5796fail 7
5797end
5798put fr add x 12 matreg get rk 0 get rv 0 get rt 0
5799rdval 0 2 255 0
5800if ne get rt 0 2
5801fail 7
5802end
5803v = matreg get rk 0 get rv 0 get rt 0
5804ro = get fr add x 11
5805rn = get fr add x 12
5806vs_chk e ro rn
5807if ne get xtarget 0 0
5808xcount e ro rn v
5809put nslot 0 sl
5810d = destreg dst
5811xrr 137 0 2
5812xstore 0 d
5813vdone 1 d 5
5814ret
5815end
5816s = cnt_a64 e ro rn v
5817put nslot 0 sl
5818d = destreg dst
5819if ne d s
5820emit or or 2852127712 d shl s 16
5821end
5822vdone 1 d 5
5823end
5824
5825note cnt_a64: the AArch64 loop of cnt_word (see there), after vs_chk; gives S, the register of the count
5826fn cnt_a64 e ulong ro ulong rn ulong v ulong to ulong
5827i = sub e 1
5828p = alloc
5829if ge get nm_kind i 4
5830matconst p get nm_addr i
5831e3 2332033024 p p ro
5832else
5833e3 2332033024 p ulong get nm_reg i ro
5834end
5835m = alloc
5836emit or or 2852127712 m shl rn 16
5837emit or 1308691456 shl v 5
5838s = alloc
5839emit or 3531603968 s
5840k = alloc
5841emit or 4043325471 shl m 5
5842emit 1409286659
5843emit or 3544513552 shl m 5
5844emit 4043570719
5845emit 1409286217
5846emit 3531612144
5847emit 1862329345
5848emit or 1289711618 shl p 5
5849emit 1847626818
5850emit 1847755809
5851emit or or 3506454528 m shl m 5
5852emit 4043310608
5853emit 1426063201
5854emit 1848653858
5855emit 235027536
5856emit or or 2333081600 s shl s 5
5857emit 402653168
5858emit or 3019899104 m
5859emit or 234961920 k
5860emit or 943723536 shl p 5
5861emit or 1795162655 shl k 16
5862emit or or or 2592084992 s shl s 5 shl s 16
5863emit or or 4043310080 m shl m 5
5864emit 1426063233
5865ret s
5866end
5867
5868note Native Linux ARM64 host operations. Encoding forms are those in seed/rung2/table.s2.
5869note Syscall numbers and layouts: Linux asm-generic/unistd.h, fcntl.h and time.h.
5870note No raw address or arbitrary syscall is exposed to an Omega program.
5871fn hargc c ulong to ulong
5872if eq c 107
5873ret 0
5874end
5875if eq c 109
5876ret 0
5877end
5878if eq c 129
5879ret 0
5880end
5881if eq c 145
5882ret 0
5883end
5884if eq c 146
5885ret 0
5886end
5887if eq c 147
5888ret 0
5889end
5890if eq c 148
5891ret 0
5892end
5893if eq c 154
5894ret 0
5895end
5896if eq c 158
5897ret 0
5898end
5899if eq c 100
5900ret 1
5901end
5902if eq c 101
5903ret 1
5904end
5905if eq c 153
5906ret 1
5907end
5908if eq c 155
5909ret 1
5910end
5911if eq c 156
5912ret 1
5913end
5914if eq c 159
5915ret 1
5916end
5917if eq c 163
5918ret 1
5919end
5920if eq c 164
5921ret 1
5922end
5923if eq c 102
5924ret 2
5925end
5926if eq c 105
5927ret 2
5928end
5929if eq c 106
5930ret 2
5931end
5932if eq c 127
5933ret 2
5934end
5935if eq c 139
5936ret 2
5937end
5938if eq c 160
5939ret 2
5940end
5941if eq c 165
5942ret 2
5943end
5944if eq c 197
5945ret 2
5946end
5947ret 3
5948end
5949fn hsysno c ulong to ulong
5950if eq c 127
5951ret 129
5952end
5953if eq c 129
5954ret 172
5955end
5956if eq c 100
5957ret 57
5958end
5959if eq c 101
5960ret 82
5961end
5962if eq c 102
5963ret 52
5964end
5965if eq c 103
5966ret 62
5967end
5968if eq c 104
5969ret 198
5970end
5971if eq c 139
5972ret 46
5973end
5974if eq c 145
5975ret 174
5976end
5977if eq c 146
5978ret 175
5979end
5980if eq c 147
5981ret 176
5982end
5983if eq c 148
5984ret 177
5985end
5986if eq c 144
5987ret 48
5988end
5989if eq c 153
5990ret 50
5991end
5992if eq c 154
5993ret 173
5994end
5995if eq c 155
5996ret 166
5997end
5998if eq c 156
5999ret 83
6000end
6001if eq c 157
6002ret 279
6003end
6004if eq c 158
6005ret 157
6006end
6007if eq c 159
6008ret 155
6009end
6010if eq c 160
6011ret 154
6012end
6013if eq c 163
6014ret 156
6015end
6016if eq c 164
6017ret 267
6018end
6019if eq c 165
6020ret 210
6021end
6022if eq c 197
6023ret 32
6024end
6025if eq c 105
6026ret 201
6027end
6028if eq c 106
6029ret 242
6030end
6031if eq c 107
6032ret 220
6033end
6034ret 24
6035end
6036fn harg x ulong i ulong ty ulong to void
6037rdval 0 ty 255 0
6038if ne get rt 0 ty
6039fail 7
6040end
6041q = add add x 8 mul i 2
6042put fr q get rk 0
6043put fr add q 1 get rv 0
6044end
6045fn hload x ulong i ulong reg ulong to void
6046q = add add x 8 mul i 2
6047loadarg reg get fr q get fr add q 1
6048end
6049fn hsys n ulong to void
6050matconst 8 n
6051emit 3556769793
6052end
6053fn hreturn dst ulong x ulong to void
6054sl = get fr add x 7
6055n = get svn 0
6056a = get sva 0
6057r = restore dst sl n a 1
6058vdone 1 r 9
6059end
6060fn hscalar c ulong dst ulong x ulong to void
6061np = hargc c
6062for i ulong np
6063ty = ulong 8
6064if or and eq c 103 eq i 1 and eq c 139 eq i 1
6065ty = ulong 9
6066end
6067harg x i ty
6068end
6069note VFIO descriptor-kind state exists only in standalone programs, never in OM-2 unit contexts.
6070if and ne get progmode 0 0 or eq c 100 eq c 108
6071if eq get fdtab 0 0
6072fdpool = get poolb 0
6073if gt fdpool 1073739776
6074fail 23
6075end
6076put fdtab 0 add 276824064 fdpool
6077put poolb 0 add fdpool 2048
6078end
6079end
6080save
6081if and ne get progmode 0 0 eq c 100
6082hload x 0 10
6083fdforget
6084end
6085for i ulong np
6086hload x i i
6087end
6088if eq c 106
6089hload x 1 3
6090matconst 1 0
6091matconst 2 0
6092end
6093if eq c 107
6094matconst 0 17
6095for i ulong 5
6096matconst add i 1 0
6097end
6098end
6099hsys hsysno c
6100if and ne get progmode 0 0 eq c 108
6101mimmed 4043309056 31 0 0
6102baddup = mbranch 11
6103hload x 1 10
6104fdforget
6105patch19 baddup get oc 0
6106end
6107hreturn dst x
6108end
6109fn hclock c ulong dst ulong x ulong to void
6110save
6111spsub 16
6112stkchk
6113matconst 0 1
6114if eq c 130
6115matconst 0 0
6116end
6117emit 2432697313
6118hsys 113
6119emit 4043309087
6120bad = get oc 0
6121emit 1409286155
6122emit 4181722081
6123emit 4181723106
6124note Reject overflow before seconds are multiplied; clock result is a signed nanosecond count.
6125matconst 3 9223372036
6126e3 3942645760 31 1 3
6127trapc 8 1
6128matconst 3 1000000000
6129e3 2600500224 1 1 3
6130e3 2868903936 0 1 2
6131trapc 6 1
6132patch19 bad get oc 0
6133spadd 16
6134hreturn dst x
6135end
6136fn harray x ulong writable ulong to ulong
6137e = arrname
6138if ne get nm_type sub e 1 2
6139fail 7
6140end
6141if ne writable 0
6142vs_arr 53 e
6143end
6144fz_arg e
6145put fr add x 20 e
6146harg x 0 5
6147harg x 1 5
6148ro = matreg get fr add x 8 get fr add x 9 5
6149rn = matreg get fr add x 10 get fr add x 11 5
6150put fr add x 21 ro
6151put fr add x 22 rn
6152vs_chk e ro rn
6153ret e
6154end
6155note Copy a checked path to a private 4096-byte stack buffer. Reject embedded zero bytes.
6156note Public paths remain length-delimited; only the kernel-facing private copy has a terminator.
6157fn hpathcp x ulong to void
6158e = get fr add x 20
6159save
6160basearg 4 e
6161loadarg 16 1 get fr add x 21
6162e3 2332033024 4 4 16
6163loadarg 5 1 get fr add x 22
6164matconst 6 4095
6165e3 3942645760 31 5 6
6166trapc 8 3
6167end
6168fn hcopy to void
6169spsub 4096
6170stkchk
6171emit 2432697313
6172head = get oc 0
6173emit 4043309247
6174done = get oc 0
6175emit 1409286144
6176emit 960495750
6177emit 4043309279
6178trapc 0 3
6179emit 956301350
6180emit 2432697476
6181emit 2432697377
6182emit 3506439333
6183back head
6184patch19 done get oc 0
6185emit 956301375
6186emit 2432697313
6187matconst 0 18446744073709551516
6188end
6189fn hpath c ulong dst ulong x ulong to void
6190e = harray x 0
6191if or eq c 110 eq c 151
6192harg x 2 8
6193harg x 3 8
6194end
6195if or or or eq c 112 eq c 143 eq c 144 eq c 157
6196harg x 2 8
6197end
6198if eq c 150
6199harg x 2 9
6200end
6201hpathcp x
6202if or eq c 110 eq c 151
6203hload x 2 2
6204hload x 3 3
6205end
6206if eq c 111
6207matconst 2 0
6208end
6209if eq c 126
6210matconst 2 512
6211end
6212if eq c 112
6213hload x 2 2
6214end
6215if or eq c 143 eq c 144
6216hload x 2 2
6217matconst 3 0
6218end
6219if eq c 157
6220hload x 2 2
6221end
6222if eq c 150
6223hload x 2 2
6224end
6225hcopy
6226nr = ulong 56
6227if or eq c 111 eq c 126
6228nr = ulong 35
6229end
6230if eq c 112
6231nr = ulong 34
6232end
6233if eq c 143
6234nr = ulong 53
6235end
6236if eq c 144
6237nr = ulong 48
6238end
6239if eq c 157
6240nr = ulong 279
6241end
6242if eq c 150
6243nr = ulong 45
6244end
6245if eq c 151
6246nr = ulong 54
6247end
6248if eq c 138
6249nr = ulong 49
6250note hcopy leaves AT_FDCWD in X0 and the path in X1; chdir takes the path in X0.
6251e3 2852126720 0 31 1
6252end
6253if eq c 150
6254note truncate takes the path in X0 and the length in X1.
6255e3 2852126720 0 31 1
6256e3 2852126720 1 31 2
6257end
6258if eq c 151
6259note fchownat(dirfd, path, uid, gid, 0): X2/X3 are the ids, X4 is set after the copy.
6260matconst 4 0
6261end
6262if eq c 157
6263note memfd_create(name, flags): X0 = name, X1 = flags.
6264e3 2852126720 0 31 1
6265e3 2852126720 1 31 2
6266end
6267hsys nr
6268spadd 4096
6269hreturn dst x
6270end
6271fn hbuffer c ulong dst ulong x ulong to void
6272harg x 4 8
6273wr = ulong 0
6274if or or or or or eq c 113 eq c 116 eq c 117 eq c 118 eq c 131 eq c 224
6275wr = ulong 1
6276end
6277e = harray x wr
6278if or or eq c 116 eq c 118 eq c 119
6279harg x 2 8
6280end
6281if or eq c 116 eq c 117
6282need = ulong 4
6283if eq c 117
6284need = ulong 128
6285end
6286tmp = alloc
6287matconst tmp need
6288e3 3942645760 31 get fr add x 22 tmp
6289trapc 3 3
6290end
6291save
6292hload x 4 0
6293basearg 1 e
6294loadarg 16 1 get fr add x 21
6295e3 2332033024 1 1 16
6296loadarg 2 1 get fr add x 22
6297nr = ulong 61
6298if eq c 114
6299nr = ulong 203
6300end
6301if eq c 115
6302nr = ulong 200
6303end
6304if eq c 116
6305nr = ulong 260
6306hload x 2 2
6307matconst 3 0
6308end
6309if eq c 117
6310nr = ulong 80
6311end
6312if or eq c 118 eq c 119
6313nr = ulong 207
6314hload x 2 3
6315matconst 4 0
6316matconst 5 0
6317if eq c 119
6318nr = ulong 206
6319matconst 16 16384
6320e3 2852126720 3 3 16
6321end
6322end
6323
6324if or eq c 131 eq c 224
6325note Socket-name queries use a private 32-bit socklen and return the full length.
6326spsub 16
6327stkchk
6328emit 3103785954
6329emit 2432697314
6330if eq c 224
6331hsys 205
6332else
6333hsys 204
6334end
6335emit 4043309087
6336failed = get oc 0
6337emit 1409286155
6338emit 3107980256
6339patch19 failed get oc 0
6340spadd 16
6341else
6342hsys nr
6343end
6344hreturn dst x
6345end
6346fn hargs c ulong dst ulong x ulong to void
6347if eq c 121
6348save
6349emit 4181721952
6350hreturn dst x
6351ret
6352end
6353if eq c 136
6354note envcnt: walk the environment vector X27 + 8*(argc+2) (after argv's own null word) until the
6355note terminating null.
6356save
6357emit 4181721953
6358emit 2432698401
6359bo_lsl 1 3 1
6360e3 2332033024 0 27 1
6361matconst 2 0
6362lb = get oc 0
6363emit 4181721091
6364brend = get oc 0
6365emit 3019898883
6366emit 2432697410
6367emit 2432704512
6368back lb
6369patch19 brend get oc 0
6370emit 2852258784
6371hreturn dst x
6372ret
6373end
6374harg x 4 5
6375e = harray x 1
6376save
6377hload x 4 0
6378if eq c 137
6379note envread: envp[index] lives at X27 + 8*(argc+2+index); scan the entries before it so an
6380note index at or past the count traps 3 even when the vector is sparse or empty.
6381emit 2852127714
6382emit 4181721953
6383emit 2432698401
6384e3 2852126720 0 31 1
6385matconst 16 8
6386emit 2601548800
6387e3 2332033024 3 27 0
6388lb = get oc 0
6389brend = get oc 0
6390emit 3019898882
6391emit 4181721188
6392emit 4043309215
6393trapc 0 3
6394emit 2432704611
6395emit 3506439234
6396back lb
6397patch19 brend get oc 0
6398emit 4181721187
6399emit 4043309183
6400trapc 0 3
6401else
6402emit 4181721953
6403e3 3942645760 31 0 1
6404trapc 2 3
6405emit 2432697344
6406matconst 16 8
6407e3 2600500224 0 0 16
6408e3 2332033024 3 27 0
6409emit 4181721187
6410end
6411e3 2852126720 4 31 3
6412matconst 5 0
6413scan = get oc 0
6414emit 960495750
6415emit 4043309279
6416done = get oc 0
6417emit 1409286144
6418emit 2432697476
6419emit 2432697509
6420matconst 16 1048576
6421e3 3942645760 31 5 16
6422trapc 2 3
6423back scan
6424patch19 done get oc 0
6425loadarg 2 1 get fr add x 22
6426e3 3942645760 31 5 2
6427small = get oc 0
6428emit 1409286152
6429basearg 0 e
6430loadarg 16 1 get fr add x 21
6431e3 2332033024 0 0 16
6432e3 2852126720 2 31 5
6433copy = get oc 0
6434emit 4043309247
6435endcp = get oc 0
6436emit 1409286144
6437emit 960495718
6438emit 956301318
6439emit 2432697443
6440emit 2432697344
6441emit 3506439333
6442back copy
6443patch19 endcp get oc 0
6444e3 2852126720 0 31 2
6445endbr = get oc 0
6446emit 335544320
6447patch19 small get oc 0
6448matconst 0 18446744073709551511
6449patch26 endbr get oc 0
6450hreturn dst x
6451end
6452
6453note waitio(fd int, events int, timeout_ns long): revents, 0 timeout, or negative errno.
6454note ppoll uses one private pollfd and one private timespec. Negative timeout is infinite.
6455fn hwaitio dst ulong x ulong to void
6456harg x 0 8
6457harg x 1 8
6458harg x 2 9
6459save
6460hload x 0 0
6461hload x 1 1
6462hload x 2 2
6463note pollfd.events is 16 bits; refuse truncation of the public int argument.
6464emit 4043309119
6465trapc 11 3
6466matconst 16 65535
6467e3 3942645760 31 1 16
6468trapc 8 3
6469spsub 32
6470stkchk
6471emit 4177527807
6472emit 3103785952
6473emit 2030046177
6474matconst 3 1000000000
6475emit 4043309151
6476forever = get oc 0
6477emit 1409286155
6478e3 2596276224 4 2 3
6479emit 2600700037
6480emit 4177528804
6481emit 4177529829
6482emit 2432705506
6483jump = get oc 0
6484emit 335544320
6485patch19 forever get oc 0
6486matconst 2 0
6487patch26 jump get oc 0
6488emit 2432697312
6489matconst 1 1
6490matconst 3 0
6491matconst 4 8
6492hsys 73
6493emit 4043309087
6494failed = get oc 0
6495emit 1409286157
6496emit 2034241504
6497patch19 failed get oc 0
6498spadd 32
6499hreturn dst x
6500end
6501
6502note A packed argument list is u16 count followed by count (u16 length, bytes) records.
6503note Byte order is little-endian. The packet is at most 4096 bytes and 64 records.
6504note The decoder constructs kernel pointers only in private stack memory.
6505note execenv 233 (execenv A off n B off m) decodes a second packet, the environment, with the same
6506note rules except that every record must be nonempty. Its base and length are loaded into x14/x15
6507note before SPSUB (save-area reads are SP-relative) and both packets are checked before any fd closes.
6508note Stack: argv strings SP, argv pointers SP+4096, env strings SP+8192, env pointers SP+12288.
6509fn hpack c ulong dst ulong x ulong to void
6510errs = new ulong 16 0
6511tbl = new ulong 27 0
6512e = harray x 0
6513if eq c 125
6514harg x 2 8
6515end
6516if eq c 233
6517put fr add x 23 e
6518put fr add x 24 get fr add x 21
6519put fr add x 25 get fr add x 22
6520e = harray x 0
6521put fr add x 26 e
6522put fr add x 27 get fr add x 21
6523put fr add x 28 get fr add x 22
6524put fr add x 20 get fr add x 23
6525put fr add x 21 get fr add x 24
6526put fr add x 22 get fr add x 25
6527e = get fr add x 23
6528end
6529save
6530basearg 4 e
6531loadarg 16 1 get fr add x 21
6532e3 2332033024 4 4 16
6533loadarg 5 1 get fr add x 22
6534matconst 16 4096
6535e3 3942645760 31 5 16
6536trapc 8 3
6537emit 4043311295
6538trapc 3 3
6539if eq c 125
6540hload x 2 14
6541end
6542note stack is 8192 bytes per packet: one packet pass, or two for execenv (the pass bound is stack >> 13).
6543stack = ulong 8192
6544if eq c 233
6545note x14 = env base + off (ADD), x15 = env length; length 2..4096 (CMP x15,x16 B.HI; CMP x15,#2 B.LO).
6546basearg 14 get fr add x 26
6547loadarg 16 1 get fr add x 27
6548e3 2332033024 14 14 16
6549loadarg 15 1 get fr add x 28
6550matconst 16 4096
6551e3 3942645760 31 15 16
6552trapc 8 3
6553emit 4043311615
6554trapc 3 3
6555stack = ulong 16384
6556end
6557spsub stack
6558stkchk
6559pass = ulong 0
6560while lt pass shr stack 13
6561if eq pass 1
6562note MOV x4,x14; MOV x5,x15 (ORR with XZR): the environment packet.
6563e3 2852126720 4 31 14
6564e3 2852126720 5 31 15
6565end
6566emit 2034237574
6567emit 2432698500
6568emit 3506440357
6569emit 4043309279
6570trapc 0 3
6571matconst 16 64
6572e3 3942645760 31 6 16
6573trapc 8 3
6574if or or eq c 125 eq c 141 eq c 142
6575emit 4043311327
6576trapc 1 3
6577end
6578matconst 9 0
6579if eq pass 0
6580emit 2432697322
6581emit 2436892651
6582else
6583note ADD x10,SP,#2,LSL12; ADD x11,SP,#3,LSL12: env strings and env pointers.
6584emit 2436893674
6585emit 2436894699
6586end
6587outer = get oc 0
6588e3 3942645760 31 9 6
6589done = get oc 0
6590emit 1409286146
6591emit 4043311295
6592trapc 3 3
6593emit 2034237580
6594emit 2432698500
6595emit 3506440357
6596e3 3942645760 31 12 5
6597trapc 8 3
6598note A program path and both rename paths must be nonempty; every environment record is nonempty.
6599note (Nested conditions keep om4 within its 16 live registers.)
6600if eq pass 0
6601if or or eq c 124 eq c 198 eq c 233
6602emit 4043309375
6603notfirst = get oc 0
6604emit 1409286145
6605emit 4043309471
6606trapc 0 3
6607patch19 notfirst get oc 0
6608else
6609emit 4043309471
6610trapc 0 3
6611end
6612else
6613emit 4043309471
6614trapc 0 3
6615end
6616emit 4177527146
6617emit 2432704875
6618inner = get oc 0
6619emit 4043309471
6620endstr = get oc 0
6621emit 1409286144
6622emit 960495757
6623emit 4043309503
6624trapc 0 3
6625emit 956301645
6626emit 2432697476
6627emit 2432697674
6628emit 3506439333
6629emit 3506439564
6630back inner
6631patch19 endstr get oc 0
6632emit 956301663
6633emit 2432697674
6634emit 2432697641
6635back outer
6636patch19 done get oc 0
6637emit 4043309247
6638trapc 1 3
6639emit 4177527167
6640pass = add pass 1
6641end
6642if or or eq c 124 eq c 198 eq c 233
6643note Exec admits only the caller-assigned standard streams. Linux close_range with UNSHARE
6644note closes every descriptor >=3 in a private table. Failure returns without executing a child;
6645note no guessed descriptor ceiling or permissive fallback. This is not filesystem isolation.
6646matconst 0 3
6647matconst 1 4294967295
6648matconst 2 2
6649hsys 436
6650emit 4043309087
6651note outer and done are dead after the packet scan; they are reused for the two branch sites here.
6652done = get oc 0
6653emit 1409286145
6654if eq c 198
6655note execpure strict stdio profile: Linux UAPI layouts in 640 private guest stack bytes, never an
6656note exposed native pointer. sock_filter is u16 code,u8 jt,u8 jf,u32 k; sock_fprog has its length at
6657note 0 and its pointer at 8 on AArch64. A Landlock path rule is a packed u64 allowed_access plus an
6658note s32 parent_fd; prlimit64 takes two u64s. Each step that returns a negative value branches (B.LT)
6659note to the common restore with the kernel error in x0. ADD x16,SP,#k then STR x0,[x16] stores a word.
6660n = ulong 0
6661spsub 640
6662stkchk
6663mimmed 2432697312 16 0 632
6664e3 4177526784 11 16 0
6665note Extra descriptors are already closed in a private table. Pin the executable as descriptor 3.
6666matconst 0 18446744073709551516
6667mimmed 2432697312 1 0 640
6668matconst 2 2621440
6669matconst 3 0
6670hsys 56
6671emit 4043309087
6672put errs n get oc 0
6673emit 1409286155
6674n = add n 1
6675note ABI3 rights through TRUNCATE are all handled, with only read/execute of that one inode allowed.
6676matconst 0 32767
6677mimmed 2432697312 16 0 0
6678e3 4177526784 0 16 0
6679mimmed 2432697312 0 0 0
6680matconst 1 8
6681matconst 2 0
6682hsys 444
6683emit 4043309087
6684put errs n get oc 0
6685emit 1409286155
6686n = add n 1
6687matconst 0 5
6688mimmed 2432697312 16 0 16
6689e3 4177526784 0 16 0
6690matconst 0 3
6691mimmed 2432697312 16 0 24
6692e3 4177526784 0 16 0
6693matconst 0 4
6694matconst 1 1
6695mimmed 2432697312 2 0 16
6696matconst 3 0
6697hsys 445
6698emit 4043309087
6699put errs n get oc 0
6700emit 1409286155
6701n = add n 1
6702matconst 0 38
6703matconst 1 1
6704matconst 2 0
6705matconst 3 0
6706matconst 4 0
6707hsys 167
6708emit 4043309087
6709put errs n get oc 0
6710emit 1409286155
6711n = add n 1
6712matconst 0 4
6713matconst 1 0
6714hsys 446
6715emit 4043309087
6716put errs n get oc 0
6717emit 1409286155
6718n = add n 1
6719matconst 0 4
6720hsys 57
6721emit 4043309087
6722put errs n get oc 0
6723emit 1409286155
6724n = add n 1
6725note Per-process ceilings as (resource, soft, hard): CPU, FSIZE, STACK, CORE, NPROC, NOFILE, AS.
6726note Pipe-output and wall budgets additionally require the trusted supervisor.
6727put tbl 1 1
6728put tbl 2 2
6729put tbl 3 1
6730put tbl 4 1048576
6731put tbl 5 1048576
6732put tbl 6 3
6733put tbl 7 8388608
6734put tbl 8 8388608
6735put tbl 9 4
6736put tbl 12 6
6737put tbl 15 7
6738put tbl 16 4
6739put tbl 17 4
6740put tbl 18 9
6741put tbl 19 134217728
6742put tbl 20 134217728
6743for i ulong 7
6744matconst 0 get tbl add mul i 3 1
6745mimmed 2432697312 16 0 48
6746e3 4177526784 0 16 0
6747matconst 0 get tbl add mul i 3 2
6748mimmed 2432697312 16 0 56
6749e3 4177526784 0 16 0
6750matconst 0 0
6751matconst 1 get tbl mul i 3
6752mimmed 2432697312 2 0 48
6753matconst 3 0
6754hsys 261
6755emit 4043309087
6756put errs n get oc 0
6757emit 1409286155
6758n = add n 1
6759end
6760note Validate architecture, then read only 0, write only 1/2, exit/exit_group. Everything else is EPERM.
6761note execveat is restricted to descriptor 3 and AT_EMPTY_PATH; Landlock also binds execution to inode 3.
6762note The inode rule is essential: an absolute execveat path otherwise ignores its dirfd argument.
6763note Entry i = code | jt<<16 | jf<<24 | k<<32, stored at SP+128+8i. (code jt jf k) rows in order:
6764note (32 0 0 4) (21 1 0 3221225655) (6 0 0 2147483648) (32 0 0 0) (21 0 3 63) (32 0 0 16) (21 0 1 0) (6 0 0 2147418112) (32 0 0 0)
6765note (21 0 4 64) (32 0 0 16) (21 1 0 1) (21 0 1 2) (6 0 0 2147418112) (32 0 0 0) (21 0 1 93) (6 0 0 2147418112) (21 0 1 94)
6766note (6 0 0 2147418112) (32 0 0 0) (21 0 5 281) (32 0 0 16) (21 0 3 3) (32 0 0 48) (21 0 1 4096) (6 0 0 2147418112) (6 0 0 327681)
6767put tbl 0 ulong 17179869216
6768put tbl 1 ulong 13835058841261244437
6769put tbl 2 ulong 9223372036854775814
6770put tbl 3 ulong 32
6771put tbl 4 ulong 270633271317
6772put tbl 5 ulong 68719476768
6773put tbl 6 ulong 16777237
6774put tbl 7 ulong 9223090561878065158
6775put tbl 8 ulong 32
6776put tbl 9 ulong 274945015829
6777put tbl 10 ulong 68719476768
6778put tbl 11 ulong 4295032853
6779put tbl 12 ulong 8606711829
6780put tbl 13 ulong 9223090561878065158
6781put tbl 14 ulong 32
6782put tbl 15 ulong 399448735765
6783put tbl 16 ulong 9223090561878065158
6784put tbl 17 ulong 403743703061
6785put tbl 18 ulong 9223090561878065158
6786put tbl 19 ulong 32
6787put tbl 20 ulong 1206969696277
6788put tbl 21 ulong 68719476768
6789put tbl 22 ulong 12935233557
6790put tbl 23 ulong 206158430240
6791put tbl 24 ulong 17592202821653
6792put tbl 25 ulong 9223090561878065158
6793put tbl 26 ulong 1407379178520582
6794for i ulong 27
6795matconst 0 get tbl i
6796mimmed 2432697312 16 0 add 128 mul i 8
6797e3 4177526784 0 16 0
6798end
6799matconst 0 27
6800mimmed 2432697312 16 0 80
6801e3 4177526784 0 16 0
6802mimmed 2432697312 0 0 128
6803mimmed 2432697312 16 0 88
6804e3 4177526784 0 16 0
6805matconst 0 1
6806matconst 1 0
6807mimmed 2432697312 2 0 80
6808hsys 277
6809emit 4043309087
6810put errs n get oc 0
6811emit 1409286155
6812n = add n 1
6813note Restore the original packet stack before entering the pinned executable. Descriptor 3 is CLOEXEC.
6814mimmed 2432697312 16 0 632
6815e3 4181721088 11 16 0
6816spadd 640
6817matconst 0 3
6818e3 2852126720 1 31 11
6819emit 2436892642
6820e3 2852126720 3 31 11
6821matconst 4 4096
6822hsys 281
6823outer = get oc 0
6824emit 335544320
6825while gt n 0
6826n = sub n 1
6827patch19 get errs n get oc 0
6828end
6829mimmed 2432697312 16 0 632
6830e3 4181721088 11 16 0
6831spadd 640
6832patch26 outer get oc 0
6833else
6834emit 2432697312
6835emit 2436892641
6836if eq c 233
6837note ADD x2,SP,#3,LSL12: envp is the private environment pointer list.
6838emit 2436894690
6839else
6840note MOV x2,x11: envp is the argv list's NULL terminator, an empty environment.
6841e3 2852126720 2 31 11
6842end
6843hsys 221
6844end
6845patch19 done get oc 0
6846else
6847if eq c 125
6848emit 2436892656
6849emit 4181721601
6850emit 4181722627
6851matconst 0 18446744073709551516
6852matconst 2 18446744073709551516
6853e3 2852126720 4 31 14
6854hsys 276
6855else
6856note linkat/symlnk: the packed list's two records are pointer-list entries 0 and 1.
6857emit 2436892656
6858emit 4181721601
6859emit 4181722627
6860if eq c 141
6861matconst 0 18446744073709551516
6862matconst 2 18446744073709551516
6863matconst 4 0
6864hsys 37
6865else
6866e3 2852126720 2 31 3
6867e3 2852126720 0 31 1
6868matconst 1 18446744073709551516
6869hsys 36
6870end
6871end
6872end
6873spadd stack
6874hreturn dst x
6875end
6876
6877fn hreadlnk dst ulong x ulong to void
6878e = harray x 0
6879put fr add x 23 e
6880put fr add x 24 get fr add x 21
6881put fr add x 25 get fr add x 22
6882e = harray x 1
6883put fr add x 26 e
6884put fr add x 27 get fr add x 21
6885put fr add x 28 get fr add x 22
6886put fr add x 20 get fr add x 23
6887put fr add x 21 get fr add x 24
6888put fr add x 22 get fr add x 25
6889hpathcp x
6890note readlnk refuses an empty path before the kernel call.
6891emit 4043309247
6892trapc 0 3
6893basearg 2 get fr add x 26
6894loadarg 16 1 get fr add x 27
6895e3 2332033024 2 2 16
6896loadarg 3 1 get fr add x 28
6897hcopy
6898hsys 78
6899spadd 4096
6900hreturn dst x
6901end
6902note stat: path slice (readonly) + output slice, at least 128 bytes for the ARM64 stat structure.
6903note Linux newfstatat(AT_FDCWD, path, out, 0); returns 0 or -errno. A short output traps 3.
6904note pread/pwrite: fd, byte slice (off, n), position long. The file offset is not moved.
6905note pread (161) writes A, so A must be writable (error 20); pwrite (162) only reads A, so a text is allowed.
6906fn hpread c ulong dst ulong x ulong to void
6907harg x 4 8
6908wr = ulong 0
6909if eq c 161
6910wr = ulong 1
6911end
6912e = harray x wr
6913harg x 3 9
6914save
6915hload x 4 0
6916basearg 1 e
6917loadarg 16 1 get fr add x 21
6918e3 2332033024 1 1 16
6919loadarg 2 1 get fr add x 22
6920hload x 3 3
6921nr = ulong 67
6922if eq c 162
6923nr = ulong 68
6924end
6925hsys nr
6926hreturn dst x
6927end
6928
6929fn hstat c ulong dst ulong x ulong to void
6930e = harray x 0
6931put fr add x 23 e
6932put fr add x 24 get fr add x 21
6933put fr add x 25 get fr add x 22
6934e = harray x 1
6935put fr add x 26 e
6936put fr add x 27 get fr add x 21
6937put fr add x 28 get fr add x 22
6938put fr add x 20 get fr add x 23
6939put fr add x 21 get fr add x 24
6940put fr add x 22 get fr add x 25
6941hpathcp x
6942note stat refuses an empty path before the kernel call.
6943emit 4043309247
6944trapc 0 3
6945basearg 2 get fr add x 26
6946loadarg 16 1 get fr add x 27
6947e3 2332033024 2 2 16
6948loadarg 3 1 get fr add x 28
6949matconst 6 128
6950e3 3942645760 31 3 6
6951trapc 3 3
6952matconst 3 0
6953if eq c 149
6954matconst 3 256
6955end
6956hcopy
6957hsys 79
6958spadd 4096
6959hreturn dst x
6960end
6961fn hsockopt dst ulong x ulong to void
6962harg x 2 8
6963harg x 3 8
6964harg x 4 8
6965e = harray x 0
6966save
6967hload x 2 0
6968hload x 3 1
6969hload x 4 2
6970basearg 3 e
6971loadarg 16 1 get fr add x 21
6972e3 2332033024 3 3 16
6973loadarg 4 1 get fr add x 22
6974note Only scalar transport options; no filters, devices, credentials or pointer-bearing options.
6975hoptgate
6976hreturn dst x
6977end
6978
6979note Socket option allowlist. Linux asm-generic/socket.h and linux/tcp.h own these IDs.
6980note Unknown options retain ENOPROTOOPT (-92); malformed scalar sizes return EINVAL (-22).
6981fn hoptgate to void
6982ok = new ulong 6 0
6983mimmed 4043309056 31 1 1
6984tcp = get oc 0
6985emit 1409286145
6986for j ulong 5
6987v = add j 5
6988if eq j 0
6989v = ulong 2
6990end
6991if eq j 1
6992v = ulong 15
6993end
6994mimmed 4043309056 31 2 v
6995put ok j get oc 0
6996emit 1409286144
6997end
6998badso = get oc 0
6999emit 335544320
7000patch19 tcp get oc 0
7001mimmed 4043309056 31 1 6
7002badtcp = get oc 0
7003emit 1409286145
7004mimmed 4043309056 31 2 1
7005put ok 5 get oc 0
7006emit 1409286144
7007patch26 badso get oc 0
7008patch19 badtcp get oc 0
7009matconst 0 sub ulong 0 92
7010done = get oc 0
7011emit 335544320
7012for j ulong 6
7013patch19 get ok j get oc 0
7014end
7015mimmed 4043309056 31 4 4
7016sizeok = get oc 0
7017emit 1409286144
7018matconst 0 sub ulong 0 22
7019badsize = get oc 0
7020emit 335544320
7021patch19 sizeok get oc 0
7022hsys 208
7023patch26 done get oc 0
7024patch26 badsize get oc 0
7025end
7026
7027note SO_PEERCRED: fixed 12-byte ucred result, never a caller-chosen getsockopt operation.
7028fn hpeercr dst ulong x ulong to void
7029harg x 4 8
7030e = harray x 1
7031save
7032hload x 4 0
7033basearg 3 e
7034loadarg 16 1 get fr add x 21
7035e3 2332033024 3 3 16
7036loadarg 4 1 get fr add x 22
7037matconst 16 12
7038e3 3942645760 31 4 16
7039trapc 3 3
7040matconst 1 1
7041matconst 2 17
7042spsub 16
7043stkchk
7044matconst 4 12
7045emit or ulong 3103785952 4
7046emit or ulong 2432697312 4
7047hsys 209
7048spadd 16
7049hreturn dst x
7050end
7051
7052note pollfds uses packed Linux pollfd records; length is bytes, each record is 8 bytes.
7053note Maximum 1024 records, checked before ppoll. Kernel writes only revents in the checked slice.
7054fn hpollfds dst ulong x ulong to void
7055e = harray x 1
7056harg x 2 9
7057save
7058basearg 0 e
7059loadarg 16 1 get fr add x 21
7060e3 2332033024 0 0 16
7061loadarg 1 1 get fr add x 22
7062matconst 16 7
7063e3 2315255808 17 1 16
7064mimmed 4043309056 31 17 0
7065trapc 1 3
7066matconst 16 8192
7067e3 3942645760 31 1 16
7068trapc 8 3
7069matconst 16 8
7070e3 2596276224 1 1 16
7071hload x 2 2
7072spsub 16
7073stkchk
7074matconst 3 1000000000
7075emit 4043309151
7076forever = get oc 0
7077emit 1409286155
7078e3 2596276224 4 2 3
7079emit 2600700037
7080emit 4177527780
7081emit 4177528805
7082emit or ulong 2432697312 2
7083jump = get oc 0
7084emit 335544320
7085patch19 forever get oc 0
7086matconst 2 0
7087patch26 jump get oc 0
7088matconst 3 0
7089matconst 4 8
7090hsys 73
7091spadd 16
7092hreturn dst x
7093end
7094
7095note sendmsg has one checked iovec and no ancillary data or exposed kernel pointers.
7096note Arm ARM STR unsigned offset: imm12 scaled by 8, Rn=SP; private msghdr then iovec.
7097fn hsendmsg dst ulong x ulong to void
7098harg x 4 8
7099e = harray x 0
7100harg x 2 8
7101save
7102hload x 4 0
7103hload x 2 2
7104matconst 16 16384
7105e3 2852126720 2 2 16
7106basearg 3 e
7107loadarg 16 1 get fr add x 21
7108e3 2332033024 3 3 16
7109loadarg 4 1 get fr add x 22
7110spsub 80
7111stkchk
7112for j ulong 10
7113emit or or ulong 4177527776 31 shl j 10
7114end
7115emit or or ulong 4177527776 3 shl ulong 8 10
7116emit or or ulong 4177527776 4 shl ulong 9 10
7117emit or or ulong 2432697312 3 shl ulong 64 10
7118emit or or ulong 4177527776 3 shl ulong 2 10
7119matconst 3 1
7120emit or or ulong 4177527776 3 shl ulong 3 10
7121emit or ulong 2432697312 1
7122hsys 211
7123spadd 80
7124hreturn dst x
7125end
7126
7127note Bounded mapping table belongs to the emitted program, not the compiler process.
7128note 128 slots of address, byte length, generation, access. Only owned spans are accessible.
7129note Function mode has no program image: the caller's runtime table supplies the zero-initialised
7130note 4096-byte table at entry 31 (mcheck and mnew load it from [x7, #248]); nothing is reserved here.
7131fn mtinit to void
7132if eq get progmode 0 0
7133ret
7134end
7135if eq get maptab 0 0
7136p = get poolb 0
7137if gt p 1073737728
7138fail 23
7139end
7140put maptab 0 add 276824064 p
7141put poolb 0 add p 4096
7142end
7143end
7144note Instruction forms in seed/rung2/table.s2. ldr/str offsets count eight-byte cells.
7145fn mload d ulong n ulong off ulong to void
7146emit or or or 4181721088 d shl n 5 shl off 10
7147end
7148fn mstore d ulong n ulong off ulong to void
7149emit or or or 4177526784 d shl n 5 shl off 10
7150end
7151fn mimmed op ulong d ulong n ulong k ulong to void
7152emit or or or op d shl n 5 shl k 10
7153end
7154fn mbranch cc ulong to ulong
7155p = get oc 0
7156emit or 1409286144 cc
7157ret p
7158end
7159
7160note Descriptor-kind table is process local, not a sandbox. Full fd keys avoid high-fd cleanup resets.
7161note Tags certify the current descriptor, not a generation-bearing stale integer reference.
7162note X10 fd, X11 entry or end, X12 remaining. X0..X8 and X15..X17 preserved.
7163fn fdscan kind ulong to void
7164matconst 11 get fdtab 0
7165matconst 12 128
7166head = get oc 0
7167mload 14 11 1
7168mimmed 4043309056 31 14 kind
7169next = mbranch 1
7170found = ulong 0
7171if ne kind 0
7172mload 13 11 0
7173e3 3942645760 31 13 10
7174found = mbranch 0
7175else
7176found = mbranch 0
7177end
7178patch19 next get oc 0
7179mimmed 2432696320 11 11 16
7180mimmed 3506438144 12 12 1
7181mimmed 4043309056 31 12 0
7182again = mbranch 1
7183patch19 again head
7184patch19 found get oc 0
7185end
7186fn fdcheck kind ulong to void
7187fdscan kind
7188mimmed 4043309056 31 12 0
7189trapc 0 3
7190end
7191fn fdforget to void
7192note Match either kind, clearing every matching key. No propagation through dupfd.
7193for kind ulong 2
7194fdscan add kind 1
7195mimmed 4043309056 31 12 0
7196none = mbranch 0
7197mstore 31 11 1
7198patch19 none get oc 0
7199end
7200end
7201fn fdrecord kind ulong to void
7202mimmed 4043309056 31 0 0
7203bad = mbranch 11
7204mstore 0 11 0
7205matconst 12 kind
7206mstore 12 11 1
7207patch19 bad get oc 0
7208end
7209
7210
7211
7212note Validate a long handle in X10; produce entry X11, base X13, length X12.
7213note The slot's generation defeats a stale handle after close and slot reuse.
7214fn mcheck to void
7215if eq get progmode 0 0
7216matconst 15 8191
7217else
7218matconst 15 255
7219end
7220e3 2315255808 11 10 15
7221mimmed 4043309056 31 11 1
7222trapc 3 3
7223if eq get progmode 0 0
7224mload 14 7 31
7225mload 14 14 0
7226mimmed 4043309056 31 14 1
7227trapc 3 3
7228matconst 15 4098
7229e3 3942645760 31 14 15
7230trapc 8 3
7231e3 3942645760 31 11 14
7232trapc 8 3
7233else
7234mimmed 4043309056 31 11 128
7235trapc 8 3
7236end
7237mimmed 3506438144 11 11 1
7238matconst 15 32
7239e3 2600500224 11 11 15
7240if eq get progmode 0 0
7241mload 15 7 31
7242mimmed 2432696320 15 15 32
7243else
7244matconst 15 get maptab 0
7245end
7246e3 2332033024 11 11 15
7247mload 14 11 2
7248mimmed 4043309056 31 14 0
7249trapc 0 3
7250if eq get progmode 0 0
7251matconst 15 13
7252else
7253matconst 15 8
7254end
7255e3 2596283392 10 10 15
7256e3 3942645760 31 10 14
7257trapc 1 3
7258mload 13 11 0
7259mimmed 4043309056 31 13 0
7260trapc 0 3
7261mload 12 11 1
7262end
7263
7264note mapnew fd:int offset:ulong bytes:ulong access:int mode:int -> long handle/errno.
7265note Access 1 or 3 and modes 1/2/33/34 must be literal. No fixed address or executable map.
7266note vfiomap fd:int A off n -> long handle/errno: n must be 32, a struct vfio_region_info the kernel
7267note filled. Its flags need READ|MMAP (1|4); size and offset come from the span. Always read-only shared.
7268note vfiomapw is the same with flags READ|WRITE|MMAP (7) and a read/write shared map, so mapset can store
7269note to device registers (BAR0 MMIO). Only a span the kernel reported writable maps writable.
7270fn mnew c ulong dst ulong x ulong to void
7271acc = ulong 0
7272mode = ulong 1048610
7273if or eq c 229 eq c 230
7274harg x 4 8
7275put fr add x 20 harray x 0
7276put fr add x 23 alloc
7277matconst get fr add x 23 32
7278e3 3942645760 31 get fr add x 22 get fr add x 23
7279trapc 1 3
7280acc = ulong 1
7281if eq c 230
7282acc = ulong 3
7283end
7284mode = ulong 1
7285end
7286if eq c 167
7287harg x 0 8
7288harg x 1 5
7289harg x 2 5
7290harg x 3 8
7291harg x 4 8
7292if or ne get fr add x 14 3 ne get fr add x 16 3
7293fail 9
7294end
7295acc = get fr add x 15
7296mode = get fr add x 17
7297if and ne acc 1 ne acc 3
7298fail 9
7299end
7300if and and and ne mode 1 ne mode 2 ne mode 33 ne mode 34
7301fail 9
7302end
7303end
7304if eq c 176
7305harg x 1 5
7306harg x 2 5
7307end
7308save
7309if or eq c 229 eq c 230
7310basearg 6 get fr add x 20
7311loadarg 16 1 get fr add x 21
7312e3 2332033024 6 6 16
7313mimmed 3107979264 15 6 1
7314matconst 14 or 5 sub acc 1
7315e3 2315255808 15 15 14
7316mimmed 4043309056 31 15 or 5 sub acc 1
7317trapc 1 3
7318mload 1 6 2
7319hload x 4 4
7320mload 5 6 3
7321else
7322hload x 2 1
7323end
7324mimmed 4043309056 31 1 0
7325trapc 0 3
7326matconst 15 68719476736
7327e3 3942645760 31 1 15
7328trapc 8 3
7329if eq c 167
7330hload x 0 4
7331hload x 1 5
7332if ge mode 32
7333matconst 15 18446744073709551615
7334e3 3942645760 31 4 15
7335trapc 1 3
7336mimmed 4043309056 31 5 0
7337trapc 1 3
7338end
7339end
7340if eq c 176
7341hload x 1 0
7342note Reservations are page aligned and bounded below the 40-bit GPU command limit.
7343matconst 15 65536
7344e3 3942645760 31 0 15
7345trapc 3 3
7346matconst 15 1099511627776
7347e3 3405774848 15 15 1
7348e3 3942645760 31 0 15
7349trapc 8 3
7350matconst 15 4095
7351e3 2315255808 14 0 15
7352mimmed 4043309056 31 14 0
7353trapc 1 3
7354e3 2315255808 14 1 15
7355mimmed 4043309056 31 14 0
7356trapc 1 3
7357matconst 4 18446744073709551615
7358matconst 5 0
7359end
7360if eq get progmode 0 0
7361mload 9 7 31
7362mload 12 9 0
7363mimmed 4043309056 31 12 1
7364trapc 3 3
7365matconst 15 4098
7366e3 3942645760 31 12 15
7367trapc 8 3
7368mload 14 9 2
7369mimmed 4043309056 31 14 1
7370trapc 3 3
7371matconst 15 1125899906842623
7372e3 3942645760 31 14 15
7373trapc 8 3
7374mimmed 2432696320 9 9 32
7375else
7376matconst 9 get maptab 0
7377end
7378matconst 10 1
7379head = get oc 0
7380mload 11 9 0
7381mimmed 4043309056 31 11 0
7382used = mbranch 1
7383mload 11 9 2
7384if eq get progmode 0 0
7385e3 3942645760 31 11 14
7386else
7387matconst 15 36028797018963967
7388e3 3942645760 31 11 15
7389end
7390vacant = mbranch 3
7391patch19 used get oc 0
7392mimmed 2432696320 9 9 32
7393mimmed 2432696320 10 10 1
7394if eq get progmode 0 0
7395e3 3942645760 31 10 12
7396else
7397mimmed 4043309056 31 10 128
7398end
7399again = mbranch 9
7400patch19 again head
7401matconst 0 18446744073709551592
7402full = get oc 0
7403emit 335544320
7404patch19 vacant get oc 0
7405if eq c 176
7406hload x 1 0
7407else
7408matconst 0 0
7409end
7410matconst 2 acc
7411matconst 3 mode
7412hsys 222
7413mimmed 4043309056 31 0 0
7414failed = mbranch 11
7415if eq c 176
7416hload x 1 14
7417e3 3942645760 31 0 14
7418exact = mbranch 0
7419note Old kernels may ignore NOREPLACE; reject and unmap any displaced result.
7420hsys 215
7421mimmed 4043309056 31 0 0
7422trapc 1 3
7423matconst 0 18446744073709551521
7424wrong = get oc 0
7425emit 335544320
7426patch19 exact get oc 0
7427put fr add x 23 wrong
7428end
7429mstore 0 9 0
7430mstore 1 9 1
7431mimmed 2432696320 11 11 1
7432mstore 11 9 2
7433matconst 15 acc
7434mstore 15 9 3
7435if eq get progmode 0 0
7436matconst 15 13
7437else
7438matconst 15 8
7439end
7440e3 2596282368 0 11 15
7441e3 2852126720 0 0 10
7442patch19 failed get oc 0
7443patch26 full get oc 0
7444if eq c 176
7445head = get fr add x 23
7446patch26 head get oc 0
7447end
7448hreturn dst x
7449end
7450
7451note Replace only an entire owned PROT_NONE reservation. Failed binds retire the handle.
7452fn mbind dst ulong x ulong to void
7453harg x 0 9
7454harg x 1 8
7455harg x 2 5
7456harg x 3 8
7457if ne get fr add x 14 3
7458fail 9
7459end
7460acc = get fr add x 15
7461if and ne acc 1 ne acc 3
7462fail 9
7463end
7464save
7465hload x 0 10
7466mcheck
7467mload 15 11 3
7468mimmed 4043309056 31 15 0
7469trapc 1 3
7470e3 2852126720 0 31 13
7471e3 2852126720 1 31 12
7472matconst 2 acc
7473matconst 3 17
7474hload x 1 4
7475hload x 2 5
7476hsys 222
7477mimmed 4043309056 31 0 0
7478failed = mbranch 11
7479e3 3942645760 31 0 13
7480trapc 1 3
7481matconst 15 acc
7482mstore 15 11 3
7483matconst 0 0
7484done = get oc 0
7485emit 335544320
7486patch19 failed get oc 0
7487e3 2852126720 10 31 0
7488e3 2852126720 0 31 13
7489e3 2852126720 1 31 12
7490note Preserve a droppable owner but forbid a second bind if cleanup also fails.
7491if eq get progmode 0 0
7492matconst 15 4
7493mstore 15 11 3
7494end
7495hsys 215
7496mimmed 4043309056 31 0 0
7497trapc 1 3
7498mstore 31 11 0
7499mstore 31 11 1
7500mstore 31 11 3
7501e3 2852126720 0 31 10
7502patch26 done get oc 0
7503hreturn dst x
7504end
7505
7506note Copies traverse only checked live mapped spans and normal bounded byte arrays.
7507note mapread writes A; mapwrite reads A. Both return the count, with no pointer decay.
7508fn mcopy c ulong dst ulong x ulong to void
7509harg x 4 9
7510harg x 3 5
7511wr = ulong 0
7512if eq c 169
7513wr = ulong 1
7514end
7515e = harray x wr
7516save
7517hload x 4 10
7518mcheck
7519mload 15 11 3
7520mimmed 4043309056 31 15 0
7521trapc 0 3
7522if eq get progmode 0 0
7523mimmed 4043309056 31 15 3
7524trapc 8 3
7525end
7526hload x 3 0
7527e3 3942645760 31 0 12
7528trapc 8 3
7529e3 3405774848 12 12 0
7530loadarg 2 1 get fr add x 22
7531e3 3942645760 31 2 12
7532trapc 8 3
7533e3 2332033024 0 13 0
7534basearg 1 e
7535loadarg 16 1 get fr add x 21
7536e3 2332033024 1 1 16
7537if eq c 170
7538mload 15 11 3
7539mimmed 4043309056 31 15 3
7540trapc 1 3
7541e3 2852126720 15 31 0
7542e3 2852126720 0 31 1
7543e3 2852126720 1 31 15
7544end
7545e3 2852126720 4 31 2
7546head = get oc 0
7547mimmed 4043309056 31 2 0
7548done = mbranch 0
7549emit 960495619
7550emit 956301347
7551mimmed 2432696320 0 0 1
7552mimmed 2432696320 1 1 1
7553mimmed 3506438144 2 2 1
7554back head
7555patch19 done get oc 0
7556e3 2852126720 0 31 4
7557hreturn dst x
7558end
7559
7560note NVRM NVOS21/NVOS54 parameter binding. The kernel sees one private header.
7561note Pointer and length are derived from a checked byte span, never from user bytes.
7562note Request is exactly the 32-byte RM_ALLOC or RM_CONTROL encoding.
7563fn hdevcall dst ulong x ulong to void
7564n = xhreq 171 x
7565e = harray x 1
7566put fr add x 23 e
7567put fr add x 24 get fr add x 21
7568t = alloc
7569matconst t 32
7570e3 3942645760 31 get fr add x 22 t
7571trapc 1 3
7572f = harray x 1
7573if eq e f
7574fail 19
7575end
7576t = alloc
7577matconst t 1048576
7578e3 3942645760 31 get fr add x 22 t
7579trapc 8 3
7580save
7581hload x 4 0
7582hload x 2 1
7583basearg 10 get fr add x 23
7584loadarg 16 1 get fr add x 24
7585e3 2332033024 10 10 16
7586basearg 11 f
7587loadarg 16 1 get fr add x 21
7588e3 2332033024 11 11 16
7589loadarg 12 1 get fr add x 22
7590note Refuse overlapping parameter/header spans even through distinct edit parameters.
7591mimmed 4043309056 31 12 0
7592empty = mbranch 0
7593e3 2332033024 14 11 12
7594e3 3942645760 31 14 10
7595before = mbranch 9
7596mimmed 2432696320 14 10 32
7597e3 3942645760 31 11 14
7598trapc 3 3
7599patch19 before get oc 0
7600patch19 empty get oc 0
7601mload 13 10 2
7602mimmed 4043309056 31 13 0
7603trapc 1 3
7604mimmed 4043309056 31 12 0
7605nonzero = mbranch 1
7606matconst 11 0
7607patch19 nonzero get oc 0
7608spsub 32
7609stkchk
7610for i ulong 4
7611mload 13 10 i
7612mstore 13 31 i
7613end
7614mstore 11 31 2
7615emit 3103792108
7616emit 2432697314
7617hsys 29
7618for i ulong 4
7619if ne i 2
7620mload 13 31 i
7621mstore 13 10 i
7622end
7623end
7624spadd 32
7625hreturn dst x
7626end
7627
7628
7629note Mapped-address export for driver GPU VA serialization; no arbitrary-address access.
7630note Aligned word access uses exactly one 32/64-bit load/store bracketed by DSB SY.
7631fn mmapio c ulong dst ulong x ulong to void
7632harg x 0 9
7633if eq c 173
7634save
7635hload x 0 10
7636mcheck
7637e3 2852126720 0 31 13
7638hreturn dst x
7639put rt 0 5
7640ret
7641end
7642harg x 1 5
7643harg x 2 8
7644if ne get fr add x 12 3
7645fail 9
7646end
7647w = get fr add x 13
7648if and ne w 4 ne w 8
7649fail 9
7650end
7651if eq c 175
7652harg x 3 5
7653end
7654save
7655hload x 0 10
7656mcheck
7657hload x 1 0
7658mload 15 11 3
7659mimmed 4043309056 31 15 0
7660trapc 0 3
7661if eq get progmode 0 0
7662mimmed 4043309056 31 15 3
7663trapc 8 3
7664end
7665e3 3942645760 31 0 12
7666trapc 8 3
7667e3 3405774848 12 12 0
7668mimmed 4043309056 31 12 w
7669trapc 3 3
7670matconst 15 sub w 1
7671e3 2315255808 14 0 15
7672mimmed 4043309056 31 14 0
7673trapc 1 3
7674e3 2332033024 13 13 0
7675if eq c 175
7676mload 15 11 3
7677mimmed 4043309056 31 15 3
7678trapc 1 3
7679hload x 3 2
7680if eq w 4
7681matconst 15 4294967295
7682e3 3942645760 31 2 15
7683trapc 8 3
7684end
7685emit 3573759903
7686if eq w 4
7687mimmed 3103784960 2 13 0
7688else
7689mstore 2 13 0
7690end
7691emit 3573759903
7692matconst 0 0
7693hreturn dst x
7694else
7695emit 3573759903
7696if eq w 4
7697mimmed 3107979264 0 13 0
7698else
7699mload 0 13 0
7700end
7701emit 3573759903
7702hreturn dst x
7703put rt 0 5
7704end
7705end
7706
7707fn hostval c ulong dst ulong to void
7708if eq get xtarget 0 1
7709if and ge c 228 le c 232
7710if eq get progmode 0 0
7711fail 9
7712end
7713fail 25
7714end
7715xhost c dst
7716ret
7717end
7718if ne get xtarget 0 0
7719fail 25
7720end
7721note Function mode (no program image: no argv in x27, no x28 stack floor, no globals) refuses host
7722note words with error 9, except in an OM-2 unit the file and identity subset that needs none of the
7723note image: fclose 100, fopen 110, fstat 117, stat 140, getuid 145, geteuid 146, getgid 147, getegid 148,
7724note lstat 149 (OMEGESE.md 10, spec/omegese-om2.md); the clocks monons 109, realns 130 and sleepns 120;
7725note and the device subset devctl 166, mapnew 167, mapdrop 168, mapread 169, mapwrite 170, devcall 171,
7726note uvmctl 172, mapaddr 173, mapget 174, mapset 175, maphold 176 and mapbind 177. Fixed replacement
7727note is only over a whole live PROT_NONE reservation owned by this table. Each is save, inline svc,
7728note restore; x7 and x19..x28 are never written. Function-mode table entry31 points at a 32-byte
7729note header (capacity1..4098, generation base, generation limit, reserved) then capacity32-byte slots.
7730note Handles use13slot bits; program mode retains its raw128slots and8slot-bit handles.
7731if eq get progmode 0 0
7732if eq get om2 0 0
7733fail 9
7734end
7735ok = ulong 0
7736if or or eq c 100 eq c 110 or eq c 117 eq c 140
7737ok = ulong 1
7738end
7739if and ge c 145 le c 149
7740ok = ulong 1
7741end
7742if or or eq c 109 eq c 120 eq c 130
7743ok = ulong 1
7744end
7745if and ge c 166 le c 177
7746ok = ulong 1
7747end
7748if eq ok 0
7749fail 9
7750end
7751end
7752x = mul get depth 0 32
7753put fr add x 7 get nslot 0
7754note VFIO words are program-only. These IDs are disjoint from efi178, qty223 and peername224.
7755if and ge c 228 le c 232
7756if eq get fdtab 0 0
7757fdpool = get poolb 0
7758if gt fdpool 1073739776
7759fail 23
7760end
7761put fdtab 0 add 276824064 fdpool
7762put poolb 0 add fdpool 2048
7763end
7764if eq c 228
7765note VFIO container, group and device requests: _IO(';', 100 + k) from linux/vfio.h, exact allowlist and
7766note shapes. GET_API_VERSION 15204 takes no argument (n 0). DEVICE_RESET 15215 is not allowed: W0 never
7767note resets the device from Omega (Drake: W0a only, no FLR). CHECK_EXTENSION 15205 and
7768note SET_IOMMU 15206 pass the 4-byte little-endian value in the span; SET_IOMMU accepts only TYPE1v2 (3).
7769note GROUP_SET_CONTAINER 15208 points at a 4-byte fd. GROUP_GET_STATUS 15207 (8), DEVICE_GET_INFO 15211
7770note (24) and DEVICE_GET_REGION_INFO 15212 (32) point at the span after the compiler itself stores
7771note argsz = n, so the kernel copies at most n bytes. GROUP_GET_DEVICE_FD 15210 passes a private zero-
7772note terminated copy of a non-empty span (at most 4095 bytes, no embedded zero).
7773note IOMMU_MAP_DMA 15217 (32) takes an Omega mapping handle in the vaddr field (bytes 8..15), never an
7774note address: the compiler checks the handle like mapget, requires a read/write mapping and flags exactly
7775note READ|WRITE (3), then stores the mapping's own base address and whole length into vaddr and size.
7776note The caller chooses only the IOVA (bytes 16..23). IOMMU_UNMAP_DMA 15218 (24) requires flags 0 (no
7777note dirty bitmap, no ALL, no VADDR) and unmaps only the caller's IOVA range in its own container.
7778mtinit
7779harg x 4 8
7780harg x 2 4
7781if ne get fr add x 12 3
7782fail 9
7783end
7784q = get fr add x 13
7785if eq q 15210
7786e = harray x 0
7787hpathcp x
7788emit 4043309247
7789trapc 0 3
7790fdscan 0
7791mimmed 4043309056 31 12 0
7792fdfound = mbranch 1
7793matconst 0 18446744073709551592
7794full = get oc 0
7795emit 335544320
7796patch19 fdfound get oc 0
7797hload x 4 3
7798hcopy
7799e3 2852126720 0 31 3
7800e3 2852126720 2 31 1
7801matconst 1 15210
7802hsys 29
7803fdrecord 1
7804spadd 4096
7805patch26 full get oc 0
7806hreturn dst x
7807ret
7808end
7809n = ulong 99
7810if eq q 15204
7811n = ulong 0
7812end
7813if or or eq q 15205 eq q 15206 eq q 15208
7814n = ulong 4
7815end
7816if eq q 15207
7817n = ulong 8
7818end
7819if eq q 15211
7820n = ulong 24
7821end
7822if eq q 15212
7823n = ulong 32
7824end
7825if eq q 15217
7826n = ulong 32
7827end
7828if eq q 15218
7829n = ulong 24
7830end
7831if eq n 99
7832fail 9
7833end
7834put fr add x 23 n
7835e = harray x 1
7836t = alloc
7837matconst t get fr add x 23
7838e3 3942645760 31 get fr add x 22 t
7839trapc 1 3
7840save
7841hload x 4 0
7842matconst 1 q
7843basearg 2 e
7844loadarg 16 1 get fr add x 21
7845e3 2332033024 2 2 16
7846if ge n 8
7847matconst 16 n
7848mimmed 3103784960 16 2 0
7849end
7850if eq q 15217
7851mimmed 3107979264 15 2 1
7852mimmed 4043309056 31 15 3
7853trapc 1 3
7854mload 10 2 1
7855mcheck
7856mload 15 11 3
7857mimmed 4043309056 31 15 3
7858trapc 1 3
7859mstore 13 2 1
7860mstore 12 2 3
7861end
7862if eq q 15218
7863mimmed 3107979264 15 2 1
7864mimmed 4043309056 31 15 0
7865trapc 1 3
7866end
7867if or eq q 15205 eq q 15206
7868mimmed 3107979264 2 2 0
7869end
7870if eq q 15206
7871mimmed 4043309056 31 2 3
7872trapc 1 3
7873end
7874if eq n 0
7875matconst 2 0
7876end
7877hsys 29
7878hreturn dst x
7879end
7880if or eq c 229 eq c 230
7881mtinit
7882mnew c dst x
7883end
7884if eq c 231
7885note Fixed eventfd flags: CLOEXEC prevents exec leakage; NONBLOCK makes drained reads safe.
7886harg x 0 4
7887save
7888fdscan 0
7889mimmed 4043309056 31 12 0
7890fdfound = mbranch 1
7891matconst 0 18446744073709551592
7892full = get oc 0
7893emit 335544320
7894patch19 fdfound get oc 0
7895hload x 0 0
7896matconst 1 526336
7897hsys 19
7898fdrecord 2
7899patch26 full get oc 0
7900hreturn dst x
7901end
7902if eq c 232
7903note GET_IRQ_INFO and SET_IRQS only; caller bytes cannot introduce pointers or arbitrary ioctls.
7904harg x 4 8
7905harg x 2 4
7906if ne get fr add x 12 3
7907fail 9
7908end
7909q = get fr add x 13
7910if and ne q 15213 ne q 15214
7911fail 9
7912end
7913e = harray x 1
7914save
7915hload x 4 10
7916fdcheck 1
7917basearg 2 e
7918loadarg 16 1 get fr add x 21
7919e3 2332033024 2 2 16
7920loadarg 3 1 get fr add x 22
7921if eq q 15213
7922mimmed 4043309056 31 3 16
7923trapc 1 3
7924else
7925mimmed 4043309056 31 3 20
7926trapc 3 3
7927mimmed 4043309056 31 3 276
7928trapc 8 3
7929end
7930mimmed 3107979264 4 2 0
7931e3 3942645760 31 4 3
7932trapc 1 3
7933if eq q 15214
7934mimmed 3107979264 5 2 4
7935mimmed 4043309056 31 5 64
7936trapc 8 3
7937mimmed 3107979264 6 2 3
7938matconst 7 4294967295
7939e3 3405774848 7 7 5
7940e3 3942645760 31 6 7
7941trapc 8 3
7942mimmed 3107979264 4 2 1
7943mimmed 4043309056 31 4 36
7944event = mbranch 0
7945mimmed 4043309056 31 3 20
7946trapc 1 3
7947mimmed 4043309056 31 4 33
7948disable = mbranch 0
7949note DATA_NONE MASK or UNMASK. Linux checks the device's MASKABLE capability.
7950mimmed 4043309056 31 4 9
7951mask = mbranch 0
7952mimmed 4043309056 31 4 17
7953trapc 1 3
7954patch19 mask get oc 0
7955mimmed 4043309056 31 5 0
7956trapc 0 3
7957done = get oc 0
7958emit 335544320
7959patch19 disable get oc 0
7960mimmed 4043309056 31 5 0
7961trapc 1 3
7962mimmed 4043309056 31 6 0
7963trapc 1 3
7964disdone = get oc 0
7965emit 335544320
7966patch19 event get oc 0
7967mimmed 4043309056 31 5 0
7968trapc 0 3
7969matconst 7 4
7970e3 2600500224 7 5 7
7971mimmed 2432696320 7 7 20
7972e3 3942645760 31 7 3
7973trapc 1 3
7974mimmed 2432696320 6 2 20
7975head = get oc 0
7976mimmed 3107979264 10 6 0
7977matconst 7 4294967295
7978e3 3942645760 31 10 7
7979skip = mbranch 0
7980fdcheck 2
7981patch19 skip get oc 0
7982mimmed 2432696320 6 6 4
7983mimmed 3506438144 5 5 1
7984mimmed 4043309056 31 5 0
7985again = mbranch 1
7986patch19 again head
7987patch26 done get oc 0
7988patch26 disdone get oc 0
7989end
7990hload x 4 0
7991matconst 1 q
7992hsys 29
7993hreturn dst x
7994end
7995ret
7996end
7997if eq c 197
7998hscalar c dst x
7999ret
8000end
8001note execpure 198 and execenv 233 are dispatched here, before the ge c 173 mapped-memory range below
8002note would claim them.
8003if or eq c 198 eq c 233
8004hpack c dst x
8005ret
8006end
8007if eq c 220
8008hpeercr dst x
8009ret
8010end
8011if eq c 221
8012hpollfds dst x
8013ret
8014end
8015if eq c 222
8016hsendmsg dst x
8017ret
8018end
8019if eq c 224
8020hbuffer c dst x
8021ret
8022end
8023if eq c 176
8024mtinit
8025mnew c dst x
8026ret
8027end
8028if eq c 177
8029mtinit
8030mbind dst x
8031ret
8032end
8033if ge c 173
8034mtinit
8035mmapio c dst x
8036ret
8037end
8038if eq c 172
8039note Linux UVM has unencoded commands. Exact allowlist and ABI580.173.02 byte sizes.
8040put fr add x 23 xhreq 172 x
8041e = harray x 1
8042t = alloc
8043matconst t get fr add x 23
8044e3 3942645760 31 get fr add x 22 t
8045trapc 1 3
8046save
8047hload x 4 0
8048hload x 2 1
8049basearg 2 e
8050loadarg 16 1 get fr add x 21
8051e3 2332033024 2 2 16
8052hsys 29
8053hreturn dst x
8054ret
8055end
8056if eq c 171
8057hdevcall dst x
8058ret
8059end
8060if ge c 167
8061mtinit
8062if eq c 167
8063mnew c dst x
8064ret
8065end
8066if eq c 168
8067harg x 0 9
8068save
8069hload x 0 10
8070mcheck
8071e3 2852126720 0 31 13
8072e3 2852126720 1 31 12
8073hsys 215
8074mimmed 4043309056 31 0 0
8075failed = mbranch 1
8076mstore 31 11 0
8077mstore 31 11 1
8078mstore 31 11 3
8079patch19 failed get oc 0
8080hreturn dst x
8081ret
8082end
8083mcopy c dst x
8084ret
8085end
8086if eq c 166
8087note Same bounded ioctl contract as L605 src/device/omc.om at 0dee699.
8088note Compose with native host words instead of maintaining a second compiler.
8089note Only literal FIONREAD, encoded read/write NVRM F, or five exact aien-gpu ABI1 A requests.
8090put fr add x 23 xhreq 166 x
8091e = harray x 1
8092t = alloc
8093matconst t get fr add x 23
8094e3 3942645760 31 get fr add x 22 t
8095trapc 1 3
8096save
8097hload x 4 0
8098hload x 2 1
8099basearg 2 e
8100loadarg 16 1 get fr add x 21
8101e3 2332033024 2 2 16
8102hsys 29
8103hreturn dst x
8104ret
8105end
8106if eq c 132
8107e = harray x 1
8108save
8109basearg 0 e
8110loadarg 16 1 get fr add x 21
8111e3 2332033024 0 0 16
8112loadarg 1 1 get fr add x 22
8113hsys 17
8114hreturn dst x
8115ret
8116end
8117if eq c 133
8118hreadlnk dst x
8119ret
8120end
8121if eq c 134
8122hsockopt dst x
8123ret
8124end
8125if eq c 135
8126note random A off n flags: Linux getrandom into a writable byte slice. Returns the bytes written or
8127note minus the Linux error number. flags follows getrandom(2): 0 blocks until the pool is ready,
8128note 1 (GRND_NONBLOCK) returns -EAGAIN instead, 2 (GRND_INSECURE) may return non-cryptographic bytes.
8129e = harray x 1
8130harg x 2 8
8131save
8132basearg 0 e
8133loadarg 16 1 get fr add x 21
8134e3 2332033024 0 0 16
8135loadarg 1 1 get fr add x 22
8136hload x 2 2
8137hsys 278
8138hreturn dst x
8139ret
8140end
8141if or eq c 109 eq c 130
8142hclock c dst x
8143ret
8144end
8145if or and ge c 113 le c 119 eq c 131
8146hbuffer c dst x
8147ret
8148end
8149if eq c 120
8150harg x 0 9
8151save
8152hload x 0 2
8153emit 4043309151
8154trapc 11 3
8155spsub 16
8156stkchk
8157matconst 3 1000000000
8158e3 2596276224 4 2 3
8159note MSUB x5,x4,x3,x2 gives the subsecond remainder.
8160emit 2600700037
8161emit 4177527780
8162emit 4177528805
8163emit 2432697312
8164matconst 1 0
8165hsys 101
8166spadd 16
8167hreturn dst x
8168ret
8169end
8170if eq c 123
8171hwaitio dst x
8172ret
8173end
8174if or or or eq c 124 eq c 125 eq c 141 eq c 142
8175hpack c dst x
8176ret
8177end
8178if or or or or or or eq c 126 eq c 138 eq c 143 eq c 144 eq c 150 eq c 151 eq c 157
8179hpath c dst x
8180ret
8181end
8182if eq c 140
8183hstat 140 dst x
8184ret
8185end
8186if eq c 149
8187hstat 149 dst x
8188ret
8189end
8190if and ge c 145 le c 148
8191hscalar c dst x
8192ret
8193end
8194if or or or eq c 153 eq c 154 eq c 155 eq c 156
8195hscalar c dst x
8196ret
8197end
8198if and ge c 158 le c 160
8199hscalar c dst x
8200ret
8201end
8202if or eq c 161 eq c 162
8203hpread c dst x
8204ret
8205end
8206if and ge c 163 le c 165
8207hscalar c dst x
8208ret
8209end
8210if or or eq c 127 eq c 129 eq c 139
8211hscalar c dst x
8212ret
8213end
8214if eq c 128
8215e = harray x 1
8216harg x 2 8
8217tmp = alloc
8218matconst tmp 8
8219e3 3942645760 31 get fr add x 22 tmp
8220trapc 3 3
8221save
8222basearg 0 e
8223loadarg 16 1 get fr add x 21
8224e3 2332033024 0 0 16
8225hload x 2 1
8226hsys 59
8227hreturn dst x
8228ret
8229end
8230if ge c 121
8231put hostargs 0 1
8232hargs c dst x
8233ret
8234end
8235if ge c 110
8236hpath c dst x
8237ret
8238end
8239hscalar c dst x
8240end
8241
8242note ======== rdval: read one value (om4 VAL, VALTOK, CALL and the VT_ parts, one self-recursive
8243note function because a function calls only earlier functions or itself) ========
8244note mode 0: read the first word; 1: the first word is the current token; 2: a call of function fe (index
8245note + 1), its arguments still to read. exp the expected type (0 none), dst the destination register (255
8246note none). Out: rk kind (1 register, 2 bare number, 3 typed number, 4 flags, 9 a float in a twin, only when
8247note asked for by fpw), rv register / number / condition, rt type (for a bare number: 1 when written as 2^63
8248note or more). Only the outermost op writes the destination. Every value nests one level deeper (at most
8249note 1024, error 14); a call does not.
8250fn rdval mode ulong exp ulong dst ulong fe ulong to void
8251note fwtake: rdval's entry: keep the caller's fpw request for this depth (none on x86, which has no twins)
8252if eq get xtarget 0 0
8253put fwv add get depth 0 1 get fpw 0
8254else
8255put fwv add get depth 0 1 0
8256end
8257put fpw 0 0
8258put dexp 0 1
8259if eq mode 2
8260if ne get cpure 0 0
8261fail 30
8262end
8263if ne get om2 0 0
8264if ne get fn_inl sub fe 1 0
8265inl_call fe dst
8266ret
8267end
8268end
8269cb = add mul get depth 0 32 8
8270put fr add cb 18 ulong get fn_np sub fe 1
8271put fr add cb 19 get nslot 0
8272if eq get progmode 0 0
8273if eq get curfn 0 fe
8274fail 9
8275end
8276end
8277i = ulong 0
8278while lt i get fr add cb 18
8279p = add mul sub fe 1 6 i
8280if eq get fn_pk p 0
8281if ne isfloat ulong get fn_pt p 0
8282put fpw 0 1
8283end
8284rdval 0 ulong get fn_pt p 255 0
8285ca_scal p add cb mul i 3
8286else
8287if ge get fn_pk p 6
8288ca_st fe i cb
8289else
8290ca_arr fe i cb
8291fz_arg get rt 0
8292end
8293end
8294i = add i 1
8295end
8296ca_end fe dst cb
8297ret
8298end
8299if eq mode 0
8300tok
8301end
8302if ne get cpure 0 0
8303cp_token
8304end
8305put depth 0 add get depth 0 1
8306if gt get depth 0 1024
8307fail 14
8308end
8309if or eq get tk 0 0 eq get tk 0 3
8310fail 4
8311end
8312if eq get tk 0 4
8313fail 16
8314end
8315if eq get tk 0 5
8316if ne get irmode 0 0
8317fail 9
8318end
8319if ge exp 64
8320fail 7
8321end
8322fparse get ts 0 get tl 0
8323t = ulong 0
8324if eq get fsuf 0 0
8325if eq exp 0
8326vdone 6 add mul get ts 0 16777216 get tl 0 ulong 0
8327ret
8328end
8329if eq isfloat exp 0
8330fail 7
8331end
8332t = exp
8333else
8334t = ulong 10
8335if eq get fsuf 0 2
8336t = ulong 11
8337end
8338if and ne exp 0 ne exp t
8339fail 7
8340end
8341end
8342v = fbits t
8343vdone 7 v t
8344ret
8345end
8346if eq get tk 0 2
8347if eq exp 0
8348vdone 2 get tw 0 get tbig 0
8349ret
8350end
8351note an integer literal where a float is expected is the mixing OMEGESE 9.9 refuses (error 7); before
8352note this it was stored as raw bits (f64 x = 1 held 0x1). The conversion f64 1 reads it with exp 0.
8353if or ge exp 64 ne isfloat exp 0
8354fail 7
8355end
8356range get tw 0 exp get tbig 0
8357vdone 3 get tw 0 exp
8358ret
8359end
8360if gt get tl 0 8
8361f = findfn get tw 0 get tl 0 get ts 0
8362if eq f 0
8363unkfail 13
8364end
8365if eq get fn_ret sub f 1 12
8366fail 7
8367end
8368rdval 2 0 dst f
8369vdec
8370ret
8371end
8372c = kw get tw 0
8373if eq c 178
8374xefival dst
8375ret
8376end
8377if eq c 0
8378if ne get cpure 0 0
8379if eq get tw 0 128009175786866
8380if eq get crkind 0 0
8381fail 30
8382end
8383vdone get crkind 0 get crval 0 get crtype 0
8384ret
8385end
8386end
8387e = findname get tw 0
8388if ne e 0
8389if eq get nm_kind sub e 1 9
8390ct = ulong get nm_type sub e 1
8391cv = get nm_addr sub e 1
8392if ne isfloat ct 0
8393vdone 7 cv ct
8394else
8395vdone 3 cv ct
8396end
8397ret
8398end
8399if ne get nm_kind sub e 1 0
8400fail 7
8401end
8402e1 = sub e 1
8403if ne get nm_fv e1 0
8404nr = ulong get nm_reg e1
8405if ne get fwv get depth 0 0
8406vdone 9 nr ulong get nm_type e1
8407else
8408fv2x ulong get nm_type e1 vtw nr nr
8409vdone 1 nr ulong get nm_type e1
8410end
8411else
8412vdone 1 ulong get nm_reg e1 ulong get nm_type e1
8413end
8414if ne get cpure 0 0
8415if ge get rt 0 10
8416fail 30
8417end
8418end
8419ret
8420end
8421f = findfn get tw 0 0 0
8422if eq f 0
8423unkfail 3
8424end
8425if eq get fn_ret sub f 1 12
8426fail 7
8427end
8428rdval 2 0 dst f
8429vdec
8430ret
8431end
8432if or eq c 88 eq c 89
8433t = ulong 10
8434if eq c 89
8435t = ulong 11
8436end
8437rdval 0 0 255 0
8438if ge get rt 0 64
8439fail 7
8440end
8441if eq get rk 0 6
8442v = fspan t get rv 0
8443put rk 0 7
8444put rv 0 v
8445put rt 0 t
8446end
8447if eq get rk 0 2
8448range get rv 0 9 get tbig 0
8449put rk 0 3
8450put rt 0 9
8451end
8452if eq get rt 0 1
8453fail 7
8454end
8455fwreq
8456vt_conv t
8457vdec
8458ret
8459end
8460if eq c 90
8461if ne get irmode 0 0
8462fail 9
8463end
8464tt = typeword
8465rdval 0 0 255 0
8466if or eq get rk 0 6 eq get rk 0 2
8467fail 11
8468end
8469ft = get rt 0
8470if eq ft 1
8471fail 7
8472end
8473if or ge ft 64 ge tt 64
8474fail 7
8475end
8476if ne twid ft twid tt
8477fail 7
8478end
8479r = matreg get rk 0 get rv 0 ft
8480note A register holds an 8/16/32-bit unsigned value and an f32 zero-extended (canon, FMOV W) and a signed
8481note one sign-extended, so a bitcast that changes the extension extends the bits again for tt, into a new
8482note register (a name's register keeps its value). f32 takes the uint form.
8483if ne twid tt 64
8484if ne and ge ft 6 le ft 8 and ge tt 6 le tt 8
8485ct = tt
8486if eq tt 10
8487ct = ulong 4
8488end
8489rd = destreg dst
8490canonto rd r ct
8491r = rd
8492end
8493end
8494put rk 0 1
8495put rv 0 r
8496put rt 0 tt
8497vdec
8498ret
8499end
8500if or or or eq c 91 eq c 92 eq c 93 eq c 94
8501fwreq
8502if or eq c 91 eq c 92
8503fintr c dst
8504else
8505fminmax c dst
8506end
8507ret
8508end
8509if eq c 227
8510fwreq
8511ffma dst
8512ret
8513end
8514if eq c 95
8515fsizeof dst
8516ret
8517end
8518if eq c 96
8519foffset dst
8520ret
8521end
8522if le sub c 1 8
8523tok
8524if eq get tk 0 2
8525range get tw 0 c get tbig 0
8526vdone 3 get tw 0 c
8527ret
8528end
8529rdval 1 0 255 0
8530if ge get rt 0 64
8531fail 7
8532end
8533if eq get rk 0 2
8534fail 11
8535end
8536vt_conv c
8537vdec
8538ret
8539end
8540if eq c 223
8541tok
8542if eq get tk 0 2
8543range get tw 0 13 get tbig 0
8544vdone 3 get tw 0 13
8545ret
8546end
8547rdval 1 0 255 0
8548if eq get rk 0 6
8549fail 7
8550end
8551if ge get rt 0 64
8552fail 7
8553end
8554if eq get rk 0 2
8555fail 11
8556end
8557vt_conv 13
8558vdec
8559ret
8560end
8561if eq c 74
8562put fpw 0 get fwv get depth 0
8563rdval 0 exp dst 0
8564tok
8565if ne get tk 0 1
8566fail 16
8567end
8568if gt get tl 0 8
8569fail 16
8570end
8571if ne kw get tw 0 75
8572fail 16
8573end
8574vdec
8575ret
8576end
8577if or eq c 77 eq c 193
8578fail 9
8579end
8580if or eq c 225 eq c 226
8581if or eq get progmode 0 0 ne get irmode 0 0
8582fail 9
8583end
8584x = mul get depth 0 32
8585sl = get nslot 0
8586note dit and scrub are emitted inline here, not in helper functions: om4 holds 512 functions and omc.om
8587note uses 511 of them (seed/omc/size-history.tsv).
8588if eq c 225
8589note dit: OM-2 `dit v` (program mode): v is int, 0 or 1, else trap 3. Sets PSTATE.DIT (Arm ARM
8590note C5.2.4: DIT is bit 24 of the special-purpose register; MRS/MSR <Xt>, DIT have op0 3, op1 3, CRn 4,
8591note CRm 2, op2 5) and gives the previous bit as a long 0 or 1. CMP Xv, #1 = SUBS XZR (C6.2.464); B.HI
8592note trap 3; LSL X16, Xv, #24 = UBFM X16, Xv, #40, #39 (C6.2.487), taken before the destination is
8593note written because the destination may be v's own register; MRS Xd, DIT (C6.2.287); MSR DIT, X16
8594note (C6.2.289); UBFX Xd, Xd, #24, #1 = UBFM Xd, Xd, #24, #24. Writes to PSTATE are in program order
8595note without further synchronization (C5.1), so the next instruction already runs under the new bit.
8596harg x 0 8
8597r = matreg get fr add x 8 get fr add x 9 8
8598emit or 4043310111 shl r 5
8599trapc 8 3
8600emit or 3546848272 shl r 5
8601put nslot 0 sl
8602d = destreg dst
8603emit or 3577430688 d
8604emit 3575333552
8605emit or or 3545784320 shl d 5 d
8606vdone 1 d 9
8607ret
8608end
8609note scrub: OM-2 `scrub n` (program mode): n is ulong, a multiple of 16 and at most 1048576, else trap 3.
8610note TST Xn, #15 = ANDS XZR (C6.2.16, N 1 immr 0 imms 3); B.NE trap 3; X16 = 1048576; CMP Xn, X16 =
8611note SUBS XZR (C6.2.465); B.HI trap 3. X16 = SP - n: SUB X16, SP, Xn, UXTX (C6.2.456); CMP X16, X28
8612note (C6.2.465); B.LO trap 6, before any store. Loop: CMP SP, X16 = SUBS XZR, SP, X16, UXTX (C6.2.463);
8613note B.LS out; STP XZR, XZR, [X16], #16 (C6.2.414, post-index); B back (C6.2.35). Then MOVZ Xr, #0
8614note (C6.2.319) for every pool register at index npar + nslot or above (no live value: parameters,
8615note locals and the held parts of the expression sit below), X16, and X7, X18-X26, X29, which program
8616note mode never names; then MOVI Vd.2D, #0 (C7.2.247) for V0-V15 and for every twin V16+j except those
8617note of a live slot j (below npar + nslot) whose value lives in its twin (vsl: an f32/f64 local name or a
8618note held kind 9 float, as save stores it); a twin of a free slot or of a slot whose value is in its x
8619note register holds nothing live. V0-V15 carry a float only inside one operation. Never touched: SP, X27
8620note (the Linux initial stack under args), X28 (the stack floor), X30 (the return address), the live pool
8621note registers and live twins, NZCV, FPCR, FPSR, and the SVE P registers and FFR. Gives long 0.
8622harg x 0 5
8623r = matreg get fr add x 8 get fr add x 9 5
8624emit or 4064283679 shl r 5
8625trapc 1 3
8626matconst 16 1048576
8627emit or 3943694367 shl r 5
8628trapc 8 3
8629emit or 3407897584 shl r 16
8630emit 3944481311
8631trapc 3 6
8632top = get oc 0
8633emit 3945817087
8634at = get oc 0
8635emit 1409286153
8636emit 2827058719
8637back top
8638patch19 at get oc 0
8639i = add get npar 0 sl
8640while lt i 16
8641emit or 3531603968 poolc i
8642i = add i 1
8643end
8644emit 3531603984
8645emit 3531603975
8646i = ulong 18
8647while le i 26
8648emit or 3531603968 i
8649i = add i 1
8650end
8651emit 3531603997
8652at = add get npar 0 sl
8653i = ulong 0
8654while lt i 32
8655top = and i 15
8656r = ulong get vsl top
8657if or lt i 16 ge top at
8658r = ulong 0
8659end
8660if eq r 0
8661emit or 1862329344 i
8662end
8663i = add i 1
8664end
8665put nslot 0 sl
8666d = destreg dst
8667matconst d 0
8668vdone 1 d 9
8669ret
8670end
8671if or or and ge c 100 le c 177 and ge c 197 le c 198 and ge c 220 le c 222
8672hostval c dst
8673ret
8674end
8675if eq c 224
8676hostval c dst
8677ret
8678end
8679if or and ge c 228 le c 232 eq c 233
8680hostval c dst
8681ret
8682end
8683if eq c 186
8684rdval 0 0 255 0
8685ab_open
8686vdec
8687ret
8688end
8689if eq c 185
8690ab_seal
8691vdec
8692ret
8693end
8694if eq c 179
8695cnt_word dst
8696ret
8697end
8698if eq c 51
8699e = arrname
8700if ge get nm_kind sub e 1 6
8701vt_fget e dst
8702vdec
8703ret
8704end
8705if ne ly_get e dst 0
8706vdec
8707ret
8708end
8709rdval 0 5 255 0
8710fz_get e
8711fwreq
8712vt_get e dst
8713vdec
8714ret
8715end
8716if eq c 52
8717e = arrname
8718if ge get nm_kind sub e 1 6
8719fail 7
8720end
8721if ne unsz sub e 1 0
8722vdone 1 ulong get nm_lr sub e 1 5
8723ret
8724end
8725vdone 3 get nm_len sub e 1 5
8726ret
8727end
8728if or eq c 53 eq c 54
8729x = mul get depth 0 32
8730put fr add x 8 get nslot 0
8731rdval 0 8 255 0
8732vs_fd x
8733e = arrname
8734vs_arr c e
8735rdval 0 5 255 0
8736vs_off x
8737rdval 0 5 255 0
8738vs_end c e dst x
8739ret
8740end
8741if eq c 55
8742vdone 3 1 1
8743ret
8744end
8745if eq c 56
8746vdone 3 0 1
8747ret
8748end
8749if and gt sub c 33 17 gt sub c 59 1
8750fail 16
8751end
8752if and ge c 49 le c 50
8753put fpw 0 1
8754rdval 0 exp 255 0
8755if ge get rt 0 64
8756fail 7
8757end
8758if and eq c 50 eq get rk 0 4
8759put rv 0 xor get rv 0 1
8760vdec
8761ret
8762end
8763if eq get rk 0 2
8764fail 11
8765end
8766fwreq
8767vt_un c dst
8768vdec
8769ret
8770end
8771put fpw 0 1
8772rdval 0 0 255 0
8773ak = get rk 0
8774av = get rv 0
8775at = get rt 0
8776if eq ak 4
8777av = matreg 4 av at
8778ak = ulong 1
8779at = ulong 1
8780end
8781put fpw 0 1
8782rdval 0 0 255 0
8783bk = get rk 0
8784bv = get rv 0
8785bt = get rt 0
8786if eq bk 4
8787bv = matreg 4 bv bt
8788bk = ulong 1
8789bt = ulong 1
8790end
8791if or ge at 64 ge bt 64
8792x = mul get depth 0 32
8793put fr x ak
8794put fr add x 1 av
8795put fr add x 2 at
8796put fr add x 3 bk
8797put fr add x 4 bv
8798put fr add x 5 bt
8799ab_bin c dst x
8800vdec
8801ret
8802end
8803if and eq ak 6 eq bk 6
8804fail 11
8805end
8806if or ne isfloat at 0 ne isfloat bt 0
8807if eq ak 6
8808if eq bt 0
8809fail 11
8810end
8811if eq isfloat bt 0
8812fail 7
8813end
8814av = fspan bt av
8815ak = ulong 7
8816at = bt
8817end
8818if eq bk 6
8819if eq at 0
8820fail 11
8821end
8822if eq isfloat at 0
8823fail 7
8824end
8825bv = fspan at bv
8826bk = ulong 7
8827bt = at
8828end
8829if or eq ak 2 eq ak 3
8830fail 7
8831end
8832if or eq bk 2 eq bk 3
8833fail 7
8834end
8835end
8836if or eq ak 6 eq bk 6
8837fail 7
8838end
8839if eq ak 2
8840if eq bk 2
8841fail 11
8842end
8843range av bt at
8844at = bt
8845ak = ulong 3
8846end
8847if eq bk 2
8848range bv at bt
8849bt = at
8850bk = ulong 3
8851end
8852if and ne c 41 ne c 42
8853if ne at bt
8854fail 7
8855end
8856end
8857x = mul get depth 0 32
8858put fr x ak
8859put fr add x 1 av
8860put fr add x 2 at
8861put fr add x 3 bk
8862put fr add x 4 bv
8863put fr add x 5 bt
8864if ne isfloat at 0
8865fwreq
8866end
8867binop c dst x
8868vdec
8869end
8870
8871note cond: read a condition (a bool value); emit a branch taken when it is false: B.cond with the opposite
8872note condition (C6.2.34), or CBZ Xr (C6.2.78). Returns that branch's place. om4 COND.
8873fn cond to ulong
8874if ne get xtarget 0 0
8875rdval 0 1 255 0
8876if ne get rt 0 1
8877fail 7
8878end
8879if eq get rk 0 4
8880ret xbranch xor get rv 0 1
8881end
8882r = matreg get rk 0 get rv 0 get rt 0
8883xcmp r 0 1
8884ret xbranch 0
8885end
8886rdval 0 1 255 0
8887if ne get rt 0 1
8888fail 7
8889end
8890if eq get rk 0 4
8891at = get oc 0
8892emit or 1409286144 xor get rv 0 1
8893ret at
8894end
8895r = matreg get rk 0 get rv 0 get rt 0
8896at = get oc 0
8897emit or 3019898880 r
8898ret at
8899end
8900
8901note ======== statements, one per line (om4 STMT and the S_ parts) ========
8902note skipnl: note ...: the rest of the line is not read (up to the newline, which tok reads). om4 S_NOTE.
8903fn skipnl to void
8904go = true
8905while go
8906n = ulong 0
8907while and go lt n 999999
8908if ge get cur 0 get inlen 0
8909go = false
8910else
8911if eq get inp get cur 0 10
8912go = false
8913else
8914put cur 0 add get cur 0 1
8915end
8916end
8917n = add n 1
8918end
8919end
8920end
8921
8922note blo: B.LO back to an earlier place (C6.2.34)
8923fn blo at ulong to void
8924if ne get xtarget 0 0
8925p = xbranch 3
8926xpatch p at
8927ret
8928end
8929d = bdisp get oc 0 at 19
8930emit or 1409286147 shl d 5
8931end
8932
8933note sa_arr: x = new T N v: N elements of type T on the stack, each set to v; only in the function's
8934note outermost block, so the stack held by local arrays is known at every ret. SUB SP (C6.2.457); program
8935note mode: the stack check; MOV Xb, SP (ADD immediate, C6.2.5); then the fill loop: STR v, [Xb, Xi, LSL];
8936note ADD Xi, Xi, #1; CMP Xi, #N or Xn; B.LO back (C6.2.34). om4 SA_ARR.
8937fn sa_arr w ulong to void
8938if ne get xtarget 0 0
8939xarray w
8940ret
8941end
8942if ne get nb 0 0
8943fail 9
8944end
8945t = typeword
8946elemtype t
8947n = lenval 16
8948arrlen n
8949by = shl shr add shl n tlog t 15 4 4
8950s = add get stkbytes 0 by
8951if gt s 4194304
8952fail 23
8953end
8954put stkbytes 0 s
8955r = alloc
8956put held 0 get nslot 0
8957rdval 0 t 255 0
8958if ne get rt 0 t
8959fail 7
8960end
8961v = matreg get rk 0 get rv 0 get rt 0
8962spsub by
8963stkchk
8964emit or 2432697312 r
8965i = alloc
8966emit or 3531603968 i
8967m = ulong 0
8968if gt n 4095
8969m = alloc
8970matconst m n
8971end
8972lp = get oc 0
8973emit or or or stop t v shl r 5 shl i 16
8974emit or or 2432697344 i shl i 5
8975if le n 4095
8976emit or or 4043309087 shl n 10 shl i 5
8977else
8978emit or or 3942645791 shl m 16 shl i 5
8979end
8980blo lp
8981addname w r t n 1 0
8982end
8983
8984note s_assign: name [=] value (an existing scalar, or a new one), or name = new T N v. om4 S_ASSIGN. A new
8985note f32/f64 name lives in the twin of its register (nm_fv, vsl): the value is made there when it can be.
8986fn s_assign w ulong to void
8987tok
8988if eq get tk 0 1
8989if le get tl 0 8
8990if eq kw get tw 0 73
8991tok
8992end
8993end
8994end
8995e = findname w
8996if ne e 0
8997i = sub e 1
8998if ne get nm_kind i 0
8999fail 7
9000end
9001r = ulong get nm_reg i
9002t = ulong get nm_type i
9003if ne get nm_fv i 0
9004put fpw 0 add r 2
9005rdval 1 t 255 0
9006if ne get rt 0 t
9007fail 7
9008end
9009fvset get rk 0 get rv 0 t r
9010eol
9011ret
9012end
9013rdval 1 t r 0
9014if ne get rt 0 t
9015fail 7
9016end
9017force get rk 0 get rv 0 get rt 0 r
9018eol
9019ret
9020end
9021if ne findfn w 0 0 0
9022fail 10
9023end
9024if eq get tk 0 1
9025if le get tl 0 8
9026if eq kw get tw 0 84
9027sa_new w
9028eol
9029ret
9030end
9031end
9032end
9033r = alloc
9034put held 0 get nslot 0
9035put fpw 0 add r 2
9036rdval 1 0 r 0
9037if eq get rk 0 2
9038fail 11
9039end
9040t = get rt 0
9041if and ne isfloat t 0 eq get xtarget 0 0
9042fvset get rk 0 get rv 0 t r
9043put vsl regidx r uchar 1
9044addname w r t 0 0 0
9045put nm_fv sub get nn 0 1 uchar 1
9046else
9047force get rk 0 get rv 0 t r
9048addname w r t 0 0 0
9049end
9050eol
9051end
9052
9053note s_name: a name at the start of a line: a call of a void function, or name [=] value. om4 S_NAME.
9054fn s_name w ulong to void
9055if ne findname w 0
9056s_assign w
9057ret
9058end
9059f = findfn w get tl 0 0
9060if eq f 0
9061s_assign w
9062ret
9063end
9064if ne get fn_ret sub f 1 12
9065fail 7
9066end
9067rdval 2 0 255 f
9068eol
9069end
9070
9071note fn_p: the type of parameter np (name pw) of function f (index): T, or view / edit T N (an array:
9072note kind 2 / 3, element type, length 1 .. 2^24). om4 FN_P .. FN_ADD.
9073fn fn_p f ulong np ulong pw ulong to void
9074tok
9075if ne get tk 0 1
9076fail 16
9077end
9078if gt get tl 0 8
9079fail 16
9080end
9081c = kw get tw 0
9082t = ulong 0
9083ln = ulong 0
9084k = ulong 0
9085if eq c 194
9086fn_p_out f np pw
9087ret
9088end
9089if or eq c 57 eq c 58
9090k = sub c 55
9091sid = peek_struct
9092if ne sid 0
9093fn_p_st f np pw sid k
9094ret
9095end
9096if ne get om2 0 0
9097lid = peek_lay
9098if ne lid 0
9099fn_p_ly f np pw lid k
9100ret
9101end
9102end
9103t = typeword
9104elemtype t
9105if and ne get om2 0 0 ne peekstar 0
9106tok
9107else
9108ln = lenval 16
9109arrlen ln
9110if ne get om2 0 0
9111if eq peekw 183
9112tok
9113fz_par f np k ln
9114end
9115end
9116end
9117else
9118if eq c 88
9119t = ulong 10
9120else
9121if eq c 89
9122t = ulong 11
9123else
9124if eq c 223
9125t = ulong 13
9126else
9127if gt sub c 1 8
9128t = abty
9129else
9130t = c
9131end
9132end
9133end
9134end
9135end
9136addname pw poolc get psl 0 t ln k 0
9137if and ne k 0 eq ln 0
9138put nm_lr sub get nn 0 1 uchar poolc add get psl 0 1
9139end
9140fz_bind add mul f 6 np ln
9141p = add mul f 6 np
9142put fn_pt p uchar t
9143put fn_pl p ln
9144put fn_pk p uchar k
9145end
9146
9147note fn_p_out (OM-2, spec 6, erratum E1): `NAME out T N`, a sized array the unit writes and the bridge owns
9148note (copied out only after status 0). Inside the unit it is an edit of T N (kind 3) in one slot, its
9149note pointer; a length `*` is error 16, and so is `fresh` after it.
9150fn fn_p_out f ulong np ulong pw ulong to void
9151t = typeword
9152elemtype t
9153ln = lenval 16
9154arrlen ln
9155addname pw poolc get psl 0 t ln 3 0
9156p = add mul f 6 np
9157put fn_pt p uchar t
9158put fn_pl p ln
9159put fn_pk p 3
9160end
9161
9162note fn_to: to T | to void at the end of a fn line; the function's state starts. om4 FN_TO .. FN_T2.
9163fn fn_to f ulong np ulong to void
9164tok
9165if eq get tk 0 0
9166fail 4
9167end
9168if ne get tk 0 1
9169fail 16
9170end
9171if gt get tl 0 8
9172fail 16
9173end
9174c = kw get tw 0
9175r = c
9176if eq c 76
9177r = ulong 12
9178else
9179if eq c 88
9180r = ulong 10
9181else
9182if eq c 89
9183r = ulong 11
9184else
9185if eq c 223
9186r = ulong 13
9187else
9188if gt sub c 1 8
9189r = abty
9190else
9191r = c
9192end
9193end
9194end
9195end
9196end
9197put curret 0 r
9198put fn_ret f uchar r
9199put fn_np f uchar np
9200put stkbytes 0 0
9201put dead 0 0
9202put npar 0 get psl 0
9203put nslot 0 0
9204for j ulong 16
9205put vsl j 0
9206end
9207put held 0 0
9208put nb 0 0
9209put nt 0 0
9210fn_tail f
9211if and ne get xtarget 0 0 eq get scanning 0 0
9212xpro get psl 0
9213end
9214end
9215
9216note s_fn: fn name (param type | param view T N | param edit T N)* to type | void. om4 S_FN.
9217note A name of 9..63 bytes is never a keyword: tw packs only its first 8 bytes (continue_x is not continue).
9218fn s_fn to void
9219if ne get curret 0 0
9220fail 15
9221end
9222tok
9223if ne get tk 0 1
9224fail 16
9225end
9226if gt get tl 0 63
9227fail 13
9228end
9229w = get tw 0
9230if le get tl 0 8
9231if ne kw w 0
9232fail 16
9233end
9234end
9235l = get tl 0
9236s = get ts 0
9237f = ulong 0
9238if eq get scanning 0 1
9239newname w l s
9240f = get nf 0
9241if ge f 4096
9242fail 14
9243end
9244put nf 0 add f 1
9245put curfn 0 add f 1
9246put lastfn 0 add f 1
9247put fn_name f w
9248put fn_addr f get oc 0
9249put fn_ret f 0
9250put fn_np f 0
9251put fn_nmlen f l
9252put fn_off f s
9253put fn_exp f uchar get expf 0
9254else
9255f = sub findfn w l s 1
9256put curfn 0 add f 1
9257put lastfn 0 add f 1
9258put fn_addr f get oc 0
9259put fstart 0 get oc 0
9260put fuel 0 0
9261put fz_n 0 0
9262end
9263put nn 0 get nglob 0
9264put psl 0 0
9265np = ulong 0
9266go = true
9267while go
9268tok
9269if eq get tk 0 0
9270fail 4
9271end
9272if ne get tk 0 1
9273fail 16
9274end
9275if gt get tl 0 8
9276fail 13
9277end
9278pw = get tw 0
9279c = kw pw
9280if eq c 65
9281go = false
9282else
9283if ne c 0
9284fail 16
9285end
9286newname pw get tl 0 get ts 0
9287if ge get psl 0 6
9288fail 9
9289end
9290fn_p f np pw
9291put fn_pn add mul f 6 np pw
9292put psl 0 add get psl 0 pwid add mul f 6 np
9293if gt get psl 0 6
9294fail 9
9295end
9296np = add np 1
9297end
9298end
9299fn_to f np
9300ab_entry f
9301if eq get scanning 0 0
9302cp_entry
9303end
9304if eq get scanning 0 1
9305put curret 0 0
9306note keep the top-level consts read so far: a later const must not take an earlier one's slot.
9307put nn 0 get nglob 0
9308end
9309end
9310
9311note s_ret: ret [value]: the value into X0, give back the local arrays' stack, RET. om4 S_RET.
9312fn s_ret to void
9313if ne get inl 0 0
9314s_iret
9315ret
9316end
9317r = get curret 0
9318if eq r 12
9319eol
9320cp_post 0 0 r
9321epi
9322dead1
9323ret
9324end
9325dst = ulong 0
9326if and ne get cfhas 0 0 eq get crel 0 0
9327dst = ulong 255
9328end
9329rdval 0 r dst 0
9330if ne get rt 0 r
9331fail 7
9332end
9333if and ne get cfhas 0 0 eq get crel 0 0
9334rr = matreg get rk 0 get rv 0 r
9335eol
9336cp_post 1 rr r
9337force 1 rr r 0
9338else
9339force get rk 0 get rv 0 get rt 0 0
9340eol
9341cp_post 1 0 r
9342end
9343epi
9344dead1
9345end
9346
9347note s_break: leave the innermost loop: find the nearest block of kind 3 (while) or 4 (for), emit an
9348note always-taken B.cond to the loop's exit and record the word for s_end to patch. Outside a loop the
9349note line is error 9. Like ret, the line ends the path (a later line in the same block is error 17).
9350fn s_break to void
9351j = get nb 0
9352found = ulong 0
9353bi = ulong 0
9354while and eq found 0 gt j 0
9355j = sub j 1
9356if ge get bkind j 3
9357found = ulong 1
9358bi = j
9359end
9360end
9361if eq found 0
9362fail 9
9363end
9364if ge get bkind bi 5
9365fail 9
9366end
9367n = get nbrk 0
9368if ge n 1024
9369fail 14
9370end
9371put brkpos n get oc 0
9372put brkblk n bi
9373put nbrk 0 add n 1
9374if ne get xtarget 0 0
9375p = xbranch 14
9376else
9377emit 1409286158
9378end
9379dead1
9380eol
9381end
9382
9383note s_cont: continue: like break it finds the innermost loop, but its exit is the latch: the head of a
9384note while, the increment of a for (patched by s_end). Outside a loop it is error 9; the line ends the
9385note path (error 17 for a later line in the same block).
9386fn s_cont to void
9387j = get nb 0
9388found = ulong 0
9389bi = ulong 0
9390while and eq found 0 gt j 0
9391j = sub j 1
9392if ge get bkind j 3
9393found = ulong 1
9394bi = j
9395end
9396end
9397if eq found 0
9398fail 9
9399end
9400if ge get bkind bi 5
9401fail 9
9402end
9403n = get ncont 0
9404if ge n 1024
9405fail 14
9406end
9407put cpos n get oc 0
9408put cblk n bi
9409put ncont 0 add n 1
9410if ne get xtarget 0 0
9411p = xbranch 14
9412else
9413emit 1409286158
9414end
9415dead1
9416eol
9417end
9418
9419note stel: the checked store of v (type t) at ea_b / ea_i (the STOP table: register offset, scaled)
9420fn stel t ulong v ulong to void
9421if ne get xtarget 0 0
9422xmemval 1 get ea_b 0 get ea_i 0 t v
9423ret
9424end
9425eaem stop t v
9426end
9427
9428note s_put: put A i v: index, then value, then the checked store (STOP table). om4 S_PUT.
9429fn s_put to void
9430e = arrname
9431k = get nm_kind sub e 1
9432if ge k 6
9433s_putf e
9434ret
9435end
9436if or eq k 2 eq k 5
9437fail 20
9438end
9439if ne ly_put e 0
9440ret
9441end
9442rdval 0 5 255 0
9443ik = get rk 0
9444iv = get rv 0
9445it = get rt 0
9446t = ulong get nm_type sub e 1
9447if ne isfloat t 0
9448put fpw 0 1
9449end
9450rdval 0 t 255 0
9451if ne get rt 0 t
9452fail 7
9453end
9454if eq get rk 0 9
9455vv = vtw get rv 0
9456fz_put e ik iv
9457elemaddr e ik iv it
9458eaem or stop t 67108864 vv
9459eol
9460ret
9461end
9462v = matreg get rk 0 get rv 0 get rt 0
9463fz_put e ik iv
9464elemaddr e ik iv it
9465stel t v
9466eol
9467end
9468
9469note s_const: const NAME TYPE LITERAL at top level. The value is a typed number (integer or float)
9470note read wherever a value goes; it may not be assigned and is refused in IR mode.
9471fn s_const to void
9472tok
9473if ne get tk 0 1
9474fail 16
9475end
9476if gt get tl 0 8
9477fail 13
9478end
9479w = get tw 0
9480if ne kw w 0
9481fail 16
9482end
9483newname w get tl 0 get ts 0
9484t = typeword
9485if ge t 64
9486fail 7
9487end
9488if ne isfloat t 0
9489note float constant: rdval reads the float token as a value (kind 6); fspan converts it to the
9490note target type's bits and rdval has already balanced the nesting depth.
9491rdval 0 0 255 0
9492if ne get rk 0 6
9493fail 16
9494end
9495v = fspan t get rv 0
9496addname w 0 t 0 9 v
9497put nglob 0 get nn 0
9498eol
9499ret
9500end
9501tok
9502if eq get tk 0 0
9503fail 4
9504end
9505if eq get tk 0 5
9506fail 7
9507end
9508if ne get tk 0 2
9509fail 16
9510end
9511v = get tw 0
9512range v t get tbig 0
9513addname w 0 t 0 9 v
9514put nglob 0 get nn 0
9515eol
9516end
9517
9518note s_global: global NAME T N: a fixed pool of N elements, zero at start, in the read/write part of the
9519note program image after the 8 MiB stack pool (address 0x10800000 + pool bytes so far); at most 1 GiB of
9520note pools (error 23). om4 S_GLOBAL.
9521fn s_global to void
9522w = topname
9523t = typeword
9524elemtype t
9525n = lenval 16
9526arrlen n
9527by = shl shr add shl n tlog t 15 4 4
9528pb = get poolb 0
9529a = add 276824064 pb
9530s = add pb by
9531if gt s 1073741824
9532fail 23
9533end
9534put poolb 0 s
9535addname w 0 t n 4 a
9536put nglob 0 get nn 0
9537eol
9538end
9539
9540note s_text: text NAME "...": read-only bytes in the program image (escapes \n \t \\ \", any other is error
9541note 22), padded to a word, with their length; address 0x400000 + its place. om4 S_TEXT.
9542fn s_text to void
9543w = topname
9544tok
9545if ne get tk 0 4
9546fail 16
9547end
9548br = ulong 0
9549if and ne get xtarget 0 0 ne get tl 0 0
9550br = xbranch 14
9551end
9552a = add get oc 0 4194304
9553p = get tw 0
9554e = add p get tl 0
9555n = ulong 0
9556go = true
9557while go
9558m = ulong 0
9559while and go lt m 999999
9560if ge p e
9561go = false
9562else
9563b = get inp p
9564p = add p 1
9565if eq b 92
9566b = get inp p
9567p = add p 1
9568if eq b 110
9569b = uchar 10
9570else
9571if eq b 116
9572b = uchar 9
9573else
9574if and ne b 92 ne b 34
9575fail 22
9576end
9577end
9578end
9579end
9580emitb b
9581n = add n 1
9582end
9583m = add m 1
9584end
9585end
9586note Empty text carries no data or padding; its zero count forbids every access.
9587if ne n 0
9588while ne and get oc 0 3 0
9589emitb 0
9590end
9591end
9592if ne br 0
9593xpatch br get oc 0
9594end
9595addname w 0 2 n 5 a
9596put nglob 0 get nn 0
9597eol
9598end
9599
9600note s_else: jump over the else part (B, C6.2.35); the if's false branch lands after that jump. om4 S_ELSE.
9601fn s_else to void
9602n = get nb 0
9603if eq n 0
9604fail 15
9605end
9606i = sub n 1
9607if ne get bkind i 1
9608fail 15
9609end
9610b = get oc 0
9611if ne get xtarget 0 0
9612note A terminated then-path needs no skip jump, particularly at a bare unit's final byte.
9613if eq get dead 0 0
9614p = xbranch 14
9615end
9616else
9617emit 335544320
9618end
9619patch19 get bbr i get oc 0
9620put bkind i 2
9621put bbr i b
9622fz_else i
9623put nn 0 get bnames i
9624put held 0 get bheld i
9625put bdead i get dead 0
9626put dead 0 0
9627eol
9628end
9629
9630note lpcnt: the hidden counter check of a loop: CMP Xc, #N (C6.2.96) or CMP Xc, Xn (C6.2.97); B.HS trap
9631note 4; ADD Xc, Xc, #1 (C6.2.5). om4 SW_NOB .. SW_G and the same in S_FOR.
9632fn lpcnt c ulong n ulong to void
9633if ne get xtarget 0 0
9634xcmp c n 1
9635trapc 2 4
9636xarith 33 5 c 1 1 c
9637ret
9638end
9639if le n 4095
9640emit or or 4043309087 shl n 10 shl c 5
9641else
9642m = alloc
9643matconst m n
9644emit or or 3942645791 shl c 5 shl m 16
9645end
9646trapc 2 4
9647emit or or 2432697344 c shl c 5
9648end
9649
9650note s_while: while cond [bound N]: hidden counter = 0; head: cond false -> exit; counter >= N -> trap 4;
9651note counter = counter + 1; body; B head. N is 1 .. 9223372036854775807 (error 8), 1000000 with no bound. om4 S_WHILE.
9652fn s_while to void
9653sn = get nn 0
9654sh = get held 0
9655c = alloc
9656put held 0 get nslot 0
9657if ne get xtarget 0 0
9658matconst c 0
9659else
9660emit or 3531603968 c
9661end
9662hd = get oc 0
9663cp_head skipln get cur 0 add get line 0 1
9664br = cond
9665n = ulong 1000000
9666tok
9667k = get tk 0
9668if and ne k 0 ne k 3
9669if ne k 1
9670fail 5
9671end
9672if gt get tl 0 8
9673fail 5
9674end
9675if ne kw get tw 0 70
9676fail 5
9677end
9678v = lenval 4
9679if ge sub v 1 9223372036854775807
9680fail 8
9681end
9682n = v
9683eol
9684end
9685lpcnt c n
9686pushb br 3 hd c 0
9687pushnh sn sh
9688cp_skip
9689end
9690
9691note sf_br: B.HS (unsigned) or B.GE (signed) to the loop exit, C6.2.34 (distance filled at end); returns
9692note its place. om4 SF_BR.
9693fn sf_br t ulong to ulong
9694if ne get xtarget 0 0
9695if lt t 6
9696ret xbranch 2
9697end
9698ret xbranch 10
9699end
9700at = get oc 0
9701if lt t 6
9702emit 1409286146
9703else
9704emit 1409286154
9705end
9706ret at
9707end
9708
9709note 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.
9710note With `bound N` a hidden counter c = 0 is checked after the exit test, as in s_while. om4 SF_NUM .. SF_PUSH.
9711note The optional `bound N` before the line's end: N a number or an integer constant, 1 ..
9712note 9223372036854775807 (error 8, as s_while; 4 when N is missing); bn = 0 when no bound is written.
9713fn sf_num w ulong t ulong kk ulong sn ulong sh ulong to void
9714bn = ulong 0
9715if eq peekw 70
9716tok
9717bn = lenval 4
9718if ge sub bn 1 9223372036854775807
9719fail 8
9720end
9721end
9722if ne get xtarget 0 0
9723xfornum w t kk sn sh bn
9724ret
9725end
9726eol
9727i = alloc
9728put held 0 get nslot 0
9729addname w i t 0 0 0
9730emit or 3531603968 i
9731c = ulong 0
9732if ne bn 0
9733c = alloc
9734put held 0 get nslot 0
9735emit or 3531603968 c
9736end
9737hd = get oc 0
9738cp_head get cur 0 get line 0
9739if le kk 4095
9740emit or or 4043309087 shl kk 10 shl i 5
9741else
9742k = alloc
9743matconst k kk
9744emit or or 3942645791 shl i 5 shl k 16
9745end
9746br = sf_br t
9747if ne bn 0
9748lpcnt c bn
9749end
9750pushb br 4 hd i t
9751pushnh sn sh
9752cp_skip
9753end
9754
9755note 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
9756note Xi, Xk; B.HS/B.GE exit; the counter guard of a while: `bound N` (read as in sf_num), else 1000000.
9757note om4 SF_VAL.
9758fn sf_val w ulong t ulong kr ulong sn ulong sh ulong to void
9759n = ulong 1000000
9760if eq peekw 70
9761tok
9762n = lenval 4
9763if ge sub n 1 9223372036854775807
9764fail 8
9765end
9766end
9767if ne get xtarget 0 0
9768xforval w t kr sn sh n
9769ret
9770end
9771eol
9772i = alloc
9773put held 0 get nslot 0
9774addname w i t 0 0 0
9775emit or 3531603968 i
9776c = alloc
9777put held 0 get nslot 0
9778emit or 3531603968 c
9779hd = get oc 0
9780cp_head get cur 0 get line 0
9781emit or or 3942645791 shl kr 16 shl i 5
9782br = sf_br t
9783lpcnt c n
9784pushb br 4 hd i t
9785pushnh sn sh
9786cp_skip
9787end
9788
9789note s_for: for i T K [bound N]: i = 0; head: not i < K -> exit; body; latch i = add i 1; B head. K a number,
9790note or a value of type T: a name is read at every pass, another value once before the loop. More than N
9791note passes (default 1000000 for a value K, no limit for a number K) trap 4. om4 S_FOR.
9792fn s_for to void
9793sn = get nn 0
9794sh = get held 0
9795tok
9796if eq get tk 0 0
9797fail 4
9798end
9799if ne get tk 0 1
9800fail 16
9801end
9802if gt get tl 0 8
9803fail 13
9804end
9805w = get tw 0
9806if ne kw w 0
9807fail 16
9808end
9809newname w get tl 0 get ts 0
9810t = typeword
9811if or eq t 1 ge t 10
9812fail 7
9813end
9814tok
9815if eq get tk 0 0
9816fail 4
9817end
9818if eq get tk 0 2
9819range get tw 0 t get tbig 0
9820if ne get om2 0 0
9821kk = get tw 0
9822if eq peekw 181
9823s_unroll w t kk sn sh
9824ret
9825end
9826put tw 0 kk
9827end
9828sf_num w t get tw 0 sn sh
9829ret
9830end
9831rdval 1 t 255 0
9832if ne get rt 0 t
9833fail 7
9834end
9835if ne get om2 0 0
9836if eq peekw 181
9837fail 16
9838end
9839end
9840if eq get rk 0 3
9841sf_num w t get rv 0 sn sh
9842ret
9843end
9844kr = matreg get rk 0 get rv 0 get rt 0
9845sf_val w t kr sn sh
9846end
9847
9848note ======== OM-2 section 4: unroll, inline, folding, the growth bound and trap-site records ========
9849note peekstar: 1 when the next word is `*` (an unsized array, OM-2), without reading it
9850fn peekstar to ulong
9851c0 = get cur 0
9852l0 = get line 0
9853a0 = get tka 0
9854b0 = get tkb 0
9855tok
9856r = ulong 0
9857if and eq get tk 0 1 eq get tl 0 1
9858if eq get tw 0 42
9859r = ulong 1
9860end
9861end
9862put cur 0 c0
9863put line 0 l0
9864put tka 0 a0
9865put tkb 0 b0
9866ret r
9867end
9868
9869note peekw: the keyword code of the next word (0 for anything else) without reading it
9870fn peekw to ulong
9871c0 = get cur 0
9872l0 = get line 0
9873a0 = get tka 0
9874b0 = get tkb 0
9875tok
9876r = ulong 0
9877if eq get tk 0 1
9878if le get tl 0 8
9879r = kw get tw 0
9880end
9881end
9882put cur 0 c0
9883put line 0 l0
9884put tka 0 a0
9885put tkb 0 b0
9886ret r
9887end
9888
9889note s_unroll: for i T K unroll (K a number 1..64, error 8 outside). i is a constant name (kind 9) bound to
9890note 0..K-1 in turn; the body is read K times from its first line (s_unr). No branch is emitted.
9891fn s_unroll w ulong t ulong kk ulong sn ulong sh ulong to void
9892if or eq kk 0 gt kk 64
9893fail 8
9894end
9895fuelc get line 0 kk
9896tok
9897eol
9898addname w 0 t 0 9 0
9899n = get nb 0
9900pushb 0 5 0 sub get nn 0 1 t
9901pushnh sn sh
9902put u_cur n get cur 0
9903put u_line n get line 0
9904put u_it n 0
9905put u_k n kk
9906put u_nn n get nn 0
9907put u_held n get held 0
9908put fzone 0 add get fzone 0 1
9909cp_head get cur 0 get line 0
9910cp_skip
9911end
9912
9913note fuelc: charge n to the function's fuel at the construct on line ln, before anything of it expands; a
9914note total above 65536 is error 14 on line ln (E14, E16)
9915fn fuelc ln ulong n ulong to void
9916if gt add get fuel 0 n 65536
9917put line 0 ln
9918fail 14
9919end
9920put fuel 0 add get fuel 0 n
9921end
9922
9923note s_unr: the end of an unrolled body: read it again with the next value of i, or close the block
9924fn s_unr i ulong to void
9925eol
9926it = add get u_it i 1
9927if lt it get u_k i
9928put u_it i it
9929put nm_addr get breg i it
9930put nn 0 get u_nn i
9931put held 0 get u_held i
9932put nslot 0 get u_held i
9933put dead 0 0
9934put cur 0 get u_cur i
9935put line 0 get u_line i
9936cp_head get cur 0 get line 0
9937cp_skip
9938ret
9939end
9940put u_it i it
9941put nm_addr get breg i it
9942put nn 0 get u_nn i
9943put nslot 0 get u_held i
9944cp_head get u_cur i get u_line i
9945put nb 0 i
9946put nn 0 get bnames i
9947put held 0 get bheld i
9948put dead 0 0
9949put fzone 0 sub get fzone 0 1
9950end
9951
9952note fn_tail: the end of a fn line: an optional inline (OM-2), then the line ends; records the body start
9953fn fn_tail f ulong to void
9954tok
9955k = get tk 0
9956if eq k 1
9957if ne get om2 0 0
9958if le get tl 0 8
9959if eq kw get tw 0 182
9960put fn_inl f 1
9961eol
9962put fn_body f get cur 0
9963put fn_bln f get line 0
9964ret
9965end
9966end
9967end
9968end
9969if and ne k 0 ne k 3
9970put dexp 0 6
9971fail 5
9972end
9973put fn_body f get cur 0
9974put fn_bln f get line 0
9975end
9976
9977note inl_call: a call of an inline function fe (index + 1): the result register first (dst, or the next
9978note pool slot), then the arguments left to right exactly as for a call, then the body in place.
9979fn inl_call fe ulong dst ulong to void
9980s0 = get nslot 0
9981rres = ulong 255
9982ns = s0
9983if ne get fn_ret sub fe 1 12
9984if eq dst 255
9985rres = alloc
9986ns = add s0 1
9987else
9988rres = dst
9989end
9990end
9991cb = add mul get depth 0 32 8
9992np = ulong get fn_np sub fe 1
9993put fr add cb 18 np
9994put fr add cb 19 get nslot 0
9995i = ulong 0
9996while lt i np
9997p = add mul sub fe 1 6 i
9998if eq get fn_pk p 0
9999rdval 0 ulong get fn_pt p 255 0
10000ca_scal p add cb mul i 3
10001else
10002if ge get fn_pk p 6
10003ca_st fe i cb
10004else
10005ca_arr fe i cb
10006end
10007end
10008i = add i 1
10009end
10010inl_body fe cb ns rres
10011end
10012
10013note inl_par: bind parameter i of inline function fe to its argument in frame cb. A scalar held in a
10014note temporary register (pool index hb or above) is taken as it is; any other scalar is copied to a new
10015note register. An array or struct takes its base register; a text's address is loaded into one. An
10016note unsized array parameter also takes its count register, or a new one holding a number count.
10017fn inl_par fe ulong cb ulong i ulong hb ulong to void
10018p = add mul sub fe 1 6 i
10019q = add cb mul i 3
10020k = get fr q
10021v = get fr add q 1
10022r = ulong 0
10023if eq get fn_pk p 0
10024if and eq k 1 ge regidx v hb
10025r = v
10026else
10027r = alloc
10028force k v ulong get fn_pt p r
10029end
10030else
10031if eq k 5
10032r = v
10033else
10034r = alloc
10035matconst r v
10036end
10037end
10038inl_add p r
10039if eq pwid p 2
10040inl_lr add mul div cb 32 6 i
10041end
10042fz_inl p get fr add q 2
10043end
10044
10045note inl_lr: the unsized parameter name just added gets the count of argument ux (ul_k / ul_v): its
10046note register, or a new one holding the number
10047fn inl_lr ux ulong to void
10048lr = get ul_v ux
10049if ne get ul_k ux 1
10050lr = alloc
10051matconst lr get ul_v ux
10052end
10053put nm_lr sub get nn 0 1 uchar lr
10054end
10055
10056note inl_add: the name of parameter slot p on register r (a view struct is name kind 8, an edit one 7)
10057fn inl_add p ulong r ulong to void
10058pk = ulong get fn_pk p
10059if eq pk 0
10060addname get fn_pn p r ulong get fn_pt p 0 0 0
10061ret
10062end
10063nk = pk
10064if eq pk 6
10065nk = ulong 8
10066end
10067addname get fn_pn p r ulong get fn_pt p get fn_pl p nk 0
10068put nm_lay sub get nn 0 1 get fn_play p
10069end
10070
10071note inl_edge: record the call-graph edge (expanding function -> inline function) once (16384 edges, error 14)
10072fn inl_edge a ulong b ulong to void
10073n = get nie 0
10074j = ulong 0
10075while lt j n
10076if and eq get ie_from j a eq get ie_to j b
10077ret
10078end
10079j = add j 1
10080end
10081if ge n 16384
10082fail 14
10083end
10084put ie_from n a
10085put ie_to n b
10086put nie 0 add n 1
10087end
10088
10089note inl_body: at most 4 nested inline frames (error 14); an inline function reached again while it is
10090note being inlined, or from its own body, is error 9. The caller's local names are hidden, the parameters
10091note bound, and the body's lines compiled here until its end (s_inlx) closes the frame.
10092note Each expansion costs one unit of the function's fuel (E14).
10093fn inl_body fe ulong cb ulong ns ulong rres ulong to void
10094lv = get inl 0
10095fuelc get line 0 1
10096if ge lv 4
10097fail 14
10098end
10099if eq get curfn 0 fe
10100fail 9
10101end
10102j = ulong 0
10103while lt j lv
10104if eq get il_fn j fe
10105fail 9
10106end
10107j = add j 1
10108end
10109inl_edge get curfn 0 fe
10110put il_cur lv get cur 0
10111put il_line lv get line 0
10112put il_ret lv get curret 0
10113put il_nn lv get nn 0
10114put il_lo lv get nmlo 0
10115put il_held lv get held 0
10116put il_ns lv ns
10117put il_res lv rres
10118put il_fn lv fe
10119put il_p0 lv get nilp 0
10120put il_fz lv get fz_n 0
10121inl_bind fe cb
10122pushb 0 6 0 fe 0
10123pushnh get il_nn lv get il_held lv
10124put inl 0 add lv 1
10125put fzone 0 add get fzone 0 1
10126put curret 0 ulong get fn_ret sub fe 1
10127put dead 0 0
10128put cur 0 get fn_body sub fe 1
10129put line 0 get fn_bln sub fe 1
10130cp_entry
10131inl_run lv
10132end
10133
10134note inl_bind: hide the caller's local names, bind the parameters, protect every register in use
10135fn inl_bind fe ulong cb ulong to void
10136hb = add get npar 0 get held 0
10137put nmlo 0 get nn 0
10138np = ulong get fn_np sub fe 1
10139i = ulong 0
10140while lt i np
10141inl_par fe cb i hb
10142i = add i 1
10143end
10144put held 0 get nslot 0
10145end
10146
10147note inl_run: compile the inlined body's lines until its frame (level lv) is closed
10148fn inl_run lv ulong to void
10149go = true
10150while go
10151m = ulong 0
10152while and go lt m 999999
10153if ne stmt 0
10154fail 15
10155end
10156if le get inl 0 lv
10157go = false
10158end
10159m = add m 1
10160end
10161end
10162end
10163
10164note inlb: the B of an inlined ret, patched to the frame's end
10165fn inlb to void
10166n = get nilp 0
10167if ge n 4096
10168fail 14
10169end
10170put ilp n get oc 0
10171put nilp 0 add n 1
10172if ne get xtarget 0 0
10173p = xbranch 14
10174put ilp n p
10175else
10176emit 335544320
10177end
10178put lastb 0 get oc 0
10179end
10180
10181note s_iret: ret inside an inlined body: the value into the frame's result register, then B to its end
10182fn s_iret to void
10183lv = sub get inl 0 1
10184r = get curret 0
10185if eq r 12
10186eol
10187cp_post 0 0 r
10188inlb
10189dead1
10190ret
10191end
10192if and ne get cfhas get inl 0 0 eq get crel 0 0
10193rdval 0 r 255 0
10194if ne get rt 0 r
10195fail 7
10196end
10197rr = matreg get rk 0 get rv 0 r
10198eol
10199cp_post 1 rr r
10200force 1 rr r get il_res lv
10201else
10202rdval 0 r get il_res lv 0
10203if ne get rt 0 r
10204fail 7
10205end
10206force get rk 0 get rv 0 get rt 0 get il_res lv
10207eol
10208cp_post 1 get il_res lv r
10209end
10210inlb
10211dead1
10212end
10213
10214note s_inlx: the end of an inlined body: a value function must not reach it (error 18). A last ret's B
10215note that is the last word, with no branch patched to the place after it, is taken back; the other rets
10216note branch here. The caller's line, names, registers and return type come back; the result is in rk/rv/rt.
10217fn s_inlx i ulong to void
10218eol
10219lv = sub get inl 0 1
10220fe = get il_fn lv
10221if eq get dead 0 0
10222if ne get fn_ret sub fe 1 12
10223fail 18
10224end
10225cp_post 0 0 12
10226end
10227inl_pat get il_p0 lv
10228put nb 0 i
10229put cur 0 get il_cur lv
10230put line 0 get il_line lv
10231put curret 0 get il_ret lv
10232put nn 0 get il_nn lv
10233put nmlo 0 get il_lo lv
10234put held 0 get il_held lv
10235put nslot 0 get il_ns lv
10236put dead 0 0
10237put inl 0 lv
10238put fzone 0 sub get fzone 0 1
10239put fz_n 0 get il_fz lv
10240put rk 0 1
10241put rv 0 get il_res lv
10242put rt 0 ulong get fn_ret sub fe 1
10243end
10244
10245note inl_pat: patch the frame's ret B words (from p0) to here, after taking back a last one that is the
10246note last word and that no branch was patched past
10247fn inl_pat p0 ulong to void
10248n = get nilp 0
10249o = get oc 0
10250if and ne get dead 0 0 gt n p0
10251if eq get lastb 0 o
10252if ne get tgoc 0 o
10253if eq get ilp sub n 1 sub o 4
10254put oc 0 sub o 4
10255n = sub n 1
10256end
10257end
10258end
10259end
10260j = p0
10261while lt j n
10262patch26 get ilp j get oc 0
10263j = add j 1
10264end
10265put nilp 0 p0
10266end
10267
10268note ---- folding (only while fzone is above 0): an operation on two typed numbers is computed with OM-1
10269note arithmetic: unsigned wraps to its width, signed results are sign-extended; an operation that would
10270note trap (signed overflow, divide by 0, a shift or rotate amount at or above the width) is not folded.
10271fn fmask w ulong to ulong
10272if ge w 64
10273ret not ulong 0
10274end
10275ret sub shl ulong 1 w 1
10276end
10277
10278fn fasr x ulong s ulong to ulong
10279if eq shr x 63 0
10280ret shr x s
10281end
10282ret not shr not x s
10283end
10284
10285fn fsext r ulong w ulong to ulong
10286if ge w 64
10287ret r
10288end
10289ret fasr shl r sub 64 w sub 64 w
10290end
10291
10292fn fyes v ulong t ulong to void
10293put fval 0 v
10294put fty 0 t
10295put fok 0 1
10296end
10297
10298fn fb_cmp op ulong sg ulong a ulong b ulong to void
10299x = a
10300y = b
10301if ne sg 0
10302x = xor a shl ulong 1 63
10303y = xor b shl ulong 1 63
10304end
10305r = false
10306if eq op 43
10307r = eq x y
10308end
10309if eq op 44
10310r = ne x y
10311end
10312if eq op 45
10313r = lt x y
10314end
10315if eq op 46
10316r = le x y
10317end
10318if eq op 47
10319r = gt x y
10320end
10321if eq op 48
10322r = ge x y
10323end
10324v = ulong 0
10325if r
10326v = ulong 1
10327end
10328fyes v 1
10329end
10330
10331fn fb_arith op ulong ty ulong w ulong sg ulong a ulong b ulong to void
10332r = ulong 0
10333if eq op 33
10334r = add a b
10335end
10336if eq op 34
10337r = sub a b
10338end
10339if eq op 35
10340if ne sg 0
10341if ge add a shl ulong 1 31 shl ulong 1 32
10342ret
10343end
10344if ge add b shl ulong 1 31 shl ulong 1 32
10345ret
10346end
10347end
10348r = mul a b
10349end
10350if eq sg 0
10351fyes and r fmask w ty
10352ret
10353end
10354if eq w 64
10355if eq op 33
10356if ne shr and xor a r xor b r 63 0
10357ret
10358end
10359end
10360if eq op 34
10361if ne shr and xor a b xor a r 63 0
10362ret
10363end
10364end
10365fyes r ty
10366ret
10367end
10368if ne fsext r w r
10369ret
10370end
10371fyes r ty
10372end
10373
10374fn fb_div op ulong ty ulong w ulong sg ulong a ulong b ulong to void
10375if eq b 0
10376ret
10377end
10378if eq sg 0
10379if eq op 36
10380fyes div a b ty
10381else
10382fyes rem a b ty
10383end
10384ret
10385end
10386na = shr a 63
10387sb = shr b 63
10388ma = a
10389if ne na 0
10390ma = sub 0 a
10391end
10392mb = b
10393if ne sb 0
10394mb = sub 0 b
10395end
10396if eq op 37
10397rr = rem ma mb
10398if ne na 0
10399rr = sub 0 rr
10400end
10401fyes rr ty
10402ret
10403end
10404if eq w 64
10405if eq a shl ulong 1 63
10406if eq b not ulong 0
10407ret
10408end
10409end
10410end
10411q = div ma mb
10412if ne xor na sb 0
10413q = sub 0 q
10414end
10415if ne fsext q w q
10416ret
10417end
10418fyes q ty
10419end
10420
10421fn fb_shift op ulong ty ulong w ulong sg ulong a ulong b ulong to void
10422if ge b w
10423ret
10424end
10425if eq op 42
10426if eq sg 0
10427fyes shr a b ty
10428else
10429fyes fasr a b ty
10430end
10431ret
10432end
10433r = shl a b
10434if eq sg 0
10435fyes and r fmask w ty
10436ret
10437end
10438if eq w 64
10439if ne fasr r b a
10440ret
10441end
10442fyes r ty
10443ret
10444end
10445if ne fsext r w r
10446ret
10447end
10448fyes r ty
10449end
10450
10451fn fb_rot op ulong ty ulong w ulong a ulong b ulong to void
10452if ge b w
10453ret
10454end
10455s = b
10456if and eq op 60 ne s 0
10457s = sub w s
10458end
10459if eq s 0
10460fyes a ty
10461ret
10462end
10463fyes and or shr a s shl a sub w s fmask w ty
10464end
10465
10466note foldbin: op on the typed numbers a, b of type ty (b's own type for a shift); fok 1 when folded
10467fn foldbin op ulong ty ulong a ulong b ulong to void
10468put fok 0 0
10469if or eq ty 0 ge ty 10
10470ret
10471end
10472sg = ulong 0
10473if ge ty 6
10474sg = ulong 1
10475end
10476if and ge op 43 le op 48
10477fb_cmp op sg a b
10478ret
10479end
10480if and ge op 38 le op 40
10481r = and a b
10482if eq op 39
10483r = or a b
10484end
10485if eq op 40
10486r = xor a b
10487end
10488fyes r ty
10489ret
10490end
10491if eq ty 1
10492ret
10493end
10494w = twid ty
10495if le op 35
10496fb_arith op ty w sg a b
10497ret
10498end
10499if le op 37
10500fb_div op ty w sg a b
10501ret
10502end
10503if le op 42
10504fb_shift op ty w sg a b
10505ret
10506end
10507if or eq op 59 eq op 60
10508if le ty 5
10509fb_rot op ty w a b
10510end
10511end
10512end
10513
10514note foldun: neg (49) / not (50) of the typed number a
10515fn foldun c ulong ty ulong a ulong to void
10516put fok 0 0
10517if or eq ty 0 ge ty 10
10518ret
10519end
10520if eq c 50
10521if eq ty 1
10522fyes xor a 1 ty
10523ret
10524end
10525if lt ty 6
10526fyes and not a fmask twid ty ty
10527ret
10528end
10529fyes not a ty
10530ret
10531end
10532if eq ty 1
10533ret
10534end
10535r = sub 0 a
10536if lt ty 6
10537fyes and r fmask twid ty ty
10538ret
10539end
10540w = twid ty
10541if eq w 64
10542if eq a shl ulong 1 63
10543ret
10544end
10545fyes r ty
10546ret
10547end
10548if ne fsext r w r
10549ret
10550end
10551fyes r ty
10552end
10553
10554note ---- omc-site records (om2 diag): one per runtime trap site, in emission order, after the artifact
10555note on stderr: omc-site 1 line=N iter=k, k the counters of the open unrolled loops outer to inner
10556note joined by '.', or '-'.
10557fn siteb b ulong to void
10558n = get nsite 0
10559if ge n 8388608
10560fail 14
10561end
10562put sitebuf n uchar b
10563put nsite 0 add n 1
10564end
10565
10566fn sitew w ulong n ulong to void
10567i = ulong 0
10568x = w
10569while lt i n
10570siteb and x 255
10571x = shr x 8
10572i = add i 1
10573end
10574end
10575
10576fn siten v ulong to void
10577d = new uchar 20 0
10578i = ulong 0
10579x = v
10580put d 0 uchar add rem x 10 48
10581x = div x 10
10582i = add i 1
10583while gt x 0
10584put d i uchar add rem x 10 48
10585x = div x 10
10586i = add i 1
10587end
10588j = i
10589while gt j 0
10590j = sub j 1
10591siteb ulong get d j
10592end
10593end
10594
10595fn siterec to void
10596sitew 7310584038532803951 8
10597sitew 4424063608026050848 8
10598siten add get line 0 get lbase 0
10599sitew 67561537693984 6
10600any = false
10601j = ulong 0
10602while lt j get nb 0
10603if eq get bkind j 5
10604if any
10605siteb 46
10606end
10607siten get u_it j
10608any = true
10609end
10610j = add j 1
10611end
10612if not any
10613siteb 45
10614end
10615siteb 10
10616end
10617
10618fn sitesout to void
10619p = ulong 0
10620go = true
10621while go
10622m = ulong 0
10623while and go lt m 999999
10624if ge p get nsite 0
10625go = false
10626else
10627n = write 2 sitebuf p sub get nsite 0 p
10628if le n 0
10629go = false
10630else
10631p = add p ulong n
10632end
10633end
10634m = add m 1
10635end
10636end
10637end
10638
10639note finish: end of a function: a function that needs a value must not reach its end (error 18), a void
10640note one returns; then the trap stubs: shared tail, function mode MOV X0, X7; LDR X16, [X7, #16]; BR X16
10641note (C6.2.282, C6.2.216, C6.2.46: OscRt.trap); program mode ADD X0, X1, #100; MOVZ X8, #93; SVC #0 (exit
10642note 100 + code). Per trap code used: MOVZ X1, #code (C6.2.285); B tail. om4 FINISH.
10643fn finish to void
10644if ne get xtarget 0 0
10645xfinish
10646ret
10647end
10648eol
10649if eq get dead 0 0
10650if ne get curret 0 12
10651fail 18
10652end
10653cp_post 0 0 12
10654epi
10655end
10656nt0 = get nt 0
10657if ne nt0 0
10658tail = get oc 0
10659if eq get progmode 0 0
10660emit 2852586464
10661emit 4181723376
10662emit 3592356352
10663else
10664emit 2432798752
10665emit 3531606952
10666emit 3556769793
10667end
10668code = ulong 1
10669while le code 14
10670st = ulong 0
10671j = ulong 0
10672while lt j nt0
10673if eq get tcode j code
10674if eq st 0
10675st = get oc 0
10676emit or 3531603969 shl code 5
10677back tail
10678end
10679patch19 get tsite j st
10680end
10681j = add j 1
10682end
10683code = add code 1
10684end
10685end
10686put curret 0 0
10687put nn 0 get nglob 0
10688put curfn 0 0
10689put nbrk 0 0
10690put ncont 0 0
10691end
10692
10693note s_end: close the innermost block (if: patch its branch; else: patch its jump; while: B head; for: i =
10694note add i 1, B head), or the function. om4 S_END.
10695fn s_end to void
10696n = get nb 0
10697if eq n 0
10698finish
10699ret
10700end
10701i = sub n 1
10702if eq get bkind i 5
10703s_unr i
10704ret
10705end
10706if eq get bkind i 6
10707s_inlx i
10708ret
10709end
10710put nb 0 i
10711k = get bkind i
10712fz_join i k
10713if eq k 2
10714put dead 0 and get dead 0 get bdead i
10715else
10716put dead 0 0
10717end
10718if eq k 4
10719lat = get oc 0
10720j = ulong 0
10721while lt j get ncont 0
10722if eq get cblk j i
10723patch19 get cpos j lat
10724put cblk j 65535
10725end
10726j = add j 1
10727end
10728put fr 0 1
10729put fr 1 get breg i
10730put fr 2 get btyp i
10731put fr 3 3
10732put fr 4 1
10733put fr 5 get btyp i
10734binop 33 get breg i 0
10735end
10736if eq k 3
10737j = ulong 0
10738while lt j get ncont 0
10739if eq get cblk j i
10740patch19 get cpos j get bhead i
10741put cblk j 65535
10742end
10743j = add j 1
10744end
10745end
10746if ge k 3
10747back get bhead i
10748ex = get oc 0
10749j = ulong 0
10750while lt j get nbrk 0
10751if eq get brkblk j i
10752patch19 get brkpos j ex
10753put brkblk j 65535
10754end
10755j = add j 1
10756end
10757end
10758if eq k 2
10759if or eq get xtarget 0 0 eq get bdead i 0
10760patch26 get bbr i get oc 0
10761end
10762else
10763patch19 get bbr i get oc 0
10764end
10765put nn 0 get bnames i
10766put held 0 get bheld i
10767eol
10768end
10769
10770note ======== the end of the input: the output ========
10771note writeout: the output from byte st to the end, to stdout (error 2), then exit 0. om4 WRITEOUT.
10772fn writeout st ulong to void
10773if eq get chkmode 0 1
10774put mc 0 0
10775mputw 7594244768023276911 8
10776mputw 11207551510369 6
10777mflush
10778exit int 0
10779end
10780if ne get fmtm 0 0
10781fmtout
10782end
10783p = st
10784go = true
10785while go
10786m = ulong 0
10787while and go lt m 999999
10788if ge p get oc 0
10789go = false
10790else
10791n = write 1 out p sub get oc 0 p
10792if le n 0
10793fail 2
10794end
10795p = add p ulong n
10796end
10797m = add m 1
10798end
10799end
10800if eq get om2m 0 1
10801sitesout
10802end
10803exit int 0
10804end
10805
10806note elfhdr: the 176-byte file header (om4 ELFHDR, 44 words): ELF64 little-endian, executable, AArch64,
10807note entry 0x4000b0 (the word after the header), 2 program headers: code read+execute from 0x400000, and
10808note the read/write pools from 0x10000000. Only the words that are not 0 (the output starts at 0).
10809fn elfhdr to void
10810wrword 0 1179403647
10811wrword 4 65794
10812wrword 16 11993090
10813wrword 20 1
10814wrword 24 4194480
10815wrword 32 64
10816wrword 52 3670080
10817wrword 56 2
10818wrword 64 1
10819wrword 68 5
10820wrword 80 4194304
10821wrword 88 4194304
10822wrword 112 4096
10823wrword 120 1
10824wrword 124 6
10825wrword 136 268435456
10826wrword 144 268435456
10827wrword 168 4096
10828end
10829
10830note eof_prog: program mode: start code = MOVZ X28, #0x1000, LSL 16 (stack pool floor); MOVZ X16, #0x1080,
10831note LSL 16; MOV SP, X16 (ADD immediate, C6.2.5); BL main (C6.2.43); [MOVZ X0, #0 for a void main]; MOVZ X8,
10832note #93; SVC #0. The entry word after the header is a B to it; the header gets the file and pool sizes.
10833note om4 EOF_PROG.
10834fn eof_prog to void
10835if ne get xtarget 0 0
10836xeof
10837ret
10838end
10839f = findfn 1852399981 4 0
10840if eq f 0
10841fail 16
10842end
10843if ne get fn_np sub f 1 0
10844fail 9
10845end
10846wrword 176 or 335544320 bdisp 176 get oc 0 26
10847if ne get hostargs 0 0
10848note Preserve Linux's initial argument stack privately before installing Omega's bounded stack.
10849emit 2432697339
10850end
10851emit 3533832220
10852emit 3533836304
10853emit 2432696863
10854emit or 2483027968 bdisp get oc 0 get fn_addr sub f 1 26
10855if eq get fn_ret sub f 1 12
10856emit 3531603968
10857end
10858emit 3531606952
10859emit 3556769793
10860elfhdr
10861wrword 96 get oc 0
10862wrword 104 get oc 0
10863wrword 160 add get poolb 0 8388608
10864writeout 0
10865end
10866
10867note s_eof: end of input: no open function (error 15), some function (16); function mode: no globals or
10868note texts (9, fnstore; consts fold and are allowed); one function: its bare words; more: the entry word, a
10869note B to the last function. om4 S_EOF.
10870note Resolve every call after all addresses are known. In function mode remove leaves of the call
10871note graph; any functions left after nf rounds form or reach a cycle (error 9).
10872fn resolve to void
10873n = get ncalls 0
10874i = ulong 0
10875while lt i n
10876s = get callsite i
10877f = sub get callto i 1
10878if ne get xtarget 0 0
10879wrword add s 1 sub get fn_addr f add s 5
10880else
10881wrword s or 2483027968 bdisp s get fn_addr f 26
10882end
10883i = add i 1
10884end
10885if eq get progmode 0 0
10886pass = ulong 0
10887while lt pass get nf 0
10888f = ulong 0
10889while lt f get nf 0
10890if eq get settled f 0
10891ok = true
10892i = ulong 0
10893while and ok lt i n
10894if eq get callfrom i add f 1
10895if eq get settled sub get callto i 1 0
10896ok = false
10897end
10898end
10899i = add i 1
10900end
10901if ok
10902put settled f 1
10903end
10904end
10905f = add f 1
10906end
10907pass = add pass 1
10908end
10909f = ulong 0
10910while lt f get nf 0
10911if eq get settled f 0
10912put dsev 0 3
10913fail 9
10914end
10915f = add f 1
10916end
10917end
10918end
10919
10920note reachself: 1 when function f (index + 1) reaches itself
10921fn reachself f ulong to ulong
10922i = ulong 0
10923while lt i get nf 0
10924put reach i 0
10925i = add i 1
10926end
10927put reach sub f 1 1
10928go = true
10929pass = ulong 0
10930while and go le pass get nf 0
10931r = rscan f 0
10932if eq r 2
10933ret 1
10934end
10935s = rscan f 1
10936if eq s 2
10937ret 1
10938end
10939go = or ne r 0 ne s 0
10940pass = add pass 1
10941end
10942ret 0
10943end
10944
10945note rscan: one pass over the calls (w 0) or the inline edges (w 1): 2 when an edge from a reached
10946note function leads to f, 1 when a function was newly reached, else 0
10947fn rscan f ulong w ulong to ulong
10948n = get ncalls 0
10949if ne w 0
10950n = get nie 0
10951end
10952ch = ulong 0
10953j = ulong 0
10954while lt j n
10955a = get callfrom j
10956b = get callto j
10957if ne w 0
10958a = get ie_from j
10959b = get ie_to j
10960end
10961if ne get reach sub a 1 0
10962if eq b f
10963ret 2
10964end
10965if eq get reach sub b 1 0
10966put reach sub b 1 1
10967ch = ulong 1
10968end
10969end
10970j = add j 1
10971end
10972ret ch
10973end
10974
10975fn s_eof to void
10976if eq get chkmode 0 2
10977emit_query
10978end
10979if ne get curret 0 0
10980fail 15
10981end
10982if eq get nf 0 0
10983fail 16
10984end
10985if ne get om2 0 0
10986note inlcyc (OM-2): an inline function that reaches itself through the whole call graph (the calls made,
10987note plus an edge from each function to every inline function expanded in it) is error 9 at the end.
10988f = ulong 1
10989while le f get nf 0
10990if ne get fn_inl sub f 1 0
10991if ne reachself f 0
10992put dsev 0 3
10993fail 9
10994end
10995end
10996f = add f 1
10997end
10998end
10999resolve
11000if ne get progmode 0 0
11001eof_prog
11002end
11003if eq get progmode 0 0
11004note fnstore: function mode. A top-level global pool (kind 4) or text (kind 5) needs the program image and
11005note is error 9; a const (kind 9) is folded to a typed number wherever it is read, so it composes here too.
11006i = ulong 0
11007while lt i get nglob 0
11008if ne get nm_kind i 9
11009fail 9
11010end
11011i = add i 1
11012end
11013end
11014if ne get xtarget 0 0
11015note Bare x86 units share the private frame ABI; exports have six-byte JMP/NOP slots.
11016n = get nexp 0
11017p = ulong 512
11018f = ulong 0
11019while lt f get nf 0
11020take = ne get fn_exp f 0
11021if eq n 0
11022take = eq f sub get lastfn 0 1
11023end
11024if take
11025put out p 233
11026wrword add p 1 sub get fn_addr f add p 5
11027put out add p 5 144
11028p = add p 6
11029end
11030f = add f 1
11031end
11032writeout 512
11033end
11034if ne get nexp 0 0
11035note expwords (OM-2, spec 6): words 0 .. E-1 of a multi-entry unit, B to each export in source order
11036at = ulong 176
11037f = ulong 0
11038while lt f get nf 0
11039if ne get fn_exp f 0
11040wrword at or 335544320 bdisp at get fn_addr f 26
11041at = add at 4
11042end
11043f = add f 1
11044end
11045writeout 176
11046end
11047if eq get nf 0 1
11048writeout 180
11049end
11050a = get fn_addr sub get lastfn 0 1
11051wrword 176 or 335544320 bdisp 176 a 26
11052writeout 176
11053end
11054
11055note stmt: one line. Returns 1 at the end of the input. om4 STMT.
11056fn stmt to ulong
11057put dexp 0 0
11058put nslot 0 get held 0
11059tok
11060k = get tk 0
11061if eq k 0
11062ret 0
11063end
11064if eq k 3
11065s_eof
11066ret 1
11067end
11068if ne k 1
11069fail 16
11070end
11071if and ne get chkmode 0 0 eq get line 0 get chkln 0
11072skipnl
11073ret 0
11074end
11075if and ne get om2 0 0 eq get line 0 get om2ln 0
11076skipnl
11077ret 0
11078end
11079if ne cp_kind 0
11080fail 30
11081end
11082if gt get tl 0 8
11083note s_long: a word over 8 characters at the start of a line: only a call of a void function. om4 S_LONG.
11084if eq get curret 0 0
11085fail 13
11086end
11087if ne get dead 0 0
11088fail 17
11089end
11090f = findfn get tw 0 get tl 0 get ts 0
11091if eq f 0
11092unkfail 13
11093end
11094if ne get fn_ret sub f 1 12
11095fail 7
11096end
11097rdval 2 0 255 f
11098eol
11099ret 0
11100end
11101w = get tw 0
11102c = kw w
11103if eq c 83
11104skipnl
11105ret 0
11106end
11107if eq c 64
11108s_fn
11109ret 0
11110end
11111if eq get curret 0 0
11112if eq c 78
11113s_global
11114ret 0
11115end
11116if eq c 79
11117s_text
11118ret 0
11119end
11120if eq c 152
11121s_const
11122ret 0
11123end
11124if eq c 85
11125skipnl
11126ret 0
11127end
11128if eq c 195
11129s_export
11130ret 0
11131end
11132if eq c 188
11133note s_lyskip: the body pass jumps to the place after the layout's end (read in the names pass)
11134tok
11135li = sub findlay get tw 0 1
11136put cur 0 get ly_ec li
11137put line 0 get ly_el li
11138ret 0
11139end
11140if or eq c 184 eq c 187
11141skipnl
11142ret 0
11143end
11144fail 16
11145end
11146if eq c 66
11147s_end
11148ret 0
11149end
11150if eq c 68
11151s_else
11152ret 0
11153end
11154if ne get dead 0 0
11155fail 17
11156end
11157if eq c 0
11158s_name w
11159ret 0
11160end
11161if eq c 67
11162note s_if: if cond: the false branch is patched at else / end. om4 S_IF.
11163at = cond
11164pushb at 1 0 0 0
11165pushnh get nn 0 get held 0
11166eol
11167ret 0
11168end
11169if eq c 69
11170s_while
11171ret 0
11172end
11173if eq c 71
11174s_for
11175ret 0
11176end
11177if eq c 72
11178s_ret
11179ret 0
11180end
11181if eq c 80
11182s_put
11183ret 0
11184end
11185if eq c 82
11186note s_exit: exit value (int): Linux exit (system call 93: MOVZ X8, #93, C6.2.285; SVC #0, C6.2.466); the
11187note path ends. om4 S_EXIT.
11188rdval 0 8 255 0
11189if ne get rt 0 8
11190fail 7
11191end
11192force get rk 0 get rv 0 get rt 0 0
11193if ne get xtarget 0 0
11194xload 0 0
11195xleave
11196else
11197emit 3531606952
11198emit 3556769793
11199end
11200dead1
11201eol
11202ret 0
11203end
11204if eq c 86
11205s_break
11206ret 0
11207end
11208if eq c 87
11209note s_trap: trap N (1 .. 14): an always-taken branch to the trap stub for code N, which finish makes.
11210note Program mode exits 100 + N; function mode calls OscRt.trap (OMEGESE 9.1). The line ends the path.
11211v = lenval 4
11212if or lt v 1 gt v 14
11213fail 8
11214end
11215trapc 14 v
11216dead1
11217eol
11218ret 0
11219end
11220note cpufence (keyword 199, a statement without operands): the host CPU publication barrier. AArch64:
11221note DSB SY (d5033f9f, the full-system barrier mapget/mapset already bracket their access with). x86-64:
11222note MFENCE (0F AE F0), the fence the x86 mapget/mapset use. Every earlier load and store completes before
11223note any later one, so a GPU or another CPU that observes a later store also observes the earlier ones.
11224note Inline here, not a function: om4 builds omc and holds at most 512 functions, 511 in use.
11225if eq c 199
11226eol
11227if ne get xtarget 0 0
11228xb 15 174 240
11229else
11230emit 3573759903
11231end
11232ret 0
11233end
11234note cpurelax (keyword 200, a statement without operands): spin-wait hint, no ordering. AArch64: YIELD
11235note (d503203f). x86-64: PAUSE (F3 90). Use it in a bounded poll loop; pair it with cpufence for order.
11236if eq c 200
11237eol
11238if ne get xtarget 0 0
11239emitb 243
11240emitb 144
11241else
11242emit 3573751871
11243end
11244ret 0
11245end
11246if eq c 97
11247s_cont
11248ret 0
11249end
11250if or or or eq c 77 eq c 78 eq c 79 eq c 81
11251fail 9
11252end
11253if or eq c 85 eq c 193
11254fail 9
11255end
11256fail 16
11257ret 0
11258end
11259
11260note ======== program mode: some line is `fn main` (blanks, fn, blanks, main, then a blank or the end) ========
11261note skipbl: the first place at or after q that is not a space or tab (or the end)
11262fn skipbl q ulong to ulong
11263e = get inlen 0
11264p = q
11265go = true
11266while go
11267m = ulong 0
11268while and go lt m 999999
11269if ge p e
11270go = false
11271else
11272b = get inp p
11273if or eq b 32 eq b 9
11274p = add p 1
11275else
11276go = false
11277end
11278end
11279m = add m 1
11280end
11281end
11282ret p
11283end
11284
11285note skipln: the place after the next newline at or after q (or the end)
11286fn skipln q ulong to ulong
11287e = get inlen 0
11288p = q
11289go = true
11290while go
11291m = ulong 0
11292while and go lt m 999999
11293if ge p e
11294go = false
11295else
11296b = get inp p
11297p = add p 1
11298if eq b 10
11299go = false
11300end
11301end
11302m = add m 1
11303end
11304end
11305ret p
11306end
11307
11308note 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
11309note ends while deciding, else 0. om4 PS_LINE .. PS_YES.
11310fn ps_at p ulong to ulong
11311e = get inlen 0
11312if ne get inp p 102
11313ret 0
11314end
11315q = add p 1
11316if ge q e
11317ret 2
11318end
11319if ne get inp q 110
11320ret 0
11321end
11322r = add q 1
11323q = skipbl r
11324if ge q e
11325ret 2
11326end
11327if eq q r
11328ret 0
11329end
11330if gt add q 4 e
11331ret 0
11332end
11333if ne get inp q 109
11334ret 0
11335end
11336if ne get inp add q 1 97
11337ret 0
11338end
11339if ne get inp add q 2 105
11340ret 0
11341end
11342if ne get inp add q 3 110
11343ret 0
11344end
11345if ge add q 4 e
11346ret 1
11347end
11348if gt get inp add q 4 32
11349ret 0
11350end
11351ret 1
11352end
11353
11354fn prescan to void
11355e = get inlen 0
11356p = ulong 0
11357go = true
11358while go
11359m = ulong 0
11360while and go lt m 999999
11361p = skipbl p
11362if ge p e
11363go = false
11364else
11365r = ps_at p
11366if eq r 1
11367put progmode 0 1
11368go = false
11369end
11370if eq r 2
11371go = false
11372end
11373if go
11374p = skipln p
11375if ge p e
11376go = false
11377end
11378end
11379end
11380m = add m 1
11381end
11382end
11383end
11384
11385note First collect signatures. Only fn headers are parsed here; other lines are read in the body pass.
11386fn namescan to void
11387put scanning 0 1
11388put line 0 1
11389go = true
11390while go
11391n = ulong 0
11392while and go lt n 999999
11393tok
11394if eq get tk 0 3
11395go = false
11396else
11397if ne get tk 0 0
11398if and eq get tk 0 1 eq get tw 0 28262
11399s_fn
11400else
11401if and eq get tk 0 1 eq get tw 0 127970521019507
11402s_struct
11403else
11404if and eq get tk 0 1 eq get tw 0 500152823651
11405s_const
11406else
11407if eq ns_om2 0
11408skipnl
11409end
11410end
11411end
11412end
11413end
11414end
11415n = add n 1
11416end
11417end
11418put scanning 0 0
11419put cur 0 0
11420put line 0 1
11421put nn 0 0
11422put nglob 0 0
11423put curfn 0 0
11424put curret 0 0
11425put npar 0 0
11426put nslot 0 0
11427for j ulong 16
11428put vsl j 0
11429end
11430put held 0 0
11431end
11432
11433note ======== R6 structs (omc only; om4 stays the parent that rebuilds this file) ========
11434note A struct is 8-byte cells, declaration order, no padding (OSC-2). new is a stack slot, given
11435note back at ret, like a local array. There is no free (decision 24). Field scale: cell << (3 - tlog)
11436note so a typed store lands on cell * 8. Pack of struct is 127970521019507 (Wolfram, little-endian).
11437
11438note findstruct: packed name -> index + 1, or 0
11439fn findstruct w ulong to ulong
11440i = ulong 0
11441n = get nst 0
11442while lt i n
11443if eq get st_name i w
11444ret add i 1
11445end
11446i = add i 1
11447end
11448ret 0
11449end
11450
11451note findfield: struct index, packed field name -> field-table index + 1, or 0
11452fn findfield si ulong w ulong to ulong
11453fo = ulong get st_foff si
11454nfd = ulong get st_nf si
11455i = ulong 0
11456while lt i nfd
11457j = add fo i
11458if eq get sf_name j w
11459ret add j 1
11460end
11461i = add i 1
11462end
11463ret 0
11464end
11465
11466note field_dup: 1 if w is already a field of the struct being read (fields fo .. nsf)
11467fn field_dup fo ulong w ulong to ulong
11468i = fo
11469e = get nsf 0
11470while lt i e
11471if eq get sf_name i w
11472ret 1
11473end
11474i = add i 1
11475end
11476ret 0
11477end
11478
11479note s_field: the current word is a field name of struct w. Returns the cell count after this field.
11480fn s_field w ulong fo ulong cells ulong to ulong
11481if gt get tl 0 8
11482fail 13
11483end
11484if ne kw get tw 0 0
11485fail 16
11486end
11487fw = get tw 0
11488if eq fw w
11489fail 10
11490end
11491if ne findstruct fw 0
11492fail 10
11493end
11494if ne field_dup fo fw 0
11495fail 10
11496end
11497t = typeword
11498if ge t 64
11499fail 7
11500end
11501ln = ulong 0
11502p = get cur 0
11503ln0 = get line 0
11504tok
11505k = get tk 0
11506if eq k 2
11507ln = get tw 0
11508if ne get tbig 0 0
11509fail 14
11510end
11511if or eq ln 0 gt ln 64
11512fail 14
11513end
11514else
11515if eq k 1
11516e = findname get tw 0
11517if ne e 0
11518if eq get nm_kind sub e 1 9
11519ct = ulong get nm_type sub e 1
11520if ge ct 10
11521fail 14
11522end
11523ln = get nm_addr sub e 1
11524if or eq ln 0 gt ln 64
11525fail 14
11526end
11527else
11528put cur 0 p
11529put line 0 ln0
11530end
11531else
11532put cur 0 p
11533put line 0 ln0
11534end
11535else
11536put cur 0 p
11537put line 0 ln0
11538end
11539end
11540addc = ulong 1
11541if ne ln 0
11542addc = ln
11543end
11544if gt add cells addc 64
11545fail 14
11546end
11547put sf_name get nsf 0 fw
11548put sf_type get nsf 0 uchar t
11549put sf_len get nsf 0 uchar ln
11550put sf_off get nsf 0 uchar cells
11551put nsf 0 add get nsf 0 1
11552ret add cells addc
11553end
11554
11555note s_struct: struct Name (field type [len])* end. Before any fn (namescan). 1..16 fields, <= 64 cells.
11556fn s_struct to void
11557if ne get nf 0 0
11558fail 9
11559end
11560tok
11561if ne get tk 0 1
11562fail 16
11563end
11564if gt get tl 0 8
11565fail 13
11566end
11567if ne kw get tw 0 0
11568fail 16
11569end
11570w = get tw 0
11571if ne findstruct w 0
11572fail 10
11573end
11574if ne findfn w get tl 0 get ts 0 0
11575fail 10
11576end
11577if ge get nst 0 16
11578fail 14
11579end
11580sid = get nst 0
11581fo = get nsf 0
11582cells = ulong 0
11583nfld = ulong 0
11584go = true
11585while go
11586tok
11587if eq get tk 0 0
11588fail 4
11589end
11590if ne get tk 0 1
11591fail 16
11592end
11593if eq kw get tw 0 66
11594go = false
11595else
11596if ge nfld 16
11597fail 14
11598end
11599cells = s_field w fo cells
11600nfld = add nfld 1
11601end
11602end
11603if eq nfld 0
11604fail 16
11605end
11606put st_name sid w
11607put st_nf sid uchar nfld
11608put st_cells sid uchar cells
11609put st_foff sid uchar fo
11610put nst 0 add sid 1
11611eol
11612end
11613
11614note peek_struct: if the next word is a struct name, consume it and return index + 1; else rewind
11615fn peek_struct to ulong
11616p = get cur 0
11617ln = get line 0
11618tok
11619if ne get tk 0 1
11620put cur 0 p
11621put line 0 ln
11622ret 0
11623end
11624sid = findstruct get tw 0
11625if eq sid 0
11626put cur 0 p
11627put line 0 ln
11628ret 0
11629end
11630ret sid
11631end
11632
11633note fn_p_st: parameter pw is view (k 2, name kind 8) or edit (k 3, name kind 7). fn_pk is 6 or 7.
11634fn fn_p_st f ulong np ulong pw ulong sid ulong k ulong to void
11635si = sub sid 1
11636t = add si 16
11637ln = ulong get st_cells si
11638sk = add k 4
11639if eq k 2
11640addname pw poolc get psl 0 t ln 8 0
11641else
11642addname pw poolc get psl 0 t ln 7 0
11643end
11644p = add mul f 6 np
11645put fn_pt p uchar t
11646put fn_pl p ln
11647put fn_pk p uchar sk
11648end
11649
11650note sa_new: name = new <type or struct>. Rewind unless the next word is a struct name.
11651fn sa_new w ulong to void
11652if ne get om2 0 0
11653if ne get inl 0 0
11654fail 9
11655end
11656if ne get fn_inl sub get curfn 0 1 0
11657fail 9
11658end
11659end
11660p = get cur 0
11661ln = get line 0
11662tok
11663sid = ulong 0
11664if eq get tk 0 1
11665sid = findstruct get tw 0
11666end
11667if ne sid 0
11668sa_st w sid
11669ret
11670end
11671put cur 0 p
11672put line 0 ln
11673sa_arr w
11674end
11675
11676note st_read: one value per field, saved in svk/svv/svt. An array field takes one value.
11677fn st_read si ulong to void
11678fo = ulong get st_foff si
11679nfd = ulong get st_nf si
11680i = ulong 0
11681while lt i nfd
11682ft = ulong get sf_type add fo i
11683rdval 0 ft 255 0
11684if ne get rt 0 ft
11685fail 7
11686end
11687put svk i get rk 0
11688put svv i get rv 0
11689put svt i get rt 0
11690i = add i 1
11691end
11692end
11693
11694note st_store: STR of v at cell, scaled so the typed store lands on cell * 8. ADD is not used; the
11695note index register holds cell << (3 - tlog). STR register-offset is the STOP table (om4).
11696fn st_store base ulong idx ulong cell ulong t ulong v ulong to void
11697matconst idx shl cell sub 3 tlog t
11698put ea_b 0 base
11699put ea_i 0 idx
11700stel t v
11701end
11702
11703note st_ixreg: runtime element index iv, field length fl, cell offset off. CMP + B.HS trap 3
11704note (elemaddr, C6.2.96 / C6.2.34), then ADD Xidx, Xiv, #off (C6.2.5, base word 2432696320) and
11705note LSL by 3 - tlog (bo_lsl) so the STOP store lands on (off + i) * 8.
11706fn st_ixreg idx ulong iv ulong off ulong fl ulong t ulong to void
11707if ne get xtarget 0 0
11708xcmp iv fl 1
11709trapc 2 3
11710xarith 33 5 iv off 1 idx
11711sh = sub 3 tlog t
11712if ne sh 0
11713bo_lsl idx sh idx
11714end
11715ret
11716end
11717emit or or 4043309087 shl fl 10 shl iv 5
11718trapc 2 3
11719emit or or or 2432696320 idx shl iv 5 shl off 10
11720sh = sub 3 tlog t
11721if ne sh 0
11722bo_lsl idx sh idx
11723end
11724end
11725
11726note st_ld: LDR of type t from base + idx into dst (or a new register). LDOP table, as vt_get.
11727fn st_ld base ulong idx ulong t ulong dst ulong to void
11728rd = destreg dst
11729if ne get xtarget 0 0
11730xmemval 0 base idx t rd
11731else
11732w = or ldop t rd
11733emit or or w shl base 5 shl idx 16
11734end
11735put rk 0 1
11736put rv 0 rd
11737put rt 0 t
11738end
11739
11740note st_write1: store field j (value saved at sv* i) into the struct at base.
11741fn st_write1 base ulong tmp ulong idx ulong j ulong i ulong to void
11742ft = ulong get sf_type j
11743fl = ulong get sf_len j
11744off = ulong get sf_off j
11745force get svk i get svv i get svt i tmp
11746if eq fl 0
11747st_store base idx off ft tmp
11748else
11749k = ulong 0
11750while lt k fl
11751st_store base idx add off k ft tmp
11752k = add k 1
11753end
11754end
11755end
11756
11757note st_write: store each field value. An array field is filled with its one value.
11758fn st_write base ulong tmp ulong idx ulong si ulong to void
11759fo = ulong get st_foff si
11760nfd = ulong get st_nf si
11761i = ulong 0
11762while lt i nfd
11763st_write1 base tmp idx add fo i i
11764i = add i 1
11765end
11766end
11767
11768note sa_st: p = new S v1 .. vn. Stack slot of the cell count, 16-byte aligned, outermost block only.
11769fn sa_st w ulong sid ulong to void
11770if ne get nb 0 0
11771fail 9
11772end
11773si = sub sid 1
11774cells = ulong get st_cells si
11775by = shl shr add shl cells 3 15 4 4
11776s = add get stkbytes 0 by
11777if gt s 4194304
11778fail 23
11779end
11780put stkbytes 0 s
11781r = alloc
11782put held 0 get nslot 0
11783st_read si
11784spsub by
11785stkchk
11786if ne get xtarget 0 0
11787xmovsp r
11788else
11789emit or 2432697312 r
11790end
11791tmp = alloc
11792idx = alloc
11793st_write r tmp idx si
11794addname w r add si 16 cells 6 0
11795end
11796
11797note ======== OM-2 section 5: abstract types (spec 5, errata E8, E9). `abstract NAME T [ops W..]` and
11798note `holds NAME dup32` at top level before the first fn (names pass). NAME is type 64 + index: a value of
11799note it comes only from `seal NAME v` inside the function NAMEmk returning NAME (else error 28), `open v`
11800note gives the T value, and the listed operations (rotr/rotl with a T count, and/or/xor of two NAME
11801note values) keep NAME; anything else on it, a T value or a number where NAME is needed, an array of it and
11802note the entry function's parameters and result are error 7. With holds, seal checks high32 == low32
11803note (LSR, CMP ..., UXTW, B.NE trap 10) unless the constructor body is exactly `ret seal NAME or shl ulong
11804note x 32 ulong x` with x a uint parameter (rule dup32: uint values are held zero-extended). om2 diag
11805note reports each check as omc-check 1 kept|erased line=N rule=dup32|-. ========
11806fn findab w ulong to ulong
11807i = ulong 0
11808while lt i get nab 0
11809if eq get ab_name i w
11810ret add i 1
11811end
11812i = add i 1
11813end
11814ret 0
11815end
11816
11817note abty: the current word (not a type word) as an abstract type; anything else is error 16
11818fn abty to ulong
11819a = findab get tw 0
11820if eq a 0
11821fail 16
11822end
11823ret add a 63
11824end
11825
11826note opbit: the ops bit of a preserving operation (rotr rotl and or xor), else 0
11827fn opbit c ulong to ulong
11828if eq c 59
11829ret 1
11830end
11831if eq c 60
11832ret 2
11833end
11834if eq c 38
11835ret 4
11836end
11837if eq c 39
11838ret 8
11839end
11840if eq c 40
11841ret 16
11842end
11843ret 0
11844end
11845
11846note s_abstract: abstract NAME uint|ulong [ops W ..] (another type is error 7, another op word 16)
11847fn s_abstract to void
11848if ne get nf 0 0
11849fail 9
11850end
11851tok
11852if ne get tk 0 1
11853fail 16
11854end
11855if gt get tl 0 8
11856fail 13
11857end
11858w = get tw 0
11859if ne kw w 0
11860fail 16
11861end
11862newname w get tl 0 get ts 0
11863n = get nab 0
11864if ge n 16
11865fail 14
11866end
11867put ab_len n get tl 0
11868t = typeword
11869if and ne t 4 ne t 5
11870fail 7
11871end
11872ops = ulong 0
11873tok
11874if eq get tk 0 1
11875if or gt get tl 0 8 ne kw get tw 0 196
11876fail 5
11877end
11878go = true
11879while go
11880tok
11881k = get tk 0
11882if or eq k 0 eq k 3
11883go = false
11884else
11885if or ne k 1 gt get tl 0 8
11886fail 16
11887end
11888b = opbit kw get tw 0
11889if eq b 0
11890fail 16
11891end
11892ops = or ops b
11893end
11894end
11895else
11896if and ne get tk 0 0 ne get tk 0 3
11897fail 5
11898end
11899end
11900put ab_name n w
11901put ab_base n uchar t
11902put ab_ops n ops
11903put nab 0 add n 1
11904end
11905
11906note ab_entry: function f (index) is the entry of a function-mode unit: no abstract parameter or result
11907fn ab_entry f ulong to void
11908if eq get om2 0 0
11909ret
11910end
11911if ne get scanning 0 0
11912ret
11913end
11914if ne get progmode 0 0
11915ret
11916end
11917if ne get nexp 0 0
11918if eq get fn_exp f 0
11919ret
11920end
11921else
11922if ne add f 1 get nf 0
11923ret
11924end
11925end
11926if ge get fn_ret f 64
11927fail 7
11928end
11929i = ulong 0
11930while lt i ulong get fn_np f
11931if ge get fn_pt add mul f 6 i 64
11932fail 7
11933end
11934i = add i 1
11935end
11936end
11937
11938note ab_open: open v (the value read): an abstract value becomes its representation type
11939fn ab_open to void
11940t = get rt 0
11941if lt t 64
11942fail 7
11943end
11944put rt 0 ulong get ab_base sub t 64
11945end
11946
11947note ab_seal: seal NAME v, only in the function NAMEmk (inlined or not) returning NAME (else error 28)
11948fn ab_seal to void
11949tok
11950if or eq get tk 0 0 eq get tk 0 3
11951fail 4
11952end
11953a = ulong 0
11954if and eq get tk 0 1 le get tl 0 8
11955a = findab get tw 0
11956end
11957if eq a 0
11958fail 16
11959end
11960a = sub a 1
11961fe = get curfn 0
11962if ne get inl 0 0
11963fe = get il_fn sub get inl 0 1
11964end
11965if eq ab_ctor fe a 0
11966fail 28
11967end
11968t = ulong get ab_base a
11969rdval 0 t 255 0
11970if ne get rt 0 t
11971fail 7
11972end
11973r = matreg get rk 0 get rv 0 get rt 0
11974if ne get ab_holds a 0
11975ab_check fe a r
11976end
11977put rk 0 1
11978put rv 0 r
11979put rt 0 add a 64
11980end
11981
11982note ab_ctor: 1 when function fe (index + 1) is NAMEmk for abstract a and returns it
11983fn ab_ctor fe ulong a ulong to ulong
11984if eq fe 0
11985ret 0
11986end
11987f = sub fe 1
11988if ne ulong get fn_ret f add a 64
11989ret 0
11990end
11991l = get ab_len a
11992if ne get fn_nmlen f add l 2
11993ret 0
11994end
11995o = get fn_off f
11996w = get ab_name a
11997i = ulong 0
11998while lt i l
11999if ne ulong get inp add o i and shr w shl i 3 255
12000ret 0
12001end
12002i = add i 1
12003end
12004if ne get inp add o l 109
12005ret 0
12006end
12007if ne get inp add add o l 1 107
12008ret 0
12009end
12010ret 1
12011end
12012
12013note ab_check: the dup32 check of a seal (value in r): erased for the exact body, else LSR Xt, Xr, #32
12014note (UBFM, C6.2.487); CMP Xt, Wr, UXTW (SUBS extended, C6.2.463); B.NE trap 10
12015fn ab_check fe ulong a ulong r ulong to void
12016ex = ab_exact fe a
12017if eq get om2m 0 1
12018note ab_rec: omc-check 1 kept|erased line=N rule=dup32|- (om2 diag, with the omc-site records)
12019sitew 7162245557575183727 8
12020sitew 540090475 4
12021if ne ex 0
12022sitew 110386890240613 6
12023else
12024sitew 1953523051 4
12025end
12026sitew 67505853393952 6
12027siten add get line 0 get lbase 0
12028sitew 67505820627488 6
12029if ne ex 0
12030sitew 215611372900 5
12031else
12032siteb 45
12033end
12034siteb 10
12035end
12036if eq ex 0
12037t = alloc
12038if ne get xtarget 0 0
12039xload 0 r
12040xload 1 r
12041emitb 137
12042emitb 192
12043xb 72 193 233
12044emitb 32
12045xrr 57 0 1
12046xtrap 1 10
12047else
12048emit or or 3546348544 t shl r 5
12049emit or or 3944759327 shl r 16 shl t 5
12050trapc 1 10
12051end
12052end
12053end
12054
12055note aw_next: the next word of the input from aw_p (blanks skipped, newlines counted in aw_nl)
12056fn aw_next to void
12057p = get aw_p 0
12058e = get inlen 0
12059nl = ulong 0
12060go = true
12061while and go lt p e
12062b = get inp p
12063if or eq b 32 eq b 9
12064p = add p 1
12065else
12066if eq b 10
12067nl = add nl 1
12068p = add p 1
12069else
12070go = false
12071end
12072end
12073end
12074w = ulong 0
12075l = ulong 0
12076go2 = true
12077while and go2 lt p e
12078c = get inp p
12079if or or eq c 32 eq c 9 eq c 10
12080go2 = false
12081else
12082if lt l 8
12083w = or w shl ulong c shl l 3
12084end
12085l = add l 1
12086p = add p 1
12087end
12088end
12089put aw_p 0 p
12090put aw_w 0 w
12091put aw_l 0 l
12092put aw_nl 0 nl
12093end
12094
12095note aw_is: the next word is w (length l) after exactly nl newlines
12096fn aw_is w ulong l ulong nl ulong to ulong
12097aw_next
12098if ne get aw_w 0 w
12099ret 0
12100end
12101if ne get aw_l 0 l
12102ret 0
12103end
12104if ne get aw_nl 0 nl
12105ret 0
12106end
12107ret 1
12108end
12109
12110note ab_upar: x (length xl) is a uint scalar parameter of function f (index)
12111fn ab_upar f ulong x ulong xl ulong to ulong
12112if gt xl 8
12113ret 0
12114end
12115i = ulong 0
12116while lt i ulong get fn_np f
12117p = add mul f 6 i
12118if eq get fn_pn p x
12119if eq get fn_pk p 0
12120if eq get fn_pt p 4
12121ret 1
12122end
12123end
12124end
12125i = add i 1
12126end
12127ret 0
12128end
12129
12130note ab_exact: the body of constructor fe is exactly `ret seal NAME or shl ulong x 32 ulong x` then `end`
12131fn ab_exact fe ulong a ulong to ulong
12132f = sub fe 1
12133put aw_p 0 get fn_body f
12134if eq aw_is 7628146 3 0 0
12135ret 0
12136end
12137if eq aw_is 1818322291 4 0 0
12138ret 0
12139end
12140if eq aw_is get ab_name a get ab_len a 0 0
12141ret 0
12142end
12143if eq aw_is 29295 2 0 0
12144ret 0
12145end
12146if eq aw_is 7104627 3 0 0
12147ret 0
12148end
12149if eq aw_is 444234427509 5 0 0
12150ret 0
12151end
12152aw_next
12153if ne get aw_nl 0 0
12154ret 0
12155end
12156x = get aw_w 0
12157xl = get aw_l 0
12158if eq ab_upar f x xl 0
12159ret 0
12160end
12161if eq aw_is 12851 2 0 0
12162ret 0
12163end
12164if eq aw_is 444234427509 5 0 0
12165ret 0
12166end
12167if eq aw_is x xl 0 0
12168ret 0
12169end
12170if eq aw_is 6581861 3 1 0
12171ret 0
12172end
12173aw_next
12174if and ne get aw_l 0 0 eq get aw_nl 0 0
12175ret 0
12176end
12177ret 1
12178end
12179
12180note ab_bin: a binary operation whose operand (fr[x..x+5]) is abstract: only a listed preserving one, run
12181note on the representation type T and giving NAME; rotr/rotl take a T count (E9), and/or/xor two NAME values
12182fn ab_bin c ulong dst ulong x ulong to void
12183at = get fr add x 2
12184if lt at 64
12185fail 7
12186end
12187a = sub at 64
12188b = opbit c
12189if eq b 0
12190fail 7
12191end
12192if eq and get ab_ops a b 0
12193fail 7
12194end
12195t = ulong get ab_base a
12196bk = get fr add x 3
12197bt = get fr add x 5
12198if or eq c 59 eq c 60
12199if eq bk 2
12200range get fr add x 4 t bt
12201put fr add x 3 3
12202else
12203if ne bt t
12204fail 7
12205end
12206end
12207else
12208if ne bt at
12209fail 7
12210end
12211end
12212put fr add x 2 t
12213put fr add x 5 t
12214binop c dst x
12215put rt 0 at
12216end
12217
12218note ======== OM-2 section 5: fresh scratch (spec 5, erratum E8). `NAME edit T N fresh` (N <= 64): the
12219note incoming elements are unspecified. Each such array has a written set (bit i: element i definitely
12220note written on the path being compiled). A put with a number index (after unrolling and inlining) sets
12221note its bit; a get with a number index needs its bit, any other get all N bits (else error 27,
12222note obligation=init). Joins: an if without else, a while and a rolled for keep the set they opened with;
12223note an if/else takes the intersection of its arms (or the arm that does not end in ret); an unrolled
12224note body and an inlined body keep what they wrote. Passing the array to a non-inline call, read, write or
12225note a host operation is error 27; an inline callee works on the same set. ========
12226note fz_par: parameter slot f*6+np of kind k with N = ln is fresh: an edit only (else 16), N <= 64 (else 14)
12227fn fz_par f ulong np ulong k ulong ln ulong to void
12228if ne k 3
12229fail 16
12230end
12231if gt ln 64
12232put dobx 0 1
12233fail 14
12234end
12235put fn_pfr add mul f 6 np 1
12236end
12237
12238note fz_bind: the parameter name just added for slot p gets a new written set in the body pass
12239fn fz_bind p ulong ln ulong to void
12240if eq get fn_pfr p 0
12241ret
12242end
12243if ne get scanning 0 0
12244ret
12245end
12246fz_new sub get nn 0 1 ln
12247end
12248
12249note fz_new: a new, empty written set of N elements for name index n (16 per function, error 14)
12250fn fz_new n ulong ln ulong to void
12251s = get fz_n 0
12252if ge s 16
12253put dobx 0 1
12254fail 14
12255end
12256put fz_len s ln
12257put fz_cur s 0
12258put nm_fr n uchar add s 1
12259put fz_n 0 add s 1
12260end
12261
12262note fz_inl: an inline parameter slot p bound to array name e: its own fresh set, or e's set
12263fn fz_inl p ulong e ulong to void
12264pk = ulong get fn_pk p
12265if or eq pk 0 ge pk 6
12266ret
12267end
12268n = sub get nn 0 1
12269if ne get fn_pfr p 0
12270fz_new n get fn_pl p
12271ret
12272end
12273put nm_fr n get nm_fr sub e 1
12274end
12275
12276fn fz_else i ulong to void
12277j = ulong 0
12278while lt j get fz_n 0
12279x = add mul i 16 j
12280put fz_thn x get fz_cur j
12281put fz_cur j get fz_ent x
12282j = add j 1
12283end
12284end
12285
12286fn fz_join i ulong k ulong to void
12287td = get bdead i
12288ed = get dead 0
12289j = ulong 0
12290while lt j get fz_n 0
12291x = add mul i 16 j
12292if eq k 2
12293v = and get fz_cur j get fz_thn x
12294if and ne td 0 eq ed 0
12295v = get fz_cur j
12296end
12297if and ne ed 0 eq td 0
12298v = get fz_thn x
12299end
12300put fz_cur j v
12301else
12302put fz_cur j get fz_ent x
12303end
12304j = add j 1
12305end
12306end
12307
12308note fz_get: get e i with the index in rk/rv/rt
12309fn fz_get e ulong to void
12310if ne get cvalid 0 0
12311ret
12312end
12313s = ulong get nm_fr sub e 1
12314if eq s 0
12315ret
12316end
12317s = sub s 1
12318n = get fz_len s
12319if eq get rk 0 3
12320v = get rv 0
12321if lt v n
12322if eq and shr get fz_cur s v 1 0
12323fail 27
12324end
12325end
12326ret
12327end
12328if ne get fz_cur s fmask n
12329fail 27
12330end
12331end
12332
12333note fz_put: put e i v with index (k, v): a number index sets its bit
12334fn fz_put e ulong k ulong v ulong to void
12335s = ulong get nm_fr sub e 1
12336if eq s 0
12337ret
12338end
12339s = sub s 1
12340if eq k 3
12341if lt v get fz_len s
12342put fz_cur s or get fz_cur s shl ulong 1 v
12343end
12344end
12345end
12346
12347note fz_arg: array name e leaves the analysis (a non-inline call, read, write, a host operation): error 27
12348note when it is tracked
12349fn fz_arg e ulong to void
12350if ne get nm_fr sub e 1 0
12351fail 27
12352end
12353end
12354
12355note ======== OM-2 section 5: external layouts (spec 5, errata E5, E8, E9). `layout NAME SIZE`, then one
12356note `field F T [COUNT] at OFF little|big` per line, then `end`, before the first fn. Read in the names pass;
12357note the body pass jumps over the block. A parameter `c view|edit NAME` is a uchar array of SIZE bytes (one
12358note pointer); `get c F [i]` / `put c F [i] v` load / store the field at its width and byte order:
12359note LDR/STR (register, unscaled offset; C6.2.216 family) at base + OFF + i * width, REV16 / REV (C6.2.333,
12360note C6.2.331) for big, a run-time index checked like an array's (CMP, B.HS trap 3) ========
12361note findlay: packed name -> layout index + 1, or 0
12362fn findlay w ulong to ulong
12363i = ulong 0
12364while lt i get nly 0
12365if eq get ly_name i w
12366ret add i 1
12367end
12368i = add i 1
12369end
12370ret 0
12371end
12372
12373note findlf: layout index li, packed field name -> field-table index + 1, or 0
12374fn findlf li ulong w ulong to ulong
12375fo = get ly_fo li
12376i = ulong 0
12377while lt i get ly_nf li
12378if eq get lf_name add fo i w
12379ret add add fo i 1
12380end
12381i = add i 1
12382end
12383ret 0
12384end
12385
12386note ns_om2: an OM-2 declaration line in the names pass (1 when it was read), else 0
12387fn ns_om2 to ulong
12388if eq get om2 0 0
12389ret 0
12390end
12391if ne get tk 0 1
12392ret 0
12393end
12394if gt get tl 0 8
12395ret 0
12396end
12397if eq get tw 0 128047730221420
12398s_layout
12399ret 1
12400end
12401if eq get tw 0 8386654075301880417
12402s_abstract
12403ret 1
12404end
12405if eq get tw 0 495606067048
12406note s_holds: holds NAME dup32 (ulong representation only); any other predicate is error 28 on this line
12407if ne get nf 0 0
12408fail 9
12409end
12410tok
12411if ne get tk 0 1
12412fail 16
12413end
12414a = ulong 0
12415if le get tl 0 8
12416a = findab get tw 0
12417end
12418if eq a 0
12419fail 16
12420end
12421a = sub a 1
12422tok
12423if or eq get tk 0 0 eq get tk 0 3
12424fail 4
12425end
12426if ne get tk 0 1
12427fail 28
12428end
12429if or ne get tl 0 5 ne get tw 0 215611372900
12430fail 28
12431end
12432if ne get ab_base a 5
12433fail 28
12434end
12435if ne get ab_holds a 0
12436fail 10
12437end
12438put ab_holds a 1
12439eol
12440ret 1
12441end
12442if eq get tw 0 128034844735589
12443s_export
12444ret 1
12445end
12446ret 0
12447end
12448
12449note s_export (OM-2, spec 6): `export fn ...` at top level: the fn line of an exported entry, in both
12450note passes. Function mode only (error 9), at most 32 (error 14), counted and marked in the names pass.
12451fn s_export to void
12452tok
12453if or ne get tk 0 1 ne get tw 0 28262
12454fail 16
12455end
12456if eq get scanning 0 1
12457if ne get progmode 0 0
12458fail 9
12459end
12460if ge get nexp 0 32
12461fail 14
12462end
12463put nexp 0 add get nexp 0 1
12464end
12465put expf 0 1
12466s_fn
12467put expf 0 0
12468end
12469
12470note ly_num: the next token must be a number (error 4 at the line end, 16 for another token, 29 when it
12471note was written as 2^63 or more)
12472fn ly_num to ulong
12473tok
12474if or eq get tk 0 0 eq get tk 0 3
12475fail 4
12476end
12477if ne get tk 0 2
12478fail 16
12479end
12480if ne get tbig 0 0
12481fail 29
12482end
12483ret get tw 0
12484end
12485
12486note s_layout: SIZE 1..4096 and a layout after the first fn are error 29 on the layout line
12487fn s_layout to void
12488if ne get nf 0 0
12489fail 29
12490end
12491tok
12492if ne get tk 0 1
12493fail 16
12494end
12495if gt get tl 0 8
12496fail 13
12497end
12498w = get tw 0
12499if ne kw w 0
12500fail 16
12501end
12502newname w get tl 0 get ts 0
12503if ge get nly 0 16
12504fail 14
12505end
12506sz = ly_num
12507if or eq sz 0 gt sz 4096
12508fail 29
12509end
12510eol
12511li = get nly 0
12512fo = get nlf 0
12513i = ulong 0
12514while lt i sz
12515put lymap i 0
12516i = add i 1
12517end
12518go = true
12519while go
12520tok
12521k = get tk 0
12522if eq k 3
12523fail 4
12524end
12525if eq k 1
12526c = ulong 0
12527if le get tl 0 8
12528c = kw get tw 0
12529end
12530if eq c 66
12531eol
12532go = false
12533else
12534if eq c 83
12535skipnl
12536else
12537if ne c 189
12538fail 16
12539end
12540ly_field fo sz
12541end
12542end
12543else
12544if ne k 0
12545fail 16
12546end
12547end
12548end
12549put ly_name li w
12550put ly_size li sz
12551put ly_fo li fo
12552put ly_nf li sub get nlf 0 fo
12553put ly_ec li get cur 0
12554put ly_el li get line 0
12555put nly 0 add li 1
12556end
12557
12558note ly_field: F T [COUNT] at OFF little|big. A field over SIZE, a COUNT outside 1..4096, an OFF out of range
12559note or two fields sharing a byte are error 29 on the field line. Field types are the integer types.
12560fn ly_field fo ulong sz ulong to void
12561tok
12562if or eq get tk 0 0 eq get tk 0 3
12563fail 4
12564end
12565if ne get tk 0 1
12566fail 16
12567end
12568if gt get tl 0 8
12569fail 13
12570end
12571fw = get tw 0
12572if ne kw fw 0
12573fail 16
12574end
12575n = get nlf 0
12576i = fo
12577while lt i n
12578if eq get lf_name i fw
12579fail 10
12580end
12581i = add i 1
12582end
12583if ge n 256
12584fail 14
12585end
12586t = typeword
12587if or lt t 2 gt t 9
12588fail 7
12589end
12590cnt = ulong 0
12591tok
12592if eq get tk 0 2
12593if ne get tbig 0 0
12594fail 29
12595end
12596cnt = get tw 0
12597if or eq cnt 0 gt cnt 4096
12598fail 29
12599end
12600tok
12601end
12602if or eq get tk 0 0 eq get tk 0 3
12603fail 4
12604end
12605if ne get tk 0 1
12606fail 16
12607end
12608if ne kw get tw 0 190
12609fail 16
12610end
12611off = ly_num
12612big = ly_end
12613ly_mark off cnt t sz
12614eol
12615put lf_name n fw
12616put lf_type n uchar t
12617put lf_cnt n cnt
12618put lf_off n off
12619put lf_big n uchar big
12620put nlf 0 add n 1
12621end
12622
12623note ly_end: little (0) or big (1)
12624fn ly_end to ulong
12625tok
12626if or eq get tk 0 0 eq get tk 0 3
12627fail 4
12628end
12629if ne get tk 0 1
12630fail 16
12631end
12632c = kw get tw 0
12633if eq c 191
12634ret 0
12635end
12636if eq c 192
12637ret 1
12638end
12639fail 16
12640ret 0
12641end
12642
12643note ly_mark: the bytes OFF .. OFF + max(COUNT, 1) * width must lie inside SIZE and be free (error 29)
12644fn ly_mark off ulong cnt ulong t ulong sz ulong to void
12645n = cnt
12646if eq n 0
12647n = ulong 1
12648end
12649by = shl n tlog t
12650if gt off sz
12651fail 29
12652end
12653e = add off by
12654if gt e sz
12655fail 29
12656end
12657i = off
12658while lt i e
12659if ne get lymap i 0
12660fail 29
12661end
12662put lymap i 1
12663i = add i 1
12664end
12665end
12666
12667note peek_lay: if the next word is a layout name, consume it and return index + 1; else rewind
12668fn peek_lay to ulong
12669p = get cur 0
12670ln = get line 0
12671tok
12672if eq get tk 0 1
12673if le get tl 0 8
12674lid = findlay get tw 0
12675if ne lid 0
12676ret lid
12677end
12678end
12679end
12680put cur 0 p
12681put line 0 ln
12682ret 0
12683end
12684
12685note fn_p_ly: parameter pw is a layout (view k 2 / edit k 3): a uchar array of SIZE bytes, name kind 2 / 3
12686fn fn_p_ly f ulong np ulong pw ulong lid ulong k ulong to void
12687ln = get ly_size sub lid 1
12688addname pw poolc get psl 0 2 ln k 0
12689put nm_lay sub get nn 0 1 uchar lid
12690p = add mul f 6 np
12691put fn_pt p 2
12692put fn_pl p ln
12693put fn_pk p uchar k
12694put fn_play p uchar lid
12695end
12696
12697note ly_fld: for a layout name e, when the next word is one of its fields: consume it, return the field-table
12698note index + 1; otherwise rewind and return 0 (the name is then an ordinary uchar array)
12699fn ly_fld e ulong to ulong
12700lid = ulong get nm_lay sub e 1
12701if eq lid 0
12702ret 0
12703end
12704p = get cur 0
12705ln = get line 0
12706tok
12707if eq get tk 0 1
12708if le get tl 0 8
12709j = findlf sub lid 1 get tw 0
12710if ne j 0
12711ret j
12712end
12713end
12714end
12715put cur 0 p
12716put line 0 ln
12717ret 0
12718end
12719
12720note ldun / stun: LDRB / LDRH / LDR w / LDR x and the stores, register offset, LSL #0 (S = 0)
12721fn ldun t ulong to ulong
12722if or eq t 2 eq t 6
12723ret 945842176
12724end
12725if or eq t 3 eq t 7
12726ret 2019584000
12727end
12728if or eq t 4 eq t 8
12729ret 3093325824
12730end
12731ret 4167067648
12732end
12733
12734fn stun t ulong to ulong
12735if or eq t 2 eq t 6
12736ret 941647872
12737end
12738if or eq t 3 eq t 7
12739ret 2015389696
12740end
12741if or eq t 4 eq t 8
12742ret 3089131520
12743end
12744ret 4162873344
12745end
12746
12747note ly_rev: Rd = the bytes of Rs reversed at width t: REV16 Wd (C6.2.333), REV Wd, REV Xd (C6.2.331)
12748fn ly_rev t ulong s ulong d ulong to void
12749if ne get xtarget 0 0
12750xload 0 s
12751if or eq t 2 eq t 6
12752xstore 0 d
12753ret
12754end
12755if or eq t 3 eq t 7
12756emitb 102
12757xb 193 200 8
12758else
12759if or eq t 5 eq t 9
12760emitb 72
12761end
12762emitb 15
12763emitb 200
12764end
12765xstore 0 d
12766ret
12767end
12768if or eq t 3 eq t 7
12769emit or or 1522533376 d shl s 5
12770end
12771if or eq t 4 eq t 8
12772emit or or 1522534400 d shl s 5
12773end
12774if or eq t 5 eq t 9
12775emit or or 3670019072 d shl s 5
12776end
12777end
12778
12779note ly_ix: the byte-offset register of element (k, v, t) of array field j: a number index must be below
12780note COUNT (error 21); a register index: CMP Xi, #COUNT or Xc (C6.2.96 / C6.2.97), B.HS trap 3, then
12781note Xo = OFF; ADD Xo, Xo, Xi, LSL #log2 width (C6.2.6)
12782fn ly_ix j ulong k ulong v ulong t ulong to ulong
12783if or eq t 1 ge t 10
12784fail 7
12785end
12786cnt = get lf_cnt j
12787sh = tlog ulong get lf_type j
12788x = alloc
12789if eq k 3
12790if ge v cnt
12791fail 21
12792end
12793matconst x add get lf_off j shl v sh
12794ret x
12795end
12796r = matreg k v t
12797if ne get xtarget 0 0
12798xcmp r cnt 1
12799xtrap 2 3
12800xload 0 r
12801xb 72 193 224
12802emitb uchar sh
12803ximm 1 get lf_off j
12804xrr 1 0 1
12805xstore 0 x
12806ret x
12807end
12808if le cnt 4095
12809emit or or 4043309087 shl cnt 10 shl r 5
12810else
12811y = alloc
12812matconst y cnt
12813emit or or 3942645791 shl y 16 shl r 5
12814end
12815trapc 2 3
12816matconst x get lf_off j
12817emit or or or or 2332033024 x shl x 5 shl r 16 shl sh 10
12818ret x
12819end
12820
12821note ly_get: get c F [i] on a layout name e (1 when it was a field, the value in rk/rv/rt), else 0
12822fn ly_get e ulong dst ulong to ulong
12823j = ly_fld e
12824if eq j 0
12825ret 0
12826end
12827j = sub j 1
12828ft = ulong get lf_type j
12829x = ulong 0
12830if eq get lf_cnt j 0
12831x = alloc
12832matconst x get lf_off j
12833else
12834rdval 0 5 255 0
12835x = ly_ix j get rk 0 get rv 0 get rt 0
12836end
12837rd = destreg dst
12838if ne get xtarget 0 0
12839xlayout 0 ulong get nm_reg sub e 1 x ft rd
12840else
12841emit or or or ldun ft rd shl ulong get nm_reg sub e 1 5 shl x 16
12842end
12843if ne get lf_big j 0
12844ly_rev ft rd rd
12845end
12846if gt ft 5
12847canon rd ft
12848end
12849put rk 0 1
12850put rv 0 rd
12851put rt 0 ft
12852ret 1
12853end
12854
12855note ly_put: put c F [i] v on a layout name e (an edit; a view was refused 20 before): index, value, then
12856note the store; 1 when it was a field, else 0
12857fn ly_put e ulong to ulong
12858j = ly_fld e
12859if eq j 0
12860ret 0
12861end
12862j = sub j 1
12863if ne get lf_cnt j 0
12864rdval 0 5 255 0
12865put svk 0 get rk 0
12866put svv 0 get rv 0
12867put svt 0 get rt 0
12868end
12869ly_st e j
12870eol
12871ret 1
12872end
12873
12874note ly_st: the value of field j (index saved in svk/svv/svt), then the store through layout name e
12875fn ly_st e ulong j ulong to void
12876ft = ulong get lf_type j
12877cnt = get lf_cnt j
12878rdval 0 ft 255 0
12879if ne get rt 0 ft
12880fail 7
12881end
12882v = matreg get rk 0 get rv 0 get rt 0
12883x = ulong 0
12884if eq cnt 0
12885x = alloc
12886matconst x get lf_off j
12887else
12888x = ly_ix j get svk 0 get svv 0 get svt 0
12889end
12890if and ne get lf_big j 0 ne tlog ft 0
12891y = alloc
12892ly_rev ft v y
12893v = y
12894end
12895if ne get xtarget 0 0
12896xlayout 1 ulong get nm_reg sub e 1 x ft v
12897else
12898emit or or or stun ft v shl ulong get nm_reg sub e 1 5 shl x 16
12899end
12900end
12901
12902note vt_fget_go: field j of struct name e. Scalar, or an index then the element.
12903fn vt_fget_go e ulong dst ulong j ulong to void
12904fl = ulong get sf_len j
12905ft = ulong get sf_type j
12906off = ulong get sf_off j
12907base = ulong get nm_reg sub e 1
12908idx = alloc
12909if eq fl 0
12910st_store_ld base idx off ft dst
12911else
12912rdval 0 5 255 0
12913if eq get rt 0 1
12914fail 7
12915end
12916if eq get rk 0 3
12917if ge get rv 0 fl
12918fail 21
12919end
12920st_store_ld base idx add off get rv 0 ft dst
12921else
12922if ne get rk 0 1
12923fail 7
12924end
12925st_ixreg idx get rv 0 off fl ft
12926st_ld base idx ft dst
12927end
12928end
12929end
12930
12931note vt_fget: get p f, or get p f i for an array field. Kind 6 own, 7 edit, 8 view; all may be read.
12932fn vt_fget e ulong dst ulong to void
12933i = sub e 1
12934tok
12935if ne get tk 0 1
12936fail 16
12937end
12938if gt get tl 0 8
12939fail 13
12940end
12941si = sub ulong get nm_type i 16
12942fid = findfield si get tw 0
12943if eq fid 0
12944fail 3
12945end
12946vt_fget_go e dst sub fid 1
12947end
12948
12949note st_store_ld: constant cell, then load. Split out so vt_fget stays under 16 registers.
12950fn st_store_ld base ulong idx ulong cell ulong t ulong dst ulong to void
12951matconst idx shl cell sub 3 tlog t
12952st_ld base idx t dst
12953end
12954
12955note s_putf_go: field j of struct name e. Read the value (and the index, if an array field) and store.
12956fn s_putf_go e ulong j ulong to void
12957fl = ulong get sf_len j
12958ft = ulong get sf_type j
12959off = ulong get sf_off j
12960base = ulong get nm_reg sub e 1
12961if ne fl 0
12962rdval 0 5 255 0
12963if eq get rt 0 1
12964fail 7
12965end
12966put svk 0 get rk 0
12967put svv 0 get rv 0
12968put svt 0 get rt 0
12969end
12970rdval 0 ft 255 0
12971if ne get rt 0 ft
12972fail 7
12973end
12974v = matreg get rk 0 get rv 0 get rt 0
12975idx = alloc
12976if eq fl 0
12977st_store base idx off ft v
12978else
12979s_putf_ix base idx v off fl ft
12980end
12981eol
12982end
12983
12984note s_putf: put p f v, or put p f i v. A view struct is error 20.
12985fn s_putf e ulong to void
12986i = sub e 1
12987if eq ulong get nm_kind i 8
12988fail 20
12989end
12990tok
12991if ne get tk 0 1
12992fail 16
12993end
12994if gt get tl 0 8
12995fail 13
12996end
12997si = sub ulong get nm_type i 16
12998fid = findfield si get tw 0
12999if eq fid 0
13000fail 3
13001end
13002s_putf_go e sub fid 1
13003end
13004
13005note s_putf_ix: the index was saved in svk/svv/svt before the value was read
13006fn s_putf_ix base ulong idx ulong v ulong off ulong fl ulong ft ulong to void
13007ik = get svk 0
13008iv = get svv 0
13009if eq get svt 0 1
13010fail 7
13011end
13012if eq ik 3
13013if ge iv fl
13014fail 21
13015end
13016st_store base idx add off iv ft v
13017else
13018if ne ik 1
13019fail 7
13020end
13021st_ixreg idx iv off fl ft
13022put ea_b 0 base
13023put ea_i 0 idx
13024stel ft v
13025end
13026end
13027
13028note ca_st_ok: read one struct argument of parameter i of function f. Returns the name entry.
13029fn ca_st_ok f ulong i ulong to ulong
13030md = ulong 0
13031aa_word
13032w = get tw 0
13033c = kw w
13034if ne c 0
13035if or lt c 57 gt c 58
13036fail 16
13037end
13038md = c
13039aa_word
13040w = get tw 0
13041end
13042e = findname w
13043if eq e 0
13044fail 3
13045end
13046ni = sub e 1
13047k = ulong get nm_kind ni
13048if lt k 6
13049fail 7
13050end
13051p = add mul sub f 1 6 i
13052pk = ulong get fn_pk p
13053if eq pk 7
13054if eq md 57
13055fail 20
13056end
13057if eq k 8
13058fail 20
13059end
13060else
13061if eq md 58
13062fail 7
13063end
13064end
13065if ne ulong get nm_type ni ulong get fn_pt p
13066fail 7
13067end
13068ret e
13069end
13070
13071note ca_st: a struct argument. edit (fn_pk 7) needs own or edit; view takes any. Same struct.
13072fn ca_st f ulong i ulong cb ulong to void
13073e = ca_st_ok f i
13074note ca_dist: the same struct name passed twice in one call is error 19.
13075j = ulong 0
13076while lt j i
13077if eq get fr add add cb mul j 3 2 e
13078fail 19
13079end
13080j = add j 1
13081end
13082q = add cb mul i 3
13083put fr q 5
13084put fr add q 1 ulong get nm_reg sub e 1
13085put fr add q 2 e
13086end
13087note ---- R6 IR. A leading line `ir` writes OSC IR version 1. The AArch64 path is not taken.
13088note Worksheet ir_match.py matched omega_emit on the 10 dev programs; this is that algorithm.
13089
13090global ir_rty uchar 64
13091global ir_nreg ulong 1
13092global ir_nm ulong 32
13093global ir_nrr uchar 32
13094global ir_nn ulong 1
13095global ir_cty uchar 32
13096global ir_cm ulong 32
13097global ir_cr uchar 32
13098global ir_nc ulong 1
13099global ir_bord ulong 32
13100global ir_nord ulong 1
13101global ir_cur ulong 1
13102global ir_dead ulong 1
13103global ir_nb ulong 1
13104global ir_ok uchar 256
13105global ir_oa ulong 256
13106global ir_ob ulong 256
13107global ir_oc ulong 256
13108global ir_od ulong 256
13109global ir_obl ulong 256
13110global ir_no ulong 1
13111global ir_bf ulong 32
13112global ir_bc ulong 32
13113global ir_fk uchar 8
13114global ir_fa ulong 8
13115global ir_fb ulong 8
13116global ir_fc ulong 8
13117global ir_fd ulong 8
13118global ir_nf ulong 1
13119global ir_ret ulong 1
13120global ir_npar ulong 1
13121global ir_rr ulong 1
13122global ir_vt ulong 1
13123global ir_bare ulong 1
13124global ir_isb ulong 1
13125global ir_ns ulong 1
13126global ir_nl ulong 1
13127global ir_have ulong 1
13128global irrec uchar 2620252
13129global irrlen ulong 1
13130global irnf ulong 1
13131global irf_nm ulong 32
13132global irf_ns ulong 32
13133global irf_nl ulong 32
13134global irf_np ulong 32
13135global irf_ret uchar 32
13136global irf_pt uchar 192
13137global irf_n ulong 1
13138global ir_args ulong 6
13139global ir_oarg ulong 1536
13140global ir_miss ulong 1
13141global ir_cl ulong 1
13142global ir_cs ulong 1
13143note per open block frame, the local count when it opened (names bound inside go out of scope at else / end)
13144global ir_fnn ulong 8
13145note ir_bbig: tbig of the bare number in ir_bare, for the range check once its type is known (error 8).
13146global ir_bbig ulong 1
13147
13148note ir_lead: 1 when the first line is "ir" (after blanks). Both scans run as nested 999999-pass loops,
13149note so more than 1000000 leading blanks or first-line bytes are read, not trapped 4 (#671).
13150fn ir_lead to ulong
13151p = ulong 0
13152go = true
13153while go
13154m = ulong 0
13155while and go lt m 999999
13156if ge p get inlen 0
13157go = false
13158else
13159c = get inp p
13160if gt c 32
13161go = false
13162else
13163if eq c 10
13164ret 0
13165end
13166p = add p 1
13167end
13168end
13169m = add m 1
13170end
13171end
13172if ge add p 3 get inlen 0
13173ret 0
13174end
13175if ne get inp p 105
13176ret 0
13177end
13178if ne get inp add p 1 114
13179ret 0
13180end
13181if gt get inp add p 2 32
13182ret 0
13183end
13184q = add p 2
13185go = true
13186while go
13187m = ulong 0
13188while and go lt m 999999
13189if ge q get inlen 0
13190go = false
13191else
13192if eq get inp q 10
13193go = false
13194else
13195q = add q 1
13196end
13197end
13198m = add m 1
13199end
13200end
13201if lt q get inlen 0
13202q = add q 1
13203end
13204put cur 0 q
13205put line 0 2
13206ret 1
13207end
13208
13209fn ir_w16 v ulong to void
13210emitb uchar and v 255
13211emitb uchar and shr v 8 255
13212end
13213
13214fn ir_w32 v ulong to void
13215ir_w16 v
13216ir_w16 shr v 16
13217end
13218
13219fn ir_w64 v ulong to void
13220ir_w32 v
13221ir_w32 shr v 32
13222end
13223
13224fn ir_newreg ty ulong to ulong
13225r = get ir_nreg 0
13226if ge r 64
13227fail 14
13228end
13229put ir_rty r uchar ty
13230put ir_nreg 0 add r 1
13231ret r
13232end
13233
13234fn ir_block to ulong
13235b = get ir_nb 0
13236if ge b 32
13237fail 14
13238end
13239put ir_bord b 65535
13240put ir_nb 0 add b 1
13241ret b
13242end
13243
13244fn ir_start b ulong to void
13245put ir_bord b get ir_nord 0
13246put ir_nord 0 add get ir_nord 0 1
13247put ir_cur 0 b
13248put ir_dead 0 0
13249end
13250
13251fn ir_op k ulong a ulong b ulong c ulong d ulong to void
13252if ne get ir_dead 0 0
13253fail 17
13254end
13255n = get ir_no 0
13256if ge n 256
13257fail 14
13258end
13259put ir_ok n uchar k
13260put ir_oa n a
13261put ir_ob n b
13262put ir_oc n c
13263put ir_od n d
13264put ir_obl n get ir_cur 0
13265put ir_no 0 add n 1
13266end
13267
13268fn ir_lit mag ulong ty ulong to ulong
13269i = ulong 0
13270n = get ir_nc 0
13271while lt i n
13272if and eq ulong get ir_cty i ty eq get ir_cm i mag
13273ret ulong get ir_cr i
13274end
13275i = add i 1
13276end
13277if ge n 32
13278fail 14
13279end
13280r = ir_newreg ty
13281put ir_cty n uchar ty
13282put ir_cm n mag
13283put ir_cr n uchar r
13284put ir_nc 0 add n 1
13285ret r
13286end
13287
13288note ir_find: the local in scope named w (packed) -> its index + 1, or 0. Locals are at most 8 bytes, so a
13289note word of length l over 8 is never one (its packed first 8 bytes may equal a local's name).
13290fn ir_find w ulong l ulong to ulong
13291if gt l 8
13292ret 0
13293end
13294i = ulong 0
13295while lt i get ir_nn 0
13296if eq get ir_nm i w
13297ret add i 1
13298end
13299i = add i 1
13300end
13301ret 0
13302end
13303
13304fn ir_bind w ulong r ulong to void
13305n = get ir_nn 0
13306if ge n 32
13307fail 14
13308end
13309put ir_nm n w
13310put ir_nrr n uchar r
13311put ir_nn 0 add n 1
13312end
13313
13314note ir_kwt: the keyword number of the current word, or 0 for a word over 8 bytes (only 8 are packed).
13315fn ir_kwt to ulong
13316if gt get tl 0 8
13317ret 0
13318end
13319ret kw get tw 0
13320end
13321
13322note ir_newname: a new local, parameter or function name must not be a keyword (16), a local in scope, a
13323note constant, or a function of the unit so far, the current one included (10, as newname). Function
13324note names compare in full (up to 63 bytes).
13325fn ir_newname w ulong s ulong l ulong to void
13326if le l 8
13327if ne kw w 0
13328fail 16
13329end
13330if ne findname w 0
13331fail 10
13332end
13333end
13334if ne ir_find w l 0
13335fail 10
13336end
13337put ir_cl 0 l
13338put ir_cs 0 s
13339j = ulong 0
13340while lt j get irf_n 0
13341if eq get irf_nm j w
13342if ne ir_nameeq j 0
13343fail 10
13344end
13345end
13346j = add j 1
13347end
13348end
13349
13350note ir_newloc: a new local, parameter or for counter: at most 8 bytes (13, as native), then ir_newname.
13351fn ir_newloc w ulong s ulong l ulong to void
13352if gt l 8
13353fail 13
13354end
13355ir_newname w s l
13356end
13357
13358note ir_type: a type word IR v1 lowers: 1 .. 9, or void (76) when isret is 1. A float, a view / edit array or
13359note void elsewhere is error 9 (as typeword in IR mode); no word is 4; another word 16.
13360fn ir_type isret ulong to ulong
13361tok
13362if or eq get tk 0 0 eq get tk 0 3
13363fail 4
13364end
13365if ne get tk 0 1
13366fail 16
13367end
13368c = ir_kwt
13369if and ge c 1 le c 9
13370ret c
13371end
13372if and eq c 76 eq isret 1
13373ret c
13374end
13375if or or eq c 76 eq c 88 eq c 89
13376fail 9
13377end
13378if or eq c 57 eq c 58
13379fail 9
13380end
13381fail 16
13382ret 0
13383end
13384
13385fn ir_push k ulong a ulong b ulong c ulong d ulong to void
13386n = get ir_nf 0
13387if ge n 8
13388fail 14
13389end
13390put ir_fk n uchar k
13391put ir_fa n a
13392put ir_fb n b
13393put ir_fc n c
13394put ir_fd n d
13395put ir_fnn n get ir_nn 0
13396put ir_nf 0 add n 1
13397end
13398
13399fn ir_set dst ulong r ulong ty ulong to void
13400put ir_isb 0 0
13401if eq dst 65534
13402put ir_rr 0 r
13403put ir_vt 0 ty
13404ret
13405end
13406d = dst
13407if eq dst 65535
13408d = ir_newreg ty
13409end
13410ir_op 2 d r 0 0
13411put ir_rr 0 d
13412put ir_vt 0 ty
13413end
13414
13415fn ir_ord id ulong to ulong
13416ret get ir_bord id
13417end
13418
13419
13420
13421
13422fn ir_wins k ulong to void
13423if eq k 0
13424i = ulong 0
13425while lt i get ir_nc 0
13426emitb 1
13427ir_w16 ulong get ir_cr i
13428ir_w64 get ir_cm i
13429i = add i 1
13430end
13431end
13432i = ulong 0
13433while lt i get ir_no 0
13434if eq ir_ord get ir_obl i k
13435op = ulong get ir_ok i
13436emitb uchar op
13437if eq op 1
13438ir_w16 get ir_oa i
13439ir_w64 get ir_ob i
13440end
13441if eq op 2
13442ir_w16 get ir_oa i
13443ir_w16 get ir_ob i
13444end
13445if eq op 3
13446emitb uchar get ir_oa i
13447ir_w16 get ir_ob i
13448ir_w16 get ir_oc i
13449ir_w16 get ir_od i
13450end
13451if eq op 4
13452emitb uchar get ir_oa i
13453ir_w16 get ir_ob i
13454ir_w16 get ir_oc i
13455end
13456if eq op 5
13457emitb uchar get ir_oa i
13458ir_w16 get ir_ob i
13459ir_w16 get ir_oc i
13460ir_w16 get ir_od i
13461end
13462if eq op 6
13463ir_w16 get ir_oa i
13464ir_w16 get ir_ob i
13465end
13466if eq op 11
13467ir_w16 get ir_oa i
13468ir_w16 get ir_ob i
13469emitb uchar get ir_oc i
13470j = ulong 0
13471while lt j get ir_oc i
13472ir_w16 get ir_oarg add mul i 6 j
13473j = add j 1
13474end
13475end
13476if eq op 12
13477emitb uchar get ir_oa i
13478end
13479if eq op 13
13480ir_w16 ir_ord get ir_oa i
13481end
13482if eq op 14
13483ir_w16 get ir_oa i
13484ir_w16 ir_ord get ir_ob i
13485ir_w16 ir_ord get ir_oc i
13486end
13487if eq op 15
13488ir_w16 get ir_oa i
13489end
13490end
13491i = add i 1
13492end
13493end
13494
13495fn ir_record to void
13496emitb uchar get ir_nl 0
13497p = get ir_ns 0
13498e = add p get ir_nl 0
13499while lt p e
13500emitb get inp p
13501p = add p 1
13502end
13503emitb uchar get ir_npar 0
13504if eq get ir_ret 0 76
13505emitb 0
13506else
13507emitb uchar get ir_ret 0
13508end
13509ir_w16 get ir_nreg 0
13510i = ulong 0
13511while lt i get ir_nreg 0
13512emitb get ir_rty i
13513i = add i 1
13514end
13515note Lay out blocks and count their instructions here, preserving constant records in block zero.
13516k = ulong 0
13517at = ulong 0
13518while lt k get ir_nord 0
13519put ir_bf k at
13520c = ulong 0
13521if eq k 0
13522c = get ir_nc 0
13523end
13524i = ulong 0
13525while lt i get ir_no 0
13526if eq ir_ord get ir_obl i k
13527c = add c 1
13528end
13529i = add i 1
13530end
13531put ir_bc k c
13532at = add at c
13533k = add k 1
13534end
13535put ir_no 0 get ir_no 0
13536ir_w16 get ir_nord 0
13537k = ulong 0
13538while lt k get ir_nord 0
13539ir_w32 get ir_bf k
13540ir_w32 get ir_bc k
13541k = add k 1
13542end
13543tot = ulong 0
13544k = ulong 0
13545while lt k get ir_nord 0
13546tot = add tot get ir_bc k
13547k = add k 1
13548end
13549ir_w32 tot
13550k = ulong 0
13551while lt k get ir_nord 0
13552ir_wins k
13553k = add k 1
13554end
13555ir_w16 0
13556ir_w16 0
13557end
13558
13559note ir_emit_record: encode the current function into irrec and count it. The per-function IR state is
13560note reset by ir_fn, so this must run before the next fn line.
13561fn ir_emit_record to void
13562if ge get irnf 0 32
13563fail 14
13564end
13565put oc 0 0
13566ir_record
13567n = get oc 0
13568base = get irrlen 0
13569i = ulong 0
13570while lt i n
13571put irrec add base i get out i
13572i = add i 1
13573end
13574put irrlen 0 add base n
13575put irnf 0 add get irnf 0 1
13576end
13577
13578
13579note ir_nameeq: table entry j's name equals the current source name token (length and bytes past 8).
13580fn ir_nameeq j ulong to ulong
13581fl = get irf_nl j
13582if ne fl get ir_cl 0
13583ret 0
13584end
13585k = ulong 8
13586same = ulong 1
13587while and eq same 1 lt k get ir_cl 0
13588left = get inp add get irf_ns j k
13589right = get inp add get ir_cs 0 k
13590if ne left right
13591same = 0
13592end
13593k = add k 1
13594end
13595ret same
13596end
13597
13598note ir_findfn: packed name -> earlier function index + 1, or 0. The current function is the newest
13599note table entry and is never returned, so self and forward calls fail.
13600fn ir_findfn w ulong to ulong
13601if le get irf_n 0 1
13602ret 0
13603end
13604fc = sub get irf_n 0 1
13605j = ulong 0
13606while lt j fc
13607if eq get irf_nm j w
13608if ne ir_nameeq j 0
13609ret add j 1
13610end
13611end
13612j = add j 1
13613end
13614ret 0
13615end
13616
13617note ir_callop: CALL in the IR's five-field form; the argument registers live in ir_args[0..nargs).
13618fn ir_callop dst ulong callee ulong nargs ulong to void
13619if ne get ir_dead 0 0
13620fail 17
13621end
13622n = get ir_no 0
13623if ge n 256
13624fail 14
13625end
13626put ir_ok n 11
13627put ir_oa n dst
13628put ir_ob n callee
13629put ir_oc n nargs
13630put ir_od n 0
13631put ir_obl n get ir_cur 0
13632j = ulong 0
13633while lt j nargs
13634put ir_oarg add mul n 6 j get ir_args j
13635j = add j 1
13636end
13637put ir_no 0 add n 1
13638end
13639
13640fn ir_name to void
13641e = ir_find get tw 0 get tl 0
13642if eq e 0
13643if le get tl 0 8
13644e = findname get tw 0
13645end
13646if ne e 0
13647if eq get nm_kind sub e 1 9
13648t = ulong get nm_type sub e 1
13649if ge t 10
13650fail 9
13651end
13652r = ir_lit get nm_addr sub e 1 t
13653ir_set 65534 r t
13654ret
13655end
13656end
13657put ir_miss 0 1
13658ret
13659end
13660r = ulong get ir_nrr sub e 1
13661ir_set 65534 r ulong get ir_rty r
13662end
13663
13664fn ir_un c ulong dst ulong to void
13665ir_val 65534 0
13666if ne get ir_isb 0 0
13667fail 11
13668end
13669ty = get ir_vt 0
13670if and eq c 49 eq ty 1
13671fail 7
13672end
13673d = dst
13674if or eq dst 65534 eq dst 65535
13675d = ir_newreg ty
13676end
13677su = ulong 1
13678if eq c 50
13679su = ulong 3
13680if ne ty 1
13681su = ulong 2
13682end
13683end
13684ir_op 4 su d get ir_rr 0 0
13685put ir_rr 0 d
13686put ir_vt 0 ty
13687put ir_isb 0 0
13688end
13689
13690fn ir_bin_r left ulong lty ulong c ulong dst ulong to void
13691exp2 = lty
13692if or eq c 41 eq c 42
13693exp2 = ulong 0
13694end
13695ir_val 65534 exp2
13696ir_bin_done left lty c dst
13697end
13698
13699fn ir_bin_done left ulong lty ulong c ulong dst ulong to void
13700isb2 = get ir_isb 0
13701ty2 = get ir_vt 0
13702if or eq c 41 eq c 42
13703if or ge lty 10 eq lty 1
13704fail 7
13705end
13706if eq isb2 0
13707if or ge ty2 10 eq ty2 1
13708fail 7
13709end
13710end
13711else
13712if ge lty 10
13713fail 7
13714end
13715if and eq isb2 0 ne ty2 lty
13716fail 7
13717end
13718if and and ge c 33 le c 37 eq lty 1
13719fail 7
13720end
13721end
13722right = get ir_rr 0
13723if ne isb2 0
13724range get ir_bare 0 lty get ir_bbig 0
13725right = ir_lit get ir_bare 0 lty
13726end
13727if ge c 43
13728d = dst
13729if or eq dst 65534 eq dst 65535
13730d = ir_newreg 1
13731end
13732ir_op 5 sub c 42 d left right
13733put ir_rr 0 d
13734put ir_vt 0 1
13735ret
13736end
13737d = dst
13738if or eq dst 65534 eq dst 65535
13739d = ir_newreg lty
13740end
13741ir_op 3 sub c 32 d left right
13742put ir_rr 0 d
13743put ir_vt 0 lty
13744put ir_isb 0 0
13745end
13746
13747fn ir_bin c ulong dst ulong to void
13748ir_val 65534 0
13749if ne get ir_isb 0 0
13750mag = get ir_bare 0
13751bg = get ir_bbig 0
13752ir_val 65534 0
13753if ne get ir_isb 0 0
13754fail 11
13755end
13756ty = get ir_vt 0
13757range mag ty bg
13758left = ir_lit mag ty
13759ir_bin_done left ty c dst
13760ret
13761end
13762left = get ir_rr 0
13763lty = get ir_vt 0
13764ir_bin_r left lty c dst
13765end
13766
13767note ir_cast: TYPE v. The operand keeps its own type (as native rdval reads it, exp 0); a bare number is
13768note checked against TYPE (8). bool converts to and from numbers only by comparison (9, as vt_conv).
13769note Only a constant of TYPE is the value itself; any other same-type value is a mov, as the C oracle.
13770fn ir_cast c ulong dst ulong to void
13771ir_val 65534 0
13772if ne get ir_isb 0 0
13773range get ir_bare 0 c get ir_bbig 0
13774r = ir_lit get ir_bare 0 c
13775ir_set dst r c
13776ret
13777end
13778src = get ir_rr 0
13779sty = get ir_vt 0
13780if xor eq sty 1 eq c 1
13781fail 9
13782end
13783k = ulong 0
13784if and eq sty c eq dst 65534
13785i = ulong 0
13786while lt i get ir_nc 0
13787if eq ulong get ir_cr i src
13788k = ulong 1
13789end
13790i = add i 1
13791end
13792end
13793if ne k 0
13794put ir_rr 0 src
13795put ir_vt 0 c
13796put ir_isb 0 0
13797ret
13798end
13799d = dst
13800if or eq dst 65534 eq dst 65535
13801d = ir_newreg c
13802end
13803if eq sty c
13804ir_op 2 d src 0 0
13805else
13806ir_op 6 d src 0 0
13807end
13808put ir_rr 0 d
13809put ir_vt 0 c
13810put ir_isb 0 0
13811end
13812
13813fn ir_val dst ulong exp ulong to void
13814put ir_isb 0 0
13815put ir_miss 0 0
13816tok
13817if or eq get tk 0 0 eq get tk 0 3
13818fail 4
13819end
13820if or eq get tk 0 4 eq get tk 0 5
13821fail 9
13822end
13823if eq get tk 0 2
13824if eq exp 0
13825put ir_bare 0 get tw 0
13826put ir_bbig 0 get tbig 0
13827put ir_isb 0 1
13828ret
13829end
13830range get tw 0 exp get tbig 0
13831r = ir_lit get tw 0 exp
13832ir_set dst r exp
13833ret
13834end
13835c = ir_kwt
13836if and ge c 1 le c 9
13837ir_cast c dst
13838ret
13839end
13840if and ge c 33 le c 48
13841ir_bin c dst
13842if and ne exp 0 ne get ir_vt 0 exp
13843fail 7
13844end
13845ret
13846end
13847if or eq c 49 eq c 50
13848ir_un c dst
13849if and ne exp 0 ne get ir_vt 0 exp
13850fail 7
13851end
13852ret
13853end
13854if eq c 55
13855if and ne exp 0 ne exp 1
13856fail 7
13857end
13858r = ir_lit 1 1
13859ir_set dst r 1
13860ret
13861end
13862if eq c 56
13863if and ne exp 0 ne exp 1
13864fail 7
13865end
13866r = ir_lit 0 1
13867ir_set dst r 1
13868ret
13869end
13870ir_name
13871if ne get ir_miss 0 0
13872put ir_cl 0 get tl 0
13873put ir_cs 0 get ts 0
13874f = ir_findfn get tw 0
13875if eq f 0
13876fail 3
13877end
13878j = sub f 1
13879rtv = ulong get irf_ret j
13880if eq rtv 76
13881fail 7
13882end
13883np = get irf_np j
13884i = ulong 0
13885while lt i np
13886ty = ulong get irf_pt add mul j 6 i
13887ir_val 65534 ty
13888if ne get ir_vt 0 ty
13889fail 7
13890end
13891put ir_args i get ir_rr 0
13892i = add i 1
13893end
13894r = dst
13895if or eq dst 65534 eq dst 65535
13896r = ir_newreg rtv
13897end
13898ir_callop r j np
13899put ir_rr 0 r
13900put ir_vt 0 rtv
13901ret
13902end
13903if ne dst 65534
13904ir_set dst get ir_rr 0 get ir_vt 0
13905end
13906end
13907
13908fn ir_end to void
13909n = sub get ir_nf 0 1
13910put ir_nf 0 n
13911k = ulong get ir_fk n
13912put ir_nn 0 get ir_fnn n
13913if eq k 1
13914if eq get ir_fc n 0
13915if eq get ir_dead 0 0
13916ir_op 13 get ir_fb n 0 0 0
13917put ir_dead 0 1
13918end
13919ir_start get ir_fb n
13920else
13921if eq get ir_dead 0 0
13922if eq get ir_fd n 65535
13923j = ir_block
13924put ir_fd n j
13925end
13926ir_op 13 get ir_fd n 0 0 0
13927put ir_dead 0 1
13928end
13929if ne get ir_fd n 65535
13930ir_start get ir_fd n
13931end
13932end
13933end
13934if eq k 2
13935if eq get ir_dead 0 0
13936ir_op 13 get ir_fa n 0 0 0
13937put ir_dead 0 1
13938end
13939ir_start get ir_fb n
13940end
13941if eq k 3
13942if eq get ir_dead 0 0
13943one = ir_lit 1 ulong get ir_rty get ir_fc n
13944ir_op 3 1 get ir_fc n get ir_fc n one
13945ir_op 13 get ir_fa n 0 0 0
13946put ir_dead 0 1
13947end
13948ir_start get ir_fb n
13949end
13950if eq k 4
13951if eq get ir_dead 0 0
13952if ne get ir_ret 0 76
13953fail 18
13954end
13955ir_op 15 65535 0 0 0
13956put ir_dead 0 1
13957end
13958put ir_have 0 1
13959end
13960eol
13961end
13962
13963note ir_bound: the rest of a while or for line: `bound N` (1 .. 1000000, error 8: the C oracle's range,
13964note omegese.c, and the guard range checker2 proves) -> N, or 0 when the line ends there.
13965fn ir_bound to ulong
13966tok
13967if or eq get tk 0 0 eq get tk 0 3
13968ret 0
13969end
13970if or ne get tk 0 1 ne ir_kwt 70
13971fail 5
13972end
13973bnd = lenval 4
13974if ge sub bnd 1 1000000
13975fail 8
13976end
13977eol
13978ret bnd
13979end
13980
13981fn ir_while to void
13982z = ir_lit 0 5
13983one = ir_lit 1 5
13984cnt = ir_newreg 5
13985ir_op 2 cnt z 0 0
13986h = ir_block
13987g = ir_block
13988tb = ir_block
13989body = ir_block
13990ex = ir_block
13991ir_op 13 h 0 0 0
13992put ir_dead 0 1
13993ir_start h
13994ir_val 65534 1
13995if ne get ir_vt 0 1
13996fail 7
13997end
13998lim = ir_bound
13999if eq lim 0
14000lim = ulong 1000000
14001end
14002lim = ir_lit lim 5
14003ir_op 14 get ir_rr 0 g ex 0
14004ir_start g
14005ger = ir_newreg 1
14006ir_op 5 6 ger cnt lim
14007ir_op 14 ger tb body 0
14008ir_start tb
14009ir_op 12 4 0 0 0
14010ir_op 13 ex 0 0 0
14011put ir_dead 0 1
14012ir_start body
14013ir_op 3 1 cnt cnt one
14014ir_push 2 h ex cnt 0
14015end
14016
14017note ir_for: for name type count [bound N]. The counter is bound after the count is read, so the count cannot
14018note name it. nc: 1 when the count is a number or an integer constant name (not a local), seen ahead without
14019note reading it. Such a count must be 0..1000000 (error 8 otherwise, a minus sign included): the C oracle's
14020note count range (omegese.c for_line), so a plain loop never counts past what checker2 proves. Such a count
14021note with no bound opens the plain loop (ir_forg with N = 0); any other count, and any count with a bound,
14022note opens the guarded loop with N, default 1000000. The counter leaves scope at its end.
14023fn ir_for to void
14024tok
14025if or eq get tk 0 0 eq get tk 0 3
14026fail 4
14027end
14028if ne get tk 0 1
14029fail 16
14030end
14031w = get tw 0
14032ir_newloc w get ts 0 get tl 0
14033ty = ir_type 0
14034if eq ty 1
14035fail 7
14036end
14037c0 = get cur 0
14038l0 = get line 0
14039a0 = get tka 0
14040b0 = get tkb 0
14041tok
14042nc = ulong 0
14043if eq get tk 0 2
14044nc = ulong 1
14045if eq get inp get ts 0 45
14046fail 8
14047end
14048if gt get tw 0 1000000
14049fail 8
14050end
14051end
14052if and eq get tk 0 1 le get tl 0 8
14053if eq ir_find get tw 0 get tl 0 0
14054e = findname get tw 0
14055if ne e 0
14056if eq get nm_kind sub e 1 9
14057nc = ulong 1
14058if lt get nm_type sub e 1 10
14059if gt get nm_addr sub e 1 1000000
14060fail 8
14061end
14062end
14063end
14064end
14065end
14066end
14067put cur 0 c0
14068put line 0 l0
14069put tka 0 a0
14070put tkb 0 b0
14071if eq nc 1
14072z = ir_lit 0 ty
14073end
14074ir_val 65534 ty
14075if ne get ir_vt 0 ty
14076fail 7
14077end
14078kr = get ir_rr 0
14079bn = ir_bound
14080if and eq bn 0 eq nc 0
14081bn = ulong 1000000
14082end
14083ir_forg w ty kr bn
14084end
14085
14086note ir_forg: the for loop over counter ctr = 0 below kr. N = 0: the plain loop, head: lt ctr kr -> body | exit.
14087note Else the guarded loop, the C oracle's shape (omegese.c guarded_loop): a ulong counter cnt = 0 as well; head:
14088note lt ctr kr -> guard | exit; guard: ge cnt N -> trap 4 | body; body starts with cnt = add cnt 1. Literals and
14089note blocks are made in the oracle's order (literals are shared). The latch adds 1 to ctr (ir_end kind 3). The
14090note for frame (kind 3: head h, exit ex, counter ctr): the counter, bound last, leaves scope at the end.
14091note The literals 0 and 1 are made first (shared; ir_lit returns the same register again), then the counters.
14092note om4 holds 16 registers per function, so the guard blocks reuse names already dead: ty is the guard block
14093note and w the trap block (both after ir_bind); cnt is 0 only in the plain loop (two literals precede it).
14094fn ir_forg w ulong ty ulong kr ulong bn ulong to void
14095cnt = ulong 0
14096if ne bn 0
14097cnt = ir_lit 0 5
14098cnt = ir_lit 1 5
14099cnt = ir_newreg 5
14100end
14101ctr = ir_lit 0 ty
14102ctr = ir_lit 1 ty
14103ctr = ir_newreg ty
14104ir_op 2 ctr ir_lit 0 ty 0 0
14105ir_bind w ctr
14106h = ir_block
14107if ne bn 0
14108ir_op 2 cnt ir_lit 0 5 0 0
14109ty = ir_block
14110w = ir_block
14111end
14112body = ir_block
14113ex = ir_block
14114ir_op 13 h 0 0 0
14115put ir_dead 0 1
14116ir_start h
14117c = ir_newreg 1
14118ir_op 5 3 c ctr kr
14119if eq bn 0
14120ir_op 14 c body ex 0
14121else
14122bn = ir_lit bn 5
14123ir_op 14 c ty ex 0
14124ir_start ty
14125c = ir_newreg 1
14126ir_op 5 6 c cnt bn
14127ir_op 14 c w body 0
14128ir_start w
14129ir_op 12 4 0 0 0
14130ir_op 13 ex 0 0 0
14131put ir_dead 0 1
14132end
14133ir_start body
14134if ne cnt 0
14135ir_op 3 1 cnt cnt ir_lit 1 5
14136end
14137ir_push 3 h ex ctr 0
14138kr = sub get ir_nf 0 1
14139put ir_fnn kr sub get ir_nn 0 1
14140end
14141
14142note ir_stmtcall: a void call as a statement; f is the table index + 1.
14143fn ir_stmtcall f ulong to void
14144j = sub f 1
14145if ne get irf_ret j 76
14146fail 7
14147end
14148np = get irf_np j
14149i = ulong 0
14150while lt i np
14151ty = ulong get irf_pt add mul j 6 i
14152ir_val 65534 ty
14153if ne get ir_vt 0 ty
14154fail 7
14155end
14156put ir_args i get ir_rr 0
14157i = add i 1
14158end
14159ir_callop 65535 j np
14160eol
14161end
14162
14163note ir_assign: name [=] value for a local in scope, a void call, or a new typed local (a new name that is
14164note a keyword, constant, local or function is refused; a bare number has no type, error 11).
14165fn ir_assign w ulong to void
14166p = get ts 0
14167l = get tl 0
14168c0 = get cur 0
14169tok
14170eqs = false
14171if and eq get tk 0 1 eq ir_kwt 73
14172eqs = true
14173else
14174put cur 0 c0
14175if eq get tk 0 0
14176put line 0 sub get line 0 1
14177end
14178end
14179e = ir_find w l
14180if ne e 0
14181r = ulong get ir_nrr sub e 1
14182ir_val r ulong get ir_rty r
14183if ne get ir_vt 0 ulong get ir_rty r
14184fail 7
14185end
14186eol
14187ret
14188end
14189put ir_cl 0 l
14190put ir_cs 0 p
14191f = ir_findfn w
14192if ne f 0
14193if eqs
14194fail 7
14195end
14196ir_stmtcall f
14197ret
14198end
14199if le l 8
14200if ne findname w 0
14201fail 7
14202end
14203end
14204ir_newloc w p l
14205ir_val 65535 0
14206if ne get ir_isb 0 0
14207fail 11
14208end
14209eol
14210ir_bind w get ir_rr 0
14211end
14212
14213note ir_loopexit: innermost loop block's exit + 1, or 0 outside a loop.
14214fn ir_loopexit to ulong
14215i = get ir_nf 0
14216while gt i 0
14217i = sub i 1
14218k = ulong get ir_fk i
14219if or eq k 2 eq k 3
14220ret add get ir_fb i 1
14221end
14222end
14223ret 0
14224end
14225
14226note ir_stmt: one line. The end of the input inside a function is error 15; a line starting with a number
14227note is 16; a note is skipped even after the path ended; a statement IR v1 does not lower is 9.
14228fn ir_stmt to ulong
14229tok
14230if eq get tk 0 0
14231ret 0
14232end
14233if eq get tk 0 3
14234fail 15
14235end
14236if ne get tk 0 1
14237fail 16
14238end
14239c = ir_kwt
14240if eq c 83
14241skipnl
14242ret 0
14243end
14244if and ne c 66 ne c 68
14245if ne get ir_dead 0 0
14246fail 17
14247end
14248end
14249if eq c 77
14250fail 9
14251end
14252if eq c 87
14253note ir_trap: OMEGESE section 4. A trap ends the path but the IR still
14254note needs an unreachable ret terminator (zero of the return type, or void).
14255note trap is packed little endian 1885434484; independently checked in Wolfram.
14256code = lenval 4
14257if or lt code 1 gt code 14
14258fail 8
14259end
14260tok
14261if not or eq get tk 0 0 eq get tk 0 3
14262fail 4
14263end
14264ir_op 12 code 0 0 0
14265z = ulong 65535
14266if ne get ir_ret 0 76
14267z = ir_lit 0 get ir_ret 0
14268end
14269ir_op 15 z 0 0 0
14270put ir_dead 0 1
14271ret 0
14272end
14273if eq c 86
14274note ir_break: BR to the innermost loop's exit, then the path is dead. The if/loop closes skip the
14275note unreachable join exactly as they do after ret or trap.
14276ex = ir_loopexit
14277if eq ex 0
14278fail 9
14279end
14280ir_op 13 sub ex 1 0 0 0
14281put ir_dead 0 1
14282eol
14283ret 0
14284end
14285if eq c 66
14286ir_end
14287ret 0
14288end
14289if eq c 68
14290n = sub get ir_nf 0 1
14291if or ne get ir_fk n 1 ne get ir_fc n 0
14292fail 15
14293end
14294eol
14295put ir_nn 0 get ir_fnn n
14296if eq get ir_dead 0 0
14297j = ir_block
14298ir_op 13 j 0 0 0
14299put ir_dead 0 1
14300put ir_fd n j
14301else
14302put ir_fd n 65535
14303end
14304put ir_fc n 1
14305ir_start get ir_fb n
14306ret 0
14307end
14308if eq c 67
14309ir_val 65534 1
14310if ne get ir_vt 0 1
14311fail 7
14312end
14313eol
14314th = ir_block
14315el = ir_block
14316ir_op 14 get ir_rr 0 th el 0
14317ir_push 1 th el 0 65535
14318ir_start th
14319ret 0
14320end
14321if eq c 69
14322ir_while
14323ret 0
14324end
14325if eq c 71
14326ir_for
14327ret 0
14328end
14329if eq c 72
14330note ir_doret: ret alone in a void function, ret value otherwise (4 when the value is missing, 5 for a
14331note value or any other word after it); the value is read from just after ret.
14332c0 = get cur 0
14333tok
14334if or eq get tk 0 0 eq get tk 0 3
14335if ne get ir_ret 0 76
14336fail 4
14337end
14338ir_op 15 65535 0 0 0
14339else
14340if eq get ir_ret 0 76
14341fail 5
14342end
14343put cur 0 c0
14344ir_val 65534 get ir_ret 0
14345if ne get ir_vt 0 get ir_ret 0
14346fail 7
14347end
14348eol
14349ir_op 15 get ir_rr 0 0 0 0
14350end
14351put ir_dead 0 1
14352ret 0
14353end
14354if eq c 64
14355fail 15
14356end
14357if ne c 0
14358if eq c 97
14359note continue keeps its documented IR refusal, error 3 (gate-ir.sh continue_use)
14360fail 3
14361end
14362if or eq c 80 eq c 82
14363fail 9
14364end
14365if or or or eq c 78 eq c 79 eq c 81 eq c 85
14366fail 9
14367end
14368if eq c 193
14369fail 9
14370end
14371fail 16
14372end
14373note a contract clause (requires, ensures, invariant not followed by =) is not an IR v1 statement: refuse it, never
14374note read it as a local (#791). `ensures = v` assigns a local of that name, as the C oracle and native mode accept.
14375if ne cp_kind 0
14376p = get ts 0
14377l = get line 0
14378tok
14379eqs = and eq get tk 0 1 eq ir_kwt 73
14380put cur 0 p
14381put line 0 l
14382tok
14383if not eqs
14384fail 30
14385end
14386end
14387ir_assign get tw 0
14388ret 0
14389end
14390
14391note ir_param: parameter np of function f, named by the current word: a new name and an IR v1 type.
14392fn ir_param f ulong to void
14393w = get tw 0
14394s = get ts 0
14395l = get tl 0
14396ir_newloc w s l
14397np = get irf_np f
14398if ge np 6
14399fail 9
14400end
14401ty = ir_type 0
14402put irf_pt add mul f 6 np uchar ty
14403put irf_np f add np 1
14404r = ir_newreg ty
14405ir_bind w r
14406end
14407
14408note ir_fn: fn name (param type)* to type | void, then the body. The name is checked (16, 13 over 63
14409note bytes, 10 when in use) and entered before the parameters, so a parameter cannot reuse it.
14410fn ir_fn to void
14411tok
14412if ne get tk 0 1
14413fail 16
14414end
14415if gt get tl 0 63
14416fail 13
14417end
14418fcur = get irf_n 0
14419if ge fcur 32
14420fail 14
14421end
14422put ir_nn 0 0
14423ir_newname get tw 0 get ts 0 get tl 0
14424put irf_nm fcur get tw 0
14425put irf_ns fcur get ts 0
14426put irf_nl fcur get tl 0
14427put irf_np fcur 0
14428put irf_n 0 add fcur 1
14429put ir_ns 0 get ts 0
14430put ir_nl 0 get tl 0
14431put ir_nreg 0 0
14432put ir_nc 0 0
14433put ir_nb 0 0
14434put ir_nord 0 0
14435put ir_no 0 0
14436put ir_nf 0 0
14437put ir_dead 0 0
14438go = true
14439while go
14440tok
14441if or eq get tk 0 0 eq get tk 0 3
14442fail 4
14443end
14444if ne get tk 0 1
14445fail 16
14446end
14447if eq ir_kwt 65
14448go = false
14449else
14450ir_param fcur
14451end
14452end
14453put ir_ret 0 ir_type 1
14454eol
14455put irf_ret fcur uchar get ir_ret 0
14456put ir_npar 0 get ir_nreg 0
14457b = ir_block
14458ir_start b
14459ir_push 4 0 0 0 0
14460put ir_have 0 0
14461while eq get ir_have 0 0
14462ignored = ir_stmt
14463end
14464end
14465
14466fn ir_run to void
14467go = true
14468while go
14469tok
14470k = get tk 0
14471if eq k 3
14472go = false
14473else
14474if eq k 0
14475else
14476if ne k 1
14477fail 16
14478end
14479c = kw get tw 0
14480if eq c 64
14481ir_fn
14482ir_emit_record
14483else
14484if eq c 83
14485skipnl
14486else
14487if eq c 152
14488s_const
14489else
14490if or or eq c 85 eq c 78 eq c 79
14491fail 9
14492end
14493fail 16
14494end
14495end
14496end
14497end
14498end
14499end
14500note ir_encode: header (magic, version 1, nfuncs) followed by every collected function record.
14501put oc 0 0
14502emitb 79
14503emitb 83
14504emitb 67
14505emitb 49
14506emitb 73
14507emitb 82
14508emitb 0
14509emitb 1
14510ir_w16 get irnf 0
14511i = ulong 0
14512while lt i get irrlen 0
14513emitb get irrec i
14514i = add i 1
14515end
14516if ne get fmtm 0 0
14517fmtout
14518end
14519n = write 1 out 0 get oc 0
14520exit 0
14521end
14522
14523note ======== executable contract predicates (OM-2, native targets) ========
14524note Token packs below: Wolfram FromDigits[Reverse[ToCharacterCode[word]],256].
14525note Contract words are contextual: OM-1 name meanings and keyword-table bytes do not change.
14526note Explicit native build policy, with syntax and purity checked in both policies.
14527note om2 debug / om2 release and om2 diag debug / om2 diag release; fmt stays canonical.
14528fn cp_mode q ulong to ulong
14529if gt add q 4 get inlen 0
14530ret 0
14531end
14532if ne get inp q 111
14533ret 0
14534end
14535if ne get inp add q 1 109
14536ret 0
14537end
14538if ne get inp add q 2 50
14539ret 0
14540end
14541if ne get inp add q 3 32
14542ret 0
14543end
14544p = add q 4
14545m = ulong 1
14546if le add p 5 get inlen 0
14547if and eq get inp p 100 eq get inp add p 1 105
14548if and eq get inp add p 2 97 eq get inp add p 3 103
14549if eq get inp add p 4 32
14550p = add p 5
14551m = ulong 2
14552end
14553end
14554end
14555end
14556if ne lineis p 28555834798400882 7 0
14557put crel 0 1
14558ret m
14559end
14560if ne lineis p 444351014244 5 0
14561ret m
14562end
14563ret 0
14564end
14565
14566fn cp_kind to ulong
14567if and eq get om2 0 0 eq get irmode 0 0
14568ret 0
14569end
14570if ne get tk 0 1
14571ret 0
14572end
14573if and eq get tl 0 8 eq get tw 0 8315178084276987250
14574ret 1
14575end
14576if and eq get tl 0 7 eq get tw 0 32481164592901733
14577ret 2
14578end
14579if and eq get tl 0 9 eq get tw 0 7953754356895346281
14580if eq get inp add get ts 0 8 116
14581ret 3
14582end
14583end
14584ret 0
14585end
14586
14587note cp_next reads the next prefix clause, ignoring comments and blanks; a nonclause is unread.
14588fn cp_next to ulong
14589p = get cur 0
14590l = get line 0
14591go = true
14592while go
14593tok
14594if eq get tk 0 1
14595if and eq get tl 0 4 eq get tw 0 1702129518
14596skipnl
14597else
14598go = false
14599end
14600else
14601if ne get tk 0 0
14602go = false
14603end
14604end
14605end
14606k = cp_kind
14607if eq k 0
14608put cur 0 p
14609put line 0 l
14610end
14611ret k
14612end
14613
14614note Recursive purity allowlist, before rdval dispatch. No calls, floats, host or mapped operations.
14615fn cp_token to void
14616if eq get tk 0 2
14617ret
14618end
14619if ne get tk 0 1
14620fail 30
14621end
14622if gt get tl 0 8
14623fail 30
14624end
14625c = kw get tw 0
14626if eq c 0
14627ret
14628end
14629if le sub c 1 8
14630ret
14631end
14632if le sub c 33 19
14633ret
14634end
14635if or eq c 55 eq c 56
14636ret
14637end
14638if or or eq c 59 eq c 60 eq c 74
14639ret
14640end
14641fail 30
14642end
14643
14644fn cp_erase to void
14645put oc 0 get csnap 0
14646put nt 0 get csnap 1
14647put nsite 0 get csnap 2
14648put tgoc 0 get csnap 5
14649put lastb 0 get csnap 6
14650end
14651
14652fn cp_eval code ulong erase ulong to void
14653ln = get line 0
14654rule = ulong 0
14655note Pure evaluation changes no names, calls, inline frames or writes.
14656note Snapshot emission effects, including trap records, diagnostic sites and branch targets.
14657put csnap 0 get oc 0
14658put csnap 1 get nt 0
14659put csnap 2 get nsite 0
14660put csnap 3 get nslot 0
14661put csnap 4 get fzone 0
14662put csnap 5 get tgoc 0
14663put csnap 6 get lastb 0
14664put cpure 0 1
14665put fzone 0 1
14666rdval 0 1 255 0
14667if ne get rt 0 1
14668fail 7
14669end
14670eol
14671nextln = get line 0
14672put line 0 ln
14673if and eq get rk 0 3 ne get rv 0 0
14674rule = ulong 1
14675cp_erase
14676else
14677if or ne erase 0 ne get crel 0 0
14678rule = ulong 2
14679cp_erase
14680else
14681if eq get rk 0 3
14682trapc 14 code
14683else
14684if eq get rk 0 4
14685trapc xor get rv 0 1 code
14686else
14687r = matreg get rk 0 get rv 0 get rt 0
14688trapsite or 3019898880 r code
14689end
14690end
14691end
14692end
14693if and eq get om2m 0 1 eq erase 0
14694cp_rec rule
14695end
14696put line 0 nextln
14697put cpure 0 0
14698put cvalid 0 0
14699put fzone 0 get csnap 4
14700put nslot 0 get csnap 3
14701end
14702
14703note omc-check distinguishes a proof from release policy; initial deferred validation is silent.
14704fn cp_rec rule ulong to void
14705sitew 7162245557575183727 8
14706sitew 540090475 4
14707if eq rule 0
14708sitew 1953523051 4
14709else
14710sitew 110386890240613 6
14711end
14712sitew 67505853393952 6
14713siten add get line 0 get lbase 0
14714sitew 67505820627488 6
14715if eq rule 0
14716sitew 28549272340166002 7
14717end
14718if eq rule 1
14719sitew 8389750308618530659 8
14720sitew 435762132013 5
14721end
14722if eq rule 2
14723sitew 28555834798400882 7
14724end
14725sitew 67501558426400 6
14726if eq get ckind 0 1
14727sitew 8315178084276987250 8
14728end
14729if eq get ckind 0 2
14730sitew 32481164592901733 7
14731end
14732if eq get ckind 0 3
14733sitew 7953754356895346281 8
14734siteb 116
14735end
14736siteb 10
14737end
14738
14739note Function prefix predicates bind only globals and parameters visible on entry.
14740fn cp_entry to void
14741lv = get inl 0
14742put cfhas lv 0
14743if eq get om2 0 0
14744ret
14745end
14746put cfpos lv get cur 0
14747put cfline lv get line 0
14748put cflim lv get nn 0
14749put clim 0 get nn 0
14750k = cp_next
14751n = ulong 0
14752while ne k 0
14753put ckind 0 k
14754if ge n 64
14755fail 14
14756end
14757if eq k 3
14758fail 30
14759end
14760put crkind 0 0
14761if eq k 1
14762cp_eval 9 0
14763else
14764put cfhas lv 1
14765ns = get nslot 0
14766put crtype 0 get curret 0
14767if ne get curret 0 12
14768put crkind 0 1
14769put crval 0 alloc
14770end
14771put cvalid 0 1
14772cp_eval 10 1
14773put nslot 0 ns
14774end
14775n = add n 1
14776k = cp_next
14777end
14778end
14779
14780note Return expressions are evaluated once, before postconditions, without overwriting argument X0.
14781fn cp_post kind ulong value ulong ty ulong to void
14782lv = get inl 0
14783if eq get cfhas lv 0
14784ret
14785end
14786p = get cur 0
14787l = get line 0
14788put cur 0 get cfpos lv
14789put line 0 get cfline lv
14790put clim 0 get cflim lv
14791put crkind 0 kind
14792put crval 0 value
14793put crtype 0 ty
14794k = cp_next
14795while ne k 0
14796if eq k 2
14797put ckind 0 2
14798cp_eval 10 0
14799else
14800skipnl
14801end
14802k = cp_next
14803end
14804put cur 0 p
14805put line 0 l
14806end
14807
14808note Loop invariants execute at the head, including zero trips and final false condition.
14809note Continue reaches the checked head (for increments first); break bypasses it.
14810fn cp_head pos ulong ln ulong to void
14811if eq get om2 0 0
14812ret
14813end
14814p = get cur 0
14815l = get line 0
14816put cur 0 pos
14817put line 0 ln
14818put clim 0 get nn 0
14819put crkind 0 0
14820k = cp_next
14821n = ulong 0
14822while eq k 3
14823put ckind 0 3
14824if ge n 64
14825fail 14
14826end
14827cp_eval 10 0
14828n = add n 1
14829k = cp_next
14830end
14831put cur 0 p
14832put line 0 l
14833end
14834
14835note Head clauses have already been checked and emitted before the condition, never at body entry.
14836fn cp_skip to void
14837if eq get om2 0 0
14838ret
14839end
14840p = get cur 0
14841l = get line 0
14842k = cp_next
14843while eq k 3
14844skipnl
14845p = get cur 0
14846l = get line 0
14847k = cp_next
14848end
14849put cur 0 p
14850put line 0 l
14851end
14852
14853note main: read all of stdin
14854note (8 MiB at most, error 14; a read error is 1), find the mode, then one
14855note statement per line from output byte 180 (om4 START .. STMT)
14856fn main to int
14857off = ulong 0
14858go = true
14859while go
14860m = ulong 0
14861while and go lt m 999999
14862if ge off len inp
14863fail 14
14864end
14865n = read 0 inp off sub len inp off
14866if eq n 0
14867go = false
14868else
14869if lt n 0
14870fail 1
14871end
14872off = add off ulong n
14873end
14874m = add m 1
14875end
14876end
14877put inlen 0 off
14878xlead
14879chk_lead
14880om2lead
14881if eq get om2m 0 2
14882fmtprep
14883end
14884if ne ir_lead 0
14885put irmode 0 1
14886ir_run
14887end
14888prescan
14889namescan
14890if eq get nexp 0 0
14891put oc 0 180
14892else
14893put oc 0 add 176 shl get nexp 0 2
14894end
14895if ne get xtarget 0 0
14896put oc 0 512
14897if eq get xtarget 0 2
14898if eq get progmode 0 0
14899fail 25
14900end
14901note The PE headers fill 520 bytes; code is mapped from file offset 512 at RVA 4096 (xefx 2).
14902put oc 0 520
14903end
14904if eq get progmode 0 0
14905m = get nexp 0
14906if eq m 0
14907m = ulong 1
14908end
14909put oc 0 add 512 mul m 6
14910end
14911end
14912put line 0 1
14913done = false
14914while not done
14915m = ulong 0
14916while and not done lt m 999999
14917if ne stmt 0
14918done = true
14919end
14920m = add m 1
14921end
14922end
14923ret 0
14924end
14925
14926note Linux x86-64 host ABI: Linux v6.17 arch/x86/entry/syscalls/syscall_64.tbl.
14927note RBX anchors the saved slot snapshot; preserve R13 (initial argument stack) in its spare tail.
14928note R12 (nonlocal native return), R14 (runtime dispatch), R15 (stack floor) are never scratch.
14929fn xhbegin to void
14930save
14931xmem 137 13 4 144
14932xrr 137 3 4
14933end
14934fn xha x ulong i ulong r ulong to void
14935q = add add x 8 mul i 2
14936k = get fr q
14937v = get fr add q 1
14938if or eq k 3 eq k 7
14939ximm r v
14940else
14941if eq k 6
14942xaddr r v
14943else
14944xmem 139 r 3 mul v 8
14945end
14946end
14947end
14948fn xhs r ulong s ulong to void
14949xmem 139 r 3 mul s 8
14950end
14951fn xhb r ulong e ulong off ulong to void
14952if ge get nm_kind sub e 1 4
14953xaddr r get nm_addr sub e 1
14954else
14955xhs r ulong get nm_reg sub e 1
14956end
14957xhs 0 off
14958xrr 1 r 0
14959end
14960fn xhcall n ulong to void
14961ximm 0 n
14962emitb 15
14963emitb 5
14964end
14965fn xhdone dst ulong x ulong bytes ulong to void
14966xsp 0 bytes
14967xmem 139 13 3 144
14968hreturn dst x
14969end
14970fn xhcmp r ulong v ulong to void
14971ximm 11 v
14972xrr 57 r 11
14973end
14974fn xhadd r ulong v ulong to void
14975ximm 11 v
14976xrr 1 r 11
14977end
14978fn xhsub r ulong v ulong to void
14979ximm 11 v
14980xrr 41 r 11
14981end
14982fn xhraw r ulong b ulong off ulong size ulong to void
14983emitb uchar add add 64 shl shr r 3 2 shr b 3
14984if lt size 4
14985emitb 15
14986if eq size 1
14987emitb 182
14988else
14989emitb 183
14990end
14991else
14992emitb 139
14993end
14994emitb uchar add add 128 shl and r 7 3 and b 7
14995if eq and b 7 4
14996emitb 36
14997end
14998xword off
14999end
15000fn xhput r ulong b ulong off ulong size ulong to void
15001if eq size 2
15002emitb 102
15003end
15004emitb uchar add add 64 shl shr r 3 2 shr b 3
15005if eq size 1
15006emitb 136
15007else
15008emitb 137
15009end
15010emitb uchar add add 128 shl and r 7 3 and b 7
15011if eq and b 7 4
15012emitb 36
15013end
15014xword off
15015end
15016fn xhpath x ulong to void
15017xhb 6 get fr add x 20 get fr add x 21
15018xhs 2 get fr add x 22
15019xhcmp 2 4095
15020xtrap 8 3
15021xsp 1 4096
15022xrr 137 7 4
15023loop = get oc 0
15024xrr 133 2 2
15025done = xbranch 0
15026xhraw 0 6 0 1
15027xrr 133 0 0
15028xtrap 0 3
15029xhput 0 7 0 1
15030xhadd 6 1
15031xhadd 7 1
15032xhsub 2 1
15033p = xbranch 14
15034xpatch p loop
15035xpatch done get oc 0
15036ximm 0 0
15037xhput 0 7 0 1
15038end
15039fn xhscalar c ulong dst ulong x ulong to void
15040np = hargc c
15041for i ulong np
15042ty = ulong 8
15043if or and eq c 103 eq i 1 and eq c 139 eq i 1
15044ty = ulong 9
15045end
15046harg x i ty
15047end
15048xhbegin
15049for i ulong np
15050r = ulong 7
15051if eq i 1
15052r = ulong 6
15053end
15054if eq i 2
15055r = ulong 2
15056end
15057xha x i r
15058end
15059nr = ulong 292
15060if eq c 100
15061nr = ulong 3
15062end
15063if eq c 101
15064nr = ulong 74
15065end
15066if eq c 102
15067nr = ulong 91
15068end
15069if eq c 103
15070nr = ulong 8
15071end
15072if eq c 104
15073nr = ulong 41
15074end
15075if eq c 105
15076nr = ulong 50
15077end
15078if eq c 106
15079nr = ulong 288
15080xha x 1 10
15081ximm 6 0
15082ximm 2 0
15083end
15084if eq c 107
15085nr = ulong 57
15086end
15087if eq c 127
15088nr = ulong 62
15089end
15090if eq c 129
15091nr = ulong 39
15092end
15093if eq c 139
15094nr = ulong 77
15095end
15096if eq c 145
15097nr = ulong 102
15098end
15099if eq c 146
15100nr = ulong 107
15101end
15102if eq c 147
15103nr = ulong 104
15104end
15105if eq c 148
15106nr = ulong 108
15107end
15108if eq c 153
15109nr = ulong 81
15110end
15111if eq c 154
15112nr = ulong 110
15113end
15114if eq c 155
15115nr = ulong 95
15116end
15117if eq c 156
15118nr = ulong 75
15119end
15120if eq c 158
15121nr = ulong 112
15122end
15123if eq c 159
15124nr = ulong 121
15125end
15126if eq c 160
15127nr = ulong 109
15128end
15129if eq c 163
15130nr = ulong 124
15131end
15132if eq c 164
15133nr = ulong 306
15134end
15135if eq c 165
15136nr = ulong 48
15137end
15138if eq c 197
15139nr = ulong 73
15140end
15141xhcall nr
15142xhdone dst x 0
15143end
15144fn xhpaths c ulong dst ulong x ulong to void
15145e = harray x 0
15146if or eq c 110 eq c 151
15147harg x 2 8
15148harg x 3 8
15149end
15150if or or or eq c 112 eq c 143 eq c 144 eq c 157
15151harg x 2 8
15152end
15153if eq c 150
15154harg x 2 9
15155end
15156xhbegin
15157xhpath x
15158ximm 7 18446744073709551516
15159xrr 137 6 4
15160nr = ulong 257
15161if eq c 110
15162xha x 2 2
15163xha x 3 10
15164end
15165if or eq c 111 eq c 126
15166nr = ulong 263
15167ximm 2 0
15168if eq c 126
15169ximm 2 512
15170end
15171end
15172if eq c 112
15173nr = ulong 258
15174xha x 2 2
15175end
15176if eq c 138
15177nr = ulong 80
15178xrr 137 7 6
15179end
15180if or eq c 143 eq c 144
15181nr = ulong 268
15182if eq c 144
15183nr = ulong 269
15184end
15185xha x 2 2
15186ximm 10 0
15187end
15188if eq c 150
15189nr = ulong 76
15190xrr 137 7 6
15191xha x 2 6
15192end
15193if eq c 151
15194nr = ulong 260
15195xha x 2 2
15196xha x 3 10
15197ximm 8 0
15198end
15199if eq c 157
15200nr = ulong 319
15201xrr 137 7 6
15202xha x 2 6
15203end
15204xhcall nr
15205xhdone dst x 4096
15206end
15207note Native x86 struct stat is private; output remains Omega's 128-byte asm-generic ABI.
15208fn xhstatcv to void
15209xrr 133 0 0
15210bad = xbranch 11
15211ximm 2 0
15212for j ulong 16
15213xmem 137 2 13 mul j 8
15214end
15215for j ulong 2
15216xmem 139 2 4 mul j 8
15217xmem 137 2 13 mul j 8
15218end
15219xhraw 2 4 24 4
15220xhput 2 13 16 4
15221xhraw 2 4 16 4
15222xhput 2 13 20 4
15223xmem 139 2 4 28
15224xmem 137 2 13 24
15225xmem 139 2 4 40
15226xmem 137 2 13 32
15227xmem 139 2 4 48
15228xmem 137 2 13 48
15229xhraw 2 4 56 4
15230xhput 2 13 56 4
15231for j ulong 7
15232xmem 139 2 4 add 64 mul j 8
15233xmem 137 2 13 add 64 mul j 8
15234end
15235ximm 0 0
15236xpatch bad get oc 0
15237end
15238fn xhbuffer c ulong dst ulong x ulong to void
15239harg x 4 8
15240wr = ulong 0
15241if or or or or or eq c 113 eq c 116 eq c 117 eq c 118 eq c 131 eq c 161
15242wr = ulong 1
15243end
15244if eq c 224
15245wr = ulong 1
15246end
15247e = harray x wr
15248if or or eq c 116 eq c 118 eq c 119
15249harg x 2 8
15250end
15251if or eq c 161 eq c 162
15252harg x 3 9
15253end
15254xhbegin
15255xha x 4 7
15256xhb 6 e get fr add x 21
15257xhs 2 get fr add x 22
15258nr = ulong 217
15259if or eq c 114 eq c 115
15260nr = ulong 42
15261if eq c 115
15262nr = ulong 49
15263end
15264end
15265if eq c 116
15266xhcmp 2 4
15267xtrap 3 3
15268nr = ulong 61
15269xha x 2 2
15270ximm 10 0
15271end
15272if eq c 117
15273xhcmp 2 128
15274xtrap 3 3
15275xrr 137 13 6
15276xsp 1 144
15277xrr 137 6 4
15278xhcall 5
15279xhstatcv
15280xhdone dst x 144
15281ret
15282end
15283if or eq c 118 eq c 119
15284nr = ulong 45
15285xha x 2 10
15286ximm 8 0
15287ximm 9 0
15288if eq c 119
15289nr = ulong 44
15290ximm 0 16384
15291xrr 9 10 0
15292end
15293end
15294if or eq c 131 eq c 224
15295note peername shares the private 32-bit socklen; x86-64 getsockname 51, getpeername 52.
15296xsp 1 16
15297xhput 2 4 0 4
15298xrr 137 2 4
15299if eq c 224
15300xhcall 52
15301else
15302xhcall 51
15303end
15304xrr 133 0 0
15305bad = xbranch 11
15306xhraw 0 4 0 4
15307xpatch bad get oc 0
15308xhdone dst x 16
15309ret
15310end
15311if or eq c 161 eq c 162
15312nr = ulong 17
15313if eq c 162
15314nr = ulong 18
15315end
15316xha x 3 10
15317end
15318xhcall nr
15319xhdone dst x 0
15320end
15321fn xhstats c ulong dst ulong x ulong to void
15322e = harray x 0
15323put fr add x 23 e
15324put fr add x 24 get fr add x 21
15325put fr add x 25 get fr add x 22
15326e = harray x 1
15327put fr add x 26 e
15328put fr add x 27 get fr add x 21
15329put fr add x 28 get fr add x 22
15330put fr add x 20 get fr add x 23
15331put fr add x 21 get fr add x 24
15332put fr add x 22 get fr add x 25
15333xhbegin
15334xhs 2 get fr add x 22
15335xrr 133 2 2
15336xtrap 0 3
15337xhpath x
15338xhb 13 e get fr add x 27
15339xhs 2 get fr add x 28
15340xhcmp 2 128
15341xtrap 3 3
15342ximm 7 18446744073709551516
15343xrr 137 6 4
15344xsp 1 144
15345xrr 137 2 4
15346ximm 10 0
15347if eq c 149
15348ximm 10 256
15349end
15350xhcall 262
15351xhstatcv
15352xhdone dst x 4240
15353end
15354fn xhtime c ulong dst ulong x ulong to void
15355if eq c 120
15356harg x 0 9
15357end
15358xhbegin
15359xsp 1 16
15360if eq c 120
15361xha x 0 0
15362xrr 133 0 0
15363xtrap 11 3
15364ximm 1 1000000000
15365ximm 2 0
15366xb 72 247 241
15367xmem 137 0 4 0
15368xmem 137 2 4 8
15369xrr 137 7 4
15370ximm 6 0
15371xhcall 35
15372else
15373ximm 7 1
15374if eq c 130
15375ximm 7 0
15376end
15377xrr 137 6 4
15378xhcall 228
15379xrr 133 0 0
15380bad = xbranch 11
15381xmem 139 2 4 0
15382xhcmp 2 9223372036
15383xtrap 8 1
15384ximm 0 1000000000
15385xb 72 15 175
15386emitb 194
15387xmem 139 2 4 8
15388xrr 1 0 2
15389xtrap 6 1
15390xpatch bad get oc 0
15391end
15392xhdone dst x 16
15393end
15394
15395fn xrot ty ulong ra ulong b ulong imm ulong rd ulong to void
15396xload 0 ra
15397if ne imm 0
15398ximm 1 b
15399else
15400xload 1 b
15401end
15402if eq ty 3
15403emitb 102
15404end
15405if eq ty 5
15406emitb 72
15407end
15408if eq ty 2
15409emitb 210
15410else
15411emitb 211
15412end
15413emitb 200
15414xstore 0 rd
15415canon rd ty
15416end
15417
15418fn xhts off ulong to void
15419xrr 133 2 2
15420forever = xbranch 11
15421xrr 137 0 2
15422ximm 1 1000000000
15423ximm 2 0
15424xb 72 247 241
15425xmem 137 0 4 off
15426xmem 137 2 4 add off 8
15427xmem 141 2 4 off
15428jump = xbranch 14
15429xpatch forever get oc 0
15430ximm 2 0
15431xpatch jump get oc 0
15432end
15433fn xhpoll c ulong dst ulong x ulong to void
15434if eq c 123
15435harg x 0 8
15436harg x 1 8
15437harg x 2 9
15438xhbegin
15439xha x 0 7
15440xha x 1 6
15441xha x 2 2
15442xrr 133 6 6
15443xtrap 11 3
15444xhcmp 6 65535
15445xtrap 8 3
15446xsp 1 32
15447ximm 0 0
15448xmem 137 0 4 0
15449xhput 7 4 0 4
15450xhput 6 4 4 2
15451xhts 16
15452xrr 137 7 4
15453ximm 6 1
15454else
15455e = harray x 1
15456harg x 2 9
15457xhbegin
15458xhb 7 e get fr add x 21
15459xhs 6 get fr add x 22
15460xhcmp 6 8192
15461xtrap 8 3
15462xrr 137 0 6
15463ximm 1 7
15464xrr 33 0 1
15465xrr 133 0 0
15466xtrap 1 3
15467xb 72 193 238
15468emitb 3
15469xha x 2 2
15470xsp 1 32
15471xhts 16
15472end
15473ximm 10 0
15474ximm 8 8
15475xhcall 271
15476if eq c 123
15477xrr 133 0 0
15478bad = xbranch 13
15479xhraw 0 4 6 2
15480xpatch bad get oc 0
15481end
15482xhdone dst x 32
15483end
15484fn xhargs c ulong dst ulong x ulong to void
15485put hostargs 0 1
15486if or eq c 121 eq c 136
15487xhbegin
15488xmem 139 0 13 0
15489if eq c 136
15490xmem 141 6 13 16
15491xb 72 193 224
15492emitb 3
15493xrr 1 6 0
15494ximm 0 0
15495loop = get oc 0
15496xmem 139 2 6 0
15497xrr 133 2 2
15498done = xbranch 0
15499xhadd 6 8
15500xhadd 0 1
15501p = xbranch 14
15502xpatch p loop
15503xpatch done get oc 0
15504end
15505xhdone dst x 0
15506ret
15507end
15508harg x 4 5
15509e = harray x 1
15510xhbegin
15511xha x 4 2
15512xmem 139 1 13 0
15513xmem 141 6 13 8
15514if eq c 137
15515xhadd 1 1
15516xb 72 193 225
15517emitb 3
15518xrr 1 6 1
15519loop = get oc 0
15520xmem 139 0 6 0
15521xrr 133 0 0
15522xtrap 0 3
15523xrr 133 2 2
15524done = xbranch 0
15525xhsub 2 1
15526xhadd 6 8
15527p = xbranch 14
15528xpatch p loop
15529xpatch done get oc 0
15530else
15531xrr 57 2 1
15532xtrap 2 3
15533xb 72 193 226
15534emitb 3
15535xrr 1 6 2
15536end
15537xmem 139 6 6 0
15538xrr 137 13 6
15539ximm 2 0
15540scan = get oc 0
15541xhraw 0 6 0 1
15542xrr 133 0 0
15543found = xbranch 0
15544xhadd 6 1
15545xhadd 2 1
15546xhcmp 2 1048576
15547xtrap 2 3
15548p = xbranch 14
15549xpatch p scan
15550xpatch found get oc 0
15551xhs 1 get fr add x 22
15552xrr 57 2 1
15553small = xbranch 8
15554xhb 7 e get fr add x 21
15555xrr 137 6 13
15556xrr 137 1 2
15557emitb 243
15558emitb 164
15559xrr 137 0 2
15560tail = xbranch 14
15561xpatch small get oc 0
15562ximm 0 18446744073709551511
15563xpatch tail get oc 0
15564xhdone dst x 0
15565end
15566fn xhsimple c ulong dst ulong x ulong to void
15567e = harray x 1
15568if or eq c 128 eq c 135
15569harg x 2 8
15570end
15571xhbegin
15572xhb 7 e get fr add x 21
15573xhs 6 get fr add x 22
15574nr = ulong 79
15575if eq c 128
15576xhcmp 6 8
15577xtrap 3 3
15578xha x 2 6
15579nr = ulong 293
15580end
15581if eq c 135
15582xha x 2 2
15583nr = ulong 318
15584end
15585xhcall nr
15586xhdone dst x 0
15587end
15588fn xhreadln dst ulong x ulong to void
15589e = harray x 0
15590put fr add x 23 e
15591put fr add x 24 get fr add x 21
15592put fr add x 25 get fr add x 22
15593e = harray x 1
15594put fr add x 26 e
15595put fr add x 27 get fr add x 21
15596put fr add x 28 get fr add x 22
15597put fr add x 20 get fr add x 23
15598put fr add x 21 get fr add x 24
15599put fr add x 22 get fr add x 25
15600xhbegin
15601xhs 2 get fr add x 22
15602xrr 133 2 2
15603xtrap 0 3
15604xhpath x
15605xhb 2 e get fr add x 27
15606xhs 10 get fr add x 28
15607ximm 7 18446744073709551516
15608xrr 137 6 4
15609xhcall 267
15610xhdone dst x 4096
15611end
15612fn xhsock c ulong dst ulong x ulong to void
15613ok = new ulong 6 0
15614if eq c 134
15615harg x 2 8
15616harg x 3 8
15617harg x 4 8
15618e = harray x 0
15619xhbegin
15620xha x 2 7
15621xha x 3 6
15622xha x 4 2
15623xhb 10 e get fr add x 21
15624xhs 8 get fr add x 22
15625xhcmp 6 1
15626tcp = xbranch 1
15627for j ulong 5
15628v = add j 5
15629if eq j 0
15630v = ulong 2
15631end
15632if eq j 1
15633v = ulong 15
15634end
15635xhcmp 2 v
15636put ok j xbranch 0
15637end
15638badso = xbranch 14
15639xpatch tcp get oc 0
15640xhcmp 6 6
15641badtcp = xbranch 1
15642xhcmp 2 1
15643put ok 5 xbranch 0
15644xpatch badso get oc 0
15645xpatch badtcp get oc 0
15646ximm 0 sub ulong 0 92
15647done = xbranch 14
15648for j ulong 6
15649xpatch get ok j get oc 0
15650end
15651xhcmp 8 4
15652sizeok = xbranch 0
15653ximm 0 sub ulong 0 22
15654badsize = xbranch 14
15655xpatch sizeok get oc 0
15656xhcall 54
15657xpatch done get oc 0
15658xpatch badsize get oc 0
15659xhdone dst x 0
15660ret
15661end
15662harg x 4 8
15663if eq c 220
15664e = harray x 1
15665xhbegin
15666xha x 4 7
15667xhb 10 e get fr add x 21
15668xhs 8 get fr add x 22
15669xhcmp 8 12
15670xtrap 3 3
15671ximm 6 1
15672ximm 2 17
15673xsp 1 16
15674ximm 8 12
15675xhput 8 4 0 4
15676xrr 137 8 4
15677xhcall 55
15678xhdone dst x 16
15679ret
15680end
15681e = harray x 0
15682harg x 2 8
15683xhbegin
15684xha x 4 7
15685xha x 2 2
15686ximm 0 16384
15687xrr 9 2 0
15688xhb 6 e get fr add x 21
15689xhs 8 get fr add x 22
15690xsp 1 80
15691ximm 0 0
15692for j ulong 10
15693xmem 137 0 4 mul j 8
15694end
15695xmem 137 6 4 64
15696xmem 137 8 4 72
15697xmem 141 6 4 64
15698xmem 137 6 4 16
15699ximm 6 1
15700xmem 137 6 4 24
15701xrr 137 6 4
15702xhcall 46
15703xhdone dst x 80
15704end
15705note execenv 233 on x86-64: as on AArch64, a second pass decodes the environment packet (every record
15706note nonempty) into env strings at RSP+8192 and env pointers at RSP+12288; both checks precede close_range.
15707fn xhpack c ulong dst ulong x ulong to void
15708e = harray x 0
15709if eq c 125
15710harg x 2 8
15711end
15712if eq c 233
15713put fr add x 23 e
15714put fr add x 24 get fr add x 21
15715put fr add x 25 get fr add x 22
15716e = harray x 0
15717put fr add x 26 e
15718put fr add x 27 get fr add x 21
15719put fr add x 28 get fr add x 22
15720put fr add x 20 get fr add x 23
15721put fr add x 21 get fr add x 24
15722put fr add x 22 get fr add x 25
15723e = get fr add x 23
15724end
15725xhbegin
15726note stack is 8192 bytes per packet; the pass bound is stack >> 13 (two passes for execenv).
15727stack = ulong 8192
15728if eq c 233
15729stack = ulong 16384
15730end
15731pass = ulong 0
15732while lt pass shr stack 13
15733if eq pass 0
15734xhb 6 e get fr add x 21
15735xhs 2 get fr add x 22
15736else
15737note The slot snapshot is RBX-relative, so the environment slice is read after the RSP move.
15738xhb 6 get fr add x 26 get fr add x 27
15739xhs 2 get fr add x 28
15740end
15741xhcmp 2 4096
15742xtrap 8 3
15743xhcmp 2 2
15744xtrap 3 3
15745xhraw 8 6 0 2
15746xhadd 6 2
15747xhsub 2 2
15748xrr 133 8 8
15749xtrap 0 3
15750xhcmp 8 64
15751xtrap 8 3
15752if or or eq c 125 eq c 141 eq c 142
15753xhcmp 8 2
15754xtrap 1 3
15755end
15756if eq pass 0
15757xsp 1 stack
15758xrr 137 10 4
15759xmem 141 13 4 4096
15760else
15761xmem 141 10 4 8192
15762xmem 141 13 4 12288
15763end
15764ximm 9 0
15765outer = get oc 0
15766xrr 57 9 8
15767done = xbranch 2
15768xhcmp 2 2
15769xtrap 3 3
15770xhraw 1 6 0 2
15771xhadd 6 2
15772xhsub 2 2
15773xrr 57 1 2
15774xtrap 8 3
15775if and eq pass 0 or eq c 124 eq c 233
15776xrr 133 9 9
15777notfirst = xbranch 1
15778xrr 133 1 1
15779xtrap 0 3
15780xpatch notfirst get oc 0
15781else
15782xrr 133 1 1
15783xtrap 0 3
15784end
15785xmem 137 10 13 0
15786xhadd 13 8
15787xrr 41 2 1
15788inner = get oc 0
15789xrr 133 1 1
15790endstr = xbranch 0
15791xhraw 0 6 0 1
15792xrr 133 0 0
15793xtrap 0 3
15794xhput 0 10 0 1
15795xhadd 6 1
15796xhadd 10 1
15797xhsub 1 1
15798p = xbranch 14
15799xpatch p inner
15800xpatch endstr get oc 0
15801ximm 0 0
15802xhput 0 10 0 1
15803xhadd 10 1
15804xhadd 9 1
15805p = xbranch 14
15806xpatch p outer
15807xpatch done get oc 0
15808xrr 133 2 2
15809xtrap 1 3
15810ximm 0 0
15811xmem 137 0 13 0
15812pass = add pass 1
15813end
15814if or eq c 124 eq c 233
15815note As on AArch64: close_range(3, ~0U, CLOSE_RANGE_UNSHARE) first; a nonzero result skips execve
15816note and returns that kernel error. RDI/RSI/RDX are reloaded after the call; R13 holds the argv end.
15817ximm 7 3
15818ximm 6 4294967295
15819ximm 2 2
15820xhcall 436
15821xrr 133 0 0
15822p = xbranch 1
15823xmem 139 7 4 4096
15824xmem 141 6 4 4096
15825if eq c 233
15826xmem 141 2 4 12288
15827else
15828xrr 137 2 13
15829end
15830xhcall 59
15831xpatch p get oc 0
15832else
15833xmem 139 6 4 4096
15834xmem 139 10 4 4104
15835ximm 7 18446744073709551516
15836ximm 2 18446744073709551516
15837ximm 8 0
15838if eq c 125
15839xha x 2 8
15840xhcall 316
15841else
15842if eq c 141
15843xhcall 265
15844else
15845xrr 137 7 6
15846ximm 6 18446744073709551516
15847xrr 137 2 10
15848xhcall 266
15849end
15850end
15851end
15852xhdone dst x stack
15853end
15854note Hosted handles use the entry31 context header and 13 slot bits; programs retain raw128 slots.
15855fn xhmcheck to void
15856xrr 137 13 10
15857if eq get progmode 0 0
15858ximm 0 8191
15859else
15860ximm 0 255
15861end
15862xrr 33 13 0
15863xhcmp 13 1
15864xtrap 3 3
15865if eq get progmode 0 0
15866xmem 139 0 14 248
15867xmem 139 0 0 0
15868xhcmp 0 1
15869xtrap 3 3
15870xhcmp 0 4098
15871xtrap 8 3
15872xrr 57 13 0
15873xtrap 8 3
15874else
15875xhcmp 13 128
15876xtrap 8 3
15877end
15878xhsub 13 1
15879xb 73 193 229
15880emitb 5
15881if eq get progmode 0 0
15882xmem 139 0 14 248
15883xhadd 0 32
15884else
15885xaddr 0 get maptab 0
15886end
15887xrr 1 13 0
15888xmem 139 9 13 16
15889xrr 133 9 9
15890xtrap 0 3
15891xb 73 193 234
15892if eq get progmode 0 0
15893emitb 13
15894else
15895emitb 8
15896end
15897xrr 57 10 9
15898xtrap 1 3
15899xmem 139 8 13 0
15900xrr 133 8 8
15901xtrap 0 3
15902xmem 139 9 13 8
15903end
15904fn xhmnew c ulong dst ulong x ulong to void
15905acc = ulong 0
15906mode = ulong 1048610
15907if eq c 167
15908harg x 0 8
15909harg x 1 5
15910harg x 2 5
15911harg x 3 8
15912harg x 4 8
15913if or ne get fr add x 14 3 ne get fr add x 16 3
15914fail 9
15915end
15916acc = get fr add x 15
15917mode = get fr add x 17
15918if and ne acc 1 ne acc 3
15919fail 9
15920end
15921if and and and ne mode 1 ne mode 2 ne mode 33 ne mode 34
15922fail 9
15923end
15924else
15925harg x 1 5
15926harg x 2 5
15927end
15928xhbegin
15929xha x 2 6
15930xrr 133 6 6
15931xtrap 0 3
15932xhcmp 6 68719476736
15933xtrap 8 3
15934if eq c 167
15935xha x 0 8
15936xha x 1 9
15937if ge mode 32
15938xhcmp 8 18446744073709551615
15939xtrap 1 3
15940xrr 133 9 9
15941xtrap 1 3
15942end
15943else
15944xha x 1 7
15945xhcmp 7 65536
15946xtrap 3 3
15947ximm 0 1099511627776
15948xrr 41 0 6
15949xrr 57 7 0
15950xtrap 8 3
15951ximm 0 4095
15952xrr 137 1 7
15953xrr 33 1 0
15954xrr 133 1 1
15955xtrap 1 3
15956xrr 137 1 6
15957xrr 33 1 0
15958xrr 133 1 1
15959xtrap 1 3
15960ximm 8 18446744073709551615
15961ximm 9 0
15962end
15963if eq get progmode 0 0
15964xmem 139 13 14 248
15965xmem 139 0 13 0
15966xhcmp 0 1
15967xtrap 3 3
15968xhcmp 0 4098
15969xtrap 8 3
15970xmem 139 0 13 16
15971xhcmp 0 1
15972xtrap 3 3
15973xhcmp 0 1125899906842623
15974xtrap 8 3
15975xhadd 13 32
15976else
15977xaddr 13 get maptab 0
15978end
15979ximm 2 1
15980head = get oc 0
15981xmem 139 0 13 0
15982xrr 133 0 0
15983used = xbranch 1
15984xmem 139 10 13 16
15985if eq get progmode 0 0
15986xmem 139 11 14 248
15987xmem 139 11 11 16
15988xrr 57 10 11
15989else
15990xhcmp 10 36028797018963967
15991end
15992vacant = xbranch 3
15993xpatch used get oc 0
15994xhadd 13 32
15995xhadd 2 1
15996if eq get progmode 0 0
15997xmem 139 11 14 248
15998xmem 139 11 11 0
15999xrr 57 2 11
16000else
16001xhcmp 2 128
16002end
16003again = xbranch 9
16004xpatch again head
16005ximm 0 sub ulong 0 24
16006full = xbranch 14
16007xpatch vacant get oc 0
16008xsp 1 32
16009xmem 137 2 4 0
16010xmem 137 6 4 8
16011xmem 137 10 4 16
16012if eq c 167
16013ximm 7 0
16014else
16015xha x 1 7
16016end
16017ximm 2 acc
16018ximm 10 mode
16019xhcall 9
16020xrr 133 0 0
16021failed = xbranch 11
16022wrong = ulong 0
16023if eq c 176
16024xha x 1 2
16025xrr 57 0 2
16026exact = xbranch 0
16027xrr 137 7 0
16028xmem 139 6 4 8
16029xhcall 11
16030xrr 133 0 0
16031xtrap 1 3
16032ximm 0 sub ulong 0 95
16033wrong = xbranch 14
16034xpatch exact get oc 0
16035end
16036xmem 137 0 13 0
16037xmem 139 2 4 8
16038xmem 137 2 13 8
16039xmem 139 2 4 16
16040xhadd 2 1
16041xmem 137 2 13 16
16042ximm 0 acc
16043xmem 137 0 13 24
16044xrr 137 0 2
16045xb 72 193 224
16046if eq get progmode 0 0
16047emitb 13
16048else
16049emitb 8
16050end
16051xmem 139 2 4 0
16052xrr 9 0 2
16053xpatch failed get oc 0
16054if ne wrong 0
16055xpatch wrong get oc 0
16056end
16057xsp 0 32
16058xpatch full get oc 0
16059xhdone dst x 0
16060end
16061fn xhmbind dst ulong x ulong to void
16062harg x 0 9
16063harg x 1 8
16064harg x 2 5
16065harg x 3 8
16066if ne get fr add x 14 3
16067fail 9
16068end
16069acc = get fr add x 15
16070if and ne acc 1 ne acc 3
16071fail 9
16072end
16073xhbegin
16074xha x 0 10
16075xhmcheck
16076xmem 139 0 13 24
16077xrr 133 0 0
16078xtrap 1 3
16079xsp 1 32
16080xmem 137 8 4 0
16081xmem 137 9 4 8
16082xrr 137 7 8
16083xrr 137 6 9
16084xha x 1 8
16085xha x 2 9
16086ximm 2 acc
16087ximm 10 17
16088xhcall 9
16089xrr 133 0 0
16090failed = xbranch 11
16091xmem 139 2 4 0
16092xrr 57 0 2
16093xtrap 1 3
16094ximm 0 acc
16095xmem 137 0 13 24
16096ximm 0 0
16097done = xbranch 14
16098xpatch failed get oc 0
16099xmem 137 0 4 16
16100ximm 0 4
16101xmem 137 0 13 24
16102xmem 139 7 4 0
16103xmem 139 6 4 8
16104xhcall 11
16105xrr 133 0 0
16106xtrap 1 3
16107ximm 0 0
16108xmem 137 0 13 0
16109xmem 137 0 13 8
16110xmem 137 0 13 24
16111xmem 139 0 4 16
16112xpatch done get oc 0
16113xhdone dst x 32
16114end
16115fn xhmcopy c ulong dst ulong x ulong to void
16116harg x 4 9
16117harg x 3 5
16118wr = ulong 0
16119if eq c 169
16120wr = ulong 1
16121end
16122e = harray x wr
16123xhbegin
16124xha x 4 10
16125xhmcheck
16126xmem 139 0 13 24
16127xrr 133 0 0
16128xtrap 0 3
16129xhcmp 0 3
16130xtrap 8 3
16131xha x 3 2
16132xrr 57 2 9
16133xtrap 8 3
16134xrr 41 9 2
16135xhs 1 get fr add x 22
16136xrr 57 1 9
16137xtrap 8 3
16138xrr 1 8 2
16139xhb 7 e get fr add x 21
16140xrr 137 6 8
16141if eq c 170
16142xmem 139 0 13 24
16143xhcmp 0 3
16144xtrap 1 3
16145xrr 137 6 7
16146xrr 137 7 8
16147end
16148xrr 137 2 1
16149emitb 243
16150emitb 164
16151xrr 137 0 2
16152xhdone dst x 0
16153end
16154fn xhmmio c ulong dst ulong x ulong to void
16155harg x 0 9
16156if eq c 173
16157xhbegin
16158xha x 0 10
16159xhmcheck
16160xrr 137 0 8
16161xhdone dst x 0
16162put rt 0 5
16163ret
16164end
16165harg x 1 5
16166harg x 2 8
16167if ne get fr add x 12 3
16168fail 9
16169end
16170w = get fr add x 13
16171if and ne w 4 ne w 8
16172fail 9
16173end
16174if eq c 175
16175harg x 3 5
16176end
16177xhbegin
16178xha x 0 10
16179xhmcheck
16180xha x 1 2
16181xmem 139 0 13 24
16182xrr 133 0 0
16183xtrap 0 3
16184xhcmp 0 3
16185xtrap 8 3
16186xrr 57 2 9
16187xtrap 8 3
16188xrr 41 9 2
16189xhcmp 9 w
16190xtrap 3 3
16191xrr 137 0 2
16192ximm 1 sub w 1
16193xrr 33 0 1
16194xrr 133 0 0
16195xtrap 1 3
16196xrr 1 8 2
16197if eq c 175
16198xmem 139 0 13 24
16199xhcmp 0 3
16200xtrap 1 3
16201xha x 3 2
16202if eq w 4
16203xhcmp 2 4294967295
16204xtrap 8 3
16205end
16206xb 15 174 240
16207if eq w 4
16208xhput 2 8 0 4
16209else
16210xmem 137 2 8 0
16211end
16212xb 15 174 240
16213ximm 0 0
16214xhdone dst x 0
16215else
16216xb 15 174 240
16217if eq w 4
16218xhraw 0 8 0 4
16219else
16220xmem 139 0 8 0
16221end
16222xb 15 174 240
16223xhdone dst x 0
16224put rt 0 5
16225end
16226end
16227note One compile-time ioctl allowlist is shared by both native Linux backends.
16228fn xhreq c ulong x ulong to ulong
16229harg x 4 8
16230harg x 2 4
16231if ne get fr add x 12 3
16232fail 9
16233end
16234q = get fr add x 13
16235n = ulong 4
16236if eq c 166
16237if ne q 21531
16238if ne and q 3221290752 3221243392
16239ok = eq q 2154840336
16240ok = or ok eq q 3224387873
16241ok = or ok eq q 3222815010
16242ok = or ok eq q 3225960739
16243ok = or ok eq q 3222815012
16244if not ok
16245fail 9
16246end
16247end
16248n = and shr q 16 16383
16249if eq n 0
16250fail 9
16251end
16252end
16253end
16254if eq c 172
16255n = ulong 0
16256if eq q 805306369
16257n = ulong 16
16258end
16259if eq q 75
16260n = ulong 8
16261end
16262if eq q 37
16263n = ulong 40
16264end
16265if or eq q 38 eq q 26
16266n = ulong 20
16267end
16268if eq q 25
16269n = ulong 32
16270end
16271if eq q 27
16272n = ulong 56
16273end
16274if eq q 28
16275n = ulong 28
16276end
16277if eq q 33
16278n = ulong 9264
16279end
16280if or eq q 34 eq q 73
16281n = ulong 24
16282end
16283if eq n 0
16284fail 9
16285end
16286end
16287if eq c 171
16288if and ne q 3223340586 ne q 3223340587
16289fail 9
16290end
16291n = ulong 32
16292end
16293ret n
16294end
16295fn xhdev c ulong dst ulong x ulong to void
16296n = xhreq c x
16297put fr add x 23 n
16298e = harray x 1
16299if ne c 171
16300xhbegin
16301xhs 2 get fr add x 22
16302xhcmp 2 get fr add x 23
16303xtrap 1 3
16304xha x 4 7
16305xha x 2 6
16306xhb 2 e get fr add x 21
16307xhcall 16
16308xhdone dst x 0
16309ret
16310end
16311put fr add x 23 e
16312put fr add x 24 get fr add x 21
16313put fr add x 25 get fr add x 22
16314f = harray x 1
16315if eq e f
16316fail 19
16317end
16318xhbegin
16319xhs 2 get fr add x 25
16320xhcmp 2 32
16321xtrap 1 3
16322xhs 9 get fr add x 22
16323xhcmp 9 1048576
16324xtrap 8 3
16325xhb 13 e get fr add x 24
16326xhb 8 f get fr add x 21
16327xrr 133 9 9
16328empty = xbranch 0
16329xrr 137 2 8
16330xrr 1 2 9
16331xrr 57 2 13
16332before = xbranch 9
16333xmem 141 2 13 32
16334xrr 57 8 2
16335xtrap 3 3
16336xpatch before get oc 0
16337xpatch empty get oc 0
16338xmem 139 2 13 16
16339xrr 133 2 2
16340xtrap 1 3
16341xrr 133 9 9
16342nonzero = xbranch 1
16343ximm 8 0
16344xpatch nonzero get oc 0
16345xsp 1 32
16346for j ulong 4
16347xmem 139 0 13 mul j 8
16348xmem 137 0 4 mul j 8
16349end
16350xmem 137 8 4 16
16351xhput 9 4 24 4
16352xha x 4 7
16353xha x 2 6
16354xrr 137 2 4
16355xhcall 16
16356for j ulong 4
16357if ne j 2
16358xmem 139 2 4 mul j 8
16359xmem 137 2 13 mul j 8
16360end
16361end
16362xhdone dst x 32
16363end
16364fn xhost c ulong dst ulong to void
16365if ne get xtarget 0 1
16366fail 25
16367end
16368if eq 0 get progmode 0
16369if eq 0 get om2 0
16370fail 9
16371end
16372ok = ulong 0
16373if or or eq c 100 eq c 110 or eq c 117 eq c 140
16374ok = ulong 1
16375end
16376if and le c 149 ge c 145
16377ok = ulong 1
16378end
16379if or or eq c 130 eq c 109 eq c 120
16380ok = ulong 1
16381end
16382if and le c 177 ge c 166
16383ok = ulong 1
16384end
16385if eq ok 0
16386fail 9
16387end
16388end
16389note execpure (198) has no x86 strict-profile lowering; refuse it rather than run an unconfined exec.
16390if eq c 198
16391fail 25
16392end
16393x = mul get depth 0 32
16394put fr add x 7 get nslot 0
16395if or eq c 220 eq c 222
16396xhsock c dst x
16397ret
16398end
16399if or eq c 123 eq c 221
16400xhpoll c dst x
16401ret
16402end
16403if and ge c 167 le c 177
16404if or eq c 171 eq c 172
16405xhdev c dst x
16406ret
16407end
16408mtinit
16409if or eq c 167 eq c 176
16410xhmnew c dst x
16411ret
16412end
16413if eq c 177
16414xhmbind dst x
16415ret
16416end
16417if eq c 168
16418harg x 0 9
16419xhbegin
16420xha x 0 10
16421xhmcheck
16422xrr 137 7 8
16423xrr 137 6 9
16424xhcall 11
16425xrr 133 0 0
16426failed = xbranch 1
16427xmem 137 0 13 0
16428xmem 137 0 13 8
16429xmem 137 0 13 24
16430xpatch failed get oc 0
16431xhdone dst x 0
16432ret
16433end
16434if or eq c 169 eq c 170
16435xhmcopy c dst x
16436ret
16437end
16438xhmmio c dst x
16439ret
16440end
16441if eq c 166
16442xhdev c dst x
16443ret
16444end
16445if or eq c 132 eq c 128
16446xhsimple c dst x
16447ret
16448end
16449if eq c 135
16450xhsimple c dst x
16451ret
16452end
16453if eq c 133
16454xhreadln dst x
16455ret
16456end
16457if eq c 134
16458xhsock c dst x
16459ret
16460end
16461if or or eq c 109 eq c 130 eq c 120
16462xhtime c dst x
16463ret
16464end
16465if or or or and ge c 113 le c 119 eq c 131 or eq c 161 eq c 162 eq c 224
16466xhbuffer c dst x
16467ret
16468end
16469if or or or eq c 121 eq c 122 eq c 136 eq c 137
16470xhargs c dst x
16471ret
16472end
16473if or or or or eq c 124 eq c 125 eq c 141 eq c 142 eq c 233
16474xhpack c dst x
16475ret
16476end
16477if or eq c 140 eq c 149
16478xhstats c dst x
16479ret
16480end
16481if or or or or eq c 110 eq c 111 eq c 112 eq c 126 eq c 138
16482xhpaths c dst x
16483ret
16484end
16485if or or or or eq c 143 eq c 144 eq c 150 eq c 151 eq c 157
16486xhpaths c dst x
16487ret
16488end
16489xhscalar c dst x
16490end
16491
16492note ======== Native x86-64 semantic backend ========
16493note Intel SDM volume 2 instruction entries MOV, LEA, CALL, RET, Jcc, SETcc, MOVSX,
16494note MOVZX, ADD, SUB, IMUL, DIV, IDIV, SHL, SHR, SAR, SYSCALL; volume 2 chapter 2
16495note supplies REX, ModRM and SIB. No A64 word decoding occurs in this backend.
16496fn xb a ulong b ulong c ulong to void
16497emitb uchar a
16498emitb uchar b
16499emitb uchar c
16500end
16501
16502fn xword v ulong to void
16503emitb uchar and v 255
16504emitb uchar and shr v 8 255
16505emitb uchar and shr v 16 255
16506emitb uchar and shr v 24 255
16507end
16508
16509fn ximm r ulong v ulong to void
16510emitb uchar add 72 shr r 3
16511emitb uchar add 184 and r 7
16512xword v
16513xword shr v 32
16514end
16515
16516note Hardware registers are private emitter operands. Language values are RBP slots.
16517fn xrr op ulong d ulong s ulong to void
16518emitb uchar add add 72 shl shr s 3 2 shr d 3
16519emitb uchar op
16520emitb uchar add add 192 shl and s 7 3 and d 7
16521end
16522
16523fn xmem op ulong r ulong b ulong off ulong to void
16524emitb uchar add add 72 shl shr r 3 2 shr b 3
16525emitb uchar op
16526emitb uchar add add 128 shl and r 7 3 and b 7
16527if eq and b 7 4
16528emitb 36
16529end
16530xword off
16531end
16532
16533fn xload r ulong s ulong to void
16534xmem 139 r 5 sub ulong 0 mul add s 1 8
16535end
16536
16537fn xstore r ulong s ulong to void
16538xmem 137 r 5 sub ulong 0 mul add s 1 8
16539end
16540
16541fn xcc cc ulong to ulong
16542if eq cc 0
16543ret 4
16544end
16545if eq cc 1
16546ret 5
16547end
16548if eq cc 2
16549ret 3
16550end
16551if eq cc 3
16552ret 2
16553end
16554if eq cc 6
16555ret 0
16556end
16557if eq cc 7
16558ret 1
16559end
16560if eq cc 8
16561ret 7
16562end
16563if eq cc 9
16564ret 6
16565end
16566if eq cc 10
16567ret 13
16568end
16569if eq cc 11
16570ret 12
16571end
16572if eq cc 12
16573ret 15
16574end
16575if eq cc 13
16576ret 14
16577end
16578fail 25
16579ret 0
16580end
16581
16582note Every branch reserves six bytes; all targets are byte offsets, not word offsets.
16583fn xbranch cc ulong to ulong
16584p = get oc 0
16585if eq cc 14
16586emitb 233
16587xword 0
16588emitb 144
16589else
16590emitb 15
16591emitb uchar add 128 xcc cc
16592xword 0
16593end
16594ret p
16595end
16596
16597fn xpatch p ulong dest ulong to void
16598if eq get out p 233
16599wrword add p 1 sub dest add p 5
16600else
16601wrword add p 2 sub dest add p 6
16602end
16603end
16604
16605fn xtrap cc ulong code ulong to void
16606n = get nt 0
16607if ge n 8192
16608fail 14
16609end
16610put tcode n code
16611put tsite n xbranch cc
16612put nt 0 add n 1
16613if eq get om2m 0 1
16614siterec
16615end
16616end
16617
16618fn xcmp a ulong b ulong imm ulong to void
16619xload 0 a
16620if ne imm 0
16621ximm 1 b
16622else
16623xload 1 b
16624end
16625xrr 57 0 1
16626end
16627
16628note Canonicalization uses shifts, preserving the checked language's signed widths.
16629fn xcanrax t ulong to void
16630if eq t 10
16631emitb 137
16632emitb 192
16633ret
16634end
16635w = twid t
16636if eq w 64
16637ret
16638end
16639xb 72 193 224
16640emitb uchar sub 64 w
16641xb 72 193 232
16642if ge t 6
16643put out sub get oc 0 1 248
16644end
16645emitb uchar sub 64 w
16646end
16647
16648fn xext r ulong t ulong code ulong to void
16649xload 0 r
16650xrr 137 2 0
16651xcanrax t
16652xrr 57 0 2
16653xtrap 1 code
16654end
16655
16656fn xarith op ulong ty ulong a ulong b ulong imm ulong d ulong to void
16657if or eq ty 10 eq ty 11
16658if or lt op 33 gt op 35
16659fail 7
16660end
16661if ne imm 0
16662matconst 16 b
16663b = ulong 16
16664end
16665xfbin op ty a b d
16666ret
16667end
16668xload 0 a
16669if ne imm 0
16670ximm 1 b
16671else
16672xload 1 b
16673end
16674if eq op 33
16675xrr 1 0 1
16676if eq ty 13
16677xtrap 3 1
16678end
16679end
16680if eq op 34
16681xrr 41 0 1
16682if eq ty 13
16683xtrap 3 1
16684end
16685end
16686if eq op 35
16687if eq ty 13
16688xb 72 247 225
16689xrr 133 2 2
16690xtrap 1 1
16691else
16692xb 72 15 175
16693emitb 193
16694end
16695end
16696if eq op 38
16697xrr 33 0 1
16698end
16699if eq op 39
16700xrr 9 0 1
16701end
16702if eq op 40
16703xrr 49 0 1
16704end
16705if and eq ty 9 le op 35
16706xtrap 6 1
16707end
16708xstore 0 d
16709if le op 35
16710bo_fix d ty
16711end
16712end
16713
16714fn xdiv op ulong ty ulong a ulong b ulong d ulong fl ulong to void
16715if or eq ty 10 eq ty 11
16716if ne op 36
16717fail 7
16718end
16719xfbin op ty a b d
16720ret
16721end
16722xload 0 a
16723xload 1 b
16724note Match bo_div's constant-divisor proofs and source trap sites.
16725if eq and fl 1 0
16726xrr 133 1 1
16727xtrap 0 2
16728end
16729done = ulong 0
16730if and ge ty 6 le ty 9
16731if and eq ty 9 eq and fl 2 0
16732note Only 64-bit IDIV can fault on MIN/-1; remainder must bypass with zero.
16733ximm 2 18446744073709551615
16734xrr 57 1 2
16735other = xbranch 1
16736if eq op 37
16737ximm 0 0
16738done = xbranch 14
16739else
16740xb 72 247 216
16741xtrap 6 1
16742done = xbranch 14
16743end
16744xpatch other get oc 0
16745end
16746xb 72 153 144
16747xb 72 247 249
16748else
16749xrr 49 2 2
16750xb 72 247 241
16751end
16752if eq op 37
16753xrr 137 0 2
16754end
16755if ne done 0
16756xpatch done get oc 0
16757end
16758xstore 0 d
16759note Narrow signed quotients can overflow; remainders and unsigned quotients cannot.
16760if and eq op 36 and ge ty 6 lt ty 9
16761bo_fix d ty
16762end
16763end
16764
16765fn xshift op ulong ty ulong a ulong b ulong imm ulong d ulong to void
16766xload 0 a
16767if ne imm 0
16768ximm 1 b
16769else
16770xload 1 b
16771end
16772note imm is set only for a statically valid shift amount, as in bo_shl/bo_shr.
16773if eq imm 0
16774ximm 2 twid ty
16775xrr 57 1 2
16776xtrap 2 7
16777end
16778xrr 137 2 0
16779if eq op 41
16780xb 72 211 224
16781if eq ty 9
16782xrr 137 8 0
16783xb 73 211 248
16784xrr 57 8 2
16785xtrap 1 1
16786end
16787else
16788if ge ty 6
16789xb 72 211 248
16790else
16791xb 72 211 232
16792end
16793end
16794xstore 0 d
16795if eq op 41
16796bo_fix d ty
16797end
16798end
16799
16800fn xunary op ulong dst ulong to void
16801ty = get rt 0
16802if eq ty 13
16803fail 7
16804end
16805if or eq ty 10 eq ty 11
16806if eq op 50
16807fail 7
16808end
16809fa = matreg get rk 0 get rv 0 ty
16810fd = destreg dst
16811note xfneg / xfsqrt / xfabs: the x86 forms of fp_neg / fp_un1 on x registers ra into rd (sign mask, SQRTS).
16812xload 0 fa
16813ximm 1 xfmask ty 0
16814xrr 49 0 1
16815xstore 0 fd
16816put rk 0 1
16817put rv 0 fd
16818put rt 0 ty
16819ret
16820end
16821a = matreg get rk 0 get rv 0 ty
16822d = destreg dst
16823xload 0 a
16824if eq op 50
16825if eq ty 1
16826ximm 1 1
16827xrr 49 0 1
16828else
16829xb 72 247 208
16830end
16831else
16832if eq ty 1
16833fail 7
16834end
16835xb 72 247 216
16836if eq ty 9
16837xtrap 6 1
16838end
16839end
16840xstore 0 d
16841if eq op 50
16842canon d ty
16843else
16844bo_fix d ty
16845end
16846put rk 0 1
16847put rv 0 d
16848put rt 0 ty
16849end
16850
16851fn xconv tt ulong to void
16852ft = get rt 0
16853if or or or eq ft 10 eq ft 11 eq tt 10 eq tt 11
16854if or eq ft 13 eq tt 13
16855fail 7
16856end
16857if or eq ft 1 eq tt 1
16858fail 9
16859end
16860fa = matreg get rk 0 get rv 0 ft
16861fd = fp_conv ft tt fa
16862put rk 0 1
16863put rv 0 fd
16864put rt 0 tt
16865ret
16866end
16867if or eq ft 1 eq tt 1
16868fail 9
16869end
16870r = matreg get rk 0 get rv 0 ft
16871wf = twid ft
16872wt = twid tt
16873s = ulong 99
16874if not and ge ft 6 le ft 9
16875if and ge tt 6 le tt 9
16876if ge wf wt
16877s = sub wt 1
16878end
16879else
16880if gt wf wt
16881s = wt
16882end
16883end
16884else
16885if not and ge tt 6 le tt 9
16886s = wt
16887if gt s 63
16888s = ulong 63
16889end
16890else
16891if gt wf wt
16892xext r tt 5
16893end
16894end
16895end
16896if ne s 99
16897xload 0 r
16898xb 72 193 232
16899emitb uchar s
16900xrr 133 0 0
16901xtrap 1 5
16902end
16903put rk 0 1
16904put rv 0 r
16905put rt 0 tt
16906end
16907
16908note Reserve checked stack BEFORE stores, CALL return addresses, or PUSH.
16909fn xcheck bytes ulong to void
16910xmem 141 0 4 sub ulong 0 bytes
16911xrr 57 0 15
16912xtrap 3 6
16913end
16914
16915fn xsp down ulong bytes ulong to void
16916if eq bytes 0
16917ret
16918end
16919if ne down 0
16920xcheck bytes
16921xb 72 129 236
16922else
16923xb 72 129 196
16924end
16925xword bytes
16926end
16927
16928fn xmovsp r ulong to void
16929xrr 137 0 4
16930xstore 0 r
16931end
16932
16933fn xpro np ulong to void
16934xrr 137 11 5
16935emitb 85
16936xrr 137 5 4
16937xb 72 129 236
16938xword 256
16939j = ulong 0
16940while lt j np
16941xmem 139 0 11 sub ulong 0 mul add j 1 8
16942xstore 0 j
16943j = add j 1
16944end
16945end
16946
16947
16948fn xarg t ulong k ulong v ulong to void
16949if eq k 6
16950mataddr t v
16951ret
16952end
16953if or eq k 3 eq k 7
16954matconst t v
16955ret
16956end
16957xmem 139 0 4 mul v 8
16958xstore 0 t
16959end
16960
16961fn xrestore d ulong slot ulong res ulong to ulong
16962put nslot 0 slot
16963rd = ulong 255
16964if ne res 0
16965rd = destreg d
16966xstore 0 rd
16967end
16968j = ulong 0
16969while lt j 18
16970if ne j rd
16971xmem 139 0 4 mul j 8
16972xstore 0 j
16973end
16974j = add j 1
16975end
16976xsp 0 256
16977ret rd
16978end
16979
16980note Typed addresses carry origin through fixup records. Numeric constants never relocate.
16981fn mataddr r ulong v ulong to void
16982if eq get xtarget 0 0
16983matconst r v
16984ret
16985end
16986xaddr 0 v
16987xstore 0 r
16988end
16989
16990fn xaddr r ulong v ulong to void
16991n = get xnaddr 0
16992if ge n 65536
16993fail 14
16994end
16995emitb uchar add 72 shl shr r 3 2
16996emitb 141
16997emitb uchar add 5 shl and r 7 3
16998put xasite n get oc 0
16999put xavalue n v
17000put xnaddr 0 add n 1
17001xword 0
17002end
17003
17004fn xelem e ulong k ulong v ulong t ulong to void
17005if or or eq t 1 eq t 10 eq t 11
17006fail 7
17007end
17008i = sub e 1
17009ln = get nm_len i
17010x = v
17011if ne unsz i 0
17012x = matreg k v t
17013xcmp x ulong get nm_lr i 0
17014xtrap 2 3
17015else
17016if eq k 3
17017if le ln v
17018fail 21
17019end
17020if and ne get om2 0 0 le v 4095
17021x = ulong 255
17022put ea_k 0 v
17023else
17024x = alloc
17025matconst x v
17026end
17027else
17028xcmp v ln 1
17029xtrap 2 3
17030end
17031end
17032put ea_i 0 x
17033if ge get nm_kind i 4
17034b = alloc
17035mataddr b get nm_addr i
17036put ea_b 0 b
17037else
17038put ea_b 0 ulong get nm_reg i
17039end
17040end
17041
17042note Indexed load/store uses RDX=base+index*element-size; pointers never enter source scalars.
17043fn xmemval store ulong base ulong idx ulong ty ulong val ulong to void
17044xload 2 base
17045if eq idx 255
17046ximm 1 get ea_k 0
17047else
17048xload 1 idx
17049end
17050xb 72 193 225
17051emitb uchar tlog ty
17052xrr 1 2 1
17053xmemraw store ty val
17054end
17055
17056note RDX is a checked aggregate address; layout byte offsets do not scale by the field width.
17057fn xlayout store ulong base ulong idx ulong ty ulong val ulong to void
17058xload 2 base
17059xload 1 idx
17060xrr 1 2 1
17061xmemraw store ty val
17062end
17063
17064fn xmemraw store ulong ty ulong val ulong to void
17065if ne store 0
17066xload 0 val
17067if eq twid ty 16
17068emitb 102
17069end
17070if eq twid ty 64
17071emitb 72
17072end
17073if le twid ty 8
17074emitb 136
17075else
17076emitb 137
17077end
17078emitb 2
17079else
17080if eq twid ty 64
17081xb 72 139 2
17082else
17083if eq twid ty 32
17084if and ge ty 6 le ty 9
17085xb 72 99 2
17086else
17087emitb 139
17088emitb 2
17089end
17090else
17091emitb 72
17092emitb 15
17093op = ulong 182
17094if eq twid ty 16
17095op = ulong 183
17096end
17097if and ge ty 6 le ty 9
17098op = add op 8
17099end
17100emitb uchar op
17101emitb 2
17102end
17103end
17104xstore 0 val
17105end
17106end
17107
17108fn xio c ulong fk ulong fv ulong e ulong ro ulong rn ulong to void
17109if ne get xtarget 0 1
17110fail 25
17111end
17112save
17113i = sub e 1
17114if ge get nm_kind i 4
17115xaddr 6 get nm_addr i
17116else
17117xload 6 ulong get nm_reg i
17118end
17119xmem 139 0 4 mul ro 8
17120xrr 1 6 0
17121if eq fk 3
17122ximm 7 fv
17123else
17124xmem 139 7 4 mul fv 8
17125end
17126xmem 139 2 4 mul rn 8
17127ximm 0 sub c 53
17128emitb 15
17129emitb 5
17130end
17131
17132note xcount: the x86-64 loop of cnt_word, after vs_chk. RSI = A + off (LEA of a global or text, else the
17133note array's slot; ADD), RCX = n, AL = v (zero-extended uchar). XMM0 = v in all 16 byte lanes (MOVD,
17134note PUNPCKLBW, PSHUFLW 0, PSHUFD 0), XMM3 = 0, EDX = 0 (the count). While RCX >= 16 (CMP; JB): a block of
17135note R8 = min(RCX >> 4, 255) chunks (MOV, SHR, CMP, JBE, MOV R8D 255; PXOR XMM1), each chunk MOVDQU XMM2,
17136note [RSI]; PCMPEQB XMM2, XMM0 (0xff where equal); PSUBB XMM1, XMM2 (+1 per equal lane; 255 chunks keep
17137note each lane <= 255); ADD RSI, 16; SUB RCX, 16; DEC R8; JNE; then PSADBW XMM1, XMM3 (each 8-lane half
17138note summed into its quadword), MOVQ R9 / ADD RDX twice around PSRLDQ XMM1, 8, JMP back. The last RCX < 16
17139note bytes one at a time: TEST; JE; CMP [RSI], AL; JNE; INC RDX; INC RSI; DEC RCX; JNE. SSE2 only (every
17140note x86-64 CPU), unaligned loads, never a byte outside the checked slice. The 128 bytes after the four
17141note setup moves are fixed (Intel SDM vol. 2 encodings, checked byte for byte against llvm-mc 18; the short
17142note jumps' distances are constant); RDX holds the result for cnt_word. RAX, RCX, RDX, RSI, R8, R9 and
17143note XMM0-XMM3 are emitter scratch here: language values live in RBP slots.
17144fn xcount e ulong ro ulong rn ulong v ulong to void
17145i = sub e 1
17146if ge get nm_kind i 4
17147xaddr 6 get nm_addr i
17148else
17149xload 6 ulong get nm_reg i
17150end
17151xload 0 ro
17152xrr 1 6 0
17153xload 1 rn
17154xload 0 v
17155xb 102 15 110
17156emitb 192
17157xb 102 15 96
17158emitb 192
17159xb 242 15 112
17160emitb 192
17161emitb 0
17162xb 102 15 112
17163emitb 192
17164emitb 0
17165xb 102 15 239
17166emitb 219
17167emitb 49
17168emitb 210
17169xb 72 131 249
17170emitb 16
17171emitb 114
17172emitb 78
17173xb 73 137 200
17174xb 73 193 232
17175emitb 4
17176xb 73 129 248
17177xb 255 0 0
17178emitb 0
17179emitb 118
17180emitb 6
17181xb 65 184 255
17182xb 0 0 0
17183xb 102 15 239
17184emitb 201
17185xb 243 15 111
17186emitb 22
17187xb 102 15 116
17188emitb 208
17189xb 102 15 248
17190emitb 202
17191xb 72 131 198
17192emitb 16
17193xb 72 131 233
17194emitb 16
17195xb 73 255 200
17196emitb 117
17197emitb 231
17198xb 102 15 246
17199emitb 203
17200xb 102 73 15
17201emitb 126
17202emitb 201
17203xb 76 1 202
17204xb 102 15 115
17205emitb 217
17206emitb 8
17207xb 102 73 15
17208emitb 126
17209emitb 201
17210xb 76 1 202
17211emitb 235
17212emitb 172
17213xb 72 133 201
17214emitb 116
17215emitb 15
17216emitb 56
17217emitb 6
17218emitb 117
17219emitb 3
17220xb 72 255 194
17221xb 72 255 198
17222xb 72 255 201
17223emitb 117
17224emitb 241
17225end
17226
17227fn xarray w ulong to void
17228if ne get nb 0 0
17229fail 9
17230end
17231t = typeword
17232elemtype t
17233n = lenval 16
17234arrlen n
17235by = shl shr add shl n tlog t 15 4 4
17236if gt add get stkbytes 0 by 4194304
17237fail 23
17238end
17239put stkbytes 0 add get stkbytes 0 by
17240r = alloc
17241put held 0 get nslot 0
17242rdval 0 t 255 0
17243if ne get rt 0 t
17244fail 7
17245end
17246v = matreg get rk 0 get rv 0 t
17247xsp 1 by
17248xmovsp r
17249i = alloc
17250matconst i 0
17251lp = get oc 0
17252xmemval 1 r i t v
17253xarith 33 5 i 1 1 i
17254xcmp i n 1
17255blo lp
17256addname w r t n 1 0
17257end
17258
17259note xfornum / xforval: the x86 sf_num / sf_val. bn and n come from sf_num / sf_val, which parse the bound
17260note themselves (sf_val: 1000000 when no bound is written); bn = 0 leaves a number count unguarded, as on Arm.
17261fn xfornum w ulong t ulong k ulong sn ulong sh ulong bn ulong to void
17262eol
17263i = alloc
17264put held 0 get nslot 0
17265addname w i t 0 0 0
17266matconst i 0
17267c = ulong 0
17268if ne bn 0
17269c = alloc
17270put held 0 get nslot 0
17271matconst c 0
17272end
17273hd = get oc 0
17274cp_head get cur 0 get line 0
17275xcmp i k 1
17276br = sf_br t
17277if ne bn 0
17278lpcnt c bn
17279end
17280pushb br 4 hd i t
17281pushnh sn sh
17282cp_skip
17283end
17284
17285fn xforval w ulong t ulong kr ulong sn ulong sh ulong n ulong to void
17286eol
17287i = alloc
17288put held 0 get nslot 0
17289addname w i t 0 0 0
17290matconst i 0
17291c = alloc
17292put held 0 get nslot 0
17293matconst c 0
17294hd = get oc 0
17295cp_head get cur 0 get line 0
17296xcmp i kr 0
17297br = sf_br t
17298lpcnt c n
17299pushb br 4 hd i t
17300pushnh sn sh
17301cp_skip
17302end
17303
17304fn xleave to void
17305if eq get xtarget 0 2
17306note UEFI: every exit jumps to the shared firmware return emitted by xefx 1; RAX is the status.
17307n = get xnexit 0
17308if ge n 65536
17309fail 14
17310end
17311put xexsite n xbranch 14
17312put xnexit 0 add n 1
17313ret
17314end
17315xrr 137 7 0
17316ximm 0 60
17317emitb 15
17318emitb 5
17319end
17320
17321fn xfinish to void
17322eol
17323if eq get dead 0 0
17324if ne get curret 0 12
17325fail 18
17326end
17327cp_post 0 0 12
17328epi
17329end
17330j = ulong 0
17331n = get nt 0
17332while lt j n
17333xpatch get tsite j get oc 0
17334if eq get progmode 0 0
17335note Bare units reserve physical R12/R14: recovery stack and immutable runtime table.
17336note Host helpers clobber these registers and must remain forbidden in bare units.
17337emitb 190
17338xword get tcode j
17339xb 65 255 102
17340emitb 16
17341else
17342if eq get xtarget 0 2
17343ximm 0 or 9223372036854775808 add 100 get tcode j
17344else
17345ximm 0 add 100 get tcode j
17346end
17347xleave
17348end
17349j = add j 1
17350end
17351put curret 0 0
17352put nn 0 get nglob 0
17353put curfn 0 0
17354put nbrk 0 0
17355put ncont 0 0
17356end
17357
17358fn xlead to void
17359note Only the exact bytes "target " at offset zero select x86. Default parsing is untouched.
17360if lt get inlen 0 8
17361ret
17362end
17363if or ne get inp 0 116 ne get inp 1 97
17364ret
17365end
17366if or ne get inp 2 114 ne get inp 3 103
17367ret
17368end
17369if or ne get inp 4 101 ne get inp 5 116
17370ret
17371end
17372if ne get inp 6 32
17373ret
17374end
17375put cur 0 0
17376put line 0 1
17377tok
17378tok
17379if and eq get tw 0 8679965255892613240 eq get tl 0 8
17380put xtarget 0 1
17381else
17382if and eq get tw 0 115888505632888 eq get tl 0 6
17383put xtarget 0 2
17384else
17385fail 25
17386end
17387end
17388eol
17389p = ulong 0
17390while lt p get cur 0
17391if ne get inp p 10
17392put inp p 32
17393end
17394p = add p 1
17395end
17396put cur 0 0
17397put line 0 1
17398end
17399
17400
17401fn xeof to void
17402f = findfn 1852399981 4 0
17403if eq f 0
17404fail 16
17405end
17406if ne get fn_np sub f 1 0
17407fail 9
17408end
17409entry = get oc 0
17410efi = eq get xtarget 0 2
17411if efi
17412xefx 0 0
17413else
17414xrr 137 13 4
17415ximm 15 268435456
17416ximm 4 276824064
17417ximm 5 0
17418end
17419emitb 232
17420p = get oc 0
17421xword sub get fn_addr sub f 1 add p 4
17422if eq get fn_ret sub f 1 12
17423ximm 0 0
17424end
17425xleave
17426if efi
17427xefx 1 0
17428xefx 2 entry
17429else
17430elfhdr
17431wrword 16 4063234
17432wrword 24 add 4194304 entry
17433wrword 96 get oc 0
17434wrword 104 get oc 0
17435wrword 160 add get poolb 0 8388608
17436end
17437i = ulong 0
17438while lt i get xnaddr 0
17439p = get xasite i
17440v = get xavalue i
17441if efi
17442note PE: code RVA = file offset + 3584; static data lives in the bss section at xbss.
17443if ge v 268435456
17444v = add get xbss 0 sub v 268435456
17445else
17446v = add sub v 4194304 3584
17447end
17448p = add p 3584
17449else
17450v = sub v 4194304
17451end
17452wrword get xasite i sub v add p 4
17453i = add i 1
17454end
17455writeout 0
17456end
17457
17458note ======== x86efi: UEFI 2.10 PE32+ application (docs/X86-EFI.md) ========
17459note efi OP ARG A OFF N -> ulong EFI_STATUS or result. OP is a literal 0..14; A is writable uchar
17460note storage, bounds checked before any firmware call; ARG is never a pointer. Errors are 2^63|code.
17461note Private state occupies the first 16 KiB of the PE .bss, below the owned 8 MiB stack. Firmware is
17462note called per UEFI 2.3.4.2: RCX/RDX/R8/R9, 32-byte home area, an 80-byte 16-aligned reservation.
17463note Language values stay in RBP slots, so integers and floats survive every firmware call.
17464note Port of the Allen x86efi fork (operations 0..11) onto main's backend; 12, 13 and 14 are new.
17465
17466note xp k: 0 LEA r,private+off; 1 load r from private+off; 2 store r to private+off (via R10);
17467note 3 call BootServices+off (R11); 4 call method off of the protocol held in private slot r (RCX = This).
17468fn xp k ulong r ulong off ulong to void
17469if eq k 0
17470xaddr r add 268435456 off
17471ret
17472end
17473if eq k 1
17474xaddr r add 268435456 off
17475xmem 139 r r 0
17476ret
17477end
17478if eq k 2
17479xaddr 10 add 268435456 off
17480xmem 137 r 10 0
17481ret
17482end
17483if eq k 3
17484xaddr 11 268435456
17485xmem 139 11 11 0
17486xmem 139 11 11 96
17487xmem 139 11 11 off
17488xb 65 255 211
17489ret
17490end
17491xaddr 1 add 268435456 r
17492xmem 139 1 1 0
17493xmem 139 11 1 off
17494xb 65 255 211
17495end
17496
17497note xer k: 0 branch on cc to an error tail that returns 2^63|code; 1 jump to the operation's end;
17498note 2 RDTSC into RAX (Intel SDM: LFENCE before and after serializes local instruction execution).
17499fn xer k ulong cc ulong code ulong to void
17500if eq k 2
17501xb 15 174 232
17502emitb 15
17503emitb 49
17504xb 15 174 232
17505xb 72 193 226
17506emitb 32
17507xrr 9 0 2
17508ret
17509end
17510if eq k 0
17511n = get xenerr 0
17512if ge n 256
17513fail 14
17514end
17515put xerrpos n xbranch cc
17516put xerrcode n code
17517put xenerr 0 add n 1
17518ret
17519end
17520m = get xenend 0
17521if ge m 256
17522fail 14
17523end
17524put xendpos m xbranch 14
17525put xenend 0 add m 1
17526end
17527
17528note xg k: inline constant data skipped by a jump; returns its address for xaddr. k 0..7 a UEFI GUID
17529note packed Data1|Data2<<32|Data3<<48 then Data4 little-endian (0 GOP, 1 SNP, 2 RNG, 3 simple pointer,
17530note 4 absolute pointer, 5 Simple File System, 6 LoadedImage, 7 FileInfo, 9 SMBIOS3 table, 10 SMBIOS
17531note table); 8 UCS-2 \EFI\BOOT\BOOTX64.EFI.
17532fn xg k ulong to ulong
17533br = xbranch 14
17534p = get oc 0
17535if eq k 8
17536xb 92 0 69
17537xb 0 70 0
17538xb 73 0 92
17539xb 0 66 0
17540xb 79 0 79
17541xb 0 84 0
17542xb 92 0 66
17543xb 0 79 0
17544xb 79 0 84
17545xb 0 88 0
17546xb 54 0 52
17547xb 0 46 0
17548xb 69 0 70
17549xb 0 73 0
17550emitb 0
17551emitb 0
17552else
17553a = ulong 5348063987722529246
17554b = ulong 7661046075708078998
17555if eq k 1
17556a = ulong 1284559596355596985
17557b = ulong 5602828850577812890
17558end
17559if eq k 2
17560a = ulong 4845287014151666853
17561b = ulong 4908688144209030790
17562end
17563if eq k 3
17564a = ulong 1284945866654190727
17565b = ulong 5602828850577821594
17566end
17567if eq k 4
17568a = ulong 5398063699126244139
17569b = ulong 4839848993530975643
17570end
17571if eq k 5
17572a = ulong 1284199179690597154
17573b = ulong 4283602510276475278
17574end
17575if eq k 6
17576a = ulong 1284253093421855137
17577b = ulong 4283602510276476814
17578end
17579if eq k 7
17580a = ulong 1284208961261104786
17581b = ulong 4283602510276475278
17582end
17583if eq k 9
17584a = ulong 5344813523769562436
17585b = ulong 10728454813909331609
17586end
17587if eq k 10
17588a = ulong 1284420384822471985
17589b = ulong 5602828850577807002
17590end
17591xword a
17592xword shr a 32
17593xword b
17594xword shr b 32
17595end
17596xpatch br get oc 0
17597ret add p 4194304
17598end
17599
17600note xe k: 2 close the operation (error tails, then the shared end); 3 need N >= a (5); 4 need the
17601note protocol in private slot a (3); 5 leave on a firmware error; 7 OR a into the info flags at A+4;
17602note 8 LocateProtocol GUID a into slot b; 9 calibrate TSC ticks/ms over a 100 ms Stall (176, base 168);
17603note 10 settle the transmit buffer at exit (status 88); 12 elapsed milliseconds into A.
17604fn xe k ulong a ulong b ulong to void
17605if eq k 2
17606xer 1 0 0
17607j = ulong 0
17608while lt j get xenerr 0
17609xpatch get xerrpos j get oc 0
17610ximm 0 or 9223372036854775808 get xerrcode j
17611xer 1 0 0
17612j = add j 1
17613end
17614i = ulong 0
17615while lt i get xenend 0
17616xpatch get xendpos i get oc 0
17617i = add i 1
17618end
17619ret
17620end
17621if eq k 3
17622ximm 0 a
17623xrr 57 13 0
17624xer 0 3 5
17625ret
17626end
17627if eq k 4
17628xp 1 1 a
17629xrr 133 1 1
17630xer 0 0 3
17631ret
17632end
17633if eq k 5
17634xrr 133 0 0
17635ok = xbranch 0
17636xleave
17637xpatch ok get oc 0
17638ret
17639end
17640if eq k 7
17641xhraw 0 12 4 4
17642ximm 2 a
17643xrr 9 0 2
17644xhput 0 12 4 4
17645ret
17646end
17647if eq k 8
17648g = xg a
17649xaddr 1 g
17650ximm 2 0
17651xp 0 8 b
17652xp 3 0 320
17653note LocateProtocol is optional. Failed output is discarded, never dereferenced.
17654xrr 133 0 0
17655ok8 = xbranch 0
17656ximm 0 0
17657xp 2 0 b
17658xpatch ok8 get oc 0
17659ret
17660end
17661if eq k 9
17662xer 2 0 0
17663xp 2 0 160
17664ximm 1 100000
17665xp 3 0 248
17666xrr 133 0 0
17667ok9 = xbranch 0
17668xleave
17669xpatch ok9 get oc 0
17670xer 2 0 0
17671xp 1 1 160
17672xrr 41 0 1
17673mono = xbranch 3
17674ximm 1 100
17675ximm 2 0
17676xb 72 247 241
17677xp 2 0 176
17678xer 2 0 0
17679xp 2 0 168
17680xpatch mono get oc 0
17681ret
17682end
17683if eq k 10
17684xp 1 1 96
17685xrr 133 1 1
17686none = xbranch 0
17687xp 1 0 72
17688xrr 133 0 0
17689idle = xbranch 0
17690ximm 0 0
17691xp 2 0 80
17692ximm 2 0
17693xp 0 8 80
17694xp 4 24 88
17695xp 1 0 80
17696xp 1 2 96
17697xrr 57 0 2
17698busy = xbranch 1
17699xpatch idle get oc 0
17700xp 1 1 96
17701xp 3 0 72
17702xrr 133 0 0
17703freed = xbranch 0
17704xp 2 0 88
17705xpatch freed get oc 0
17706fin = xbranch 14
17707xpatch busy get oc 0
17708note Do not free or reuse an unreturned DMA buffer. Expose NOT_READY on exit.
17709ximm 0 9223372036854775814
17710xp 2 0 88
17711xpatch fin get oc 0
17712xpatch none get oc 0
17713ret
17714end
17715xp 1 1 176
17716xrr 133 1 1
17717xer 0 0 3
17718xer 2 0 0
17719xp 1 2 168
17720xrr 41 0 2
17721xer 0 3 3
17722ximm 2 0
17723xb 72 247 241
17724xmem 137 0 12 0
17725ximm 0 0
17726end
17727
17728note xefx k: 0 image entry, 1 the shared firmware return (every exit site lands here), 2 PE32+ headers.
17729fn xefx k ulong a ulong to void
17730if eq k 0
17731note Preserve the integer nonvolatiles, MXCSR and XMM6-15 (UEFI 2.3.4.2), then install the owned stack.
17732emitb 83
17733emitb 85
17734emitb 86
17735emitb 87
17736xb 65 84 65
17737emitb 85
17738xb 65 86 65
17739emitb 87
17740xp 2 4 2048
17741xp 2 1 8
17742xp 2 2 0
17743xp 0 10 2056
17744xb 65 15 174
17745emitb 26
17746xp 0 10 2080
17747j = ulong 6
17748while lt j 16
17749emitb 243
17750emitb uchar add 65 mul shr j 3 4
17751xb 15 127 add 130 shl and j 7 3
17752xword mul sub j 6 16
17753j = add j 1
17754end
17755note MXCSR 0x1F80: all exceptions masked, round to nearest, as on x86 Linux.
17756ximm 0 8064
17757xp 2 0 2064
17758xp 0 10 2064
17759xb 65 15 174
17760emitb 18
17761xp 0 15 16384
17762xp 0 4 8388608
17763ximm 5 0
17764note Entry has its own 80-byte EFI home/argument reservation, not an Omega frame.
17765xb 72 129 236
17766xword 80
17767ximm 1 0
17768ximm 2 0
17769ximm 8 0
17770ximm 9 0
17771xp 3 0 256
17772xe 5 0 0
17773note TX storage outlives the image if firmware fails to release an in-flight packet.
17774ximm 1 2
17775ximm 2 2048
17776xp 0 8 96
17777xp 3 0 64
17778xe 5 0 0
17779xe 8 0 16
17780xe 8 2 32
17781xe 8 3 40
17782xe 8 4 48
17783note Intel SDM 20.17.1 invariant TSC, CPUID.80000007H:EDX[8]; otherwise calibrate lazily (op 10).
17784ximm 0 1
17785emitb 15
17786emitb 162
17787ximm 0 16
17788xrr 33 2 0
17789xrr 133 2 2
17790no1 = xbranch 0
17791ximm 0 2147483648
17792emitb 15
17793emitb 162
17794ximm 2 2147483655
17795xrr 57 0 2
17796no2 = xbranch 3
17797ximm 0 2147483655
17798emitb 15
17799emitb 162
17800ximm 0 256
17801xrr 33 2 0
17802xrr 133 2 2
17803no3 = xbranch 0
17804xe 9 0 0
17805xpatch no1 get oc 0
17806xpatch no2 get oc 0
17807xpatch no3 get oc 0
17808xb 72 129 196
17809xword 80
17810ret
17811end
17812if eq k 1
17813i = ulong 0
17814while lt i get xnexit 0
17815xpatch get xexsite i get oc 0
17816i = add i 1
17817end
17818note Restore a known-good private stack even when an Omega stack check failed.
17819xp 0 4 8388608
17820xb 72 129 236
17821xword 80
17822xp 2 0 88
17823note An efi 12 file still open at exit (return, exit or trap) is flushed when writable, then the file
17824note and its volume root are closed and the slot cleared, as a process exit closes descriptors.
17825xp 1 1 2304
17826xrr 133 1 1
17827p1 = xbranch 0
17828xp 1 0 2320
17829xrr 133 0 0
17830p2 = xbranch 0
17831xp 4 2304 80
17832xpatch p2 get oc 0
17833xp 4 2304 16
17834xpatch p1 get oc 0
17835xp 1 1 2312
17836xrr 133 1 1
17837p3 = xbranch 0
17838xp 4 2312 16
17839xpatch p3 get oc 0
17840ximm 0 0
17841xp 2 0 2304
17842xp 2 0 2312
17843xp 2 0 2320
17844xe 10 0 0
17845xp 0 10 2080
17846r = ulong 6
17847while lt r 16
17848emitb 243
17849emitb uchar add 65 mul shr r 3 4
17850xb 15 111 add 130 shl and r 7 3
17851xword mul sub r 6 16
17852r = add r 1
17853end
17854xp 0 10 2056
17855xb 65 15 174
17856emitb 18
17857xp 1 0 88
17858xp 1 11 2048
17859xrr 137 4 11
17860xb 65 95 65
17861emitb 94
17862xb 65 93 65
17863emitb 92
17864emitb 95
17865emitb 94
17866emitb 93
17867emitb 91
17868emitb 195
17869ret
17870end
17871note PE32+ UEFI application. Section/file alignment 4096/512; every address is RIP-relative, so the
17872note image is position independent. A valid empty relocation block keeps loader relocation support.
17873while ne and get oc 0 511 0
17874emitb 0
17875end
17876raw = sub get oc 0 512
17877bs = shl shr add add raw 4096 4095 12 12
17878put xbss 0 bs
17879wrword 0 23117
17880wrword 60 64
17881wrword 64 17744
17882wrword 68 165476
17883wrword 84 2228464
17884wrword 88 523
17885wrword 92 raw
17886wrword 100 add get poolb 0 8388608
17887wrword 104 add a 3584
17888wrword 108 4096
17889wrword 112 4194304
17890wrword 120 4096
17891wrword 124 512
17892wrword 128 6
17893wrword 136 6
17894wrword 144 shl shr add add bs add get poolb 0 8388608 4095 12 12
17895wrword 148 512
17896wrword 156 4194314
17897wrword 160 1048576
17898wrword 168 4096
17899wrword 176 1048576
17900wrword 184 4096
17901wrword 196 16
17902wrword 512 0
17903wrword 516 8
17904note Base relocation directory points to the first eight bytes of .text, reserved by the driver.
17905wrword 240 4096
17906wrword 244 8
17907wrword 328 2019914798
17908wrword 332 116
17909wrword 336 raw
17910wrword 340 4096
17911wrword 344 raw
17912wrword 348 512
17913wrword 364 1610612768
17914wrword 368 1936941614
17915wrword 376 add get poolb 0 8388608
17916wrword 380 bs
17917wrword 404 3221225600
17918end
17919
17920fn xefival dst ulong to void
17921if ne get xtarget 0 2
17922fail 25
17923end
17924x = mul get depth 0 32
17925put fr add x 7 get nslot 0
17926op = lenval 4
17927if gt op 14
17928fail 25
17929end
17930harg x 2 5
17931e = harray x 1
17932note RBX anchors the snapshot only until ARG is loaded; firmware-preserved R14 carries it across the call
17933note (UEFI 2.3.4.2), and the caller's R14 waits in the snapshot tail beside R13.
17934xhbegin
17935xmem 137 14 3 152
17936xrr 137 14 3
17937xhb 12 e get fr add x 21
17938xhs 13 get fr add x 22
17939xha x 2 3
17940note Firmware runs on the owned stack: reserve 64 KiB (trap 6), then align RSP for UEFI 2.3.4.2.
17941xcheck 65536
17942xb 72 131 228
17943emitb 240
17944xb 72 129 236
17945xword 80
17946put xenerr 0 0
17947put xenend 0 0
17948xeop op
17949xe 2 0 0
17950xrr 137 4 14
17951xmem 139 13 4 144
17952xmem 139 14 4 152
17953r = restore dst get fr add x 7 get svn 0 get sva 0 1
17954vdone 1 r 5
17955end
17956
17957note Operation registers: RBX = ARG, R12 = A+OFF, R13 = N; the result or error is RAX.
17958fn xeop op ulong to void
17959if eq op 1
17960xe 3 4 0
17961xp 1 1 0
17962xmem 139 1 1 48
17963xrr 133 1 1
17964xer 0 0 3
17965xrr 137 2 12
17966xmem 139 11 1 8
17967xb 65 255 211
17968ret
17969end
17970if eq op 2
17971xrr 137 1 3
17972xp 3 0 248
17973ret
17974end
17975if eq op 4
17976xe 3 16 0
17977xp 1 11 0
17978xmem 139 11 11 88
17979xmem 139 11 11 24
17980xrr 137 1 12
17981ximm 2 0
17982xb 65 255 211
17983ret
17984end
17985if eq op 8
17986xenet
17987ret
17988end
17989if eq op 10
17990xe 3 8 0
17991xp 1 1 176
17992xrr 133 1 1
17993ok = xbranch 1
17994xe 9 0 0
17995xpatch ok get oc 0
17996xe 12 0 0
17997ret
17998end
17999if eq op 11
18000xefile
18001ret
18002end
18003if eq op 12
18004xf12
18005ret
18006end
18007if eq op 14
18008xf14
18009ret
18010end
18011xdev op
18012end
18013
18014note efi 14 machine identity; ARG bits 0..15 sub, 16..63 its parameter. Sub 0: CPUID leaf ARG>>16
18015note (below 2^32, else 32) with ECX 0, EAX EBX ECX EDX as four u32 at A+OFF (N >= 16, else 5),
18016note result 16. ARG 1: the UCS-2 code units of SystemTable FirmwareVendor (+24) up to its NUL, at
18017note most N bytes in whole units, result bytes copied (0 when the pointer is NULL). ARG 2: result
18018note FirmwareRevision (+32). Sub 3: SMBIOS structure table bytes from position ARG>>16 into A
18019note (at most N), result bytes copied, 0 at or past the end. The table comes from the configuration
18020note table: the SMBIOS3 entry point (_SM3_, size u32 @12, address u64 @16) first, else SMBIOS 2.x
18021note (_SM_, length u16 @22, address u32 @24); 44 when neither is there, an anchor is wrong or the
18022note address is 0. Any other ARG is 32. Nothing is written past A+OFF+N.
18023fn xf14 to void
18024ximm 0 1
18025xrr 57 3 0
18026p = xbranch 1
18027xp 1 1 0
18028xmem 139 6 1 24
18029ximm 0 0
18030xrr 137 7 12
18031xrr 137 9 13
18032xrr 133 6 6
18033z = xbranch 0
18034loop = get oc 0
18035ximm 2 2
18036xrr 57 9 2
18037d1 = xbranch 3
18038xhraw 2 6 0 2
18039xrr 133 2 2
18040d2 = xbranch 0
18041xhput 2 7 0 2
18042xb 72 131 198
18043emitb 2
18044xb 72 131 199
18045emitb 2
18046xb 72 131 192
18047emitb 2
18048xb 73 131 233
18049emitb 2
18050q = xbranch 14
18051xpatch q loop
18052xpatch z get oc 0
18053xpatch d1 get oc 0
18054xpatch d2 get oc 0
18055xer 1 0 0
18056xpatch p get oc 0
18057ximm 0 2
18058xrr 57 3 0
18059p = xbranch 1
18060xp 1 1 0
18061xhraw 0 1 32 4
18062xer 1 0 0
18063xpatch p get oc 0
18064xrr 137 6 3
18065ximm 0 65535
18066xrr 33 6 0
18067p = xbranch 1
18068xe 3 16 0
18069xrr 137 0 3
18070xb 72 193 232
18071emitb 16
18072xrr 137 2 0
18073xb 72 193 234
18074emitb 32
18075xrr 133 2 2
18076xer 0 1 32
18077ximm 1 0
18078emitb 15
18079emitb 162
18080xhput 0 12 0 4
18081xhput 3 12 4 4
18082xhput 1 12 8 4
18083xhput 2 12 12 4
18084ximm 0 16
18085xer 1 0 0
18086xpatch p get oc 0
18087ximm 0 3
18088xrr 57 6 0
18089xer 0 1 32
18090xp 1 1 0
18091xmem 139 8 1 104
18092xmem 139 9 1 112
18093f3 = ulong 0
18094f = ulong 0
18095g = ulong 9
18096while le g 10
18097xaddr 10 xg g
18098xrr 137 11 9
18099xrr 137 1 8
18100loop = get oc 0
18101xrr 133 1 1
18102miss = xbranch 0
18103xmem 139 0 11 0
18104xmem 59 0 10 0
18105n1 = xbranch 1
18106xmem 139 0 11 8
18107xmem 59 0 10 8
18108n2 = xbranch 1
18109xmem 139 6 11 16
18110f = xbranch 14
18111xpatch n1 get oc 0
18112xpatch n2 get oc 0
18113xb 73 131 195
18114emitb 24
18115xb 72 131 233
18116emitb 1
18117q = xbranch 14
18118xpatch q loop
18119xpatch miss get oc 0
18120if eq g 9
18121f3 = f
18122end
18123g = add g 1
18124end
18125xer 0 14 44
18126xpatch f get oc 0
18127xrr 133 6 6
18128xer 0 0 44
18129xhraw 0 6 0 4
18130ximm 2 1598903135
18131xrr 57 0 2
18132xer 0 1 44
18133xhraw 8 6 22 2
18134xhraw 6 6 24 4
18135cp = xbranch 14
18136xpatch f3 get oc 0
18137xrr 133 6 6
18138xer 0 0 44
18139xhraw 0 6 0 4
18140ximm 2 860705631
18141xrr 57 0 2
18142xer 0 1 44
18143xhraw 8 6 12 4
18144xmem 139 6 6 16
18145xpatch cp get oc 0
18146xrr 133 6 6
18147xer 0 0 44
18148xrr 137 2 3
18149xb 72 193 234
18150emitb 16
18151ximm 0 0
18152xrr 57 2 8
18153done = xbranch 2
18154xrr 137 1 8
18155xrr 41 1 2
18156xrr 57 1 13
18157ok = xbranch 9
18158xrr 137 1 13
18159xpatch ok get oc 0
18160xrr 1 6 2
18161xrr 137 7 12
18162xrr 137 0 1
18163emitb 252
18164emitb 243
18165emitb 164
18166xpatch done get oc 0
18167end
18168
18169note Device operations: 0 info, 3 GOP Blt, 5 entropy, 6 receive, 7 transmit, 9 pointer, 13 console.
18170note OP0 info, 64 bytes: u32 version=1, flags (1 GOP, 2 net, 4 RNG, 8 pointer), u32 width/height@8,
18171note pixelFormat@16, stride@20, MAC[6]@24, maxPacket@32, hwAddrSize@36, media@40, state@44, txBusy@48.
18172note OP3 ARG: width bits0..31, height bits32..47, destination row bits48..63 (UEFI 12.9.2 Blt).
18173fn xdev op ulong to void
18174if eq op 0
18175xe 3 64 0
18176ximm 0 0
18177j = ulong 0
18178while lt j 64
18179xmem 137 0 12 j
18180j = add j 8
18181end
18182ximm 0 1
18183xmem 137 0 12 0
18184xp 1 1 16
18185xrr 133 1 1
18186p = xbranch 0
18187xmem 139 1 1 24
18188xrr 133 1 1
18189z1 = xbranch 0
18190xmem 139 1 1 8
18191xrr 133 1 1
18192z2 = xbranch 0
18193xmem 139 0 1 4
18194xmem 137 0 12 8
18195xhraw 0 1 12 4
18196xhput 0 12 16 4
18197xhraw 0 1 32 4
18198xhput 0 12 20 4
18199ximm 0 1
18200xhput 0 12 4 4
18201xpatch p get oc 0
18202xp 1 1 24
18203xrr 133 1 1
18204p = xbranch 0
18205xmem 139 1 1 120
18206xrr 133 1 1
18207z3 = xbranch 0
18208xhraw 0 1 552 4
18209xhput 0 12 24 4
18210xhraw 0 1 556 2
18211xhput 0 12 28 4
18212xhraw 0 1 12 4
18213xhput 0 12 32 4
18214xhraw 0 1 4 4
18215xhput 0 12 36 4
18216xhraw 0 1 652 1
18217xhput 0 12 40 4
18218xhraw 0 1 0 4
18219xhput 0 12 44 4
18220xp 1 0 72
18221xhput 0 12 48 4
18222xe 7 2 0
18223xpatch p get oc 0
18224xp 1 1 32
18225xrr 133 1 1
18226p = xbranch 0
18227xe 7 4 0
18228xpatch p get oc 0
18229xp 1 1 40
18230xp 1 2 48
18231xrr 9 1 2
18232xrr 133 1 1
18233p = xbranch 0
18234xe 7 8 0
18235xpatch p get oc 0
18236xpatch z1 get oc 0
18237xpatch z2 get oc 0
18238xpatch z3 get oc 0
18239ximm 0 0
18240ret
18241end
18242if eq op 3
18243xe 4 16 0
18244xrr 137 8 3
18245xb 73 193 232
18246emitb 32
18247ximm 0 65535
18248xrr 33 8 0
18249xrr 137 10 3
18250xb 73 193 234
18251emitb 48
18252xrr 137 9 3
18253ximm 0 4294967295
18254xrr 33 9 0
18255xrr 133 8 8
18256xer 0 0 2
18257xrr 133 9 9
18258xer 0 0 2
18259xmem 139 2 1 24
18260xrr 133 2 2
18261xer 0 0 2
18262xmem 139 2 2 8
18263xrr 133 2 2
18264xer 0 0 2
18265xhraw 0 2 4 4
18266xrr 57 9 0
18267xer 0 8 2
18268xhraw 0 2 8 4
18269xrr 41 0 10
18270xer 0 3 2
18271xrr 57 8 0
18272xer 0 8 2
18273xrr 137 0 8
18274xb 73 15 175
18275emitb 193
18276xb 72 193 224
18277emitb 2
18278xrr 57 0 13
18279xer 0 1 2
18280xmem 137 9 4 56
18281xmem 137 8 4 64
18282ximm 0 0
18283xmem 137 0 4 32
18284xmem 137 0 4 40
18285xmem 137 10 4 48
18286xmem 137 0 4 72
18287xrr 137 2 12
18288ximm 8 2
18289ximm 9 0
18290xp 4 16 16
18291ret
18292end
18293if eq op 5
18294note RNG protocol when present; else Intel SDM RDRAND (CPUID.1:ECX[30], CF = valid), ten attempts
18295note per byte, then EFI_NOT_READY, never fallback data.
18296xp 1 1 32
18297xrr 133 1 1
18298p = xbranch 0
18299ximm 2 0
18300xrr 137 8 13
18301xrr 137 9 12
18302xp 4 32 8
18303xer 1 0 0
18304xpatch p get oc 0
18305ximm 0 0
18306emitb 15
18307emitb 162
18308ximm 2 1
18309xrr 57 0 2
18310xer 0 3 3
18311ximm 0 1
18312emitb 15
18313emitb 162
18314ximm 2 1073741824
18315xrr 33 1 2
18316xrr 133 1 1
18317xer 0 0 3
18318xrr 137 7 12
18319xrr 137 6 13
18320lp = get oc 0
18321xrr 133 6 6
18322q = xbranch 0
18323ximm 1 10
18324rw = get oc 0
18325xb 72 15 199
18326emitb 240
18327s = xbranch 3
18328xb 72 255 201
18329p = xbranch 1
18330xpatch p rw
18331xer 0 14 6
18332xpatch s get oc 0
18333emitb 136
18334emitb 7
18335xb 72 255 199
18336xb 72 255 206
18337back lp
18338xpatch q get oc 0
18339ximm 0 0
18340ret
18341end
18342if eq op 6
18343xe 3 9 0
18344xe 4 24 0
18345xrr 137 0 13
18346xb 72 131 232
18347emitb 8
18348xmem 137 0 12 0
18349ximm 2 0
18350xrr 137 8 12
18351xmem 141 9 12 8
18352ximm 0 0
18353xmem 137 0 4 32
18354xmem 137 0 4 40
18355xmem 137 0 4 48
18356xp 4 24 104
18357ret
18358end
18359if eq op 7
18360note Transmit always owns its private packet until GetStatus reclaims that exact address (UEFI 24.1.12).
18361xe 4 24 0
18362xe 3 14 0
18363ximm 0 2048
18364xrr 57 13 0
18365xer 0 8 4
18366xp 1 0 72
18367xrr 133 0 0
18368p = xbranch 0
18369ximm 0 0
18370xp 2 0 80
18371ximm 2 0
18372xp 0 8 80
18373xp 4 24 88
18374xrr 133 0 0
18375xer 0 1 6
18376xp 1 0 80
18377xp 1 2 96
18378xrr 57 0 2
18379xer 0 1 6
18380ximm 0 0
18381xp 2 0 72
18382xpatch p get oc 0
18383xrr 137 6 12
18384xp 1 7 96
18385xrr 137 1 13
18386emitb 252
18387emitb 243
18388emitb 164
18389ximm 2 0
18390xrr 137 8 13
18391xp 1 9 96
18392ximm 0 0
18393xmem 137 0 4 32
18394xmem 137 0 4 40
18395xmem 137 0 4 48
18396xp 4 24 96
18397xrr 133 0 0
18398p = xbranch 1
18399ximm 2 1
18400xp 2 2 72
18401xpatch p get oc 0
18402ret
18403end
18404if eq op 9
18405xe 3 32 0
18406xp 1 1 40
18407xrr 133 1 1
18408q = xbranch 0
18409xp 0 2 256
18410xp 4 40 8
18411xrr 133 0 0
18412p = xbranch 1
18413xp 0 2 256
18414ximm 0 1
18415xmem 137 0 12 0
18416xhraw 0 2 12 1
18417xhraw 1 2 13 1
18418xb 72 193 225
18419emitb 1
18420xrr 9 0 1
18421xhput 0 12 4 4
18422i = ulong 0
18423while lt i 3
18424note MOVSXD rax,dword [rdx+disp32], Intel MOVSX/MOVSXD.
18425xb 72 99 130
18426xword mul i 4
18427xmem 137 0 12 add 8 mul i 8
18428i = add i 1
18429end
18430ximm 0 0
18431xpatch p get oc 0
18432xer 1 0 0
18433xpatch q get oc 0
18434xe 4 48 0
18435xp 0 2 256
18436xp 4 48 8
18437xrr 133 0 0
18438p = xbranch 1
18439xp 0 2 256
18440ximm 0 2
18441xmem 137 0 12 0
18442xhraw 0 2 24 4
18443xhput 0 12 4 4
18444i = ulong 0
18445while lt i 3
18446xmem 139 0 2 mul i 8
18447xmem 137 0 12 add 8 mul i 8
18448i = add i 1
18449end
18450ximm 0 0
18451xpatch p get oc 0
18452ret
18453end
18454if ne op 13
18455fail 25
18456end
18457note efi 13: write A[OFF..OFF+N) to ConOut (UEFI 12.4 OutputString). ARG must be 0; only bytes 9, 10,
18458note 13 and 32..126, all checked before any output; LF becomes CR LF; UCS-2 chunks of at most 255
18459note characters in private 4096. Warnings (bit 63 clear) are not errors. Result N.
18460xrr 133 3 3
18461xer 0 1 32
18462xrr 137 8 12
18463xrr 137 9 13
18464lp = get oc 0
18465xrr 133 9 9
18466p = xbranch 0
18467xhraw 0 8 0 1
18468ximm 2 9
18469xrr 57 0 2
18470q = xbranch 0
18471ximm 2 10
18472xrr 57 0 2
18473rw = xbranch 0
18474ximm 2 13
18475xrr 57 0 2
18476s = xbranch 0
18477ximm 2 32
18478xrr 57 0 2
18479xer 0 3 42
18480ximm 2 126
18481xrr 57 0 2
18482xer 0 8 42
18483xpatch q get oc 0
18484xpatch rw get oc 0
18485xpatch s get oc 0
18486xb 73 131 192
18487emitb 1
18488xb 73 131 233
18489emitb 1
18490back lp
18491xpatch p get oc 0
18492note ConOut (SystemTable+64) may be NULL on headless firmware: refuse with 43 before any call.
18493xp 1 1 0
18494xmem 139 1 1 64
18495xrr 133 1 1
18496xer 0 0 43
18497xrr 137 6 12
18498xrr 137 3 13
18499ximm 7 0
18500lp = get oc 0
18501xrr 133 3 3
18502p = xbranch 0
18503ximm 0 254
18504xrr 57 7 0
18505q = xbranch 3
18506xpatch p get oc 0
18507xrr 133 7 7
18508rw = xbranch 0
18509xp 0 10 4096
18510xrr 137 11 7
18511xrr 1 11 11
18512xrr 1 10 11
18513ximm 0 0
18514xhput 0 10 0 2
18515xp 1 1 0
18516xmem 139 1 1 64
18517xp 0 2 4096
18518xmem 139 11 1 8
18519xb 65 255 211
18520xrr 133 0 0
18521xer 0 11 43
18522ximm 7 0
18523xpatch rw get oc 0
18524xrr 133 3 3
18525s = xbranch 0
18526xpatch q get oc 0
18527xp 0 10 4096
18528xrr 137 11 7
18529xrr 1 11 11
18530xrr 1 10 11
18531xhraw 0 6 0 1
18532ximm 2 10
18533xrr 57 0 2
18534p = xbranch 1
18535ximm 0 13
18536xhput 0 10 0 2
18537ximm 0 10
18538xhput 0 10 2 2
18539xb 72 131 199
18540emitb 1
18541q = xbranch 14
18542xpatch p get oc 0
18543xhput 0 10 0 2
18544xpatch q get oc 0
18545xb 72 131 199
18546emitb 1
18547xb 72 131 198
18548emitb 1
18549xb 72 131 235
18550emitb 1
18551back lp
18552xpatch s get oc 0
18553xrr 137 0 13
18554end
18555
18556note UEFI 24.1 SNP mode: MAC array capacity 16, 32-byte MAC cells, current@552, IfType@648,
18557note MediaPresentSupported@651, MediaPresent@652. k 0 probe candidate 128 (own error scope);
18558note k 1 reject it: shut down/stop only what this application initialized/started.
18559fn xenpr k ulong to void
18560if eq k 1
18561xp 1 0 144
18562xrr 133 0 0
18563p = xbranch 0
18564xp 4 128 40
18565xpatch p get oc 0
18566xp 1 0 136
18567xrr 133 0 0
18568p = xbranch 0
18569xp 4 128 16
18570xpatch p get oc 0
18571ret
18572end
18573put xenerr 0 0
18574put xenend 0 0
18575ximm 0 0
18576xp 2 0 136
18577xp 2 0 144
18578xp 1 1 128
18579xmem 139 2 1 120
18580xrr 133 2 2
18581xer 0 0 3
18582xhraw 0 2 4 4
18583ximm 8 6
18584xrr 57 0 8
18585xer 0 1 3
18586xhraw 0 2 648 1
18587ximm 8 1
18588xrr 57 0 8
18589xer 0 1 3
18590xhraw 0 2 0 4
18591xrr 133 0 0
18592q = xbranch 1
18593xp 4 128 8
18594xrr 133 0 0
18595xer 0 1 7
18596ximm 0 1
18597xp 2 0 136
18598xpatch q get oc 0
18599xp 1 1 128
18600xmem 139 2 1 120
18601xhraw 0 2 0 4
18602ximm 8 1
18603xrr 57 0 8
18604q = xbranch 1
18605ximm 2 0
18606ximm 8 0
18607xp 4 128 24
18608xrr 133 0 0
18609xer 0 1 7
18610ximm 0 1
18611xp 2 0 144
18612xpatch q get oc 0
18613ximm 2 0
18614ximm 8 0
18615xp 4 128 88
18616xrr 133 0 0
18617xer 0 1 7
18618xp 1 1 128
18619xmem 139 2 1 120
18620xhraw 0 2 0 4
18621ximm 8 2
18622xrr 57 0 8
18623xer 0 1 6
18624note MediaPresent is binding only when the driver says media detection is supported.
18625xhraw 0 2 651 1
18626xrr 133 0 0
18627q = xbranch 0
18628xhraw 0 2 652 1
18629xrr 133 0 0
18630xer 0 0 12
18631xpatch q get oc 0
18632xhraw 0 2 24 4
18633ximm 8 5
18634xrr 33 0 8
18635xrr 57 0 8
18636xer 0 1 3
18637ximm 2 5
18638ximm 8 0
18639ximm 9 0
18640ximm 0 0
18641xmem 137 0 4 32
18642xmem 137 0 4 40
18643xp 4 128 48
18644xe 2 0 0
18645end
18646
18647note UEFI 2.4 7.3 OpenProtocol EXCLUSIVE (32) disconnects MNP and the IP stack above SNP; while they
18648note stay bound, MNP's poll timer drains frames no child wants (aienux#454). Firmware that refuses
18649note keeps the shared HandleProtocol path. All candidates are tried for a live link.
18650fn xenscan g ulong to void
18651ximm 3 0
18652lp = get oc 0
18653xrr 57 3 7
18654ex = xbranch 2
18655xrr 137 0 3
18656xb 72 193 224
18657emitb 3
18658xrr 1 0 6
18659xmem 139 1 0 0
18660xaddr 2 g
18661xp 0 8 128
18662xp 1 9 8
18663ximm 0 0
18664xmem 137 0 4 32
18665ximm 0 32
18666xmem 137 0 4 40
18667xp 3 0 280
18668xrr 133 0 0
18669p = xbranch 0
18670xrr 137 0 3
18671xb 72 193 224
18672emitb 3
18673xrr 1 0 6
18674xmem 139 1 0 0
18675xaddr 2 g
18676xp 0 8 128
18677xp 3 0 152
18678xpatch p get oc 0
18679xrr 133 0 0
18680sk = xbranch 1
18681xenpr 0
18682xrr 133 0 0
18683p = xbranch 1
18684xp 1 0 128
18685xp 2 0 24
18686sel = xbranch 14
18687xpatch p get oc 0
18688xenpr 1
18689xpatch sk get oc 0
18690xb 72 255 195
18691back lp
18692xpatch ex get oc 0
18693ximm 0 9223372036854775822
18694p = xbranch 14
18695xpatch sel get oc 0
18696ximm 0 0
18697xpatch p get oc 0
18698end
18699
18700note OP8: select a live Simple Network Protocol interface once (result 0), or an EFI status.
18701fn xenet to void
18702xp 1 1 24
18703xrr 133 1 1
18704al = xbranch 1
18705g = xg 1
18706ximm 1 2
18707xaddr 2 g
18708ximm 8 0
18709xp 0 9 104
18710xp 0 0 112
18711xmem 137 0 4 32
18712xp 3 0 312
18713xrr 133 0 0
18714lerr = xbranch 1
18715xp 1 6 112
18716xp 1 7 104
18717ximm 0 1024
18718xrr 57 7 0
18719lg = xbranch 8
18720xenscan g
18721sv = xbranch 14
18722xpatch lg get oc 0
18723ximm 0 9223372036854775813
18724xpatch sv get oc 0
18725xp 2 0 152
18726xrr 137 1 6
18727xp 3 0 72
18728xrr 133 0 0
18729fe = xbranch 1
18730xp 1 0 152
18731dn = xbranch 14
18732xpatch al get oc 0
18733ximm 0 0
18734xpatch dn get oc 0
18735xpatch lerr get oc 0
18736xpatch fe get oc 0
18737put xenerr 0 0
18738put xenend 0 0
18739end
18740
18741note Volumes (UEFI 13.4 Simple File System, 9.1 LoadedImage). k 0: copy the handles (at most 64) to
18742note private 1024 and free the firmware buffer (clobbers RSI/RDI/RCX); 1: result count | boot index<<8
18743note (255 none) and finish; 2: open the root of volume RDI into slot root (19 index, 21, 22);
18744note 3: open the root of the volume this image was loaded from into slot root (18, 21, 22).
18745fn xvol k ulong root ulong to void
18746if eq k 0
18747g = xg 5
18748ximm 1 2
18749xaddr 2 g
18750ximm 8 0
18751xp 0 9 512
18752xp 0 0 520
18753xmem 137 0 4 32
18754xp 3 0 312
18755xrr 133 0 0
18756xer 0 1 16
18757xp 1 1 512
18758ximm 0 64
18759xrr 57 1 0
18760p = xbranch 3
18761ximm 1 64
18762xpatch p get oc 0
18763xp 2 1 512
18764xp 1 6 520
18765xp 0 7 1024
18766emitb 252
18767emitb 243
18768emitb 72
18769emitb 165
18770xp 1 1 520
18771xp 3 0 72
18772xrr 133 0 0
18773xer 0 1 17
18774ret
18775end
18776if eq k 1
18777gl = xg 6
18778xp 1 1 8
18779xaddr 2 gl
18780xp 0 8 528
18781xp 3 0 152
18782xrr 133 0 0
18783xer 0 1 18
18784xp 1 0 528
18785xmem 139 0 0 24
18786xp 1 6 512
18787xp 0 7 1024
18788ximm 2 0
18789lp = get oc 0
18790xrr 57 2 6
18791nm = xbranch 2
18792xmem 139 1 7 0
18793xrr 57 1 0
18794fd = xbranch 0
18795xb 72 131 199
18796emitb 8
18797xb 72 255 194
18798back lp
18799xpatch nm get oc 0
18800ximm 2 255
18801xpatch fd get oc 0
18802xrr 137 0 6
18803xb 72 193 226
18804emitb 8
18805xrr 9 0 2
18806xer 1 0 0
18807ret
18808end
18809gs = xg 5
18810if eq k 3
18811gi = xg 6
18812xp 1 1 8
18813xaddr 2 gi
18814xp 0 8 528
18815xp 3 0 152
18816xrr 133 0 0
18817xer 0 1 18
18818xp 1 1 528
18819xmem 139 1 1 24
18820else
18821xp 1 0 512
18822xrr 57 7 0
18823xer 0 2 19
18824xp 0 1 1024
18825xrr 137 0 7
18826xb 72 193 224
18827emitb 3
18828xrr 1 1 0
18829xmem 139 1 1 0
18830end
18831xaddr 2 gs
18832xp 0 8 536
18833xp 3 0 152
18834xrr 133 0 0
18835xer 0 1 21
18836xp 0 2 root
18837xp 4 536 8
18838xrr 133 0 0
18839xer 0 1 22
18840end
18841
18842note File I/O (UEFI 13.5). k 0: Open(root, path, mode) into slot file; the path is the fixed
18843note \EFI\BOOT\BOOTX64.EFI for root 544 (efi 11), else the UCS-2 copy at private 3584 (efi 12).
18844note k 1: efi 11 Read/Write (method mode) of N bytes at R12 via size slot 560. Failure: RDI = code,
18845note jump to cleanup.
18846fn xfio k ulong root ulong file ulong mode ulong code ulong cleanup ulong to void
18847if eq k 0
18848if eq root 544
18849p = xg 8
18850xaddr 8 p
18851else
18852xp 0 8 3584
18853end
18854xp 0 2 file
18855ximm 9 mode
18856ximm 0 0
18857xmem 137 0 4 32
18858xp 4 root 8
18859else
18860xp 2 13 560
18861xp 0 2 560
18862xrr 137 8 12
18863xp 4 552 mode
18864end
18865xrr 133 0 0
18866ximm 7 code
18867bad = xbranch 1
18868xpatch bad cleanup
18869end
18870
18871note OP11 (Allen fork contract): ARG sub bits0..7, volume bits8..15. Sub 0 counts the volumes and
18872note finds the boot one (count | index<<8); sub 1 reads \EFI\BOOT\BOOTX64.EFI of volume k into A
18873note (result bytes); sub 2 writes A's N bytes as that file (deleted first; result bytes). File and
18874note root handles are closed on every path.
18875fn xefile to void
18876xrr 137 0 3
18877ximm 2 255
18878xrr 33 0 2
18879ximm 2 2
18880xrr 57 0 2
18881xer 0 8 15
18882ximm 0 0
18883xp 2 0 544
18884xp 2 0 552
18885skip = xbranch 14
18886cleanup = get oc 0
18887xp 1 1 552
18888xrr 133 1 1
18889p = xbranch 0
18890xp 4 552 16
18891xpatch p get oc 0
18892xp 1 1 544
18893xrr 133 1 1
18894p = xbranch 0
18895xp 4 544 16
18896xpatch p get oc 0
18897xrr 133 7 7
18898p = xbranch 0
18899ximm 0 9223372036854775808
18900xrr 9 0 7
18901xer 1 0 0
18902xpatch p get oc 0
18903xp 1 0 568
18904xer 1 0 0
18905xpatch skip get oc 0
18906xvol 0 0
18907xrr 137 6 3
18908ximm 2 255
18909xrr 33 6 2
18910xrr 137 7 3
18911xb 72 193 239
18912emitb 8
18913xrr 33 7 2
18914xrr 133 6 6
18915p = xbranch 1
18916xvol 1 0
18917xpatch p get oc 0
18918xp 1 0 512
18919xrr 57 7 0
18920xer 0 2 19
18921xe 3 1 0
18922xvol 2 544
18923ximm 0 1
18924xrr 57 6 0
18925wr = xbranch 1
18926xfio 0 544 552 1 23 cleanup
18927xfio 1 0 0 32 25 cleanup
18928xp 1 0 560
18929xp 2 0 568
18930ximm 0 1
18931xp 2 0 560
18932xp 0 2 560
18933xp 0 8 576
18934xp 4 552 32
18935xrr 133 0 0
18936ximm 7 25
18937p = xbranch 1
18938xpatch p cleanup
18939xp 1 0 560
18940xrr 133 0 0
18941ximm 7 24
18942p = xbranch 1
18943xpatch p cleanup
18944ximm 7 0
18945p = xbranch 14
18946xpatch p cleanup
18947xpatch wr get oc 0
18948xfio 0 544 552 3 26 cleanup
18949xp 4 552 24
18950ximm 2 0
18951xp 2 2 552
18952xrr 133 0 0
18953ximm 7 27
18954p = xbranch 1
18955xpatch p cleanup
18956xfio 0 544 552 9223372036854775811 28 cleanup
18957xfio 1 0 0 40 29 cleanup
18958xp 1 0 560
18959xrr 57 0 13
18960ximm 7 30
18961p = xbranch 1
18962xpatch p cleanup
18963xp 4 552 80
18964xrr 133 0 0
18965ximm 7 31
18966p = xbranch 1
18967xpatch p cleanup
18968xp 1 0 560
18969xp 2 0 568
18970ximm 7 0
18971p = xbranch 14
18972xpatch p cleanup
18973end
18974
18975note OP12 general file (new): ARG sub bits0..7, volume bits8..15 (255 = the volume this image was
18976note loaded from), position bits16..63. Open subs take the path A[OFF..OFF+N): 1..255 bytes, first
18977note '\', each 32..126. Sub 0 open read-only, 5 open an existing file read/write (no truncate), both
18978note result FileSize; 1 create empty, replacing any file (result 0). Sub 2 reads up to N bytes at
18979note the position into A (result bytes read, 0 at end), 3 writes N bytes there (result N; needs sub
18980note 1 or 5), 4 flushes and closes (result 0). One file at a time: its handle stays in private 2304
18981note and its volume root in 2312 between operations. Errors: 32 sub, 33 path, 34 busy, 35 nothing
18982note open, 36 open/create/delete, 37 read, 38 write (or not writable), 39 short write, 40 flush,
18983note 41 GetInfo; volume errors 16..22 as efi 11. Refusals 32..35 and 38 not-writable leave the slot
18984note as it was (the caller still owns an open file); so do the firmware errors 37, 38 and 39 of subs
18985note 2 and 3. Volume errors leave the slot empty. 36 and 41 close the file and root, slot empty;
18986note sub 4 with a file open closes both and empties the slot on every path, returning 40 when Flush
18987note failed. A file still open at exit is flushed and closed by the shared exit (xefx 1).
18988fn xf12 to void
18989skip = xbranch 14
18990cleanup = get oc 0
18991xp 1 1 2304
18992xrr 133 1 1
18993p = xbranch 0
18994xp 4 2304 16
18995xpatch p get oc 0
18996xp 1 1 2312
18997xrr 133 1 1
18998p = xbranch 0
18999xp 4 2312 16
19000xpatch p get oc 0
19001ximm 0 0
19002xp 2 0 2304
19003xp 2 0 2312
19004xp 2 0 2320
19005xrr 133 7 7
19006p = xbranch 0
19007ximm 0 9223372036854775808
19008xrr 9 0 7
19009xer 1 0 0
19010xpatch p get oc 0
19011xer 1 0 0
19012xpatch skip get oc 0
19013xrr 137 6 3
19014ximm 0 255
19015xrr 33 6 0
19016ximm 0 5
19017xrr 57 6 0
19018xer 0 8 32
19019ximm 0 2
19020xrr 57 6 0
19021opn = xbranch 3
19022ximm 0 5
19023xrr 57 6 0
19024op5 = xbranch 0
19025xp 1 1 2304
19026xrr 133 1 1
19027xer 0 0 35
19028ximm 0 4
19029xrr 57 6 0
19030p = xbranch 1
19031ximm 7 0
19032xp 1 0 2320
19033xrr 133 0 0
19034q = xbranch 0
19035xp 4 2304 80
19036xrr 133 0 0
19037rw = xbranch 0
19038ximm 7 40
19039xpatch rw get oc 0
19040xpatch q get oc 0
19041ximm 0 0
19042rw = xbranch 14
19043xpatch rw cleanup
19044xpatch p get oc 0
19045xrr 137 2 3
19046xb 72 193 234
19047emitb 16
19048ximm 0 3
19049xrr 57 6 0
19050p = xbranch 0
19051xp 4 2304 56
19052xrr 133 0 0
19053xer 0 1 37
19054xp 2 13 2336
19055xp 0 2 2336
19056xrr 137 8 12
19057xp 4 2304 32
19058xrr 133 0 0
19059xer 0 1 37
19060xp 1 0 2336
19061xer 1 0 0
19062xpatch p get oc 0
19063xp 1 0 2320
19064xrr 133 0 0
19065xer 0 0 38
19066xp 4 2304 56
19067xrr 133 0 0
19068xer 0 1 38
19069xp 2 13 2336
19070xp 0 2 2336
19071xrr 137 8 12
19072xp 4 2304 40
19073xrr 133 0 0
19074xer 0 1 38
19075xp 1 0 2336
19076xrr 57 0 13
19077xer 0 1 39
19078xrr 137 0 13
19079xer 1 0 0
19080xpatch opn get oc 0
19081xpatch op5 get oc 0
19082xp 1 1 2304
19083xrr 133 1 1
19084xer 0 1 34
19085xrr 133 13 13
19086xer 0 0 33
19087ximm 0 255
19088xrr 57 13 0
19089xer 0 8 33
19090xhraw 0 12 0 1
19091ximm 2 92
19092xrr 57 0 2
19093xer 0 1 33
19094xrr 137 8 12
19095xrr 137 9 13
19096xp 0 10 3584
19097lp = get oc 0
19098xrr 133 9 9
19099p = xbranch 0
19100xhraw 0 8 0 1
19101ximm 2 32
19102xrr 57 0 2
19103xer 0 3 33
19104ximm 2 126
19105xrr 57 0 2
19106xer 0 8 33
19107xhput 0 10 0 2
19108xb 73 131 192
19109emitb 1
19110xb 73 131 194
19111emitb 2
19112xb 73 131 233
19113emitb 1
19114back lp
19115xpatch p get oc 0
19116ximm 0 0
19117xhput 0 10 0 2
19118xrr 137 0 3
19119ximm 2 65280
19120xrr 33 0 2
19121xrr 57 0 2
19122p = xbranch 0
19123xvol 0 0
19124xrr 137 7 3
19125xb 72 193 239
19126emitb 8
19127ximm 2 255
19128xrr 33 7 2
19129xvol 2 2312
19130q = xbranch 14
19131xpatch p get oc 0
19132xvol 3 2312
19133xpatch q get oc 0
19134xrr 137 6 3
19135ximm 0 255
19136xrr 33 6 0
19137ximm 0 1
19138xrr 57 6 0
19139p = xbranch 1
19140note Sub 1: delete an existing file first (UEFI 13.5 Delete closes the handle), then create.
19141xp 0 8 3584
19142xp 0 2 2304
19143ximm 9 3
19144ximm 0 0
19145xmem 137 0 4 32
19146xp 4 2312 8
19147xrr 133 0 0
19148q = xbranch 1
19149xp 4 2304 24
19150ximm 2 0
19151xp 2 2 2304
19152xrr 133 0 0
19153ximm 7 36
19154rw = xbranch 1
19155xpatch rw cleanup
19156xpatch q get oc 0
19157ximm 2 0
19158xp 2 2 2304
19159xfio 0 2312 2304 9223372036854775811 36 cleanup
19160ximm 0 1
19161xp 2 0 2320
19162ximm 0 0
19163xer 1 0 0
19164xpatch p get oc 0
19165ximm 0 5
19166xrr 57 6 0
19167p = xbranch 0
19168xfio 0 2312 2304 1 36 cleanup
19169ximm 0 0
19170xp 2 0 2320
19171q = xbranch 14
19172xpatch p get oc 0
19173xfio 0 2312 2304 3 36 cleanup
19174ximm 0 1
19175xp 2 0 2320
19176xpatch q get oc 0
19177ximm 0 1024
19178xp 2 0 2336
19179gi = xg 7
19180xaddr 2 gi
19181xp 0 8 2336
19182xp 0 9 2560
19183xp 4 2304 64
19184xrr 133 0 0
19185ximm 7 41
19186p = xbranch 1
19187xpatch p cleanup
19188xp 1 0 2568
19189xp 2 0 2328
19190end
19191
19192