Move Compiler.program to Program.program

Этот коммит содержится в:
Ayke van Laethem 2018-08-19 20:41:05 +02:00
родитель e18fd6da16
коммит 3146cc86d4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
2 изменённых файлов: 13 добавлений и 12 удалений

Просмотреть файл

@ -46,8 +46,6 @@ type Compiler struct {
coroSuspendFunc llvm.Value coroSuspendFunc llvm.Value
coroEndFunc llvm.Value coroEndFunc llvm.Value
coroFreeFunc llvm.Value coroFreeFunc llvm.Value
program *ssa.Program
mainPkg *ssa.Package
initFuncs []llvm.Value initFuncs []llvm.Value
ir *Program ir *Program
} }
@ -73,7 +71,6 @@ func NewCompiler(pkgName, triple string, dumpSSA bool) (*Compiler, error) {
c := &Compiler{ c := &Compiler{
dumpSSA: dumpSSA, dumpSSA: dumpSSA,
triple: triple, triple: triple,
ir: NewProgram(),
} }
target, err := llvm.GetTargetFromTriple(triple) 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) program := ssautil.CreateProgram(lprogram, ssa.SanityCheckFunctions|ssa.BareInits)
c.program.Build() program.Build()
c.ir = NewProgram(program, mainPath)
c.mainPkg = c.program.ImportedPackage(mainPath)
// Make a list of packages in import order. // Make a list of packages in import order.
packageList := []*ssa.Package{} packageList := []*ssa.Package{}
@ -171,7 +167,7 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error {
worklist := []string{"runtime", mainPath} worklist := []string{"runtime", mainPath}
for len(worklist) != 0 { for len(worklist) != 0 {
pkgPath := worklist[0] pkgPath := worklist[0]
pkg := c.program.ImportedPackage(pkgPath) pkg := program.ImportedPackage(pkgPath)
if pkg == nil { if pkg == nil {
// Non-SSA package (e.g. cgo). // Non-SSA package (e.g. cgo).
packageSet[pkgPath] = struct{}{} packageSet[pkgPath] = struct{}{}
@ -322,12 +318,12 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error {
// Adjust main function. // Adjust main function.
main := c.mod.NamedFunction("main.main") 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() { if !realMain.IsNil() {
main.ReplaceAllUsesWith(realMain) main.ReplaceAllUsesWith(realMain)
} }
mainAsync := c.mod.NamedFunction("main.main$async") 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() { if !realMainAsync.IsNil() {
mainAsync.ReplaceAllUsesWith(realMainAsync) mainAsync.ReplaceAllUsesWith(realMainAsync)
} }
@ -361,7 +357,7 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error {
tuple := llvm.ConstNamedStruct(tupleType, tupleValues) tuple := llvm.ConstNamedStruct(tupleType, tupleValues)
tuples = append(tuples, tuple) tuples = append(tuples, tuple)
for _, method := range meta.Methods { for _, method := range meta.Methods {
f := c.ir.GetFunction(c.program.MethodValue(method)) f := c.ir.GetFunction(program.MethodValue(method))
if f.llvmFn.IsNil() { if f.llvmFn.IsNil() {
return errors.New("cannot find function: " + f.Name(false)) return errors.New("cannot find function: " + f.Name(false))
} }

7
ir.go
Просмотреть файл

@ -12,6 +12,8 @@ import (
// View on all functions, types, and globals in a program, with analysis // View on all functions, types, and globals in a program, with analysis
// results. // results.
type Program struct { type Program struct {
program *ssa.Program
mainPkg *ssa.Package
Functions []*Function Functions []*Function
functionMap map[*ssa.Function]*Function functionMap map[*ssa.Function]*Function
Globals []*Global Globals []*Global
@ -52,8 +54,11 @@ type InterfaceType struct {
Methods map[string]*types.Selection 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{ return &Program{
program: program,
mainPkg: program.ImportedPackage(mainPath),
functionMap: make(map[*ssa.Function]*Function), functionMap: make(map[*ssa.Function]*Function),
globalMap: make(map[*ssa.Global]*Global), globalMap: make(map[*ssa.Global]*Global),
methodSignatureNames: make(map[string]int), methodSignatureNames: make(map[string]int),