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