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 / tools / pi-drive-27b / omguard.om

752 lines · 10172 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tools/pi-drive-27b/omguard.om: Pure seeded Omega (OM-1) library unit.
3note 1) Incremental OM-1 line/stream syntax and register guard (X925 cores).
4note 2) Unified-RAM prefix-pool index with FNV-1a 64-bit chunk hashing and LRU eviction (A725 cores).
5note Library unit: no fn main. Prefix: og / OG.
6
7const OGLINEMAX ulong 512
8const OGMAXPAR ulong 6
9const OGMAXLOC ulong 16
10const OGSLOTS ulong 64
11const OGCHUNKS ulong 16
12const OGPOOLH ulong 1024
13const OGPBUFMAX ulong 4096
14
15const OGFNVOFF ulong 14695981039346656037
16const OGFNVPRM ulong 1099511628211
17
18const OGOK long 0
19const OGEHEX long -1
20const OGEPAR long -2
21const OGELOC long -3
22const OGEBND long -4
23const OGEEND long -5
24const OGENFN long -6
25
26note Stream guard state:
27note ogst[0]=depth, ogst[1]=infn, ogst[2]=nlocs, ogst[3]=lines
28global ogline uchar 512
29global ogst ulong 8
30global oglocs ulong 32
31
32note Unified-RAM prefix-pool state:
33note ogp_meta[0]=tick, ogp_meta[1]=best_slot, ogp_meta[2]=best_len, ogp_meta[3]=best_pa
34global ogp_act ulong 64
35global ogp_nch ulong 64
36global ogp_hsh ulong 1024
37global ogp_pa ulong 64
38global ogp_lru ulong 64
39global ogp_q ulong 16
40global ogp_meta ulong 8
41global ogp_buf uchar 4096
42
43note --- Part 1: Incremental OM-1 Line/Stream Guard ---
44
45fn oginit to void
46i = ulong 0
47while lt i 8 bound 8
48put ogst i 0
49i = add i 1
50end
51i = ulong 0
52while lt i 32 bound 32
53put oglocs i 0
54i = add i 1
55end
56end
57
58fn ogdepth to ulong
59ret get ogst 0
60end
61
62fn oginfn to ulong
63ret get ogst 1
64end
65
66fn ognlocs to ulong
67ret get ogst 2
68end
69
70fn ogisws c uchar to ulong
71if eq c 32
72ret 1
73end
74if eq c 9
75ret 1
76end
77if eq c 13
78ret 1
79end
80if eq c 10
81ret 1
82end
83ret 0
84end
85
86fn ogskipws pos ulong n ulong to ulong
87p = pos
88while lt p n bound 512
89c = get ogline p
90if eq ogisws c 0
91break
92end
93p = add p 1
94end
95ret p
96end
97
98fn ogtokend pos ulong n ulong to ulong
99p = pos
100while lt p n bound 512
101c = get ogline p
102if ne ogisws c 0
103break
104end
105p = add p 1
106end
107ret p
108end
109
110fn ogtrim n ulong to ulong
111m = n
112if gt m OGLINEMAX
113m = OGLINEMAX
114end
115while gt m 0 bound 512
116c = get ogline sub m 1
117if eq c 10
118m = sub m 1
119else
120if eq c 13
121m = sub m 1
122else
123break
124end
125end
126end
127ret m
128end
129
130fn ogisnote s ulong e ulong to ulong
131if ne sub e s 4
132ret 0
133end
134if ne get ogline s 110
135ret 0
136end
137if ne get ogline add s 1 111
138ret 0
139end
140if ne get ogline add s 2 116
141ret 0
142end
143if ne get ogline add s 3 101
144ret 0
145end
146ret 1
147end
148
149fn ogisfn s ulong e ulong to ulong
150if ne sub e s 2
151ret 0
152end
153if ne get ogline s 102
154ret 0
155end
156if ne get ogline add s 1 110
157ret 0
158end
159ret 1
160end
161
162fn ogisend s ulong e ulong to ulong
163if ne sub e s 3
164ret 0
165end
166if ne get ogline s 101
167ret 0
168end
169if ne get ogline add s 1 110
170ret 0
171end
172if ne get ogline add s 2 100
173ret 0
174end
175ret 1
176end
177
178fn ogisif s ulong e ulong to ulong
179if ne sub e s 2
180ret 0
181end
182if ne get ogline s 105
183ret 0
184end
185if ne get ogline add s 1 102
186ret 0
187end
188ret 1
189end
190
191fn ogisfor s ulong e ulong to ulong
192if ne sub e s 3
193ret 0
194end
195if ne get ogline s 102
196ret 0
197end
198if ne get ogline add s 1 111
199ret 0
200end
201if ne get ogline add s 2 114
202ret 0
203end
204ret 1
205end
206
207fn ogiswhile s ulong e ulong to ulong
208if ne sub e s 5
209ret 0
210end
211if ne get ogline s 119
212ret 0
213end
214if ne get ogline add s 1 104
215ret 0
216end
217if ne get ogline add s 2 105
218ret 0
219end
220if ne get ogline add s 3 108
221ret 0
222end
223if ne get ogline add s 4 101
224ret 0
225end
226ret 1
227end
228
229fn ogisbound s ulong e ulong to ulong
230if ne sub e s 5
231ret 0
232end
233if ne get ogline s 98
234ret 0
235end
236if ne get ogline add s 1 111
237ret 0
238end
239if ne get ogline add s 2 117
240ret 0
241end
242if ne get ogline add s 3 110
243ret 0
244end
245if ne get ogline add s 4 100
246ret 0
247end
248ret 1
249end
250
251fn ogisto s ulong e ulong to ulong
252if ne sub e s 2
253ret 0
254end
255if ne get ogline s 116
256ret 0
257end
258if ne get ogline add s 1 111
259ret 0
260end
261ret 1
262end
263
264fn ogisview s ulong e ulong to ulong
265if ne sub e s 4
266ret 0
267end
268if ne get ogline s 118
269ret 0
270end
271if ne get ogline add s 1 105
272ret 0
273end
274if ne get ogline add s 2 101
275ret 0
276end
277if ne get ogline add s 3 119
278ret 0
279end
280ret 1
281end
282
283fn ogisedit s ulong e ulong to ulong
284if ne sub e s 4
285ret 0
286end
287if ne get ogline s 101
288ret 0
289end
290if ne get ogline add s 1 100
291ret 0
292end
293if ne get ogline add s 2 105
294ret 0
295end
296if ne get ogline add s 3 116
297ret 0
298end
299ret 1
300end
301
302fn ogiseq s ulong e ulong to ulong
303if ne sub e s 1
304ret 0
305end
306if ne get ogline s 61
307ret 0
308end
309ret 1
310end
311
312fn ogisnumstar s ulong e ulong to ulong
313if le e s
314ret 0
315end
316c = get ogline s
317if eq c 42
318ret 1
319end
320if ge c 48
321if le c 57
322ret 1
323end
324end
325ret 0
326end
327
328fn ogtokhash s ulong e ulong to ulong
329h = OGFNVOFF
330p = s
331while lt p e bound 512
332b = ulong get ogline p
333h = xor h b
334h = mul h OGFNVPRM
335p = add p 1
336end
337ret h
338end
339
340fn ogaddloc s ulong e ulong to long
341h = ogtokhash s e
342cnt = get ogst 2
343i = ulong 0
344while lt i cnt bound 32
345if eq get oglocs i h
346ret OGOK
347end
348i = add i 1
349end
350if ge cnt OGMAXLOC
351ret OGELOC
352end
353put oglocs cnt h
354put ogst 2 add cnt 1
355ret OGOK
356end
357
358fn ogishexsep c uchar to ulong
359if ne ogisws c 0
360ret 1
361end
362if eq c 40
363ret 1
364end
365if eq c 45
366ret 1
367end
368ret 0
369end
370
371note Scan line outside double-quoted strings for 0x or 0X hex prefix.
372fn ogscanhex pos ulong n ulong to long
373i = pos
374instr = ulong 0
375esc = ulong 0
376while lt i n bound 512
377c = get ogline i
378if ne instr 0
379if ne esc 0
380esc = 0
381else
382if eq c 92
383esc = 1
384else
385if eq c 34
386instr = 0
387end
388end
389end
390else
391if eq c 34
392instr = 1
393else
394if eq c 48
395if lt add i 1 n
396nxt = get ogline add i 1
397isx = ulong 0
398if eq nxt 120
399isx = 1
400end
401if eq nxt 88
402isx = 1
403end
404if ne isx 0
405okprev = ulong 0
406if eq i pos
407okprev = 1
408else
409prev = get ogline sub i 1
410okprev = ogishexsep prev
411end
412if ne okprev 0
413ret OGEHEX
414end
415end
416end
417end
418end
419end
420i = add i 1
421end
422ret OGOK
423end
424
425fn ogparsefn pos ulong n ulong to long
426if ne get ogst 1 0
427ret OGENFN
428end
429note Skip function name token.
430p = ogskipws pos n
431p = ogtokend p n
432put ogst 2 0
433np = ulong 0
434while lt p n bound 512
435s = ogskipws p n
436if ge s n
437break
438end
439e = ogtokend s n
440if ne ogisto s e 0
441break
442end
443np = add np 1
444if gt np OGMAXPAR
445put ogst 2 0
446ret OGEPAR
447end
448lr = ogaddloc s e
449if lt lr 0
450put ogst 2 0
451ret lr
452end
453note Skip parameter type token (and array type/len if view/edit).
454ts = ogskipws e n
455te = ogtokend ts n
456p = te
457isarr = ulong 0
458if ne ogisview ts te 0
459isarr = 1
460end
461if ne ogisedit ts te 0
462isarr = 1
463end
464if ne isarr 0
465es = ogskipws te n
466ee = ogtokend es n
467p = ee
468ls = ogskipws ee n
469le2 = ogtokend ls n
470if ne ogisnumstar ls le2 0
471p = le2
472end
473end
474end
475put ogst 1 1
476put ogst 0 add get ogst 0 1
477ret OGOK
478end
479
480fn ogparsewhile pos ulong n ulong to long
481p = pos
482found = ulong 0
483while lt p n bound 512
484s = ogskipws p n
485if ge s n
486break
487end
488e = ogtokend s n
489if ne ogisbound s e 0
490ns = ogskipws e n
491if lt ns n
492found = 1
493break
494end
495end
496p = e
497end
498if eq found 0
499ret OGEBND
500end
501put ogst 0 add get ogst 0 1
502ret OGOK
503end
504
505fn ogcheckline n ulong to long
506m = ogtrim n
507put ogst 3 add get ogst 3 1
508s0 = ogskipws 0 m
509if ge s0 m
510ret OGOK
511end
512e0 = ogtokend s0 m
513if ne ogisnote s0 e0 0
514ret OGOK
515end
516hr = ogscanhex s0 m
517if lt hr 0
518ret hr
519end
520if ne ogisfn s0 e0 0
521ret ogparsefn e0 m
522end
523if ne ogisend s0 e0 0
524d = get ogst 0
525if eq d 0
526ret OGEEND
527end
528nd = sub d 1
529put ogst 0 nd
530if eq nd 0
531put ogst 1 0
532put ogst 2 0
533end
534ret OGOK
535end
536if ne ogisif s0 e0 0
537put ogst 0 add get ogst 0 1
538ret OGOK
539end
540if ne ogisfor s0 e0 0
541put ogst 0 add get ogst 0 1
542if ne get ogst 1 0
543vs = ogskipws e0 m
544if lt vs m
545ve = ogtokend vs m
546ret ogaddloc vs ve
547end
548end
549ret OGOK
550end
551if ne ogiswhile s0 e0 0
552ret ogparsewhile e0 m
553end
554if ne get ogst 1 0
555s1 = ogskipws e0 m
556if lt s1 m
557e1 = ogtokend s1 m
558if ne ogiseq s1 e1 0
559ret ogaddloc s0 e0
560end
561end
562end
563ret OGOK
564end
565
566note --- Part 2: Unified-RAM Prefix-Pool Index ---
567
568fn ogpinit to void
569i = ulong 0
570while lt i OGSLOTS bound 64
571put ogp_act i 0
572put ogp_nch i 0
573put ogp_pa i 0
574put ogp_lru i 0
575i = add i 1
576end
577i = ulong 0
578while lt i OGPOOLH bound 1024
579put ogp_hsh i 0
580i = add i 1
581end
582i = ulong 0
583while lt i OGCHUNKS bound 16
584put ogp_q i 0
585i = add i 1
586end
587i = ulong 0
588while lt i 8 bound 8
589put ogp_meta i 0
590i = add i 1
591end
592end
593
594note FNV-1a 64-bit hash over byte chunk ogp_buf[off .. off+n).
595fn ogphash off ulong n ulong to ulong
596if ge off OGPBUFMAX
597ret OGFNVOFF
598end
599lim = sub OGPBUFMAX off
600cnt = n
601if gt cnt lim
602cnt = lim
603end
604h = OGFNVOFF
605i = ulong 0
606while lt i cnt bound 4096
607b = ulong get ogp_buf add off i
608h = xor h b
609h = mul h OGFNVPRM
610i = add i 1
611end
612ret h
613end
614
615note Feed a 64-bit token ID (8 bytes LE) into FNV-1a 64-bit state.
616fn ogphash_tok tok ulong prev ulong to ulong
617h = prev
618v = tok
619i = ulong 0
620while lt i 8 bound 8
621b = and v 255
622h = xor h b
623h = mul h OGFNVPRM
624v = shr v 8
625i = add i 1
626end
627ret h
628end
629
630fn ogpput slot ulong nchunks ulong pa ulong to long
631if ge slot OGSLOTS
632ret -1
633end
634if gt nchunks OGCHUNKS
635ret -1
636end
637tick = add get ogp_meta 0 1
638put ogp_meta 0 tick
639put ogp_act slot 1
640put ogp_nch slot nchunks
641put ogp_pa slot pa
642put ogp_lru slot tick
643base = mul slot OGCHUNKS
644i = ulong 0
645while lt i nchunks bound 16
646put ogp_hsh add base i get ogp_q i
647i = add i 1
648end
649ret 0
650end
651
652fn ogslotmatch slot ulong nchunks ulong to ulong
653sn = get ogp_nch slot
654lim = nchunks
655if lt sn lim
656lim = sn
657end
658base = mul slot OGCHUNKS
659m = ulong 0
660while lt m lim bound 16
661if ne get ogp_hsh add base m get ogp_q m
662break
663end
664m = add m 1
665end
666ret m
667end
668
669fn ogpmatch nchunks ulong to long
670qlen = nchunks
671if gt qlen OGCHUNKS
672qlen = OGCHUNKS
673end
674bslot = OGSLOTS
675blen = ulong 0
676blru = ulong 0
677s = ulong 0
678while lt s OGSLOTS bound 64
679if ne get ogp_act s 0
680m = ogslotmatch s qlen
681take = ulong 0
682if gt m blen
683take = 1
684end
685if eq m blen
686if gt m 0
687if gt get ogp_lru s blru
688take = 1
689end
690end
691end
692if ne take 0
693blen = m
694bslot = s
695blru = get ogp_lru s
696end
697end
698s = add s 1
699end
700if eq blen 0
701put ogp_meta 1 OGSLOTS
702put ogp_meta 2 0
703put ogp_meta 3 0
704ret -1
705end
706tick = add get ogp_meta 0 1
707put ogp_meta 0 tick
708put ogp_lru bslot tick
709put ogp_meta 1 bslot
710put ogp_meta 2 blen
711put ogp_meta 3 get ogp_pa bslot
712ret long bslot
713end
714
715fn ogpmatch_len to ulong
716ret get ogp_meta 2
717end
718
719fn ogpmatch_pa to ulong
720ret get ogp_meta 3
721end
722
723fn ogpevict to long
724found = ulong 0
725vslot = ulong 0
726vlru = ulong 0
727s = ulong 0
728while lt s OGSLOTS bound 64
729if ne get ogp_act s 0
730tlru = get ogp_lru s
731if eq found 0
732found = 1
733vslot = s
734vlru = tlru
735else
736if lt tlru vlru
737vslot = s
738vlru = tlru
739end
740end
741end
742s = add s 1
743end
744if eq found 0
745ret -1
746end
747put ogp_act vslot 0
748put ogp_nch vslot 0
749put ogp_pa vslot 0
750put ogp_lru vslot 0
751ret long vslot
752end