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

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