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 / ng / a64 / gate.om
| 1 | note gate.om: the ng-a64 encoder gate. Run from the omega-mv root: seed/out/omc < seed/ng/a64/gate.om > gate && ./gate |
| 2 | note with stdin one line, an existing scratch directory (e.g. /tmp/a64gate). Referee: GNU objdump (/usr/bin/objdump). |
| 3 | note 1. Build ng: scratch/enc.om = the units of seed/ng/ng.units in order, compiled by seed/out/omc -> scratch/enc.bin. |
| 4 | note (The unit sha256s are ngbuild's check, not this gate's.) |
| 5 | note 2. Run ng on seed/ng/a64/tests/enc.cases (first line `@ng a64-enc`) -> scratch/words.bin (one 4-byte word per |
| 6 | note case, case k at byte 4k). |
| 7 | note 3. objdump -D -b binary -m aarch64 scratch/words.bin -> scratch/dis.txt. |
| 8 | note 4. Each case's expected text (after `|`) must equal objdump's text for its word: blanks collapsed, objdump's |
| 9 | note trailing `//` comment dropped, `@+N`/`@-N` replaced by 0x<hex of 4k+N / 4k-N>. |
| 10 | note 5. Negative control: the same with one wrong bit in the tool (ADD immediate base 0x91000000 -> 0x91400000, the |
| 11 | note shift bit 22). The gate requires the mutant to fail at least one case. |
| 12 | note 6. Refusals: each case of seed/ng/a64/tests/enc-refuse.cases (`FORM operands | stderr line`) runs alone as line 2 |
| 13 | note of `@ng a64-enc` + the case; ng must exit 5, write no word and print exactly that stderr line. |
| 14 | note 7. Negative control of step 6, mutant M-A: a6_fld masks the value to its field instead of refusing it (review |
| 15 | note P3-2 of 8a3680b0). The gate requires it to fail at least one refusal case. |
| 16 | note Output: one line per mismatch and a summary; exit 0 only when every case matches, the word count equals the case |
| 17 | note count, every refusal case holds and both mutants are caught. Exit 1 otherwise; 2 when a step could not run. |
| 18 | om2 |
| 19 | |
| 20 | global mem uchar 16777216 |
| 21 | global pb uchar 4096 |
| 22 | global pn ulong 1 |
| 23 | global sd uchar 1024 |
| 24 | global sn ulong 1 |
| 25 | global av uchar 4096 |
| 26 | global ap ulong 2 |
| 27 | global stb uchar 4 |
| 28 | global ob uchar 65536 |
| 29 | global on ulong 1 |
| 30 | global ex uchar 4096 |
| 31 | global en ulong 1 |
| 32 | global dso ulong 4096 |
| 33 | global dsl ulong 4096 |
| 34 | global nd ulong 1 |
| 35 | global rg ulong 16 |
| 36 | global mk ulong 1 |
| 37 | |
| 38 | note regions of mem: source [0, 4 MiB), cases [4, 6), disassembly [6, 14), stdin [14, 15) |
| 39 | const rsrc ulong 0 |
| 40 | const rcas ulong 4194304 |
| 41 | const rdis ulong 6291456 |
| 42 | const rin ulong 14680064 |
| 43 | const rinu ulong 14684160 |
| 44 | note refusal cases [15 MiB, 15.5 MiB), the one-case list and ng's stderr after them |
| 45 | const rref ulong 15728640 |
| 46 | const rone ulong 16252928 |
| 47 | const rerr ulong 16257024 |
| 48 | |
| 49 | text tomc "seed/out/omc" |
| 50 | text ta64 "seed/ng/src/a64.om" |
| 51 | text tmain "seed/ng/ng.units" |
| 52 | text tcase "seed/ng/a64/tests/enc.cases" |
| 53 | text tobjd "/usr/bin/objdump" |
| 54 | text tencom "/enc.om" |
| 55 | text tencb "/enc.bin" |
| 56 | text twrd "/words.bin" |
| 57 | text tdis "/dis.txt" |
| 58 | text terr "/enc.err" |
| 59 | text tdash "-D" |
| 60 | text tdb "-b" |
| 61 | text tbin "binary" |
| 62 | text tdm "-m" |
| 63 | text tarch "aarch64" |
| 64 | text tref "seed/ng/a64/tests/enc-refuse.cases" |
| 65 | text tone "/one.cases" |
| 66 | text tmpat "fset 36 0x91000000 4" |
| 67 | text tmrep "fset 36 0x91400000 4" |
| 68 | text tmpa2 "if ge v shl ulong 1 w\na6_die 5 6 v\nend\nret v" |
| 69 | text tmre2 "if ge v 99\nend\nret and v sub shl ulong 1 w 1" |
| 70 | text tmsg "a64 gate: case line\nexpected\ngot\ncases\nmatch\nmutant mismatches\nwords\nPASS\nFAIL\ncannot run step\na64 gate: refusal case line\nrefusal cases\nrefused as written\nmutant M-A refusal failures\nstatus\n" |
| 71 | |
| 72 | note ---- output ---- |
| 73 | fn oput b ulong to void |
| 74 | n = get on 0 |
| 75 | if lt n len ob |
| 76 | put ob n uchar b |
| 77 | put on 0 add n 1 |
| 78 | end |
| 79 | end |
| 80 | |
| 81 | fn oflush to void |
| 82 | n = get on 0 |
| 83 | p = ulong 0 |
| 84 | while lt p n bound 65536 |
| 85 | k = write 1 ob p sub n p |
| 86 | if le k 0 |
| 87 | p = n |
| 88 | else |
| 89 | p = add p ulong k |
| 90 | end |
| 91 | end |
| 92 | put on 0 0 |
| 93 | end |
| 94 | |
| 95 | note omsg k: line k of tmsg (without its newline) |
| 96 | fn omsg k ulong to void |
| 97 | i = ulong 0 |
| 98 | j = ulong 0 |
| 99 | while and lt i len tmsg lt j k bound 4096 |
| 100 | if eq get tmsg i 10 |
| 101 | j = add j 1 |
| 102 | end |
| 103 | i = add i 1 |
| 104 | end |
| 105 | while and lt i len tmsg ne get tmsg i 10 bound 4096 |
| 106 | oput ulong get tmsg i |
| 107 | i = add i 1 |
| 108 | end |
| 109 | end |
| 110 | |
| 111 | fn odec v ulong to void |
| 112 | d = ulong 1 |
| 113 | while ge div v d 10 bound 20 |
| 114 | d = mul d 10 |
| 115 | end |
| 116 | x = v |
| 117 | while gt d 0 bound 20 |
| 118 | oput add 48 div x d |
| 119 | x = rem x d |
| 120 | d = div d 10 |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | fn obig o ulong n ulong to void |
| 125 | i = ulong 0 |
| 126 | while lt i n bound 4096 |
| 127 | oput ulong get mem add o i |
| 128 | i = add i 1 |
| 129 | end |
| 130 | end |
| 131 | |
| 132 | fn fail k ulong to void |
| 133 | omsg 9 |
| 134 | oput 32 |
| 135 | odec k |
| 136 | oput 10 |
| 137 | omsg 8 |
| 138 | oput 10 |
| 139 | oflush |
| 140 | exit 2 |
| 141 | end |
| 142 | |
| 143 | note ---- paths: pb = a text, or the scratch directory followed by a text ---- |
| 144 | fn pclr to void |
| 145 | put pn 0 0 |
| 146 | end |
| 147 | |
| 148 | fn pb1 b ulong to void |
| 149 | n = get pn 0 |
| 150 | if ge n len pb |
| 151 | fail 0 |
| 152 | end |
| 153 | put pb n uchar b |
| 154 | put pn 0 add n 1 |
| 155 | end |
| 156 | |
| 157 | fn ptxt k ulong to void |
| 158 | note k: 1 omc, 2 a64.om, 3 ng.units, 4 enc.cases, 5 objdump, 6 /enc.om, 7 /enc.bin, 8 /words.bin, 9 /dis.txt, 10 /enc.err |
| 159 | note 11 -D, 12 -b, 13 binary, 14 -m, 15 aarch64 |
| 160 | i = ulong 0 |
| 161 | if eq k 1 |
| 162 | while lt i len tomc bound 64 |
| 163 | pb1 ulong get tomc i |
| 164 | i = add i 1 |
| 165 | end |
| 166 | end |
| 167 | if eq k 2 |
| 168 | while lt i len ta64 bound 64 |
| 169 | pb1 ulong get ta64 i |
| 170 | i = add i 1 |
| 171 | end |
| 172 | end |
| 173 | if eq k 3 |
| 174 | while lt i len tmain bound 64 |
| 175 | pb1 ulong get tmain i |
| 176 | i = add i 1 |
| 177 | end |
| 178 | end |
| 179 | if eq k 4 |
| 180 | while lt i len tcase bound 64 |
| 181 | pb1 ulong get tcase i |
| 182 | i = add i 1 |
| 183 | end |
| 184 | end |
| 185 | if eq k 5 |
| 186 | while lt i len tobjd bound 64 |
| 187 | pb1 ulong get tobjd i |
| 188 | i = add i 1 |
| 189 | end |
| 190 | end |
| 191 | if ge k 6 |
| 192 | ptxt2 k |
| 193 | end |
| 194 | end |
| 195 | |
| 196 | fn ptxt2 k ulong to void |
| 197 | i = ulong 0 |
| 198 | if eq k 6 |
| 199 | while lt i len tencom bound 64 |
| 200 | pb1 ulong get tencom i |
| 201 | i = add i 1 |
| 202 | end |
| 203 | end |
| 204 | if eq k 7 |
| 205 | while lt i len tencb bound 64 |
| 206 | pb1 ulong get tencb i |
| 207 | i = add i 1 |
| 208 | end |
| 209 | end |
| 210 | if eq k 8 |
| 211 | while lt i len twrd bound 64 |
| 212 | pb1 ulong get twrd i |
| 213 | i = add i 1 |
| 214 | end |
| 215 | end |
| 216 | if eq k 9 |
| 217 | while lt i len tdis bound 64 |
| 218 | pb1 ulong get tdis i |
| 219 | i = add i 1 |
| 220 | end |
| 221 | end |
| 222 | if eq k 10 |
| 223 | while lt i len terr bound 64 |
| 224 | pb1 ulong get terr i |
| 225 | i = add i 1 |
| 226 | end |
| 227 | end |
| 228 | if ge k 11 |
| 229 | ptxt3 k |
| 230 | end |
| 231 | end |
| 232 | |
| 233 | fn ptxt3 k ulong to void |
| 234 | i = ulong 0 |
| 235 | if eq k 11 |
| 236 | while lt i len tdash bound 64 |
| 237 | pb1 ulong get tdash i |
| 238 | i = add i 1 |
| 239 | end |
| 240 | end |
| 241 | if eq k 12 |
| 242 | while lt i len tdb bound 64 |
| 243 | pb1 ulong get tdb i |
| 244 | i = add i 1 |
| 245 | end |
| 246 | end |
| 247 | if eq k 13 |
| 248 | while lt i len tbin bound 64 |
| 249 | pb1 ulong get tbin i |
| 250 | i = add i 1 |
| 251 | end |
| 252 | end |
| 253 | if eq k 14 |
| 254 | while lt i len tdm bound 64 |
| 255 | pb1 ulong get tdm i |
| 256 | i = add i 1 |
| 257 | end |
| 258 | end |
| 259 | if eq k 15 |
| 260 | while lt i len tarch bound 64 |
| 261 | pb1 ulong get tarch i |
| 262 | i = add i 1 |
| 263 | end |
| 264 | end |
| 265 | if eq k 16 |
| 266 | while lt i len tref bound 64 |
| 267 | pb1 ulong get tref i |
| 268 | i = add i 1 |
| 269 | end |
| 270 | end |
| 271 | if eq k 17 |
| 272 | while lt i len tone bound 64 |
| 273 | pb1 ulong get tone i |
| 274 | i = add i 1 |
| 275 | end |
| 276 | end |
| 277 | end |
| 278 | |
| 279 | note pset k: pb = text k; pscr k: pb = scratch directory + text k |
| 280 | fn pset k ulong to void |
| 281 | pclr |
| 282 | ptxt k |
| 283 | end |
| 284 | |
| 285 | fn pscr k ulong to void |
| 286 | pclr |
| 287 | i = ulong 0 |
| 288 | while lt i get sn 0 bound 1024 |
| 289 | pb1 ulong get sd i |
| 290 | i = add i 1 |
| 291 | end |
| 292 | ptxt k |
| 293 | end |
| 294 | |
| 295 | note ---- files ---- |
| 296 | fn openr to int |
| 297 | r = fopen pb 0 get pn 0 0 0 |
| 298 | if lt r 0 |
| 299 | fail 1 |
| 300 | end |
| 301 | ret int r |
| 302 | end |
| 303 | |
| 304 | note openw md: create or truncate pb for writing with mode md (O_WRONLY|O_CREAT|O_TRUNC = 577) |
| 305 | fn openw md int to int |
| 306 | r = fopen pb 0 get pn 0 577 md |
| 307 | if lt r 0 |
| 308 | fail 2 |
| 309 | end |
| 310 | ret int r |
| 311 | end |
| 312 | |
| 313 | fn fclo fd int to void |
| 314 | r = fclose fd |
| 315 | end |
| 316 | |
| 317 | note readin: all of file pb into mem at offset o (capacity c bytes); returns the byte count |
| 318 | fn readin o ulong c ulong to ulong |
| 319 | fd = openr |
| 320 | n = ulong 0 |
| 321 | go = true |
| 322 | while go bound 1000000 |
| 323 | if ge n c |
| 324 | fail 3 |
| 325 | end |
| 326 | k = read fd mem add o n sub c n |
| 327 | if lt k 0 |
| 328 | fail 3 |
| 329 | end |
| 330 | if eq k 0 |
| 331 | go = false |
| 332 | else |
| 333 | n = add n ulong k |
| 334 | end |
| 335 | end |
| 336 | fclo fd |
| 337 | ret n |
| 338 | end |
| 339 | |
| 340 | fn wall fd int o ulong n ulong to void |
| 341 | p = ulong 0 |
| 342 | while lt p n bound 1000000 |
| 343 | k = write fd mem add o p sub n p |
| 344 | if le k 0 |
| 345 | fail 4 |
| 346 | end |
| 347 | p = add p ulong k |
| 348 | end |
| 349 | end |
| 350 | |
| 351 | note ---- children: av = u16 count, then per argument u16 length and its bytes ---- |
| 352 | fn aclr to void |
| 353 | put ap 0 2 |
| 354 | put ap 1 0 |
| 355 | end |
| 356 | |
| 357 | fn apb to void |
| 358 | n = get pn 0 |
| 359 | p = get ap 0 |
| 360 | if gt add add p n 2 len av |
| 361 | fail 5 |
| 362 | end |
| 363 | put av p uchar and n 255 |
| 364 | put av add p 1 uchar shr n 8 |
| 365 | i = ulong 0 |
| 366 | while lt i n bound 4096 |
| 367 | put av add add p 2 i get pb i |
| 368 | i = add i 1 |
| 369 | end |
| 370 | put ap 0 add add p 2 n |
| 371 | put ap 1 add get ap 1 1 |
| 372 | end |
| 373 | |
| 374 | fn redir s int d int to void |
| 375 | if ne s d |
| 376 | if ne dupfd s d 0 long d |
| 377 | exit 127 |
| 378 | end |
| 379 | end |
| 380 | end |
| 381 | |
| 382 | note spawn: run av with fds fi, fo, fe as stdin, stdout, stderr; returns the exit status, or 256 + signal |
| 383 | fn spawn fi int fo int fe int to ulong |
| 384 | put av 0 uchar and get ap 1 255 |
| 385 | put av 1 uchar shr get ap 1 8 |
| 386 | pid = fork |
| 387 | if lt pid 0 |
| 388 | fail 6 |
| 389 | end |
| 390 | if eq pid 0 |
| 391 | redir fi 0 |
| 392 | redir fo 1 |
| 393 | redir fe 2 |
| 394 | r = execprog av 0 get ap 0 |
| 395 | exit 127 |
| 396 | end |
| 397 | fclo fi |
| 398 | fclo fo |
| 399 | fclo fe |
| 400 | r = waitpid int pid stb 0 len stb 0 |
| 401 | if ne r pid |
| 402 | fail 7 |
| 403 | end |
| 404 | sg = and ulong get stb 0 127 |
| 405 | if ne sg 0 |
| 406 | ret add 256 sg |
| 407 | end |
| 408 | ret ulong get stb 1 |
| 409 | end |
| 410 | |
| 411 | note ---- steps ---- |
| 412 | note mlen, mpat j, mrep j: the pattern and replacement of mutant mk (1 the wrong bit, 2 M-A) |
| 413 | fn mlen to ulong |
| 414 | if eq get mk 0 2 |
| 415 | ret len tmpa2 |
| 416 | end |
| 417 | ret len tmpat |
| 418 | end |
| 419 | |
| 420 | fn mpat j ulong to ulong |
| 421 | if eq get mk 0 2 |
| 422 | ret ulong get tmpa2 j |
| 423 | end |
| 424 | ret ulong get tmpat j |
| 425 | end |
| 426 | |
| 427 | fn mrep j ulong to ulong |
| 428 | if eq get mk 0 2 |
| 429 | ret ulong get tmre2 j |
| 430 | end |
| 431 | ret ulong get tmrep j |
| 432 | end |
| 433 | |
| 434 | note mkmut o n: replace the one occurrence of mutant mk's pattern in the source region [o, o+n) (equal lengths) |
| 435 | fn mkmut o ulong n ulong to void |
| 436 | m = mlen |
| 437 | hits = ulong 0 |
| 438 | hit = ulong 0 |
| 439 | i = o |
| 440 | while le add i m add o n bound 4194304 |
| 441 | if samepat i |
| 442 | hits = add hits 1 |
| 443 | hit = i |
| 444 | end |
| 445 | i = add i 1 |
| 446 | end |
| 447 | if ne hits 1 |
| 448 | fail 8 |
| 449 | end |
| 450 | j = ulong 0 |
| 451 | while lt j m bound 64 |
| 452 | put mem add hit j uchar mrep j |
| 453 | j = add j 1 |
| 454 | end |
| 455 | end |
| 456 | |
| 457 | fn samepat i ulong to bool |
| 458 | j = ulong 0 |
| 459 | while lt j mlen bound 64 |
| 460 | if ne ulong get mem add i j mpat j |
| 461 | ret false |
| 462 | end |
| 463 | j = add j 1 |
| 464 | end |
| 465 | ret true |
| 466 | end |
| 467 | |
| 468 | note unitpath p q: pb = the path of the ng.units line mem[p, q) (`HEX PATH`, 64 hex digits and one space) |
| 469 | fn unitpath p ulong q ulong to void |
| 470 | if or le q add p 65 ne get mem add p 64 32 |
| 471 | fail 17 |
| 472 | end |
| 473 | pclr |
| 474 | i = add p 65 |
| 475 | while lt i q bound 4096 |
| 476 | pb1 ulong get mem i |
| 477 | i = add i 1 |
| 478 | end |
| 479 | end |
| 480 | |
| 481 | note isa64: pb is seed/ng/src/a64.om (the unit the mutant changes) |
| 482 | fn isa64 to bool |
| 483 | if ne get pn 0 len ta64 |
| 484 | ret false |
| 485 | end |
| 486 | i = ulong 0 |
| 487 | while lt i len ta64 bound 64 |
| 488 | if ne get pb i get ta64 i |
| 489 | ret false |
| 490 | end |
| 491 | i = add i 1 |
| 492 | end |
| 493 | ret true |
| 494 | end |
| 495 | |
| 496 | note srcall: the units of ng.units concatenated into the source region, mutant mk (0 none) applied to a64.om; |
| 497 | note returns its length |
| 498 | fn srcall to ulong |
| 499 | pset 3 |
| 500 | nu = readin rinu 65536 |
| 501 | s = ulong 0 |
| 502 | p = rinu |
| 503 | e = add rinu nu |
| 504 | hit = ulong 0 |
| 505 | mut = ne get mk 0 0 |
| 506 | while lt p e bound 128 |
| 507 | q = p |
| 508 | while and lt q e ne get mem q 10 bound 4096 |
| 509 | q = add q 1 |
| 510 | end |
| 511 | unitpath p q |
| 512 | m = readin s sub rcas s |
| 513 | if and mut isa64 |
| 514 | mkmut s m |
| 515 | hit = add hit 1 |
| 516 | end |
| 517 | s = add s m |
| 518 | p = add q 1 |
| 519 | end |
| 520 | if and mut ne hit 1 |
| 521 | fail 8 |
| 522 | end |
| 523 | ret s |
| 524 | end |
| 525 | |
| 526 | note build m: scratch/enc.om and scratch/enc.bin (ng) with mutant m (0 none, 1 the wrong bit, 2 M-A) in a64.om |
| 527 | fn build m ulong to void |
| 528 | put mk 0 m |
| 529 | n = srcall |
| 530 | pscr 6 |
| 531 | fd = openw 420 |
| 532 | wall fd 0 n |
| 533 | fclo fd |
| 534 | pscr 6 |
| 535 | fi = openr |
| 536 | pscr 7 |
| 537 | fo = openw 493 |
| 538 | pscr 10 |
| 539 | fe = openw 420 |
| 540 | aclr |
| 541 | pset 1 |
| 542 | apb |
| 543 | if ne spawn fi fo fe 0 |
| 544 | fail 9 |
| 545 | end |
| 546 | end |
| 547 | |
| 548 | note runenc: scratch/enc.bin < enc.cases > words.bin; returns the word count |
| 549 | fn runenc to ulong |
| 550 | pset 4 |
| 551 | fi = openr |
| 552 | pscr 8 |
| 553 | fo = openw 420 |
| 554 | pscr 10 |
| 555 | fe = openw 420 |
| 556 | aclr |
| 557 | pscr 7 |
| 558 | apb |
| 559 | if ne spawn fi fo fe 0 |
| 560 | fail 10 |
| 561 | end |
| 562 | pscr 8 |
| 563 | n = readin rdis 8388608 |
| 564 | if ne and n 3 0 |
| 565 | fail 11 |
| 566 | end |
| 567 | ret shr n 2 |
| 568 | end |
| 569 | |
| 570 | note rundis: objdump -D -b binary -m aarch64 words.bin > dis.txt; returns its length (text at rdis) |
| 571 | fn rundis to ulong |
| 572 | pset 4 |
| 573 | fi = openr |
| 574 | pscr 9 |
| 575 | fo = openw 420 |
| 576 | pscr 10 |
| 577 | fe = openw 420 |
| 578 | aclr |
| 579 | pset 5 |
| 580 | apb |
| 581 | pset 11 |
| 582 | apb |
| 583 | pset 12 |
| 584 | apb |
| 585 | pset 13 |
| 586 | apb |
| 587 | pset 14 |
| 588 | apb |
| 589 | pset 15 |
| 590 | apb |
| 591 | pscr 8 |
| 592 | apb |
| 593 | if ne spawn fi fo fe 0 |
| 594 | fail 12 |
| 595 | end |
| 596 | pscr 9 |
| 597 | ret readin rdis 8388608 |
| 598 | end |
| 599 | |
| 600 | note ---- parsing the disassembly: dso[k], dsl[k] = start, length of the instruction text of the word at 4k ---- |
| 601 | fn ishex c ulong to bool |
| 602 | ret or and ge c 48 le c 57 and ge c 97 le c 102 |
| 603 | end |
| 604 | |
| 605 | fn hexv c ulong to ulong |
| 606 | if le c 57 |
| 607 | ret sub c 48 |
| 608 | end |
| 609 | ret sub c 87 |
| 610 | end |
| 611 | |
| 612 | note disline: the line [s, e) of the disassembly; if it is `<spaces><hex>:\t<word> \t<text>` record <text> |
| 613 | fn disline s ulong e ulong to void |
| 614 | p = s |
| 615 | while and lt p e eq get mem p 32 bound 64 |
| 616 | p = add p 1 |
| 617 | end |
| 618 | a = ulong 0 |
| 619 | q = p |
| 620 | while and lt q e ishex ulong get mem q bound 64 |
| 621 | a = or shl a 4 hexv ulong get mem q |
| 622 | q = add q 1 |
| 623 | end |
| 624 | if or eq q p ge add q 1 e |
| 625 | ret |
| 626 | end |
| 627 | if or ne get mem q 58 ne get mem add q 1 9 |
| 628 | ret |
| 629 | end |
| 630 | t = add q 2 |
| 631 | while and lt t e ne get mem t 9 bound 64 |
| 632 | t = add t 1 |
| 633 | end |
| 634 | t = add t 1 |
| 635 | if or ge t e ne and a 3 0 |
| 636 | ret |
| 637 | end |
| 638 | k = shr a 2 |
| 639 | if ge k len dso |
| 640 | fail 13 |
| 641 | end |
| 642 | put dso k t |
| 643 | put dsl k sub e t |
| 644 | if ge k get nd 0 |
| 645 | put nd 0 add k 1 |
| 646 | end |
| 647 | end |
| 648 | |
| 649 | fn disparse n ulong to void |
| 650 | put nd 0 0 |
| 651 | i = ulong 0 |
| 652 | while lt i len dsl bound 4096 |
| 653 | put dsl i 0 |
| 654 | put dso i 0 |
| 655 | i = add i 1 |
| 656 | end |
| 657 | p = rdis |
| 658 | e = add rdis n |
| 659 | while lt p e bound 1000000 |
| 660 | q = p |
| 661 | while and lt q e ne get mem q 10 bound 100000 |
| 662 | q = add q 1 |
| 663 | end |
| 664 | disline p q |
| 665 | p = add q 1 |
| 666 | end |
| 667 | end |
| 668 | |
| 669 | note ---- normalized text into ex: blanks (space, tab) collapsed to one space, trimmed ---- |
| 670 | fn exclr to void |
| 671 | put en 0 0 |
| 672 | end |
| 673 | |
| 674 | note exsp n: ex holds n > 0 bytes and the last is a blank (and/or are eager, so n = 0 is tested here) |
| 675 | fn exsp n ulong to bool |
| 676 | if eq n 0 |
| 677 | ret false |
| 678 | end |
| 679 | ret eq get ex sub n 1 32 |
| 680 | end |
| 681 | |
| 682 | fn ex1 c ulong to void |
| 683 | n = get en 0 |
| 684 | if ge n len ex |
| 685 | fail 14 |
| 686 | end |
| 687 | if or eq c 32 eq c 9 |
| 688 | if or eq n 0 exsp n |
| 689 | ret |
| 690 | end |
| 691 | put ex n 32 |
| 692 | else |
| 693 | put ex n uchar c |
| 694 | end |
| 695 | put en 0 add n 1 |
| 696 | end |
| 697 | |
| 698 | fn extrim to void |
| 699 | n = get en 0 |
| 700 | while exsp n bound 4096 |
| 701 | n = sub n 1 |
| 702 | end |
| 703 | put en 0 n |
| 704 | end |
| 705 | |
| 706 | fn exhex v ulong to void |
| 707 | ex1 48 |
| 708 | ex1 120 |
| 709 | d = ulong 0 |
| 710 | while ne shr v shl add d 1 2 0 bound 16 |
| 711 | d = add d 1 |
| 712 | end |
| 713 | go = true |
| 714 | while go bound 17 |
| 715 | h = and shr v shl d 2 15 |
| 716 | if lt h 10 |
| 717 | ex1 add 48 h |
| 718 | else |
| 719 | ex1 add 87 h |
| 720 | end |
| 721 | if eq d 0 |
| 722 | go = false |
| 723 | else |
| 724 | d = sub d 1 |
| 725 | end |
| 726 | end |
| 727 | end |
| 728 | |
| 729 | note expect: the expected text [s, e) of case k into ex, with @+N/@-N resolved |
| 730 | fn expect s ulong e ulong k ulong to void |
| 731 | exclr |
| 732 | p = s |
| 733 | while lt p e bound 4096 |
| 734 | c = ulong get mem p |
| 735 | if eq c 64 |
| 736 | p = exaddr add p 1 e k |
| 737 | else |
| 738 | ex1 c |
| 739 | p = add p 1 |
| 740 | end |
| 741 | end |
| 742 | extrim |
| 743 | end |
| 744 | |
| 745 | fn isdig q ulong e ulong to bool |
| 746 | if ge q e |
| 747 | ret false |
| 748 | end |
| 749 | c = ulong get mem q |
| 750 | ret and ge c 48 le c 57 |
| 751 | end |
| 752 | |
| 753 | fn exaddr p ulong e ulong k ulong to ulong |
| 754 | sg = ulong get mem p |
| 755 | q = add p 1 |
| 756 | v = ulong 0 |
| 757 | while isdig q e bound 20 |
| 758 | v = add mul v 10 sub ulong get mem q 48 |
| 759 | q = add q 1 |
| 760 | end |
| 761 | b = shl k 2 |
| 762 | if eq sg 43 |
| 763 | exhex add b v |
| 764 | else |
| 765 | exhex sub b v |
| 766 | end |
| 767 | ret q |
| 768 | end |
| 769 | |
| 770 | note same: ex equals the objdump text of word k (from dso[k], `//` comment dropped, normalized the same way) |
| 771 | fn same k ulong to bool |
| 772 | if ge k get nd 0 |
| 773 | ret false |
| 774 | end |
| 775 | o = get dso k |
| 776 | e = add o get dsl k |
| 777 | i = o |
| 778 | while and lt i e not slash2 i e bound 4096 |
| 779 | i = add i 1 |
| 780 | end |
| 781 | ret cmpex get en 0 o i |
| 782 | end |
| 783 | |
| 784 | fn slash2 i ulong e ulong to bool |
| 785 | if ge add i 1 e |
| 786 | ret false |
| 787 | end |
| 788 | ret and eq get mem i 47 eq get mem add i 1 47 |
| 789 | end |
| 790 | |
| 791 | note cmpex: ex[0, m) against mem [o, i) with blanks collapsed, leading and trailing blanks dropped |
| 792 | fn cmpex m ulong o ulong i ulong to bool |
| 793 | j = ulong 0 |
| 794 | p = o |
| 795 | pend = false |
| 796 | while lt p i bound 4096 |
| 797 | c = ulong get mem p |
| 798 | if or eq c 9 eq c 32 |
| 799 | if gt j 0 |
| 800 | pend = true |
| 801 | end |
| 802 | else |
| 803 | if pend |
| 804 | if or ge j m ne get ex j 32 |
| 805 | ret false |
| 806 | end |
| 807 | j = add j 1 |
| 808 | pend = false |
| 809 | end |
| 810 | if or ge j m ne ulong get ex j c |
| 811 | ret false |
| 812 | end |
| 813 | j = add j 1 |
| 814 | end |
| 815 | p = add p 1 |
| 816 | end |
| 817 | ret eq j m |
| 818 | end |
| 819 | |
| 820 | note ---- comparing every case ---- |
| 821 | note cmpall verbose: rg[0] cases, rg[1] matches; prints each mismatch when verbose |
| 822 | fn cmpall vb bool to void |
| 823 | put rg 0 0 |
| 824 | put rg 1 0 |
| 825 | n = get rg 4 |
| 826 | p = rcas |
| 827 | e = add rcas n |
| 828 | while and lt p e ne get mem p 10 bound 4096 |
| 829 | p = add p 1 |
| 830 | end |
| 831 | ln = ulong 1 |
| 832 | while lt p e bound 1000000 |
| 833 | p = add p 1 |
| 834 | ln = add ln 1 |
| 835 | q = p |
| 836 | while and lt q e ne get mem q 10 bound 100000 |
| 837 | q = add q 1 |
| 838 | end |
| 839 | cmpline p q ln vb |
| 840 | p = q |
| 841 | end |
| 842 | end |
| 843 | |
| 844 | fn isblk p ulong e ulong to bool |
| 845 | if ge p e |
| 846 | ret false |
| 847 | end |
| 848 | c = ulong get mem p |
| 849 | ret or eq c 32 eq c 9 |
| 850 | end |
| 851 | |
| 852 | note barat: the `|` of the case line [p, e) (fail 15 if none) |
| 853 | fn barat p ulong e ulong to ulong |
| 854 | b = p |
| 855 | while and lt b e ne get mem b 124 bound 4096 |
| 856 | b = add b 1 |
| 857 | end |
| 858 | if ge b e |
| 859 | fail 15 |
| 860 | end |
| 861 | ret b |
| 862 | end |
| 863 | |
| 864 | fn cmpline s ulong e ulong ln ulong vb bool to void |
| 865 | p = s |
| 866 | while isblk p e bound 4096 |
| 867 | p = add p 1 |
| 868 | end |
| 869 | if ge p e |
| 870 | ret |
| 871 | end |
| 872 | if eq get mem p 59 |
| 873 | ret |
| 874 | end |
| 875 | b = barat p e |
| 876 | k = get rg 0 |
| 877 | put rg 0 add k 1 |
| 878 | expect add b 1 e k |
| 879 | if same k |
| 880 | put rg 1 add get rg 1 1 |
| 881 | else |
| 882 | if vb |
| 883 | showbad ln k |
| 884 | end |
| 885 | end |
| 886 | end |
| 887 | |
| 888 | fn showbad ln ulong k ulong to void |
| 889 | omsg 0 |
| 890 | oput 32 |
| 891 | odec ln |
| 892 | oput 10 |
| 893 | omsg 1 |
| 894 | oput 58 |
| 895 | oput 32 |
| 896 | i = ulong 0 |
| 897 | while lt i get en 0 bound 4096 |
| 898 | oput ulong get ex i |
| 899 | i = add i 1 |
| 900 | end |
| 901 | oput 10 |
| 902 | omsg 2 |
| 903 | oput 58 |
| 904 | oput 32 |
| 905 | if lt k get nd 0 |
| 906 | obig get dso k get dsl k |
| 907 | end |
| 908 | oput 10 |
| 909 | oflush |
| 910 | end |
| 911 | |
| 912 | note ---- step 6: refusal cases (rg: 2 cases, 3 refused as written, 5 mode line length, 6 status, 7 stderr bytes) ---- |
| 913 | note rfall vb: run every case of enc-refuse.cases alone; prints each case that does not hold when vb |
| 914 | fn rfall vb bool to void |
| 915 | pset 16 |
| 916 | n = readin rref sub rone rref |
| 917 | put rg 2 0 |
| 918 | put rg 3 0 |
| 919 | p = rref |
| 920 | e = add rref n |
| 921 | while and lt p e ne get mem p 10 bound 4096 |
| 922 | p = add p 1 |
| 923 | end |
| 924 | put rg 5 add sub p rref 1 |
| 925 | ln = ulong 1 |
| 926 | while lt p e bound 1000000 |
| 927 | p = add p 1 |
| 928 | ln = add ln 1 |
| 929 | q = p |
| 930 | while and lt q e ne get mem q 10 bound 100000 |
| 931 | q = add q 1 |
| 932 | end |
| 933 | rfline p q ln vb |
| 934 | p = q |
| 935 | end |
| 936 | end |
| 937 | |
| 938 | fn rfline s ulong e ulong ln ulong vb bool to void |
| 939 | p = s |
| 940 | while isblk p e bound 4096 |
| 941 | p = add p 1 |
| 942 | end |
| 943 | if ge p e |
| 944 | ret |
| 945 | end |
| 946 | if eq get mem p 59 |
| 947 | ret |
| 948 | end |
| 949 | b = barat p e |
| 950 | put rg 2 add get rg 2 1 |
| 951 | rfwr p b |
| 952 | if and rfrun rfsame add b 1 e |
| 953 | put rg 3 add get rg 3 1 |
| 954 | else |
| 955 | if vb |
| 956 | rfbad ln |
| 957 | end |
| 958 | end |
| 959 | end |
| 960 | |
| 961 | note rfwr p b: scratch/one.cases = the mode line of enc-refuse.cases, then the case mem[p, b) without trailing blanks |
| 962 | fn rfwr p ulong b ulong to void |
| 963 | m = get rg 5 |
| 964 | i = ulong 0 |
| 965 | while lt i m bound 4096 |
| 966 | put mem add rone i get mem add rref i |
| 967 | i = add i 1 |
| 968 | end |
| 969 | q = b |
| 970 | while and gt q p isblk sub q 1 b bound 4096 |
| 971 | q = sub q 1 |
| 972 | end |
| 973 | j = p |
| 974 | while lt j q bound 4096 |
| 975 | put mem add rone i get mem j |
| 976 | i = add i 1 |
| 977 | j = add j 1 |
| 978 | end |
| 979 | put mem add rone i 10 |
| 980 | pscr 17 |
| 981 | fd = openw 420 |
| 982 | wall fd rone add i 1 |
| 983 | fclo fd |
| 984 | end |
| 985 | |
| 986 | note rfrun: scratch/enc.bin < one.cases > words.bin 2> enc.err; true when it exits 5 and writes no word |
| 987 | fn rfrun to bool |
| 988 | pscr 17 |
| 989 | fi = openr |
| 990 | pscr 8 |
| 991 | fo = openw 420 |
| 992 | pscr 10 |
| 993 | fe = openw 420 |
| 994 | aclr |
| 995 | pscr 7 |
| 996 | apb |
| 997 | st = spawn fi fo fe |
| 998 | put rg 6 st |
| 999 | pscr 10 |
| 1000 | put rg 7 readin rerr 4096 |
| 1001 | pscr 8 |
| 1002 | nw = readin rdis 8388608 |
| 1003 | ret and eq st 5 eq nw 0 |
| 1004 | end |
| 1005 | |
| 1006 | note rfsame s e: ng's stderr is the text mem[s, e) (blanks trimmed at both ends) and one newline |
| 1007 | fn rfsame s ulong e ulong to bool |
| 1008 | n = get rg 7 |
| 1009 | p = s |
| 1010 | while isblk p e bound 4096 |
| 1011 | p = add p 1 |
| 1012 | end |
| 1013 | q = e |
| 1014 | while and gt q p isblk sub q 1 e bound 4096 |
| 1015 | q = sub q 1 |
| 1016 | end |
| 1017 | if ne add sub q p 1 n |
| 1018 | ret false |
| 1019 | end |
| 1020 | i = ulong 0 |
| 1021 | while lt i sub q p bound 4096 |
| 1022 | if ne get mem add p i get mem add rerr i |
| 1023 | ret false |
| 1024 | end |
| 1025 | i = add i 1 |
| 1026 | end |
| 1027 | ret eq get mem add rerr sub n 1 10 |
| 1028 | end |
| 1029 | |
| 1030 | fn rfbad ln ulong to void |
| 1031 | omsg 10 |
| 1032 | oput 32 |
| 1033 | odec ln |
| 1034 | oput 10 |
| 1035 | omsg 14 |
| 1036 | oput 32 |
| 1037 | odec get rg 6 |
| 1038 | oput 10 |
| 1039 | omsg 2 |
| 1040 | oput 58 |
| 1041 | oput 32 |
| 1042 | obig rerr get rg 7 |
| 1043 | oput 10 |
| 1044 | oflush |
| 1045 | end |
| 1046 | |
| 1047 | fn sline k ulong v ulong to void |
| 1048 | omsg k |
| 1049 | oput 32 |
| 1050 | odec v |
| 1051 | oput 10 |
| 1052 | end |
| 1053 | |
| 1054 | note summary: rg 8 cases, 9 matches, 11 words, 10 mutant mismatches, 13 refusal cases, 14 held, 15 M-A failures |
| 1055 | fn summary to void |
| 1056 | sline 3 get rg 8 |
| 1057 | sline 4 get rg 9 |
| 1058 | sline 6 get rg 11 |
| 1059 | sline 5 get rg 10 |
| 1060 | sline 11 get rg 13 |
| 1061 | sline 12 get rg 14 |
| 1062 | sline 13 get rg 15 |
| 1063 | end |
| 1064 | |
| 1065 | fn rdscr to void |
| 1066 | pclr |
| 1067 | n = read 0 mem rin 4096 |
| 1068 | if le n 0 |
| 1069 | fail 16 |
| 1070 | end |
| 1071 | i = ulong 0 |
| 1072 | while and lt i ulong n ne get mem add rin i 10 bound 4096 |
| 1073 | put sd i get mem add rin i |
| 1074 | i = add i 1 |
| 1075 | end |
| 1076 | if or eq i 0 ge i 1000 |
| 1077 | fail 16 |
| 1078 | end |
| 1079 | put sn 0 i |
| 1080 | end |
| 1081 | |
| 1082 | fn main to int |
| 1083 | rdscr |
| 1084 | pset 4 |
| 1085 | put rg 4 readin rcas sub rdis rcas |
| 1086 | build 0 |
| 1087 | w = runenc |
| 1088 | disparse rundis |
| 1089 | cmpall true |
| 1090 | cs = get rg 0 |
| 1091 | put rg 8 cs |
| 1092 | put rg 9 get rg 1 |
| 1093 | put rg 11 w |
| 1094 | ok = and eq get rg 1 cs eq w cs |
| 1095 | rfall true |
| 1096 | put rg 13 get rg 2 |
| 1097 | put rg 14 get rg 3 |
| 1098 | ok = and ok and gt get rg 2 0 eq get rg 3 get rg 2 |
| 1099 | build 1 |
| 1100 | w2 = runenc |
| 1101 | disparse rundis |
| 1102 | cmpall false |
| 1103 | mm = sub get rg 0 get rg 1 |
| 1104 | put rg 10 mm |
| 1105 | ok = and ok and gt mm 0 eq w2 cs |
| 1106 | build 2 |
| 1107 | rfall false |
| 1108 | rm = sub get rg 2 get rg 3 |
| 1109 | put rg 15 rm |
| 1110 | ok = and ok gt rm 0 |
| 1111 | summary |
| 1112 | if ok |
| 1113 | omsg 7 |
| 1114 | oput 10 |
| 1115 | oflush |
| 1116 | ret 0 |
| 1117 | end |
| 1118 | omsg 8 |
| 1119 | oput 10 |
| 1120 | oflush |
| 1121 | ret 1 |
| 1122 | end |