interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / compiler / osc_scalar.om

1083 lines · 26518 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Scalar semantics of validated OSC IR. Checked operations return the OSC
3note trap code as data. The host delivers that trap through the program runtime.
4om2
5
6note IDs from osc_ir.h. Operands enter in their canonical 64-bit representation.
7const T_BOOL ulong 1
8const T_U8 ulong 2
9const T_U64 ulong 5
10const T_I8 ulong 6
11const T_I64 ulong 9
12const SIGN64 ulong 0x8000000000000000
13const OVERFLOW ulong 1
14const DIV_ZERO ulong 2
15const CAST_RANGE ulong 5
16const BAD_SHIFT ulong 7
17const BAD_OPCODE ulong 8
18
19note Contract bindings contain known scalar arguments and the optional result.
20note bridge c-type contract OscContractBindings
21note Function calls preserve left-to-right checking. This frame pairs parsed
22note arguments with parameters and records cell slices already bound at the call.
23note bridge c-type callargs OscCallArgs
24note bridge c-define OSC_ARG_SCALAR 1
25note bridge c-define OSC_ARG_SLICE 2
26note bridge c-define OSC_ARG_OWNER 3
27note bridge c-define OSC_ARG_BORROW 4
28note bridge c-define OSC_ARG_SCALAR_BORROW 5
29note bridge c-define OSC_ARG_SLICE_BORROW 6
30note bridge c-define OSC_ARG_OWNER_FORM 7
31note bridge c-define OSC_ARG_BORROW_FORM 8
32layout callargs 112
33field params int 6 at 0 little
34field args int 7 at 24 little
35field actions uint 6 at 52 little
36field cells int 6 at 76 little
37field np uint at 100 little
38field na uint at 104 little
39field clause uint at 108 little
40end
41
42layout contract 80
43field v ulong 6 at 0 little
44field k uchar 6 at 48 little
45field nk int at 56 little
46field res_sym int at 60 little
47field known uchar at 64 little
48field result ulong at 72 little
49end
50
51note Offsets refer to the OscNode representation asserted at its C boundary.
52const ast_stride ulong 104
53const arg_scalar uint 1
54const arg_slice uint 2
55const arg_owner uint 3
56const arg_borrow uint 4
57const bad_scalar_borrow uint 5
58const bad_slice_borrow uint 6
59const bad_owner_form uint 7
60const bad_borrow_form uint 8
61const node_borrow ulong 20
62const type_ref ulong 10
63const type_bytes ulong 11
64const type_cells ulong 12
65const ref_own ulong 1
66const ref_mut ulong 3
67const node_int ulong 15
68const node_bool ulong 16
69const node_name ulong 17
70const node_bin ulong 21
71const node_un ulong 22
72const node_cast ulong 23
73const node_type ulong 56
74const node_flag ulong 73
75const node_known ulong 75
76const node_value ulong 80
77const node_symbol ulong 88
78
79note The checker calls back into its general expression checker for each
80note operand. The frame preserves the exact evaluation order and type context.
81note bridge c-type infer OscInference
82note bridge c-define OSC_INFER_DONE 0
83note bridge c-define OSC_INFER_CHECK 1
84note bridge c-define OSC_INFER_CAST_LITERAL 2
85note bridge c-define OSC_INFER_COMPARE_LITERALS 3
86note bridge c-define OSC_INFER_COMPARE_BOOL 4
87note bridge c-define OSC_INFER_ARITH_BOOL 5
88note bridge c-define OSC_INFER_UNARY_TYPE 6
89note bridge c-define OSC_INFER_CAST_TYPE 7
90note bridge c-define OSC_INFER_SHIFT_TYPE 8
91note bridge c-define OSC_INFER_AMOUNT_TYPE 9
92note bridge c-define OSC_INFER_SHIFT_RANGE 10
93note bridge c-define OSC_INFER_ARITH_TYPE 11
94note bridge c-define OSC_INFER_ZERO_DIVISOR 12
95
96layout infer 72
97field root ulong at 0 little
98field want ulong at 8 little
99field phase ulong at 16 little
100field first ulong at 24 little
101field second ulong at 32 little
102field literal2 ulong at 40 little
103field optype ulong at 48 little
104field site ulong at 56 little
105field context ulong at 64 little
106end
107
108fn alu_signed t ulong to bool
109ret and ge t T_I8 le t T_I64
110end
111
112export fn osc_alu_width t ulong to ulong
113if eq t T_BOOL
114 ret 1
115end
116if and ge t T_U8 le t T_U64
117 ret shl ulong 8 sub t T_U8
118end
119if alu_signed t
120 ret shl ulong 8 sub t T_I8
121end
122ret 0
123end
124
125fn alu_mask width ulong to ulong
126if eq width 64
127 ret 0xffffffffffffffff
128end
129ret sub shl ulong 1 width 1
130end
131
132export fn osc_alu_canon t ulong v ulong to ulong
133w = osc_alu_width t
134v = and v alu_mask w
135if alu_signed t
136 sign = shl ulong 1 sub w 1
137 v = sub xor v sign sign
138end
139ret v
140end
141
142fn alu_mag v ulong to ulong
143if ge v SIGN64
144 ret sub ulong 0 v
145end
146ret v
147end
148
149fn alu_limit width ulong negative bool to ulong
150top = shl ulong 1 sub width 1
151if negative
152 ret top
153end
154ret sub top 1
155end
156
157fn alu_addsub op ulong t ulong a ulong b ulong value edit ulong 1 to ulong
158w = osc_alu_width t
159signedop = alu_signed t
160r = add a b
161if eq op 2
162 r = sub a b
163end
164if 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
173end
174put value 0 osc_alu_canon t r
175ret 0
176end
177
178fn alu_mul t ulong a ulong b ulong value edit ulong 1 to ulong
179w = osc_alu_width t
180signedop = alu_signed t
181if 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
189end
190put value 0 osc_alu_canon t mul a b
191ret 0
192end
193
194fn alu_divrem op ulong t ulong a ulong b ulong value edit ulong 1 to ulong
195w = osc_alu_width t
196signedop = alu_signed t
197if eq b 0
198 ret DIV_ZERO
199end
200am = a
201bm = b
202if signedop
203 am = alu_mag a
204 bm = alu_mag b
205end
206if 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
218else
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
224end
225ret 0
226end
227
228fn alu_shift op ulong t ulong a ulong b ulong value edit ulong 1 to ulong
229w = osc_alu_width t
230signedop = alu_signed t
231note A negative canonical count is unsigned-greater than every valid width.
232if ge b w
233 ret BAD_SHIFT
234end
235if 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
243else
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
249end
250ret 0
251end
252
253export fn osc_alu_bin op ulong t ulong a ulong b ulong value out ulong 1 to ulong
254put value 0 0
255if eq osc_alu_width t 0
256 ret BAD_OPCODE
257end
258if or eq op 1 eq op 2
259 ret alu_addsub op t a b value
260end
261if eq op 3
262 ret alu_mul t a b value
263end
264if or eq op 4 eq op 5
265 ret alu_divrem op t a b value
266end
267if eq op 6
268 put value 0 and a b
269 ret 0
270end
271if eq op 7
272 put value 0 or a b
273 ret 0
274end
275if eq op 8
276 put value 0 xor a b
277 ret 0
278end
279if or eq op 9 eq op 10
280 ret alu_shift op t a b value
281end
282ret BAD_OPCODE
283end
284
285export fn osc_alu_un op ulong t ulong a ulong value out ulong 1 to ulong
286put value 0 0
287if 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
297end
298if eq op 2
299 put value 0 osc_alu_canon t not a
300 ret 0
301end
302if eq op 3
303 put value 0 xor a 1
304 ret 0
305end
306ret BAD_OPCODE
307end
308
309fn alu_truth flag bool to ulong
310if flag
311 ret 1
312end
313ret 0
314end
315
316export fn osc_alu_cmp cc ulong t ulong a ulong b ulong to ulong
317if alu_signed t
318 a = xor a SIGN64
319 b = xor b SIGN64
320end
321if eq cc 1
322 ret alu_truth eq a b
323end
324if eq cc 2
325 ret alu_truth ne a b
326end
327if eq cc 3
328 ret alu_truth lt a b
329end
330if eq cc 4
331 ret alu_truth le a b
332end
333if eq cc 5
334 ret alu_truth gt a b
335end
336if eq cc 6
337 ret alu_truth ge a b
338end
339ret 0
340end
341
342export fn osc_alu_cast target ulong src ulong a ulong value out ulong 1 to ulong
343put value 0 0
344negative = and alu_signed src ge a SIGN64
345width = osc_alu_width target
346if 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
354else
355 if or negative gt a alu_mask width
356 ret CAST_RANGE
357 end
358end
359put value 0 a
360ret 0
361end
362
363note OSC surface token numbers from osc_lex.h and scalar sub-opcodes from osc_ir.h.
364const TOK_PLUS ulong 42
365const TOK_XOR ulong 49
366const TOK_SHL ulong 54
367const TOK_SHR ulong 55
368const TOK_EQ ulong 56
369const TOK_GE ulong 61
370
371export fn osc_binary_opcode tok ulong to ulong
372if and ge tok TOK_PLUS le tok TOK_XOR
373 ret add sub tok TOK_PLUS 1
374end
375if eq tok TOK_SHL
376 ret 9
377end
378if eq tok TOK_SHR
379 ret 10
380end
381ret 0
382end
383
384export fn osc_compare_opcode tok ulong to ulong
385if and ge tok TOK_EQ le tok TOK_GE
386 ret add sub tok TOK_EQ 1
387end
388ret 0
389end
390
391note The minus of a literal may denote the signed minimum directly, even
392note though its positive magnitude cannot stand alone as that signed type.
393export fn osc_literal_fits t ulong magnitude ulong negative ulong to ulong
394w = osc_alu_width t
395if alu_signed t
396 ret alu_truth le magnitude alu_limit w ne negative 0
397end
398if ne negative 0
399 ret alu_truth eq magnitude 0
400end
401ret alu_truth le magnitude alu_mask w
402end
403
404fn 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
410end
411
412fn 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
443end
444
445fn 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
462end
463
464fn 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
476end
477
478fn 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
481end
482
483note Each work entry holds the node and its continuation. The second half of
484note the stack holds left operands while right subtrees are being evaluated.
485export 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
531end
532
533note Both classifications accept integer literals and arithmetic subtrees.
534note Shift width comes from its left child; folding also needs a literal RHS.
535fn needs_rhs op ulong literals ulong to bool
536 ret or ne literals 0 not or eq op TOK_SHL eq op TOK_SHR
537end
538
539export 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
567end
568
569note Folding annotates the node only after its operands succeed. Negative
570note signed minimum is allowed as a single literal, though its positive
571note magnitude cannot stand alone in that type.
572fn 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
576end
577
578fn 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
582end
583
584fn 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
592end
593
594fn 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
607end
608
609fn 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
624end
625
626fn 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
652end
653
654fn 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
669end
670
671export 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
712end
713
714fn infer_integer t ulong to bool
715 ret and ge t T_U8 le t T_I64
716end
717
718fn 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
769end
770
771fn 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
817end
818
819fn 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
855end
856
857export 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
866end
867
868fn 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
896end
897
898fn 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
904end
905
906export 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
929end
930
931export fn osc_bind_cell_argument call edit callargs index ulong symbol long to ulong
932requires 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
940end
941
942note Only scalar parameters bind constants. Unknown actual arguments keep
943note their slots unknown, including names in the caller's scope.
944export fn osc_call_requires nodes view uchar * call view callargs bindings edit contract stack edit ulong * to ulong
945requires 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
964end
965
966note Native and interpreted calls use the same byte range rule. A pair of
967note read-only byte slices may overlap. A nonempty cell slice is exclusive.
968fn 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
988end
989
990fn 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
1005end
1006
1007fn 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
1015end
1016
1017fn 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
1029end
1030
1031export fn osc_slice_args types view uint * args view ulong * to long
1032requires 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
1053end
1054
1055note A return expression and its postcondition use different result bindings.
1056note Decision 0 leaves the run-time check, 1 elides it, 2 refuses the return.
1057export 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
1083end