make Tuple compile to a struct

This commit is contained in:
Brian Picciano 2016-08-21 12:20:07 -06:00
parent 066dc257f9
commit b0b5b01fd9
2 changed files with 33 additions and 4 deletions

View File

@ -87,11 +87,35 @@ func (bctx BuildCtx) buildExprTill(ctx Ctx, e Expr, fn func(e Expr) bool) Expr {
case Statement: case Statement:
return bctx.BuildStmt(ctx, ea) return bctx.BuildStmt(ctx, ea)
case Tuple: case Tuple:
// if the tuple is empty then it is a void
if len(ea) == 0 {
return llvmVal(llvm.Undef(llvm.VoidType()))
}
ea2 := make(Tuple, len(ea)) ea2 := make(Tuple, len(ea))
for i := range ea { for i := range ea {
ea2[i] = bctx.buildExprTill(ctx, ea[i], fn) ea2[i] = bctx.buildExprTill(ctx, ea[i], fn)
} }
// 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 return ea2
}
}
str := llvm.Undef(llvm.StructType(typs, false))
for i := range vals {
str = bctx.B.CreateInsertValue(str, vals[i], i, "")
}
return llvmVal(str)
case List: case List:
ea2 := make(Tuple, len(ea)) ea2 := make(Tuple, len(ea))
for i := range ea { for i := range ea {
@ -119,9 +143,9 @@ var _ = func() bool {
globalCtx = &Ctx{ globalCtx = &Ctx{
macros: map[Macro]MacroFn{ macros: map[Macro]MacroFn{
"add": func(bctx BuildCtx, ctx Ctx, e Expr) Expr { "add": func(bctx BuildCtx, ctx Ctx, e Expr) Expr {
tup := bctx.buildExpr(ctx, e).(Tuple) tup := bctx.buildExpr(ctx, e).(llvmVal)
a := bctx.buildVal(ctx, tup[0]) a := bctx.B.CreateExtractValue(llvm.Value(tup), 0, "")
b := bctx.buildVal(ctx, tup[1]) b := bctx.B.CreateExtractValue(llvm.Value(tup), 1, "")
return llvmVal(bctx.B.CreateAdd(a, b, "")) return llvmVal(bctx.B.CreateAdd(a, b, ""))
}, },

View File

@ -141,6 +141,11 @@ func (tup Tuple) equal(e equaler) bool {
return ok && exprsEqual(tup, tuptup) return ok && exprsEqual(tup, tuptup)
} }
func isTuple(e Expr) bool {
_, ok := e.(Tuple)
return ok
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// List represents an ordered set of Exprs, all of the same type. A List's size // List represents an ordered set of Exprs, all of the same type. A List's size