| |
| |
| |
|
|
| package noder |
|
|
| import ( |
| "cmp" |
| "fmt" |
| "internal/pkgbits" |
| "internal/types/errors" |
| "io" |
| "runtime" |
| "slices" |
| "strings" |
|
|
| "cmd/compile/internal/base" |
| "cmd/compile/internal/inline" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/pgoir" |
| "cmd/compile/internal/typecheck" |
| "cmd/compile/internal/types" |
| "cmd/compile/internal/types2" |
| "cmd/internal/src" |
| ) |
|
|
| |
| |
| |
| var localPkgReader *pkgReader |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func LookupFunc(fullName string) (*ir.Func, error) { |
| pkgPath, symName, err := ir.ParseLinkFuncName(fullName) |
| if err != nil { |
| return nil, fmt.Errorf("error parsing symbol name %q: %v", fullName, err) |
| } |
|
|
| pkg, ok := types.PkgMap()[pkgPath] |
| if !ok { |
| return nil, fmt.Errorf("pkg %s doesn't exist in %v", pkgPath, types.PkgMap()) |
| } |
|
|
| |
| |
| |
| |
|
|
| fn, err := lookupFunction(pkg, symName) |
| if err == nil { |
| return fn, nil |
| } |
|
|
| fn, mErr := lookupMethod(pkg, symName) |
| if mErr == nil { |
| return fn, nil |
| } |
|
|
| return nil, fmt.Errorf("%s is not a function (%v) or method (%v)", fullName, err, mErr) |
| } |
|
|
| |
| |
| |
| |
| func PostLookupCleanup() { |
| readBodies(typecheck.Target, false) |
| } |
|
|
| func lookupFunction(pkg *types.Pkg, symName string) (*ir.Func, error) { |
| sym := pkg.Lookup(symName) |
|
|
| |
| |
| |
| pri, ok := objReader[sym] |
| if !ok { |
| return nil, fmt.Errorf("func sym %v missing objReader", sym) |
| } |
|
|
| node, err := pri.pr.objIdxMayFail(pri.idx, nil, nil, false) |
| if err != nil { |
| return nil, fmt.Errorf("func sym %v lookup error: %w", sym, err) |
| } |
| name := node.(*ir.Name) |
| if name.Op() != ir.ONAME || name.Class != ir.PFUNC { |
| return nil, fmt.Errorf("func sym %v refers to non-function name: %v", sym, name) |
| } |
| return name.Func, nil |
| } |
|
|
| func lookupMethod(pkg *types.Pkg, symName string) (*ir.Func, error) { |
| |
| |
| |
| |
| |
| |
| |
| typ, meth, err := ir.LookupMethodSelector(pkg, symName) |
| if err != nil { |
| return nil, fmt.Errorf("error looking up method symbol %q: %v", symName, err) |
| } |
|
|
| pri, ok := objReader[typ] |
| if !ok { |
| return nil, fmt.Errorf("type sym %v missing objReader", typ) |
| } |
|
|
| node, err := pri.pr.objIdxMayFail(pri.idx, nil, nil, false) |
| if err != nil { |
| return nil, fmt.Errorf("func sym %v lookup error: %w", typ, err) |
| } |
| name := node.(*ir.Name) |
| if name.Op() != ir.OTYPE { |
| return nil, fmt.Errorf("type sym %v refers to non-type name: %v", typ, name) |
| } |
| if name.Alias() { |
| return nil, fmt.Errorf("type sym %v refers to alias", typ) |
| } |
| if name.Type().IsInterface() { |
| return nil, fmt.Errorf("type sym %v refers to interface type", typ) |
| } |
|
|
| for _, m := range name.Type().Methods() { |
| if m.Sym == meth { |
| fn := m.Nname.(*ir.Name).Func |
| return fn, nil |
| } |
| } |
|
|
| return nil, fmt.Errorf("method %s missing from method set of %v", symName, typ) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func unified(m posMap, noders []*noder) { |
| inline.InlineCall = unifiedInlineCall |
| typecheck.HaveInlineBody = unifiedHaveInlineBody |
| pgoir.LookupFunc = LookupFunc |
| pgoir.PostLookupCleanup = PostLookupCleanup |
|
|
| data := writePkgStub(m, noders) |
|
|
| target := typecheck.Target |
|
|
| localPkgReader = newPkgReader(pkgbits.NewPkgDecoder(types.LocalPkg.Path, data)) |
| readPackage(localPkgReader, types.LocalPkg, true) |
|
|
| r := localPkgReader.newReader(pkgbits.SectionMeta, pkgbits.PrivateRootIdx, pkgbits.SyncPrivate) |
| r.pkgInit(types.LocalPkg, target) |
|
|
| readBodies(target, false) |
|
|
| |
| for _, fn := range target.Funcs { |
| if fn.Typecheck() == 0 { |
| base.FatalfAt(fn.Pos(), "missed typecheck: %v", fn) |
| } |
|
|
| |
| |
| if len(fn.Body) != 0 { |
| if stmt := fn.Body[0]; stmt.Typecheck() == 0 { |
| base.FatalfAt(stmt.Pos(), "missed typecheck: %v", stmt) |
| } |
| } |
| } |
|
|
| |
| |
| for _, fn := range target.Funcs { |
| if !base.Flag.CompilingRuntime && types.RuntimeSymName(fn.Sym()) != "" { |
| fn.Pragma |= ir.Norace |
| } |
| } |
|
|
| base.ExitIfErrors() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func readBodies(target *ir.Package, duringInlining bool) { |
| var inlDecls []*ir.Func |
|
|
| |
| for { |
| |
| |
| |
| |
| |
| |
| |
|
|
| if len(todoDicts) > 0 { |
| fn := todoDicts[len(todoDicts)-1] |
| todoDicts = todoDicts[:len(todoDicts)-1] |
| fn() |
| continue |
| } |
|
|
| if len(todoBodies) > 0 { |
| fn := todoBodies[len(todoBodies)-1] |
| todoBodies = todoBodies[:len(todoBodies)-1] |
|
|
| pri, ok := bodyReader[fn] |
| assert(ok) |
| pri.funcBody(fn) |
|
|
| |
| |
| if fn.OClosure == nil && len(pri.dict.targs) != 0 { |
| |
| |
| |
| canSkipNonGenericMethod := !(base.Ctxt.Flag_linkshared && ir.IsMethod(fn)) |
| if duringInlining && canSkipNonGenericMethod { |
| inlDecls = append(inlDecls, fn) |
| } else { |
| target.Funcs = append(target.Funcs, fn) |
| } |
| } |
|
|
| continue |
| } |
|
|
| break |
| } |
|
|
| todoDicts = nil |
| todoBodies = nil |
|
|
| if len(inlDecls) != 0 { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| oldLowerM := base.Flag.LowerM |
| base.Flag.LowerM = 0 |
| inline.CanInlineFuncs(inlDecls, nil) |
| base.Flag.LowerM = oldLowerM |
|
|
| for _, fn := range inlDecls { |
| fn.Body = nil |
| } |
| } |
| } |
|
|
| |
| |
| |
| func writePkgStub(m posMap, noders []*noder) string { |
| pkg, info, otherInfo := checkFiles(m, noders) |
|
|
| pw := newPkgWriter(m, pkg, info, otherInfo) |
|
|
| pw.collectDecls(noders) |
|
|
| publicRootWriter := pw.newWriter(pkgbits.SectionMeta, pkgbits.SyncPublic) |
| privateRootWriter := pw.newWriter(pkgbits.SectionMeta, pkgbits.SyncPrivate) |
|
|
| assert(publicRootWriter.Idx == pkgbits.PublicRootIdx) |
| assert(privateRootWriter.Idx == pkgbits.PrivateRootIdx) |
|
|
| { |
| w := publicRootWriter |
| w.pkg(pkg) |
|
|
| if w.Version().Has(pkgbits.HasInit) { |
| w.Bool(false) |
| } |
|
|
| scope := pkg.Scope() |
| names := scope.Names() |
| w.Len(len(names)) |
| for _, name := range names { |
| w.obj(scope.Lookup(name), nil) |
| } |
|
|
| w.Sync(pkgbits.SyncEOF) |
| w.Flush() |
| } |
|
|
| { |
| w := privateRootWriter |
| w.pkgInit(noders) |
| w.Flush() |
| } |
|
|
| var sb strings.Builder |
| pw.DumpTo(&sb) |
|
|
| |
| |
| freePackage(pkg) |
|
|
| return sb.String() |
| } |
|
|
| |
| func freePackage(pkg *types2.Package) { |
| |
| |
| |
| |
| |
| |
| if base.CompilerBootstrap || base.Debug.GCCheck == 0 { |
| *pkg = types2.Package{} |
| return |
| } |
|
|
| |
| done := make(chan struct{}) |
| runtime.SetFinalizer(pkg, func(*types2.Package) { close(done) }) |
|
|
| |
| |
| *pkg = types2.Package{} |
|
|
| |
| |
| for i := 0; i < 10; i++ { |
| select { |
| case <-done: |
| return |
| default: |
| runtime.GC() |
| } |
| } |
|
|
| base.Fatalf("package never finalized") |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func readPackage(pr *pkgReader, importpkg *types.Pkg, localStub bool) { |
| { |
| r := pr.newReader(pkgbits.SectionMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic) |
|
|
| pkg := r.pkg() |
| |
| if pkg != importpkg { |
| base.ErrorfAt(base.AutogeneratedPos, errors.BadImportPath, "mismatched import path, have %q (%p), want %q (%p)", pkg.Path, pkg, importpkg.Path, importpkg) |
| base.ErrorExit() |
| } |
|
|
| if r.Version().Has(pkgbits.HasInit) { |
| r.Bool() |
| } |
|
|
| for i, n := 0, r.Len(); i < n; i++ { |
| r.Sync(pkgbits.SyncObject) |
| if r.Version().Has(pkgbits.DerivedFuncInstance) { |
| assert(!r.Bool()) |
| } |
| idx := r.Reloc(pkgbits.SectionObj) |
| assert(r.Len() == 0) |
|
|
| path, name, code := r.p.PeekObj(idx) |
| if code != pkgbits.ObjStub { |
| objReader[types.NewPkg(path, "").Lookup(name)] = pkgReaderIndex{pr, idx, nil, nil, nil} |
| } |
| } |
|
|
| r.Sync(pkgbits.SyncEOF) |
| } |
|
|
| if !localStub { |
| r := pr.newReader(pkgbits.SectionMeta, pkgbits.PrivateRootIdx, pkgbits.SyncPrivate) |
|
|
| if r.Bool() { |
| sym := importpkg.Lookup(".inittask") |
| task := ir.NewNameAt(src.NoXPos, sym, nil) |
| task.Class = ir.PEXTERN |
| sym.Def = task |
| } |
|
|
| for i, n := 0, r.Len(); i < n; i++ { |
| path := r.String() |
| name := r.String() |
| idx := r.Reloc(pkgbits.SectionBody) |
|
|
| sym := types.NewPkg(path, "").Lookup(name) |
| if _, ok := importBodyReader[sym]; !ok { |
| importBodyReader[sym] = pkgReaderIndex{pr, idx, nil, nil, nil} |
| } |
| } |
|
|
| r.Sync(pkgbits.SyncEOF) |
| } |
| } |
|
|
| |
| |
| func writeUnifiedExport(out io.Writer) { |
| |
| version := pkgbits.V2 |
| l := linker{ |
| pw: pkgbits.NewPkgEncoder(version, base.Debug.SyncFrames), |
|
|
| pkgs: make(map[string]index), |
| decls: make(map[*types.Sym]index), |
| bodies: make(map[*types.Sym]index), |
| } |
|
|
| publicRootWriter := l.pw.NewEncoder(pkgbits.SectionMeta, pkgbits.SyncPublic) |
| privateRootWriter := l.pw.NewEncoder(pkgbits.SectionMeta, pkgbits.SyncPrivate) |
| assert(publicRootWriter.Idx == pkgbits.PublicRootIdx) |
| assert(privateRootWriter.Idx == pkgbits.PrivateRootIdx) |
|
|
| var selfPkgIdx index |
|
|
| { |
| pr := localPkgReader |
| r := pr.NewDecoder(pkgbits.SectionMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic) |
|
|
| r.Sync(pkgbits.SyncPkg) |
| selfPkgIdx = l.relocIdx(pr, pkgbits.SectionPkg, r.Reloc(pkgbits.SectionPkg)) |
|
|
| if r.Version().Has(pkgbits.HasInit) { |
| r.Bool() |
| } |
|
|
| for i, n := 0, r.Len(); i < n; i++ { |
| r.Sync(pkgbits.SyncObject) |
| if r.Version().Has(pkgbits.DerivedFuncInstance) { |
| assert(!r.Bool()) |
| } |
| idx := r.Reloc(pkgbits.SectionObj) |
| assert(r.Len() == 0) |
|
|
| xpath, xname, xtag := pr.PeekObj(idx) |
| assert(xpath == pr.PkgPath()) |
| assert(xtag != pkgbits.ObjStub) |
|
|
| if types.IsExported(xname) { |
| l.relocIdx(pr, pkgbits.SectionObj, idx) |
| } |
| } |
|
|
| r.Sync(pkgbits.SyncEOF) |
| } |
|
|
| { |
| var idxs []index |
| for _, idx := range l.decls { |
| idxs = append(idxs, idx) |
| } |
| slices.Sort(idxs) |
|
|
| w := publicRootWriter |
|
|
| w.Sync(pkgbits.SyncPkg) |
| w.Reloc(pkgbits.SectionPkg, selfPkgIdx) |
|
|
| if w.Version().Has(pkgbits.HasInit) { |
| w.Bool(false) |
| } |
|
|
| w.Len(len(idxs)) |
| for _, idx := range idxs { |
| w.Sync(pkgbits.SyncObject) |
| if w.Version().Has(pkgbits.DerivedFuncInstance) { |
| w.Bool(false) |
| } |
| w.Reloc(pkgbits.SectionObj, idx) |
| w.Len(0) |
| } |
|
|
| w.Sync(pkgbits.SyncEOF) |
| w.Flush() |
| } |
|
|
| { |
| type symIdx struct { |
| sym *types.Sym |
| idx index |
| } |
| var bodies []symIdx |
| for sym, idx := range l.bodies { |
| bodies = append(bodies, symIdx{sym, idx}) |
| } |
| slices.SortFunc(bodies, func(a, b symIdx) int { return cmp.Compare(a.idx, b.idx) }) |
|
|
| w := privateRootWriter |
|
|
| w.Bool(typecheck.Lookup(".inittask").Def != nil) |
|
|
| w.Len(len(bodies)) |
| for _, body := range bodies { |
| w.String(body.sym.Pkg.Path) |
| w.String(body.sym.Name) |
| w.Reloc(pkgbits.SectionBody, body.idx) |
| } |
|
|
| w.Sync(pkgbits.SyncEOF) |
| w.Flush() |
| } |
|
|
| base.Ctxt.Fingerprint = l.pw.DumpTo(out) |
| } |
|
|