-
Notifications
You must be signed in to change notification settings - Fork 0
/
compilerUtils.ts
70 lines (64 loc) · 1.9 KB
/
compilerUtils.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
import { BinOp, ClassDef, Type, UniOp } from "./ast";
import { RuntimeErrors } from "./panic";
import { isObject } from "./tcUtils";
/**
* Map of BinOps to their cooresponding WAT instructions. If it's not so simple,
* you'll need to edit codeGenExpr.
*/
export const binOpToInstr: Map<BinOp, string> = new Map([
[BinOp.ADD, "(i32.add)"],
[BinOp.SUB, "(i32.sub)"],
[BinOp.MUL, "(i32.mul)"],
[BinOp.DIV, "(i32.div_s)"],
[BinOp.MOD, "(i32.rem_s)"],
[BinOp.EQ, "(i32.eq)"],
[BinOp.NE, "(i32.ne)"],
[BinOp.LT, "(i32.lt_s)"],
[BinOp.LE, "(i32.le_s)"],
[BinOp.GT, "(i32.gt_s)"],
[BinOp.GE, "(i32.ge_s)"],
]);
export const uniOpToInstr: Map<UniOp, string> = new Map([
[UniOp.NEG, "(i32.mul (i32.const -1))"],
[UniOp.NOT, "(i32.xor (i32.const 1))"],
]);
type ClassEnv = Map<string, ClassDef<Type>>;
/**
* Check the given object to see if it's None, if so panic at runtime.
* Assumes an object address is on the stack. Will restore its argument.
*/
export const checkNone: string = [
`(local.set $$scratch)`,
`(local.get $$scratch)`,
`(i32.eq (i32.const 0))`, // 0 is the representation of none
`(if`,
`(then`,
`(call $panic (i32.const ${RuntimeErrors.OperationOnNone}))`,
`)`,
`)`,
`(local.get $$scratch)`,
].join("\n");
/**
* Find the index for a field based off an object type, a field, and a classenv.
*
* @param t An object type
* @param fieldName the Field being retrieved
* @param classes Environment of all classes in the program
* @returns index of the field
* @throws if `t` is not an object
*/
export function getFieldIndex(
t: Type,
fieldName: string,
classes: ClassEnv
): number {
if (!isObject(t))
throw new Error(
`This should be impossible - at compiler, getting field of nonobject ${t}`
);
const classdata = classes.get(t.class);
const fieldIndex = classdata.fields.findIndex(
(f) => f.typedVar.name === fieldName
);
return fieldIndex;
}