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
|
//
// WebAssembly representation and processing library
//
#ifndef __wasm_h__
#define __wasm_h__
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <map>
#include <vector>
#include "simple_ast.h"
#include "colors.h"
namespace wasm {
// Utilities
// Arena allocation for mixed-type data.
struct Arena {
std::vector<char*> chunks;
int index; // in last chunk
template<class T>
T* alloc() {
const size_t CHUNK = 10000;
size_t currSize = (sizeof(T) + 7) & (-8); // same alignment as malloc TODO optimize?
assert(currSize < CHUNK);
if (chunks.size() == 0 || index + currSize >= CHUNK) {
chunks.push_back(new char[CHUNK]);
index = 0;
}
T* ret = (T*)(chunks.back() + index);
index += currSize;
new (ret) T();
return ret;
}
void clear() {
for (char* chunk : chunks) {
delete[] chunk;
}
chunks.clear();
}
~Arena() {
clear();
}
};
std::ostream &doIndent(std::ostream &o, unsigned indent) {
for (unsigned i = 0; i < indent; i++) {
o << " ";
}
return o;
}
std::ostream &incIndent(std::ostream &o, unsigned& indent) {
o << '\n';
indent++;
return o;
}
std::ostream &decIndent(std::ostream &o, unsigned& indent) {
indent--;
doIndent(o, indent);
return o << ')';
}
// Basics
struct Name : public cashew::IString {
Name() : cashew::IString() {}
Name(const char *str) : cashew::IString(str) {}
Name(cashew::IString str) : cashew::IString(str) {}
friend std::ostream& operator<<(std::ostream &o, Name name) {
assert(name.str);
return o << '$' << name.str; // reference interpreter requires we prefix all names
}
};
// Types
enum WasmType {
none,
i32,
i64,
f32,
f64
};
const char* printWasmType(WasmType type) {
switch (type) {
case WasmType::none: return "none";
case WasmType::i32: return "i32";
case WasmType::i64: return "i64";
case WasmType::f32: return "f32";
case WasmType::f64: return "f64";
}
}
unsigned getWasmTypeSize(WasmType type) {
switch (type) {
case WasmType::none: abort();
case WasmType::i32: return 4;
case WasmType::i64: return 8;
case WasmType::f32: return 4;
case WasmType::f64: return 8;
}
}
bool isFloat(WasmType type) {
switch (type) {
case f32:
case f64: return true;
default: return false;
}
}
WasmType getWasmType(unsigned size, bool float_) {
if (size < 4) return WasmType::i32;
if (size == 4) return float_ ? WasmType::f32 : WasmType::i32;
if (size == 8) return float_ ? WasmType::f64 : WasmType::i64;
abort();
}
std::ostream &prepareMajorColor(std::ostream &o) {
Colors::red(o);
Colors::bold(o);
return o;
}
std::ostream &prepareColor(std::ostream &o) {
Colors::magenta(o);
Colors::bold(o);
return o;
}
std::ostream &prepareMinorColor(std::ostream &o) {
Colors::orange(o);
return o;
}
std::ostream &restoreNormalColor(std::ostream &o) {
Colors::normal(o);
return o;
}
std::ostream& printText(std::ostream &o, const char *str) {
o << '"';
Colors::green(o);
o << str;
Colors::normal(o);
return o << '"';
}
struct Literal {
WasmType type;
union {
int32_t i32;
int64_t i64;
float f32;
double f64;
};
Literal() : type(WasmType::none), f64(0) {}
Literal(int32_t init) : type(WasmType::i32), i32(init) {}
Literal(int64_t init) : type(WasmType::i64), i64(init) {}
Literal(float init) : type(WasmType::f32), f32(init) {}
Literal(double init) : type(WasmType::f64), f64(init) {}
int32_t geti32() { assert(type == WasmType::i32); return i32; }
int64_t geti64() { assert(type == WasmType::i64); return i64; }
float getf32() { assert(type == WasmType::f32); return f32; }
double getf64() { assert(type == WasmType::f64); return f64; }
void printDouble(std::ostream &o, double d) {
const char *text = cashew::JSPrinter::numToString(d);
// spec interpreter hates floats starting with '.'
if (text[0] == '.') {
o << '0';
} else if (text[0] == '-' && text[1] == '.') {
o << "-0";
text++;
}
o << text;
}
friend std::ostream& operator<<(std::ostream &o, Literal literal) {
o << '(';
prepareMinorColor(o) << printWasmType(literal.type) << ".const ";
switch (literal.type) {
case none: abort();
case WasmType::i32: o << literal.i32; break;
case WasmType::i64: o << literal.i64; break;
case WasmType::f32: literal.printDouble(o, literal.f32); break;
case WasmType::f64: literal.printDouble(o, literal.f64); break;
}
restoreNormalColor(o);
return o << ')';
}
};
// Operators
enum UnaryOp {
Clz, Ctz, Popcnt, // int
Neg, Abs, Ceil, Floor, Trunc, Nearest, Sqrt // float
};
enum BinaryOp {
Add, Sub, Mul, // int or float
DivS, DivU, RemS, RemU, And, Or, Xor, Shl, ShrU, ShrS, // int
Div, CopySign, Min, Max // float
};
enum RelationalOp {
Eq, Ne, // int or float
LtS, LtU, LeS, LeU, GtS, GtU, GeS, GeU, // int
Lt, Le, Gt, Ge // float
};
enum ConvertOp {
ExtendSInt32, ExtendUInt32, WrapInt64, TruncSFloat32, TruncUFloat32, TruncSFloat64, TruncUFloat64, ReinterpretFloat, // int
ConvertSInt32, ConvertUInt32, ConvertSInt64, ConvertUInt64, PromoteFloat32, DemoteFloat64, ReinterpretInt // float
};
enum HostOp {
PageSize, MemorySize, GrowMemory, HasFeature
};
// Expressions
class Expression {
public:
WasmType type; // the type of the expression: its output, not necessarily its input(s)
virtual std::ostream& print(std::ostream &o, unsigned indent) = 0;
template<class T>
bool is() {
return !!dynamic_cast<T*>(this);
}
};
std::ostream& printFullLine(std::ostream &o, unsigned indent, Expression *expression) {
doIndent(o, indent);
expression->print(o, indent);
return o << '\n';
}
std::ostream& printOpening(std::ostream &o, const char *str, bool major=false) {
o << '(';
major ? prepareMajorColor(o) : prepareColor(o);
o << str;
restoreNormalColor(o);
return o;
}
std::ostream& printMinorOpening(std::ostream &o, const char *str) {
o << '(';
prepareMinorColor(o);
o << str;
restoreNormalColor(o);
return o;
}
typedef std::vector<Expression*> ExpressionList; // TODO: optimize
class Nop : public Expression {
std::ostream& print(std::ostream &o, unsigned indent) override {
return printMinorOpening(o, "nop") << ')';
}
};
class Block : public Expression {
public:
Name name;
ExpressionList list;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "block");
if (name.is()) {
o << ' ' << name;
}
incIndent(o, indent);
for (auto expression : list) {
printFullLine(o, indent, expression);
}
return decIndent(o, indent);
}
};
class If : public Expression {
public:
Expression *condition, *ifTrue, *ifFalse;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "if");
incIndent(o, indent);
printFullLine(o, indent, condition);
printFullLine(o, indent, ifTrue);
if (ifFalse) printFullLine(o, indent, ifFalse);
return decIndent(o, indent);
}
};
class Loop : public Expression {
public:
Name out, in;
Expression *body;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "loop");
if (out.is()) {
o << ' ' << out;
if (in.is()) {
o << ' ' << in;
}
}
incIndent(o, indent);
printFullLine(o, indent, body);
return decIndent(o, indent);
}
};
class Label : public Expression {
public:
Name name;
Expression* body;
};
class Break : public Expression {
public:
Name name;
Expression *condition, *value;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "break ") << name;
incIndent(o, indent);
if (condition) printFullLine(o, indent, condition);
if (value) printFullLine(o, indent, value);
return decIndent(o, indent);
}
};
class Switch : public Expression {
public:
struct Case {
Literal value;
Expression *body;
bool fallthru;
};
Name name;
Expression *value;
std::vector<Case> cases;
Expression *default_;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "switch ") << name;
incIndent(o, indent);
printFullLine(o, indent, value);
o << "TODO: cases/default\n";
return decIndent(o, indent);
}
};
class Call : public Expression {
public:
Name target;
ExpressionList operands;
std::ostream& printBody(std::ostream &o, unsigned indent) {
o << target;
if (operands.size() > 0) {
incIndent(o, indent);
for (auto operand : operands) {
printFullLine(o, indent, operand);
}
decIndent(o, indent);
} else {
o << ')';
}
return o;
}
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "call ");
return printBody(o, indent);
}
};
class CallImport : public Call {
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "call_import ");
return printBody(o, indent);
}
};
class FunctionType {
public:
Name name;
WasmType result;
std::vector<WasmType> params;
std::ostream& print(std::ostream &o, unsigned indent, bool full=false) {
if (full) {
printOpening(o, "type") << ' ' << name << " (func";
}
if (params.size() > 0) {
o << ' ';
printMinorOpening(o, "param");
for (auto& param : params) {
o << ' ' << printWasmType(param);
}
o << ')';
}
if (result != none) {
o << ' ';
printMinorOpening(o, "result ") << printWasmType(result) << ')';
}
if (full) {
o << "))";;
}
return o;
}
bool operator==(FunctionType& b) {
if (name != b.name) return false; // XXX
if (result != b.result) return false;
if (params.size() != b.params.size()) return false;
for (size_t i = 0; i < params.size(); i++) {
if (params[i] != b.params[i]) return false;
}
return true;
}
bool operator!=(FunctionType& b) {
return !(*this == b);
}
};
class CallIndirect : public Expression {
public:
FunctionType *type;
Expression *target;
ExpressionList operands;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "call_indirect ") << type->name;
incIndent(o, indent);
printFullLine(o, indent, target);
for (auto operand : operands) {
printFullLine(o, indent, operand);
}
return decIndent(o, indent);
}
};
class GetLocal : public Expression {
public:
Name name;
std::ostream& print(std::ostream &o, unsigned indent) override {
return printOpening(o, "get_local ") << name << ')';
}
};
class SetLocal : public Expression {
public:
Name name;
Expression *value;
std::ostream& print(std::ostream &o, unsigned indent) override {
printOpening(o, "set_local ") << name;
incIndent(o, indent);
printFullLine(o, indent, value);
return decIndent(o, indent);
}
};
class Load : public Expression {
public:
unsigned bytes;
bool signed_;
bool float_;
int offset;
unsigned align;
Expression *ptr;
std::ostream& print(std::ostream &o, unsigned indent) override {
o << '(';
prepareColor(o) << printWasmType(getWasmType(bytes, float_)) << ".load";
if (bytes < 4) {
if (bytes == 1) {
o << '8';
} else if (bytes == 2) {
o << "16";
} else {
abort();
}
o << (signed_ ? "_s" : "_u");
}
restoreNormalColor(o);
o << " align=" << align;
assert(!offset);
incIndent(o, indent);
printFullLine(o, indent, ptr);
return decIndent(o, indent);
}
};
class Store : public Expression {
public:
unsigned bytes;
bool float_;
int offset;
unsigned align;
Expression *ptr, *value;
std::ostream& print(std::ostream &o, unsigned indent) override {
o << '(';
prepareColor(o) << printWasmType(getWasmType(bytes, float_)) << ".store";
if (bytes < 4) {
if (bytes == 1) {
o << '8';
} else if (bytes == 2) {
o << "16";
} else {
abort();
}
}
restoreNormalColor(o);
o << " align=" << align;
assert(!offset);
incIndent(o, indent);
printFullLine(o, indent, ptr);
printFullLine(o, indent, value);
return decIndent(o, indent);
}
};
class Const : public Expression {
public:
Literal value;
Const* set(Literal value_) {
value = value_;
type = value.type;
return this;
}
std::ostream& print(std::ostream &o, unsigned indent) override {
return o << value;
}
};
class Unary : public Expression {
public:
UnaryOp op;
Expression *value;
std::ostream& print(std::ostream &o, unsigned indent) override {
o << '(';
prepareColor(o) << printWasmType(type) << '.';
switch (op) {
case Clz: o << "clz"; break;
case Neg: o << "neg"; break;
case Floor: o << "floor"; break;
default: abort();
}
incIndent(o, indent);
printFullLine(o, indent, value);
return decIndent(o, indent);
}
};
class Binary : public Expression {
public:
BinaryOp op;
Expression *left, *right;
std::ostream& print(std::ostream &o, unsigned indent) override {
o << '(';
prepareColor(o) << printWasmType(type) << '.';
switch (op) {
case Add: o << "add"; break;
case Sub: o << "sub"; break;
case Mul: o << "mul"; break;
case DivS: o << "div_s"; break;
case DivU: o << "div_u"; break;
case RemS: o << "rem_s"; break;
case RemU: o << "rem_u"; break;
case And: o << "and"; break;
case Or: o << "or"; break;
case Xor: o << "xor"; break;
case Shl: o << "shl"; break;
case ShrU: o << "shr_u"; break;
case ShrS: o << "shr_s"; break;
case Div: o << "div"; break;
case CopySign: o << "copysign"; break;
case Min: o << "min"; break;
case Max: o << "max"; break;
default: abort();
}
restoreNormalColor(o);
incIndent(o, indent);
printFullLine(o, indent, left);
printFullLine(o, indent, right);
return decIndent(o, indent);
}
};
class Compare : public Expression {
public:
RelationalOp op;
WasmType inputType;
Expression *left, *right;
Compare() {
type = WasmType::i32; // output is always i32
}
std::ostream& print(std::ostream &o, unsigned indent) override {
o << '(';
prepareColor(o) << printWasmType(inputType) << '.';
switch (op) {
case Eq: o << "eq"; break;
case Ne: o << "ne"; break;
case LtS: o << "lt_s"; break;
case LtU: o << "lt_u"; break;
case LeS: o << "le_s"; break;
case LeU: o << "le_u"; break;
case GtS: o << "gt_s"; break;
case GtU: o << "gt_u"; break;
case GeS: o << "ge_s"; break;
case GeU: o << "ge_u"; break;
case Lt: o << "lt"; break;
case Le: o << "le"; break;
case Gt: o << "gt"; break;
case Ge: o << "ge"; break;
default: abort();
}
restoreNormalColor(o);
incIndent(o, indent);
printFullLine(o, indent, left);
printFullLine(o, indent, right);
return decIndent(o, indent);
}
};
class Convert : public Expression {
public:
ConvertOp op;
Expression *value;
std::ostream& print(std::ostream &o, unsigned indent) override {
o << '(';
prepareColor(o);
switch (op) {
case ConvertUInt32: o << "f64.convert_u/i32"; break;
case ConvertSInt32: o << "f64.convert_s/i32"; break;
case TruncSFloat64: o << "i32.trunc_s/f64"; break;
default: abort();
}
restoreNormalColor(o);
incIndent(o, indent);
printFullLine(o, indent, value);
return decIndent(o, indent);
}
};
class Host : public Expression {
public:
HostOp op;
ExpressionList operands;
};
// Globals
struct NameType {
Name name;
WasmType type;
NameType() : name(nullptr), type(none) {}
NameType(Name name, WasmType type) : name(name), type(type) {}
};
class Function {
public:
Name name;
WasmType result;
std::vector<NameType> params;
std::vector<NameType> locals;
Expression *body;
std::ostream& print(std::ostream &o, unsigned indent) {
printOpening(o, "func ", true) << name;
if (params.size() > 0) {
for (auto& param : params) {
o << ' ';
printMinorOpening(o, "param ") << param.name << ' ' << printWasmType(param.type) << ")";
}
}
if (result != none) {
o << ' ';
printMinorOpening(o, "result ") << printWasmType(result) << ")";
}
incIndent(o, indent);
for (auto& local : locals) {
doIndent(o, indent);
printMinorOpening(o, "local ") << local.name << ' ' << printWasmType(local.type) << ")\n";
}
printFullLine(o, indent, body);
return decIndent(o, indent);
}
};
class Import {
public:
Name name, module, base; // name = module.base
FunctionType type;
std::ostream& print(std::ostream &o, unsigned indent) {
printOpening(o, "import ") << name << ' ';
printText(o, module.str) << ' ';
printText(o, base.str) << ' ';
type.print(o, indent);
return o << ')';
}
};
class Export {
public:
Name name;
Name value;
std::ostream& print(std::ostream &o, unsigned indent) {
printOpening(o, "export ");
return printText(o, name.str) << ' ' << value << ')';
}
};
class Table {
public:
std::vector<Name> names;
std::ostream& print(std::ostream &o, unsigned indent) {
printOpening(o, "table");
for (auto name : names) {
o << ' ' << name;
}
return o << ')';
}
};
class Module {
public:
// wasm contents
std::map<Name, FunctionType*> functionTypes;
std::map<Name, Import> imports;
std::vector<Export> exports;
Table table;
std::vector<Function*> functions;
Module() {}
friend std::ostream& operator<<(std::ostream &o, Module module) {
unsigned indent = 0;
printOpening(o, "module", true);
incIndent(o, indent);
doIndent(o, indent);
printOpening(o, "memory") << " 16777216)\n"; // XXX
for (auto& curr : module.functionTypes) {
doIndent(o, indent);
curr.second->print(o, indent, true);
o << '\n';
}
#if 0
for (auto& curr : module.imports) {
doIndent(o, indent);
curr.second.print(o, indent);
o << '\n';
}
#endif
for (auto& curr : module.exports) {
doIndent(o, indent);
curr.print(o, indent);
o << '\n';
}
if (module.table.names.size() > 0) {
doIndent(o, indent);
module.table.print(o, indent);
o << '\n';
}
for (auto& curr : module.functions) {
doIndent(o, indent);
curr->print(o, indent);
o << '\n';
}
decIndent(o, indent);
return o << '\n';
}
};
//
// Simple WebAssembly AST visiting and children-first walking
//
template<class ReturnType>
struct WasmVisitor {
virtual ReturnType visitBlock(Block *curr) = 0;
virtual ReturnType visitIf(If *curr) = 0;
virtual ReturnType visitLoop(Loop *curr) = 0;
virtual ReturnType visitLabel(Label *curr) = 0;
virtual ReturnType visitBreak(Break *curr) = 0;
virtual ReturnType visitSwitch(Switch *curr) = 0;
virtual ReturnType visitCall(Call *curr) = 0;
virtual ReturnType visitCallImport(CallImport *curr) = 0;
virtual ReturnType visitCallIndirect(CallIndirect *curr) = 0;
virtual ReturnType visitGetLocal(GetLocal *curr) = 0;
virtual ReturnType visitSetLocal(SetLocal *curr) = 0;
virtual ReturnType visitLoad(Load *curr) = 0;
virtual ReturnType visitStore(Store *curr) = 0;
virtual ReturnType visitConst(Const *curr) = 0;
virtual ReturnType visitUnary(Unary *curr) = 0;
virtual ReturnType visitBinary(Binary *curr) = 0;
virtual ReturnType visitCompare(Compare *curr) = 0;
virtual ReturnType visitConvert(Convert *curr) = 0;
virtual ReturnType visitHost(Host *curr) = 0;
virtual ReturnType visitNop(Nop *curr) = 0;
ReturnType visit(Expression *curr) {
assert(curr);
if (Block *cast = dynamic_cast<Block*>(curr)) return visitBlock(cast);
if (If *cast = dynamic_cast<If*>(curr)) return visitIf(cast);
if (Loop *cast = dynamic_cast<Loop*>(curr)) return visitLoop(cast);
if (Label *cast = dynamic_cast<Label*>(curr)) return visitLabel(cast);
if (Break *cast = dynamic_cast<Break*>(curr)) return visitBreak(cast);
if (Switch *cast = dynamic_cast<Switch*>(curr)) return visitSwitch(cast);
if (Call *cast = dynamic_cast<Call*>(curr)) return visitCall(cast);
if (CallImport *cast = dynamic_cast<CallImport*>(curr)) return visitCallImport(cast);
if (CallIndirect *cast = dynamic_cast<CallIndirect*>(curr)) return visitCallIndirect(cast);
if (GetLocal *cast = dynamic_cast<GetLocal*>(curr)) return visitGetLocal(cast);
if (SetLocal *cast = dynamic_cast<SetLocal*>(curr)) return visitSetLocal(cast);
if (Load *cast = dynamic_cast<Load*>(curr)) return visitLoad(cast);
if (Store *cast = dynamic_cast<Store*>(curr)) return visitStore(cast);
if (Const *cast = dynamic_cast<Const*>(curr)) return visitConst(cast);
if (Unary *cast = dynamic_cast<Unary*>(curr)) return visitUnary(cast);
if (Binary *cast = dynamic_cast<Binary*>(curr)) return visitBinary(cast);
if (Compare *cast = dynamic_cast<Compare*>(curr)) return visitCompare(cast);
if (Convert *cast = dynamic_cast<Convert*>(curr)) return visitConvert(cast);
if (Host *cast = dynamic_cast<Host*>(curr)) return visitHost(cast);
if (Nop *cast = dynamic_cast<Nop*>(curr)) return visitNop(cast);
abort();
}
};
struct WasmWalker : public WasmVisitor<Expression*> {
wasm::Arena* allocator; // use an existing allocator, or null if no allocations
WasmWalker() : allocator(nullptr) {}
WasmWalker(wasm::Arena* allocator) : allocator(allocator) {}
// Each method receives an AST pointer, and it is replaced with what is returned.
Expression* visitBlock(Block *curr) override { return curr; };
Expression* visitIf(If *curr) override { return curr; };
Expression* visitLoop(Loop *curr) override { return curr; };
Expression* visitLabel(Label *curr) override { return curr; };
Expression* visitBreak(Break *curr) override { return curr; };
Expression* visitSwitch(Switch *curr) override { return curr; };
Expression* visitCall(Call *curr) override { return curr; };
Expression* visitCallImport(CallImport *curr) override { return curr; };
Expression* visitCallIndirect(CallIndirect *curr) override { return curr; };
Expression* visitGetLocal(GetLocal *curr) override { return curr; };
Expression* visitSetLocal(SetLocal *curr) override { return curr; };
Expression* visitLoad(Load *curr) override { return curr; };
Expression* visitStore(Store *curr) override { return curr; };
Expression* visitConst(Const *curr) override { return curr; };
Expression* visitUnary(Unary *curr) override { return curr; };
Expression* visitBinary(Binary *curr) override { return curr; };
Expression* visitCompare(Compare *curr) override { return curr; };
Expression* visitConvert(Convert *curr) override { return curr; };
Expression* visitHost(Host *curr) override { return curr; };
Expression* visitNop(Nop *curr) override { return curr; };
// children-first
Expression *walk(Expression *curr) {
if (!curr) return curr;
if (Block *cast = dynamic_cast<Block*>(curr)) {
ExpressionList& list = cast->list;
for (size_t z = 0; z < list.size(); z++) {
list[z] = walk(list[z]);
}
} else if (If *cast = dynamic_cast<If*>(curr)) {
cast->condition = walk(cast->condition);
cast->ifTrue = walk(cast->ifTrue);
cast->ifFalse = walk(cast->ifFalse);
} else if (Loop *cast = dynamic_cast<Loop*>(curr)) {
cast->body = walk(cast->body);
} else if (Label *cast = dynamic_cast<Label*>(curr)) {
} else if (Break *cast = dynamic_cast<Break*>(curr)) {
cast->condition = walk(cast->condition);
cast->value = walk(cast->value);
} else if (Switch *cast = dynamic_cast<Switch*>(curr)) {
cast->value = walk(cast->value);
for (auto& curr : cast->cases) {
curr.body = walk(curr.body);
}
cast->default_ = walk(cast->default_);
} else if (Call *cast = dynamic_cast<Call*>(curr)) {
ExpressionList& list = cast->operands;
for (size_t z = 0; z < list.size(); z++) {
list[z] = walk(list[z]);
}
} else if (CallImport *cast = dynamic_cast<CallImport*>(curr)) {
ExpressionList& list = cast->operands;
for (size_t z = 0; z < list.size(); z++) {
list[z] = walk(list[z]);
}
} else if (CallIndirect *cast = dynamic_cast<CallIndirect*>(curr)) {
cast->target = walk(cast->target);
ExpressionList& list = cast->operands;
for (size_t z = 0; z < list.size(); z++) {
list[z] = walk(list[z]);
}
} else if (GetLocal *cast = dynamic_cast<GetLocal*>(curr)) {
} else if (SetLocal *cast = dynamic_cast<SetLocal*>(curr)) {
cast->value = walk(cast->value);
} else if (Load *cast = dynamic_cast<Load*>(curr)) {
cast->ptr = walk(cast->ptr);
} else if (Store *cast = dynamic_cast<Store*>(curr)) {
cast->ptr = walk(cast->ptr);
cast->value = walk(cast->value);
} else if (Const *cast = dynamic_cast<Const*>(curr)) {
} else if (Unary *cast = dynamic_cast<Unary*>(curr)) {
cast->value = walk(cast->value);
} else if (Binary *cast = dynamic_cast<Binary*>(curr)) {
cast->left = walk(cast->left);
cast->right = walk(cast->right);
} else if (Compare *cast = dynamic_cast<Compare*>(curr)) {
cast->left = walk(cast->left);
cast->right = walk(cast->right);
} else if (Convert *cast = dynamic_cast<Convert*>(curr)) {
cast->value = walk(cast->value);
} else if (Host *cast = dynamic_cast<Host*>(curr)) {
ExpressionList& list = cast->operands;
for (size_t z = 0; z < list.size(); z++) {
list[z] = walk(list[z]);
}
} else if (Nop *cast = dynamic_cast<Nop*>(curr)) {
} else {
abort();
}
return visit(curr);
}
void startWalk(Function *func) {
func->body = walk(func->body);
}
};
} // namespace wasm
#endif // __wasm_h__
|