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 / check / a64check.om

922 lines · 15163 bytes Raw Blame History
1om2
2note a64check.om -- REFERENCE CHECK ONLY for the seed chain (seed/README.md). It is never used to make a
3note byte of seed/. Seeded-Omega port of seed/check/a64check.c together with the two C files that program
4note links, src/aarch64_decoder.c (aarch64_decode_instruction) and src/aarch64_encoder.c (the legacy
5note emitters), so the hand-written words can be compared without a C compiler.
6note om/aarch64/aarch64.om is an existing API port of the same routines. This CLI currently duplicates
7note those routines; consolidation needs separate parity of its CLI conversions and refusal output.
8note Signal identity parity is scoped to default signal dispositions. With SIGABRT inherited ignored,
9note killpid does not reset that disposition: this port falls back to exit 134, unlike C abort().
10note a64check decode stdin: one hex word per line; stdout: the decoder's fields per word
11note a64check encode <form> <args...> one word from the legacy encoder for that form, as 8 hex digits
12note Parity contract with the C program (stdout, stderr and exit status byte for byte):
13note - decode reads stdin the way fgets with a 128-byte buffer does (chunks of at most 127 bytes, each
14note ending at a newline or at the end of input) and reads each chunk the way strtoul base 16 does
15note (leading C-locale space, a sign, an optional 0x, hex digits; overflow gives all ones), then keeps
16note the low 32 bits. An unknown word prints op=INVALID with every field 0.
17note - encode reads each argument the way strtol base 0 does (sign, 0x hex, leading-0 octal, decimal,
18note saturating at the long limits) and then converts it the way the C call does to the emitter's
19note parameter type: bool, uint8_t, uint16_t, int16_t or int32_t (Aarch64Cond is read as int32_t).
20note - an out-of-range field prints "A64_LEGACY_FIELD_OUT_OF_RANGE aarch64_emit_FORM FIELD=VALUE" on
21note stderr, like the C refuse(), and the program stops itself with SIGABRT (status 134 in sh), like abort().
22note - wrong form or argument count: "a64check: unknown form or argument count: FORM" on stderr, exit 2;
23note anything else: the usage line on stderr, exit 2.
24note Not ported: aarch64_validate_code_buffer (a64check.c never calls it) and the "REFUSED" exit 1 path,
25note which the C program cannot reach (every emitter gets a 4-byte buffer at position 0).
26text tminl "9223372036854775808"
27text opn "INVALID ADD SUB MUL AND ORR EOR MOVZ RET B B_COND CBZ CBNZ MOVK ADR LDR STR LDRB STRB SUBS "
28text forms "add_reg sub_reg mul_reg and_reg orr_reg eor_reg subs_reg subs_imm mov_reg movz movk ret b b_cond cbz cbnz adr ldr_uoff str_uoff ldrb_uoff strb_uoff ldr_post str_post ldr_x_post str_x_post "
29text usage "usage: a64check decode | a64check encode <form> <args...>\n"
30text unk "a64check: unknown form or argument count: "
31text oor "A64_LEGACY_FIELD_OUT_OF_RANGE aarch64_emit_"
32text tdecode "decode"
33text tencode "encode"
34text hexd "0123456789abcdef"
35text tsf " sf="
36text trdq " rd="
37text trnq " rn="
38text trmq " rm="
39text timmq " imm16="
40text thwq " hw="
41text tbimq " bimm="
42text tcndq " cond="
43text topq "op="
44text trd "rd"
45text trn "rn"
46text trm "rm"
47text trt "rt"
48text tshift "shift"
49text timm26 "imm26"
50text tcond "cond"
51text timm19 "imm19"
52text timm21 "imm21"
53text tuoff "uoff"
54text tsimm9 "simm9"
55text timm12 "imm12"
56global ib uchar 65536
57global ist ulong 3
58global ob uchar 65536
59global oc ulong 1
60global lb uchar 128
61global ab uchar 131072
62global fm uchar 131072
63global fmn ulong 1
64global df ulong 9
65global av ulong 8
66
67note ---- output: one buffer, flushed to a descriptor when full and at the end ----
68note oflush: every pass writes at least one byte of at most len ob, so len ob passes suffice.
69fn oflush fd int to void
70off = ulong 0
71n = get oc 0
72while lt off n bound 65536
73w = write fd ob off sub n off
74if le w 0
75put oc 0 0
76ret
77end
78off = add off ulong w
79end
80put oc 0 0
81end
82
83fn oputb b uchar to void
84m = get oc 0
85if ge m len ob
86oflush 1
87m = get oc 0
88end
89put ob m b
90put oc 0 add m 1
91end
92
93fn oputt t view uchar * to void
94for i ulong len t
95oputb get t i
96end
97end
98
99fn oputn v ulong to void
100d = new uchar 20 0
101i = ulong 0
102x = v
103put d 0 uchar add rem x 10 48
104x = div x 10
105i = add i 1
106while gt x 0 bound 20
107put d i uchar add rem x 10 48
108x = div x 10
109i = add i 1
110end
111j = i
112while gt j 0 bound 20
113j = sub j 1
114oputb get d j
115end
116end
117
118note oputs: a signed value in decimal, as printf %lld does.
119fn oputs v long to void
120if lt v 0
121oputb 45
122if eq v -9223372036854775808
123oputt tminl
124ret
125end
126oputn ulong neg v
127ret
128end
129oputn ulong v
130end
131
132
133fn ohex8 x ulong to void
134for i ulong 8
135s = sub 28 mul i 4
136oputb get hexd and shr x s 15
137end
138end
139
140note oname: word k of opn, the C opname() table.
141fn oname k ulong to void
142w = ulong 0
143for i ulong len opn
144c = get opn i
145if eq c 32
146w = add w 1
147else
148if eq w k
149oputb c
150end
151end
152end
153end
154
155fn oputfm to void
156for i ulong get fmn 0
157oputb get fm i
158end
159end
160
161note ---- input: stdin in 64 KiB blocks ----
162fn getc to long
163p = get ist 0
164n = get ist 1
165if ge p n
166if ne get ist 2 0
167ret -1
168end
169r = read 0 ib 0 len ib
170if le r 0
171put ist 2 1
172ret -1
173end
174put ist 0 0
175put ist 1 ulong r
176p = get ist 0
177end
178c = get ib p
179put ist 0 add p 1
180ret long c
181end
182
183note rdline: one fgets chunk into lb: at most 127 bytes, ending after a newline; 0 only at end of input.
184fn rdline to ulong
185k = ulong 0
186while lt k 127 bound 128
187c = getc
188if lt c 0
189ret k
190end
191put lb k uchar c
192k = add k 1
193if eq c 10
194ret k
195end
196end
197ret k
198end
199
200note lc: byte i of the current chunk, or 0 (the C string end) past it.
201fn lc i ulong k ulong to ulong
202if lt i k
203ret ulong get lb i
204end
205ret 0
206end
207
208note ac: byte i of the current argument (length k in ab), or 0 past it.
209fn ac i ulong k ulong to ulong
210if lt i k
211ret ulong get ab i
212end
213ret 0
214end
215
216fn isspc c ulong to bool
217if eq c 32
218ret true
219end
220ret and ge c 9 le c 13
221end
222
223note dval: the value of a digit or letter in bases up to 36, 99 for anything else.
224fn dval c ulong to ulong
225if and ge c 48 le c 57
226ret sub c 48
227end
228if and ge c 97 le c 122
229ret sub c 87
230end
231if and ge c 65 le c 90
232ret sub c 55
233end
234ret 99
235end
236
237note pul: strtoul(chunk, NULL, 16) over lb[0..k).
238fn pul k ulong to ulong
239i = ulong 0
240while isspc lc i k bound 128
241i = add i 1
242end
243ng = false
244c = lc i k
245if eq c 45
246ng = true
247i = add i 1
248else
249if eq c 43
250i = add i 1
251end
252end
253if eq lc i k 48
254if eq or lc add i 1 k 32 120
255if lt dval lc add i 2 k 16
256i = add i 2
257end
258end
259end
260v = ulong 0
261ov = false
262d = dval lc i k
263while lt d 16 bound 130
264if gt v div sub 0xffffffffffffffff d 16
265ov = true
266end
267v = add mul v 16 d
268i = add i 1
269d = dval lc i k
270end
271if ov
272ret 0xffffffffffffffff
273end
274if ng
275ret neg v
276end
277ret v
278end
279
280note sl: strtol(argv[i], NULL, 0) as the bits of a long.
281fn sl i ulong to ulong
282n = argread i ab 0 len ab
283k = ulong 0
284if gt n 0
285k = ulong n
286end
287j = ulong 0
288while isspc ac j k bound 131080
289j = add j 1
290end
291ng = false
292c = ac j k
293if eq c 45
294ng = true
295j = add j 1
296else
297if eq c 43
298j = add j 1
299end
300end
301b = ulong 10
302if eq ac j k 48
303b = 8
304if eq or ac add j 1 k 32 120
305b = 16
306j = add j 2
307end
308end
309v = ulong 0
310ov = false
311d = dval ac j k
312while lt d b bound 131080
313if gt v div sub 0xffffffffffffffff d b
314ov = true
315end
316v = add mul v b d
317j = add j 1
318d = dval ac j k
319end
320if ov
321if ng
322ret 0x8000000000000000
323end
324ret 0x7fffffffffffffff
325end
326if ng
327if gt v 0x8000000000000000
328ret 0x8000000000000000
329end
330ret neg v
331end
332if gt v 0x7fffffffffffffff
333ret 0x7fffffffffffffff
334end
335ret v
336end
337
338note ---- the decoder (src/aarch64_decoder.c, same order of tests) ----
339fn f3 op ulong w ulong to void
340put df 0 op
341put df 1 and shr w 31 1
342put df 2 and w 31
343put df 3 and shr w 5 31
344put df 4 and shr w 16 31
345end
346
347fn fcb op ulong w ulong to void
348put df 0 op
349put df 1 and shr w 31 1
350put df 2 and w 31
351put df 7 and shr w 5 0x7ffff
352end
353
354fn fmv op ulong w ulong to void
355put df 0 op
356put df 1 and shr w 31 1
357put df 2 and w 31
358put df 5 and shr w 5 0xffff
359put df 6 and shr w 21 3
360end
361
362fn fls op ulong w ulong to void
363put df 0 op
364put df 1 and shr w 30 1
365put df 2 and w 31
366put df 3 and shr w 5 31
367end
368
369fn dec w ulong to void
370for i ulong 9
371put df i 0
372end
373if eq w 0xd65f03c0
374put df 0 8
375put df 3 30
376ret
377end
378if eq and w 0xfc000000 0x14000000
379put df 0 9
380put df 7 and w 0x3ffffff
381ret
382end
383if eq and w 0xff000010 0x54000000
384put df 0 10
385put df 8 and w 15
386put df 7 and shr w 5 0x7ffff
387ret
388end
389if eq and w 0x7f000000 0x34000000
390fcb 11 w
391ret
392end
393if eq and w 0x7f000000 0x35000000
394fcb 12 w
395ret
396end
397if eq and w 0x7f800000 0x52800000
398fmv 7 w
399ret
400end
401if eq and w 0x7fe07c00 0x1b007c00
402f3 3 w
403ret
404end
405dec2 w
406end
407
408fn dec2 w ulong to void
409m = and w 0x7f200000
410if eq m 0xa000000
411f3 4 w
412ret
413end
414if eq m 0x2a000000
415f3 5 w
416ret
417end
418if eq m 0x4a000000
419f3 6 w
420ret
421end
422if eq m 0xb000000
423f3 1 w
424ret
425end
426if eq m 0x4b000000
427f3 2 w
428ret
429end
430if eq and w 0x7f800000 0x72800000
431fmv 13 w
432ret
433end
434if eq and w 0x9f000000 0x10000000
435put df 0 14
436put df 2 and w 31
437ret
438end
439dec3 w
440end
441
442fn dec3 w ulong to void
443if eq and w 0xbf400000 0xb9400000
444fls 15 w
445ret
446end
447if eq and w 0xbf400000 0xb9000000
448fls 16 w
449ret
450end
451if eq and w 0xbfe00400 0xb8400400
452fls 15 w
453ret
454end
455if eq and w 0xbfe00400 0xb8000400
456fls 16 w
457ret
458end
459if eq and w 0xffc00000 0x39400000
460fls 17 w
461put df 1 0
462ret
463end
464if eq and w 0xffc00000 0x39000000
465fls 18 w
466put df 1 0
467ret
468end
469if eq and w 0x7f800000 0x71000000
470f3 19 w
471put df 4 0
472ret
473end
474if eq and w 0x7f200000 0x6b000000
475f3 19 w
476ret
477end
478end
479
480fn decline to void
481oputt topq
482oname get df 0
483oputt tsf
484oputn get df 1
485oputt trdq
486oputn get df 2
487oputt trnq
488oputn get df 3
489oputt trmq
490oputn get df 4
491oputt timmq
492oputn get df 5
493oputt thwq
494oputn get df 6
495oputt tbimq
496oputn get df 7
497oputt tcndq
498oputn get df 8
499oputb 10
500end
501
502fn decode to int
503while true bound 9223372036854775807
504k = rdline
505if eq k 0
506oflush 1
507ret 0
508end
509dec and pul k 0xffffffff
510decline
511end
512oflush 1
513ret 0
514end
515
516note ---- the legacy encoder (src/aarch64_encoder.c) ----
517note refuse: the C refuse(): one stderr line, then SIGABRT like abort().
518fn refuse f view uchar * v long to void
519put oc 0 0
520oputt oor
521oputfm
522oputb 32
523oputt f
524oputb 61
525oputs v
526oputb 10
527oflush 2
528r = killpid int getpid 6
529exit 134
530end
531
532fn creg r ulong f view uchar * to void
533if gt r 31
534refuse f long r
535end
536end
537
538note csig: CHECK_SIGNED(v, bits) on the value the C parameter holds.
539fn csig v long bits ulong f view uchar * to void
540h = long shl ulong 1 sub bits 1
541if or lt v neg h ge v h
542refuse f v
543end
544end
545
546fn cumax v ulong f view uchar * to void
547if gt v 0xfff
548refuse f long v
549end
550end
551
552fn cv8 x ulong to ulong
553ret and x 255
554end
555
556fn cv16 x ulong to ulong
557ret and x 0xffff
558end
559
560fn s16 x ulong to long
561v = and x 0xffff
562if ge v 0x8000
563ret sub long v 65536
564end
565ret long v
566end
567
568fn s32 x ulong to long
569v = and x 0xffffffff
570if ge v 0x80000000
571ret sub long v 4294967296
572end
573ret long v
574end
575
576fn bsf x ulong to ulong
577if ne x 0
578ret 1
579end
580ret 0
581end
582
583note formid: 1-based index of the form name in forms, 0 when it is not one.
584fn formid to ulong
585id = ulong 1
586st = ulong 0
587n = get fmn 0
588for i ulong len forms
589if eq get forms i 32
590if eq sub i st n
591if fmeq st
592ret id
593end
594end
595id = add id 1
596st = add i 1
597end
598end
599ret 0
600end
601
602fn fmeq st ulong to bool
603for i ulong get fmn 0
604if ne get fm i get forms add st i
605ret false
606end
607end
608ret true
609end
610
611note needc: the argc the C chain requires for form id.
612fn needc id ulong to long
613if le id 8
614ret 7
615end
616if le id 9
617ret 6
618end
619if le id 11
620ret 7
621end
622if eq id 12
623ret 3
624end
625if eq id 13
626ret 4
627end
628if eq id 14
629ret 5
630end
631if le id 16
632ret 6
633end
634if eq id 17
635ret 5
636end
637if le id 19
638ret 7
639end
640ret 6
641end
642
643fn a3base id ulong to ulong
644if eq id 1
645ret 0xb000000
646end
647if eq id 2
648ret 0x4b000000
649end
650if eq id 3
651ret 0x1b007c00
652end
653if eq id 4
654ret 0xa000000
655end
656if eq id 5
657ret 0x2a000000
658end
659if eq id 6
660ret 0x4a000000
661end
662ret 0x6b000000
663end
664
665note enc3: add_reg .. subs_reg (ids 1..7): sf rd rn rm, CHECK3.
666fn enc3 id ulong to ulong
667sf = bsf get av 3
668rd = cv8 get av 4
669rn = cv8 get av 5
670rm = cv8 get av 6
671creg rd trd
672creg rn trn
673creg rm trm
674ret or or or or shl sf 31 a3base id shl rm 16 shl rn 5 rd
675end
676
677fn encsi to ulong
678sf = bsf get av 3
679rd = cv8 get av 4
680rn = cv8 get av 5
681im = cv16 get av 6
682creg rd trd
683creg rn trn
684cumax im timm12
685ret or or or or shl sf 31 0x71000000 shl im 10 shl rn 5 rd
686end
687
688fn encmov to ulong
689sf = bsf get av 3
690rd = cv8 get av 4
691rm = cv8 get av 5
692creg rd trd
693creg rm trm
694ret or or or or shl sf 31 0x2a000000 shl rm 16 0x3e0 rd
695end
696
697fn encmz base ulong to ulong
698sf = bsf get av 3
699rd = cv8 get av 4
700im = cv16 get av 5
701s = cv8 get av 6
702creg rd trd
703mx = ulong 16
704if ne sf 0
705mx = 48
706end
707if or ne rem s 16 0 gt s mx
708refuse tshift long s
709end
710hw = and div s 16 3
711ret or or or or shl sf 31 base shl hw 21 shl im 5 rd
712end
713
714fn encb to ulong
715x = get av 3
716csig s32 x 26 timm26
717ret or 0x14000000 and x 0x3ffffff
718end
719
720fn encbc to ulong
721c = s32 get av 3
722if or lt c 0 gt c 15
723refuse tcond c
724end
725x = get av 4
726csig s32 x 19 timm19
727ret or or 0x54000000 shl and x 0x7ffff 5 and get av 3 15
728end
729
730fn enccb base ulong to ulong
731sf = bsf get av 3
732rt = cv8 get av 4
733x = get av 5
734creg rt trt
735csig s32 x 19 timm19
736ret or or or shl sf 31 base shl and x 0x7ffff 5 rt
737end
738
739fn encadr to ulong
740rd = cv8 get av 3
741x = get av 4
742creg rd trd
743csig s32 x 21 timm21
744lo = shl and x 3 29
745hi = shl and shr and x 0xffffffff 2 0x7ffff 5
746ret or or or 0x10000000 lo hi rd
747end
748
749fn encuo ld bool to ulong
750sf = bsf get av 3
751rt = cv8 get av 4
752rn = cv8 get av 5
753uo = cv16 get av 6
754creg rt trt
755creg rn trn
756sz = ulong 4
757sc = ulong 2
758if ne sf 0
759sz = 8
760sc = 3
761end
762if or ne rem uo sz 0 gt div uo sz 0xfff
763refuse tuoff long uo
764end
765base = ulong 0xb9000000
766if ne sf 0
767base = 0xf9000000
768end
769if ld
770base = or base 0x400000
771end
772ret or or or base shl and shr uo sc 0xfff 10 shl rn 5 rt
773end
774
775fn encbo base ulong to ulong
776rt = cv8 get av 3
777rn = cv8 get av 4
778uo = cv16 get av 5
779creg rt trt
780creg rn trn
781cumax uo tuoff
782ret or or or base shl and uo 0xfff 10 shl rn 5 rt
783end
784
785fn encpo base ulong to ulong
786rt = cv8 get av 3
787rn = cv8 get av 4
788x = get av 5
789creg rt trt
790creg rn trn
791csig s16 x 9 tsimm9
792ret or or or base shl and x 0x1ff 12 shl rn 5 rt
793end
794
795fn encid id ulong to ulong
796if le id 7
797ret enc3 id
798end
799if eq id 8
800ret encsi
801end
802if eq id 9
803ret encmov
804end
805if eq id 10
806ret encmz 0x52800000
807end
808if eq id 11
809ret encmz 0x72800000
810end
811if eq id 12
812ret 0xd65f03c0
813end
814if eq id 13
815ret encb
816end
817if eq id 14
818ret encbc
819end
820if eq id 15
821ret enccb 0x34000000
822end
823if eq id 16
824ret enccb 0x35000000
825end
826if eq id 17
827ret encadr
828end
829ret encid2 id
830end
831
832fn encid2 id ulong to ulong
833if eq id 18
834ret encuo true
835end
836if eq id 19
837ret encuo false
838end
839if eq id 20
840ret encbo 0x39400000
841end
842if eq id 21
843ret encbo 0x39000000
844end
845if eq id 22
846ret encpo 0xb8400400
847end
848if eq id 23
849ret encpo 0xb8000400
850end
851if eq id 24
852ret encpo 0xf8400400
853end
854ret encpo 0xf8000400
855end
856
857fn encode to int
858n = argread 2 fm 0 len fm
859if lt n 0
860n = 0
861end
862put fmn 0 ulong n
863id = formid
864c = argcnt
865if eq id 0
866oputt unk
867oputfm
868oputb 10
869oflush 2
870ret 2
871end
872if ne c needc id
873oputt unk
874oputfm
875oputb 10
876oflush 2
877ret 2
878end
879for i ulong 7
880if ge i 3
881if lt long i c
882put av i sl i
883end
884end
885end
886w = encid id
887ohex8 w
888oputb 10
889oflush 1
890ret 0
891end
892
893note argis: argument 1 equals text t.
894fn argis t view uchar * to bool
895n = argread 1 ab 0 len ab
896if ne n long len t
897ret false
898end
899for i ulong len t
900if ne get ab i get t i
901ret false
902end
903end
904ret true
905end
906
907fn main to int
908c = argcnt
909if eq c 2
910if argis tdecode
911ret decode
912end
913end
914if ge c 3
915if argis tencode
916ret encode
917end
918end
919oputt usage
920oflush 2
921ret 2
922end