func (c *compiler) buildPackageInitData(mainPkg *ssa.Package) gccgoimporter.InitData { var inits []gccgoimporter.PackageInit for _, imp := range mainPkg.Object.Imports() { inits = append(inits, c.InitMap[imp].Inits...) } sort.Sort(byPriorityThenFunc(inits)) // Deduplicate init entries. We want to preserve the entry with the highest priority. // Normally a package's priorities will be consistent among its dependencies, but it is // possible for them to be different. For example, if a standard library test augments a // package which is a dependency of 'regexp' (which is imported by every test main package) // with additional dependencies, those dependencies may cause the package under test to // receive a higher priority than indicated by its init clause in 'regexp'. uniqinits := make([]gccgoimporter.PackageInit, len(inits)) uniqinitpos := len(inits) uniqinitnames := make(map[string]struct{}) for i, _ := range inits { init := inits[len(inits)-1-i] if _, ok := uniqinitnames[init.InitFunc]; !ok { uniqinitnames[init.InitFunc] = struct{}{} uniqinitpos-- uniqinits[uniqinitpos] = init } } uniqinits = uniqinits[uniqinitpos:] ourprio := 1 if len(uniqinits) != 0 { ourprio = uniqinits[len(uniqinits)-1].Priority + 1 } if imp := mainPkg.Func("init"); imp != nil { impname := c.types.mc.mangleFunctionName(imp) uniqinits = append(uniqinits, gccgoimporter.PackageInit{mainPkg.Object.Name(), impname, ourprio}) } return gccgoimporter.InitData{ourprio, uniqinits} }
// Interpret interprets the Go program whose main package is mainpkg. // mode specifies various interpreter options. filename and args are // the initial values of os.Args for the target program. sizes is the // effective type-sizing function for this program. // // Interpret returns the exit code of the program: 2 for panic (like // gc does), or the argument to os.Exit for normal termination. // // The SSA program must include the "runtime" package. // func Interpret(mainpkg *ssa.Package, mode Mode, sizes types.Sizes, filename string, args []string) (exitCode int) { i := &interpreter{ prog: mainpkg.Prog, globals: make(map[ssa.Value]*value), mode: mode, sizes: sizes, } runtimePkg := i.prog.ImportedPackage("runtime") if runtimePkg == nil { panic("ssa.Program doesn't include runtime package") } i.runtimeErrorString = runtimePkg.Type("errorString").Object().Type() initReflect(i) i.osArgs = append(i.osArgs, filename) for _, arg := range args { i.osArgs = append(i.osArgs, arg) } for _, pkg := range i.prog.AllPackages() { // Initialize global storage. for _, m := range pkg.Members { switch v := m.(type) { case *ssa.Global: cell := zero(deref(v.Type())) i.globals[v] = &cell } } // Ad-hoc initialization for magic system variables. switch pkg.Object.Path() { case "syscall": setGlobal(i, pkg, "envs", environ) case "reflect": deleteBodies(pkg, "DeepEqual", "deepValueEqual") case "runtime": sz := sizes.Sizeof(pkg.Object.Scope().Lookup("MemStats").Type()) setGlobal(i, pkg, "sizeof_C_MStats", uintptr(sz)) deleteBodies(pkg, "GOROOT", "gogetenv") } } // Top-level error handler. exitCode = 2 defer func() { if exitCode != 2 || i.mode&DisableRecover != 0 { return } switch p := recover().(type) { case exitPanic: exitCode = int(p) return case targetPanic: fmt.Fprintln(os.Stderr, "panic:", toString(p.v)) case runtime.Error: fmt.Fprintln(os.Stderr, "panic:", p.Error()) case string: fmt.Fprintln(os.Stderr, "panic:", p) default: fmt.Fprintf(os.Stderr, "panic: unexpected type: %T: %v\n", p, p) } // TODO(adonovan): dump panicking interpreter goroutine? // buf := make([]byte, 0x10000) // runtime.Stack(buf, false) // fmt.Fprintln(os.Stderr, string(buf)) // (Or dump panicking target goroutine?) }() // Run! call(i, nil, token.NoPos, mainpkg.Func("init"), nil) if mainFn := mainpkg.Func("main"); mainFn != nil { call(i, nil, token.NoPos, mainFn, nil) exitCode = 0 } else { fmt.Fprintln(os.Stderr, "No main function.") exitCode = 1 } return }