forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.ts
174 lines (152 loc) · 4.4 KB
/
builder.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
import * as bril from './bril.ts';
/**
* A utility for building up Bril programs.
*/
export class Builder {
/**
* The program we have built so far.
*/
public program: bril.Program = { functions: [] };
private curFunction: bril.Function | null = null;
private nextFresh: number = 0;
/**
* Create a new, empty function into which further code will be generated.
*/
buildFunction(name: string, args: bril.Argument[], type?: bril.Type) {
let func: bril.Function;
if (type === undefined) {
func = {name: name, instrs: [], args: args};
} else {
func = {name: name, instrs: [], args: args, type: type};
}
this.program.functions.push(func);
this.curFunction = func;
this.nextFresh = 0;
return func;
}
/**
* Build an operation instruction that produces a result. If the name is
* omitted, a fresh variable is chosen automatically.
*/
buildValue(op: bril.ValueOpCode, type: bril.Type,
args: string[], funcs?: string[], labels?: string[],
dest?: string) {
dest = dest || this.freshVar();
let instr: bril.ValueOperation = { op, dest, type, args, funcs, labels };
this.insert(instr);
return instr;
}
/**
* Build a non-value-producing (side-effecting) operation instruction.
*/
buildEffect(op: bril.EffectOpCode,
args: string[], funcs?: string[], labels?: string[]) {
let instr: bril.EffectOperation = { op, args, funcs, labels };
this.insert(instr);
return instr;
}
/**
* Build a function call operation. If a type is specified, the call
* produces a return value.
*/
buildCall(func: string, args: string[],
type: bril.Type, dest?: string): bril.ValueOperation;
buildCall(func: string, args: string[],
type?: undefined, dest?: string): bril.EffectOperation;
buildCall(func: string, args: string[],
type?: bril.Type, dest?: string): bril.Operation {
if (type) {
return this.buildValue("call", type, args, [func], undefined, dest);
} else {
return this.buildEffect("call", args, [func], undefined);
}
}
/**
* Build a constant instruction. As above, the destination name is optional.
*/
buildConst(value: bril.Value, type: bril.Type, dest?: string) {
dest = dest || this.freshVar();
let instr: bril.Constant = { op: "const", value, dest, type };
this.insert(instr);
return instr;
}
/**
* Build a constant integer value.
*/
buildInt(value: number, dest?: string) {
return this.buildConst(value, "int", dest);
}
/**
* Build a constant boolean value.
*/
buildBool(value: boolean, dest?: string) {
return this.buildConst(value, "bool", dest);
}
/**
* Build a constant floating-point value.
*/
buildFloat(value: number, dest?: string) {
return this.buildConst(value, "float", dest);
}
/**
* Build a constant character value.
*/
buildChar(value: string, dest?: string) {
return this.buildConst(value, "char", dest);
}
/**
* Add a label to the function at the current position.
*/
buildLabel(name: string) {
let label = {label: name};
this.insert(label);
}
/**
* Insert an instruction at the end of the current function.
*/
private insert(instr: bril.Instruction | bril.Label) {
if (!this.curFunction) {
throw "cannot build instruction/label without a function";
}
this.curFunction.instrs.push(instr);
}
/**
* Checks whether the last emitted instruction in the current function is the specified op code.
* Useful for checking for terminating instructions.
*/
getLastInstr(): bril.Instruction | undefined {
if (!this.curFunction) {
return undefined
}
if (!this.curFunction.instrs) {
return undefined
}
if (this.curFunction.instrs.length === 0) {
return undefined
}
const last_instr : bril.Instruction | bril.Label = this.curFunction.instrs[this.curFunction.instrs.length - 1];
if ('label' in last_instr) {
return undefined
}
return last_instr
}
/**
* Generate an unused variable name.
*/
freshVar() {
let out = 'v' + this.nextFresh.toString();
this.nextFresh += 1;
return out;
}
/**
* Generate an unused suffix.
*/
freshSuffix() {
let out = '.' + this.nextFresh.toString();
this.nextFresh += 1;
return out;
}
setCurrentFunction(func: bril.Function) {
this.curFunction = func;
}
}