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