interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / src / ir.om

2104 lines · 48978 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note seed/ng/src/ir.om: ΩIR in memory (seed/ng/IR.md s14), its builder, and the one binary reader
4note and writer of the ng compiler. Owner: ng-ir lane. First unit of seed/ng/ng.units; no fn main.
5note Names: functions ir_*, globals i_* (D1b). Other lanes read the i_* tables and call ir_*; they
6note never write a table directly. Handles are uint, 0 = none, numbered from 1 in creation order.
7note
8note Row table: one line per op, "OP NAME TK IMM FIX VAR TY" (IR.md s7.12). TK 0 P, 1 E, 2 R, 3 T.
9note TY 0 always 0, 1 result type, 2 optional result, 3 parameter (no result value).
10text i_rows "1 const 0 1 0 0 1\n2 select 0 0 3 0 1\n10 add 0 0 2 0 1\n11 sub 0 0 2 0 1\n12 mul 0 0 2 0 1\n13 div 0 0 2 0 1\n14 rem 0 0 2 0 1\n15 neg 0 0 1 0 1\n16 and 0 0 2 0 1\n17 or 0 0 2 0 1\n18 xor 0 0 2 0 1\n19 not 0 0 1 0 1\n20 shl 0 0 2 0 1\n21 shr 0 0 2 0 1\n22 rotl 0 0 2 0 1\n23 rotr 0 0 2 0 1\n24 min 0 0 2 0 1\n25 max 0 0 2 0 1\n26 fsqrt 0 0 1 0 1\n27 fabs 0 0 1 0 1\n28 fmath 0 1 1 0 1\n30 eq 0 0 2 0 1\n31 ne 0 0 2 0 1\n32 lt 0 0 2 0 1\n33 le 0 0 2 0 1\n34 gt 0 0 2 0 1\n35 ge 0 0 2 0 1\n36 conv 0 1 1 0 1\n37 bitcast 0 0 1 0 1\n40 gpu.id 0 1 0 0 1\n41 shared 0 0 0 0 1\n42 load 2 0 2 0 1\n43 store 1 0 3 0 0\n44 sync 1 0 0 0 0\n45 shfl 0 1 2 0 1\n46 fma 0 0 3 0 1\n47 atom 1 1 3 0 1\n48 atomcas 1 1 4 0 1\n50 alloca 1 0 0 0 1\n51 fill 1 0 2 0 0\n52 gaddr 0 1 0 0 1\n53 len 0 0 1 0 1\n54 slice 2 0 3 0 1\n55 fload 2 1 1 0 1\n56 fstore 1 1 2 0 0\n57 fref 0 1 1 0 1\n58 vload 2 0 2 0 1\n59 vstore 1 0 3 0 0\n60 br 3 0 1 1 0\n61 cbr 3 0 3 0 0\n62 ret 3 0 0 0 0\n63 retv 3 0 1 0 0\n64 call 1 0 1 1 2\n65 trap 3 1 0 0 0\n66 exit 3 0 1 0 0\n67 launch 1 0 4 1 0\n70 chk.add 1 1 2 0 0\n71 chk.sub 1 1 2 0 0\n72 chk.mul 1 1 2 0 0\n73 chk.neg 1 1 1 0 0\n74 chk.divz 1 1 1 0 0\n75 chk.divovf 1 1 2 0 0\n76 chk.shl 1 1 2 0 0\n77 chk.shift 1 1 1 0 0\n78 chk.conv 1 1 1 0 3\n79 chk.bounds 1 1 3 0 0\n80 chk.slice 1 1 3 0 0\n81 chk.fuel 1 1 2 0 0\n82 chk.true 1 1 1 0 0\n90 fact.true 0 1 1 0 0\n91 fact.range 0 1 3 0 0\n92 fact.inbounds 0 1 3 0 0\n93 fact.slice 0 1 3 0 0\n100 vsplat 0 0 1 0 1\n101 vextract 0 1 1 0 1\n102 vinsert 0 1 2 0 1\n103 vreduce 0 1 1 0 1\n104 mmald 2 1 4 0 1\n105 mmast 1 1 5 0 0\n106 mma 0 1 3 0 1\n110 host 1 1 0 1 2\n111 pool.grow 1 1 1 0 1\n112 pool.len 2 1 0 0 1\n113 pool.load 2 1 1 0 1\n114 pool.store 1 1 2 0 0\n115 pool.read 1 1 3 0 1\n116 pool.write 1 1 3 0 1\n"
11text i_magic "OSC1IR"
12text i_tag "omega.ng.ir.v1"
13global i_rok uchar 256
14global i_rtk uchar 256
15global i_rim uchar 256
16global i_rfx uchar 256
17global i_rvr uchar 256
18global i_rty uchar 256
19global i_rno uint 256
20global i_rnl uchar 256
21global i_rp ulong 1
22note counters: 0 table types, 1 struct fields, 2 globals, 3 functions, 4 blocks, 5 ops, 6 values,
23note 7 operand slots, 8 global data bytes, 15 row table parsed
24global i_n ulong 16
25global i_sp ulong 4
26global i_tq uint 8
27global i_tk uchar 65568
28global i_t1 uint 65568
29global i_t2 uint 65568
30global i_t3 uint 65568
31global i_t4 uint 65568
32global i_t5 uint 65568
33global i_xty uint 65536
34global i_xof uint 65536
35global i_xor uchar 65536
36global i_fk uchar 65537
37global i_ffl uchar 65537
38global i_frs uint 65537
39global i_fnp uint 65537
40global i_ftk uint 65537
41global i_fp1 uint 65537
42global i_fpt uint 65537
43global i_fb1 uint 65537
44global i_fbt uint 65537
45global i_fnb uint 65537
46global i_ftx uint 65537
47global i_fty uint 65537
48global i_ftz uint 65537
49global i_fsg uchar 65537
50global i_bf uint 1048577
51global i_bnx uint 1048577
52global i_bp1 uint 1048577
53global i_bpt uint 1048577
54global i_bnp uint 1048577
55global i_bo1 uint 1048577
56global i_bot uint 1048577
57global i_bno uint 1048577
58global i_bix uint 1048577
59global i_oop uchar 4194305
60global i_oty uint 4194305
61global i_oim ulong 4194305
62global i_ona uint 4194305
63global i_oa0 uint 4194305
64global i_ob uint 4194305
65global i_onx uint 4194305
66global i_otk uint 4194305
67global i_ors uint 4194305
68global i_oof uint 4194305
69global i_arg uint 16777216
70global i_vty uint 8388609
71global i_vk uchar 8388609
72global i_vo uint 8388609
73global i_vnx uint 8388609
74global i_vno uint 8388609
75note ---- the arena (no table capacities; Drake 2026-11-11, OMEGESE 9.6; port of omc.om's a_* unit) -------
76note Every ng table whose size depends on the input is a region of one pool, ar_m (16777216 words, 128 MiB):
77note the payload (front input or ΩIR binary), the output, ΩIR global data and globals, the front's names,
78note long names, functions, calls, blocks, breaks, continues, inline edges, value-depth cells, call-graph
79note order, om2 diag site records and the front-ir lowering tables, and a64's global addresses. A region
80note starts empty and doubles when an index reaches its group's capacity (ar_need); when the moved copies
81note do not fit, the arena is compacted and the group grows in place to the largest size that fits, so the
82note only capacity left is the arena itself. Running out of it is a refusal of the stage that asked
83note (front error 14, a reader's LIMIT class 23, a writer's `ng: arena full`) with an ng-use record on
84note stderr naming every group and the words it held (ar_rec). What bounds the arena: ng has no run-time
85note growth (no heap, decision 24); one pool is at most 16777216 elements (OMEGESE 9.4), so 128 MiB of
86note ulong words; its pages are never resident until touched. It lives in ir.om because every ng tool is
87note composed with ir.om first (ng, ngrun, ngrand and the oracles), so each has the same arena.
88note Regions (ar_defs): ar_rb base word, ar_rw words held, ar_rg group, ar_rm words per group index (0: a
89note byte region, eight bytes to a word). Groups: ar_gc capacity, ar_gp highest index asked for, plus 1.
90note ar_top words handed out, ar_wst words left behind by moves, ar_fl 1 once the arena was full, ar_nr
91note regions defined, ar_wb staging bytes of read and write. Element i of a word region R is
92note `get ar_m add get ar_rb R i` (a uchar or uint region keeps one element per word, read through a
93note conversion and written by ar_pc / ar_pu); an index below the group capacity holds by construction
94note at every such access (the count that bounds it was asked of ar_need first). Where an index comes from
95note input it is checked (ar_g, ar_p): past its region is a fault of ng itself and traps 3, never another
96note region's word.
97global ar_m ulong 16777216
98global ar_rb ulong 160
99global ar_rw ulong 160
100global ar_rg ulong 160
101global ar_rm ulong 160
102global ar_gc ulong 48
103global ar_gp ulong 48
104global ar_top ulong 1
105global ar_wst ulong 1
106global ar_fl ulong 1
107global ar_nr ulong 1
108global ar_ord ulong 160
109global ar_np ulong 160
110global ar_wb uchar 65536
111global ar_rbuf uchar 4096
112global ar_rc ulong 1
113note ar_inl: bytes read into region 0 by ar_rdin (also while it reads, so a full arena's record shows them)
114global ar_inl ulong 1
115note ar_oc: bytes of region 1 (the output) written so far, for the record
116global ar_oc ulong 1
117note ar_gl (generated with ar_defs): the group labels of the ng-use record, each ended by a space
118text ar_gl "in out gdat site names namehash long longhash fns fnhash calls blocks breaks conts inledge depth kahnfn kahncall lnames lnamehash lstack lsnap laq lca irglob a64glob a64val a64live a64call a64code a64fix "
119
120note ar_def: region r is in group g with m words per group index (0: a byte region)
121fn ar_def r ulong g ulong m ulong to void
122put ar_rg r g
123put ar_rm r m
124if ge r get ar_nr 0
125put ar_nr 0 add r 1
126end
127end
128
129note ar_words: the words region r takes at group capacity c
130fn ar_words r ulong c ulong to ulong
131m = get ar_rm r
132if eq m 0
133ret shr add c 7 3
134end
135ret mul c m
136end
137
138note ar_move: room in region r for group capacity c. The last region handed out grows in place; any other
139note moves to the top with its words copied, and the words it leaves behind are counted in ar_wst. Words
140note past ar_top were never handed out and are 0. ar_need has checked that the group fits first, so the two
141note full tests below are never met (they stay as a defence: 1 is returned and nothing moves).
142fn ar_move r ulong c ulong to ulong
143w = ar_words r c
144o = get ar_rw r
145if le w o
146ret 0
147end
148b = get ar_rb r
149t = get ar_top 0
150if and ne o 0 eq add b o t
151if gt sub w o sub len ar_m t
152ret 1
153end
154put ar_top 0 add b w
155put ar_rw r w
156ret 0
157end
158if gt w sub len ar_m t
159ret 1
160end
161ar_copy t b o
162put ar_wst 0 add get ar_wst 0 o
163put ar_rb r t
164put ar_rw r w
165put ar_top 0 add t w
166ret 0
167end
168
169note ar_copy: words s .. s + n - 1 to d .. d + n - 1 in ascending order (d above s + n, or d below s)
170fn ar_copy d ulong s ulong n ulong to void
171i = ulong 0
172while lt i n bound 16777217
173put ar_m add d i get ar_m add s i
174i = add i 1
175end
176end
177
178note ar_copyd: words s .. s + n - 1 to d .. d + n - 1 in descending order (d above s: a slide up)
179fn ar_copyd d ulong s ulong n ulong to void
180i = n
181while gt i 0 bound 16777217
182i = sub i 1
183put ar_m add d i get ar_m add s i
184end
185end
186
187note ar_clear: words a .. b - 1 become 0
188fn ar_clear a ulong b ulong to void
189i = a
190while lt i b bound 16777217
191put ar_m i 0
192i = add i 1
193end
194end
195
196note ar_need: group g is about to use index n; 0 when it may, 1 when the arena has no room (ar_fl set,
197note nothing changed but a compaction). Below the group's capacity nothing happens; at or above it every
198note region of the group grows to twice the capacity (at least n + 1, at least 64), moving to the top when
199note not there already. When the moved copies do not fit in the free words, the arena is compacted (the
200note words moved regions left behind are reclaimed) and the group grows in place (ar_relayout), to twice
201note when that fits, else to the largest capacity that does (ar_gfit). So a table is refused only when
202note the live tables with its group grown by one entry no longer fit in the arena.
203fn ar_need g ulong n ulong to ulong
204if eq get ar_nr 0 0
205ar_defs
206end
207if ge n get ar_gp g
208put ar_gp g add n 1
209end
210c = get ar_gc g
211if lt n c
212ret 0
213end
214c = shl c 1
215if le c n
216c = add n 1
217end
218if lt c 64
219c = ulong 64
220end
221if gt ar_gneed g c sub len ar_m get ar_top 0
222ar_compact
223if gt ar_gextra g c sub len ar_m get ar_top 0
224c = ar_gfit g n c
225if eq c 0
226put ar_fl 0 1
227ret 1
228end
229end
230ar_relayout g c
231put ar_gc g c
232ret 0
233end
234r = ulong 0
235while lt r get ar_nr 0 bound 161
236if eq get ar_rg r g
237if ne ar_move r c 0
238put ar_fl 0 1
239ret 1
240end
241end
242r = add r 1
243end
244put ar_gc g c
245ret 0
246end
247
248note ar_gneed: the words group g's regions take at capacity c, counted as if every one of them moved
249fn ar_gneed g ulong c ulong to ulong
250s = ulong 0
251r = ulong 0
252while lt r get ar_nr 0 bound 161
253if eq get ar_rg r g
254s = add s ar_words r c
255end
256r = add r 1
257end
258ret s
259end
260
261note ar_gextra: the words group g's regions need beyond the words they hold, at capacity c
262fn ar_gextra g ulong c ulong to ulong
263s = ulong 0
264r = ulong 0
265while lt r get ar_nr 0 bound 161
266if eq get ar_rg r g
267w = ar_words r c
268if gt w get ar_rw r
269s = add s sub w get ar_rw r
270end
271end
272r = add r 1
273end
274ret s
275end
276
277note ar_gfit: when doubling does not fit, the capacity group g grows to in place: the largest in n + 1 .. hi whose
278note growth takes at most half the free words (binary search; the need grows with the capacity), so the other
279note groups keep room to grow (omc's a_gfit takes all of it; ng's a64 stage grows several groups per function),
280note else n + 1 when that alone fits in the free words, else 0 (the arena is full)
281fn ar_gfit g ulong n ulong hi ulong to ulong
282f = sub len ar_m get ar_top 0
283lo = add n 1
284if gt ar_gextra g lo f
285ret 0
286end
287h = shr f 1
288if gt ar_gextra g lo h
289ret lo
290end
291while gt sub hi lo 1 bound 64
292mid = add lo shr sub hi lo 1
293if gt ar_gextra g mid h
294hi = mid
295else
296lo = mid
297end
298end
299ret lo
300end
301
302note ar_lowest: index + 1 of the region holding words with the lowest base above lb (any base when any is
303note false), or 0 when none is left
304fn ar_lowest any bool lb ulong to ulong
305best = ulong 0
306bb = ulong 0
307r = ulong 0
308while lt r get ar_nr 0 bound 161
309if ne get ar_rw r 0
310b = get ar_rb r
311if or not any gt b lb
312if or eq best 0 lt b bb
313best = add r 1
314bb = b
315end
316end
317end
318r = add r 1
319end
320ret best
321end
322
323note ar_compact: slide every region down over the words moved regions left behind, in order of base, so
324note the free words are one run at the top again; the words above the new top become 0. Region 0, the
325note payload, has base 0 and stays there.
326fn ar_compact to void
327t = get ar_top 0
328pos = ulong 0
329lb = ulong 0
330any = false
331k = ulong 0
332while lt k 161 bound 162
333best = ar_lowest any lb
334if eq best 0
335k = ulong 161
336else
337r = sub best 1
338b = get ar_rb r
339w = get ar_rw r
340if ne b pos
341ar_copy pos b w
342put ar_rb r pos
343end
344pos = add pos w
345lb = b
346any = true
347k = add k 1
348end
349end
350ar_clear pos t
351put ar_top 0 pos
352put ar_wst 0 0
353end
354
355note ar_relayout: after ar_compact (regions contiguous from word 0 in order of base), give group g's
356note regions their words at capacity c in place: new places in the same order, every region at or above
357note its old place, moved from the top down (ar_copyd), each new tail zeroed. A region of g that holds no
358note words yet goes at the top.
359fn ar_relayout g ulong c ulong to void
360k = ulong 0
361lb = ulong 0
362any = false
363go = true
364while go bound 162
365best = ar_lowest any lb
366if eq best 0
367go = false
368else
369r = sub best 1
370put ar_ord k r
371k = add k 1
372lb = get ar_rb r
373any = true
374end
375end
376r = ulong 0
377while lt r get ar_nr 0 bound 161
378if and eq get ar_rg r g eq get ar_rw r 0
379put ar_ord k r
380k = add k 1
381end
382r = add r 1
383end
384pos = ulong 0
385i = ulong 0
386while lt i k bound 161
387r = get ar_ord i
388put ar_np i pos
389w = get ar_rw r
390if eq get ar_rg r g
391w = ar_words r c
392end
393pos = add pos w
394i = add i 1
395end
396i = k
397while gt i 0 bound 161
398i = sub i 1
399r = get ar_ord i
400np = get ar_np i
401b = get ar_rb r
402w = get ar_rw r
403if and ne np b ne w 0
404ar_copyd np b w
405end
406put ar_rb r np
407if eq get ar_rg r g
408nw = ar_words r c
409if gt nw w
410ar_clear add np w add np nw
411put ar_rw r nw
412end
413end
414end
415put ar_top 0 pos
416end
417
418note ar_zero: the first k words of region r become 0
419fn ar_zero r ulong k ulong to void
420b = get ar_rb r
421ar_clear b add b k
422end
423
424note ar_gzero: every word of every region of group g becomes 0 (a hash table about to be rebuilt)
425fn ar_gzero g ulong to void
426r = ulong 0
427while lt r get ar_nr 0 bound 161
428if eq get ar_rg r g
429ar_zero r get ar_rw r
430end
431r = add r 1
432end
433end
434
435note ar_hash: the b-bit multiplicative hash of w (2^64 divided by the golden ratio, odd)
436fn ar_hash w ulong b ulong to ulong
437ret shr mul w 11400714819323198485 sub 64 b
438end
439
440note ar_oob: an index past its region is a fault of ng itself, never an input's: trap 3 (the pool check on
441note ar_rb at index 160 + i, always past its 160), as an index past a fixed pool trapped
442fn ar_oob i ulong to void
443z = get ar_rb add 160 i
444end
445
446note ar_g / ar_p: element i of word region r, read or written, checked against the region (indices that
447note come from input). ar_pc / ar_pu: element i of a uchar / uint region written (index by construction).
448fn ar_g r ulong i ulong to ulong
449if ge i get ar_rw r
450ar_oob i
451end
452ret get ar_m add get ar_rb r i
453end
454fn ar_p r ulong i ulong v ulong to void
455if ge i get ar_rw r
456ar_oob i
457end
458put ar_m add get ar_rb r i v
459end
460note ar_w / ar_c / ar_u: element i of a ulong / uchar / uint region read through a call: the form of a line
461note whose inline reads do not fit omc's 16-register expression pool (index by construction, unchecked)
462fn ar_w r ulong i ulong to ulong
463ret get ar_m add get ar_rb r i
464end
465fn ar_c r ulong i ulong to uchar
466ret uchar get ar_m add get ar_rb r i
467end
468fn ar_u r ulong i ulong to uint
469ret uint get ar_m add get ar_rb r i
470end
471fn ar_pw r ulong i ulong v ulong to void
472put ar_m add get ar_rb r i v
473end
474fn ar_pc r ulong i ulong v uchar to void
475put ar_m add get ar_rb r i ulong v
476end
477fn ar_pu r ulong i ulong v uint to void
478put ar_m add get ar_rb r i ulong v
479end
480
481note ar_ib: byte i of region 0, the payload. It is the first region placed, so it starts at word 0. Past its
482note words the byte is 0, as the zero pool past the input was.
483fn ar_ib i ulong to uchar
484x = shr i 3
485if ge x get ar_rw 0
486ret 0
487end
488ret uchar and shr get ar_m x shl and i 7 3 255
489end
490
491note ar_bg: byte i of byte region r (0 past its words)
492fn ar_bg r ulong i ulong to uchar
493x = shr i 3
494if ge x get ar_rw r
495ret 0
496end
497y = get ar_m add get ar_rb r x
498ret uchar and shr y shl and i 7 3 255
499end
500
501note ar_bp: byte i of byte region r becomes v. Every writer asked ar_need for the byte first, so the index is in
502note the region by construction and is not checked here (the omc owner measured 11 to 13% for checking every
503note arena access; checks stay where an index comes from input: ar_g, ar_p, ar_ib)
504fn ar_bp r ulong i ulong v uchar to void
505x = add get ar_rb r shr i 3
506s = shl and i 7 3
507y = and get ar_m x not shl ulong 255 s
508put ar_m x or y shl ulong v s
509end
510
511note ar_stage: bytes p .. p + c - 1 of the byte region at word b into ar_wb from 0 (c at most 65536). Bytes up to a
512note word boundary one at a time, then each whole word read once for its 8 bytes, then the last bytes
513fn ar_stage b ulong p ulong c ulong to void
514i = ulong 0
515while and lt i c ne and add p i 7 0 bound 8
516ar_stage1 b add p i i
517i = add i 1
518end
519while le add i 8 c bound 8193
520y = get ar_m add b shr add p i 3
521put ar_wb i uchar and y 255
522put ar_wb add i 1 uchar and shr y 8 255
523put ar_wb add i 2 uchar and shr y 16 255
524put ar_wb add i 3 uchar and shr y 24 255
525put ar_wb add i 4 uchar and shr y 32 255
526put ar_wb add i 5 uchar and shr y 40 255
527put ar_wb add i 6 uchar and shr y 48 255
528put ar_wb add i 7 uchar shr y 56
529i = add i 8
530end
531while lt i c bound 8
532ar_stage1 b add p i i
533i = add i 1
534end
535end
536
537note ar_stage1: byte x of the byte region at word b into ar_wb at i
538fn ar_stage1 b ulong x ulong i ulong to void
539y = get ar_m add b shr x 3
540put ar_wb i uchar and shr y shl and x 7 3 255
541end
542
543note ar_out: write bytes p .. e - 1 of byte region r to fd, 65536 at a time through ar_wb: 1 when every
544note byte was written, 0 when a write returned 0 or less
545fn ar_out fd ulong r ulong p ulong e ulong to ulong
546b = get ar_rb r
547q0 = p
548while lt q0 e bound 2049
549c = sub e q0
550if gt c 65536
551c = ulong 65536
552end
553ar_stage b q0 c
554q = ulong 0
555while lt q c bound 65537
556n = write int fd ar_wb q sub c q
557if le n 0
558ret 0
559end
560q = add q ulong n
561end
562q0 = add q0 c
563end
564ret 1
565end
566
567note ar_rdin: all of fd into region 0 (the payload), eight bytes to a word from word 0, before any other
568note region is placed: 0 done, 1 a read error, 2 the arena is full (ar_inl: the bytes read). The region
569note then keeps the words the bytes fill and 8 more (zero): the input bytes are read inline past their end
570note by at most that much (the tokenizers look one or two bytes ahead), and the next region starts after.
571fn ar_rdin fd int to ulong
572off = ulong 0
573go = true
574while go bound 134217729
575n = read fd ar_wb 0 65536
576if eq n 0
577go = false
578else
579if lt n 0
580put ar_inl 0 off
581ret 1
582end
583c = ulong n
584put ar_inl 0 add off c
585if ne ar_need 0 add off c 0
586ret 2
587end
588ar_pack off c
589off = add off c
590end
591end
592put ar_inl 0 off
593w = add shr off 3 8
594if ne ar_need 0 sub shl w 3 1 0
595ret 2
596end
597put ar_rw 0 w
598put ar_top 0 w
599put ar_gc 0 shl w 3
600ret 0
601end
602
603note ar_pack: the c bytes of ar_wb become payload bytes off .. off + c - 1 (their words are 0 until now). Bytes
604note up to a word boundary one at a time, then whole words of 8 (one store each), then the last bytes (measured
605note under qemu-aarch64 on a 14785-byte unit: 502253 guest instructions a byte at a time, 160761 this way)
606fn ar_pack off ulong c ulong to void
607i = ulong 0
608while and lt i c ne and add off i 7 0 bound 8
609ar_pack1 add off i get ar_wb i
610i = add i 1
611end
612while le add i 8 c bound 8193
613y = ulong get ar_wb i
614y = or y shl ulong get ar_wb add i 1 8
615y = or y shl ulong get ar_wb add i 2 16
616y = or y shl ulong get ar_wb add i 3 24
617y = or y shl ulong get ar_wb add i 4 32
618y = or y shl ulong get ar_wb add i 5 40
619y = or y shl ulong get ar_wb add i 6 48
620y = or y shl ulong get ar_wb add i 7 56
621put ar_m shr add off i 3 y
622i = add i 8
623end
624while lt i c bound 8
625ar_pack1 add off i get ar_wb i
626i = add i 1
627end
628end
629
630note ar_pack1: payload byte x becomes b (its word's other bytes are kept)
631fn ar_pack1 x ulong b uchar to void
632w = shr x 3
633put ar_m w or get ar_m w shl ulong b shl and x 7 3
634end
635
636note ar_gwords: the words the regions of group g hold
637fn ar_gwords g ulong to ulong
638s = ulong 0
639r = ulong 0
640while lt r get ar_nr 0 bound 161
641if eq get ar_rg r g
642s = add s get ar_rw r
643end
644r = add r 1
645end
646ret s
647end
648
649note ar_gcount: what group g was asked to hold: the payload's and the output's bytes, else the highest index
650note asked for, plus 1 (bytes for a byte group)
651fn ar_gcount g ulong to ulong
652if eq g 0
653ret get ar_inl 0
654end
655if eq g 1
656ret get ar_oc 0
657end
658ret get ar_gp g
659end
660
661note record writer: bytes into ar_rbuf (flushed to fd when near full)
662fn ar_rb1 fd ulong b ulong to void
663c = get ar_rc 0
664if ge c 4000
665w = write int fd ar_rbuf 0 c
666c = ulong 0
667end
668put ar_rbuf c uchar b
669put ar_rc 0 add c 1
670end
671fn ar_rn fd ulong v ulong to void
672d = ulong 1
673while le d div v 10 bound 20
674d = mul d 10
675end
676x = v
677while gt d 0 bound 21
678ar_rb1 fd add 48 div x d
679x = rem x d
680d = div d 10
681end
682end
683note ar_rs: the bytes "k=" of a key packed little-endian in w (n bytes)
684fn ar_rs fd ulong w ulong n ulong to void
685x = w
686for i ulong n bound 8
687ar_rb1 fd and x 255
688x = shr x 8
689end
690end
691
692note ar_rec: the ng-use record, one line to fd: ng-use 1 arena=A top=T waste=W full=F, then for each group
693note LABEL=N/H, N what it was asked to hold (ar_gcount) and H the words it holds. A, T, W and H count 8-byte
694note words of the arena; F is 1 when a stage was refused because the arena was full.
695fn ar_rec fd ulong to void
696put ar_rc 0 0
697ar_rs fd 3539940853674764142 8
698ar_rs fd 17277100355641632 7
699ar_rn fd len ar_m
700ar_rs fd 263879357472 5
701ar_rn fd get ar_top 0
702ar_rs fd 17281524405729056 7
703ar_rn fd get ar_wst 0
704ar_rs fd 67535885395488 6
705ar_rn fd get ar_fl 0
706g = ulong 0
707p = ulong 0
708while lt p len ar_gl bound 1024
709ar_rb1 fd 32
710while ne get ar_gl p 32 bound 16
711ar_rb1 fd ulong get ar_gl p
712p = add p 1
713end
714p = add p 1
715ar_rb1 fd 61
716ar_rn fd ar_gcount g
717ar_rb1 fd 47
718ar_rn fd ar_gwords g
719g = add g 1
720end
721ar_rb1 fd 10
722w = write int fd ar_rbuf 0 get ar_rc 0
723put ar_rc 0 0
724end
725
726note ar_die: a stage with no refusal path of its own: `ng: arena full`, the record, exit 14
727text ar_dmsg "ng: arena full\n"
728fn ar_die to void
729w = write 2 ar_dmsg 0 len ar_dmsg
730ar_rec 2
731exit int 14
732end
733
734note ar_defs: the regions (generated by the one-off NgNoCaps codemod; region: name, group, kind)
735note 0 f_inp group 0 (in) bytes
736note 1 i_out group 1 (out) bytes
737note 2 i_gdat group 2 (gdat) bytes
738note 3 f_sitebu group 3 (site) bytes
739note 4 f_nmname group 4 (names) ulong x1
740note 5 f_nm_reg group 4 (names) uchar in a word x1
741note 6 f_nmtype group 4 (names) uchar in a word x1
742note 7 f_nm_len group 4 (names) ulong x1
743note 8 f_nmkind group 4 (names) uchar in a word x1
744note 9 f_nmaddr group 4 (names) ulong x1
745note 10 f_nm_fv group 4 (names) uchar in a word x1
746note 11 f_nm_lay group 4 (names) uchar in a word x1
747note 12 f_nm_fr group 4 (names) uchar in a word x1
748note 13 f_nm_lr group 4 (names) uchar in a word x1
749note 14 f_nm_nx group 4 (names) ulong x1
750note 15 f_nm_h group 5 (namehash) ulong x1
751note 16 f_ln_o group 6 (long) ulong x1
752note 17 f_ln_l group 6 (long) uchar in a word x1
753note 18 f_ln_x group 6 (long) ulong x1
754note 19 f_ln_h group 7 (longhash) ulong x1
755note 20 f_fnname group 8 (fns) ulong x1
756note 21 f_fn_ret group 8 (fns) uchar in a word x1
757note 22 f_fn_np group 8 (fns) uchar in a word x1
758note 23 f_fnnmle group 8 (fns) ulong x1
759note 24 f_fn_off group 8 (fns) ulong x1
760note 25 f_fn_inl group 8 (fns) uchar in a word x1
761note 26 f_fnbody group 8 (fns) ulong x1
762note 27 f_fn_bln group 8 (fns) ulong x1
763note 28 f_settle group 8 (fns) uchar in a word x1
764note 29 f_reach group 8 (fns) uchar in a word x1
765note 30 f_fn_exp group 8 (fns) uchar in a word x1
766note 31 f_fn_nx group 8 (fns) ulong x1
767note 32 f_fn_pn group 8 (fns) ulong x6
768note 33 f_fn_pt group 8 (fns) uchar in a word x6
769note 34 f_fn_pl group 8 (fns) ulong x6
770note 35 f_fn_pk group 8 (fns) uchar in a word x6
771note 36 f_fnplay group 8 (fns) uchar in a word x6
772note 37 f_fn_pfr group 8 (fns) uchar in a word x6
773note 38 f_fn_h group 9 (fnhash) ulong x1
774note 39 f_callfr group 10 (calls) ulong x1
775note 40 f_callto group 10 (calls) ulong x1
776note 41 f_bkind group 11 (blocks) ulong x1
777note 42 f_breg group 11 (blocks) ulong x1
778note 43 f_btyp group 11 (blocks) ulong x1
779note 44 f_bnames group 11 (blocks) ulong x1
780note 45 f_bheld group 11 (blocks) ulong x1
781note 46 f_bdead group 11 (blocks) ulong x1
782note 47 f_u_cur group 11 (blocks) ulong x1
783note 48 f_u_line group 11 (blocks) ulong x1
784note 49 f_u_it group 11 (blocks) ulong x1
785note 50 f_u_k group 11 (blocks) ulong x1
786note 51 f_u_nn group 11 (blocks) ulong x1
787note 52 f_u_held group 11 (blocks) ulong x1
788note 53 f_fz_ent group 11 (blocks) ulong x16
789note 54 f_fz_thn group 11 (blocks) ulong x16
790note 55 f_brkblk group 12 (breaks) ulong x1
791note 56 f_cblk group 13 (conts) ulong x1
792note 57 f_iefrom group 14 (inledge) ulong x1
793note 58 f_ie_to group 14 (inledge) ulong x1
794note 59 f_fr group 15 (depth) ulong x32
795note 60 f_fwv group 15 (depth) ulong x1
796note 61 f_ul_k group 15 (depth) ulong x6
797note 62 f_ul_v group 15 (depth) ulong x6
798note 63 f_k_deg group 16 (kahnfn) ulong x1
799note 64 f_k_st group 16 (kahnfn) ulong x1
800note 65 f_k_cu group 16 (kahnfn) ulong x1
801note 66 f_k_q group 16 (kahnfn) ulong x1
802note 67 f_k_ed group 17 (kahncall) ulong x1
803note 68 f_lnw group 18 (lnames) ulong x1
804note 69 f_lnk group 18 (lnames) uchar in a word x1
805note 70 f_lnt group 18 (lnames) ulong x1
806note 71 f_lnv group 18 (lnames) uint in a word x1
807note 72 f_lnc group 18 (lnames) ulong x1
808note 73 f_lnx group 18 (lnames) ulong x1
809note 74 f_lnh group 19 (lnamehash) ulong x1
810note 75 f_lsk group 20 (lstack) uchar in a word x1
811note 76 f_lsn group 20 (lstack) ulong x1
812note 77 f_lsm group 20 (lstack) ulong x1
813note 78 f_lsj group 20 (lstack) uint in a word x1
814note 79 f_lsh group 20 (lstack) uint in a word x1
815note 80 f_lsx group 20 (lstack) uint in a word x1
816note 81 f_lse group 20 (lstack) uint in a word x1
817note 82 f_lsl group 20 (lstack) uint in a word x1
818note 83 f_lss group 20 (lstack) ulong x1
819note 84 f_lsv group 21 (lsnap) uint in a word x1
820note 85 f_laq group 22 (laq) uint in a word x1
821note 86 f_lca group 23 (lca) uint in a word x1
822note 87 i_gty group 24 (irglob) uint in a word x1
823note 88 i_gcl group 24 (irglob) uchar in a word x1
824note 89 i_gd0 group 24 (irglob) ulong x1
825note 90 a_gad group 25 (a64glob) ulong x1
826note 91 a_vh group 26 (a64val) ulong x1
827note 92 a_vs group 26 (a64val) ulong x1
828note 93 a_ve group 26 (a64val) ulong x1
829note 94 a_vx group 26 (a64val) uchar in a word x1
830note 95 a_vr group 26 (a64val) uchar in a word x1
831note 96 a_vsl group 26 (a64val) ulong x1
832note 97 a_vnx group 26 (a64val) ulong x1
833note 98 a_vln group 26 (a64val) ulong x1
834note 99 a_vf group 26 (a64val) uchar in a word x1
835note 100 a_nu group 26 (a64val) uint in a word x1
836note 101 a_lin group 27 (a64live) ulong x1
837note 102 a_lout group 27 (a64live) ulong x1
838note 103 a_luse group 27 (a64live) ulong x1
839note 104 a_ldef group 27 (a64live) ulong x1
840note 105 a_cpc group 28 (a64call) ulong x1
841note 106 a_cft group 28 (a64call) ulong x1
842note 107 a_code group 29 (a64code) bytes
843note 108 a_xpc group 30 (a64fix) ulong x1
844note 109 a_xfm group 30 (a64fix) ulong x1
845note 110 a_xa group 30 (a64fix) ulong x1
846note 111 a_xl group 30 (a64fix) ulong x1
847fn ar_defs to void
848ar_def 0 0 0
849ar_def 1 1 0
850ar_def 2 2 0
851ar_def 3 3 0
852ar_def 4 4 1
853ar_def 5 4 1
854ar_def 6 4 1
855ar_def 7 4 1
856ar_def 8 4 1
857ar_def 9 4 1
858ar_def 10 4 1
859ar_def 11 4 1
860ar_def 12 4 1
861ar_def 13 4 1
862ar_def 14 4 1
863ar_def 15 5 1
864ar_def 16 6 1
865ar_def 17 6 1
866ar_def 18 6 1
867ar_def 19 7 1
868ar_def 20 8 1
869ar_def 21 8 1
870ar_def 22 8 1
871ar_def 23 8 1
872ar_def 24 8 1
873ar_def 25 8 1
874ar_def 26 8 1
875ar_def 27 8 1
876ar_def 28 8 1
877ar_def 29 8 1
878ar_def 30 8 1
879ar_def 31 8 1
880ar_def 32 8 6
881ar_def 33 8 6
882ar_def 34 8 6
883ar_def 35 8 6
884ar_def 36 8 6
885ar_def 37 8 6
886ar_def 38 9 1
887ar_def 39 10 1
888ar_def 40 10 1
889ar_def 41 11 1
890ar_def 42 11 1
891ar_def 43 11 1
892ar_def 44 11 1
893ar_def 45 11 1
894ar_def 46 11 1
895ar_def 47 11 1
896ar_def 48 11 1
897ar_def 49 11 1
898ar_def 50 11 1
899ar_def 51 11 1
900ar_def 52 11 1
901ar_def 53 11 16
902ar_def 54 11 16
903ar_def 55 12 1
904ar_def 56 13 1
905ar_def 57 14 1
906ar_def 58 14 1
907ar_def 59 15 32
908ar_def 60 15 1
909ar_def 61 15 6
910ar_def 62 15 6
911ar_def 63 16 1
912ar_def 64 16 1
913ar_def 65 16 1
914ar_def 66 16 1
915ar_def 67 17 1
916ar_def 68 18 1
917ar_def 69 18 1
918ar_def 70 18 1
919ar_def 71 18 1
920ar_def 72 18 1
921ar_def 73 18 1
922ar_def 74 19 1
923ar_def 75 20 1
924ar_def 76 20 1
925ar_def 77 20 1
926ar_def 78 20 1
927ar_def 79 20 1
928ar_def 80 20 1
929ar_def 81 20 1
930ar_def 82 20 1
931ar_def 83 20 1
932ar_def 84 21 1
933ar_def 85 22 1
934ar_def 86 23 1
935ar_def 87 24 1
936ar_def 88 24 1
937ar_def 89 24 1
938ar_def 90 25 1
939ar_def 91 26 1
940ar_def 92 26 1
941ar_def 93 26 1
942ar_def 94 26 1
943ar_def 95 26 1
944ar_def 96 26 1
945ar_def 97 26 1
946ar_def 98 26 1
947ar_def 99 26 1
948ar_def 100 26 1
949ar_def 101 27 1
950ar_def 102 27 1
951ar_def 103 27 1
952ar_def 104 27 1
953ar_def 105 28 1
954ar_def 106 28 1
955ar_def 107 29 0
956ar_def 108 30 1
957ar_def 109 30 1
958ar_def 110 30 1
959ar_def 111 30 1
960end
961note ar_init: every region but the payload gets its first room after it: hash buckets 1024
962note (fnhash, lnamehash, longhash, namehash), the output 65536 bytes, every other group 64 entries
963fn ar_init to void
964ar_defs
965z = ar_need 1 65535
966z = ar_need 2 63
967z = ar_need 3 63
968z = ar_need 4 63
969z = ar_need 5 1023
970z = ar_need 6 63
971z = ar_need 7 1023
972z = ar_need 8 63
973z = ar_need 9 1023
974z = ar_need 10 63
975z = ar_need 11 63
976z = ar_need 12 63
977z = ar_need 13 63
978z = ar_need 14 63
979z = ar_need 15 63
980z = ar_need 16 63
981z = ar_need 17 63
982z = ar_need 18 63
983z = ar_need 19 1023
984z = ar_need 20 63
985z = ar_need 21 63
986z = ar_need 22 63
987z = ar_need 23 63
988z = ar_need 24 63
989z = ar_need 25 63
990z = ar_need 26 63
991z = ar_need 27 63
992z = ar_need 28 63
993z = ar_need 29 63
994z = ar_need 30 63
995for g ulong 31
996put ar_gp g 0
997end
998put ar_gp 0 get ar_inl 0
999end
1000global i_on ulong 1
1001note reader state: 0 cursor, 1 end, 2 refusal class (0 none), 3 refusal offset, 4 value base,
1002note 5 block base, 6 function value count
1003global i_rd ulong 8
1004note ---- row table ----------------------------------------------------------------------------
1005fn ir_rskip to void
1006while lt get i_rp 0 len i_rows bound 1000000
1007c = get i_rows get i_rp 0
1008if and ne c 32 ne c 10
1009ret
1010end
1011put i_rp 0 add get i_rp 0 1
1012end
1013end
1014fn ir_rnum to ulong
1015ir_rskip
1016v = ulong 0
1017while lt get i_rp 0 len i_rows bound 32
1018c = get i_rows get i_rp 0
1019if or lt c 48 gt c 57
1020ret v
1021end
1022v = add mul v 10 ulong sub c 48
1023put i_rp 0 add get i_rp 0 1
1024end
1025ret v
1026end
1027fn ir_rinit to void
1028if ne get i_n 15 0
1029ret
1030end
1031put i_n 15 1
1032put i_rp 0 0
1033while lt get i_rp 0 len i_rows bound 256
1034o = ir_rnum
1035ir_rskip
1036s = get i_rp 0
1037put i_rno o uint s
1038while ne get i_rows get i_rp 0 32 bound 64
1039put i_rp 0 add get i_rp 0 1
1040end
1041put i_rnl o uchar sub get i_rp 0 s
1042put i_rtk o uchar ir_rnum
1043put i_rim o uchar ir_rnum
1044put i_rfx o uchar ir_rnum
1045put i_rvr o uchar ir_rnum
1046put i_rty o uchar ir_rnum
1047put i_rok o 1
1048ir_rskip
1049end
1050end
1051note 1 when op o carries a token operand (E, R, and T except br)
1052fn ir_htok op uint to uint
1053k = get i_rtk op
1054if or eq k 1 eq k 2
1055ret 1
1056end
1057if and eq k 3 ne op 60
1058ret 1
1059end
1060ret 0
1061end
1062note operand kind of slot k of op: 0 value, 1 block, 2 function
1063fn ir_okd op uint k ulong to uint
1064if eq op 60
1065if eq k 0
1066ret 1
1067end
1068end
1069if eq op 61
1070if ge k 2
1071ret 1
1072end
1073end
1074if or eq op 64 eq op 67
1075if eq k 1
1076ret 2
1077end
1078end
1079ret 0
1080end
1081note ---- builder ------------------------------------------------------------------------------
1082note The builder's tables are those of IR.md s12 for a whole module (65536 types, struct fields and functions,
1083note 1048576 blocks, 4194304 ops, 16777216 operand slots, 8388608 values); the reader refuses a count past
1084note s12 before building. A builder (front-ir, ngrand, the oracles) asked for more ends with ir_lim: one stderr
1085note line and exit 23 (LIMIT), never an index past a table. Globals and their data are in the arena (ir_glob).
1086text i_limm "ng: refused class 23 (IR.md s12 table full while building)\n"
1087fn ir_lim to void
1088w = write 2 i_limm 0 len i_limm
1089exit 23
1090end
1091fn ir_init to void
1092ir_rinit
1093for i ulong 15
1094put i_n i 0
1095end
1096put i_on 0 0
1097end
1098fn ir_newv t uint k uint o uint to uint
1099n = add get i_n 6 1
1100if gt n 8388608
1101ir_lim
1102end
1103put i_n 6 n
1104put i_vty n t
1105put i_vk n uchar k
1106put i_vo n o
1107put i_vnx n 0
1108put i_vno n 0
1109ret uint n
1110end
1111note a type id equal to (k a b c d e) among table types, else 0 (structs compare their fields)
1112fn ir_tfind k uint a uint b uint c uint d uint e uint to uint
1113put i_tq 0 k
1114put i_tq 1 a
1115put i_tq 2 b
1116put i_tq 3 c
1117put i_tq 4 d
1118put i_tq 5 e
1119for i ulong get i_n 0 bound 65536
1120t = add i 32
1121if ir_tmat t
1122ret uint t
1123end
1124end
1125ret 0
1126end
1127fn ir_tmat t ulong to bool
1128if ne uint get i_tk t get i_tq 0
1129ret false
1130end
1131if ne get i_t1 t get i_tq 1
1132ret false
1133end
1134if ne get i_t2 t get i_tq 2
1135ret false
1136end
1137if ne get i_t3 t get i_tq 3
1138ret false
1139end
1140if ne get i_t4 t get i_tq 4
1141ret false
1142end
1143ret eq get i_t5 t get i_tq 5
1144end
1145fn ir_tadd k uint a uint b uint c uint d uint e uint to uint
1146t = add get i_n 0 32
1147if ge get i_n 0 65536
1148ir_lim
1149end
1150put i_n 0 add get i_n 0 1
1151put i_tk t uchar k
1152put i_t1 t a
1153put i_t2 t b
1154put i_t3 t c
1155put i_t4 t d
1156put i_t5 t e
1157ret uint t
1158end
1159fn ir_tget k uint a uint b uint c uint d uint e uint to uint
1160t = ir_tfind k a b c d e
1161if ne t 0
1162ret t
1163end
1164ret ir_tadd k a b c d e
1165end
1166fn ir_tvec e uint l uint to uint
1167ret ir_tget 1 e l 0 0 0
1168end
1169fn ir_tarr e uint c uint to uint
1170ret ir_tget 2 e c 0 0 0
1171end
1172fn ir_tref t uint a uint s uint to uint
1173ret ir_tget 4 t a s 0 0
1174end
1175fn ir_tslc e uint a uint s uint to uint
1176ret ir_tget 5 e a s 0 0
1177end
1178fn ir_tfrg r uint m uint n uint k uint e uint to uint
1179ret ir_tget 6 r m n k e
1180end
1181fn ir_tsb size uint to void
1182put i_sp 0 ulong size
1183put i_sp 1 get i_n 1
1184end
1185fn ir_tsf t uint off uint ord uint to void
1186x = get i_n 1
1187if ge x 65536
1188ir_lim
1189end
1190put i_xty x t
1191put i_xof x off
1192put i_xor x uchar ord
1193put i_n 1 add x 1
1194end
1195note the struct with the same size and fields as fields [f0, f0+nf) among table types, else 0
1196fn ir_sfind size uint f0 ulong nf ulong to uint
1197for i ulong get i_n 0 bound 65536
1198t = add i 32
1199if ir_smat t size nf
1200if ir_seq ulong get i_t3 t f0 nf
1201ret uint t
1202end
1203end
1204end
1205ret 0
1206end
1207fn ir_smat t ulong size uint nf ulong to bool
1208if ne get i_tk t 3
1209ret false
1210end
1211if ne get i_t1 t size
1212ret false
1213end
1214ret eq ulong get i_t2 t nf
1215end
1216fn ir_seq a ulong b ulong n ulong to bool
1217for j ulong n bound 64
1218x = add a j
1219y = add b j
1220if ne get i_xty x get i_xty y
1221ret false
1222end
1223if ne get i_xof x get i_xof y
1224ret false
1225end
1226if ne get i_xor x get i_xor y
1227ret false
1228end
1229end
1230ret true
1231end
1232fn ir_tse to uint
1233f0 = get i_sp 1
1234nf = sub get i_n 1 f0
1235size = uint get i_sp 0
1236t = ir_sfind size f0 nf
1237if ne t 0
1238put i_n 1 f0
1239ret t
1240end
1241ret ir_tadd 3 size uint nf uint f0 0 0
1242end
1243note ir_glob / ir_gbyt: a new global and its data bytes, in the arena (groups irglob and gdat); a full
1244note arena ends the build (ar_die), and the reader asks for the room first so that it refuses with LIMIT
1245fn ir_glob t uint cls uint to uint
1246g = add get i_n 2 1
1247if ne ar_need 24 g 0
1248ar_die
1249end
1250put i_n 2 g
1251ar_pu 87 g t
1252ar_pc 88 g uchar cls
1253put ar_m add get ar_rb 89 g get i_n 8
1254ret uint g
1255end
1256fn ir_gbyt g uint b uint to void
1257x = get i_n 8
1258if ne ar_need 2 x 0
1259ar_die
1260end
1261ar_bp 2 x uchar b
1262put i_n 8 add x 1
1263end
1264fn ir_fnew kind uint flags uint res uint to uint
1265f = add get i_n 3 1
1266if gt f 65536
1267ir_lim
1268end
1269put i_n 3 f
1270fu = uint f
1271put i_fk f uchar kind
1272put i_ffl f uchar flags
1273put i_frs f res
1274put i_fnp f 0
1275put i_fp1 f 0
1276put i_fpt f 0
1277put i_fb1 f 0
1278put i_fbt f 0
1279put i_fnb f 0
1280put i_ftx f 0
1281put i_fty f 0
1282put i_ftz f 0
1283put i_fsg f 0
1284put i_ftk f ir_newv 20 0 fu
1285ret fu
1286end
1287fn ir_fpar f uint t uint to uint
1288v = ir_newv t 1 f
1289if eq get i_fp1 f 0
1290put i_fp1 f v
1291else
1292put i_vnx get i_fpt f v
1293end
1294put i_fpt f v
1295put i_fnp f add get i_fnp f 1
1296ret v
1297end
1298fn ir_fker f uint x uint y uint z uint sg uint to void
1299put i_ftx f x
1300put i_fty f y
1301put i_ftz f z
1302put i_fsg f uchar sg
1303end
1304fn ir_ftok f uint to uint
1305ret get i_ftk f
1306end
1307fn ir_bpar b uint t uint to uint
1308v = ir_newv t 2 b
1309if eq get i_bp1 b 0
1310put i_bp1 b v
1311else
1312put i_vnx get i_bpt b v
1313end
1314put i_bpt b v
1315put i_bnp b add get i_bnp b 1
1316ret v
1317end
1318fn ir_blk f uint to uint
1319b = add get i_n 4 1
1320if gt b 1048576
1321ir_lim
1322end
1323put i_n 4 b
1324bu = uint b
1325put i_bf b f
1326put i_bnx b 0
1327put i_bp1 b 0
1328put i_bpt b 0
1329put i_bnp b 0
1330put i_bo1 b 0
1331put i_bot b 0
1332put i_bno b 0
1333put i_bix b 0
1334if eq get i_fb1 f 0
1335put i_fb1 f bu
1336else
1337put i_bnx get i_fbt f bu
1338end
1339put i_fbt f bu
1340put i_fnb f add get i_fnb f 1
1341if gt get i_fnb f 1
1342t = ir_bpar bu 20
1343end
1344ret bu
1345end
1346fn ir_btok b uint to uint
1347ret get i_bp1 b
1348end
1349fn ir_odefs o uint to void
1350op = uint get i_oop o
1351t = get i_oty o
1352put i_otk o 0
1353put i_ors o 0
1354if eq get i_rtk op 1
1355put i_otk o ir_newv 20 3 o
1356end
1357c = get i_rty op
1358if and ne t 0 or eq c 1 eq c 2
1359put i_ors o ir_newv t 4 o
1360end
1361end
1362fn ir_op b uint op uint ty uint imm ulong n uint to uint
1363o = add get i_n 5 1
1364if or gt o 4194304 gt add get i_n 7 ulong n 16777216
1365ir_lim
1366end
1367put i_n 5 o
1368ou = uint o
1369put i_oop o uchar op
1370put i_oty o ty
1371put i_oim o imm
1372put i_ona o n
1373a = get i_n 7
1374put i_oa0 o uint a
1375for j ulong ulong n bound 1024
1376put i_arg add a j 0
1377end
1378put i_n 7 add a ulong n
1379put i_ob o b
1380put i_onx o 0
1381put i_oof o 0
1382if eq get i_bo1 b 0
1383put i_bo1 b ou
1384else
1385put i_onx get i_bot b ou
1386end
1387put i_bot b ou
1388put i_bno b add get i_bno b 1
1389ir_odefs ou
1390ret ou
1391end
1392fn ir_set o uint k uint h uint to void
1393put i_arg add ulong get i_oa0 o ulong k h
1394end
1395fn ir_arg o uint k uint to uint
1396ret get i_arg add ulong get i_oa0 o ulong k
1397end
1398fn ir_res o uint to uint
1399ret get i_ors o
1400end
1401fn ir_otok o uint to uint
1402ret get i_otk o
1403end
1404note wire value ids (IR.md s6.2) into i_vno and block positions into i_bix; returns the value count
1405fn ir_num f uint to uint
1406n = uint 0
1407put i_vno get i_ftk f n
1408n = add n 1
1409v = get i_fp1 f
1410while ne v 0 bound 65
1411put i_vno v n
1412n = add n 1
1413v = get i_vnx v
1414end
1415b = get i_fb1 f
1416x = uint 0
1417while ne b 0 bound 1048577
1418put i_bix b x
1419x = add x 1
1420n = ir_numb b n
1421b = get i_bnx b
1422end
1423ret n
1424end
1425fn ir_numb b uint n0 uint to uint
1426n = n0
1427v = get i_bp1 b
1428while ne v 0 bound 65
1429put i_vno v n
1430n = add n 1
1431v = get i_vnx v
1432end
1433o = get i_bo1 b
1434while ne o 0 bound 4194305
1435if ne get i_otk o 0
1436put i_vno get i_otk o n
1437n = add n 1
1438end
1439if ne get i_ors o 0
1440put i_vno get i_ors o n
1441n = add n 1
1442end
1443o = get i_onx o
1444end
1445ret n
1446end
1447note bytes of one element or object of type t (0 for void, tok and invalid ids)
1448fn ir_tsize t uint to ulong
1449if lt t 32
1450if or eq t 1 or eq t 2 or eq t 6 or eq t 18 eq t 19
1451ret 1
1452end
1453if or eq t 3 or eq t 7 or eq t 16 eq t 17
1454ret 2
1455end
1456if or eq t 4 or eq t 8 eq t 13
1457ret 4
1458end
1459if or eq t 5 or eq t 9 eq t 14
1460ret 8
1461end
1462ret 0
1463end
1464if ge ulong t add get i_n 0 32
1465ret 0
1466end
1467k = get i_tk t
1468if eq k 1
1469ret mul ulong get i_t2 t ir_tsize get i_t1 t
1470end
1471if eq k 2
1472ret mul ulong get i_t2 t ir_tsize get i_t1 t
1473end
1474if eq k 3
1475ret ulong get i_t1 t
1476end
1477ret 0
1478end
1479note ---- binary writer (IR.md s3-s7) ----------------------------------------------------------
1480note ir_wn: the k (at most 8) low bytes of v at the output's end (region 1 asked for them first). The output is
1481note written from byte 0 in order, so the first byte of each word sets the word and the others are ORed in (inline)
1482fn ir_wn v ulong k ulong to void
1483p = get i_on 0
1484if ne ar_need 1 add p k 0
1485ar_die
1486end
1487b = get ar_rb 1
1488for j ulong k bound 8
1489q = add p j
1490x = add b shr q 3
1491d = and shr v mul j 8 255
1492c = shl d shl and q 7 3
1493if ne and q 7 0
1494c = or c get ar_m x
1495end
1496put ar_m x c
1497end
1498put i_on 0 add p k
1499put ar_oc 0 add p k
1500end
1501fn ir_wty t ulong to void
1502k = ulong get i_tk t
1503ir_wn k 1
1504if eq k 6
1505ir_wn ulong get i_t1 t 1
1506ir_wn ulong get i_t2 t 2
1507ir_wn ulong get i_t3 t 2
1508ir_wn ulong get i_t4 t 2
1509ir_wn ulong get i_t5 t 4
1510ret
1511end
1512if eq k 3
1513ir_wn ulong get i_t1 t 4
1514ir_wn ulong get i_t2 t 4
1515f0 = ulong get i_t3 t
1516for j ulong ulong get i_t2 t bound 64
1517ir_wn ulong get i_xty add f0 j 4
1518ir_wn ulong get i_xof add f0 j 4
1519ir_wn ulong get i_xor add f0 j 1
1520end
1521ret
1522end
1523ir_wn ulong get i_t1 t 4
1524if or eq k 1 eq k 2
1525ir_wn ulong get i_t2 t 4
1526ret
1527end
1528ir_wn ulong get i_t2 t 1
1529ir_wn ulong get i_t3 t 1
1530end
1531fn ir_wglb g ulong to void
1532ir_wn ulong uint get ar_m add get ar_rb 87 g 4
1533ir_wn ulong uchar get ar_m add get ar_rb 88 g 1
1534if eq ar_c 88 g 1
1535s = ir_tsize uint get ar_m add get ar_rb 87 g
1536d = get ar_m add get ar_rb 89 g
1537p = get i_on 0
1538if ne ar_need 1 add p s 0
1539ar_die
1540end
1541for j ulong s bound 134217729
1542ar_bp 1 add p j ar_bg 2 add d j
1543end
1544put i_on 0 add p s
1545end
1546end
1547fn ir_wval h uint to void
1548ir_wn ulong get i_vno h 4
1549end
1550fn ir_wop o uint to void
1551op = uint get i_oop o
1552ir_wn ulong op 1
1553ir_wn ulong get i_oty o 4
1554if eq get i_rim op 1
1555if or eq op 52 and ge op 111 le op 116
1556ir_wn sub get i_oim o 1 8
1557else
1558ir_wn get i_oim o 8
1559end
1560end
1561na = ulong get i_ona o
1562if eq get i_rvr op 1
1563ir_wn sub sub na ulong get i_rfx op ulong ir_htok op 4
1564end
1565a0 = ulong get i_oa0 o
1566for k ulong na bound 1024
1567h = get i_arg add a0 k
1568d = ir_okd op k
1569if eq d 0
1570ir_wval h
1571end
1572if eq d 1
1573ir_wn ulong get i_bix h 4
1574end
1575if eq d 2
1576ir_wn sub ulong h 1 4
1577end
1578end
1579end
1580fn ir_wblk b uint to void
1581ir_wn ulong get i_bnp b 4
1582v = get i_bp1 b
1583while ne v 0 bound 65
1584ir_wn ulong get i_vty v 4
1585v = get i_vnx v
1586end
1587ir_wn ulong get i_bno b 4
1588o = get i_bo1 b
1589while ne o 0 bound 4194305
1590ir_wop o
1591o = get i_onx o
1592end
1593end
1594fn ir_wfn f uint to void
1595n = ir_num f
1596ir_wn ulong get i_fk f 1
1597ir_wn ulong get i_ffl f 1
1598ir_wn ulong get i_fnp f 4
1599v = get i_fp1 f
1600while ne v 0 bound 65
1601ir_wn ulong get i_vty v 4
1602v = get i_vnx v
1603end
1604ir_wn ulong get i_frs f 4
1605if eq get i_fk f 1
1606ir_wn ulong get i_ftx f 4
1607ir_wn ulong get i_fty f 4
1608ir_wn ulong get i_ftz f 4
1609ir_wn ulong get i_fsg f 1
1610end
1611ir_wn ulong get i_fnb f 4
1612b = get i_fb1 f
1613while ne b 0 bound 1048577
1614ir_wblk b
1615b = get i_bnx b
1616end
1617end
1618fn ir_wr to ulong
1619put i_on 0 0
1620for j ulong 6
1621ir_wn ulong get i_magic j 1
1622end
1623ir_wn 0 1
1624ir_wn 9 1
1625for j ulong 14
1626ir_wn ulong get i_tag j 1
1627end
1628ir_wn 0 2
1629ir_wn get i_n 0 4
1630for i ulong get i_n 0 bound 65536
1631ir_wty add i 32
1632end
1633ir_wn get i_n 2 4
1634for i ulong get i_n 2 bound 134217729
1635ir_wglb add i 1
1636end
1637ir_wn get i_n 3 4
1638for i ulong get i_n 3 bound 65536
1639ir_wfn uint add i 1
1640end
1641ret get i_on 0
1642end
1643note ---- binary reader ------------------------------------------------------------------------
1644note refusal: the first class sticks, with the cursor offset (or at, when given)
1645fn ir_bad c ulong ofs ulong to void
1646if eq get i_rd 2 0
1647put i_rd 2 c
1648put i_rd 3 ofs
1649end
1650end
1651fn ir_ok to bool
1652ret eq get i_rd 2 0
1653end
1654fn ir_rn k ulong to ulong
1655p = get i_rd 0
1656if gt add p k get i_rd 1
1657ir_bad 12 p
1658put i_rd 0 get i_rd 1
1659ret 0
1660end
1661note the payload bytes p .. p + k - 1 are inside region 0 (p + k is at most the payload's end, checked above),
1662note which sits at word 0: read inline, no per-byte check
1663v = ulong 0
1664for j ulong k bound 8
1665q = add p j
1666w = get ar_m shr q 3
1667v = or v shl and shr w shl and q 7 3 255 mul j 8
1668end
1669put i_rd 0 add p k
1670ret v
1671end
1672fn ir_rhdr to void
1673p = get i_rd 0
1674for j ulong 6
1675if ne ir_rn 1 ulong get i_magic j
1676ir_bad 10 p
1677end
1678end
1679if ne ir_rn 1 0
1680ir_bad 10 p
1681end
1682q = get i_rd 0
1683if ne ir_rn 1 9
1684ir_bad 11 q
1685end
1686q = get i_rd 0
1687for j ulong 14
1688if ne ir_rn 1 ulong get i_tag j
1689ir_bad 11 q
1690end
1691end
1692if ne ir_rn 2 0
1693ir_bad 11 q
1694end
1695end
1696note a type id usable inside table entry id: a valid built-in or an earlier table id
1697fn ir_tval t ulong id ulong to bool
1698if lt t 32
1699if or or eq t 10 eq t 11 or eq t 12 eq t 15
1700ret false
1701end
1702ret le t 20
1703end
1704ret lt t id
1705end
1706fn ir_rtref t ulong id ulong ofs ulong to uint
1707if not ir_tval t id
1708ir_bad 24 ofs
1709end
1710ret uint t
1711end
1712fn ir_rty1 to void
1713ofs = get i_rd 0
1714id = add get i_n 0 32
1715k = ir_rn 1
1716if or eq k 1 eq k 2
1717e = ir_rtref ir_rn 4 id ofs
1718c = uint ir_rn 4
1719if ne ir_tfind uint k e c 0 0 0 0
1720ir_bad 13 ofs
1721end
1722t = ir_tadd uint k e c 0 0 0
1723ret
1724end
1725if eq k 3
1726ir_rstr ofs id
1727ret
1728end
1729if or eq k 4 eq k 5
1730e = ir_rtref ir_rn 4 id ofs
1731a = ir_rn 1
1732s = ir_rn 1
1733if or gt a 1 gt s 4
1734ir_bad 13 ofs
1735end
1736if ne ir_tfind uint k e uint a uint s 0 0 0
1737ir_bad 13 ofs
1738end
1739t = ir_tadd uint k e uint a uint s 0 0
1740ret
1741end
1742if eq k 6
1743r = uint ir_rn 1
1744m = uint ir_rn 2
1745n = uint ir_rn 2
1746kk = uint ir_rn 2
1747e = ir_rtref ir_rn 4 id ofs
1748if gt r 2
1749ir_bad 13 ofs
1750end
1751if ne ir_tfind 6 r m n kk e 0
1752ir_bad 13 ofs
1753end
1754t = ir_tadd 6 r m n kk e
1755ret
1756end
1757ir_bad 13 ofs
1758end
1759fn ir_rstr ofs ulong id ulong to void
1760size = uint ir_rn 4
1761nf = ir_rn 4
1762if gt nf 64
1763ir_bad 23 sub get i_rd 0 4
1764ret
1765end
1766f0 = get i_n 1
1767if gt add f0 nf 65536
1768ir_bad 23 sub get i_rd 0 4
1769ret
1770end
1771for j ulong nf bound 64
1772t = ir_rtref ir_rn 4 id ofs
1773o = uint ir_rn 4
1774r = ir_rn 1
1775if gt r 1
1776ir_bad 13 ofs
1777end
1778ir_tsf t o uint r
1779end
1780if ne ir_sfind size f0 nf 0
1781ir_bad 13 ofs
1782end
1783t = ir_tadd 3 size uint nf uint f0 0 0
1784end
1785fn ir_rtys to void
1786ofs = get i_rd 0
1787n = ir_rn 4
1788if gt n 65536
1789ir_bad 23 ofs
1790ret
1791end
1792for i ulong n bound 65536
1793if not ir_ok
1794ret
1795end
1796ir_rty1
1797end
1798end
1799note globals: the count is bounded by the arena only (group irglob; src/ir.om, no-caps 2026-11-11); ngcheck
1800note and the text tools keep the 65536 of IR.md s12. A count the arena cannot hold is LIMIT at the entry.
1801fn ir_rglbs to void
1802n = ir_rn 4
1803for i ulong n bound 134217729
1804if not ir_ok
1805ret
1806end
1807if ne ar_need 24 add get i_n 2 1 0
1808ir_bad 23 get i_rd 0
1809ret
1810end
1811ir_rglb
1812end
1813end
1814fn ir_rglb to void
1815ofs = get i_rd 0
1816t = uint ir_rn 4
1817c = ir_rn 1
1818if gt c 2
1819ir_bad 13 ofs
1820ret
1821end
1822g = ir_glob t uint c
1823if ne c 1
1824ret
1825end
1826if or ge ulong t add get i_n 0 32 lt t 32
1827ir_bad 16 ofs
1828ret
1829end
1830if ne get i_tk t 2
1831ir_bad 16 ofs
1832ret
1833end
1834s = ir_tsize t
1835p = get i_rd 0
1836if gt add p s get i_rd 1
1837ir_bad 12 p
1838put i_rd 0 get i_rd 1
1839ret
1840end
1841d = get i_n 8
1842if ne ar_need 2 add d s 0
1843ir_bad 23 p
1844put i_rd 0 get i_rd 1
1845ret
1846end
1847for j ulong s bound 134217729
1848ar_bp 2 add d j ar_ib add p j
1849end
1850put i_n 8 add d s
1851put i_rd 0 add p s
1852end
1853fn ir_rfns to void
1854ofs = get i_rd 0
1855n = ir_rn 4
1856if gt n 65536
1857ir_bad 23 ofs
1858ret
1859end
1860for i ulong n bound 65536
1861if not ir_ok
1862ret
1863end
1864ir_rfn
1865end
1866end
1867fn ir_rfn to void
1868ofs = get i_rd 0
1869k = ir_rn 1
1870fl = ir_rn 1
1871if or gt k 2 gt fl 3
1872ir_bad 13 ofs
1873ret
1874end
1875pa = get i_rd 0
1876np = ir_rn 4
1877if gt np 64
1878ir_bad 23 pa
1879ret
1880end
1881note flags bit 1 (IR.md s6.1): the callback function of a `target macos` unit, six u64 and a u64 result
1882bk = ge fl 2
1883if and bk or ne k 0 ne np 6
1884ir_bad 13 ofs
1885ret
1886end
1887f = ir_fnew uint k uint fl 0
1888if bk
1889g = ulong 1
1890while lt g ulong f bound 65537
1891if ge ulong get i_ffl g 2
1892ir_bad 13 ofs
1893end
1894g = add g 1
1895end
1896end
1897put i_rd 4 ulong get i_ftk f
1898put i_rd 5 add get i_n 4 1
1899for j ulong np bound 64
1900pt = ir_rn 4
1901if and bk ne pt 5
1902ir_bad 13 ofs
1903end
1904v = ir_fpar f uint pt
1905end
1906rs = ir_rn 4
1907if and bk ne rs 5
1908ir_bad 13 ofs
1909end
1910if not ir_ok
1911ret
1912end
1913put i_frs f uint rs
1914if eq k 1
1915x = uint ir_rn 4
1916y = uint ir_rn 4
1917z = uint ir_rn 4
1918ir_fker f x y z uint ir_rn 1
1919end
1920pb = get i_rd 0
1921nb = ir_rn 4
1922if or eq nb 0 gt nb 65536
1923ir_bad 23 pb
1924ret
1925end
1926for j ulong nb bound 65536
1927if not ir_ok
1928ret
1929end
1930ir_rblk f
1931end
1932if ir_ok
1933ir_rfix f
1934end
1935end
1936fn ir_rblk f uint to void
1937b = ir_blk f
1938pa = get i_rd 0
1939np = ir_rn 4
1940if gt np 64
1941ir_bad 23 pa
1942ret
1943end
1944for j ulong np bound 64
1945t = uint ir_rn 4
1946if and eq j 0 gt get i_fnb f 1
1947put i_vty get i_bp1 b t
1948else
1949v = ir_bpar b t
1950end
1951end
1952if and gt get i_fnb f 1 eq np 0
1953ir_bad 19 pa
1954ret
1955end
1956po = get i_rd 0
1957no = ir_rn 4
1958if gt no 1048576
1959ir_bad 23 po
1960ret
1961end
1962if eq no 0
1963ir_bad 20 po
1964ret
1965end
1966for j ulong no bound 1048576
1967if not ir_ok
1968ret
1969end
1970ir_rop b
1971end
1972end
1973fn ir_rop b uint to void
1974ofs = get i_rd 0
1975op = uint ir_rn 1
1976if eq get i_rok op 0
1977ir_bad 15 ofs
1978ret
1979end
1980ty = uint ir_rn 4
1981im = ulong 0
1982if eq get i_rim op 1
1983im = ir_rn 8
1984end
1985n = add uint get i_rfx op ir_htok op
1986if eq get i_rvr op 1
1987pn = get i_rd 0
1988x = ir_rn 4
1989if gt x 64
1990ir_bad 23 pn
1991ret
1992end
1993n = add n uint x
1994end
1995if or eq op 52 and ge op 111 le op 116
1996im = add im 1
1997end
1998if gt add get i_n 5 1 4194304
1999ir_bad 23 ofs
2000ret
2001end
2002o = ir_op b op ty im n
2003put i_oof o uint ofs
2004a0 = ulong get i_oa0 o
2005for k ulong ulong n bound 1024
2006put i_arg add a0 k uint ir_rn 4
2007end
2008end
2009note turn wire ids of function f into handles (values, blocks) and check them (IR.md s6.2)
2010fn ir_rfix f uint to void
2011put i_rd 7 ulong ir_num f
2012put i_rd 6 ulong get i_fnb f
2013b = get i_fb1 f
2014while ne b 0 bound 1048577
2015o = get i_bo1 b
2016while ne o 0 bound 4194305
2017ir_rfxo o
2018o = get i_onx o
2019end
2020b = get i_bnx b
2021end
2022end
2023fn ir_rfxo o uint to void
2024n = ulong get i_ona o
2025for k ulong n bound 1024
2026if not ir_rfx1 o k
2027ir_bad 24 ulong get i_oof o
2028ret
2029end
2030end
2031if eq get i_oop o 52
2032im = get i_oim o
2033if or eq im 0 gt im get i_n 2
2034ir_bad 24 ulong get i_oof o
2035end
2036end
2037end
2038note map operand slot k of op o from a wire id to a handle; false when the id is out of range
2039fn ir_rfx1 o uint k ulong to bool
2040s = add ulong get i_oa0 o k
2041x = ulong get i_arg s
2042d = ir_okd uint get i_oop o k
2043if eq d 0
2044if ge x get i_rd 7
2045ir_bad 17 ulong get i_oof o
2046ret true
2047end
2048put i_arg s uint add get i_rd 4 x
2049end
2050if eq d 1
2051if ge x get i_rd 6
2052ret false
2053end
2054put i_arg s uint add get i_rd 5 x
2055end
2056if eq d 2
2057if ge x 65536
2058ret false
2059end
2060put i_arg s uint add x 1
2061end
2062ret true
2063end
2064note function operands after all functions are known
2065fn ir_rfnref to void
2066for i ulong get i_n 5 bound 4194304
2067o = add i 1
2068op = uint get i_oop o
2069if or eq op 64 eq op 67
2070if gt ulong ir_arg uint o 1 get i_n 3
2071ir_bad 24 ulong get i_oof o
2072end
2073end
2074end
2075end
2076fn ir_read off ulong stop ulong to ulong
2077ir_init
2078put i_rd 0 off
2079put i_rd 1 stop
2080put i_rd 2 0
2081put i_rd 3 0
2082ir_rhdr
2083if ir_ok
2084ir_rtys
2085end
2086if ir_ok
2087ir_rglbs
2088end
2089if ir_ok
2090ir_rfns
2091end
2092if ir_ok
2093ir_rfnref
2094end
2095if ir_ok
2096if ne get i_rd 0 stop
2097ir_bad 14 get i_rd 0
2098end
2099end
2100ret get i_rd 2
2101end
2102fn ir_badat to ulong
2103ret get i_rd 3
2104end