forked from ucsd-cse231-s22/chocopy-wasm-compiler-A
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.ts
292 lines (263 loc) · 8.72 KB
/
compiler.ts
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
import { Program, Stmt, Expr, Value, Class, VarInit, FunDef } from "./ir"
import { BinOp, Type, UniOp } from "./ast"
import { BOOL, NONE, NUM } from "./utils";
export type GlobalEnv = {
globals: Map<string, boolean>;
classes: Map<string, Map<string, [number, Value<Type>]>>;
locals: Set<string>;
labels: Array<string>;
offset: number;
}
export const emptyEnv : GlobalEnv = {
globals: new Map(),
classes: new Map(),
locals: new Set(),
labels: [],
offset: 0
};
type CompileResult = {
globals: string[],
functions: string,
mainSource: string,
newEnv: GlobalEnv
};
export function makeLocals(locals: Set<string>) : Array<string> {
const localDefines : Array<string> = [];
locals.forEach(v => {
localDefines.push(`(local $${v} i32)`);
});
return localDefines;
}
export function compile(ast: Program<Type>, env: GlobalEnv) : CompileResult {
const withDefines = env;
const definedVars : Set<string> = new Set(); //getLocals(ast);
definedVars.add("$last");
definedVars.add("$selector");
definedVars.forEach(env.locals.add, env.locals);
const localDefines = makeLocals(definedVars);
const globalNames = ast.inits.map(init => init.name);
console.log(ast.inits, globalNames);
const funs : Array<string> = [];
ast.funs.forEach(f => {
funs.push(codeGenDef(f, withDefines).join("\n"));
});
const classes : Array<string> = ast.classes.map(cls => codeGenClass(cls, withDefines)).flat();
const allFuns = funs.concat(classes).join("\n\n");
// const stmts = ast.filter((stmt) => stmt.tag !== "fun");
const inits = ast.inits.map(init => codeGenInit(init, withDefines)).flat();
withDefines.labels = ast.body.map(block => block.label);
var bodyCommands = "(local.set $$selector (i32.const 0))\n"
bodyCommands += "(loop $loop\n"
var blockCommands = "(local.get $$selector)\n"
blockCommands += `(br_table ${ast.body.map(block => block.label).join(" ")})`;
ast.body.forEach(block => {
blockCommands = `(block ${block.label}
${blockCommands}
) ;; end ${block.label}
${block.stmts.map(stmt => codeGenStmt(stmt, withDefines).join('\n')).join('\n')}
`
})
bodyCommands += blockCommands;
bodyCommands += ") ;; end $loop"
// const commandGroups = ast.stmts.map((stmt) => codeGenStmt(stmt, withDefines));
const allCommands = [...localDefines, ...inits, bodyCommands];
withDefines.locals.clear();
return {
globals: globalNames,
functions: allFuns,
mainSource: allCommands.join("\n"),
newEnv: withDefines
};
}
function codeGenStmt(stmt: Stmt<Type>, env: GlobalEnv): Array<string> {
switch (stmt.tag) {
case "store":
return [
...codeGenValue(stmt.start, env),
...codeGenValue(stmt.offset, env),
...codeGenValue(stmt.value, env),
`call $store`
]
case "assign":
var valStmts = codeGenExpr(stmt.value, env);
if (env.locals.has(stmt.name)) {
return valStmts.concat([`(local.set $${stmt.name})`]);
} else {
return valStmts.concat([`(global.set $${stmt.name})`]);
}
case "return":
var valStmts = codeGenValue(stmt.value, env);
valStmts.push("return");
return valStmts;
case "expr":
var exprStmts = codeGenExpr(stmt.expr, env);
return exprStmts.concat([`(local.set $$last)`]);
case "pass":
return []
case "ifjmp":
const thnIdx = env.labels.findIndex(e => e === stmt.thn);
const elsIdx = env.labels.findIndex(e => e === stmt.els);
return [...codeGenValue(stmt.cond, env),
`(if
(then
(local.set $$selector (i32.const ${thnIdx}))
(br $loop)
)
(else
(local.set $$selector (i32.const ${elsIdx}))
(br $loop)
)
)`]
case "jmp":
const lblIdx = env.labels.findIndex(e => e === stmt.lbl);
return [`(local.set $$selector (i32.const ${lblIdx}))`, `(br $loop)`]
}
}
function codeGenExpr(expr: Expr<Type>, env: GlobalEnv): Array<string> {
switch (expr.tag) {
case "value":
return codeGenValue(expr.value, env)
case "binop":
const lhsStmts = codeGenValue(expr.left, env);
const rhsStmts = codeGenValue(expr.right, env);
return [...lhsStmts, ...rhsStmts, codeGenBinOp(expr.op)]
case "uniop":
const exprStmts = codeGenValue(expr.expr, env);
switch(expr.op){
case UniOp.Neg:
return [`(i32.const 0)`, ...exprStmts, `(i32.sub)`];
case UniOp.Not:
return [`(i32.const 0)`, ...exprStmts, `(i32.eq)`];
}
case "builtin1":
const argTyp = expr.a;
const argStmts = codeGenValue(expr.arg, env);
var callName = expr.name;
if (expr.name === "print" && argTyp === NUM) {
callName = "print_num";
} else if (expr.name === "print" && argTyp === BOOL) {
callName = "print_bool";
} else if (expr.name === "print" && argTyp === NONE) {
callName = "print_none";
}
return argStmts.concat([`(call $${callName})`]);
case "builtin2":
const leftStmts = codeGenValue(expr.left, env);
const rightStmts = codeGenValue(expr.right, env);
return [...leftStmts, ...rightStmts, `(call $${expr.name})`]
case "call":
var valStmts = expr.arguments.map((arg) => codeGenValue(arg, env)).flat();
valStmts.push(`(call $${expr.name})`);
return valStmts;
case "alloc":
return [
...codeGenValue(expr.amount, env),
`call $alloc`
];
case "load":
return [
...codeGenValue(expr.start, env),
`call $assert_not_none`,
...codeGenValue(expr.offset, env),
`call $load`
]
}
}
function codeGenValue(val: Value<Type>, env: GlobalEnv): Array<string> {
switch (val.tag) {
case "num":
return ["(i32.const " + val.value + ")"];
case "wasmint":
return ["(i32.const " + val.value + ")"];
case "bool":
return [`(i32.const ${Number(val.value)})`];
case "none":
return [`(i32.const 0)`];
case "id":
if (env.locals.has(val.name)) {
return [`(local.get $${val.name})`];
} else {
return [`(global.get $${val.name})`];
}
}
}
function codeGenBinOp(op : BinOp) : string {
switch(op) {
case BinOp.Plus:
return "(i32.add)"
case BinOp.Minus:
return "(i32.sub)"
case BinOp.Mul:
return "(i32.mul)"
case BinOp.IDiv:
return "(i32.div_s)"
case BinOp.Mod:
return "(i32.rem_s)"
case BinOp.Eq:
return "(i32.eq)"
case BinOp.Neq:
return "(i32.ne)"
case BinOp.Lte:
return "(i32.le_s)"
case BinOp.Gte:
return "(i32.ge_s)"
case BinOp.Lt:
return "(i32.lt_s)"
case BinOp.Gt:
return "(i32.gt_s)"
case BinOp.Is:
return "(i32.eq)";
case BinOp.And:
return "(i32.and)"
case BinOp.Or:
return "(i32.or)"
}
}
function codeGenInit(init : VarInit<Type>, env : GlobalEnv) : Array<string> {
const value = codeGenValue(init.value, env);
if (env.locals.has(init.name)) {
return [...value, `(local.set $${init.name})`];
} else {
return [...value, `(global.set $${init.name})`];
}
}
function codeGenDef(def : FunDef<Type>, env : GlobalEnv) : Array<string> {
var definedVars : Set<string> = new Set();
def.inits.forEach(v => definedVars.add(v.name));
definedVars.add("$last");
definedVars.add("$selector");
// def.parameters.forEach(p => definedVars.delete(p.name));
definedVars.forEach(env.locals.add, env.locals);
def.parameters.forEach(p => env.locals.add(p.name));
env.labels = def.body.map(block => block.label);
const localDefines = makeLocals(definedVars);
const locals = localDefines.join("\n");
const inits = def.inits.map(init => codeGenInit(init, env)).flat().join("\n");
var params = def.parameters.map(p => `(param $${p.name} i32)`).join(" ");
var bodyCommands = "(local.set $$selector (i32.const 0))\n"
bodyCommands += "(loop $loop\n"
var blockCommands = "(local.get $$selector)\n"
blockCommands += `(br_table ${def.body.map(block => block.label).join(" ")})`;
def.body.forEach(block => {
blockCommands = `(block ${block.label}
${blockCommands}
) ;; end ${block.label}
${block.stmts.map(stmt => codeGenStmt(stmt, env).join('\n')).join('\n')}
`
})
bodyCommands += blockCommands;
bodyCommands += ") ;; end $loop"
env.locals.clear();
return [`(func $${def.name} ${params} (result i32)
${locals}
${inits}
${bodyCommands}
(i32.const 0)
(return))`];
}
function codeGenClass(cls : Class<Type>, env : GlobalEnv) : Array<string> {
const methods = [...cls.methods];
methods.forEach(method => method.name = `${cls.name}$${method.name}`);
const result = methods.map(method => codeGenDef(method, env));
return result.flat();
}