-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (41 loc) · 1.44 KB
/
index.js
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
llvm = module.exports = require('bindings')('llvm')
llvm.initializeNativeTarget()
ref = require('ref')
ffi = require('ffi')
llvm.Function.prototype.addBasicBlock = function (nameOrBlock){
if (typeof nameOrBlock == 'string'){
nameOrBlock = new llvm.BasicBlock(this.context, nameOrBlock);
}
return this._addBasicBlock(nameOrBlock);
}
function initContext(ctx){
// FFI/ref types have signedness, while LLVM types do not
// We'll just assume unsigned...
ctx.voidTy.ffiType = ref.types.void
ctx.floatTy.ffiType = ref.types.float
ctx.doubleTy.ffiType = ref.types.double
ctx.int1Ty.ffiType = ref.types.bool
ctx.int8Ty.ffiType = ref.types.uint8
ctx.int16Ty.ffiType = ref.types.uint16
ctx.int32Ty.ffiType = ref.types.uint32
ctx.int64Ty.ffiType = ref.types.uint64
}
initContext(llvm.globalContext)
llvm.Type.prototype.getPointerTo = function(){
var t = this._getPointerTo()
t.ffiType = ref.refType(this.ffiType || ref.types.void);
return t;
}
llvm.ExecutionEngine.prototype.getFFIFunction = function(func){
if (!ffi) throw new Error("Couldn't load FFI module")
var ptr = this.getPointerToFunction(func)
var argTypes = func.functionType.paramTypes
var args = []
for (var i=0; i<argTypes.length; i++){
args.push(argTypes[i].ffiType || ref.types.void)
}
var retType = func.functionType.returnType.ffiType || ref.types.void
var fn = ffi.ForeignFunction(ptr, retType, args)
fn.executionEngine = this; // keep it from being GC'd
return fn
}