interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / tools / omegallm / wdrive / wbuild.om

1418 lines · 29263 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note wbuild.om (DRA-187, lane wsp): build driver for the Omega tools in tools/omegallm, and the
3note shared driver library of tools/omegallm/wdrive (wrun, wcheck, wpolicy are composed with this
4note unit's main renamed to wbmain). Bootstrap, from the aien-train checkout:
5note OMC < tools/omegallm/wdrive/wbuild.om > wbuild && chmod +x wbuild
6note Usage: wbuild OUTBIN PART... (environment OMC = the pinned omc, CORE = taskset core, default 0)
7note A PART is FILE, appended as is, or FILE@NAME, appended with its one line `fn main to int`
8note renamed to `fn NAME to int` (exit 4 unless that line occurs exactly once). Either may end in
9note =HEX64 (FILE=HEX64, FILE@NAME=HEX64): 64 lowercase hex digits, the file's pinned sha256. The bytes
10note read are hashed each time the part is read, before they are used; a mismatch prints
11note PART_PIN_MISMATCH FILE <sha256> on stderr and exits 3 before anything is compiled. A PART with an
12note `=` whose text after the last `=` is not 64 lowercase hex digits prints PART_PIN_BAD PART and
13note exits 2. Composition:
14note when any part has a line `om2`, the output starts with one line `om2`, then every
15note `layout ... end` block of every part in order, then the parts without their `om2` lines and
16note layout blocks. Every line starting `export fn ` is written as `fn ` (the output is a program,
17note and a program has no exports; om/sha256/sha256-om2.om has four). With no layout blocks, no
18note exports and `om2` only as the first line of the first part, this is plain concatenation (cat,
19note sed main rename) as tools/omegallm/wprobe/build.sh did.
20note Writes OUTBIN-all.om, runs `taskset -c CORE nice -n 19 OMC < OUTBIN-all.om > OUTBIN.tmp`,
21note sets mode 0775, renames OUTBIN.tmp to OUTBIN and prints
22note BUILD_OK <sha256 OUTBIN> OUTBIN source_sha256 <sha256 OUTBIN-all.om> omc <sha256 OMC>
23note Exits: 0 built, 1 omc refused the unit (stderr BUILD_FAIL omc rc=N, OUTBIN.tmp removed), 2 usage
24note or a malformed pin, 3 OMC is not the pinned compiler (stderr OMC_PIN_MISMATCH <sha256>) or a part
25note does not match its pin (PART_PIN_MISMATCH), 4 main count in a renamed part is not 1, 61 a part
26note over 8 MiB, 70 I/O.
27note The SHA-256 below is omega-mv e5b32567 om/sha256/sha256-om2.om (sha256 dbf002dd6aa36585632b9d01d
28note 98ac3f0bd2fa0f0475082940de53638847994bb), composed as aien-train-wpatch tools/omegallm/wpatch/
29note build.sh composes it: the layout shactx first, `export fn` made `fn`, private names renamed
30note (kinit wskinit, iv wsiv, sched wssched, rounds wsround, compress wscomp, run wsrun, blocks wsblks,
31note finish wsfin, tobuf wstobuf, absorb wsabsb). The lines from the next `layout` to the line
32note before `note wdrive library` are byte for byte that build's sha256-layout.om followed by its
33note sha256-functions.om.
34om2
35layout shactx 104
36field state uint 8 at 0 little
37field count ulong at 32 little
38field buf uchar 64 at 40 little
39end
40note SPDX-License-Identifier: Apache-2.0
41note sha256-om2.om: FIPS 180-4 SHA-256 in OM-2 (spec/omegese-om2.md, #747/#748, ledger L838).
42note The four entries are the API of src/sha256.h, exported through omega-bridge ABI v2 as
43note omega2_sha256_init/update/final/hash; the bridge's generated omega_api.h maps the C names.
44note
45note Invariants:
46note - All hashing state is the caller's 104-byte context (layout shactx below, equal to sha256_ctx
47note on LP64 AArch64). Nothing else persists between calls, so a byte copy of a live context is an
48note equivalent context (erratum E3).
49note - count is the number of bytes absorbed since init, modulo 2^64. buf[0 .. count mod 64) holds the
50note pending bytes of the current block; the rest of buf is unspecified. state is the chaining value
51note after the count div 64 whole blocks.
52note - final reads the context and writes only the digest, so the context is unchanged by it. The
53note contract promises less (after final the context is unspecified, memory safety only).
54note - Every branch and every loop count depends only on lengths, never on message bytes (section 8).
55note - Indices into the unsized input view are checked at run time (trap 3); indices into sized
56note arrays inside unrolled bodies are constants and checked when compiled.
57note bridge c-type shactx sha256_ctx
58note bridge c-define SHA256_DIGEST_SIZE 32
59note bridge c-define SHA256_BLOCK_SIZE 64
60
61
62note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
63note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed).
64fn wskinit k edit uint 64 to void
65put k 0 0x428a2f98
66put k 1 0x71374491
67put k 2 0xb5c0fbcf
68put k 3 0xe9b5dba5
69put k 4 0x3956c25b
70put k 5 0x59f111f1
71put k 6 0x923f82a4
72put k 7 0xab1c5ed5
73put k 8 0xd807aa98
74put k 9 0x12835b01
75put k 10 0x243185be
76put k 11 0x550c7dc3
77put k 12 0x72be5d74
78put k 13 0x80deb1fe
79put k 14 0x9bdc06a7
80put k 15 0xc19bf174
81put k 16 0xe49b69c1
82put k 17 0xefbe4786
83put k 18 0x0fc19dc6
84put k 19 0x240ca1cc
85put k 20 0x2de92c6f
86put k 21 0x4a7484aa
87put k 22 0x5cb0a9dc
88put k 23 0x76f988da
89put k 24 0x983e5152
90put k 25 0xa831c66d
91put k 26 0xb00327c8
92put k 27 0xbf597fc7
93put k 28 0xc6e00bf3
94put k 29 0xd5a79147
95put k 30 0x06ca6351
96put k 31 0x14292967
97put k 32 0x27b70a85
98put k 33 0x2e1b2138
99put k 34 0x4d2c6dfc
100put k 35 0x53380d13
101put k 36 0x650a7354
102put k 37 0x766a0abb
103put k 38 0x81c2c92e
104put k 39 0x92722c85
105put k 40 0xa2bfe8a1
106put k 41 0xa81a664b
107put k 42 0xc24b8b70
108put k 43 0xc76c51a3
109put k 44 0xd192e819
110put k 45 0xd6990624
111put k 46 0xf40e3585
112put k 47 0x106aa070
113put k 48 0x19a4c116
114put k 49 0x1e376c08
115put k 50 0x2748774c
116put k 51 0x34b0bcb5
117put k 52 0x391c0cb3
118put k 53 0x4ed8aa4a
119put k 54 0x5b9cca4f
120put k 55 0x682e6ff3
121put k 56 0x748f82ee
122put k 57 0x78a5636f
123put k 58 0x84c87814
124put k 59 0x8cc70208
125put k 60 0x90befffa
126put k 61 0xa4506ceb
127put k 62 0xbef9a3f7
128put k 63 0xc67178f2
129end
130
131note H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes
132note (fixtures/sha256-iv.dec).
133fn wsiv s edit uint 8 to void
134put s 0 0x6a09e667
135put s 1 0xbb67ae85
136put s 2 0x3c6ef372
137put s 3 0xa54ff53a
138put s 4 0x510e527f
139put s 5 0x9b05688c
140put s 6 0x1f83d9ab
141put s 7 0x5be0cd19
142end
143
144note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read.
145note The compiler proves that every get of w reads an element written earlier in this function
146note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence.
147fn wssched w edit uint 64 fresh m view uchar * o ulong to void
148q = ulong 0
149x = uint 0
150y = uint 0
151for i ulong 16 unroll
152q = add o mul i 4
153x = uint get m q
154x = or shl x 8 uint get m add q 1
155x = or shl x 8 uint get m add q 2
156put w i or shl x 8 uint get m add q 3
157end
158for i ulong 48 unroll
159x = get w add i 1
160y = get w add i 14
161x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
162put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
163end
164end
165
166note The 64 rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant
167note in each copy, so get kt i and get w i need no run-time check. uint arithmetic wraps mod 2^32.
168note Ch(e,f,g) = (e and f) xor (not e and g) is written g xor (e and (f xor g)); Maj(a,b,c) =
169note (a and b) xor (a and c) xor (b and c) is written (a and b) or (c and (a or b)). Both are bitwise
170note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one.
171note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and
172note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines).
173fn wsround s edit uint 8 w view uint 64 kt view uint 64 to void
174a = get s 0
175b = get s 1
176c = get s 2
177d = get s 3
178e = get s 4
179f = get s 5
180g = get s 6
181h = get s 7
182t = uint 0
183for i ulong 64 unroll
184h = add h get kt i
185h = add h get w i
186t = xor rotr e 6 rotr e 11
187t = xor t rotr e 25
188h = add h t
189h = add h xor g and e xor f g
190d = add d h
191t = xor rotr a 2 rotr a 13
192t = xor t rotr a 22
193h = add h t
194t = and c or a b
195h = add h or and a b t
196t = h
197h = g
198g = f
199f = e
200e = d
201d = c
202c = b
203b = a
204a = t
205end
206put s 0 add get s 0 a
207put s 1 add get s 1 b
208put s 2 add get s 2 c
209put s 3 add get s 3 d
210put s 4 add get s 4 e
211put s 5 add get s 5 f
212put s 6 add get s 6 g
213put s 7 add get s 7 h
214end
215
216note One block m[o .. o+64) into s. sched and rounds stay calls: inlined, their names would share
217note one 16-register frame with this function's parameters (no spilling, error 12).
218fn wscomp s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
219wssched w m o
220wsround s w kt
221end
222
223note The whole blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes).
224fn wsrun s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
225for j ulong shr len m 6
226wscomp s w kt m shl j 6
227end
228end
229
230note The whole blocks of m (len m a multiple of 64). A rolled loop traps 4 after 1000000 passes
231note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 blocks):
232note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches 1000000
233note only above 1.7e19 bytes, which no AArch64 address space holds.
234fn wsblks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
235q = ulong 0
236for a ulong shr len m 44
237for b ulong 0x80000
238wsrun s w kt part m q 0x2000000
239q = add q 0x2000000
240end
241end
242for b ulong and shr len m 25 0x7ffff
243wsrun s w kt part m q 0x2000000
244q = add q 0x2000000
245end
246wsrun s w kt part m q sub len m q
247end
248
249note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the
250note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two blocks.
251fn wsfin s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void
252put p k 0x80
253e = ulong 64
254if ge k 56
255e = 128
256end
257bits = shl n 3
258for i ulong 8 unroll
259put p sub e add i 1 uchar and shr bits mul i 8 0xff
260end
261wscomp s w kt view p 0
262if eq e 128
263wscomp s w kt view p 64
264end
265end
266
267fn sha256_init c edit shactx to void
268s = new uint 8 0
269wsiv s
270for i ulong 8 unroll
271put c state i get s i
272end
273put c count 0
274end
275
276note k bytes of d from o into the context's buffer at bi (bi + k <= 64). A function of its own: inside
277note absorb the copy loop needs more than the 16 registers (error 12 under compiler-s4).
278fn wstobuf c edit shactx bi ulong d view uchar * o ulong k ulong to void
279for i ulong k
280put c buf add bi i get d add o i
281end
282end
283
284note Whole blocks are hashed straight from d; the bytes that complete a pending block go through blk.
285fn wsabsb c edit shactx d view uchar * bi ulong to void
286n = len d
287s = new uint 8 0
288w = new uint 64 0
289kt = new uint 64 0
290blk = new uchar 64 0
291wskinit kt
292for i ulong 8 unroll
293put s i get c state i
294end
295off = ulong 0
296if gt bi 0
297off = sub 64 bi
298for i ulong bi
299put blk i get c buf i
300end
301for i ulong off
302put blk add bi i get d i
303end
304wscomp s w kt view blk 0
305end
306nb = shr sub n off 6
307wsblks s w kt part d off shl nb 6
308off = add off shl nb 6
309wstobuf c 0 d off sub n off
310for i ulong 8 unroll
311put c state i get s i
312end
313end
314
315note Any length, including 0 and >= 2^32. lt n sub 64 bi cannot wrap (bi < 64); add bi n could.
316fn sha256_update c edit shactx d view uchar * to void
317n = len d
318cnt = get c count
319bi = and cnt 63
320put c count add cnt n
321if lt n sub 64 bi
322wstobuf c bi d 0 n
323ret
324end
325wsabsb c d bi
326end
327
328fn sha256_final c edit shactx o out uchar 32 to void
329s = new uint 8 0
330w = new uint 64 0
331kt = new uint 64 0
332p = new uchar 128 0
333v = ulong 0
334wskinit kt
335for i ulong 8 unroll
336put s i get c state i
337end
338cnt = get c count
339k = and cnt 63
340for i ulong k
341put p i get c buf i
342end
343wsfin s w kt p k cnt
344for i ulong 8 unroll
345v = ulong get s i
346for j ulong 4 unroll
347put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
348end
349end
350end
351
352note Equal to init, update(d), final on a fresh context (section 7), with the state in locals. o may
353note overlap d: the binding copies o out only after the call returns status 0.
354fn sha256_hash d view uchar * o out uchar 32 to void
355s = new uint 8 0
356w = new uint 64 0
357kt = new uint 64 0
358p = new uchar 128 0
359v = ulong 0
360wskinit kt
361wsiv s
362n = len d
363nb = shr n 6
364wsblks s w kt part d 0 shl nb 6
365off = shl nb 6
366k = sub n off
367for i ulong k
368put p i get d add off i
369end
370wsfin s w kt p k n
371for i ulong 8 unroll
372v = ulong get s i
373for j ulong 4 unroll
374put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
375end
376end
377end
378
379note wdrive library. Globals are named wd*, functions wd*. Host words take these globals directly;
380note an Omega function never receives a global as an argument (error 19).
381note wdab argument and scratch bytes; wdp/wdpn the path builder; wdk/wdkn the execprog packed list
382note (count, write position); wdo/wdon/wdofd the output buffer and the descriptor it flushes to when
383note full; wdsrc/wdsn a whole file; wdd the last digest; wdv/wdvn the last environment value.
384global wdab uchar 4096
385global wdp uchar 4096
386global wdpn ulong 1
387global wdk uchar 4096
388global wdkn ulong 2
389global wdo uchar 65536
390global wdon ulong 1
391global wdofd int 1
392global wdsrc uchar 8388608
393global wdsn ulong 1
394global wdd uchar 32
395global wdv uchar 4096
396global wdvn ulong 1
397global wdst uchar 4
398note wdlkf: the held pool lock's descriptor plus 1, 0 when none (closed in spawned children, as
399note flock -o does)
400global wdlkf long 1
401global wdt uchar 24
402text wdhex "0123456789abcdef"
403
404note argument i into wdab; returns its length (exit 2 when it does not fit)
405fn wdarg i ulong to ulong
406r = argread i wdab 0 4096
407if lt r 0
408exit 2
409end
410ret ulong r
411end
412
413fn wdpclr to void
414put wdpn 0 0
415end
416
417fn wdpc c ulong to void
418n = get wdpn 0
419if ge n 4095
420exit 2
421end
422put wdp n uchar c
423put wdpn 0 add n 1
424end
425
426fn wdpt s view uchar * to void
427for i ulong len s
428wdpc ulong get s i
429end
430end
431
432fn wdpa i ulong to void
433n = wdarg i
434for j ulong n
435wdpc ulong get wdab j
436end
437end
438
439fn wdpv to void
440for j ulong get wdvn 0
441wdpc ulong get wdv j
442end
443end
444
445note wdsrc[o .. o + n) onto the path
446fn wdpb o ulong n ulong to void
447for j ulong n
448wdpc ulong get wdsrc add o j
449end
450end
451
452note decimal digits of v into wdt, least significant first; returns the count
453fn wddec v ulong to ulong
454k = ulong 0
455x = v
456c = true
457while c bound 25
458put wdt k uchar add 48 rem x 10
459k = add k 1
460x = div x 10
461c = gt x 0
462end
463ret k
464end
465
466fn wdpd v ulong to void
467k = wddec v
468while gt k 0 bound 25
469k = sub k 1
470wdpc ulong get wdt k
471end
472end
473
474note open the path in wdp; returns the descriptor or minus errno
475fn wdopen fl int md int to int
476r = fopen wdp 0 get wdpn 0 fl md
477ret int r
478end
479
480fn wdexists to bool
481r = faccess wdp 0 get wdpn 0 0
482ret eq r 0
483end
484
485note packed list for execprog: u16 count, then per record u16 length and bytes
486fn wdkclr to void
487put wdkn 0 0
488put wdkn 1 2
489end
490
491note push the path builder's bytes as the next record
492fn wdkp to void
493c = get wdkn 0
494p = get wdkn 1
495n = get wdpn 0
496if or ge c 64 gt add add p 2 n 4096
497exit 2
498end
499put wdk p uchar and n 255
500put wdk add p 1 uchar shr n 8
501for j ulong n
502put wdk add add p 2 j get wdp j
503end
504put wdkn 0 add c 1
505put wdkn 1 add add p 2 n
506end
507
508fn wdkt s view uchar * to void
509wdpclr
510wdpt s
511wdkp
512end
513
514fn wdka i ulong to void
515wdpclr
516wdpa i
517wdkp
518end
519
520fn wdkfin to ulong
521c = get wdkn 0
522put wdk 0 uchar and c 255
523put wdk 1 uchar shr c 8
524ret get wdkn 1
525end
526
527note fork and exec the packed list (empty environment, no PATH search); the child takes fi as
528note stdin, fo as stdout and fe as stderr when they are not negative. Returns the child's pid.
529fn wdspawn fi int fo int fe int to long
530pn = wdkfin
531pid = fork
532if lt pid 0
533exit 70
534end
535if eq pid 0
536if gt get wdlkf 0 0
537cl = fclose int sub get wdlkf 0 1
538end
539if ge fi 0
540d0 = dupfd fi 0 0
541end
542if ge fo 0
543d1 = dupfd fo 1 0
544end
545if ge fe 0
546d2 = dupfd fe 2 0
547end
548e = execprog wdk 0 pn
549exit 127
550end
551ret pid
552end
553
554note wait for pid; its exit status, or 128 + the signal that ended it
555fn wdwait pid long to long
556r = waitpid int pid wdst 0 4 0
557while eq r -4 bound 1000
558r = waitpid int pid wdst 0 4 0
559end
560if lt r 0
561exit 70
562end
563s = uint get wdst 0
564s = or s shl uint get wdst 1 8
565s = or s shl uint get wdst 2 16
566s = or s shl uint get wdst 3 24
567if eq and s 127 0
568ret long and shr s 8 255
569end
570ret add long and s 127 128
571end
572
573note value of environment entry NM=VALUE into wdv/wdvn; false when absent
574fn wdenv nm view uchar * to bool
575c = ulong envcnt
576for i ulong c
577r = envread i wdv 0 4096
578if gt r long len nm
579if wdenvm nm ulong r
580ret true
581end
582end
583end
584ret false
585end
586
587fn wdenvm nm view uchar * r ulong to bool
588k = len nm
589for i ulong k
590if ne get wdv i get nm i
591ret false
592end
593end
594if ne get wdv k 61
595ret false
596end
597m = sub r add k 1
598for j ulong m
599put wdv j get wdv add add k 1 j
600end
601put wdvn 0 m
602ret true
603end
604
605note the output buffer; wdoc flushes to wdofd when full
606fn wdowr fd int to void
607n = get wdon 0
608t = ulong 0
609while lt t n bound 999999
610w = write fd wdo t sub n t
611if le w 0
612exit 70
613end
614t = add t ulong w
615end
616end
617
618fn wdoclr to void
619put wdon 0 0
620end
621
622fn wdoflush fd int to void
623wdowr fd
624wdoclr
625end
626
627fn wdoc c ulong to void
628n = get wdon 0
629if ge n 65536
630wdoflush get wdofd 0
631n = 0
632end
633put wdo n uchar c
634put wdon 0 add n 1
635end
636
637fn wdot s view uchar * to void
638for i ulong len s
639wdoc ulong get s i
640end
641end
642
643fn wdod v ulong to void
644k = wddec v
645while gt k 0 bound 25
646k = sub k 1
647wdoc ulong get wdt k
648end
649end
650
651fn wdop to void
652for j ulong get wdpn 0
653wdoc ulong get wdp j
654end
655end
656
657fn wdohex to void
658for i ulong 32
659v = ulong get wdd i
660wdoc ulong get wdhex shr v 4
661wdoc ulong get wdhex and v 15
662end
663end
664
665note true when the hex of wdd equals the 64 bytes of h
666fn wdhexeq h view uchar * to bool
667if ne len h 64
668ret false
669end
670for i ulong 32
671v = ulong get wdd i
672if ne get h mul i 2 get wdhex shr v 4
673ret false
674end
675if ne get h add mul i 2 1 get wdhex and v 15
676ret false
677end
678end
679ret true
680end
681
682note read the whole file named by wdp into wdsrc; returns its size or minus errno; exit 61 when it
683note is larger than wdsrc
684fn wdload to long
685b = new uchar 1 0
686fd = wdopen 0 0
687if lt fd 0
688ret long fd
689end
690t = ulong 0
691fin = false
692while and lt t len wdsrc not fin bound 999999
693r = read fd wdsrc t sub len wdsrc t
694if lt r 0
695c = fclose fd
696ret r
697end
698if eq r 0
699fin = true
700end
701t = add t ulong r
702end
703if not fin
704r2 = read fd b 0 1
705if ne r2 0
706exit 61
707end
708end
709c2 = fclose fd
710put wdsn 0 t
711ret long t
712end
713
714note SHA-256 of the file named by wdp into wdd; false when it cannot be opened, exit 70 on a read error
715fn wdfsha to bool
716fd = wdopen 0 0
717if lt fd 0
718ret false
719end
720b = new uchar 65536 0
721c = new uchar 104 0
722d = new uchar 32 0
723sha256_init c
724fin = false
725while not fin bound 999999
726r = read fd b 0 65536
727if lt r 0
728exit 70
729end
730if eq r 0
731fin = true
732else
733sha256_update c part b 0 ulong r
734end
735end
736c2 = fclose fd
737sha256_final c d
738for i ulong 32
739put wdd i get d i
740end
741ret true
742end
743
744note SHA-256 of wdsrc[0, wdsn) into wdd (a global cannot be passed, so it goes through a local copy)
745fn wdmsha to void
746b = new uchar 65536 0
747c = new uchar 104 0
748d = new uchar 32 0
749sha256_init c
750n = get wdsn 0
751o = ulong 0
752while lt o n bound 999999
753k = sub n o
754if gt k 65536
755k = ulong 65536
756end
757for j ulong k
758put b j get wdsrc add o j
759end
760sha256_update c part b 0 k
761o = add o k
762end
763sha256_final c d
764for i ulong 32
765put wdd i get d i
766end
767end
768
769note copy everything readable from descriptor fi to descriptor fo
770fn wdcopyfd fi int fo int to void
771b = new uchar 65536 0
772fin = false
773while not fin bound 999999
774r = read fi b 0 65536
775if lt r 0
776exit 70
777end
778if eq r 0
779fin = true
780else
781wdwrall fo b ulong r
782end
783end
784end
785
786fn wdwrall fd int b view uchar * n ulong to void
787t = ulong 0
788while lt t n bound 999999
789w = write fd b t sub n t
790if le w 0
791exit 70
792end
793t = add t ulong w
794end
795end
796
797note rename the first record of the packed list to the second (renameat2 flags 0)
798fn wdrename to void
799pn = wdkfin
800r = renames wdk 0 pn 0
801if ne r 0
802exit 70
803end
804end
805
806note string slots: slot k is wdz[wdzo[k] .. wdzo[k] + wdzl[k]). wdzpool fills 0 HOME, 1 POOL, 2 LOCK;
807note a tool numbers its own slots from 3.
808global wdz uchar 65536
809global wdzo ulong 16
810global wdzl ulong 16
811global wdzn ulong 1
812text wdthome "HOME"
813text wdtpool "POOL"
814text wdtlock "LOCK"
815text wddpool "19"
816text wddlock "/handoffs/hive/queen/pool-"
817text wddlk2 ".lock"
818text wdtlkto "LOCK_TIMEOUT "
819text wdtlkno "cannot open lock "
820text wdtnl "\n"
821text wdxts "/usr/bin/taskset"
822text wdxc "-c"
823text wdxnice "/usr/bin/nice"
824text wdxn "-n"
825text wdx10 "10"
826text wdtnoh "HOME is not set\n"
827
828note save the path builder as slot k
829fn wdzsave k ulong to void
830p = get wdzn 0
831n = get wdpn 0
832if gt add p n 65536
833exit 2
834end
835for j ulong n
836put wdz add p j get wdp j
837end
838put wdzo k p
839put wdzl k n
840put wdzn 0 add p n
841end
842
843note append slot k to the path builder
844fn wdzp k ulong to void
845o = get wdzo k
846for j ulong get wdzl k
847wdpc ulong get wdz add o j
848end
849end
850
851note slot k from environment entry nm, else HOME followed by d
852fn wdzenv nm view uchar * k ulong d view uchar * to void
853wdpclr
854if wdenv nm
855wdpv
856else
857wdzp 0
858wdpt d
859end
860wdzsave k
861end
862
863note slots 0 HOME (exit 2 when unset), 1 POOL (default 19), 2 LOCK (default
864note HOME/handoffs/hive/queen/pool-POOL.lock)
865fn wdzpool to void
866if not wdenv wdthome
867wdoclr
868wdot wdtnoh
869wdoflush 2
870exit 2
871end
872wdpclr
873wdpv
874wdzsave 0
875wdpclr
876if wdenv wdtpool
877wdpv
878else
879wdpt wddpool
880end
881wdzsave 1
882wdpclr
883if wdenv wdtlock
884wdpv
885else
886wdzp 0
887wdpt wddlock
888wdzp 1
889wdpt wddlk2
890end
891wdzsave 2
892end
893
894note start the packed list with the pool prefix (7 records):
895note /usr/bin/taskset -c POOL /usr/bin/nice -n 10
896fn wdkpre to void
897wdkclr
898wdkt wdxts
899wdkt wdxc
900wdpclr
901wdzp 1
902wdkp
903wdkt wdxnice
904wdkt wdxn
905wdkt wdx10
906end
907
908
909fn wdlkmsg t view uchar * to void
910wdoclr
911wdot t
912wdpclr
913wdzp 2
914wdop
915wdot wdtnl
916wdoflush 2
917end
918
919note take the pool lock LOCK (slot 2) as `flock -w 7200` did: open it read-only, creating it with
920note mode 0666, and try an exclusive flock every 10 ms for at most 7200 s. False on timeout.
921fn wdlock to bool
922wdpclr
923wdzp 2
924fd = wdopen 64 438
925if lt fd 0
926wdlkmsg wdtlkno
927exit 70
928end
929d = add monons 7200000000000
930while true bound 999999
931r = flock fd 6
932if eq r 0
933put wdlkf 0 add long fd 1
934ret true
935end
936if ne r -11
937wdlkmsg wdtlkno
938exit 70
939end
940if gt monons d
941c = fclose fd
942wdlkmsg wdtlkto
943ret false
944end
945s = sleepns 10000000
946end
947ret false
948end
949
950note run the packed list (built after wdkpre) under the pool lock: lock, spawn, wait, unlock.
951note Returns the child's status, or 1 when the lock was not had within 7200 s (flock -w's status).
952fn wdpoolrun fi int fo int fe int to long
953if not wdlock
954ret 1
955end
956pid = wdspawn fi fo fe
957rc = wdwait pid
958c = fclose int sub get wdlkf 0 1
959put wdlkf 0 0
960ret rc
961end
962
963note wbuild ------------------------------------------------------------------------------------
964global wbomc uchar 4096
965global wbomn ulong 1
966global wbcore uchar 64
967global wbcn ulong 1
968global wbren uchar 64
969global wbrnn ulong 1
970global wbom2 ulong 1
971text wbpin "2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f"
972text wbtomc "OMC"
973text wbtcore "CORE"
974text wbtall "-all.om"
975text wbttmp ".tmp"
976text wbtts "/usr/bin/taskset"
977text wbtc "-c"
978text wbtnice "/usr/bin/nice"
979text wbtn "-n"
980text wbt19 "19"
981text wbtom2 "om2"
982text wbtlay "layout "
983text wbtend "end"
984text wbtmain "fn main to int"
985text wbtfn "fn "
986text wbtto " to int"
987text wbtnl "\n"
988text wbtok "BUILD_OK "
989text wbtsrc " source_sha256 "
990text wbtomcs " omc "
991text wbtfail "BUILD_FAIL omc rc="
992text wbtpinm "OMC_PIN_MISMATCH "
993text wbtmc "MAIN_COUNT "
994text wbtwant ", want 1 in "
995text wbtrd "cannot read "
996text wbtpbad "PART_PIN_BAD "
997text wbtpmis "PART_PIN_MISMATCH "
998text wbtexp "export fn "
999text wbtuse "usage: OMC=omc [CORE=n] wbuild OUTBIN PART... (PART is FILE[@NAME][=SHA256HEX])\n"
1000
1001fn wbusage to void
1002wdoclr
1003wdot wbtuse
1004wdoflush 2
1005exit 2
1006end
1007
1008fn wbpomc to void
1009wdpclr
1010for j ulong get wbomn 0
1011wdpc ulong get wbomc j
1012end
1013end
1014
1015fn wbpcore to void
1016wdpclr
1017for j ulong get wbcn 0
1018wdpc ulong get wbcore j
1019end
1020end
1021
1022note index of the end of the line starting at s (after its newline, or n)
1023fn wbeol s ulong n ulong to ulong
1024i = s
1025while lt i n bound 999999
1026if eq get wdsrc i 10
1027ret add i 1
1028end
1029i = add i 1
1030end
1031ret n
1032end
1033
1034note content length of the line [s, e): without its newline
1035fn wbcl s ulong e ulong to ulong
1036if gt e s
1037if eq get wdsrc sub e 1 10
1038ret sub sub e s 1
1039end
1040end
1041ret sub e s
1042end
1043
1044note wdsrc[s .. s + cl) equals t (pre false) or starts with t (pre true)
1045fn wbeq s ulong cl ulong t view uchar * pre bool to bool
1046k = len t
1047if lt cl k
1048ret false
1049end
1050if and not pre ne cl k
1051ret false
1052end
1053for i ulong k
1054if ne get wdsrc add s i get t i
1055ret false
1056end
1057end
1058ret true
1059end
1060
1061note 1 om2, 2 layout start, 3 end, 4 main, else 0
1062fn wbkind s ulong e ulong to ulong
1063cl = wbcl s e
1064if wbeq s cl wbtom2 false
1065ret 1
1066end
1067if wbeq s cl wbtlay true
1068ret 2
1069end
1070if wbeq s cl wbtend false
1071ret 3
1072end
1073if wbeq s cl wbtmain false
1074ret 4
1075end
1076ret 0
1077end
1078
1079fn wbemit s ulong e ulong to void
1080for j ulong sub e s
1081wdoc ulong get wdsrc add s j
1082end
1083end
1084
1085fn wbemitm s ulong e ulong to void
1086wdot wbtfn
1087for j ulong get wbrnn 0
1088wdoc ulong get wbren j
1089end
1090wdot wbtto
1091if ne wbcl s e sub e s
1092wdoc 10
1093end
1094end
1095
1096note one pass over the part in wdsrc: 0 counts om2 and main lines, 1 emits layout blocks, 2 emits
1097note the rest (main renamed when the part has a name; a line starting `export fn ` loses its
1098note `export `, since the output is a program and a program has no exports); returns the number of
1099note main lines
1100fn wblines pass ulong to ulong
1101n = get wdsn 0
1102s = ulong 0
1103mains = ulong 0
1104inl = false
1105while lt s n bound 999999
1106e = wbeol s n
1107k = wbkind s e
1108if eq k 2
1109inl = true
1110end
1111if eq pass 0
1112if eq k 1
1113put wbom2 0 1
1114end
1115if eq k 4
1116mains = add mains 1
1117end
1118end
1119if eq pass 1
1120if inl
1121wbemit s e
1122end
1123end
1124if eq pass 2
1125if not or inl eq k 1
1126if and eq k 4 gt get wbrnn 0 0
1127wbemitm s e
1128else
1129if wbeq s wbcl s e wbtexp true
1130wbemit add s 7 e
1131else
1132wbemit s e
1133end
1134end
1135end
1136end
1137if and inl eq k 3
1138inl = false
1139end
1140s = e
1141end
1142ret mains
1143end
1144
1145note wbpinx/wbpinn: the current part's pin (64 lowercase hex digits), wbpinn 0 when it has none
1146global wbpinx uchar 64
1147global wbpinn ulong 1
1148
1149note wdab[0, n) holds a PART. When it has an `=`, the text after the last `=` must be 64 lowercase
1150note hex digits (else stderr PART_PIN_BAD PART, exit 2); it becomes the pin and the PART ends at the
1151note `=`. Returns the PART's length without the pin.
1152fn wbpinparse n ulong to ulong
1153q = n
1154for j ulong n
1155if eq get wdab j 61
1156q = j
1157end
1158end
1159put wbpinn 0 0
1160if eq q n
1161ret n
1162end
1163ok = eq sub sub n q 1 64
1164if ok
1165for k ulong 64
1166v = get wdab add add q 1 k
1167if not or and ge v 48 le v 57 and ge v 97 le v 102
1168ok = false
1169end
1170put wbpinx k v
1171end
1172end
1173if not ok
1174wdoclr
1175wdot wbtpbad
1176for j2 ulong n
1177wdoc ulong get wdab j2
1178end
1179wdot wbtnl
1180wdoflush 2
1181exit 2
1182end
1183put wbpinn 0 64
1184ret q
1185end
1186
1187note true when the hex of wdd equals the pin wbpinx
1188fn wbpineq to bool
1189for i ulong 32
1190v = ulong get wdd i
1191if ne get wbpinx mul i 2 get wdhex shr v 4
1192ret false
1193end
1194if ne get wbpinx add mul i 2 1 get wdhex and v 15
1195ret false
1196end
1197end
1198ret true
1199end
1200
1201note argument i is FILE, FILE@NAME, FILE=HEX64 or FILE@NAME=HEX64: load FILE, check its pin, set the
1202note rename, run pass
1203fn wbpart i ulong pass ulong to void
1204n = wbpinparse wdarg i
1205ap = n
1206for j ulong n
1207if eq get wdab j 64
1208ap = j
1209end
1210end
1211put wbrnn 0 0
1212if lt ap n
1213m = sub sub n ap 1
1214if or eq m 0 gt m 63
1215exit 2
1216end
1217for j2 ulong m
1218put wbren j2 get wdab add add ap 1 j2
1219end
1220put wbrnn 0 m
1221end
1222wdpclr
1223for j3 ulong ap
1224wdpc ulong get wdab j3
1225end
1226r = wdload
1227if lt r 0
1228wdoclr
1229wdot wbtrd
1230wdop
1231wdot wbtnl
1232wdoflush 2
1233exit 70
1234end
1235if gt get wbpinn 0 0
1236wdmsha
1237if not wbpineq
1238wdoclr
1239wdot wbtpmis
1240wdop
1241wdoc 32
1242wdohex
1243wdot wbtnl
1244wdoflush 2
1245exit 3
1246end
1247end
1248c = wblines pass
1249if and eq pass 0 gt get wbrnn 0 0
1250if ne c 1
1251wdoclr
1252wdot wbtmc
1253wdod c
1254wdot wbtwant
1255wdop
1256wdot wbtnl
1257wdoflush 2
1258exit 4
1259end
1260end
1261end
1262
1263note OUTBIN followed by the suffix t
1264fn wbpout t view uchar * to void
1265wdpclr
1266wdpa 1
1267wdpt t
1268end
1269
1270fn wbcompose na ulong to void
1271for i ulong sub na 2
1272wbpart add i 2 0
1273end
1274wbpout wbtall
1275fd = wdopen 577 436
1276if lt fd 0
1277exit 70
1278end
1279put wdofd 0 fd
1280wdoclr
1281if eq get wbom2 0 1
1282wdot wbtom2
1283wdot wbtnl
1284end
1285for p ulong 2
1286for i2 ulong sub na 2
1287wbpart add i2 2 add p 1
1288end
1289end
1290wdoflush fd
1291put wdofd 0 1
1292c = fclose fd
1293if ne c 0
1294exit 70
1295end
1296end
1297
1298note taskset -c CORE nice -n 19 OMC < OUTBIN-all.om > OUTBIN.tmp; returns omc's exit status
1299fn wbcomp to long
1300wbpout wbtall
1301fi = wdopen 0 0
1302wbpout wbttmp
1303fo = wdopen 577 436
1304if or lt fi 0 lt fo 0
1305exit 70
1306end
1307wdkclr
1308wdkt wbtts
1309wdkt wbtc
1310wbpcore
1311wdkp
1312wdkt wbtnice
1313wdkt wbtn
1314wdkt wbt19
1315wbpomc
1316wdkp
1317pid = wdspawn fi fo -1
1318c1 = fclose fi
1319c2 = fclose fo
1320ret wdwait pid
1321end
1322
1323fn wbenv to void
1324if not wdenv wbtomc
1325wbusage
1326end
1327for j ulong get wdvn 0
1328put wbomc j get wdv j
1329end
1330put wbomn 0 get wdvn 0
1331put wbcore 0 48
1332put wbcn 0 1
1333if wdenv wbtcore
1334if or eq get wdvn 0 0 gt get wdvn 0 63
1335exit 2
1336end
1337for j2 ulong get wdvn 0
1338put wbcore j2 get wdv j2
1339end
1340put wbcn 0 get wdvn 0
1341end
1342end
1343
1344fn wbchkpin to void
1345wbpomc
1346if not wdfsha
1347exit 70
1348end
1349if not wdhexeq wbpin
1350wdoclr
1351wdot wbtpinm
1352wdohex
1353wdot wbtnl
1354wdoflush 2
1355exit 3
1356end
1357end
1358
1359fn wbfinish to void
1360wbpout wbttmp
1361r = chmod wdp 0 get wdpn 0 509
1362if ne r 0
1363exit 70
1364end
1365wdkclr
1366wdkp
1367wdpclr
1368wdpa 1
1369wdkp
1370wdrename
1371end
1372
1373fn main to int
1374na = ulong argcnt
1375if lt na 3
1376wbusage
1377end
1378wbenv
1379wbchkpin
1380wbcompose na
1381rc = wbcomp
1382if ne rc 0
1383wbpout wbttmp
1384u = unlink wdp 0 get wdpn 0
1385wdoclr
1386wdot wbtfail
1387wdod ulong rc
1388wdot wbtnl
1389wdoflush 2
1390exit 1
1391end
1392wbfinish
1393wdoclr
1394wdot wbtok
1395wdpclr
1396wdpa 1
1397if not wdfsha
1398exit 70
1399end
1400wdohex
1401wdoc 32
1402wdop
1403wdot wbtsrc
1404wbpout wbtall
1405if not wdfsha
1406exit 70
1407end
1408wdohex
1409wdot wbtomcs
1410wbpomc
1411if not wdfsha
1412exit 70
1413end
1414wdohex
1415wdot wbtnl
1416wdoflush 1
1417ret 0
1418end