Skip to content

Commit

Permalink
Add compilation of named function statements
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk committed Apr 16, 2015
1 parent b14688f commit d97f0ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/targets/llvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ function compileBlock (ctx, block, parentFn, preStatementsCb) {
for (var i = 0; i < statements.length; i++) {
var stmt = statements[i]
stmt.compile(ctx, blockCtx)
}
}
}//for
}//compileBlock

// Look up the type and Slots for a given name; begins search from the passed
// block-context. Returns a 2-tuple of Slots and Type.
Expand Down Expand Up @@ -1023,6 +1023,28 @@ AST.Function.prototype.getAnonymousNativeFunction = function (ctx) {
return fn
}

AST.Function.prototype.compile = function(ctx, blockCtx) {
var instance = this.type,
type = unboxInstanceType(instance, types.Function)
if (type.parentMultiType) {
throw new ICE('Compilation of multi-functions not yet implemented')
} else {
if (typeof this.name !== 'string') {
throw new ICE('Missing name of Function statement')
}
var name = type.getNativePrefix()+this.name
// Setup the native function
var fn = new NativeFunction(name, type.args, type.ret)
type.setNativeFunction(fn)
// Compile the native function with our block
genericCompileFunction(ctx, fn, this)
// Set the linkage of the function to private
LLVM.Library.LLVMSetLinkage(fn.getPtr(), LLVM.Library.LLVMPrivateLinkage)
// Add this to the slots
blockCtx.slots.buildSet(ctx, this.name, fn.getPtr())
}
}

AST.Function.prototype.compileToValue = function (ctx, blockCtx) {
var self = this,
fn = this.getAnonymousNativeFunction(ctx)
Expand Down
7 changes: 6 additions & 1 deletion src/typesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,11 @@ TypeSystem.prototype.visitMultiFunction = function (node, scope, multiNode) {
}
}

TypeSystem.prototype.visitNamedFunction = function (node, scope) {
this.visitFunction(node, scope)
scope.setLocal(node.name, node.type)
}

TypeSystem.prototype.visitFunctionStatement = function (node, scope, searchInParent) {
var name = node.name
// Now look up the parent `multi` in the containing block
Expand All @@ -802,7 +807,7 @@ TypeSystem.prototype.visitFunctionStatement = function (node, scope, searchInPar
if (multiNode) {
this.visitMultiFunction(node, scope, multiNode)
} else {
throw new TypeError('Failed to find associated multi statement')
this.visitNamedFunction(node, scope)
}
}

Expand Down

0 comments on commit d97f0ee

Please sign in to comment.