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 / ng / bench / hand.om

882 lines · 15612 bytes Raw Blame History
1note hand: builds the hand-edited arm binaries of the M3 payoff map (seed/ng/bench/PREREG-payoff.md) and
2note checks them against objdump. Built by seed/out/omc from this file alone. Lane ng-bench.
3note
4note A SPEC is an encoder case file of seed/ng/src/a64.om (ng mode `@ng a64-enc`, ng-a64 lane): line 1 is
5note `@ng a64-enc`, every case line `FORM operands | expected disassembly` becomes one instruction word, and
6note lines starting with `;` are comments to the encoder. hand reads directives from those comment lines:
7note ; @at ADDR the next word goes to virtual address ADDR (0x hex, 4-aligned, >= 0x4000b0)
8note ; @old ADDR W .. the input ELF must hold the words W .. at ADDR, ADDR+4, .. (else exit 1)
9note ; @word W | TEXT one literal word W at the cursor (forms the encoder lacks: REV, SIMD)
10note ; @nop ADDR NOP words from the cursor up to (not including) ADDR
11note Every other comment line is ignored. A word written at or past the end of the input file grows the
12note file (the write must start exactly at the end: no holes) and program header 0 (offset 0, vaddr
13note 0x400000, the code segment of omc's layout) gets the new p_filesz and p_memsz.
14note
15note hand splice SPEC WORDS IN OUT
16note WORDS: the encoder's output for SPEC (one little-endian word per case line, in order). OUT = IN
17note with every word of SPEC written. stderr "hand: OK case=N word=N nop=N grow=N size=N".
18note hand check SPEC
19note stdin: `objdump -D -b binary -m aarch64 --adjust-vma=0x400000 OUT`. For every word SPEC writes,
20note objdump's text at its address must equal the SPEC's expected text (NOP fills expect `nop`).
21note Comparison: blanks collapsed, objdump's `//` comment dropped, `@+N` / `@-N` in the expected text
22note means the word's own address plus / minus N bytes, printed as objdump prints it (0x, lowercase).
23note stdout "hand: check OK words=N".
24note Exit 0 ok; 1 a check failed (named on stderr: old word, word count, expected text, hole, header);
25note 2 usage; 3 a file unreadable, too large or not writable; 4 a SPEC line malformed (line number).
26note A 0x number of more than 16 hex digits, or an `@+N` / `@-N` of more than 19 decimal digits, is a malformed
27note line (exit 4); before bay 10's fix they ran out of their loop bound (trap 4, exit 104). That fix changed the
28note binary from f352309b (the PREREG-payoff pin that built the round's arms) to db7f7808; all 20 arms it splices
29note are byte-identical to payoff-manifest.sha256.
30om2
31text tusage "usage: hand splice SPEC WORDS IN OUT | hand check SPEC < objdump-text\n"
32text tok "hand: OK case="
33text tword " word="
34text tnop " nop="
35text tgrow " grow="
36text tsize " size="
37text tchk "hand: check OK words="
38text tbad "hand: SPEC line "
39text tmal " malformed\n"
40text tunr "hand: cannot read or write argument "
41text told "hand: old word differs at "
42text tcnt "hand: WORDS holds a different number of words than SPEC has case lines\n"
43text thole "hand: write leaves a hole or runs past 4 MiB at "
44text thdr "hand: program header 0 is not omc's code segment\n"
45text tmis "hand: check differs at "
46text tobj " objdump: "
47text texp " expected: "
48text tnoexp "hand: no expected text for the word at "
49text tnodis "hand: objdump has no line for "
50text tnl "\n"
51text tnopt "nop"
52global hsp uchar 1048576
53global hwd uchar 1048576
54global hel uchar 4194304
55global hob uchar 16777216
56global hix ulong 1048576
57global hpb uchar 4096
58global hln uchar 4096
59global hlnn ulong 1
60global hta uchar 512
61global htb uchar 512
62global hst ulong 24
63note hst: 0 SPEC length, 1 WORDS length, 2 ELF length, 3 cursor address, 4 case words taken, 5 literal
64note words, 6 NOP words, 7 grown bytes, 8 line number, 9 parse position, 10 parsed value, 11 objdump
65note length, 12 words checked, 13 mode (1 splice, 2 check), 14 ELF length before growth, 15 hta length,
66note 16 htb length, 17 position of the expected text (0: none)
67
68note ---- output line
69fn ltext t view uchar * to void
70n = get hlnn 0
71if gt add n len t 4096
72trap 9
73end
74for i ulong len t
75put hln add n i get t i
76end
77put hlnn 0 add n len t
78end
79fn lbyte c uchar to void
80n = get hlnn 0
81if lt n 4096
82put hln n c
83put hlnn 0 add n 1
84end
85end
86fn lnum v ulong to void
87d = new uchar 24 0
88p = ulong 24
89x = v
90while gt x 9
91p = sub p 1
92put d p uchar add rem x 10 48
93x = div x 10
94end
95p = sub p 1
96put d p uchar add x 48
97for i ulong sub 24 p
98lbyte get d add p i
99end
100end
101fn hexc v ulong to uchar
102if lt v 10
103ret uchar add v 48
104end
105ret uchar add v 87
106end
107note lhex: 0x and the lowercase hex digits of v without leading zeros, the way objdump prints addresses
108fn lhex v ulong to void
109lbyte 48
110lbyte 120
111s = ulong 60
112going = true
113while and going gt s 0
114if eq and shr v s 15 0
115s = sub s 4
116else
117going = false
118end
119end
120k = add div s 4 1
121for i ulong k
122sh = sub s mul i 4
123lbyte hexc and shr v sh 15
124end
125end
126fn lflush fd int to void
127w = write fd hln 0 get hlnn 0
128put hlnn 0 0
129end
130fn lline to void
131ltext tbad
132lnum get hst 8
133ltext tmal
134lflush 2
135exit 4
136end
137
138fn lnoarg idx ulong to void
139ltext tunr
140lnum idx
141ltext tnl
142lflush 2
143exit 3
144end
145
146note ---- files: buffer selector 1 hsp (SPEC), 2 hwd (WORDS), 3 hel (ELF), 4 hob (objdump text); globals
147note cannot be passed (error 19), so the reads go through these selectors
148fn bcap sel ulong to ulong
149if eq sel 1
150ret len hsp
151end
152if eq sel 2
153ret len hwd
154end
155if eq sel 3
156ret len hel
157end
158ret len hob
159end
160fn rdpart fd int sel ulong n ulong to long
161m = sub bcap sel n
162if eq sel 1
163ret read fd hsp n m
164end
165if eq sel 2
166ret read fd hwd n m
167end
168if eq sel 3
169ret read fd hel n m
170end
171ret read fd hob n m
172end
173note rdfd: everything from fd into buffer sel; its size, or exit 3 (argument idx named) when it fills
174note the buffer completely
175fn rdfd fd int sel ulong idx ulong to ulong
176n = ulong 0
177going = true
178while going bound 100000
179k = rdpart fd sel n
180if le k 0
181going = false
182else
183n = add n ulong k
184if eq n bcap sel
185lnoarg idx
186end
187end
188end
189ret n
190end
191note rdarg: the file named by argument idx into buffer sel
192fn rdarg idx ulong sel ulong to ulong
193r = argread idx hpb 0 4096
194if le r 0
195exit 2
196end
197fd = int fopen hpb 0 ulong r 0 0
198if lt fd 0
199lnoarg idx
200end
201n = rdfd fd sel idx
202c = fclose fd
203ret n
204end
205
206note ---- little-endian fields of the ELF
207fn elw o ulong to ulong
208b0 = ulong get hel o
209b1 = ulong get hel add o 1
210b2 = ulong get hel add o 2
211b3 = ulong get hel add o 3
212ret or or b0 shl b1 8 or shl b2 16 shl b3 24
213end
214fn elq o ulong to ulong
215ret or elw o shl elw add o 4 32
216end
217fn elput o ulong v ulong to void
218for i ulong 4
219put hel add o i uchar and shr v mul i 8 255
220end
221end
222fn elputq o ulong v ulong to void
223elput o and v 4294967295
224elput add o 4 shr v 32
225end
226fn wdw k ulong to ulong
227o = mul k 4
228b0 = ulong get hwd o
229b1 = ulong get hwd add o 1
230b2 = ulong get hwd add o 2
231b3 = ulong get hwd add o 3
232ret or or b0 shl b1 8 or shl b2 16 shl b3 24
233end
234
235note ---- SPEC scanning: positions in hsp; hst 9 is the parse position
236fn isblank c uchar to bool
237ret or eq c 32 eq c 9
238end
239fn skipb e ulong to void
240p = get hst 9
241going = true
242while and going lt p e bound 1048576
243if isblank get hsp p
244p = add p 1
245else
246going = false
247end
248end
249put hst 9 p
250end
251fn hexv c uchar to ulong
252if and ge c 48 le c 57
253ret sub ulong c 48
254end
255if and ge c 97 le c 102
256ret sub ulong c 87
257end
258ret 99
259end
260note phex: a 0x number at the parse position (blanks skipped first) into hst 10; false when there is none
261fn phex e ulong to bool
262skipb e
263p = get hst 9
264if ge add p 2 e
265ret false
266end
267if or ne get hsp p 48 ne get hsp add p 1 120
268ret false
269end
270p = add p 2
271v = ulong 0
272k = ulong 0
273going = true
274while and going and lt p e le k 16 bound 64
275d = hexv get hsp p
276if gt d 15
277going = false
278else
279v = or shl v 4 d
280k = add k 1
281p = add p 1
282end
283end
284if or eq k 0 gt k 16
285ret false
286end
287put hst 9 p
288put hst 10 v
289ret true
290end
291note kw: the SPEC holds the bytes of t at p
292fn kw p ulong e ulong t view uchar * to bool
293if gt add p len t e
294ret false
295end
296for i ulong len t
297if ne get hsp add p i get t i
298ret false
299end
300end
301ret true
302end
303note bar: position just after the first `|` in [s, e), or 0
304fn bar s ulong e ulong to ulong
305for i ulong sub e s
306if eq get hsp add s i 124
307ret add add s i 1
308end
309end
310ret 0
311end
312
313note ---- one word at the cursor
314fn putword w ulong to void
315a = get hst 3
316if or lt a 0x4000b0 ne and a 3 0
317lline
318end
319o = sub a 0x400000
320n = get hst 2
321if ge o n
322if or ne o n gt add o 4 4194304
323ltext thole
324lhex a
325ltext tnl
326lflush 2
327exit 1
328end
329put hst 2 add n 4
330put hst 7 add get hst 7 4
331end
332elput o w
333put hst 3 add a 4
334end
335
336note ---- normalised texts: hta from objdump, htb from the SPEC (blanks collapsed, ends trimmed)
337fn ta c uchar to void
338n = get hst 15
339if eq c 32
340if eq n 0
341ret
342end
343if eq get hta sub n 1 32
344ret
345end
346end
347if lt n 512
348put hta n c
349put hst 15 add n 1
350end
351end
352fn tb c uchar to void
353n = get hst 16
354if eq c 32
355if eq n 0
356ret
357end
358if eq get htb sub n 1 32
359ret
360end
361end
362if lt n 512
363put htb n c
364put hst 16 add n 1
365end
366end
367fn trim to void
368n = get hst 15
369if gt n 0
370if eq get hta sub n 1 32
371put hst 15 sub n 1
372end
373end
374m = get hst 16
375if gt m 0
376if eq get htb sub m 1 32
377put hst 16 sub m 1
378end
379end
380end
381note nobj: objdump's line at hob[p ..] (p just after `ADDR:`): skip the tab, the word and its tab, then the
382note instruction text up to the newline or `//`
383fn nobj p0 ulong to void
384put hst 15 0
385n = get hst 11
386p = p0
387tabs = ulong 0
388while and lt p n lt tabs 2 bound 64
389c = get hob p
390if eq c 10
391tabs = 9
392else
393if eq c 9
394tabs = add tabs 1
395end
396p = add p 1
397end
398end
399going = true
400while and going lt p n bound 4096
401c = get hob p
402if eq c 10
403going = false
404else
405if eq c 47
406if lt add p 1 n
407if eq get hob add p 1 47
408going = false
409end
410end
411end
412end
413if going
414if eq c 9
415ta 32
416else
417ta c
418end
419p = add p 1
420end
421end
422trim
423end
424fn nfix t view uchar * to void
425put hst 16 0
426for i ulong len t
427tb get t i
428end
429trim
430end
431note nexp: the SPEC's expected text from q to the end of the line, `@+N` / `@-N` resolved against a
432fn nexp q ulong a ulong to void
433put hst 16 0
434n = get hst 0
435p = q
436going = true
437while and going lt p n bound 4096
438c = get hsp p
439if eq c 10
440going = false
441else
442if and eq c 64 lt add p 1 n
443p = nrel p n a
444else
445if eq c 9
446tb 32
447else
448tb c
449end
450p = add p 1
451end
452end
453end
454nfix2
455end
456fn nfix2 to void
457m = get hst 16
458if gt m 0
459if eq get htb sub m 1 32
460put hst 16 sub m 1
461end
462end
463s = ulong 0
464while and lt s get hst 16 eq get htb s 32 bound 512
465s = add s 1
466end
467if gt s 0
468k = sub get hst 16 s
469for i ulong k
470put htb i get htb add i s
471end
472put hst 16 k
473end
474end
475note nrel: `@+N` or `@-N` at p; writes the target as 0x hex into htb; returns the position after N
476fn nrel p0 ulong n ulong a ulong to ulong
477sg = get hsp add p0 1
478p = add p0 2
479v = ulong 0
480k = ulong 0
481going = true
482while and going and lt p n le k 19 bound 24
483c = get hsp p
484if and ge c 48 le c 57
485v = add mul v 10 sub ulong c 48
486k = add k 1
487p = add p 1
488else
489going = false
490end
491end
492if gt k 19
493lline
494end
495t = add a v
496if eq sg 45
497t = sub a v
498else
499if ne sg 43
500lline
501end
502end
503put hlnn 0 0
504lhex t
505for i ulong get hlnn 0
506tb get hln i
507end
508put hlnn 0 0
509ret p
510end
511
512note tsame: hta equals htb
513fn tsame to bool
514n = get hst 15
515if ne n get hst 16
516ret false
517end
518for i ulong n
519if ne get hta i get htb i
520ret false
521end
522end
523ret true
524end
525note in check mode: compare objdump's text at the cursor with the expected text at q (isnop: `nop`;
526note else q 0 means the line has no expected text, an error), step the cursor
527fn chkword q ulong isnop bool to void
528a = get hst 3
529if or lt a 0x400000 ne and a 3 0
530lline
531end
532k = div sub a 0x400000 4
533if ge k 1048576
534lline
535end
536if and eq q 0 not isnop
537ltext tnoexp
538lhex a
539ltext tnl
540lflush 2
541exit 1
542end
543ix = get hix k
544if eq ix 0
545ltext tnodis
546lhex a
547ltext tnl
548lflush 2
549exit 1
550end
551nobj sub ix 1
552if isnop
553nfix tnopt
554else
555nexp q a
556end
557if not tsame
558ltext tmis
559lhex a
560ltext tobj
561for i ulong get hst 15
562lbyte get hta i
563end
564ltext texp
565for i ulong get hst 16
566lbyte get htb i
567end
568ltext tnl
569lflush 2
570exit 1
571end
572put hst 12 add get hst 12 1
573put hst 3 add a 4
574end
575
576note ---- objdump index: hix[(addr - 0x400000) / 4] = 1 + position just after `ADDR:`
577fn objix to void
578n = get hst 11
579p = ulong 0
580while lt p n bound 20000000
581s = p
582while and lt p n eq get hob p 32 bound 64
583p = add p 1
584end
585v = ulong 0
586k = ulong 0
587going = true
588while and going lt p n bound 24
589d = hexv get hob p
590if gt d 15
591going = false
592else
593v = or shl v 4 d
594k = add k 1
595p = add p 1
596end
597end
598if and gt k 0 lt p n
599if eq get hob p 58
600if and ge v 0x400000 lt v 0x800000
601if eq and v 3 0
602put hix div sub v 0x400000 4 add p 2
603end
604end
605end
606end
607while and lt p n ne get hob p 10 bound 4096
608p = add p 1
609end
610p = add p 1
611if eq p s
612p = add p 1
613end
614end
615end
616
617fn caseline s ulong e ulong to void
618k = get hst 4
619put hst 4 add k 1
620if eq get hst 13 1
621if gt mul add k 1 4 get hst 1
622ltext tcnt
623lflush 2
624exit 1
625end
626putword wdw k
627else
628chkword bar s e false
629end
630end
631fn dold e ulong to void
632if not phex e
633lline
634end
635a = get hst 10
636if eq get hst 13 2
637ret
638end
639going = true
640while going bound 4096
641if phex e
642w = get hst 10
643o = sub a 0x400000
644if or lt a 0x400000 gt add o 4 get hst 14
645lline
646end
647if ne elw o w
648ltext told
649lhex a
650ltext tnl
651lflush 2
652exit 1
653end
654a = add a 4
655else
656going = false
657end
658end
659end
660fn dword e ulong to void
661s = get hst 9
662if not phex e
663lline
664end
665w = get hst 10
666if gt w 4294967295
667lline
668end
669put hst 5 add get hst 5 1
670if eq get hst 13 1
671putword w
672else
673chkword bar s e false
674end
675end
676fn dnop e ulong to void
677if not phex e
678lline
679end
680t = get hst 10
681a = get hst 3
682if or lt t a ne and t 3 0
683lline
684end
685k = div sub t a 4
686for i ulong k
687put hst 6 add get hst 6 1
688if eq get hst 13 1
689putword 0xd503201f
690else
691chkword 0 true
692end
693end
694end
695text tat "at"
696text tolds "old"
697text twd "word"
698text tnps "nop"
699fn directive e ulong to void
700p = get hst 9
701if kw p e tolds
702put hst 9 add p 3
703dold e
704ret
705end
706if kw p e tat
707put hst 9 add p 2
708if not phex e
709lline
710end
711put hst 3 get hst 10
712ret
713end
714if kw p e twd
715put hst 9 add p 4
716dword e
717ret
718end
719if kw p e tnps
720put hst 9 add p 3
721dnop e
722ret
723end
724lline
725end
726note ---- the SPEC walk, shared by splice (mode 1) and check (mode 2)
727fn walk to void
728n = get hst 0
729p = ulong 0
730ln = ulong 0
731while lt p n bound 1048576
732s = p
733while and lt p n ne get hsp p 10 bound 1048576
734p = add p 1
735end
736e = p
737p = add p 1
738ln = add ln 1
739put hst 8 ln
740oneline s e
741end
742end
743fn oneline s ulong e ulong to void
744put hst 9 s
745skipb e
746q = get hst 9
747if ge q e
748ret
749end
750c = get hsp q
751if eq c 64
752ret
753end
754if eq c 59
755put hst 9 add q 1
756skipb e
757r = get hst 9
758if lt r e
759if eq get hsp r 64
760put hst 9 add r 1
761directive e
762end
763end
764ret
765end
766caseline s e
767end
768
769fn usage to void
770w = write 2 tusage 0 len tusage
771exit 2
772end
773fn dosplice to int
774if ne argcnt 6
775usage
776end
777put hst 13 1
778put hst 0 rdarg 2 1
779put hst 1 rdarg 3 2
780n = rdarg 4 3
781put hst 2 n
782put hst 14 n
783if lt n 0x100
784ltext thdr
785lflush 2
786exit 1
787end
788if or ne elq 0x48 0 ne elq 0x50 0x400000
789ltext thdr
790lflush 2
791exit 1
792end
793if ne elq 0x60 n
794ltext thdr
795lflush 2
796exit 1
797end
798walk
799if ne mul get hst 4 4 get hst 1
800ltext tcnt
801lflush 2
802exit 1
803end
804m = get hst 2
805if ne m n
806elputq 0x60 m
807elputq 0x68 m
808end
809r = argread 5 hpb 0 4096
810if le r 0
811exit 2
812end
813fd = int fopen hpb 0 ulong r 577 493
814if lt fd 0
815lnoarg 5
816end
817w = write fd hel 0 m
818c = fclose fd
819if ne w long m
820lnoarg 5
821end
822ltext tok
823lnum get hst 4
824ltext tword
825lnum get hst 5
826ltext tnop
827lnum get hst 6
828ltext tgrow
829lnum get hst 7
830ltext tsize
831lnum m
832ltext tnl
833lflush 2
834ret 0
835end
836fn docheck to int
837if ne argcnt 3
838usage
839end
840put hst 13 2
841put hst 0 rdarg 2 1
842put hst 11 rdfd int 0 4 0
843objix
844walk
845ltext tchk
846lnum get hst 12
847ltext tnl
848lflush 1
849ret 0
850end
851text tsplice "splice"
852text tcheck "check"
853fn main to int
854if lt argcnt 3
855usage
856end
857r = argread 1 hpb 0 4096
858if eq r 6
859ok = true
860for i ulong 6
861if ne get hpb i get tsplice i
862ok = false
863end
864end
865if ok
866ret dosplice
867end
868end
869if eq r 5
870ok = true
871for i ulong 5
872if ne get hpb i get tcheck i
873ok = false
874end
875end
876if ok
877ret docheck
878end
879end
880usage
881ret 2
882end