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 / om / aarch64 / aarch64_om2.om

705 lines · 16025 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note OM-2 unit for AArch64 legacy encoder, decoder, and validator (om/units.sh).
3om2
4note aarch64.om: seeded Omega port of src/aarch64_encoder.c (the legacy AArch64 instruction writer)
5note and src/aarch64_decoder.c (its decoder and code-buffer validator), with the enum numbering of
6note src/aarch64_target.h and src/aarch64_decoder.h (aien-dev/omega-mv#726). Same words, same
7note decode fields, same refusals, same validator messages as the C built with the Makefile CFLAGS.
8note
9note Exported functions (omega-bridge native call ABI v1: ulong/long scalars, fixed-length
10note ulong/uchar arrays, long result). Each a64<form> returns the 32-bit instruction word the
11note matching aarch64_emit_<form> writes, or a refusal code where the C prints
12note A64_LEGACY_FIELD_OUT_OF_RANGE and aborts: -(k+1) when field k (1-based, in the C parameter order
13note after max_len) is dst of its architectural range, the first such field in the C's check order.
14note a64put b st w is the C's emit_u32_le: a refusal code passes through unwritten, else -1 when
15note st 0 + 4 > st 1 (pos, max_len), else the word goes little-endian to b at st 0, st 0 += 4, 0.
16note So aarch64_emit_<form>(buf, &pos, max_len, fields) = a64put b st a64<form> fields.
17note a64addreg sf rd rn rm a64subreg a64mulreg a64andreg a64orrreg a64eorreg (same fields)
18note a64movreg sf rd rm a64movz sf rd imm16 shift a64movk sf rd imm16 shift
19note a64ret a64b imm26 a64bcond cond imm19
20note a64cbz sf rt imm19 a64cbnz sf rt imm19 a64adr rd imm21
21note a64ldruoff sf rt rn uoff a64struoff sf rt rn uoff a64ldrbuoff rt rn uoff a64strbuoff rt rn uoff
22note a64ldrpost rt rn simm9 a64strpost a64ldrxpost a64strxpost (same fields)
23note a64subsimm sf rd rn imm12 a64subsreg sf rd rn rm
24note a64put b st w b edit uchar * (AARCH64_MAX_CODE_BYTES), st edit ulong 2
25note a64decode insn dst aarch64_decode_instruction; dst edit ulong 9 = DecodedInsn cells
26note op sf rd rn rm imm16 hw branch_imm cond; returns 0 or -1
27note a64validate code n msg ml aarch64_validate_code_buffer; code view uchar *, msg edit uchar *
28note A value outside the C parameter's type (bool sf, uint8_t register or shift, uint16_t imm16/uoff/
29note imm12, int16_t simm9, int32_t immediates and cond) traps 5: the C cannot receive it.
30
31note ---- conversions the C does by parameter type: trap 5 where the value does not fit ----
32
33fn nbool v ulong to void
34if gt v 1
35trap 5
36end
37end
38
39fn nu8 v ulong to void
40if gt v 255
41trap 5
42end
43end
44
45fn nu16 v ulong to void
46if gt v 65535
47trap 5
48end
49end
50
51fn ni16 v long to void
52if or lt v -32768 gt v 32767
53trap 5
54end
55end
56
57fn ni32 v long to void
58if or lt v -2147483648 gt v 2147483647
59trap 5
60end
61end
62
63note ---- encoder: src/aarch64_encoder.c ----
64
65note base | sf<<31 | rm<<16 | rn<<5 | rd, after CHECK3(rd, rn, rm); fields sf rd rn rm = 1 2 3 4
66fn a64rrr base ulong sf ulong rd ulong rn ulong rm ulong to long
67nbool sf
68nu8 rd
69nu8 rn
70nu8 rm
71if gt rd 31
72ret -3
73end
74if gt rn 31
75ret -4
76end
77if gt rm 31
78ret -5
79end
80w = or base shl sf 31
81w = or w shl rm 16
82w = or w shl rn 5
83ret long or w rd
84end
85
86note 0x0B000000
87export fn a64addreg sf ulong rd ulong rn ulong rm ulong to long
88ret a64rrr 184549376 sf rd rn rm
89end
90
91note 1<<30 | 0x0B000000
92export fn a64subreg sf ulong rd ulong rn ulong rm ulong to long
93ret a64rrr 1258291200 sf rd rn rm
94end
95
96note MADD Xd, Xn, Xm, XZR: 0x1B000000 | 0x1f<<10
97export fn a64mulreg sf ulong rd ulong rn ulong rm ulong to long
98ret a64rrr 453016576 sf rd rn rm
99end
100
101note 0x0A000000
102export fn a64andreg sf ulong rd ulong rn ulong rm ulong to long
103ret a64rrr 167772160 sf rd rn rm
104end
105
106note 0x2A000000
107export fn a64orrreg sf ulong rd ulong rn ulong rm ulong to long
108ret a64rrr 704643072 sf rd rn rm
109end
110
111note 0x4A000000
112export fn a64eorreg sf ulong rd ulong rn ulong rm ulong to long
113ret a64rrr 1241513984 sf rd rn rm
114end
115
116note ORR rd, XZR, rm after its own CHECK_REG(rd), CHECK_REG(rm); fields sf rd rm = 1 2 3
117export fn a64movreg sf ulong rd ulong rm ulong to long
118nbool sf
119nu8 rd
120nu8 rm
121if gt rd 31
122ret -3
123end
124if gt rm 31
125ret -4
126end
127ret a64orrreg sf rd 31 rm
128end
129
130note MOVZ/MOVK: base | sf<<31 | (shift/16 & 3)<<21 | imm16<<5 | rd; fields sf rd imm16 shift = 1 2 3 4
131fn a64hw base ulong sf ulong rd ulong imm ulong sh ulong to long
132nbool sf
133nu8 rd
134nu16 imm
135nu8 sh
136if gt rd 31
137ret -3
138end
139mx = ulong 16
140if eq sf 1
141mx = 48
142end
143if or ne rem sh 16 0 gt sh mx
144ret -5
145end
146w = or base shl sf 31
147w = or w shl and div sh 16 3 21
148w = or w shl imm 5
149ret long or w rd
150end
151
152note 0x52800000
153export fn a64movz sf ulong rd ulong imm ulong sh ulong to long
154ret a64hw 1384120320 sf rd imm sh
155end
156
157note 0x72800000
158export fn a64movk sf ulong rd ulong imm ulong sh ulong to long
159ret a64hw 1920991232 sf rd imm sh
160end
161
162note 0xD65F03C0
163export fn a64ret to long
164ret 3596551104
165end
166
167note 0x14000000 | imm26 & 0x03FFFFFF; imm26 in [-2^25, 2^25) is field 1
168export fn a64b imm long to long
169ni32 imm
170if or lt imm -33554432 ge imm 33554432
171ret -2
172end
173ret long or 335544320 and bitcast ulong imm 67108863
174end
175
176note 0x54000000 | (imm19 & 0x7FFFF)<<5 | cond; cond 0..15 field 1, imm19 field 2
177export fn a64bcond cond long imm long to long
178ni32 cond
179ni32 imm
180if or lt cond 0 gt cond 15
181ret -2
182end
183if or lt imm -262144 ge imm 262144
184ret -3
185end
186w = shl and bitcast ulong imm 524287 5
187ret long or or 1409286144 w bitcast ulong cond
188end
189
190note CBZ/CBNZ: base | sf<<31 | (imm19 & 0x7FFFF)<<5 | rt; fields sf rt imm19 = 1 2 3
191fn a64cbx base ulong sf ulong rt ulong imm long to long
192nbool sf
193nu8 rt
194ni32 imm
195if gt rt 31
196ret -3
197end
198if or lt imm -262144 ge imm 262144
199ret -4
200end
201w = or base shl sf 31
202w = or w shl and bitcast ulong imm 524287 5
203ret long or w rt
204end
205
206note 0x34000000
207export fn a64cbz sf ulong rt ulong imm long to long
208ret a64cbx 872415232 sf rt imm
209end
210
211note 0x35000000
212export fn a64cbnz sf ulong rt ulong imm long to long
213ret a64cbx 889192448 sf rt imm
214end
215
216note 0x10000000 | (imm21 & 3)<<29 | ((imm21 >> 2) & 0x7FFFF)<<5 | rd; fields rd imm21 = 1 2
217export fn a64adr rd ulong imm long to long
218nu8 rd
219ni32 imm
220if gt rd 31
221ret -2
222end
223if or lt imm -1048576 ge imm 1048576
224ret -3
225end
226x = and bitcast ulong imm 2097151
227w = or 268435456 shl and x 3 29
228w = or w shl shr x 2 5
229ret long or w rd
230end
231
232note LDR/STR (unsigned offset): (sf ? bx : bw) | (uoff >> (sf ? 3 : 2))<<10 | rn<<5 | rt;
233note CHECK_SCALED: uoff a multiple of 8 (sf) or 4 with uoff/size <= 4095; fields sf rt rn uoff = 1 2 3 4
234fn a64ls bx ulong bw ulong sf ulong rt ulong rn ulong uo ulong to long
235nbool sf
236nu8 rt
237nu8 rn
238nu16 uo
239if gt rt 31
240ret -3
241end
242if gt rn 31
243ret -4
244end
245sc = ulong 2
246sz = ulong 4
247base = bw
248if eq sf 1
249sc = 3
250sz = 8
251base = bx
252end
253if or ne rem uo sz 0 gt div uo sz 4095
254ret -5
255end
256w = or base shl and shr uo sc 4095 10
257w = or w shl rn 5
258ret long or w rt
259end
260
261note 0xF9400000 / 0xB9400000
262export fn a64ldruoff sf ulong rt ulong rn ulong uo ulong to long
263ret a64ls 4181721088 3107979264 sf rt rn uo
264end
265
266note 0xF9000000 / 0xB9000000
267export fn a64struoff sf ulong rt ulong rn ulong uo ulong to long
268ret a64ls 4177526784 3103784960 sf rt rn uo
269end
270
271note LDRB/STRB (unsigned offset): base | uoff<<10 | rn<<5 | rt; uoff 0..4095; fields rt rn uoff = 1 2 3
272fn a64lsb base ulong rt ulong rn ulong uo ulong to long
273nu8 rt
274nu8 rn
275nu16 uo
276if gt rt 31
277ret -2
278end
279if gt rn 31
280ret -3
281end
282if gt uo 4095
283ret -4
284end
285w = or base shl uo 10
286w = or w shl rn 5
287ret long or w rt
288end
289
290note 0x39400000
291export fn a64ldrbuoff rt ulong rn ulong uo ulong to long
292ret a64lsb 960495616 rt rn uo
293end
294
295note 0x39000000
296export fn a64strbuoff rt ulong rn ulong uo ulong to long
297ret a64lsb 956301312 rt rn uo
298end
299
300note post-index: base | (simm9 & 0x1FF)<<12 | rn<<5 | rt; simm9 in [-256, 255]; fields rt rn simm9 = 1 2 3
301fn a64post base ulong rt ulong rn ulong imm long to long
302nu8 rt
303nu8 rn
304ni16 imm
305if gt rt 31
306ret -2
307end
308if gt rn 31
309ret -3
310end
311if or lt imm -256 ge imm 256
312ret -4
313end
314w = or base shl and bitcast ulong imm 511 12
315w = or w shl rn 5
316ret long or w rt
317end
318
319note 0xB8400400
320export fn a64ldrpost rt ulong rn ulong imm long to long
321ret a64post 3091203072 rt rn imm
322end
323
324note 0xB8000400
325export fn a64strpost rt ulong rn ulong imm long to long
326ret a64post 3087008768 rt rn imm
327end
328
329note 0xF8400400
330export fn a64ldrxpost rt ulong rn ulong imm long to long
331ret a64post 4164944896 rt rn imm
332end
333
334note 0xF8000400
335export fn a64strxpost rt ulong rn ulong imm long to long
336ret a64post 4160750592 rt rn imm
337end
338
339note SUBS (immediate): (sf ? 0xF1000000 : 0x71000000) | imm12<<10 | rn<<5 | rd; fields sf rd rn imm12 = 1 2 3 4
340export fn a64subsimm sf ulong rd ulong rn ulong imm ulong to long
341nbool sf
342nu8 rd
343nu8 rn
344nu16 imm
345if gt rd 31
346ret -3
347end
348if gt rn 31
349ret -4
350end
351if gt imm 4095
352ret -5
353end
354base = ulong 1895825408
355if eq sf 1
356base = 4043309056
357end
358w = or base shl imm 10
359w = or w shl rn 5
360ret long or w rd
361end
362
363note SUBS (shifted register): (sf ? 0xEB000000 : 0x6B000000) | rm<<16 | rn<<5 | rd, which is
364note 0x6B000000 | sf<<31 | ...
365export fn a64subsreg sf ulong rd ulong rn ulong rm ulong to long
366ret a64rrr 1795162112 sf rd rn rm
367end
368
369note emit_u32_le over a 4096-byte buffer; st 0 = *pos, st 1 = max_len. Where the C would write past
370note a 4096-byte buffer (max_len above 4096, or pos + 4 wrapping), the first byte at index 4096 or
371note beyond traps 3; the bytes below 4096 are already written.
372export fn a64put b edit uchar * st edit ulong 2 w long to long
373if lt w 0
374ret w
375end
376x = ulong uint w
377p = get st 0
378if gt add p 4 get st 1
379ret -1
380end
381put b p uchar and x 255
382put b add p 1 uchar and shr x 8 255
383put b add p 2 uchar and shr x 16 255
384put b add p 3 uchar shr x 24
385put st 0 add p 4
386ret 0
387end
388
389note ---- decoder: src/aarch64_decoder.c ----
390
391note op; sf = bit sb of insn unless sb is 32; rd; rn when n >= 2; rm when n >= 3
392fn dregs dst edit ulong 9 op ulong insn ulong sb ulong n ulong to void
393put dst 0 op
394if lt sb 32
395put dst 1 and shr insn sb 1
396end
397put dst 2 and insn 31
398if ge n 2
399put dst 3 and shr insn 5 31
400end
401if ge n 3
402put dst 4 and shr insn 16 31
403end
404end
405
406note MOVZ/MOVK fields
407fn dwide dst edit ulong 9 op ulong insn ulong to void
408dregs dst op insn 31 1
409put dst 5 and shr insn 5 65535
410put dst 6 and shr insn 21 3
411end
412
413note the checks of aarch64_decode_instruction 1-9 (RET .. SUB); 0 matched, 1 not
414fn dec1 insn ulong dst edit ulong 9 to ulong
415if eq insn 3596551104
416put dst 0 8
417put dst 3 30
418ret 0
419end
420if eq and insn 4227858432 335544320
421put dst 0 9
422put dst 7 and insn 67108863
423ret 0
424end
425if eq and insn 4278190096 1409286144
426put dst 0 10
427put dst 8 and insn 15
428put dst 7 and shr insn 5 524287
429ret 0
430end
431m = and insn 2130706432
432if eq m 872415232
433dregs dst 11 insn 31 1
434put dst 7 and shr insn 5 524287
435ret 0
436end
437if eq m 889192448
438dregs dst 12 insn 31 1
439put dst 7 and shr insn 5 524287
440ret 0
441end
442if eq and insn 2139095040 1384120320
443dwide dst 7 insn
444ret 0
445end
446if eq and insn 2145418240 453016576
447dregs dst 3 insn 31 3
448ret 0
449end
450m = and insn 2132803584
451if eq m 167772160
452dregs dst 4 insn 31 3
453ret 0
454end
455if eq m 704643072
456dregs dst 5 insn 31 3
457ret 0
458end
459if eq m 1241513984
460dregs dst 6 insn 31 3
461ret 0
462end
463if eq m 184549376
464dregs dst 1 insn 31 3
465ret 0
466end
467if eq m 1258291200
468dregs dst 2 insn 31 3
469ret 0
470end
471ret 1
472end
473
474note the checks 10-15 (MOVK .. SUBS); 0 matched, 1 not
475fn dec2 insn ulong dst edit ulong 9 to ulong
476if eq and insn 2139095040 1920991232
477dwide dst 13 insn
478ret 0
479end
480if eq and insn 2667577344 268435456
481dregs dst 14 insn 32 1
482ret 0
483end
484m = and insn 3208642560
485if eq m 3107979264
486dregs dst 15 insn 30 2
487ret 0
488end
489if eq m 3103784960
490dregs dst 16 insn 30 2
491ret 0
492end
493m = and insn 3219129344
494if eq m 3091203072
495dregs dst 15 insn 30 2
496ret 0
497end
498if eq m 3087008768
499dregs dst 16 insn 30 2
500ret 0
501end
502m = and insn 4290772992
503if eq m 960495616
504dregs dst 17 insn 32 2
505ret 0
506end
507if eq m 956301312
508dregs dst 18 insn 32 2
509ret 0
510end
511if eq and insn 2139095040 1895825408
512dregs dst 19 insn 31 2
513ret 0
514end
515if eq and insn 2132803584 1795162112
516dregs dst 19 insn 31 3
517ret 0
518end
519ret 1
520end
521
522note aarch64_decode_instruction: dst = DecodedInsn (op sf rd rn rm imm16 hw branch_imm cond), all
523note zero first; 0, or -1 with op 0 (DECODED_INVALID). branch_imm is the raw field, not sign-extended.
524export fn a64decode insn ulong dst edit ulong 9 to long
525nu32 insn
526for i ulong 9
527put dst i 0
528end
529if eq dec1 insn dst 0
530ret 0
531end
532if eq dec2 insn dst 0
533ret 0
534end
535put dst 0 0
536ret -1
537end
538
539fn nu32 v ulong to void
540if gt v 4294967295
541trap 5
542end
543end
544
545note ---- validator: aarch64_validate_code_buffer and its snprintf messages ----
546
547note append the c low bytes of v (little-endian) to t at n; the new length
548fn app t edit uchar 128 n ulong v ulong c ulong to ulong
549for i ulong c
550put t add n i uchar and shr v mul i 8 255
551end
552ret add n c
553end
554
555fn hexch v ulong to ulong
556if lt v 10
557ret add v 48
558end
559ret add v 87
560end
561
562note %x of v with at least w digits (w >= 1): %08x is w 8, %zx is w 1
563fn apphex t edit uchar 128 n ulong v ulong w ulong to ulong
564k = ulong 1
565while lt k 16 bound 16
566if eq shr v mul k 4 0
567break
568end
569k = add k 1
570end
571if lt k w
572k = w
573end
574for i ulong k
575put t add n i uchar hexch and shr v mul sub sub k 1 i 4 15
576end
577ret add n k
578end
579
580note %zu / %d (nonnegative) of v
581fn appdec t edit uchar 128 n ulong v ulong to ulong
582k = ulong 1
583q = div v 10
584while gt q 0 bound 20
585k = add k 1
586q = div q 10
587end
588q = v
589for i ulong k
590put t add n sub sub k 1 i uchar add 48 rem q 10
591q = div q 10
592end
593ret add n k
594end
595
596note snprintf(msg, ml, ...) of the n bytes in t: at most ml - 1 bytes and a NUL, nothing when ml is 0
597fn snp msg edit uchar * ml ulong t view uchar 128 n ulong to void
598if eq ml 0
599ret
600end
601m = n
602if gt m sub ml 1
603m = sub ml 1
604end
605for i ulong m
606put msg i get t i
607end
608put msg m 0
609end
610
611note "Empty or null code buffer"
612fn msgnull t edit uchar 128 to ulong
613n = app t 0 8245845148755324229 8
614n = app t n 8026294610952547872 8
615n = app t n 7306640099798705508 8
616ret app t n 114 1
617end
618
619note "Code buffer length %zu is not a multiple of 4 bytes"
620fn msglen t edit uchar 128 v ulong to ulong
621n = app t 0 7382915055423352643 8
622n = app t n 7453005948419990886 8
623n = app t n 2123892 3
624n = appdec t n v
625n = app t n 2338616625293650208 8
626n = app t n 8100133413977923681 8
627n = app t n 2320515332325795180 8
628ret app t n 495623371106 5
629end
630
631note "Undefined/unrecognized AArch64 opcode 0x%08x at offset 0x%zx"
632fn msgop t edit uchar 128 w ulong o ulong to ulong
633n = app t 0 7308895133777555029 8
634n = app t n 8026370503058206564 8
635n = app t n 4692861198728261223 8
636n = app t n 8007457545748968001 8
637n = app t n 8660457703282271088 8
638n = apphex t n w 8
639n = app t n 8315446389458100512 8
640n = app t n 516203508837 5
641ret apphex t n o 1
642end
643
644note "Function does not terminate with RET (terminates with op %d)"
645fn msgret t edit uchar 128 op ulong to ulong
646n = app t 0 7957695015192261958 8
647n = app t n 8029390865643299872 8
648n = app t n 7956010554822500468 8
649n = app t n 7526756837608485985 8
650n = app t n 7310512214447051296 8
651n = app t n 8315180248822541682 8
652n = app t n 8101729887664568096 8
653n = app t n 32 1
654n = appdec t n op
655ret app t n 41 1
656end
657
658fn ld32 a view uchar * o ulong to ulong
659x = ulong get a add o 3
660x = or shl x 8 ulong get a add o 2
661x = or shl x 8 ulong get a add o 1
662ret or shl x 8 ulong get a o
663end
664
665note the instruction loop of the validator over c words: 0 when valid, else the message length in t
666fn valscan code view uchar * c ulong t edit uchar 128 d edit ulong 9 to ulong
667for i ulong c
668o = mul i 4
669w = ld32 code o
670if or ne a64decode w d 0 eq get d 0 0
671ret msgop t w o
672end
673if and eq i sub c 1 ne get d 0 8
674ret msgret t get d 0
675end
676end
677ret 0
678end
679
680note aarch64_validate_code_buffer(code, n, msg, ml): 0, or -1 with the C's message in msg.
681note ml above 256 (more capacity than msg has) traps 3 before anything is read or written.
682export fn a64validate code view uchar * n ulong msg edit uchar * ml ulong to long
683if gt ml len msg
684trap 3
685end
686t = new uchar 128 0
687d = new ulong 9 0
688if eq n 0
689k = msgnull t
690snp msg ml t k
691ret -1
692end
693if ne rem n 4 0
694k = msglen t n
695snp msg ml t k
696ret -1
697end
698k = valscan code div n 4 t d
699if gt k 0
700snp msg ml t k
701ret -1
702end
703ret 0
704end
705