don't name function blocks, make build operations immutable

This commit is contained in:
Brian Picciano 2016-08-20 13:11:25 -06:00
parent 867f83377f
commit 551c5efd7e
3 changed files with 16 additions and 12 deletions

View File

@ -35,11 +35,13 @@ func (bctx BuildCtx) BuildStmt(ctx Ctx, s Statement) Expr {
case Macro: case Macro:
return ctx.Macro(o)(bctx, ctx, s.Arg) return ctx.Macro(o)(bctx, ctx, s.Arg)
case Identifier: case Identifier:
s.Op = ctx.Identifier(o).(llvmVal) s2 := s
return bctx.BuildStmt(ctx, s) s2.Op = ctx.Identifier(o).(llvmVal)
return bctx.BuildStmt(ctx, s2)
case Statement: case Statement:
s.Op = bctx.BuildStmt(ctx, o) s2 := s
return bctx.BuildStmt(ctx, s) s2.Op = bctx.BuildStmt(ctx, o)
return bctx.BuildStmt(ctx, s2)
case llvmVal: case llvmVal:
arg := bctx.buildExpr(ctx, s.Arg).(llvmVal) arg := bctx.buildExpr(ctx, s.Arg).(llvmVal)
out := bctx.B.CreateCall(llvm.Value(o), []llvm.Value{llvm.Value(arg)}, "") out := bctx.B.CreateCall(llvm.Value(o), []llvm.Value{llvm.Value(arg)}, "")
@ -71,15 +73,17 @@ 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:
ea2 := make(Tuple, len(ea))
for i := range ea { for i := range ea {
ea[i] = bctx.buildExprTill(ctx, ea[i], fn) ea2[i] = bctx.buildExprTill(ctx, ea[i], fn)
} }
return ea return ea2
case List: case List:
ea2 := make(Tuple, len(ea))
for i := range ea { for i := range ea {
ea[i] = bctx.buildExprTill(ctx, ea[i], fn) ea2[i] = bctx.buildExprTill(ctx, ea[i], fn)
} }
return ea return ea2
case Ctx: case Ctx:
return ea return ea
default: default:
@ -157,7 +161,7 @@ var _ = func() bool {
// TODO obviously this needs to be fixed // TODO obviously this needs to be fixed
fn := llvm.AddFunction(bctx.M, "", llvm.FunctionType(llvm.Int64Type(), []llvm.Type{llvm.Int64Type()}, false)) fn := llvm.AddFunction(bctx.M, "", llvm.FunctionType(llvm.Int64Type(), []llvm.Type{llvm.Int64Type()}, false))
fnbl := llvm.AddBasicBlock(fn, "entry") fnbl := llvm.AddBasicBlock(fn, "")
prevbl := bctx.B.GetInsertBlock() prevbl := bctx.B.GetInsertBlock()
bctx.B.SetInsertPoint(fnbl, fnbl.FirstInstruction()) bctx.B.SetInsertPoint(fnbl, fnbl.FirstInstruction())

View File

@ -72,7 +72,7 @@ func (i Int) equal(e equaler) bool {
} }
func (i Int) String() string { func (i Int) String() string {
return fmt.Sprint(int64(i)) return fmt.Sprintf("%d", i)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -48,13 +48,13 @@ func main() {
stmts := []expr.Statement{ stmts := []expr.Statement{
expr.NewStatement( expr.NewStatement(
incr, incr,
expr.Int(5), expr.NewStatement(incr, expr.Int(4)),
), ),
} }
log.Printf("creating main function") log.Printf("creating main function")
mainFn := llvm.AddFunction(bctx.M, "main", llvm.FunctionType(llvm.Int64Type(), []llvm.Type{}, false)) mainFn := llvm.AddFunction(bctx.M, "main", llvm.FunctionType(llvm.Int64Type(), []llvm.Type{}, false))
mainBlock := llvm.AddBasicBlock(mainFn, "entry") mainBlock := llvm.AddBasicBlock(mainFn, "")
bctx.B.SetInsertPoint(mainBlock, mainBlock.FirstInstruction()) bctx.B.SetInsertPoint(mainBlock, mainBlock.FirstInstruction())
log.Printf("actually processing program") log.Printf("actually processing program")
out := bctx.Build(ctx, stmts...) out := bctx.Build(ctx, stmts...)