From 551c5efd7e80d997d9182f00c6bdbcc754c76ee2 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 20 Aug 2016 13:11:25 -0600 Subject: [PATCH] don't name function blocks, make build operations immutable --- expr/build.go | 22 +++++++++++++--------- expr/expr.go | 2 +- main.go | 4 ++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/expr/build.go b/expr/build.go index 209a121..80510a9 100644 --- a/expr/build.go +++ b/expr/build.go @@ -35,11 +35,13 @@ func (bctx BuildCtx) BuildStmt(ctx Ctx, s Statement) Expr { case Macro: return ctx.Macro(o)(bctx, ctx, s.Arg) case Identifier: - s.Op = ctx.Identifier(o).(llvmVal) - return bctx.BuildStmt(ctx, s) + s2 := s + s2.Op = ctx.Identifier(o).(llvmVal) + return bctx.BuildStmt(ctx, s2) case Statement: - s.Op = bctx.BuildStmt(ctx, o) - return bctx.BuildStmt(ctx, s) + s2 := s + s2.Op = bctx.BuildStmt(ctx, o) + return bctx.BuildStmt(ctx, s2) case llvmVal: arg := bctx.buildExpr(ctx, s.Arg).(llvmVal) 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: return bctx.BuildStmt(ctx, ea) case Tuple: + ea2 := make(Tuple, len(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: + ea2 := make(Tuple, len(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: return ea default: @@ -157,7 +161,7 @@ var _ = func() bool { // TODO obviously this needs to be fixed 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() bctx.B.SetInsertPoint(fnbl, fnbl.FirstInstruction()) diff --git a/expr/expr.go b/expr/expr.go index 06d7020..e3d6697 100644 --- a/expr/expr.go +++ b/expr/expr.go @@ -72,7 +72,7 @@ func (i Int) equal(e equaler) bool { } func (i Int) String() string { - return fmt.Sprint(int64(i)) + return fmt.Sprintf("%d", i) } //////////////////////////////////////////////////////////////////////////////// diff --git a/main.go b/main.go index ab69516..7d43474 100644 --- a/main.go +++ b/main.go @@ -48,13 +48,13 @@ func main() { stmts := []expr.Statement{ expr.NewStatement( incr, - expr.Int(5), + expr.NewStatement(incr, expr.Int(4)), ), } log.Printf("creating main function") 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()) log.Printf("actually processing program") out := bctx.Build(ctx, stmts...)