2016-08-05 18:34:17 +00:00
|
|
|
package expr
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-20 18:42:02 +00:00
|
|
|
"log"
|
2016-08-09 01:43:05 +00:00
|
|
|
|
|
|
|
"llvm.org/llvm/bindings/go/llvm"
|
|
|
|
)
|
2016-08-05 18:34:17 +00:00
|
|
|
|
2016-08-21 16:36:19 +00:00
|
|
|
func init() {
|
|
|
|
log.Printf("initializing llvm")
|
|
|
|
llvm.LinkInMCJIT()
|
|
|
|
llvm.InitializeNativeTarget()
|
|
|
|
llvm.InitializeNativeAsmPrinter()
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:20:53 +00:00
|
|
|
type BuildCtx struct {
|
2016-08-05 18:34:17 +00:00
|
|
|
B llvm.Builder
|
|
|
|
M llvm.Module
|
|
|
|
}
|
|
|
|
|
2016-08-21 16:36:19 +00:00
|
|
|
func NewBuildCtx(moduleName string) BuildCtx {
|
|
|
|
return BuildCtx{
|
|
|
|
B: llvm.NewBuilder(),
|
|
|
|
M: llvm.NewModule(moduleName),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) Build(ctx Ctx, stmts ...Statement) llvm.Value {
|
2016-08-06 18:20:53 +00:00
|
|
|
var lastVal llvm.Value
|
2016-08-05 18:34:17 +00:00
|
|
|
for _, stmt := range stmts {
|
2016-08-09 01:43:05 +00:00
|
|
|
if e := bctx.BuildStmt(ctx, stmt); e != nil {
|
|
|
|
if lv, ok := e.(llvmVal); ok {
|
|
|
|
lastVal = llvm.Value(lv)
|
2016-08-20 18:42:02 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("BuildStmt returned non llvmVal from %v: %v (%T)", stmt, e, e)
|
2016-08-09 01:43:05 +00:00
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
2016-08-05 18:34:17 +00:00
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
if (lastVal == llvm.Value{}) {
|
|
|
|
lastVal = bctx.B.CreateRetVoid()
|
|
|
|
}
|
|
|
|
return lastVal
|
2016-08-05 18:34:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) BuildStmt(ctx Ctx, s Statement) Expr {
|
2016-08-20 18:42:02 +00:00
|
|
|
log.Printf("building: %v", s)
|
|
|
|
switch o := s.Op.(type) {
|
|
|
|
case Macro:
|
|
|
|
return ctx.Macro(o)(bctx, ctx, s.Arg)
|
|
|
|
case Identifier:
|
2016-08-20 19:11:25 +00:00
|
|
|
s2 := s
|
|
|
|
s2.Op = ctx.Identifier(o).(llvmVal)
|
|
|
|
return bctx.BuildStmt(ctx, s2)
|
2016-08-20 18:48:14 +00:00
|
|
|
case Statement:
|
2016-08-20 19:11:25 +00:00
|
|
|
s2 := s
|
|
|
|
s2.Op = bctx.BuildStmt(ctx, o)
|
|
|
|
return bctx.BuildStmt(ctx, s2)
|
2016-08-20 18:48:14 +00:00
|
|
|
case llvmVal:
|
2016-08-20 18:42:02 +00:00
|
|
|
arg := bctx.buildExpr(ctx, s.Arg).(llvmVal)
|
2016-08-20 18:48:14 +00:00
|
|
|
out := bctx.B.CreateCall(llvm.Value(o), []llvm.Value{llvm.Value(arg)}, "")
|
2016-08-20 18:42:02 +00:00
|
|
|
return llvmVal(out)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("non op type %v (%T)", s.Op, s.Op))
|
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// may return nil if e is a Statement which has no return
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) buildExpr(ctx Ctx, e Expr) Expr {
|
|
|
|
return bctx.buildExprTill(ctx, e, func(Expr) bool { return false })
|
2016-08-07 00:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// like buildExpr, but will stop short and stop recursing when the function
|
|
|
|
// returns true
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) buildExprTill(ctx Ctx, e Expr, fn func(e Expr) bool) Expr {
|
2016-08-07 00:10:31 +00:00
|
|
|
if fn(e) {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:20:53 +00:00
|
|
|
switch ea := e.(type) {
|
|
|
|
case llvmVal:
|
|
|
|
return e
|
|
|
|
case Int:
|
|
|
|
return llvmVal(llvm.ConstInt(llvm.Int64Type(), uint64(ea), false))
|
|
|
|
case Identifier:
|
2016-08-09 01:43:05 +00:00
|
|
|
return ctx.Identifier(ea)
|
2016-08-06 18:20:53 +00:00
|
|
|
case Statement:
|
2016-08-09 01:43:05 +00:00
|
|
|
return bctx.BuildStmt(ctx, ea)
|
2016-08-06 18:20:53 +00:00
|
|
|
case Tuple:
|
2016-08-21 18:20:07 +00:00
|
|
|
// if the tuple is empty then it is a void
|
|
|
|
if len(ea) == 0 {
|
|
|
|
return llvmVal(llvm.Undef(llvm.VoidType()))
|
|
|
|
}
|
|
|
|
|
2016-08-20 19:11:25 +00:00
|
|
|
ea2 := make(Tuple, len(ea))
|
2016-08-06 18:20:53 +00:00
|
|
|
for i := range ea {
|
2016-08-20 19:11:25 +00:00
|
|
|
ea2[i] = bctx.buildExprTill(ctx, ea[i], fn)
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
2016-08-21 18:20:07 +00:00
|
|
|
|
|
|
|
// if the fields of the tuple are all llvmVal then we can make a proper
|
|
|
|
// struct
|
|
|
|
vals := make([]llvm.Value, len(ea2))
|
|
|
|
typs := make([]llvm.Type, len(ea2))
|
|
|
|
for i := range ea2 {
|
|
|
|
if v, ok := ea2[i].(llvmVal); ok {
|
|
|
|
val := llvm.Value(v)
|
|
|
|
vals[i] = val
|
|
|
|
typs[i] = val.Type()
|
|
|
|
} else {
|
|
|
|
return ea2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str := llvm.Undef(llvm.StructType(typs, false))
|
|
|
|
for i := range vals {
|
|
|
|
str = bctx.B.CreateInsertValue(str, vals[i], i, "")
|
|
|
|
}
|
|
|
|
return llvmVal(str)
|
2016-08-07 15:25:03 +00:00
|
|
|
case List:
|
2016-08-20 19:11:25 +00:00
|
|
|
ea2 := make(Tuple, len(ea))
|
2016-08-07 15:25:03 +00:00
|
|
|
for i := range ea {
|
2016-08-20 19:11:25 +00:00
|
|
|
ea2[i] = bctx.buildExprTill(ctx, ea[i], fn)
|
2016-08-07 15:25:03 +00:00
|
|
|
}
|
2016-08-20 19:11:25 +00:00
|
|
|
return ea2
|
2016-08-09 01:43:05 +00:00
|
|
|
case Ctx:
|
|
|
|
return ea
|
2016-08-06 18:20:53 +00:00
|
|
|
default:
|
2016-08-09 01:43:05 +00:00
|
|
|
panicf("%v (type %T) can't express a value", ea, ea)
|
2016-08-05 18:34:17 +00:00
|
|
|
}
|
2016-08-06 18:20:53 +00:00
|
|
|
panic("go is dumb")
|
|
|
|
}
|
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
func (bctx BuildCtx) buildVal(ctx Ctx, e Expr) llvm.Value {
|
|
|
|
return llvm.Value(bctx.buildExpr(ctx, e).(llvmVal))
|
2016-08-06 18:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// globalCtx describes what's available to *all* contexts, and is what all
|
2016-08-09 01:43:05 +00:00
|
|
|
// contexts should have as the root parent in the tree.
|
|
|
|
//
|
|
|
|
// We define in this weird way cause NewCtx actually references globalCtx
|
|
|
|
var globalCtx *Ctx
|
|
|
|
var _ = func() bool {
|
|
|
|
globalCtx = &Ctx{
|
|
|
|
macros: map[Macro]MacroFn{
|
|
|
|
"add": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
2016-08-21 18:20:07 +00:00
|
|
|
tup := bctx.buildExpr(ctx, e).(llvmVal)
|
|
|
|
a := bctx.B.CreateExtractValue(llvm.Value(tup), 0, "")
|
|
|
|
b := bctx.B.CreateExtractValue(llvm.Value(tup), 1, "")
|
2016-08-09 01:43:05 +00:00
|
|
|
return llvmVal(bctx.B.CreateAdd(a, b, ""))
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO this chould be a user macro!!!! WUT this language is baller
|
|
|
|
"bind": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isIdentifier).(Tuple)
|
|
|
|
id := bctx.buildExprTill(ctx, tup[0], isIdentifier).(Identifier)
|
2016-08-20 18:42:02 +00:00
|
|
|
val := bctx.buildExpr(ctx, tup[1])
|
|
|
|
ctx.Bind(id, val)
|
2016-08-09 01:43:05 +00:00
|
|
|
return NewTuple()
|
|
|
|
},
|
|
|
|
|
|
|
|
"ctxnew": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
return NewCtx()
|
|
|
|
},
|
2016-08-06 18:20:53 +00:00
|
|
|
|
2016-08-09 01:43:05 +00:00
|
|
|
"ctxthis": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
|
|
|
|
"ctxbind": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isIdentifier).(Tuple)
|
|
|
|
thisCtx := bctx.buildExpr(ctx, tup[0]).(Ctx)
|
|
|
|
id := bctx.buildExprTill(ctx, tup[1], isIdentifier).(Identifier)
|
|
|
|
thisCtx.Bind(id, bctx.buildExpr(ctx, tup[2]))
|
|
|
|
return NewTuple()
|
|
|
|
},
|
|
|
|
|
|
|
|
"ctxget": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isIdentifier).(Tuple)
|
|
|
|
thisCtx := bctx.buildExpr(ctx, tup[0]).(Ctx)
|
|
|
|
id := bctx.buildExprTill(ctx, tup[1], isIdentifier).(Identifier)
|
|
|
|
return thisCtx.Identifier(id)
|
|
|
|
},
|
|
|
|
|
|
|
|
"do": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
tup := bctx.buildExprTill(ctx, e, isStmt).(Tuple)
|
|
|
|
thisCtx := tup[0].(Ctx)
|
|
|
|
for _, stmtE := range tup[1].(List) {
|
|
|
|
bctx.BuildStmt(thisCtx, stmtE.(Statement))
|
|
|
|
}
|
|
|
|
return NewTuple()
|
|
|
|
},
|
2016-08-20 18:42:02 +00:00
|
|
|
|
|
|
|
"op": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
l := bctx.buildExprTill(ctx, e, isList).(List)
|
|
|
|
stmts := make([]Statement, len(l))
|
|
|
|
for i := range l {
|
|
|
|
stmts[i] = l[i].(Statement)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO obviously this needs to be fixed
|
|
|
|
fn := llvm.AddFunction(bctx.M, "", llvm.FunctionType(llvm.Int64Type(), []llvm.Type{llvm.Int64Type()}, false))
|
2016-08-20 19:11:25 +00:00
|
|
|
fnbl := llvm.AddBasicBlock(fn, "")
|
2016-08-20 18:42:02 +00:00
|
|
|
|
|
|
|
prevbl := bctx.B.GetInsertBlock()
|
|
|
|
bctx.B.SetInsertPoint(fnbl, fnbl.FirstInstruction())
|
|
|
|
out := bctx.Build(NewCtx(), stmts...)
|
|
|
|
bctx.B.CreateRet(out)
|
|
|
|
bctx.B.SetInsertPointAtEnd(prevbl)
|
|
|
|
return llvmVal(fn)
|
|
|
|
},
|
|
|
|
|
|
|
|
"in": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
|
|
|
|
fn := bctx.B.GetInsertBlock().Parent()
|
|
|
|
return llvmVal(fn.Param(0))
|
|
|
|
},
|
2016-08-06 18:20:53 +00:00
|
|
|
},
|
2016-08-09 01:43:05 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|