-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathvalid.ml
More file actions
1279 lines (1063 loc) · 42 KB
/
valid.ml
File metadata and controls
1279 lines (1063 loc) · 42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Ast
open Source
open Types
open Match
(* Errors *)
module Invalid = Error.Make ()
exception Invalid = Invalid.Error
let error = Invalid.error
let require b at s = if not b then error at s
(* Context *)
type context =
{
types : def_type list;
funcs : def_type list;
tables : table_type list;
memories : memory_type list;
tags : tag_type list;
globals : global_type list;
elems : ref_type list;
datas : unit list;
locals : local_type list;
results : val_type list;
labels : result_type list;
refs : Free.t;
}
let empty_context =
{ types = []; funcs = []; globals = []; tables = [];
memories = []; tags = []; elems = []; datas = [];
locals = []; results = []; labels = [];
refs = Free.empty
}
let lookup category list x =
try Lib.List32.nth list x.it with Failure _ ->
error x.at ("unknown " ^ category ^ " " ^ I32.to_string_u x.it)
let type_ (c : context) x = lookup "type" c.types x
let func (c : context) x = lookup "function" c.funcs x
let table (c : context) x = lookup "table" c.tables x
let memory (c : context) x = lookup "memory" c.memories x
let global (c : context) x = lookup "global" c.globals x
let tag (c : context) x = lookup "tag" c.tags x
let elem (c : context) x = lookup "elem segment" c.elems x
let data (c : context) x = lookup "data segment" c.datas x
let local (c : context) x = lookup "local" c.locals x
let label (c : context) x = lookup "label" c.labels x
let replace category list x y =
try Lib.List32.replace list x.it y with Failure _ ->
error x.at ("unknown " ^ category ^ " " ^ I32.to_string_u x.it)
let init_local (c : context) x =
let LocalT (_init, t) = local c x in
{c with locals = replace "local" c.locals x (LocalT (Set, t))}
let init_locals (c : context) xs =
List.fold_left init_local c xs
let func_type (c : context) x =
match expand_def_type (type_ c x) with
| DefFuncT ft -> ft
| _ -> error x.at ("non-function type " ^ Int32.to_string x.it)
let cont_type (c : context) x =
match expand_def_type (type_ c x) with
| DefContT ct -> ct
| _ -> error x.at ("non-continuation type " ^ Int32.to_string x.it)
let struct_type (c : context) x =
match expand_def_type (type_ c x) with
| DefStructT st -> st
| _ -> error x.at ("non-structure type " ^ I32.to_string_u x.it)
let array_type (c : context) x =
match expand_def_type (type_ c x) with
| DefArrayT at -> at
| _ -> error x.at ("non-array type " ^ I32.to_string_u x.it)
let refer category (s : Free.Set.t) x =
if not (Free.Set.mem x.it s) then
error x.at
("undeclared " ^ category ^ " reference " ^ I32.to_string_u x.it)
let refer_func (c : context) x = refer "function" c.refs.Free.funcs x
(* Conversions *)
let cont_type_of_heap_type (c : context) (ht : heap_type) at : cont_type =
match ht with
| DefHT dt -> as_cont_str_type (expand_def_type dt)
| VarHT (StatX x) -> cont_type c (x @@ at)
| _ -> assert false
let func_type_of_heap_type (c : context) (ht : heap_type) at : func_type =
match ht with
| DefHT dt -> as_func_str_type (expand_def_type dt)
| VarHT (StatX x) -> func_type c (x @@ at)
| _ -> assert false
let func_type_of_cont_type (c : context) (ContT ht) at : func_type =
func_type_of_heap_type c ht at
let func_type_of_tag_type (c : context) (TagT dt) at : func_type =
match expand_def_type dt with
| DefFuncT ft -> ft
| _ -> error at "non-function type"
(* Types *)
let check_limits {min; max} range at msg =
require (I64.le_u min range) at msg;
match max with
| None -> ()
| Some max ->
require (I64.le_u max range) at msg;
require (I64.le_u min max) at
"size minimum must not be greater than maximum"
let check_num_type (c : context) (t : num_type) at =
()
let check_vec_type (c : context) (t : vec_type) at =
()
let check_heap_type (c : context) (t : heap_type) at =
match t with
| AnyHT | NoneHT | EqHT | I31HT | StructHT | ArrayHT
| FuncHT | NoFuncHT
| ContHT | NoContHT
| ExnHT | NoExnHT
| ExternHT | NoExternHT -> ()
| VarHT (StatX x) -> let _dt = type_ c (x @@ at) in ()
| VarHT (RecX _) | DefHT _ -> assert false
| BotHT -> ()
let check_ref_type (c : context) (t : ref_type) at =
match t with
| (_nul, ht) -> check_heap_type c ht at
let check_val_type (c : context) (t : val_type) at =
match t with
| NumT t' -> check_num_type c t' at
| VecT t' -> check_vec_type c t' at
| RefT t' -> check_ref_type c t' at
| BotT -> assert false
let check_result_type (c : context) (ts : result_type) at =
List.iter (fun t -> check_val_type c t at) ts
let check_storage_type (c : context) (st : storage_type) at =
match st with
| ValStorageT t -> check_val_type c t at
| PackStorageT p -> assert Pack.(p = Pack8 || p = Pack16)
let check_field_type (c : context) (ft : field_type) at =
match ft with
| FieldT (_mut, st) -> check_storage_type c st at
let check_struct_type (c : context) (st : struct_type) at =
match st with
| StructT fts -> List.iter (fun ft -> check_field_type c ft at) fts
let check_array_type (c : context) (rt : array_type) at =
match rt with
| ArrayT ft -> check_field_type c ft at
let check_func_type (c : context) (ft : func_type) at =
let FuncT (ts1, ts2) = ft in
check_result_type c ts1 at;
check_result_type c ts2 at
let check_cont_type (c : context) (ct : cont_type) at =
match ct with
| ContT (VarHT (StatX x)) ->
let _dt = func_type c (x @@ at) in ()
| _ -> assert false
let check_table_type (c : context) (tt : table_type) at =
let TableT (at_, lim, t) = tt in
check_ref_type c t at;
let sz, s =
match at_ with
| I32AT -> 0xffff_ffffL, "2^32-1 for i32"
| I64AT -> 0xffff_ffff_ffff_ffffL, "2^64-1 for i64"
in
check_limits lim sz at ("table size must be at most " ^ s)
let check_memory_type (c : context) (mt : memory_type) at =
let MemoryT (at_, lim) = mt in
let sz, s =
match at_ with
| I32AT -> 0x1_0000L, "2^16 pages (4 GiB) for i32"
| I64AT -> 0x1_0000_0000_0000L, "2^48 pages (256 TiB) for i64"
in
check_limits lim sz at ("memory size must be at most " ^ s)
let check_tag_type (c : context) (et : tag_type) at =
match et with
| TagT dt -> let _ft = as_func_str_type (expand_def_type dt) in ()
let check_global_type (c : context) (gt : global_type) at =
let GlobalT (_mut, t) = gt in
check_val_type c t at
let check_str_type (c : context) (st : str_type) at =
match st with
| DefStructT st -> check_struct_type c st at
| DefArrayT rt -> check_array_type c rt at
| DefFuncT ft -> check_func_type c ft at
| DefContT ct -> check_cont_type c ct at
let check_sub_type (c : context) (sut : sub_type) at =
let SubT (_fin, hts, st) = sut in
List.iter (fun ht -> check_heap_type c ht at) hts;
check_str_type c st at
let check_sub_type_sub (c : context) (sut : sub_type) x at =
let SubT (_fin, hts, st) = sut in
List.iter (fun hti ->
let xi = match hti with VarHT (StatX xi) -> xi | _ -> assert false in
let SubT (fini, _, sti) = unroll_def_type (type_ c (xi @@ at)) in
require (xi < x) at ("forward use of type " ^ I32.to_string_u xi ^
" in sub type definition");
require (fini = NoFinal) at ("sub type " ^ I32.to_string_u x ^
" has final super type " ^ I32.to_string_u xi);
require (match_str_type c.types st sti) at ("sub type " ^ I32.to_string_u x ^
" does not match super type " ^ I32.to_string_u xi)
) hts
let check_rec_type (c : context) (rt : rec_type) at : context =
let RecT sts = rt in
let x = Lib.List32.length c.types in
let c' = {c with types = c.types @ roll_def_types x rt} in
List.iter (fun st -> check_sub_type c' st at) sts;
Lib.List32.iteri
(fun i st -> check_sub_type_sub c' st (Int32.add x i) at) sts;
c'
let check_type (c : context) (t : type_) : context =
check_rec_type c t.it t.at
let diff_ref_type (nul1, ht1) (nul2, ht2) =
match nul2 with
| Null -> (NoNull, ht1)
| NoNull -> (nul1, ht1)
(* Stack typing *)
(*
* Note: The declarative typing rules are non-deterministic, that is, they
* have the liberty to locally "guess" the right types implied by the context.
* In the algorithmic formulation required here, stack types may hence pick
* `BotT` as the principal choice for a locally unknown type.
* Furthermore, an ellipses flag represents arbitrary sequences
* of unknown types, in order to handle stack polymorphism algorithmically.
*)
type ellipses = NoEllipses | Ellipses
type infer_result_type = ellipses * val_type list
type infer_func_type = {ins : infer_result_type; outs : infer_result_type}
type infer_instr_type = infer_func_type * idx list
let stack ts = (NoEllipses, ts)
let (-->) ts1 ts2 = {ins = NoEllipses, ts1; outs = NoEllipses, ts2}
let (-->...) ts1 ts2 = {ins = Ellipses, ts1; outs = Ellipses, ts2}
let match_resulttype s1 s2 (c : context) ts1 ts2 at =
require
( List.length ts1 = List.length ts2 &&
List.for_all2 (match_val_type c.types) ts1 ts2 ) at
("type mismatch: " ^ s2 ^ " requires " ^ string_of_result_type ts2 ^
" but " ^ s1 ^ " has " ^ string_of_result_type ts1)
let match_stack (c : context) ts1 ts2 at =
match_resulttype "stack" "instruction" c ts1 ts2 at
let pop c (ell1, ts1) (ell2, ts2) at =
let n1 = List.length ts1 in
let n2 = List.length ts2 in
let n = min n1 n2 in
let n3 = if ell2 = Ellipses then (n1 - n) else 0 in
match_stack c (Lib.List.make n3 (BotT : val_type) @ Lib.List.drop (n2 - n) ts2) ts1 at;
(ell2, if ell1 = Ellipses then [] else Lib.List.take (n2 - n) ts2)
let push c (ell1, ts1) (ell2, ts2) =
assert (ell1 = NoEllipses || ts2 = []);
(if ell1 = Ellipses || ell2 = Ellipses then Ellipses else NoEllipses),
ts2 @ ts1
let peek i (ell, ts) : val_type =
try List.nth (List.rev ts) i with Failure _ -> BotT
let peek_ref i (ell, ts) at : ref_type =
match peek i (ell, ts) with
| RefT rt -> rt
| BotT -> (NoNull, BotHT)
| t ->
error at
("type mismatch: instruction requires reference type" ^
" but stack has " ^ string_of_val_type t)
(* Type Synthesis *)
let type_num = Value.type_of_op
let type_vec = Value.type_of_vecop
let type_vec_lane = function
| Value.V128 laneop -> V128.type_of_lane laneop
let type_cvtop at = function
| Value.I32 cvtop ->
let open I32Op in
(match cvtop with
| ExtendSI32 | ExtendUI32 -> error at "invalid conversion"
| WrapI64 -> I64T
| TruncSF32 | TruncUF32 | TruncSatSF32 | TruncSatUF32
| ReinterpretFloat -> F32T
| TruncSF64 | TruncUF64 | TruncSatSF64 | TruncSatUF64 -> F64T
), I32T
| Value.I64 cvtop ->
let open I64Op in
(match cvtop with
| ExtendSI32 | ExtendUI32 -> I32T
| WrapI64 -> error at "invalid conversion"
| TruncSF32 | TruncUF32 | TruncSatSF32 | TruncSatUF32 -> F32T
| TruncSF64 | TruncUF64 | TruncSatSF64 | TruncSatUF64
| ReinterpretFloat -> F64T
), I64T
| Value.F32 cvtop ->
let open F32Op in
(match cvtop with
| ConvertSI32 | ConvertUI32 | ReinterpretInt -> I32T
| ConvertSI64 | ConvertUI64 -> I64T
| PromoteF32 -> error at "invalid conversion"
| DemoteF64 -> F64T
), F32T
| Value.F64 cvtop ->
let open F64Op in
(match cvtop with
| ConvertSI32 | ConvertUI32 -> I32T
| ConvertSI64 | ConvertUI64 | ReinterpretInt -> I64T
| PromoteF32 -> F32T
| DemoteF64 -> error at "invalid conversion"
), F64T
let num_lanes = function
| Value.V128 laneop -> V128.num_lanes laneop
let lane_extractop = function
| Value.V128 extractop ->
let open V128 in let open V128Op in
match extractop with
| I8x16 (Extract (i, _)) | I16x8 (Extract (i, _))
| I32x4 (Extract (i, _)) | I64x2 (Extract (i, _))
| F32x4 (Extract (i, _)) | F64x2 (Extract (i, _)) -> i
let lane_replaceop = function
| Value.V128 replaceop ->
let open V128 in let open V128Op in
match replaceop with
| I8x16 (Replace i) | I16x8 (Replace i)
| I32x4 (Replace i) | I64x2 (Replace i)
| F32x4 (Replace i) | F64x2 (Replace i) -> i
let type_externop op =
match op with
| Internalize -> ExternHT, AnyHT
| Externalize -> AnyHT, ExternHT
(* Expressions *)
let check_pack sz t_sz at =
require (Pack.packed_size sz < t_sz) at "invalid sign extension"
let check_unop unop at =
match unop with
| Value.I32 (IntOp.ExtendS sz) | Value.I64 (IntOp.ExtendS sz) ->
check_pack sz (num_size (Value.type_of_op unop)) at
| _ -> ()
let check_vec_binop binop at =
match binop with
| Value.(V128 (V128.I8x16 (V128Op.Shuffle is))) ->
if List.exists ((<=) 32) is then
error at "invalid lane index"
| _ -> ()
let check_memop (c : context) (memop : ('t, 's) memop) ty_size get_sz at =
let size =
match get_sz memop.pack with
| None -> ty_size memop.ty
| Some sz ->
check_pack sz (ty_size memop.ty) at;
Pack.packed_size sz
in
require (1 lsl memop.align >= 1 && 1 lsl memop.align <= size) at
"alignment must not be larger than natural";
let MemoryT (at_, _lim) = memory c (0l @@ at) in
if at_ = I32AT then
require (I64.lt_u memop.offset 0x1_0000_0000L) at
"offset out of range";
memop.ty
let check_cast (c : context) rt at =
require (not (match_ref_type c.types rt (Null, ContHT))) at
"invalid cast to continuation types"
(*
* Conventions:
* c : context
* e : instr
* es : instr list
* v : value
* t : val_type
* ts : result_type
* x : variable
*
* Note: To deal with the non-determinism in some of the declarative rules,
* the function takes the current stack `s` as an additional argument, allowing
* it to "peek" when it would otherwise have to guess an input type.
*
* Furthermore, stack-polymorphic types are given with the `-->...` operator:
* a type `ts1 -->... ts2` expresses any type `(ts1' @ ts1) -> (ts2' @ ts2)`
* where `ts1'` and `ts2'` would be chosen non-deterministically in the
* declarative typing rules.
*)
let check_resume_table (c : context) ts2 (xys : (idx * hdl) list) at =
List.iter (fun (x1, x2) ->
match x2 with
| OnLabel x2 ->
let FuncT (ts3, ts4) = func_type_of_tag_type c (tag c x1) x1.at in
let ts' = label c x2 in
(match Lib.List.last_opt ts' with
| Some (RefT (nul', ht)) ->
let ct = cont_type_of_heap_type c ht x2.at in
let ft' = func_type_of_cont_type c ct x2.at in
require (match_func_type c.types (FuncT (ts4, ts2)) ft') x2.at
"type mismatch in continuation type";
match_stack c (ts3 @ [RefT (nul', ht)]) ts' x2.at
| _ ->
error at
("type mismatch: instruction requires continuation reference type" ^
" but label has " ^ string_of_result_type ts'))
| OnSwitch ->
let FuncT (ts3, ts4) = func_type_of_tag_type c (tag c x1) x1.at in
require (match_result_type c.types ts3 []) x1.at
"type mismatch tag type"
) xys
let check_block_type (c : context) (bt : block_type) at : instr_type =
match bt with
| ValBlockType None -> InstrT ([], [], [])
| ValBlockType (Some t) -> check_val_type c t at; InstrT ([], [t], [])
| VarBlockType x ->
let FuncT (ts1, ts2) = func_type c x in InstrT (ts1, ts2, [])
let rec check_instr (c : context) (e : instr) (s : infer_result_type) : infer_instr_type =
match e.it with
| Unreachable ->
[] -->... [], []
| Nop ->
[] --> [], []
| Drop ->
[peek 0 s] --> [], []
| Select None ->
let t = peek 1 s in
require (is_num_type t || is_vec_type t) e.at
("type mismatch: instruction requires numeric or vector type" ^
" but stack has " ^ string_of_val_type t);
[t; t; NumT I32T] --> [t], []
| Select (Some ts) ->
require (List.length ts = 1) e.at
"invalid result arity other than 1 is not (yet) allowed";
check_result_type c ts e.at;
(ts @ ts @ [NumT I32T]) --> ts, []
| Block (bt, es) ->
let InstrT (ts1, ts2, xs) as it = check_block_type c bt e.at in
check_block {c with labels = ts2 :: c.labels} es it e.at;
ts1 --> ts2, List.map (fun x -> x @@ e.at) xs
| Loop (bt, es) ->
let InstrT (ts1, ts2, xs) as it = check_block_type c bt e.at in
check_block {c with labels = ts1 :: c.labels} es it e.at;
ts1 --> ts2, List.map (fun x -> x @@ e.at) xs
| If (bt, es1, es2) ->
let InstrT (ts1, ts2, xs) as it = check_block_type c bt e.at in
check_block {c with labels = ts2 :: c.labels} es1 it e.at;
check_block {c with labels = ts2 :: c.labels} es2 it e.at;
(ts1 @ [NumT I32T]) --> ts2, List.map (fun x -> x @@ e.at) xs
| Br x ->
let ts = label c x in
ts -->... [], []
| BrIf x ->
let ts = label c x in
(ts @ [NumT I32T]) --> ts, []
| BrTable (xs, x) ->
let n = List.length (label c x) in
let ts = List.init n (fun i -> peek (n - i) s) in
match_stack c ts (label c x) x.at;
List.iter (fun x' -> match_stack c ts (label c x') x'.at) xs;
(ts @ [NumT I32T]) -->... [], []
| BrOnNull x ->
let (_nul, ht) = peek_ref 0 s e.at in
let ts = label c x in
(ts @ [RefT (Null, ht)]) --> (ts @ [RefT (NoNull, ht)]), []
| BrOnNonNull x ->
let (_nul, ht) = peek_ref 0 s e.at in
let t' = RefT (NoNull, ht) in
let ts = label c x in
require (ts <> []) e.at
("type mismatch: instruction requires type " ^ string_of_val_type t' ^
" but label has " ^ string_of_result_type (label c x));
let ts0, t1 = Lib.List.split_last (label c x) in
require (match_val_type c.types t' t1) e.at
("type mismatch: instruction requires type " ^ string_of_val_type t' ^
" but label has " ^ string_of_result_type ts);
(ts0 @ [RefT (Null, ht)]) --> ts0, []
| BrOnCast (x, rt1, rt2) ->
check_ref_type c rt1 e.at;
check_ref_type c rt2 e.at;
check_cast c rt2 e.at;
require
(match_ref_type c.types rt2 rt1) e.at
("type mismatch on cast: type " ^ string_of_ref_type rt2 ^
" does not match " ^ string_of_ref_type rt1);
require (label c x <> []) e.at
("type mismatch: instruction requires type " ^ string_of_ref_type rt2 ^
" but label has " ^ string_of_result_type (label c x));
let ts0, t1 = Lib.List.split_last (label c x) in
require (match_val_type c.types (RefT rt2) t1) e.at
("type mismatch: instruction requires type " ^ string_of_ref_type rt2 ^
" but label has " ^ string_of_result_type (label c x));
(ts0 @ [RefT rt1]) --> (ts0 @ [RefT (diff_ref_type rt1 rt2)]), []
| BrOnCastFail (x, rt1, rt2) ->
check_ref_type c rt1 e.at;
check_ref_type c rt2 e.at;
check_cast c rt2 e.at;
let rt1' = diff_ref_type rt1 rt2 in
require
(match_ref_type c.types rt2 rt1) e.at
("type mismatch on cast: type " ^ string_of_ref_type rt2 ^
" does not match " ^ string_of_ref_type rt1);
require (label c x <> []) e.at
("type mismatch: instruction requires type " ^ string_of_ref_type rt1' ^
" but label has " ^ string_of_result_type (label c x));
let ts0, t1 = Lib.List.split_last (label c x) in
require (match_val_type c.types (RefT rt1') t1) e.at
("type mismatch: instruction requires type " ^ string_of_ref_type rt1' ^
" but label has " ^ string_of_result_type (label c x));
(ts0 @ [RefT rt1]) --> (ts0 @ [RefT rt2]), []
| Return ->
c.results -->... [], []
| Call x ->
let FuncT (ts1, ts2) = as_func_str_type (expand_def_type (func c x)) in
ts1 --> ts2, []
| CallRef x ->
let FuncT (ts1, ts2) = func_type c x in
(ts1 @ [RefT (Null, DefHT (type_ c x))]) --> ts2, []
| CallIndirect (x, y) ->
let TableT (at, _lim, t) = table c x in
let FuncT (ts1, ts2) = func_type c y in
require (match_ref_type c.types t (Null, FuncHT)) x.at
("type mismatch: instruction requires table of function type" ^
" but table has element type " ^ string_of_ref_type t);
(ts1 @ [NumT (num_type_of_addr_type at)]) --> ts2, []
| ReturnCall x ->
let FuncT (ts1, ts2) = as_func_str_type (expand_def_type (func c x)) in
require (match_result_type c.types ts2 c.results) e.at
("type mismatch: current function requires result type " ^
string_of_result_type c.results ^
" but callee returns " ^ string_of_result_type ts2);
ts1 -->... [], []
| ReturnCallRef x ->
let FuncT (ts1, ts2) = func_type c x in
require (match_result_type c.types ts2 c.results) e.at
("type mismatch: current function requires result type " ^
string_of_result_type c.results ^
" but callee returns " ^ string_of_result_type ts2);
(ts1 @ [RefT (Null, DefHT (type_ c x))]) -->... [], []
| ReturnCallIndirect (x, y) ->
let TableT (at, _lim, t) = table c x in
let FuncT (ts1, ts2) = func_type c y in
require (match_result_type c.types ts2 c.results) e.at
("type mismatch: current function requires result type " ^
string_of_result_type c.results ^
" but callee returns " ^ string_of_result_type ts2);
(ts1 @ [NumT (num_type_of_addr_type at)]) -->... [], []
| ContNew x ->
let ContT ht = cont_type c x in
[RefT (Null, ht)] -->
[RefT (NoNull, VarHT (StatX x.it))], []
| ContBind (x, y) ->
let ct = cont_type c x in
let FuncT (ts1, ts2) = func_type_of_cont_type c ct x.at in
let ct' = cont_type c y in
let FuncT (ts1', _) as ft' = func_type_of_cont_type c ct' y.at in
require (List.length ts1 >= List.length ts1') x.at
"type mismatch in continuation arguments";
let ts11, ts12 = Lib.List.split (List.length ts1 - List.length ts1') ts1 in
require (match_func_type c.types (FuncT (ts12, ts2)) ft') e.at
"type mismatch in continuation types";
(ts11 @ [RefT (Null, VarHT (StatX x.it))]) -->
[RefT (NoNull, VarHT (StatX y.it))], []
| Suspend x ->
let tag = tag c x in
let FuncT (ts1, ts2) = func_type_of_tag_type c tag x.at in
ts1 --> ts2, []
| Resume (x, xys) ->
let ct = cont_type c x in
let FuncT (ts1, ts2) = func_type_of_cont_type c ct x.at in
check_resume_table c ts2 xys e.at;
(ts1 @ [RefT (Null, VarHT (StatX x.it))]) --> ts2, []
| ResumeThrow (x, y, xys) ->
let ct = cont_type c x in
let FuncT (ts1, ts2) = func_type_of_cont_type c ct x.at in
let tag = tag c y in
let FuncT (ts0, _) = func_type_of_tag_type c tag y.at in
check_resume_table c ts2 xys e.at;
(ts0 @ [RefT (Null, VarHT (StatX x.it))]) --> ts2, []
| Switch (x, y) ->
let ct1 = cont_type c x in
let FuncT (ts11, ts12) = func_type_of_cont_type c ct1 x.at in
let FuncT (ts21, ts22) =
match Lib.List.last_opt ts11 with
| Some (RefT (nul', ht)) ->
func_type_of_cont_type c (cont_type_of_heap_type c ht x.at) x.at
| _ ->
error x.at
("type mismatch: instruction requires continuation reference type" ^
" but the type annotation has " ^ string_of_result_type ts11)
in
let et = tag c y in
let FuncT (ts31, t) = func_type_of_tag_type c et y.at in
require (ts31 = []) y.at
"type mismatch in switch tag";
require (match_result_type c.types ts12 t) y.at
"type mismatch in continuation types";
require (match_result_type c.types t ts22) y.at
"type mismatch in continuation types";
let ts11' = Lib.List.lead ts11 in
(ts11' @ [RefT (Null, VarHT (StatX x.it))]) --> ts21, []
| Throw x ->
let FuncT (ts1, ts2) = func_type_of_tag_type c (tag c x) x.at in
ts1 -->... [], []
| ThrowRef ->
[RefT (Null, ExnHT)] -->... [], []
| TryTable (bt, cs, es) ->
let InstrT (ts1, ts2, xs) as it = check_block_type c bt e.at in
let c' = {c with labels = ts2 :: c.labels} in
List.iter (fun ct -> check_catch c ct ts2 e.at) cs;
check_block c' es it e.at;
ts1 --> ts2, List.map (fun x -> x @@ e.at) xs
| LocalGet x ->
let LocalT (init, t) = local c x in
require (init = Set) x.at "uninitialized local";
[] --> [t], []
| LocalSet x ->
let LocalT (_init, t) = local c x in
[t] --> [], [x]
| LocalTee x ->
let LocalT (_init, t) = local c x in
[t] --> [t], [x]
| GlobalGet x ->
let GlobalT (_mut, t) = global c x in
[] --> [t], []
| GlobalSet x ->
let GlobalT (mut, t) = global c x in
require (mut = Var) x.at "immutable global";
[t] --> [], []
| TableGet x ->
let TableT (at, _lim, rt) = table c x in
[NumT (num_type_of_addr_type at)] --> [RefT rt], []
| TableSet x ->
let TableT (at, _lim, rt) = table c x in
[NumT (num_type_of_addr_type at); RefT rt] --> [], []
| TableSize x ->
let TableT (at, _lim, _rt) = table c x in
[] --> [NumT (num_type_of_addr_type at)], []
| TableGrow x ->
let TableT (at, _lim, rt) = table c x in
[RefT rt; NumT (num_type_of_addr_type at)] -->
[NumT (num_type_of_addr_type at)], []
| TableFill x ->
let TableT (at, _lim, rt) = table c x in
[NumT (num_type_of_addr_type at); RefT rt;
NumT (num_type_of_addr_type at)] --> [], []
| TableCopy (x, y) ->
let TableT (at1, _lim1, t1) = table c x in
let TableT (at2, _lim2, t2) = table c y in
require (match_ref_type c.types t2 t1) x.at
("type mismatch: source element type " ^ string_of_ref_type t1 ^
" does not match destination element type " ^ string_of_ref_type t2);
[NumT (num_type_of_addr_type at1); NumT (num_type_of_addr_type at2);
NumT (num_type_of_addr_type (min at1 at2))] --> [], []
| TableInit (x, y) ->
let TableT (at, _lim1, t1) = table c x in
let t2 = elem c y in
require (match_ref_type c.types t2 t1) x.at
("type mismatch: element segment's type " ^ string_of_ref_type t1 ^
" does not match table's element type " ^ string_of_ref_type t2);
[NumT (num_type_of_addr_type at); NumT I32T; NumT I32T] --> [], []
| ElemDrop x ->
ignore (elem c x);
[] --> [], []
| Load (x, memop) ->
let MemoryT (at, _lim) = memory c x in
let t = check_memop c memop num_size (Lib.Option.map fst) e.at in
[NumT (num_type_of_addr_type at)] --> [NumT t], []
| Store (x, memop) ->
let MemoryT (at, _lim) = memory c x in
let t = check_memop c memop num_size (fun sz -> sz) e.at in
[NumT (num_type_of_addr_type at); NumT t] --> [], []
| VecLoad (x, memop) ->
let MemoryT (at, _lim) = memory c x in
let t = check_memop c memop vec_size (Lib.Option.map fst) e.at in
[NumT (num_type_of_addr_type at)] --> [VecT t], []
| VecStore (x, memop) ->
let MemoryT (at, _lim) = memory c x in
let t = check_memop c memop vec_size (fun _ -> None) e.at in
[NumT (num_type_of_addr_type at); VecT t] --> [], []
| VecLoadLane (x, memop, i) ->
let MemoryT (at, _lim) = memory c x in
let t = check_memop c memop vec_size (fun sz -> Some sz) e.at in
require (i < vec_size t / Pack.packed_size memop.pack) e.at
"invalid lane index";
[NumT (num_type_of_addr_type at); VecT t] --> [VecT t], []
| VecStoreLane (x, memop, i) ->
let MemoryT (at, _lim) = memory c x in
let t = check_memop c memop vec_size (fun sz -> Some sz) e.at in
require (i < vec_size t / Pack.packed_size memop.pack) e.at
"invalid lane index";
[NumT (num_type_of_addr_type at); VecT t] --> [], []
| MemorySize x ->
let MemoryT (at, _lim) = memory c x in
[] --> [NumT (num_type_of_addr_type at)], []
| MemoryGrow x ->
let MemoryT (at, _lim) = memory c x in
[NumT (num_type_of_addr_type at)] --> [NumT (num_type_of_addr_type at)], []
| MemoryFill x ->
let MemoryT (at, _lim) = memory c x in
[NumT (num_type_of_addr_type at); NumT I32T;
NumT (num_type_of_addr_type at)] --> [], []
| MemoryCopy (x, y)->
let MemoryT (at1, _lib1) = memory c x in
let MemoryT (at2, _lib2) = memory c y in
[NumT (num_type_of_addr_type at1); NumT (num_type_of_addr_type at2);
NumT (num_type_of_addr_type (min at1 at2))] --> [], []
| MemoryInit (x, y) ->
let MemoryT (at, _lib) = memory c x in
let () = data c y in
[NumT (num_type_of_addr_type at); NumT I32T; NumT I32T] --> [], []
| DataDrop x ->
let () = data c x in
[] --> [], []
| RefNull ht ->
check_heap_type c ht e.at;
[] --> [RefT (Null, ht)], []
| RefFunc x ->
let dt = func c x in
refer_func c x;
[] --> [RefT (NoNull, DefHT dt)], []
| RefIsNull ->
let (_nul, ht) = peek_ref 0 s e.at in
[RefT (Null, ht)] --> [NumT I32T], []
| RefAsNonNull ->
let (_nul, ht) = peek_ref 0 s e.at in
[RefT (Null, ht)] --> [RefT (NoNull, ht)], []
| RefTest rt ->
let (_nul, ht) = rt in
check_ref_type c rt e.at;
check_cast c rt e.at;
[RefT (Null, top_of_heap_type c.types ht)] --> [NumT I32T], []
| RefCast rt ->
let (nul, ht) = rt in
check_ref_type c rt e.at;
check_cast c rt e.at;
[RefT (Null, top_of_heap_type c.types ht)] --> [RefT (nul, ht)], []
| RefEq ->
[RefT (Null, EqHT); RefT (Null, EqHT)] --> [NumT I32T], []
| RefI31 ->
[NumT I32T] --> [RefT (NoNull, I31HT)], []
| I31Get ext ->
[RefT (Null, I31HT)] --> [NumT I32T], []
| StructNew (x, initop) ->
let StructT fts = struct_type c x in
require
( initop = Explicit || List.for_all (fun ft ->
defaultable (unpacked_field_type ft)) fts ) x.at
"field type is not defaultable";
let ts = if initop = Implicit then [] else List.map unpacked_field_type fts in
ts --> [RefT (NoNull, DefHT (type_ c x))], []
| StructGet (x, y, exto) ->
let StructT fts = struct_type c x in
require (y.it < Lib.List32.length fts) y.at
("unknown field " ^ I32.to_string_u y.it);
let FieldT (_mut, st) = Lib.List32.nth fts y.it in
require ((exto <> None) == is_packed_storage_type st) y.at
("field is " ^ (if exto = None then "packed" else "unpacked"));
let t = unpacked_storage_type st in
[RefT (Null, DefHT (type_ c x))] --> [t], []
| StructSet (x, y) ->
let StructT fts = struct_type c x in
require (y.it < Lib.List32.length fts) y.at
("unknown field " ^ I32.to_string_u y.it);
let FieldT (mut, st) = Lib.List32.nth fts y.it in
require (mut == Var) y.at "field is immutable";
let t = unpacked_storage_type st in
[RefT (Null, DefHT (type_ c x)); t] --> [], []
| ArrayNew (x, initop) ->
let ArrayT ft = array_type c x in
require
(initop = Explicit || defaultable (unpacked_field_type ft)) x.at
"array type is not defaultable";
let ts = if initop = Implicit then [] else [unpacked_field_type ft] in
(ts @ [NumT I32T]) --> [RefT (NoNull, DefHT (type_ c x))], []
| ArrayNewFixed (x, n) ->
let ArrayT ft = array_type c x in
let ts = Lib.List32.make n (unpacked_field_type ft) in
ts --> [RefT (NoNull, DefHT (type_ c x))], []
| ArrayNewElem (x, y) ->
let ArrayT ft = array_type c x in
let rt = elem c y in
require (match_val_type c.types (RefT rt) (unpacked_field_type ft)) x.at
("type mismatch: element segment's type " ^ string_of_ref_type rt ^
" does not match array's field type " ^ string_of_field_type ft);
[NumT I32T; NumT I32T] --> [RefT (NoNull, DefHT (type_ c x))], []
| ArrayNewData (x, y) ->
let ArrayT ft = array_type c x in
let () = data c y in
let t = unpacked_field_type ft in
require (is_num_type t || is_vec_type t) x.at
"array type is not numeric or vector";
[NumT I32T; NumT I32T] --> [RefT (NoNull, DefHT (type_ c x))], []
| ArrayGet (x, exto) ->
let ArrayT (FieldT (_mut, st)) = array_type c x in
require ((exto <> None) == is_packed_storage_type st) e.at
("array is " ^ (if exto = None then "packed" else "unpacked"));
let t = unpacked_storage_type st in
[RefT (Null, DefHT (type_ c x)); NumT I32T] --> [t], []
| ArraySet x ->
let ArrayT (FieldT (mut, st)) = array_type c x in
require (mut == Var) e.at "array is immutable";
let t = unpacked_storage_type st in
[RefT (Null, DefHT (type_ c x)); NumT I32T; t] --> [], []
| ArrayLen ->
[RefT (Null, ArrayHT)] --> [NumT I32T], []
| ArrayCopy (x, y) ->
let ArrayT (FieldT (mutd, std)) = array_type c x in
let ArrayT (FieldT (_muts, sts)) = array_type c y in
require (mutd = Var) e.at "array is immutable";
require (match_storage_type c.types sts std) e.at "array types do not match";
[RefT (Null, DefHT (type_ c x)); NumT I32T; RefT (Null, DefHT (type_ c y)); NumT I32T; NumT I32T] --> [], []
| ArrayFill x ->
let ArrayT (FieldT (mut, st)) = array_type c x in
require (mut = Var) e.at "array is immutable";
let t = unpacked_storage_type st in
[RefT (Null, DefHT (type_ c x)); NumT I32T; t; NumT I32T] --> [], []
| ArrayInitData (x, y) ->
let ArrayT (FieldT (mut, st)) = array_type c x in
require (mut = Var) e.at "array is immutable";
let () = data c y in
let t = unpacked_storage_type st in
require (is_num_type t || is_vec_type t) x.at
"array type is not numeric or vector";
[RefT (Null, DefHT (type_ c x)); NumT I32T; NumT I32T; NumT I32T] --> [], []
| ArrayInitElem (x, y) ->
let ArrayT (FieldT (mut, st)) = array_type c x in
require (mut = Var) e.at "array is immutable";
let rt = elem c y in
require (match_val_type c.types (RefT rt) (unpacked_storage_type st)) x.at
("type mismatch: element segment's type " ^ string_of_ref_type rt ^
" does not match array's field type " ^ string_of_field_type (FieldT (mut, st)));
[RefT (Null, DefHT (type_ c x)); NumT I32T; NumT I32T; NumT I32T] --> [], []
| ExternConvert op ->
let ht1, ht2 = type_externop op in
let (nul, _ht) = peek_ref 0 s e.at in
[RefT (nul, ht1)] --> [RefT (nul, ht2)], []
| Const v ->
let t = NumT (type_num v.it) in
[] --> [t], []
| Test testop ->
let t = NumT (type_num testop) in
[t] --> [NumT I32T], []
| Compare relop ->
let t = NumT (type_num relop) in
[t; t] --> [NumT I32T], []
| Unary unop ->
check_unop unop e.at;
let t = NumT (type_num unop) in
[t] --> [t], []
| Binary binop ->
let t = NumT (type_num binop) in
[t; t] --> [t], []
| Convert cvtop ->
let t1, t2 = type_cvtop e.at cvtop in
[NumT t1] --> [NumT t2], []
| VecConst v ->
let t = VecT (type_vec v.it) in
[] --> [t], []