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 / ngbuild.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/ngbuild.om: builds an Omega program from an ordered list of sha256-pinned units (D1b): ng |
| 3 | note from seed/ng/ng.units, ngbuild itself from seed/ng/ngbuild.units, each NG tool from its own |
| 4 | note seed/ng/TOOL.units (seed/ng/README.md). Owner: ng-ir lane. |
| 5 | note First birth, no cat: seed/ng/tests/ngbuild/gate.om joins seed/ng/ngbuild.units itself, compiles it, |
| 6 | note and requires the result to rebuild itself byte for byte with every pin checked (README.md). |
| 7 | note Run from the repository root. |
| 8 | note ngbuild build OMC UNITS OUT check UNITS and every unit's sha256 pin, concatenate the units in |
| 9 | note order into OUT.src (exactly the bytes `cat` of the listed paths |
| 10 | note writes: nothing is added between units), compile it with OMC |
| 11 | note (stdin OUT.src, stdout OUT, mode 0755), print on stderr |
| 12 | note "ngbuild: OK units=N bytes=M sha256=HEX" (sha256 of OUT) |
| 13 | note ngbuild sums UNITS print each unit's current "HEX PATH" line (to refresh UNITS) |
| 14 | note UNITS: one unit per line, "HEX PATH": 64 lowercase hex digits, one space, a path (relative to the |
| 15 | note working directory, or absolute; 1..4095 bytes, no NUL), a newline. No other lines. The line order is the build |
| 16 | note order; there is no dependency metadata, so a cycle cannot be written. At most 64 units, UNITS at |
| 17 | note most 65536 bytes, the units at most 8388607 bytes in total (the omc input capacity, OMEGESE s9.6). |
| 18 | note The whole of UNITS is checked before any unit is read, and every unit before OUT.src is opened |
| 19 | note or OMC runs; so exits 2..5, 8 and 9 from those checks write no file and run nothing. After OMC |
| 20 | note exits 0, OUT is read back for its sha256: exit 5 (OUT unreadable) or 9 (OUT of 16777216 bytes or |
| 21 | note more) then leaves OUT.src and OUT. |
| 22 | note Exit 0 built (or sums printed) |
| 23 | note 1 omc refused: "ngbuild: omc exit N" (omc's own stderr line passes through); an exec or |
| 24 | note redirect failure in the child is "ngbuild: omc exit 127" |
| 25 | note 2 usage, also any argument count other than build's 4 or sums' 2 (checked before any is read) |
| 26 | note 3 a pin is not the unit's sha256: "ngbuild: sha256 mismatch PATH have HEX" |
| 27 | note 4 malformed UNITS: "ngbuild: malformed units line OFFSET" (byte offset of the line), or |
| 28 | note "ngbuild: no units" (empty UNITS) |
| 29 | note 5 UNITS or a unit unreadable (missing, a directory): "ngbuild: unreadable: PATH" |
| 30 | note 6 OUT.src or OUT not writable: "ngbuild: cannot write output" |
| 31 | note 7 fork or wait failed, or OMC ended by a signal: "ngbuild: fork or wait failed, or omc ended |
| 32 | note by a signal"; OUT.src, and OUT once opened, can remain after exits 1, 6 and 7 |
| 33 | note 8 a unit listed twice: the same path on two lines, or (build) the same pin on two lines: |
| 34 | note "ngbuild: duplicate unit line N: PATH" (N counts from 1; the later of the two lines) |
| 35 | note 9 over a limit: "ngbuild: more than 64 units", or "ngbuild: too large: PATH" for UNITS over |
| 36 | note 65536 bytes, a unit of 16777216 bytes or more, or (build) the unit that takes the total |
| 37 | note past 8388607 bytes |
| 38 | text nb_txt "build\nsums\n.src\nngbuild: OK units=\n bytes=\n sha256=\nngbuild: sha256 mismatch \n have \nngbuild: omc exit \nngbuild: malformed units line \nngbuild: unreadable: \nngbuild: cannot write output\nngbuild: fork or wait failed, or omc ended by a signal\nngbuild: usage: ngbuild build OMC UNITS OUT | ngbuild sums UNITS\nngbuild: too large: \nngbuild: more than 64 units\nngbuild: no units\nngbuild: duplicate unit line \n: \n" |
| 39 | global nb_u uchar 65536 |
| 40 | global nb_un ulong 1 |
| 41 | global nb_src uchar 8388608 |
| 42 | global nb_sn ulong 1 |
| 43 | global nb_a uchar 4096 |
| 44 | global nb_al ulong 128 |
| 45 | global nb_p uchar 4096 |
| 46 | global nb_pn ulong 1 |
| 47 | global nb_hx uchar 66 |
| 48 | global nb_d uchar 24 |
| 49 | global nb_st uchar 4 |
| 50 | global nb_av uchar 8192 |
| 51 | global nb_ap ulong 2 |
| 52 | note unit k: its line starts at nb_ls[k] (the pin), its path (nb_pl[k] bytes) at nb_ls[k] + 65 |
| 53 | global nb_ls ulong 64 |
| 54 | global nb_pl ulong 64 |
| 55 | global nb_uc ulong 1 |
| 56 | note ---- output --------------------------------------------------------------------------------- |
| 57 | note line k (1-based) of nb_txt, without its newline |
| 58 | fn nb_say k ulong to void |
| 59 | s = ulong 0 |
| 60 | c = ulong 1 |
| 61 | for i ulong len nb_txt |
| 62 | if eq get nb_txt i 10 |
| 63 | if eq c k |
| 64 | r = write 2 nb_txt s sub i s |
| 65 | ret |
| 66 | end |
| 67 | c = add c 1 |
| 68 | s = add i 1 |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 | fn nb_nl to void |
| 73 | put nb_d 0 10 |
| 74 | r = write 2 nb_d 0 1 |
| 75 | end |
| 76 | fn nb_num v ulong to void |
| 77 | x = v |
| 78 | i = ulong 24 |
| 79 | for j ulong 21 |
| 80 | i = sub i 1 |
| 81 | put nb_d i uchar add 48 rem x 10 |
| 82 | x = div x 10 |
| 83 | if eq x 0 |
| 84 | r = write 2 nb_d i sub 24 i |
| 85 | ret |
| 86 | end |
| 87 | end |
| 88 | end |
| 89 | note message line k, the path in nb_p, a newline; exit x |
| 90 | fn nb_sayp k ulong x int to void |
| 91 | nb_say k |
| 92 | r = write 2 nb_p 0 get nb_pn 0 |
| 93 | nb_nl |
| 94 | exit x |
| 95 | end |
| 96 | note hex of sh_dig into nb_hx[0..64) |
| 97 | fn nb_hex to void |
| 98 | for j ulong 32 |
| 99 | b = uint get sh_dig j |
| 100 | put nb_hx mul j 2 sh_hexd shr b 4 |
| 101 | put nb_hx add mul j 2 1 sh_hexd and b 15 |
| 102 | end |
| 103 | end |
| 104 | note ---- arguments and files -------------------------------------------------------------------- |
| 105 | note argument i into nb_a at offset nb_al[2i], length nb_al[2i+1]; false when absent or too long |
| 106 | fn nb_arg i ulong to bool |
| 107 | o = ulong 0 |
| 108 | if gt i 0 |
| 109 | o = add get nb_al sub mul i 2 2 get nb_al sub mul i 2 1 |
| 110 | end |
| 111 | k = argread i nb_a o sub len nb_a o |
| 112 | if lt k 0 |
| 113 | ret false |
| 114 | end |
| 115 | put nb_al mul i 2 o |
| 116 | put nb_al add mul i 2 1 ulong k |
| 117 | ret true |
| 118 | end |
| 119 | fn nb_aeq i ulong w ulong to bool |
| 120 | o = get nb_al mul i 2 |
| 121 | n = get nb_al add mul i 2 1 |
| 122 | s = ulong 0 |
| 123 | c = ulong 1 |
| 124 | for j ulong len nb_txt |
| 125 | if eq get nb_txt j 10 |
| 126 | if eq c w |
| 127 | if ne n sub j s |
| 128 | ret false |
| 129 | end |
| 130 | for q ulong n bound 4096 |
| 131 | if ne get nb_a add o q get nb_txt add s q |
| 132 | ret false |
| 133 | end |
| 134 | end |
| 135 | ret true |
| 136 | end |
| 137 | c = add c 1 |
| 138 | s = add j 1 |
| 139 | end |
| 140 | end |
| 141 | ret false |
| 142 | end |
| 143 | note read the file at nb_p[0..nb_pn) into sh_dat: its length; the capacity + 1 when it holds the |
| 144 | note capacity or more (too large), the capacity + 2 when it cannot be opened or read (unreadable) |
| 145 | fn nb_load to ulong |
| 146 | fd = fopen nb_p 0 get nb_pn 0 0 0 |
| 147 | if lt fd 0 |
| 148 | ret add len sh_dat 2 |
| 149 | end |
| 150 | n = ulong 0 |
| 151 | while lt n len sh_dat bound 16777217 |
| 152 | k = read int fd sh_dat n sub len sh_dat n |
| 153 | if lt k 0 |
| 154 | r = fclose int fd |
| 155 | ret add len sh_dat 2 |
| 156 | end |
| 157 | if eq k 0 |
| 158 | r = fclose int fd |
| 159 | ret n |
| 160 | end |
| 161 | n = add n ulong k |
| 162 | end |
| 163 | r = fclose int fd |
| 164 | ret add len sh_dat 1 |
| 165 | end |
| 166 | note refuse a file nb_load could not take (n its result): unreadable 5, too large 9 |
| 167 | fn nb_chk n ulong to void |
| 168 | if eq n add len sh_dat 2 |
| 169 | nb_sayp 11 5 |
| 170 | end |
| 171 | if gt n len sh_dat |
| 172 | nb_sayp 15 9 |
| 173 | end |
| 174 | end |
| 175 | note path of argument i into nb_p |
| 176 | fn nb_ap2p i ulong to void |
| 177 | o = get nb_al mul i 2 |
| 178 | n = get nb_al add mul i 2 1 |
| 179 | for j ulong n bound 4096 |
| 180 | put nb_p j get nb_a add o j |
| 181 | end |
| 182 | put nb_pn 0 n |
| 183 | end |
| 184 | note read UNITS (argument i) into nb_u |
| 185 | fn nb_units i ulong to void |
| 186 | nb_ap2p i |
| 187 | n = nb_load |
| 188 | nb_chk n |
| 189 | if gt n len nb_u |
| 190 | nb_sayp 15 9 |
| 191 | end |
| 192 | for j ulong n bound 65536 |
| 193 | put nb_u j get sh_dat j |
| 194 | end |
| 195 | put nb_un 0 n |
| 196 | end |
| 197 | note ---- units ---------------------------------------------------------------------------------- |
| 198 | note parse the line at nb_u[s..) as unit k (format above) into nb_ls, nb_pl; the offset after its newline |
| 199 | fn nb_line s ulong k ulong to ulong |
| 200 | n = get nb_un 0 |
| 201 | if gt add s 66 n |
| 202 | nb_bad s |
| 203 | end |
| 204 | for j ulong 64 |
| 205 | c = get nb_u add s j |
| 206 | if not or and ge c 48 le c 57 and ge c 97 le c 102 |
| 207 | nb_bad s |
| 208 | end |
| 209 | end |
| 210 | if ne get nb_u add s 64 32 |
| 211 | nb_bad s |
| 212 | end |
| 213 | p = add s 65 |
| 214 | e = p |
| 215 | while lt e n bound 65536 |
| 216 | c = get nb_u e |
| 217 | if eq c 10 |
| 218 | break |
| 219 | end |
| 220 | if eq c 0 |
| 221 | nb_bad s |
| 222 | end |
| 223 | e = add e 1 |
| 224 | end |
| 225 | if or or ge e n eq e p gt sub e p 4095 |
| 226 | nb_bad s |
| 227 | end |
| 228 | put nb_ls k s |
| 229 | put nb_pl k sub e p |
| 230 | ret add e 1 |
| 231 | end |
| 232 | fn nb_bad s ulong to void |
| 233 | nb_say 10 |
| 234 | nb_num s |
| 235 | nb_nl |
| 236 | exit 4 |
| 237 | end |
| 238 | note path of unit k into nb_p |
| 239 | fn nb_setp k ulong to void |
| 240 | p = add get nb_ls k 65 |
| 241 | n = get nb_pl k |
| 242 | for j ulong n bound 4096 |
| 243 | put nb_p j get nb_u add p j |
| 244 | end |
| 245 | put nb_pn 0 n |
| 246 | end |
| 247 | fn nb_eqb a ulong b ulong n ulong to bool |
| 248 | for j ulong n bound 4096 |
| 249 | if ne get nb_u add a j get nb_u add b j |
| 250 | ret false |
| 251 | end |
| 252 | end |
| 253 | ret true |
| 254 | end |
| 255 | note true when units i and k have the same path, or (pins) the same pin |
| 256 | fn nb_dup i ulong k ulong pins bool to bool |
| 257 | a = get nb_ls i |
| 258 | b = get nb_ls k |
| 259 | n = get nb_pl i |
| 260 | if eq n get nb_pl k |
| 261 | if nb_eqb add a 65 add b 65 n |
| 262 | ret true |
| 263 | end |
| 264 | end |
| 265 | if pins |
| 266 | ret nb_eqb a b 64 |
| 267 | end |
| 268 | ret false |
| 269 | end |
| 270 | note check all of UNITS before any unit is read: every line well formed (4), at least one unit (4), |
| 271 | note at most 64 (9), no path twice and (pins) no pin twice (8); sets nb_uc |
| 272 | fn nb_scan pins bool to void |
| 273 | s = ulong 0 |
| 274 | u = ulong 0 |
| 275 | while lt s get nb_un 0 bound 65 |
| 276 | if eq u 64 |
| 277 | nb_say 16 |
| 278 | nb_nl |
| 279 | exit 9 |
| 280 | end |
| 281 | s = nb_line s u |
| 282 | u = add u 1 |
| 283 | end |
| 284 | if eq u 0 |
| 285 | nb_say 17 |
| 286 | nb_nl |
| 287 | exit 4 |
| 288 | end |
| 289 | put nb_uc 0 u |
| 290 | for k ulong u bound 64 |
| 291 | for i ulong k bound 64 |
| 292 | if nb_dup i k pins |
| 293 | nb_setp k |
| 294 | nb_say 18 |
| 295 | nb_num add k 1 |
| 296 | nb_say 19 |
| 297 | r = write 2 nb_p 0 get nb_pn 0 |
| 298 | nb_nl |
| 299 | exit 8 |
| 300 | end |
| 301 | end |
| 302 | end |
| 303 | end |
| 304 | note true when the 64 hex digits at nb_u[s..) equal nb_hx |
| 305 | fn nb_same s ulong to bool |
| 306 | for j ulong 64 |
| 307 | if ne get nb_u add s j get nb_hx j |
| 308 | ret false |
| 309 | end |
| 310 | end |
| 311 | ret true |
| 312 | end |
| 313 | fn nb_sums to int |
| 314 | nb_scan false |
| 315 | for k ulong get nb_uc 0 bound 64 |
| 316 | nb_setp k |
| 317 | n = nb_load |
| 318 | nb_chk n |
| 319 | sh_all n |
| 320 | nb_hex |
| 321 | put nb_hx 64 32 |
| 322 | r = write 1 nb_hx 0 65 |
| 323 | r = write 1 nb_p 0 get nb_pn 0 |
| 324 | put nb_d 0 10 |
| 325 | r = write 1 nb_d 0 1 |
| 326 | end |
| 327 | ret 0 |
| 328 | end |
| 329 | note every unit in order: its size against the total (9), its pin (3), then appended to nb_src |
| 330 | fn nb_cat to void |
| 331 | for k ulong get nb_uc 0 bound 64 |
| 332 | nb_setp k |
| 333 | n = nb_load |
| 334 | nb_chk n |
| 335 | o = get nb_sn 0 |
| 336 | if gt add o n sub len nb_src 1 |
| 337 | nb_sayp 15 9 |
| 338 | end |
| 339 | sh_all n |
| 340 | nb_hex |
| 341 | if not nb_same get nb_ls k |
| 342 | nb_say 7 |
| 343 | r = write 2 nb_p 0 get nb_pn 0 |
| 344 | nb_say 8 |
| 345 | r = write 2 nb_hx 0 64 |
| 346 | nb_nl |
| 347 | exit 3 |
| 348 | end |
| 349 | for j ulong n bound 8388608 |
| 350 | put nb_src add o j get sh_dat j |
| 351 | end |
| 352 | put nb_sn 0 add o n |
| 353 | end |
| 354 | end |
| 355 | note ---- child -------------------------------------------------------------------------------- |
| 356 | fn nb_avadd i ulong to void |
| 357 | o = get nb_al mul i 2 |
| 358 | n = get nb_al add mul i 2 1 |
| 359 | p = get nb_ap 0 |
| 360 | put nb_av p uchar and n 255 |
| 361 | put nb_av add p 1 uchar shr n 8 |
| 362 | for j ulong n bound 4096 |
| 363 | put nb_av add add p 2 j get nb_a add o j |
| 364 | end |
| 365 | put nb_ap 0 add add p 2 n |
| 366 | end |
| 367 | note OUT path (argument 4) plus ".src" into nb_p when src, else OUT alone |
| 368 | fn nb_out src bool to void |
| 369 | nb_ap2p 4 |
| 370 | if src |
| 371 | n = get nb_pn 0 |
| 372 | for j ulong 4 |
| 373 | put nb_p add n j get nb_txt add 11 j |
| 374 | end |
| 375 | put nb_pn 0 add n 4 |
| 376 | end |
| 377 | end |
| 378 | fn nb_wsrc to void |
| 379 | nb_out true |
| 380 | fd = fopen nb_p 0 get nb_pn 0 577 420 |
| 381 | if lt fd 0 |
| 382 | nb_say 12 |
| 383 | nb_nl |
| 384 | exit 6 |
| 385 | end |
| 386 | n = get nb_sn 0 |
| 387 | o = ulong 0 |
| 388 | while lt o n bound 16777216 |
| 389 | k = write int fd nb_src o sub n o |
| 390 | if le k 0 |
| 391 | nb_say 12 |
| 392 | nb_nl |
| 393 | exit 6 |
| 394 | end |
| 395 | o = add o ulong k |
| 396 | end |
| 397 | r = fclose int fd |
| 398 | end |
| 399 | fn nb_omc to ulong |
| 400 | nb_out true |
| 401 | fi = fopen nb_p 0 get nb_pn 0 0 0 |
| 402 | nb_out false |
| 403 | fo = fopen nb_p 0 get nb_pn 0 577 493 |
| 404 | if or lt fi 0 lt fo 0 |
| 405 | nb_say 12 |
| 406 | nb_nl |
| 407 | exit 6 |
| 408 | end |
| 409 | put nb_ap 0 2 |
| 410 | nb_avadd 2 |
| 411 | put nb_av 0 1 |
| 412 | put nb_av 1 0 |
| 413 | pid = fork |
| 414 | if lt pid 0 |
| 415 | nb_fail |
| 416 | end |
| 417 | if eq pid 0 |
| 418 | if ne dupfd int fi 0 0 0 |
| 419 | exit 127 |
| 420 | end |
| 421 | if ne dupfd int fo 1 0 1 |
| 422 | exit 127 |
| 423 | end |
| 424 | r = execprog nb_av 0 get nb_ap 0 |
| 425 | exit 127 |
| 426 | end |
| 427 | r = fclose int fi |
| 428 | r = fclose int fo |
| 429 | w = waitpid int pid nb_st 0 4 0 |
| 430 | if ne w pid |
| 431 | nb_fail |
| 432 | end |
| 433 | if ne get nb_st 0 0 |
| 434 | nb_fail |
| 435 | end |
| 436 | ret ulong get nb_st 1 |
| 437 | end |
| 438 | fn nb_fail to void |
| 439 | nb_say 13 |
| 440 | nb_nl |
| 441 | exit 7 |
| 442 | end |
| 443 | fn nb_build to int |
| 444 | nb_units 3 |
| 445 | nb_scan true |
| 446 | nb_cat |
| 447 | nb_wsrc |
| 448 | x = nb_omc |
| 449 | if ne x 0 |
| 450 | nb_say 9 |
| 451 | nb_num x |
| 452 | nb_nl |
| 453 | ret 1 |
| 454 | end |
| 455 | nb_out false |
| 456 | n = nb_load |
| 457 | nb_chk n |
| 458 | sh_all n |
| 459 | nb_hex |
| 460 | put nb_hx 64 10 |
| 461 | k = nb_okline get nb_uc 0 |
| 462 | ret 0 |
| 463 | end |
| 464 | fn nb_okline u ulong to long |
| 465 | nb_say 4 |
| 466 | nb_num u |
| 467 | nb_say 5 |
| 468 | nb_num get nb_sn 0 |
| 469 | nb_say 6 |
| 470 | ret write 2 nb_hx 0 65 |
| 471 | end |
| 472 | fn main to int |
| 473 | c = argcnt |
| 474 | if or lt c 3 gt c 5 |
| 475 | nb_say 14 |
| 476 | nb_nl |
| 477 | ret 2 |
| 478 | end |
| 479 | for i ulong ulong c bound 64 |
| 480 | if not nb_arg i |
| 481 | nb_say 14 |
| 482 | nb_nl |
| 483 | ret 2 |
| 484 | end |
| 485 | end |
| 486 | if and eq c 3 nb_aeq 1 2 |
| 487 | nb_units 2 |
| 488 | ret nb_sums |
| 489 | end |
| 490 | if and eq c 5 nb_aeq 1 1 |
| 491 | ret nb_build |
| 492 | end |
| 493 | nb_say 14 |
| 494 | nb_nl |
| 495 | ret 2 |
| 496 | end |