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 / protocol / synapse / board / tools / build.om

952 lines · 15550 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tools/build.om: build synapse-board from src/*.om with the pinned seeded omc (replaces build.sh).
3note It is an OM-2 program composed after the pinned omega-mv's om/tls/sha2.om (SHA-256 of the compiler);
4note README.md (Build) holds the one command that compiles it.
5note usage: build OMEGA_SRC OUT (run from the synapse-board repository root)
6note OMEGA_SRC checkout of omega-mv at the commit in omega.pin; its seed/out/omc must have the pinned sha256
7note OUT output binary; OUT.unit.om keeps the composed unit, OUT.err omc's diagnostics; missing
8note directories above OUT are created
9note The unit is the om2 line, then OMEGA_SRC seed/json/json.om, seed/net/http.om and seed/net/notls.om,
10note then every src/*.om in byte order of its name. OMEGA_SRC's commit is read from its .git directory
11note (or the gitdir named by a .git file): HEAD, the loose ref it names, or that ref's packed-refs line.
12note Exit: 0 built; 1 omc rejected the unit (OUT removed, never run); 64 usage or not run from the
13note repository root; 65 omega.pin malformed, OMEGA_SRC not at the pinned commit, omc missing or not the
14note pinned compiler, an input that cannot be read, or an output that cannot be written.
15
16text tuse "usage: build OMEGA_SRC OUT (run from the synapse-board repository root)\n"
17text tme "build: "
18text tnl "\n"
19text tpin "omega.pin"
20text tsrc "src"
21text tdotom ".om"
22text tgit "/.git"
23text thead "/HEAD"
24text tslash "/"
25text tpacked "/packed-refs"
26text tcommon "/commondir"
27text tomc "/seed/out/omc"
28text tjson "/seed/json/json.om"
29text thttp "/seed/net/http.om"
30text tnotls "/seed/net/notls.om"
31text tunit ".unit.om"
32text ttmp ".tmp"
33text terr ".err"
34text tom2 "om2\n"
35text tpre1 "aien-dev/omega-mv@"
36text tpre2 "omc-sha256 "
37text tref "ref: "
38text tgd "gitdir: "
39text emroot "omega.pin not found; run from the synapse-board repository root"
40text empin "omega.pin malformed"
41text emhead "cannot read the commit of OMEGA_SRC from its .git"
42text emrev "OMEGA_SRC is at "
43text emwant ", omega.pin wants "
44text emomc "omc missing or unreadable: "
45text emsha "omc sha256 "
46text emsrc "cannot list src/"
47text emfull "the host refused memory for the src/ name table (enlarge of gno, gnl or gnm)"
48text emread "cannot read "
49text emwrite "cannot write "
50text emrej "omc rejected "
51text emcdir "the commondir of OMEGA_SRC's gitdir names a path over 8191 bytes"
52text embuilt "built "
53text hexd "0123456789abcdef"
54
55global gsrc uchar 4096
56global gout uchar 4096
57global gl ulong 6
58note gl: 0 OMEGA_SRC length, 1 OUT length, 2 path length, 3 git dir length, 4 bytes used in gnm,
59note 5 path overflow (gp would pass 8191 bytes: openp then refuses with -36, ENAMETOOLONG)
60global gp uchar 8192
61global gdir uchar 8192
62global gbuf uchar 65536
63global gpin uchar 4096
64global gpinl ulong 1
65global gwant uchar 40
66global gwomc uchar 64
67global ghave uchar 40
68global gsha uchar 64
69global gline uchar 4096
70global gpk uchar 16384
71global gst uchar 4
72global gdn uchar 32768
73note the src/ name table: run-time pools grown to fit (gnm the name bytes, gno and gnl each name's offset
74note and length); a host refusal of the memory is the only way it stops
75global gnm uchar *
76global gno ulong *
77global gnl ulong *
78global gnc ulong 2
79note gnc: 0 names collected, 1 set when the host refused to grow the name table
80
81note ======== diagnostics
82fn say s view uchar * to void
83r = write 2 tme 0 len tme
84r = write 2 s 0 len s
85end
86fn sayl s view uchar * to void
87say s
88r = write 2 tnl 0 len tnl
89end
90fn saypath to void
91r = write 2 gp 0 get gl 2
92r = write 2 tnl 0 len tnl
93end
94
95note ======== the path scratch gp
96fn pclr to void
97put gl 2 0
98put gl 5 0
99end
100fn pc c uchar to void
101n = get gl 2
102if lt n 8191
103put gp n c
104put gl 2 add n 1
105else
106put gl 5 1
107end
108end
109fn pt s view uchar * to void
110for i ulong len s
111pc get s i
112end
113end
114fn psrc to void
115for i ulong get gl 0
116pc get gsrc i
117end
118end
119fn pout to void
120for i ulong get gl 1
121pc get gout i
122end
123end
124fn pdir to void
125for i ulong get gl 3
126pc get gdir i
127end
128end
129note pname: append the i-th collected src/ name
130fn pname i ulong to void
131o = get gno i
132for k ulong get gnl i
133pc get gnm add o k
134end
135end
136fn openp flags ulong mode ulong to long
137if ne get gl 5 0
138ret -36
139end
140ret fopen gp 0 get gl 2 int flags int mode
141end
142note mkparents: create each missing directory above OUT (like mkdir -p of its dirname); an existing one is kept
143fn mkparents to void
144pclr
145for i ulong get gl 1
146c = get gout i
147if and eq c 47 gt i 0
148r = makedir gp 0 get gl 2 493
149end
150pc c
151end
152end
153
154note ======== small files: rd reads gp into gline (at most 4095 bytes); byte count or negative
155fn rdline to long
156fd = openp 524288 0
157if lt fd 0
158ret fd
159end
160p = ulong 0
161for it ulong 4096
162if ge p 4095
163break
164end
165r = read int fd gline p sub 4095 p
166if lt r 0
167if ne r -4
168c = fclose int fd
169ret r
170end
171else
172if eq r 0
173break
174end
175p = add p ulong r
176end
177end
178c = fclose int fd
179if ge p 4095
180ret -27
181end
182ret long p
183end
184
185note ======== omega.pin: line 1 aien-dev/omega-mv@SHA40, line 2 omc-sha256 HEX64
186fn ishex c uchar to bool
187ret or and ge c 48 le c 57 and ge c 97 le c 102
188end
189fn pinload to bool
190pclr
191pt tpin
192fd = openp 524288 0
193if lt fd 0
194ret false
195end
196r = read int fd gpin 0 4096
197c = fclose int fd
198if or lt r 0 ge r 4096
199put gpinl 0 4096
200ret true
201end
202put gpinl 0 ulong r
203ret true
204end
205fn pinparse to bool
206n = get gpinl 0
207a = len tpre1
208b = len tpre2
209want = add add add a 41 add b 64 1
210if ne n want
211ret false
212end
213for i ulong a
214if ne get gpin i get tpre1 i
215ret false
216end
217end
218for i ulong 40
219c = get gpin add a i
220if not ishex c
221ret false
222end
223put gwant i c
224end
225if ne get gpin add a 40 10
226ret false
227end
228s = add a 41
229for i ulong b
230if ne get gpin add s i get tpre2 i
231ret false
232end
233end
234s = add s b
235for i ulong 64
236c = get gpin add s i
237if not ishex c
238ret false
239end
240put gwomc i c
241end
242ret eq get gpin add s 64 10
243end
244
245note ======== OMEGA_SRC's commit, read from its git directory
246note gitdir: OMEGA_SRC/.git when that is a directory, else the path a ".git" file names
247fn findgit to bool
248pclr
249psrc
250pt tgit
251pt thead
252fd = openp 524288 0
253if ge fd 0
254c = fclose int fd
255n = sub get gl 2 len thead
256for i ulong n
257put gdir i get gp i
258end
259put gl 3 n
260ret true
261end
262pclr
263psrc
264pt tgit
265r = rdline
266if lt r 0
267ret false
268end
269m = len tgd
270if lt ulong r add m 1
271ret false
272end
273for i ulong m
274if ne get gline i get tgd i
275ret false
276end
277end
278e = ulong r
279while and gt e m eq get gline sub e 1 10 bound 4096
280e = sub e 1
281end
282n = ulong 0
283if ne get gline m 47
284for i ulong get gl 0
285put gdir n get gsrc i
286n = add n 1
287end
288put gdir n 47
289n = add n 1
290end
291for i ulong sub e m
292put gdir n get gline add m i
293n = add n 1
294end
295put gl 3 n
296ret true
297end
298note commondir: a worktree's gitdir names the shared repository in its commondir file; refs live there
299fn common to void
300pclr
301pdir
302pt tcommon
303r = rdline
304if le r 0
305ret
306end
307e = ulong r
308for k ulong 4096
309if eq e 0
310break
311end
312if ne get gline sub e 1 10
313break
314end
315e = sub e 1
316end
317d = get gl 3
318if eq get gline 0 47
319d = 0
320else
321put gdir d 47
322d = add d 1
323end
324for i ulong e
325if lt d 8191
326put gdir d get gline i
327d = add d 1
328else
329sayl emcdir
330put gl 3 0
331ret
332end
333end
334put gl 3 d
335end
336note hex40: gline[o..o+40) is 40 lowercase hex digits; copy them into ghave
337fn hex40 o ulong to bool
338for i ulong 40
339c = get gline add o i
340if not ishex c
341ret false
342end
343put ghave i c
344end
345ret true
346end
347note headsha: 1 found, 0 not resolvable
348fn headsha to bool
349if not findgit
350ret false
351end
352pclr
353pdir
354pt thead
355r = rdline
356m = len tref
357if lt r long add m 1
358ret false
359end
360isref = true
361for i ulong m
362if ne get gline i get tref i
363isref = false
364end
365end
366if not isref
367if lt r 40
368ret false
369end
370ret hex40 0
371end
372e = ulong r
373while and gt e m eq get gline sub e 1 10 bound 4096
374e = sub e 1
375end
376rl = sub e m
377rn = new uchar 1024 0
378if gt rl 1024
379ret false
380end
381for i ulong rl
382put rn i get gline add m i
383end
384common
385pclr
386pdir
387pt tslash
388for i ulong rl
389pc get rn i
390end
391r = rdline
392if ge r 40
393ret hex40 0
394end
395pclr
396pdir
397pt tpacked
398ret packed rn rl
399end
400note packed: scan packed-refs line by line for "SHA40 NAME"; lines longer than gline cannot be NAME
401fn packed rn view uchar 1024 rl ulong to bool
402fd = openp 524288 0
403if lt fd 0
404ret false
405end
406ll = ulong 0
407skip = false
408for it ulong 999999
409r = read int fd gbuf 0 65536
410if lt r 0
411if ne r -4
412c = fclose int fd
413ret false
414end
415continue
416end
417if eq r 0
418break
419end
420for i ulong ulong r
421c = get gbuf i
422if eq c 10
423if and not skip linematch rn rl ll
424f = fclose int fd
425ret true
426end
427ll = 0
428skip = false
429else
430if lt ll 4095
431put gline ll c
432ll = add ll 1
433else
434skip = true
435end
436end
437end
438end
439note a last line without its newline is matched too
440m = and not skip linematch rn rl ll
441c = fclose int fd
442ret m
443end
444fn linematch rn view uchar 1024 rl ulong ll ulong to bool
445if ne ll add 41 rl
446ret false
447end
448if ne get gline 40 32
449ret false
450end
451for i ulong rl
452if ne get gline add 41 i get rn i
453ret false
454end
455end
456ret hex40 0
457end
458
459note ======== SHA-256 of OMEGA_SRC/seed/out/omc, streamed (sha2.om from the pinned omega-mv)
460fn hashomc to bool
461pclr
462psrc
463pt tomc
464fd = openp 524288 0
465if lt fd 0
466ret false
467end
468ctx = new ulong 40 0
469sha256_init ctx
470blk = new uchar 65536 0
471one = new uchar 1 0
472for it ulong 999999
473r = read int fd gbuf 0 65536
474if lt r 0
475if ne r -4
476c = fclose int fd
477ret false
478end
479continue
480end
481if eq r 0
482break
483end
484n = ulong r
485if eq n 65536
486for i ulong 65536
487put blk i get gbuf i
488end
489sha256_update ctx blk
490else
491for i ulong n
492put one 0 get gbuf i
493sha256_update ctx one
494end
495end
496end
497c = fclose int fd
498d = new uchar 32 0
499sha256_final ctx d
500for i ulong 32
501v = get d i
502put gsha mul i 2 get hexd ulong shr v 4
503put gsha add mul i 2 1 get hexd ulong and v 15
504end
505ret true
506end
507
508note ======== src/*.om names, sorted by bytes
509fn nameless a ulong b ulong to bool
510oa = get gno a
511ob = get gno b
512na = get gnl a
513nb = get gnl b
514m = na
515if lt nb m
516m = nb
517end
518for i ulong m
519ca = get gnm add oa i
520cb = get gnm add ob i
521if ne ca cb
522ret lt ca cb
523end
524end
525ret lt na nb
526end
527note takeent: record the getdents64 record at gdn[p] when it names a visible *.om file; false when the name table is full
528fn takeent p ulong to bool
529q = add p 19
530n = ulong 0
531while ne get gdn add q n 0 bound 256
532n = add n 1
533end
534k = len tdotom
535if or le n k eq get gdn q 46
536ret true
537end
538for i ulong k
539if ne get gdn add q add sub n k i get tdotom i
540ret true
541end
542end
543c = get gnc 0
544used = get gl 4
545if lt len gno add c 1
546r1 = enlarge gno add mul c 2 64
547r2 = enlarge gnl add mul c 2 64
548if or ne r1 0 ne r2 0
549put gnc 1 1
550ret false
551end
552end
553if lt len gnm add used n
554r3 = enlarge gnm add mul add used n 2 4096
555if ne r3 0
556put gnc 1 1
557ret false
558end
559end
560put gno c used
561put gnl c n
562for i ulong n
563put gnm add used i get gdn add q i
564end
565put gl 4 add used n
566put gnc 0 add c 1
567ret true
568end
569fn sortnames to void
570for i ulong get gnc 0
571j = i
572while gt j 0 bound 9223372036854775807
573if not nameless j sub j 1
574break
575end
576ho = get gno j
577tl = get gnl j
578put gno j get gno sub j 1
579put gnl j get gnl sub j 1
580put gno sub j 1 ho
581put gnl sub j 1 tl
582j = sub j 1
583end
584end
585end
586fn listsrc to bool
587pclr
588pt tsrc
589fd = openp 540672 0
590if lt fd 0
591ret false
592end
593put gl 4 0
594put gnc 0 0
595for it ulong 999999
596r = getdnts int fd gdn 0 32768
597if le r 0
598c = fclose int fd
599if lt r 0
600ret false
601end
602sortnames
603ret true
604end
605p = ulong 0
606while lt p ulong r bound 32768
607if not takeent p
608c = fclose int fd
609ret false
610end
611p = add p add ulong get gdn add p 16 shl ulong get gdn add p 17 8
612end
613end
614c = fclose int fd
615ret false
616end
617
618note ======== composing the unit
619fn wall fd int s view uchar * to bool
620p = ulong 0
621for it ulong 4096
622if ge p len s
623ret true
624end
625w = write fd s p sub len s p
626if le w 0
627ret false
628end
629p = add p ulong w
630end
631ret false
632end
633note copyin: append the file named by gp to fd; false (gp still names it) when it cannot be read or written
634fn copyin fd int to bool
635in = openp 524288 0
636if lt in 0
637ret false
638end
639for it ulong 999999
640r = read int in gbuf 0 65536
641if lt r 0
642if ne r -4
643c = fclose int in
644ret false
645end
646continue
647end
648if eq r 0
649c = fclose int in
650ret true
651end
652p = ulong 0
653for k ulong 65536
654if ge p ulong r
655break
656end
657w = write fd gbuf p sub ulong r p
658if le w 0
659c = fclose int in
660ret false
661end
662p = add p ulong w
663end
664end
665c = fclose int in
666ret false
667end
668note compose: write OUT.unit.om; 0 ok, 65 an input could not be read, 66 the unit could not be written
669fn compose to int
670pclr
671pout
672pt tunit
673fd = openp 524865 420
674if lt fd 0
675ret 66
676end
677if not wall int fd tom2
678c = fclose int fd
679ret 66
680end
681for s ulong 4
682pclr
683if eq s 0
684psrc
685pt tjson
686end
687if eq s 1
688psrc
689pt thttp
690end
691if eq s 2
692psrc
693pt tnotls
694end
695if eq s 3
696break
697end
698if not copyin int fd
699c = fclose int fd
700ret 65
701end
702end
703for i ulong get gnc 0
704pclr
705pt tsrc
706pt tslash
707pname i
708if not copyin int fd
709c = fclose int fd
710ret 65
711end
712end
713c = fclose int fd
714if lt c 0
715ret 66
716end
717ret 0
718end
719
720note ======== running omc: stdin OUT.unit.om, stdout OUT.tmp, stderr OUT.err
721fn pkadd to void
722n = get gl 2
723c = add ulong get gpk 0 shl ulong get gpk 1 8
724o = ulong 2
725for i ulong c
726o = add add o 2 add ulong get gpk o shl ulong get gpk add o 1 8
727end
728put gpk o uchar and n 255
729put gpk add o 1 uchar shr n 8
730for i ulong n
731put gpk add add o 2 i get gp i
732end
733c = add c 1
734put gpk 0 uchar and c 255
735put gpk 1 uchar shr c 8
736end
737fn pklen to ulong
738c = add ulong get gpk 0 shl ulong get gpk 1 8
739o = ulong 2
740for i ulong c
741o = add add o 2 add ulong get gpk o shl ulong get gpk add o 1 8
742end
743ret o
744end
745fn child to void
746pclr
747pout
748pt tunit
749fd = openp 524288 0
750if lt fd 0
751exit 126
752end
753r = dupfd int fd 0 0
754pclr
755pout
756pt ttmp
757fd = openp 524865 493
758if lt fd 0
759exit 126
760end
761r = fchmod int fd 493
762if lt r 0
763exit 126
764end
765r = dupfd int fd 1 0
766pclr
767pout
768pt terr
769fd = openp 524865 420
770if lt fd 0
771exit 126
772end
773r = dupfd int fd 2 0
774put gpk 0 0
775put gpk 1 0
776pclr
777psrc
778pt tomc
779pkadd
780r = execprog gpk 0 pklen
781exit 127
782end
783note runomc: omc's exit status, or -1 when it could not be run or did not exit normally
784fn runomc to long
785pid = fork
786if lt pid 0
787ret -1
788end
789if eq pid 0
790child
791end
792r = waitpid int pid gst 0 4 0
793if ne r pid
794ret -1
795end
796if ne get gst 0 0
797ret -1
798end
799ret long get gst 1
800end
801fn errempty to bool
802pclr
803pout
804pt terr
805fd = openp 524288 0
806if lt fd 0
807ret false
808end
809r = read int fd gbuf 0 1
810c = fclose int fd
811ret eq r 0
812end
813fn rmtmp to void
814pclr
815pout
816pt ttmp
817r = unlink gp 0 get gl 2
818end
819fn catom to void
820pclr
821pout
822pt terr
823fd = openp 524288 0
824if lt fd 0
825ret
826end
827for it ulong 999999
828r = read int fd gbuf 0 65536
829if le r 0
830break
831end
832w = write 2 gbuf 0 ulong r
833end
834c = fclose int fd
835end
836
837fn main to int
838if ne argcnt 3
839r = write 2 tuse 0 len tuse
840ret 64
841end
842n = argread 1 gsrc 0 4096
843if lt n 1
844r = write 2 tuse 0 len tuse
845ret 64
846end
847put gl 0 ulong n
848n = argread 2 gout 0 4096
849if lt n 1
850r = write 2 tuse 0 len tuse
851ret 64
852end
853put gl 1 ulong n
854if not pinload
855sayl emroot
856ret 64
857end
858if not pinparse
859sayl empin
860ret 65
861end
862if not headsha
863sayl emhead
864ret 65
865end
866same = true
867for i ulong 40
868if ne get ghave i get gwant i
869same = false
870end
871end
872if not same
873say emrev
874r = write 2 ghave 0 40
875r = write 2 emwant 0 len emwant
876r = write 2 gwant 0 40
877r = write 2 tnl 0 len tnl
878ret 65
879end
880if not hashomc
881say emomc
882saypath
883ret 65
884end
885same = true
886for i ulong 64
887if ne get gsha i get gwomc i
888same = false
889end
890end
891if not same
892say emsha
893r = write 2 gsha 0 64
894r = write 2 emwant 0 len emwant
895r = write 2 gwomc 0 64
896r = write 2 tnl 0 len tnl
897ret 65
898end
899if not listsrc
900if ne get gnc 1 0
901sayl emfull
902ret 65
903end
904sayl emsrc
905ret 65
906end
907mkparents
908pclr
909pout
910r = unlink gp 0 get gl 2
911rmtmp
912k = compose
913if eq k 65
914say emread
915saypath
916ret 65
917end
918if ne k 0
919say emwrite
920saypath
921ret 65
922end
923e = runomc
924if or ne e 0 not errempty
925rmtmp
926catom
927say emrej
928pclr
929pout
930pt tunit
931saypath
932ret 1
933end
934pclr
935pout
936pt ttmp
937put gpk 0 0
938put gpk 1 0
939pkadd
940pclr
941pout
942pkadd
943r = renames gpk 0 pklen 0
944if lt r 0
945say emwrite
946saypath
947ret 65
948end
949say embuilt
950saypath
951ret 0
952end