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 / src / adapters / zig-u64-export.om

429 lines · 6476 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Handwritten extended-S1-family upper-to-Zig 0.17 exporter (spec/universal/zig-u64-v1.md section 4).
3note Prepended unchanged at build: src/source-aware/vendor/sha256.om and src/source-aware/core-v2.om (decode n).
4note Input: one OMSB envelope to EOF. Output is generated from the OMSA operation records only; the
5note OMSP sidecar is validated by the v2 decoder, never read for output.
6global Zone uchar 1
7global Zo uchar 4096
8global Zl ulong 1
9global Zni ulong 1
10global Zob ulong 1
11global Zpr ulong 64
12global Zln ulong 64
13global Zdp ulong 64
14global Zcn ulong 64
15global Zrc ulong 64
16global Zus ulong 6
17text Zhead "pub fn calc("
18text Zpar ": u64"
19text Zret ") u64 {\n return "
20text Ztail ";\n}\n"
21text Zas "@as(u64, "
22text Zop " +% -% *% "
23text ZMal "ZIG_EXPORT_MALFORMED\n"
24text ZLim "ZIG_EXPORT_LIMIT\n"
25text ZProf "ZIG_EXPORT_PROFILE_MISMATCH\n"
26text ZIO "ZIG_EXPORT_IO\n"
27
28fn zdie code ulong to void
29if eq code 65
30w = write 2 ZMal 0 len ZMal
31end
32if eq code 67
33w = write 2 ZLim 0 len ZLim
34end
35if eq code 68
36w = write 2 ZProf 0 len ZProf
37end
38if eq code 77
39w = write 2 ZIO 0 len ZIO
40end
41exit int code
42end
43
44fn zbyte c ulong to void
45put Zo get Zl 0 uchar c
46put Zl 0 add get Zl 0 1
47end
48
49fn ztext off ulong n ulong to void
50for i ulong n
51zbyte ulong get Zop add off i
52end
53end
54
55fn zdigits v ulong to ulong
56d = ulong 1
57x = div v 10
58while gt x 0
59d = add d 1
60x = div x 10
61end
62ret d
63end
64
65fn zdec v ulong to void
66d = new uchar 20 0
67i = ulong 0
68x = v
69put d 0 uchar add rem x 10 48
70x = div x 10
71i = add i 1
72while gt x 0
73put d i uchar add rem x 10 48
74x = div x 10
75i = add i 1
76end
77while gt i 0
78i = sub i 1
79zbyte ulong get d i
80end
81end
82
83note Family membership per translation-compose-v2 section 1, on fields already validated by decode.
84fn zkind i ulong to ulong
85c = get Oc i
86if or ne get Ot i 1 ne get Oef i 0
87ret 68
88end
89if ne get Oal i 0
90ret 68
91end
92if eq c 9
93if or ne get Oat i 0 ne get Oob i 1
94ret 68
95end
96put Zni 0 add get Zni 0 1
97ret 0
98end
99if eq c 1
100if or ne get Oat i 1 ne get Oob i 0
101ret 68
102end
103ret 0
104end
105if or lt c 2 gt c 4
106ret 68
107end
108if or ne get Oat i 1 ne get Oob i get Zob 0
109ret 68
110end
111ret 0
112end
113
114note (language, profile) -> arithmetic obligations: C 1/1, Rust 2/2, Python 3/4 (5), C++ 5/7, Zig 6/8, Go 7/9.
115fn zmember to ulong
116l = get M 0
117p = get M 1
118if and eq l 1 eq p 1
119ret 1
120end
121if and eq l 2 eq p 2
122ret 1
123end
124if and eq l 3 eq p 4
125ret 5
126end
127if and eq l 5 eq p 7
128ret 1
129end
130if and eq l 6 eq p 8
131ret 1
132end
133if and eq l 7 eq p 9
134ret 1
135end
136ret 0
137end
138
139fn zs1 to ulong
140ob = zmember
141if eq ob 0
142ret 68
143end
144put Zob 0 ob
145if or ne get M 2 0 ne get M 3 1
146ret 68
147end
148put Zni 0 0
149for i ulong get M 4
150if ne zkind i 0
151ret 68
152end
153end
154for j ulong get M 4
155if eq get Oc j 9
156if ge get Oi j get Zni 0
157ret 68
158end
159end
160end
161ret 0
162end
163
164fn zsat v ulong to ulong
165if gt v 1048576
166ret 1048576
167end
168ret v
169end
170
171note Left operand parenthesised iff its precedence is lower than the parent's, right iff lower or equal.
172fn zpa k ulong to ulong
173if lt get Zpr get Oa k get Zpr k
174ret 1
175end
176ret 0
177end
178
179fn zpb k ulong to ulong
180if le get Zpr get Ob k get Zpr k
181ret 1
182end
183ret 0
184end
185
186note Two constant operands: the left one is written @as(u64, N) so the operation is u64, not comptime_int.
187fn zwrap k ulong to ulong
188if and eq get Oc get Oa k 1 eq get Oc get Ob k 1
189ret 1
190end
191ret 0
192end
193
194note Length, parenthesis depth and re-import operation count of each result's expression.
195fn zmetric k ulong to void
196c = get Oc k
197put Zdp k 0
198if eq c 9
199put Zpr k 3
200put Zln k 2
201put Zcn k 0
202ret
203end
204if eq c 1
205put Zpr k 3
206put Zln k zdigits get Oi k
207put Zcn k 1
208ret
209end
210put Zpr k 1
211if eq c 4
212put Zpr k 2
213end
214a = get Oa k
215b = get Ob k
216pa = zpa k
217pb = zpb k
218wr = zwrap k
219l = add get Zln a get Zln b
220l = add l add 4 mul 2 add pa pb
221l = add l mul 10 wr
222put Zln k zsat l
223da = add add get Zdp a pa wr
224db = add get Zdp b pb
225if gt da db
226put Zdp k da
227else
228put Zdp k db
229end
230put Zcn k zsat add add get Zcn a get Zcn b 1
231end
232
233fn zemit k ulong to void
234c = get Oc k
235if eq c 9
236zbyte 120
237zbyte add 48 get Oi k
238ret
239end
240if eq c 1
241zdec get Oi k
242ret
243end
244pa = zpa k
245pb = zpb k
246wr = zwrap k
247if eq pa 1
248zbyte 40
249end
250if eq wr 1
251for i ulong len Zas
252zbyte ulong get Zas i
253end
254end
255zemit get Oa k
256if eq wr 1
257zbyte 41
258end
259if eq pa 1
260zbyte 41
261end
262ztext mul sub c 2 4 4
263if eq pb 1
264zbyte 40
265end
266zemit get Ob k
267if eq pb 1
268zbyte 41
269end
270end
271
272note translation-v1 section 4: the pre-normalization root of a NORMALIZED unit, else the last result.
273fn zroot to ulong
274n = get M 4
275if lt n 3
276ret sub n 1
277end
278z = sub n 2
279y = sub n 1
280if or ne get Oc z 1 ne get Oi z 0
281ret y
282end
283if or ne get Oc y 2 ne get Ob y z
284ret y
285end
286if ge get Oa y z
287ret y
288end
289ret get Oa y
290end
291
292note Input slots reached from the root are named x<slot>; every other slot is the discard _ (Zig rejects
293note an unused named parameter).
294fn zreach root ulong to void
295for i ulong 64
296put Zrc i 0
297end
298for s ulong 6
299put Zus s 0
300end
301put Zrc root 1
302k = add root 1
303while gt k 0
304k = sub k 1
305if eq get Zrc k 1
306c = get Oc k
307if and ge c 2 le c 4
308put Zrc get Oa k 1
309put Zrc get Ob k 1
310end
311if eq c 9
312put Zus get Oi k 1
313end
314end
315end
316end
317
318fn zparams to ulong
319ni = get Zni 0
320if eq ni 0
321ret 0
322end
323t = sub mul ni 8 2
324for s ulong ni
325t = add t get Zus s
326end
327ret t
328end
329
330note ", " separators; a used slot is "x<s>: u64" (7 bytes), an unused one "_: u64" (6 bytes).
331fn zsignature to void
332for i ulong len Zhead
333zbyte ulong get Zhead i
334end
335for s ulong get Zni 0
336if gt s 0
337zbyte 44
338zbyte 32
339end
340if eq get Zus s 1
341zbyte 120
342zbyte add 48 s
343else
344zbyte 95
345end
346for t ulong len Zpar
347zbyte ulong get Zpar t
348end
349end
350for u ulong len Zret
351zbyte ulong get Zret u
352end
353end
354
355fn zreadunit to ulong
356n = ulong 0
357more = ulong 1
358while and eq more 1 lt n 8192
359r = read 0 Ib n sub ulong 8192 n
360if lt r 0
361zdie 77
362end
363if eq r 0
364more = 0
365else
366n = add n ulong r
367end
368end
369if eq n 8192
370r = read 0 Zone 0 1
371if lt r 0
372zdie 77
373end
374if gt r 0
375zdie 65
376end
377end
378ret n
379end
380
381fn main to int
382constants
383n = zreadunit
384code = decode n
385if eq code 68
386zdie 68
387end
388if ne code 0
389zdie 65
390end
391if ne zs1 0
392zdie 68
393end
394for k ulong get M 4
395zmetric k
396end
397root = zroot
398zreach root
399total = add add len Zhead len Zret len Ztail
400total = add total zparams
401total = add total get Zln root
402if gt total 4096
403zdie 67
404end
405if gt get Zdp root 16
406zdie 67
407end
408if gt add add get Zni 0 get Zcn root 2 64
409zdie 67
410end
411put Zl 0 0
412zsignature
413zemit root
414for i ulong len Ztail
415zbyte ulong get Ztail i
416end
417if ne get Zl 0 total
418zdie 65
419end
420sent = ulong 0
421while lt sent total
422r = write 1 Zo sent sub total sent
423if le r 0
424zdie 77
425end
426sent = add sent ulong r
427end
428ret 0
429end