]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/noder/unified.go
cmd/compile: remove 'ext' fields from unified IR reader/writer types
[gostls13.git] / src / cmd / compile / internal / noder / unified.go
1 // UNREVIEWED
2
3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package noder
8
9 import (
10         "bytes"
11         "fmt"
12         "internal/goversion"
13         "io"
14         "runtime"
15         "sort"
16
17         "cmd/compile/internal/base"
18         "cmd/compile/internal/inline"
19         "cmd/compile/internal/ir"
20         "cmd/compile/internal/typecheck"
21         "cmd/compile/internal/types"
22         "cmd/compile/internal/types2"
23         "cmd/internal/src"
24 )
25
26 // localPkgReader holds the package reader used for reading the local
27 // package. It exists so the unified IR linker can refer back to it
28 // later.
29 var localPkgReader *pkgReader
30
31 // unified construct the local package's IR from syntax's AST.
32 //
33 // The pipeline contains 2 steps:
34 //
35 // (1) Generate package export data "stub".
36 //
37 // (2) Generate package IR from package export data.
38 //
39 // The package data "stub" at step (1) contains everything from the local package,
40 // but nothing that have been imported. When we're actually writing out export data
41 // to the output files (see writeNewExport function), we run the "linker", which does
42 // a few things:
43 //
44 // + Updates compiler extensions data (e.g., inlining cost, escape analysis results).
45 //
46 // + Handles re-exporting any transitive dependencies.
47 //
48 // + Prunes out any unnecessary details (e.g., non-inlineable functions, because any
49 //   downstream importers only care about inlinable functions).
50 //
51 // The source files are typechecked twice, once before writing export data
52 // using types2 checker, once after read export data using gc/typecheck.
53 // This duplication of work will go away once we always use types2 checker,
54 // we can remove the gc/typecheck pass. The reason it is still here:
55 //
56 // + It reduces engineering costs in maintaining a fork of typecheck
57 //   (e.g., no need to backport fixes like CL 327651).
58 //
59 // + It makes it easier to pass toolstash -cmp.
60 //
61 // + Historically, we would always re-run the typechecker after import, even though
62 //   we know the imported data is valid. It's not ideal, but also not causing any
63 //   problem either.
64 //
65 // + There's still transformation that being done during gc/typecheck, like rewriting
66 //   multi-valued function call, or transform ir.OINDEX -> ir.OINDEXMAP.
67 //
68 // Using syntax+types2 tree, which already has a complete representation of generics,
69 // the unified IR has the full typed AST for doing introspection during step (1).
70 // In other words, we have all necessary information to build the generic IR form
71 // (see writer.captureVars for an example).
72 func unified(noders []*noder) {
73         inline.NewInline = InlineCall
74
75         if !quirksMode() {
76                 writeNewExportFunc = writeNewExport
77         } else if base.Flag.G != 0 {
78                 base.Errorf("cannot use -G and -d=quirksmode together")
79         }
80
81         newReadImportFunc = func(data string, pkg1 *types.Pkg, env *types2.Environment, packages map[string]*types2.Package) (pkg2 *types2.Package, err error) {
82                 pr := newPkgDecoder(pkg1.Path, data)
83
84                 // Read package descriptors for both types2 and compiler backend.
85                 readPackage(newPkgReader(pr), pkg1)
86                 pkg2 = readPackage2(env, packages, pr)
87                 return
88         }
89
90         data := writePkgStub(noders)
91
92         // We already passed base.Flag.Lang to types2 to handle validating
93         // the user's source code. Bump it up now to the current version and
94         // re-parse, so typecheck doesn't complain if we construct IR that
95         // utilizes newer Go features.
96         base.Flag.Lang = fmt.Sprintf("go1.%d", goversion.Version)
97         types.ParseLangFlag()
98
99         assert(types.LocalPkg.Path == "")
100         types.LocalPkg.Height = 0 // reset so pkgReader.pkgIdx doesn't complain
101         target := typecheck.Target
102
103         typecheck.TypecheckAllowed = true
104
105         localPkgReader = newPkgReader(newPkgDecoder(types.LocalPkg.Path, data))
106         readPackage(localPkgReader, types.LocalPkg)
107
108         r := localPkgReader.newReader(relocMeta, privateRootIdx, syncPrivate)
109         r.pkgInit(types.LocalPkg, target)
110
111         // Type-check any top-level assignments. We ignore non-assignments
112         // here because other declarations are typechecked as they're
113         // constructed.
114         for i, ndecls := 0, len(target.Decls); i < ndecls; i++ {
115                 switch n := target.Decls[i]; n.Op() {
116                 case ir.OAS, ir.OAS2:
117                         target.Decls[i] = typecheck.Stmt(n)
118                 }
119         }
120
121         // Don't use range--bodyIdx can add closures to todoBodies.
122         for len(todoBodies) > 0 {
123                 // The order we expand bodies doesn't matter, so pop from the end
124                 // to reduce todoBodies reallocations if it grows further.
125                 fn := todoBodies[len(todoBodies)-1]
126                 todoBodies = todoBodies[:len(todoBodies)-1]
127
128                 pri, ok := bodyReader[fn]
129                 assert(ok)
130                 pri.funcBody(fn)
131
132                 // Instantiated generic function: add to Decls for typechecking
133                 // and compilation.
134                 if fn.OClosure == nil && len(pri.dict.targs) != 0 {
135                         target.Decls = append(target.Decls, fn)
136                 }
137         }
138         todoBodies = nil
139
140         // Check that nothing snuck past typechecking.
141         for _, n := range target.Decls {
142                 if n.Typecheck() == 0 {
143                         base.FatalfAt(n.Pos(), "missed typecheck: %v", n)
144                 }
145
146                 // For functions, check that at least their first statement (if
147                 // any) was typechecked too.
148                 if fn, ok := n.(*ir.Func); ok && len(fn.Body) != 0 {
149                         if stmt := fn.Body[0]; stmt.Typecheck() == 0 {
150                                 base.FatalfAt(stmt.Pos(), "missed typecheck: %v", stmt)
151                         }
152                 }
153         }
154
155         base.ExitIfErrors() // just in case
156 }
157
158 // writePkgStub type checks the given parsed source files,
159 // writes an export data package stub representing them,
160 // and returns the result.
161 func writePkgStub(noders []*noder) string {
162         m, pkg, info := checkFiles(noders)
163
164         pw := newPkgWriter(m, pkg, info)
165
166         pw.collectDecls(noders)
167
168         publicRootWriter := pw.newWriter(relocMeta, syncPublic)
169         privateRootWriter := pw.newWriter(relocMeta, syncPrivate)
170
171         assert(publicRootWriter.idx == publicRootIdx)
172         assert(privateRootWriter.idx == privateRootIdx)
173
174         {
175                 w := publicRootWriter
176                 w.pkg(pkg)
177                 w.bool(false) // has init; XXX
178
179                 scope := pkg.Scope()
180                 names := scope.Names()
181                 w.len(len(names))
182                 for _, name := range scope.Names() {
183                         w.obj(scope.Lookup(name), nil)
184                 }
185
186                 w.sync(syncEOF)
187                 w.flush()
188         }
189
190         {
191                 w := privateRootWriter
192                 w.pkgInit(noders)
193                 w.flush()
194         }
195
196         var sb bytes.Buffer // TODO(mdempsky): strings.Builder after #44505 is resolved
197         pw.dump(&sb)
198
199         // At this point, we're done with types2. Make sure the package is
200         // garbage collected.
201         freePackage(pkg)
202
203         return sb.String()
204 }
205
206 // freePackage ensures the given package is garbage collected.
207 func freePackage(pkg *types2.Package) {
208         // The GC test below relies on a precise GC that runs finalizers as
209         // soon as objects are unreachable. Our implementation provides
210         // this, but other/older implementations may not (e.g., Go 1.4 does
211         // not because of #22350). To avoid imposing unnecessary
212         // restrictions on the GOROOT_BOOTSTRAP toolchain, we skip the test
213         // during bootstrapping.
214         if base.CompilerBootstrap {
215                 return
216         }
217
218         // Set a finalizer on pkg so we can detect if/when it's collected.
219         done := make(chan struct{})
220         runtime.SetFinalizer(pkg, func(*types2.Package) { close(done) })
221
222         // Important: objects involved in cycles are not finalized, so zero
223         // out pkg to break its cycles and allow the finalizer to run.
224         *pkg = types2.Package{}
225
226         // It typically takes just 1 or 2 cycles to release pkg, but it
227         // doesn't hurt to try a few more times.
228         for i := 0; i < 10; i++ {
229                 select {
230                 case <-done:
231                         return
232                 default:
233                         runtime.GC()
234                 }
235         }
236
237         base.Fatalf("package never finalized")
238 }
239
240 func readPackage(pr *pkgReader, importpkg *types.Pkg) {
241         r := pr.newReader(relocMeta, publicRootIdx, syncPublic)
242
243         pkg := r.pkg()
244         assert(pkg == importpkg)
245
246         if r.bool() {
247                 sym := pkg.Lookup(".inittask")
248                 task := ir.NewNameAt(src.NoXPos, sym)
249                 task.Class = ir.PEXTERN
250                 sym.Def = task
251         }
252
253         for i, n := 0, r.len(); i < n; i++ {
254                 r.sync(syncObject)
255                 assert(!r.bool())
256                 idx := r.reloc(relocObj)
257                 assert(r.len() == 0)
258
259                 path, name, code := r.p.peekObj(idx)
260                 if code != objStub {
261                         objReader[types.NewPkg(path, "").Lookup(name)] = pkgReaderIndex{pr, idx, nil}
262                 }
263         }
264 }
265
266 func writeNewExport(out io.Writer) {
267         l := linker{
268                 pw: newPkgEncoder(),
269
270                 pkgs:  make(map[string]int),
271                 decls: make(map[*types.Sym]int),
272         }
273
274         publicRootWriter := l.pw.newEncoder(relocMeta, syncPublic)
275         assert(publicRootWriter.idx == publicRootIdx)
276
277         var selfPkgIdx int
278
279         {
280                 pr := localPkgReader
281                 r := pr.newDecoder(relocMeta, publicRootIdx, syncPublic)
282
283                 r.sync(syncPkg)
284                 selfPkgIdx = l.relocIdx(pr, relocPkg, r.reloc(relocPkg))
285
286                 r.bool() // has init
287
288                 for i, n := 0, r.len(); i < n; i++ {
289                         r.sync(syncObject)
290                         assert(!r.bool())
291                         idx := r.reloc(relocObj)
292                         assert(r.len() == 0)
293
294                         xpath, xname, xtag := pr.peekObj(idx)
295                         assert(xpath == pr.pkgPath)
296                         assert(xtag != objStub)
297
298                         if types.IsExported(xname) {
299                                 l.relocIdx(pr, relocObj, idx)
300                         }
301                 }
302
303                 r.sync(syncEOF)
304         }
305
306         {
307                 var idxs []int
308                 for _, idx := range l.decls {
309                         idxs = append(idxs, idx)
310                 }
311                 sort.Ints(idxs)
312
313                 w := publicRootWriter
314
315                 w.sync(syncPkg)
316                 w.reloc(relocPkg, selfPkgIdx)
317
318                 w.bool(typecheck.Lookup(".inittask").Def != nil)
319
320                 w.len(len(idxs))
321                 for _, idx := range idxs {
322                         w.sync(syncObject)
323                         w.bool(false)
324                         w.reloc(relocObj, idx)
325                         w.len(0)
326                 }
327
328                 w.sync(syncEOF)
329                 w.flush()
330         }
331
332         l.pw.dump(out)
333 }