omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / src / freest.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Portable scalar freest profile. Lexer, names and typed nodes derived from toc.om at dfd8393. |
| 3 | note Entire unit is structurally checked before stdout, including unused helpers. |
| 4 | note Supported: integer scalar signatures/constants/locals, if/else/ret, acyclic calls, |
| 5 | note unsigned wrap arithmetic, bitwise operations and literal-count shifts below operand width, comparisons, safe widening casts. |
| 6 | note No loops, storage, arrays, text, main, floats, host words, recursion or potentially trapping operations. |
| 7 | global src uchar 65536 |
| 8 | global slen ulong 1 |
| 9 | global cur ulong 1 |
| 10 | global line ulong 1 |
| 11 | global tk ulong 1 |
| 12 | global tw ulong 1 |
| 13 | global tl ulong 1 |
| 14 | global ts ulong 1 |
| 15 | global held ulong 1 |
| 16 | global outb uchar 262144 |
| 17 | global oc ulong 1 |
| 18 | global msg uchar 64 |
| 19 | global digs uchar 24 |
| 20 | global mc ulong 1 |
| 21 | global nf ulong 1 |
| 22 | global fn_off ulong 64 |
| 23 | global fn_nl ulong 64 |
| 24 | global fn_ret ulong 64 |
| 25 | global fn_np ulong 64 |
| 26 | global fn_pt ulong 384 |
| 27 | global fn_poff ulong 384 |
| 28 | global fn_pnl ulong 384 |
| 29 | global nloc ulong 1 |
| 30 | global loc_off ulong 32 |
| 31 | global loc_nl ulong 32 |
| 32 | global loc_ty ulong 32 |
| 33 | global bd ulong 1 |
| 34 | global bk ulong 32 |
| 35 | global cfi ulong 1 |
| 36 | global hasmain ulong 1 |
| 37 | |
| 38 | global nn ulong 1 |
| 39 | global nk ulong 1024 |
| 40 | global nt ulong 1024 |
| 41 | global na ulong 1024 |
| 42 | global nb ulong 1024 |
| 43 | global nv ulong 1024 |
| 44 | global nx ulong 1024 |
| 45 | global args ulong 6144 |
| 46 | global saved ulong 32 |
| 47 | note Signature metadata matches the separately seeded checked frontend. |
| 48 | global seeded ulong 1 |
| 49 | global nc ulong 1 |
| 50 | global co_off ulong 32 |
| 51 | global co_nl ulong 32 |
| 52 | global co_ty ulong 32 |
| 53 | global co_val ulong 32 |
| 54 | global co_neg ulong 32 |
| 55 | global edges ulong 4096 |
| 56 | text cword "const" |
| 57 | text capmsg "unsupported freest scalar capability\n" |
| 58 | |
| 59 | text preamble "/* Omega Bridge portable nontrapping scalar profile. No host runtime. */\n#include <stdint.h>\n#if defined(__GNUC__)\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wunused-function\"\n#pragma GCC diagnostic ignored \"-Wunused-parameter\"\n#pragma GCC diagnostic ignored \"-Wunused-variable\"\n#endif\nstatic unsigned ombr_fs_width(unsigned t){static const unsigned w[]={0,1,8,16,32,64,8,16,32,64};return w[t];}\nstatic int ombr_fs_valid(unsigned t,uint64_t v){unsigned w=ombr_fs_width(t);if(t<6)return w==64||v<((uint64_t)1<<w);if(w==64)return 1;uint64_t m=(uint64_t)1<<(w-1);return v<m||v>=UINT64_MAX-m+1;}\nstatic uint64_t ombr_fs_op(unsigned op,unsigned t,uint64_t a,uint64_t b){unsigned w=ombr_fs_width(t);uint64_t mask=UINT64_MAX>>(64-w);if(op>=6&&op<=11){uint64_t x=a,y=b;if(t>=6){x^=(uint64_t)1<<63;y^=(uint64_t)1<<63;}switch(op){case 6:return x==y;case 7:return x!=y;case 8:return x<y;case 9:return x<=y;case 10:return x>y;default:return x>=y;}}switch(op){case 1:return (a+b)&mask;case 2:return (a-b)&mask;case 3:return (a*b)&mask;case 12:return a&b;case 13:return a|b;case 14:return a^b;case 15:return (a<<b)&mask;case 16:return a>>b;case 17:return (0-a)&mask;default:return t==1?!a:t>=6?~a:(~a)&mask;}}\n" |
| 60 | text tail "#if defined(__GNUC__)\n#pragma GCC diagnostic pop\n#endif\n" |
| 61 | text cstatic "static uint64_t " |
| 62 | text cuint "uint64_t " |
| 63 | text cvoid "void" |
| 64 | text cret "return " |
| 65 | text cif "if(" |
| 66 | text celse "}else{\n" |
| 67 | text cclose "}\n" |
| 68 | text copen "){\n" |
| 69 | text cul "ULL" |
| 70 | text cop "ombr_fs_op(" |
| 71 | text cvalid "if(!ombr_fs_valid(" |
| 72 | text cbad "))return 32;\n" |
| 73 | text cexport "uint32_t omega_" |
| 74 | text cresult "uint64_t *result" |
| 75 | text cptr "){\nif(!result || (uintptr_t)result%_Alignof(uint64_t) || (uintptr_t)result>UINTPTR_MAX-sizeof(uint64_t))return 32;\n" |
| 76 | text cstore "*result=" |
| 77 | text cdone ");\nreturn 0;\n}\n" |
| 78 | text cproto ");\n" |
| 79 | |
| 80 | fn mputb b uchar to void |
| 81 | m = get mc 0 |
| 82 | put msg m b |
| 83 | put mc 0 add m 1 |
| 84 | end |
| 85 | |
| 86 | fn mputn v ulong to void |
| 87 | d = new uchar 20 0 |
| 88 | i = ulong 0 |
| 89 | x = v |
| 90 | put d 0 uchar add rem x 10 48 |
| 91 | x = div x 10 |
| 92 | i = add i 1 |
| 93 | while gt x 0 |
| 94 | put d i uchar add rem x 10 48 |
| 95 | x = div x 10 |
| 96 | i = add i 1 |
| 97 | end |
| 98 | j = i |
| 99 | while gt j 0 |
| 100 | j = sub j 1 |
| 101 | mputb get d j |
| 102 | end |
| 103 | end |
| 104 | |
| 105 | fn fail code ulong to void |
| 106 | put mc 0 0 |
| 107 | mputb 116 |
| 108 | mputb 111 |
| 109 | mputb 99 |
| 110 | mputb 58 |
| 111 | mputb 32 |
| 112 | mputb 108 |
| 113 | mputb 105 |
| 114 | mputb 110 |
| 115 | mputb 101 |
| 116 | mputb 32 |
| 117 | mputn get line 0 |
| 118 | mputb 58 |
| 119 | mputb 32 |
| 120 | mputb 101 |
| 121 | mputb 114 |
| 122 | mputb 114 |
| 123 | mputb 111 |
| 124 | mputb 114 |
| 125 | mputb 32 |
| 126 | mputn code |
| 127 | mputb 10 |
| 128 | w = write 2 msg 0 get mc 0 |
| 129 | if eq code 24 |
| 130 | w = write 2 capmsg 0 len capmsg |
| 131 | end |
| 132 | exit int code |
| 133 | end |
| 134 | |
| 135 | fn e_b c ulong to void |
| 136 | o = get oc 0 |
| 137 | if ge o 262144 |
| 138 | fail 14 |
| 139 | end |
| 140 | put outb o uchar c |
| 141 | put oc 0 add o 1 |
| 142 | end |
| 143 | |
| 144 | fn e_src a ulong n ulong to void |
| 145 | i = ulong 0 |
| 146 | while lt i n |
| 147 | e_b ulong get src add a i |
| 148 | i = add i 1 |
| 149 | end |
| 150 | end |
| 151 | |
| 152 | fn isw w ulong to ulong |
| 153 | if and eq get tk 0 1 eq get tw 0 w |
| 154 | ret 1 |
| 155 | end |
| 156 | ret 0 |
| 157 | end |
| 158 | |
| 159 | fn tok_num to void |
| 160 | v = ulong 0 |
| 161 | i = ulong 0 |
| 162 | minus = ulong 0 |
| 163 | p = get ts 0 |
| 164 | if eq get src p 45 |
| 165 | minus = 1 |
| 166 | i = 1 |
| 167 | end |
| 168 | while lt i get tl 0 |
| 169 | b = get src add p i |
| 170 | v = add mul v 10 sub ulong b ulong 48 |
| 171 | i = add i 1 |
| 172 | end |
| 173 | if eq minus 1 |
| 174 | v = sub 0 v |
| 175 | end |
| 176 | put tw 0 v |
| 177 | put tk 0 2 |
| 178 | end |
| 179 | |
| 180 | fn tok2 c uchar to void |
| 181 | put ts 0 get cur 0 |
| 182 | w = ulong 0 |
| 183 | sh = ulong 0 |
| 184 | n = ulong 0 |
| 185 | go = ulong 1 |
| 186 | while eq go 1 |
| 187 | if ge get cur 0 get slen 0 |
| 188 | go = 0 |
| 189 | else |
| 190 | b = get src get cur 0 |
| 191 | if le b 32 |
| 192 | go = 0 |
| 193 | else |
| 194 | if lt n 8 |
| 195 | w = or w shl ulong b sh |
| 196 | sh = add sh 8 |
| 197 | end |
| 198 | n = add n 1 |
| 199 | put cur 0 add get cur 0 1 |
| 200 | end |
| 201 | end |
| 202 | end |
| 203 | put tw 0 w |
| 204 | put tl 0 n |
| 205 | f = get src get ts 0 |
| 206 | num = ulong 0 |
| 207 | if eq f 45 |
| 208 | num = 1 |
| 209 | end |
| 210 | if and ge f 48 le f 57 |
| 211 | num = 1 |
| 212 | end |
| 213 | if eq num 1 |
| 214 | tok_num |
| 215 | else |
| 216 | put tk 0 1 |
| 217 | end |
| 218 | end |
| 219 | |
| 220 | fn tok to void |
| 221 | if eq get held 0 1 |
| 222 | put held 0 0 |
| 223 | ret |
| 224 | end |
| 225 | go = ulong 1 |
| 226 | while eq go 1 |
| 227 | if ge get cur 0 get slen 0 |
| 228 | put tk 0 3 |
| 229 | ret |
| 230 | end |
| 231 | c = get src get cur 0 |
| 232 | if or or eq c 32 eq c 9 eq c 13 |
| 233 | put cur 0 add get cur 0 1 |
| 234 | else |
| 235 | if eq c 10 |
| 236 | put cur 0 add get cur 0 1 |
| 237 | put line 0 add get line 0 1 |
| 238 | put tk 0 0 |
| 239 | ret |
| 240 | end |
| 241 | if lt c 32 |
| 242 | fail 16 |
| 243 | end |
| 244 | tok2 c |
| 245 | ret |
| 246 | end |
| 247 | end |
| 248 | end |
| 249 | |
| 250 | fn ty_of w ulong to ulong |
| 251 | if eq w 1819242338 |
| 252 | ret 1 |
| 253 | end |
| 254 | if eq w 491260502901 |
| 255 | ret 2 |
| 256 | end |
| 257 | if eq w 128034844210037 |
| 258 | ret 3 |
| 259 | end |
| 260 | if eq w 1953393013 |
| 261 | ret 4 |
| 262 | end |
| 263 | if eq w 444234427509 |
| 264 | ret 5 |
| 265 | end |
| 266 | if eq w 1702132066 |
| 267 | ret 6 |
| 268 | end |
| 269 | if eq w 500136110195 |
| 270 | ret 7 |
| 271 | end |
| 272 | if eq w 7630441 |
| 273 | ret 8 |
| 274 | end |
| 275 | if eq w 1735290732 |
| 276 | ret 9 |
| 277 | end |
| 278 | ret 0 |
| 279 | end |
| 280 | |
| 281 | fn signed t ulong to ulong |
| 282 | if ge t 6 |
| 283 | ret 1 |
| 284 | end |
| 285 | ret 0 |
| 286 | end |
| 287 | |
| 288 | fn same_name o ulong n ulong to ulong |
| 289 | if ne n get tl 0 |
| 290 | ret 0 |
| 291 | end |
| 292 | i = ulong 0 |
| 293 | while lt i n |
| 294 | if ne get src add o i get src add get ts 0 i |
| 295 | ret 0 |
| 296 | end |
| 297 | i = add i 1 |
| 298 | end |
| 299 | ret 1 |
| 300 | end |
| 301 | |
| 302 | fn loc_of to ulong |
| 303 | i = ulong 0 |
| 304 | while lt i get nloc 0 |
| 305 | if eq same_name get loc_off i get loc_nl i 1 |
| 306 | ret i |
| 307 | end |
| 308 | i = add i 1 |
| 309 | end |
| 310 | ret 99 |
| 311 | end |
| 312 | |
| 313 | fn fn_of to ulong |
| 314 | i = ulong 0 |
| 315 | while lt i get nf 0 |
| 316 | if eq same_name get fn_off i get fn_nl i 1 |
| 317 | ret i |
| 318 | end |
| 319 | i = add i 1 |
| 320 | end |
| 321 | ret 99 |
| 322 | end |
| 323 | |
| 324 | fn kwconst to ulong |
| 325 | if or eq get seeded 0 0 ne get tl 0 len cword |
| 326 | ret 0 |
| 327 | end |
| 328 | for i ulong len cword |
| 329 | if ne get src add get ts 0 i get cword i |
| 330 | ret 0 |
| 331 | end |
| 332 | end |
| 333 | ret 1 |
| 334 | end |
| 335 | |
| 336 | fn constof to ulong |
| 337 | i = ulong 0 |
| 338 | while lt i get nc 0 |
| 339 | if eq same_name get co_off i get co_nl i 1 |
| 340 | ret i |
| 341 | end |
| 342 | i = add i 1 |
| 343 | end |
| 344 | ret 99 |
| 345 | end |
| 346 | |
| 347 | note A length is a literal or a previously checked unsigned numeric constant. |
| 348 | |
| 349 | fn parsecon to void |
| 350 | tok |
| 351 | if ne get tk 0 1 |
| 352 | fail 16 |
| 353 | end |
| 354 | i = get nc 0 |
| 355 | if ge i 32 |
| 356 | fail 14 |
| 357 | end |
| 358 | if ne constof 99 |
| 359 | fail 10 |
| 360 | end |
| 361 | put co_off i get ts 0 |
| 362 | put co_nl i get tl 0 |
| 363 | tok |
| 364 | t = ty_of get tw 0 |
| 365 | if eq t 0 |
| 366 | fail 7 |
| 367 | end |
| 368 | tok |
| 369 | if ne get tk 0 2 |
| 370 | fail 24 |
| 371 | end |
| 372 | negative = ulong 0 |
| 373 | if eq get src get ts 0 45 |
| 374 | negative = 1 |
| 375 | end |
| 376 | n = node 1 0 0 negative get tw 0 |
| 377 | checknum n t |
| 378 | put co_ty i t |
| 379 | put co_val i get nv n |
| 380 | put co_neg i get nb n |
| 381 | put nc 0 add i 1 |
| 382 | end |
| 383 | |
| 384 | fn add_loc o ulong n ulong ty ulong to void |
| 385 | i = get nloc 0 |
| 386 | if ge i 32 |
| 387 | fail 14 |
| 388 | end |
| 389 | put loc_off i o |
| 390 | put loc_nl i n |
| 391 | put loc_ty i ty |
| 392 | put nloc 0 add i 1 |
| 393 | end |
| 394 | |
| 395 | fn caplim n ulong old ulong ext ulong code ulong to void |
| 396 | if eq get seeded 0 0 |
| 397 | if ge n old |
| 398 | fail code |
| 399 | end |
| 400 | else |
| 401 | if ge n ext |
| 402 | fail code |
| 403 | end |
| 404 | end |
| 405 | end |
| 406 | |
| 407 | fn walk_sig rec ulong to void |
| 408 | tok |
| 409 | if ne get tk 0 1 |
| 410 | fail 16 |
| 411 | end |
| 412 | caplim get tl 0 9 65 13 |
| 413 | ix = get nf 0 |
| 414 | if eq rec 1 |
| 415 | if ne fn_of 99 |
| 416 | fail 10 |
| 417 | end |
| 418 | caplim ix 32 64 14 |
| 419 | put fn_off ix get ts 0 |
| 420 | put fn_nl ix get tl 0 |
| 421 | put fn_np ix 0 |
| 422 | if eq get tw 0 1852399981 |
| 423 | put hasmain 0 1 |
| 424 | end |
| 425 | put cfi 0 ix |
| 426 | else |
| 427 | put cfi 0 fn_of |
| 428 | if eq get cfi 0 99 |
| 429 | fail 3 |
| 430 | end |
| 431 | end |
| 432 | tok |
| 433 | while eq isw 28532 0 |
| 434 | if ne get tk 0 1 |
| 435 | fail 16 |
| 436 | end |
| 437 | po = get ts 0 |
| 438 | pn = get tl 0 |
| 439 | tok |
| 440 | if or eq isw 2003134838 1 eq isw 1953064037 1 |
| 441 | fail 24 |
| 442 | end |
| 443 | pty = ty_of get tw 0 |
| 444 | if eq pty 0 |
| 445 | fail 7 |
| 446 | end |
| 447 | if eq rec 1 |
| 448 | caplim get fn_np ix 4 6 24 |
| 449 | slot = add mul ix 6 get fn_np ix |
| 450 | put fn_pt slot pty |
| 451 | put fn_poff slot po |
| 452 | put fn_pnl slot pn |
| 453 | put fn_np ix add get fn_np ix 1 |
| 454 | end |
| 455 | tok |
| 456 | end |
| 457 | tok |
| 458 | rt = ulong 10 |
| 459 | if eq isw 1684631414 0 |
| 460 | rt = ty_of get tw 0 |
| 461 | if eq rt 0 |
| 462 | fail 7 |
| 463 | end |
| 464 | end |
| 465 | if eq rec 1 |
| 466 | put fn_ret ix rt |
| 467 | put nf 0 add ix 1 |
| 468 | end |
| 469 | end |
| 470 | |
| 471 | fn e_comma to void |
| 472 | e_b 44 |
| 473 | e_b 32 |
| 474 | end |
| 475 | |
| 476 | fn e_dec v ulong to void |
| 477 | x = v |
| 478 | p = ulong 0 |
| 479 | put digs 0 uchar add rem x 10 48 |
| 480 | x = div x 10 |
| 481 | p = add p 1 |
| 482 | while gt x 0 |
| 483 | put digs p uchar add rem x 10 48 |
| 484 | x = div x 10 |
| 485 | p = add p 1 |
| 486 | end |
| 487 | while gt p 0 |
| 488 | p = sub p 1 |
| 489 | e_b ulong get digs p |
| 490 | end |
| 491 | end |
| 492 | |
| 493 | fn op_of w ulong to ulong |
| 494 | if eq w 6579297 |
| 495 | ret 1 |
| 496 | end |
| 497 | if eq w 6452595 |
| 498 | ret 2 |
| 499 | end |
| 500 | if eq w 7107949 |
| 501 | ret 3 |
| 502 | end |
| 503 | if eq w 7760228 |
| 504 | ret 4 |
| 505 | end |
| 506 | if eq w 7169394 |
| 507 | ret 5 |
| 508 | end |
| 509 | if eq w 29029 |
| 510 | ret 6 |
| 511 | end |
| 512 | if eq w 25966 |
| 513 | ret 7 |
| 514 | end |
| 515 | if eq w 29804 |
| 516 | ret 8 |
| 517 | end |
| 518 | if eq w 25964 |
| 519 | ret 9 |
| 520 | end |
| 521 | if eq w 29799 |
| 522 | ret 10 |
| 523 | end |
| 524 | if eq w 25959 |
| 525 | ret 11 |
| 526 | end |
| 527 | if eq w 6581857 |
| 528 | ret 12 |
| 529 | end |
| 530 | if eq w 29295 |
| 531 | ret 13 |
| 532 | end |
| 533 | ret 0 |
| 534 | end |
| 535 | |
| 536 | fn node k ulong t ulong a ulong b ulong v ulong to ulong |
| 537 | n = get nn 0 |
| 538 | if ge n 1024 |
| 539 | fail 14 |
| 540 | end |
| 541 | put nk n k |
| 542 | put nt n t |
| 543 | put na n a |
| 544 | put nb n b |
| 545 | put nv n v |
| 546 | put nn 0 add n 1 |
| 547 | ret n |
| 548 | end |
| 549 | |
| 550 | fn width t ulong to ulong |
| 551 | if eq t 1 |
| 552 | ret 1 |
| 553 | end |
| 554 | if or eq t 2 eq t 6 |
| 555 | ret 8 |
| 556 | end |
| 557 | if or eq t 3 eq t 7 |
| 558 | ret 16 |
| 559 | end |
| 560 | if or eq t 4 eq t 8 |
| 561 | ret 32 |
| 562 | end |
| 563 | ret 64 |
| 564 | end |
| 565 | |
| 566 | fn checknum n ulong t ulong to void |
| 567 | v = get nv n |
| 568 | isneg = get nb n |
| 569 | w = width t |
| 570 | if eq signed t 1 |
| 571 | lim = shl ulong 1 sub w 1 |
| 572 | if eq isneg 1 |
| 573 | if gt sub 0 v lim |
| 574 | fail 8 |
| 575 | end |
| 576 | else |
| 577 | if ge v lim |
| 578 | fail 8 |
| 579 | end |
| 580 | end |
| 581 | else |
| 582 | if eq isneg 1 |
| 583 | fail 8 |
| 584 | end |
| 585 | if lt w 64 |
| 586 | if ge v shl ulong 1 w |
| 587 | fail 8 |
| 588 | end |
| 589 | end |
| 590 | end |
| 591 | put nt n t |
| 592 | end |
| 593 | |
| 594 | fn expect n ulong t ulong to void |
| 595 | if eq get nt n 0 |
| 596 | checknum n t |
| 597 | else |
| 598 | if ne get nt n t |
| 599 | fail 7 |
| 600 | end |
| 601 | end |
| 602 | end |
| 603 | |
| 604 | fn named ch ulong n ulong to void |
| 605 | e_b ch |
| 606 | e_dec n |
| 607 | end |
| 608 | |
| 609 | fn emitstr k ulong to void |
| 610 | if eq k 0 |
| 611 | for j ulong len preamble |
| 612 | e_b ulong get preamble j |
| 613 | end |
| 614 | ret |
| 615 | end |
| 616 | if eq k 1 |
| 617 | for j ulong len tail |
| 618 | e_b ulong get tail j |
| 619 | end |
| 620 | ret |
| 621 | end |
| 622 | if eq k 2 |
| 623 | for j ulong len cstatic |
| 624 | e_b ulong get cstatic j |
| 625 | end |
| 626 | ret |
| 627 | end |
| 628 | if eq k 3 |
| 629 | for j ulong len cuint |
| 630 | e_b ulong get cuint j |
| 631 | end |
| 632 | ret |
| 633 | end |
| 634 | if eq k 4 |
| 635 | for j ulong len cvoid |
| 636 | e_b ulong get cvoid j |
| 637 | end |
| 638 | ret |
| 639 | end |
| 640 | if eq k 5 |
| 641 | for j ulong len cret |
| 642 | e_b ulong get cret j |
| 643 | end |
| 644 | ret |
| 645 | end |
| 646 | if eq k 6 |
| 647 | for j ulong len cif |
| 648 | e_b ulong get cif j |
| 649 | end |
| 650 | ret |
| 651 | end |
| 652 | if eq k 7 |
| 653 | for j ulong len celse |
| 654 | e_b ulong get celse j |
| 655 | end |
| 656 | ret |
| 657 | end |
| 658 | if eq k 8 |
| 659 | for j ulong len cclose |
| 660 | e_b ulong get cclose j |
| 661 | end |
| 662 | ret |
| 663 | end |
| 664 | if eq k 9 |
| 665 | for j ulong len copen |
| 666 | e_b ulong get copen j |
| 667 | end |
| 668 | ret |
| 669 | end |
| 670 | if eq k 10 |
| 671 | for j ulong len cul |
| 672 | e_b ulong get cul j |
| 673 | end |
| 674 | ret |
| 675 | end |
| 676 | if eq k 11 |
| 677 | for j ulong len cop |
| 678 | e_b ulong get cop j |
| 679 | end |
| 680 | ret |
| 681 | end |
| 682 | if eq k 12 |
| 683 | for j ulong len cvalid |
| 684 | e_b ulong get cvalid j |
| 685 | end |
| 686 | ret |
| 687 | end |
| 688 | if eq k 13 |
| 689 | for j ulong len cbad |
| 690 | e_b ulong get cbad j |
| 691 | end |
| 692 | ret |
| 693 | end |
| 694 | if eq k 14 |
| 695 | for j ulong len cexport |
| 696 | e_b ulong get cexport j |
| 697 | end |
| 698 | ret |
| 699 | end |
| 700 | if eq k 15 |
| 701 | for j ulong len cresult |
| 702 | e_b ulong get cresult j |
| 703 | end |
| 704 | ret |
| 705 | end |
| 706 | if eq k 16 |
| 707 | for j ulong len cptr |
| 708 | e_b ulong get cptr j |
| 709 | end |
| 710 | ret |
| 711 | end |
| 712 | if eq k 17 |
| 713 | for j ulong len cstore |
| 714 | e_b ulong get cstore j |
| 715 | end |
| 716 | ret |
| 717 | end |
| 718 | if eq k 18 |
| 719 | for j ulong len cdone |
| 720 | e_b ulong get cdone j |
| 721 | end |
| 722 | ret |
| 723 | end |
| 724 | if eq k 19 |
| 725 | for j ulong len cproto |
| 726 | e_b ulong get cproto j |
| 727 | end |
| 728 | ret |
| 729 | end |
| 730 | fail 24 |
| 731 | end |
| 732 | |
| 733 | |
| 734 | fn namescan to void |
| 735 | put cur 0 1 |
| 736 | put line 0 1 |
| 737 | put held 0 0 |
| 738 | tok |
| 739 | while ne get tk 0 3 |
| 740 | if eq isw 28262 1 |
| 741 | walk_sig 1 |
| 742 | else |
| 743 | if eq kwconst 1 |
| 744 | parsecon |
| 745 | end |
| 746 | end |
| 747 | while and ne get tk 0 0 ne get tk 0 3 |
| 748 | tok |
| 749 | end |
| 750 | if ne get tk 0 3 |
| 751 | tok |
| 752 | end |
| 753 | end |
| 754 | if or eq get nf 0 0 eq get hasmain 0 1 |
| 755 | fail 24 |
| 756 | end |
| 757 | for f ulong get nf 0 |
| 758 | if eq get fn_ret f 10 |
| 759 | fail 24 |
| 760 | end |
| 761 | end |
| 762 | end |
| 763 | |
| 764 | note Expression nodes are shared with the checked hosted converter, but the accepted |
| 765 | note operations here must be total over every admitted scalar value, not just literals. |
| 766 | fn expr exp ulong to ulong |
| 767 | tok |
| 768 | if eq get tk 0 2 |
| 769 | negative = ulong 0 |
| 770 | if eq get src get ts 0 45 |
| 771 | negative = 1 |
| 772 | end |
| 773 | n = node 1 0 0 negative get tw 0 |
| 774 | if gt exp 0 |
| 775 | checknum n exp |
| 776 | end |
| 777 | ret n |
| 778 | end |
| 779 | if ne get tk 0 1 |
| 780 | fail 4 |
| 781 | end |
| 782 | w = get tw 0 |
| 783 | if or eq w 1702195828 eq w 435728179558 |
| 784 | bv = ulong 0 |
| 785 | if eq w 1702195828 |
| 786 | bv = 1 |
| 787 | end |
| 788 | ret node 1 1 0 0 bv |
| 789 | end |
| 790 | t = ty_of w |
| 791 | if gt t 0 |
| 792 | a = expr 0 |
| 793 | if eq get nt a 0 |
| 794 | checknum a t |
| 795 | ret a |
| 796 | end |
| 797 | s = get nt a |
| 798 | if or and ge t 6 lt s 6 and lt t 6 ge s 6 |
| 799 | fail 24 |
| 800 | end |
| 801 | if lt width t width s |
| 802 | fail 24 |
| 803 | end |
| 804 | ret node 4 t a 0 0 |
| 805 | end |
| 806 | op = op_of w |
| 807 | if eq w 7499640 |
| 808 | op = 14 |
| 809 | end |
| 810 | if eq w 7104627 |
| 811 | op = 15 |
| 812 | end |
| 813 | if eq w 7497843 |
| 814 | op = 16 |
| 815 | end |
| 816 | if eq w 6776174 |
| 817 | op = 17 |
| 818 | end |
| 819 | if eq w 7630702 |
| 820 | op = 18 |
| 821 | end |
| 822 | if or eq op 4 eq op 5 |
| 823 | fail 24 |
| 824 | end |
| 825 | if gt op 0 |
| 826 | a = expr 0 |
| 827 | b = ulong 0 |
| 828 | if lt op 17 |
| 829 | b = expr 0 |
| 830 | if and eq get nt a 0 eq get nt b 0 |
| 831 | fail 11 |
| 832 | end |
| 833 | if eq get nt a 0 |
| 834 | checknum a get nt b |
| 835 | end |
| 836 | if eq get nt b 0 |
| 837 | checknum b get nt a |
| 838 | end |
| 839 | if ne get nt a get nt b |
| 840 | fail 7 |
| 841 | end |
| 842 | else |
| 843 | if eq get nt a 0 |
| 844 | fail 11 |
| 845 | end |
| 846 | end |
| 847 | t = get nt a |
| 848 | if or eq op 15 eq op 16 |
| 849 | if or eq t 1 ge t 6 |
| 850 | fail 24 |
| 851 | end |
| 852 | if ne get nk b 1 |
| 853 | fail 24 |
| 854 | end |
| 855 | if ge get nv b width t |
| 856 | fail 24 |
| 857 | end |
| 858 | end |
| 859 | if or le op 3 eq op 17 |
| 860 | if or eq t 1 ge t 6 |
| 861 | fail 24 |
| 862 | end |
| 863 | end |
| 864 | rt = t |
| 865 | if and ge op 6 le op 11 |
| 866 | rt = 1 |
| 867 | end |
| 868 | n = node 3 rt a b op |
| 869 | put nx n t |
| 870 | ret n |
| 871 | end |
| 872 | c = constof |
| 873 | if ne c 99 |
| 874 | ret node 1 get co_ty c 0 get co_neg c get co_val c |
| 875 | end |
| 876 | f = fn_of |
| 877 | if ne f 99 |
| 878 | ret callnode f |
| 879 | end |
| 880 | l = loc_of |
| 881 | if eq l 99 |
| 882 | fail 24 |
| 883 | end |
| 884 | ret node 2 get loc_ty l l 0 0 |
| 885 | end |
| 886 | |
| 887 | fn callnode f ulong to ulong |
| 888 | put edges add mul get cfi 0 64 f 1 |
| 889 | n = node 7 get fn_ret f 0 0 f |
| 890 | i = ulong 0 |
| 891 | while lt i get fn_np f |
| 892 | slot = add mul f 6 i |
| 893 | arg = expr get fn_pt slot |
| 894 | expect arg get fn_pt slot |
| 895 | put args add mul n 6 i arg |
| 896 | i = add i 1 |
| 897 | end |
| 898 | ret n |
| 899 | end |
| 900 | |
| 901 | fn eexpr n ulong to void |
| 902 | k = get nk n |
| 903 | a = get na n |
| 904 | b = get nb n |
| 905 | v = get nv n |
| 906 | if eq k 1 |
| 907 | e_dec v |
| 908 | emitstr 10 |
| 909 | ret |
| 910 | end |
| 911 | if eq k 2 |
| 912 | named 118 a |
| 913 | ret |
| 914 | end |
| 915 | if eq k 4 |
| 916 | eexpr a |
| 917 | ret |
| 918 | end |
| 919 | if eq k 3 |
| 920 | emitstr 11 |
| 921 | e_dec v |
| 922 | e_comma |
| 923 | e_dec get nx n |
| 924 | e_comma |
| 925 | eexpr a |
| 926 | e_comma |
| 927 | if lt v 17 |
| 928 | eexpr b |
| 929 | else |
| 930 | e_b 48 |
| 931 | end |
| 932 | e_b 41 |
| 933 | ret |
| 934 | end |
| 935 | if eq k 7 |
| 936 | named 102 v |
| 937 | e_b 40 |
| 938 | for i ulong get fn_np v |
| 939 | if gt i 0 |
| 940 | e_comma |
| 941 | end |
| 942 | eexpr get args add mul n 6 i |
| 943 | end |
| 944 | e_b 41 |
| 945 | ret |
| 946 | end |
| 947 | fail 24 |
| 948 | end |
| 949 | |
| 950 | fn signature f ulong to void |
| 951 | emitstr 2 |
| 952 | named 102 f |
| 953 | e_b 40 |
| 954 | for i ulong get fn_np f |
| 955 | if gt i 0 |
| 956 | e_comma |
| 957 | end |
| 958 | emitstr 3 |
| 959 | named 118 i |
| 960 | end |
| 961 | if eq get fn_np f 0 |
| 962 | emitstr 4 |
| 963 | end |
| 964 | e_b 41 |
| 965 | end |
| 966 | |
| 967 | fn eassign to void |
| 968 | o = get ts 0 |
| 969 | n = get tl 0 |
| 970 | known = loc_of |
| 971 | if gt n 8 |
| 972 | fail 13 |
| 973 | end |
| 974 | tok |
| 975 | if ne isw 61 1 |
| 976 | fail 24 |
| 977 | end |
| 978 | t = ulong 0 |
| 979 | if ne known 99 |
| 980 | t = get loc_ty known |
| 981 | end |
| 982 | v = expr t |
| 983 | if or eq get nt v 0 eq get nt v 10 |
| 984 | fail 7 |
| 985 | end |
| 986 | if eq known 99 |
| 987 | known = get nloc 0 |
| 988 | add_loc o n get nt v |
| 989 | emitstr 3 |
| 990 | else |
| 991 | expect v t |
| 992 | end |
| 993 | named 118 known |
| 994 | e_b 61 |
| 995 | eexpr v |
| 996 | e_b 59 |
| 997 | e_b 10 |
| 998 | end |
| 999 | |
| 1000 | fn eone to ulong |
| 1001 | w = get tw 0 |
| 1002 | if eq w 1702129518 |
| 1003 | while and ne get tk 0 0 ne get tk 0 3 |
| 1004 | tok |
| 1005 | end |
| 1006 | put held 0 1 |
| 1007 | ret 1 |
| 1008 | end |
| 1009 | if eq w 6581861 |
| 1010 | d = sub get bd 0 1 |
| 1011 | put bd 0 d |
| 1012 | put nloc 0 get saved d |
| 1013 | emitstr 8 |
| 1014 | if eq d 0 |
| 1015 | ret 0 |
| 1016 | end |
| 1017 | ret 1 |
| 1018 | end |
| 1019 | if eq w 1702063205 |
| 1020 | d = sub get bd 0 1 |
| 1021 | if ne get bk d 2 |
| 1022 | fail 15 |
| 1023 | end |
| 1024 | put nloc 0 get saved d |
| 1025 | put bk d 3 |
| 1026 | emitstr 7 |
| 1027 | ret 1 |
| 1028 | end |
| 1029 | if eq w 26217 |
| 1030 | v = expr 1 |
| 1031 | expect v 1 |
| 1032 | emitstr 6 |
| 1033 | eexpr v |
| 1034 | emitstr 9 |
| 1035 | d = get bd 0 |
| 1036 | if ge d 32 |
| 1037 | fail 14 |
| 1038 | end |
| 1039 | put bk d 2 |
| 1040 | put saved d get nloc 0 |
| 1041 | put bd 0 add d 1 |
| 1042 | ret 1 |
| 1043 | end |
| 1044 | if eq w 7628146 |
| 1045 | t = get fn_ret get cfi 0 |
| 1046 | v = expr t |
| 1047 | expect v t |
| 1048 | emitstr 5 |
| 1049 | eexpr v |
| 1050 | e_b 59 |
| 1051 | e_b 10 |
| 1052 | ret 1 |
| 1053 | end |
| 1054 | eassign |
| 1055 | ret 1 |
| 1056 | end |
| 1057 | |
| 1058 | fn efn to void |
| 1059 | walk_sig 0 |
| 1060 | f = get cfi 0 |
| 1061 | signature f |
| 1062 | e_b 123 |
| 1063 | e_b 10 |
| 1064 | put nloc 0 0 |
| 1065 | for i ulong get fn_np f |
| 1066 | s = add mul f 6 i |
| 1067 | add_loc get fn_poff s get fn_pnl s get fn_pt s |
| 1068 | end |
| 1069 | put bd 0 1 |
| 1070 | put saved 0 0 |
| 1071 | go = ulong 1 |
| 1072 | while eq go 1 |
| 1073 | tok |
| 1074 | if eq get tk 0 3 |
| 1075 | fail 15 |
| 1076 | end |
| 1077 | if ne get tk 0 0 |
| 1078 | put nn 0 0 |
| 1079 | go = eone |
| 1080 | tok |
| 1081 | if and ne get tk 0 0 ne get tk 0 3 |
| 1082 | fail 16 |
| 1083 | end |
| 1084 | put held 0 1 |
| 1085 | end |
| 1086 | end |
| 1087 | end |
| 1088 | |
| 1089 | fn spread k ulong i ulong to void |
| 1090 | for j ulong get nf 0 |
| 1091 | if eq get edges add mul k 64 j 1 |
| 1092 | put edges add mul i 64 j 1 |
| 1093 | end |
| 1094 | end |
| 1095 | end |
| 1096 | |
| 1097 | note Transitive closure rejects all recursive cycles, even in unexported helpers. |
| 1098 | fn acyclic to void |
| 1099 | for k ulong get nf 0 |
| 1100 | for i ulong get nf 0 |
| 1101 | if eq get edges add mul i 64 k 1 |
| 1102 | spread k i |
| 1103 | end |
| 1104 | end |
| 1105 | end |
| 1106 | for i ulong get nf 0 |
| 1107 | if eq get edges add mul i 64 i 1 |
| 1108 | fail 24 |
| 1109 | end |
| 1110 | end |
| 1111 | end |
| 1112 | |
| 1113 | fn exdecl f ulong to void |
| 1114 | emitstr 14 |
| 1115 | e_src get fn_off f get fn_nl f |
| 1116 | e_b 40 |
| 1117 | emitstr 15 |
| 1118 | for i ulong get fn_np f |
| 1119 | e_comma |
| 1120 | emitstr 3 |
| 1121 | named 112 i |
| 1122 | end |
| 1123 | end |
| 1124 | |
| 1125 | fn exportfn f ulong to void |
| 1126 | exdecl f |
| 1127 | emitstr 16 |
| 1128 | for i ulong get fn_np f |
| 1129 | emitstr 12 |
| 1130 | e_dec get fn_pt add mul f 6 i |
| 1131 | e_comma |
| 1132 | named 112 i |
| 1133 | emitstr 13 |
| 1134 | end |
| 1135 | emitstr 17 |
| 1136 | named 102 f |
| 1137 | e_b 40 |
| 1138 | for i ulong get fn_np f |
| 1139 | if gt i 0 |
| 1140 | e_comma |
| 1141 | end |
| 1142 | named 112 i |
| 1143 | end |
| 1144 | emitstr 18 |
| 1145 | end |
| 1146 | |
| 1147 | fn generate to void |
| 1148 | put oc 0 0 |
| 1149 | emitstr 0 |
| 1150 | for f ulong get nf 0 |
| 1151 | signature f |
| 1152 | e_b 59 |
| 1153 | e_b 10 |
| 1154 | end |
| 1155 | put cur 0 1 |
| 1156 | put line 0 1 |
| 1157 | put held 0 0 |
| 1158 | tok |
| 1159 | while ne get tk 0 3 |
| 1160 | if eq isw 28262 1 |
| 1161 | efn |
| 1162 | else |
| 1163 | if or eq kwconst 1 eq isw 1702129518 1 |
| 1164 | while and ne get tk 0 0 ne get tk 0 3 |
| 1165 | tok |
| 1166 | end |
| 1167 | else |
| 1168 | if ne get tk 0 0 |
| 1169 | fail 24 |
| 1170 | end |
| 1171 | end |
| 1172 | end |
| 1173 | tok |
| 1174 | end |
| 1175 | acyclic |
| 1176 | exportfn sub get nf 0 1 |
| 1177 | emitstr 1 |
| 1178 | end |
| 1179 | |
| 1180 | fn main to int |
| 1181 | off = ulong 0 |
| 1182 | go = ulong 1 |
| 1183 | while eq go 1 |
| 1184 | n = read 0 src off sub 65536 off |
| 1185 | if lt n 0 |
| 1186 | fail 1 |
| 1187 | end |
| 1188 | if eq n 0 |
| 1189 | go = 0 |
| 1190 | else |
| 1191 | off = add off ulong n |
| 1192 | if ge off 65536 |
| 1193 | fail 14 |
| 1194 | end |
| 1195 | end |
| 1196 | end |
| 1197 | put slen 0 off |
| 1198 | put seeded 0 1 |
| 1199 | put line 0 1 |
| 1200 | mode = get src 0 |
| 1201 | if and ne mode 70 ne mode 72 |
| 1202 | fail 24 |
| 1203 | end |
| 1204 | namescan |
| 1205 | generate |
| 1206 | if eq mode 72 |
| 1207 | put oc 0 0 |
| 1208 | exdecl sub get nf 0 1 |
| 1209 | emitstr 19 |
| 1210 | end |
| 1211 | w = write 1 outb 0 get oc 0 |
| 1212 | if ne w long get oc 0 |
| 1213 | ret 1 |
| 1214 | end |
| 1215 | ret 0 |
| 1216 | end |