forked from WebAssembly/binaryen
/
wasm-s-parser.h
1349 lines (1264 loc) · 41.2 KB
/
wasm-s-parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Parses WebAssembly code in S-Expression format, as in .wast files
// such as are in the spec test suite.
//
#ifndef wasm_wasm_s_parser_h
#define wasm_wasm_s_parser_h
#include <cmath>
#include <cctype>
#include <limits>
#include "wasm.h"
#include "wasm-binary.h"
#include "shared-constants.h"
#include "asmjs/shared-constants.h"
#include "mixed_arena.h"
#include "parsing.h"
#include "asm_v_wasm.h"
#include "ast_utils.h"
#include "wasm-builder.h"
namespace wasm {
using namespace cashew;
// Globals
inline int unhex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
abort();
}
//
// An element in an S-Expression: a list or a string
//
class Element {
typedef ArenaVector<Element*> List;
bool isList_;
List list_;
IString str_;
bool dollared_;
public:
Element(MixedArena& allocator) : isList_(true), list_(allocator), line(-1), col(-1) {}
bool isList() { return isList_; }
bool isStr() { return !isList_; }
bool dollared() { return dollared_; }
size_t line, col;
// list methods
List& list() {
if (!isList()) throw ParseException("expected list", line, col);
return list_;
}
Element* operator[](unsigned i) {
if (i >= list().size()) throw ParseException("expected more elements in list", line, col);
return list()[i];
}
size_t size() {
return list().size();
}
// string methods
IString str() {
assert(!isList_);
return str_;
}
const char* c_str() {
assert(!isList_);
return str_.str;
}
Element* setString(IString str__, bool dollared__) {
isList_ = false;
str_ = str__;
dollared_ = dollared__;
return this;
}
Element* setMetadata(size_t line_, size_t col_) {
line = line_;
col = col_;
return this;
}
// printing
friend std::ostream& operator<<(std::ostream& o, Element& e) {
if (e.isList_) {
o << '(';
for (auto item : e.list_) o << ' ' << *item;
o << " )";
} else {
o << e.str_.str;
}
return o;
}
void dump() {
std::cout << "dumping " << this << " : " << *this << ".\n";
}
};
//
// Generic S-Expression parsing into lists
//
class SExpressionParser {
char* input;
size_t line;
char* lineStart;
MixedArena allocator;
public:
// Assumes control of and modifies the input.
SExpressionParser(char* input) : input(input) {
root = nullptr;
line = 0;
lineStart = input;
while (!root) { // keep parsing until we pass an initial comment
root = parse();
}
}
Element* root;
private:
Element* parse() {
std::vector<Element *> stack;
Element *curr = allocator.alloc<Element>();
while (1) {
skipWhitespace();
if (input[0] == 0) break;
if (input[0] == '(') {
input++;
stack.push_back(curr);
curr = allocator.alloc<Element>()->setMetadata(line, input - lineStart - 1);
} else if (input[0] == ')') {
input++;
auto last = curr;
curr = stack.back();
assert(stack.size());
stack.pop_back();
curr->list().push_back(last);
} else {
curr->list().push_back(parseString());
}
}
if (stack.size() != 0) throw ParseException("stack is not empty", curr->line, curr->col);
return curr;
}
void skipWhitespace() {
while (1) {
while (isspace(input[0])) {
if (input[0] == '\n') {
line++;
lineStart = input + 1;
}
input++;
}
if (input[0] == ';' && input[1] == ';') {
while (input[0] && input[0] != '\n') input++;
line++;
lineStart = input;
} else if (input[0] == '(' && input[1] == ';') {
// Skip nested block comments.
input += 2;
int depth = 1;
while (1) {
if (input[0] == 0) {
return;
}
if (input[0] == '(' && input[1] == ';') {
input += 2;
depth++;
} else if (input[0] == ';' && input[1] == ')') {
input += 2;
--depth;
if (depth == 0) {
break;
}
} else {
input++;
}
}
} else {
return;
}
}
}
Element* parseString() {
bool dollared = false;
if (input[0] == '$') {
input++;
dollared = true;
}
char *start = input;
if (input[0] == '"') {
// parse escaping \", but leave code escaped - we'll handle escaping in memory segments specifically
input++;
std::string str;
while (1) {
if (input[0] == '"') break;
if (input[0] == '\\') {
str += input[0];
str += input[1];
input += 2;
continue;
}
str += input[0];
input++;
}
input++;
return allocator.alloc<Element>()->setString(IString(str.c_str(), false), dollared)->setMetadata(line, start - lineStart);
}
while (input[0] && !isspace(input[0]) && input[0] != ')' && input[0] != '(' && input[0] != ';') input++;
if (start == input) throw ParseException("expected string", line, input - lineStart);
char temp = input[0];
input[0] = 0;
auto ret = allocator.alloc<Element>()->setString(IString(start, false), dollared)->setMetadata(line, start - lineStart);
input[0] = temp;
return ret;
}
};
//
// SExpressions => WebAssembly module
//
class SExpressionWasmBuilder {
Module& wasm;
MixedArena& allocator;
std::vector<Name> functionNames;
int functionCounter;
int importCounter;
std::map<Name, WasmType> functionTypes; // we need to know function return types before we parse their contents
public:
// Assumes control of and modifies the input.
SExpressionWasmBuilder(Module& wasm, Element& module) : wasm(wasm), allocator(wasm.allocator), importCounter(0) {
assert(module[0]->str() == MODULE);
if (module.size() > 1 && module[1]->isStr()) {
// these s-expressions contain a binary module, actually
std::vector<char> data;
size_t i = 1;
while (i < module.size()) {
auto str = module[i++]->c_str();
if (auto size = strlen(str)) {
stringToBinary(str, size, data);
}
}
WasmBinaryBuilder binaryBuilder(wasm, data, false);
binaryBuilder.read();
return;
}
functionCounter = 0;
for (unsigned i = 1; i < module.size(); i++) {
preParseFunctionType(*module[i]);
preParseImports(*module[i]);
}
functionCounter = 0;
for (unsigned i = 1; i < module.size(); i++) {
parseModuleElement(*module[i]);
}
}
private:
// pre-parse types and function definitions, so we know function return types before parsing their contents
void preParseFunctionType(Element& s) {
IString id = s[0]->str();
if (id == TYPE) return parseType(s);
if (id != FUNC) return;
size_t i = 1;
Name name, exportName;
i = parseFunctionNames(s, name, exportName);
if (!name.is()) {
// unnamed, use an index
name = Name::fromInt(functionCounter);
}
functionNames.push_back(name);
functionCounter++;
for (;i < s.size(); i++) {
Element& curr = *s[i];
IString id = curr[0]->str();
if (id == RESULT) {
functionTypes[name] = stringToWasmType(curr[1]->str());
return;
} else if (id == TYPE) {
Name typeName = curr[1]->str();
if (!wasm.checkFunctionType(typeName)) throw ParseException("unknown function");
FunctionType* type = wasm.getFunctionType(typeName);
functionTypes[name] = type->result;
return;
}
}
functionTypes[name] = none;
}
void preParseImports(Element& curr) {
IString id = curr[0]->str();
if (id == IMPORT) parseImport(curr);
}
void parseModuleElement(Element& curr) {
IString id = curr[0]->str();
if (id == START) return parseStart(curr);
if (id == FUNC) return parseFunction(curr);
if (id == MEMORY) return parseMemory(curr);
if (id == EXPORT) return parseExport(curr);
if (id == IMPORT) return; // already done
if (id == TABLE) return parseTable(curr);
if (id == TYPE) return; // already done
std::cerr << "bad module element " << id.str << '\n';
throw ParseException("unknown module element");
}
// function parsing state
std::unique_ptr<Function> currFunction;
std::map<Name, WasmType> currLocalTypes;
size_t localIndex; // params and vars
size_t otherIndex;
std::vector<Name> labelStack;
bool brokeToAutoBlock;
Name getPrefixedName(std::string prefix) {
return IString((prefix + std::to_string(otherIndex++)).c_str(), false);
}
Name getFunctionName(Element& s) {
if (s.dollared()) {
return s.str();
} else {
// index
size_t offset = atoi(s.str().c_str());
if (offset >= functionNames.size()) throw ParseException("unknown function");
return functionNames[offset];
}
}
void parseStart(Element& s) {
wasm.addStart(getFunctionName(*s[1]));
}
// returns the next index in s
size_t parseFunctionNames(Element& s, Name& name, Name& exportName) {
size_t i = 1;
while (i < s.size() && s[i]->isStr()) {
if (!s[i]->dollared()) {
// an export name
exportName = s[i]->str();
i++;
} else {
name = s[i]->str();
i++;
}
}
return i;
}
void parseFunction(Element& s) {
size_t i = 1;
Name name, exportName;
i = parseFunctionNames(s, name, exportName);
if (!name.is()) {
// unnamed, use an index
name = Name::fromInt(functionCounter);
}
if (exportName.is()) {
auto ex = make_unique<Export>();
ex->name = exportName;
ex->value = name;
wasm.addExport(ex.release());
}
functionCounter++;
Expression* body = nullptr;
localIndex = 0;
otherIndex = 0;
brokeToAutoBlock = false;
std::vector<NameType> typeParams; // we may have both params and a type. store the type info here
std::vector<NameType> params;
std::vector<NameType> vars;
WasmType result = none;
Name type;
Block* autoBlock = nullptr; // we may need to add a block for the very top level
auto makeFunction = [&]() {
currFunction = std::unique_ptr<Function>(Builder(wasm).makeFunction(
name,
std::move(params),
result,
std::move(vars)
));
};
auto ensureAutoBlock = [&]() {
if (!autoBlock) {
autoBlock = allocator.alloc<Block>();
autoBlock->list.push_back(body);
body = autoBlock;
}
};
for (;i < s.size(); i++) {
Element& curr = *s[i];
IString id = curr[0]->str();
if (id == PARAM || id == LOCAL) {
size_t j = 1;
while (j < curr.size()) {
IString name;
WasmType type = none;
if (!curr[j]->dollared()) { // dollared input symbols cannot be types
type = stringToWasmType(curr[j]->str(), true);
}
if (type != none) {
// a type, so an unnamed parameter
name = Name::fromInt(localIndex);
} else {
name = curr[j]->str();
type = stringToWasmType(curr[j+1]->str());
j++;
}
j++;
if (id == PARAM) {
params.emplace_back(name, type);
} else {
vars.emplace_back(name, type);
}
localIndex++;
currLocalTypes[name] = type;
}
} else if (id == RESULT) {
result = stringToWasmType(curr[1]->str());
} else if (id == TYPE) {
Name name = curr[1]->str();
type = name;
if (!wasm.checkFunctionType(name)) throw ParseException("unknown function");
FunctionType* type = wasm.getFunctionType(name);
result = type->result;
for (size_t j = 0; j < type->params.size(); j++) {
IString name = Name::fromInt(j);
WasmType currType = type->params[j];
typeParams.emplace_back(name, currType);
currLocalTypes[name] = currType;
}
} else {
// body
if (typeParams.size() > 0 && params.size() == 0) {
params = typeParams;
}
if (!currFunction) makeFunction();
Expression* ex = parseExpression(curr);
if (!body) {
body = ex;
} else {
ensureAutoBlock();
autoBlock->list.push_back(ex);
}
}
}
if (brokeToAutoBlock) {
ensureAutoBlock();
autoBlock->name = FAKE_RETURN;
}
if (autoBlock) {
autoBlock->finalize();
}
if (!currFunction) {
makeFunction();
body = allocator.alloc<Nop>();
}
if (currFunction->result != result) throw ParseException("bad func declaration", s.line, s.col);
/* TODO: spec in flux, https://github.com/WebAssembly/spec/pull/301
if (type.isNull()) {
// if no function type provided, generate a private one for this function
auto* functionType = sigToFunctionType(getSig(currFunction.get()));
wasm.addFunctionType(functionType);
type = functionType->name;
}
*/
currFunction->body = body;
currFunction->type = type;
wasm.addFunction(currFunction.release());
currLocalTypes.clear();
labelStack.clear();
}
WasmType stringToWasmType(IString str, bool allowError=false, bool prefix=false) {
return stringToWasmType(str.str, allowError, prefix);
}
WasmType stringToWasmType(const char* str, bool allowError=false, bool prefix=false) {
if (str[0] == 'i') {
if (str[1] == '3' && str[2] == '2' && (prefix || str[3] == 0)) return i32;
if (str[1] == '6' && str[2] == '4' && (prefix || str[3] == 0)) return i64;
}
if (str[0] == 'f') {
if (str[1] == '3' && str[2] == '2' && (prefix || str[3] == 0)) return f32;
if (str[1] == '6' && str[2] == '4' && (prefix || str[3] == 0)) return f64;
}
if (allowError) return none;
throw ParseException("unknown type");
abort();
}
public:
Expression* parseExpression(Element* s) {
return parseExpression(*s);
}
#define abort_on(str) { throw ParseException(std::string("abort_on ") + str); }
Expression* parseExpression(Element& s) {
IString id = s[0]->str();
const char *str = id.str;
const char *dot = strchr(str, '.');
if (dot) {
// type.operation (e.g. i32.add)
WasmType type = stringToWasmType(str, false, true);
// Local copy to index into op without bounds checking.
enum { maxNameSize = 15 };
char op[maxNameSize + 1] = {'\0'};
strncpy(op, dot + 1, maxNameSize);
#define BINARY_INT_OR_FLOAT(op) (type == i32 ? BinaryOp::op##Int32 : (type == i64 ? BinaryOp::op##Int64 : (type == f32 ? BinaryOp::op##Float32 : BinaryOp::op##Float64)))
#define BINARY_INT(op) (type == i32 ? BinaryOp::op##Int32 : BinaryOp::op##Int64)
#define BINARY_FLOAT(op) (type == f32 ? BinaryOp::op##Float32 : BinaryOp::op##Float64)
switch (op[0]) {
case 'a': {
if (op[1] == 'b') return makeUnary(s, type == f32 ? UnaryOp::AbsFloat32 : UnaryOp::AbsFloat64, type);
if (op[1] == 'd') return makeBinary(s, BINARY_INT_OR_FLOAT(Add), type);
if (op[1] == 'n') return makeBinary(s, BINARY_INT(And), type);
abort_on(op);
}
case 'c': {
if (op[1] == 'e') return makeUnary(s, type == f32 ? UnaryOp::CeilFloat32 : UnaryOp::CeilFloat64, type);
if (op[1] == 'l') return makeUnary(s, type == i32 ? UnaryOp::ClzInt32 : UnaryOp::ClzInt64, type);
if (op[1] == 'o') {
if (op[2] == 'p') return makeBinary(s, BINARY_FLOAT(CopySign), type);
if (op[2] == 'n') {
if (op[3] == 'v') {
if (op[8] == 's') return makeUnary(s, op[11] == '3' ? (type == f32 ? UnaryOp::ConvertSInt32ToFloat32 : UnaryOp::ConvertSInt32ToFloat64) : (type == f32 ? UnaryOp::ConvertSInt64ToFloat32 : UnaryOp::ConvertSInt64ToFloat64), type);
if (op[8] == 'u') return makeUnary(s, op[11] == '3' ? (type == f32 ? UnaryOp::ConvertUInt32ToFloat32 : UnaryOp::ConvertUInt32ToFloat64) : (type == f32 ? UnaryOp::ConvertUInt64ToFloat32 : UnaryOp::ConvertUInt64ToFloat64), type);
}
if (op[3] == 's') return makeConst(s, type);
}
}
if (op[1] == 't') return makeUnary(s, type == i32 ? UnaryOp::CtzInt32 : UnaryOp::CtzInt64, type);
abort_on(op);
}
case 'd': {
if (op[1] == 'i') {
if (op[3] == '_') return makeBinary(s, op[4] == 'u' ? BINARY_INT(DivU) : BINARY_INT(DivS), type);
if (op[3] == 0) return makeBinary(s, BINARY_FLOAT(Div), type);
}
if (op[1] == 'e') return makeUnary(s, UnaryOp::DemoteFloat64, type);
abort_on(op);
}
case 'e': {
if (op[1] == 'q') {
if (op[2] == 0) return makeBinary(s, BINARY_INT_OR_FLOAT(Eq), type);
if (op[2] == 'z') return makeUnary(s, type == i32 ? UnaryOp::EqZInt32 : UnaryOp::EqZInt64, type);
}
if (op[1] == 'x') return makeUnary(s, op[7] == 'u' ? UnaryOp::ExtendUInt32 : UnaryOp::ExtendSInt32, type);
abort_on(op);
}
case 'f': {
if (op[1] == 'l') return makeUnary(s, type == f32 ? UnaryOp::FloorFloat32 : UnaryOp::FloorFloat64, type);
abort_on(op);
}
case 'g': {
if (op[1] == 't') {
if (op[2] == '_') return makeBinary(s, op[3] == 'u' ? BINARY_INT(GtU) : BINARY_INT(GtS), type);
if (op[2] == 0) return makeBinary(s, BINARY_FLOAT(Gt), type);
}
if (op[1] == 'e') {
if (op[2] == '_') return makeBinary(s, op[3] == 'u' ? BINARY_INT(GeU) : BINARY_INT(GeS), type);
if (op[2] == 0) return makeBinary(s, BINARY_FLOAT(Ge), type);
}
abort_on(op);
}
case 'l': {
if (op[1] == 't') {
if (op[2] == '_') return makeBinary(s, op[3] == 'u' ? BINARY_INT(LtU) : BINARY_INT(LtS), type);
if (op[2] == 0) return makeBinary(s, BINARY_FLOAT(Lt), type);
}
if (op[1] == 'e') {
if (op[2] == '_') return makeBinary(s, op[3] == 'u' ? BINARY_INT(LeU) : BINARY_INT(LeS), type);
if (op[2] == 0) return makeBinary(s, BINARY_FLOAT(Le), type);
}
if (op[1] == 'o') return makeLoad(s, type);
abort_on(op);
}
case 'm': {
if (op[1] == 'i') return makeBinary(s, BINARY_FLOAT(Min), type);
if (op[1] == 'a') return makeBinary(s, BINARY_FLOAT(Max), type);
if (op[1] == 'u') return makeBinary(s, BINARY_INT_OR_FLOAT(Mul), type);
abort_on(op);
}
case 'n': {
if (op[1] == 'e') {
if (op[2] == 0) return makeBinary(s, BINARY_INT_OR_FLOAT(Ne), type);
if (op[2] == 'a') return makeUnary(s, type == f32 ? UnaryOp::NearestFloat32 : UnaryOp::NearestFloat64, type);
if (op[2] == 'g') return makeUnary(s, type == f32 ? UnaryOp::NegFloat32 : UnaryOp::NegFloat64, type);
}
abort_on(op);
}
case 'o': {
if (op[1] == 'r') return makeBinary(s, BINARY_INT(Or), type);
abort_on(op);
}
case 'p': {
if (op[1] == 'r') return makeUnary(s, UnaryOp::PromoteFloat32, type);
if (op[1] == 'o') return makeUnary(s, type == i32 ? UnaryOp::PopcntInt32 : UnaryOp::PopcntInt64, type);
abort_on(op);
}
case 'r': {
if (op[1] == 'e') {
if (op[2] == 'm') return makeBinary(s, op[4] == 'u' ? BINARY_INT(RemU) : BINARY_INT(RemS), type);
if (op[2] == 'i') return makeUnary(s, isWasmTypeFloat(type) ? (type == f32 ? UnaryOp::ReinterpretInt32 : UnaryOp::ReinterpretInt64) : (type == i32 ? UnaryOp::ReinterpretFloat32 : UnaryOp::ReinterpretFloat64), type);
}
if (op[1] == 'o' && op[2] == 't') {
return makeBinary(s, op[3] == 'l' ? BINARY_INT(RotL) : BINARY_INT(RotR), type);
}
abort_on(op);
}
case 's': {
if (op[1] == 'h') {
if (op[2] == 'l') return makeBinary(s, BINARY_INT(Shl), type);
return makeBinary(s, op[4] == 'u' ? BINARY_INT(ShrU) : BINARY_INT(ShrS), type);
}
if (op[1] == 'u') return makeBinary(s, BINARY_INT_OR_FLOAT(Sub), type);
if (op[1] == 'q') return makeUnary(s, type == f32 ? UnaryOp::SqrtFloat32 : UnaryOp::SqrtFloat64, type);
if (op[1] == 't') return makeStore(s, type);
abort_on(op);
}
case 't': {
if (op[1] == 'r') {
if (op[6] == 's') return makeUnary(s, op[9] == '3' ? (type == i32 ? UnaryOp::TruncSFloat32ToInt32 : UnaryOp::TruncSFloat32ToInt64) : (type == i32 ? UnaryOp::TruncSFloat64ToInt32 : UnaryOp::TruncSFloat64ToInt64), type);
if (op[6] == 'u') return makeUnary(s, op[9] == '3' ? (type == i32 ? UnaryOp::TruncUFloat32ToInt32 : UnaryOp::TruncUFloat32ToInt64) : (type == i32 ? UnaryOp::TruncUFloat64ToInt32 : UnaryOp::TruncUFloat64ToInt64), type);
if (op[2] == 'u') return makeUnary(s, type == f32 ? UnaryOp::TruncFloat32 : UnaryOp::TruncFloat64, type);
}
abort_on(op);
}
case 'w': {
if (op[1] == 'r') return makeUnary(s, UnaryOp::WrapInt64, type);
abort_on(op);
}
case 'x': {
if (op[1] == 'o') return makeBinary(s, BINARY_INT(Xor), type);
abort_on(op);
}
default: abort_on(op);
}
} else {
// other expression
switch (str[0]) {
case 'b': {
if (str[1] == 'l') return makeBlock(s);
if (str[1] == 'r') {
if (str[2] == '_' && str[3] == 't') return makeBreakTable(s);
return makeBreak(s);
}
abort_on(str);
}
case 'c': {
if (str[1] == 'a') {
if (id == CALL) return makeCall(s);
if (id == CALL_IMPORT) return makeCallImport(s);
if (id == CALL_INDIRECT) return makeCallIndirect(s);
} else if (str[1] == 'u') return makeHost(s, HostOp::CurrentMemory);
abort_on(str);
}
case 'e': {
if (str[1] == 'l') return makeThenOrElse(s);
abort_on(str);
}
case 'g': {
if (str[1] == 'e') return makeGetLocal(s);
if (str[1] == 'r') return makeHost(s, HostOp::GrowMemory);
abort_on(str);
}
case 'h': {
if (str[1] == 'a') return makeHost(s, HostOp::HasFeature);
abort_on(str);
}
case 'i': {
if (str[1] == 'f') return makeIf(s);
abort_on(str);
}
case 'l': {
if (str[1] == 'o') return makeLoop(s);
abort_on(str);
}
case 'n': {
if (str[1] == 'o') return allocator.alloc<Nop>();
abort_on(str);
}
case 'p': {
if (str[1] == 'a') return makeHost(s, HostOp::PageSize);
abort_on(str);
}
case 's': {
if (str[1] == 'e' && str[2] == 't') return makeSetLocal(s);
if (str[1] == 'e' && str[2] == 'l') return makeSelect(s);
abort_on(str);
}
case 'r': {
if (str[1] == 'e') return makeReturn(s);
abort_on(str);
}
case 't': {
if (str[1] == 'h') return makeThenOrElse(s);
abort_on(str);
}
case 'u': {
if (str[1] == 'n') return allocator.alloc<Unreachable>();
abort_on(str);
}
default: abort_on(str);
}
}
abort();
}
private:
Expression* makeBinary(Element& s, BinaryOp op, WasmType type) {
auto ret = allocator.alloc<Binary>();
ret->op = op;
ret->left = parseExpression(s[1]);
ret->right = parseExpression(s[2]);
ret->finalize();
return ret;
}
Expression* makeUnary(Element& s, UnaryOp op, WasmType type) {
auto ret = allocator.alloc<Unary>();
ret->op = op;
ret->value = parseExpression(s[1]);
ret->finalize();
// type is the reported type, e.g. i64.ctz reports i64 (but has a return type of i32, in this case)
// verify the reported type is correct
switch (op) {
case EqZInt32:
case NegFloat32:
case AbsFloat32:
case CeilFloat32:
case FloorFloat32:
case TruncFloat32:
case NearestFloat32:
case SqrtFloat32:
case ClzInt32:
case CtzInt32:
case PopcntInt32:
case EqZInt64:
case NegFloat64:
case AbsFloat64:
case CeilFloat64:
case FloorFloat64:
case TruncFloat64:
case NearestFloat64:
case SqrtFloat64:
case ClzInt64:
case CtzInt64:
case PopcntInt64: {
if (ret->value->type != unreachable && type != ret->value->type) throw ParseException(std::string("bad type for ") + getExpressionName(ret) + ": " + printWasmType(type) + " vs value type " + printWasmType(ret->value->type), s.line, s.col);
break;
}
case ExtendSInt32: case ExtendUInt32:
case WrapInt64:
case PromoteFloat32:
case DemoteFloat64:
case TruncSFloat32ToInt32:
case TruncUFloat32ToInt32:
case TruncSFloat64ToInt32:
case TruncUFloat64ToInt32:
case ReinterpretFloat32:
case TruncSFloat32ToInt64:
case TruncUFloat32ToInt64:
case TruncSFloat64ToInt64:
case TruncUFloat64ToInt64:
case ReinterpretFloat64:
case ReinterpretInt32:
case ConvertSInt32ToFloat32:
case ConvertUInt32ToFloat32:
case ConvertSInt64ToFloat32:
case ConvertUInt64ToFloat32:
case ReinterpretInt64:
case ConvertSInt32ToFloat64:
case ConvertUInt32ToFloat64:
case ConvertSInt64ToFloat64:
case ConvertUInt64ToFloat64: break;
default: WASM_UNREACHABLE();
}
return ret;
}
Expression* makeSelect(Element& s) {
auto ret = allocator.alloc<Select>();
ret->ifTrue = parseExpression(s[1]);
ret->ifFalse = parseExpression(s[2]);
ret->condition = parseExpression(s[3]);
ret->finalize();
return ret;
}
Expression* makeHost(Element& s, HostOp op) {
auto ret = allocator.alloc<Host>();
ret->op = op;
if (op == HostOp::HasFeature) {
ret->nameOperand = s[1]->str();
} else {
parseCallOperands(s, 1, ret);
}
ret->finalize();
return ret;
}
Index getLocalIndex(Element& s) {
if (s.dollared()) {
auto ret = s.str();
if (currFunction->localIndices.count(ret) == 0) throw ParseException("bad local name", s.line, s.col);
return currFunction->getLocalIndex(ret);
}
// this is a numeric index
Index ret = atoi(s.c_str());
if (ret >= currFunction->getNumLocals()) throw ParseException("bad local index", s.line, s.col);
return ret;
}
Expression* makeGetLocal(Element& s) {
auto ret = allocator.alloc<GetLocal>();
ret->index = getLocalIndex(*s[1]);
ret->type = currFunction->getLocalType(ret->index);
return ret;
}
Expression* makeSetLocal(Element& s) {
auto ret = allocator.alloc<SetLocal>();
ret->index = getLocalIndex(*s[1]);
ret->value = parseExpression(s[2]);
ret->type = currFunction->getLocalType(ret->index);
return ret;
}
Expression* makeBlock(Element& s) {
// special-case Block, because Block nesting (in their first element) can be incredibly deep
auto curr = allocator.alloc<Block>();
auto* sp = &s;
std::vector<std::pair<Element*, Block*>> stack;
while (1) {
stack.emplace_back(sp, curr);
auto& s = *sp;
size_t i = 1;
if (i < s.size() && s[i]->isStr()) {
curr->name = s[i]->str();
i++;
} else {
curr->name = getPrefixedName("block");
}
labelStack.push_back(curr->name);
if (i >= s.size()) break; // labeled empty block
auto& first = *s[i];
if (first[0]->str() == BLOCK) {
// recurse
curr = allocator.alloc<Block>();
sp = &first;
continue;
}
break;
}
// we now have a stack of Blocks, with their labels, but no contents yet
for (int t = int(stack.size()) - 1; t >= 0; t--) {
auto* sp = stack[t].first;
auto* curr = stack[t].second;
auto& s = *sp;
size_t i = 1;
if (i < s.size()) {
if (s[i]->isStr()) {
i++;
}
if (t < int(stack.size()) - 1) {
// first child is one of our recursions
curr->list.push_back(stack[t + 1].second);
i++;
}
for (; i < s.size(); i++) {
curr->list.push_back(parseExpression(s[i]));
}
}
assert(labelStack.back() == curr->name);
labelStack.pop_back();
curr->finalize();
}
return stack[0].second;
}
// Similar to block, but the label is handled by the enclosing if (since there might not be a then or else, ick)
Expression* makeThenOrElse(Element& s) {
auto ret = allocator.alloc<Block>();
size_t i = 1;
if (s[1]->isStr()) {
i++;
}
for (; i < s.size(); i++) {
ret->list.push_back(parseExpression(s[i]));
}
ret->finalize();
return ret;
}
Expression* makeConst(Element& s, WasmType type) {
auto ret = parseConst(s[1]->str(), type, allocator);
if (!ret) throw ParseException("bad const");
return ret;
}
Expression* makeLoad(Element& s, WasmType type) {
const char *extra = strchr(s[0]->c_str(), '.') + 5; // after "type.load"
auto ret = allocator.alloc<Load>();
ret->type = type;
ret->bytes = getWasmTypeSize(type);
if (extra[0] == '8') {
ret->bytes = 1;
extra++;
} else if (extra[0] == '1') {
assert(extra[1] == '6');
ret->bytes = 2;
extra += 2;
} else if (extra[0] == '3') {
assert(extra[1] == '2');
ret->bytes = 4;
extra += 2;
}
ret->signed_ = extra[0] && extra[1] == 's';
size_t i = 1;
ret->offset = 0;
ret->align = ret->bytes;
while (!s[i]->isList()) {
const char *str = s[i]->c_str();
const char *eq = strchr(str, '=');
assert(eq);
eq++;
if (str[0] == 'a') {
ret->align = atoi(eq);
} else if (str[0] == 'o') {
uint64_t offset = atoll(eq);
if (offset > std::numeric_limits<uint32_t>::max()) throw ParseException("bad offset");
ret->offset = (uint32_t)offset;
} else throw ParseException("bad load attribute");
i++;
}
ret->ptr = parseExpression(s[i]);
return ret;
}
Expression* makeStore(Element& s, WasmType type) {
const char *extra = strchr(s[0]->c_str(), '.') + 6; // after "type.store"
auto ret = allocator.alloc<Store>();
ret->type = type;
ret->bytes = getWasmTypeSize(type);
if (extra[0] == '8') {
ret->bytes = 1;
extra++;
} else if (extra[0] == '1') {
assert(extra[1] == '6');
ret->bytes = 2;
extra += 2;
} else if (extra[0] == '3') {
assert(extra[1] == '2');
ret->bytes = 4;
extra += 2;
}