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/front/prune.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 / front / prune.om` 1248 lines ยท 21426 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note seed/ng/front/prune.om: structural line pruning of an Omega unit (ng-front: the machine-code paths of 3 note omc.om are removed from the front end with it, DECISIONS D1b/D8 and the queen's front.om scope ruling). 4 note Handwritten seeded Omega, program mode, Linux AArch64, built by the pinned seed/out/omc. 5 note prune SCRIPT < IN.om > OUT.om 6 note An Omega unit is line structured: a line whose first word is fn, if, while or for opens a block, `else` 7 note splits an if, `end` closes. SCRIPT lines (# lines and blank lines skipped), applied in order: 8 note false COND every `if COND` line (COND the exact rest of the line): the if, its then-part and its end 9 note go; an else-part stays in place (its else line goes). Use for a condition that is false 10 note in every unit the output accepts. 11 note true COND every `if COND` line: the if line goes, and the else-part with its else line; then stays. 12 note call NAME every line whose first word is NAME (a call statement) goes. 13 note dead ROOT from function ROOT, every fn and global no kept line of a reachable function names goes, 14 note with the note lines right above it. 15 note Every false, true and call line must match at least once (a stale script is refused). stderr gets one 16 note line per script line: the command and how many lines it removed. Exit 0, or 1 on any refusal. 17 18 text msg "prune: usage: prune SCRIPT < IN > OUT\nprune: script unreadable, malformed or too large\nprune: input too large or too many lines\nprune: unbalanced block at line \nprune: script line matched nothing: \nprune: dead root is not a function\nprune: output write failed\nprune: too many names\n removed \nprune: sink is not a kept name: \nprune: sink read at line \n: \nprune: sink function \n" 19 text kw "if\nwhile\nfor\nfn\nend\nelse\nnote\nglobal\nfalse\ntrue\ncall\ndead\nput\nexit\nwrite\nread\nsink\npure\nsinkcheck\ntidy\nret\n" 20 21 global inp uchar `8388608` 22 global inn ulong 1 23 global lo ulong 65536 24 global lend ulong 65536 25 global kp uchar 65536 26 global lk uchar 65536 27 global td uchar 65536 28 global nl ulong 1 29 global cm uchar `1048576` 30 global cmn ulong 1 31 global out uchar `8388608` 32 global otn ulong 1 33 global pb uchar 4096 34 global wpos ulong 2 35 global me ulong 2 36 global no ulong 8192 37 global nln ulong 8192 38 global nk uchar 8192 39 global nd ulong 8192 40 global nend ulong 8192 41 global nlv uchar 8192 42 global nn ulong 1 43 global ht ulong 16384 44 global wk ulong 8192 45 global nrm ulong 1 46 global nsk uchar 8192 47 global npu uchar 8192 48 global sko ulong 512 49 global skl ulong 512 50 global nsl ulong 1 51 global tno ulong 256 52 global tnl ulong 256 53 global ntn ulong 1 54 global lh ulong 2 55 global tnot ulong 1 56 57 note ---- messages -------------------------------------------------------------------------------------- 58 fn seg id ulong to void 59 k = ulong 1 60 s = ulong 0 61 for i ulong len msg 62 if eq get msg i 10 63 if eq k id 64 put wpos 0 s 65 put wpos 1 sub i s 66 ret 67 end 68 k = add k 1 69 s = add i 1 70 end 71 end 72 exit 99 73 end 74 fn say id ulong to void 75 seg id 76 r = write 2 msg get wpos 0 get wpos 1 77 end 78 fn nl2 to void 79 put pb 0 10 80 r = write 2 pb 0 1 81 end 82 fn num v ulong to void 83 x = v 84 n = ulong 0 85 while ge x 10 bound 32 86 put pb n uchar add 48 rem x 10 87 x = div x 10 88 n = add n 1 89 end 90 put pb n uchar add 48 x 91 n = add n 1 92 for i ulong n 93 r = write 2 pb sub sub n i 1 1 94 end 95 end 96 97 note ---- words --------------------------------------------------------------------------------------------- 98 fn blank c uchar to bool 99 ret or eq c 32 eq c 9 100 end 101 note the first word of inp[o .. e) into wpos (length 0 for none) 102 fn firstw o ulong e ulong to void 103 i = o 104 while and lt i e blank get inp i bound `8388608` 105 i = add i 1 106 end 107 j = i 108 while and lt j e not blank get inp j bound `8388608` 109 j = add j 1 110 end 111 put wpos 0 i 112 put wpos 1 sub j i 113 end 114 note inp[o .. o+n) is word id of kw 115 fn iskw o ulong n ulong id ulong to bool 116 k = ulong 1 117 s = ulong 0 118 for i ulong len kw 119 if eq get kw i 10 120 if eq k id 121 if ne n sub i s 122 ret false 123 end 124 for j ulong n 125 if ne get inp add o j get kw add s j 126 ret false 127 end 128 end 129 ret true 130 end 131 k = add k 1 132 s = add i 1 133 end 134 end 135 ret false 136 end 137 note cm[o .. o+n) is word id of kw 138 fn cmkw o ulong n ulong id ulong to bool 139 k = ulong 1 140 s = ulong 0 141 for i ulong len kw 142 if eq get kw i 10 143 if eq k id 144 if ne n sub i s 145 ret false 146 end 147 for j ulong n 148 if ne get cm add o j get kw add s j 149 ret false 150 end 151 end 152 ret true 153 end 154 k = add k 1 155 s = add i 1 156 end 157 end 158 ret false 159 end 160 note the kind of line i: 1 opens a block, 2 else, 3 end, 0 other 161 fn lkind i ulong to ulong 162 firstw get lo i get lend i 163 o = get wpos 0 164 n = get wpos 1 165 if or iskw o n 1 or iskw o n 2 or iskw o n 3 iskw o n 4 166 ret 1 167 end 168 if iskw o n 6 169 ret 2 170 end 171 if iskw o n 5 172 ret 3 173 end 174 ret 0 175 end 176 177 note ---- input and lines ---------------------------------------------------------------------------------- 178 fn loadinp to bool 179 n = ulong 0 180 go = true 181 while go bound 100000 182 q = read 0 inp n sub len inp n 183 if le q 0 184 go = false 185 else 186 n = add n ulong q 187 if ge n len inp 188 go = false 189 end 190 end 191 end 192 put inn 0 n 193 if ge n len inp 194 ret false 195 end 196 k = ulong 0 197 s = ulong 0 198 for i ulong n bound `8388608` 199 if eq get inp i 10 200 if ge k len lo 201 ret false 202 end 203 put lo k s 204 put lend k i 205 k = add k 1 206 s = add i 1 207 end 208 end 209 if lt s n 210 if ge k len lo 211 ret false 212 end 213 put lo k s 214 put lend k n 215 k = add k 1 216 end 217 put nl 0 k 218 for i ulong k bound 65536 219 put kp i 1 220 put lk i uchar lkind i 221 end 222 ret true 223 end 224 note the else line (or nl) and the end line of the block opened at line i, into me 225 fn matchend i ulong to bool 226 d = ulong 0 227 put me 0 get nl 0 228 for j ulong sub get nl 0 add i 1 bound 65536 229 x = add add i 1 j 230 k = get lk x 231 if eq k 1 232 d = add d 1 233 end 234 if eq k 2 235 if eq d 0 236 put me 0 x 237 end 238 end 239 if eq k 3 240 if eq d 0 241 put me 1 x 242 ret true 243 end 244 d = sub d 1 245 end 246 end 247 ret false 248 end 249 250 note ---- the script ---------------------------------------------------------------------------------------- 251 fn loadcm to bool 252 r = argread 1 pb 0 4095 253 if le r 0 254 ret false 255 end 256 fd = fopen pb 0 ulong r 524288 0 257 if lt fd 0 258 ret false 259 end 260 n = ulong 0 261 go = true 262 while go bound 100000 263 q = read int fd cm n sub len cm n 264 if le q 0 265 go = false 266 else 267 n = add n ulong q 268 if ge n len cm 269 go = false 270 end 271 end 272 end 273 q = fclose int fd 274 put cmn 0 n 275 ret lt n len cm 276 end 277 note line i is `while ` followed by exactly cm[o .. o+n) (false removes such a loop whole) 278 fn whis i ulong o ulong n ulong to bool 279 s = get lo i 280 if ne sub get lend i s add n 6 281 ret false 282 end 283 firstw s get lend i 284 if not iskw get wpos 0 get wpos 1 2 285 ret false 286 end 287 for j ulong n 288 if ne get inp add add s 6 j get cm add o j 289 ret false 290 end 291 end 292 ret true 293 end 294 note line i is `if ` followed by exactly cm[o .. o+n) 295 fn ifis i ulong o ulong n ulong to bool 296 s = get lo i 297 if ne sub get lend i s add n 3 298 ret false 299 end 300 if ne get inp s 105 301 ret false 302 end 303 if or ne get inp add s 1 102 ne get inp add s 2 32 304 ret false 305 end 306 for j ulong n 307 if ne get inp add add s 3 j get cm add o j 308 ret false 309 end 310 end 311 ret true 312 end 313 fn drop i ulong to void 314 if ne get kp i 0 315 put kp i 0 316 put nrm 0 add get nrm 0 1 317 end 318 end 319 fn dropr a ulong b ulong to void 320 for j ulong add sub b a 1 bound 65536 321 drop add a j 322 end 323 end 324 note false (t = false) or true (t = true) for condition cm[o .. o+n); lines matched 325 fn branch o ulong n ulong t bool to ulong 326 c = ulong 0 327 for i ulong get nl 0 bound 65536 328 if eq get lk i 1 329 if or ifis i o n whis i o n 330 if not matchend i 331 say 4 332 num add i 1 333 nl2 334 exit 1 335 end 336 c = add c 1 337 branch1 i t 338 end 339 end 340 end 341 ret c 342 end 343 fn branch1 i ulong t bool to void 344 el = get me 0 345 en = get me 1 346 if t 347 drop i 348 if lt el get nl 0 349 dropr el en 350 else 351 drop en 352 end 353 else 354 if lt el get nl 0 355 dropr i el 356 drop en 357 else 358 dropr i en 359 end 360 end 361 end 362 fn calls o ulong n ulong to ulong 363 c = ulong 0 364 for i ulong get nl 0 bound 65536 365 if eq get kp i 1 366 firstw get lo i get lend i 367 if eq get wpos 1 n 368 if cmsame get wpos 0 o n 369 drop i 370 c = add c 1 371 end 372 end 373 end 374 end 375 ret c 376 end 377 fn cmsame a ulong o ulong n ulong to bool 378 for j ulong n 379 if ne get inp add a j get cm add o j 380 ret false 381 end 382 end 383 ret true 384 end 385 386 note ---- names and reachability ------------------------------------------------------------------------------ 387 fn hb o ulong n ulong to ulong 388 h = ulong 5381 389 for i ulong n 390 h = xor mul h 33 ulong get inp add o i 391 end 392 ret and h 16383 393 end 394 fn nsame k ulong o ulong n ulong to bool 395 if ne get nln k n 396 ret false 397 end 398 a = get no k 399 for j ulong n 400 if ne get inp add a j get inp add o j 401 ret false 402 end 403 end 404 ret true 405 end 406 note 1 + the name entry spelled inp[o .. o+n), or 0 407 fn nfind o ulong n ulong to ulong 408 h = hb o n 409 for t ulong 16384 410 s = and add h t 16383 411 v = get ht s 412 if eq v 0 413 ret 0 414 end 415 if nsame sub v 1 o n 416 ret v 417 end 418 end 419 ret 0 420 end 421 fn nadd o ulong n ulong kind ulong d ulong to bool 422 k = get nn 0 423 if ge k len no 424 ret false 425 end 426 put no k o 427 put nln k n 428 put nk k uchar kind 429 put nd k d 430 put nend k d 431 put nlv k 0 432 put nsk k 0 433 put npu k 0 434 h = hb o n 435 for t ulong 16384 436 s = and add h t 16383 437 if eq get ht s 0 438 put ht s add k 1 439 put nn 0 add k 1 440 ret true 441 end 442 end 443 ret false 444 end 445 note the name declared by kept line i (fn or global, at top level), as a table entry 446 fn declname i ulong to bool 447 firstw get lo i get lend i 448 o = get wpos 0 449 n = get wpos 1 450 kind = ulong 0 451 if iskw o n 4 452 kind = ulong 1 453 end 454 if iskw o n 8 455 kind = ulong 2 456 end 457 if eq kind 0 458 ret true 459 end 460 firstw add o n get lend i 461 if not nadd get wpos 0 get wpos 1 kind i 462 ret false 463 end 464 if eq kind 1 465 if not matchend i 466 say 4 467 num add i 1 468 nl2 469 exit 1 470 end 471 put nend sub get nn 0 1 get me 1 472 end 473 ret true 474 end 475 note mark name k live and queue it 476 fn live k ulong qn ulong to ulong 477 if ne get nlv k 0 478 ret qn 479 end 480 put nlv k 1 481 put wk qn k 482 ret add qn 1 483 end 484 note every word of kept line i that names an entry is marked live 485 fn scanline i ulong qn ulong to ulong 486 q = qn 487 e = get lend i 488 p = get lo i 489 firstw p e 490 if iskw get wpos 0 get wpos 1 7 491 ret q 492 end 493 while lt p e bound 65536 494 firstw p e 495 if eq get wpos 1 0 496 ret q 497 end 498 v = nfind get wpos 0 get wpos 1 499 if ne v 0 500 q = live sub v 1 q 501 end 502 p = add get wpos 0 get wpos 1 503 end 504 ret q 505 end 506 note 1 + the fn entry named cm[o .. o+n), or 0 507 fn findroot o ulong n ulong to ulong 508 for k ulong get nn 0 509 if and eq get nk k 1 eq get nln k n 510 if rootis k o n 511 ret add k 1 512 end 513 end 514 end 515 ret 0 516 end 517 note the name table of the kept lines; sink globals are marked from the sink list 518 fn buildnames to void 519 put nn 0 0 520 for i ulong len ht 521 put ht i 0 522 end 523 for i ulong get nl 0 bound 65536 524 if eq get kp i 1 525 if not declname i 526 say 8 527 nl2 528 exit 1 529 end 530 end 531 end 532 for s ulong get nsl 0 533 v = nfindcm get sko s get skl s 534 if ne v 0 535 put nsk sub v 1 1 536 end 537 end 538 end 539 note 1 + the name entry spelled cm[o .. o+n), or 0 540 fn nfindcm o ulong n ulong to ulong 541 for k ulong get nn 0 542 if eq get nln k n 543 if rootis k o n 544 ret add k 1 545 end 546 end 547 end 548 ret 0 549 end 550 note remember sink global cm[o .. o+n) 551 fn sinkadd o ulong n ulong to ulong 552 k = get nsl 0 553 if ge k len sko 554 say 8 555 nl2 556 exit 1 557 end 558 put sko k o 559 put skl k n 560 put nsl 0 add k 1 561 ret 1 562 end 563 note the word after inp[o .. o+n) on line i, as 1 + its name entry (0 when it names nothing) 564 fn nextname o ulong n ulong i ulong to ulong 565 firstw add o n get lend i 566 if eq get wpos 1 0 567 ret 0 568 end 569 ret nfind get wpos 0 get wpos 1 570 end 571 note line i stores to a sink global: `put G ...` with G a sink 572 fn sinkput i ulong to bool 573 firstw get lo i get lend i 574 o = get wpos 0 575 n = get wpos 1 576 if not iskw o n 13 577 ret false 578 end 579 v = nextname o n i 580 if eq v 0 581 ret false 582 end 583 ret ne get nsk sub v 1 0 584 end 585 note line i may have an effect beyond the dropped sinks: a store to a global that is not a sink, a host word, 586 note exit, or a call of a function not (yet) known pure 587 fn lineeff i ulong to bool 588 e = get lend i 589 firstw get lo i e 590 o = get wpos 0 591 n = get wpos 1 592 if iskw o n 7 593 ret false 594 end 595 if iskw o n 13 596 v = nextname o n i 597 if ne v 0 598 if and eq get nk sub v 1 2 eq get nsk sub v 1 0 599 ret true 600 end 601 end 602 end 603 ret toksff get lo i e 604 end 605 note name entry v (1-based, 0 none) is a function not known pure 606 fn impure v ulong to bool 607 if eq v 0 608 ret false 609 end 610 k = sub v 1 611 ret and eq get nk k 1 eq get npu k 0 612 end 613 note some word of inp[p .. e) is exit, write or read, or names a function that is not pure 614 fn toksff p ulong e ulong to bool 615 q = p 616 while lt q e bound 65536 617 firstw q e 618 o = get wpos 0 619 n = get wpos 1 620 if eq n 0 621 ret false 622 end 623 if or iskw o n 14 or iskw o n 15 iskw o n 16 624 ret true 625 end 626 if impure nfind o n 627 ret true 628 end 629 q = add o n 630 end 631 ret false 632 end 633 fn fneff k ulong to bool 634 a = add get nd k 1 635 b = get nend k 636 while lt a b bound 65536 637 if eq get kp a 1 638 if lineeff a 639 ret true 640 end 641 end 642 a = add a 1 643 end 644 ret false 645 end 646 note drop the stores to sinks, find the pure functions, drop every call statement of one; lines removed 647 fn pure to ulong 648 buildnames 649 before = get nrm 0 650 for i ulong get nl 0 bound 65536 651 if eq get kp i 1 652 if sinkput i 653 drop i 654 end 655 end 656 end 657 purity 658 for i ulong get nl 0 bound 65536 659 if eq get kp i 1 660 purecall i 661 end 662 end 663 sinkfns 664 for k ulong get nn 0 665 if eq get nk k 1 666 taintdrop k 667 end 668 end 669 ret add sub get nrm 0 before 1 670 end 671 note npu: 1 for every function with no effect (to a fixpoint over the calls) 672 fn purity to void 673 for k ulong get nn 0 674 put npu k 0 675 if eq get nk k 1 676 put npu k 1 677 end 678 end 679 go = true 680 while go bound 100000 681 go = false 682 for k ulong get nn 0 683 if ne get npu k 0 684 if fneff k 685 put npu k 0 686 go = true 687 end 688 end 689 end 690 end 691 end 692 note tidy: drop if/while/for blocks left with nothing kept inside, and assignments to locals that no kept 693 note line reads, when they call nothing impure; to a fixpoint; lines removed 694 fn tidy to ulong 695 buildnames 696 purity 697 before = get nrm 0 698 go = true 699 while go bound 1000 700 go = false 701 for k ulong get nn 0 702 if eq get nk k 1 703 if tidyfn k 704 go = true 705 end 706 end 707 end 708 end 709 ret add sub get nrm 0 before 1 710 end 711 fn tidyfn k ulong to bool 712 ch = false 713 a = add get nd k 1 714 b = get nend k 715 while lt a b bound 65536 716 if eq get kp a 1 717 if tidy1 a k 718 ch = true 719 end 720 end 721 a = add a 1 722 end 723 ret ch 724 end 725 fn tidy1 a ulong k ulong to bool 726 if eq get lk a 1 727 ret emptyblk a 728 end 729 r = asgrhs a 730 if eq r 0 731 ret false 732 end 733 if toksff r get lend a 734 ret false 735 end 736 if readin get lh 0 get lh 1 k 737 ret false 738 end 739 drop a 740 ret true 741 end 742 note the if/while/for block opened at kept line a has no kept line inside but else and notes, and its 743 note condition calls nothing impure: drop it whole 744 fn emptyblk a ulong to bool 745 firstw get lo a get lend a 746 o = get wpos 0 747 n = get wpos 1 748 if not or iskw o n 1 or iskw o n 2 iskw o n 3 749 ret false 750 end 751 if toksff add o n get lend a 752 ret false 753 end 754 if not matchend a 755 ret false 756 end 757 en = get me 1 758 x = add a 1 759 while lt x en bound 65536 760 if eq get kp x 1 761 if ne get lk x 2 762 if not notel x 763 ret false 764 end 765 end 766 end 767 x = add x 1 768 end 769 dropr a en 770 ret true 771 end 772 fn notel x ulong to bool 773 firstw get lo x get lend x 774 ret iskw get wpos 0 get wpos 1 7 775 end 776 note some kept line of function k reads the local inp[o .. o+n): a word of it past the target of an 777 note assignment 778 fn readin o ulong n ulong k ulong to bool 779 x = get nd k 780 e = get nend k 781 while le x e bound 65536 782 if eq get kp x 1 783 if rdname x o n 784 ret true 785 end 786 end 787 x = add x 1 788 end 789 ret false 790 end 791 fn rdname x ulong o ulong n ulong to bool 792 q = asgrhs x 793 if eq q 0 794 q = get lo x 795 end 796 e = get lend x 797 while lt q e bound 65536 798 firstw q e 799 p = get wpos 0 800 m = get wpos 1 801 if eq m 0 802 ret false 803 end 804 if eq m n 805 if inpsame p o n 806 ret true 807 end 808 end 809 q = add p m 810 end 811 ret false 812 end 813 note mark every function that returns a value read from a sink as a sink itself, to a fixpoint 814 fn sinkfns to void 815 go = true 816 while go bound 64 817 go = false 818 for k ulong get nn 0 819 if sinknew k 820 put nsk k 1 821 go = true 822 end 823 end 824 end 825 end 826 fn sinknew k ulong to bool 827 if or ne get nk k 1 ne get nsk k 0 828 ret false 829 end 830 taintfn k 831 a = add get nd k 1 832 b = get nend k 833 while lt a b bound 65536 834 if retsink a 835 ret true 836 end 837 a = add a 1 838 end 839 ret false 840 end 841 note kept line a is `ret EXPR` with EXPR reading a sink 842 fn retsink a ulong to bool 843 if eq get kp a 0 844 ret false 845 end 846 firstw get lo a get lend a 847 if not iskw get wpos 0 get wpos 1 21 848 ret false 849 end 850 ret rdsink add get wpos 0 3 get lend a 851 end 852 fn purecall i ulong to void 853 firstw get lo i get lend i 854 v = nfind get wpos 0 get wpos 1 855 if ne v 0 856 if and eq get nk sub v 1 1 ne get npu sub v 1 0 857 drop i 858 end 859 end 860 end 861 note every kept line of a function that reads a sink global or a local assigned from one goes to stderr; 862 note their count (the script refuses when not 0) 863 fn sinkchk to ulong 864 buildnames 865 sinkfns 866 c = ulong 0 867 for k ulong get nn 0 868 if eq get nk k 1 869 if ne get nsk k 0 870 say 13 871 r = write 2 inp get no k get nln k 872 nl2 873 end 874 c = add c sinkfn k 875 end 876 end 877 ret c 878 end 879 fn sinkfn k ulong to ulong 880 taintfn k 881 c = ulong 0 882 a = add get nd k 1 883 b = get nend k 884 while lt a b bound 65536 885 if eq get kp a 1 886 if sinkread a 887 c = add c 1 888 say 11 889 num add a 1 890 say 12 891 showline a 892 nl2 893 end 894 end 895 a = add a 1 896 end 897 ret c 898 end 899 fn showline i ulong to void 900 s = get lo i 901 r = write 2 inp s sub get lend i s 902 end 903 fn sinkread i ulong to bool 904 e = get lend i 905 firstw get lo i e 906 if iskw get wpos 0 get wpos 1 7 907 ret false 908 end 909 ret rdsink get lo i e 910 end 911 fn inpsame a ulong o ulong n ulong to bool 912 for j ulong n 913 if ne get inp add a j get inp add o j 914 ret false 915 end 916 end 917 ret true 918 end 919 note inp[o .. o+n) is a tainted local of the current function 920 fn tainted o ulong n ulong to bool 921 for t ulong get ntn 0 922 if eq get tnl t n 923 if inpsame get tno t o n 924 ret true 925 end 926 end 927 end 928 ret false 929 end 930 fn issink v ulong to bool 931 if eq v 0 932 ret false 933 end 934 ret ne get nsk sub v 1 0 935 end 936 note inp[p .. e) has a word that is a sink global or function, or (unless tnot) a tainted local 937 fn rdsink p ulong e ulong to bool 938 q = p 939 while lt q e bound 65536 940 firstw q e 941 o = get wpos 0 942 n = get wpos 1 943 if eq n 0 944 ret false 945 end 946 if issink nfind o n 947 ret true 948 end 949 if eq get tnot 0 0 950 if tainted o n 951 ret true 952 end 953 end 954 q = add o n 955 end 956 ret false 957 end 958 note line i is `NAME = ...` or `put NAME ...` with NAME not a fn or global: NAME into lh and the offset of 959 note the rest of the line, or 0 960 fn asgrhs i ulong to ulong 961 e = get lend i 962 firstw get lo i e 963 o = get wpos 0 964 n = get wpos 1 965 if eq n 0 966 ret 0 967 end 968 isput = iskw o n 13 969 firstw add o n e 970 p = get wpos 0 971 m = get wpos 1 972 if isput 973 if or eq m 0 ne nfind p m 0 974 ret 0 975 end 976 put lh 0 p 977 put lh 1 m 978 ret add p m 979 end 980 if or ne m 1 ne get inp p 61 981 ret 0 982 end 983 put lh 0 o 984 put lh 1 n 985 ret add p 1 986 end 987 note taint the locals of function k assigned from a sink or a tainted local (to a fixpoint) 988 fn taintfn k ulong to void 989 put ntn 0 0 990 go = true 991 while go bound 64 992 go = false 993 a = add get nd k 1 994 b = get nend k 995 while lt a b bound 65536 996 if taint1 a 997 go = true 998 end 999 a = add a 1 1000 end 1001 end 1002 end 1003 fn taint1 a ulong to bool 1004 if and eq get kp a 0 eq get td a 0 1005 ret false 1006 end 1007 r = asgrhs a 1008 if eq r 0 1009 ret false 1010 end 1011 o = get lh 0 1012 n = get lh 1 1013 if tainted o n 1014 ret false 1015 end 1016 if not rdsink r get lend a 1017 ret false 1018 end 1019 t = get ntn 0 1020 if ge t len tno 1021 ret false 1022 end 1023 put tno t o 1024 put tnl t n 1025 put ntn 0 add t 1 1026 ret true 1027 end 1028 note drop the assignments to tainted locals of function k whose right-hand side reads a sink global or 1029 note function directly and calls nothing impure (a use of the dropped local then fails to compile: by hand) 1030 fn taintdrop k ulong to void 1031 taintfn k 1032 a = add get nd k 1 1033 b = get nend k 1034 while lt a b bound 65536 1035 if eq get kp a 1 1036 tdrop1 a 1037 end 1038 a = add a 1 1039 end 1040 end 1041 fn tdrop1 a ulong to void 1042 r = asgrhs a 1043 if eq r 0 1044 ret 1045 end 1046 put tnot 0 1 1047 s = rdsink r get lend a 1048 put tnot 0 0 1049 if not s 1050 ret 1051 end 1052 if not toksff r get lend a 1053 drop a 1054 put td a 1 1055 end 1056 end 1057 fn dead o ulong n ulong to ulong 1058 buildnames 1059 root = findroot o n 1060 if eq root 0 1061 say 6 1062 nl2 1063 exit 1 1064 end 1065 q = live sub root 1 0 1066 h = ulong 0 1067 while lt h q bound 8192 1068 k = get wk h 1069 if eq get nk k 1 1070 q = scanfn k q 1071 end 1072 h = add h 1 1073 end 1074 before = get nrm 0 1075 for k ulong get nn 0 1076 if eq get nlv k 0 1077 dropname k 1078 end 1079 end 1080 ret sub get nrm 0 before 1081 end 1082 fn rootis k ulong o ulong n ulong to bool 1083 a = get no k 1084 for j ulong n 1085 if ne get inp add a j get cm add o j 1086 ret false 1087 end 1088 end 1089 ret true 1090 end 1091 fn scanfn k ulong qn ulong to ulong 1092 q = qn 1093 a = add get nd k 1 1094 b = get nend k 1095 while lt a b bound 65536 1096 if eq get kp a 1 1097 q = scanline a q 1098 end 1099 a = add a 1 1100 end 1101 ret q 1102 end 1103 note drop entry k's declaration (a fn through its end) and the note lines right above it 1104 fn dropname k ulong to void 1105 a = get nd k 1106 dropr a get nend k 1107 go = true 1108 while and go gt a 0 bound 65536 1109 a = sub a 1 1110 firstw get lo a get lend a 1111 if and eq get kp a 1 iskw get wpos 0 get wpos 1 7 1112 drop a 1113 else 1114 go = false 1115 end 1116 end 1117 end 1118 1119 note ---- running the script and the output ------------------------------------------------------------------- 1120 fn cmblank i ulong to bool 1121 c = get cm i 1122 ret or eq c 32 eq c 9 1123 end 1124 fn runcm to bool 1125 n = get cmn 0 1126 p = ulong 0 1127 while lt p n bound `1048576` 1128 e = p 1129 while and lt e n ne get cm e 10 bound `1048576` 1130 e = add e 1 1131 end 1132 if and lt p e ne get cm p 35 1133 if not cmline p e 1134 ret false 1135 end 1136 end 1137 p = add e 1 1138 end 1139 ret true 1140 end 1141 fn cmline p ulong e ulong to bool 1142 j = p 1143 while and lt j e not cmblank j bound 4096 1144 j = add j 1 1145 end 1146 a = e 1147 if lt j e 1148 a = add j 1 1149 end 1150 n = sub e a 1151 put nrm 0 0 1152 c = ulong 0 1153 if cmkw p sub j p 9 1154 c = branch a n false 1155 end 1156 if cmkw p sub j p 10 1157 c = branch a n true 1158 end 1159 if cmkw p sub j p 11 1160 c = calls a n 1161 end 1162 if cmkw p sub j p 12 1163 c = add dead a n 1 1164 end 1165 if cmkw p sub j p 17 1166 c = sinkadd a n 1167 end 1168 if cmkw p sub j p 18 1169 c = pure 1170 end 1171 if cmkw p sub j p 20 1172 c = tidy 1173 end 1174 if cmkw p sub j p 19 1175 if ne sinkchk 0 1176 ret false 1177 end 1178 c = ulong 1 1179 end 1180 r = write 2 cm p sub e p 1181 say 9 1182 num get nrm 0 1183 nl2 1184 if eq c 0 1185 say 5 1186 r = write 2 cm p sub e p 1187 nl2 1188 ret false 1189 end 1190 ret true 1191 end 1192 fn obyte b uchar to void 1193 n = get otn 0 1194 if lt n len out 1195 put out n b 1196 end 1197 put otn 0 add n 1 1198 end 1199 fn emitout to bool 1200 for i ulong get nl 0 bound 65536 1201 if eq get kp i 1 1202 s = get lo i 1203 for j ulong sub get lend i s bound 65536 1204 obyte get inp add s j 1205 end 1206 obyte 10 1207 end 1208 end 1209 n = get otn 0 1210 if gt n len out 1211 ret false 1212 end 1213 off = ulong 0 1214 while lt off n bound 100000 1215 r = write 1 out off sub n off 1216 if le r 0 1217 ret false 1218 end 1219 off = add off ulong r 1220 end 1221 ret true 1222 end 1223 fn main to int 1224 if ne argcnt 2 1225 say 1 1226 nl2 1227 ret 1 1228 end 1229 if not loadcm 1230 say 2 1231 nl2 1232 ret 1 1233 end 1234 if not loadinp 1235 say 3 1236 nl2 1237 ret 1 1238 end 1239 if not runcm 1240 ret 1 1241 end 1242 if not emitout 1243 say 7 1244 nl2 1245 ret 1 1246 end 1247 ret 0 1248 end