2013-08-27 16:49:13 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-06-03 14:46:57 -06:00
|
|
|
package ssa
|
|
|
|
|
|
|
|
// This file implements the CREATE phase of SSA construction.
|
|
|
|
// See builder.go for explanation.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
2013-09-06 16:13:57 -06:00
|
|
|
"strings"
|
2013-06-03 14:46:57 -06:00
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
|
|
|
"code.google.com/p/go.tools/importer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BuilderMode is a bitmask of options for diagnostics and checking.
|
|
|
|
type BuilderMode uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogPackages BuilderMode = 1 << iota // Dump package inventory to stderr
|
|
|
|
LogFunctions // Dump function SSA code to stderr
|
|
|
|
LogSource // Show source locations as SSA builder progresses
|
|
|
|
SanityCheckFunctions // Perform sanity checking of function bodies
|
|
|
|
NaiveForm // Build naïve SSA form: don't replace local loads/stores with registers
|
|
|
|
BuildSerially // Build packages serially, not in parallel.
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewProgram returns a new SSA Program initially containing no
|
|
|
|
// packages.
|
|
|
|
//
|
|
|
|
// fset specifies the mapping from token positions to source location
|
|
|
|
// that will be used by all ASTs of this program.
|
|
|
|
//
|
|
|
|
// mode controls diagnostics and checking during SSA construction.
|
|
|
|
//
|
|
|
|
func NewProgram(fset *token.FileSet, mode BuilderMode) *Program {
|
|
|
|
prog := &Program{
|
2013-07-01 13:24:50 -06:00
|
|
|
Fset: fset,
|
2013-09-06 16:13:57 -06:00
|
|
|
imported: make(map[string]*Package),
|
2013-06-13 12:43:35 -06:00
|
|
|
packages: make(map[*types.Package]*Package),
|
2013-09-23 16:18:35 -06:00
|
|
|
builtins: make(map[*types.Builtin]*Builtin),
|
2013-07-26 09:22:34 -06:00
|
|
|
boundMethodWrappers: make(map[*types.Func]*Function),
|
2013-06-14 13:50:37 -06:00
|
|
|
ifaceMethodWrappers: make(map[*types.Func]*Function),
|
2013-06-13 12:43:35 -06:00
|
|
|
mode: mode,
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create Values for built-in functions.
|
2013-07-24 21:02:54 -06:00
|
|
|
for _, name := range types.Universe.Names() {
|
2013-09-23 16:18:35 -06:00
|
|
|
if obj, ok := types.Universe.Lookup(name).(*types.Builtin); ok {
|
2013-07-01 13:24:50 -06:00
|
|
|
prog.builtins[obj] = &Builtin{obj}
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return prog
|
|
|
|
}
|
|
|
|
|
|
|
|
// memberFromObject populates package pkg with a member for the
|
|
|
|
// typechecker object obj.
|
|
|
|
//
|
|
|
|
// For objects from Go source code, syntax is the associated syntax
|
|
|
|
// tree (for funcs and vars only); it will be used during the build
|
|
|
|
// phase.
|
|
|
|
//
|
|
|
|
func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
|
|
|
|
name := obj.Name()
|
|
|
|
switch obj := obj.(type) {
|
|
|
|
case *types.TypeName:
|
go.tools/ssa: fix computation of set of types requiring method sets.
Motivation:
Previously, we assumed that the set of types for which a
complete method set (containing all synthesized wrapper
functions) is required at runtime was the set of types
used as operands to some *ssa.MakeInterface instruction.
In fact, this is an underapproximation because types can
be derived from other ones via reflection, and some of
these may need methods. The reflect.Type API allows *T to
be derived from T, and these may have different method
sets. Reflection also allows almost any subcomponent of a
type to be accessed (with one exception: given T, defined
'type T struct{S}', you can reach S but not struct{S}).
As a result, the pointer analysis was unable to generate
all necessary constraints before running the solver,
causing a crash when reflection derives types whose
methods are unavailable. (A similar problem would afflict
an ahead-of-time compiler based on ssa. The ssa/interp
interpreter was immune only because it does not require
all wrapper methods to be created before execution
begins.)
Description:
This change causes the SSA builder to record, for each
package, the set of all types with non-empty method sets that
are referenced within that package. This set is accessed via
Packages.TypesWithMethodSets(). Program.TypesWithMethodSets()
returns its union across all packages.
The set of references that matter are:
- types of operands to some MakeInterface instruction (as before)
- types of all exported package members
- all subcomponents of the above, recursively.
This is a conservative approximation to the set of types
whose methods may be called dynamically.
We define the owning package of a type as follows:
- the owner of a named type is the package in which it is defined;
- the owner of a pointer-to-named type is the owner of that named type;
- the owner of all other types is nil.
A package must include the method sets for all types that it
owns, and all subcomponents of that type that are not owned by
another package, recursively. Types with an owner appear in
exactly one package; types with no owner (such as struct{T})
may appear within multiple packages.
(A typical Go compiler would emit multiple copies of these
methods as weak symbols; a typical linker would eliminate
duplicates.)
Also:
- go/types/typemap: implement hash function for *Tuple.
- pointer: generate nodes/constraints for all of
ssa.Program.TypesWithMethodSets().
Add rtti.go regression test.
- Add API test of Package.TypesWithMethodSets().
- Set Function.Pkg to nil (again) for wrapper functions,
since these may be shared by many packages.
- Remove a redundant logging statement.
- Document that ssa CREATE phase is in fact sequential.
Fixes golang/go#6605
R=gri
CC=golang-dev
https://golang.org/cl/14920056
2013-10-23 15:07:52 -06:00
|
|
|
pkg.values[obj] = nil // for needMethods
|
2013-11-15 07:21:48 -07:00
|
|
|
pkg.Members[name] = &Type{
|
|
|
|
object: obj,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
2013-06-03 14:46:57 -06:00
|
|
|
|
|
|
|
case *types.Const:
|
2013-07-16 11:50:08 -06:00
|
|
|
c := &NamedConst{
|
2013-07-11 12:12:30 -06:00
|
|
|
object: obj,
|
2013-07-16 11:50:08 -06:00
|
|
|
Value: NewConst(obj.Val(), obj.Type()),
|
2013-11-15 07:21:48 -07:00
|
|
|
pkg: pkg,
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
go.tools/ssa: add debug information for all ast.Idents.
This CL adds three new functions to determine the SSA Value
for a given syntactic var, func or const object:
Program.{Const,Func,Var}Value.
Since constants and functions are immutable, the first
two only need a types.Object; but each distinct
reference to a var may return a distinct Value, so the third
requires an ast.Ident parameter too.
Debug information for local vars is encoded in the
instruction stream in the form of DebugRef instructions,
which are a no-op but relate their operand to a particular
ident in the AST. The beauty of this approach is that it
naturally stays consistent during optimisation passes
(e.g. lifting) without additional bookkeeping.
DebugRef instructions are only generated if the DebugMode
builder flag is set; I plan to make the policy more fine-
grained (per function).
DebugRef instructions are inserted for:
- expr(Ident) for rvalue idents
- address.store() for idents that update an lvalue
- address.address() for idents that take address of lvalue
(this new method replaces all uses of lval.(address).addr)
- expr() for all constant expressions
- local ValueSpecs with implicit zero initialization (no RHS)
(this case doesn't call store() or address())
To ensure we don't forget to emit debug info for uses of Idents,
we must use the lvalue mechanism consistently. (Previously,
many simple cases had effectively inlined these functions.)
Similarly setCallFunc no longer inlines expr(Ident).
Also:
- Program.Value() has been inlined & specialized.
- Program.Package() has moved nearer the new lookup functions.
- refactoring: funcSyntax has lost paramFields, resultFields;
gained funcType, which provides access to both.
- add package-level constants to Package.values map.
- opt: don't call localValueSpec for constants.
(The resulting code is always optimised away.)
There are a number of comments asking whether Literals
should have positions. Will address in a follow-up.
Added tests of all interesting cases.
R=gri
CC=golang-dev
https://golang.org/cl/11259044
2013-07-15 11:56:46 -06:00
|
|
|
pkg.values[obj] = c.Value
|
|
|
|
pkg.Members[name] = c
|
2013-06-03 14:46:57 -06:00
|
|
|
|
|
|
|
case *types.Var:
|
|
|
|
g := &Global{
|
2013-07-11 12:12:30 -06:00
|
|
|
Pkg: pkg,
|
|
|
|
name: name,
|
|
|
|
object: obj,
|
2013-07-16 10:23:55 -06:00
|
|
|
typ: types.NewPointer(obj.Type()), // address
|
2013-07-11 12:12:30 -06:00
|
|
|
pos: obj.Pos(),
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
pkg.values[obj] = g
|
|
|
|
pkg.Members[name] = g
|
|
|
|
|
|
|
|
case *types.Func:
|
|
|
|
fn := &Function{
|
|
|
|
name: name,
|
2013-07-11 12:12:30 -06:00
|
|
|
object: obj,
|
2013-07-29 12:24:09 -06:00
|
|
|
Signature: obj.Type().(*types.Signature),
|
2013-10-27 08:55:21 -06:00
|
|
|
syntax: syntax,
|
2013-06-03 14:46:57 -06:00
|
|
|
pos: obj.Pos(), // (iff syntax)
|
|
|
|
Pkg: pkg,
|
|
|
|
Prog: pkg.Prog,
|
|
|
|
}
|
2013-10-27 08:55:21 -06:00
|
|
|
if syntax == nil {
|
|
|
|
fn.Synthetic = "loaded from gc object file"
|
|
|
|
}
|
|
|
|
|
2013-07-29 12:24:09 -06:00
|
|
|
pkg.values[obj] = fn
|
|
|
|
if fn.Signature.Recv() == nil {
|
|
|
|
pkg.Members[name] = fn // package-level function
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
default: // (incl. *types.Package)
|
2013-07-26 09:22:34 -06:00
|
|
|
panic("unexpected Object type: " + obj.String())
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// membersFromDecl populates package pkg with members for each
|
|
|
|
// typechecker object (var, func, const or type) associated with the
|
|
|
|
// specified decl.
|
|
|
|
//
|
|
|
|
func membersFromDecl(pkg *Package, decl ast.Decl) {
|
|
|
|
switch decl := decl.(type) {
|
|
|
|
case *ast.GenDecl: // import, const, type or var
|
|
|
|
switch decl.Tok {
|
|
|
|
case token.CONST:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
for _, id := range spec.(*ast.ValueSpec).Names {
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.objectOf(id), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case token.VAR:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
for _, id := range spec.(*ast.ValueSpec).Names {
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.objectOf(id), spec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case token.TYPE:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
id := spec.(*ast.TypeSpec).Name
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.objectOf(id), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
id := decl.Name
|
|
|
|
if decl.Recv == nil && id.Name == "init" {
|
2013-10-14 12:08:23 -06:00
|
|
|
return // no object
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
if !isBlankIdent(id) {
|
|
|
|
memberFromObject(pkg, pkg.objectOf(id), decl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 14:59:06 -06:00
|
|
|
// CreatePackage constructs and returns an SSA Package from an
|
|
|
|
// error-free package described by info, and populates its Members
|
|
|
|
// mapping.
|
|
|
|
//
|
go.tools/ssa: implement correct control flow for recovered panic.
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixes golang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
|
|
|
// Repeated calls with the same info return the same Package.
|
2013-06-03 14:46:57 -06:00
|
|
|
//
|
|
|
|
// The real work of building SSA form for each function is not done
|
|
|
|
// until a subsequent call to Package.Build().
|
|
|
|
//
|
2013-07-18 14:59:06 -06:00
|
|
|
func (prog *Program) CreatePackage(info *importer.PackageInfo) *Package {
|
|
|
|
if info.Err != nil {
|
|
|
|
panic(fmt.Sprintf("package %s has errors: %s", info, info.Err))
|
|
|
|
}
|
|
|
|
if p := prog.packages[info.Pkg]; p != nil {
|
|
|
|
return p // already loaded
|
|
|
|
}
|
|
|
|
|
2013-06-03 14:46:57 -06:00
|
|
|
p := &Package{
|
|
|
|
Prog: prog,
|
|
|
|
Members: make(map[string]Member),
|
|
|
|
values: make(map[types.Object]Value),
|
2013-07-01 13:24:50 -06:00
|
|
|
Object: info.Pkg,
|
2013-06-03 14:46:57 -06:00
|
|
|
info: info, // transient (CREATE and BUILD phases)
|
|
|
|
}
|
|
|
|
|
2013-07-10 16:37:52 -06:00
|
|
|
// Add init() function.
|
|
|
|
p.init = &Function{
|
2013-06-03 14:46:57 -06:00
|
|
|
name: "init",
|
|
|
|
Signature: new(types.Signature),
|
2013-07-03 15:57:20 -06:00
|
|
|
Synthetic: "package initializer",
|
2013-06-03 14:46:57 -06:00
|
|
|
Pkg: p,
|
|
|
|
Prog: prog,
|
|
|
|
}
|
2013-07-10 16:37:52 -06:00
|
|
|
p.Members[p.init.name] = p.init
|
2013-06-03 14:46:57 -06:00
|
|
|
|
|
|
|
// CREATE phase.
|
2013-07-19 15:35:29 -06:00
|
|
|
// Allocate all package members: vars, funcs, consts and types.
|
2013-06-03 14:46:57 -06:00
|
|
|
if len(info.Files) > 0 {
|
|
|
|
// Go source package.
|
|
|
|
for _, file := range info.Files {
|
|
|
|
for _, decl := range file.Decls {
|
|
|
|
membersFromDecl(p, decl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// GC-compiled binary package.
|
|
|
|
// No code.
|
|
|
|
// No position information.
|
2013-07-01 13:24:50 -06:00
|
|
|
scope := p.Object.Scope()
|
2013-07-24 21:02:54 -06:00
|
|
|
for _, name := range scope.Names() {
|
|
|
|
obj := scope.Lookup(name)
|
2013-07-26 09:22:34 -06:00
|
|
|
memberFromObject(p, obj, nil)
|
2013-07-10 16:08:42 -06:00
|
|
|
if obj, ok := obj.(*types.TypeName); ok {
|
2013-07-26 09:22:34 -06:00
|
|
|
named := obj.Type().(*types.Named)
|
|
|
|
for i, n := 0, named.NumMethods(); i < n; i++ {
|
|
|
|
memberFromObject(p, named.Method(i), nil)
|
2013-07-10 16:08:42 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add initializer guard variable.
|
|
|
|
initguard := &Global{
|
|
|
|
Pkg: p,
|
|
|
|
name: "init$guard",
|
2013-07-16 10:23:55 -06:00
|
|
|
typ: types.NewPointer(tBool),
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
|
|
|
p.Members[initguard.Name()] = initguard
|
|
|
|
|
|
|
|
if prog.mode&LogPackages != 0 {
|
|
|
|
p.DumpTo(os.Stderr)
|
|
|
|
}
|
|
|
|
|
2013-09-06 16:13:57 -06:00
|
|
|
if info.Importable {
|
|
|
|
prog.imported[info.Pkg.Path()] = p
|
|
|
|
}
|
2013-07-01 13:24:50 -06:00
|
|
|
prog.packages[p.Object] = p
|
2013-07-11 12:12:30 -06:00
|
|
|
|
|
|
|
if prog.mode&SanityCheckFunctions != 0 {
|
|
|
|
sanityCheckPackage(p)
|
|
|
|
}
|
2013-07-18 14:59:06 -06:00
|
|
|
|
|
|
|
return p
|
2013-06-03 14:46:57 -06:00
|
|
|
}
|
2013-09-06 16:13:57 -06:00
|
|
|
|
|
|
|
// CreatePackages creates SSA Packages for all error-free packages
|
|
|
|
// loaded by the specified Importer.
|
|
|
|
//
|
|
|
|
// If all packages were error-free, it is safe to call
|
|
|
|
// prog.BuildAll(), and nil is returned. Otherwise an error is
|
|
|
|
// returned.
|
|
|
|
//
|
|
|
|
func (prog *Program) CreatePackages(imp *importer.Importer) error {
|
|
|
|
var errpkgs []string
|
|
|
|
for _, info := range imp.AllPackages() {
|
|
|
|
if info.Err != nil {
|
|
|
|
errpkgs = append(errpkgs, info.Pkg.Path())
|
|
|
|
} else {
|
|
|
|
prog.CreatePackage(info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errpkgs != nil {
|
|
|
|
return fmt.Errorf("couldn't create these SSA packages due to type errors: %s",
|
|
|
|
strings.Join(errpkgs, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllPackages returns a new slice containing all packages in the
|
|
|
|
// program prog in unspecified order.
|
|
|
|
//
|
|
|
|
func (prog *Program) AllPackages() []*Package {
|
|
|
|
pkgs := make([]*Package, 0, len(prog.packages))
|
|
|
|
for _, pkg := range prog.packages {
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
}
|
|
|
|
return pkgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportedPackage returns the importable SSA Package whose import
|
|
|
|
// path is path, or nil if no such SSA package has been created.
|
|
|
|
//
|
|
|
|
// Not all packages are importable. For example, no import
|
|
|
|
// declaration can resolve to the x_test package created by 'go test'
|
|
|
|
// or the ad-hoc main package created 'go build foo.go'.
|
|
|
|
//
|
|
|
|
func (prog *Program) ImportedPackage(path string) *Package {
|
|
|
|
return prog.imported[path]
|
|
|
|
}
|