-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwrapped.ts
332 lines (290 loc) · 7.05 KB
/
wrapped.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* This module contains a set of classes that add abstraction over the
* low-level functions in the LLVM C API.
*
* Unlike using the lower-level functions, these wrappers aim for *type
* safety* in the TypeScript level. As much as possible, you use a real
* TypeScript class instead of an opaque pointer from the API.
*/
import { LLVM, PointerArray } from './llvmc';
/**
* A base class for our wrapper classes that abstract an `LLVM*Ref` type.
*/
export class Ref {
/**
* Create a new wrapepr for an underlying `LLVM*Ref` value.
*/
constructor(public ref: any) {}
}
/**
* An LLVM wrapper object that has a `free` method that you must call when
* you're done with the memory.
*/
export interface Freeable {
free(): void;
}
/**
* Represents an LLVM module: specifically, and underlying `LLVMModuleRef`.
*/
export class Module extends Ref implements Freeable {
/**
* Create a new module.
*/
static create(name: String) {
let modref = LLVM.LLVMModuleCreateWithName(name);
return new Module(modref);
}
/**
* Dump the IR to a file on disk.
*/
writeBitcodeToFile(filename: string): number {
return LLVM.LLVMWriteBitcodeToFile(this.ref, filename);
}
/**
* Dump the textual IR to a string.
*/
toString(): string {
return LLVM.LLVMPrintModuleToString(this.ref);
}
/**
* Add a function to the module, returning a `Function` wrapper.
*/
addFunction(name: string, type: FunctionType): Function {
let funcref = LLVM.LLVMAddFunction(this.ref, name, type.ref);
return new Function(funcref);
}
/**
* Retrieve the function in module with provided name
*/
getFunction(name: string): Function {
let funcref = LLVM.LLVMGetNamedFunction(this.ref, name);
return new Function(funcref);
}
/**
* Free the memory for this module.
*/
free() {
LLVM.LLVMDisposeModule(this.ref);
}
}
export class BasicBlock extends Ref {
}
/**
* Represents an LLVM IR builder.
*/
export class Builder extends Ref implements Freeable {
/**
* Create a new builder.
*/
static create() {
let bref = LLVM.LLVMCreateBuilder();
return new Builder(bref);
}
/**
* Build function call
*/
buildCall(func: Function, args: Value[], name: string): Value {
return new Value(LLVM.LLVMBuildCall(func, args, args.length, name));
}
/**
* Position the builder's insertion point at the end of the given basic block.
*/
positionAtEnd(bb: BasicBlock) {
LLVM.LLVMPositionBuilderAtEnd(this.ref, bb.ref);
}
/**
* Build an integer addition instruction.
*/
add(lhs: Value, rhs: Value, name: string): Value {
let vref = LLVM.LLVMBuildAdd(this.ref, lhs.ref, rhs.ref, name);
return new Value(vref);
}
/**
* Build an floating point addition instruction.
*/
addf(lhs: Value, rhs: Value, name: string): Value {
let vref = LLVM.LLVMBuildFAdd(this.ref, lhs.ref, rhs.ref, name);
return new Value(vref);
}
/**
* Build an integer subtraction instruction
*/
sub(lhs: Value, rhs: Value, name: string): Value {
let vref = LLVM.LLVMBuildSub(this.ref, lhs.ref, rhs.ref, name);
return new Value(vref);
}
/**
* Build a floating point subtraction instruction
*/
subf(lhs: Value, rhs: Value, name: string): Value {
let vref = LLVM.LLVMBuildFSub(this.ref, lhs.ref, rhs.ref, name);
return new Value(vref);
}
/**
* Build an integer multiplication instruction
*/
mul(lhs: Value, rhs: Value, name: string): Value {
let vref = LLVM.LLVMBuildMul(this.ref, lhs.ref, rhs.ref, name);
return new Value(vref);
}
/**
* Build a floating point multiplication instruction
*/
mulf(lhs: Value, rhs: Value, name: string): Value {
let vref = LLVM.LLVMBuildFMul(this.ref, lhs.ref, rhs.ref, name);
return new Value(vref);
}
/**
* Build a return instruction.
*/
ret(arg: Value): any {
return LLVM.LLVMBuildRet(this.ref, arg.ref);
}
/**
* Free the memory for this builder.
*/
free() {
LLVM.LLVMDisposeBuilder(this.ref);
}
}
/**
* An LLVM type; wraps `LLVMTypeRef`.
*/
export class Type extends Ref {
/**
* Get the i1 type.
*/
static int1(): Type {
return new Type(LLVM.LLVMInt1Type());
}
/**
* Get the i8 type.
*/
static int8(): Type {
return new Type(LLVM.LLVMInt8Type());
}
/**
* Get the i16 type.
*/
static int16(): Type {
return new Type(LLVM.LLVMInt16Type());
}
/**
* Get the i32 type.
*/
static int32(): Type {
return new Type(LLVM.LLVMInt32Type());
}
/**
* Get the i64 type.
*/
static int64(): Type {
return new Type(LLVM.LLVMInt64Type());
}
/**
* Get the i128 type.
*/
static int128(): Type {
return new Type(LLVM.LLVMInt128Type());
}
/**
* Get a float type
*/
static float():Type {
return new Type(LLVM.LLVMFloatType());
}
/**
* Get a double type
*/
static double():Type {
return new Type(LLVM.LLVMDoubleType());
}
}
/**
* Wraps a function type: an `LLVMFunctionTypeRef`.
*/
export class FunctionType extends Ref {
static create(ret: Type, params: Type[], isVarArg = false) {
// Construct an array containing the parameter references.
let paramtypes = new PointerArray(params.length);
let i = 0;
for (let param of params) {
paramtypes[i] = param.ref;
++i;
}
// Construct the function type.
let ftref = LLVM.LLVMFunctionType(ret.ref, paramtypes, params.length, isVarArg);
return new FunctionType(ftref);
}
}
/**
* Wraps *any* LLVM value via an `LLVMValueRef`.
*/
export class Value extends Ref {
/**
* Get the value's name.
*/
getName(): string {
return LLVM.LLVMGetValueName(this.ref);
}
/**
* Set the value's name.
*/
setName(name: string): void {
LLVM.LLVMSetValueName(this.ref, name);
}
}
/**
* Represents an LLVM function, wrapping an `LLVMValueRef` that points to a
* function.
*/
export class Function extends Value {
/**
* Add a new basic block to this function.
*/
appendBasicBlock(name: string): BasicBlock {
let bbref = LLVM.LLVMAppendBasicBlock(this.ref, "entry");
return new BasicBlock(bbref);
}
/**
* Get number of parameters to the function.
*/
countParams(): number {
return LLVM.LLVMCountParams(this.ref);
}
/**
* Get function parameter at the specified index.
*/
getParam(idx: number): Value {
return new Value(LLVM.LLVMGetParam(this.ref, idx));
}
/**
* Iterate over the parameters in the function.
*/
*params() {
let count = this.countParams();
for (let i = 0; i < count; ++i) {
yield this.getParam(i);
}
}
/**
* Delete the function from its containing module.
*/
deleteFromParent(): void {
LLVM.LLVMDeleteFunction(this.ref);
}
}
/**
* Build an integer constant.
*/
export function constInt(value: number, type: Type): Value {
let vref = LLVM.LLVMConstInt(type.ref, value, false);
return new Value(vref);
}
/**
* Build a floating point number
*/
export function constFloat(value: number, type: Type): Value {
let vref = LLVM.LLVMConstReal(type.ref, value);
return new Value(vref);
}