Skip to content

Commit

Permalink
Add type annotations to LLVM target; convert utility library to TypeS…
Browse files Browse the repository at this point in the history
…cript
  • Loading branch information
dirk committed May 10, 2015
1 parent cf8cd3f commit 1691dbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ src/targets/javascript.js
src/targets/llvm.js
src/targets/llvm/native-function.js
src/targets/llvm/native-types.js
src/targets/llvm/util.js
src/**/*.js.map

# Compiler-generated files
Expand Down
40 changes: 23 additions & 17 deletions src/targets/llvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@ class BlockContext {
}
}

function BasicLogger () {
this.info = console.log
interface ExprContext {
type?: any
value?: Buffer
path?: any[]
}

class BasicLogger {
info = console.log
}


function unboxInstanceType (instance, expectedType?) {
function unboxInstanceType (instance: any, expectedType?) {
assertInstanceOf(instance, types.Instance)
var type = instance.type
if (expectedType !== undefined) {
Expand Down Expand Up @@ -319,7 +325,7 @@ export class LLVMCompiler {
}
}

compileExpression(expr: AST.Node, blockCtx: BlockContext, exprCtx?) {
compileExpression(expr: AST.Node, blockCtx: BlockContext, exprCtx?: ExprContext) {
switch (expr.constructor) {
case AST.Property:
return this.compileProperty(<AST.Property>expr, blockCtx, exprCtx)
Expand Down Expand Up @@ -411,14 +417,14 @@ export class LLVMCompiler {
}


compileAsModuleMember(node: AST.Node, blockCtx: BlockContext, exprCtx) {
compileAsModuleMember(node: AST.Node, blockCtx: BlockContext, exprCtx: ExprContext) {
switch (node.constructor) {
default:
throw new ICE('Cannot compile as module member: '+node.constructor['name'])
}
}

compileProperty(prop: AST.Property, blockCtx: BlockContext, exprCtx?) {
compileProperty(prop: AST.Property, blockCtx: BlockContext, exprCtx?: ExprContext) {
var base = prop.base,
parent = prop.parent,
property = prop.property,
Expand All @@ -445,7 +451,7 @@ export class LLVMCompiler {
}
return ret
}
compilePropertyAsModuleMember(prop: AST.Property, blockCtx: BlockContext, exprCtx) {
compilePropertyAsModuleMember(prop: AST.Property, blockCtx: BlockContext, exprCtx: ExprContext) {
var parent = null,
path = []
if (parent === null) {
Expand Down Expand Up @@ -586,7 +592,7 @@ export class LLVMCompiler {
}
}

compileCall(call: AST.Call, blockCtx, exprCtx) {
compileCall(call: AST.Call, blockCtx, exprCtx: ExprContext) {
var self = this,
parent = call.parent,
type = null,
Expand Down Expand Up @@ -637,7 +643,7 @@ export class LLVMCompiler {
tryUpdatingExpressionContext(exprCtx, call.type, retValue)
return retValue
}
compileCallAsModuleMember(call: AST.Call, blockCtx, exprCtx) {
compileCallAsModuleMember(call: AST.Call, blockCtx, exprCtx: ExprContext) {
var self = this,
parent = call.parent
if (parent === null) {
Expand Down Expand Up @@ -680,7 +686,7 @@ export class LLVMCompiler {
return this.ctx.builder.buildCall(fn, args, '')
}

compileInstanceMethodCall(call: AST.Call, blockCtx: BlockContext, exprCtx) {
compileInstanceMethodCall(call: AST.Call, blockCtx: BlockContext, exprCtx: ExprContext) {
var self = this,
recvValue = exprCtx.value,
recvInstance = exprCtx.type,
Expand All @@ -704,7 +710,7 @@ export class LLVMCompiler {
return retValue
}

compileIntrinsicInstanceMethodCall(call: AST.Call, blockCtx: BlockContext, exprCtx) {
compileIntrinsicInstanceMethodCall(call: AST.Call, blockCtx: BlockContext, exprCtx: ExprContext) {
var self = this,
receiverInstance = call.parent.type,
receiverType = receiverInstance.type
Expand All @@ -730,7 +736,7 @@ export class LLVMCompiler {
return retValue
}

compileIdentifier(id: AST.Identifier, blockCtx: BlockContext, exprCtx) {
compileIdentifier(id: AST.Identifier, blockCtx: BlockContext, exprCtx: ExprContext) {
var parent = id.parent,
newType = null,
newValue = null
Expand Down Expand Up @@ -758,7 +764,7 @@ export class LLVMCompiler {
tryUpdatingExpressionContext(exprCtx, newType, newValue)
return newValue
}
compileIdentifierAsModuleMember(id: AST.Identifier, blockCtx: BlockContext, exprCtx) {
compileIdentifierAsModuleMember(id: AST.Identifier, blockCtx: BlockContext, exprCtx: ExprContext) {
var path = (exprCtx.path ? exprCtx.path : []),
type = id.type,
name = null
Expand Down Expand Up @@ -943,7 +949,7 @@ export class LLVMCompiler {
ifCounter: number = 1

compileIf(node: AST.If, blockCtx: BlockContext) {
var truthyVal = compileTruthyTest(this.ctx, blockCtx, node.cond),
var truthyVal = compileTruthyTest(this, blockCtx, node.cond),
blockNum = (this.ifCounter++),
// Get the parent function of the block
parentFn = blockCtx.fn.ptr
Expand Down Expand Up @@ -1032,7 +1038,7 @@ export class LLVMCompiler {
}
}

compileBinary(binary: AST.Binary, blockCtx: BlockContext, exprCtx) {
compileBinary(binary: AST.Binary, blockCtx: BlockContext, exprCtx: ExprContext) {
var lexpr = binary.lexpr,
rexpr = binary.rexpr
// Check (and unbox) the types
Expand Down Expand Up @@ -1097,7 +1103,7 @@ export class LLVMCompiler {

// 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.
function getTypeAndSlotsForName (ctx, blockCtx, name, foundCb?) {
function getTypeAndSlotsForName (ctx: Context, blockCtx: BlockContext, name: string, foundCb?) {
// Keep tracking of the scope of the beginning of the chain
var outermostScope = null
var type = blockCtx.block.scope.get(name, function (scope, _type) {
Expand All @@ -1112,7 +1118,7 @@ function getTypeAndSlotsForName (ctx, blockCtx, name, foundCb?) {
return [slots, type]
}

function tryUpdatingExpressionContext (exprCtx, type, value) {
function tryUpdatingExpressionContext (exprCtx: ExprContext, type: any, value: Buffer) {
if (!exprCtx) { return }
exprCtx.type = type
exprCtx.value = value
Expand Down
27 changes: 11 additions & 16 deletions src/targets/llvm/util.js → src/targets/llvm/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var LLVM = require('./library'),
types = require('../../types'),
errors = require('../../errors'),
ICE = errors.InternalCompilerError
import errors = require('../../errors')
import types = require('../../types')

var LLVM = require('./library'),
ICE = errors.InternalCompilerError

var Int64Type = LLVM.Types.Int64Type,
Int8Type = LLVM.Types.Int8Type,
Expand All @@ -11,7 +12,7 @@ var Int64Type = LLVM.Types.Int64Type,
var TypeOf = LLVM.Library.LLVMTypeOf,
PrintTypeToString = LLVM.Library.LLVMPrintTypeToString

function isLastInstructionTerminator (bb) {
export function isLastInstructionTerminator (bb: Buffer) {
var lastInstr = LLVM.Library.LLVMGetLastInstruction(bb)
// Do nothing if this block is empty
if (lastInstr.isNull()) {
Expand All @@ -22,7 +23,7 @@ function isLastInstructionTerminator (bb) {
return (LLVM.Library.TerminatorInstructions.indexOf(lastInstrOpcode) !== -1)
}

function assertInstanceOf (value, type, message) {
export function assertInstanceOf (value, type, message?) {
if (!(value instanceof type)) {
if (!message) {
message = 'Incorrect type; expected '+type.name+', got '+value.constructor.name
Expand All @@ -31,8 +32,8 @@ function assertInstanceOf (value, type, message) {
}
}

function compileTruthyTest (ctx, blockCtx, expr) {
var value = expr.compileToValue(ctx, blockCtx),
export function compileTruthyTest (compiler, blockCtx, expr) {
var value = compiler.compileExpression(expr, blockCtx),
instance = expr.type
// Can only truthy-test instances
assertInstanceOf(instance, types.Instance)
Expand All @@ -41,10 +42,10 @@ function compileTruthyTest (ctx, blockCtx, expr) {
case types.String:
var nullStringPtr = LLVM.Library.LLVMConstNull(Int8PtrType)
// Compare the string pointer to the NULL pointer
return ctx.builder.buildICmp(IntNE, value, nullStringPtr, '')
return compiler.ctx.builder.buildICmp(IntNE, value, nullStringPtr, '')
case types.Integer:
var zeroInteger = LLVM.Library.LLVMConstInt(Int64Type, 0, true)
return ctx.builder.buildICmp(IntNE, value, zeroInteger, '')
return compiler.ctx.builder.buildICmp(IntNE, value, zeroInteger, '')
case types.Boolean:
// Pre-condition check to make sure we really have an `i1`
var type = TypeOf(value),
Expand All @@ -58,9 +59,3 @@ function compileTruthyTest (ctx, blockCtx, expr) {
}
}

module.exports = {
isLastInstructionTerminator: isLastInstructionTerminator,
assertInstanceOf: assertInstanceOf,
compileTruthyTest: compileTruthyTest
}

0 comments on commit 1691dbc

Please sign in to comment.