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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
|
/*
* Copyright 2016 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.
*/
#ifndef WABT_IR_H_
#define WABT_IR_H_
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include "wabt/binding-hash.h"
#include "wabt/common.h"
#include "wabt/intrusive-list.h"
#include "wabt/opcode.h"
namespace wabt {
struct Module;
enum class VarType {
Index,
Name,
};
struct Var {
explicit Var();
explicit Var(Index index, const Location& loc);
explicit Var(std::string_view name, const Location& loc);
Var(Var&&);
Var(const Var&);
Var& operator=(const Var&);
Var& operator=(Var&&);
~Var();
VarType type() const { return type_; }
bool is_index() const { return type_ == VarType::Index; }
bool is_name() const { return type_ == VarType::Name; }
Index index() const {
assert(is_index());
return index_;
}
const std::string& name() const {
assert(is_name());
return name_;
}
void set_index(Index);
void set_name(std::string&&);
void set_name(std::string_view);
Location loc;
private:
void Destroy();
VarType type_;
union {
Index index_;
std::string name_;
};
};
using VarVector = std::vector<Var>;
struct Const {
static constexpr uintptr_t kRefNullBits = ~uintptr_t(0);
Const() : Const(Type::I32, uint32_t(0)) {}
static Const I32(uint32_t val = 0, const Location& loc = Location()) {
return Const(Type::I32, val, loc);
}
static Const I64(uint64_t val = 0, const Location& loc = Location()) {
return Const(Type::I64, val, loc);
}
static Const F32(uint32_t val = 0, const Location& loc = Location()) {
return Const(Type::F32, val, loc);
}
static Const F64(uint64_t val = 0, const Location& loc = Location()) {
return Const(Type::F64, val, loc);
}
static Const V128(v128 val, const Location& loc = Location()) {
return Const(Type::V128, val, loc);
}
Type type() const { return type_; }
Type lane_type() const {
assert(type_ == Type::V128);
return lane_type_;
}
int lane_count() const {
switch (lane_type()) {
case Type::I8: return 16;
case Type::I16: return 8;
case Type::I32: return 4;
case Type::I64: return 2;
case Type::F32: return 4;
case Type::F64: return 2;
default: WABT_UNREACHABLE;
}
}
uint32_t u32() const { return data_.u32(0); }
uint64_t u64() const { return data_.u64(0); }
uint32_t f32_bits() const { return data_.f32_bits(0); }
uint64_t f64_bits() const { return data_.f64_bits(0); }
uintptr_t ref_bits() const { return data_.To<uintptr_t>(0); }
v128 vec128() const { return data_; }
template <typename T>
T v128_lane(int lane) const {
return data_.To<T>(lane);
}
void set_u32(uint32_t x) { From(Type::I32, x); }
void set_u64(uint64_t x) { From(Type::I64, x); }
void set_f32(uint32_t x) { From(Type::F32, x); }
void set_f64(uint64_t x) { From(Type::F64, x); }
void set_v128_u8(int lane, uint8_t x) { set_v128_lane(lane, Type::I8, x); }
void set_v128_u16(int lane, uint16_t x) { set_v128_lane(lane, Type::I16, x); }
void set_v128_u32(int lane, uint32_t x) { set_v128_lane(lane, Type::I32, x); }
void set_v128_u64(int lane, uint64_t x) { set_v128_lane(lane, Type::I64, x); }
void set_v128_f32(int lane, uint32_t x) { set_v128_lane(lane, Type::F32, x); }
void set_v128_f64(int lane, uint64_t x) { set_v128_lane(lane, Type::F64, x); }
// Only used for expectations. (e.g. wast assertions)
void set_f32(ExpectedNan nan) {
set_f32(0);
set_expected_nan(0, nan);
}
void set_f64(ExpectedNan nan) {
set_f64(0);
set_expected_nan(0, nan);
}
void set_funcref() { From<uintptr_t>(Type::FuncRef, 0); }
void set_externref(uintptr_t x) { From(Type::ExternRef, x); }
void set_null(Type type) { From<uintptr_t>(type, kRefNullBits); }
bool is_expected_nan(int lane = 0) const {
return expected_nan(lane) != ExpectedNan::None;
}
ExpectedNan expected_nan(int lane = 0) const {
return lane < 4 ? nan_[lane] : ExpectedNan::None;
}
void set_expected_nan(int lane, ExpectedNan nan) {
if (lane < 4) {
nan_[lane] = nan;
}
}
// v128 support
Location loc;
private:
template <typename T>
void set_v128_lane(int lane, Type lane_type, T x) {
lane_type_ = lane_type;
From(Type::V128, x, lane);
set_expected_nan(lane, ExpectedNan::None);
}
template <typename T>
Const(Type type, T data, const Location& loc = Location()) : loc(loc) {
From<T>(type, data);
}
template <typename T>
void From(Type type, T data, int lane = 0) {
static_assert(sizeof(T) <= sizeof(data_), "Invalid cast!");
assert((lane + 1) * sizeof(T) <= sizeof(data_));
type_ = type;
data_.From<T>(lane, data);
set_expected_nan(lane, ExpectedNan::None);
}
Type type_;
Type lane_type_; // Only valid if type_ == Type::V128.
v128 data_;
ExpectedNan nan_[4];
};
using ConstVector = std::vector<Const>;
enum class ExpectationType {
Values,
Either,
};
class Expectation {
public:
Expectation() = delete;
virtual ~Expectation() = default;
ExpectationType type() const { return type_; }
Location loc;
ConstVector expected;
protected:
explicit Expectation(ExpectationType type, const Location& loc = Location())
: loc(loc), type_(type) {}
private:
ExpectationType type_;
};
template <ExpectationType TypeEnum>
class ExpectationMixin : public Expectation {
public:
static bool classof(const Expectation* expectation) {
return expectation->type() == TypeEnum;
}
explicit ExpectationMixin(const Location& loc = Location())
: Expectation(TypeEnum, loc) {}
};
class ValueExpectation : public ExpectationMixin<ExpectationType::Values> {
public:
explicit ValueExpectation(const Location& loc = Location())
: ExpectationMixin<ExpectationType::Values>(loc) {}
};
struct EitherExpectation : public ExpectationMixin<ExpectationType::Either> {
public:
explicit EitherExpectation(const Location& loc = Location())
: ExpectationMixin<ExpectationType::Either>(loc) {}
};
typedef std::unique_ptr<Expectation> ExpectationPtr;
struct FuncSignature {
TypeVector param_types;
TypeVector result_types;
// Some types can have names, for example (ref $foo) has type $foo.
// So to use this type we need to translate its name into
// a proper index from the module type section.
// This is the mapping from parameter/result index to its name.
std::unordered_map<uint32_t, std::string> param_type_names;
std::unordered_map<uint32_t, std::string> result_type_names;
Index GetNumParams() const { return param_types.size(); }
Index GetNumResults() const { return result_types.size(); }
Type GetParamType(Index index) const { return param_types[index]; }
Type GetResultType(Index index) const { return result_types[index]; }
bool operator==(const FuncSignature&) const;
};
enum class TypeEntryKind {
Func,
Struct,
Array,
};
class TypeEntry {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(TypeEntry);
virtual ~TypeEntry() = default;
TypeEntryKind kind() const { return kind_; }
Location loc;
std::string name;
protected:
explicit TypeEntry(TypeEntryKind kind,
std::string_view name = std::string_view(),
const Location& loc = Location())
: loc(loc), name(name), kind_(kind) {}
TypeEntryKind kind_;
};
class FuncType : public TypeEntry {
public:
static bool classof(const TypeEntry* entry) {
return entry->kind() == TypeEntryKind::Func;
}
explicit FuncType(std::string_view name = std::string_view())
: TypeEntry(TypeEntryKind::Func, name) {}
Index GetNumParams() const { return sig.GetNumParams(); }
Index GetNumResults() const { return sig.GetNumResults(); }
Type GetParamType(Index index) const { return sig.GetParamType(index); }
Type GetResultType(Index index) const { return sig.GetResultType(index); }
FuncSignature sig;
};
struct Field {
std::string name;
Type type = Type::Void;
bool mutable_ = false;
};
class StructType : public TypeEntry {
public:
static bool classof(const TypeEntry* entry) {
return entry->kind() == TypeEntryKind::Struct;
}
explicit StructType(std::string_view name = std::string_view())
: TypeEntry(TypeEntryKind::Struct) {}
std::vector<Field> fields;
};
class ArrayType : public TypeEntry {
public:
static bool classof(const TypeEntry* entry) {
return entry->kind() == TypeEntryKind::Array;
}
explicit ArrayType(std::string_view name = std::string_view())
: TypeEntry(TypeEntryKind::Array) {}
Field field;
};
struct FuncDeclaration {
Index GetNumParams() const { return sig.GetNumParams(); }
Index GetNumResults() const { return sig.GetNumResults(); }
Type GetParamType(Index index) const { return sig.GetParamType(index); }
Type GetResultType(Index index) const { return sig.GetResultType(index); }
bool has_func_type = false;
Var type_var;
FuncSignature sig;
};
enum class ExprType {
AtomicLoad,
AtomicRmw,
AtomicRmwCmpxchg,
AtomicStore,
AtomicNotify,
AtomicFence,
AtomicWait,
Binary,
Block,
Br,
BrIf,
BrTable,
Call,
CallIndirect,
CallRef,
CodeMetadata,
Compare,
Const,
Convert,
Drop,
GlobalGet,
GlobalSet,
If,
Load,
LocalGet,
LocalSet,
LocalTee,
Loop,
MemoryCopy,
DataDrop,
MemoryFill,
MemoryGrow,
MemoryInit,
MemorySize,
Nop,
RefIsNull,
RefFunc,
RefNull,
Rethrow,
Return,
ReturnCall,
ReturnCallIndirect,
Select,
SimdLaneOp,
SimdLoadLane,
SimdStoreLane,
SimdShuffleOp,
LoadSplat,
LoadZero,
Store,
TableCopy,
ElemDrop,
TableInit,
TableGet,
TableGrow,
TableSize,
TableSet,
TableFill,
Ternary,
Throw,
Try,
Unary,
Unreachable,
First = AtomicLoad,
Last = Unreachable
};
const char* GetExprTypeName(ExprType type);
class Expr;
using ExprList = intrusive_list<Expr>;
using BlockDeclaration = FuncDeclaration;
struct Block {
Block() = default;
explicit Block(ExprList exprs) : exprs(std::move(exprs)) {}
std::string label;
BlockDeclaration decl;
ExprList exprs;
Location end_loc;
};
struct Catch {
explicit Catch(const Location& loc = Location()) : loc(loc) {}
explicit Catch(const Var& var, const Location& loc = Location())
: loc(loc), var(var) {}
Location loc;
Var var;
ExprList exprs;
bool IsCatchAll() const {
return var.is_index() && var.index() == kInvalidIndex;
}
};
using CatchVector = std::vector<Catch>;
enum class TryKind { Plain, Catch, Delegate };
class Expr : public intrusive_list_base<Expr> {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(Expr);
Expr() = delete;
virtual ~Expr() = default;
ExprType type() const { return type_; }
Location loc;
protected:
explicit Expr(ExprType type, const Location& loc = Location())
: loc(loc), type_(type) {}
ExprType type_;
};
const char* GetExprTypeName(const Expr& expr);
template <ExprType TypeEnum>
class ExprMixin : public Expr {
public:
static bool classof(const Expr* expr) { return expr->type() == TypeEnum; }
explicit ExprMixin(const Location& loc = Location()) : Expr(TypeEnum, loc) {}
};
template <ExprType TypeEnum>
class MemoryExpr : public ExprMixin<TypeEnum> {
public:
MemoryExpr(Var memidx, const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), memidx(memidx) {}
Var memidx;
};
template <ExprType TypeEnum>
class MemoryBinaryExpr : public ExprMixin<TypeEnum> {
public:
MemoryBinaryExpr(Var srcmemidx,
Var destmemidx,
const Location& loc = Location())
: ExprMixin<TypeEnum>(loc),
srcmemidx(srcmemidx),
destmemidx(destmemidx) {}
Var srcmemidx;
Var destmemidx;
};
using DropExpr = ExprMixin<ExprType::Drop>;
using NopExpr = ExprMixin<ExprType::Nop>;
using ReturnExpr = ExprMixin<ExprType::Return>;
using UnreachableExpr = ExprMixin<ExprType::Unreachable>;
using MemoryGrowExpr = MemoryExpr<ExprType::MemoryGrow>;
using MemorySizeExpr = MemoryExpr<ExprType::MemorySize>;
using MemoryFillExpr = MemoryExpr<ExprType::MemoryFill>;
using MemoryCopyExpr = MemoryBinaryExpr<ExprType::MemoryCopy>;
template <ExprType TypeEnum>
class RefTypeExpr : public ExprMixin<TypeEnum> {
public:
RefTypeExpr(Type type, const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), type(type) {}
Type type;
};
using RefNullExpr = RefTypeExpr<ExprType::RefNull>;
using RefIsNullExpr = ExprMixin<ExprType::RefIsNull>;
template <ExprType TypeEnum>
class OpcodeExpr : public ExprMixin<TypeEnum> {
public:
OpcodeExpr(Opcode opcode, const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), opcode(opcode) {}
Opcode opcode;
};
using BinaryExpr = OpcodeExpr<ExprType::Binary>;
using CompareExpr = OpcodeExpr<ExprType::Compare>;
using ConvertExpr = OpcodeExpr<ExprType::Convert>;
using UnaryExpr = OpcodeExpr<ExprType::Unary>;
using TernaryExpr = OpcodeExpr<ExprType::Ternary>;
class SimdLaneOpExpr : public ExprMixin<ExprType::SimdLaneOp> {
public:
SimdLaneOpExpr(Opcode opcode, uint64_t val, const Location& loc = Location())
: ExprMixin<ExprType::SimdLaneOp>(loc), opcode(opcode), val(val) {}
Opcode opcode;
uint64_t val;
};
class SimdLoadLaneExpr : public MemoryExpr<ExprType::SimdLoadLane> {
public:
SimdLoadLaneExpr(Opcode opcode,
Var memidx,
Address align,
Address offset,
uint64_t val,
const Location& loc = Location())
: MemoryExpr<ExprType::SimdLoadLane>(memidx, loc),
opcode(opcode),
align(align),
offset(offset),
val(val) {}
Opcode opcode;
Address align;
Address offset;
uint64_t val;
};
class SimdStoreLaneExpr : public MemoryExpr<ExprType::SimdStoreLane> {
public:
SimdStoreLaneExpr(Opcode opcode,
Var memidx,
Address align,
Address offset,
uint64_t val,
const Location& loc = Location())
: MemoryExpr<ExprType::SimdStoreLane>(memidx, loc),
opcode(opcode),
align(align),
offset(offset),
val(val) {}
Opcode opcode;
Address align;
Address offset;
uint64_t val;
};
class SimdShuffleOpExpr : public ExprMixin<ExprType::SimdShuffleOp> {
public:
SimdShuffleOpExpr(Opcode opcode, v128 val, const Location& loc = Location())
: ExprMixin<ExprType::SimdShuffleOp>(loc), opcode(opcode), val(val) {}
Opcode opcode;
v128 val;
};
template <ExprType TypeEnum>
class VarExpr : public ExprMixin<TypeEnum> {
public:
VarExpr(const Var& var, const Location& loc = Location())
: ExprMixin<TypeEnum>(loc), var(var) {}
Var var;
};
template <ExprType TypeEnum>
class MemoryVarExpr : public MemoryExpr<TypeEnum> {
public:
MemoryVarExpr(const Var& var, Var memidx, const Location& loc = Location())
: MemoryExpr<TypeEnum>(memidx, loc), var(var) {}
Var var;
};
using BrExpr = VarExpr<ExprType::Br>;
using BrIfExpr = VarExpr<ExprType::BrIf>;
using CallExpr = VarExpr<ExprType::Call>;
using RefFuncExpr = VarExpr<ExprType::RefFunc>;
using GlobalGetExpr = VarExpr<ExprType::GlobalGet>;
using GlobalSetExpr = VarExpr<ExprType::GlobalSet>;
using LocalGetExpr = VarExpr<ExprType::LocalGet>;
using LocalSetExpr = VarExpr<ExprType::LocalSet>;
using LocalTeeExpr = VarExpr<ExprType::LocalTee>;
using ReturnCallExpr = VarExpr<ExprType::ReturnCall>;
using ThrowExpr = VarExpr<ExprType::Throw>;
using RethrowExpr = VarExpr<ExprType::Rethrow>;
using DataDropExpr = VarExpr<ExprType::DataDrop>;
using ElemDropExpr = VarExpr<ExprType::ElemDrop>;
using TableGetExpr = VarExpr<ExprType::TableGet>;
using TableSetExpr = VarExpr<ExprType::TableSet>;
using TableGrowExpr = VarExpr<ExprType::TableGrow>;
using TableSizeExpr = VarExpr<ExprType::TableSize>;
using TableFillExpr = VarExpr<ExprType::TableFill>;
using MemoryInitExpr = MemoryVarExpr<ExprType::MemoryInit>;
class SelectExpr : public ExprMixin<ExprType::Select> {
public:
SelectExpr(TypeVector type, const Location& loc = Location())
: ExprMixin<ExprType::Select>(loc), result_type(type) {}
TypeVector result_type;
};
class TableInitExpr : public ExprMixin<ExprType::TableInit> {
public:
TableInitExpr(const Var& segment_index,
const Var& table_index,
const Location& loc = Location())
: ExprMixin<ExprType::TableInit>(loc),
segment_index(segment_index),
table_index(table_index) {}
Var segment_index;
Var table_index;
};
class TableCopyExpr : public ExprMixin<ExprType::TableCopy> {
public:
TableCopyExpr(const Var& dst,
const Var& src,
const Location& loc = Location())
: ExprMixin<ExprType::TableCopy>(loc), dst_table(dst), src_table(src) {}
Var dst_table;
Var src_table;
};
class CallIndirectExpr : public ExprMixin<ExprType::CallIndirect> {
public:
explicit CallIndirectExpr(const Location& loc = Location())
: ExprMixin<ExprType::CallIndirect>(loc) {}
FuncDeclaration decl;
Var table;
};
class CodeMetadataExpr : public ExprMixin<ExprType::CodeMetadata> {
public:
explicit CodeMetadataExpr(std::string_view name,
std::vector<uint8_t> data,
const Location& loc = Location())
: ExprMixin<ExprType::CodeMetadata>(loc),
name(std::move(name)),
data(std::move(data)) {}
std::string_view name;
std::vector<uint8_t> data;
};
class ReturnCallIndirectExpr : public ExprMixin<ExprType::ReturnCallIndirect> {
public:
explicit ReturnCallIndirectExpr(const Location& loc = Location())
: ExprMixin<ExprType::ReturnCallIndirect>(loc) {}
FuncDeclaration decl;
Var table;
};
class CallRefExpr : public ExprMixin<ExprType::CallRef> {
public:
explicit CallRefExpr(const Location& loc = Location())
: ExprMixin<ExprType::CallRef>(loc) {}
// This field is setup only during Validate phase,
// so keep that in mind when you use it.
Var function_type_index;
};
template <ExprType TypeEnum>
class BlockExprBase : public ExprMixin<TypeEnum> {
public:
explicit BlockExprBase(const Location& loc = Location())
: ExprMixin<TypeEnum>(loc) {}
Block block;
};
using BlockExpr = BlockExprBase<ExprType::Block>;
using LoopExpr = BlockExprBase<ExprType::Loop>;
class IfExpr : public ExprMixin<ExprType::If> {
public:
explicit IfExpr(const Location& loc = Location())
: ExprMixin<ExprType::If>(loc) {}
Block true_;
ExprList false_;
Location false_end_loc;
};
class TryExpr : public ExprMixin<ExprType::Try> {
public:
explicit TryExpr(const Location& loc = Location())
: ExprMixin<ExprType::Try>(loc), kind(TryKind::Plain) {}
TryKind kind;
Block block;
CatchVector catches;
Var delegate_target;
};
class BrTableExpr : public ExprMixin<ExprType::BrTable> {
public:
BrTableExpr(const Location& loc = Location())
: ExprMixin<ExprType::BrTable>(loc) {}
VarVector targets;
Var default_target;
};
class ConstExpr : public ExprMixin<ExprType::Const> {
public:
ConstExpr(const Const& c, const Location& loc = Location())
: ExprMixin<ExprType::Const>(loc), const_(c) {}
Const const_;
};
// TODO(binji): Rename this, it is used for more than loads/stores now.
template <ExprType TypeEnum>
class LoadStoreExpr : public MemoryExpr<TypeEnum> {
public:
LoadStoreExpr(Opcode opcode,
Var memidx,
Address align,
Address offset,
const Location& loc = Location())
: MemoryExpr<TypeEnum>(memidx, loc),
opcode(opcode),
align(align),
offset(offset) {}
Opcode opcode;
Address align;
Address offset;
};
using LoadExpr = LoadStoreExpr<ExprType::Load>;
using StoreExpr = LoadStoreExpr<ExprType::Store>;
using AtomicLoadExpr = LoadStoreExpr<ExprType::AtomicLoad>;
using AtomicStoreExpr = LoadStoreExpr<ExprType::AtomicStore>;
using AtomicRmwExpr = LoadStoreExpr<ExprType::AtomicRmw>;
using AtomicRmwCmpxchgExpr = LoadStoreExpr<ExprType::AtomicRmwCmpxchg>;
using AtomicWaitExpr = LoadStoreExpr<ExprType::AtomicWait>;
using AtomicNotifyExpr = LoadStoreExpr<ExprType::AtomicNotify>;
using LoadSplatExpr = LoadStoreExpr<ExprType::LoadSplat>;
using LoadZeroExpr = LoadStoreExpr<ExprType::LoadZero>;
class AtomicFenceExpr : public ExprMixin<ExprType::AtomicFence> {
public:
explicit AtomicFenceExpr(uint32_t consistency_model,
const Location& loc = Location())
: ExprMixin<ExprType::AtomicFence>(loc),
consistency_model(consistency_model) {}
uint32_t consistency_model;
};
struct Tag {
explicit Tag(std::string_view name) : name(name) {}
std::string name;
FuncDeclaration decl;
};
class LocalTypes {
public:
using Decl = std::pair<Type, Index>;
using Decls = std::vector<Decl>;
struct const_iterator {
const_iterator(Decls::const_iterator decl, Index index)
: decl(decl), index(index) {}
Type operator*() const { return decl->first; }
const_iterator& operator++();
const_iterator operator++(int);
Decls::const_iterator decl;
Index index;
};
void Set(const TypeVector&);
const Decls& decls() const { return decls_; }
void AppendDecl(Type type, Index count) {
if (count != 0) {
decls_.emplace_back(type, count);
}
}
Index size() const;
Type operator[](Index) const;
const_iterator begin() const { return {decls_.begin(), 0}; }
const_iterator end() const { return {decls_.end(), 0}; }
private:
Decls decls_;
};
inline LocalTypes::const_iterator& LocalTypes::const_iterator::operator++() {
++index;
if (index >= decl->second) {
++decl;
index = 0;
}
return *this;
}
inline LocalTypes::const_iterator LocalTypes::const_iterator::operator++(int) {
const_iterator result = *this;
operator++();
return result;
}
inline bool operator==(const LocalTypes::const_iterator& lhs,
const LocalTypes::const_iterator& rhs) {
return lhs.decl == rhs.decl && lhs.index == rhs.index;
}
inline bool operator!=(const LocalTypes::const_iterator& lhs,
const LocalTypes::const_iterator& rhs) {
return !operator==(lhs, rhs);
}
struct Func {
explicit Func(std::string_view name) : name(name) {}
Type GetParamType(Index index) const { return decl.GetParamType(index); }
Type GetResultType(Index index) const { return decl.GetResultType(index); }
Type GetLocalType(Index index) const;
Type GetLocalType(const Var& var) const;
Index GetNumParams() const { return decl.GetNumParams(); }
Index GetNumLocals() const { return local_types.size(); }
Index GetNumParamsAndLocals() const {
return GetNumParams() + GetNumLocals();
}
Index GetNumResults() const { return decl.GetNumResults(); }
Index GetLocalIndex(const Var&) const;
std::string name;
FuncDeclaration decl;
LocalTypes local_types;
BindingHash bindings;
ExprList exprs;
Location loc;
};
struct Global {
explicit Global(std::string_view name) : name(name) {}
std::string name;
Type type = Type::Void;
bool mutable_ = false;
ExprList init_expr;
};
struct Table {
explicit Table(std::string_view name)
: name(name), elem_type(Type::FuncRef) {}
std::string name;
Limits elem_limits;
Type elem_type;
};
using ExprListVector = std::vector<ExprList>;
struct ElemSegment {
explicit ElemSegment(std::string_view name) : name(name) {}
uint8_t GetFlags(const Module*) const;
SegmentKind kind = SegmentKind::Active;
std::string name;
Var table_var;
Type elem_type;
ExprList offset;
ExprListVector elem_exprs;
};
struct Memory {
explicit Memory(std::string_view name) : name(name) {}
std::string name;
Limits page_limits;
};
struct DataSegment {
explicit DataSegment(std::string_view name) : name(name) {}
uint8_t GetFlags(const Module*) const;
SegmentKind kind = SegmentKind::Active;
std::string name;
Var memory_var;
ExprList offset;
std::vector<uint8_t> data;
};
class Import {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(Import);
Import() = delete;
virtual ~Import() = default;
ExternalKind kind() const { return kind_; }
std::string module_name;
std::string field_name;
protected:
Import(ExternalKind kind) : kind_(kind) {}
ExternalKind kind_;
};
template <ExternalKind TypeEnum>
class ImportMixin : public Import {
public:
static bool classof(const Import* import) {
return import->kind() == TypeEnum;
}
ImportMixin() : Import(TypeEnum) {}
};
class FuncImport : public ImportMixin<ExternalKind::Func> {
public:
explicit FuncImport(std::string_view name = std::string_view())
: ImportMixin<ExternalKind::Func>(), func(name) {}
Func func;
};
class TableImport : public ImportMixin<ExternalKind::Table> {
public:
explicit TableImport(std::string_view name = std::string_view())
: ImportMixin<ExternalKind::Table>(), table(name) {}
Table table;
};
class MemoryImport : public ImportMixin<ExternalKind::Memory> {
public:
explicit MemoryImport(std::string_view name = std::string_view())
: ImportMixin<ExternalKind::Memory>(), memory(name) {}
Memory memory;
};
class GlobalImport : public ImportMixin<ExternalKind::Global> {
public:
explicit GlobalImport(std::string_view name = std::string_view())
: ImportMixin<ExternalKind::Global>(), global(name) {}
Global global;
};
class TagImport : public ImportMixin<ExternalKind::Tag> {
public:
explicit TagImport(std::string_view name = std::string_view())
: ImportMixin<ExternalKind::Tag>(), tag(name) {}
Tag tag;
};
struct Export {
std::string name;
ExternalKind kind;
Var var;
};
enum class ModuleFieldType {
Func,
Global,
Import,
Export,
Type,
Table,
ElemSegment,
Memory,
DataSegment,
Start,
Tag
};
class ModuleField : public intrusive_list_base<ModuleField> {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(ModuleField);
ModuleField() = delete;
virtual ~ModuleField() = default;
ModuleFieldType type() const { return type_; }
Location loc;
protected:
ModuleField(ModuleFieldType type, const Location& loc)
: loc(loc), type_(type) {}
ModuleFieldType type_;
};
using ModuleFieldList = intrusive_list<ModuleField>;
template <ModuleFieldType TypeEnum>
class ModuleFieldMixin : public ModuleField {
public:
static bool classof(const ModuleField* field) {
return field->type() == TypeEnum;
}
explicit ModuleFieldMixin(const Location& loc) : ModuleField(TypeEnum, loc) {}
};
class FuncModuleField : public ModuleFieldMixin<ModuleFieldType::Func> {
public:
explicit FuncModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::Func>(loc), func(name) {}
Func func;
};
class GlobalModuleField : public ModuleFieldMixin<ModuleFieldType::Global> {
public:
explicit GlobalModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::Global>(loc), global(name) {}
Global global;
};
class ImportModuleField : public ModuleFieldMixin<ModuleFieldType::Import> {
public:
explicit ImportModuleField(const Location& loc = Location())
: ModuleFieldMixin<ModuleFieldType::Import>(loc) {}
explicit ImportModuleField(std::unique_ptr<Import> import,
const Location& loc = Location())
: ModuleFieldMixin<ModuleFieldType::Import>(loc),
import(std::move(import)) {}
std::unique_ptr<Import> import;
};
class ExportModuleField : public ModuleFieldMixin<ModuleFieldType::Export> {
public:
explicit ExportModuleField(const Location& loc = Location())
: ModuleFieldMixin<ModuleFieldType::Export>(loc) {}
Export export_;
};
class TypeModuleField : public ModuleFieldMixin<ModuleFieldType::Type> {
public:
explicit TypeModuleField(const Location& loc = Location())
: ModuleFieldMixin<ModuleFieldType::Type>(loc) {}
std::unique_ptr<TypeEntry> type;
};
class TableModuleField : public ModuleFieldMixin<ModuleFieldType::Table> {
public:
explicit TableModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::Table>(loc), table(name) {}
Table table;
};
class ElemSegmentModuleField
: public ModuleFieldMixin<ModuleFieldType::ElemSegment> {
public:
explicit ElemSegmentModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::ElemSegment>(loc),
elem_segment(name) {}
ElemSegment elem_segment;
};
class MemoryModuleField : public ModuleFieldMixin<ModuleFieldType::Memory> {
public:
explicit MemoryModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::Memory>(loc), memory(name) {}
Memory memory;
};
class DataSegmentModuleField
: public ModuleFieldMixin<ModuleFieldType::DataSegment> {
public:
explicit DataSegmentModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::DataSegment>(loc),
data_segment(name) {}
DataSegment data_segment;
};
class TagModuleField : public ModuleFieldMixin<ModuleFieldType::Tag> {
public:
explicit TagModuleField(const Location& loc = Location(),
std::string_view name = std::string_view())
: ModuleFieldMixin<ModuleFieldType::Tag>(loc), tag(name) {}
Tag tag;
};
class StartModuleField : public ModuleFieldMixin<ModuleFieldType::Start> {
public:
explicit StartModuleField(Var start = Var(), const Location& loc = Location())
: ModuleFieldMixin<ModuleFieldType::Start>(loc), start(start) {}
Var start;
};
struct Module {
Index GetFuncTypeIndex(const Var&) const;
Index GetFuncTypeIndex(const FuncDeclaration&) const;
Index GetFuncTypeIndex(const FuncSignature&) const;
const FuncType* GetFuncType(const Var&) const;
FuncType* GetFuncType(const Var&);
Index GetFuncIndex(const Var&) const;
const Func* GetFunc(const Var&) const;
Func* GetFunc(const Var&);
Index GetTableIndex(const Var&) const;
const Table* GetTable(const Var&) const;
Table* GetTable(const Var&);
Index GetMemoryIndex(const Var&) const;
const Memory* GetMemory(const Var&) const;
Memory* GetMemory(const Var&);
Index GetGlobalIndex(const Var&) const;
const Global* GetGlobal(const Var&) const;
Global* GetGlobal(const Var&);
const Export* GetExport(std::string_view) const;
Tag* GetTag(const Var&) const;
Index GetTagIndex(const Var&) const;
const DataSegment* GetDataSegment(const Var&) const;
DataSegment* GetDataSegment(const Var&);
Index GetDataSegmentIndex(const Var&) const;
const ElemSegment* GetElemSegment(const Var&) const;
ElemSegment* GetElemSegment(const Var&);
Index GetElemSegmentIndex(const Var&) const;
bool IsImport(ExternalKind kind, const Var&) const;
bool IsImport(const Export& export_) const {
return IsImport(export_.kind, export_.var);
}
// TODO(binji): move this into a builder class?
void AppendField(std::unique_ptr<DataSegmentModuleField>);
void AppendField(std::unique_ptr<ElemSegmentModuleField>);
void AppendField(std::unique_ptr<TagModuleField>);
void AppendField(std::unique_ptr<ExportModuleField>);
void AppendField(std::unique_ptr<FuncModuleField>);
void AppendField(std::unique_ptr<TypeModuleField>);
void AppendField(std::unique_ptr<GlobalModuleField>);
void AppendField(std::unique_ptr<ImportModuleField>);
void AppendField(std::unique_ptr<MemoryModuleField>);
void AppendField(std::unique_ptr<StartModuleField>);
void AppendField(std::unique_ptr<TableModuleField>);
void AppendField(std::unique_ptr<ModuleField>);
void AppendFields(ModuleFieldList*);
Location loc;
std::string name;
ModuleFieldList fields;
Index num_tag_imports = 0;
Index num_func_imports = 0;
Index num_table_imports = 0;
Index num_memory_imports = 0;
Index num_global_imports = 0;
// Cached for convenience; the pointers are shared with values that are
// stored in either ModuleField or Import.
std::vector<Tag*> tags;
std::vector<Func*> funcs;
std::vector<Global*> globals;
std::vector<Import*> imports;
std::vector<Export*> exports;
std::vector<TypeEntry*> types;
std::vector<Table*> tables;
std::vector<ElemSegment*> elem_segments;
std::vector<Memory*> memories;
std::vector<DataSegment*> data_segments;
std::vector<Var*> starts;
BindingHash tag_bindings;
BindingHash func_bindings;
BindingHash global_bindings;
BindingHash export_bindings;
BindingHash type_bindings;
BindingHash table_bindings;
BindingHash memory_bindings;
BindingHash data_segment_bindings;
BindingHash elem_segment_bindings;
};
enum class ScriptModuleType {
Text,
Binary,
Quoted,
};
// A ScriptModule is a module that may not yet be decoded. This allows for text
// and binary parsing errors to be deferred until validation time.
class ScriptModule {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(ScriptModule);
ScriptModule() = delete;
virtual ~ScriptModule() = default;
ScriptModuleType type() const { return type_; }
virtual const Location& location() const = 0;
protected:
explicit ScriptModule(ScriptModuleType type) : type_(type) {}
ScriptModuleType type_;
};
template <ScriptModuleType TypeEnum>
class ScriptModuleMixin : public ScriptModule {
public:
static bool classof(const ScriptModule* script_module) {
return script_module->type() == TypeEnum;
}
ScriptModuleMixin() : ScriptModule(TypeEnum) {}
};
class TextScriptModule : public ScriptModuleMixin<ScriptModuleType::Text> {
public:
const Location& location() const override { return module.loc; }
Module module;
};
template <ScriptModuleType TypeEnum>
class DataScriptModule : public ScriptModuleMixin<TypeEnum> {
public:
const Location& location() const override { return loc; }
Location loc;
std::string name;
std::vector<uint8_t> data;
};
using BinaryScriptModule = DataScriptModule<ScriptModuleType::Binary>;
using QuotedScriptModule = DataScriptModule<ScriptModuleType::Quoted>;
enum class ActionType {
Invoke,
Get,
};
class Action {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(Action);
Action() = delete;
virtual ~Action() = default;
ActionType type() const { return type_; }
Location loc;
Var module_var;
std::string name;
protected:
explicit Action(ActionType type, const Location& loc = Location())
: loc(loc), type_(type) {}
ActionType type_;
};
using ActionPtr = std::unique_ptr<Action>;
template <ActionType TypeEnum>
class ActionMixin : public Action {
public:
static bool classof(const Action* action) {
return action->type() == TypeEnum;
}
explicit ActionMixin(const Location& loc = Location())
: Action(TypeEnum, loc) {}
};
class GetAction : public ActionMixin<ActionType::Get> {
public:
explicit GetAction(const Location& loc = Location())
: ActionMixin<ActionType::Get>(loc) {}
};
class InvokeAction : public ActionMixin<ActionType::Invoke> {
public:
explicit InvokeAction(const Location& loc = Location())
: ActionMixin<ActionType::Invoke>(loc) {}
ConstVector args;
};
enum class CommandType {
Module,
ScriptModule,
Action,
Register,
AssertMalformed,
AssertInvalid,
AssertUnlinkable,
AssertUninstantiable,
AssertReturn,
AssertTrap,
AssertExhaustion,
AssertException,
First = Module,
Last = AssertException,
};
constexpr int kCommandTypeCount = WABT_ENUM_COUNT(CommandType);
class Command {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(Command);
Command() = delete;
virtual ~Command() = default;
CommandType type;
protected:
explicit Command(CommandType type) : type(type) {}
};
template <CommandType TypeEnum>
class CommandMixin : public Command {
public:
static bool classof(const Command* cmd) { return cmd->type == TypeEnum; }
CommandMixin() : Command(TypeEnum) {}
};
class ModuleCommand : public CommandMixin<CommandType::Module> {
public:
Module module;
};
class ScriptModuleCommand : public CommandMixin<CommandType::ScriptModule> {
public:
// Both the module and the script_module need to be stored since the module
// has the parsed information about the module, but the script_module has the
// original contents (binary or quoted).
Module module;
std::unique_ptr<ScriptModule> script_module;
};
template <CommandType TypeEnum>
class ActionCommandBase : public CommandMixin<TypeEnum> {
public:
ActionPtr action;
};
using ActionCommand = ActionCommandBase<CommandType::Action>;
class RegisterCommand : public CommandMixin<CommandType::Register> {
public:
RegisterCommand(std::string_view module_name, const Var& var)
: module_name(module_name), var(var) {}
std::string module_name;
Var var;
};
class AssertReturnCommand : public CommandMixin<CommandType::AssertReturn> {
public:
ActionPtr action;
ExpectationPtr expected;
};
template <CommandType TypeEnum>
class AssertTrapCommandBase : public CommandMixin<TypeEnum> {
public:
ActionPtr action;
std::string text;
};
using AssertTrapCommand = AssertTrapCommandBase<CommandType::AssertTrap>;
using AssertExhaustionCommand =
AssertTrapCommandBase<CommandType::AssertExhaustion>;
template <CommandType TypeEnum>
class AssertModuleCommand : public CommandMixin<TypeEnum> {
public:
std::unique_ptr<ScriptModule> module;
std::string text;
};
using AssertMalformedCommand =
AssertModuleCommand<CommandType::AssertMalformed>;
using AssertInvalidCommand = AssertModuleCommand<CommandType::AssertInvalid>;
using AssertUnlinkableCommand =
AssertModuleCommand<CommandType::AssertUnlinkable>;
using AssertUninstantiableCommand =
AssertModuleCommand<CommandType::AssertUninstantiable>;
class AssertExceptionCommand
: public CommandMixin<CommandType::AssertException> {
public:
ActionPtr action;
};
using CommandPtr = std::unique_ptr<Command>;
using CommandPtrVector = std::vector<CommandPtr>;
struct Script {
WABT_DISALLOW_COPY_AND_ASSIGN(Script);
Script() = default;
const Module* GetFirstModule() const;
Module* GetFirstModule();
const Module* GetModule(const Var&) const;
CommandPtrVector commands;
BindingHash module_bindings;
};
void MakeTypeBindingReverseMapping(
size_t num_types,
const BindingHash& bindings,
std::vector<std::string>* out_reverse_mapping);
} // namespace wabt
#endif /* WABT_IR_H_ */
|