diff --git a/cl/_testrt/fprintf/out.ll b/cl/_testrt/fprintf/out.ll index 6c2c224c..ee0cd9d7 100644 --- a/cl/_testrt/fprintf/out.ll +++ b/cl/_testrt/fprintf/out.ll @@ -2,7 +2,7 @@ source_filename = "main" @"main.init$guard" = global ptr null -@"github.com/goplus/llgo/internal/runtime/c.Stderr" = external global ptr +@__stderrp = external global ptr @0 = private unnamed_addr constant [10 x i8] c"Hello %d\0A\00", align 1 define void @main.init() { @@ -22,7 +22,7 @@ define void @main() { _llgo_0: call void @"github.com/goplus/llgo/internal/runtime.init"() call void @main.init() - %0 = load ptr, ptr @"github.com/goplus/llgo/internal/runtime/c.Stderr", align 8 + %0 = load ptr, ptr @__stderrp, align 8 %1 = call i32 (ptr, ptr, ...) @fprintf(ptr %0, ptr @0, i64 100) ret void } diff --git a/cl/compile.go b/cl/compile.go index c70c59c6..a8975ed7 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -177,7 +177,7 @@ func (p *context) compileMethods(pkg llssa.Package, typ types.Type) { // Global variable. func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) { typ := gbl.Type() - name := llssa.FullName(gbl.Pkg.Pkg, gbl.Name()) + name := p.varName(gbl.Pkg.Pkg, gbl) if ignoreName(name) || checkCgo(gbl.Name()) { return } diff --git a/cl/compile_test.go b/cl/compile_test.go index de7abd2a..b9c3443f 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -29,7 +29,7 @@ func testCompile(t *testing.T, src, expected string) { } func TestFromTestrt(t *testing.T) { - cltest.FromDir(t, "fprintf", "./_testrt", true) + cltest.FromDir(t, "", "./_testrt", true) } func TestFromTestdata(t *testing.T) { diff --git a/cl/import.go b/cl/import.go index fbb06e49..0404deb5 100644 --- a/cl/import.go +++ b/cl/import.go @@ -210,6 +210,14 @@ func (p *context) funcName(pkg *types.Package, fn *ssa.Function, ignore bool) (s return name, goFunc } +func (p *context) varName(pkg *types.Package, v *ssa.Global) string { + name := llssa.FullName(pkg, v.Name()) + if v, ok := p.link[name]; ok { + return v + } + return name +} + // funcOf returns a function by name and set ftype = goFunc, cFunc, etc. // or returns nil and set ftype = llgoCstr, llgoAlloca, llgoUnreachable, etc. func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) { @@ -235,14 +243,14 @@ func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) { return } -func (p *context) varOf(v *ssa.Global) llssa.Global { +func (p *context) varOf(v *ssa.Global) (ret llssa.Global) { pkgTypes := p.ensureLoaded(v.Pkg.Pkg) pkg := p.pkg - name := llssa.FullName(pkgTypes, v.Name()) - if ret := pkg.VarOf(name); ret != nil { - return ret + name := p.varName(pkgTypes, v) + if ret = pkg.VarOf(name); ret == nil { + ret = pkg.NewVar(name, v.Type()) } - return pkg.NewVar(name, v.Type()) + return } func (p *context) ensureLoaded(pkgTypes *types.Package) *types.Package {