From 5bae55d75506f57b65a66a2be2402ab90c2705d4 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 24 Jan 2021 17:52:44 +0100 Subject: [PATCH] compiler: create runtime types lazily when needed This fixes a longstanding TODO comment and similar to https://github.com/tinygo-org/tinygo/pull/1593 it removes some code out of the compiler.CompileProgram function that doesn't need to be there. --- compiler/compiler.go | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 05895783..8bf9fcac 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -52,6 +52,7 @@ type compilerContext struct { program *ssa.Program diagnostics []error astComments map[string]*ast.CommentGroup + runtimePkg *types.Package } // newCompilerContext returns a new compiler context ready for use, most @@ -222,6 +223,7 @@ func CompileProgram(pkgName string, lprogram *loader.Program, machine llvm.Targe c.program = lprogram.LoadSSA() c.program.Build() + c.runtimePkg = c.program.ImportedPackage("runtime").Pkg // Run a simple dead code elimination pass. functions, err := c.simpleDCE(lprogram) @@ -242,20 +244,6 @@ func CompileProgram(pkgName string, lprogram *loader.Program, machine llvm.Targe c.loadASTComments(lprogram) - // Declare runtime types. - // TODO: lazily create runtime types in getLLVMRuntimeType when they are - // needed. Eventually this will be required anyway, when packages are - // compiled independently (and the runtime types are not available). - for _, member := range c.program.ImportedPackage("runtime").Members { - if member, ok := member.(*ssa.Type); ok { - if typ, ok := member.Type().(*types.Named); ok { - if _, ok := typ.Underlying().(*types.Struct); ok { - c.getLLVMType(typ) - } - } - } - } - // Predeclare the runtime.alloc function, which is used by the wordpack // functionality. c.getFunction(c.program.ImportedPackage("runtime").Members["alloc"].(*ssa.Function)) @@ -367,13 +355,8 @@ func CompilePackage(moduleName string, pkg *loader.Package, machine llvm.TargetM // it as a LLVM type, creating it if necessary. It is a shorthand for // getLLVMType(getRuntimeType(name)). func (c *compilerContext) getLLVMRuntimeType(name string) llvm.Type { - fullName := "runtime." + name - typ := c.mod.GetTypeByName(fullName) - if typ.IsNil() { - println(c.mod.String()) - panic("could not find runtime type: " + fullName) - } - return typ + typ := c.runtimePkg.Scope().Lookup(name).(*types.TypeName).Type() + return c.getLLVMType(typ) } // getLLVMType creates and returns a LLVM type for a Go type. In the case of