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/omega-mv/blob/main/om/compiler/osc_scalar.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / compiler / osc_scalar.om` 1083 lines ยท 26518 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Scalar semantics of validated OSC IR. Checked operations return the OSC 3 note trap code as data. The host delivers that trap through the program runtime. 4 om2 5 6 note IDs from osc_ir.h. Operands enter in their canonical 64-bit representation. 7 const T_BOOL ulong 1 8 const T_U8 ulong 2 9 const T_U64 ulong 5 10 const T_I8 ulong 6 11 const T_I64 ulong 9 12 const SIGN64 ulong 0x8000000000000000 13 const OVERFLOW ulong 1 14 const DIV_ZERO ulong 2 15 const CAST_RANGE ulong 5 16 const BAD_SHIFT ulong 7 17 const BAD_OPCODE ulong 8 18 19 note Contract bindings contain known scalar arguments and the optional result. 20 note bridge c-type contract OscContractBindings 21 note Function calls preserve left-to-right checking. This frame pairs parsed 22 note arguments with parameters and records cell slices already bound at the call. 23 note bridge c-type callargs OscCallArgs 24 note bridge c-define OSC_ARG_SCALAR 1 25 note bridge c-define OSC_ARG_SLICE 2 26 note bridge c-define OSC_ARG_OWNER 3 27 note bridge c-define OSC_ARG_BORROW 4 28 note bridge c-define OSC_ARG_SCALAR_BORROW 5 29 note bridge c-define OSC_ARG_SLICE_BORROW 6 30 note bridge c-define OSC_ARG_OWNER_FORM 7 31 note bridge c-define OSC_ARG_BORROW_FORM 8 32 layout callargs 112 33 field params int 6 at 0 little 34 field args int 7 at 24 little 35 field actions uint 6 at 52 little 36 field cells int 6 at 76 little 37 field np uint at 100 little 38 field na uint at 104 little 39 field clause uint at 108 little 40 end 41 42 layout contract 80 43 field v ulong 6 at 0 little 44 field k uchar 6 at 48 little 45 field nk int at 56 little 46 field res_sym int at 60 little 47 field known uchar at 64 little 48 field result ulong at 72 little 49 end 50 51 note Offsets refer to the OscNode representation asserted at its C boundary. 52 const ast_stride ulong 104 53 const arg_scalar uint 1 54 const arg_slice uint 2 55 const arg_owner uint 3 56 const arg_borrow uint 4 57 const bad_scalar_borrow uint 5 58 const bad_slice_borrow uint 6 59 const bad_owner_form uint 7 60 const bad_borrow_form uint 8 61 const node_borrow ulong 20 62 const type_ref ulong 10 63 const type_bytes ulong 11 64 const type_cells ulong 12 65 const ref_own ulong 1 66 const ref_mut ulong 3 67 const node_int ulong 15 68 const node_bool ulong 16 69 const node_name ulong 17 70 const node_bin ulong 21 71 const node_un ulong 22 72 const node_cast ulong 23 73 const node_type ulong 56 74 const node_flag ulong 73 75 const node_known ulong 75 76 const node_value ulong 80 77 const node_symbol ulong 88 78 79 note The checker calls back into its general expression checker for each 80 note operand. The frame preserves the exact evaluation order and type context. 81 note bridge c-type infer OscInference 82 note bridge c-define OSC_INFER_DONE 0 83 note bridge c-define OSC_INFER_CHECK 1 84 note bridge c-define OSC_INFER_CAST_LITERAL 2 85 note bridge c-define OSC_INFER_COMPARE_LITERALS 3 86 note bridge c-define OSC_INFER_COMPARE_BOOL 4 87 note bridge c-define OSC_INFER_ARITH_BOOL 5 88 note bridge c-define OSC_INFER_UNARY_TYPE 6 89 note bridge c-define OSC_INFER_CAST_TYPE 7 90 note bridge c-define OSC_INFER_SHIFT_TYPE 8 91 note bridge c-define OSC_INFER_AMOUNT_TYPE 9 92 note bridge c-define OSC_INFER_SHIFT_RANGE 10 93 note bridge c-define OSC_INFER_ARITH_TYPE 11 94 note bridge c-define OSC_INFER_ZERO_DIVISOR 12 95 96 layout infer 72 97 field root ulong at 0 little 98 field want ulong at 8 little 99 field phase ulong at 16 little 100 field first ulong at 24 little 101 field second ulong at 32 little 102 field literal2 ulong at 40 little 103 field optype ulong at 48 little 104 field site ulong at 56 little 105 field context ulong at 64 little 106 end 107 108 fn alu_signed t ulong to bool 109 ret and ge t T_I8 le t T_I64 110 end 111 112 export fn osc_alu_width t ulong to ulong 113 if eq t T_BOOL 114 ret 1 115 end 116 if and ge t T_U8 le t T_U64 117 ret shl ulong 8 sub t T_U8 118 end 119 if alu_signed t 120 ret shl ulong 8 sub t T_I8 121 end 122 ret 0 123 end 124 125 fn alu_mask width ulong to ulong 126 if eq width 64 127 ret 0xffffffffffffffff 128 end 129 ret sub shl ulong 1 width 1 130 end 131 132 export fn osc_alu_canon t ulong v ulong to ulong 133 w = osc_alu_width t 134 v = and v alu_mask w 135 if alu_signed t 136 sign = shl ulong 1 sub w 1 137 v = sub xor v sign sign 138 end 139 ret v 140 end 141 142 fn alu_mag v ulong to ulong 143 if ge v SIGN64 144 ret sub ulong 0 v 145 end 146 ret v 147 end 148 149 fn alu_limit width ulong negative bool to ulong 150 top = shl ulong 1 sub width 1 151 if negative 152 ret top 153 end 154 ret sub top 1 155 end 156 157 fn alu_addsub op ulong t ulong a ulong b ulong value edit ulong 1 to ulong 158 w = osc_alu_width t 159 signedop = alu_signed t 160 r = add a b 161 if eq op 2 162 r = sub a b 163 end 164 if signedop 165 sign = shl ulong 1 sub w 1 166 changed = and xor r a sign 167 diff = and xor a b sign 168 if ne changed 0 169 if eq eq op 1 eq diff 0 170 ret OVERFLOW 171 end 172 end 173 end 174 put value 0 osc_alu_canon t r 175 ret 0 176 end 177 178 fn alu_mul t ulong a ulong b ulong value edit ulong 1 to ulong 179 w = osc_alu_width t 180 signedop = alu_signed t 181 if signedop 182 limit = alu_limit w ge xor a b SIGN64 183 bm = alu_mag b 184 if ne bm 0 185 if gt alu_mag a div limit bm 186 ret OVERFLOW 187 end 188 end 189 end 190 put value 0 osc_alu_canon t mul a b 191 ret 0 192 end 193 194 fn alu_divrem op ulong t ulong a ulong b ulong value edit ulong 1 to ulong 195 w = osc_alu_width t 196 signedop = alu_signed t 197 if eq b 0 198 ret DIV_ZERO 199 end 200 am = a 201 bm = b 202 if signedop 203 am = alu_mag a 204 bm = alu_mag b 205 end 206 if eq op 4 207 r = div am bm 208 if signedop 209 negative = ge xor a b SIGN64 210 if gt r alu_limit w negative 211 ret OVERFLOW 212 end 213 if negative 214 r = sub ulong 0 r 215 end 216 end 217 put value 0 r 218 else 219 r = rem am bm 220 if and signedop ge a SIGN64 221 r = sub ulong 0 r 222 end 223 put value 0 r 224 end 225 ret 0 226 end 227 228 fn alu_shift op ulong t ulong a ulong b ulong value edit ulong 1 to ulong 229 w = osc_alu_width t 230 signedop = alu_signed t 231 note A negative canonical count is unsigned-greater than every valid width. 232 if ge b w 233 ret BAD_SHIFT 234 end 235 if eq op 9 236 if signedop 237 limit = alu_limit w ge a SIGN64 238 if gt alu_mag a shr limit b 239 ret OVERFLOW 240 end 241 end 242 put value 0 osc_alu_canon t shl a b 243 else 244 r = shr a b 245 if and signedop ge a SIGN64 246 r = not shr not a b 247 end 248 put value 0 r 249 end 250 ret 0 251 end 252 253 export fn osc_alu_bin op ulong t ulong a ulong b ulong value out ulong 1 to ulong 254 put value 0 0 255 if eq osc_alu_width t 0 256 ret BAD_OPCODE 257 end 258 if or eq op 1 eq op 2 259 ret alu_addsub op t a b value 260 end 261 if eq op 3 262 ret alu_mul t a b value 263 end 264 if or eq op 4 eq op 5 265 ret alu_divrem op t a b value 266 end 267 if eq op 6 268 put value 0 and a b 269 ret 0 270 end 271 if eq op 7 272 put value 0 or a b 273 ret 0 274 end 275 if eq op 8 276 put value 0 xor a b 277 ret 0 278 end 279 if or eq op 9 eq op 10 280 ret alu_shift op t a b value 281 end 282 ret BAD_OPCODE 283 end 284 285 export fn osc_alu_un op ulong t ulong a ulong value out ulong 1 to ulong 286 put value 0 0 287 if eq op 1 288 if alu_signed t 289 if ge a SIGN64 290 if gt alu_mag a alu_limit osc_alu_width t false 291 ret OVERFLOW 292 end 293 end 294 end 295 put value 0 osc_alu_canon t sub ulong 0 a 296 ret 0 297 end 298 if eq op 2 299 put value 0 osc_alu_canon t not a 300 ret 0 301 end 302 if eq op 3 303 put value 0 xor a 1 304 ret 0 305 end 306 ret BAD_OPCODE 307 end 308 309 fn alu_truth flag bool to ulong 310 if flag 311 ret 1 312 end 313 ret 0 314 end 315 316 export fn osc_alu_cmp cc ulong t ulong a ulong b ulong to ulong 317 if alu_signed t 318 a = xor a SIGN64 319 b = xor b SIGN64 320 end 321 if eq cc 1 322 ret alu_truth eq a b 323 end 324 if eq cc 2 325 ret alu_truth ne a b 326 end 327 if eq cc 3 328 ret alu_truth lt a b 329 end 330 if eq cc 4 331 ret alu_truth le a b 332 end 333 if eq cc 5 334 ret alu_truth gt a b 335 end 336 if eq cc 6 337 ret alu_truth ge a b 338 end 339 ret 0 340 end 341 342 export fn osc_alu_cast target ulong src ulong a ulong value out ulong 1 to ulong 343 put value 0 0 344 negative = and alu_signed src ge a SIGN64 345 width = osc_alu_width target 346 if alu_signed target 347 magnitude = a 348 if negative 349 magnitude = alu_mag a 350 end 351 if gt magnitude alu_limit width negative 352 ret CAST_RANGE 353 end 354 else 355 if or negative gt a alu_mask width 356 ret CAST_RANGE 357 end 358 end 359 put value 0 a 360 ret 0 361 end 362 363 note OSC surface token numbers from osc_lex.h and scalar sub-opcodes from osc_ir.h. 364 const TOK_PLUS ulong 42 365 const TOK_XOR ulong 49 366 const TOK_SHL ulong 54 367 const TOK_SHR ulong 55 368 const TOK_EQ ulong 56 369 const TOK_GE ulong 61 370 371 export fn osc_binary_opcode tok ulong to ulong 372 if and ge tok TOK_PLUS le tok TOK_XOR 373 ret add sub tok TOK_PLUS 1 374 end 375 if eq tok TOK_SHL 376 ret 9 377 end 378 if eq tok TOK_SHR 379 ret 10 380 end 381 ret 0 382 end 383 384 export fn osc_compare_opcode tok ulong to ulong 385 if and ge tok TOK_EQ le tok TOK_GE 386 ret add sub tok TOK_EQ 1 387 end 388 ret 0 389 end 390 391 note The minus of a literal may denote the signed minimum directly, even 392 note though its positive magnitude cannot stand alone as that signed type. 393 export fn osc_literal_fits t ulong magnitude ulong negative ulong to ulong 394 w = osc_alu_width t 395 if alu_signed t 396 ret alu_truth le magnitude alu_limit w ne negative 0 397 end 398 if ne negative 0 399 ret alu_truth eq magnitude 0 400 end 401 ret alu_truth le magnitude alu_mask w 402 end 403 404 fn ast_read nodes view uchar * atbyte ulong bytes ulong to ulong 405 v = ulong 0 406 for i ulong bytes 407 v = or v shl ulong get nodes add atbyte i mul i 8 408 end 409 ret v 410 end 411 412 fn contract_leaf nodes view uchar * base ulong env view contract value edit ulong 1 to ulong 413 if ne get nodes add base node_known 0 414 put value 0 ast_read nodes add base node_value 8 415 ret 1 416 end 417 kind = ast_read nodes base 2 418 if eq kind node_bool 419 put value 0 alu_truth ne ast_read nodes add base 32 8 0 420 ret 1 421 end 422 if eq kind node_name 423 symbol = long int uint ast_read nodes add base node_symbol 4 424 if lt symbol 0 425 ret 0 426 end 427 if lt symbol long get env nk 428 if ne get env k ulong symbol 0 429 put value 0 get env v ulong symbol 430 ret 1 431 end 432 end 433 if and eq symbol long get env res_sym ne get env known 0 434 put value 0 get env result 435 ret 1 436 end 437 ret 0 438 end 439 if or eq kind node_un or eq kind node_cast eq kind node_bin 440 ret 2 441 end 442 ret 0 443 end 444 445 fn contract_unary nodes view uchar * base ulong value edit ulong 1 to ulong 446 typ = ast_read nodes add base node_type 4 447 if eq ast_read nodes base 2 node_cast 448 child = ast_read nodes add base 12 4 449 src = ast_read nodes add mul child ast_stride node_type 4 450 ret alu_truth eq osc_alu_cast typ src get value 0 value 0 451 end 452 tok = ast_read nodes add base 2 2 453 unop = ulong 1 454 if eq tok 51 455 unop = 3 456 else 457 if eq tok 50 458 unop = 2 459 end 460 end 461 ret alu_truth eq osc_alu_un unop typ get value 0 value 0 462 end 463 464 fn contract_binary nodes view uchar * base ulong left ulong value edit ulong 1 to ulong 465 tok = ast_read nodes add base 2 2 466 if or eq tok 52 eq tok 53 467 ret 1 468 end 469 cc = osc_compare_opcode tok 470 if ne cc 0 471 put value 0 osc_alu_cmp cc ulong get nodes add base node_flag left get value 0 472 ret 1 473 end 474 typ = ast_read nodes add base node_type 4 475 ret alu_truth eq osc_alu_bin osc_binary_opcode tok typ left get value 0 value 0 476 end 477 478 fn short_circuit nodes view uchar * base ulong value ulong to bool 479 op = ast_read nodes add base 2 2 480 ret or and eq op 52 eq value 0 and eq op 53 ne value 0 481 end 482 483 note Each work entry holds the node and its continuation. The second half of 484 note the stack holds left operands while right subtrees are being evaluated. 485 export fn osc_contract_value nodes view uchar * env view contract stack edit ulong * request edit ulong 2 to ulong 486 value = new ulong 1 0 487 depth = ulong 1 488 put stack 0 mul get request 0 4 489 while ne depth 0 bound 65537 490 top = sub depth 1 491 base = mul shr get stack top 2 ast_stride 492 phase = and get stack top 3 493 if eq phase 0 494 choice = contract_leaf nodes base env value 495 if eq choice 0 496 ret 0 497 end 498 if eq choice 2 499 put stack top add get stack top 1 500 put stack depth mul ast_read nodes add base 12 4 4 501 depth = add depth 1 502 continue 503 end 504 else 505 if ne ast_read nodes base 2 node_bin 506 if eq contract_unary nodes base value 0 507 ret 0 508 end 509 else 510 if eq phase 1 511 if short_circuit nodes base get value 0 512 put value 0 alu_truth ne get value 0 0 513 else 514 put stack add top div len stack 2 get value 0 515 put stack top add get stack top 1 516 put stack depth mul ast_read nodes add base 16 4 4 517 depth = add depth 1 518 continue 519 end 520 else 521 if eq contract_binary nodes base get stack add top div len stack 2 value 0 522 ret 0 523 end 524 end 525 end 526 end 527 depth = top 528 end 529 put request 1 get value 0 530 ret 1 531 end 532 533 note Both classifications accept integer literals and arithmetic subtrees. 534 note Shift width comes from its left child; folding also needs a literal RHS. 535 fn needs_rhs op ulong literals ulong to bool 536 ret or ne literals 0 not or eq op TOK_SHL eq op TOK_SHR 537 end 538 539 export fn osc_constant_tree nodes view uchar * id ulong require_literals ulong pending edit ulong * to ulong 540 count = ulong 1 541 put pending 0 id 542 while ne count 0 bound 16384 543 count = sub count 1 544 base = mul get pending count ast_stride 545 kind = ast_read nodes base 2 546 if eq kind node_int 547 continue 548 end 549 op = ast_read nodes add base 2 2 550 if eq kind node_un 551 if eq op 51 552 ret 0 553 end 554 else 555 if or ne kind node_bin eq osc_binary_opcode op 0 556 ret 0 557 end 558 if needs_rhs op require_literals 559 put pending count ast_read nodes add base 16 4 560 count = add count 1 561 end 562 end 563 put pending count ast_read nodes add base 12 4 564 count = add count 1 565 end 566 ret 1 567 end 568 569 note Folding annotates the node only after its operands succeed. Negative 570 note signed minimum is allowed as a single literal, though its positive 571 note magnitude cannot stand alone in that type. 572 fn ast_write nodes edit uchar * pos ulong bytes ulong v ulong to void 573 for j ulong bytes 574 put nodes add pos j uchar and shr v mul j 8 255 575 end 576 end 577 578 fn mark_folded nodes edit uchar * base ulong typ ulong v ulong to void 579 ast_write nodes add base node_type 4 typ 580 put nodes add base node_known 1 581 ast_write nodes add base node_value 8 v 582 end 583 584 fn fold_leaf nodes edit uchar * base ulong typ ulong report edit ulong 3 to ulong 585 magnitude = ast_read nodes add base 32 8 586 if eq osc_literal_fits typ magnitude 0 0 587 put report 2 magnitude 588 ret 2 589 end 590 mark_folded nodes base typ magnitude 591 ret 0 592 end 593 594 fn fold_neg_literal nodes edit uchar * base ulong typ ulong report edit ulong 3 to ulong 595 child = mul ast_read nodes add base 12 4 ast_stride 596 magnitude = ast_read nodes add child 32 8 597 put report 2 magnitude 598 if and not alu_signed typ ne magnitude 0 599 ret 3 600 end 601 if eq osc_literal_fits typ magnitude 1 0 602 ret 4 603 end 604 mark_folded nodes child typ magnitude 605 mark_folded nodes base typ sub ulong 0 magnitude 606 ret 0 607 end 608 609 fn fold_unary nodes edit uchar * base ulong typ ulong to ulong 610 input = ast_read nodes add mul ast_read nodes add base 12 4 ast_stride node_value 8 611 op = ulong 2 612 if eq ast_read nodes add base 2 2 43 613 op = 1 614 if and not alu_signed typ ne input 0 615 ret 5 616 end 617 end 618 value = new ulong 1 0 619 if ne osc_alu_un op typ input value 0 620 ret 6 621 end 622 mark_folded nodes base typ get value 0 623 ret 0 624 end 625 626 fn fold_binary nodes edit uchar * base ulong typ ulong report edit ulong 3 to ulong 627 left = ast_read nodes add mul ast_read nodes add base 12 4 ast_stride node_value 8 628 right_id = ast_read nodes add base 16 4 629 right = ast_read nodes add mul right_id ast_stride node_value 8 630 tok = ast_read nodes add base 2 2 631 value = new ulong 1 0 632 status = osc_alu_bin osc_binary_opcode tok typ left right value 633 if eq status 0 634 mark_folded nodes base typ get value 0 635 ret 0 636 end 637 if or eq status DIV_ZERO eq status BAD_SHIFT 638 put report 1 right_id 639 put report 2 right 640 if eq status DIV_ZERO 641 ret 7 642 end 643 ret 8 644 end 645 if eq status OVERFLOW 646 if eq tok TOK_SHL 647 ret 9 648 end 649 ret 10 650 end 651 ret 11 652 end 653 654 fn fold_enter nodes edit uchar * base ulong typ ulong report edit ulong 3 to ulong 655 kind = ast_read nodes base 2 656 if eq kind node_int 657 ret fold_leaf nodes base typ report 658 end 659 if not or eq kind node_un eq kind node_bin 660 ret 12 661 end 662 if and eq kind node_un eq ast_read nodes add base 2 2 43 663 child = ast_read nodes add base 12 4 664 if eq ast_read nodes mul child ast_stride 2 node_int 665 ret fold_neg_literal nodes base typ report 666 end 667 end 668 ret 13 669 end 670 671 export fn osc_fold_tree nodes edit uchar * typ ulong stack edit ulong * report edit ulong 3 to ulong 672 id = get report 1 673 if eq typ T_BOOL 674 ret 1 675 end 676 depth = ulong 1 677 put stack 0 mul id 4 678 while ne depth 0 bound 65537 679 top = sub depth 1 680 base = mul shr get stack top 2 ast_stride 681 put report 1 shr get stack top 2 682 phase = and get stack top 3 683 status = ulong 0 684 if eq phase 0 685 status = fold_enter nodes base typ report 686 if eq status 13 687 put stack top add get stack top 1 688 put stack depth mul ast_read nodes add base 12 4 4 689 depth = add depth 1 690 continue 691 end 692 else 693 if eq ast_read nodes base 2 node_un 694 status = fold_unary nodes base typ 695 else 696 if eq phase 1 697 put stack top add get stack top 1 698 put stack depth mul ast_read nodes add base 16 4 4 699 depth = add depth 1 700 continue 701 end 702 status = fold_binary nodes base typ report 703 end 704 end 705 if ne status 0 706 ret status 707 end 708 depth = top 709 end 710 put report 0 ast_read nodes add mul id ast_stride node_value 8 711 ret 0 712 end 713 714 fn infer_integer t ulong to bool 715 ret and ge t T_U8 le t T_I64 716 end 717 718 fn infer_begin nodes view uchar * stack edit ulong * frame edit infer to ulong 719 base = mul get frame root ast_stride 720 op = ast_read nodes add base 2 2 721 kind = ast_read nodes base 2 722 a = ast_read nodes add base 12 4 723 b = ast_read nodes add base 16 4 724 put frame first a 725 put frame second b 726 put frame context 0 727 if or eq op 51 or eq op 52 eq op 53 728 put frame context T_BOOL 729 else 730 left_literal = osc_constant_tree nodes a 0 stack 731 if eq kind node_cast 732 if ne left_literal 0 733 put frame site a 734 ret 2 735 end 736 else 737 if eq kind node_un 738 if ne left_literal 0 739 put frame context get frame want 740 end 741 else 742 right_literal = osc_constant_tree nodes b 0 stack 743 if or eq op TOK_SHL eq op TOK_SHR 744 if ne left_literal 0 745 put frame context get frame want 746 end 747 else 748 if ne left_literal 0 749 if ne right_literal 0 750 if ne osc_compare_opcode op 0 751 put frame site get frame root 752 ret 3 753 end 754 put frame context get frame want 755 else 756 put frame first b 757 put frame second a 758 right_literal = 1 759 end 760 end 761 end 762 put frame literal2 right_literal 763 end 764 end 765 end 766 put frame site get frame first 767 put frame phase 1 768 ret 1 769 end 770 771 fn infer_after_first nodes view uchar * frame edit infer t ulong to ulong 772 base = mul get frame root ast_stride 773 kind = ast_read nodes base 2 774 op = ast_read nodes add base 2 2 775 put frame optype t 776 put frame site get frame root 777 if eq kind node_un 778 if and ne op 51 not infer_integer t 779 ret 6 780 end 781 put frame context t 782 ret 0 783 end 784 if eq kind node_cast 785 target = ast_read nodes add base node_type 4 786 if not and infer_integer t infer_integer target 787 ret 7 788 end 789 put frame context target 790 ret 0 791 end 792 context = t 793 if or eq op TOK_SHL eq op TOK_SHR 794 if not infer_integer t 795 ret 8 796 end 797 if eq get frame literal2 0 798 context = 0 799 end 800 else 801 if ne osc_compare_opcode op 0 802 if and eq t T_BOOL ne get frame literal2 0 803 put frame site get frame second 804 ret 4 805 end 806 else 807 if and eq t T_BOOL not or eq op 52 eq op 53 808 put frame site get frame first 809 ret 5 810 end 811 end 812 end 813 put frame site get frame second 814 put frame context context 815 put frame phase 2 816 ret 1 817 end 818 819 fn infer_after_second nodes edit uchar * frame edit infer second_type ulong to ulong 820 base = mul get frame root ast_stride 821 op = ast_read nodes add base 2 2 822 t = get frame optype 823 put frame context t 824 if or eq op 52 eq op 53 825 ret 0 826 end 827 if ne osc_compare_opcode op 0 828 put nodes add base node_flag uchar t 829 put frame context T_BOOL 830 ret 0 831 end 832 right = ast_read nodes add base 16 4 833 rb = mul right ast_stride 834 if or eq op TOK_SHL eq op TOK_SHR 835 if not infer_integer second_type 836 put frame optype second_type 837 ret 9 838 end 839 if and ne get nodes add rb node_known 0 ge ast_read nodes add rb node_value 8 osc_alu_width t 840 ret 10 841 end 842 else 843 if not infer_integer t 844 put frame site get frame root 845 ret 11 846 end 847 if or eq op 45 eq op 46 848 if and ne get nodes add rb node_known 0 eq ast_read nodes add rb node_value 8 0 849 put frame site right 850 ret 12 851 end 852 end 853 end 854 ret 0 855 end 856 857 export fn osc_infer_step nodes edit uchar * stack edit ulong * frame edit infer checked ulong to ulong 858 phase = get frame phase 859 if eq phase 0 860 ret infer_begin nodes stack frame 861 end 862 if eq phase 1 863 ret infer_after_first nodes frame checked 864 end 865 ret infer_after_second nodes frame checked 866 end 867 868 fn arg_action nodes view uchar * param ulong arg ulong to uint 869 typ = ast_read nodes add param 56 4 870 kind = ast_read nodes arg 2 871 if or eq typ type_bytes eq typ type_cells 872 if eq kind node_borrow 873 ret bad_slice_borrow 874 end 875 ret arg_slice 876 end 877 if ne typ type_ref 878 if eq kind node_borrow 879 ret bad_scalar_borrow 880 end 881 ret arg_scalar 882 end 883 refkind = ast_read nodes add param 60 4 884 if eq refkind ref_own 885 if ne kind node_name 886 ret bad_owner_form 887 end 888 ret arg_owner 889 end 890 mutable = ne get nodes add arg 72 0 891 expected_mutable = eq refkind ref_mut 892 if or ne kind node_borrow ne mutable expected_mutable 893 ret bad_borrow_form 894 end 895 ret arg_borrow 896 end 897 898 fn call_next nodes view uchar * id long to long 899 raw = ast_read nodes add mul ulong id ast_stride 24 4 900 if eq raw `4294967295` 901 ret -1 902 end 903 ret long raw 904 end 905 906 export fn osc_call_args nodes view uchar * param long arg long call edit callargs to void 907 np = ulong 0 908 while and ge param 0 lt np 6 bound 6 909 put call params np int param 910 param = call_next nodes param 911 np = add np 1 912 end 913 na = ulong 0 914 while and ge arg 0 lt na 7 bound 7 915 put call args na int arg 916 arg = call_next nodes arg 917 na = add na 1 918 end 919 put call np uint np 920 put call na uint na 921 if eq np na 922 for k ulong na 923 p = mul ulong get call params k ast_stride 924 a = mul ulong get call args k ast_stride 925 put call actions k arg_action nodes p a 926 put call cells k int -1 927 end 928 end 929 end 930 931 export fn osc_bind_cell_argument call edit callargs index ulong symbol long to ulong 932 requires lt index 6 933 for k ulong index 934 if eq long get call cells k symbol 935 ret 0 936 end 937 end 938 put call cells index int symbol 939 ret 1 940 end 941 942 note Only scalar parameters bind constants. Unknown actual arguments keep 943 note their slots unknown, including names in the caller's scope. 944 export fn osc_call_requires nodes view uchar * call view callargs bindings edit contract stack edit ulong * to ulong 945 requires le get call na 6 946 request = new ulong 2 0 947 count = ulong get call na 948 note nk stays zero while actuals are evaluated in the caller. 949 for i ulong count 950 if eq get call actions i arg_scalar 951 put request 0 ulong get call args i 952 if ne osc_contract_value nodes bindings stack request 0 953 put bindings k i uchar 1 954 put bindings v i get request 1 955 end 956 end 957 end 958 put bindings nk int count 959 put request 0 ulong get call clause 960 if eq osc_contract_value nodes bindings stack request 0 961 ret 1 962 end 963 ret alu_truth ne get request 1 0 964 end 965 966 note Native and interpreted calls use the same byte range rule. A pair of 967 note read-only byte slices may overlap. A nonempty cell slice is exclusive. 968 fn slice_width types view uint * p ulong to ulong 969 base = mul p 4 970 if ne get types add base 1 0 971 ret 0 972 end 973 if ne get types add base 2 0 974 ret 0 975 end 976 note Length and struct id occupy the low 24 bits of the last word. 977 if ne and get types add base 3 0x00ffffff 0 978 ret 0 979 end 980 typ = get types base 981 if eq ulong typ type_bytes 982 ret 1 983 end 984 if eq ulong typ type_cells 985 ret 8 986 end 987 ret 0 988 end 989 990 fn slice_limit base ulong count ulong width ulong to ulong 991 maxaddr = ulong 0xffffffffffffffff 992 if eq base 0 993 ret 0 994 end 995 if eq width 8 996 if or ne and base 7 0 gt count shr maxaddr 3 997 ret 0 998 end 999 end 1000 size = mul count width 1001 if gt size sub maxaddr base 1002 ret 0 1003 end 1004 ret add base size 1005 end 1006 1007 fn slices_overlap args view ulong * p ulong q ulong limit ulong width ulong to bool 1008 count = get args add q 1 1009 if eq count 0 1010 ret false 1011 end 1012 begin = get args q 1013 note This earlier range passed the arithmetic checks. 1014 ret and lt begin limit lt get args p add begin mul count width 1015 end 1016 1017 fn check_aliases types view uint * args view ulong * p ulong limit ulong to bool 1018 for q ulong p 1019 width = slice_width types q 1020 if ne width 0 1021 if or eq width 8 eq ulong get types mul p 4 type_cells 1022 if slices_overlap args p q limit width 1023 ret false 1024 end 1025 end 1026 end 1027 end 1028 ret true 1029 end 1030 1031 export fn osc_slice_args types view uint * args view ulong * to long 1032 requires eq len types mul len args 4 1033 if lt len args 2 1034 ret 0 1035 end 1036 for p ulong sub len args 1 1037 width = slice_width types p 1038 if eq width 0 1039 continue 1040 end 1041 count = get args add p 1 1042 if ne count 0 1043 limit = slice_limit get args p count width 1044 if eq limit 0 1045 ret -1 1046 end 1047 if not check_aliases types args p limit 1048 ret -1 1049 end 1050 end 1051 end 1052 ret 0 1053 end 1054 1055 note A return expression and its postcondition use different result bindings. 1056 note Decision 0 leaves the run-time check, 1 elides it, 2 refuses the return. 1057 export fn osc_return_ensures nodes view uchar * selectors view uint 3 bindings edit contract stack edit ulong * to ulong 1058 returned = ulong get selectors 0 1059 post = ulong get selectors 1 1060 count = div len nodes ast_stride 1061 if or ge returned count ge post count 1062 ret 0 1063 end 1064 put bindings nk 0 1065 put bindings res_sym -1 1066 put bindings known 0 1067 request = new ulong 2 0 1068 put request 0 returned 1069 if eq osc_contract_value nodes bindings stack request 0 1070 ret 0 1071 end 1072 put bindings res_sym int get selectors 2 1073 put bindings known 1 1074 put bindings result get request 1 1075 put request 0 post 1076 if eq osc_contract_value nodes bindings stack request 0 1077 ret 0 1078 end 1079 if eq get request 1 0 1080 ret 2 1081 end 1082 ret 1 1083 end