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.om

1215 lines · 23902 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aarch64.om: seeded Omega port of src/aarch64_encoder.c (the legacy AArch64 instruction writer)
3note and src/aarch64_decoder.c (its decoder and code-buffer validator), with the enum numbering of
4note src/aarch64_target.h and src/aarch64_decoder.h (aien-dev/omega-mv#726). Same words, same
5note decode fields, same refusals, same validator messages as the C built with the Makefile CFLAGS.
6note
7note Exported functions (omega-bridge native call ABI v1: ulong/long scalars, fixed-length
8note ulong/uchar arrays, long result). Each a64<form> returns the 32-bit instruction word the
9note matching aarch64_emit_<form> writes, or a refusal code where the C prints
10note A64_LEGACY_FIELD_OUT_OF_RANGE and aborts: -(k+1) when field k (1-based, in the C parameter order
11note after max_len) is out of its architectural range, the first such field in the C's check order.
12note a64put b st w is the C's emit_u32_le: a refusal code passes through unwritten, else -1 when
13note st 0 + 4 > st 1 (pos, max_len), else the word goes little-endian to b at st 0, st 0 += 4, 0.
14note So aarch64_emit_<form>(buf, &pos, max_len, fields) = a64put b st a64<form> fields.
15note a64addreg sf rd rn rm a64subreg a64mulreg a64andreg a64orrreg a64eorreg (same fields)
16note a64movreg sf rd rm a64movz sf rd imm16 shift a64movk sf rd imm16 shift
17note a64ret a64b imm26 a64bcond cond imm19
18note a64cbz sf rt imm19 a64cbnz sf rt imm19 a64adr rd imm21
19note a64ldruoff sf rt rn uoff a64struoff sf rt rn uoff a64ldrbuoff rt rn uoff a64strbuoff rt rn uoff
20note a64ldrpost rt rn simm9 a64strpost a64ldrxpost a64strxpost (same fields)
21note a64subsimm sf rd rn imm12 a64subsreg sf rd rn rm
22note a64put b st w b edit uchar 4096 (AARCH64_MAX_CODE_BYTES), st edit ulong 2
23note a64decode insn out aarch64_decode_instruction; out edit ulong 9 = DecodedInsn cells
24note op sf rd rn rm imm16 hw branch_imm cond; returns 0 or -1
25note a64validate code n msg ml aarch64_validate_code_buffer; code view uchar 4096, msg edit uchar 256
26note A value outside the C parameter's type (bool sf, uint8_t register or shift, uint16_t imm16/uoff/
27note imm12, int16_t simm9, int32_t immediates and cond) traps 5: the C cannot receive it.
28
29note ---- conversions the C does by parameter type: trap 5 where the value does not fit ----
30
31fn nbool v ulong to void
32if gt v 1
33trap 5
34end
35end
36
37fn nu8 v ulong to void
38if gt v 255
39trap 5
40end
41end
42
43fn nu16 v ulong to void
44if gt v 65535
45trap 5
46end
47end
48
49fn ni16 v long to void
50if or lt v -32768 gt v 32767
51trap 5
52end
53end
54
55fn ni32 v long to void
56if or lt v -2147483648 gt v 2147483647
57trap 5
58end
59end
60
61note ---- encoder: src/aarch64_encoder.c ----
62
63note base | sf<<31 | rm<<16 | rn<<5 | rd, after CHECK3(rd, rn, rm); fields sf rd rn rm = 1 2 3 4
64fn a64rrr base ulong sf ulong rd ulong rn ulong rm ulong to long
65nbool sf
66nu8 rd
67nu8 rn
68nu8 rm
69if gt rd 31
70ret -3
71end
72if gt rn 31
73ret -4
74end
75if gt rm 31
76ret -5
77end
78w = or base shl sf 31
79w = or w shl rm 16
80w = or w shl rn 5
81ret long or w rd
82end
83
84note 0x0B000000
85fn a64addreg sf ulong rd ulong rn ulong rm ulong to long
86ret a64rrr 184549376 sf rd rn rm
87end
88
89note 1<<30 | 0x0B000000
90fn a64subreg sf ulong rd ulong rn ulong rm ulong to long
91ret a64rrr 1258291200 sf rd rn rm
92end
93
94note MADD Xd, Xn, Xm, XZR: 0x1B000000 | 0x1f<<10
95fn a64mulreg sf ulong rd ulong rn ulong rm ulong to long
96ret a64rrr 453016576 sf rd rn rm
97end
98
99note 0x0A000000
100fn a64andreg sf ulong rd ulong rn ulong rm ulong to long
101ret a64rrr 167772160 sf rd rn rm
102end
103
104note 0x2A000000
105fn a64orrreg sf ulong rd ulong rn ulong rm ulong to long
106ret a64rrr 704643072 sf rd rn rm
107end
108
109note 0x4A000000
110fn a64eorreg sf ulong rd ulong rn ulong rm ulong to long
111ret a64rrr 1241513984 sf rd rn rm
112end
113
114note ORR rd, XZR, rm after its own CHECK_REG(rd), CHECK_REG(rm); fields sf rd rm = 1 2 3
115fn a64movreg sf ulong rd ulong rm ulong to long
116nbool sf
117nu8 rd
118nu8 rm
119if gt rd 31
120ret -3
121end
122if gt rm 31
123ret -4
124end
125ret a64orrreg sf rd 31 rm
126end
127
128note MOVZ/MOVK: base | sf<<31 | (shift/16 & 3)<<21 | imm16<<5 | rd; fields sf rd imm16 shift = 1 2 3 4
129fn a64hw base ulong sf ulong rd ulong imm ulong sh ulong to long
130nbool sf
131nu8 rd
132nu16 imm
133nu8 sh
134if gt rd 31
135ret -3
136end
137mx = ulong 16
138if eq sf 1
139mx = 48
140end
141if or ne rem sh 16 0 gt sh mx
142ret -5
143end
144w = or base shl sf 31
145w = or w shl and div sh 16 3 21
146w = or w shl imm 5
147ret long or w rd
148end
149
150note 0x52800000
151fn a64movz sf ulong rd ulong imm ulong sh ulong to long
152ret a64hw 1384120320 sf rd imm sh
153end
154
155note 0x72800000
156fn a64movk sf ulong rd ulong imm ulong sh ulong to long
157ret a64hw 1920991232 sf rd imm sh
158end
159
160note 0xD65F03C0
161fn a64ret to long
162ret 3596551104
163end
164
165note 0x14000000 | imm26 & 0x03FFFFFF; imm26 in [-2^25, 2^25) is field 1
166fn a64b imm long to long
167ni32 imm
168if or lt imm -33554432 ge imm 33554432
169ret -2
170end
171ret long or 335544320 and bitcast ulong imm 67108863
172end
173
174note 0x54000000 | (imm19 & 0x7FFFF)<<5 | cond; cond 0..15 field 1, imm19 field 2
175fn a64bcond cond long imm long to long
176ni32 cond
177ni32 imm
178if or lt cond 0 gt cond 15
179ret -2
180end
181if or lt imm -262144 ge imm 262144
182ret -3
183end
184w = shl and bitcast ulong imm 524287 5
185ret long or or 1409286144 w bitcast ulong cond
186end
187
188note CBZ/CBNZ: base | sf<<31 | (imm19 & 0x7FFFF)<<5 | rt; fields sf rt imm19 = 1 2 3
189fn a64cbx base ulong sf ulong rt ulong imm long to long
190nbool sf
191nu8 rt
192ni32 imm
193if gt rt 31
194ret -3
195end
196if or lt imm -262144 ge imm 262144
197ret -4
198end
199w = or base shl sf 31
200w = or w shl and bitcast ulong imm 524287 5
201ret long or w rt
202end
203
204note 0x34000000
205fn a64cbz sf ulong rt ulong imm long to long
206ret a64cbx 872415232 sf rt imm
207end
208
209note 0x35000000
210fn a64cbnz sf ulong rt ulong imm long to long
211ret a64cbx 889192448 sf rt imm
212end
213
214note 0x10000000 | (imm21 & 3)<<29 | ((imm21 >> 2) & 0x7FFFF)<<5 | rd; fields rd imm21 = 1 2
215fn a64adr rd ulong imm long to long
216nu8 rd
217ni32 imm
218if gt rd 31
219ret -2
220end
221if or lt imm -1048576 ge imm 1048576
222ret -3
223end
224x = and bitcast ulong imm 2097151
225w = or 268435456 shl and x 3 29
226w = or w shl shr x 2 5
227ret long or w rd
228end
229
230note LDR/STR (unsigned offset): (sf ? bx : bw) | (uoff >> (sf ? 3 : 2))<<10 | rn<<5 | rt;
231note CHECK_SCALED: uoff a multiple of 8 (sf) or 4 with uoff/size <= 4095; fields sf rt rn uoff = 1 2 3 4
232fn a64ls bx ulong bw ulong sf ulong rt ulong rn ulong uo ulong to long
233nbool sf
234nu8 rt
235nu8 rn
236nu16 uo
237if gt rt 31
238ret -3
239end
240if gt rn 31
241ret -4
242end
243sc = ulong 2
244sz = ulong 4
245base = bw
246if eq sf 1
247sc = 3
248sz = 8
249base = bx
250end
251if or ne rem uo sz 0 gt div uo sz 4095
252ret -5
253end
254w = or base shl and shr uo sc 4095 10
255w = or w shl rn 5
256ret long or w rt
257end
258
259note 0xF9400000 / 0xB9400000
260fn a64ldruoff sf ulong rt ulong rn ulong uo ulong to long
261ret a64ls 4181721088 3107979264 sf rt rn uo
262end
263
264note 0xF9000000 / 0xB9000000
265fn a64struoff sf ulong rt ulong rn ulong uo ulong to long
266ret a64ls 4177526784 3103784960 sf rt rn uo
267end
268
269note LDRB/STRB (unsigned offset): base | uoff<<10 | rn<<5 | rt; uoff 0..4095; fields rt rn uoff = 1 2 3
270fn a64lsb base ulong rt ulong rn ulong uo ulong to long
271nu8 rt
272nu8 rn
273nu16 uo
274if gt rt 31
275ret -2
276end
277if gt rn 31
278ret -3
279end
280if gt uo 4095
281ret -4
282end
283w = or base shl uo 10
284w = or w shl rn 5
285ret long or w rt
286end
287
288note 0x39400000
289fn a64ldrbuoff rt ulong rn ulong uo ulong to long
290ret a64lsb 960495616 rt rn uo
291end
292
293note 0x39000000
294fn a64strbuoff rt ulong rn ulong uo ulong to long
295ret a64lsb 956301312 rt rn uo
296end
297
298note post-index: base | (simm9 & 0x1FF)<<12 | rn<<5 | rt; simm9 in [-256, 255]; fields rt rn simm9 = 1 2 3
299fn a64post base ulong rt ulong rn ulong imm long to long
300nu8 rt
301nu8 rn
302ni16 imm
303if gt rt 31
304ret -2
305end
306if gt rn 31
307ret -3
308end
309if or lt imm -256 ge imm 256
310ret -4
311end
312w = or base shl and bitcast ulong imm 511 12
313w = or w shl rn 5
314ret long or w rt
315end
316
317note 0xB8400400
318fn a64ldrpost rt ulong rn ulong imm long to long
319ret a64post 3091203072 rt rn imm
320end
321
322note 0xB8000400
323fn a64strpost rt ulong rn ulong imm long to long
324ret a64post 3087008768 rt rn imm
325end
326
327note 0xF8400400
328fn a64ldrxpost rt ulong rn ulong imm long to long
329ret a64post 4164944896 rt rn imm
330end
331
332note 0xF8000400
333fn a64strxpost rt ulong rn ulong imm long to long
334ret a64post 4160750592 rt rn imm
335end
336
337note SUBS (immediate): (sf ? 0xF1000000 : 0x71000000) | imm12<<10 | rn<<5 | rd; fields sf rd rn imm12 = 1 2 3 4
338fn a64subsimm sf ulong rd ulong rn ulong imm ulong to long
339nbool sf
340nu8 rd
341nu8 rn
342nu16 imm
343if gt rd 31
344ret -3
345end
346if gt rn 31
347ret -4
348end
349if gt imm 4095
350ret -5
351end
352base = ulong 1895825408
353if eq sf 1
354base = 4043309056
355end
356w = or base shl imm 10
357w = or w shl rn 5
358ret long or w rd
359end
360
361note SUBS (shifted register): (sf ? 0xEB000000 : 0x6B000000) | rm<<16 | rn<<5 | rd, which is
362note 0x6B000000 | sf<<31 | ...
363fn a64subsreg sf ulong rd ulong rn ulong rm ulong to long
364ret a64rrr 1795162112 sf rd rn rm
365end
366
367note emit_u32_le over a 4096-byte buffer; st 0 = *pos, st 1 = max_len. Where the C would write past
368note a 4096-byte buffer (max_len above 4096, or pos + 4 wrapping), the first byte at index 4096 or
369note beyond traps 3; the bytes below 4096 are already written.
370fn a64put b edit uchar 4096 st edit ulong 2 w long to long
371if lt w 0
372ret w
373end
374x = ulong uint w
375p = get st 0
376if gt add p 4 get st 1
377ret -1
378end
379put b p uchar and x 255
380put b add p 1 uchar and shr x 8 255
381put b add p 2 uchar and shr x 16 255
382put b add p 3 uchar shr x 24
383put st 0 add p 4
384ret 0
385end
386
387note ---- decoder: src/aarch64_decoder.c ----
388
389note op; sf = bit sb of insn unless sb is 32; rd; rn when n >= 2; rm when n >= 3
390fn dregs out edit ulong 9 op ulong insn ulong sb ulong n ulong to void
391put out 0 op
392if lt sb 32
393put out 1 and shr insn sb 1
394end
395put out 2 and insn 31
396if ge n 2
397put out 3 and shr insn 5 31
398end
399if ge n 3
400put out 4 and shr insn 16 31
401end
402end
403
404note MOVZ/MOVK fields
405fn dwide out edit ulong 9 op ulong insn ulong to void
406dregs out op insn 31 1
407put out 5 and shr insn 5 65535
408put out 6 and shr insn 21 3
409end
410
411note the checks of aarch64_decode_instruction 1-9 (RET .. SUB); 0 matched, 1 not
412fn dec1 insn ulong out edit ulong 9 to ulong
413if eq insn 3596551104
414put out 0 8
415put out 3 30
416ret 0
417end
418if eq and insn 4227858432 335544320
419put out 0 9
420put out 7 and insn 67108863
421ret 0
422end
423if eq and insn 4278190096 1409286144
424put out 0 10
425put out 8 and insn 15
426put out 7 and shr insn 5 524287
427ret 0
428end
429m = and insn 2130706432
430if eq m 872415232
431dregs out 11 insn 31 1
432put out 7 and shr insn 5 524287
433ret 0
434end
435if eq m 889192448
436dregs out 12 insn 31 1
437put out 7 and shr insn 5 524287
438ret 0
439end
440if eq and insn 2139095040 1384120320
441dwide out 7 insn
442ret 0
443end
444if eq and insn 2145418240 453016576
445dregs out 3 insn 31 3
446ret 0
447end
448m = and insn 2132803584
449if eq m 167772160
450dregs out 4 insn 31 3
451ret 0
452end
453if eq m 704643072
454dregs out 5 insn 31 3
455ret 0
456end
457if eq m 1241513984
458dregs out 6 insn 31 3
459ret 0
460end
461if eq m 184549376
462dregs out 1 insn 31 3
463ret 0
464end
465if eq m 1258291200
466dregs out 2 insn 31 3
467ret 0
468end
469ret 1
470end
471
472note the checks 10-15 (MOVK .. SUBS); 0 matched, 1 not
473fn dec2 insn ulong out edit ulong 9 to ulong
474if eq and insn 2139095040 1920991232
475dwide out 13 insn
476ret 0
477end
478if eq and insn 2667577344 268435456
479dregs out 14 insn 32 1
480ret 0
481end
482m = and insn 3208642560
483if eq m 3107979264
484dregs out 15 insn 30 2
485ret 0
486end
487if eq m 3103784960
488dregs out 16 insn 30 2
489ret 0
490end
491m = and insn 3219129344
492if eq m 3091203072
493dregs out 15 insn 30 2
494ret 0
495end
496if eq m 3087008768
497dregs out 16 insn 30 2
498ret 0
499end
500m = and insn 4290772992
501if eq m 960495616
502dregs out 17 insn 32 2
503ret 0
504end
505if eq m 956301312
506dregs out 18 insn 32 2
507ret 0
508end
509if eq and insn 2139095040 1895825408
510dregs out 19 insn 31 2
511ret 0
512end
513if eq and insn 2132803584 1795162112
514dregs out 19 insn 31 3
515ret 0
516end
517ret 1
518end
519
520note aarch64_decode_instruction: out = DecodedInsn (op sf rd rn rm imm16 hw branch_imm cond), all
521note zero first; 0, or -1 with op 0 (DECODED_INVALID). branch_imm is the raw field, not sign-extended.
522fn a64decode insn ulong out edit ulong 9 to long
523nu32 insn
524for i ulong 9
525put out i 0
526end
527if eq dec1 insn out 0
528ret 0
529end
530if eq dec2 insn out 0
531ret 0
532end
533put out 0 0
534ret -1
535end
536
537fn nu32 v ulong to void
538if gt v 4294967295
539trap 5
540end
541end
542
543note ---- validator: aarch64_validate_code_buffer and its snprintf messages ----
544
545note append the c low bytes of v (little-endian) to t at n; the new length
546fn app t edit uchar 128 n ulong v ulong c ulong to ulong
547for i ulong c
548put t add n i uchar and shr v mul i 8 255
549end
550ret add n c
551end
552
553fn hexch v ulong to ulong
554if lt v 10
555ret add v 48
556end
557ret add v 87
558end
559
560note %x of v with at least w digits (w >= 1): %08x is w 8, %zx is w 1
561fn apphex t edit uchar 128 n ulong v ulong w ulong to ulong
562k = ulong 1
563while lt k 16 bound 16
564if eq shr v mul k 4 0
565break
566end
567k = add k 1
568end
569if lt k w
570k = w
571end
572for i ulong k
573put t add n i uchar hexch and shr v mul sub sub k 1 i 4 15
574end
575ret add n k
576end
577
578note %zu / %d (nonnegative) of v
579fn appdec t edit uchar 128 n ulong v ulong to ulong
580k = ulong 1
581q = div v 10
582while gt q 0 bound 20
583k = add k 1
584q = div q 10
585end
586q = v
587for i ulong k
588put t add n sub sub k 1 i uchar add 48 rem q 10
589q = div q 10
590end
591ret add n k
592end
593
594note snprintf(msg, ml, ...) of the n bytes in t: at most ml - 1 bytes and a NUL, nothing when ml is 0
595fn snp msg edit uchar 256 ml ulong t view uchar 128 n ulong to void
596if eq ml 0
597ret
598end
599m = n
600if gt m sub ml 1
601m = sub ml 1
602end
603for i ulong m
604put msg i get t i
605end
606put msg m 0
607end
608
609note "Empty or null code buffer"
610fn msgnull t edit uchar 128 to ulong
611n = app t 0 8245845148755324229 8
612n = app t n 8026294610952547872 8
613n = app t n 7306640099798705508 8
614ret app t n 114 1
615end
616
617note "Code buffer length %zu is not a multiple of 4 bytes"
618fn msglen t edit uchar 128 v ulong to ulong
619n = app t 0 7382915055423352643 8
620n = app t n 7453005948419990886 8
621n = app t n 2123892 3
622n = appdec t n v
623n = app t n 2338616625293650208 8
624n = app t n 8100133413977923681 8
625n = app t n 2320515332325795180 8
626ret app t n 495623371106 5
627end
628
629note "Undefined/unrecognized AArch64 opcode 0x%08x at offset 0x%zx"
630fn msgop t edit uchar 128 w ulong o ulong to ulong
631n = app t 0 7308895133777555029 8
632n = app t n 8026370503058206564 8
633n = app t n 4692861198728261223 8
634n = app t n 8007457545748968001 8
635n = app t n 8660457703282271088 8
636n = apphex t n w 8
637n = app t n 8315446389458100512 8
638n = app t n 516203508837 5
639ret apphex t n o 1
640end
641
642note "Function does not terminate with RET (terminates with op %d)"
643fn msgret t edit uchar 128 op ulong to ulong
644n = app t 0 7957695015192261958 8
645n = app t n 8029390865643299872 8
646n = app t n 7956010554822500468 8
647n = app t n 7526756837608485985 8
648n = app t n 7310512214447051296 8
649n = app t n 8315180248822541682 8
650n = app t n 8101729887664568096 8
651n = app t n 32 1
652n = appdec t n op
653ret app t n 41 1
654end
655
656fn ld32 a view uchar 4096 o ulong to ulong
657x = ulong get a add o 3
658x = or shl x 8 ulong get a add o 2
659x = or shl x 8 ulong get a add o 1
660ret or shl x 8 ulong get a o
661end
662
663note the instruction loop of the validator over c words: 0 when valid, else the message length in t
664fn valscan code view uchar 4096 c ulong t edit uchar 128 d edit ulong 9 to ulong
665for i ulong c
666o = mul i 4
667w = ld32 code o
668if or ne a64decode w d 0 eq get d 0 0
669ret msgop t w o
670end
671if and eq i sub c 1 ne get d 0 8
672ret msgret t get d 0
673end
674end
675ret 0
676end
677
678note aarch64_validate_code_buffer(code, n, msg, ml): 0, or -1 with the C's message in msg.
679note ml above 256 (more capacity than msg has) traps 3 before anything is read or written.
680fn a64validate code view uchar 4096 n ulong msg edit uchar 256 ml ulong to long
681if gt ml 256
682trap 3
683end
684t = new uchar 128 0
685d = new ulong 9 0
686if eq n 0
687k = msgnull t
688snp msg ml t k
689ret -1
690end
691if ne rem n 4 0
692k = msglen t n
693snp msg ml t k
694ret -1
695end
696k = valscan code div n 4 t d
697if gt k 0
698snp msg ml t k
699ret -1
700end
701ret 0
702end
703
704note ==== parity harness: program mode only; everything above this line is the library ====
705note Reads corpus records on stdin (om/aarch64/parity/corpus.py), writes one result line per record:
706note E form f1 f2 f3 f4 one emitter call (aarch64_emit_<form> into a fresh 4096-byte buffer), then
707note the decode of the written word and the round-trip mark (see README)
708note P maxlen pos n calls a sequence of emitter calls into one buffer (buffer-full path)
709note D word aarch64_decode_instruction
710note V len ml bytes aarch64_validate_code_buffer
711note Numbers are lowercase hex without leading zeros (words: 8 digits), refusal values signed decimal.
712
713text enm "add_reg sub_reg mul_reg and_reg orr_reg eor_reg mov_reg movz ret b b_cond cbz cbnz movk adr ldr_uoff str_uoff ldrb_uoff strb_uoff ldr_post str_post ldr_x_post str_x_post subs_imm subs_reg "
714text fnm "sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rm |sf rd imm16 shift |- |imm26 |cond imm19 |sf rt imm19 |sf rt imm19 |sf rd imm16 shift |rd imm21 |sf rt rn uoff |sf rt rn uoff |rt rn uoff |rt rn uoff |rt rn simm9 |rt rn simm9 |rt rn simm9 |rt rn simm9 |sf rd rn imm12 |sf rd rn rm |"
715text tref "A64_LEGACY_FIELD_OUT_OF_RANGE aarch64_emit_"
716
717global ib uchar 1048576
718global ist ulong 2
719global ob uchar 1048576
720global obn ulong 1
721global fv ulong 4
722
723fn inb to ulong
724p = get ist 0
725if ge p get ist 1
726r = read 0 ib 0 1048576
727if le r 0
728ret 256
729end
730put ist 1 ulong r
731p = 0
732end
733put ist 0 add p 1
734ret ulong get ib p
735end
736
737fn obflush to void
738n = get obn 0
739if gt n 0
740r = write 1 ob 0 n
741end
742put obn 0 0
743end
744
745fn ob1 c ulong to void
746n = get obn 0
747if ge n 1048576
748obflush
749n = 0
750end
751put ob n uchar c
752put obn 0 add n 1
753end
754
755fn ohx v ulong to void
756ob1 32
757k = ulong 1
758while lt k 16 bound 16
759if eq shr v mul k 4 0
760break
761end
762k = add k 1
763end
764for i ulong k
765ob1 hexch and shr v mul sub sub k 1 i 4 15
766end
767end
768
769fn ohx8 v ulong to void
770ob1 32
771for i ulong 8
772ob1 hexch and shr v mul sub 7 i 4 15
773end
774end
775
776fn ohxb b ulong to void
777ob1 hexch shr b 4
778ob1 hexch and b 15
779end
780
781fn odec v long to void
782if lt v 0
783ob1 45
784end
785u = ulong 0
786if lt v 0
787u = add not bitcast ulong v 1
788else
789u = ulong v
790end
791t = new uchar 24 0
792k = ulong 0
793while true bound 21
794put t k uchar add 48 rem u 10
795k = add k 1
796u = div u 10
797if eq u 0
798break
799end
800end
801for i ulong k
802ob1 ulong get t sub sub k 1 i
803end
804end
805
806fn hexval c ulong to ulong
807if and ge c 48 le c 57
808ret sub c 48
809end
810if and ge c 97 le c 102
811ret sub c 87
812end
813ret 16
814end
815
816fn rdcode to ulong
817c = inb
818while or eq c 10 eq c 32 bound 999999
819c = inb
820end
821ret c
822end
823
824fn rdhex to ulong
825c = inb
826while eq c 32 bound 999999
827c = inb
828end
829v = ulong 0
830while lt hexval c 16 bound 17
831v = or shl v 4 hexval c
832c = inb
833end
834ret v
835end
836
837fn sgnl v ulong to long
838if ge v 9223372036854775808
839ret neg long add not v 1
840end
841ret long v
842end
843
844note the word of emitter form f (1..25, the order of aarch64_encoder.h) on the fields in fv
845fn enca f ulong to long
846a = get fv 0
847b = get fv 1
848c = get fv 2
849d = get fv 3
850if eq f 1
851ret a64addreg a b c d
852end
853if eq f 2
854ret a64subreg a b c d
855end
856if eq f 3
857ret a64mulreg a b c d
858end
859if eq f 4
860ret a64andreg a b c d
861end
862if eq f 5
863ret a64orrreg a b c d
864end
865if eq f 6
866ret a64eorreg a b c d
867end
868if eq f 7
869ret a64movreg a b c
870end
871if eq f 8
872ret a64movz a b c d
873end
874if eq f 9
875ret a64ret
876end
877if eq f 10
878ret a64b sgnl a
879end
880if eq f 11
881ret a64bcond sgnl a sgnl b
882end
883if eq f 12
884ret a64cbz a b sgnl c
885end
886ret encb f
887end
888
889fn encb f ulong to long
890a = get fv 0
891b = get fv 1
892c = get fv 2
893d = get fv 3
894if eq f 13
895ret a64cbnz a b sgnl c
896end
897if eq f 14
898ret a64movk a b c d
899end
900if eq f 15
901ret a64adr a sgnl b
902end
903if eq f 16
904ret a64ldruoff a b c d
905end
906if eq f 17
907ret a64struoff a b c d
908end
909if eq f 18
910ret a64ldrbuoff a b c
911end
912if eq f 19
913ret a64strbuoff a b c
914end
915if eq f 20
916ret a64ldrpost a b sgnl c
917end
918if eq f 21
919ret a64strpost a b sgnl c
920end
921if eq f 22
922ret a64ldrxpost a b sgnl c
923end
924if eq f 23
925ret a64strxpost a b sgnl c
926end
927if eq f 24
928ret a64subsimm a b c d
929end
930if eq f 25
931ret a64subsreg a b c d
932end
933trap 9
934end
935
936note print the k-th (from 1) space-ended word of the f-th |-ended entry of fnm
937fn ofield f ulong k ulong to void
938e = ulong 1
939w = ulong 1
940for i ulong len fnm
941c = ulong get fnm i
942if eq c 124
943e = add e 1
944w = 1
945continue
946end
947if eq c 32
948w = add w 1
949continue
950end
951if and eq e f eq w k
952ob1 c
953end
954end
955end
956
957fn oname f ulong to void
958e = ulong 1
959for i ulong len enm
960c = ulong get enm i
961if eq c 32
962e = add e 1
963continue
964end
965if eq e f
966ob1 c
967end
968end
969end
970
971note the C's stderr line for a refusal code r of form f
972fn orefuse f ulong r long to void
973for i ulong len tref
974ob1 ulong get tref i
975end
976oname f
977ob1 32
978k = ulong sub neg r 1
979ofield f k
980ob1 61
981odec sgnl get fv sub k 1
982end
983
984fn sx v ulong bits ulong to long
985h = shl ulong 1 sub bits 1
986if ge v h
987ret sub long v long shl h 1
988end
989ret long v
990end
991
992note re-encode a decoded word whose op carries every field (-100 for the others)
993fn reenc d edit ulong 9 to long
994op = get d 0
995if and ge op 1 le op 6
996ret enc4 op get d 1 get d 2 get d 3 get d 4
997end
998if eq op 7
999ret a64movz get d 1 get d 2 get d 5 mul get d 6 16
1000end
1001if eq op 13
1002ret a64movk get d 1 get d 2 get d 5 mul get d 6 16
1003end
1004if eq op 8
1005ret a64ret
1006end
1007if eq op 9
1008ret a64b sx get d 7 26
1009end
1010if eq op 10
1011ret a64bcond long get d 8 sx get d 7 19
1012end
1013if eq op 11
1014ret a64cbz get d 1 get d 2 sx get d 7 19
1015end
1016if eq op 12
1017ret a64cbnz get d 1 get d 2 sx get d 7 19
1018end
1019ret -100
1020end
1021
1022fn enc4 op ulong a ulong b ulong c ulong d ulong to long
1023if eq op 1
1024ret a64addreg a b c d
1025end
1026if eq op 2
1027ret a64subreg a b c d
1028end
1029if eq op 3
1030ret a64mulreg a b c d
1031end
1032if eq op 4
1033ret a64andreg a b c d
1034end
1035if eq op 5
1036ret a64orrreg a b c d
1037end
1038ret a64eorreg a b c d
1039end
1040
1041note for a form whose decode drops fields: does the decode show the expected op and the input fields?
1042fn lossok f ulong d edit ulong 9 to bool
1043op = get d 0
1044if eq f 15
1045ret and eq op 14 eq get d 2 get fv 0
1046end
1047if and ge f 20 le f 23
1048ret lospost f d
1049end
1050if and ge f 18 le f 19
1051ok = and eq op sub f 1 eq get d 2 get fv 0
1052ret and ok eq get d 3 get fv 1
1053end
1054if or lt f 16 gt f 25
1055ret false
1056end
1057note forms 16 17 24 25: op, sf, rd, rn; and rm (0 for subs_imm) for 24 25
1058eop = sub f 1
1059if ge f 24
1060eop = 19
1061end
1062ok = and eq op eop eq get d 1 get fv 0
1063ok = and ok eq get d 2 get fv 1
1064ok = and ok eq get d 3 get fv 2
1065if eq f 24
1066ok = and ok eq get d 4 0
1067end
1068if eq f 25
1069ok = and ok eq get d 4 get fv 3
1070end
1071ret ok
1072end
1073
1074fn lospost f ulong d edit ulong 9 to bool
1075eop = ulong 15
1076if or eq f 21 eq f 23
1077eop = 16
1078end
1079esf = ulong 0
1080if ge f 22
1081esf = 1
1082end
1083ok = and eq get d 0 eop eq get d 1 esf
1084ok = and ok eq get d 2 get fv 0
1085ret and ok eq get d 3 get fv 1
1086end
1087
1088note E form f1 f2 f3 f4
1089fn doenc to void
1090b = new uchar 4096 170
1091st = new ulong 2 0
1092d = new ulong 9 0
1093f = rdhex
1094for i ulong 4
1095put fv i rdhex
1096end
1097put st 1 4096
1098w = enca f
1099if lt w 0
1100orefuse f w
1101ret
1102end
1103r = a64put b st w
1104x = ld32 b 0
1105ob1 69
1106ohx8 x
1107ohx bitcast ulong a64decode x d
1108for i ulong 9
1109ohx get d i
1110end
1111ob1 32
1112e = reenc d
1113if ge e 0
1114if eq ulong e x
1115ob1 61
1116else
1117ob1 33
1118end
1119ret
1120end
1121if lossok f d
1122ob1 126
1123else
1124ob1 33
1125end
1126end
1127
1128note P maxlen pos n, then n times form f1 f2 f3 f4
1129fn dobuf to void
1130b = new uchar 4096 170
1131st = new ulong 2 0
1132put st 1 rdhex
1133p0 = rdhex
1134put st 0 p0
1135n = rdhex
1136ob1 80
1137for j ulong n
1138f = rdhex
1139for i ulong 4
1140put fv i rdhex
1141end
1142ob1 32
1143odec a64put b st enca f
1144end
1145ohx get st 0
1146ob1 32
1147e = add p0 mul n 4
1148if gt e 4096
1149e = 4096
1150end
1151for i ulong sub e p0
1152ohxb ulong get b add p0 i
1153end
1154end
1155
1156fn dodec to void
1157d = new ulong 9 0
1158w = rdhex
1159ob1 68
1160ohx bitcast ulong a64decode w d
1161for i ulong 9
1162ohx get d i
1163end
1164end
1165
1166note V len ml bytes: bytes is one hex token of len bytes ("-" when empty)
1167fn doval to void
1168code = new uchar 4096 0
1169msg = new uchar 256 170
1170n = rdhex
1171ml = rdhex
1172c = inb
1173while eq c 32 bound 999999
1174c = inb
1175end
1176k = ulong 0
1177while lt hexval c 16 bound 999999
1178e = inb
1179put code k uchar or shl hexval c 4 hexval e
1180k = add k 1
1181c = inb
1182end
1183r = a64validate code n msg ml
1184ob1 86
1185ob1 32
1186odec r
1187ob1 32
1188for i ulong 100
1189ohxb ulong get msg i
1190end
1191end
1192
1193fn main to int
1194for q ulong 4294967296
1195c = rdcode
1196if eq c 256
1197break
1198end
1199if eq c 69
1200doenc
1201end
1202if eq c 80
1203dobuf
1204end
1205if eq c 68
1206dodec
1207end
1208if eq c 86
1209doval
1210end
1211ob1 10
1212end
1213obflush
1214ret 0
1215end