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 / ngbuild.om

496 lines · 11603 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/ngbuild.om: builds an Omega program from an ordered list of sha256-pinned units (D1b): ng
3note from seed/ng/ng.units, ngbuild itself from seed/ng/ngbuild.units, each NG tool from its own
4note seed/ng/TOOL.units (seed/ng/README.md). Owner: ng-ir lane.
5note First birth, no cat: seed/ng/tests/ngbuild/gate.om joins seed/ng/ngbuild.units itself, compiles it,
6note and requires the result to rebuild itself byte for byte with every pin checked (README.md).
7note Run from the repository root.
8note ngbuild build OMC UNITS OUT check UNITS and every unit's sha256 pin, concatenate the units in
9note order into OUT.src (exactly the bytes `cat` of the listed paths
10note writes: nothing is added between units), compile it with OMC
11note (stdin OUT.src, stdout OUT, mode 0755), print on stderr
12note "ngbuild: OK units=N bytes=M sha256=HEX" (sha256 of OUT)
13note ngbuild sums UNITS print each unit's current "HEX PATH" line (to refresh UNITS)
14note UNITS: one unit per line, "HEX PATH": 64 lowercase hex digits, one space, a path (relative to the
15note working directory, or absolute; 1..4095 bytes, no NUL), a newline. No other lines. The line order is the build
16note order; there is no dependency metadata, so a cycle cannot be written. At most 64 units, UNITS at
17note most 65536 bytes, the units at most 8388607 bytes in total (the omc input capacity, OMEGESE s9.6).
18note The whole of UNITS is checked before any unit is read, and every unit before OUT.src is opened
19note or OMC runs; so exits 2..5, 8 and 9 from those checks write no file and run nothing. After OMC
20note exits 0, OUT is read back for its sha256: exit 5 (OUT unreadable) or 9 (OUT of 16777216 bytes or
21note more) then leaves OUT.src and OUT.
22note Exit 0 built (or sums printed)
23note 1 omc refused: "ngbuild: omc exit N" (omc's own stderr line passes through); an exec or
24note redirect failure in the child is "ngbuild: omc exit 127"
25note 2 usage, also any argument count other than build's 4 or sums' 2 (checked before any is read)
26note 3 a pin is not the unit's sha256: "ngbuild: sha256 mismatch PATH have HEX"
27note 4 malformed UNITS: "ngbuild: malformed units line OFFSET" (byte offset of the line), or
28note "ngbuild: no units" (empty UNITS)
29note 5 UNITS or a unit unreadable (missing, a directory): "ngbuild: unreadable: PATH"
30note 6 OUT.src or OUT not writable: "ngbuild: cannot write output"
31note 7 fork or wait failed, or OMC ended by a signal: "ngbuild: fork or wait failed, or omc ended
32note by a signal"; OUT.src, and OUT once opened, can remain after exits 1, 6 and 7
33note 8 a unit listed twice: the same path on two lines, or (build) the same pin on two lines:
34note "ngbuild: duplicate unit line N: PATH" (N counts from 1; the later of the two lines)
35note 9 over a limit: "ngbuild: more than 64 units", or "ngbuild: too large: PATH" for UNITS over
36note 65536 bytes, a unit of 16777216 bytes or more, or (build) the unit that takes the total
37note past 8388607 bytes
38text nb_txt "build\nsums\n.src\nngbuild: OK units=\n bytes=\n sha256=\nngbuild: sha256 mismatch \n have \nngbuild: omc exit \nngbuild: malformed units line \nngbuild: unreadable: \nngbuild: cannot write output\nngbuild: fork or wait failed, or omc ended by a signal\nngbuild: usage: ngbuild build OMC UNITS OUT | ngbuild sums UNITS\nngbuild: too large: \nngbuild: more than 64 units\nngbuild: no units\nngbuild: duplicate unit line \n: \n"
39global nb_u uchar 65536
40global nb_un ulong 1
41global nb_src uchar 8388608
42global nb_sn ulong 1
43global nb_a uchar 4096
44global nb_al ulong 128
45global nb_p uchar 4096
46global nb_pn ulong 1
47global nb_hx uchar 66
48global nb_d uchar 24
49global nb_st uchar 4
50global nb_av uchar 8192
51global nb_ap ulong 2
52note unit k: its line starts at nb_ls[k] (the pin), its path (nb_pl[k] bytes) at nb_ls[k] + 65
53global nb_ls ulong 64
54global nb_pl ulong 64
55global nb_uc ulong 1
56note ---- output ---------------------------------------------------------------------------------
57note line k (1-based) of nb_txt, without its newline
58fn nb_say k ulong to void
59s = ulong 0
60c = ulong 1
61for i ulong len nb_txt
62if eq get nb_txt i 10
63if eq c k
64r = write 2 nb_txt s sub i s
65ret
66end
67c = add c 1
68s = add i 1
69end
70end
71end
72fn nb_nl to void
73put nb_d 0 10
74r = write 2 nb_d 0 1
75end
76fn nb_num v ulong to void
77x = v
78i = ulong 24
79for j ulong 21
80i = sub i 1
81put nb_d i uchar add 48 rem x 10
82x = div x 10
83if eq x 0
84r = write 2 nb_d i sub 24 i
85ret
86end
87end
88end
89note message line k, the path in nb_p, a newline; exit x
90fn nb_sayp k ulong x int to void
91nb_say k
92r = write 2 nb_p 0 get nb_pn 0
93nb_nl
94exit x
95end
96note hex of sh_dig into nb_hx[0..64)
97fn nb_hex to void
98for j ulong 32
99b = uint get sh_dig j
100put nb_hx mul j 2 sh_hexd shr b 4
101put nb_hx add mul j 2 1 sh_hexd and b 15
102end
103end
104note ---- arguments and files --------------------------------------------------------------------
105note argument i into nb_a at offset nb_al[2i], length nb_al[2i+1]; false when absent or too long
106fn nb_arg i ulong to bool
107o = ulong 0
108if gt i 0
109o = add get nb_al sub mul i 2 2 get nb_al sub mul i 2 1
110end
111k = argread i nb_a o sub len nb_a o
112if lt k 0
113ret false
114end
115put nb_al mul i 2 o
116put nb_al add mul i 2 1 ulong k
117ret true
118end
119fn nb_aeq i ulong w ulong to bool
120o = get nb_al mul i 2
121n = get nb_al add mul i 2 1
122s = ulong 0
123c = ulong 1
124for j ulong len nb_txt
125if eq get nb_txt j 10
126if eq c w
127if ne n sub j s
128ret false
129end
130for q ulong n bound 4096
131if ne get nb_a add o q get nb_txt add s q
132ret false
133end
134end
135ret true
136end
137c = add c 1
138s = add j 1
139end
140end
141ret false
142end
143note read the file at nb_p[0..nb_pn) into sh_dat: its length; the capacity + 1 when it holds the
144note capacity or more (too large), the capacity + 2 when it cannot be opened or read (unreadable)
145fn nb_load to ulong
146fd = fopen nb_p 0 get nb_pn 0 0 0
147if lt fd 0
148ret add len sh_dat 2
149end
150n = ulong 0
151while lt n len sh_dat bound 16777217
152k = read int fd sh_dat n sub len sh_dat n
153if lt k 0
154r = fclose int fd
155ret add len sh_dat 2
156end
157if eq k 0
158r = fclose int fd
159ret n
160end
161n = add n ulong k
162end
163r = fclose int fd
164ret add len sh_dat 1
165end
166note refuse a file nb_load could not take (n its result): unreadable 5, too large 9
167fn nb_chk n ulong to void
168if eq n add len sh_dat 2
169nb_sayp 11 5
170end
171if gt n len sh_dat
172nb_sayp 15 9
173end
174end
175note path of argument i into nb_p
176fn nb_ap2p i ulong to void
177o = get nb_al mul i 2
178n = get nb_al add mul i 2 1
179for j ulong n bound 4096
180put nb_p j get nb_a add o j
181end
182put nb_pn 0 n
183end
184note read UNITS (argument i) into nb_u
185fn nb_units i ulong to void
186nb_ap2p i
187n = nb_load
188nb_chk n
189if gt n len nb_u
190nb_sayp 15 9
191end
192for j ulong n bound 65536
193put nb_u j get sh_dat j
194end
195put nb_un 0 n
196end
197note ---- units ----------------------------------------------------------------------------------
198note parse the line at nb_u[s..) as unit k (format above) into nb_ls, nb_pl; the offset after its newline
199fn nb_line s ulong k ulong to ulong
200n = get nb_un 0
201if gt add s 66 n
202nb_bad s
203end
204for j ulong 64
205c = get nb_u add s j
206if not or and ge c 48 le c 57 and ge c 97 le c 102
207nb_bad s
208end
209end
210if ne get nb_u add s 64 32
211nb_bad s
212end
213p = add s 65
214e = p
215while lt e n bound 65536
216c = get nb_u e
217if eq c 10
218break
219end
220if eq c 0
221nb_bad s
222end
223e = add e 1
224end
225if or or ge e n eq e p gt sub e p 4095
226nb_bad s
227end
228put nb_ls k s
229put nb_pl k sub e p
230ret add e 1
231end
232fn nb_bad s ulong to void
233nb_say 10
234nb_num s
235nb_nl
236exit 4
237end
238note path of unit k into nb_p
239fn nb_setp k ulong to void
240p = add get nb_ls k 65
241n = get nb_pl k
242for j ulong n bound 4096
243put nb_p j get nb_u add p j
244end
245put nb_pn 0 n
246end
247fn nb_eqb a ulong b ulong n ulong to bool
248for j ulong n bound 4096
249if ne get nb_u add a j get nb_u add b j
250ret false
251end
252end
253ret true
254end
255note true when units i and k have the same path, or (pins) the same pin
256fn nb_dup i ulong k ulong pins bool to bool
257a = get nb_ls i
258b = get nb_ls k
259n = get nb_pl i
260if eq n get nb_pl k
261if nb_eqb add a 65 add b 65 n
262ret true
263end
264end
265if pins
266ret nb_eqb a b 64
267end
268ret false
269end
270note check all of UNITS before any unit is read: every line well formed (4), at least one unit (4),
271note at most 64 (9), no path twice and (pins) no pin twice (8); sets nb_uc
272fn nb_scan pins bool to void
273s = ulong 0
274u = ulong 0
275while lt s get nb_un 0 bound 65
276if eq u 64
277nb_say 16
278nb_nl
279exit 9
280end
281s = nb_line s u
282u = add u 1
283end
284if eq u 0
285nb_say 17
286nb_nl
287exit 4
288end
289put nb_uc 0 u
290for k ulong u bound 64
291for i ulong k bound 64
292if nb_dup i k pins
293nb_setp k
294nb_say 18
295nb_num add k 1
296nb_say 19
297r = write 2 nb_p 0 get nb_pn 0
298nb_nl
299exit 8
300end
301end
302end
303end
304note true when the 64 hex digits at nb_u[s..) equal nb_hx
305fn nb_same s ulong to bool
306for j ulong 64
307if ne get nb_u add s j get nb_hx j
308ret false
309end
310end
311ret true
312end
313fn nb_sums to int
314nb_scan false
315for k ulong get nb_uc 0 bound 64
316nb_setp k
317n = nb_load
318nb_chk n
319sh_all n
320nb_hex
321put nb_hx 64 32
322r = write 1 nb_hx 0 65
323r = write 1 nb_p 0 get nb_pn 0
324put nb_d 0 10
325r = write 1 nb_d 0 1
326end
327ret 0
328end
329note every unit in order: its size against the total (9), its pin (3), then appended to nb_src
330fn nb_cat to void
331for k ulong get nb_uc 0 bound 64
332nb_setp k
333n = nb_load
334nb_chk n
335o = get nb_sn 0
336if gt add o n sub len nb_src 1
337nb_sayp 15 9
338end
339sh_all n
340nb_hex
341if not nb_same get nb_ls k
342nb_say 7
343r = write 2 nb_p 0 get nb_pn 0
344nb_say 8
345r = write 2 nb_hx 0 64
346nb_nl
347exit 3
348end
349for j ulong n bound 8388608
350put nb_src add o j get sh_dat j
351end
352put nb_sn 0 add o n
353end
354end
355note ---- child --------------------------------------------------------------------------------
356fn nb_avadd i ulong to void
357o = get nb_al mul i 2
358n = get nb_al add mul i 2 1
359p = get nb_ap 0
360put nb_av p uchar and n 255
361put nb_av add p 1 uchar shr n 8
362for j ulong n bound 4096
363put nb_av add add p 2 j get nb_a add o j
364end
365put nb_ap 0 add add p 2 n
366end
367note OUT path (argument 4) plus ".src" into nb_p when src, else OUT alone
368fn nb_out src bool to void
369nb_ap2p 4
370if src
371n = get nb_pn 0
372for j ulong 4
373put nb_p add n j get nb_txt add 11 j
374end
375put nb_pn 0 add n 4
376end
377end
378fn nb_wsrc to void
379nb_out true
380fd = fopen nb_p 0 get nb_pn 0 577 420
381if lt fd 0
382nb_say 12
383nb_nl
384exit 6
385end
386n = get nb_sn 0
387o = ulong 0
388while lt o n bound 16777216
389k = write int fd nb_src o sub n o
390if le k 0
391nb_say 12
392nb_nl
393exit 6
394end
395o = add o ulong k
396end
397r = fclose int fd
398end
399fn nb_omc to ulong
400nb_out true
401fi = fopen nb_p 0 get nb_pn 0 0 0
402nb_out false
403fo = fopen nb_p 0 get nb_pn 0 577 493
404if or lt fi 0 lt fo 0
405nb_say 12
406nb_nl
407exit 6
408end
409put nb_ap 0 2
410nb_avadd 2
411put nb_av 0 1
412put nb_av 1 0
413pid = fork
414if lt pid 0
415nb_fail
416end
417if eq pid 0
418if ne dupfd int fi 0 0 0
419exit 127
420end
421if ne dupfd int fo 1 0 1
422exit 127
423end
424r = execprog nb_av 0 get nb_ap 0
425exit 127
426end
427r = fclose int fi
428r = fclose int fo
429w = waitpid int pid nb_st 0 4 0
430if ne w pid
431nb_fail
432end
433if ne get nb_st 0 0
434nb_fail
435end
436ret ulong get nb_st 1
437end
438fn nb_fail to void
439nb_say 13
440nb_nl
441exit 7
442end
443fn nb_build to int
444nb_units 3
445nb_scan true
446nb_cat
447nb_wsrc
448x = nb_omc
449if ne x 0
450nb_say 9
451nb_num x
452nb_nl
453ret 1
454end
455nb_out false
456n = nb_load
457nb_chk n
458sh_all n
459nb_hex
460put nb_hx 64 10
461k = nb_okline get nb_uc 0
462ret 0
463end
464fn nb_okline u ulong to long
465nb_say 4
466nb_num u
467nb_say 5
468nb_num get nb_sn 0
469nb_say 6
470ret write 2 nb_hx 0 65
471end
472fn main to int
473c = argcnt
474if or lt c 3 gt c 5
475nb_say 14
476nb_nl
477ret 2
478end
479for i ulong ulong c bound 64
480if not nb_arg i
481nb_say 14
482nb_nl
483ret 2
484end
485end
486if and eq c 3 nb_aeq 1 2
487nb_units 2
488ret nb_sums
489end
490if and eq c 5 nb_aeq 1 1
491ret nb_build
492end
493nb_say 14
494nb_nl
495ret 2
496end