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
|
//
// .s to WebAssembly translator.
//
#include "wasm.h"
#include "parsing.h"
namespace wasm {
extern int debug; // wasm::debug is set in main(), typically from an env var
//
// S2WasmBuilder - parses a .s file into WebAssembly
//
class S2WasmBuilder {
AllocatingModule& wasm;
MixedArena& allocator;
char *s;
public:
S2WasmBuilder(AllocatingModule& wasm, char *s) : wasm(wasm), allocator(wasm.allocator), s(s) {
process();
}
private:
// utilities
void skipWhitespace() {
while (1) {
while (*s && isspace(*s)) s++;
if (*s != '#') break;
while (*s != '\n') s++;
}
}
bool skipComma() {
skipWhitespace();
if (*s != ',') return false;
s++;
skipWhitespace();
return true;
}
void findComma() {
while (*s && *s != ',') s++;
s++;
skipWhitespace();
}
// match and skip the pattern, if matched
bool match(const char *pattern) {
size_t size = strlen(pattern);
if (strncmp(s, pattern, size) == 0) {
s += size;
skipWhitespace();
return true;
}
return false;
}
void mustMatch(const char *pattern) {
bool matched = match(pattern);
assert(matched);
}
#define abort_on(why) { \
printf("%s : %20s\n", why, s); \
abort(); \
}
void dump(const char *text) {
std::cerr << text << "\n==========\n" << s << "\n==========\n";
}
void unget(Name str) {
s -= strlen(str.str);
}
Name getStr() {
std::string str;
while (*s && !isspace(*s)) {
str += *s;
s++;
}
return cashew::IString(str.c_str(), false);
}
Name getStrToComma() {
std::string str;
while (*s && !isspace(*s) && *s != ',') {
str += *s;
s++;
}
return cashew::IString(str.c_str(), false);
}
Name getCommaSeparated() {
skipWhitespace();
std::string str;
while (*s && *s != ',') {
str += *s;
s++;
}
skipWhitespace();
return cashew::IString(str.c_str(), false);
}
WasmType getType() {
if (match("i32")) return i32;
if (match("i64")) return i64;
if (match("f32")) return f32;
if (match("f64")) return f64;
abort_on("getType");
}
// state
typedef std::pair<Const*, Name> Addressing;
std::vector<Addressing> addressings;
// processors
void process() {
while (*s) {
skipWhitespace();
if (!*s) break;
if (*s != '.') break;
s++;
if (match("text")) parseText();
else if (match("data")) parseData();
else abort_on(s);
}
}
void parseText() {
while (*s) {
skipWhitespace();
if (!*s) break;
if (*s != '.') break;
s++;
if (match("file")) parseFile();
else if (match("globl")) parseGlobl();
else abort_on(s);
}
}
void parseFile() {
assert(*s == '"');
s++;
std::string filename;
while (*s != '"') {
filename += *s;
s++;
}
s++;
// TODO: use the filename?
}
void parseGlobl() {
unsigned nextId = 0;
auto getNextId = [&nextId]() {
return cashew::IString(('$' + std::to_string(nextId++)).c_str(), false);
};
Name name = getStr();
skipWhitespace();
mustMatch(".type");
mustMatch(name.str);
mustMatch(",@function");
mustMatch(name.str);
mustMatch(":");
auto func = allocator.alloc<Function>();
std::map<Name, WasmType> localTypes;
// params and result
while (1) {
if (match(".param")) {
while (1) {
Name name = getNextId();
WasmType type = getType();
func->params.emplace_back(name, type);
localTypes[name] = type;
skipWhitespace();
if (!match(",")) break;
}
} else if (match(".result")) {
func->result = getType();
} else break;
}
// parse body
auto currBlock = allocator.alloc<Block>();
func->body = currBlock;
std::vector<Expression*> stack;
auto push = [&](Expression* curr) {
stack.push_back(curr);
};
auto pop = [&]() {
Expression* ret = stack.back();
stack.pop_back();
return ret;
};
auto getInput = [&]() {
if (match("$pop")) {
while (isdigit(*s)) s++;
return pop();
} else {
auto curr = allocator.alloc<GetLocal>();
curr->name = getStrToComma();
curr->type = localTypes[curr->name];
return (Expression*)curr;
}
};
auto setOutput = [&](Expression* curr, Name assign) {
if (assign.str[1] == 'p') { // push
stack.push_back(curr);
} else if (assign.str[1] == 'd') { // discard
currBlock->list.push_back(curr);
} else { // set to a local
auto set = allocator.alloc<SetLocal>();
set->name = assign;
set->value = curr;
set->type = curr->type;
currBlock->list.push_back(set);
}
};
auto makeBinary = [&](BinaryOp op, WasmType type) {
Name assign = getCommaSeparated();
skipComma();
auto curr = allocator.alloc<Binary>();
curr->op = op;
curr->right = getInput();
skipComma();
curr->left = getInput();
curr->finalize();
assert(curr->type == type);
setOutput(curr, assign);
};
// main loop
while (1) {
skipWhitespace();
if (match("i32.")) {
switch (*s) {
case 'a': {
if (match("add")) makeBinary(BinaryOp::Add, i32);
else if (match("and")) makeBinary(BinaryOp::And, i32);
else abort_on("i32.a");
break;
}
case 'c': {
if (match("const")) {
mustMatch("$push");
findComma();
if (*s == '.') {
// global address
auto curr = allocator.alloc<Const>();
curr->type = i32;
addressings.emplace_back(curr, getStr());
push(curr);
} else {
// constant
push(parseConst(getStr(), i32, allocator));
}
} else abort_on("i32.c");
break;
}
case 'n': {
if (match("ne")) makeBinary(BinaryOp::Ne, i32);
else abort_on("i32.n");
break;
}
case 's': {
if (match("shr_s")) makeBinary(BinaryOp::ShrS, i32);
else if (match("shr_u")) makeBinary(BinaryOp::ShrU, i32);
else if (match("sub")) makeBinary(BinaryOp::Sub, i32);
else abort_on("i32.s");
break;
}
default: abort_on("i32.?");
}
} else if (match("call")) {
Name assign = getCommaSeparated();
skipComma();
auto curr = allocator.alloc<Call>();
curr->target = getCommaSeparated();
while (1) {
if (!skipComma()) break;
curr->operands.push_back(getInput());
}
std::reverse(curr->operands.begin(), curr->operands.end());
setOutput(curr, assign);
} else if (match("return")) {
Block *temp;
if (!(func->body && (temp = func->body->dyn_cast<Block>()) && temp->name == FAKE_RETURN)) {
Expression* old = func->body;
temp = allocator.alloc<Block>();
temp->name = FAKE_RETURN;
if (old) temp->list.push_back(old);
func->body = temp;
}
auto curr = allocator.alloc<Break>();
curr->name = FAKE_RETURN;
if (*s == '$') {
getStr();
curr->value = pop();
}
currBlock->list.push_back(curr);
} else if (match("func_end0:")) {
mustMatch(".size");
mustMatch("main,");
mustMatch("func_end0-main");
wasm.addFunction(func);
return; // the function is done
} else {
abort_on("function element");
}
}
}
void parseData() {
abort();
}
};
} // namespace wasm
|