From 3146cc86d4bf40370b59d2dc6c62763585eb7274 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 19 Aug 2018 20:41:05 +0200 Subject: [PATCH] Move Compiler.program to Program.program --- compiler.go | 18 +++++++----------- ir.go | 7 ++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/compiler.go b/compiler.go index df103f75..b32c3780 100644 --- a/compiler.go +++ b/compiler.go @@ -46,8 +46,6 @@ type Compiler struct { coroSuspendFunc llvm.Value coroEndFunc llvm.Value coroFreeFunc llvm.Value - program *ssa.Program - mainPkg *ssa.Package initFuncs []llvm.Value ir *Program } @@ -73,7 +71,6 @@ func NewCompiler(pkgName, triple string, dumpSSA bool) (*Compiler, error) { c := &Compiler{ dumpSSA: dumpSSA, triple: triple, - ir: NewProgram(), } target, err := llvm.GetTargetFromTriple(triple) @@ -160,10 +157,9 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error { } } - c.program = ssautil.CreateProgram(lprogram, ssa.SanityCheckFunctions|ssa.BareInits) - c.program.Build() - - c.mainPkg = c.program.ImportedPackage(mainPath) + program := ssautil.CreateProgram(lprogram, ssa.SanityCheckFunctions|ssa.BareInits) + program.Build() + c.ir = NewProgram(program, mainPath) // Make a list of packages in import order. packageList := []*ssa.Package{} @@ -171,7 +167,7 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error { worklist := []string{"runtime", mainPath} for len(worklist) != 0 { pkgPath := worklist[0] - pkg := c.program.ImportedPackage(pkgPath) + pkg := program.ImportedPackage(pkgPath) if pkg == nil { // Non-SSA package (e.g. cgo). packageSet[pkgPath] = struct{}{} @@ -322,12 +318,12 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error { // Adjust main function. main := c.mod.NamedFunction("main.main") - realMain := c.mod.NamedFunction(c.mainPkg.Pkg.Path() + ".main") + realMain := c.mod.NamedFunction(c.ir.mainPkg.Pkg.Path() + ".main") if !realMain.IsNil() { main.ReplaceAllUsesWith(realMain) } mainAsync := c.mod.NamedFunction("main.main$async") - realMainAsync := c.mod.NamedFunction(c.mainPkg.Pkg.Path() + ".main$async") + realMainAsync := c.mod.NamedFunction(c.ir.mainPkg.Pkg.Path() + ".main$async") if !realMainAsync.IsNil() { mainAsync.ReplaceAllUsesWith(realMainAsync) } @@ -361,7 +357,7 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error { tuple := llvm.ConstNamedStruct(tupleType, tupleValues) tuples = append(tuples, tuple) for _, method := range meta.Methods { - f := c.ir.GetFunction(c.program.MethodValue(method)) + f := c.ir.GetFunction(program.MethodValue(method)) if f.llvmFn.IsNil() { return errors.New("cannot find function: " + f.Name(false)) } diff --git a/ir.go b/ir.go index 1d007bc3..074e7932 100644 --- a/ir.go +++ b/ir.go @@ -12,6 +12,8 @@ import ( // View on all functions, types, and globals in a program, with analysis // results. type Program struct { + program *ssa.Program + mainPkg *ssa.Package Functions []*Function functionMap map[*ssa.Function]*Function Globals []*Global @@ -52,8 +54,11 @@ type InterfaceType struct { Methods map[string]*types.Selection } -func NewProgram() *Program { +// Create and intialize a new *Program from a *ssa.Program. +func NewProgram(program *ssa.Program, mainPath string) *Program { return &Program{ + program: program, + mainPkg: program.ImportedPackage(mainPath), functionMap: make(map[*ssa.Function]*Function), globalMap: make(map[*ssa.Global]*Global), methodSignatureNames: make(map[string]int),