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 / rust-i64-export.om

571 lines · 8909 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Handwritten S2 upper-to-Rust 2021 exporter (spec/universal/translation-s2-v1.md sections 2 and 4).
3note Prepended unchanged at build: src/source-aware/vendor/sha256.om and src/source-aware/core-v3.om (decode n).
4note Input: one OMSB envelope to EOF, a source-aware v3 unit of profile 10..15. Output is generated from the
5note OMSA operation records only; the OMSP sidecar is validated by the v3 decoder, never read for output.
6note u64 arithmetic and i64 attribute 5 use wrapping_add/sub/mul; i64 attribute 6 uses plain + - *, which
7note panics on overflow in a debug build; select is `if c { x } else { y }`, evaluated lazily like op 16.
8global Yone uchar 1
9global Yo uchar 4096
10global Yl ulong 1
11global Yni ulong 1
12global Ypt ulong 6
13global Yln ulong 64
14global Ynm uchar 64
15global Ynl ulong 1
16global Ylb uchar 1
17text Yhead "pub fn "
18text Ydflt "calc"
19text Ykw "_ as break const continue crate else enum extern false fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait true type unsafe use where while async await dyn abstract become box do final macro override priv typeof unsized virtual yield try gen"
20text Yu ": u64"
21text Yi ": i64"
22text Yru ") -> u64 { "
23text Yri ") -> i64 { "
24text Yrb ") -> bool { "
25text Ytail " }\n"
26text Ymeth ".wrapping_add(.wrapping_sub(.wrapping_mul("
27text Yops " + - * "
28text Ycmp " == != < <= > >= "
29text Ysel1 "if "
30text Ysel2 " { "
31text Ysel3 " } else { "
32text Ysel4 " }"
33text Ytrue "true"
34text Yfalse "false"
35text YMal "RUST_EXPORT_MALFORMED\n"
36text YLim "RUST_EXPORT_LIMIT\n"
37text YProf "RUST_EXPORT_PROFILE_MISMATCH\n"
38text YIO "RUST_EXPORT_IO\n"
39text YName "RUST_EXPORT_BAD_NAME\n"
40
41fn ydie code ulong to void
42if eq code 65
43w = write 2 YMal 0 len YMal
44end
45if eq code 67
46w = write 2 YLim 0 len YLim
47end
48if eq code 64
49w = write 2 YName 0 len YName
50end
51if eq code 68
52w = write 2 YProf 0 len YProf
53end
54if eq code 77
55w = write 2 YIO 0 len YIO
56end
57exit int code
58end
59
60fn yalnum c ulong to bool
61if and ge c 65 le c 90
62ret true
63end
64if and ge c 97 le c 122
65ret true
66end
67if and ge c 48 le c 57
68ret true
69end
70ret eq c 95
71end
72
73fn ykwchar p ulong to bool
74if ge p len Ykw
75ret false
76end
77ret ne get Ykw p 32
78end
79
80fn ykwsame b ulong e ulong to bool
81if ne sub e b get Ynl 0
82ret false
83end
84for i ulong get Ynl 0
85if ne get Ykw add b i get Ynm i
86ret false
87end
88end
89ret true
90end
91
92note ykeyword: NAME is a Rust 2021 strict or reserved keyword (or the lone `_`), so not a function name.
93fn ykeyword to bool
94p = ulong 0
95while lt p len Ykw
96b = p
97while ykwchar p
98p = add p 1
99end
100if ykwsame b p
101ret true
102end
103p = add p 1
104end
105ret false
106end
107
108note ytakename (amendment A8): input frame as c-export: one byte name length L (0 = calc), L name bytes, then
109note the envelope. NAME is a C identifier of at most 63 bytes and not a Rust keyword; otherwise 64.
110fn ytakename to void
111r = read 0 Ylb 0 1
112if lt r 0
113ydie 77
114end
115if eq r 0
116ydie 64
117end
118n = ulong get Ylb 0
119if eq n 0
120for i ulong len Ydflt
121put Ynm i get Ydflt i
122end
123put Ynl 0 len Ydflt
124ret
125end
126if gt n 63
127ydie 64
128end
129got = ulong 0
130while lt got n
131r = read 0 Ynm got sub n got
132if lt r 0
133ydie 77
134end
135if eq r 0
136ydie 64
137end
138got = add got ulong r
139end
140put Ynl 0 n
141f = ulong get Ynm 0
142if and ge f 48 le f 57
143ydie 64
144end
145for i ulong n
146if not yalnum ulong get Ynm i
147ydie 64
148end
149end
150if ykeyword
151ydie 64
152end
153end
154
155fn ybyte c ulong to void
156put Yo get Yl 0 uchar c
157put Yl 0 add get Yl 0 1
158end
159
160fn ydigits v ulong to ulong
161d = ulong 1
162x = div v 10
163while gt x 0
164d = add d 1
165x = div x 10
166end
167ret d
168end
169
170fn ydec v ulong to void
171d = new uchar 20 0
172i = ulong 0
173x = v
174put d 0 uchar add rem x 10 48
175x = div x 10
176i = add i 1
177while gt x 0
178put d i uchar add rem x 10 48
179x = div x 10
180i = add i 1
181end
182while gt i 0
183i = sub i 1
184ybyte ulong get d i
185end
186end
187
188note ys2: a profile 10..15 unit (already fully validated by decode); records each input slot's type.
189fn ys2 to ulong
190p = get M 1
191if or lt p 10 gt p 15
192ret 68
193end
194put Yni 0 0
195for i ulong get M 4
196if eq get Oc i 9
197put Yni 0 add get Yni 0 1
198end
199end
200for j ulong get M 4
201if eq get Oc j 9
202if ge get Oi j get Yni 0
203ret 68
204end
205put Ypt get Oi j get Ot j
206end
207end
208ret 0
209end
210
211fn ysat v ulong to ulong
212if gt v 1048576
213ret 1048576
214end
215ret v
216end
217
218note ywrapped: the record is printed inside one pair of parentheses (dropped at the root).
219fn ywrapped k ulong to bool
220c = get Oc k
221if ge c 10
222ret true
223end
224if and ge c 2 le c 4
225ret and eq get Ot k 5 eq get Oat k 6
226end
227if eq c 1
228ret and eq get Ot k 5 ge get Oi k 9223372036854775808
229end
230ret false
231end
232
233fn ytrim k ulong to ulong
234if ywrapped k
235ret 2
236end
237ret 0
238end
239
240fn yclen c ulong to ulong
241if or eq c 12 eq c 14
242ret 3
243end
244ret 4
245end
246
247note Length of each result's expression as printed inside a larger expression.
248fn ymetric k ulong to void
249c = get Oc k
250t = get Ot k
251if eq c 9
252put Yln k 2
253ret
254end
255if eq c 1
256v = get Oi k
257if eq t 6
258put Yln k sub 5 v
259ret
260end
261if and eq t 5 ge v 9223372036854775808
262put Yln k add ydigits sub 0 v 6
263ret
264end
265put Yln k add ydigits v 3
266ret
267end
268a = get Oa k
269if eq c 17
270put Yln k ysat add get Yln a 3
271ret
272end
273b = get Ob k
274if eq c 16
275s = add add get Yln a get Yln b get Yln get Oi k
276s = sub s add add ytrim a ytrim b ytrim get Oi k
277put Yln k ysat add s 20
278ret
279end
280if ge c 10
281put Yln k ysat add add add get Yln a get Yln b yclen c 2
282ret
283end
284if ywrapped k
285put Yln k ysat add add get Yln a get Yln b 5
286ret
287end
288put Yln k ysat add add get Yln a get Yln b 15
289end
290
291fn ytext off ulong n ulong to void
292for i ulong n
293ybyte ulong get Ycmp add off i
294end
295end
296
297fn yemit k ulong top ulong to void
298c = get Oc k
299t = get Ot k
300if eq c 9
301ybyte 105
302ybyte add 48 get Oi k
303ret
304end
305if eq c 1
306v = get Oi k
307if eq t 6
308if eq v 1
309for i ulong len Ytrue
310ybyte ulong get Ytrue i
311end
312else
313for i ulong len Yfalse
314ybyte ulong get Yfalse i
315end
316end
317ret
318end
319if and eq t 5 ge v 9223372036854775808
320if eq top 0
321ybyte 40
322end
323ybyte 45
324ydec sub 0 v
325ybyte 105
326ybyte 54
327ybyte 52
328if eq top 0
329ybyte 41
330end
331ret
332end
333ydec v
334if eq t 5
335ybyte 105
336else
337ybyte 117
338end
339ybyte 54
340ybyte 52
341ret
342end
343if eq c 16
344if eq top 0
345ybyte 40
346end
347for i ulong len Ysel1
348ybyte ulong get Ysel1 i
349end
350yemit get Oa k 1
351for i ulong len Ysel2
352ybyte ulong get Ysel2 i
353end
354yemit get Ob k 1
355for i ulong len Ysel3
356ybyte ulong get Ysel3 i
357end
358yemit get Oi k 1
359for i ulong len Ysel4
360ybyte ulong get Ysel4 i
361end
362if eq top 0
363ybyte 41
364end
365ret
366end
367if eq c 17
368if eq top 0
369ybyte 40
370end
371ybyte 33
372yemit get Oa k 0
373if eq top 0
374ybyte 41
375end
376ret
377end
378if ge c 10
379if eq top 0
380ybyte 40
381end
382yemit get Oa k 0
383off = mul sub c 10 4
384ytext off yclen c
385yemit get Ob k 0
386if eq top 0
387ybyte 41
388end
389ret
390end
391if ywrapped k
392if eq top 0
393ybyte 40
394end
395yemit get Oa k 0
396off = mul sub c 2 2
397for i ulong 3
398ybyte ulong get Yops add off i
399end
400yemit get Ob k 0
401if eq top 0
402ybyte 41
403end
404ret
405end
406yemit get Oa k 0
407off = mul sub c 2 14
408for i ulong 14
409ybyte ulong get Ymeth add off i
410end
411yemit get Ob k 0
412ybyte 41
413end
414
415note translation-v1 section 4 on S2 (translation-s2-v1 amendment A3): the pre-normalization root of a
416note NORMALIZED unit (const 0 then add attribute 5 of an i64 root, or the S1 u64 pair), else the last result.
417fn yroot to ulong
418n = get M 4
419if lt n 3
420ret sub n 1
421end
422z = sub n 2
423y = sub n 1
424if or ne get Oc z 1 ne get Oi z 0
425ret y
426end
427if or ne get Oc y 2 ne get Ob y z
428ret y
429end
430if ge get Oa y z
431ret y
432end
433if eq get Ot y 5
434if ne get Oat y 5
435ret y
436end
437end
438ret get Oa y
439end
440
441fn yparams to ulong
442ni = get Yni 0
443if eq ni 0
444ret 0
445end
446ret sub mul ni 9 2
447end
448
449fn yrettext t ulong to void
450if eq t 1
451for u ulong len Yru
452ybyte ulong get Yru u
453end
454end
455if eq t 5
456for u ulong len Yri
457ybyte ulong get Yri u
458end
459end
460if eq t 6
461for u ulong len Yrb
462ybyte ulong get Yrb u
463end
464end
465end
466
467fn ysignature t ulong to void
468for i ulong len Yhead
469ybyte ulong get Yhead i
470end
471for i ulong get Ynl 0
472ybyte ulong get Ynm i
473end
474ybyte 40
475for s ulong get Yni 0
476if gt s 0
477ybyte 44
478ybyte 32
479end
480ybyte 105
481ybyte add 48 s
482if eq get Ypt s 5
483for q ulong len Yi
484ybyte ulong get Yi q
485end
486else
487for q ulong len Yu
488ybyte ulong get Yu q
489end
490end
491end
492yrettext t
493end
494
495fn yreadunit to ulong
496n = ulong 0
497more = ulong 1
498while and eq more 1 lt n 8192
499r = read 0 Ib n sub ulong 8192 n
500if lt r 0
501ydie 77
502end
503if eq r 0
504more = 0
505else
506n = add n ulong r
507end
508end
509if eq n 8192
510r = read 0 Yone 0 1
511if lt r 0
512ydie 77
513end
514if gt r 0
515ydie 65
516end
517end
518ret n
519end
520
521fn main to int
522constants
523ytakename
524n = yreadunit
525code = decode n
526if eq code 68
527ydie 68
528end
529if ne code 0
530ydie 65
531end
532if ne ys2 0
533ydie 68
534end
535for k ulong get M 4
536ymetric k
537end
538root = yroot
539t = get Ot root
540total = add add len Yhead len Yru len Ytail
541total = add total add get Ynl 0 1
542if eq t 6
543total = add total 1
544end
545total = add total yparams
546total = add total get Yln root
547if ywrapped root
548total = sub total 2
549end
550if gt total 4096
551ydie 67
552end
553put Yl 0 0
554ysignature t
555yemit root 1
556for i ulong len Ytail
557ybyte ulong get Ytail i
558end
559if ne get Yl 0 total
560ydie 65
561end
562sent = ulong 0
563while lt sent total
564r = write 1 Yo sent sub total sent
565if le r 0
566ydie 77
567end
568sent = add sent ulong r
569end
570ret 0
571end