| |
| |
| |
|
|
| package gc |
|
|
| import ( |
| "bufio" |
| "bytes" |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/bloop" |
| "cmd/compile/internal/coverage" |
| "cmd/compile/internal/deadlocals" |
| "cmd/compile/internal/dwarfgen" |
| "cmd/compile/internal/escape" |
| "cmd/compile/internal/inline" |
| "cmd/compile/internal/inline/interleaved" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/logopt" |
| "cmd/compile/internal/loopvar" |
| "cmd/compile/internal/noder" |
| "cmd/compile/internal/pgoir" |
| "cmd/compile/internal/pkginit" |
| "cmd/compile/internal/reflectdata" |
| "cmd/compile/internal/rttype" |
| "cmd/compile/internal/slice" |
| "cmd/compile/internal/ssa" |
| "cmd/compile/internal/ssagen" |
| "cmd/compile/internal/staticinit" |
| "cmd/compile/internal/typecheck" |
| "cmd/compile/internal/types" |
| "cmd/internal/dwarf" |
| "cmd/internal/obj" |
| "cmd/internal/objabi" |
| "cmd/internal/src" |
| "cmd/internal/telemetry/counter" |
| "flag" |
| "fmt" |
| "internal/buildcfg" |
| "log" |
| "os" |
| "runtime" |
| ) |
|
|
| |
| |
| |
| |
| func handlePanic() { |
| if err := recover(); err != nil { |
| if err == "-h" { |
| |
| |
| panic(err) |
| } |
| base.Fatalf("panic: %v", err) |
| } |
| } |
|
|
| |
| |
| |
| func Main(archInit func(*ssagen.ArchInfo)) { |
| base.Timer.Start("fe", "init") |
| counter.Open() |
| counter.Inc("compile/invocations") |
|
|
| defer handlePanic() |
|
|
| archInit(&ssagen.Arch) |
|
|
| base.Ctxt = obj.Linknew(ssagen.Arch.LinkArch) |
| base.Ctxt.DiagFunc = base.Errorf |
| base.Ctxt.DiagFlush = base.FlushErrors |
| base.Ctxt.Bso = bufio.NewWriter(os.Stdout) |
|
|
| |
| |
| |
| |
| base.Ctxt.UseBASEntries = base.Ctxt.Headtype != objabi.Hdarwin |
|
|
| base.DebugSSA = ssa.PhaseOption |
| base.ParseFlags() |
|
|
| if flagGCStart := base.Debug.GCStart; flagGCStart > 0 || |
| os.Getenv("GOGC") == "" && os.Getenv("GOMEMLIMIT") == "" && base.Flag.LowerC != 1 { |
| startHeapMB := int64(128) |
| if flagGCStart > 0 { |
| startHeapMB = int64(flagGCStart) |
| } |
| base.AdjustStartingHeap(uint64(startHeapMB)<<20, 0, 0, 0, base.Debug.GCAdjust == 1) |
| } |
|
|
| types.LocalPkg = types.NewPkg(base.Ctxt.Pkgpath, "") |
|
|
| |
| types.BuiltinPkg = types.NewPkg("go.builtin", "") |
| types.BuiltinPkg.Prefix = "go:builtin" |
|
|
| |
| types.UnsafePkg = types.NewPkg("unsafe", "unsafe") |
|
|
| |
| |
| |
| |
| |
| ir.Pkgs.Runtime = types.NewPkg("go.runtime", "runtime") |
| ir.Pkgs.Runtime.Prefix = "runtime" |
|
|
| |
| |
| ir.Pkgs.InternalMaps = types.NewPkg("go.internal/runtime/maps", "internal/runtime/maps") |
| ir.Pkgs.InternalMaps.Prefix = "internal/runtime/maps" |
|
|
| |
| ir.Pkgs.Itab = types.NewPkg("go.itab", "go.itab") |
| ir.Pkgs.Itab.Prefix = "go:itab" |
|
|
| |
| ir.Pkgs.Go = types.NewPkg("go", "") |
|
|
| |
| ir.Pkgs.Coverage = types.NewPkg("go.coverage", "runtime/coverage") |
| ir.Pkgs.Coverage.Prefix = "runtime/coverage" |
|
|
| |
| |
| |
| dwarfgen.RecordFlags("B", "N", "l", "msan", "race", "asan", "shared", "dynlink", "dwarf", "dwarflocationlists", "dwarfbasentries", "smallframes", "spectre") |
|
|
| if !base.EnableTrace && base.Flag.LowerT { |
| log.Fatalf("compiler not built with support for -t") |
| } |
|
|
| |
| |
| |
| |
| if base.Flag.LowerL <= 1 { |
| base.Flag.LowerL = 1 - base.Flag.LowerL |
| } |
|
|
| if base.Flag.SmallFrames { |
| ir.MaxStackVarSize = 64 * 1024 |
| ir.MaxImplicitStackVarSize = 16 * 1024 |
| } |
|
|
| if base.Flag.Dwarf { |
| base.Ctxt.DebugInfo = dwarfgen.Info |
| base.Ctxt.GenAbstractFunc = dwarfgen.AbstractFunc |
| base.Ctxt.DwFixups = obj.NewDwarfFixupTable(base.Ctxt) |
| } else { |
| |
| base.Flag.GenDwarfInl = 0 |
| base.Ctxt.Flag_locationlists = false |
| } |
| if base.Ctxt.Flag_locationlists && len(base.Ctxt.Arch.DWARFRegisters) == 0 { |
| log.Fatalf("location lists requested but register mapping not available on %v", base.Ctxt.Arch.Name) |
| } |
|
|
| types.ParseLangFlag() |
|
|
| symABIs := ssagen.NewSymABIs() |
| if base.Flag.SymABIs != "" { |
| symABIs.ReadSymABIs(base.Flag.SymABIs) |
| } |
|
|
| if objabi.LookupPkgSpecial(base.Ctxt.Pkgpath).NoInstrument { |
| base.Flag.Race = false |
| base.Flag.MSan = false |
| base.Flag.ASan = false |
| } |
|
|
| ssagen.Arch.LinkArch.Init(base.Ctxt) |
| startProfile() |
| if base.Flag.Race || base.Flag.MSan || base.Flag.ASan { |
| base.Flag.Cfg.Instrumenting = true |
| } |
| if base.Flag.Dwarf { |
| dwarf.EnableLogging(base.Debug.DwarfInl != 0) |
| } |
| if base.Debug.SoftFloat != 0 { |
| ssagen.Arch.SoftFloat = true |
| } |
|
|
| if base.Flag.JSON != "" { |
| logopt.LogJsonOption(base.Flag.JSON) |
| } |
|
|
| ir.EscFmt = escape.Fmt |
| ir.IsIntrinsicCall = ssagen.IsIntrinsicCall |
| ir.IsIntrinsicSym = ssagen.IsIntrinsicSym |
| inline.SSADumpInline = ssagen.DumpInline |
| ssagen.InitEnv() |
|
|
| types.PtrSize = ssagen.Arch.LinkArch.PtrSize |
| types.RegSize = ssagen.Arch.LinkArch.RegSize |
| types.MaxWidth = ssagen.Arch.MAXWIDTH |
|
|
| typecheck.Target = new(ir.Package) |
|
|
| base.AutogeneratedPos = makePos(src.NewFileBase("<autogenerated>", "<autogenerated>"), 1, 0) |
|
|
| typecheck.InitUniverse() |
| typecheck.InitRuntime() |
| rttype.Init() |
|
|
| |
| |
| |
| ssagen.InitTables() |
|
|
| |
| noder.LoadPackage(flag.Args()) |
|
|
| |
| |
| |
| if base.Ctxt.Pkgpath == obj.UnlinkablePkg && types.LocalPkg.Name == "main" { |
| base.Ctxt.Pkgpath = "main" |
| types.LocalPkg.Path = "main" |
| types.LocalPkg.Prefix = "main" |
| } |
|
|
| dwarfgen.RecordPackageName() |
|
|
| |
| ssagen.InitConfig() |
|
|
| |
| coverage.Fixup() |
|
|
| |
| base.Timer.Start("fe", "pgo-load-profile") |
| var profile *pgoir.Profile |
| if base.Flag.PgoProfile != "" { |
| var err error |
| profile, err = pgoir.New(base.Flag.PgoProfile) |
| if err != nil { |
| log.Fatalf("%s: PGO error: %v", base.Flag.PgoProfile, err) |
| } |
| } |
|
|
| |
| bloop.BloopWalk(typecheck.Target) |
|
|
| |
| base.Timer.Start("fe", "devirtualize-and-inline") |
| interleaved.DevirtualizeAndInlinePackage(typecheck.Target, profile) |
|
|
| noder.MakeWrappers(typecheck.Target) |
|
|
| |
| var transformed []loopvar.VarAndLoop |
| for _, fn := range typecheck.Target.Funcs { |
| transformed = append(transformed, loopvar.ForCapture(fn)...) |
| } |
| ir.CurFunc = nil |
|
|
| |
| pkginit.MakeTask() |
|
|
| |
| |
| symABIs.GenABIWrappers() |
|
|
| deadlocals.Funcs(typecheck.Target.Funcs) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| base.Timer.Start("fe", "escapes") |
| escape.Funcs(typecheck.Target.Funcs) |
|
|
| slice.Funcs(typecheck.Target.Funcs) |
|
|
| loopvar.LogTransformations(transformed) |
|
|
| |
| |
| |
| |
| if base.Flag.CompilingRuntime { |
| ssagen.EnableNoWriteBarrierRecCheck() |
| } |
|
|
| ir.CurFunc = nil |
|
|
| reflectdata.WriteBasicTypes() |
|
|
| |
| |
| |
| |
| base.Timer.Start("be", "compilefuncs") |
| for nextFunc, nextExtern := 0, 0; ; { |
| reflectdata.WriteRuntimeTypes() |
|
|
| if nextExtern < len(typecheck.Target.Externs) { |
| switch n := typecheck.Target.Externs[nextExtern]; n.Op() { |
| case ir.ONAME: |
| dumpGlobal(n) |
| case ir.OLITERAL: |
| dumpGlobalConst(n) |
| case ir.OTYPE: |
| reflectdata.NeedRuntimeType(n.Type()) |
| } |
| nextExtern++ |
| continue |
| } |
|
|
| if nextFunc < len(typecheck.Target.Funcs) { |
| enqueueFunc(typecheck.Target.Funcs[nextFunc], symABIs) |
| nextFunc++ |
| continue |
| } |
|
|
| |
| |
| |
| if len(compilequeue) != 0 { |
| compileFunctions(profile) |
| continue |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| if base.Ctxt.DwFixups != nil { |
| base.Ctxt.DwFixups.Finalize(base.Ctxt.Pkgpath, base.Debug.DwarfInl != 0) |
| base.Ctxt.DwFixups = nil |
| base.Flag.GenDwarfInl = 0 |
| continue |
| } |
|
|
| break |
| } |
|
|
| base.Timer.AddEvent(int64(len(typecheck.Target.Funcs)), "funcs") |
|
|
| if base.Flag.CompilingRuntime { |
| |
| ssagen.NoWriteBarrierRecCheck() |
| } |
|
|
| |
| if base.Debug.WrapGlobalMapCtl != 1 { |
| staticinit.AddKeepRelocations() |
| } |
|
|
| |
| base.Timer.Start("be", "dumpobj") |
| dumpdata() |
| base.Ctxt.NumberSyms() |
| dumpobj() |
| if base.Flag.AsmHdr != "" { |
| dumpasmhdr() |
| } |
|
|
| ssagen.CheckLargeStacks() |
| typecheck.CheckFuncStack() |
|
|
| if len(compilequeue) != 0 { |
| base.Fatalf("%d uncompiled functions", len(compilequeue)) |
| } |
|
|
| logopt.FlushLoggedOpts(base.Ctxt, base.Ctxt.Pkgpath) |
| base.ExitIfErrors() |
|
|
| base.FlushErrors() |
| base.Timer.Stop() |
|
|
| if base.Flag.Bench != "" { |
| if err := writebench(base.Flag.Bench); err != nil { |
| log.Fatalf("cannot write benchmark data: %v", err) |
| } |
| } |
| } |
|
|
| func writebench(filename string) error { |
| f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) |
| if err != nil { |
| return err |
| } |
|
|
| var buf bytes.Buffer |
| fmt.Fprintln(&buf, "commit:", buildcfg.Version) |
| fmt.Fprintln(&buf, "goos:", runtime.GOOS) |
| fmt.Fprintln(&buf, "goarch:", runtime.GOARCH) |
| base.Timer.Write(&buf, "BenchmarkCompile:"+base.Ctxt.Pkgpath+":") |
|
|
| n, err := f.Write(buf.Bytes()) |
| if err != nil { |
| return err |
| } |
| if n != buf.Len() { |
| panic("bad writer") |
| } |
|
|
| return f.Close() |
| } |
|
|
| func makePos(b *src.PosBase, line, col uint) src.XPos { |
| return base.Ctxt.PosTable.XPos(src.MakePos(b, line, col)) |
| } |
|
|