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 / ngas.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/ngas.om: canonical ΩIR text on stdin to the binary on stdout (seed/ng/IR.md s10). |
| 3 | note Owner: ng-ir lane. Build: cat seed/ng/ngtxt.om seed/ng/ngas.om | seed/out/omc > ngas |
| 4 | note ngas reads only the text ngdis prints and checks syntax and numbering only (type, global and |
| 5 | note function numbers in order, defined values numbered in order, the defined values an op row and |
| 6 | note its ty call for, operand counts, const forms); every semantic rule is ngcheck's. Refusal: |
| 7 | note "ngas: syntax error at line N" on stderr and exit 3; input or output over 16 MiB exit 23; more |
| 8 | note than 65536 types, globals, functions, blocks in a function, or parameters of a function or block: |
| 9 | note "ngas: limit at line N" (the line of the first one over) and exit 23 (IR.md s12); |
| 10 | note unreadable input exit 27. Wire ids are written as the text gives them, in range or not. |
| 11 | text w_msg "ngas: syntax error at line \n" |
| 12 | text w_lim "ngas: limit at line " |
| 13 | text w_tag "omega.ng.ir.v1" |
| 14 | global w_in uchar 16777216 |
| 15 | global w_ob uchar 16777216 |
| 16 | global w_s ulong 8 |
| 17 | global w_k ulong 8 |
| 18 | global w_op uint 128 |
| 19 | note w_s: 0 cursor, 1 input end, 2 line number, 3 output length, 4 next value id |
| 20 | note refuse with LIMIT when k items are already in the table (IR.md s12: at most 65536) |
| 21 | fn as_lim k ulong to void |
| 22 | if lt k 65536 |
| 23 | ret |
| 24 | end |
| 25 | r = write 2 w_lim 0 len w_lim |
| 26 | put x_on 0 0 |
| 27 | tx_num get w_s 2 |
| 28 | tx_c 10 |
| 29 | r = write 2 x_out 0 get x_on 0 |
| 30 | exit 23 |
| 31 | end |
| 32 | fn as_err to void |
| 33 | r = write 2 w_msg 0 27 |
| 34 | put x_on 0 0 |
| 35 | tx_num get w_s 2 |
| 36 | tx_c 10 |
| 37 | r = write 2 x_out 0 get x_on 0 |
| 38 | exit 3 |
| 39 | end |
| 40 | note ---- input ------------------------------------------------------------------------------------ |
| 41 | note the byte at the cursor plus d, or 256 past the end |
| 42 | fn as_pk d ulong to ulong |
| 43 | p = add get w_s 0 d |
| 44 | if ge p get w_s 1 |
| 45 | ret 256 |
| 46 | end |
| 47 | ret ulong get w_in p |
| 48 | end |
| 49 | fn as_eat c ulong to void |
| 50 | if ne as_pk 0 c |
| 51 | as_err |
| 52 | end |
| 53 | put w_s 0 add get w_s 0 1 |
| 54 | if eq c 10 |
| 55 | put w_s 2 add get w_s 2 1 |
| 56 | end |
| 57 | end |
| 58 | fn as_sp to void |
| 59 | as_eat 32 |
| 60 | end |
| 61 | fn as_eol to void |
| 62 | as_eat 10 |
| 63 | end |
| 64 | fn as_isd c ulong to bool |
| 65 | ret and ge c 48 le c 57 |
| 66 | end |
| 67 | note a decimal number without sign, leading zeros or overflow |
| 68 | fn as_dec to ulong |
| 69 | c = as_pk 0 |
| 70 | if not as_isd c |
| 71 | as_err |
| 72 | end |
| 73 | if and eq c 48 as_isd as_pk 1 |
| 74 | as_err |
| 75 | end |
| 76 | v = ulong 0 |
| 77 | while as_isd as_pk 0 bound 21 |
| 78 | d = sub as_pk 0 48 |
| 79 | if gt v div sub 18446744073709551615 d 10 |
| 80 | as_err |
| 81 | end |
| 82 | v = add mul v 10 d |
| 83 | put w_s 0 add get w_s 0 1 |
| 84 | end |
| 85 | ret v |
| 86 | end |
| 87 | fn as_dlim m ulong to ulong |
| 88 | v = as_dec |
| 89 | if gt v m |
| 90 | as_err |
| 91 | end |
| 92 | ret v |
| 93 | end |
| 94 | fn as_d32 to ulong |
| 95 | ret as_dlim 4294967295 |
| 96 | end |
| 97 | note the end of the token at the cursor (next space, newline, @ or input end) |
| 98 | fn as_tend to ulong |
| 99 | p = get w_s 0 |
| 100 | while lt p get w_s 1 bound 16777216 |
| 101 | c = get w_in p |
| 102 | if or or eq c 32 eq c 10 eq c 64 |
| 103 | ret p |
| 104 | end |
| 105 | p = add p 1 |
| 106 | end |
| 107 | ret p |
| 108 | end |
| 109 | note word i of x_kw as the whole token at the cursor; consumed when matched and take |
| 110 | fn as_word i ulong take bool to bool |
| 111 | p = get w_s 0 |
| 112 | e = as_tend |
| 113 | n = ulong get x_kwl i |
| 114 | if ne sub e p n |
| 115 | ret false |
| 116 | end |
| 117 | s = ulong get x_kwo i |
| 118 | for j ulong n bound 16 |
| 119 | if ne get w_in add p j get x_kw add s j |
| 120 | ret false |
| 121 | end |
| 122 | end |
| 123 | if take |
| 124 | put w_s 0 e |
| 125 | end |
| 126 | ret true |
| 127 | end |
| 128 | fn as_kw i ulong to void |
| 129 | if not as_word i true |
| 130 | as_err |
| 131 | end |
| 132 | end |
| 133 | note one of words lo..hi: its index minus lo |
| 134 | fn as_wsel lo ulong hi ulong to ulong |
| 135 | for i ulong add sub hi lo 1 bound 32 |
| 136 | if as_word add lo i true |
| 137 | ret i |
| 138 | end |
| 139 | end |
| 140 | as_err |
| 141 | ret 0 |
| 142 | end |
| 143 | note a type: a built-in name, or %tN for any N that has no name |
| 144 | fn as_ty to ulong |
| 145 | if eq as_pk 0 37 |
| 146 | as_sp2 37 116 |
| 147 | n = as_d32 |
| 148 | if le n 20 |
| 149 | ck_named n |
| 150 | end |
| 151 | ret n |
| 152 | end |
| 153 | p = get w_s 0 |
| 154 | e = as_tend |
| 155 | for t ulong 21 |
| 156 | if as_tyis t p e |
| 157 | put w_s 0 e |
| 158 | ret t |
| 159 | end |
| 160 | end |
| 161 | as_err |
| 162 | ret 0 |
| 163 | end |
| 164 | fn ck_named n ulong to void |
| 165 | if ne get x_tnl n 1 |
| 166 | as_err |
| 167 | end |
| 168 | end |
| 169 | fn as_tyis t ulong p ulong e ulong to bool |
| 170 | n = ulong get x_tnl t |
| 171 | if ne sub e p n |
| 172 | ret false |
| 173 | end |
| 174 | s = ulong get x_tno t |
| 175 | if eq get x_tyn s 45 |
| 176 | ret false |
| 177 | end |
| 178 | for j ulong n bound 8 |
| 179 | if ne get w_in add p j get x_tyn add s j |
| 180 | ret false |
| 181 | end |
| 182 | end |
| 183 | ret true |
| 184 | end |
| 185 | fn as_sp2 a ulong b ulong to void |
| 186 | as_eat a |
| 187 | as_eat b |
| 188 | end |
| 189 | fn as_hexd to ulong |
| 190 | c = as_pk 0 |
| 191 | put w_s 0 add get w_s 0 1 |
| 192 | if as_isd c |
| 193 | ret sub c 48 |
| 194 | end |
| 195 | if and ge c 97 le c 102 |
| 196 | ret sub c 87 |
| 197 | end |
| 198 | as_err |
| 199 | ret 0 |
| 200 | end |
| 201 | fn as_hexn n ulong to ulong |
| 202 | v = ulong 0 |
| 203 | for j ulong n bound 16 |
| 204 | v = or shl v 4 as_hexd |
| 205 | end |
| 206 | ret v |
| 207 | end |
| 208 | note ---- output ------------------------------------------------------------------------------------- |
| 209 | fn as_b k ulong v ulong to void |
| 210 | n = get w_s 3 |
| 211 | if gt add n k len w_ob |
| 212 | exit 23 |
| 213 | end |
| 214 | for j ulong k bound 8 |
| 215 | put w_ob add n j uchar and shr v mul j 8 255 |
| 216 | end |
| 217 | put w_s 3 add n k |
| 218 | end |
| 219 | fn as_patch at ulong v ulong to void |
| 220 | for j ulong 4 |
| 221 | put w_ob add at j uchar and shr v mul j 8 255 |
| 222 | end |
| 223 | end |
| 224 | fn as_here to ulong |
| 225 | ret get w_s 3 |
| 226 | end |
| 227 | note ---- module ------------------------------------------------------------------------------------ |
| 228 | fn as_head to void |
| 229 | as_kw 29 |
| 230 | as_sp |
| 231 | as_eat 49 |
| 232 | as_eol |
| 233 | as_b 1 79 |
| 234 | as_b 1 83 |
| 235 | as_b 1 67 |
| 236 | as_b 1 49 |
| 237 | as_b 1 73 |
| 238 | as_b 1 82 |
| 239 | as_b 1 0 |
| 240 | as_b 1 9 |
| 241 | as_tagw |
| 242 | as_b 2 0 |
| 243 | end |
| 244 | fn as_tagw to void |
| 245 | for j ulong len w_tag |
| 246 | as_b 1 ulong get w_tag j |
| 247 | end |
| 248 | end |
| 249 | fn as_types to void |
| 250 | at = as_here |
| 251 | as_b 4 0 |
| 252 | k = ulong 0 |
| 253 | while as_word 27 true bound 65537 |
| 254 | as_lim k |
| 255 | as_type add k 32 |
| 256 | k = add k 1 |
| 257 | end |
| 258 | as_patch at k |
| 259 | end |
| 260 | fn as_type t ulong to void |
| 261 | as_sp |
| 262 | as_sp2 37 116 |
| 263 | ck_eqn as_d32 t |
| 264 | as_sp |
| 265 | as_eat 61 |
| 266 | as_sp |
| 267 | kd = add as_wsel 15 20 1 |
| 268 | as_b 1 kd |
| 269 | sz = ulong 0 |
| 270 | if or eq kd 1 eq kd 2 |
| 271 | sp = as_tva |
| 272 | sz = sp |
| 273 | end |
| 274 | if eq kd 3 |
| 275 | sz = as_tstr |
| 276 | end |
| 277 | if or eq kd 4 eq kd 5 |
| 278 | as_tref |
| 279 | end |
| 280 | if eq kd 6 |
| 281 | as_tfrg |
| 282 | end |
| 283 | as_eol |
| 284 | tx_tset t kd sz |
| 285 | end |
| 286 | fn ck_eqn a ulong b ulong to void |
| 287 | if ne a b |
| 288 | as_err |
| 289 | end |
| 290 | end |
| 291 | fn as_tva to ulong |
| 292 | as_sp |
| 293 | e = as_ty |
| 294 | as_sp |
| 295 | c = as_d32 |
| 296 | as_b 4 e |
| 297 | as_b 4 c |
| 298 | ret tx_mul c tx_size e |
| 299 | end |
| 300 | fn as_tstr to ulong |
| 301 | as_sp |
| 302 | sz = as_d32 |
| 303 | as_b 4 sz |
| 304 | at = as_here |
| 305 | as_b 4 0 |
| 306 | nf = ulong 0 |
| 307 | while eq as_pk 0 32 bound 16777216 |
| 308 | as_sp |
| 309 | as_b 4 as_ty |
| 310 | as_eat 64 |
| 311 | as_b 4 as_d32 |
| 312 | as_eat 58 |
| 313 | as_b 1 as_wsel 13 14 |
| 314 | nf = add nf 1 |
| 315 | end |
| 316 | as_patch at nf |
| 317 | ret sz |
| 318 | end |
| 319 | fn as_tref to void |
| 320 | as_sp |
| 321 | ac = as_wsel 3 4 |
| 322 | as_sp |
| 323 | sp = as_wsel 5 9 |
| 324 | as_sp |
| 325 | as_b 4 as_ty |
| 326 | as_b 1 ac |
| 327 | as_b 1 sp |
| 328 | end |
| 329 | fn as_tfrg to void |
| 330 | as_sp |
| 331 | as_b 1 as_wsel 10 12 |
| 332 | for j ulong 3 |
| 333 | as_sp |
| 334 | as_b 2 as_dlim 65535 |
| 335 | end |
| 336 | as_sp |
| 337 | as_b 4 as_ty |
| 338 | end |
| 339 | fn as_globs to void |
| 340 | at = as_here |
| 341 | as_b 4 0 |
| 342 | k = ulong 0 |
| 343 | while as_word 6 true bound 65537 |
| 344 | as_lim k |
| 345 | as_glob k |
| 346 | k = add k 1 |
| 347 | end |
| 348 | as_patch at k |
| 349 | end |
| 350 | fn as_glob g ulong to void |
| 351 | as_sp |
| 352 | as_sp2 64 103 |
| 353 | ck_eqn as_d32 g |
| 354 | as_sp |
| 355 | t = as_ty |
| 356 | as_sp |
| 357 | c = ulong 2 |
| 358 | if not as_word 32 true |
| 359 | c = as_wsel 21 22 |
| 360 | end |
| 361 | as_b 4 t |
| 362 | as_b 1 c |
| 363 | if eq c 1 |
| 364 | n = tx_size t |
| 365 | if gt n 0 |
| 366 | as_sp |
| 367 | end |
| 368 | for j ulong n bound 16777216 |
| 369 | as_b 1 as_hexn 2 |
| 370 | end |
| 371 | end |
| 372 | as_eol |
| 373 | end |
| 374 | note ---- functions ---------------------------------------------------------------------------------- |
| 375 | fn as_funcs to void |
| 376 | at = as_here |
| 377 | as_b 4 0 |
| 378 | k = ulong 0 |
| 379 | while lt get w_s 0 get w_s 1 bound 65537 |
| 380 | as_lim k |
| 381 | as_fn k |
| 382 | k = add k 1 |
| 383 | end |
| 384 | as_patch at k |
| 385 | end |
| 386 | fn as_fn f ulong to void |
| 387 | k = as_wsel 0 2 |
| 388 | as_sp |
| 389 | as_sp2 64 102 |
| 390 | ck_eqn as_d32 f |
| 391 | as_sp |
| 392 | as_kw 23 |
| 393 | as_sp |
| 394 | fl = as_dlim 255 |
| 395 | if eq k 1 |
| 396 | as_kern |
| 397 | end |
| 398 | as_b 1 k |
| 399 | as_b 1 fl |
| 400 | at = as_here |
| 401 | as_b 4 0 |
| 402 | n = ulong 0 |
| 403 | while and eq as_pk 0 32 eq as_pk 1 37 bound 65537 |
| 404 | as_lim n |
| 405 | as_sp |
| 406 | as_eat 37 |
| 407 | ck_eqn as_dec add n 1 |
| 408 | as_eat 58 |
| 409 | as_b 4 as_ty |
| 410 | n = add n 1 |
| 411 | end |
| 412 | as_patch at n |
| 413 | as_sp |
| 414 | as_sp2 45 62 |
| 415 | as_sp |
| 416 | as_b 4 as_ty |
| 417 | as_eol |
| 418 | if eq k 1 |
| 419 | for j ulong 3 |
| 420 | as_b 4 get w_k j |
| 421 | end |
| 422 | as_b 1 get w_k 3 |
| 423 | end |
| 424 | put w_s 4 add n 1 |
| 425 | as_blks |
| 426 | end |
| 427 | fn as_kern to void |
| 428 | as_sp |
| 429 | as_kw 24 |
| 430 | for j ulong 3 |
| 431 | as_sp |
| 432 | put w_k j as_d32 |
| 433 | end |
| 434 | as_sp |
| 435 | as_kw 25 |
| 436 | as_sp |
| 437 | put w_k 3 as_dlim 255 |
| 438 | end |
| 439 | fn as_blks to void |
| 440 | at = as_here |
| 441 | as_b 4 0 |
| 442 | j = ulong 0 |
| 443 | while as_word 26 true bound 65537 |
| 444 | as_lim j |
| 445 | as_blk j |
| 446 | j = add j 1 |
| 447 | end |
| 448 | as_kw 28 |
| 449 | as_eol |
| 450 | as_patch at j |
| 451 | end |
| 452 | fn as_blk j ulong to void |
| 453 | as_sp |
| 454 | as_eat 94 |
| 455 | ck_eqn as_dec j |
| 456 | at = as_here |
| 457 | as_b 4 0 |
| 458 | n = ulong 0 |
| 459 | while eq as_pk 0 32 bound 65537 |
| 460 | as_lim n |
| 461 | as_sp |
| 462 | as_eat 37 |
| 463 | v = get w_s 4 |
| 464 | ck_eqn as_dec v |
| 465 | put w_s 4 add v 1 |
| 466 | as_eat 58 |
| 467 | as_b 4 as_ty |
| 468 | n = add n 1 |
| 469 | end |
| 470 | as_eol |
| 471 | as_patch at n |
| 472 | at = as_here |
| 473 | as_b 4 0 |
| 474 | n = 0 |
| 475 | while as_more bound 16777216 |
| 476 | as_op |
| 477 | n = add n 1 |
| 478 | end |
| 479 | as_patch at n |
| 480 | end |
| 481 | note another op line follows (not block, not end, not the input end) |
| 482 | fn as_more to bool |
| 483 | if ge get w_s 0 get w_s 1 |
| 484 | ret false |
| 485 | end |
| 486 | if as_word 26 false |
| 487 | ret false |
| 488 | end |
| 489 | ret not as_word 28 false |
| 490 | end |
| 491 | fn as_op to void |
| 492 | nd = ulong 0 |
| 493 | put w_k 4 0 |
| 494 | put w_k 5 0 |
| 495 | if eq as_pk 0 37 |
| 496 | as_eat 37 |
| 497 | put w_k 4 as_dec |
| 498 | nd = 1 |
| 499 | if and eq as_pk 0 32 eq as_pk 1 37 |
| 500 | as_sp |
| 501 | as_eat 37 |
| 502 | put w_k 5 as_dec |
| 503 | nd = 2 |
| 504 | end |
| 505 | as_sp |
| 506 | as_eat 61 |
| 507 | as_sp |
| 508 | end |
| 509 | op = as_mnem |
| 510 | as_b 1 op |
| 511 | t = ulong 0 |
| 512 | if ne get x_rty op 0 |
| 513 | as_sp |
| 514 | t = as_ty |
| 515 | end |
| 516 | as_b 4 t |
| 517 | if ne get x_rim op 0 |
| 518 | as_sp |
| 519 | as_b 8 as_imm op t |
| 520 | end |
| 521 | as_defs op t nd |
| 522 | as_opnds op |
| 523 | end |
| 524 | fn as_defs op ulong t ulong nd ulong to void |
| 525 | d = tx_ndef op t |
| 526 | ck_eqn nd d |
| 527 | v = get w_s 4 |
| 528 | if ge d 1 |
| 529 | ck_eqn get w_k 4 v |
| 530 | end |
| 531 | if eq d 2 |
| 532 | ck_eqn get w_k 5 add v 1 |
| 533 | end |
| 534 | put w_s 4 add v d |
| 535 | end |
| 536 | fn as_mnem to ulong |
| 537 | p = get w_s 0 |
| 538 | e = as_tend |
| 539 | for op ulong 256 |
| 540 | if ne get x_rok op 0 |
| 541 | if as_rnis op p e |
| 542 | put w_s 0 e |
| 543 | ret op |
| 544 | end |
| 545 | end |
| 546 | end |
| 547 | as_err |
| 548 | ret 0 |
| 549 | end |
| 550 | fn as_rnis op ulong p ulong e ulong to bool |
| 551 | n = ulong get x_rnl op |
| 552 | if ne sub e p n |
| 553 | ret false |
| 554 | end |
| 555 | s = ulong get x_rno op |
| 556 | for j ulong n bound 16 |
| 557 | if ne get w_in add p j get x_rows add s j |
| 558 | ret false |
| 559 | end |
| 560 | end |
| 561 | ret true |
| 562 | end |
| 563 | fn as_imm op ulong t ulong to ulong |
| 564 | if or eq op 52 and ge op 111 le op 116 |
| 565 | as_sp2 64 103 |
| 566 | ret as_dec |
| 567 | end |
| 568 | if ne op 1 |
| 569 | ret as_dec |
| 570 | end |
| 571 | f = tx_cf t |
| 572 | if eq f 1 |
| 573 | ret sub 1 as_wsel 30 31 |
| 574 | end |
| 575 | if eq f 4 |
| 576 | as_sp2 48 120 |
| 577 | ret as_hexn mul tx_sb t 2 |
| 578 | end |
| 579 | if eq f 3 |
| 580 | ret as_sint t |
| 581 | end |
| 582 | v = as_dec |
| 583 | if not tx_canon t v |
| 584 | as_err |
| 585 | end |
| 586 | ret v |
| 587 | end |
| 588 | fn as_sint t ulong to ulong |
| 589 | ng = eq as_pk 0 45 |
| 590 | if ng |
| 591 | as_eat 45 |
| 592 | end |
| 593 | v = as_dec |
| 594 | if ng |
| 595 | if or eq v 0 gt v 9223372036854775808 |
| 596 | as_err |
| 597 | end |
| 598 | v = sub 0 v |
| 599 | else |
| 600 | if ne shr v 63 0 |
| 601 | as_err |
| 602 | end |
| 603 | end |
| 604 | if not tx_canon t v |
| 605 | as_err |
| 606 | end |
| 607 | ret v |
| 608 | end |
| 609 | fn as_opnds op ulong to void |
| 610 | c = ulong 0 |
| 611 | while eq as_pk 0 32 bound 129 |
| 612 | as_sp |
| 613 | if ge c 128 |
| 614 | as_err |
| 615 | end |
| 616 | put w_op c uint as_opnd op c |
| 617 | c = add c 1 |
| 618 | end |
| 619 | as_eol |
| 620 | fx = add tx_ntok op ulong get x_rfx op |
| 621 | if ne get x_rva op 0 |
| 622 | if lt c fx |
| 623 | as_err |
| 624 | end |
| 625 | as_b 4 sub c fx |
| 626 | else |
| 627 | ck_eqn c fx |
| 628 | end |
| 629 | for j ulong c bound 128 |
| 630 | as_b 4 ulong get w_op j |
| 631 | end |
| 632 | end |
| 633 | fn as_opnd op ulong s ulong to ulong |
| 634 | k = tx_slot op s |
| 635 | if eq k 1 |
| 636 | as_eat 94 |
| 637 | ret as_d32 |
| 638 | end |
| 639 | if eq k 2 |
| 640 | as_sp2 64 102 |
| 641 | ret as_d32 |
| 642 | end |
| 643 | as_eat 37 |
| 644 | ret as_d32 |
| 645 | end |
| 646 | fn main to int |
| 647 | tx_init |
| 648 | n = ulong 0 |
| 649 | while lt n len w_in bound 16777217 |
| 650 | k = read 0 w_in n sub len w_in n |
| 651 | if lt k 0 |
| 652 | exit 27 |
| 653 | end |
| 654 | if eq k 0 |
| 655 | break |
| 656 | end |
| 657 | n = add n ulong k |
| 658 | end |
| 659 | if eq n len w_in |
| 660 | k = read 0 w_ob 0 1 |
| 661 | if lt k 0 |
| 662 | exit 27 |
| 663 | end |
| 664 | if gt k 0 |
| 665 | exit 23 |
| 666 | end |
| 667 | end |
| 668 | put w_s 1 n |
| 669 | put w_s 2 1 |
| 670 | as_head |
| 671 | as_types |
| 672 | as_globs |
| 673 | as_funcs |
| 674 | o = ulong 0 |
| 675 | m = get w_s 3 |
| 676 | while lt o m bound 16777217 |
| 677 | k = write 1 w_ob o sub m o |
| 678 | if le k 0 |
| 679 | exit 27 |
| 680 | end |
| 681 | o = add o ulong k |
| 682 | end |
| 683 | ret 0 |
| 684 | end |