-
Notifications
You must be signed in to change notification settings - Fork 73
/
eval.ml
1223 lines (1027 loc) · 44.2 KB
/
eval.ml
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 Pack
open Source
open Types
open Value
open Instance
(* Errors *)
module Link = Error.Make ()
module Trap = Error.Make ()
module Crash = Error.Make ()
module Exhaustion = Error.Make ()
exception Link = Link.Error
exception Trap = Trap.Error
exception Crash = Crash.Error (* failure that cannot happen in valid code *)
exception Exhaustion = Exhaustion.Error
let table_error at = function
| Table.Bounds -> "out of bounds table access"
| Table.SizeOverflow -> "table size overflow"
| Table.SizeLimit -> "table size limit reached"
| Table.Type -> Crash.error at "type mismatch at table access"
| exn -> raise exn
let memory_error at = function
| Memory.Bounds -> "out of bounds memory access"
| Memory.SizeOverflow -> "memory size overflow"
| Memory.SizeLimit -> "memory size limit reached"
| Memory.Type -> Crash.error at "type mismatch at memory access"
| exn -> raise exn
let numeric_error at = function
| Ixx.Overflow -> "integer overflow"
| Ixx.DivideByZero -> "integer divide by zero"
| Ixx.InvalidConversion -> "invalid conversion to integer"
| Value.TypeError (i, v, t) ->
Crash.error at
("type error, expected " ^ string_of_num_type t ^ " as operand " ^
string_of_int i ^ ", got " ^ string_of_num_type (type_of_num v))
| exn -> raise exn
(* Administrative Expressions & Configurations *)
type 'a stack = 'a list
type frame =
{
inst : module_inst;
locals : value option ref list;
}
type code = value stack * admin_instr list
and admin_instr = admin_instr' phrase
and admin_instr' =
| Plain of instr'
| Refer of ref_
| Invoke of func_inst
| Trapping of string
| Returning of value stack
| ReturningInvoke of value stack * func_inst
| Breaking of int32 * value stack
| Label of int * instr list * code
| Frame of int * frame * code
type config =
{
frame : frame;
code : code;
budget : int; (* to model stack overflow *)
}
let frame inst locals = {inst; locals}
let config inst vs es =
{frame = frame inst []; code = vs, es; budget = !Flags.budget}
let plain e = Plain e.it @@ e.at
let admin_instr_of_value (v : value) at : admin_instr' =
match v with
| Num n -> Plain (Const (n @@ at))
| Vec v -> Plain (VecConst (v @@ at))
| Ref r -> Refer r
let is_jumping e =
match e.it with
| Trapping _ | Returning _ | ReturningInvoke _ | Breaking _ -> true
| _ -> false
let lookup category list x =
try Lib.List32.nth list x.it with Failure _ ->
Crash.error x.at ("undefined " ^ category ^ " " ^ Int32.to_string x.it)
let type_ (inst : module_inst) x = lookup "type" inst.types x
let func (inst : module_inst) x = lookup "function" inst.funcs x
let table (inst : module_inst) x = lookup "table" inst.tables x
let memory (inst : module_inst) x = lookup "memory" inst.memories x
let global (inst : module_inst) x = lookup "global" inst.globals x
let elem (inst : module_inst) x = lookup "element segment" inst.elems x
let data (inst : module_inst) x = lookup "data segment" inst.datas x
let local (frame : frame) x = lookup "local" frame.locals x
let str_type (inst : module_inst) x = expand_def_type (type_ inst x)
let func_type (inst : module_inst) x = as_func_str_type (str_type inst x)
let struct_type (inst : module_inst) x = as_struct_str_type (str_type inst x)
let array_type (inst : module_inst) x = as_array_str_type (str_type inst x)
let subst_of (inst : module_inst) = function
| StatX x when x < Lib.List32.length inst.types ->
DefHT (type_ inst (x @@ Source.no_region))
| x -> VarHT x
let any_ref (inst : module_inst) x i at =
try Table.load (table inst x) i with Table.Bounds ->
Trap.error at ("undefined element " ^ Int32.to_string i)
let func_ref (inst : module_inst) x i at =
match any_ref inst x i at with
| FuncRef f -> f
| NullRef _ -> Trap.error at ("uninitialized element " ^ Int32.to_string i)
| _ -> Crash.error at ("type mismatch for element " ^ Int32.to_string i)
let block_type (inst : module_inst) bt at =
match bt with
| ValBlockType None -> InstrT ([], [], [])
| ValBlockType (Some t) -> InstrT ([], [subst_val_type (subst_of inst) t], [])
| VarBlockType x ->
let FuncT (ts1, ts2) = func_type inst x in InstrT (ts1, ts2, [])
let take n (vs : 'a stack) at =
try Lib.List.take n vs with Failure _ -> Crash.error at "stack underflow"
let drop n (vs : 'a stack) at =
try Lib.List.drop n vs with Failure _ -> Crash.error at "stack underflow"
let split n (vs : 'a stack) at = take n vs at, drop n vs at
(* Evaluation *)
(*
* Conventions:
* e : instr
* v : value
* es : instr list
* vs : value stack
* c : config
*)
let mem_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(Memory.bound (memory frame.inst x))
let data_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(Data.size (data frame.inst x))
let table_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(I64_convert.extend_i32_u (Table.size (table frame.inst x)))
let elem_oob frame x i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(I64_convert.extend_i32_u (Elem.size (elem frame.inst x)))
let array_oob a i n =
I64.gt_u (I64.add (I64_convert.extend_i32_u i) (I64_convert.extend_i32_u n))
(I64_convert.extend_i32_u (Aggr.array_length a))
let rec step (c : config) : config =
let vs, es = c.code in
let e = List.hd es in
let vs', es' =
match e.it, vs with
| Plain e', vs ->
(match e', vs with
| Unreachable, vs ->
vs, [Trapping "unreachable executed" @@ e.at]
| Nop, vs ->
vs, []
| Block (bt, es'), vs ->
let InstrT (ts1, ts2, _xs) = block_type c.frame.inst bt e.at in
let n1 = List.length ts1 in
let n2 = List.length ts2 in
let args, vs' = split n1 vs e.at in
vs', [Label (n2, [], (args, List.map plain es')) @@ e.at]
| Loop (bt, es'), vs ->
let InstrT (ts1, ts2, _xs) = block_type c.frame.inst bt e.at in
let n1 = List.length ts1 in
let args, vs' = split n1 vs e.at in
vs', [Label (n1, [e' @@ e.at], (args, List.map plain es')) @@ e.at]
| If (bt, es1, es2), Num (I32 i) :: vs' ->
if i = 0l then
vs', [Plain (Block (bt, es2)) @@ e.at]
else
vs', [Plain (Block (bt, es1)) @@ e.at]
| Br x, vs ->
[], [Breaking (x.it, vs) @@ e.at]
| BrIf x, Num (I32 i) :: vs' ->
if i = 0l then
vs', []
else
vs', [Plain (Br x) @@ e.at]
| BrTable (xs, x), Num (I32 i) :: vs' ->
if I32.ge_u i (Lib.List32.length xs) then
vs', [Plain (Br x) @@ e.at]
else
vs', [Plain (Br (Lib.List32.nth xs i)) @@ e.at]
| BrOnNull x, Ref (NullRef _) :: vs' ->
vs', [Plain (Br x) @@ e.at]
| BrOnNull x, Ref r :: vs' ->
Ref r :: vs', []
| BrOnNonNull x, Ref (NullRef _) :: vs' ->
vs', []
| BrOnNonNull x, Ref r :: vs' ->
Ref r :: vs', [Plain (Br x) @@ e.at]
| BrOnCast (x, _rt1, rt2), Ref r :: vs' ->
let rt2' = subst_ref_type (subst_of c.frame.inst) rt2 in
if Match.match_ref_type [] (type_of_ref r) rt2' then
Ref r :: vs', [Plain (Br x) @@ e.at]
else
Ref r :: vs', []
| BrOnCastFail (x, _rt1, rt2), Ref r :: vs' ->
let rt2' = subst_ref_type (subst_of c.frame.inst) rt2 in
if Match.match_ref_type [] (type_of_ref r) rt2' then
Ref r :: vs', []
else
Ref r :: vs', [Plain (Br x) @@ e.at]
| Return, vs ->
[], [Returning vs @@ e.at]
| Call x, vs ->
vs, [Invoke (func c.frame.inst x) @@ e.at]
| CallRef _x, Ref (NullRef _) :: vs ->
vs, [Trapping "null function reference" @@ e.at]
| CallRef _x, Ref (FuncRef f) :: vs ->
vs, [Invoke f @@ e.at]
| CallIndirect (x, y), Num (I32 i) :: vs ->
let f = func_ref c.frame.inst x i e.at in
if Match.match_def_type [] (Func.type_of f) (type_ c.frame.inst y) then
vs, [Invoke f @@ e.at]
else
vs, [Trapping ("indirect call type mismatch, expected " ^
string_of_def_type (type_ c.frame.inst y) ^ " but got " ^
string_of_def_type (Func.type_of f)) @@ e.at]
| ReturnCallRef _x, Ref (NullRef _) :: vs ->
vs, [Trapping "null function reference" @@ e.at]
| ReturnCallRef x, vs ->
(match (step {c with code = (vs, [Plain (CallRef x) @@ e.at])}).code with
| vs', [{it = Invoke a; at}] -> vs', [ReturningInvoke (vs', a) @@ at]
| vs', [{it = Trapping s; at}] -> vs', [Trapping s @@ at]
| _ -> assert false
)
| ReturnCall x, vs ->
(match (step {c with code = (vs, [Plain (Call x) @@ e.at])}).code with
| vs', [{it = Invoke a; at}] -> vs', [ReturningInvoke (vs', a) @@ at]
| _ -> assert false
)
| ReturnCallIndirect (x, y), vs ->
(match
(step {c with code = (vs, [Plain (CallIndirect (x, y)) @@ e.at])}).code
with
| vs', [{it = Invoke a; at}] -> vs', [ReturningInvoke (vs', a) @@ at]
| vs', [{it = Trapping s; at}] -> vs', [Trapping s @@ at]
| _ -> assert false
)
| Drop, v :: vs' ->
vs', []
| Select _, Num (I32 i) :: v2 :: v1 :: vs' ->
if i = 0l then
v2 :: vs', []
else
v1 :: vs', []
| LocalGet x, vs ->
(match !(local c.frame x) with
| Some v ->
v :: vs, []
| None ->
Crash.error e.at "read of uninitialized local"
)
| LocalSet x, v :: vs' ->
local c.frame x := Some v;
vs', []
| LocalTee x, v :: vs' ->
local c.frame x := Some v;
v :: vs', []
| GlobalGet x, vs ->
Global.load (global c.frame.inst x) :: vs, []
| GlobalSet x, v :: vs' ->
(try Global.store (global c.frame.inst x) v; vs', []
with Global.NotMutable -> Crash.error e.at "write to immutable global"
| Global.Type -> Crash.error e.at "type mismatch at global write")
| TableGet x, Num (I32 i) :: vs' ->
(try Ref (Table.load (table c.frame.inst x) i) :: vs', []
with exn -> vs', [Trapping (table_error e.at exn) @@ e.at])
| TableSet x, Ref r :: Num (I32 i) :: vs' ->
(try Table.store (table c.frame.inst x) i r; vs', []
with exn -> vs', [Trapping (table_error e.at exn) @@ e.at])
| TableSize x, vs ->
Num (I32 (Table.size (table c.frame.inst x))) :: vs, []
| TableGrow x, Num (I32 delta) :: Ref r :: vs' ->
let tab = table c.frame.inst x in
let old_size = Table.size tab in
let result =
try Table.grow tab delta r; old_size
with Table.SizeOverflow | Table.SizeLimit | Table.OutOfMemory -> -1l
in Num (I32 result) :: vs', []
| TableFill x, Num (I32 n) :: Ref r :: Num (I32 i) :: vs' ->
if table_oob c.frame x i n then
vs', [Trapping (table_error e.at Table.Bounds) @@ e.at]
else if n = 0l then
vs', []
else
let _ = assert (I32.lt_u i 0xffff_ffffl) in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 i @@ e.at));
Refer r;
Plain (TableSet x);
Plain (Const (I32 (I32.add i 1l) @@ e.at));
Refer r;
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (TableFill x);
]
| TableCopy (x, y), Num (I32 n) :: Num (I32 s) :: Num (I32 d) :: vs' ->
if table_oob c.frame x d n || table_oob c.frame y s n then
vs', [Trapping (table_error e.at Table.Bounds) @@ e.at]
else if n = 0l then
vs', []
else if I32.le_u d s then
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 d @@ e.at));
Plain (Const (I32 s @@ e.at));
Plain (TableGet y);
Plain (TableSet x);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (TableCopy (x, y));
]
else (* d > s *)
let n' = I32.sub n 1l in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 (I32.add d n') @@ e.at));
Plain (Const (I32 (I32.add s n') @@ e.at));
Plain (TableGet y);
Plain (TableSet x);
Plain (Const (I32 d @@ e.at));
Plain (Const (I32 s @@ e.at));
Plain (Const (I32 n' @@ e.at));
Plain (TableCopy (x, y));
]
| TableInit (x, y), Num (I32 n) :: Num (I32 s) :: Num (I32 d) :: vs' ->
if table_oob c.frame x d n || elem_oob c.frame y s n then
vs', [Trapping (table_error e.at Table.Bounds) @@ e.at]
else if n = 0l then
vs', []
else
let seg = elem c.frame.inst y in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 d @@ e.at));
Refer (Elem.load seg s);
Plain (TableSet x);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (TableInit (x, y));
]
| ElemDrop x, vs ->
let seg = elem c.frame.inst x in
Elem.drop seg;
vs, []
| Load {offset; ty; pack; _}, Num (I32 i) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let a = I64_convert.extend_i32_u i in
(try
let n =
match pack with
| None -> Memory.load_num mem a offset ty
| Some (sz, ext) -> Memory.load_num_packed sz ext mem a offset ty
in Num n :: vs', []
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at])
| Store {offset; pack; _}, Num n :: Num (I32 i) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let a = I64_convert.extend_i32_u i in
(try
(match pack with
| None -> Memory.store_num mem a offset n
| Some sz -> Memory.store_num_packed sz mem a offset n
);
vs', []
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at]);
| VecLoad {offset; ty; pack; _}, Num (I32 i) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let a = I64_convert.extend_i32_u i in
(try
let v =
match pack with
| None -> Memory.load_vec mem a offset ty
| Some (sz, ext) -> Memory.load_vec_packed sz ext mem a offset ty
in Vec v :: vs', []
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at])
| VecStore {offset; _}, Vec v :: Num (I32 i) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let addr = I64_convert.extend_i32_u i in
(try
Memory.store_vec mem addr offset v;
vs', []
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at]);
| VecLoadLane ({offset; ty; pack; _}, j), Vec (V128 v) :: Num (I32 i) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let addr = I64_convert.extend_i32_u i in
(try
let v =
match pack with
| Pack8 ->
V128.I8x16.replace_lane j v
(I32Num.of_num 0 (Memory.load_num_packed Pack8 SX mem addr offset I32T))
| Pack16 ->
V128.I16x8.replace_lane j v
(I32Num.of_num 0 (Memory.load_num_packed Pack16 SX mem addr offset I32T))
| Pack32 ->
V128.I32x4.replace_lane j v
(I32Num.of_num 0 (Memory.load_num mem addr offset I32T))
| Pack64 ->
V128.I64x2.replace_lane j v
(I64Num.of_num 0 (Memory.load_num mem addr offset I64T))
in Vec (V128 v) :: vs', []
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at])
| VecStoreLane ({offset; ty; pack; _}, j), Vec (V128 v) :: Num (I32 i) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let addr = I64_convert.extend_i32_u i in
(try
(match pack with
| Pack8 ->
Memory.store_num_packed Pack8 mem addr offset (I32 (V128.I8x16.extract_lane_s j v))
| Pack16 ->
Memory.store_num_packed Pack16 mem addr offset (I32 (V128.I16x8.extract_lane_s j v))
| Pack32 ->
Memory.store_num mem addr offset (I32 (V128.I32x4.extract_lane_s j v))
| Pack64 ->
Memory.store_num mem addr offset (I64 (V128.I64x2.extract_lane_s j v))
);
vs', []
with exn -> vs', [Trapping (memory_error e.at exn) @@ e.at])
| MemorySize, vs ->
let mem = memory c.frame.inst (0l @@ e.at) in
Num (I32 (Memory.size mem)) :: vs, []
| MemoryGrow, Num (I32 delta) :: vs' ->
let mem = memory c.frame.inst (0l @@ e.at) in
let old_size = Memory.size mem in
let result =
try Memory.grow mem delta; old_size
with Memory.SizeOverflow | Memory.SizeLimit | Memory.OutOfMemory -> -1l
in Num (I32 result) :: vs', []
| MemoryFill, Num (I32 n) :: Num k :: Num (I32 i) :: vs' ->
if mem_oob c.frame (0l @@ e.at) i n then
vs', [Trapping (memory_error e.at Memory.Bounds) @@ e.at]
else if n = 0l then
vs', []
else
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 i @@ e.at));
Plain (Const (k @@ e.at));
Plain (Store
{ty = Types.I32T; align = 0; offset = 0l; pack = Some Pack8});
Plain (Const (I32 (I32.add i 1l) @@ e.at));
Plain (Const (k @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (MemoryFill);
]
| MemoryCopy, Num (I32 n) :: Num (I32 s) :: Num (I32 d) :: vs' ->
if mem_oob c.frame (0l @@ e.at) s n || mem_oob c.frame (0l @@ e.at) d n then
vs', [Trapping (memory_error e.at Memory.Bounds) @@ e.at]
else if n = 0l then
vs', []
else if I32.le_u d s then
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 d @@ e.at));
Plain (Const (I32 s @@ e.at));
Plain (Load
{ty = Types.I32T; align = 0; offset = 0l; pack = Some (Pack8, ZX)});
Plain (Store
{ty = Types.I32T; align = 0; offset = 0l; pack = Some Pack8});
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (MemoryCopy);
]
else (* d > s *)
let n' = I32.sub n 1l in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 (I32.add d n') @@ e.at));
Plain (Const (I32 (I32.add s n') @@ e.at));
Plain (Load
{ty = Types.I32T; align = 0; offset = 0l; pack = Some (Pack8, ZX)});
Plain (Store
{ty = Types.I32T; align = 0; offset = 0l; pack = Some Pack8});
Plain (Const (I32 d @@ e.at));
Plain (Const (I32 s @@ e.at));
Plain (Const (I32 n' @@ e.at));
Plain (MemoryCopy);
]
| MemoryInit x, Num (I32 n) :: Num (I32 s) :: Num (I32 d) :: vs' ->
if mem_oob c.frame (0l @@ e.at) d n || data_oob c.frame x s n then
vs', [Trapping (memory_error e.at Memory.Bounds) @@ e.at]
else if n = 0l then
vs', []
else
let seg = data c.frame.inst x in
let a = I64_convert.extend_i32_u s in
let b = Data.load_byte seg a in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Plain (Const (I32 d @@ e.at));
Plain (Const (I32 (I32.of_int_u (Char.code b)) @@ e.at));
Plain (Store
{ty = Types.I32T; align = 0; offset = 0l; pack = Some Pack8});
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (MemoryInit x);
]
| DataDrop x, vs ->
let seg = data c.frame.inst x in
Data.drop seg;
vs, []
| RefNull t, vs' ->
Ref (NullRef (subst_heap_type (subst_of c.frame.inst) t)) :: vs', []
| RefFunc x, vs' ->
let f = func c.frame.inst x in
Ref (FuncRef f) :: vs', []
| RefIsNull, Ref (NullRef _) :: vs' ->
value_of_bool true :: vs', []
| RefIsNull, Ref _ :: vs' ->
value_of_bool false :: vs', []
| RefAsNonNull, Ref (NullRef _) :: vs' ->
vs', [Trapping "null reference" @@ e.at]
| RefAsNonNull, Ref r :: vs' ->
Ref r :: vs', []
| RefTest rt, Ref r :: vs' ->
let rt' = subst_ref_type (subst_of c.frame.inst) rt in
value_of_bool (Match.match_ref_type [] (type_of_ref r) rt') :: vs', []
| RefCast rt, Ref r :: vs' ->
let rt' = subst_ref_type (subst_of c.frame.inst) rt in
if Match.match_ref_type [] (type_of_ref r) rt' then
Ref r :: vs', []
else
vs', [Trapping ("cast failure, expected " ^
string_of_ref_type rt ^ " but got " ^
string_of_ref_type (type_of_ref r)) @@ e.at]
| RefEq, Ref r1 :: Ref r2 :: vs' ->
value_of_bool (eq_ref r1 r2) :: vs', []
| RefI31, Num (I32 i) :: vs' ->
Ref (I31.I31Ref (I31.of_i32 i)) :: vs', []
| I31Get ext, Ref (NullRef _) :: vs' ->
vs', [Trapping "null i31 reference" @@ e.at]
| I31Get ext, Ref (I31.I31Ref i) :: vs' ->
Num (I32 (I31.to_i32 ext i)) :: vs', []
| StructNew (x, initop), vs' ->
let StructT fts = struct_type c.frame.inst x in
let args, vs'' =
match initop with
| Explicit ->
let args, vs'' = split (List.length fts) vs' e.at in
List.rev args, vs''
| Implicit ->
let ts = List.map unpacked_field_type fts in
try List.map Option.get (List.map default_value ts), vs'
with Invalid_argument _ -> Crash.error e.at "non-defaultable type"
in
let struct_ =
try Aggr.alloc_struct (type_ c.frame.inst x) args
with Failure _ -> Crash.error e.at "type mismatch packing value"
in Ref (Aggr.StructRef struct_) :: vs'', []
| StructGet (x, y, exto), Ref (NullRef _) :: vs' ->
vs', [Trapping "null structure reference" @@ e.at]
| StructGet (x, y, exto), Ref Aggr.(StructRef (Struct (_, fs))) :: vs' ->
let f =
try Lib.List32.nth fs y.it
with Failure _ -> Crash.error y.at "undefined field"
in
(try Aggr.read_field f exto :: vs', []
with Failure _ -> Crash.error e.at "type mismatch reading field")
| StructSet (x, y), v :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null structure reference" @@ e.at]
| StructSet (x, y), v :: Ref Aggr.(StructRef (Struct (_, fs))) :: vs' ->
let f =
try Lib.List32.nth fs y.it
with Failure _ -> Crash.error y.at "undefined field"
in
(try Aggr.write_field f v; vs', []
with Failure _ -> Crash.error e.at "type mismatch writing field")
| ArrayNew (x, initop), Num (I32 n) :: vs' ->
let ArrayT (FieldT (_mut, st)) = array_type c.frame.inst x in
let arg, vs'' =
match initop with
| Explicit -> List.hd vs', List.tl vs'
| Implicit ->
try Option.get (default_value (unpacked_storage_type st)), vs'
with Invalid_argument _ -> Crash.error e.at "non-defaultable type"
in
let array =
try Aggr.alloc_array (type_ c.frame.inst x) (Lib.List32.make n arg)
with Failure _ -> Crash.error e.at "type mismatch packing value"
in Ref (Aggr.ArrayRef array) :: vs'', []
| ArrayNewFixed (x, n), vs' ->
let args, vs'' = split (I32.to_int_u n) vs' e.at in
let array =
try Aggr.alloc_array (type_ c.frame.inst x) (List.rev args)
with Failure _ -> Crash.error e.at "type mismatch packing value"
in Ref (Aggr.ArrayRef array) :: vs'', []
| ArrayNewElem (x, y), Num (I32 n) :: Num (I32 s) :: vs' ->
if elem_oob c.frame y s n then
vs', [Trapping (table_error e.at Table.Bounds) @@ e.at]
else
let seg = elem c.frame.inst y in
let rs = Lib.List32.init n (fun i -> Elem.load seg (Int32.add s i)) in
let args = List.map (fun r -> Ref r) rs in
let array =
try Aggr.alloc_array (type_ c.frame.inst x) args
with Failure _ -> Crash.error e.at "type mismatch packing value"
in Ref (Aggr.ArrayRef array) :: vs', []
| ArrayNewData (x, y), Num (I32 n) :: Num (I32 s) :: vs' ->
if data_oob c.frame y s n then
vs', [Trapping (memory_error e.at Memory.Bounds) @@ e.at]
else
let ArrayT (FieldT (_mut, st)) = array_type c.frame.inst x in
let seg = data c.frame.inst y in
let args = Lib.List32.init n
(fun i ->
let a = I32.(add s (mul i (I32.of_int_u (storage_size st)))) in
Data.load_val_storage seg (I64_convert.extend_i32_u a) st
)
in
let array =
try Aggr.alloc_array (type_ c.frame.inst x) args
with Failure _ -> Crash.error e.at "type mismatch packing value"
in Ref (Aggr.ArrayRef array) :: vs', []
| ArrayGet (x, exto), Num (I32 i) :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayGet (x, exto), Num (I32 i) :: Ref (Aggr.ArrayRef a) :: vs'
when array_oob a i 1l ->
vs', [Trapping "out of bounds array access" @@ e.at]
| ArrayGet (x, exto), Num (I32 i) :: Ref Aggr.(ArrayRef (Array (_, fs))) :: vs' ->
(try Aggr.read_field (Lib.List32.nth fs i) exto :: vs', []
with Failure _ -> Crash.error e.at "type mismatch reading array")
| ArraySet x, v :: Num (I32 i) :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArraySet x, v :: Num (I32 i) :: Ref (Aggr.ArrayRef a) :: vs'
when array_oob a i 1l ->
vs', [Trapping "out of bounds array access" @@ e.at]
| ArraySet x, v :: Num (I32 i) :: Ref Aggr.(ArrayRef (Array (_, fs))) :: vs' ->
(try Aggr.write_field (Lib.List32.nth fs i) v; vs', []
with Failure _ -> Crash.error e.at "type mismatch writing array")
| ArrayLen, Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayLen, Ref Aggr.(ArrayRef (Array (_, fs))) :: vs' ->
Num (I32 (Lib.List32.length fs)) :: vs', []
| ArrayCopy (x, y),
Num _ :: Num _ :: Ref (NullRef _) :: Num _ :: Ref _ :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayCopy (x, y),
Num _ :: Num _ :: Ref _ :: Num _ :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayCopy (x, y),
Num (I32 n) ::
Num (I32 s) :: Ref (Aggr.ArrayRef sa) ::
Num (I32 d) :: Ref (Aggr.ArrayRef da) :: vs' ->
if array_oob sa s n || array_oob da d n then
vs', [Trapping "out of bounds array access" @@ e.at]
else if n = 0l then
vs', []
else
let exto =
match as_array_str_type (expand_def_type (Aggr.type_of_array sa)) with
| ArrayT (FieldT (_, PackStorageT _)) -> Some ZX
| _ -> None
in
if I32.le_u d s then
vs', List.map (Lib.Fun.flip (@@) e.at) [
Refer (Aggr.ArrayRef da);
Plain (Const (I32 d @@ e.at));
Refer (Aggr.ArrayRef sa);
Plain (Const (I32 s @@ e.at));
Plain (ArrayGet (y, exto));
Plain (ArraySet x);
Refer (Aggr.ArrayRef da);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Refer (Aggr.ArrayRef sa);
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (ArrayCopy (x, y));
]
else (* d > s *)
vs', List.map (Lib.Fun.flip (@@) e.at) [
Refer (Aggr.ArrayRef da);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Refer (Aggr.ArrayRef sa);
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (ArrayCopy (x, y));
Refer (Aggr.ArrayRef da);
Plain (Const (I32 d @@ e.at));
Refer (Aggr.ArrayRef sa);
Plain (Const (I32 s @@ e.at));
Plain (ArrayGet (y, exto));
Plain (ArraySet x);
]
| ArrayFill x, Num (I32 n) :: v :: Num (I32 i) :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayFill x, Num (I32 n) :: v :: Num (I32 i) :: Ref (Aggr.ArrayRef a) :: vs' ->
if array_oob a i n then
vs', [Trapping "out of bounds array access" @@ e.at]
else if n = 0l then
vs', []
else
vs', List.map (Lib.Fun.flip (@@) e.at) [
Refer (Aggr.ArrayRef a);
Plain (Const (I32 i @@ e.at));
admin_instr_of_value v e.at;
Plain (ArraySet x);
Refer (Aggr.ArrayRef a);
Plain (Const (I32 (I32.add i 1l) @@ e.at));
admin_instr_of_value v e.at;
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (ArrayFill x);
]
| ArrayInitData (x, y),
Num _ :: Num _ :: Num _ :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayInitData (x, y),
Num (I32 n) :: Num (I32 s) :: Num (I32 d) :: Ref (Aggr.ArrayRef a) :: vs' ->
if array_oob a d n then
vs', [Trapping "out of bounds array access" @@ e.at]
else if data_oob c.frame y s n then
vs', [Trapping (memory_error e.at Memory.Bounds) @@ e.at]
else if n = 0l then
vs', []
else
let ArrayT (FieldT (_mut, st)) = array_type c.frame.inst x in
let seg = data c.frame.inst y in
let v = Data.load_val_storage seg (I64_convert.extend_i32_u s) st in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Refer (Aggr.ArrayRef a);
Plain (Const (I32 d @@ e.at));
admin_instr_of_value v e.at;
Plain (ArraySet x);
Refer (Aggr.ArrayRef a);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s (I32.of_int_u (storage_size st))) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (ArrayInitData (x, y));
]
| ArrayInitElem (x, y),
Num _ :: Num _ :: Num _ :: Ref (NullRef _) :: vs' ->
vs', [Trapping "null array reference" @@ e.at]
| ArrayInitElem (x, y),
Num (I32 n) :: Num (I32 s) :: Num (I32 d) :: Ref (Aggr.ArrayRef a) :: vs' ->
if array_oob a d n then
vs', [Trapping "out of bounds array access" @@ e.at]
else if elem_oob c.frame y s n then
vs', [Trapping (table_error e.at Table.Bounds) @@ e.at]
else if n = 0l then
vs', []
else
let seg = elem c.frame.inst y in
let v = Ref (Elem.load seg s) in
vs', List.map (Lib.Fun.flip (@@) e.at) [
Refer (Aggr.ArrayRef a);
Plain (Const (I32 d @@ e.at));
admin_instr_of_value v e.at;
Plain (ArraySet x);
Refer (Aggr.ArrayRef a);
Plain (Const (I32 (I32.add d 1l) @@ e.at));
Plain (Const (I32 (I32.add s 1l) @@ e.at));
Plain (Const (I32 (I32.sub n 1l) @@ e.at));
Plain (ArrayInitElem (x, y));
]
| ExternConvert Internalize, Ref (NullRef _) :: vs' ->
Ref (NullRef NoneHT) :: vs', []
| ExternConvert Internalize, Ref (Extern.ExternRef r) :: vs' ->
Ref r :: vs', []
| ExternConvert Externalize, Ref (NullRef _) :: vs' ->
Ref (NullRef NoExternHT) :: vs', []
| ExternConvert Externalize, Ref r :: vs' ->
Ref (Extern.ExternRef r) :: vs', []
| Const n, vs ->
Num n.it :: vs, []
| Test testop, Num n :: vs' ->
(try value_of_bool (Eval_num.eval_testop testop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| Compare relop, Num n2 :: Num n1 :: vs' ->
(try value_of_bool (Eval_num.eval_relop relop n1 n2) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| Unary unop, Num n :: vs' ->
(try Num (Eval_num.eval_unop unop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| Binary binop, Num n2 :: Num n1 :: vs' ->
(try Num (Eval_num.eval_binop binop n1 n2) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| Convert cvtop, Num n :: vs' ->
(try Num (Eval_num.eval_cvtop cvtop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecConst v, vs ->
Vec v.it :: vs, []
| VecTest testop, Vec n :: vs' ->
(try value_of_bool (Eval_vec.eval_testop testop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecUnary unop, Vec n :: vs' ->
(try Vec (Eval_vec.eval_unop unop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecBinary binop, Vec n2 :: Vec n1 :: vs' ->
(try Vec (Eval_vec.eval_binop binop n1 n2) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecCompare relop, Vec n2 :: Vec n1 :: vs' ->
(try Vec (Eval_vec.eval_relop relop n1 n2) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecConvert cvtop, Vec n :: vs' ->
(try Vec (Eval_vec.eval_cvtop cvtop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecShift shiftop, Num s :: Vec v :: vs' ->
(try Vec (Eval_vec.eval_shiftop shiftop v s) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecBitmask bitmaskop, Vec v :: vs' ->
(try Num (Eval_vec.eval_bitmaskop bitmaskop v) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecTestBits vtestop, Vec n :: vs' ->
(try value_of_bool (Eval_vec.eval_vtestop vtestop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecUnaryBits vunop, Vec n :: vs' ->
(try Vec (Eval_vec.eval_vunop vunop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecBinaryBits vbinop, Vec n2 :: Vec n1 :: vs' ->
(try Vec (Eval_vec.eval_vbinop vbinop n1 n2) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecTernaryBits vternop, Vec v3 :: Vec v2 :: Vec v1 :: vs' ->
(try Vec (Eval_vec.eval_vternop vternop v1 v2 v3) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecSplat splatop, Num n :: vs' ->
(try Vec (Eval_vec.eval_splatop splatop n) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecExtract extractop, Vec v :: vs' ->
(try Num (Eval_vec.eval_extractop extractop v) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| VecReplace replaceop, Num r :: Vec v :: vs' ->
(try Vec (Eval_vec.eval_replaceop replaceop v r) :: vs', []
with exn -> vs', [Trapping (numeric_error e.at exn) @@ e.at])
| _ ->
let s1 = string_of_values (List.rev vs) in
let s2 = string_of_result_type (List.map type_of_value (List.rev vs)) in
Crash.error e.at
("missing or ill-typed operand on stack (" ^ s1 ^ " : " ^ s2 ^ ")")
)
| Refer r, vs ->
Ref r :: vs, []
| Trapping _, vs ->
assert false
| Returning _, vs
| ReturningInvoke _, vs ->
Crash.error e.at "undefined frame"
| Breaking _, vs ->
Crash.error e.at "undefined label"
| Label (n, es0, (vs', [])), vs ->
vs' @ vs, []
| Label (n, es0, (vs', {it = Breaking (0l, vs0); at} :: es')), vs ->
take n vs0 e.at @ vs, List.map plain es0
| Label (n, es0, (vs', {it = Breaking (k, vs0); at} :: es')), vs ->
vs, [Breaking (Int32.sub k 1l, vs0) @@ at]
| Label (n, es0, (vs', e' :: es')), vs when is_jumping e' ->
vs, [e']
| Label (n, es0, code'), vs ->
let c' = step {c with code = code'} in
vs, [Label (n, es0, c'.code) @@ e.at]
| Frame (n, frame', (vs', [])), vs ->
vs' @ vs, []