forked from WebAssembly/binaryen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm-interpreter.h
1968 lines (1863 loc) · 59.1 KB
/
wasm-interpreter.h
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
/*
* Copyright 2015 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Simple WebAssembly interpreter. This operates directly on the AST,
// for simplicity and clarity. A goal is for it to be possible for
// people to read this code and understand WebAssembly semantics.
//
#ifndef wasm_wasm_interpreter_h
#define wasm_wasm_interpreter_h
#include <cmath>
#include <limits.h>
#include <sstream>
#include "ir/module-utils.h"
#include "support/bits.h"
#include "support/safe_integer.h"
#include "wasm-traversal.h"
#include "wasm.h"
#ifdef WASM_INTERPRETER_DEBUG
#include "wasm-printing.h"
#endif
namespace wasm {
using namespace cashew;
// Utilities
extern Name WASM, RETURN_FLOW;
// Stuff that flows around during executing expressions: a literal, or a change
// in control flow.
class Flow {
public:
Flow() = default;
Flow(Literal value) : value(value) {}
Flow(Name breakTo) : breakTo(breakTo) {}
Literal value;
Name breakTo; // if non-null, a break is going on
bool breaking() { return breakTo.is(); }
void clearIf(Name target) {
if (breakTo == target) {
breakTo.clear();
}
}
friend std::ostream& operator<<(std::ostream& o, Flow& flow) {
o << "(flow " << (flow.breakTo.is() ? flow.breakTo.str : "-") << " : "
<< flow.value << ')';
return o;
}
};
// A list of literals, for function calls
typedef std::vector<Literal> LiteralList;
// Debugging helpers
#ifdef WASM_INTERPRETER_DEBUG
class Indenter {
static int indentLevel;
const char* entryName;
public:
Indenter(const char* entry);
~Indenter();
static void print();
};
#define NOTE_ENTER(x) \
Indenter _int_blah(x); \
{ \
Indenter::print(); \
std::cout << "visit " << x << " : " << curr << "\n"; \
}
#define NOTE_ENTER_(x) \
Indenter _int_blah(x); \
{ \
Indenter::print(); \
std::cout << "visit " << x << "\n"; \
}
#define NOTE_NAME(p0) \
{ \
Indenter::print(); \
std::cout << "name " << '(' << Name(p0) << ")\n"; \
}
#define NOTE_EVAL1(p0) \
{ \
Indenter::print(); \
std::cout << "eval " #p0 " (" << p0 << ")\n"; \
}
#define NOTE_EVAL2(p0, p1) \
{ \
Indenter::print(); \
std::cout << "eval " #p0 " (" << p0 << "), " #p1 " (" << p1 << ")\n"; \
}
#else // WASM_INTERPRETER_DEBUG
#define NOTE_ENTER(x)
#define NOTE_ENTER_(x)
#define NOTE_NAME(p0)
#define NOTE_EVAL1(p0)
#define NOTE_EVAL2(p0, p1)
#endif // WASM_INTERPRETER_DEBUG
// Execute an expression
template<typename SubType>
class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
protected:
Index maxDepth;
Index depth = 0;
public:
ExpressionRunner(Index maxDepth) : maxDepth(maxDepth) {}
Flow visit(Expression* curr) {
depth++;
if (depth > maxDepth) {
trap("interpreter recursion limit");
}
auto ret = OverriddenVisitor<SubType, Flow>::visit(curr);
if (!ret.breaking() &&
(isConcreteType(curr->type) || isConcreteType(ret.value.type))) {
#if 1 // def WASM_INTERPRETER_DEBUG
if (ret.value.type != curr->type) {
std::cerr << "expected " << printType(curr->type) << ", seeing "
<< printType(ret.value.type) << " from\n"
<< curr << '\n';
}
#endif
assert(ret.value.type == curr->type);
}
depth--;
return ret;
}
Flow visitBlock(Block* curr) {
NOTE_ENTER("Block");
// special-case Block, because Block nesting (in their first element) can be
// incredibly deep
std::vector<Block*> stack;
stack.push_back(curr);
while (curr->list.size() > 0 && curr->list[0]->is<Block>()) {
curr = curr->list[0]->cast<Block>();
stack.push_back(curr);
}
Flow flow;
auto* top = stack.back();
while (stack.size() > 0) {
curr = stack.back();
stack.pop_back();
if (flow.breaking()) {
flow.clearIf(curr->name);
continue;
}
auto& list = curr->list;
for (size_t i = 0; i < list.size(); i++) {
if (curr != top && i == 0) {
// one of the block recursions we already handled
continue;
}
flow = visit(list[i]);
if (flow.breaking()) {
flow.clearIf(curr->name);
break;
}
}
}
return flow;
}
Flow visitIf(If* curr) {
NOTE_ENTER("If");
Flow flow = visit(curr->condition);
if (flow.breaking()) {
return flow;
}
NOTE_EVAL1(flow.value);
if (flow.value.geti32()) {
Flow flow = visit(curr->ifTrue);
if (!flow.breaking() && !curr->ifFalse) {
flow.value = Literal(); // if_else returns a value, but if does not
}
return flow;
}
if (curr->ifFalse) {
return visit(curr->ifFalse);
}
return Flow();
}
Flow visitLoop(Loop* curr) {
NOTE_ENTER("Loop");
while (1) {
Flow flow = visit(curr->body);
if (flow.breaking()) {
if (flow.breakTo == curr->name) {
continue; // lol
}
}
// loop does not loop automatically, only continue achieves that
return flow;
}
}
Flow visitBreak(Break* curr) {
NOTE_ENTER("Break");
bool condition = true;
Flow flow;
if (curr->value) {
flow = visit(curr->value);
if (flow.breaking()) {
return flow;
}
}
if (curr->condition) {
Flow conditionFlow = visit(curr->condition);
if (conditionFlow.breaking()) {
return conditionFlow;
}
condition = conditionFlow.value.getInteger() != 0;
if (!condition) {
return flow;
}
}
flow.breakTo = curr->name;
return flow;
}
Flow visitSwitch(Switch* curr) {
NOTE_ENTER("Switch");
Flow flow;
Literal value;
if (curr->value) {
flow = visit(curr->value);
if (flow.breaking()) {
return flow;
}
value = flow.value;
NOTE_EVAL1(value);
}
flow = visit(curr->condition);
if (flow.breaking()) {
return flow;
}
int64_t index = flow.value.getInteger();
Name target = curr->default_;
if (index >= 0 && (size_t)index < curr->targets.size()) {
target = curr->targets[(size_t)index];
}
flow.breakTo = target;
flow.value = value;
return flow;
}
Flow visitConst(Const* curr) {
NOTE_ENTER("Const");
NOTE_EVAL1(curr->value);
return Flow(curr->value); // heh
}
// Unary and Binary nodes, the core math computations. We mostly just
// delegate to the Literal::* methods, except we handle traps here.
Flow visitUnary(Unary* curr) {
NOTE_ENTER("Unary");
Flow flow = visit(curr->value);
if (flow.breaking()) {
return flow;
}
Literal value = flow.value;
NOTE_EVAL1(value);
switch (curr->op) {
case ClzInt32:
case ClzInt64:
return value.countLeadingZeroes();
case CtzInt32:
case CtzInt64:
return value.countTrailingZeroes();
case PopcntInt32:
case PopcntInt64:
return value.popCount();
case EqZInt32:
case EqZInt64:
return value.eqz();
case ReinterpretInt32:
return value.castToF32();
case ReinterpretInt64:
return value.castToF64();
case ExtendSInt32:
return value.extendToSI64();
case ExtendUInt32:
return value.extendToUI64();
case WrapInt64:
return value.wrapToI32();
case ConvertUInt32ToFloat32:
case ConvertUInt64ToFloat32:
return value.convertUIToF32();
case ConvertUInt32ToFloat64:
case ConvertUInt64ToFloat64:
return value.convertUIToF64();
case ConvertSInt32ToFloat32:
case ConvertSInt64ToFloat32:
return value.convertSIToF32();
case ConvertSInt32ToFloat64:
case ConvertSInt64ToFloat64:
return value.convertSIToF64();
case ExtendS8Int32:
case ExtendS8Int64:
return value.extendS8();
case ExtendS16Int32:
case ExtendS16Int64:
return value.extendS16();
case ExtendS32Int64:
return value.extendS32();
case NegFloat32:
case NegFloat64:
return value.neg();
case AbsFloat32:
case AbsFloat64:
return value.abs();
case CeilFloat32:
case CeilFloat64:
return value.ceil();
case FloorFloat32:
case FloorFloat64:
return value.floor();
case TruncFloat32:
case TruncFloat64:
return value.trunc();
case NearestFloat32:
case NearestFloat64:
return value.nearbyint();
case SqrtFloat32:
case SqrtFloat64:
return value.sqrt();
case TruncSFloat32ToInt32:
case TruncSFloat64ToInt32:
case TruncSFloat32ToInt64:
case TruncSFloat64ToInt64:
return truncSFloat(curr, value);
case TruncUFloat32ToInt32:
case TruncUFloat64ToInt32:
case TruncUFloat32ToInt64:
case TruncUFloat64ToInt64:
return truncUFloat(curr, value);
case TruncSatSFloat32ToInt32:
case TruncSatSFloat64ToInt32:
return value.truncSatToSI32();
case TruncSatSFloat32ToInt64:
case TruncSatSFloat64ToInt64:
return value.truncSatToSI64();
case TruncSatUFloat32ToInt32:
case TruncSatUFloat64ToInt32:
return value.truncSatToUI32();
case TruncSatUFloat32ToInt64:
case TruncSatUFloat64ToInt64:
return value.truncSatToUI64();
case ReinterpretFloat32:
return value.castToI32();
case PromoteFloat32:
return value.extendToF64();
case ReinterpretFloat64:
return value.castToI64();
case DemoteFloat64:
return value.demote();
case SplatVecI8x16:
return value.splatI8x16();
case SplatVecI16x8:
return value.splatI16x8();
case SplatVecI32x4:
return value.splatI32x4();
case SplatVecI64x2:
return value.splatI64x2();
case SplatVecF32x4:
return value.splatF32x4();
case SplatVecF64x2:
return value.splatF64x2();
case NotVec128:
return value.notV128();
case NegVecI8x16:
return value.negI8x16();
case AnyTrueVecI8x16:
return value.anyTrueI8x16();
case AllTrueVecI8x16:
return value.allTrueI8x16();
case NegVecI16x8:
return value.negI16x8();
case AnyTrueVecI16x8:
return value.anyTrueI16x8();
case AllTrueVecI16x8:
return value.allTrueI16x8();
case NegVecI32x4:
return value.negI32x4();
case AnyTrueVecI32x4:
return value.anyTrueI32x4();
case AllTrueVecI32x4:
return value.allTrueI32x4();
case NegVecI64x2:
return value.negI64x2();
case AnyTrueVecI64x2:
return value.anyTrueI64x2();
case AllTrueVecI64x2:
return value.allTrueI64x2();
case AbsVecF32x4:
return value.absF32x4();
case NegVecF32x4:
return value.negF32x4();
case SqrtVecF32x4:
return value.sqrtF32x4();
case AbsVecF64x2:
return value.absF64x2();
case NegVecF64x2:
return value.negF64x2();
case SqrtVecF64x2:
return value.sqrtF64x2();
case TruncSatSVecF32x4ToVecI32x4:
return value.truncSatToSI32x4();
case TruncSatUVecF32x4ToVecI32x4:
return value.truncSatToUI32x4();
case TruncSatSVecF64x2ToVecI64x2:
return value.truncSatToSI64x2();
case TruncSatUVecF64x2ToVecI64x2:
return value.truncSatToUI64x2();
case ConvertSVecI32x4ToVecF32x4:
return value.convertSToF32x4();
case ConvertUVecI32x4ToVecF32x4:
return value.convertUToF32x4();
case ConvertSVecI64x2ToVecF64x2:
return value.convertSToF64x2();
case ConvertUVecI64x2ToVecF64x2:
return value.convertUToF64x2();
case InvalidUnary:
WASM_UNREACHABLE();
}
WASM_UNREACHABLE();
}
Flow visitBinary(Binary* curr) {
NOTE_ENTER("Binary");
Flow flow = visit(curr->left);
if (flow.breaking()) {
return flow;
}
Literal left = flow.value;
flow = visit(curr->right);
if (flow.breaking()) {
return flow;
}
Literal right = flow.value;
NOTE_EVAL2(left, right);
assert(isConcreteType(curr->left->type) ? left.type == curr->left->type
: true);
assert(isConcreteType(curr->right->type) ? right.type == curr->right->type
: true);
switch (curr->op) {
case AddInt32:
case AddInt64:
case AddFloat32:
case AddFloat64:
return left.add(right);
case SubInt32:
case SubInt64:
case SubFloat32:
case SubFloat64:
return left.sub(right);
case MulInt32:
case MulInt64:
case MulFloat32:
case MulFloat64:
return left.mul(right);
case DivSInt32: {
if (right.getInteger() == 0) {
trap("i32.div_s by 0");
}
if (left.getInteger() == std::numeric_limits<int32_t>::min() &&
right.getInteger() == -1) {
trap("i32.div_s overflow"); // signed division overflow
}
return left.divS(right);
}
case DivUInt32: {
if (right.getInteger() == 0) {
trap("i32.div_u by 0");
}
return left.divU(right);
}
case RemSInt32: {
if (right.getInteger() == 0) {
trap("i32.rem_s by 0");
}
if (left.getInteger() == std::numeric_limits<int32_t>::min() &&
right.getInteger() == -1) {
return Literal(int32_t(0));
}
return left.remS(right);
}
case RemUInt32: {
if (right.getInteger() == 0) {
trap("i32.rem_u by 0");
}
return left.remU(right);
}
case DivSInt64: {
if (right.getInteger() == 0) {
trap("i64.div_s by 0");
}
if (left.getInteger() == LLONG_MIN && right.getInteger() == -1LL) {
trap("i64.div_s overflow"); // signed division overflow
}
return left.divS(right);
}
case DivUInt64: {
if (right.getInteger() == 0) {
trap("i64.div_u by 0");
}
return left.divU(right);
}
case RemSInt64: {
if (right.getInteger() == 0) {
trap("i64.rem_s by 0");
}
if (left.getInteger() == LLONG_MIN && right.getInteger() == -1LL) {
return Literal(int64_t(0));
}
return left.remS(right);
}
case RemUInt64: {
if (right.getInteger() == 0) {
trap("i64.rem_u by 0");
}
return left.remU(right);
}
case DivFloat32:
case DivFloat64:
return left.div(right);
case AndInt32:
case AndInt64:
return left.and_(right);
case OrInt32:
case OrInt64:
return left.or_(right);
case XorInt32:
case XorInt64:
return left.xor_(right);
case ShlInt32:
case ShlInt64:
return left.shl(right);
case ShrUInt32:
case ShrUInt64:
return left.shrU(right);
case ShrSInt32:
case ShrSInt64:
return left.shrS(right);
case RotLInt32:
case RotLInt64:
return left.rotL(right);
case RotRInt32:
case RotRInt64:
return left.rotR(right);
case EqInt32:
case EqInt64:
case EqFloat32:
case EqFloat64:
return left.eq(right);
case NeInt32:
case NeInt64:
case NeFloat32:
case NeFloat64:
return left.ne(right);
case LtSInt32:
case LtSInt64:
return left.ltS(right);
case LtUInt32:
case LtUInt64:
return left.ltU(right);
case LeSInt32:
case LeSInt64:
return left.leS(right);
case LeUInt32:
case LeUInt64:
return left.leU(right);
case GtSInt32:
case GtSInt64:
return left.gtS(right);
case GtUInt32:
case GtUInt64:
return left.gtU(right);
case GeSInt32:
case GeSInt64:
return left.geS(right);
case GeUInt32:
case GeUInt64:
return left.geU(right);
case LtFloat32:
case LtFloat64:
return left.lt(right);
case LeFloat32:
case LeFloat64:
return left.le(right);
case GtFloat32:
case GtFloat64:
return left.gt(right);
case GeFloat32:
case GeFloat64:
return left.ge(right);
case CopySignFloat32:
case CopySignFloat64:
return left.copysign(right);
case MinFloat32:
case MinFloat64:
return left.min(right);
case MaxFloat32:
case MaxFloat64:
return left.max(right);
case EqVecI8x16:
return left.eqI8x16(right);
case NeVecI8x16:
return left.neI8x16(right);
case LtSVecI8x16:
return left.ltSI8x16(right);
case LtUVecI8x16:
return left.ltUI8x16(right);
case GtSVecI8x16:
return left.gtSI8x16(right);
case GtUVecI8x16:
return left.gtUI8x16(right);
case LeSVecI8x16:
return left.leSI8x16(right);
case LeUVecI8x16:
return left.leUI8x16(right);
case GeSVecI8x16:
return left.geSI8x16(right);
case GeUVecI8x16:
return left.geUI8x16(right);
case EqVecI16x8:
return left.eqI16x8(right);
case NeVecI16x8:
return left.neI16x8(right);
case LtSVecI16x8:
return left.ltSI16x8(right);
case LtUVecI16x8:
return left.ltUI16x8(right);
case GtSVecI16x8:
return left.gtSI16x8(right);
case GtUVecI16x8:
return left.gtUI16x8(right);
case LeSVecI16x8:
return left.leSI16x8(right);
case LeUVecI16x8:
return left.leUI16x8(right);
case GeSVecI16x8:
return left.geSI16x8(right);
case GeUVecI16x8:
return left.geUI16x8(right);
case EqVecI32x4:
return left.eqI32x4(right);
case NeVecI32x4:
return left.neI32x4(right);
case LtSVecI32x4:
return left.ltSI32x4(right);
case LtUVecI32x4:
return left.ltUI32x4(right);
case GtSVecI32x4:
return left.gtSI32x4(right);
case GtUVecI32x4:
return left.gtUI32x4(right);
case LeSVecI32x4:
return left.leSI32x4(right);
case LeUVecI32x4:
return left.leUI32x4(right);
case GeSVecI32x4:
return left.geSI32x4(right);
case GeUVecI32x4:
return left.geUI32x4(right);
case EqVecF32x4:
return left.eqF32x4(right);
case NeVecF32x4:
return left.neF32x4(right);
case LtVecF32x4:
return left.ltF32x4(right);
case GtVecF32x4:
return left.gtF32x4(right);
case LeVecF32x4:
return left.leF32x4(right);
case GeVecF32x4:
return left.geF32x4(right);
case EqVecF64x2:
return left.eqF64x2(right);
case NeVecF64x2:
return left.neF64x2(right);
case LtVecF64x2:
return left.ltF64x2(right);
case GtVecF64x2:
return left.gtF64x2(right);
case LeVecF64x2:
return left.leF64x2(right);
case GeVecF64x2:
return left.geF64x2(right);
case AndVec128:
return left.andV128(right);
case OrVec128:
return left.orV128(right);
case XorVec128:
return left.xorV128(right);
case AddVecI8x16:
return left.addI8x16(right);
case AddSatSVecI8x16:
return left.addSaturateSI8x16(right);
case AddSatUVecI8x16:
return left.addSaturateUI8x16(right);
case SubVecI8x16:
return left.subI8x16(right);
case SubSatSVecI8x16:
return left.subSaturateSI8x16(right);
case SubSatUVecI8x16:
return left.subSaturateUI8x16(right);
case MulVecI8x16:
return left.mulI8x16(right);
case AddVecI16x8:
return left.addI16x8(right);
case AddSatSVecI16x8:
return left.addSaturateSI16x8(right);
case AddSatUVecI16x8:
return left.addSaturateUI16x8(right);
case SubVecI16x8:
return left.subI16x8(right);
case SubSatSVecI16x8:
return left.subSaturateSI16x8(right);
case SubSatUVecI16x8:
return left.subSaturateUI16x8(right);
case MulVecI16x8:
return left.mulI16x8(right);
case AddVecI32x4:
return left.addI32x4(right);
case SubVecI32x4:
return left.subI32x4(right);
case MulVecI32x4:
return left.mulI32x4(right);
case AddVecI64x2:
return left.addI64x2(right);
case SubVecI64x2:
return left.subI64x2(right);
case AddVecF32x4:
return left.addF32x4(right);
case SubVecF32x4:
return left.subF32x4(right);
case MulVecF32x4:
return left.mulF32x4(right);
case DivVecF32x4:
return left.divF32x4(right);
case MinVecF32x4:
return left.minF32x4(right);
case MaxVecF32x4:
return left.maxF32x4(right);
case AddVecF64x2:
return left.addF64x2(right);
case SubVecF64x2:
return left.subF64x2(right);
case MulVecF64x2:
return left.mulF64x2(right);
case DivVecF64x2:
return left.divF64x2(right);
case MinVecF64x2:
return left.minF64x2(right);
case MaxVecF64x2:
return left.maxF64x2(right);
case InvalidBinary:
WASM_UNREACHABLE();
}
WASM_UNREACHABLE();
}
Flow visitSIMDExtract(SIMDExtract* curr) {
NOTE_ENTER("SIMDExtract");
Flow flow = this->visit(curr->vec);
if (flow.breaking()) {
return flow;
}
Literal vec = flow.value;
switch (curr->op) {
case ExtractLaneSVecI8x16:
return vec.extractLaneSI8x16(curr->index);
case ExtractLaneUVecI8x16:
return vec.extractLaneUI8x16(curr->index);
case ExtractLaneSVecI16x8:
return vec.extractLaneSI16x8(curr->index);
case ExtractLaneUVecI16x8:
return vec.extractLaneUI16x8(curr->index);
case ExtractLaneVecI32x4:
return vec.extractLaneI32x4(curr->index);
case ExtractLaneVecI64x2:
return vec.extractLaneI64x2(curr->index);
case ExtractLaneVecF32x4:
return vec.extractLaneF32x4(curr->index);
case ExtractLaneVecF64x2:
return vec.extractLaneF64x2(curr->index);
}
WASM_UNREACHABLE();
}
Flow visitSIMDReplace(SIMDReplace* curr) {
NOTE_ENTER("SIMDReplace");
Flow flow = this->visit(curr->vec);
if (flow.breaking()) {
return flow;
}
Literal vec = flow.value;
flow = this->visit(curr->value);
if (flow.breaking()) {
return flow;
}
Literal value = flow.value;
switch (curr->op) {
case ReplaceLaneVecI8x16:
return vec.replaceLaneI8x16(value, curr->index);
case ReplaceLaneVecI16x8:
return vec.replaceLaneI16x8(value, curr->index);
case ReplaceLaneVecI32x4:
return vec.replaceLaneI32x4(value, curr->index);
case ReplaceLaneVecI64x2:
return vec.replaceLaneI64x2(value, curr->index);
case ReplaceLaneVecF32x4:
return vec.replaceLaneF32x4(value, curr->index);
case ReplaceLaneVecF64x2:
return vec.replaceLaneF64x2(value, curr->index);
}
WASM_UNREACHABLE();
}
Flow visitSIMDShuffle(SIMDShuffle* curr) {
NOTE_ENTER("SIMDShuffle");
Flow flow = this->visit(curr->left);
if (flow.breaking()) {
return flow;
}
Literal left = flow.value;
flow = this->visit(curr->right);
if (flow.breaking()) {
return flow;
}
Literal right = flow.value;
return left.shuffleV8x16(right, curr->mask);
}
Flow visitSIMDBitselect(SIMDBitselect* curr) {
NOTE_ENTER("SIMDBitselect");
Flow flow = this->visit(curr->left);
if (flow.breaking()) {
return flow;
}
Literal left = flow.value;
flow = this->visit(curr->right);
if (flow.breaking()) {
return flow;
}
Literal right = flow.value;
flow = this->visit(curr->cond);
if (flow.breaking()) {
return flow;
}
Literal cond = flow.value;
return cond.bitselectV128(left, right);
}
Flow visitSIMDShift(SIMDShift* curr) {
NOTE_ENTER("SIMDShift");
Flow flow = this->visit(curr->vec);
if (flow.breaking()) {
return flow;
}
Literal vec = flow.value;
flow = this->visit(curr->shift);
if (flow.breaking()) {
return flow;
}
Literal shift = flow.value;
switch (curr->op) {
case ShlVecI8x16:
return vec.shlI8x16(shift);
case ShrSVecI8x16:
return vec.shrSI8x16(shift);
case ShrUVecI8x16:
return vec.shrUI8x16(shift);
case ShlVecI16x8:
return vec.shlI16x8(shift);
case ShrSVecI16x8:
return vec.shrSI16x8(shift);
case ShrUVecI16x8:
return vec.shrUI16x8(shift);
case ShlVecI32x4:
return vec.shlI32x4(shift);
case ShrSVecI32x4:
return vec.shrSI32x4(shift);
case ShrUVecI32x4:
return vec.shrUI32x4(shift);
case ShlVecI64x2:
return vec.shlI64x2(shift);
case ShrSVecI64x2:
return vec.shrSI64x2(shift);
case ShrUVecI64x2:
return vec.shrUI64x2(shift);
}
WASM_UNREACHABLE();
}
Flow visitSelect(Select* curr) {
NOTE_ENTER("Select");
Flow ifTrue = visit(curr->ifTrue);
if (ifTrue.breaking()) {
return ifTrue;
}
Flow ifFalse = visit(curr->ifFalse);
if (ifFalse.breaking()) {
return ifFalse;
}
Flow condition = visit(curr->condition);
if (condition.breaking()) {
return condition;
}
NOTE_EVAL1(condition.value);
return condition.value.geti32() ? ifTrue : ifFalse; // ;-)
}
Flow visitDrop(Drop* curr) {
NOTE_ENTER("Drop");
Flow value = visit(curr->value);
if (value.breaking()) {
return value;
}
return Flow();
}
Flow visitReturn(Return* curr) {
NOTE_ENTER("Return");
Flow flow;
if (curr->value) {
flow = visit(curr->value);
if (flow.breaking()) {
return flow;
}
NOTE_EVAL1(flow.value);
}
flow.breakTo = RETURN_FLOW;
return flow;
}
Flow visitNop(Nop* curr) {
NOTE_ENTER("Nop");
return Flow();
}
Flow visitUnreachable(Unreachable* curr) {
NOTE_ENTER("Unreachable");
trap("unreachable");
WASM_UNREACHABLE();
}
Literal truncSFloat(Unary* curr, Literal value) {
double val = value.getFloat();
if (std::isnan(val)) {
trap("truncSFloat of nan");
}
if (curr->type == i32) {
if (value.type == f32) {
if (!isInRangeI32TruncS(value.reinterpreti32())) {
trap("i32.truncSFloat overflow");
}
} else {
if (!isInRangeI32TruncS(value.reinterpreti64())) {
trap("i32.truncSFloat overflow");
}
}
return Literal(int32_t(val));
} else {
if (value.type == f32) {
if (!isInRangeI64TruncS(value.reinterpreti32())) {
trap("i64.truncSFloat overflow");
}
} else {
if (!isInRangeI64TruncS(value.reinterpreti64())) {
trap("i64.truncSFloat overflow");
}
}
return Literal(int64_t(val));
}