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
|
/*
* 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.
*/
//
// Print out text in s-expression format
//
#include <wasm.h>
#include <wasm-printing.h>
#include <pass.h>
#include <pretty_printing.h>
namespace wasm {
static int isFullForced() {
if (getenv("BINARYEN_PRINT_FULL")) {
return std::stoi(getenv("BINARYEN_PRINT_FULL"));
}
return 0;
}
struct PrintSExpression : public Visitor<PrintSExpression> {
std::ostream& o;
unsigned indent = 0;
bool minify;
const char *maybeSpace;
const char *maybeNewLine;
bool full = false; // whether to not elide nodes in output when possible
// (like implicit blocks) and to emit types
Module* currModule = nullptr;
Function* currFunction = nullptr;
Function::DebugLocation lastPrintedLocation;
PrintSExpression(std::ostream& o) : o(o) {
setMinify(false);
if (!full) full = isFullForced();
}
void visit(Expression* curr) {
if (currFunction) {
// show an annotation, if there is one
auto& debugLocations = currFunction->debugLocations;
auto iter = debugLocations.find(curr);
if (iter != debugLocations.end()) {
auto fileName = currModule->debugInfoFileNames[iter->second.fileIndex];
if (lastPrintedLocation != iter->second) {
lastPrintedLocation = iter->second;
o << ";;@ " << fileName << ":" << iter->second.lineNumber << ":" << iter->second.columnNumber << '\n';
doIndent(o, indent);
}
}
}
Visitor<PrintSExpression>::visit(curr);
}
void setMinify(bool minify_) {
minify = minify_;
maybeSpace = minify ? "" : " ";
maybeNewLine = minify ? "" : "\n";
}
void setFull(bool full_) { full = full_; }
void incIndent() {
if (minify) return;
o << '\n';
indent++;
}
void decIndent() {
if (!minify) {
indent--;
doIndent(o, indent);
}
o << ')';
}
void printFullLine(Expression *expression) {
!minify && doIndent(o, indent);
if (full) {
o << "[" << printWasmType(expression->type) << "] ";
}
visit(expression);
o << maybeNewLine;
}
Name printableLocal(Index index) {
Name name;
if (currFunction) {
name = currFunction->getLocalNameOrDefault(index);
}
if (!name.is()) {
name = Name::fromInt(index);
}
return name;
}
std::ostream& printName(Name name) {
// we need to quote names if they have tricky chars
if (strpbrk(name.str, "()")) {
o << '"' << name << '"';
} else {
o << name;
}
return o;
}
void visitBlock(Block *curr) {
// special-case Block, because Block nesting (in their first element) can be incredibly deep
std::vector<Block*> stack;
while (1) {
if (stack.size() > 0) doIndent(o, indent);
stack.push_back(curr);
if (full) {
o << "[" << printWasmType(curr->type) << "] ";
}
printOpening(o, "block");
if (curr->name.is()) {
o << ' ';
printName(curr->name);
}
if (isConcreteWasmType(curr->type)) {
o << " (result " << printWasmType(curr->type) << ')';
}
incIndent();
if (curr->list.size() > 0 && curr->list[0]->is<Block>()) {
// recurse into the first element
curr = curr->list[0]->cast<Block>();
continue;
} else {
break; // that's all we can recurse, start to unwind
}
}
auto* top = stack.back();
while (stack.size() > 0) {
curr = stack.back();
stack.pop_back();
auto& list = curr->list;
for (size_t i = 0; i < list.size(); i++) {
if (curr != top && i == 0) {
// one of the block recursions we already handled
decIndent();
o << '\n';
continue;
}
printFullLine(list[i]);
}
}
decIndent();
}
void visitIf(If *curr) {
printOpening(o, "if");
if (isConcreteWasmType(curr->type)) {
o << " (result " << printWasmType(curr->type) << ')';
}
incIndent();
printFullLine(curr->condition);
// ifTrue and False have implict blocks, avoid printing them if possible
if (!full && curr->ifTrue->is<Block>() && curr->ifTrue->dynCast<Block>()->name.isNull() && curr->ifTrue->dynCast<Block>()->list.size() == 1) {
printFullLine(curr->ifTrue->dynCast<Block>()->list.back());
} else {
printFullLine(curr->ifTrue);
}
if (curr->ifFalse) {
if (!full && curr->ifFalse->is<Block>() && curr->ifFalse->dynCast<Block>()->name.isNull() && curr->ifFalse->dynCast<Block>()->list.size() == 1) {
printFullLine(curr->ifFalse->dynCast<Block>()->list.back());
} else {
printFullLine(curr->ifFalse);
}
}
decIndent();
}
void visitLoop(Loop *curr) {
printOpening(o, "loop");
if (curr->name.is()) {
o << ' ' << curr->name;
}
if (isConcreteWasmType(curr->type)) {
o << " (result " << printWasmType(curr->type) << ')';
}
incIndent();
auto block = curr->body->dynCast<Block>();
if (!full && block && block->name.isNull()) {
// wasm spec has loops containing children directly, while our ast
// has a single child for simplicity. print out the optimal form.
for (auto expression : block->list) {
printFullLine(expression);
}
} else {
printFullLine(curr->body);
}
decIndent();
}
void visitBreak(Break *curr) {
if (curr->condition) {
printOpening(o, "br_if ");
printName(curr->name);
incIndent();
} else {
printOpening(o, "br ");
printName(curr->name);
if (!curr->value || curr->value->is<Nop>()) {
// avoid a new line just for the parens
o << ')';
return;
}
incIndent();
}
if (curr->value && !curr->value->is<Nop>()) printFullLine(curr->value);
if (curr->condition) {
printFullLine(curr->condition);
}
decIndent();
}
void visitSwitch(Switch *curr) {
printOpening(o, "br_table");
for (auto& t : curr->targets) {
o << ' ' << t;
}
o << ' ' << curr->default_;
incIndent();
if (curr->value && !curr->value->is<Nop>()) printFullLine(curr->value);
printFullLine(curr->condition);
decIndent();
}
template<typename CallBase>
void printCallBody(CallBase* curr) {
printName(curr->target);
if (curr->operands.size() > 0) {
incIndent();
for (auto operand : curr->operands) {
printFullLine(operand);
}
decIndent();
} else {
o << ')';
}
}
void visitCall(Call *curr) {
printOpening(o, "call ");
printCallBody(curr);
}
void visitCallImport(CallImport *curr) {
printOpening(o, "call ");
printCallBody(curr);
}
void visitCallIndirect(CallIndirect *curr) {
printOpening(o, "call_indirect ") << curr->fullType;
incIndent();
for (auto operand : curr->operands) {
printFullLine(operand);
}
printFullLine(curr->target);
decIndent();
}
void visitGetLocal(GetLocal *curr) {
printOpening(o, "get_local ") << printableLocal(curr->index) << ')';
}
void visitSetLocal(SetLocal *curr) {
if (curr->isTee()) {
printOpening(o, "tee_local ");
} else {
printOpening(o, "set_local ");
}
o << printableLocal(curr->index);
incIndent();
printFullLine(curr->value);
decIndent();
}
void visitGetGlobal(GetGlobal *curr) {
printOpening(o, "get_global ");
printName(curr->name) << ')';
}
void visitSetGlobal(SetGlobal *curr) {
printOpening(o, "set_global ");
printName(curr->name);
incIndent();
printFullLine(curr->value);
decIndent();
}
void visitLoad(Load *curr) {
o << '(';
prepareColor(o) << printWasmType(curr->type) << ".load";
if (curr->bytes < 4 || (curr->type == i64 && curr->bytes < 8)) {
if (curr->bytes == 1) {
o << '8';
} else if (curr->bytes == 2) {
o << "16";
} else if (curr->bytes == 4) {
o << "32";
} else {
abort();
}
o << (curr->signed_ ? "_s" : "_u");
}
restoreNormalColor(o);
if (curr->offset) {
o << " offset=" << curr->offset;
}
if (curr->align != curr->bytes) {
o << " align=" << curr->align;
}
incIndent();
printFullLine(curr->ptr);
decIndent();
}
void visitStore(Store *curr) {
o << '(';
prepareColor(o) << printWasmType(curr->valueType) << ".store";
if (curr->bytes < 4 || (curr->valueType == i64 && curr->bytes < 8)) {
if (curr->bytes == 1) {
o << '8';
} else if (curr->bytes == 2) {
o << "16";
} else if (curr->bytes == 4) {
o << "32";
} else {
abort();
}
}
restoreNormalColor(o);
if (curr->offset) {
o << " offset=" << curr->offset;
}
if (curr->align != curr->bytes) {
o << " align=" << curr->align;
}
incIndent();
printFullLine(curr->ptr);
printFullLine(curr->value);
decIndent();
}
void visitConst(Const *curr) {
o << curr->value;
}
void visitUnary(Unary *curr) {
o << '(';
prepareColor(o);
switch (curr->op) {
case ClzInt32: o << "i32.clz"; break;
case CtzInt32: o << "i32.ctz"; break;
case PopcntInt32: o << "i32.popcnt"; break;
case EqZInt32: o << "i32.eqz"; break;
case ClzInt64: o << "i64.clz"; break;
case CtzInt64: o << "i64.ctz"; break;
case PopcntInt64: o << "i64.popcnt"; break;
case EqZInt64: o << "i64.eqz"; break;
case NegFloat32: o << "f32.neg"; break;
case AbsFloat32: o << "f32.abs"; break;
case CeilFloat32: o << "f32.ceil"; break;
case FloorFloat32: o << "f32.floor"; break;
case TruncFloat32: o << "f32.trunc"; break;
case NearestFloat32: o << "f32.nearest"; break;
case SqrtFloat32: o << "f32.sqrt"; break;
case NegFloat64: o << "f64.neg"; break;
case AbsFloat64: o << "f64.abs"; break;
case CeilFloat64: o << "f64.ceil"; break;
case FloorFloat64: o << "f64.floor"; break;
case TruncFloat64: o << "f64.trunc"; break;
case NearestFloat64: o << "f64.nearest"; break;
case SqrtFloat64: o << "f64.sqrt"; break;
case ExtendSInt32: o << "i64.extend_s/i32"; break;
case ExtendUInt32: o << "i64.extend_u/i32"; break;
case WrapInt64: o << "i32.wrap/i64"; break;
case TruncSFloat32ToInt32: o << "i32.trunc_s/f32"; break;
case TruncSFloat32ToInt64: o << "i64.trunc_s/f32"; break;
case TruncUFloat32ToInt32: o << "i32.trunc_u/f32"; break;
case TruncUFloat32ToInt64: o << "i64.trunc_u/f32"; break;
case TruncSFloat64ToInt32: o << "i32.trunc_s/f64"; break;
case TruncSFloat64ToInt64: o << "i64.trunc_s/f64"; break;
case TruncUFloat64ToInt32: o << "i32.trunc_u/f64"; break;
case TruncUFloat64ToInt64: o << "i64.trunc_u/f64"; break;
case ReinterpretFloat32: o << "i32.reinterpret/f32"; break;
case ReinterpretFloat64: o << "i64.reinterpret/f64"; break;
case ConvertUInt32ToFloat32: o << "f32.convert_u/i32"; break;
case ConvertUInt32ToFloat64: o << "f64.convert_u/i32"; break;
case ConvertSInt32ToFloat32: o << "f32.convert_s/i32"; break;
case ConvertSInt32ToFloat64: o << "f64.convert_s/i32"; break;
case ConvertUInt64ToFloat32: o << "f32.convert_u/i64"; break;
case ConvertUInt64ToFloat64: o << "f64.convert_u/i64"; break;
case ConvertSInt64ToFloat32: o << "f32.convert_s/i64"; break;
case ConvertSInt64ToFloat64: o << "f64.convert_s/i64"; break;
case PromoteFloat32: o << "f64.promote/f32"; break;
case DemoteFloat64: o << "f32.demote/f64"; break;
case ReinterpretInt32: o << "f32.reinterpret/i32"; break;
case ReinterpretInt64: o << "f64.reinterpret/i64"; break;
default: abort();
}
incIndent();
printFullLine(curr->value);
decIndent();
}
void visitBinary(Binary *curr) {
o << '(';
prepareColor(o);
switch (curr->op) {
case AddInt32: o << "i32.add"; break;
case SubInt32: o << "i32.sub"; break;
case MulInt32: o << "i32.mul"; break;
case DivSInt32: o << "i32.div_s"; break;
case DivUInt32: o << "i32.div_u"; break;
case RemSInt32: o << "i32.rem_s"; break;
case RemUInt32: o << "i32.rem_u"; break;
case AndInt32: o << "i32.and"; break;
case OrInt32: o << "i32.or"; break;
case XorInt32: o << "i32.xor"; break;
case ShlInt32: o << "i32.shl"; break;
case ShrUInt32: o << "i32.shr_u"; break;
case ShrSInt32: o << "i32.shr_s"; break;
case RotLInt32: o << "i32.rotl"; break;
case RotRInt32: o << "i32.rotr"; break;
case EqInt32: o << "i32.eq"; break;
case NeInt32: o << "i32.ne"; break;
case LtSInt32: o << "i32.lt_s"; break;
case LtUInt32: o << "i32.lt_u"; break;
case LeSInt32: o << "i32.le_s"; break;
case LeUInt32: o << "i32.le_u"; break;
case GtSInt32: o << "i32.gt_s"; break;
case GtUInt32: o << "i32.gt_u"; break;
case GeSInt32: o << "i32.ge_s"; break;
case GeUInt32: o << "i32.ge_u"; break;
case AddInt64: o << "i64.add"; break;
case SubInt64: o << "i64.sub"; break;
case MulInt64: o << "i64.mul"; break;
case DivSInt64: o << "i64.div_s"; break;
case DivUInt64: o << "i64.div_u"; break;
case RemSInt64: o << "i64.rem_s"; break;
case RemUInt64: o << "i64.rem_u"; break;
case AndInt64: o << "i64.and"; break;
case OrInt64: o << "i64.or"; break;
case XorInt64: o << "i64.xor"; break;
case ShlInt64: o << "i64.shl"; break;
case ShrUInt64: o << "i64.shr_u"; break;
case ShrSInt64: o << "i64.shr_s"; break;
case RotLInt64: o << "i64.rotl"; break;
case RotRInt64: o << "i64.rotr"; break;
case EqInt64: o << "i64.eq"; break;
case NeInt64: o << "i64.ne"; break;
case LtSInt64: o << "i64.lt_s"; break;
case LtUInt64: o << "i64.lt_u"; break;
case LeSInt64: o << "i64.le_s"; break;
case LeUInt64: o << "i64.le_u"; break;
case GtSInt64: o << "i64.gt_s"; break;
case GtUInt64: o << "i64.gt_u"; break;
case GeSInt64: o << "i64.ge_s"; break;
case GeUInt64: o << "i64.ge_u"; break;
case AddFloat32: o << "f32.add"; break;
case SubFloat32: o << "f32.sub"; break;
case MulFloat32: o << "f32.mul"; break;
case DivFloat32: o << "f32.div"; break;
case CopySignFloat32: o << "f32.copysign"; break;
case MinFloat32: o << "f32.min"; break;
case MaxFloat32: o << "f32.max"; break;
case EqFloat32: o << "f32.eq"; break;
case NeFloat32: o << "f32.ne"; break;
case LtFloat32: o << "f32.lt"; break;
case LeFloat32: o << "f32.le"; break;
case GtFloat32: o << "f32.gt"; break;
case GeFloat32: o << "f32.ge"; break;
case AddFloat64: o << "f64.add"; break;
case SubFloat64: o << "f64.sub"; break;
case MulFloat64: o << "f64.mul"; break;
case DivFloat64: o << "f64.div"; break;
case CopySignFloat64: o << "f64.copysign"; break;
case MinFloat64: o << "f64.min"; break;
case MaxFloat64: o << "f64.max"; break;
case EqFloat64: o << "f64.eq"; break;
case NeFloat64: o << "f64.ne"; break;
case LtFloat64: o << "f64.lt"; break;
case LeFloat64: o << "f64.le"; break;
case GtFloat64: o << "f64.gt"; break;
case GeFloat64: o << "f64.ge"; break;
default: abort();
}
restoreNormalColor(o);
incIndent();
printFullLine(curr->left);
printFullLine(curr->right);
decIndent();
}
void visitSelect(Select *curr) {
o << '(';
prepareColor(o) << "select";
incIndent();
printFullLine(curr->ifTrue);
printFullLine(curr->ifFalse);
printFullLine(curr->condition);
decIndent();
}
void visitDrop(Drop *curr) {
o << '(';
prepareColor(o) << "drop";
incIndent();
printFullLine(curr->value);
decIndent();
}
void visitReturn(Return *curr) {
printOpening(o, "return");
if (!curr->value) {
// avoid a new line just for the parens
o << ')';
return;
}
incIndent();
printFullLine(curr->value);
decIndent();
}
void visitHost(Host *curr) {
switch (curr->op) {
case PageSize: printOpening(o, "pagesize") << ')'; break;
case CurrentMemory: printOpening(o, "current_memory") << ')'; break;
case GrowMemory: {
printOpening(o, "grow_memory");
incIndent();
printFullLine(curr->operands[0]);
decIndent();
break;
}
case HasFeature: printOpening(o, "hasfeature ") << curr->nameOperand << ')'; break;
default: abort();
}
}
void visitNop(Nop *curr) {
printMinorOpening(o, "nop") << ')';
}
void visitUnreachable(Unreachable *curr) {
printMinorOpening(o, "unreachable") << ')';
}
// Module-level visitors
void visitFunctionType(FunctionType *curr, Name* internalName = nullptr) {
o << "(func";
if (internalName) o << ' ' << *internalName;
if (curr->params.size() > 0) {
o << maybeSpace;
printMinorOpening(o, "param");
for (auto& param : curr->params) {
o << ' ' << printWasmType(param);
}
o << ')';
}
if (curr->result != none) {
o << maybeSpace;
printMinorOpening(o, "result ") << printWasmType(curr->result) << ')';
}
o << ")";
}
void visitImport(Import *curr) {
printOpening(o, "import ");
printText(o, curr->module.str) << ' ';
printText(o, curr->base.str) << ' ';
switch (curr->kind) {
case ExternalKind::Function: if (curr->functionType.is()) visitFunctionType(currModule->getFunctionType(curr->functionType), &curr->name); break;
case ExternalKind::Table: printTableHeader(&currModule->table); break;
case ExternalKind::Memory: printMemoryHeader(&currModule->memory); break;
case ExternalKind::Global: o << "(global " << curr->name << ' ' << printWasmType(curr->globalType) << ")"; break;
default: WASM_UNREACHABLE();
}
o << ')';
}
void visitExport(Export *curr) {
printOpening(o, "export ");
printText(o, curr->name.str) << " (";
switch (curr->kind) {
case ExternalKind::Function: o << "func"; break;
case ExternalKind::Table: o << "table"; break;
case ExternalKind::Memory: o << "memory"; break;
case ExternalKind::Global: o << "global"; break;
default: WASM_UNREACHABLE();
}
o << ' ';
printName(curr->value) << "))";
}
void visitGlobal(Global *curr) {
printOpening(o, "global ");
printName(curr->name) << ' ';
if (curr->mutable_) {
o << "(mut " << printWasmType(curr->type) << ") ";
} else {
o << printWasmType(curr->type) << ' ';
}
visit(curr->init);
o << ')';
}
void visitFunction(Function *curr) {
currFunction = curr;
lastPrintedLocation = { 0, 0, 0 };
printOpening(o, "func ", true);
printName(curr->name);
if (curr->type.is()) {
o << maybeSpace << "(type " << curr->type << ')';
}
if (curr->params.size() > 0) {
for (size_t i = 0; i < curr->params.size(); i++) {
o << maybeSpace;
printMinorOpening(o, "param ") << printableLocal(i) << ' ' << printWasmType(curr->getLocalType(i)) << ')';
}
}
if (curr->result != none) {
o << maybeSpace;
printMinorOpening(o, "result ") << printWasmType(curr->result) << ')';
}
incIndent();
for (size_t i = curr->getVarIndexBase(); i < curr->getNumLocals(); i++) {
doIndent(o, indent);
printMinorOpening(o, "local ") << printableLocal(i) << ' ' << printWasmType(curr->getLocalType(i)) << ')';
o << maybeNewLine;
}
// It is ok to emit a block here, as a function can directly contain a list, even if our
// ast avoids that for simplicity. We can just do that optimization here..
if (!full && curr->body->is<Block>() && curr->body->cast<Block>()->name.isNull()) {
Block* block = curr->body->cast<Block>();
for (auto item : block->list) {
printFullLine(item);
}
} else {
printFullLine(curr->body);
}
decIndent();
}
void printTableHeader(Table* curr) {
printOpening(o, "table") << ' ';
o << curr->initial;
if (curr->max != Table::kMaxSize) o << ' ' << curr->max;
o << " anyfunc)";
}
void visitTable(Table *curr) {
// if table wasn't imported, declare it
if (!curr->imported) {
doIndent(o, indent);
printTableHeader(curr);
o << maybeNewLine;
}
if (curr->segments.empty()) return;
doIndent(o, indent);
for (auto& segment : curr->segments) {
// Don't print empty segments
if (segment.data.empty()) continue;
printOpening(o, "elem ", true);
visit(segment.offset);
for (auto name : segment.data) {
o << ' ';
printName(name);
}
o << ')';
}
o << maybeNewLine;
}
void printMemoryHeader(Memory* curr) {
printOpening(o, "memory") << ' ';
printName(curr->name) << ' ';
o << curr->initial;
if (curr->max && curr->max != Memory::kMaxSize) o << ' ' << curr->max;
o << ")";
}
void visitMemory(Memory* curr) {
// if memory wasn't imported, declare it
if (!curr->imported) {
doIndent(o, indent);
printMemoryHeader(curr);
o << '\n';
}
for (auto segment : curr->segments) {
doIndent(o, indent);
printOpening(o, "data ", true);
visit(segment.offset);
o << " \"";
for (size_t i = 0; i < segment.data.size(); i++) {
unsigned char c = segment.data[i];
switch (c) {
case '\n': o << "\\n"; break;
case '\r': o << "\\0d"; break;
case '\t': o << "\\t"; break;
case '\f': o << "\\0c"; break;
case '\b': o << "\\08"; break;
case '\\': o << "\\\\"; break;
case '"' : o << "\\\""; break;
case '\'' : o << "\\'"; break;
default: {
if (c >= 32 && c < 127) {
o << c;
} else {
o << std::hex << '\\' << (c/16) << (c%16) << std::dec;
}
}
}
}
o << "\")\n";
}
}
void visitModule(Module *curr) {
currModule = curr;
printOpening(o, "module", true);
incIndent();
for (auto& child : curr->functionTypes) {
doIndent(o, indent);
printOpening(o, "type") << ' ';
printName(child->name) << ' ';
visitFunctionType(child.get());
o << ")" << maybeNewLine;
}
for (auto& child : curr->imports) {
doIndent(o, indent);
visitImport(child.get());
o << maybeNewLine;
}
for (auto& child : curr->globals) {
doIndent(o, indent);
visitGlobal(child.get());
o << maybeNewLine;
}
if (curr->table.exists) {
visitTable(&curr->table); // Prints its own newlines
}
visitMemory(&curr->memory);
for (auto& child : curr->exports) {
doIndent(o, indent);
visitExport(child.get());
o << maybeNewLine;
}
if (curr->start.is()) {
doIndent(o, indent);
printOpening(o, "start") << ' ' << curr->start << ')';
o << maybeNewLine;
}
for (auto& child : curr->functions) {
doIndent(o, indent);
visitFunction(child.get());
o << maybeNewLine;
}
for (auto& section : curr->userSections) {
doIndent(o, indent);
o << ";; custom section \"" << section.name << "\", size " << section.data.size();
o << maybeNewLine;
}
decIndent();
o << maybeNewLine;
currModule = nullptr;
}
};
void Printer::run(PassRunner* runner, Module* module) {
PrintSExpression print(o);
print.visitModule(module);
}
Pass *createPrinterPass() {
return new Printer();
}
// Prints out a minified module
class MinifiedPrinter : public Printer {
public:
MinifiedPrinter() : Printer() {}
MinifiedPrinter(std::ostream* o) : Printer(o) {}
void run(PassRunner* runner, Module* module) override {
PrintSExpression print(o);
print.setMinify(true);
print.visitModule(module);
}
};
Pass *createMinifiedPrinterPass() {
return new MinifiedPrinter();
}
// Prints out a module withough elision, i.e., the full ast
class FullPrinter : public Printer {
public:
FullPrinter() : Printer() {}
FullPrinter(std::ostream* o) : Printer(o) {}
void run(PassRunner* runner, Module* module) override {
PrintSExpression print(o);
print.setFull(true);
print.visitModule(module);
}
};
Pass *createFullPrinterPass() {
return new FullPrinter();
}
// Print individual expressions
std::ostream& WasmPrinter::printExpression(Expression* expression, std::ostream& o, bool minify, bool full) {
if (!expression) {
o << "(null expression)";
return o;
}
PrintSExpression print(o);
print.setMinify(minify);
if (full || isFullForced()) {
print.setFull(true);
o << "[" << printWasmType(expression->type) << "] ";
}
print.visit(expression);
return o;
}
} // namespace wasm
|