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 / modules / driver.om

1437 lines · 28468 bytes Raw Blame History
1note Native module driver. SHA256 compression supplied by seed/ng/sha256.om.
2global mb uchar *
3global mt ulong *
4global ms ulong 8
5global ma uchar 4096
6global mp uchar 4096
7global mx uchar 4096
8global mh uchar 64
9global mc uchar 64
10global mz uchar 128
11global mv uchar 512
12global mw uchar 4
13global mn uchar 128
14global mm ulong *
15global mg ulong 12
16text mmagic "OMMOD001"
17text omagic "OMOBJ001"
18text err "ommod: refused (code below)\n"
19text digits "0123456789abcdef"
20text tpnames "bool uchar ushort uint ulong byte short int long f32 f64 void qty"
21text objectline "object\n"
22text omline "om2\n"
23text relline "om2 release\n"
24text extline "extern fn "
25text toline " to "
26text vwline " view "
27text edline " edit "
28text spline " "
29text newln "\n"
30text starline "*"
31text paramline " p"
32text tmptext ".ommod-"
33text dotmod ".mod"
34text dotref ".ref"
35text dottext "."
36text importnm "import"
37text externnm "extern"
38text modcmd "build"
39text linkcmd "link"
40text checkcmd "check"
41text progcmd "program"
42text funccmd "function"
43text targetnm "aarch64-linux"
44text debugnm "debug"
45text releasenm "release"
46text hittext "CACHE "
47text builttext "BUILT "
48note ms: byte used, table used, module count. mg: profile, root, import counter, cwd fd.
49fn cell p ulong k ulong to ulong
50ret get mt add p k
51end
52fn die code int to void
53r = write 2 err 0 len err
54put mw 0 uchar add 48 div code 10
55put mw 1 uchar add 48 rem code 10
56put mw 2 10
57r = write 2 mw 0 3
58exit code
59end
60fn need ok bool code int to void
61if not ok
62die code
63end
64end
65note Length arithmetic is bounded by signed host address space, not an application quota.
66fn sizeadd a ulong b ulong to ulong
67need le a 9223372036854775807 20
68need le b sub 9223372036854775807 a 20
69ret add a b
70end
71fn sizemul a ulong b ulong to ulong
72if ne a 0
73need le b div 9223372036854775807 a 20
74end
75ret mul a b
76end
77fn growto n ulong extra ulong limit ulong to ulong
78need le n limit 20
79if le extra sub limit n
80ret add n extra
81end
82ret n
83end
84fn ba n ulong to ulong
85p = get ms 0
86need le n sub 9223372036854775807 p 20
87q = add p n
88if gt q len mb
89r = enlarge mb growto q 4096 9223372036854775807
90need ge r 0 21
91end
92put ms 0 q
93ret p
94end
95fn ta n ulong to ulong
96p = get ms 1
97need le n sub 1152921504606846975 p 20
98if gt add p n len mt
99r = enlarge mt growto add p n 512 1152921504606846975
100need ge r 0 21
101end
102put ms 1 add p n
103ret p
104end
105fn rd p ulong lim ulong to ulong
106need le p lim 22
107need ge sub lim p 8 22
108v = ulong 0
109for j ulong 8
110v = or v shl ulong get mb add p j mul j 8
111end
112ret v
113end
114fn wr p ulong v ulong to void
115for j ulong 8
116put mb add p j uchar and shr v mul j 8 255
117end
118end
119fn word p ulong v ulong to void
120for j ulong 4
121put mb add p j uchar and shr v mul j 8 255
122end
123end
124fn rw p ulong to ulong
125v = ulong 0
126for j ulong 4
127v = or v shl ulong get mb add p j mul j 8
128end
129ret v
130end
131fn copy p ulong q ulong n ulong to void
132for j ulong n bound 9223372036854775807
133put mb add p j get mb add q j
134end
135end
136fn same p ulong q ulong n ulong to bool
137for j ulong n bound 9223372036854775807
138if ne get mb add p j get mb add q j
139ret false
140end
141end
142ret true
143end
144fn lit p ulong n ulong txt view uchar * to bool
145if ne n len txt
146ret false
147end
148for j ulong n bound 9223372036854775807
149if ne get mb add p j get txt j
150ret false
151end
152end
153ret true
154end
155fn append txt view uchar * to ulong
156p = ba len txt
157for j ulong len txt bound 9223372036854775807
158put mb add p j get txt j
159end
160ret p
161end
162fn nmvalid p ulong n ulong to bool
163if or eq n 0 gt n 63
164ret false
165end
166for j ulong n bound 64
167c = get mb add p j
168if not or or and ge c 65 le c 90 and ge c 97 le c 122 eq c 95
169if or eq j 0 not and ge c 48 le c 57
170ret false
171end
172end
173end
174ret true
175end
176fn hxvalid p ulong to void
177for j ulong 64
178c = get mb add p j
179need or and ge c 48 le c 57 and ge c 97 le c 102 23
180end
181end
182fn hash p ulong n ulong to void
183note SHA256 encodes message length in 64 bits, measured in bits.
184need le n 2305843009213693951 20
185sh_init
186for j ulong 8
187put sh_h j get sh_k add j 64
188end
189nb = div n 64
190for k ulong nb bound 9223372036854775807
191for j ulong 64
192put sh_dat j get mb add add p mul k 64 j
193end
194sh_blk 0 0
195end
196for j ulong 128
197put sh_pad j 0
198end
199r = rem n 64
200for j ulong r bound 64
201put sh_pad j get mb add add p mul nb 64 j
202end
203put sh_pad r 128
204e = ulong 64
205if ge r 56
206e = 128
207end
208for j ulong 8
209put sh_pad sub sub e 1 j uchar and shr mul n 8 mul j 8 255
210end
211sh_blk 1 0
212if eq e 128
213sh_blk 1 64
214end
215sh_out
216for j ulong 32
217v = uint get sh_dig j
218put mh mul j 2 sh_hexd shr v 4
219put mh add mul j 2 1 sh_hexd and v 15
220end
221end
222fn hashis p ulong to void
223for j ulong 64
224need eq get mb add p j get mh j 24
225end
226end
227fn hashput p ulong to void
228for j ulong 64
229put mb add p j get mh j
230end
231end
232fn arg i ulong to ulong
233n = argread i ma 0 4096
234need and ge n 0 lt n 4096 10
235p = ba ulong n
236for j ulong ulong n bound 4096
237put mb add p j get ma j
238end
239put mg 10 ulong n
240ret p
241end
242fn path p ulong n ulong to void
243need and gt n 0 lt n 4096 10
244for j ulong n bound 4096
245need ne get mb add p j 0 10
246put mp j get mb add p j
247end
248end
249note Walk every directory component with O_NOFOLLOW; cwd always restored by callers.
250fn parent p ulong n ulong to ulong
251path p n
252s = ulong 0
253if eq get mp 0 47
254fd = fopen mp 0 1 49152 0
255need ge fd 0 25
256r = fchdir int fd
257c = fclose int fd
258need and eq r 0 eq c 0 25
259s = 1
260end
261for i ulong n bound 4096
262if and ge i s eq get mp i 47
263if gt i s
264fd = fopen mp s sub i s 49152 0
265need ge fd 0 25
266r = fchdir int fd
267c = fclose int fd
268need and eq r 0 eq c 0 25
269end
270s = add i 1
271end
272end
273need lt s n 25
274ret s
275end
276fn restore to void
277r = fchdir int get mg 3
278need eq r 0 25
279end
280note Open read-only regular file, no symbolic links in any component. Returns -2 for absent.
281fn readpath p ulong n ulong to long
282restore
283s = parent p n
284fd = fopen mp s sub n s 34816 0
285restore
286ret fd
287end
288fn load fd int to ulong
289r = fstat fd mz 0 128
290need eq r 0 26
291mode = or ulong get mz 16 shl ulong get mz 17 8
292need eq and mode 61440 32768 26
293sz = ulong 0
294for j ulong 8
295sz = or sz shl ulong get mz add 48 j mul j 8
296end
297p = ba sz
298k = ulong 0
299while lt k sz bound 9223372036854775807
300r = read fd mb add p k sub sz k
301need gt r 0 26
302k = add k ulong r
303end
304r = read fd mw 0 1
305need eq r 0 26
306r = fclose fd
307need eq r 0 26
308put mg 10 sz
309ret p
310end
311fn writeall fd int p ulong n ulong to void
312k = ulong 0
313while lt k n bound 9223372036854775807
314r = write fd mb add p k sub n k
315need gt r 0 27
316k = add k ulong r
317end
318end
319note Module record: blob,size,source,size,object,size,imports,count,functions,count,calls,count,
320note code end, output code base, digest, dependency traversal flag. 16 cells.
321note Import record: alias pos,len,source hash,export pos,len,target function,module,expected digest.
322fn uniqueimports m ulong to void
323for i ulong cell m 7 bound 9223372036854775807
324a = add cell m 6 mul i 8
325for j ulong i bound 9223372036854775807
326b = add cell m 6 mul j 8
327if eq cell a 1 cell b 1
328need not same get mt a get mt b cell a 1 28
329end
330end
331end
332end
333fn parseline m ulong ls ulong lineend ulong to void
334q = ls
335while lt q lineend bound 9223372036854775807
336if ne get mb q 32
337break
338end
339q = add q 1
340end
341w = q
342while lt q lineend bound 9223372036854775807
343if eq get mb q 32
344break
345end
346q = add q 1
347end
348need not lit w sub q w externnm 28
349if lit w sub q w importnm
350need lt q lineend 28
351q = add q 1
352a = q
353while lt q lineend bound 9223372036854775807
354if eq get mb q 32
355break
356end
357q = add q 1
358end
359need nmvalid a sub q a 28
360need lt q lineend 28
361h = add q 1
362need ge sub lineend h 66 28
363hxvalid h
364need eq get mb add h 64 32 28
365x = add h 65
366need nmvalid x sub lineend x 28
367ls = ta 8
368put mt ls a
369put mt add ls 1 sub q a
370put mt add ls 2 h
371put mt add ls 3 x
372put mt add ls 4 sub lineend x
373put mt add ls 5 0
374put mt add ls 6 0
375put mt add ls 7 0
376put mt add m 7 add cell m 7 1
377end
378end
379fn parse m ulong to void
380s = cell m 2
381e = add s cell m 3
382p = s
383while lt p e bound 9223372036854775807
384if eq get mb p 10
385break
386end
387p = add p 1
388end
389need lt p e 28
390if eq get mg 0 0
391need lit s sub add p 1 s omline 28
392else
393need lit s sub add p 1 s relline 28
394end
395put mt add m 6 get ms 1
396p = add p 1
397while lt p e bound 9223372036854775807
398ls = p
399while lt p e bound 9223372036854775807
400if eq get mb p 10
401break
402end
403p = add p 1
404end
405lineend = p
406if lt p e
407p = add p 1
408end
409parseline m ls lineend
410end
411uniqueimports m
412end
413note Function record: flag,address,result,nparams,stack,name,len,params,module,resolved,state,need.
414fn br s ulong d ulong op ulong to ulong
415v = sub long d long s
416need eq rem v 4 0 30
417need and ge v -134217728 lt v 134217728 30
418if lt v 0
419v = add v 268435456
420end
421ret or op div ulong v 4
422end
423fn func m ulong i ulong to ulong
424need lt i cell m 9 31
425ret add cell m 8 mul i 12
426end
427fn fend m ulong f ulong to ulong
428last = cell m 12
429for i ulong cell m 9 bound 9223372036854775807
430g = func m i
431if ne get mt g 2
432a = cell g 1
433if and gt a cell f 1 lt a last
434last = a
435end
436end
437end
438ret last
439end
440fn validateparam pp ulong e ulong flag ulong to ulong
441ty = rd pp e
442ki = rd add pp 8 e
443sz = rd add pp 16 e
444fr = rd add pp 32 e
445need and ge ty 1 le ty 13 31
446need ne ty 12 31
447need or eq ki 0 or eq ki 2 eq ki 3 31
448need and eq rd add pp 24 e 0 le fr 1 31
449if eq ki 0
450need and eq sz 0 eq fr 0 31
451else
452need le sz 16777216 31
453if ne fr 0
454need and and eq ki 3 gt sz 0 ne flag 2 31
455end
456end
457if and ne ki 0 eq sz 0
458ret 2
459end
460ret 1
461end
462fn validatename m ulong f ulong i ulong to void
463for j ulong i bound 9223372036854775807
464g = func m j
465if eq cell f 6 cell g 6
466need not same cell f 5 cell g 5 cell f 6 31
467end
468end
469end
470fn validateaddr m ulong f ulong to void
471b = cell m 4
472addr = cell f 1
473if eq get mt f 2
474need and eq addr 0 eq cell f 4 0 31
475put mt add m 19 add cell m 19 1
476else
477need and ge addr 176 lt addr sub cell m 12 b 31
478need eq rem addr 4 0 31
479addr = add b addr
480need ge addr cell m 17 31
481if eq cell m 17 add add b 176 mul cell m 20 4
482need eq addr cell m 17 31
483end
484put mt add f 1 addr
485put mt add m 17 add addr 4
486if eq get mt f 1
487site = add add b 176 mul cell m 18 4
488need eq rw site br site addr 335544320 31
489put mt add m 18 add cell m 18 1
490end
491end
492end
493fn validatefn m ulong i ulong to void
494f = func m i
495pos = cell m 16
496e = add cell m 4 cell m 5
497need le pos e 31
498need ge sub e pos 48 31
499for j ulong 6
500put mt add f j rd add pos mul j 8 e
501end
502need le get mt f 2 31
503need and ge cell f 2 1 le cell f 2 13 31
504need le cell f 3 6 31
505need le cell f 4 8388608 31
506pos = add pos 48
507nl = cell f 5
508need le nl sub e pos 31
509need nmvalid pos nl 31
510put mt add f 5 pos
511put mt add f 6 nl
512put mt add f 8 m
513put mt add f 9 f
514validatename m f i
515pos = add pos nl
516while ne rem sub pos cell m 4 8 0 bound 8
517need lt pos e 31
518need eq get mb pos 0 31
519pos = add pos 1
520end
521put mt add f 7 pos
522need le cell f 3 div sub e pos 40 31
523pc = ulong 0
524for j ulong cell f 3 bound 6
525pc = add pc validateparam add pos mul j 40 e get mt f
526end
527need le pc 6 31
528put mt add m 16 add pos mul cell f 3 40
529validateaddr m f
530end
531fn uniquecalls m ulong i ulong site ulong to void
532for j ulong i bound 9223372036854775807
533need ne site get mt add cell m 10 mul j 4 32
534end
535end
536fn validatecall m ulong i ulong to void
537e = add cell m 4 cell m 5
538p = add cell m 16 mul i 32
539c = add cell m 10 mul i 4
540site = rd p e
541src = func m rd add p 8 e
542dst = func m rd add p 16 e
543need and ge site 176 lt site sub cell m 12 cell m 4 32
544need eq rem site 4 0 32
545site = add site cell m 4
546need ne get mt src 2 32
547need and ge site cell src 1 lt site fend m src 32
548need le rd add p 24 e 8388608 32
549need eq rem rd add p 24 e 16 0 32
550uniquecalls m i site
551want = ulong 2483027968
552if ne get mt dst 2
553want = br site cell dst 1 want
554end
555need eq rw site want 32
556put mt c site
557put mt add c 1 src
558put mt add c 2 dst
559put mt add c 3 rd add p 24 e
560end
561fn validatescan m ulong to void
562p = add add cell m 4 176 mul cell m 20 4
563while lt p cell m 12 bound 9223372036854775807
564if eq shr rw p 26 37
565seen = ulong 0
566for i ulong cell m 11 bound 9223372036854775807
567if eq p get mt add cell m 10 mul i 4
568seen = add seen 1
569end
570end
571need eq seen 1 32
572end
573p = add p 4
574end
575end
576fn validate m ulong to void
577b = cell m 4
578n = cell m 5
579e = add b n
580need ge n 180 31
581need lit b 8 omagic 31
582need eq rd add b 8 e 1 31
583need eq rd add b 16 e 1 31
584need eq rd add b 24 e get mg 0 31
585need eq rd add b 32 e 176 31
586ce = rd add b 40 e
587need and ge ce 180 le ce n 31
588need eq rem ce 4 0 31
589put mt add m 12 add b ce
590nf = rd add b 48 e
591nc = rd add b 56 e
592nx = rd add b 64 e
593need and gt nf 0 le nf div sub n ce 48 31
594need le nc div sub n ce 32 31
595need le nx nf 31
596for j ulong 104
597need eq get mb add add b 72 j 0 31
598end
599put mt add m 8 ta mul nf 12
600put mt add m 9 nf
601put mt add m 16 add b ce
602put mt add m 20 nx
603if eq nx 0
604put mt add m 20 1
605end
606need le cell m 20 div sub ce 176 4 31
607put mt add m 17 add add b 176 mul cell m 20 4
608for i ulong nf bound 9223372036854775807
609validatefn m i
610end
611need eq nx cell m 18 31
612need eq cell m 19 cell m 7 31
613need eq mul nc 32 sub e cell m 16 31
614put mt add m 10 ta mul nc 4
615put mt add m 11 nc
616for i ulong nc bound 9223372036854775807
617validatecall m i
618end
619validatescan m
620end
621fn signature a ulong b ulong to bool
622if or ne cell a 2 cell b 2 ne cell a 3 cell b 3
623ret false
624end
625ret same cell a 7 cell b 7 mul cell a 3 40
626end
627fn exported m ulong p ulong n ulong to ulong
628for i ulong cell m 9 bound 9223372036854775807
629f = func m i
630if and eq get mt f 1 eq cell f 6 n
631if same p cell f 5 n
632ret f
633end
634end
635end
636die 33
637ret 0
638end
639fn newmod to ulong
640m = ta 24
641k = get ms 2
642if ge k len mm
643r = enlarge mm growto sizeadd k 1 127 1152921504606846975
644need ge r 0 21
645end
646put mm k m
647put ms 2 add k 1
648ret m
649end
650note Store key hashes domain tag + compiler digest + source digest + profile + target.
651fn key srchash ulong to void
652p = ba 152
653for j ulong 8
654put mb add p j get mmagic j
655end
656for j ulong 64
657put mb add add p 8 j get mc j
658put mb add add p 72 j get mb add srchash j
659end
660wr add p 136 get mg 0
661wr add p 144 1
662hash p 152
663for j ulong 64
664put mp j get mh j
665end
666for j ulong 4
667put mp add j 64 get dotref j
668end
669end
670note Read and verify a complete cached artifact. Its source digest, compiler, profile and target
671note are checked before metadata, and dependencies are verified before any executable is written.
672fn decodeimports m ulong to void
673dp = add cell m 4 cell m 5
674for i ulong cell m 7 bound 9223372036854775807
675imp = add cell m 6 mul i 8
676put mt add imp 7 add dp mul i 64
677end
678end
679fn decode b ulong n ulong srchash ulong expected ulong to ulong
680need ge n 176 34
681hash b n
682if ne expected 0
683hashis expected
684end
685h = ba 64
686hashput h
687need lit b 8 mmagic 34
688for j ulong 64
689need eq get mb add add b 8 j get mc j 34
690end
691need same add b 72 srchash 64 34
692need eq rd add b 136 add b n get mg 0 34
693need eq rd add b 144 add b n 1 34
694sn = rd add b 152 add b n
695on = rd add b 160 add b n
696dn = rd add b 168 add b n
697need le sn sub n 176 34
698need le on sub sub n 176 sn 34
699need eq dn div sub sub sub n 176 sn on 64 34
700need eq rem sub sub sub n 176 sn on 64 0 34
701hash add b 176 sn
702hashis add b 72
703m = newmod
704put mt m b
705put mt add m 1 n
706put mt add m 2 add b 176
707put mt add m 3 sn
708put mt add m 4 add add b 176 sn
709put mt add m 5 on
710put mt add m 14 h
711parse m
712need eq dn cell m 7 34
713decodeimports m
714validate m
715ret m
716end
717fn reference srchash ulong to ulong
718key srchash
719r = fchdir int get mg 4
720need eq r 0 25
721fd = fopen mp 0 68 34816 0
722restore
723need ge fd 0 34
724p = load int fd
725need eq get mg 10 64 34
726hxvalid p
727ret p
728end
729fn cached srchash ulong expected ulong to ulong
730for k ulong get ms 2 bound 9223372036854775807
731m = get mm k
732if ne get mt m 0
733if same add get mt m 72 srchash 64
734if ne expected 0
735need same cell m 14 expected 64 24
736end
737ret m
738end
739end
740end
741h = reference srchash
742if ne expected 0
743need same h expected 64 24
744end
745for j ulong 64
746put mp j get mb add h j
747end
748for j ulong 4
749put mp add j 64 get dotmod j
750end
751r = fchdir int get mg 4
752need eq r 0 25
753fd = fopen mp 0 68 34816 0
754restore
755need ge fd 0 34
756b = load int fd
757ret decode b get mg 10 srchash h
758end
759
760fn resolveone m ulong i ulong to void
761imp = add cell m 6 mul i 8
762i = cached cell imp 2 cell imp 7
763put mt add imp 5 exported i cell imp 3 cell imp 4
764put mt add imp 6 i
765found = ulong 0
766for j ulong cell m 9 bound 9223372036854775807
767g = func m j
768if and eq get mt g 2 eq cell g 6 cell imp 1
769if same cell g 5 get mt imp cell imp 1
770need signature cell imp 5 g 35
771put mt add g 9 cell imp 5
772found = add found 1
773end
774end
775end
776need eq found 1 35
777end
778fn resolveall to void
779k = ulong 0
780while lt k get ms 2 bound 9223372036854775807
781m = get mm k
782for i ulong cell m 7 bound 9223372036854775807
783resolveone m i
784end
785k = add k 1
786end
787end
788note Iterative DAG check over module dependencies, including unused imports.
789fn modready m ulong to bool
790for i ulong cell m 7 bound 9223372036854775807
791imp = add cell m 6 mul i 8
792d = cell imp 6
793if eq cell d 15 0
794ret false
795end
796end
797ret true
798end
799fn stepmods to ulong
800count = ulong 0
801for k ulong get ms 2 bound 9223372036854775807
802m = get mm k
803if eq cell m 15 0
804if modready m
805put mt add m 15 1
806count = add count 1
807end
808end
809end
810ret count
811end
812fn fnready m ulong f ulong to bool
813cost = cell f 4
814for i ulong cell m 11 bound 9223372036854775807
815c = add cell m 10 mul i 4
816if eq cell c 1 f
817g = cell cell c 2 9
818if eq cell g 10 0
819ret false
820end
821v = add cell c 3 cell g 11
822if gt v cost
823cost = v
824end
825end
826end
827need le cost 8388608 37
828put mt add f 11 cost
829put mt add f 10 1
830ret true
831end
832fn stepfuncs countonly bool to ulong
833count = ulong 0
834for k ulong get ms 2 bound 9223372036854775807
835m = get mm k
836for j ulong cell m 9 bound 9223372036854775807
837f = func m j
838if ne get mt f 2
839if eq cell f 10 0
840if countonly
841count = add count 1
842else
843if fnready m f
844count = add count 1
845end
846end
847end
848end
849end
850end
851ret count
852end
853fn graph to void
854left = get ms 2
855while gt left 0 bound 9223372036854775807
856progress = stepmods
857need gt progress 0 36
858left = sub left progress
859end
860left = stepfuncs true
861while gt left 0 bound 9223372036854775807
862progress = stepfuncs false
863need gt progress 0 36
864left = sub left progress
865end
866end
867fn decimal x ulong to void
868q = x
869start = ulong 24
870while true bound 24
871start = sub start 1
872put mn start uchar add rem q 10 48
873q = div q 10
874if eq q 0
875break
876end
877end
878p = ba sub 24 start
879for j ulong sub 24 start bound 24
880put mb add p j get mn add start j
881end
882end
883fn typename ty ulong to void
884p = ulong 0
885idx = ulong 1
886for j ulong len tpnames
887if eq get tpnames j 32
888if eq idx ty
889r = append part tpnames p sub j p
890ret
891end
892p = add j 1
893idx = add idx 1
894end
895end
896need eq ty 13 31
897r = append part tpnames p sub len tpnames p
898end
899fn externs m ulong to void
900for i ulong cell m 7 bound 9223372036854775807
901imp = add cell m 6 mul i 8
902f = cell imp 5
903r = append extline
904p = ba cell imp 1
905copy p get mt imp cell imp 1
906for j ulong cell f 3 bound 6
907pp = add cell f 7 mul j 40
908e = get ms 0
909r = append paramline
910decimal j
911ki = rd add pp 8 e
912if eq ki 0
913r = append spline
914else
915if eq ki 2
916r = append vwline
917else
918r = append edline
919end
920end
921typename rd pp e
922if ne ki 0
923r = append spline
924sz = rd add pp 16 e
925if eq sz 0
926r = append starline
927else
928decimal sz
929end
930end
931need eq rd add pp 32 e 0 35
932end
933r = append toline
934typename cell f 2
935r = append newln
936end
937end
938fn copyline ls ulong p ulong to void
939q = ls
940while lt q p bound 9223372036854775807
941if ne get mb q 32
942break
943end
944q = add q 1
945end
946w = q
947while lt q p bound 9223372036854775807
948if or eq get mb q 32 eq get mb q 10
949break
950end
951q = add q 1
952end
953if not lit w sub q w importnm
954r = ba sub p ls
955copy r ls sub p ls
956end
957end
958fn compiletext m ulong to ulong
959start = get ms 0
960r = append objectline
961if eq get mg 0 0
962r = append omline
963else
964r = append relline
965end
966externs m
967p = cell m 2
968e = add p cell m 3
969while lt p e bound 9223372036854775807
970if eq get mb p 10
971p = add p 1
972break
973end
974p = add p 1
975end
976while lt p e bound 9223372036854775807
977ls = p
978while lt p e bound 9223372036854775807
979if eq get mb p 10
980p = add p 1
981break
982end
983p = add p 1
984end
985copyline ls p
986end
987ret start
988end
989note Unique temporary name in mx; O_EXCL is still mandatory, so collisions refuse safely.
990fn tempname to void
991r = random mn 0 16 0
992need eq r 16 27
993for j ulong 7
994put mx j get tmptext j
995end
996for j ulong 16
997b = uint get mn j
998put mx add 7 mul j 2 sh_hexd shr b 4
999put mx add 8 mul j 2 sh_hexd and b 15
1000end
1001end
1002fn tempfile p ulong n ulong mode int to long
1003tempname
1004fd = fopen mx 0 39 32961 mode
1005need ge fd 0 27
1006writeall int fd p n
1007r = fsync int fd
1008need eq r 0 27
1009r = fclose int fd
1010need eq r 0 27
1011ret 0
1012end
1013fn packet to ulong
1014put mv 0 1
1015put mv 1 0
1016put mv 2 39
1017put mv 3 0
1018for j ulong 39
1019put mv add 4 j get mx j
1020end
1021ret 43
1022end
1023fn compiler cp ulong cn ulong textpos ulong textlen ulong to ulong
1024r = fchdir int get mg 4
1025need eq r 0 25
1026r = tempfile cp cn 448
1027pn = packet
1028fi = memfd tmptext 0 7 0
1029fo = memfd tmptext 0 7 0
1030need and ge fi 0 ge fo 0 27
1031writeall int fi textpos textlen
1032r = fseek int fi 0 0
1033need eq r 0 27
1034pid = fork
1035need ge pid 0 27
1036if eq pid 0
1037r = dupfd int fi 0 0
1038if ne r 0
1039exit 127
1040end
1041r = dupfd int fo 1 0
1042if ne r 1
1043exit 127
1044end
1045r = execprog mv 0 pn
1046exit 127
1047end
1048r = waitpid int pid mw 0 4 0
1049need eq r pid 27
1050r = unlink mx 0 39
1051need eq r 0 27
1052need and eq get mw 0 0 eq get mw 1 0 39
1053r = fclose int fi
1054need eq r 0 27
1055r = fseek int fo 0 0
1056need eq r 0 27
1057cp = load int fo
1058restore
1059ret cp
1060end
1061note Atomic no-replace publication. Caller has selected a safe parent directory.
1062fn publish p ulong n ulong dest ulong dn ulong mode int to void
1063path dest dn
1064for j ulong dn bound 4096
1065put ma j get mp j
1066end
1067r = tempfile p n mode
1068put mv 0 2
1069put mv 1 0
1070put mv 2 39
1071put mv 3 0
1072for j ulong 39
1073put mv add 4 j get mx j
1074end
1075need le dn 255 25
1076put mv 43 uchar dn
1077put mv 44 0
1078for j ulong dn bound 255
1079put mv add 45 j get ma j
1080end
1081r = renames mv 0 add 45 dn 1
1082if ne r 0
1083x = unlink mx 0 39
1084die 38
1085end
1086r = fopen dottext 0 1 16384 0
1087need ge r 0 27
1088x = fsync int r
1089y = fclose int r
1090need and eq x 0 eq y 0 27
1091end
1092fn builddeps m ulong to void
1093for i ulong cell m 7 bound 9223372036854775807
1094imp = add cell m 6 mul i 8
1095d = cached cell imp 2 0
1096put mt add imp 6 d
1097put mt add imp 5 exported d cell imp 3 cell imp 4
1098end
1099end
1100fn compilemod cp ulong cn ulong m ulong to void
1101src = compiletext m
1102sz = sub get ms 0 src
1103op = compiler cp cn src sz
1104on = get mg 10
1105put mt add m 4 op
1106put mt add m 5 on
1107end
1108fn ensureblob m ulong name ulong to void
1109path name 68
1110fd = fopen mp 0 68 34816 0
1111if ge fd 0
1112p = load int fd
1113need eq get mg 10 cell m 1 24
1114hash p get mg 10
1115hashis cell m 14
1116ret
1117end
1118need eq fd -2 34
1119publish get mt m cell m 1 name 68 292
1120end
1121fn publishstore m ulong sh ulong to void
1122h = cell m 14
1123name = ba 68
1124copy name h 64
1125for j ulong 4
1126put mb add add name 64 j get dotmod j
1127end
1128r = fchdir int get mg 4
1129need eq r 0 25
1130ensureblob m name
1131key sh
1132for j ulong 68
1133put mb add name j get mp j
1134end
1135publish h 64 name 68 292
1136end
1137fn package m ulong sh ulong to ulong
1138b = ba sizeadd sizeadd sizeadd 176 cell m 3 cell m 5 sizemul cell m 7 64
1139for j ulong 8
1140put mb add b j get mmagic j
1141end
1142for j ulong 64
1143put mb add add b 8 j get mc j
1144end
1145copy add b 72 sh 64
1146wr add b 136 get mg 0
1147wr add b 144 1
1148wr add b 152 cell m 3
1149wr add b 160 cell m 5
1150wr add b 168 cell m 7
1151copy add b 176 cell m 2 cell m 3
1152copy add add b 176 cell m 3 cell m 4 cell m 5
1153dp = add add add b 176 cell m 3 cell m 5
1154for i ulong cell m 7 bound 9223372036854775807
1155imp = add cell m 6 mul i 8
1156d = cell imp 6
1157copy add dp mul i 64 cell d 14 64
1158end
1159n = sub get ms 0 b
1160hash b n
1161h = ba 64
1162hashput h
1163put mt m b
1164put mt add m 1 n
1165put mt add m 14 h
1166publishstore m sh
1167restore
1168r = write 1 builttext 0 len builttext
1169ret m
1170end
1171fn cachehit sh ulong to ulong
1172key sh
1173r = fchdir int get mg 4
1174need eq r 0 25
1175fd = fopen mp 0 68 34816 0
1176restore
1177if ge fd 0
1178r = fclose int fd
1179need eq r 0 26
1180m = cached sh 0
1181resolveall
1182graph
1183r = write 1 hittext 0 len hittext
1184ret m
1185end
1186need eq fd -2 34
1187ret 0
1188end
1189fn build cp ulong cn ulong sp ulong sn ulong sh ulong to ulong
1190m = cachehit sh
1191if ne m 0
1192ret m
1193end
1194m = newmod
1195put mt add m 2 sp
1196put mt add m 3 sn
1197parse m
1198builddeps m
1199compilemod cp cn m
1200validate m
1201resolveall
1202graph
1203ret package m sh
1204end
1205text abimagic "OMLINK01"
1206fn placed f ulong to ulong
1207m = cell f 8
1208ret add cell m 13 sub cell f 1 add cell m 4 176
1209end
1210fn descriptor root ulong start ulong to void
1211p = ba 8
1212count = ulong 0
1213for j ulong cell root 9 bound 9223372036854775807
1214f = func root j
1215if eq get mt f 1
1216count = add count 1
1217end
1218end
1219wr p count
1220for j ulong cell root 9 bound 9223372036854775807
1221f = func root j
1222if eq get mt f 1
1223q = ba 40
1224wr q sub placed f start
1225wr add q 8 cell f 11
1226wr add q 16 cell f 2
1227wr add q 24 cell f 3
1228wr add q 32 cell f 6
1229q = ba cell f 6
1230copy q cell f 5 cell f 6
1231q = ba mul cell f 3 40
1232copy q cell f 7 mul cell f 3 40
1233end
1234end
1235q = ba 8
1236wr q sub q p
1237q = append abimagic
1238end
1239fn relocate m ulong i ulong to void
1240c = add cell m 10 mul i 4
1241site = add cell m 13 sub get mt c add cell m 4 176
1242f = get mt add cell c 2 9
1243word site br site placed f 2483027968
1244end
1245fn placemodules to void
1246for k ulong get ms 2 bound 9223372036854775807
1247m = get mm k
1248n = sub cell m 12 add cell m 4 176
1249q = ba n
1250put mt add m 13 q
1251copy q add cell m 4 176 n
1252end
1253for k ulong get ms 2 bound 9223372036854775807
1254m = get mm k
1255for i ulong cell m 11 bound 9223372036854775807
1256relocate m i
1257end
1258end
1259end
1260fn programwrap start ulong entry ulong to void
1261word start 1179403647
1262word add start 4 65794
1263word add start 16 11993090
1264word add start 20 1
1265wr add start 24 4194480
1266wr add start 32 64
1267word add start 52 3670080
1268word add start 56 2
1269word add start 64 1
1270word add start 68 5
1271wr add start 80 4194304
1272wr add start 88 4194304
1273wr add start 96 sub get ms 0 start
1274wr add start 104 sub get ms 0 start
1275wr add start 112 4096
1276word add start 120 1
1277word add start 124 6
1278wr add start 136 268435456
1279wr add start 144 268435456
1280wr add start 160 8392704
1281wr add start 168 4096
1282word add start 176 3533832199
1283word add start 180 3533836304
1284word add start 184 2436892176
1285word add start 188 2432696863
1286word add start 192 268435664
1287word add start 196 4177529072
1288word add start 200 br add start 200 placed entry 2483027968
1289word add start 204 3573751839
1290if eq cell entry 2 12
1291word add start 204 3531603968
1292end
1293word add start 208 3531606952
1294word add start 212 3556769793
1295word add start 216 2432798752
1296word add start 220 3531606952
1297word add start 224 3556769793
1298end
1299fn functionwrap start ulong root ulong to void
1300idx = ulong 0
1301for j ulong cell root 9 bound 9223372036854775807
1302f = func root j
1303if eq get mt f 1
1304site = add start mul idx 4
1305word site br site placed f 335544320
1306idx = add idx 1
1307end
1308end
1309descriptor root start
1310end
1311fn linkimage root ulong mode bool ep ulong en ulong to ulong
1312entry = ulong 0
1313count = ulong 0
1314for j ulong cell root 9 bound 9223372036854775807
1315f = func root j
1316if eq get mt f 1
1317count = add count 1
1318end
1319end
1320need gt count 0 40
1321if mode
1322entry = exported root ep en
1323need eq cell entry 3 0 40
1324need or eq cell entry 2 8 eq cell entry 2 12 40
1325end
1326start = get ms 0
1327if mode
1328p = ba 228
1329else
1330p = ba mul count 4
1331end
1332placemodules
1333if mode
1334programwrap start entry
1335else
1336functionwrap start root
1337end
1338put mg 10 sub get ms 0 start
1339ret start
1340end
1341fn argpos args ulong i ulong to ulong
1342ret get mt add args mul i 2
1343end
1344fn arglen args ulong i ulong to ulong
1345ret get mt add add args mul i 2 1
1346end
1347fn runbuild args ulong to void
1348need eq arglen args 8 64 10
1349sh = argpos args 8
1350hxvalid sh
1351fd = readpath argpos args 7 arglen args 7
1352need ge fd 0 26
1353sp = load int fd
1354sn = get mg 10
1355hash sp sn
1356hashis sh
1357fd = readpath argpos args 2 arglen args 2
1358need ge fd 0 26
1359cp = load int fd
1360cn = get mg 10
1361hash cp cn
1362hashis argpos args 3
1363root = build cp cn sp sn sh
1364r = write 1 mb cell root 14 64
1365r = write 1 newln 0 1
1366end
1367fn runlink args ulong to void
1368need and eq arglen args 6 64 eq arglen args 7 64 10
1369hxvalid argpos args 6
1370hxvalid argpos args 7
1371root = cached argpos args 6 argpos args 7
1372resolveall
1373graph
1374mode = lit argpos args 8 arglen args 8 progcmd
1375need or mode lit argpos args 8 arglen args 8 funccmd 10
1376p = linkimage root mode argpos args 9 arglen args 9
1377n = get mg 10
1378restore
1379dest = argpos args 10
1380dn = arglen args 10
1381s = parent dest dn
1382perm = int 292
1383if mode
1384perm = 365
1385end
1386publish p n add dest s sub dn s perm
1387restore
1388end
1389fn main to int
1390r = ta 1
1391r = ba 1
1392cwdfile = fopen dottext 0 1 16384 0
1393need ge cwdfile 0 25
1394put mg 3 ulong cwdfile
1395ac = argcnt
1396need or eq ac 9 eq ac 11 10
1397args = ta mul ulong ac 2
1398for i ulong ulong ac bound 11
1399p = arg i
1400put mt add args mul i 2 p
1401put mt add add args mul i 2 1 get mg 10
1402end
1403isbuild = lit argpos args 1 arglen args 1 modcmd
1404islink = lit argpos args 1 arglen args 1 linkcmd
1405need or and isbuild eq ac 9 and islink eq ac 11 10
1406need eq arglen args 3 64 10
1407hxvalid argpos args 3
1408for j ulong 64
1409put mc j get mb add argpos args 3 j
1410end
1411prof = argpos args 4
1412profn = arglen args 4
1413if lit prof profn debugnm
1414put mg 0 0
1415else
1416need lit prof profn releasenm 10
1417put mg 0 1
1418end
1419need lit argpos args 5 arglen args 5 targetnm 10
1420si = ulong 2
1421if isbuild
1422si = 6
1423end
1424p = argpos args si
1425n = arglen args si
1426s = parent p n
1427fd = fopen mp s sub n s 49152 0
1428need ge fd 0 25
1429put mg 4 ulong fd
1430restore
1431if isbuild
1432runbuild args
1433else
1434runlink args
1435end
1436ret 0
1437end