-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.ts
306 lines (288 loc) · 10.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { Stmt, Expr, Type, FuncDef, Parameter, VarDef, ProgramBody } from "./ast";
import { parse } from "./parser";
import { tcProgram } from "./tc";
// https://learnxinyminutes.com/docs/wasm/
type LocalEnv = Map<string, boolean>;
type CompileResult = {
wasmSource: string,
};
type Counter = {val: number}
function codeGenParams(params: Parameter<any>[]): Array<string> {
const paramCommands: string[] = params.map(p => {
return `(param $${p.name} i32)`;
});
return paramCommands;
}
function codeGenFuncReturnType(RetType: Type): Array<string> {
switch(RetType){
case "none":
return [];
case "int":
return [`(result i32)`];
case "bool":
return [`(result i32)`];
default: //Will never reach this default condition as return type of functions are validated by the type checker
throw new Error(`TypeError: Invalid return type for the function`);
}
}
function codeGenVarDefs(vars: VarDef<any>[], isGlobal: boolean): Array<string> {
if (!isGlobal){
const varDefCommands: string[] = vars.map(v => { return `(local $${v.name} i32)`;});
const varInitCommands: string[] = vars.map(v => {
switch(v.tag){
case "int":
if (v.value.tag !== "number") throw new Error(); //Will never be executed as sanity check done by type checker
return `(i32.const ${v.value.value})\n(local.set $${v.name})`;
case "bool":
if (v.value.tag !== "True" && v.value.tag !== "False") throw new Error(); //Will never be executed as sanity check done by type checker
const boolVal = v.value.tag === "True" ? 1:0;
return `(i32.const ${boolVal})\n(local.set $${v.name})`;
}
});
return [...varDefCommands, ...varInitCommands];
}
else{
const varDefCommands: string[] = vars.map(v => {
switch(v.tag){
case "int":
if (v.value.tag !== "number") throw new Error(); //Will never be executed as sanity check done by type checker
return `(global $${v.name} (mut i32) (i32.const ${v.value.value}))`;
case "bool":
if (v.value.tag !== "True" && v.value.tag !== "False") throw new Error(); //Will never be executed as sanity check done by type checker
const boolVal = v.value.tag === "True" ? 1:0;
return `(global $${v.name} (mut i32) (i32.const ${boolVal}))`;
}
});
return varDefCommands;
}
}
function getLocalVarsFromFunc(f: FuncDef<Type>): string[]{
var localVars: string[] = [];
for (var i = 0; i < f.args.length; i++){
localVars.push(f.args[i].name);
}
for(var i = 0; i < f.body.definitions.length; i++){
localVars.push(f.body.definitions[i].name);
}
return localVars;
}
function codeGenFuncDef(f: FuncDef<Type>): Array<string>{
var loopCounter: Counter = {val: 1};
var localVars: string[] = getLocalVarsFromFunc(f);
const funcBodyCommands: string[] = codeGenBody(f.body.statements, localVars, loopCounter);
const funcParamCommands: string[] = codeGenParams(f.args);
const funcVarDefCommands: string[] = codeGenVarDefs(f.body.definitions, false);
// const funcRetTypeCommands: string[] = codeGenFuncReturnType(f.t);
const funcRetTypeCommands: string[] = [`(result i32)`];
var funcReturnCommands: string[] = [`(i32.const 0)`, `(return)`];
return [
`(func $${f.name}`,
...funcParamCommands,
...funcRetTypeCommands,
`(local $$scratch i32)`,
...funcVarDefCommands,
...funcBodyCommands,
...funcReturnCommands,
`)`
];
}
function codeGenBody(stmts: Stmt<any>[], localVars: string[], loop_counter: Counter): Array<string>{
var bodyCommands: string[] = [];
for (var i = 0; i < stmts.length; i++){
bodyCommands = [...bodyCommands, ...codeGenStmt(stmts[i], localVars, loop_counter)];
}
return bodyCommands;
}
function codeGenStmt(stmt: Stmt<any>, localVars: string[], loop_counter: Counter): Array<string>{
switch(stmt.tag){
case "assign":
var expr_commands = codeGenExpr(stmt.value, localVars);
const scopeKeyword = localVars.includes(stmt.name) ? "local" : "global";
return [...expr_commands, `(${scopeKeyword}.set $${stmt.name})`];
case "expr":
var expr_commands = codeGenExpr(stmt.expr, localVars);
return [...expr_commands, `(local.set $$scratch)`];
case "return":
if (stmt.value === undefined){
return [];
}
var expr_commands = codeGenExpr(stmt.value, localVars);
return [...expr_commands, `(return)`];
case "pass":
return [];
case "while":
var conditionCommands = codeGenExpr(stmt.condition, localVars);
var bodyCommands: string[] = codeGenBody(stmt.body, localVars, loop_counter);
const whileCommands = [
`(block $$block_label_${loop_counter.val}`,
`(loop $$loop_label_${loop_counter.val}`,
...conditionCommands,
`(i32.const 1)`,
`(i32.xor)`,
`(br_if $$block_label_${loop_counter.val})`,
...bodyCommands,
`(br $$loop_label_${loop_counter.val})`,
`)`,
`)`
];
loop_counter.val++;
return whileCommands;
case "if":
if (stmt.condition === undefined && stmt.else === undefined){
return codeGenBody(stmt.body, localVars, loop_counter);
}
//If then else is defined, then there has to be an expression
const exprCommands = codeGenExpr(stmt.condition, localVars);
const ifBodyCommands = codeGenBody(stmt.body, localVars, loop_counter);
if (stmt.else === undefined){
return [
...exprCommands,
`(if`,
`(then`,
...ifBodyCommands,
`)`,
`)`
];
}
else{
const elseBodyCommands = codeGenStmt(stmt.else, localVars, loop_counter);
return [
...exprCommands,
`(if`,
`(then`,
...ifBodyCommands,
`)`,
`(else`,
...elseBodyCommands,
`)`,
`)`
];
}
}
}
function explicitModeCommandsPrint(expr: Expr<Type>): Array<string>{
if (expr.tag !== "FuncCall") throw new Error(`CompileError: Invalid function call print`); //Will never be executed, for the compiler
const argExpr = expr.args[0];
switch(argExpr.t){
case "int":
return [`(i32.const 2)`];
case "bool":
return [`(i32.const 1)`];
case "none":
return [`(i32.const 0)`];
default:
return [`(i32.const 2)`];
}
}
function codeGenExpr(expr: Expr<any>, localVars: string[]): Array<string>{
switch(expr.tag){
case "literal":
const literal = expr.value;
switch(literal.tag){
case "None":
return ["(i32.const 0)"];
case "True":
return ["(i32.const 1)"];
case "False":
return ["(i32.const 0)"];
case "number":
return ["(i32.const " + literal.value + ")"];
}
case "id":
if (localVars.includes(expr.name)){
return [`(local.get $${expr.name})`];
}
return [`(global.get $${expr.name})`];
case "UnaryOp":
const expr_commands = codeGenExpr(expr.arg, localVars);
switch(expr.Op){
case "not":
return [...expr_commands, '(i32.const 1)', '(i32.xor)']
case "+":
return expr_commands;
case "-":
return [...expr_commands, '(i32.const -1)', '(i32.mul)'];
default: //Will never get to this point, as undefined unary operations are checked using the type checker
throw new Error(`TypeError: Undefined Unary Operation`);
}
case "BinaryOp":
const lhs_commands = codeGenExpr(expr.lhs, localVars);
const rhs_commands = codeGenExpr(expr.rhs, localVars);
const OpCode = codeGenBinOperation(expr.Op);
return [...lhs_commands, ...rhs_commands, ...OpCode];
case "ParanthesizedExpr":
return codeGenExpr(expr.arg, localVars);
case "FuncCall":
var funcCallStatements: string[] = [];
for(var i = 0; i < expr.args.length; i++){
const arg = expr.args[i];
funcCallStatements = [...funcCallStatements, ...codeGenExpr(arg, localVars)];
}
//Special case for print - To handle printing the literals exactly in case of None types and boolean types
if (expr.name === "print"){
funcCallStatements = [...funcCallStatements, ...explicitModeCommandsPrint(expr)];
}
funcCallStatements = [...funcCallStatements, `(call $${expr.name})`];
return funcCallStatements;
}
}
function codeGenBinOperation(operation: string) : Array<string> {
switch(operation) {
case "+":
return ["(i32.add)"];
case "-":
return ["(i32.sub)"];
case "*":
return ["(i32.mul)"];
case "//":
return [("i32.div_s")];
case "%":
return ["(i32.rem_s)"];
case "==":
return ["(i32.eq)"];
case "!=":
return ["(i32.ne)"];
case ">":
return ["(i32.gt_s)"];
case "<":
return ["(i32.lt_s)"];
case ">=":
return ["(i32.ge_s)"];
case "<=":
return ["(i32.le_s)"];
case "is":
return ["(i32.eq)"];
default:
throw new Error("CompileError: Unrecognized binary operator -> " + operation);
}
}
function CodeGenProgram(pgm: ProgramBody<any>): Array<string>{
const loopCounter: Counter = {val: 1};
const varDefCommands: string[] = codeGenVarDefs(pgm.variableDefinitions, true);
var funcDefCommands: string[] = [];
for(var i = 0; i < pgm.functions.length; i++){
funcDefCommands = [...funcDefCommands, ...codeGenFuncDef(pgm.functions[i])];
}
const bodyCommands = codeGenBody(pgm.body, [], loopCounter);
return [
`(module`,
`(func $print (import "imports" "print") (param i32) (param i32) (result i32))`,
`(func $min (import "imports" "min") (param i32) (param i32) (result i32))`,
`(func $abs (import "imports" "abs") (param i32) (result i32))`,
`(func $max (import "imports" "max") (param i32) (param i32) (result i32))`,
`(func $pow (import "imports" "pow") (param i32) (param i32) (result i32))`,
...varDefCommands,
...funcDefCommands,
`(func (export "exported_func")`,
`(local $$scratch i32)`,
...bodyCommands,
`)`,
`)`
];
}
export function compile(source: string): string{
const parsedOutput = parse(source);
console.log("Parsed output - ", JSON.stringify(parsedOutput, null, 2));
const tcOutput = tcProgram(parsedOutput);
return CodeGenProgram(tcOutput).join("\n");
}