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 / node / schedrcpt.om

3120 lines · 52535 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note schedrcpt.om: the scheduler-policy receipt driver of seed/node (P-DRA-14 lane). It replaces
3note ~/handoffs/hive/sched/bin/receipt.py, which produced the DRA-395/396 and DRA-397 r1 receipts,
4note and keeps that driver's case-file format, checks, refusals and receipt layout.
5note Build (json.om appended, it supplies the JSON reader):
6note cat seed/node/schedrcpt.om seed/json/json.om | seed/out/omc > build/node/schedrcpt
7note Use, from the repository root under test:
8note DRIVER_TREE/build/node/schedrcpt CASES.json OUTDIR_REL [--write]
9note The source hashed into the receipt is DRIVER_TREE/seed/node/schedrcpt.om, found from argv[0].
10note Exit 0 when every case passes, 1 when one fails, 2 on a driver error, 3 on a dirty tree,
11note 4 on a mutant whose pattern is not unique, 5 when a gate is not the concatenation of its parts.
12note
13note The SHA-256 functions below are copied from om/sha256/sha256-om2.om lines 21-317 at 7f4e1a44
14note (export dropped, sha256_final's digest parameter an edit instead of an out), since omc
15note composes one source text and json.om is the only other part.
16om2
17
18layout shactx 104
19field state uint 8 at 0 little
20field count ulong at 32 little
21field buf uchar 64 at 40 little
22end
23
24note ======== driver state ========
25note gv: scalar slots. gl: signed slots (0 expect, 1 compile exit, 2 last run code).
26note lst: nine string lists of 2048 arena indices (0 fragments, 1 compose, 2 args, 3 stdout_has,
27note 4 clean, 5 mutation files, 6 mutation patterns, 7 replacements, 8 hashed files of the receipt).
28note hsl: hex digests (0 source, 1 binary, 2 scratch). Run codes: 0..255 exit, -N signal N,
29note 4096 timeout, 4097 no run.
30const VOBN ulong 0
31const VDEP ulong 1
32const VSAN ulong 2
33const VSON ulong 3
34const VLN ulong 4
35const VKPN ulong 5
36const VKSN ulong 6
37const VWDN ulong 7
38const VCWN ulong 8
39const VPBN ulong 9
40const VPKN ulong 10
41const VPKC ulong 11
42const VPASS ulong 12
43const VFAIL ulong 13
44const VSRCN ulong 14
45const VFBN ulong 15
46const VTMO ulong 32
47const VREP ulong 33
48const VOM2 ulong 34
49const VCN ulong 35
50const VGATE ulong 36
51const VWALL ulong 37
52const VNRUN ulong 38
53const VFDO ulong 39
54const VFDE ulong 40
55const VCASP ulong 41
56const VOUT ulong 42
57const VDRV ulong 43
58const VBIN ulong 44
59const VHEAD ulong 45
60const VTREE ulong 46
61const VTISS ulong 47
62const VTSTA ulong 48
63const VTCAS ulong 49
64const VOK ulong 50
65const VNOUT ulong 51
66const VPLAT ulong 52
67const NONE ulong 4294967295
68const CTMO long 4096
69const CNONE long 4097
70
71global rb uchar 16777216
72global sx uchar 16777216
73global gsrc uchar 16777216
74global fb uchar 16777216
75global fb2 uchar 16777216
76global sa uchar 4194304
77global so ulong 65536
78global sl ulong 65536
79global lst ulong 18432
80global gv ulong 64
81global gl long 16
82global ctok ulong 16
83global kp uchar 1048576
84global ks ulong 196608
85global pb uchar 8192
86global rp uchar 8192
87global wd uchar 4096
88global cw uchar 4096
89global lb uchar 65536
90global nbuf uchar 32
91global hsl uchar 256
92global rcod long 65536
93global rmis uchar 65536
94global dst ulong 128
95global stb uchar 128
96global pk uchar 4096
97global ocod long 1024
98global omis uchar 1024
99global ocnt ulong 1024
100global oord ulong 1024
101global okb uchar 32768
102global okl ulong 1024
103
104text tpref "schedrcpt: "
105text tnl "\n"
106text tusage "usage: build/node/schedrcpt CASES.json OUTDIR_REL [--write], run from the repository root"
107text tsuffix "build/node/schedrcpt"
108text tsrcrel "seed/node/schedrcpt.om"
109text tomcabs "/seed/out/omc"
110text tomcrel "seed/out/omc"
111text tgit "/usr/bin/git"
112text tdevnull "/dev/null"
113text twrite "--write"
114text tmfd "schedrcpt"
115text twdpre "/tmp/schedrcpt-"
116text ttmppre "/tmp/sched-receipt-"
117text trcpre "receipt."
118text tjson ".json"
119text tom2 "om2\n"
120text tbuild "build/"
121text tstatus "status"
122text tporc "--porcelain"
123text tuntr "--untracked-files=normal"
124text trevp "rev-parse"
125text thead "HEAD"
126text thtree "HEAD^{tree}"
127text tostype "/proc/sys/kernel/ostype"
128text tosrel "/proc/sys/kernel/osrelease"
129text thost "/proc/sys/kernel/hostname"
130text tpstat "/proc/self/status"
131text tcpus "Cpus_allowed_list:"
132text tmach "aarch64"
133text tfull "receipt buffer full (16 MiB)"
134text tarena "string arena full"
135text tlist "list longer than 2048 entries"
136text tschema "case file does not match the schema (see seed/node/README.md)"
137text tlong "string too long (64 KiB) or not decodable"
138text tdupkey "duplicate object key in the case file"
139text tpath "path longer than 4095 bytes"
140text targs "argument list exceeds 64 entries or 4096 bytes"
141text tbig "file larger than its buffer (16 MiB)"
142text tread "read failed"
143text topen "cannot open a file named by the case file or the run"
144text tcasesf "cannot read the case file (at most 983039 bytes)"
145text tnotjson "case file is not valid JSON"
146text tgitf "git failed (run from the root of a git checkout)"
147text tfork "fork failed"
148text twait "waitpid failed"
149text tmkwork "cannot create the work directory"
150text tself "cannot locate the driver: invoke it by a path ending in build/node/schedrcpt"
151text twfail "cannot write the receipt (refusing to overwrite an existing one)"
152text tbn "build/node"
153text tbnf "cannot create build/node in the tree under test"
154text tdepth "nesting too deep"
155text trefd "REFUSED: dirty tree\n"
156text trefm "REFUSED: mutant "
157text trefnf " is not one of its fragments\n"
158text trefpo ": pattern occurs "
159text treftm " times\n"
160text tref "REFUSED: "
161text trefc " is not the concatenation\n"
162text tpass "PASS"
163text tfailw "FAIL"
164text texpe " expect="
165text tgot " got="
166text tcomp " compile="
167text truns " runs="
168text ttimeout "timeout"
169text tpynone "None"
170text tmiss " missing-stdout"
171text tnull "null"
172text ttrue "true"
173text tfalse "false"
174text tsumm "summary pass="
175text tsfail " fail="
176text tsdig " receipt_sha256="
177text tproj "P-DRA-14"
178text texec "NATIVE Linux AArch64 host CPU (DGX Spark host, no GPU opened)"
179text kcase "case"
180text kissue "issue"
181text kfrag "fragments"
182text kgate "gate"
183text kcomp "compose"
184text kom2 "om2"
185text kmut "mutate"
186text kargs "args"
187text kexp "expect"
188text ktmo "timeout"
189text kclean "clean"
190text khas "stdout_has"
191text krep "repeat"
192text kstat "statuses"
193text kcases "cases"
194text kbsha "binary_sha256"
195text kcexit "compile_exit"
196text kexpx "expected_exit"
197text kmutat "mutation"
198text krunx "run_exit"
199text ksrcsha "source_sha256"
200text kstderr "stderr"
201text kstdout "stdout"
202text kverd "verdict"
203text kwall "wall_ms"
204text kouts "outcomes"
205text kruns "runs"
206text kfile "file"
207text kfrom "from"
208text kto "to"
209text kchecks "checks"
210text kcompil "compiler"
211text kpath "path"
212text ksha "sha256"
213text kaffin "cpu_affinity"
214text kexecu "execution"
215text khashes "hashes"
216text kplat "platform"
217text kproj "project"
218text kscript "script"
219text kcsha "cases_sha256"
220text ksource "source"
221text kscom "source_commit"
222text kstree "source_tree"
223text ksumm "summary"
224text kpass "pass"
225text kfail "fail"
226text ktime "timestamp_utc"
227
228
229note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
230note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed).
231fn kinit k edit uint 64 to void
232put k 0 0x428a2f98
233put k 1 0x71374491
234put k 2 0xb5c0fbcf
235put k 3 0xe9b5dba5
236put k 4 0x3956c25b
237put k 5 0x59f111f1
238put k 6 0x923f82a4
239put k 7 0xab1c5ed5
240put k 8 0xd807aa98
241put k 9 0x12835b01
242put k 10 0x243185be
243put k 11 0x550c7dc3
244put k 12 0x72be5d74
245put k 13 0x80deb1fe
246put k 14 0x9bdc06a7
247put k 15 0xc19bf174
248put k 16 0xe49b69c1
249put k 17 0xefbe4786
250put k 18 0x0fc19dc6
251put k 19 0x240ca1cc
252put k 20 0x2de92c6f
253put k 21 0x4a7484aa
254put k 22 0x5cb0a9dc
255put k 23 0x76f988da
256put k 24 0x983e5152
257put k 25 0xa831c66d
258put k 26 0xb00327c8
259put k 27 0xbf597fc7
260put k 28 0xc6e00bf3
261put k 29 0xd5a79147
262put k 30 0x06ca6351
263put k 31 0x14292967
264put k 32 0x27b70a85
265put k 33 0x2e1b2138
266put k 34 0x4d2c6dfc
267put k 35 0x53380d13
268put k 36 0x650a7354
269put k 37 0x766a0abb
270put k 38 0x81c2c92e
271put k 39 0x92722c85
272put k 40 0xa2bfe8a1
273put k 41 0xa81a664b
274put k 42 0xc24b8b70
275put k 43 0xc76c51a3
276put k 44 0xd192e819
277put k 45 0xd6990624
278put k 46 0xf40e3585
279put k 47 0x106aa070
280put k 48 0x19a4c116
281put k 49 0x1e376c08
282put k 50 0x2748774c
283put k 51 0x34b0bcb5
284put k 52 0x391c0cb3
285put k 53 0x4ed8aa4a
286put k 54 0x5b9cca4f
287put k 55 0x682e6ff3
288put k 56 0x748f82ee
289put k 57 0x78a5636f
290put k 58 0x84c87814
291put k 59 0x8cc70208
292put k 60 0x90befffa
293put k 61 0xa4506ceb
294put k 62 0xbef9a3f7
295put k 63 0xc67178f2
296end
297
298note H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes
299note (fixtures/sha256-iv.dec).
300fn iv s edit uint 8 to void
301put s 0 0x6a09e667
302put s 1 0xbb67ae85
303put s 2 0x3c6ef372
304put s 3 0xa54ff53a
305put s 4 0x510e527f
306put s 5 0x9b05688c
307put s 6 0x1f83d9ab
308put s 7 0x5be0cd19
309end
310
311note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read.
312note The compiler proves that every get of w reads an element written earlier in this function
313note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence.
314fn sched w edit uint 64 fresh m view uchar * o ulong to void
315q = ulong 0
316x = uint 0
317y = uint 0
318for i ulong 16 unroll
319q = add o mul i 4
320x = uint get m q
321x = or shl x 8 uint get m add q 1
322x = or shl x 8 uint get m add q 2
323put w i or shl x 8 uint get m add q 3
324end
325for i ulong 48 unroll
326x = get w add i 1
327y = get w add i 14
328x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
329put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
330end
331end
332
333note The 64 rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant
334note in each copy, so get kt i and get w i need no run-time check. uint arithmetic wraps mod 2^32.
335note Ch(e,f,g) = (e and f) xor (not e and g) is written g xor (e and (f xor g)); Maj(a,b,c) =
336note (a and b) xor (a and c) xor (b and c) is written (a and b) or (c and (a or b)). Both are bitwise
337note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one.
338note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and
339note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines).
340fn rounds s edit uint 8 w view uint 64 kt view uint 64 to void
341a = get s 0
342b = get s 1
343c = get s 2
344d = get s 3
345e = get s 4
346f = get s 5
347g = get s 6
348h = get s 7
349t = uint 0
350for i ulong 64 unroll
351h = add h get kt i
352h = add h get w i
353t = xor rotr e 6 rotr e 11
354t = xor t rotr e 25
355h = add h t
356h = add h xor g and e xor f g
357d = add d h
358t = xor rotr a 2 rotr a 13
359t = xor t rotr a 22
360h = add h t
361t = and c or a b
362h = add h or and a b t
363t = h
364h = g
365g = f
366f = e
367e = d
368d = c
369c = b
370b = a
371a = t
372end
373put s 0 add get s 0 a
374put s 1 add get s 1 b
375put s 2 add get s 2 c
376put s 3 add get s 3 d
377put s 4 add get s 4 e
378put s 5 add get s 5 f
379put s 6 add get s 6 g
380put s 7 add get s 7 h
381end
382
383note One block m[o .. o+64) into s. sched and rounds stay calls: inlined, their names would share
384note one 16-register frame with this function's parameters (no spilling, error 12).
385fn compress s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
386sched w m o
387rounds s w kt
388end
389
390note The whole blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes).
391fn run s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
392for j ulong shr len m 6
393compress s w kt m shl j 6
394end
395end
396
397note The whole blocks of m (len m a multiple of 64). A rolled loop traps 4 after 1000000 passes
398note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 blocks):
399note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches 1000000
400note only above 1.7e19 bytes, which no AArch64 address space holds.
401fn blocks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
402q = ulong 0
403for a ulong shr len m 44
404for b ulong 0x80000
405run s w kt part m q 0x2000000
406q = add q 0x2000000
407end
408end
409for b ulong and shr len m 25 0x7ffff
410run s w kt part m q 0x2000000
411q = add q 0x2000000
412end
413run s w kt part m q sub len m q
414end
415
416note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the
417note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two blocks.
418fn finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void
419put p k 0x80
420e = ulong 64
421if ge k 56
422e = 128
423end
424bits = shl n 3
425for i ulong 8 unroll
426put p sub e add i 1 uchar and shr bits mul i 8 0xff
427end
428compress s w kt view p 0
429if eq e 128
430compress s w kt view p 64
431end
432end
433
434fn sha256_init c edit shactx to void
435s = new uint 8 0
436iv s
437for i ulong 8 unroll
438put c state i get s i
439end
440put c count 0
441end
442
443note k bytes of d from o into the context's buffer at bi (bi + k <= 64). A function of its own: inside
444note absorb the copy loop needs more than the 16 registers (error 12 under compiler-s4).
445fn tobuf c edit shactx bi ulong d view uchar * o ulong k ulong to void
446for i ulong k
447put c buf add bi i get d add o i
448end
449end
450
451note Whole blocks are hashed straight from d; the bytes that complete a pending block go through blk.
452fn absorb c edit shactx d view uchar * bi ulong to void
453n = len d
454s = new uint 8 0
455w = new uint 64 0
456kt = new uint 64 0
457blk = new uchar 64 0
458kinit kt
459for i ulong 8 unroll
460put s i get c state i
461end
462off = ulong 0
463if gt bi 0
464off = sub 64 bi
465for i ulong bi
466put blk i get c buf i
467end
468for i ulong off
469put blk add bi i get d i
470end
471compress s w kt view blk 0
472end
473nb = shr sub n off 6
474blocks s w kt part d off shl nb 6
475off = add off shl nb 6
476tobuf c 0 d off sub n off
477for i ulong 8 unroll
478put c state i get s i
479end
480end
481
482note Any length, including 0 and >= 2^32. lt n sub 64 bi cannot wrap (bi < 64); add bi n could.
483fn sha256_update c edit shactx d view uchar * to void
484n = len d
485cnt = get c count
486bi = and cnt 63
487put c count add cnt n
488if lt n sub 64 bi
489tobuf c bi d 0 n
490ret
491end
492absorb c d bi
493end
494
495fn sha256_final c edit shactx o edit uchar 32 to void
496s = new uint 8 0
497w = new uint 64 0
498kt = new uint 64 0
499p = new uchar 128 0
500v = ulong 0
501kinit kt
502for i ulong 8 unroll
503put s i get c state i
504end
505cnt = get c count
506k = and cnt 63
507for i ulong k
508put p i get c buf i
509end
510finish s w kt p k cnt
511for i ulong 8 unroll
512v = ulong get s i
513for j ulong 4 unroll
514put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff
515end
516end
517end
518
519note ======== messages and the line buffer ========
520
521note die: one line on stderr and exit (2 for driver errors)
522fn die t view uchar * code int to void
523r = write 2 tpref 0 len tpref
524r = write 2 t 0 len t
525r = write 2 tnl 0 1
526exit code
527end
528
529fn l_b c uchar to void
530n = get gv VLN
531if lt n len lb
532put lb n c
533put gv VLN add n 1
534end
535end
536
537fn l_txt t view uchar * to void
538for i ulong len t
539l_b get t i
540end
541end
542
543fn l_sa x ulong to void
544p = get so x
545for i ulong get sl x
546l_b get sa add p i
547end
548end
549
550note numfmt: decimal digits of v into nb; returns the length
551fn numfmt v long to ulong
552b = new uchar 24 0
553u = ulong 0
554ng = false
555if lt v 0
556ng = true
557u = ulong neg v
558else
559u = ulong v
560end
561k = ulong 0
562while lt k 24 bound 24
563put b k uchar add 48 rem u 10
564k = add k 1
565u = div u 10
566if eq u 0
567break
568end
569end
570w = ulong 0
571if ng
572put nbuf 0 uchar 45
573w = 1
574end
575while gt k 0 bound 24
576k = sub k 1
577put nbuf w get b k
578w = add w 1
579end
580ret w
581end
582
583fn l_num v long to void
584n = numfmt v
585for i ulong n
586l_b get nbuf i
587end
588end
589
590fn l_flush fd int to void
591n = get gv VLN
592r = write fd lb 0 n
593put gv VLN 0
594end
595
596note l_code: a run code as Python prints it (an int, timeout, or None)
597fn l_code v long to void
598if eq v CTMO
599l_txt ttimeout
600ret
601end
602if eq v CNONE
603l_txt tpynone
604ret
605end
606l_num v
607end
608
609note ======== JSON output: python json.dumps(indent=2, sort_keys=True, ensure_ascii=True) ========
610
611fn ob_b c uchar to void
612n = get gv VOBN
613if ge n len rb
614die tfull 2
615end
616put rb n c
617put gv VOBN add n 1
618end
619
620fn o_txt t view uchar * to void
621for i ulong len t
622ob_b get t i
623end
624end
625
626fn o_qtxt t view uchar * to void
627ob_b uchar 34
628o_txt t
629ob_b uchar 34
630end
631
632fn o_num v long to void
633n = numfmt v
634for i ulong n
635ob_b get nbuf i
636end
637end
638
639fn o_open c uchar to void
640ob_b c
641d = add get gv VDEP 1
642if ge d len dst
643die tdepth 2
644end
645put dst d 0
646put gv VDEP d
647end
648
649fn o_item to void
650d = get gv VDEP
651if gt get dst d 0
652ob_b uchar 44
653end
654ob_b uchar 10
655for i ulong mul d 2
656ob_b uchar 32
657end
658put dst d add get dst d 1
659end
660
661fn o_close c uchar to void
662d = get gv VDEP
663if gt get dst d 0
664ob_b uchar 10
665for i ulong mul sub d 1 2
666ob_b uchar 32
667end
668end
669ob_b c
670put gv VDEP sub d 1
671end
672
673fn o_colon to void
674ob_b uchar 58
675ob_b uchar 32
676end
677
678fn o_key t view uchar * to void
679o_item
680o_qtxt t
681o_colon
682end
683
684fn hexc h ulong to uchar
685if lt h 10
686ret uchar add 48 h
687end
688ret uchar add 87 h
689end
690
691fn o_hex slot ulong to void
692ob_b uchar 34
693for i ulong 64
694ob_b get hsl add mul slot 64 i
695end
696ob_b uchar 34
697end
698
699fn o_u4 v ulong to void
700ob_b uchar 92
701ob_b uchar 117
702for i ulong 4
703ob_b hexc and shr v mul sub 3 i 4 15
704end
705end
706
707fn o_bs c uchar to void
708ob_b uchar 92
709ob_b c
710end
711
712note o_asc: one ASCII byte as a JSON string character (Python ensure_ascii)
713fn o_asc c ulong to void
714if eq c 34
715o_bs uchar 34
716ret
717end
718if eq c 92
719o_bs uchar 92
720ret
721end
722if eq c 10
723o_bs uchar 110
724ret
725end
726if eq c 13
727o_bs uchar 114
728ret
729end
730if eq c 9
731o_bs uchar 116
732ret
733end
734if eq c 8
735o_bs uchar 98
736ret
737end
738if eq c 12
739o_bs uchar 102
740ret
741end
742if or lt c 32 eq c 127
743o_u4 c
744ret
745end
746ob_b uchar c
747end
748
749fn o_cp cp ulong to void
750if lt cp 65536
751o_u4 cp
752ret
753end
754v = sub cp 65536
755o_u4 add 55296 shr v 10
756o_u4 add 56320 and v 1023
757end
758
759note u8dec: decode the UTF-8 sequence at sx[i] (end e). Returns the code point or U+FFFD in the
760note low 32 bits and the bytes consumed above them; an invalid sequence consumes its maximal
761note subpart, as Python's bytes.decode(errors='replace') does
762fn u8dec i ulong e ulong to ulong
763c = ulong get sx i
764need = ulong 0
765lo = ulong 128
766hi = ulong 191
767cp = ulong 0
768if and ge c 194 le c 223
769need = 1
770cp = and c 31
771end
772if and ge c 224 le c 239
773need = 2
774cp = and c 15
775if eq c 224
776lo = 160
777end
778if eq c 237
779hi = 159
780end
781end
782if and ge c 240 le c 244
783need = 3
784cp = and c 7
785if eq c 240
786lo = 144
787end
788if eq c 244
789hi = 143
790end
791end
792if eq need 0
793ret or 65533 shl ulong 1 32
794end
795k = ulong 1
796while le k need bound 4
797if ge add i k e
798ret or 65533 shl k 32
799end
800d = ulong get sx add i k
801if or lt d lo gt d hi
802ret or 65533 shl k 32
803end
804cp = or shl cp 6 and d 63
805lo = 128
806hi = 191
807k = add k 1
808end
809ret or cp shl k 32
810end
811
812note o_esc: sx[s, s+n) as a quoted JSON string
813fn o_esc s ulong n ulong to void
814ob_b uchar 34
815i = s
816e = add s n
817while lt i e bound 16777217
818c = ulong get sx i
819if lt c 128
820o_asc c
821i = add i 1
822continue
823end
824r = u8dec i e
825o_cp and r 4294967295
826i = add i shr r 32
827end
828ob_b uchar 34
829end
830
831fn o_str_sa x ulong to void
832p = get so x
833n = get sl x
834for i ulong n
835put sx i get sa add p i
836end
837o_esc 0 n
838end
839
840fn o_ksa t view uchar * x ulong to void
841o_key t
842o_str_sa x
843end
844
845fn o_code v long to void
846if eq v CTMO
847o_qtxt ttimeout
848ret
849end
850if eq v CNONE
851o_txt tnull
852ret
853end
854o_num v
855end
856
857note ======== string arena and lists ========
858
859fn sput b view uchar * to ulong
860n = len b
861x = get gv VSON
862p = get gv VSAN
863if or ge x len so gt add p n len sa
864die tarena 2
865end
866i = ulong 0
867while lt i n bound 4194305
868put sa add p i get b i
869i = add i 1
870end
871put so x p
872put sl x n
873put gv VSAN add p n
874put gv VSON add x 1
875ret x
876end
877
878note sfb: fb[s, s+n) into the arena
879fn sfb s ulong n ulong to ulong
880b = new uchar 65536 0
881if gt n 65536
882die tlong 2
883end
884for i ulong n
885put b i get fb add s i
886end
887ret sput part b 0 n
888end
889
890fn spop x ulong to void
891put gv VSAN get so x
892put gv VSON x
893end
894
895fn saeq x ulong t view uchar * to bool
896if ne get sl x len t
897ret false
898end
899p = get so x
900for i ulong len t
901if ne get sa add p i get t i
902ret false
903end
904end
905ret true
906end
907
908note sacmp: bytewise order of two arena strings (-1, 0, 1)
909fn sacmp x ulong y ulong to long
910px = get so x
911py = get so y
912nx = get sl x
913ny = get sl y
914m = nx
915if lt ny m
916m = ny
917end
918for i ulong m
919a = get sa add px i
920b = get sa add py i
921if lt a b
922ret -1
923end
924if gt a b
925ret 1
926end
927end
928if lt nx ny
929ret -1
930end
931if gt nx ny
932ret 1
933end
934ret 0
935end
936
937fn ladd k ulong x ulong to void
938c = get gv add 16 k
939if ge c 2048
940die tlist 2
941end
942put lst add mul k 2048 c x
943put gv add 16 k add c 1
944end
945
946fn lget k ulong i ulong to ulong
947ret get lst add mul k 2048 i
948end
949
950fn lcnt k ulong to ulong
951ret get gv add 16 k
952end
953
954note ======== path and argument builders ========
955
956fn pb_clr to void
957put gv VPBN 0
958end
959
960fn pb_b c uchar to void
961n = get gv VPBN
962if ge n 4095
963die tpath 2
964end
965put pb n c
966put gv VPBN add n 1
967end
968
969fn pb_txt t view uchar * to void
970for i ulong len t
971pb_b get t i
972end
973end
974
975fn pb_sa x ulong to void
976p = get so x
977for i ulong get sl x
978pb_b get sa add p i
979end
980end
981
982fn pb_wd to void
983for i ulong get gv VWDN
984pb_b get wd i
985end
986end
987
988fn pb_cw to void
989for i ulong get gv VCWN
990pb_b get cw i
991end
992end
993
994fn pb_hex slot ulong to void
995for i ulong 64
996pb_b get hsl add mul slot 64 i
997end
998end
999
1000fn pb_open flags int mode int to long
1001ret fopen pb 0 get gv VPBN flags mode
1002end
1003
1004note pb_bin: the case binary's path, work dir / case name
1005fn pb_bin to void
1006pb_clr
1007pb_wd
1008pb_b uchar 47
1009pb_sa get gv VCN
1010end
1011
1012fn pk_clr to void
1013put pk 0 0
1014put pk 1 0
1015put gv VPKN 2
1016put gv VPKC 0
1017end
1018
1019note pk_pb: append pb as one record of the packed argument list
1020fn pk_pb to void
1021n = get gv VPBN
1022w = get gv VPKN
1023c = add get gv VPKC 1
1024if or gt c 64 gt add add w 2 n 4096
1025die targs 2
1026end
1027put pk w uchar and n 255
1028put pk add w 1 uchar shr n 8
1029for i ulong n
1030put pk add add w 2 i get pb i
1031end
1032put gv VPKN add add w 2 n
1033put gv VPKC c
1034put pk 0 uchar and c 255
1035put pk 1 uchar shr c 8
1036end
1037
1038fn pk_txt t view uchar * to void
1039pb_clr
1040pb_txt t
1041pk_pb
1042end
1043
1044note ======== files ========
1045
1046note rdfb: the whole of fd (from offset 0) into fb; returns the length
1047fn rdfb fd int to ulong
1048n = ulong 0
1049w = true
1050while w bound 100000
1051k = pread fd fb n sub len fb n long n
1052if lt k 0
1053die tread 2
1054end
1055if eq k 0
1056ret n
1057end
1058n = add n ulong k
1059if ge n len fb
1060die tbig 2
1061end
1062end
1063ret n
1064end
1065
1066note rdpb: the file named by pb into fb
1067fn rdpb to ulong
1068fd = pb_open 524288 0
1069if lt fd 0
1070l_txt tpref
1071for i ulong get gv VPBN
1072l_b get pb i
1073end
1074l_flush 2
1075die topen 2
1076end
1077n = rdfb int fd
1078r = fclose int fd
1079ret n
1080end
1081
1082fn fsize fd int to ulong
1083r = fstat fd stb 0 128
1084if lt r 0
1085die tread 2
1086end
1087v = ulong 0
1088for i ulong 8
1089v = or v shl ulong get stb add 48 i mul i 8
1090end
1091ret v
1092end
1093
1094note wrob: ob[0, n) or gsrc[0, n) (which 0) to fd
1095fn wrall fd int which ulong n ulong to void
1096p = ulong 0
1097while lt p n bound 1000000
1098k = long 0
1099if eq which 0
1100k = write fd gsrc p sub n p
1101else
1102k = write fd rb p sub n p
1103end
1104if le k 0
1105die twfail 2
1106end
1107p = add p ulong k
1108end
1109end
1110
1111note ======== SHA-256 (the copied om/sha256 functions) ========
1112
1113fn hfin c edit shactx slot ulong to void
1114o = new uchar 32 0
1115sha256_final c o
1116for i ulong 32
1117b = ulong get o i
1118q = add mul slot 64 mul i 2
1119put hsl q hexc shr b 4
1120put hsl add q 1 hexc and b 15
1121end
1122end
1123
1124note hfd: digest of everything in fd into hsl slot
1125fn hfd fd int slot ulong to void
1126c = new uchar 104 0
1127ch = new uchar 1048576 0
1128sha256_init c
1129p = long 0
1130w = true
1131while w bound 100000000
1132k = pread fd ch 0 1048576 p
1133if lt k 0
1134die tread 2
1135end
1136if eq k 0
1137break
1138end
1139sha256_update c part ch 0 ulong k
1140p = add p k
1141end
1142hfin c slot
1143end
1144
1145fn hpb slot ulong to void
1146fd = pb_open 524288 0
1147if lt fd 0
1148die topen 2
1149end
1150hfd int fd slot
1151r = fclose int fd
1152end
1153
1154note hglob: digest of gsrc[0, n) (which 0) or ob[0, n)
1155fn hglob which ulong n ulong slot ulong to void
1156c = new uchar 104 0
1157ch = new uchar 524288 0
1158sha256_init c
1159p = ulong 0
1160while lt p n bound 1000000
1161m = sub n p
1162if gt m 524288
1163m = 524288
1164end
1165for i ulong m
1166if eq which 0
1167put ch i get gsrc add p i
1168else
1169put ch i get rb add p i
1170end
1171end
1172sha256_update c part ch 0 m
1173p = add p m
1174end
1175hfin c slot
1176end
1177
1178note ======== processes ========
1179
1180fn spawn fi int fo int fe int grp bool to long
1181pid = fork
1182if eq pid 0
1183if grp
1184r = setpgid 0 0
1185end
1186r = dupfd fi 0 0
1187r = dupfd fo 1 0
1188r = dupfd fe 2 0
1189r = execprog pk 0 get gv VPKN
1190exit 127
1191end
1192if lt pid 0
1193die tfork 2
1194end
1195if grp
1196r = setpgid int pid int pid
1197end
1198ret pid
1199end
1200
1201note wcode: exit status as Python's returncode (negative signal number when killed)
1202fn wcode ws view uchar 4 to long
1203s = or ulong get ws 0 shl ulong get ws 1 8
1204sg = and s 127
1205if eq sg 0
1206ret long and shr s 8 255
1207end
1208ret neg long sg
1209end
1210
1211fn waitblk pid long to long
1212ws = new uchar 4 0
1213for i ulong 100000
1214r = waitpid int pid ws 0 4 0
1215if eq r pid
1216ret wcode ws
1217end
1218if ne r -4
1219die twait 2
1220end
1221end
1222die twait 2
1223ret 0
1224end
1225
1226note waitto: reap pid within tmo ns, polling every millisecond. At the deadline the child's
1227note process group gets SIGKILL and the code is CTMO; after a normal exit the group is killed
1228note too, so no grandchild outlives the run
1229fn waitto pid long tmo ulong to long
1230ws = new uchar 4 0
1231dl = add ulong monons tmo
1232w = true
1233while w bound 4000000000
1234r = waitpid int pid ws 0 4 1
1235if eq r pid
1236q = killpid neg int pid 9
1237ret wcode ws
1238end
1239if and lt r 0 ne r -4
1240die twait 2
1241end
1242if ge ulong monons dl
1243q = killpid neg int pid 9
1244if lt q 0
1245q = killpid int pid 9
1246end
1247c = waitblk pid
1248ret CTMO
1249end
1250s = sleepns 1000000
1251end
1252ret CTMO
1253end
1254
1255fn mfd to int
1256r = memfd tmfd 0 len tmfd 1
1257if lt r 0
1258die topen 2
1259end
1260ret int r
1261end
1262
1263fn devnull to int
1264r = fopen tdevnull 0 len tdevnull 524288 0
1265if lt r 0
1266die topen 2
1267end
1268ret int r
1269end
1270
1271note gitrun: run git (0 status, 1 rev-parse HEAD, 2 rev-parse HEAD^{tree}); stdout into fb
1272fn gitrun a ulong to ulong
1273pk_clr
1274pk_txt tgit
1275if eq a 0
1276pk_txt tstatus
1277pk_txt tporc
1278pk_txt tuntr
1279end
1280if eq a 1
1281pk_txt trevp
1282pk_txt thead
1283end
1284if eq a 2
1285pk_txt trevp
1286pk_txt thtree
1287end
1288fi = devnull
1289fo = mfd
1290fe = mfd
1291pid = spawn fi fo fe false
1292code = waitblk pid
1293if ne code 0
1294die tgitf 2
1295end
1296n = rdfb fo
1297r = fclose fi
1298r = fclose fo
1299r = fclose fe
1300ret n
1301end
1302
1303fn isws c uchar to bool
1304ret or or eq c 32 eq c 10 or eq c 13 eq c 9
1305end
1306
1307note stripfb: fb[0, n) without surrounding white space, into the arena
1308fn stripfb n ulong to ulong
1309s = ulong 0
1310e = n
1311while and lt s e isws get fb s bound 16777217
1312s = add s 1
1313end
1314while gt e s bound 16777217
1315if not isws get fb sub e 1
1316break
1317end
1318e = sub e 1
1319end
1320ret sfb s sub e s
1321end
1322
1323note dirty: Python's filter, a porcelain line whose path (line[3:]) starts with build/ is ignored
1324fn dirty to void
1325n = gitrun 0
1326s = ulong 0
1327bad = false
1328l_txt trefd
1329while lt s n bound 16777217
1330e = s
1331while and lt e n ne get fb e 10 bound 16777217
1332e = add e 1
1333end
1334skip = false
1335if ge sub e s 9
1336skip = true
1337for i ulong 6
1338if ne get fb add add s 3 i get tbuild i
1339skip = false
1340end
1341end
1342end
1343if not skip
1344if bad
1345l_b uchar 10
1346end
1347bad = true
1348for i ulong sub e s
1349l_b get fb add s i
1350end
1351end
1352s = add e 1
1353end
1354if bad
1355l_b uchar 10
1356l_flush 2
1357exit 3
1358end
1359put gv VLN 0
1360end
1361
1362note ======== recursive removal (shutil.rmtree with ignore_errors) ========
1363
1364fn isdirl n ulong to bool
1365r = lstat rp 0 n stb 0 128
1366if lt r 0
1367ret false
1368end
1369m = or ulong get stb 16 shl ulong get stb 17 8
1370ret eq and m 61440 16384
1371end
1372
1373note rment: one directory entry d[q..] (type ty) of the directory rp[0, n)
1374fn rment d view uchar 8192 q ulong ty uchar n ulong dep ulong to void
1375m = ulong 0
1376while ne get d add q m 0 bound 256
1377m = add m 1
1378end
1379if and eq m 1 eq get d q 46
1380ret
1381end
1382if and eq m 2 and eq get d q 46 eq get d add q 1 46
1383ret
1384end
1385if gt add add n m 1 4095
1386ret
1387end
1388put rp n uchar 47
1389for i ulong m
1390put rp add add n 1 i get d add q i
1391end
1392nn = add add n m 1
1393isd = eq ty 4
1394if eq ty 0
1395isd = isdirl nn
1396end
1397if isd
1398rmt nn add dep 1
1399else
1400r = unlink rp 0 nn
1401end
1402end
1403
1404note rmbuf: every entry of one getdents batch d[0, k)
1405fn rmbuf d view uchar 8192 k ulong n ulong dep ulong to void
1406p = ulong 0
1407while lt p k bound 8192
1408rl = or ulong get d add p 16 shl ulong get d add p 17 8
1409rment d add p 19 get d add p 18 n dep
1410if eq rl 0
1411ret
1412end
1413p = add p rl
1414end
1415end
1416
1417note rmt: remove the directory rp[0, n) and everything below it, never following a symbolic link
1418fn rmt n ulong dep ulong to void
1419if gt dep 64
1420ret
1421end
1422d = new uchar 8192 0
1423for pass ulong 4
1424fd = fopen rp 0 n 573440 0
1425if lt fd 0
1426ret
1427end
1428w = true
1429while w bound 1000000
1430k = getdnts int fd d 0 8192
1431if le k 0
1432break
1433end
1434rmbuf d ulong k n dep
1435end
1436r = fclose int fd
1437r = rmdir rp 0 n
1438if ge r 0
1439ret
1440end
1441end
1442end
1443
1444note rmpath: rmtree of arena string x (relative to the repository root, or absolute)
1445fn rmpath x ulong to void
1446p = get so x
1447n = get sl x
1448if or eq n 0 gt n 4095
1449ret
1450end
1451for i ulong n
1452put rp i get sa add p i
1453end
1454if isdirl n
1455rmt n 0
1456end
1457end
1458
1459fn rmwork to void
1460n = get gv VWDN
1461if eq n 0
1462ret
1463end
1464for i ulong n
1465put rp i get wd i
1466end
1467if isdirl n
1468rmt n 0
1469end
1470end
1471
1472fn refuse code int to void
1473l_flush 2
1474rmwork
1475exit code
1476end
1477
1478note ======== case file ========
1479
1480fn sdec src view uchar * tp view uint * t ulong to ulong
1481ty = js_type tp t
1482if and ne ty 5 ne ty 6
1483die tschema 2
1484end
1485b = new uchar 65536 0
1486n = js_sget src js_off tp t b
1487if lt n 0
1488die tlong 2
1489end
1490ret sput part b 0 ulong n
1491end
1492
1493fn kid x ulong to ulong
1494if saeq x kcase
1495ret 0
1496end
1497if saeq x kissue
1498ret 1
1499end
1500if saeq x kfrag
1501ret 2
1502end
1503if saeq x kgate
1504ret 3
1505end
1506if saeq x kcomp
1507ret 4
1508end
1509if saeq x kom2
1510ret 5
1511end
1512if saeq x kmut
1513ret 6
1514end
1515if saeq x kargs
1516ret 7
1517end
1518if saeq x kexp
1519ret 8
1520end
1521if saeq x ktmo
1522ret 9
1523end
1524if saeq x kclean
1525ret 10
1526end
1527if saeq x khas
1528ret 11
1529end
1530if saeq x krep
1531ret 12
1532end
1533if saeq x kstat
1534ret 13
1535end
1536if saeq x kcases
1537ret 14
1538end
1539ret 99
1540end
1541
1542note cscan: value token of each known key of object o into ctok (NONE when absent)
1543fn cscan src view uchar * tp view uint * o ulong to void
1544for i ulong 16
1545put ctok i NONE
1546end
1547if ne js_type tp o 1
1548die tschema 2
1549end
1550k = js_first tp o
1551while ne k NONE bound 1000000
1552x = sdec src tp k
1553id = kid x
1554spop x
1555if lt id 16
1556if ne get ctok id NONE
1557die tdupkey 2
1558end
1559put ctok id add k 1
1560end
1561k = js_mnext tp k
1562end
1563end
1564
1565note rdlist: a list of strings into list k (absent: nothing)
1566fn rdlist src view uchar * tp view uint * t ulong k ulong to void
1567if eq t NONE
1568ret
1569end
1570if ne js_type tp t 3
1571die tschema 2
1572end
1573e = js_first tp t
1574while ne e NONE bound 1000000
1575ladd k sdec src tp e
1576e = js_enext tp e
1577end
1578end
1579
1580fn mtrip src view uchar * tp view uint * a ulong to void
1581if ne js_type tp a 3
1582die tschema 2
1583end
1584e0 = js_first tp a
1585if eq e0 NONE
1586die tschema 2
1587end
1588e1 = js_enext tp e0
1589if eq e1 NONE
1590die tschema 2
1591end
1592e2 = js_enext tp e1
1593if eq e2 NONE
1594die tschema 2
1595end
1596if ne js_enext tp e2 NONE
1597die tschema 2
1598end
1599ladd 5 sdec src tp e0
1600ladd 6 sdec src tp e1
1601ladd 7 sdec src tp e2
1602end
1603
1604note rdmut: mutate is one [path, old, new] or a list of them; an empty list means none
1605fn rdmut src view uchar * tp view uint * t ulong to void
1606if eq t NONE
1607ret
1608end
1609if ne js_type tp t 3
1610die tschema 2
1611end
1612e = js_first tp t
1613if eq e NONE
1614ret
1615end
1616if ne js_type tp e 3
1617mtrip src tp t
1618ret
1619end
1620while ne e NONE bound 1000000
1621mtrip src tp e
1622e = js_enext tp e
1623end
1624end
1625
1626fn cint src view uchar * tp view uint * t ulong dv long to long
1627if eq t NONE
1628ret dv
1629end
1630if ne js_type tp t 7
1631die tschema 2
1632end
1633o = js_off tp t
1634if not js_isl src o
1635die tschema 2
1636end
1637ret js_tol src o
1638end
1639
1640note ctmo: timeout seconds (a plain decimal number) as nanoseconds; default 120 s
1641fn ctmo src view uchar * tp view uint * t ulong to ulong
1642if eq t NONE
1643ret 120000000000
1644end
1645if ne js_type tp t 7
1646die tschema 2
1647end
1648ret tmodec src js_off tp t js_span tp t
1649end
1650
1651note tmodec: the lexeme src[o, o+n) of a timeout (digits, one optional dot) in nanoseconds
1652fn tmodec src view uchar * o ulong n ulong to ulong
1653v = ulong 0
1654fd = ulong 0
1655dot = false
1656for i ulong n
1657c = ulong get src add o i
1658if and eq c 46 not dot
1659dot = true
1660continue
1661end
1662if or lt c 48 gt c 57
1663die tschema 2
1664end
1665if or not dot lt fd 9
1666v = add mul v 10 sub c 48
1667if dot
1668fd = add fd 1
1669end
1670end
1671if gt v 1000000000000000000
1672die tschema 2
1673end
1674end
1675while lt fd 9 bound 10
1676if gt v 1000000000000000000
1677die tschema 2
1678end
1679v = mul v 10
1680fd = add fd 1
1681end
1682ret v
1683end
1684
1685note cload: read case c into the per-case globals
1686fn cload src view uchar * tp view uint * c ulong to void
1687cscan src tp c
1688for k ulong 8
1689put gv add 16 k 0
1690end
1691t = get ctok 0
1692if eq t NONE
1693die tschema 2
1694end
1695put gv VCN sdec src tp t
1696put gv VGATE NONE
1697t = get ctok 3
1698if ne t NONE
1699g = sdec src tp t
1700if gt get sl g 0
1701put gv VGATE g
1702end
1703end
1704rdlist src tp get ctok 2 0
1705if eq lcnt 0 0
1706if eq get gv VGATE NONE
1707die tschema 2
1708end
1709ladd 0 get gv VGATE
1710end
1711rdlist src tp get ctok 4 1
1712rdlist src tp get ctok 7 2
1713rdlist src tp get ctok 11 3
1714rdlist src tp get ctok 10 4
1715rdmut src tp get ctok 6
1716cload2 src tp
1717end
1718
1719fn cload2 src view uchar * tp view uint * to void
1720put gv VOM2 0
1721t = get ctok 5
1722if ne t NONE
1723if eq js_type tp t 8
1724put gv VOM2 1
1725end
1726end
1727t = get ctok 8
1728if eq t NONE
1729die tschema 2
1730end
1731put gl 0 cint src tp t 0
1732put gv VTMO ctmo src tp get ctok 9
1733r = cint src tp get ctok 12 1
1734if lt r 0
1735r = 0
1736end
1737if gt r 65536
1738die tschema 2
1739end
1740put gv VREP ulong r
1741end
1742
1743note ======== composition ========
1744
1745fn hfadd f ulong to void
1746n = lcnt 8
1747for i ulong n
1748if eq sacmp f lget 8 i 0
1749ret
1750end
1751end
1752ladd 8 f
1753end
1754
1755fn mcheck to void
1756for i ulong lcnt 5
1757f = lget 5 i
1758found = false
1759for j ulong lcnt 0
1760if eq sacmp f lget 0 j 0
1761found = true
1762end
1763end
1764if not found
1765l_txt trefm
1766l_sa get gv VCN
1767l_b uchar 58
1768l_b uchar 32
1769l_sa f
1770l_txt trefnf
1771refuse 4
1772end
1773end
1774end
1775
1776note fbat: does arena string x occur in fb at p
1777fn fbat x ulong p ulong to bool
1778q = get so x
1779n = get sl x
1780if gt add p n get gv VFBN
1781ret false
1782end
1783for i ulong n
1784if ne get fb add p i get sa add q i
1785ret false
1786end
1787end
1788ret true
1789end
1790
1791note fbcount: non-overlapping occurrences of x in fb (Python bytes.count)
1792fn fbcount x ulong to ulong
1793n = get gv VFBN
1794m = get sl x
1795if eq m 0
1796ret add n 1
1797end
1798c = ulong 0
1799p = ulong 0
1800while le add p m n bound 16777217
1801if fbat x p
1802c = add c 1
1803p = add p m
1804else
1805p = add p 1
1806end
1807end
1808ret c
1809end
1810
1811fn fbfind x ulong to ulong
1812n = get gv VFBN
1813m = get sl x
1814p = ulong 0
1815while le add p m n bound 16777217
1816if fbat x p
1817ret p
1818end
1819p = add p 1
1820end
1821ret NONE
1822end
1823
1824note mutate: apply mutation j (its pattern occurs exactly once) to the fragment in fb
1825fn mutate j ulong to void
1826o = lget 6 j
1827w = lget 7 j
1828c = fbcount o
1829if ne c 1
1830l_txt trefm
1831l_sa get gv VCN
1832l_txt trefpo
1833l_num long c
1834l_txt treftm
1835refuse 4
1836end
1837splice fbfind o get sl o w
1838end
1839
1840note splice: replace fb[p, p+ol) by arena string w
1841fn splice p ulong ol ulong w ulong to void
1842n = get gv VFBN
1843m = get sl w
1844e = add p ol
1845nn = add add p m sub n e
1846if ge nn len fb
1847die tbig 2
1848end
1849fbto2 0 0 p
1850sato2 p w
1851fbto2 e add p m sub n e
1852for i ulong nn
1853put fb i get fb2 i
1854end
1855put gv VFBN nn
1856end
1857
1858fn fbto2 s ulong d ulong n ulong to void
1859for i ulong n
1860put fb2 add d i get fb add s i
1861end
1862end
1863
1864fn sato2 d ulong w ulong to void
1865q = get so w
1866for i ulong get sl w
1867put fb2 add d i get sa add q i
1868end
1869end
1870
1871fn src_b c uchar to void
1872n = get gv VSRCN
1873if ge n len gsrc
1874die tbig 2
1875end
1876put gsrc n c
1877put gv VSRCN add n 1
1878end
1879
1880fn compose to void
1881put gv VSRCN 0
1882if eq get gv VOM2 1
1883for i ulong len tom2
1884src_b get tom2 i
1885end
1886end
1887for i ulong lcnt 0
1888f = lget 0 i
1889hfadd f
1890pb_clr
1891pb_sa f
1892put gv VFBN rdpb
1893for j ulong lcnt 5
1894if eq sacmp lget 5 j f 0
1895mutate j
1896end
1897end
1898k = ulong 0
1899while lt k get gv VFBN bound 16777217
1900src_b get fb k
1901k = add k 1
1902end
1903end
1904end
1905
1906note ccheck: a gate case without a mutation must be exactly the concatenation of its compose list
1907fn ccheck to void
1908if eq get gv VGATE NONE
1909ret
1910end
1911if or gt lcnt 5 0 eq lcnt 1 0
1912ret
1913end
1914pos = ulong 0
1915ok = true
1916sn = get gv VSRCN
1917for i ulong lcnt 1
1918pb_clr
1919pb_sa lget 1 i
1920n = rdpb
1921if gt add pos n sn
1922ok = false
1923break
1924end
1925k = ulong 0
1926while lt k n bound 16777217
1927if ne get fb k get gsrc add pos k
1928ok = false
1929break
1930end
1931k = add k 1
1932end
1933pos = add pos n
1934end
1935if or not ok ne pos sn
1936l_txt tref
1937l_sa get gv VGATE
1938l_txt trefc
1939refuse 5
1940end
1941end
1942
1943note ======== compile and run ========
1944
1945fn closefd x ulong to void
1946if ne get gv x NONE
1947r = fclose int get gv x
1948put gv x NONE
1949end
1950end
1951
1952fn compile to long
1953fi = mfd
1954wrall fi 0 get gv VSRCN
1955r = fseek fi 0 0
1956pb_bin
1957fo = pb_open 524865 493
1958if lt fo 0
1959die topen 2
1960end
1961fe = mfd
1962pk_clr
1963pb_clr
1964pb_cw
1965pb_txt tomcabs
1966pk_pb
1967pid = spawn fi int fo fe false
1968r = fclose fi
1969r = fclose int fo
1970code = waitblk pid
1971pb_bin
1972r = chmod pb 0 get gv VPBN 493
1973closefd VFDE
1974put gv VFDE ulong fe
1975ret code
1976end
1977
1978note hasall: true when every stdout_has string occurs in fd's contents
1979fn hasall fd int to bool
1980for i ulong lcnt 3
1981if not mhas fd lget 3 i
1982ret false
1983end
1984end
1985ret true
1986end
1987
1988note mhas: substring search over fd in windows of fb2 (each window overlaps the next by m-1)
1989fn mhas fd int x ulong to bool
1990m = get sl x
1991if eq m 0
1992ret true
1993end
1994w = sub len fb2 65536
1995p = ulong 0
1996for it ulong 1000000
1997k = pread fd fb2 0 add w m long p
1998if lt k 0
1999die tread 2
2000end
2001got = ulong k
2002if lt got m
2003ret false
2004end
2005lim = add sub got m 1
2006if gt lim w
2007lim = w
2008end
2009if inwin x lim
2010ret true
2011end
2012if lt got add w m
2013ret false
2014end
2015p = add p w
2016end
2017ret false
2018end
2019
2020fn inwin x ulong lim ulong to bool
2021s = ulong 0
2022while lt s lim bound 16777217
2023if w2at s x
2024ret true
2025end
2026s = add s 1
2027end
2028ret false
2029end
2030
2031fn w2at s ulong x ulong to bool
2032q = get so x
2033for i ulong get sl x
2034if ne get fb2 add s i get sa add q i
2035ret false
2036end
2037end
2038ret true
2039end
2040
2041fn runone i ulong to void
2042for j ulong lcnt 4
2043rmpath lget 4 j
2044end
2045fi = devnull
2046fo = mfd
2047fe = mfd
2048pk_clr
2049pb_bin
2050pk_pb
2051for j ulong lcnt 2
2052pb_clr
2053pb_sa lget 2 j
2054pk_pb
2055end
2056pid = spawn fi fo fe true
2057r = fclose fi
2058code = waitto pid get gv VTMO
2059put rcod i code
2060put rmis i 0
2061if not hasall fo
2062put rmis i 1
2063end
2064put gl 2 code
2065closefd VFDO
2066closefd VFDE
2067put gv VFDO ulong fo
2068put gv VFDE ulong fe
2069end
2070
2071fn runall to void
2072n = get gv VREP
2073put gl 2 CNONE
2074t0 = ulong monons
2075for i ulong n
2076runone i
2077end
2078put gv VNRUN n
2079put gv VWALL div sub ulong monons t0 1000000
2080ok = true
2081for i ulong n
2082if or ne get rcod i get gl 0 ne get rmis i 0
2083ok = false
2084end
2085end
2086put gv VOK 0
2087if ok
2088put gv VOK 1
2089end
2090end
2091
2092note ======== outcome histogram ========
2093
2094fn okey j ulong to void
2095b = mul j 32
2096w = ulong 0
2097c = get ocod j
2098if eq c CTMO
2099for i ulong len ttimeout
2100put okb add b i get ttimeout i
2101end
2102w = len ttimeout
2103else
2104w = numfmt c
2105for i ulong w
2106put okb add b i get nbuf i
2107end
2108end
2109if ne get omis j 0
2110for i ulong len tmiss
2111put okb add add b w i get tmiss i
2112end
2113w = add w len tmiss
2114end
2115put okl j w
2116end
2117
2118fn ocmp x ulong y ulong to bool
2119nx = get okl x
2120ny = get okl y
2121m = nx
2122if lt ny m
2123m = ny
2124end
2125for i ulong m
2126a = get okb add mul x 32 i
2127b = get okb add mul y 32 i
2128if ne a b
2129ret lt a b
2130end
2131end
2132ret lt nx ny
2133end
2134
2135fn ofind nd ulong c long m uchar to ulong
2136for j ulong nd
2137if eq get ocod j c
2138if eq get omis j m
2139ret j
2140end
2141end
2142end
2143ret NONE
2144end
2145
2146note hist: distinct outcomes in first-seen order (ocod, omis, ocnt); returns their number
2147fn hist to ulong
2148nd = ulong 0
2149for i ulong get gv VNRUN
2150c = get rcod i
2151m = get rmis i
2152f = ofind nd c m
2153if eq f NONE
2154if ge nd 1024
2155die tlist 2
2156end
2157put ocod nd c
2158put omis nd m
2159put ocnt nd 0
2160okey nd
2161f = nd
2162nd = add nd 1
2163end
2164put ocnt f add get ocnt f 1
2165end
2166ret nd
2167end
2168
2169fn o_hist nd ulong to void
2170for i ulong nd
2171put oord i i
2172end
2173for i ulong nd
2174j = i
2175while gt j 0 bound 1024
2176a = get oord sub j 1
2177b = get oord j
2178if not ocmp b a
2179break
2180end
2181put oord sub j 1 b
2182put oord j a
2183j = sub j 1
2184end
2185end
2186o_open uchar 123
2187for i ulong nd
2188j = get oord i
2189o_item
2190ob_b uchar 34
2191for k ulong get okl j
2192ob_b get okb add mul j 32 k
2193end
2194ob_b uchar 34
2195o_colon
2196o_num long get ocnt j
2197end
2198o_close uchar 125
2199end
2200
2201fn l_hist nd ulong to void
2202l_b uchar 123
2203for j ulong nd
2204if gt j 0
2205l_b uchar 44
2206l_b uchar 32
2207end
2208l_b uchar 39
2209for k ulong get okl j
2210l_b get okb add mul j 32 k
2211end
2212l_b uchar 39
2213l_b uchar 58
2214l_b uchar 32
2215l_num long get ocnt j
2216end
2217l_b uchar 125
2218end
2219
2220note ======== check entries ========
2221
2222note o_tail: the last cap bytes of fd's contents, cut forward to a UTF-8 boundary
2223fn o_tail x ulong cap ulong to void
2224if eq get gv x NONE
2225o_esc 0 0
2226ret
2227end
2228fd = int get gv x
2229n = fsize fd
2230s = ulong 0
2231if gt n cap
2232s = sub n cap
2233end
2234o_tl fd s sub n s
2235end
2236
2237note o_tl: n bytes of fd from s, skipping up to 3 leading UTF-8 continuation bytes when s > 0
2238fn o_tl fd int s ulong n ulong to void
2239k = pread fd sx 0 n long s
2240if lt k 0
2241die tread 2
2242end
2243m = ulong k
2244b = ulong 0
2245w = gt s 0
2246while and w lt b 3 bound 4
2247if ge b m
2248break
2249end
2250if ne and get sx b 192 128
2251break
2252end
2253b = add b 1
2254end
2255o_esc b sub m b
2256end
2257
2258fn o_full x ulong to void
2259fd = int get gv x
2260n = fsize fd
2261if ge n len sx
2262die tbig 2
2263end
2264k = pread fd sx 0 n long 0
2265if lt k 0
2266die tread 2
2267end
2268o_esc 0 ulong k
2269end
2270
2271fn o_mutobj i ulong to void
2272o_open uchar 123
2273o_ksa kfile lget 5 i
2274o_ksa kfrom lget 6 i
2275o_ksa kto lget 7 i
2276o_close uchar 125
2277end
2278
2279fn o_mut to void
2280n = lcnt 5
2281if eq n 0
2282ret
2283end
2284o_key kmutat
2285if eq n 1
2286o_mutobj 0
2287ret
2288end
2289o_open uchar 91
2290for i ulong n
2291o_item
2292o_mutobj i
2293end
2294o_close uchar 93
2295end
2296
2297fn o_opt src view uchar * tp view uint * t ulong to void
2298if eq t NONE
2299o_txt tnull
2300else
2301o_tree src tp t
2302end
2303end
2304
2305note o_head: the keys every check entry has before mutation
2306fn o_head src view uchar * tp view uint * to void
2307o_ksa kcase get gv VCN
2308o_key kcexit
2309o_num get gl 1
2310o_key kexpx
2311o_num get gl 0
2312o_key kissue
2313o_opt src tp get ctok 1
2314o_mut
2315end
2316
2317fn emitfail src view uchar * tp view uint * to void
2318o_item
2319o_open uchar 123
2320o_head src tp
2321o_key krunx
2322o_txt tnull
2323o_key ksrcsha
2324o_hex 0
2325o_key kstderr
2326o_full VFDE
2327o_key kverd
2328o_qtxt tfailw
2329o_close uchar 125
2330put gv VFAIL add get gv VFAIL 1
2331end
2332
2333fn emitok src view uchar * tp view uint * nd ulong to void
2334o_item
2335o_open uchar 123
2336o_key kbsha
2337o_hex 1
2338o_head src tp
2339if gt get gv VNRUN 1
2340o_key krep
2341o_open uchar 123
2342o_key kouts
2343o_hist nd
2344o_key kruns
2345o_num long get gv VNRUN
2346o_close uchar 125
2347end
2348o_key krunx
2349o_code get gl 2
2350o_key ksrcsha
2351o_hex 0
2352o_key kstderr
2353o_tail VFDE 2000
2354o_key kstdout
2355o_tail VFDO 4000
2356if gt lcnt 3 0
2357o_key khas
2358o_tree src tp get ctok 11
2359end
2360o_key kverd
2361if eq get gv VOK 1
2362o_qtxt tpass
2363else
2364o_qtxt tfailw
2365end
2366o_key kwall
2367o_num long get gv VWALL
2368o_close uchar 125
2369end
2370
2371fn pline nd ulong to void
2372if eq get gv VOK 1
2373l_txt tpass
2374put gv VPASS add get gv VPASS 1
2375else
2376l_txt tfailw
2377put gv VFAIL add get gv VFAIL 1
2378end
2379l_b uchar 32
2380l_sa get gv VCN
2381l_txt texpe
2382l_num get gl 0
2383l_txt tgot
2384l_code get gl 2
2385if gt get gv VNRUN 1
2386l_txt truns
2387l_hist nd
2388end
2389l_b uchar 10
2390l_flush 1
2391end
2392
2393note A case that does not compile still gets its console line (it is a FAIL
2394note in the receipt, so the summary counts it). The Python ORACLE printed
2395note nothing for it.
2396fn cfline to void
2397l_txt tfailw
2398l_b uchar 32
2399l_sa get gv VCN
2400l_txt texpe
2401l_num get gl 0
2402l_txt tcomp
2403l_num get gl 1
2404l_b uchar 10
2405l_flush 1
2406end
2407
2408fn docase src view uchar * tp view uint * c ulong to void
2409cload src tp c
2410mcheck
2411compose
2412ccheck
2413hglob 0 get gv VSRCN 0
2414ce = compile
2415put gl 1 ce
2416if ne ce 0
2417emitfail src tp
2418cfline
2419closefd VFDE
2420ret
2421end
2422runall
2423pb_bin
2424hpb 1
2425nd = hist
2426emitok src tp nd
2427pline nd
2428closefd VFDO
2429closefd VFDE
2430end
2431
2432note ======== JSON copy of case-file values (keys sorted, duplicates refused) ========
2433
2434fn o_jstr src view uchar * tp view uint * t ulong to void
2435b = new uchar 65536 0
2436n = js_sget src js_off tp t b
2437if lt n 0
2438die tlong 2
2439end
2440for i ulong ulong n
2441put sx i get b i
2442end
2443o_esc 0 ulong n
2444end
2445
2446fn kpush src view uchar * tp view uint * k ulong to void
2447b = new uchar 65536 0
2448n = js_sget src js_off tp k b
2449if lt n 0
2450die tlong 2
2451end
2452kstore b ulong n k
2453end
2454
2455fn kstore b view uchar 65536 n ulong k ulong to void
2456p = get gv VKPN
2457s = get gv VKSN
2458if or gt add p n len kp gt add s 3 len ks
2459die tarena 2
2460end
2461for i ulong n
2462put kp add p i get b i
2463end
2464put ks s p
2465put ks add s 1 n
2466put ks add s 2 k
2467put gv VKSN add s 3
2468put gv VKPN add p n
2469end
2470
2471fn kcmp a ulong b ulong to long
2472pa = get ks a
2473na = get ks add a 1
2474pb2 = get ks b
2475nb2 = get ks add b 1
2476m = na
2477if lt nb2 m
2478m = nb2
2479end
2480for i ulong m
2481x = get kp add pa i
2482y = get kp add pb2 i
2483if lt x y
2484ret -1
2485end
2486if gt x y
2487ret 1
2488end
2489end
2490if lt na nb2
2491ret -1
2492end
2493if gt na nb2
2494ret 1
2495end
2496ret 0
2497end
2498
2499fn kswap x ulong y ulong to void
2500for q ulong 3
2501a = get ks add x q
2502put ks add x q get ks add y q
2503put ks add y q a
2504end
2505end
2506
2507fn ksort base ulong top ulong to void
2508i = add base 3
2509while lt i top bound 1000000
2510j = i
2511while gt j base bound 1000000
2512c = kcmp sub j 3 j
2513if eq c 0
2514die tdupkey 2
2515end
2516if lt c 0
2517break
2518end
2519kswap sub j 3 j
2520j = sub j 3
2521end
2522i = add i 3
2523end
2524end
2525
2526fn o_kp e ulong to void
2527p = get ks e
2528n = get ks add e 1
2529for i ulong n
2530put sx i get kp add p i
2531end
2532o_esc 0 n
2533end
2534
2535fn o_obj src view uchar * tp view uint * t ulong to void
2536o_open uchar 123
2537base = get gv VKSN
2538kb = get gv VKPN
2539k = js_first tp t
2540while ne k NONE bound 1000000
2541kpush src tp k
2542k = js_mnext tp k
2543end
2544top = get gv VKSN
2545ksort base top
2546i = base
2547while lt i top bound 1000000
2548o_item
2549o_kp i
2550o_colon
2551o_tree src tp add get ks add i 2 1
2552i = add i 3
2553end
2554put gv VKSN base
2555put gv VKPN kb
2556o_close uchar 125
2557end
2558
2559fn o_arr src view uchar * tp view uint * t ulong to void
2560o_open uchar 91
2561e = js_first tp t
2562while ne e NONE bound 1000000
2563o_item
2564o_tree src tp e
2565e = js_enext tp e
2566end
2567o_close uchar 93
2568end
2569
2570fn o_tree src view uchar * tp view uint * t ulong to void
2571ty = js_type tp t
2572if eq ty 1
2573o_obj src tp t
2574ret
2575end
2576if eq ty 3
2577o_arr src tp t
2578ret
2579end
2580if or eq ty 5 eq ty 6
2581o_jstr src tp t
2582ret
2583end
2584if eq ty 7
2585o = js_off tp t
2586for i ulong js_span tp t
2587ob_b get src add o i
2588end
2589ret
2590end
2591if eq ty 8
2592o_txt ttrue
2593ret
2594end
2595if eq ty 9
2596o_txt tfalse
2597ret
2598end
2599o_txt tnull
2600end
2601
2602note ======== receipt fields ========
2603
2604note sfile: a small text file (path text t), trimmed, into the arena
2605fn sfile t view uchar * to ulong
2606pb_clr
2607pb_txt t
2608ret stripfb rdpb
2609end
2610
2611note o_plat: system release machine node, as platform.uname() gives them
2612fn o_plat to void
2613a = sfile tostype
2614b = sfile tosrel
2615c = sfile thost
2616n = sxsa 0 a
2617put sx n 32
2618n = sxsa add n 1 b
2619put sx n 32
2620n = add n 1
2621for i ulong len tmach
2622put sx add n i get tmach i
2623end
2624n = add n len tmach
2625put sx n 32
2626n = sxsa add n 1 c
2627o_esc 0 n
2628end
2629
2630note sxsa: arena string x into sx at n; returns the end
2631fn sxsa n ulong x ulong to ulong
2632p = get so x
2633m = get sl x
2634for i ulong m
2635put sx add n i get sa add p i
2636end
2637ret add n m
2638end
2639
2640note pnum: a decimal number in fb at p; returns the value and moves gv VNOUT past it
2641fn pnum p ulong n ulong to ulong
2642v = ulong 0
2643q = p
2644while and lt q n and ge get fb q 48 le get fb q 57 bound 32
2645v = add mul v 10 sub ulong get fb q 48
2646q = add q 1
2647end
2648put gv VNOUT q
2649ret v
2650end
2651
2652note o_affin: Cpus_allowed_list of this process, expanded (os.sched_getaffinity)
2653fn o_affin to void
2654pb_clr
2655pb_txt tpstat
2656n = rdpb
2657p = ulong 0
2658f = NONE
2659while lt p n bound 16777217
2660if fbtxt p tcpus
2661f = add p len tcpus
2662break
2663end
2664while and lt p n ne get fb p 10 bound 16777217
2665p = add p 1
2666end
2667p = add p 1
2668end
2669o_open uchar 91
2670if eq f NONE
2671o_close uchar 93
2672ret
2673end
2674p = f
2675while and lt p n or eq get fb p 9 eq get fb p 32 bound 64
2676p = add p 1
2677end
2678w = true
2679while w bound 4096
2680a = pnum p n
2681p = get gv VNOUT
2682b = a
2683if and lt p n eq get fb p 45
2684b = pnum add p 1 n
2685p = get gv VNOUT
2686end
2687v = a
2688while le v b bound 65536
2689o_item
2690o_num long v
2691v = add v 1
2692end
2693if or ge p n ne get fb p 44
2694break
2695end
2696p = add p 1
2697end
2698o_close uchar 93
2699end
2700
2701fn fbtxt p ulong t view uchar * to bool
2702for i ulong len t
2703if ne get fb add p i get t i
2704ret false
2705end
2706end
2707ret true
2708end
2709
2710fn sxd p ulong v ulong w ulong to void
2711x = v
2712k = w
2713while gt k 0 bound 8
2714k = sub k 1
2715put sx add p k uchar add 48 rem x 10
2716x = div x 10
2717end
2718end
2719
2720note o_time: datetime.now(timezone.utc).isoformat()
2721fn o_time to void
2722ns = ulong realns
2723secs = div ns 1000000000
2724sdate div secs 86400
2725o_time2 rem secs 86400 div rem ns 1000000000 1000
2726end
2727
2728note sdate: YYYY-MM-DDT for day number t since 1970-01-01 (civil_from_days, H. Hinnant)
2729fn sdate t ulong to void
2730z = add t 719468
2731era = div z 146097
2732doe = sub z mul era 146097
2733yoe = yoef doe
2734y = add yoe mul era 400
2735doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
2736mp = div add mul 5 doy 2 153
2737d = add sub doy div add mul 153 mp 2 5 1
2738m = add mp 3
2739if ge mp 10
2740m = sub mp 9
2741end
2742if le m 2
2743y = add y 1
2744end
2745sxd 0 y 4
2746put sx 4 45
2747sxd 5 m 2
2748put sx 7 45
2749sxd 8 d 2
2750put sx 10 84
2751end
2752
2753fn yoef doe ulong to ulong
2754a = sub doe div doe 1460
2755b = add a div doe 36524
2756ret div sub b div doe 146096 365
2757end
2758
2759fn o_time2 sod ulong us ulong to void
2760sxd 11 div sod 3600 2
2761put sx 13 58
2762sxd 14 div rem sod 3600 60 2
2763put sx 16 58
2764sxd 17 rem sod 60 2
2765n = ulong 19
2766if gt us 0
2767put sx 19 46
2768sxd 20 us 6
2769n = 26
2770end
2771put sx n 43
2772sxd add n 1 0 2
2773put sx add n 3 58
2774sxd add n 4 0 2
2775o_esc 0 add n 6
2776end
2777
2778fn o_hashes to void
2779n = lcnt 8
2780for i ulong n
2781j = i
2782while gt j 0 bound 2048
2783a = lget 8 sub j 1
2784b = lget 8 j
2785if le sacmp a b 0
2786break
2787end
2788put lst add 16384 sub j 1 b
2789put lst add 16384 j a
2790j = sub j 1
2791end
2792end
2793o_open uchar 123
2794for i ulong n
2795f = lget 8 i
2796o_item
2797o_str_sa f
2798o_colon
2799pb_clr
2800pb_sa f
2801hpb 2
2802o_hex 2
2803end
2804o_close uchar 125
2805end
2806
2807fn o_script src view uchar * tp view uint * to void
2808o_open uchar 123
2809o_key kbsha
2810pb_clr
2811pb_sa get gv VBIN
2812hpb 2
2813o_hex 2
2814o_key kcases
2815o_tree src tp 0
2816o_key kcsha
2817pb_clr
2818pb_sa get gv VCASP
2819hpb 2
2820o_hex 2
2821o_key kpath
2822o_qtxt tsrcrel
2823o_key ksha
2824pb_clr
2825pb_sa get gv VDRV
2826hpb 2
2827o_hex 2
2828o_key ksource
2829pb_clr
2830pb_sa get gv VDRV
2831fd = pb_open 524288 0
2832if lt fd 0
2833die topen 2
2834end
2835put gv VFDO ulong fd
2836o_full VFDO
2837r = fclose int fd
2838put gv VFDO NONE
2839o_close uchar 125
2840end
2841
2842fn o_rest src view uchar * tp view uint * to void
2843o_key kcompil
2844o_open uchar 123
2845o_key kpath
2846o_qtxt tomcrel
2847o_key ksha
2848pb_clr
2849pb_txt tomcrel
2850hpb 2
2851o_hex 2
2852o_close uchar 125
2853o_key kaffin
2854o_affin
2855o_key kexecu
2856o_qtxt texec
2857o_key khashes
2858o_hashes
2859o_key kissue
2860o_tree src tp get gv VTISS
2861o_key kplat
2862o_plat
2863o_key kproj
2864o_qtxt tproj
2865o_key kscript
2866o_script src tp
2867o_ksa kscom get gv VHEAD
2868o_ksa kstree get gv VTREE
2869o_key kstat
2870if eq get gv VTSTA NONE
2871o_open uchar 123
2872o_close uchar 125
2873else
2874o_tree src tp get gv VTSTA
2875end
2876o_key ksumm
2877o_open uchar 123
2878o_key kfail
2879o_num long get gv VFAIL
2880o_key kpass
2881o_num long get gv VPASS
2882o_close uchar 125
2883o_key ktime
2884o_time
2885end
2886
2887note ======== setup ========
2888
2889note argsa: argument i into the arena
2890fn argsa i ulong to ulong
2891b = new uchar 4096 0
2892n = argread i b 0 4096
2893if lt n 0
2894die tpath 2
2895end
2896ret sput part b 0 ulong n
2897end
2898
2899note self: the driver binary is argv0; its source is the sibling seed/node/schedrcpt.om of the
2900note build/node/schedrcpt it was invoked as
2901fn self to void
2902x = argsa 0
2903n = get sl x
2904m = len tsuffix
2905if lt n m
2906die tself 2
2907end
2908p = get so x
2909for i ulong m
2910if ne get sa add add p sub n m i get tsuffix i
2911die tself 2
2912end
2913end
2914put gv VBIN x
2915b = new uchar 4096 0
2916for i ulong sub n m
2917put b i get sa add p i
2918end
2919k = sub n m
2920for i ulong len tsrcrel
2921put b add k i get tsrcrel i
2922end
2923put gv VDRV sput part b 0 add k len tsrcrel
2924end
2925
2926note mkbuild: create build/node in the tree under test, the README's build
2927note step (mkdir -p build/node; build/ is outside the dirty check). Gates
2928note open journals and work directories below it, and jn_open creates only
2929note the last directory of its path.
2930fn mkbuild to void
2931r = makedir tbn 0 5 493
2932if and lt r 0 ne r neg long 17
2933die tbnf 2
2934end
2935r = makedir tbn 0 10 493
2936if and lt r 0 ne r neg long 17
2937die tbnf 2
2938end
2939end
2940
2941fn mkwork to void
2942r = new uchar 8 0
2943q = random r 0 8 0
2944if ne q 8
2945die tmkwork 2
2946end
2947pb_clr
2948pb_txt twdpre
2949for i ulong 8
2950b = ulong get r i
2951pb_b hexc shr b 4
2952pb_b hexc and b 15
2953end
2954q = makedir pb 0 get gv VPBN 448
2955if lt q 0
2956die tmkwork 2
2957end
2958for i ulong get gv VPBN
2959put wd i get pb i
2960end
2961put gv VWDN get gv VPBN
2962end
2963
2964fn ldcases src edit uchar * to ulong
2965pb_clr
2966pb_sa get gv VCASP
2967fd = pb_open 524288 0
2968if lt fd 0
2969die tcasesf 2
2970end
2971n = ulong 0
2972w = true
2973while w bound 1000000
2974k = pread int fd src n sub len src n long n
2975if lt k 0
2976die tcasesf 2
2977end
2978if eq k 0
2979break
2980end
2981n = add n ulong k
2982if ge n len src
2983die tcasesf 2
2984end
2985end
2986r = fclose int fd
2987ret n
2988end
2989
2990fn wflag to bool
2991f = false
2992for i ulong sub ulong argcnt 1
2993x = argsa add i 1
2994if saeq x twrite
2995f = true
2996end
2997spop x
2998end
2999ret f
3000end
3001
3002note save: write the receipt (ob) and print its path (--write) or put it in /tmp
3003fn save wr bool to void
3004n = get gv VOBN
3005pb_clr
3006if wr
3007o = get gv VOUT
3008pb_sa o
3009for i ulong get gv VPBN
3010if and gt i 0 eq get pb i 47
3011r = makedir pb 0 i 493
3012end
3013end
3014r = makedir pb 0 get gv VPBN 493
3015if ne get pb sub get gv VPBN 1 47
3016pb_b uchar 47
3017end
3018pb_txt trcpre
3019else
3020pb_txt ttmppre
3021end
3022pb_hex 2
3023pb_txt tjson
3024fl = int 524865
3025if wr
3026fl = 524481
3027end
3028fd = pb_open fl 420
3029if lt fd 0
3030die twfail 2
3031end
3032wrall int fd 1 n
3033r = fclose int fd
3034if wr
3035if ne get pb 0 47
3036for i ulong get gv VCWN
3037l_b get cw i
3038end
3039l_b uchar 47
3040end
3041for i ulong get gv VPBN
3042l_b get pb i
3043end
3044l_b uchar 10
3045l_flush 1
3046end
3047end
3048
3049fn main to int
3050src = new uchar 983040 0
3051tp = new uint 262144 0
3052if lt argcnt 3
3053die tusage 2
3054end
3055put gv VFDO NONE
3056put gv VFDE NONE
3057self
3058put gv VCASP argsa 1
3059put gv VOUT argsa 2
3060wr = wflag
3061q = cwd cw 0 4096
3062if lt q 2
3063die tpath 2
3064end
3065put gv VCWN sub ulong q 1
3066n = ldcases src
3067r = js_init tp 983040 48
3068if lt r 0
3069die tnotjson 2
3070end
3071r = js_parse src tp n
3072if lt r 0
3073die tnotjson 2
3074end
3075cscan src tp 0
3076put gv VTISS get ctok 1
3077put gv VTSTA get ctok 13
3078put gv VTCAS get ctok 14
3079if or eq get gv VTISS NONE eq get gv VTCAS NONE
3080die tschema 2
3081end
3082if ne js_type tp get gv VTCAS 3
3083die tschema 2
3084end
3085dirty
3086put gv VHEAD stripfb gitrun 1
3087put gv VTREE stripfb gitrun 2
3088mkbuild
3089mkwork
3090o_open uchar 123
3091o_key kchecks
3092o_open uchar 91
3093e = js_first tp get gv VTCAS
3094while ne e NONE bound 1000000
3095docase src tp e
3096e = js_enext tp e
3097end
3098o_close uchar 93
3099rmwork
3100put gv VWDN 0
3101o_rest src tp
3102o_close uchar 125
3103ob_b uchar 10
3104hglob 1 get gv VOBN 2
3105l_txt tsumm
3106l_num long get gv VPASS
3107l_txt tsfail
3108l_num long get gv VFAIL
3109l_txt tsdig
3110for i ulong 64
3111l_b get hsl add 128 i
3112end
3113l_b uchar 10
3114l_flush 1
3115save wr
3116if eq get gv VFAIL 0
3117ret 0
3118end
3119ret 1
3120end