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

950 lines · 15100 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Critical source profile. Compile the checked unit too; this is not a parser/typechecker.
3note Full-byte token identity, finite pools, fail-closed capacity checks. See README.
4global src uchar 65536
5global off ulong 16384
6global siz ulong 16384
7global ls ulong 4096
8global lc ulong 4096
9global lf ulong 4096
10global dep ulong 4096
11global fs ulong 128
12global fe ulong 128
13global nm ulong 128
14global fx uchar 128
15global mem ulong 128
16global edges uchar 16384
17global weak uchar 16384
18global reach uchar 16384
19global alive uchar 128
20global ko ulong 64
21global kl ulong 64
22global st ulong 8
23global host ulong 128
24global hb uchar 16384
25global arg uchar 4096
26global ob uchar 24
27text keys "note fn end if else while for bound new global text const to ret exit trap put requires ensures invariant om2 release = len ulong uint ushort uchar long int short byte view edit own add gt depth p10 effect allow E1 pure main"
28text hp "seed/effects/effects.om"
29text tpre "text tab "
30text pass "PROFILE-PASS\n"
31text bad "PROFILE-REFUSED:"
32text rules "B1B2B3E1X1P1C1"
33text lpre ":"
34text inv "PROFILE-INVALID\n"
35text use "profile [bounds] [SOURCE ...] < unit.om\n"
36text bmode "bounds"
37fn stop code int to void
38if ne write 1 inv 0 len inv long len inv
39exit 4
40end
41exit code
42end
43note Rules: 0 B1 loop bound, 1 B2 recursion, 2 B3 allocation, 3 E1 checked result, 4 X1 effect boundary, 5 P1 pure, 6 C1 contract mode.
44fn refuse rule ulong l ulong to void
45if ne write 1 bad 0 len bad long len bad
46exit 4
47end
48if ne write 1 rules mul rule 2 2 2
49exit 4
50end
51if ne write 1 lpre 0 len lpre long len lpre
52exit 4
53end
54put ob 23 10
55v = add l 1
56i = ulong 23
57for j ulong 20 bound 20
58i = sub i 1
59put ob i uchar add 48 rem v 10
60v = div v 10
61if eq v 0
62break
63end
64end
65if ne write 1 ob i sub 24 i long sub 24 i
66exit 4
67end
68exit 1
69end
70fn init to void
71s = ulong 0
72k = ulong 0
73for i ulong len keys
74if eq get keys i 32
75put ko k s
76put kl k sub i s
77k = add k 1
78s = add i 1
79end
80end
81put ko k s
82put kl k sub len keys s
83end
84fn keyis t ulong k ulong to bool
85if eq t 0
86ret false
87end
88if ne get siz t get kl k
89ret false
90end
91o = get ko k
92for j ulong get siz t bound 63
93x = get src add get off t j
94if ne x get keys add o j
95ret false
96end
97end
98ret true
99end
100fn word t ulong to ulong
101if or eq t 0 gt get siz t 8
102ret 0
103end
104v = ulong 0
105for j ulong get siz t bound 8
106c = ulong get src add get off t j
107v = or v shl c mul j 8
108end
109ret v
110end
111fn at l ulong p ulong to ulong
112if ge p get lc l
113ret 0
114end
115ret add get ls l p
116end
117fn is l ulong p ulong k ulong to bool
118ret keyis at l p k
119end
120fn same a ulong b ulong to bool
121if or eq a 0 eq b 0
122ret false
123end
124if ne get siz a get siz b
125ret false
126end
127for j ulong get siz a bound 63
128x = get src add get off a j
129y = get src add get off b j
130if ne x y
131ret false
132end
133end
134ret true
135end
136fn number t ulong to ulong
137if eq t 0
138ret 1048577
139end
140v = ulong 0
141for j ulong get siz t bound 63
142c = get src add get off t j
143if or lt c 48 gt c 57
144ret 1048577
145end
146if gt v 1048576
147ret 1048577
148end
149v = add mul v 10 sub ulong c 48
150end
151ret v
152end
153fn value t ulong to ulong
154v = number t
155if le v 1048576
156ret v
157end
158for l ulong get st 1 bound 4096
159if is l 0 11
160if same t at l 1
161ret number at l 3
162end
163end
164end
165ret 1048577
166end
167fn func t ulong to ulong
168for f ulong get st 2 bound 128
169if same t get nm f
170ret f
171end
172end
173ret 128
174end
175fn token a ulong n ulong to void
176if eq n 0
177ret
178end
179if gt n 63
180stop 3
181end
182t = add get st 0 1
183if ge t 16384
184stop 3
185end
186put off t a
187put siz t n
188put st 0 t
189l = get st 1
190if eq get lc l 0
191put ls l t
192end
193put lc l add get lc l 1
194end
195fn blank c ulong to bool
196if or eq c 32 eq c 9
197ret true
198end
199if or eq c 13 eq c 40
200ret true
201end
202ret eq c 41
203end
204fn tabpre i ulong to bool
205for k ulong len tpre
206if ne get hb add sub i len tpre k get tpre k
207ret false
208end
209end
210ret true
211end
212fn lex n ulong to void
213a = ulong 0
214size = ulong 0
215quote = false
216esc = false
217skip = false
218for i ulong n bound 65536
219c = get src i
220if eq c 10
221token a size
222size = ulong 0
223quote = false
224esc = false
225skip = false
226l = add get st 1 1
227if ge l 4096
228stop 3
229end
230put st 1 l
231continue
232end
233if skip
234continue
235end
236if quote
237if esc
238esc = false
239else
240if eq c 92
241esc = true
242else
243if eq c 34
244quote = false
245end
246end
247end
248continue
249end
250if eq c 34
251token a size
252size = ulong 0
253quote = true
254continue
255end
256if blank ulong c
257token a size
258size = ulong 0
259note Preserve only p10 annotations; ordinary comments need no bounded token storage.
260l = get st 1
261if ge get lc l 2
262if and is l 0 0 not is l 1 38
263skip = true
264end
265end
266continue
267end
268if eq size 0
269a = i
270end
271size = add size 1
272end
273token a size
274put st 1 add get st 1 1
275end
276fn hosts to void
277fd = fopen hp 0 len hp 0 0
278if lt fd 0
279stop 3
280end
281n = read int fd hb 0 len hb
282if ne fclose int fd 0
283stop 3
284end
285if le n 0
286stop 3
287end
288note Find the exact authoritative effects table prefix, then decode only its first column.
289a = ulong 0
290found = false
291for i ulong ulong n bound 16384
292c = get hb i
293if ge i len tpre
294if and eq c 34 tabpre i
295a = add i 1
296found = true
297break
298end
299end
300end
301if not found
302stop 3
303end
304hdecode a ulong n
305end
306note cnt 9 marks a row whose first column was already stored.
307fn hdecode a ulong n ulong to void
308w = ulong 0
309cnt = ulong 0
310for j ulong sub n a bound 16384
311c = get hb add a j
312if eq c 34
313if eq get st 3 0
314stop 3
315end
316ret
317end
318if eq c 110
319if eq get hb sub add a j 1 92
320w = ulong 0
321cnt = ulong 0
322continue
323end
324end
325if lt cnt 9
326if eq c 32
327k = get st 3
328if ge k 128
329stop 3
330end
331put host k w
332put st 3 add k 1
333cnt = ulong 9
334else
335if ge cnt 8
336stop 3
337end
338w = or w shl ulong c mul cnt 8
339cnt = add cnt 1
340end
341end
342end
343stop 3
344end
345fn ishost t ulong to bool
346w = word t
347for i ulong get st 3 bound 128
348if eq w get host i
349ret true
350end
351end
352ret false
353end
354fn declarations to void
355f = ulong 128
356d = ulong 0
357for l ulong get st 1 bound 4096
358if is l 0 1
359if ne d 0
360stop 3
361end
362f = get st 2
363if ge f 128
364stop 3
365end
366put fs f l
367put nm f at l 1
368put st 2 add f 1
369if gt l 0
370p = sub l 1
371if and is p 0 0 and is p 1 38 is p 2 39
372put fx f 1
373end
374if and is p 0 0 and is p 1 38 is p 2 42
375put fx f 2
376end
377end
378d = ulong 1
379else
380if is l 0 2
381if eq d 0
382stop 3
383end
384d = sub d 1
385if eq d 0
386put fe f l
387end
388end
389end
390put lf l f
391put dep l d
392if or is l 0 3 or is l 0 5 is l 0 6
393d = add d 1
394end
395end
396if or ne d 0 eq get st 2 0
397stop 3
398end
399end
400note Shared assignment classifier for B2, B3 and E1: the index of the value's first token when line l
401note assigns, else 0. omc s_name accepts NAME [=] value: a leading name that is not a statement
402note keyword (indexes 0..21), a unit function or a host word assigns even without =. omc refuses a
403note parameter, global or local that shadows a function or host word (errors 10 and 16).
404fn asg l ulong to ulong
405if lt get lc l 2
406ret 0
407end
408if is l 1 22
409ret 2
410end
411for k ulong 22
412if is l 0 k
413ret 0
414end
415end
416t = at l 0
417if or lt func t 128 ishost t
418ret 0
419end
420bare = ulong 1
421ret bare
422end
423fn exempt l ulong to bool
424if eq l 0
425ret false
426end
427p = sub l 1
428ret and gt get lc p 4 and is p 0 0 and is p 1 38 and is p 2 40 is p 3 41
429end
430fn mentions k ulong name ulong to bool
431start = asg k
432for p ulong sub get lc k start bound 16384
433t = at k add p start
434if same name t
435ret true
436end
437end
438ret false
439end
440note Walk the straight-line continuation: leave blocks outward, skip the sibling else branch.
441fn used l ulong name ulong to bool
442d = get dep l
443e = get fe get lf l
444sk = ulong 0
445for j ulong sub e l bound 4096
446k = add add l j 1
447dk = get dep k
448if lt dk d
449d = dk
450end
451if ne sk 0
452if ge dk sk
453continue
454end
455sk = ulong 0
456end
457if is k 0 0
458continue
459end
460if and eq dk d is k 0 4
461sk = d
462continue
463end
464if mentions k name
465ret true
466end
467if eq dk d
468if and ne asg k 0 same name at k 0
469ret false
470end
471if or is k 0 13 or is k 0 14 is k 0 15
472ret false
473end
474end
475end
476ret false
477end
478note Size of t if line r declares it (global, text or same-function new); 0 if it does not.
479fn dsize r ulong t ulong fl ulong to ulong
480if and is r 0 9 same at r 1 t
481ret value at r 3
482end
483if and is r 0 10 same at r 1 t
484note Source span conservatively includes the declaration words, quotes and escapes.
485q = add r 1
486if lt q get st 1
487if gt get ls q 0
488ret sub get off get ls q get off at r 0
489end
490end
491ret 1048577
492end
493if ne get lf r fl
494ret 0
495end
496v = asg r
497if and ne v 0 and is r v 8 same at r 0 t
498ret value at r add v 2
499end
500ret 0
501end
502fn length l ulong t ulong to ulong
503fl = get lf l
504for r ulong l bound 4096
505v = dsize r t fl
506if ne v 0
507ret v
508end
509end
510if ge fl 128
511ret 1048577
512end
513r = get fs fl
514for p ulong get lc r bound 16384
515if same at r p t
516q = add p 1
517if or is r q 32 or is r q 33 is r q 34
518ret value at r add p 3
519end
520end
521end
522ret 1048577
523end
524fn checkloop l ulong to void
525v = ulong 1048577
526if is l 0 6
527v = value at l 3
528if is l 3 23
529v = length l at l 4
530end
531end
532for p ulong get lc l bound 16384
533if is l p 7
534v = value at l add p 1
535end
536end
537if gt v 8192
538refuse 0 l
539end
540end
541fn width l ulong t ulong to ulong
542if or keyis t 24 keyis t 28
543ret 8
544end
545if or keyis t 25 keyis t 29
546ret 4
547end
548if or keyis t 26 keyis t 30
549ret 2
550end
551if or keyis t 27 keyis t 31
552ret 1
553end
554refuse 2 l
555ret 0
556end
557fn salloc l ulong f ulong to void
558if is l 0 9
559v = value at l 3
560if gt v 65536
561refuse 2 l
562end
563b = mul v width l at l 2
564put st 5 add get st 5 b
565if gt get st 5 65536
566refuse 2 l
567end
568end
569if or ge f 128 is l 0 1
570ret
571end
572a = asg l
573if and ne a 0 is l a 8
574v = value at l add a 2
575if gt v 65536
576refuse 2 l
577end
578b = mul v width l at l add a 1
579put mem f add get mem f b
580put st 7 add get st 7 b
581if gt get st 7 65536
582refuse 2 l
583end
584if gt get mem f 65536
585refuse 2 l
586end
587end
588end
589fn scalls l ulong f ulong to bool
590call = false
591for p ulong get lc l bound 16384
592t = at l p
593g = func t
594if lt g 128
595a = add mul f 128 g
596put edges a 1
597put reach a 1
598call = true
599end
600if ishost t
601call = true
602if eq get st 4 0
603if ne get fx f 1
604refuse 4 l
605end
606end
607end
608end
609ret call
610end
611fn scan to void
612for l ulong get st 1 bound 4096
613if eq get lc l 0
614continue
615end
616if is l 0 0
617continue
618end
619if is l 0 20
620if or is l 1 21 is l 2 21
621refuse 6 l
622end
623put st 6 1
624continue
625end
626if or is l 0 5 is l 0 6
627checkloop l
628end
629f = get lf l
630salloc l f
631if or ge f 128 is l 0 1
632continue
633end
634call = scalls l f
635if and eq get st 4 0 and ne asg l 0 call
636if not exempt l
637if not used l at l 0
638refuse 3 l
639end
640end
641end
642if eq get fx f 2
643if is l 0 16
644note Pure functions may inspect arrays but cannot write even borrowed state.
645refuse 5 l
646end
647end
648if or is l 0 17 or is l 0 18 is l 0 19
649if ne get st 6 1
650refuse 6 l
651end
652end
653end
654end
655fn crow i ulong k ulong n ulong to void
656a = mul i 128
657b = mul k 128
658for j ulong n bound 128
659if ne get reach add b j 0
660put reach add a j 1
661end
662end
663end
664fn closure to void
665n = get st 2
666for k ulong n bound 128
667for i ulong n bound 128
668if ne get reach add mul i 128 k 0
669crow i k n
670end
671end
672end
673end
674fn stops q ulong to bool
675ret or is q 0 13 or is q 0 14 is q 0 15
676end
677note No mutation of the checked depth between guard and recursive call.
678fn unchanged s ulong e ulong argt ulong to bool
679for h ulong sub e s bound 4096
680r = add s h
681if ne asg r 0
682if same at r 0 argt
683ret false
684end
685end
686end
687ret true
688end
689note Line p must be NAME = add depth 1, then if gt NAME LIM (LIM <= 64), an optional call statement,
690note a stop, end: the stop is the last statement of the guard's then-block (no else, no nested block).
691fn guarded p ulong l ulong argt ulong name ulong to bool
692if not is p 2 35
693ret false
694end
695if not same at p 3 name
696ret false
697end
698if ne number at p 4 1
699ret false
700end
701q = add p 1
702if not and is q 0 3 is q 1 36
703ret false
704end
705if not same at q 2 argt
706ret false
707end
708lim = value at q 3
709if or eq lim 0 gt lim 64
710ret false
711end
712q = add q 1
713note Permit the existing nest.om stop-code call before the return; only a call to a unit function,
714note never else or another structural line, so the stop belongs to the guard's then-block.
715if not stops q
716if ge func at q 0 128
717ret false
718end
719q = add q 1
720end
721if not stops q
722ret false
723end
724q = add q 1
725if not is q 0 2
726ret false
727end
728if ge q l
729ret false
730end
731ret unchanged add q 1 l argt
732end
733note Lines p and l share one block instance: no line between them leaves the block (depth below dl)
734note or starts its sibling else arm (else at depth dl). Equal depth alone is not enough.
735fn sameblk p ulong l ulong dl ulong to bool
736for h ulong sub l add p 1 bound 4096
737r = add add p 1 h
738if lt get dep r dl
739ret false
740end
741if and eq get dep r dl is r 0 4
742ret false
743end
744end
745ret true
746end
747note A qualifying increasing edge has a canonical dominating guard in the same block instance.
748fn increasing l ulong t ulong f ulong to bool
749name = at get fs f 2
750argt = add t 1
751dl = get dep l
752for p ulong l bound 4096
753if ne get lf p f
754continue
755end
756if ne get dep p dl
757continue
758end
759if eq asg p 0
760continue
761end
762if same at p 0 argt
763if not sameblk p l dl
764continue
765end
766ret guarded p l argt name
767end
768end
769ret false
770end
771note Classify one call t to g on line r of f; keep all but guarded increasing in-SCC edges.
772fn redge r ulong t ulong f ulong g ulong cyclic bool to void
773a = add mul f 128 g
774gg = mul g 128
775if eq get reach add gg g 0
776put weak a 1
777ret
778end
779inscc = and cyclic ne get reach add gg f 0
780if not inscc
781put weak a 1
782if gt value add t 1 64
783refuse 1 r
784end
785ret
786end
787if same add t 1 at get fs f 2
788put weak a 1
789ret
790end
791if not increasing r t f
792refuse 1 r
793end
794end
795fn rline r ulong f ulong cyclic bool to void
796for p ulong get lc r bound 16384
797t = at r p
798g = func t
799if lt g 128
800redge r t f g cyclic
801end
802end
803end
804fn isleaf i ulong n ulong to bool
805b = mul i 128
806for j ulong n bound 128
807if ne get weak add b j 0
808if ne get alive j 0
809ret false
810end
811end
812end
813ret true
814end
815fn prune n ulong to void
816for i ulong n bound 128
817put alive i 1
818end
819for round ulong n bound 128
820for i ulong n bound 128
821if isleaf i n
822put alive i 0
823end
824end
825end
826end
827fn recursion to void
828n = get st 2
829for f ulong n bound 128
830h = get fs f
831cyclic = ne get reach add mul f 128 f 0
832if cyclic
833if ne get mem f 0
834refuse 1 h
835end
836if not is h 2 37
837refuse 1 h
838end
839end
840for l ulong sub get fe f h bound 4096
841r = add add h l 1
842if is r 0 0
843continue
844end
845if and cyclic and ne asg r 0 same at r 0 at h 2
846refuse 1 r
847end
848rline r f cyclic
849end
850end
851note DRA-311/omc leaf-removal analysis, applied after removing guarded increasing edges.
852prune n
853for i ulong n bound 128
854if ne get alive i 0
855refuse 1 get fs i
856end
857if eq get fx i 2
858for j ulong n bound 128
859if and ne get edges add mul i 128 j 0 ne get fx j 2
860refuse 5 get fs i
861end
862end
863end
864end
865end
866fn slurp fd int n ulong to ulong
867total = n
868while true bound 65536
869r = read fd src total sub len src total
870if lt r 0
871exit 4
872end
873if eq r 0
874ret total
875end
876total = add total ulong r
877if ge total len src
878stop 3
879end
880end
881stop 3
882ret 0
883end
884fn main to int
885init
886ac = ulong argcnt
887if gt ac 16
888if ne write 2 use 0 len use long len use
889ret 4
890end
891ret 2
892end
893first = ulong 1
894if gt ac 1
895m = argread 1 arg 0 len arg
896mode = eq m long len bmode
897if mode
898for i ulong len bmode
899if ne get arg i get bmode i
900mode = false
901end
902end
903end
904if mode
905put st 4 1
906first = ulong 2
907end
908end
909n = ulong 0
910if eq ac first
911n = slurp 0 0
912else
913for j ulong sub ac first bound 16
914m = argread add first j arg 0 len arg
915if or le m 0 ge m long len arg
916ret 2
917end
918fd = fopen arg 0 ulong m 0 0
919if lt fd 0
920ret 4
921end
922n = slurp int fd n
923if ne fclose int fd 0
924ret 4
925end
926if gt n 0
927if ne get src sub n 1 10
928put src n 10
929n = add n 1
930if ge n len src
931stop 3
932end
933end
934end
935end
936end
937if eq n 0
938stop 3
939end
940lex n
941hosts
942declarations
943scan
944closure
945recursion
946if ne write 1 pass 0 len pass long len pass
947ret 4
948end
949ret 0
950end