llgo/ssa.SetRuntime: prevent multiple loading of runtime
This commit is contained in:
@@ -465,6 +465,9 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll
|
|||||||
pkgProg := pkg.Prog
|
pkgProg := pkg.Prog
|
||||||
pkgTypes := pkg.Pkg
|
pkgTypes := pkg.Pkg
|
||||||
pkgName, pkgPath := pkgTypes.Name(), llssa.PathOf(pkgTypes)
|
pkgName, pkgPath := pkgTypes.Name(), llssa.PathOf(pkgTypes)
|
||||||
|
if pkgPath == llssa.PkgRuntime {
|
||||||
|
prog.SetRuntime(pkgTypes)
|
||||||
|
}
|
||||||
ret = prog.NewPackage(pkgName, pkgPath)
|
ret = prog.NewPackage(pkgName, pkgPath)
|
||||||
|
|
||||||
ctx := &context{
|
ctx := &context{
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/goplus/llgo/cl/cltest"
|
"github.com/goplus/llgo/cl/cltest"
|
||||||
|
"github.com/goplus/llgo/ssa"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testCompile(t *testing.T, src, expected string) {
|
func testCompile(t *testing.T, src, expected string) {
|
||||||
@@ -36,7 +37,7 @@ func TestFromTestdata(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRuntime(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) {
|
func TestVar(t *testing.T) {
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ func Do(args []string, conf *Config) {
|
|||||||
var rt []*packages.Package
|
var rt []*packages.Package
|
||||||
prog := llssa.NewProgram(nil)
|
prog := llssa.NewProgram(nil)
|
||||||
prog.SetRuntime(func() *types.Package {
|
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)
|
check(err)
|
||||||
return rt[0].Types
|
return rt[0].Types
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func GenFrom(fileOrPkg string) string {
|
|||||||
|
|
||||||
prog := llssa.NewProgram(nil)
|
prog := llssa.NewProgram(nil)
|
||||||
prog.SetRuntime(func() *types.Package {
|
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)
|
check(err)
|
||||||
return rt[0].Types
|
return rt[0].Types
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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.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/abi.Name" = type { ptr }
|
||||||
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
|
%"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.TyAny" = global ptr null
|
||||||
@"github.com/goplus/llgo/internal/runtime.basicTypes" = 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
|
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:
|
_llgo_0:
|
||||||
%1 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
|
%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
|
%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
|
%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)
|
%4 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
|
||||||
@@ -288,17 +286,17 @@ _llgo_0:
|
|||||||
|
|
||||||
declare ptr @malloc(i64)
|
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:
|
_llgo_0:
|
||||||
%0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice.1", align 8
|
%0 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
|
||||||
%1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice.1", ptr %0, i32 0, i32 0
|
%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.1", ptr %0, i32 0, i32 1
|
%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.1", ptr %0, i32 0, i32 2
|
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %0, i32 0, i32 2
|
||||||
store ptr null, ptr %1, align 8
|
store ptr null, ptr %1, align 8
|
||||||
store i64 0, ptr %2, align 4
|
store i64 0, ptr %2, align 4
|
||||||
store i64 0, ptr %3, align 4
|
store i64 0, ptr %3, align 4
|
||||||
%4 = load %"github.com/goplus/llgo/internal/runtime.Slice.1", ptr %0, align 8
|
%4 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %0, align 8
|
||||||
ret %"github.com/goplus/llgo/internal/runtime.Slice.1" %4
|
ret %"github.com/goplus/llgo/internal/runtime.Slice" %4
|
||||||
}
|
}
|
||||||
|
|
||||||
declare ptr @"(*github.com/goplus/llgo/internal/abi.Type).ArrayType"(ptr)
|
declare ptr @"(*github.com/goplus/llgo/internal/abi.Type).ArrayType"(ptr)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/goplus/llgo/cl/cltest"
|
"github.com/goplus/llgo/cl/cltest"
|
||||||
|
"github.com/goplus/llgo/ssa"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromTestcgo(t *testing.T) {
|
func TestFromTestcgo(t *testing.T) {
|
||||||
@@ -31,5 +32,5 @@ func TestFromTestdata(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRuntime(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")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ import (
|
|||||||
"golang.org/x/tools/go/types/typeutil"
|
"golang.org/x/tools/go/types/typeutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PkgRuntime = "github.com/goplus/llgo/internal/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
type dbgFlags = int
|
type dbgFlags = int
|
||||||
@@ -94,7 +98,7 @@ type aProgram struct {
|
|||||||
ctx llvm.Context
|
ctx llvm.Context
|
||||||
typs typeutil.Map
|
typs typeutil.Map
|
||||||
|
|
||||||
rt *types.Scope
|
rt *types.Package
|
||||||
rtget func() *types.Package
|
rtget func() *types.Package
|
||||||
|
|
||||||
target *Target
|
target *Target
|
||||||
@@ -140,19 +144,25 @@ func NewProgram(target *Target) Program {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetRuntime sets the runtime.
|
// SetRuntime sets the runtime.
|
||||||
func (p Program) SetRuntime(runtime func() *types.Package) {
|
// Its type can be *types.Package or func() *types.Package.
|
||||||
p.rtget = runtime
|
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 {
|
if p.rt == nil {
|
||||||
p.rt = p.rtget().Scope()
|
p.rt = p.rtget()
|
||||||
}
|
}
|
||||||
return p.rt
|
return p.rt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Program) rtNamed(name string) *types.Named {
|
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 {
|
func (p Program) rtType(name string) Type {
|
||||||
@@ -312,7 +322,7 @@ func (p Package) rtAbort() Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p Package) rtFunc(fnName string) 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)
|
name := FullName(fn.Pkg(), fnName)
|
||||||
return p.NewFunc(name, fn.Type().(*types.Signature)).Expr
|
return p.NewFunc(name, fn.Type().(*types.Signature)).Expr
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user