-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathoptimize_ir.ts
186 lines (176 loc) · 7.01 KB
/
optimize_ir.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
import { Type, Program, SourceLocation, FunDef, Expr, Stmt, Literal, BinOp, UniOp, Class} from './ast';
import * as IR from './ir';
export function optimizeIr(program: IR.Program<[Type, SourceLocation]>) : IR.Program<[Type, SourceLocation]> {
const optFuns = program.funs.map(optimizeFuncDef);
const optClss = program.classes.map(optimizeClass);
const optBody = program.body.map(optBasicBlock);
return { ...program, funs: optFuns, classes: optClss, body: optBody };
}
const bfoldable = ["num", "bool", "none"];
const ufoldable = ["num", "bool"];
function bigMax(a: bigint, b: bigint): bigint {
return a > b ? a : b;
}
function bigMin(a: bigint, b: bigint): bigint {
return a < b ? a : b;
}
function bigPow(a: bigint, p: bigint): bigint {
return a ** p;
}
function bigAbs(a: bigint): bigint {
return a < 0 ? -a : a;
}
function optBasicBlock(bb: IR.BasicBlock<[Type, SourceLocation]>): IR.BasicBlock<[Type, SourceLocation]> {
return {...bb, stmts: bb.stmts.map(optimizeIRStmt)};
}
function optimizeFuncDef(fun: IR.FunDef<[Type, SourceLocation]>): IR.FunDef<[Type, SourceLocation]> {
return {...fun, body: fun.body.map(optBasicBlock)};
}
function optimizeClass(cls: IR.Class<[Type, SourceLocation]>): IR.Class<[Type, SourceLocation]> {
return {...cls, methods: cls.methods.map(optimizeFuncDef)};
}
function optimizeIRStmt(stmt: IR.Stmt<[Type, SourceLocation]>): IR.Stmt<[Type, SourceLocation]> {
switch (stmt.tag) {
case "assign":
return {...stmt, value: optimizeIRExpr(stmt.value)};
case "expr":
return {...stmt, expr: optimizeIRExpr(stmt.expr)};
case "return":
case "pass":
case "ifjmp":
case "jmp":
case "store":
return stmt;
default:
return stmt;
}
}
function optimizeIRExpr(expr: IR.Expr<[Type, SourceLocation]>): IR.Expr<[Type, SourceLocation]> {
switch (expr.tag) {
case "value":
return expr;
case "binop":
if (bfoldable.includes(expr.left.tag) && bfoldable.includes(expr.right.tag))
return {tag: "value", value: foldBinop(expr.left, expr.right, expr.op), a: expr.a};
return expr;
case "uniop":
if (ufoldable.includes(expr.expr.tag))
return {tag: "value", value: foldUniop(expr.expr, expr.op), a: expr.a};
return expr;
case "call":
return expr;
case "alloc":
return expr;
case "load" :
return expr;
default:
return expr;
}
}
function foldBuiltin2(lhs: IR.Value<[Type, SourceLocation]>, rhs: IR.Value<[Type, SourceLocation]>, callName: string): IR.Value<[Type, SourceLocation]> {
if (lhs.tag !== "num" || rhs.tag !== "num")
return {tag: "none", a: lhs.a};
switch (callName) {
case "max":
return {tag: "num", value: bigMax(lhs.value, rhs.value), a: lhs.a};
case "min":
return {tag: "num", value: bigMin(lhs.value, rhs.value), a: lhs.a};
case "pow":
return {tag: "num", value: bigPow(lhs.value, rhs.value), a: lhs.a};
default:
return {tag: "none", a: lhs.a};
}
}
function foldBinop(lhs: IR.Value<[Type, SourceLocation]>, rhs: IR.Value<[Type, SourceLocation]>, op: BinOp): IR.Value<[Type, SourceLocation]> {
switch(op) {
case BinOp.Plus: {
if (lhs.tag != "num" || rhs.tag != "num") {
return {tag: "none", a: lhs.a};
}
return {tag: "num", value: lhs.value + rhs.value, a: lhs.a};
}
case BinOp.Minus:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "num", value: lhs.value - rhs.value, a: lhs.a};
case BinOp.Mul:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "num", value: lhs.value * rhs.value, a: lhs.a};
case BinOp.IDiv:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
// bigint do intDiv
return {tag: "num", value: lhs.value / rhs.value, a: lhs.a};
case BinOp.Mod:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "num", value: lhs.value + rhs.value, a: lhs.a};
case BinOp.Eq:
if(lhs.tag === "none" || rhs.tag === "none"){
return {tag: "bool", value: true, a: lhs.a};
} else if(lhs.tag === "id" || rhs.tag === "id") {
return {tag: "none", a: lhs.a};
}
return {a: lhs.a, tag: "bool", value: lhs.value === rhs.value};
case BinOp.Neq:
if(lhs.tag === "none" || rhs.tag === "none"){
return {tag: "bool", value: false, a: lhs.a};
} else if(lhs.tag === "id" || rhs.tag === "id") {
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value !== rhs.value, a: lhs.a};
case BinOp.Lte:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value <= rhs.value, a: lhs.a};
case BinOp.Gte:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value >= rhs.value, a: lhs.a};
case BinOp.Lt:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value < rhs.value, a: lhs.a};
case BinOp.Gt:
if(lhs.tag !== "num" || rhs.tag !== "num"){
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value > rhs.value, a: lhs.a};
case BinOp.And:
if(lhs.tag !== "bool" || rhs.tag !== "bool"){
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value && rhs.value, a: lhs.a};
case BinOp.Or:
if(lhs.tag !== "bool" || rhs.tag !== "bool"){
return {tag: "none", a: lhs.a};
}
return {tag: "bool", value: lhs.value || rhs.value, a: lhs.a};
default:
return {tag: "none", a: lhs.a};
}
}
function foldUniop(val: IR.Value<[Type, SourceLocation]>, op: UniOp): IR.Value<[Type, SourceLocation]>{
switch (op){
case UniOp.Neg:
if(val.tag != "num"){
return {tag: "none", a: val.a};
}
return {tag: "num", value: BigInt(-1) *val.value, a: val.a};
case UniOp.Not:
if(val.tag != "bool"){
return {tag: "none", a: val.a};
}
return {tag: "bool", value: !(val.value), a: val.a};
default:
return {tag: "none", a: val.a};
}
}