Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-train/blob/main/tools/omegallm/wdrive/wbuild.om # aien-train AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-train / tools / omegallm / wdrive / wbuild.om` 1418 lines ยท 29263 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note wbuild.om (DRA-187, lane wsp): build driver for the Omega tools in tools/omegallm, and the 3 note shared driver library of tools/omegallm/wdrive (wrun, wcheck, wpolicy are composed with this 4 note unit's main renamed to wbmain). Bootstrap, from the aien-train checkout: 5 note OMC < tools/omegallm/wdrive/wbuild.om > wbuild && chmod +x wbuild 6 note Usage: wbuild OUTBIN PART... (environment OMC = the pinned omc, CORE = taskset core, default 0) 7 note A PART is FILE, appended as is, or FILE@NAME, appended with its one line `fn main to int` 8 note renamed to `fn NAME to int` (exit 4 unless that line occurs exactly once). Either may end in 9 note =HEX64 (FILE=HEX64, FILE@NAME=HEX64): 64 lowercase hex digits, the file's pinned sha256. The bytes 10 note read are hashed each time the part is read, before they are used; a mismatch prints 11 note PART_PIN_MISMATCH FILE on stderr and exits 3 before anything is compiled. A PART with an 12 note `=` whose text after the last `=` is not 64 lowercase hex digits prints PART_PIN_BAD PART and 13 note exits 2. Composition: 14 note when any part has a line `om2`, the output starts with one line `om2`, then every 15 note `layout ... end` block of every part in order, then the parts without their `om2` lines and 16 note layout blocks. Every line starting `export fn ` is written as `fn ` (the output is a program, 17 note and a program has no exports; om/sha256/sha256-om2.om has four). With no layout blocks, no 18 note exports and `om2` only as the first line of the first part, this is plain concatenation (cat, 19 note sed main rename) as tools/omegallm/wprobe/build.sh did. 20 note Writes OUTBIN-all.om, runs `taskset -c CORE nice -n 19 OMC < OUTBIN-all.om > OUTBIN.tmp`, 21 note sets mode 0775, renames OUTBIN.tmp to OUTBIN and prints 22 note BUILD_OK OUTBIN source_sha256 omc 23 note Exits: 0 built, 1 omc refused the unit (stderr BUILD_FAIL omc rc=N, OUTBIN.tmp removed), 2 usage 24 note or a malformed pin, 3 OMC is not the pinned compiler (stderr OMC_PIN_MISMATCH ) or a part 25 note does not match its pin (PART_PIN_MISMATCH), 4 main count in a renamed part is not 1, 61 a part 26 note over 8 MiB, 70 I/O. 27 note The SHA-256 below is omega-mv `e5b32567` om/sha256/sha256-om2.om (sha256 `dbf002dd6aa36585632b9d01d` 28 note `98ac3f0bd2fa0f0475082940de53638847994bb`), composed as aien-train-wpatch tools/omegallm/wpatch/ 29 note build.sh composes it: the layout shactx first, `export fn` made `fn`, private names renamed 30 note (kinit wskinit, iv wsiv, sched wssched, rounds wsround, compress wscomp, run wsrun, blocks wsblks, 31 note finish wsfin, tobuf wstobuf, absorb wsabsb). The lines from the next `layout` to the line 32 note before `note wdrive library` are byte for byte that build's sha256-layout.om followed by its 33 note sha256-functions.om. 34 om2 35 layout shactx 104 36 field state uint 8 at 0 little 37 field count ulong at 32 little 38 field buf uchar 64 at 40 little 39 end 40 note SPDX-License-Identifier: Apache-2.0 41 note sha256-om2.om: FIPS 180-4 SHA-256 in OM-2 (spec/omegese-om2.md, #747/#748, ledger L838). 42 note The four entries are the API of src/sha256.h, exported through omega-bridge ABI v2 as 43 note omega2_sha256_init/update/final/hash; the bridge's generated omega_api.h maps the C names. 44 note 45 note Invariants: 46 note - All hashing state is the caller's 104-byte context (layout shactx below, equal to sha256_ctx 47 note on LP64 AArch64). Nothing else persists between calls, so a byte copy of a live context is an 48 note equivalent context (erratum E3). 49 note - count is the number of bytes absorbed since init, modulo 2^64. buf[0 .. count mod 64) holds the 50 note pending bytes of the current block; the rest of buf is unspecified. state is the chaining value 51 note after the count div 64 whole blocks. 52 note - final reads the context and writes only the digest, so the context is unchanged by it. The 53 note contract promises less (after final the context is unspecified, memory safety only). 54 note - Every branch and every loop count depends only on lengths, never on message bytes (section 8). 55 note - Indices into the unsized input view are checked at run time (trap 3); indices into sized 56 note arrays inside unrolled bodies are constants and checked when compiled. 57 note bridge c-type shactx sha256_ctx 58 note bridge c-define SHA256_DIGEST_SIZE 32 59 note bridge c-define SHA256_BLOCK_SIZE 64 60 61 62 note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes. 63 note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed). 64 fn wskinit k edit uint 64 to void 65 put k 0 0x428a2f98 66 put k 1 0x71374491 67 put k 2 0xb5c0fbcf 68 put k 3 0xe9b5dba5 69 put k 4 0x3956c25b 70 put k 5 0x59f111f1 71 put k 6 0x923f82a4 72 put k 7 0xab1c5ed5 73 put k 8 0xd807aa98 74 put k 9 0x12835b01 75 put k 10 0x243185be 76 put k 11 0x550c7dc3 77 put k 12 0x72be5d74 78 put k 13 0x80deb1fe 79 put k 14 0x9bdc06a7 80 put k 15 0xc19bf174 81 put k 16 0xe49b69c1 82 put k 17 0xefbe4786 83 put k 18 0x0fc19dc6 84 put k 19 0x240ca1cc 85 put k 20 0x2de92c6f 86 put k 21 0x4a7484aa 87 put k 22 0x5cb0a9dc 88 put k 23 0x76f988da 89 put k 24 0x983e5152 90 put k 25 0xa831c66d 91 put k 26 0xb00327c8 92 put k 27 0xbf597fc7 93 put k 28 0xc6e00bf3 94 put k 29 0xd5a79147 95 put k 30 0x06ca6351 96 put k 31 0x14292967 97 put k 32 0x27b70a85 98 put k 33 0x2e1b2138 99 put k 34 0x4d2c6dfc 100 put k 35 0x53380d13 101 put k 36 0x650a7354 102 put k 37 0x766a0abb 103 put k 38 0x81c2c92e 104 put k 39 0x92722c85 105 put k 40 0xa2bfe8a1 106 put k 41 0xa81a664b 107 put k 42 0xc24b8b70 108 put k 43 0xc76c51a3 109 put k 44 0xd192e819 110 put k 45 0xd6990624 111 put k 46 0xf40e3585 112 put k 47 0x106aa070 113 put k 48 0x19a4c116 114 put k 49 0x1e376c08 115 put k 50 0x2748774c 116 put k 51 0x34b0bcb5 117 put k 52 0x391c0cb3 118 put k 53 0x4ed8aa4a 119 put k 54 0x5b9cca4f 120 put k 55 0x682e6ff3 121 put k 56 0x748f82ee 122 put k 57 0x78a5636f 123 put k 58 0x84c87814 124 put k 59 0x8cc70208 125 put k 60 0x90befffa 126 put k 61 0xa4506ceb 127 put k 62 0xbef9a3f7 128 put k 63 0xc67178f2 129 end 130 131 note H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes 132 note (fixtures/sha256-iv.dec). 133 fn wsiv s edit uint 8 to void 134 put s 0 0x6a09e667 135 put s 1 0xbb67ae85 136 put s 2 0x3c6ef372 137 put s 3 0xa54ff53a 138 put s 4 0x510e527f 139 put s 5 0x9b05688c 140 put s 6 0x1f83d9ab 141 put s 7 0x5be0cd19 142 end 143 144 note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read. 145 note The compiler proves that every get of w reads an element written earlier in this function 146 note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence. 147 fn wssched w edit uint 64 fresh m view uchar * o ulong to void 148 q = ulong 0 149 x = uint 0 150 y = uint 0 151 for i ulong 16 unroll 152 q = add o mul i 4 153 x = uint get m q 154 x = or shl x 8 uint get m add q 1 155 x = or shl x 8 uint get m add q 2 156 put w i or shl x 8 uint get m add q 3 157 end 158 for i ulong 48 unroll 159 x = get w add i 1 160 y = get w add i 14 161 x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 162 put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 163 end 164 end 165 166 note The 64 rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant 167 note in each copy, so get kt i and get w i need no run-time check. uint arithmetic wraps mod 2^32. 168 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) = 169 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 170 note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one. 171 note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and 172 note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines). 173 fn wsround s edit uint 8 w view uint 64 kt view uint 64 to void 174 a = get s 0 175 b = get s 1 176 c = get s 2 177 d = get s 3 178 e = get s 4 179 f = get s 5 180 g = get s 6 181 h = get s 7 182 t = uint 0 183 for i ulong 64 unroll 184 h = add h get kt i 185 h = add h get w i 186 t = xor rotr e 6 rotr e 11 187 t = xor t rotr e 25 188 h = add h t 189 h = add h xor g and e xor f g 190 d = add d h 191 t = xor rotr a 2 rotr a 13 192 t = xor t rotr a 22 193 h = add h t 194 t = and c or a b 195 h = add h or and a b t 196 t = h 197 h = g 198 g = f 199 f = e 200 e = d 201 d = c 202 c = b 203 b = a 204 a = t 205 end 206 put s 0 add get s 0 a 207 put s 1 add get s 1 b 208 put s 2 add get s 2 c 209 put s 3 add get s 3 d 210 put s 4 add get s 4 e 211 put s 5 add get s 5 f 212 put s 6 add get s 6 g 213 put s 7 add get s 7 h 214 end 215 216 note One block m[o .. o+64) into s. sched and rounds stay calls: inlined, their names would share 217 note one 16-register frame with this function's parameters (no spilling, error 12). 218 fn wscomp s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void 219 wssched w m o 220 wsround s w kt 221 end 222 223 note The whole blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes). 224 fn wsrun s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void 225 for j ulong shr len m 6 226 wscomp s w kt m shl j 6 227 end 228 end 229 230 note The whole blocks of m (len m a multiple of 64). A rolled loop traps 4 after `1000000` passes 231 note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 blocks): 232 note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches `1000000` 233 note only above 1.7e19 bytes, which no AArch64 address space holds. 234 fn wsblks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void 235 q = ulong 0 236 for a ulong shr len m 44 237 for b ulong 0x80000 238 wsrun s w kt part m q 0x2000000 239 q = add q 0x2000000 240 end 241 end 242 for b ulong and shr len m 25 0x7ffff 243 wsrun s w kt part m q 0x2000000 244 q = add q 0x2000000 245 end 246 wsrun s w kt part m q sub len m q 247 end 248 249 note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the 250 note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two blocks. 251 fn wsfin s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void 252 put p k 0x80 253 e = ulong 64 254 if ge k 56 255 e = 128 256 end 257 bits = shl n 3 258 for i ulong 8 unroll 259 put p sub e add i 1 uchar and shr bits mul i 8 0xff 260 end 261 wscomp s w kt view p 0 262 if eq e 128 263 wscomp s w kt view p 64 264 end 265 end 266 267 fn sha256_init c edit shactx to void 268 s = new uint 8 0 269 wsiv s 270 for i ulong 8 unroll 271 put c state i get s i 272 end 273 put c count 0 274 end 275 276 note k bytes of d from o into the context's buffer at bi (bi + k <= 64). A function of its own: inside 277 note absorb the copy loop needs more than the 16 registers (error 12 under compiler-s4). 278 fn wstobuf c edit shactx bi ulong d view uchar * o ulong k ulong to void 279 for i ulong k 280 put c buf add bi i get d add o i 281 end 282 end 283 284 note Whole blocks are hashed straight from d; the bytes that complete a pending block go through blk. 285 fn wsabsb c edit shactx d view uchar * bi ulong to void 286 n = len d 287 s = new uint 8 0 288 w = new uint 64 0 289 kt = new uint 64 0 290 blk = new uchar 64 0 291 wskinit kt 292 for i ulong 8 unroll 293 put s i get c state i 294 end 295 off = ulong 0 296 if gt bi 0 297 off = sub 64 bi 298 for i ulong bi 299 put blk i get c buf i 300 end 301 for i ulong off 302 put blk add bi i get d i 303 end 304 wscomp s w kt view blk 0 305 end 306 nb = shr sub n off 6 307 wsblks s w kt part d off shl nb 6 308 off = add off shl nb 6 309 wstobuf c 0 d off sub n off 310 for i ulong 8 unroll 311 put c state i get s i 312 end 313 end 314 315 note Any length, including 0 and >= 2^32. lt n sub 64 bi cannot wrap (bi < 64); add bi n could. 316 fn sha256_update c edit shactx d view uchar * to void 317 n = len d 318 cnt = get c count 319 bi = and cnt 63 320 put c count add cnt n 321 if lt n sub 64 bi 322 wstobuf c bi d 0 n 323 ret 324 end 325 wsabsb c d bi 326 end 327 328 fn sha256_final c edit shactx o out uchar 32 to void 329 s = new uint 8 0 330 w = new uint 64 0 331 kt = new uint 64 0 332 p = new uchar 128 0 333 v = ulong 0 334 wskinit kt 335 for i ulong 8 unroll 336 put s i get c state i 337 end 338 cnt = get c count 339 k = and cnt 63 340 for i ulong k 341 put p i get c buf i 342 end 343 wsfin s w kt p k cnt 344 for i ulong 8 unroll 345 v = ulong get s i 346 for j ulong 4 unroll 347 put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff 348 end 349 end 350 end 351 352 note Equal to init, update(d), final on a fresh context (section 7), with the state in locals. o may 353 note overlap d: the binding copies o out only after the call returns status 0. 354 fn sha256_hash d view uchar * o out uchar 32 to void 355 s = new uint 8 0 356 w = new uint 64 0 357 kt = new uint 64 0 358 p = new uchar 128 0 359 v = ulong 0 360 wskinit kt 361 wsiv s 362 n = len d 363 nb = shr n 6 364 wsblks s w kt part d 0 shl nb 6 365 off = shl nb 6 366 k = sub n off 367 for i ulong k 368 put p i get d add off i 369 end 370 wsfin s w kt p k n 371 for i ulong 8 unroll 372 v = ulong get s i 373 for j ulong 4 unroll 374 put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff 375 end 376 end 377 end 378 379 note wdrive library. Globals are named wd*, functions wd*. Host words take these globals directly; 380 note an Omega function never receives a global as an argument (error 19). 381 note wdab argument and scratch bytes; wdp/wdpn the path builder; wdk/wdkn the execprog packed list 382 note (count, write position); wdo/wdon/wdofd the output buffer and the descriptor it flushes to when 383 note full; wdsrc/wdsn a whole file; wdd the last digest; wdv/wdvn the last environment value. 384 global wdab uchar 4096 385 global wdp uchar 4096 386 global wdpn ulong 1 387 global wdk uchar 4096 388 global wdkn ulong 2 389 global wdo uchar 65536 390 global wdon ulong 1 391 global wdofd int 1 392 global wdsrc uchar `8388608` 393 global wdsn ulong 1 394 global wdd uchar 32 395 global wdv uchar 4096 396 global wdvn ulong 1 397 global wdst uchar 4 398 note wdlkf: the held pool lock's descriptor plus 1, 0 when none (closed in spawned children, as 399 note flock -o does) 400 global wdlkf long 1 401 global wdt uchar 24 402 text wdhex "`0123456789abcdef`" 403 404 note argument i into wdab; returns its length (exit 2 when it does not fit) 405 fn wdarg i ulong to ulong 406 r = argread i wdab 0 4096 407 if lt r 0 408 exit 2 409 end 410 ret ulong r 411 end 412 413 fn wdpclr to void 414 put wdpn 0 0 415 end 416 417 fn wdpc c ulong to void 418 n = get wdpn 0 419 if ge n 4095 420 exit 2 421 end 422 put wdp n uchar c 423 put wdpn 0 add n 1 424 end 425 426 fn wdpt s view uchar * to void 427 for i ulong len s 428 wdpc ulong get s i 429 end 430 end 431 432 fn wdpa i ulong to void 433 n = wdarg i 434 for j ulong n 435 wdpc ulong get wdab j 436 end 437 end 438 439 fn wdpv to void 440 for j ulong get wdvn 0 441 wdpc ulong get wdv j 442 end 443 end 444 445 note wdsrc[o .. o + n) onto the path 446 fn wdpb o ulong n ulong to void 447 for j ulong n 448 wdpc ulong get wdsrc add o j 449 end 450 end 451 452 note decimal digits of v into wdt, least significant first; returns the count 453 fn wddec v ulong to ulong 454 k = ulong 0 455 x = v 456 c = true 457 while c bound 25 458 put wdt k uchar add 48 rem x 10 459 k = add k 1 460 x = div x 10 461 c = gt x 0 462 end 463 ret k 464 end 465 466 fn wdpd v ulong to void 467 k = wddec v 468 while gt k 0 bound 25 469 k = sub k 1 470 wdpc ulong get wdt k 471 end 472 end 473 474 note open the path in wdp; returns the descriptor or minus errno 475 fn wdopen fl int md int to int 476 r = fopen wdp 0 get wdpn 0 fl md 477 ret int r 478 end 479 480 fn wdexists to bool 481 r = faccess wdp 0 get wdpn 0 0 482 ret eq r 0 483 end 484 485 note packed list for execprog: u16 count, then per record u16 length and bytes 486 fn wdkclr to void 487 put wdkn 0 0 488 put wdkn 1 2 489 end 490 491 note push the path builder's bytes as the next record 492 fn wdkp to void 493 c = get wdkn 0 494 p = get wdkn 1 495 n = get wdpn 0 496 if or ge c 64 gt add add p 2 n 4096 497 exit 2 498 end 499 put wdk p uchar and n 255 500 put wdk add p 1 uchar shr n 8 501 for j ulong n 502 put wdk add add p 2 j get wdp j 503 end 504 put wdkn 0 add c 1 505 put wdkn 1 add add p 2 n 506 end 507 508 fn wdkt s view uchar * to void 509 wdpclr 510 wdpt s 511 wdkp 512 end 513 514 fn wdka i ulong to void 515 wdpclr 516 wdpa i 517 wdkp 518 end 519 520 fn wdkfin to ulong 521 c = get wdkn 0 522 put wdk 0 uchar and c 255 523 put wdk 1 uchar shr c 8 524 ret get wdkn 1 525 end 526 527 note fork and exec the packed list (empty environment, no PATH search); the child takes fi as 528 note stdin, fo as stdout and fe as stderr when they are not negative. Returns the child's pid. 529 fn wdspawn fi int fo int fe int to long 530 pn = wdkfin 531 pid = fork 532 if lt pid 0 533 exit 70 534 end 535 if eq pid 0 536 if gt get wdlkf 0 0 537 cl = fclose int sub get wdlkf 0 1 538 end 539 if ge fi 0 540 d0 = dupfd fi 0 0 541 end 542 if ge fo 0 543 d1 = dupfd fo 1 0 544 end 545 if ge fe 0 546 d2 = dupfd fe 2 0 547 end 548 e = execprog wdk 0 pn 549 exit 127 550 end 551 ret pid 552 end 553 554 note wait for pid; its exit status, or 128 + the signal that ended it 555 fn wdwait pid long to long 556 r = waitpid int pid wdst 0 4 0 557 while eq r -4 bound 1000 558 r = waitpid int pid wdst 0 4 0 559 end 560 if lt r 0 561 exit 70 562 end 563 s = uint get wdst 0 564 s = or s shl uint get wdst 1 8 565 s = or s shl uint get wdst 2 16 566 s = or s shl uint get wdst 3 24 567 if eq and s 127 0 568 ret long and shr s 8 255 569 end 570 ret add long and s 127 128 571 end 572 573 note value of environment entry NM=VALUE into wdv/wdvn; false when absent 574 fn wdenv nm view uchar * to bool 575 c = ulong envcnt 576 for i ulong c 577 r = envread i wdv 0 4096 578 if gt r long len nm 579 if wdenvm nm ulong r 580 ret true 581 end 582 end 583 end 584 ret false 585 end 586 587 fn wdenvm nm view uchar * r ulong to bool 588 k = len nm 589 for i ulong k 590 if ne get wdv i get nm i 591 ret false 592 end 593 end 594 if ne get wdv k 61 595 ret false 596 end 597 m = sub r add k 1 598 for j ulong m 599 put wdv j get wdv add add k 1 j 600 end 601 put wdvn 0 m 602 ret true 603 end 604 605 note the output buffer; wdoc flushes to wdofd when full 606 fn wdowr fd int to void 607 n = get wdon 0 608 t = ulong 0 609 while lt t n bound 999999 610 w = write fd wdo t sub n t 611 if le w 0 612 exit 70 613 end 614 t = add t ulong w 615 end 616 end 617 618 fn wdoclr to void 619 put wdon 0 0 620 end 621 622 fn wdoflush fd int to void 623 wdowr fd 624 wdoclr 625 end 626 627 fn wdoc c ulong to void 628 n = get wdon 0 629 if ge n 65536 630 wdoflush get wdofd 0 631 n = 0 632 end 633 put wdo n uchar c 634 put wdon 0 add n 1 635 end 636 637 fn wdot s view uchar * to void 638 for i ulong len s 639 wdoc ulong get s i 640 end 641 end 642 643 fn wdod v ulong to void 644 k = wddec v 645 while gt k 0 bound 25 646 k = sub k 1 647 wdoc ulong get wdt k 648 end 649 end 650 651 fn wdop to void 652 for j ulong get wdpn 0 653 wdoc ulong get wdp j 654 end 655 end 656 657 fn wdohex to void 658 for i ulong 32 659 v = ulong get wdd i 660 wdoc ulong get wdhex shr v 4 661 wdoc ulong get wdhex and v 15 662 end 663 end 664 665 note true when the hex of wdd equals the 64 bytes of h 666 fn wdhexeq h view uchar * to bool 667 if ne len h 64 668 ret false 669 end 670 for i ulong 32 671 v = ulong get wdd i 672 if ne get h mul i 2 get wdhex shr v 4 673 ret false 674 end 675 if ne get h add mul i 2 1 get wdhex and v 15 676 ret false 677 end 678 end 679 ret true 680 end 681 682 note read the whole file named by wdp into wdsrc; returns its size or minus errno; exit 61 when it 683 note is larger than wdsrc 684 fn wdload to long 685 b = new uchar 1 0 686 fd = wdopen 0 0 687 if lt fd 0 688 ret long fd 689 end 690 t = ulong 0 691 fin = false 692 while and lt t len wdsrc not fin bound 999999 693 r = read fd wdsrc t sub len wdsrc t 694 if lt r 0 695 c = fclose fd 696 ret r 697 end 698 if eq r 0 699 fin = true 700 end 701 t = add t ulong r 702 end 703 if not fin 704 r2 = read fd b 0 1 705 if ne r2 0 706 exit 61 707 end 708 end 709 c2 = fclose fd 710 put wdsn 0 t 711 ret long t 712 end 713 714 note SHA-256 of the file named by wdp into wdd; false when it cannot be opened, exit 70 on a read error 715 fn wdfsha to bool 716 fd = wdopen 0 0 717 if lt fd 0 718 ret false 719 end 720 b = new uchar 65536 0 721 c = new uchar 104 0 722 d = new uchar 32 0 723 sha256_init c 724 fin = false 725 while not fin bound 999999 726 r = read fd b 0 65536 727 if lt r 0 728 exit 70 729 end 730 if eq r 0 731 fin = true 732 else 733 sha256_update c part b 0 ulong r 734 end 735 end 736 c2 = fclose fd 737 sha256_final c d 738 for i ulong 32 739 put wdd i get d i 740 end 741 ret true 742 end 743 744 note SHA-256 of wdsrc[0, wdsn) into wdd (a global cannot be passed, so it goes through a local copy) 745 fn wdmsha to void 746 b = new uchar 65536 0 747 c = new uchar 104 0 748 d = new uchar 32 0 749 sha256_init c 750 n = get wdsn 0 751 o = ulong 0 752 while lt o n bound 999999 753 k = sub n o 754 if gt k 65536 755 k = ulong 65536 756 end 757 for j ulong k 758 put b j get wdsrc add o j 759 end 760 sha256_update c part b 0 k 761 o = add o k 762 end 763 sha256_final c d 764 for i ulong 32 765 put wdd i get d i 766 end 767 end 768 769 note copy everything readable from descriptor fi to descriptor fo 770 fn wdcopyfd fi int fo int to void 771 b = new uchar 65536 0 772 fin = false 773 while not fin bound 999999 774 r = read fi b 0 65536 775 if lt r 0 776 exit 70 777 end 778 if eq r 0 779 fin = true 780 else 781 wdwrall fo b ulong r 782 end 783 end 784 end 785 786 fn wdwrall fd int b view uchar * n ulong to void 787 t = ulong 0 788 while lt t n bound 999999 789 w = write fd b t sub n t 790 if le w 0 791 exit 70 792 end 793 t = add t ulong w 794 end 795 end 796 797 note rename the first record of the packed list to the second (renameat2 flags 0) 798 fn wdrename to void 799 pn = wdkfin 800 r = renames wdk 0 pn 0 801 if ne r 0 802 exit 70 803 end 804 end 805 806 note string slots: slot k is wdz[wdzo[k] .. wdzo[k] + wdzl[k]). wdzpool fills 0 HOME, 1 POOL, 2 LOCK; 807 note a tool numbers its own slots from 3. 808 global wdz uchar 65536 809 global wdzo ulong 16 810 global wdzl ulong 16 811 global wdzn ulong 1 812 text wdthome "HOME" 813 text wdtpool "POOL" 814 text wdtlock "LOCK" 815 text wddpool "19" 816 text wddlock "/handoffs/hive/queen/pool-" 817 text wddlk2 ".lock" 818 text wdtlkto "LOCK_TIMEOUT " 819 text wdtlkno "cannot open lock " 820 text wdtnl "\n" 821 text wdxts "/usr/bin/taskset" 822 text wdxc "-c" 823 text wdxnice "/usr/bin/nice" 824 text wdxn "-n" 825 text wdx10 "10" 826 text wdtnoh "HOME is not set\n" 827 828 note save the path builder as slot k 829 fn wdzsave k ulong to void 830 p = get wdzn 0 831 n = get wdpn 0 832 if gt add p n 65536 833 exit 2 834 end 835 for j ulong n 836 put wdz add p j get wdp j 837 end 838 put wdzo k p 839 put wdzl k n 840 put wdzn 0 add p n 841 end 842 843 note append slot k to the path builder 844 fn wdzp k ulong to void 845 o = get wdzo k 846 for j ulong get wdzl k 847 wdpc ulong get wdz add o j 848 end 849 end 850 851 note slot k from environment entry nm, else HOME followed by d 852 fn wdzenv nm view uchar * k ulong d view uchar * to void 853 wdpclr 854 if wdenv nm 855 wdpv 856 else 857 wdzp 0 858 wdpt d 859 end 860 wdzsave k 861 end 862 863 note slots 0 HOME (exit 2 when unset), 1 POOL (default 19), 2 LOCK (default 864 note HOME/handoffs/hive/queen/pool-POOL.lock) 865 fn wdzpool to void 866 if not wdenv wdthome 867 wdoclr 868 wdot wdtnoh 869 wdoflush 2 870 exit 2 871 end 872 wdpclr 873 wdpv 874 wdzsave 0 875 wdpclr 876 if wdenv wdtpool 877 wdpv 878 else 879 wdpt wddpool 880 end 881 wdzsave 1 882 wdpclr 883 if wdenv wdtlock 884 wdpv 885 else 886 wdzp 0 887 wdpt wddlock 888 wdzp 1 889 wdpt wddlk2 890 end 891 wdzsave 2 892 end 893 894 note start the packed list with the pool prefix (7 records): 895 note /usr/bin/taskset -c POOL /usr/bin/nice -n 10 896 fn wdkpre to void 897 wdkclr 898 wdkt wdxts 899 wdkt wdxc 900 wdpclr 901 wdzp 1 902 wdkp 903 wdkt wdxnice 904 wdkt wdxn 905 wdkt wdx10 906 end 907 908 909 fn wdlkmsg t view uchar * to void 910 wdoclr 911 wdot t 912 wdpclr 913 wdzp 2 914 wdop 915 wdot wdtnl 916 wdoflush 2 917 end 918 919 note take the pool lock LOCK (slot 2) as `flock -w 7200` did: open it read-only, creating it with 920 note mode 0666, and try an exclusive flock every 10 ms for at most 7200 s. False on timeout. 921 fn wdlock to bool 922 wdpclr 923 wdzp 2 924 fd = wdopen 64 438 925 if lt fd 0 926 wdlkmsg wdtlkno 927 exit 70 928 end 929 d = add monons `7200000000000` 930 while true bound 999999 931 r = flock fd 6 932 if eq r 0 933 put wdlkf 0 add long fd 1 934 ret true 935 end 936 if ne r -11 937 wdlkmsg wdtlkno 938 exit 70 939 end 940 if gt monons d 941 c = fclose fd 942 wdlkmsg wdtlkto 943 ret false 944 end 945 s = sleepns `10000000` 946 end 947 ret false 948 end 949 950 note run the packed list (built after wdkpre) under the pool lock: lock, spawn, wait, unlock. 951 note Returns the child's status, or 1 when the lock was not had within 7200 s (flock -w's status). 952 fn wdpoolrun fi int fo int fe int to long 953 if not wdlock 954 ret 1 955 end 956 pid = wdspawn fi fo fe 957 rc = wdwait pid 958 c = fclose int sub get wdlkf 0 1 959 put wdlkf 0 0 960 ret rc 961 end 962 963 note wbuild ------------------------------------------------------------------------------------ 964 global wbomc uchar 4096 965 global wbomn ulong 1 966 global wbcore uchar 64 967 global wbcn ulong 1 968 global wbren uchar 64 969 global wbrnn ulong 1 970 global wbom2 ulong 1 971 text wbpin "`2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f`" 972 text wbtomc "OMC" 973 text wbtcore "CORE" 974 text wbtall "-all.om" 975 text wbttmp ".tmp" 976 text wbtts "/usr/bin/taskset" 977 text wbtc "-c" 978 text wbtnice "/usr/bin/nice" 979 text wbtn "-n" 980 text wbt19 "19" 981 text wbtom2 "om2" 982 text wbtlay "layout " 983 text wbtend "end" 984 text wbtmain "fn main to int" 985 text wbtfn "fn " 986 text wbtto " to int" 987 text wbtnl "\n" 988 text wbtok "BUILD_OK " 989 text wbtsrc " source_sha256 " 990 text wbtomcs " omc " 991 text wbtfail "BUILD_FAIL omc rc=" 992 text wbtpinm "OMC_PIN_MISMATCH " 993 text wbtmc "MAIN_COUNT " 994 text wbtwant ", want 1 in " 995 text wbtrd "cannot read " 996 text wbtpbad "PART_PIN_BAD " 997 text wbtpmis "PART_PIN_MISMATCH " 998 text wbtexp "export fn " 999 text wbtuse "usage: OMC=omc [CORE=n] wbuild OUTBIN PART... (PART is FILE[@NAME][=SHA256HEX])\n" 1000 1001 fn wbusage to void 1002 wdoclr 1003 wdot wbtuse 1004 wdoflush 2 1005 exit 2 1006 end 1007 1008 fn wbpomc to void 1009 wdpclr 1010 for j ulong get wbomn 0 1011 wdpc ulong get wbomc j 1012 end 1013 end 1014 1015 fn wbpcore to void 1016 wdpclr 1017 for j ulong get wbcn 0 1018 wdpc ulong get wbcore j 1019 end 1020 end 1021 1022 note index of the end of the line starting at s (after its newline, or n) 1023 fn wbeol s ulong n ulong to ulong 1024 i = s 1025 while lt i n bound 999999 1026 if eq get wdsrc i 10 1027 ret add i 1 1028 end 1029 i = add i 1 1030 end 1031 ret n 1032 end 1033 1034 note content length of the line [s, e): without its newline 1035 fn wbcl s ulong e ulong to ulong 1036 if gt e s 1037 if eq get wdsrc sub e 1 10 1038 ret sub sub e s 1 1039 end 1040 end 1041 ret sub e s 1042 end 1043 1044 note wdsrc[s .. s + cl) equals t (pre false) or starts with t (pre true) 1045 fn wbeq s ulong cl ulong t view uchar * pre bool to bool 1046 k = len t 1047 if lt cl k 1048 ret false 1049 end 1050 if and not pre ne cl k 1051 ret false 1052 end 1053 for i ulong k 1054 if ne get wdsrc add s i get t i 1055 ret false 1056 end 1057 end 1058 ret true 1059 end 1060 1061 note 1 om2, 2 layout start, 3 end, 4 main, else 0 1062 fn wbkind s ulong e ulong to ulong 1063 cl = wbcl s e 1064 if wbeq s cl wbtom2 false 1065 ret 1 1066 end 1067 if wbeq s cl wbtlay true 1068 ret 2 1069 end 1070 if wbeq s cl wbtend false 1071 ret 3 1072 end 1073 if wbeq s cl wbtmain false 1074 ret 4 1075 end 1076 ret 0 1077 end 1078 1079 fn wbemit s ulong e ulong to void 1080 for j ulong sub e s 1081 wdoc ulong get wdsrc add s j 1082 end 1083 end 1084 1085 fn wbemitm s ulong e ulong to void 1086 wdot wbtfn 1087 for j ulong get wbrnn 0 1088 wdoc ulong get wbren j 1089 end 1090 wdot wbtto 1091 if ne wbcl s e sub e s 1092 wdoc 10 1093 end 1094 end 1095 1096 note one pass over the part in wdsrc: 0 counts om2 and main lines, 1 emits layout blocks, 2 emits 1097 note the rest (main renamed when the part has a name; a line starting `export fn ` loses its 1098 note `export `, since the output is a program and a program has no exports); returns the number of 1099 note main lines 1100 fn wblines pass ulong to ulong 1101 n = get wdsn 0 1102 s = ulong 0 1103 mains = ulong 0 1104 inl = false 1105 while lt s n bound 999999 1106 e = wbeol s n 1107 k = wbkind s e 1108 if eq k 2 1109 inl = true 1110 end 1111 if eq pass 0 1112 if eq k 1 1113 put wbom2 0 1 1114 end 1115 if eq k 4 1116 mains = add mains 1 1117 end 1118 end 1119 if eq pass 1 1120 if inl 1121 wbemit s e 1122 end 1123 end 1124 if eq pass 2 1125 if not or inl eq k 1 1126 if and eq k 4 gt get wbrnn 0 0 1127 wbemitm s e 1128 else 1129 if wbeq s wbcl s e wbtexp true 1130 wbemit add s 7 e 1131 else 1132 wbemit s e 1133 end 1134 end 1135 end 1136 end 1137 if and inl eq k 3 1138 inl = false 1139 end 1140 s = e 1141 end 1142 ret mains 1143 end 1144 1145 note wbpinx/wbpinn: the current part's pin (64 lowercase hex digits), wbpinn 0 when it has none 1146 global wbpinx uchar 64 1147 global wbpinn ulong 1 1148 1149 note wdab[0, n) holds a PART. When it has an `=`, the text after the last `=` must be 64 lowercase 1150 note hex digits (else stderr PART_PIN_BAD PART, exit 2); it becomes the pin and the PART ends at the 1151 note `=`. Returns the PART's length without the pin. 1152 fn wbpinparse n ulong to ulong 1153 q = n 1154 for j ulong n 1155 if eq get wdab j 61 1156 q = j 1157 end 1158 end 1159 put wbpinn 0 0 1160 if eq q n 1161 ret n 1162 end 1163 ok = eq sub sub n q 1 64 1164 if ok 1165 for k ulong 64 1166 v = get wdab add add q 1 k 1167 if not or and ge v 48 le v 57 and ge v 97 le v 102 1168 ok = false 1169 end 1170 put wbpinx k v 1171 end 1172 end 1173 if not ok 1174 wdoclr 1175 wdot wbtpbad 1176 for j2 ulong n 1177 wdoc ulong get wdab j2 1178 end 1179 wdot wbtnl 1180 wdoflush 2 1181 exit 2 1182 end 1183 put wbpinn 0 64 1184 ret q 1185 end 1186 1187 note true when the hex of wdd equals the pin wbpinx 1188 fn wbpineq to bool 1189 for i ulong 32 1190 v = ulong get wdd i 1191 if ne get wbpinx mul i 2 get wdhex shr v 4 1192 ret false 1193 end 1194 if ne get wbpinx add mul i 2 1 get wdhex and v 15 1195 ret false 1196 end 1197 end 1198 ret true 1199 end 1200 1201 note argument i is FILE, FILE@NAME, FILE=HEX64 or FILE@NAME=HEX64: load FILE, check its pin, set the 1202 note rename, run pass 1203 fn wbpart i ulong pass ulong to void 1204 n = wbpinparse wdarg i 1205 ap = n 1206 for j ulong n 1207 if eq get wdab j 64 1208 ap = j 1209 end 1210 end 1211 put wbrnn 0 0 1212 if lt ap n 1213 m = sub sub n ap 1 1214 if or eq m 0 gt m 63 1215 exit 2 1216 end 1217 for j2 ulong m 1218 put wbren j2 get wdab add add ap 1 j2 1219 end 1220 put wbrnn 0 m 1221 end 1222 wdpclr 1223 for j3 ulong ap 1224 wdpc ulong get wdab j3 1225 end 1226 r = wdload 1227 if lt r 0 1228 wdoclr 1229 wdot wbtrd 1230 wdop 1231 wdot wbtnl 1232 wdoflush 2 1233 exit 70 1234 end 1235 if gt get wbpinn 0 0 1236 wdmsha 1237 if not wbpineq 1238 wdoclr 1239 wdot wbtpmis 1240 wdop 1241 wdoc 32 1242 wdohex 1243 wdot wbtnl 1244 wdoflush 2 1245 exit 3 1246 end 1247 end 1248 c = wblines pass 1249 if and eq pass 0 gt get wbrnn 0 0 1250 if ne c 1 1251 wdoclr 1252 wdot wbtmc 1253 wdod c 1254 wdot wbtwant 1255 wdop 1256 wdot wbtnl 1257 wdoflush 2 1258 exit 4 1259 end 1260 end 1261 end 1262 1263 note OUTBIN followed by the suffix t 1264 fn wbpout t view uchar * to void 1265 wdpclr 1266 wdpa 1 1267 wdpt t 1268 end 1269 1270 fn wbcompose na ulong to void 1271 for i ulong sub na 2 1272 wbpart add i 2 0 1273 end 1274 wbpout wbtall 1275 fd = wdopen 577 436 1276 if lt fd 0 1277 exit 70 1278 end 1279 put wdofd 0 fd 1280 wdoclr 1281 if eq get wbom2 0 1 1282 wdot wbtom2 1283 wdot wbtnl 1284 end 1285 for p ulong 2 1286 for i2 ulong sub na 2 1287 wbpart add i2 2 add p 1 1288 end 1289 end 1290 wdoflush fd 1291 put wdofd 0 1 1292 c = fclose fd 1293 if ne c 0 1294 exit 70 1295 end 1296 end 1297 1298 note taskset -c CORE nice -n 19 OMC < OUTBIN-all.om > OUTBIN.tmp; returns omc's exit status 1299 fn wbcomp to long 1300 wbpout wbtall 1301 fi = wdopen 0 0 1302 wbpout wbttmp 1303 fo = wdopen 577 436 1304 if or lt fi 0 lt fo 0 1305 exit 70 1306 end 1307 wdkclr 1308 wdkt wbtts 1309 wdkt wbtc 1310 wbpcore 1311 wdkp 1312 wdkt wbtnice 1313 wdkt wbtn 1314 wdkt wbt19 1315 wbpomc 1316 wdkp 1317 pid = wdspawn fi fo -1 1318 c1 = fclose fi 1319 c2 = fclose fo 1320 ret wdwait pid 1321 end 1322 1323 fn wbenv to void 1324 if not wdenv wbtomc 1325 wbusage 1326 end 1327 for j ulong get wdvn 0 1328 put wbomc j get wdv j 1329 end 1330 put wbomn 0 get wdvn 0 1331 put wbcore 0 48 1332 put wbcn 0 1 1333 if wdenv wbtcore 1334 if or eq get wdvn 0 0 gt get wdvn 0 63 1335 exit 2 1336 end 1337 for j2 ulong get wdvn 0 1338 put wbcore j2 get wdv j2 1339 end 1340 put wbcn 0 get wdvn 0 1341 end 1342 end 1343 1344 fn wbchkpin to void 1345 wbpomc 1346 if not wdfsha 1347 exit 70 1348 end 1349 if not wdhexeq wbpin 1350 wdoclr 1351 wdot wbtpinm 1352 wdohex 1353 wdot wbtnl 1354 wdoflush 2 1355 exit 3 1356 end 1357 end 1358 1359 fn wbfinish to void 1360 wbpout wbttmp 1361 r = chmod wdp 0 get wdpn 0 509 1362 if ne r 0 1363 exit 70 1364 end 1365 wdkclr 1366 wdkp 1367 wdpclr 1368 wdpa 1 1369 wdkp 1370 wdrename 1371 end 1372 1373 fn main to int 1374 na = ulong argcnt 1375 if lt na 3 1376 wbusage 1377 end 1378 wbenv 1379 wbchkpin 1380 wbcompose na 1381 rc = wbcomp 1382 if ne rc 0 1383 wbpout wbttmp 1384 u = unlink wdp 0 get wdpn 0 1385 wdoclr 1386 wdot wbtfail 1387 wdod ulong rc 1388 wdot wbtnl 1389 wdoflush 2 1390 exit 1 1391 end 1392 wbfinish 1393 wdoclr 1394 wdot wbtok 1395 wdpclr 1396 wdpa 1 1397 if not wdfsha 1398 exit 70 1399 end 1400 wdohex 1401 wdoc 32 1402 wdop 1403 wdot wbtsrc 1404 wbpout wbtall 1405 if not wdfsha 1406 exit 70 1407 end 1408 wdohex 1409 wdot wbtomcs 1410 wbpomc 1411 if not wdfsha 1412 exit 70 1413 end 1414 wdohex 1415 wdot wbtnl 1416 wdoflush 1 1417 ret 0 1418 end