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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
*
* Copyright 2015 Mozilla Foundation
*
* 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 wasmast_h
#define wasmast_h
#include "ds/LifoAlloc.h"
#include "js/HashTable.h"
#include "js/Vector.h"
#include "wasm/WasmTypes.h"
namespace js {
namespace wasm {
const uint32_t AstNoIndex = UINT32_MAX;
const unsigned AST_LIFO_DEFAULT_CHUNK_SIZE = 4096;
/*****************************************************************************/
// wasm AST
class AstExpr;
template <class T>
using AstVector = mozilla::Vector<T, 0, LifoAllocPolicy<Fallible>>;
template <class K, class V, class HP>
using AstHashMap = HashMap<K, V, HP, LifoAllocPolicy<Fallible>>;
class AstName
{
const char16_t* begin_;
const char16_t* end_;
public:
template <size_t Length>
explicit AstName(const char16_t (&str)[Length]) : begin_(str), end_(str + Length - 1) {
MOZ_ASSERT(str[Length - 1] == u'\0');
}
AstName(const char16_t* begin, size_t length) : begin_(begin), end_(begin + length) {}
AstName() : begin_(nullptr), end_(nullptr) {}
const char16_t* begin() const { return begin_; }
const char16_t* end() const { return end_; }
size_t length() const { return end_ - begin_; }
bool empty() const { return begin_ == nullptr; }
bool operator==(AstName rhs) const {
if (length() != rhs.length())
return false;
if (begin() == rhs.begin())
return true;
return EqualChars(begin(), rhs.begin(), length());
}
bool operator!=(AstName rhs) const {
return !(*this == rhs);
}
};
class AstRef
{
AstName name_;
uint32_t index_;
public:
AstRef() : index_(AstNoIndex) {
MOZ_ASSERT(isInvalid());
}
explicit AstRef(AstName name) : name_(name), index_(AstNoIndex) {
MOZ_ASSERT(!isInvalid());
}
explicit AstRef(uint32_t index) : index_(index) {
MOZ_ASSERT(!isInvalid());
}
bool isInvalid() const {
return name_.empty() && index_ == AstNoIndex;
}
AstName name() const {
return name_;
}
size_t index() const {
MOZ_ASSERT(index_ != AstNoIndex);
return index_;
}
void setIndex(uint32_t index) {
MOZ_ASSERT(index_ == AstNoIndex);
index_ = index;
}
};
struct AstNameHasher
{
typedef const AstName Lookup;
static js::HashNumber hash(Lookup l) {
return mozilla::HashString(l.begin(), l.length());
}
static bool match(const AstName key, Lookup lookup) {
return key == lookup;
}
};
using AstNameMap = AstHashMap<AstName, uint32_t, AstNameHasher>;
typedef AstVector<ValType> AstValTypeVector;
typedef AstVector<AstExpr*> AstExprVector;
typedef AstVector<AstName> AstNameVector;
typedef AstVector<AstRef> AstRefVector;
struct AstBase
{
void* operator new(size_t numBytes, LifoAlloc& astLifo) throw() {
return astLifo.alloc(numBytes);
}
};
class AstSig : public AstBase
{
AstName name_;
AstValTypeVector args_;
ExprType ret_;
public:
explicit AstSig(LifoAlloc& lifo)
: args_(lifo),
ret_(ExprType::Void)
{}
AstSig(AstValTypeVector&& args, ExprType ret)
: args_(Move(args)),
ret_(ret)
{}
AstSig(AstName name, AstSig&& rhs)
: name_(name),
args_(Move(rhs.args_)),
ret_(rhs.ret_)
{}
const AstValTypeVector& args() const {
return args_;
}
ExprType ret() const {
return ret_;
}
AstName name() const {
return name_;
}
bool operator==(const AstSig& rhs) const {
return ret() == rhs.ret() && EqualContainers(args(), rhs.args());
}
typedef const AstSig& Lookup;
static HashNumber hash(Lookup sig) {
return AddContainerToHash(sig.args(), HashNumber(sig.ret()));
}
static bool match(const AstSig* lhs, Lookup rhs) {
return *lhs == rhs;
}
};
const uint32_t AstNodeUnknownOffset = 0;
class AstNode : public AstBase
{
uint32_t offset_; // if applicable, offset in the binary format file
public:
AstNode() : offset_(AstNodeUnknownOffset) {}
uint32_t offset() const { return offset_; }
void setOffset(uint32_t offset) { offset_ = offset; }
};
enum class AstExprKind
{
BinaryOperator,
Block,
Branch,
BranchTable,
Call,
CallIndirect,
ComparisonOperator,
Const,
ConversionOperator,
CurrentMemory,
Drop,
First,
GetGlobal,
GetLocal,
GrowMemory,
If,
Load,
Nop,
Pop,
Return,
SetGlobal,
SetLocal,
TeeLocal,
Store,
TernaryOperator,
UnaryOperator,
Unreachable
};
class AstExpr : public AstNode
{
const AstExprKind kind_;
ExprType type_;
protected:
AstExpr(AstExprKind kind, ExprType type)
: kind_(kind), type_(type)
{}
public:
AstExprKind kind() const { return kind_; }
bool isVoid() const { return IsVoid(type_); }
// Note that for nodes other than blocks and block-like things, this
// may return ExprType::Limit for nodes with non-void types.
ExprType type() const { return type_; }
template <class T>
T& as() {
MOZ_ASSERT(kind() == T::Kind);
return static_cast<T&>(*this);
}
};
struct AstNop : AstExpr
{
static const AstExprKind Kind = AstExprKind::Nop;
AstNop()
: AstExpr(AstExprKind::Nop, ExprType::Void)
{}
};
struct AstUnreachable : AstExpr
{
static const AstExprKind Kind = AstExprKind::Unreachable;
AstUnreachable()
: AstExpr(AstExprKind::Unreachable, ExprType::Void)
{}
};
class AstDrop : public AstExpr
{
AstExpr& value_;
public:
static const AstExprKind Kind = AstExprKind::Drop;
explicit AstDrop(AstExpr& value)
: AstExpr(AstExprKind::Drop, ExprType::Void),
value_(value)
{}
AstExpr& value() const {
return value_;
}
};
class AstConst : public AstExpr
{
const Val val_;
public:
static const AstExprKind Kind = AstExprKind::Const;
explicit AstConst(Val val)
: AstExpr(Kind, ExprType::Limit),
val_(val)
{}
Val val() const { return val_; }
};
class AstGetLocal : public AstExpr
{
AstRef local_;
public:
static const AstExprKind Kind = AstExprKind::GetLocal;
explicit AstGetLocal(AstRef local)
: AstExpr(Kind, ExprType::Limit),
local_(local)
{}
AstRef& local() {
return local_;
}
};
class AstSetLocal : public AstExpr
{
AstRef local_;
AstExpr& value_;
public:
static const AstExprKind Kind = AstExprKind::SetLocal;
AstSetLocal(AstRef local, AstExpr& value)
: AstExpr(Kind, ExprType::Void),
local_(local),
value_(value)
{}
AstRef& local() {
return local_;
}
AstExpr& value() const {
return value_;
}
};
class AstGetGlobal : public AstExpr
{
AstRef global_;
public:
static const AstExprKind Kind = AstExprKind::GetGlobal;
explicit AstGetGlobal(AstRef global)
: AstExpr(Kind, ExprType::Limit),
global_(global)
{}
AstRef& global() {
return global_;
}
};
class AstSetGlobal : public AstExpr
{
AstRef global_;
AstExpr& value_;
public:
static const AstExprKind Kind = AstExprKind::SetGlobal;
AstSetGlobal(AstRef global, AstExpr& value)
: AstExpr(Kind, ExprType::Void),
global_(global),
value_(value)
{}
AstRef& global() {
return global_;
}
AstExpr& value() const {
return value_;
}
};
class AstTeeLocal : public AstExpr
{
AstRef local_;
AstExpr& value_;
public:
static const AstExprKind Kind = AstExprKind::TeeLocal;
AstTeeLocal(AstRef local, AstExpr& value)
: AstExpr(Kind, ExprType::Limit),
local_(local),
value_(value)
{}
AstRef& local() {
return local_;
}
AstExpr& value() const {
return value_;
}
};
class AstBlock : public AstExpr
{
Op op_;
AstName name_;
AstExprVector exprs_;
public:
static const AstExprKind Kind = AstExprKind::Block;
explicit AstBlock(Op op, ExprType type, AstName name, AstExprVector&& exprs)
: AstExpr(Kind, type),
op_(op),
name_(name),
exprs_(Move(exprs))
{}
Op op() const { return op_; }
AstName name() const { return name_; }
const AstExprVector& exprs() const { return exprs_; }
};
class AstBranch : public AstExpr
{
Op op_;
AstExpr* cond_;
AstRef target_;
AstExpr* value_;
public:
static const AstExprKind Kind = AstExprKind::Branch;
explicit AstBranch(Op op, ExprType type,
AstExpr* cond, AstRef target, AstExpr* value)
: AstExpr(Kind, type),
op_(op),
cond_(cond),
target_(target),
value_(value)
{}
Op op() const { return op_; }
AstRef& target() { return target_; }
AstExpr& cond() const { MOZ_ASSERT(cond_); return *cond_; }
AstExpr* maybeValue() const { return value_; }
};
class AstCall : public AstExpr
{
Op op_;
AstRef func_;
AstExprVector args_;
public:
static const AstExprKind Kind = AstExprKind::Call;
AstCall(Op op, ExprType type, AstRef func, AstExprVector&& args)
: AstExpr(Kind, type), op_(op), func_(func), args_(Move(args))
{}
Op op() const { return op_; }
AstRef& func() { return func_; }
const AstExprVector& args() const { return args_; }
};
class AstCallIndirect : public AstExpr
{
AstRef sig_;
AstExprVector args_;
AstExpr* index_;
public:
static const AstExprKind Kind = AstExprKind::CallIndirect;
AstCallIndirect(AstRef sig, ExprType type, AstExprVector&& args, AstExpr* index)
: AstExpr(Kind, type), sig_(sig), args_(Move(args)), index_(index)
{}
AstRef& sig() { return sig_; }
const AstExprVector& args() const { return args_; }
AstExpr* index() const { return index_; }
};
class AstReturn : public AstExpr
{
AstExpr* maybeExpr_;
public:
static const AstExprKind Kind = AstExprKind::Return;
explicit AstReturn(AstExpr* maybeExpr)
: AstExpr(Kind, ExprType::Void),
maybeExpr_(maybeExpr)
{}
AstExpr* maybeExpr() const { return maybeExpr_; }
};
class AstIf : public AstExpr
{
AstExpr* cond_;
AstName name_;
AstExprVector thenExprs_;
AstExprVector elseExprs_;
public:
static const AstExprKind Kind = AstExprKind::If;
AstIf(ExprType type, AstExpr* cond, AstName name,
AstExprVector&& thenExprs, AstExprVector&& elseExprs)
: AstExpr(Kind, type),
cond_(cond),
name_(name),
thenExprs_(Move(thenExprs)),
elseExprs_(Move(elseExprs))
{}
AstExpr& cond() const { return *cond_; }
const AstExprVector& thenExprs() const { return thenExprs_; }
bool hasElse() const { return elseExprs_.length(); }
const AstExprVector& elseExprs() const { MOZ_ASSERT(hasElse()); return elseExprs_; }
AstName name() const { return name_; }
};
class AstLoadStoreAddress
{
AstExpr* base_;
int32_t flags_;
int32_t offset_;
public:
explicit AstLoadStoreAddress(AstExpr* base, int32_t flags, int32_t offset)
: base_(base),
flags_(flags),
offset_(offset)
{}
AstExpr& base() const { return *base_; }
int32_t flags() const { return flags_; }
int32_t offset() const { return offset_; }
};
class AstLoad : public AstExpr
{
Op op_;
AstLoadStoreAddress address_;
public:
static const AstExprKind Kind = AstExprKind::Load;
explicit AstLoad(Op op, const AstLoadStoreAddress &address)
: AstExpr(Kind, ExprType::Limit),
op_(op),
address_(address)
{}
Op op() const { return op_; }
const AstLoadStoreAddress& address() const { return address_; }
};
class AstStore : public AstExpr
{
Op op_;
AstLoadStoreAddress address_;
AstExpr* value_;
public:
static const AstExprKind Kind = AstExprKind::Store;
explicit AstStore(Op op, const AstLoadStoreAddress &address, AstExpr* value)
: AstExpr(Kind, ExprType::Void),
op_(op),
address_(address),
value_(value)
{}
Op op() const { return op_; }
const AstLoadStoreAddress& address() const { return address_; }
AstExpr& value() const { return *value_; }
};
class AstCurrentMemory final : public AstExpr
{
public:
static const AstExprKind Kind = AstExprKind::CurrentMemory;
explicit AstCurrentMemory()
: AstExpr(Kind, ExprType::I32)
{}
};
class AstGrowMemory final : public AstExpr
{
AstExpr* operand_;
public:
static const AstExprKind Kind = AstExprKind::GrowMemory;
explicit AstGrowMemory(AstExpr* operand)
: AstExpr(Kind, ExprType::I32), operand_(operand)
{}
AstExpr* operand() const { return operand_; }
};
class AstBranchTable : public AstExpr
{
AstExpr& index_;
AstRef default_;
AstRefVector table_;
AstExpr* value_;
public:
static const AstExprKind Kind = AstExprKind::BranchTable;
explicit AstBranchTable(AstExpr& index, AstRef def, AstRefVector&& table,
AstExpr* maybeValue)
: AstExpr(Kind, ExprType::Void),
index_(index),
default_(def),
table_(Move(table)),
value_(maybeValue)
{}
AstExpr& index() const { return index_; }
AstRef& def() { return default_; }
AstRefVector& table() { return table_; }
AstExpr* maybeValue() { return value_; }
};
class AstFunc : public AstNode
{
AstName name_;
AstRef sig_;
AstValTypeVector vars_;
AstNameVector localNames_;
AstExprVector body_;
public:
AstFunc(AstName name, AstRef sig, AstValTypeVector&& vars,
AstNameVector&& locals, AstExprVector&& body)
: name_(name),
sig_(sig),
vars_(Move(vars)),
localNames_(Move(locals)),
body_(Move(body))
{}
AstRef& sig() { return sig_; }
const AstValTypeVector& vars() const { return vars_; }
const AstNameVector& locals() const { return localNames_; }
const AstExprVector& body() const { return body_; }
AstName name() const { return name_; }
};
class AstGlobal : public AstNode
{
AstName name_;
bool isMutable_;
ValType type_;
Maybe<AstExpr*> init_;
public:
AstGlobal() : isMutable_(false), type_(ValType(TypeCode::Limit))
{}
explicit AstGlobal(AstName name, ValType type, bool isMutable,
Maybe<AstExpr*> init = Maybe<AstExpr*>())
: name_(name), isMutable_(isMutable), type_(type), init_(init)
{}
AstName name() const { return name_; }
bool isMutable() const { return isMutable_; }
ValType type() const { return type_; }
bool hasInit() const { return !!init_; }
AstExpr& init() const { MOZ_ASSERT(hasInit()); return **init_; }
};
typedef AstVector<AstGlobal*> AstGlobalVector;
class AstImport : public AstNode
{
AstName name_;
AstName module_;
AstName field_;
DefinitionKind kind_;
AstRef funcSig_;
Limits limits_;
AstGlobal global_;
public:
AstImport(AstName name, AstName module, AstName field, AstRef funcSig)
: name_(name), module_(module), field_(field), kind_(DefinitionKind::Function), funcSig_(funcSig)
{}
AstImport(AstName name, AstName module, AstName field, DefinitionKind kind, Limits limits)
: name_(name), module_(module), field_(field), kind_(kind), limits_(limits)
{}
AstImport(AstName name, AstName module, AstName field, AstGlobal global)
: name_(name), module_(module), field_(field), kind_(DefinitionKind::Global), global_(global)
{}
AstName name() const { return name_; }
AstName module() const { return module_; }
AstName field() const { return field_; }
DefinitionKind kind() const { return kind_; }
AstRef& funcSig() {
MOZ_ASSERT(kind_ == DefinitionKind::Function);
return funcSig_;
}
Limits limits() const {
MOZ_ASSERT(kind_ == DefinitionKind::Memory || kind_ == DefinitionKind::Table);
return limits_;
}
const AstGlobal& global() const {
MOZ_ASSERT(kind_ == DefinitionKind::Global);
return global_;
}
};
class AstExport : public AstNode
{
AstName name_;
DefinitionKind kind_;
AstRef ref_;
public:
AstExport(AstName name, DefinitionKind kind, AstRef ref)
: name_(name), kind_(kind), ref_(ref)
{}
explicit AstExport(AstName name, DefinitionKind kind)
: name_(name), kind_(kind)
{}
AstName name() const { return name_; }
DefinitionKind kind() const { return kind_; }
AstRef& ref() { return ref_; }
};
class AstDataSegment : public AstNode
{
AstExpr* offset_;
AstNameVector fragments_;
public:
AstDataSegment(AstExpr* offset, AstNameVector&& fragments)
: offset_(offset), fragments_(Move(fragments))
{}
AstExpr* offset() const { return offset_; }
const AstNameVector& fragments() const { return fragments_; }
};
typedef AstVector<AstDataSegment*> AstDataSegmentVector;
class AstElemSegment : public AstNode
{
AstExpr* offset_;
AstRefVector elems_;
public:
AstElemSegment(AstExpr* offset, AstRefVector&& elems)
: offset_(offset), elems_(Move(elems))
{}
AstExpr* offset() const { return offset_; }
AstRefVector& elems() { return elems_; }
const AstRefVector& elems() const { return elems_; }
};
typedef AstVector<AstElemSegment*> AstElemSegmentVector;
class AstStartFunc : public AstNode
{
AstRef func_;
public:
explicit AstStartFunc(AstRef func)
: func_(func)
{}
AstRef& func() {
return func_;
}
};
struct AstResizable
{
AstName name;
Limits limits;
bool imported;
AstResizable(Limits limits, bool imported, AstName name = AstName())
: name(name),
limits(limits),
imported(imported)
{}
};
class AstModule : public AstNode
{
public:
typedef AstVector<AstFunc*> FuncVector;
typedef AstVector<AstImport*> ImportVector;
typedef AstVector<AstExport*> ExportVector;
typedef AstVector<AstSig*> SigVector;
typedef AstVector<AstName> NameVector;
typedef AstVector<AstResizable> AstResizableVector;
private:
typedef AstHashMap<AstSig*, uint32_t, AstSig> SigMap;
LifoAlloc& lifo_;
SigVector sigs_;
SigMap sigMap_;
ImportVector imports_;
NameVector funcImportNames_;
AstResizableVector tables_;
AstResizableVector memories_;
ExportVector exports_;
Maybe<AstStartFunc> startFunc_;
FuncVector funcs_;
AstDataSegmentVector dataSegments_;
AstElemSegmentVector elemSegments_;
AstGlobalVector globals_;
public:
explicit AstModule(LifoAlloc& lifo)
: lifo_(lifo),
sigs_(lifo),
sigMap_(lifo),
imports_(lifo),
funcImportNames_(lifo),
tables_(lifo),
memories_(lifo),
exports_(lifo),
funcs_(lifo),
dataSegments_(lifo),
elemSegments_(lifo),
globals_(lifo)
{}
bool init() {
return sigMap_.init();
}
bool addMemory(AstName name, Limits memory) {
return memories_.append(AstResizable(memory, false, name));
}
bool hasMemory() const {
return !!memories_.length();
}
const AstResizableVector& memories() const {
return memories_;
}
bool addTable(AstName name, Limits table) {
return tables_.append(AstResizable(table, false, name));
}
bool hasTable() const {
return !!tables_.length();
}
const AstResizableVector& tables() const {
return tables_;
}
bool append(AstDataSegment* seg) {
return dataSegments_.append(seg);
}
const AstDataSegmentVector& dataSegments() const {
return dataSegments_;
}
bool append(AstElemSegment* seg) {
return elemSegments_.append(seg);
}
const AstElemSegmentVector& elemSegments() const {
return elemSegments_;
}
bool hasStartFunc() const {
return !!startFunc_;
}
bool setStartFunc(AstStartFunc startFunc) {
if (startFunc_)
return false;
startFunc_.emplace(startFunc);
return true;
}
AstStartFunc& startFunc() {
return *startFunc_;
}
bool declare(AstSig&& sig, uint32_t* sigIndex) {
SigMap::AddPtr p = sigMap_.lookupForAdd(sig);
if (p) {
*sigIndex = p->value();
return true;
}
*sigIndex = sigs_.length();
auto* lifoSig = new (lifo_) AstSig(AstName(), Move(sig));
return lifoSig &&
sigs_.append(lifoSig) &&
sigMap_.add(p, sigs_.back(), *sigIndex);
}
bool append(AstSig* sig) {
uint32_t sigIndex = sigs_.length();
if (!sigs_.append(sig))
return false;
SigMap::AddPtr p = sigMap_.lookupForAdd(*sig);
return p || sigMap_.add(p, sig, sigIndex);
}
const SigVector& sigs() const {
return sigs_;
}
bool append(AstFunc* func) {
return funcs_.append(func);
}
const FuncVector& funcs() const {
return funcs_;
}
bool append(AstImport* imp) {
switch (imp->kind()) {
case DefinitionKind::Function:
if (!funcImportNames_.append(imp->name()))
return false;
break;
case DefinitionKind::Table:
if (!tables_.append(AstResizable(imp->limits(), true)))
return false;
break;
case DefinitionKind::Memory:
if (!memories_.append(AstResizable(imp->limits(), true)))
return false;
break;
case DefinitionKind::Global:
break;
}
return imports_.append(imp);
}
const ImportVector& imports() const {
return imports_;
}
const NameVector& funcImportNames() const {
return funcImportNames_;
}
size_t numFuncImports() const {
return funcImportNames_.length();
}
bool append(AstExport* exp) {
return exports_.append(exp);
}
const ExportVector& exports() const {
return exports_;
}
bool append(AstGlobal* glob) {
return globals_.append(glob);
}
const AstGlobalVector& globals() const {
return globals_;
}
};
class AstUnaryOperator final : public AstExpr
{
Op op_;
AstExpr* operand_;
public:
static const AstExprKind Kind = AstExprKind::UnaryOperator;
explicit AstUnaryOperator(Op op, AstExpr* operand)
: AstExpr(Kind, ExprType::Limit),
op_(op), operand_(operand)
{}
Op op() const { return op_; }
AstExpr* operand() const { return operand_; }
};
class AstBinaryOperator final : public AstExpr
{
Op op_;
AstExpr* lhs_;
AstExpr* rhs_;
public:
static const AstExprKind Kind = AstExprKind::BinaryOperator;
explicit AstBinaryOperator(Op op, AstExpr* lhs, AstExpr* rhs)
: AstExpr(Kind, ExprType::Limit),
op_(op), lhs_(lhs), rhs_(rhs)
{}
Op op() const { return op_; }
AstExpr* lhs() const { return lhs_; }
AstExpr* rhs() const { return rhs_; }
};
class AstTernaryOperator : public AstExpr
{
Op op_;
AstExpr* op0_;
AstExpr* op1_;
AstExpr* op2_;
public:
static const AstExprKind Kind = AstExprKind::TernaryOperator;
AstTernaryOperator(Op op, AstExpr* op0, AstExpr* op1, AstExpr* op2)
: AstExpr(Kind, ExprType::Limit),
op_(op), op0_(op0), op1_(op1), op2_(op2)
{}
Op op() const { return op_; }
AstExpr* op0() const { return op0_; }
AstExpr* op1() const { return op1_; }
AstExpr* op2() const { return op2_; }
};
class AstComparisonOperator final : public AstExpr
{
Op op_;
AstExpr* lhs_;
AstExpr* rhs_;
public:
static const AstExprKind Kind = AstExprKind::ComparisonOperator;
explicit AstComparisonOperator(Op op, AstExpr* lhs, AstExpr* rhs)
: AstExpr(Kind, ExprType::Limit),
op_(op), lhs_(lhs), rhs_(rhs)
{}
Op op() const { return op_; }
AstExpr* lhs() const { return lhs_; }
AstExpr* rhs() const { return rhs_; }
};
class AstConversionOperator final : public AstExpr
{
Op op_;
AstExpr* operand_;
public:
static const AstExprKind Kind = AstExprKind::ConversionOperator;
explicit AstConversionOperator(Op op, AstExpr* operand)
: AstExpr(Kind, ExprType::Limit),
op_(op), operand_(operand)
{}
Op op() const { return op_; }
AstExpr* operand() const { return operand_; }
};
// This is an artificial AST node which can fill operand slots in an AST
// constructed from parsing or decoding stack-machine code that doesn't have
// an inherent AST structure.
class AstPop final : public AstExpr
{
public:
static const AstExprKind Kind = AstExprKind::Pop;
AstPop()
: AstExpr(Kind, ExprType::Void)
{}
};
// This is an artificial AST node which can be used to represent some forms
// of stack-machine code in an AST form. It is similar to Block, but returns the
// value of its first operand, rather than the last.
class AstFirst : public AstExpr
{
AstExprVector exprs_;
public:
static const AstExprKind Kind = AstExprKind::First;
explicit AstFirst(AstExprVector&& exprs)
: AstExpr(Kind, ExprType::Limit),
exprs_(Move(exprs))
{}
AstExprVector& exprs() { return exprs_; }
const AstExprVector& exprs() const { return exprs_; }
};
} // end wasm namespace
} // end js namespace
#endif // namespace wasmast_h
|