diff --git a/cl/_testdata/fncall/out.ll b/cl/_testdata/fncall/out.ll index 3f09abcd..6fa9a5b7 100644 --- a/cl/_testdata/fncall/out.ll +++ b/cl/_testdata/fncall/out.ll @@ -1,7 +1,7 @@ ; ModuleID = 'main' source_filename = "main" -@"init$guard" = external global ptr +@"init$guard" = global ptr null define void @init() { _llgo_0: diff --git a/cl/_testdata/varinit/out.ll b/cl/_testdata/varinit/out.ll index 4cb4f47f..aa7eaef4 100644 --- a/cl/_testdata/varinit/out.ll +++ b/cl/_testdata/varinit/out.ll @@ -1,8 +1,8 @@ ; ModuleID = 'main' source_filename = "main" -@"init$guard" = external global ptr -@a = external global ptr +@"init$guard" = global ptr null +@a = global ptr null define void @init() { _llgo_0: diff --git a/cl/compile.go b/cl/compile.go index bcffef30..eaf12345 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -53,6 +53,7 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) llssa.Global return g } g := pkg.NewVar(gbl.Name(), gbl.Type()) + g.Init(p.prog.Null(g.Type)) p.glbs[gbl] = g return g } diff --git a/cl/compile_test.go b/cl/compile_test.go index 54e9023e..d4810cd8 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -115,8 +115,8 @@ var a int `, `; ModuleID = 'foo' source_filename = "foo" -@"init$guard" = external global ptr -@a = external global ptr +@"init$guard" = global ptr null +@a = global ptr null define void @init() { _llgo_0: @@ -142,7 +142,7 @@ func fn(a int, b float64) int { `, `; ModuleID = 'foo' source_filename = "foo" -@"init$guard" = external global ptr +@"init$guard" = global ptr null define void @init() { _llgo_0: diff --git a/ssa/decl.go b/ssa/decl.go index 477afc30..878789f8 100644 --- a/ssa/decl.go +++ b/ssa/decl.go @@ -46,11 +46,13 @@ type aGlobal struct { // A Global is a named Value holding the address of a package-level // variable. -// -// Pos() returns the position of the ast.ValueSpec.Names[*] -// identifier. type Global = *aGlobal +// Init initializes the global variable with the given value. +func (g Global) Init(v Expr) { + g.impl.SetInitializer(v.impl) +} + // ----------------------------------------------------------------------------- // Function represents the parameters, results, and code of a function diff --git a/ssa/expr.go b/ssa/expr.go index e13821ad..67609313 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -43,6 +43,10 @@ func llvmValues(vals []Expr) []llvm.Value { // ----------------------------------------------------------------------------- +func (p Program) Null(t Type) Expr { + return Expr{llvm.ConstNull(t.ll), t} +} + func (p Program) BoolVal(v bool) Expr { t := p.Bool() var bv uint64 diff --git a/ssa/ssa_test.go b/ssa/ssa_test.go index 98f01bf0..3fe9f1cf 100644 --- a/ssa/ssa_test.go +++ b/ssa/ssa_test.go @@ -37,11 +37,15 @@ func assertPkg(t *testing.T, p Package, expected string) { func TestVar(t *testing.T) { prog := NewProgram(nil) pkg := prog.NewPackage("bar", "foo/bar") - pkg.NewVar("a", types.Typ[types.Int]) + a := pkg.NewVar("a", types.Typ[types.Int]) + a.Init(prog.Val(100)) + b := pkg.NewVar("b", types.Typ[types.Int]) + b.Init(a.Expr) assertPkg(t, pkg, `; ModuleID = 'foo/bar' source_filename = "foo/bar" -@a = external global i64 +@a = global i64 100 +@b = global i64 @a `) }