diff --git a/cl/compile.go b/cl/compile.go index 086408cb..1fc773fb 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -465,6 +465,9 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll pkgProg := pkg.Prog pkgTypes := pkg.Pkg pkgName, pkgPath := pkgTypes.Name(), llssa.PathOf(pkgTypes) + if pkgPath == llssa.PkgRuntime { + prog.SetRuntime(pkgTypes) + } ret = prog.NewPackage(pkgName, pkgPath) ctx := &context{ diff --git a/cl/compile_test.go b/cl/compile_test.go index 1ac09e2c..62f7bedb 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/goplus/llgo/cl/cltest" + "github.com/goplus/llgo/ssa" ) func testCompile(t *testing.T, src, expected string) { @@ -36,7 +37,7 @@ func TestFromTestdata(t *testing.T) { } func TestRuntime(t *testing.T) { - cltest.Pkg(t, "github.com/goplus/llgo/internal/runtime", "../internal/runtime/llgo_autogen.ll") + cltest.Pkg(t, ssa.PkgRuntime, "../internal/runtime/llgo_autogen.ll") } func TestVar(t *testing.T) { diff --git a/internal/build/build.go b/internal/build/build.go index 8f54116b..f55a0adf 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -108,7 +108,7 @@ func Do(args []string, conf *Config) { var rt []*packages.Package prog := llssa.NewProgram(nil) prog.SetRuntime(func() *types.Package { - rt, err = packages.Load(cfg, "github.com/goplus/llgo/internal/runtime") + rt, err = packages.Load(cfg, llssa.PkgRuntime) check(err) return rt[0].Types }) diff --git a/internal/llgen/llgenf.go b/internal/llgen/llgenf.go index b8bc71cb..2950d77c 100644 --- a/internal/llgen/llgenf.go +++ b/internal/llgen/llgenf.go @@ -50,7 +50,7 @@ func GenFrom(fileOrPkg string) string { prog := llssa.NewProgram(nil) prog.SetRuntime(func() *types.Package { - rt, err := packages.Load(cfg, "github.com/goplus/llgo/internal/runtime") + rt, err := packages.Load(cfg, llssa.PkgRuntime) check(err) return rt[0].Types }) diff --git a/internal/runtime/llgo_autogen.ll b/internal/runtime/llgo_autogen.ll index 8ba70e36..cf172692 100644 --- a/internal/runtime/llgo_autogen.ll +++ b/internal/runtime/llgo_autogen.ll @@ -8,8 +8,6 @@ source_filename = "github.com/goplus/llgo/internal/runtime" %"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, i1 (ptr, ptr), ptr, i32, i32 } %"github.com/goplus/llgo/internal/abi.Name" = type { ptr } %"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } -%"github.com/goplus/llgo/internal/runtime.String.0" = type { ptr, i64 } -%"github.com/goplus/llgo/internal/runtime.Slice.1" = type { ptr, i64, i64 } @"github.com/goplus/llgo/internal/runtime.TyAny" = global ptr null @"github.com/goplus/llgo/internal/runtime.basicTypes" = global ptr null @@ -240,10 +238,10 @@ _llgo_0: ret %"github.com/goplus/llgo/internal/runtime.iface" %12 } -define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String.0" %0) { +define %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"(%"github.com/goplus/llgo/internal/runtime.String" %0) { _llgo_0: %1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) - store %"github.com/goplus/llgo/internal/runtime.String.0" %0, ptr %1, align 8 + store %"github.com/goplus/llgo/internal/runtime.String" %0, ptr %1, align 8 %2 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 0 %4 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) @@ -288,17 +286,17 @@ _llgo_0: declare ptr @malloc(i64) -define %"github.com/goplus/llgo/internal/runtime.Slice.1" @"github.com/goplus/llgo/internal/runtime.NilSlice"() { +define %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NilSlice"() { _llgo_0: - %0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice.1", align 8 - %1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice.1", ptr %0, i32 0, i32 0 - %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice.1", ptr %0, i32 0, i32 1 - %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice.1", ptr %0, i32 0, i32 2 + %0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8 + %1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %0, i32 0, i32 0 + %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %0, i32 0, i32 1 + %3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %0, i32 0, i32 2 store ptr null, ptr %1, align 8 store i64 0, ptr %2, align 4 store i64 0, ptr %3, align 4 - %4 = load %"github.com/goplus/llgo/internal/runtime.Slice.1", ptr %0, align 8 - ret %"github.com/goplus/llgo/internal/runtime.Slice.1" %4 + %4 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %0, align 8 + ret %"github.com/goplus/llgo/internal/runtime.Slice" %4 } declare ptr @"(*github.com/goplus/llgo/internal/abi.Type).ArrayType"(ptr) diff --git a/ssa/cl_test.go b/ssa/cl_test.go index bc215f6e..ab36b123 100644 --- a/ssa/cl_test.go +++ b/ssa/cl_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/goplus/llgo/cl/cltest" + "github.com/goplus/llgo/ssa" ) func TestFromTestcgo(t *testing.T) { @@ -31,5 +32,5 @@ func TestFromTestdata(t *testing.T) { } func TestRuntime(t *testing.T) { - cltest.Pkg(t, "github.com/goplus/llgo/internal/runtime", "../internal/runtime/llgo_autogen.ll") + cltest.Pkg(t, ssa.PkgRuntime, "../internal/runtime/llgo_autogen.ll") } diff --git a/ssa/package.go b/ssa/package.go index 55c09666..5b5bd224 100644 --- a/ssa/package.go +++ b/ssa/package.go @@ -24,6 +24,10 @@ import ( "golang.org/x/tools/go/types/typeutil" ) +const ( + PkgRuntime = "github.com/goplus/llgo/internal/runtime" +) + // ----------------------------------------------------------------------------- type dbgFlags = int @@ -94,7 +98,7 @@ type aProgram struct { ctx llvm.Context typs typeutil.Map - rt *types.Scope + rt *types.Package rtget func() *types.Package target *Target @@ -140,19 +144,25 @@ func NewProgram(target *Target) Program { } // SetRuntime sets the runtime. -func (p Program) SetRuntime(runtime func() *types.Package) { - p.rtget = runtime +// Its type can be *types.Package or func() *types.Package. +func (p Program) SetRuntime(runtime any) { + switch v := runtime.(type) { + case *types.Package: + p.rt = v + case func() *types.Package: + p.rtget = v + } } -func (p Program) runtime() *types.Scope { +func (p Program) runtime() *types.Package { if p.rt == nil { - p.rt = p.rtget().Scope() + p.rt = p.rtget() } return p.rt } func (p Program) rtNamed(name string) *types.Named { - return p.runtime().Lookup(name).Type().(*types.Named) + return p.runtime().Scope().Lookup(name).Type().(*types.Named) } func (p Program) rtType(name string) Type { @@ -312,7 +322,7 @@ func (p Package) rtAbort() Expr { } func (p Package) rtFunc(fnName string) Expr { - fn := p.prog.runtime().Lookup(fnName).(*types.Func) + fn := p.prog.runtime().Scope().Lookup(fnName).(*types.Func) name := FullName(fn.Pkg(), fnName) return p.NewFunc(name, fn.Type().(*types.Signature)).Expr }