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/ngrand.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 / ngrand.om` 1208 lines · 22775 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note seed/ng/ngrand.om: seeded random ΩIR programs, valid by construction: the differential source of D9 3 note (a back end against ngrun). Lane ng-ir. Build: 4 note cat seed/ng/src/ir.om seed/ng/ngrand.om | seed/out/omc > ngrand 5 note Usage: 6 note ngrand SEED the program of SEED (decimal, < 2^64) as an ΩIR binary on stdout 7 note ngrand tape FILE the program of a choice tape (at most 65536 bytes) 8 note ngrand shrink SEED STATUS OUT CMD [ARG...] 9 note shrink the tape of SEED while `CMD ARG... OUT` exits STATUS: OUT ends as the 10 note smallest program found and OUT.tape as its tape; one summary line on stdout; 11 note the predicate runs at most 20001 times (20000 while searching, then once more 12 note on the result) 13 note Every choice is one byte read from the tape; a read past its end gives 0, and 0 is always the 14 note smallest choice (end the block, a constant, no check, no helper). SEED fills a 65536-byte tape with 15 note splitmix64. Any tape gives a unit ngcheck accepts, so deleting or zeroing tape bytes shrinks the 16 note program and keeps it valid; the program is built with src/ir.om and written by its writer. 17 note The program: global @g0 array i64 16 and @g1 array u8 8 (both zero); @f0 (i64) prints its argument as 8 18 note little-endian bytes on stdout (host write); helpers @f1..@fH (H 0..3; 0..3 parameters and a result 19 note of the types i64 i32 u64 u32 i8 u8) call only lower helpers; the export (last) prints the xor of two 20 note i64 values through @f0 and returns its low 6 bits. Statements: values (const; add sub mul with 21 note chk.add/sub/mul; div rem with chk.divz/divovf; and or xor min max; shl shr rotl rotr with 22 note chk.shift/chk.shl; neg with chk.neg, not; conv with chk.conv; bitcast; select; compare), store and 23 note load of i64 under chk.bounds (the index masked to the array, or 1 time in 8 to twice its length, 24 note which can trap 3) on @g0 or on a local alloca, calls, if/else diamonds joined by a block parameter 25 note (the else arm may end in trap, exit 64..95 or an early return), counted loops (0..4 passes, 26 note chk.fuel 4 with bound `1000000`, the counter and an accumulator as header parameters) and chk.true 27 note with a random code. Nesting depth <= 2. A static cost (ops executed, callee costs included) caps a 28 note helper at 4000 and the export at 40000, so every program ends: exit 0..63 after printing 8 bytes or 29 note on an early return, 64..95 by exit, 101..114 by a check or trap. 30 note Exit: 0; usage 2; shrink: SEED not interesting, or the result not interesting again, 3; IO 27. 31 note Globals y_*, functions rg_*. 32 text y_use "usage: ngrand SEED | ngrand tape FILE | ngrand shrink SEED STATUS OUT CMD [ARG...]\n" 33 text y_wt "tape" 34 text y_ws "shrink" 35 text y_dn "/dev/null" 36 text y_tx ".tape" 37 text y_io "ngrand: io error\n" 38 text y_ni "ngrand: not interesting\n" 39 text y_t1 "ngrand: shrink tape " 40 text y_t2 " -> " 41 text y_t3 " bytes, ops " 42 text y_t4 ", tries " 43 text y_nl "\n" 44 global y_tp uchar 65536 45 global y_bk uchar 65536 46 global y_s ulong 32 47 global y_h uint 16 48 global y_pv uint 65536 49 global y_pt uint 65536 50 global y_fc ulong 64 51 global y_ar uint 64 52 global y_pa uchar 4096 53 global y_op uchar 4096 54 global y_av uchar 65536 55 global y_stb uchar 4 56 global y_dg uchar 32 57 note y_s: 0 tape length, 1 cursor, 5 pool count, 6 depth, 7 cost multiplier, 8 cost, 12 element count of 58 note the last REF, 13 tries, 14 wanted status, 15 argv bytes, 16 argc, 17 OUT length, 19 cost cap 59 note y_h: 0 function, 1 current token, 2 current block, 3 local REF value (0 none), 4 @g0, 5 @g1, 6 REF 60 note to @g0's type, 7 REF to the local array, 8 REF to @g1's type, 9 unused, 10 @f0, 11 the export 61 note ---- choices ----------------------------------------------------------------------------------- 62 fn rg_b to ulong 63 c = get y_s 1 64 put y_s 1 add c 1 65 if lt c get y_s 0 66 ret ulong get y_tp c 67 end 68 ret 0 69 end 70 fn rg_ch n ulong to ulong 71 ret rem rg_b n 72 end 73 fn rg_seed s ulong to void 74 x = s 75 for j ulong 8192 76 x = add x `11400714819323198485` 77 z = x 78 z = mul xor z shr z 30 `13787848793156543929` 79 z = mul xor z shr z 27 `10723151780598845931` 80 z = xor z shr z 31 81 for k ulong 8 82 put y_tp add mul j 8 k uchar and shr z mul k 8 255 83 end 84 end 85 put y_s 0 65536 86 end 87 note ---- types: the value types i64 i32 u64 u32 i8 u8 ------------------------------------------------- 88 fn rg_ity k ulong to uint 89 j = rem k 6 90 if eq j 0 91 ret 9 92 end 93 if eq j 1 94 ret 8 95 end 96 if eq j 2 97 ret 5 98 end 99 if eq j 3 100 ret 4 101 end 102 if eq j 4 103 ret 6 104 end 105 ret 2 106 end 107 fn rg_sgn t uint to bool 108 ret and ge t 6 le t 9 109 end 110 fn rg_wid t uint to ulong 111 if or eq t 2 eq t 6 112 ret 8 113 end 114 if or eq t 3 eq t 7 115 ret 16 116 end 117 if or eq t 4 eq t 8 118 ret 32 119 end 120 ret 64 121 end 122 note the type of equal width and the other signedness 123 fn rg_twin t uint to uint 124 if ge t 6 125 ret sub t 4 126 end 127 ret add t 4 128 end 129 note canonical const imm of v for type t (IR.md s7.2) 130 fn rg_can t uint v ulong to ulong 131 if eq t 1 132 ret and v 1 133 end 134 w = rg_wid t 135 if eq w 64 136 ret v 137 end 138 m = sub shl ulong 1 w 1 139 x = and v m 140 if rg_sgn t 141 if ne and x shl ulong 1 sub w 1 0 142 ret or x xor m `18446744073709551615` 143 end 144 end 145 ret x 146 end 147 note a constant: -100..99 (signed) or 0..199 (unsigned) for bytes below 200, else 8 more bytes 148 fn rg_kv t uint to ulong 149 b = rg_b 150 if lt b 200 151 if rg_sgn t 152 ret rg_can t sub b 100 153 end 154 ret rg_can t b 155 end 156 v = ulong 0 157 for j ulong 8 158 v = or shl v 8 rg_b 159 end 160 ret rg_can t v 161 end 162 note ---- emitting ops into the current block --------------------------------------------------------- 163 fn rg_cost to void 164 put y_s 8 add get y_s 8 get y_s 7 165 end 166 note an op without a token 167 fn rg_p op uint ty uint imm ulong n uint to uint 168 rg_cost 169 ret ir_op get y_h 2 op ty imm n 170 end 171 note an effect op (IR.md s6.4): it consumes the current token and its new token becomes current 172 fn rg_e op uint ty uint imm ulong n uint to uint 173 rg_cost 174 o = ir_op get y_h 2 op ty imm n 175 ir_set o 0 get y_h 1 176 put y_h 1 ir_otok o 177 ret o 178 end 179 note a terminator or reader with the current token as operand 0 180 fn rg_t op uint imm ulong n uint to uint 181 rg_cost 182 o = ir_op get y_h 2 op 0 imm n 183 ir_set o 0 get y_h 1 184 ret o 185 end 186 fn rg_k t uint v ulong to uint 187 o = rg_p 1 t v 0 188 ret ir_res o 189 end 190 fn rg_op2 op uint t uint a uint b uint to uint 191 o = rg_p op t 0 2 192 ir_set o 0 a 193 ir_set o 1 b 194 ret ir_res o 195 end 196 fn rg_op1 op uint t uint a uint to uint 197 o = rg_p op t 0 1 198 ir_set o 0 a 199 ret ir_res o 200 end 201 note a check of the random statements with one (n 1) or two (n 2) operands and trap code c; its own 202 note token step is the site of the gate's mutant (the check's new token not made current) 203 fn rg_chk op uint c ulong a uint b uint n uint to void 204 rg_cost 205 q = ir_op get y_h 2 op 0 c add n 1 206 ir_set q 0 get y_h 1 207 put y_h 1 ir_otok q 208 ir_set q 1 a 209 if eq n 2 210 ir_set q 2 b 211 end 212 end 213 fn rg_go b uint to void 214 put y_h 2 b 215 put y_h 1 ir_btok b 216 end 217 fn rg_cbr c uint bt uint bf uint to void 218 o = rg_t 61 0 4 219 ir_set o 1 c 220 ir_set o 2 bt 221 ir_set o 3 bf 222 end 223 fn rg_br1 b uint v uint to void 224 rg_cost 225 o = ir_op get y_h 2 60 0 0 3 226 ir_set o 0 b 227 ir_set o 1 get y_h 1 228 ir_set o 2 v 229 end 230 fn rg_br2 b uint v uint w uint to void 231 rg_cost 232 o = ir_op get y_h 2 60 0 0 4 233 ir_set o 0 b 234 ir_set o 1 get y_h 1 235 ir_set o 2 v 236 ir_set o 3 w 237 end 238 note ---- the pool of values visible at the current point -------------------------------------------- 239 fn rg_add v uint t uint to void 240 n = get y_s 5 241 if lt n 65536 242 put y_pv n v 243 put y_pt n t 244 put y_s 5 add n 1 245 end 246 end 247 fn rg_cnt t uint to ulong 248 c = ulong 0 249 for j ulong get y_s 5 bound 65536 250 if eq get y_pt j t 251 c = add c 1 252 end 253 end 254 ret c 255 end 256 fn rg_nth t uint k ulong to uint 257 c = ulong 0 258 for j ulong get y_s 5 bound 65536 259 if eq get y_pt j t 260 if eq c k 261 ret get y_pv j 262 end 263 c = add c 1 264 end 265 end 266 ret 0 267 end 268 fn rg_kc t uint to uint 269 v = rg_k t rg_kv t 270 rg_add v t 271 ret v 272 end 273 note a visible value of type t, or 1 time in 8 (and when there is none) a new constant 274 fn rg_pick t uint to uint 275 c = rg_cnt t 276 if eq c 0 277 ret rg_kc t 278 end 279 b = rg_b 280 if lt b 32 281 ret rg_kc t 282 end 283 ret rg_nth t rem b c 284 end 285 fn rg_bool to uint 286 c = rg_cnt 1 287 if gt c 0 288 b = rg_b 289 if ge b 128 290 ret rg_nth 1 rem b c 291 end 292 end 293 t = rg_ity rg_b 294 a = rg_pick t 295 b2 = rg_pick t 296 op = uint add 30 rg_ch 6 297 v = rg_op2 op 1 a b2 298 rg_add v 1 299 ret v 300 end 301 note ---- value statements -------------------------------------------------------------------------- 302 fn rg_val to void 303 t = rg_ity rg_b 304 k = rg_ch 10 305 if eq k 0 306 x = rg_kc t 307 ret 308 end 309 if eq k 8 310 y = rg_bool 311 ret 312 end 313 v = rg_vx k t 314 rg_add v t 315 end 316 fn rg_vx k ulong t uint to uint 317 if eq k 1 318 ret rg_ari t 319 end 320 if eq k 2 321 ret rg_div t 322 end 323 if eq k 3 324 ret rg_bit t 325 end 326 if eq k 4 327 ret rg_sh t 328 end 329 if eq k 5 330 ret rg_neg t 331 end 332 if eq k 6 333 ret rg_cv t 334 end 335 if eq k 7 336 ret rg_sel t 337 end 338 ret rg_bc t 339 end 340 fn rg_ari t uint to uint 341 a = rg_pick t 342 b = rg_pick t 343 j = rg_ch 3 344 if eq rg_ch 4 0 345 rg_chk uint add 70 j 1 a b 2 346 end 347 ret rg_op2 uint add 10 j t a b 348 end 349 fn rg_div t uint to uint 350 a = rg_pick t 351 b = rg_pick t 352 op = uint add 13 rg_ch 2 353 if eq rg_ch 2 0 354 rg_chk 74 2 b 0 1 355 end 356 if and eq op 13 rg_sgn t 357 if eq rg_ch 2 0 358 rg_chk 75 1 a b 2 359 end 360 end 361 ret rg_op2 op t a b 362 end 363 fn rg_bit t uint to uint 364 a = rg_pick t 365 b = rg_pick t 366 j = rg_ch 5 367 op = uint 16 368 if eq j 1 369 op = 17 370 end 371 if eq j 2 372 op = 18 373 end 374 if eq j 3 375 op = 24 376 end 377 if eq j 4 378 op = 25 379 end 380 ret rg_op2 op t a b 381 end 382 fn rg_sh t uint to uint 383 a = rg_pick t 384 b = uint 0 385 if eq rg_ch 2 0 386 b = rg_k t rg_ch rg_wid t 387 else 388 b = rg_pick t 389 end 390 op = uint add 20 rg_ch 2 391 if not rg_sgn t 392 op = uint add 20 rg_ch 4 393 end 394 if eq rg_ch 3 0 395 rg_chk 77 7 b 0 1 396 if and eq op 20 rg_sgn t 397 rg_chk 76 1 a b 2 398 end 399 end 400 ret rg_op2 op t a b 401 end 402 fn rg_neg t uint to uint 403 a = rg_pick t 404 if eq rg_ch 2 0 405 ret rg_op1 19 t a 406 end 407 if eq rg_ch 3 0 408 rg_chk 73 1 a 0 1 409 end 410 ret rg_op1 15 t a 411 end 412 fn rg_cv t uint to uint 413 s = rg_ity rg_b 414 if eq s t 415 s = rg_twin t 416 end 417 a = rg_pick s 418 if eq rg_ch 3 0 419 o = rg_e 78 t 5 2 420 ir_set o 1 a 421 end 422 ret rg_op1 36 t a 423 end 424 fn rg_sel t uint to uint 425 c = rg_bool 426 a = rg_pick t 427 b = rg_pick t 428 o = rg_p 2 t 0 3 429 ir_set o 0 c 430 ir_set o 1 a 431 ir_set o 2 b 432 ret ir_res o 433 end 434 fn rg_bc t uint to uint 435 a = rg_pick rg_twin t 436 ret rg_op1 37 t a 437 end 438 note ---- memory: i64 elements of @g0 (16) or of the local alloca (8) --------------------------------- 439 fn rg_ref to uint 440 l = get y_h 3 441 if ne l 0 442 if eq rg_ch 2 1 443 put y_s 12 8 444 ret l 445 end 446 end 447 put y_s 12 16 448 o = rg_p 52 get y_h 6 ulong get y_h 4 0 449 ret ir_res o 450 end 451 fn rg_idx to uint 452 s = rg_ity rg_b 453 v = rg_pick s 454 if ne s 5 455 v = rg_op1 36 5 v 456 end 457 m = sub get y_s 12 1 458 if eq rg_ch 8 7 459 m = add m get y_s 12 460 end 461 k = rg_k 5 m 462 ret rg_op2 16 5 v k 463 end 464 fn rg_bnd r uint i uint to void 465 e = rg_k 5 1 466 o = rg_e 79 0 3 4 467 ir_set o 1 r 468 ir_set o 2 i 469 ir_set o 3 e 470 end 471 fn rg_st to void 472 r = rg_ref 473 i = rg_idx 474 v = rg_pick 9 475 rg_bnd r i 476 o = rg_e 43 0 0 4 477 ir_set o 1 r 478 ir_set o 2 i 479 ir_set o 3 v 480 end 481 fn rg_ld to void 482 r = rg_ref 483 i = rg_idx 484 rg_bnd r i 485 rg_cost 486 o = ir_op get y_h 2 42 9 0 3 487 ir_set o 0 get y_h 1 488 ir_set o 1 r 489 ir_set o 2 i 490 rg_add ir_res o 9 491 end 492 note ---- calls: a helper below the current function, within the cost cap ----------------------------- 493 fn rg_call to void 494 f = get y_h 0 495 if le f 2 496 rg_val 497 ret 498 end 499 c = uint add 2 rg_ch ulong sub f 2 500 w = mul get y_s 7 get y_fc c 501 if gt add get y_s 8 w get y_s 19 502 rg_val 503 ret 504 end 505 np = uint 0 506 v = get i_fp1 c 507 while ne v 0 bound 65 508 put y_ar np rg_pick get i_vty v 509 np = add np 1 510 v = get i_vnx v 511 end 512 o = rg_e 64 get i_frs c 0 add np 2 513 ir_set o 1 c 514 for j ulong ulong np bound 64 515 ir_set o uint add j 2 get y_ar j 516 end 517 put y_s 8 add get y_s 8 w 518 rg_add ir_res o get i_frs c 519 end 520 fn rg_true to void 521 c = rg_bool 522 rg_chk 82 add 1 rg_ch 14 c 0 1 523 end 524 note ---- control: diamonds and counted loops -------------------------------------------------------- 525 fn rg_if to void 526 c = rg_bool 527 t = rg_ity rg_b 528 f = get y_h 0 529 ba = ir_blk f 530 bb = ir_blk f 531 bj = ir_blk f 532 jv = ir_bpar bj t 533 rg_cbr c ba bb 534 mk = get y_s 5 535 put y_s 6 add get y_s 6 1 536 rg_go ba 537 rg_stmts 4 538 rg_br1 bj rg_pick t 539 put y_s 5 mk 540 rg_go bb 541 rg_stmts 4 542 if eq rg_ch 6 5 543 rg_endx 544 else 545 rg_br1 bj rg_pick t 546 end 547 put y_s 5 mk 548 put y_s 6 sub get y_s 6 1 549 rg_go bj 550 rg_add jv t 551 end 552 note the else arm's other ends: trap, exit 64..95, an early return 553 fn rg_endx to void 554 k = rg_ch 3 555 if eq k 0 556 o = rg_t 65 add 1 rg_ch 14 1 557 ret 558 end 559 if eq k 1 560 v = rg_k 8 add 64 rg_ch 32 561 o2 = rg_t 66 0 2 562 ir_set o2 1 v 563 ret 564 end 565 rg_ret 566 end 567 note return from the current function: the export returns a constant 0..63, a helper a visible value 568 fn rg_ret to void 569 f = get y_h 0 570 v = uint 0 571 if eq f get y_h 11 572 v = rg_k 8 rg_ch 64 573 else 574 v = rg_pick get i_frs f 575 end 576 o = rg_t 63 0 2 577 ir_set o 1 v 578 end 579 fn rg_loop to void 580 t = rg_ity rg_b 581 n = rg_ch 5 582 a0 = rg_pick t 583 k0 = rg_k 5 0 584 h = ir_blk get y_h 0 585 kp = ir_bpar h 5 586 ap = ir_bpar h t 587 rg_br2 h k0 a0 588 rg_go h 589 bd = rg_lhd kp n 590 ex = get y_h 9 591 ml = get y_s 7 592 put y_s 7 mul ml add n 1 593 rg_lbody bd h kp ap t 594 put y_s 7 ml 595 rg_go ex 596 rg_add kp 5 597 rg_add ap t 598 end 599 note the loop head: k < n, to the body or the exit (its handle in y_h 9) 600 fn rg_lhd kp uint n ulong to uint 601 kn = rg_k 5 n 602 c = rg_op2 32 1 kp kn 603 f = get y_h 0 604 bd = ir_blk f 605 ex = ir_blk f 606 rg_cbr c bd ex 607 put y_h 9 ex 608 ret bd 609 end 610 fn rg_lbody bd uint h uint kp uint ap uint t uint to void 611 mk = get y_s 5 612 put y_s 6 add get y_s 6 1 613 rg_add kp 5 614 rg_add ap t 615 rg_go bd 616 km = rg_k 5 `1000000` 617 rg_chk 81 4 kp km 2 618 rg_stmts 4 619 rg_lend h kp ap t 620 put y_s 5 mk 621 put y_s 6 sub get y_s 6 1 622 end 623 note the back edge: the accumulator combined with a visible value, the counter + 1 624 fn rg_lend h uint kp uint ap uint t uint to void 625 v = rg_pick t 626 j = rg_ch 3 627 op = uint 10 628 if eq j 1 629 op = 18 630 end 631 if eq j 2 632 op = 11 633 end 634 a2 = rg_op2 op t ap v 635 k1 = rg_k 5 1 636 kq = rg_op2 10 5 kp k1 637 rg_br2 h kq a2 638 end 639 note ---- statements ------------------------------------------------------------------------------- 640 fn rg_stmts m ulong to void 641 i = ulong 0 642 while lt i m bound 64 643 if lt rg_b 40 644 ret 645 end 646 if gt get y_s 8 get y_s 19 647 ret 648 end 649 rg_stmt 650 i = add i 1 651 end 652 end 653 fn rg_stmt to void 654 k = rg_ch 16 655 d = get y_s 6 656 if lt k 6 657 rg_val 658 ret 659 end 660 if lt k 8 661 rg_st 662 ret 663 end 664 if eq k 8 665 rg_ld 666 ret 667 end 668 if lt k 11 669 rg_call 670 ret 671 end 672 if and lt k 13 lt d 2 673 rg_if 674 ret 675 end 676 if and lt k 15 lt d 2 677 rg_loop 678 ret 679 end 680 if eq k 15 681 if eq rg_ch 4 0 682 rg_true 683 ret 684 end 685 end 686 rg_val 687 end 688 note ---- functions ------------------------------------------------------------------------------- 689 fn rg_fn kind uint res uint np ulong to uint 690 f = ir_fnew kind 0 res 691 put y_h 0 f 692 put y_s 5 0 693 for j ulong np bound 4 694 p = ir_fpar f rg_ity rg_b 695 rg_add p get i_vty p 696 end 697 b = ir_blk f 698 put y_h 2 b 699 put y_h 1 ir_ftok f 700 put y_s 6 0 701 put y_s 7 1 702 put y_s 8 0 703 put y_h 3 0 704 if eq rg_ch 2 1 705 o = rg_e 50 get y_h 7 0 1 706 put y_h 3 ir_res o 707 end 708 ret f 709 end 710 fn rg_help to void 711 res = rg_ity rg_b 712 np = rg_ch 4 713 f = rg_fn 0 res np 714 put y_s 19 4000 715 rg_stmts 10 716 rg_ret 717 put y_fc f get y_s 8 718 end 719 fn rg_main to void 720 f = rg_fn 2 8 0 721 put y_h 11 f 722 put y_s 19 40000 723 rg_stmts 14 724 x = rg_fold 725 o = rg_e 64 0 0 3 726 ir_set o 1 get y_h 10 727 ir_set o 2 x 728 m = rg_k 9 63 729 r = rg_op2 16 9 x m 730 v = rg_op1 36 8 r 731 o2 = rg_t 63 0 2 732 ir_set o2 1 v 733 end 734 note x = x * 31 + v over the last 48 visible int values v (as i64), from 0 735 fn rg_fold to uint 736 x = rg_k 9 0 737 k = rg_k 9 31 738 n = get y_s 5 739 s = ulong 0 740 if gt n 48 741 s = sub n 48 742 end 743 for j ulong sub n s bound 65536 744 v = get y_pv add s j 745 t = get y_pt add s j 746 if ne t 1 747 if ne t 9 748 v = rg_op1 36 9 v 749 end 750 m = rg_op2 12 9 x k 751 x = rg_op2 10 9 m v 752 end 753 end 754 ret x 755 end 756 note @f0 (i64) -> void: 8 bytes of its argument into @g1, then host write 1 @g1 0 8 757 fn rg_prt to void 758 f = ir_fnew 0 0 0 759 put y_h 10 f 760 put y_h 0 f 761 p = ir_fpar f 9 762 b = ir_blk f 763 put y_h 2 b 764 put y_h 1 ir_ftok f 765 put y_s 7 1 766 o = rg_p 52 get y_h 8 ulong get y_h 5 0 767 g = ir_res o 768 for k ulong 8 769 rg_pb g p k 770 end 771 z = rg_k 5 0 772 n = rg_k 5 8 773 o2 = rg_e 80 0 3 4 774 ir_set o2 1 g 775 ir_set o2 2 z 776 ir_set o2 3 n 777 fd = rg_k 8 1 778 o3 = rg_e 110 9 54 5 779 ir_set o3 1 fd 780 ir_set o3 2 g 781 ir_set o3 3 z 782 ir_set o3 4 n 783 o4 = rg_t 62 0 1 784 put y_fc f 60 785 end 786 fn rg_pb g uint p uint k ulong to void 787 s = rg_k 9 mul k 8 788 x = rg_op2 21 9 p s 789 y = rg_op1 36 2 x 790 i = rg_k 5 k 791 rg_bnd g i 792 o = rg_e 43 0 0 4 793 ir_set o 1 g 794 ir_set o 2 i 795 ir_set o 3 y 796 end 797 note the whole unit from the tape; the binary is i_out[0, result) 798 fn rg_gen to ulong 799 ir_init 800 put y_s 1 0 801 put y_h 11 0 802 ai = ir_tarr 9 16 803 put y_h 6 ir_tref ai 1 0 804 al = ir_tarr 9 8 805 put y_h 7 ir_tref al 1 0 806 ab = ir_tarr 2 8 807 put y_h 8 ir_tref ab 1 0 808 put y_h 4 ir_glob ai 0 809 put y_h 5 ir_glob ab 0 810 rg_prt 811 h = rg_ch 4 812 for j ulong h bound 4 813 rg_help 814 end 815 rg_main 816 ret ir_wr 817 end 818 note ---- input and output --------------------------------------------------------------------------- 819 fn rg_put fd int n ulong to bool 820 ret ne ar_out ulong fd 1 0 n 0 821 end 822 fn rg_num v ulong to void 823 n = ulong 0 824 x = v 825 while true bound 21 826 put y_dg n uchar add 48 rem x 10 827 n = add n 1 828 x = div x 10 829 if eq x 0 830 for j ulong n bound 21 831 put y_dg 31 get y_dg sub sub n 1 j 832 r = write 1 y_dg 31 1 833 end 834 ret 835 end 836 end 837 end 838 note argument a into y_pa; its length, or exit 2 839 fn rg_arg a ulong to ulong 840 m = argread a y_pa 0 4096 841 if or le m 0 ge m 4096 842 r = write 2 y_use 0 len y_use 843 exit 2 844 end 845 ret ulong m 846 end 847 note y_pa[0, m) as a decimal u64, or exit 2 848 fn rg_dec m ulong to ulong 849 v = ulong 0 850 for j ulong m bound 4096 851 c = ulong get y_pa j 852 if or lt c 48 gt c 57 853 r = write 2 y_use 0 len y_use 854 exit 2 855 end 856 if gt v `1844674407370955161` 857 r = write 2 y_use 0 len y_use 858 exit 2 859 end 860 w = add mul v 10 sub c 48 861 if lt w mul v 10 862 r = write 2 y_use 0 len y_use 863 exit 2 864 end 865 v = w 866 end 867 ret v 868 end 869 fn rg_ist m ulong to bool 870 if ne m len y_wt 871 ret false 872 end 873 for j ulong m bound 8 874 if ne get y_pa j get y_wt j 875 ret false 876 end 877 end 878 ret true 879 end 880 fn rg_iss m ulong to bool 881 if ne m len y_ws 882 ret false 883 end 884 for j ulong m bound 8 885 if ne get y_pa j get y_ws j 886 ret false 887 end 888 end 889 ret true 890 end 891 note the tape file named in y_pa[0, m); exit 27 unreadable, 2 when longer than 65536 bytes 892 fn rg_ltape m ulong to void 893 fd = fopen y_pa 0 m 524288 0 894 if lt fd 0 895 r = write 2 y_io 0 len y_io 896 exit 27 897 end 898 n = ulong 0 899 while true bound 65538 900 if ge n len y_tp 901 k = read int fd y_bk 0 1 902 if ne k 0 903 r = write 2 y_use 0 len y_use 904 exit 2 905 end 906 put y_s 0 n 907 ret 908 end 909 k = read int fd y_tp n sub len y_tp n 910 if lt k 0 911 r = write 2 y_io 0 len y_io 912 exit 27 913 end 914 if eq k 0 915 put y_s 0 n 916 ret 917 end 918 n = add n ulong k 919 end 920 end 921 note ---- shrink ------------------------------------------------------------------------------------ 922 note write i_out[0, n) to OUT (y_op); with x 1 to OUT.tape the tape instead 923 fn rg_wout n ulong x ulong to bool 924 m = get y_s 17 925 if eq x 1 926 for j ulong len y_tx bound 8 927 put y_op add m j get y_tx j 928 end 929 m = add m len y_tx 930 end 931 fd = fopen y_op 0 m 524865 420 932 if lt fd 0 933 ret false 934 end 935 ok = true 936 if eq x 1 937 o = ulong 0 938 while lt o get y_s 0 bound 65537 939 k = write int fd y_tp o sub get y_s 0 o 940 if le k 0 941 ok = false 942 o = get y_s 0 943 else 944 o = add o ulong k 945 end 946 end 947 else 948 ok = rg_put int fd n 949 end 950 r = fclose int fd 951 ret ok 952 end 953 fn rg_aend n ulong to void 954 p = get y_s 15 955 put y_av p uchar and n 255 956 put y_av add p 1 uchar shr n 8 957 put y_s 15 add p add n 2 958 c = add get y_s 16 1 959 put y_s 16 c 960 put y_av 0 uchar and c 255 961 put y_av 1 uchar shr c 8 962 end 963 note argv of the predicate: arguments 5 .. argcnt-1, then OUT 964 fn rg_argv to void 965 put y_s 15 2 966 put y_s 16 0 967 for a ulong ulong sub argcnt 5 bound 1024 968 m = rg_arg add a 5 969 p = add get y_s 15 2 970 if gt add p m 65000 971 r = write 2 y_use 0 len y_use 972 exit 2 973 end 974 for j ulong m bound 4096 975 put y_av add p j get y_pa j 976 end 977 rg_aend m 978 end 979 p = add get y_s 15 2 980 m = get y_s 17 981 for j ulong m bound 4096 982 put y_av add p j get y_op j 983 end 984 rg_aend m 985 end 986 fn rg_redir s int d int to void 987 if ne dupfd s d 0 long d 988 exit 127 989 end 990 end 991 note generate the current tape into OUT, run the predicate; true when it exits with the wanted status. 992 note A true result also cuts the tape to the bytes the generator read. 993 fn rg_try to bool 994 n = rg_gen 995 put y_s 13 add get y_s 13 1 996 if not rg_wout n 0 997 r = write 2 y_io 0 len y_io 998 exit 27 999 end 1000 nul = fopen y_dn 0 len y_dn 524290 0 1001 if lt nul 0 1002 r = write 2 y_io 0 len y_io 1003 exit 27 1004 end 1005 pid = fork 1006 if lt pid 0 1007 exit 27 1008 end 1009 if eq pid 0 1010 rg_redir int nul 0 1011 rg_redir int nul 1 1012 rg_redir int nul 2 1013 r = execprog y_av 0 get y_s 15 1014 exit 127 1015 end 1016 r = fclose int nul 1017 w = waitpid int pid y_stb 0 len y_stb 0 1018 if ne w pid 1019 exit 27 1020 end 1021 if ne get y_stb 0 0 1022 ret false 1023 end 1024 if ne ulong get y_stb 1 get y_s 14 1025 ret false 1026 end 1027 if lt get y_s 1 get y_s 0 1028 put y_s 0 get y_s 1 1029 end 1030 ret true 1031 end 1032 note delete tape bytes [i, i+c) (fewer at the end); kept when the predicate still holds 1033 fn rg_del i ulong c ulong to bool 1034 n = get y_s 0 1035 e = add i c 1036 if gt e n 1037 e = n 1038 end 1039 k = sub e i 1040 rg_save i n 1041 rg_shft i sub n k k 1042 put y_s 0 sub n k 1043 if rg_try 1044 ret true 1045 end 1046 rg_back i n 1047 put y_s 0 n 1048 ret false 1049 end 1050 note y_tp[i+j] = y_bk[i+j+k] for i+j < m 1051 fn rg_shft i ulong m ulong k ulong to void 1052 for j ulong sub m i bound 65536 1053 p = add i j 1054 put y_tp p get y_bk add p k 1055 end 1056 end 1057 fn rg_save i ulong n ulong to void 1058 for j ulong sub n i bound 65536 1059 put y_bk add i j get y_tp add i j 1060 end 1061 end 1062 fn rg_back i ulong n ulong to void 1063 for j ulong sub n i bound 65536 1064 put y_tp add i j get y_bk add i j 1065 end 1066 end 1067 note tape byte i = v; kept when the predicate still holds; refused once the search has used its 20000 tries 1068 fn rg_set i ulong v ulong to bool 1069 if ge get y_s 13 20000 1070 ret false 1071 end 1072 b = get y_tp i 1073 put y_tp i uchar v 1074 if rg_try 1075 ret true 1076 end 1077 put y_tp i b 1078 ret false 1079 end 1080 note one round: delete every run of 64, 32 and 16 down to 1 bytes, then zero or halve single bytes; 1081 note true on change 1082 fn rg_round to bool 1083 ch = false 1084 for q ulong 18 1085 c = ulong 64 1086 if eq q 1 1087 c = 32 1088 end 1089 if gt q 1 1090 c = sub 18 q 1091 end 1092 if rg_dels c 1093 ch = true 1094 end 1095 end 1096 for i ulong get y_s 0 bound 65536 1097 if lt i get y_s 0 1098 b = ulong get y_tp i 1099 if ne b 0 1100 if rg_set i 0 1101 ch = true 1102 else 1103 if gt b 1 1104 if rg_set i div b 2 1105 ch = true 1106 end 1107 end 1108 end 1109 end 1110 end 1111 end 1112 ret ch 1113 end 1114 note delete runs of c bytes at every position (the next try starts at the same position after a kept 1115 note deletion, one byte later otherwise); true when one was kept 1116 fn rg_dels c ulong to bool 1117 ch = false 1118 i = ulong 0 1119 while lt i get y_s 0 bound `1048576` 1120 if ge get y_s 13 20000 1121 ret ch 1122 end 1123 if rg_del i c 1124 ch = true 1125 else 1126 i = add i 1 1127 end 1128 end 1129 ret ch 1130 end 1131 fn rg_shr to int 1132 if lt argcnt 6 1133 r = write 2 y_use 0 len y_use 1134 ret 2 1135 end 1136 seed = rg_dec rg_arg 2 1137 put y_s 14 rg_dec rg_arg 3 1138 m = rg_arg 4 1139 if gt m 4000 1140 r = write 2 y_use 0 len y_use 1141 ret 2 1142 end 1143 for j ulong m bound 4096 1144 put y_op j get y_pa j 1145 end 1146 put y_s 17 m 1147 rg_argv 1148 put y_s 13 0 1149 rg_seed seed 1150 if not rg_try 1151 r = write 2 y_ni 0 len y_ni 1152 ret 3 1153 end 1154 t0 = get y_s 0 1155 o0 = get i_n 5 1156 while rg_round bound 64 1157 end 1158 if not rg_try 1159 r = write 2 y_ni 0 len y_ni 1160 ret 3 1161 end 1162 if not rg_wout 0 1 1163 r = write 2 y_io 0 len y_io 1164 ret 27 1165 end 1166 r = write 1 y_t1 0 len y_t1 1167 rg_num t0 1168 r = write 1 y_t2 0 len y_t2 1169 rg_num get y_s 0 1170 r = write 1 y_t3 0 len y_t3 1171 rg_num o0 1172 r = write 1 y_t2 0 len y_t2 1173 rg_num get i_n 5 1174 r = write 1 y_t4 0 len y_t4 1175 rg_num get y_s 13 1176 r = write 1 y_nl 0 len y_nl 1177 ret 0 1178 end 1179 fn main to int 1180 if lt argcnt 2 1181 r = write 2 y_use 0 len y_use 1182 ret 2 1183 end 1184 m = rg_arg 1 1185 if rg_iss m 1186 ret rg_shr 1187 end 1188 if rg_ist m 1189 if ne argcnt 3 1190 r = write 2 y_use 0 len y_use 1191 ret 2 1192 end 1193 m2 = rg_arg 2 1194 rg_ltape m2 1195 else 1196 if ne argcnt 2 1197 r = write 2 y_use 0 len y_use 1198 ret 2 1199 end 1200 rg_seed rg_dec m 1201 end 1202 n = rg_gen 1203 if not rg_put 1 n 1204 r = write 2 y_io 0 len y_io 1205 ret 27 1206 end 1207 ret 0 1208 end