// Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package ssa // This file implements the BUILD phase of SSA construction. // // SSA construction has two phases, CREATE and BUILD. In the CREATE phase // (create.go), all packages are constructed and type-checked and // definitions of all package members are created, method-sets are // computed, and wrapper methods are synthesized. // ssa.Packages are created in arbitrary order. // // In the BUILD phase (builder.go), the builder traverses the AST of // each Go source function and generates SSA instructions for the // function body. Initializer expressions for package-level variables // are emitted to the package's init() function in the order specified // by go/types.Info.InitOrder, then code for each function in the // package is generated in lexical order. // The BUILD phases for distinct packages are independent and are // executed in parallel. // // TODO(adonovan): indeed, building functions is now embarrassingly parallel. // Audit for concurrency then benchmark using more goroutines. // // State: // // The Package's and Program's indices (maps) are populated and // mutated during the CREATE phase, but during the BUILD phase they // remain constant. The sole exception is Prog.methodSets and its // related maps, which are protected by a dedicated mutex. // // Generic functions declared in a package P can be instantiated from functions // outside of P. This happens independently of the CREATE and BUILD phase of P. // // Locks: // // Mutexes are currently acquired according to the following order: // Prog.methodsMu ⊃ canonizer.mu ⊃ printMu // where x ⊃ y denotes that y can be acquired while x is held // and x cannot be acquired while y is held. // // Synthetics: // // During the BUILD phase new functions can be created and built. These include: // - wrappers (wrappers, bounds, thunks) // - generic function instantiations // These functions do not belong to a specific Pkg (Pkg==nil). Instead the // Package that led to them being CREATED is obligated to ensure these // are BUILT during the BUILD phase of the Package. // // Runtime types: // // A concrete type is a type that is fully monomorphized with concrete types, // i.e. it cannot reach a TypeParam type. // Some concrete types require full runtime type information. Cases // include checking whether a type implements an interface or // interpretation by the reflect package. All such types that may require // this information will have all of their method sets built and will be added to Prog.methodSets. // A type T is considered to require runtime type information if it is // a runtime type and has a non-empty method set and either: // - T flows into a MakeInterface instructions, // - T appears in a concrete exported member, or // - T is a type reachable from a type S that has non-empty method set. // For any such type T, method sets must be created before the BUILD // phase of the package is done. // // Function literals: // // The BUILD phase of a function literal (anonymous function) is tied to the // BUILD phase of the enclosing parent function. The FreeVars of an anonymous // function are discovered by building the anonymous function. This in turn // changes which variables must be bound in a MakeClosure instruction in the // parent. Anonymous functions also track where they are referred to in their // parent function. // // Happens-before: // // The above discussion leads to the following happens-before relation for // the BUILD and CREATE phases. // The happens-before relation (with X