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-05-17 14:25:48 -06:00
|
|
|
package ssa
|
|
|
|
|
|
|
|
// This package defines a high-level intermediate representation for
|
|
|
|
// Go programs using static single-assignment (SSA) form.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"code.google.com/p/go.tools/go/exact"
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-07-10 16:01:11 -06:00
|
|
|
"code.google.com/p/go.tools/go/types/typemap"
|
2013-05-31 14:14:13 -06:00
|
|
|
"code.google.com/p/go.tools/importer"
|
2013-05-17 14:25:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Program is a partial or complete Go program converted to SSA form.
|
|
|
|
//
|
|
|
|
type Program struct {
|
2013-09-06 16:13:57 -06:00
|
|
|
Fset *token.FileSet // position information for the files of this Program
|
|
|
|
imported map[string]*Package // all importable Packages, keyed by import path
|
|
|
|
packages map[*types.Package]*Package // all loaded Packages, keyed by object
|
|
|
|
mode BuilderMode // set of mode bits for SSA construction
|
2013-06-13 12:43:35 -06:00
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
methodsMu sync.Mutex // guards the following maps:
|
2013-07-30 12:28:14 -06:00
|
|
|
methodSets typemap.M // maps type to its concrete methodSet
|
2013-07-26 09:22:34 -06:00
|
|
|
boundMethodWrappers map[*types.Func]*Function // wrappers for curried x.Method closures
|
2013-06-14 13:50:37 -06:00
|
|
|
ifaceMethodWrappers map[*types.Func]*Function // wrappers for curried I.Method functions
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Package is a single analyzed Go package containing Members for
|
|
|
|
// all package-level functions, variables, constants and types it
|
|
|
|
// declares. These may be accessed directly via Members, or via the
|
|
|
|
// type-specific accessor methods Func, Type, Var and Const.
|
|
|
|
//
|
|
|
|
type Package struct {
|
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
|
|
|
Prog *Program // the owning program
|
|
|
|
Object *types.Package // the type checker's package object for this package
|
|
|
|
Members map[string]Member // all package members keyed by name
|
|
|
|
methodSets []types.Type // types whose method sets are included in this package
|
|
|
|
values map[types.Object]Value // package members (incl. types and methods), keyed by object
|
|
|
|
init *Function // Func("init"); the package's init function
|
|
|
|
debug bool // include full debug info in this package.
|
2013-06-03 12:15:19 -06:00
|
|
|
|
|
|
|
// The following fields are set transiently, then cleared
|
|
|
|
// after building.
|
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
|
|
|
started int32 // atomically tested and set at start of build phase
|
|
|
|
ninit int32 // number of init functions
|
|
|
|
info *importer.PackageInfo // package ASTs and type information
|
|
|
|
needRTTI typemap.M // types for which runtime type info is needed
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-16 11:50:08 -06:00
|
|
|
// A Member is a member of a Go package, implemented by *NamedConst,
|
2013-05-17 14:25:48 -06:00
|
|
|
// *Global, *Function, or *Type; they are created by package-level
|
|
|
|
// const, var, func and type declarations respectively.
|
|
|
|
//
|
|
|
|
type Member interface {
|
2013-11-15 07:21:48 -07:00
|
|
|
Name() string // declared name of the package member
|
|
|
|
String() string // package-qualified name of the package member
|
|
|
|
RelString(*types.Package) string // like String, but relative refs are unqualified
|
|
|
|
Object() types.Object // typechecker's object for this member, if any
|
|
|
|
Pos() token.Pos // position of member's declaration, if known
|
|
|
|
Type() types.Type // type of the package member
|
|
|
|
Token() token.Token // token.{VAR,FUNC,CONST,TYPE}
|
|
|
|
Package() *Package // returns the containing package. (TODO: rename Pkg)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-06-13 12:43:35 -06:00
|
|
|
// A Type is a Member of a Package representing a package-level named type.
|
|
|
|
//
|
|
|
|
// Type() returns a *types.Named.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type Type struct {
|
2013-07-11 12:12:30 -06:00
|
|
|
object *types.TypeName
|
2013-11-15 07:21:48 -07:00
|
|
|
pkg *Package
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-16 11:50:08 -06:00
|
|
|
// A NamedConst is a Member of Package representing a package-level
|
|
|
|
// named constant value.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the position of the declaring ast.ValueSpec.Names[*]
|
|
|
|
// identifier.
|
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
// NB: a NamedConst is not a Value; it contains a constant Value, which
|
2013-05-17 15:02:47 -06:00
|
|
|
// it augments with the name and position of its 'const' declaration.
|
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
type NamedConst struct {
|
2013-07-11 12:12:30 -06:00
|
|
|
object *types.Const
|
2013-07-16 11:50:08 -06:00
|
|
|
Value *Const
|
2013-07-11 12:12:30 -06:00
|
|
|
pos token.Pos
|
2013-11-15 07:21:48 -07:00
|
|
|
pkg *Package
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2014-01-13 14:45:46 -07:00
|
|
|
// A Value is an SSA value that can be referenced by an instruction.
|
2013-05-17 14:25:48 -06:00
|
|
|
type Value interface {
|
|
|
|
// Name returns the name of this value, and determines how
|
|
|
|
// this Value appears when used as an operand of an
|
|
|
|
// Instruction.
|
|
|
|
//
|
|
|
|
// This is the same as the source name for Parameters,
|
2013-10-29 09:07:09 -06:00
|
|
|
// Builtins, Functions, Captures, Globals.
|
2013-07-16 11:50:08 -06:00
|
|
|
// For constants, it is a representation of the constant's value
|
2013-05-17 14:25:48 -06:00
|
|
|
// and type. For all other Values this is the name of the
|
|
|
|
// virtual register defined by the instruction.
|
|
|
|
//
|
|
|
|
// The name of an SSA Value is not semantically significant,
|
|
|
|
// and may not even be unique within a function.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// If this value is an Instruction, String returns its
|
|
|
|
// disassembled form; otherwise it returns unspecified
|
|
|
|
// human-readable information about the Value, such as its
|
|
|
|
// kind, name and type.
|
|
|
|
String() string
|
|
|
|
|
|
|
|
// Type returns the type of this value. Many instructions
|
2013-10-29 09:07:09 -06:00
|
|
|
// (e.g. IndexAddr) change their behaviour depending on the
|
2013-05-17 14:25:48 -06:00
|
|
|
// types of their operands.
|
|
|
|
Type() types.Type
|
|
|
|
|
|
|
|
// Referrers returns the list of instructions that have this
|
|
|
|
// value as one of their operands; it may contain duplicates
|
|
|
|
// if an instruction has a repeated operand.
|
|
|
|
//
|
|
|
|
// Referrers actually returns a pointer through which the
|
|
|
|
// caller may perform mutations to the object's state.
|
|
|
|
//
|
|
|
|
// Referrers is currently only defined for the function-local
|
2013-11-07 08:08:51 -07:00
|
|
|
// values Capture, Parameter, Functions (iff anonymous) and
|
|
|
|
// all value-defining instructions.
|
|
|
|
// It returns nil for named Functions, Builtin, Const and Global.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Instruction.Operands contains the inverse of this relation.
|
|
|
|
Referrers() *[]Instruction
|
|
|
|
|
2013-07-31 11:13:05 -06:00
|
|
|
// Pos returns the location of the AST token most closely
|
|
|
|
// associated with the operation that gave rise to this value,
|
|
|
|
// or token.NoPos if it was not explicit in the source.
|
2013-05-30 07:59:17 -06:00
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// For each ast.Node type, a particular token is designated as
|
|
|
|
// the closest location for the expression, e.g. the Lparen
|
|
|
|
// for an *ast.CallExpr. This permits a compact but
|
|
|
|
// approximate mapping from Values to source positions for use
|
|
|
|
// in diagnostic messages, for example.
|
|
|
|
//
|
|
|
|
// (Do not use this position to determine which Value
|
|
|
|
// corresponds to an ast.Expr; use Function.ValueForExpr
|
|
|
|
// instead. NB: it requires that the function was built with
|
|
|
|
// debug information.)
|
2013-05-30 07:59:17 -06:00
|
|
|
//
|
|
|
|
Pos() token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// An Instruction is an SSA instruction that computes a new Value or
|
|
|
|
// has some effect.
|
|
|
|
//
|
|
|
|
// An Instruction that defines a value (e.g. BinOp) also implements
|
|
|
|
// the Value interface; an Instruction that only has an effect (e.g. Store)
|
|
|
|
// does not.
|
|
|
|
//
|
|
|
|
type Instruction interface {
|
|
|
|
// String returns the disassembled form of this value. e.g.
|
|
|
|
//
|
|
|
|
// Examples of Instructions that define a Value:
|
|
|
|
// e.g. "x + y" (BinOp)
|
|
|
|
// "len([])" (Call)
|
|
|
|
// Note that the name of the Value is not printed.
|
|
|
|
//
|
|
|
|
// Examples of Instructions that do define (are) Values:
|
2013-10-08 10:31:39 -06:00
|
|
|
// e.g. "return x" (Return)
|
2013-05-17 14:25:48 -06:00
|
|
|
// "*y = x" (Store)
|
|
|
|
//
|
|
|
|
// (This separation is useful for some analyses which
|
|
|
|
// distinguish the operation from the value it
|
|
|
|
// defines. e.g. 'y = local int' is both an allocation of
|
|
|
|
// memory 'local int' and a definition of a pointer y.)
|
|
|
|
String() string
|
|
|
|
|
2013-06-13 12:43:35 -06:00
|
|
|
// Parent returns the function to which this instruction
|
|
|
|
// belongs.
|
|
|
|
Parent() *Function
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
// Block returns the basic block to which this instruction
|
|
|
|
// belongs.
|
|
|
|
Block() *BasicBlock
|
|
|
|
|
2013-11-07 08:08:51 -07:00
|
|
|
// setBlock sets the basic block to which this instruction belongs.
|
|
|
|
setBlock(*BasicBlock)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
// Operands returns the operands of this instruction: the
|
|
|
|
// set of Values it references.
|
|
|
|
//
|
|
|
|
// Specifically, it appends their addresses to rands, a
|
|
|
|
// user-provided slice, and returns the resulting slice,
|
|
|
|
// permitting avoidance of memory allocation.
|
|
|
|
//
|
|
|
|
// The operands are appended in undefined order; the addresses
|
|
|
|
// are always non-nil but may point to a nil Value. Clients
|
|
|
|
// may store through the pointers, e.g. to effect a value
|
|
|
|
// renaming.
|
|
|
|
//
|
|
|
|
// Value.Referrers is a subset of the inverse of this
|
|
|
|
// relation. (Referrers are not tracked for all types of
|
|
|
|
// Values.)
|
|
|
|
Operands(rands []*Value) []*Value
|
|
|
|
|
2013-07-31 11:13:05 -06:00
|
|
|
// Pos returns the location of the AST token most closely
|
|
|
|
// associated with the operation that gave rise to this
|
|
|
|
// instruction, or token.NoPos if it was not explicit in the
|
|
|
|
// source.
|
|
|
|
//
|
|
|
|
// For each ast.Node type, a particular token is designated as
|
|
|
|
// the closest location for the expression, e.g. the Go token
|
|
|
|
// for an *ast.GoStmt. This permits a compact but approximate
|
|
|
|
// mapping from Instructions to source positions for use in
|
|
|
|
// diagnostic messages, for example.
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// (Do not use this position to determine which Instruction
|
|
|
|
// corresponds to an ast.Expr; see the notes for Value.Pos.
|
|
|
|
// This position may be used to determine which non-Value
|
|
|
|
// Instruction corresponds to some ast.Stmts, but not all: If
|
|
|
|
// and Jump instructions have no Pos(), for example.)
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
|
|
|
Pos() token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Function represents the parameters, results and code of a function
|
|
|
|
// or method.
|
|
|
|
//
|
|
|
|
// If Blocks is nil, this indicates an external function for which no
|
2014-01-13 14:45:46 -07:00
|
|
|
// Go source code is available. In this case, FreeVars and Locals
|
2013-05-17 14:25:48 -06:00
|
|
|
// will be nil too. Clients performing whole-program analysis must
|
|
|
|
// handle external functions specially.
|
|
|
|
//
|
|
|
|
// Functions are immutable values; they do not have addresses.
|
|
|
|
//
|
2013-12-05 07:50:18 -07:00
|
|
|
// Blocks contains the function's control-flow graph (CFG).
|
2013-05-17 14:25:48 -06:00
|
|
|
// Blocks[0] is the function entry point; block order is not otherwise
|
|
|
|
// semantically significant, though it may affect the readability of
|
|
|
|
// the disassembly.
|
2013-12-05 07:50:18 -07:00
|
|
|
// To iterate over the blocks in dominance order, use DomPreorder().
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
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
|
|
|
// Recover is an optional second entry point to which control resumes
|
|
|
|
// after a recovered panic. The Recover block may contain only a load
|
|
|
|
// of the function's named return parameters followed by a return of
|
|
|
|
// the loaded values.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// A nested function that refers to one or more lexically enclosing
|
|
|
|
// local variables ("free variables") has Capture parameters. Such
|
|
|
|
// functions cannot be called directly but require a value created by
|
|
|
|
// MakeClosure which, via its Bindings, supplies values for these
|
2013-05-22 15:56:18 -06:00
|
|
|
// parameters.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// If the function is a method (Signature.Recv() != nil) then the first
|
2013-05-17 14:25:48 -06:00
|
|
|
// element of Params is the receiver parameter.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the declaring ast.FuncLit.Type.Func or the position
|
|
|
|
// of the ast.FuncDecl.Name, if the function was explicit in the
|
2013-07-03 15:57:20 -06:00
|
|
|
// source. Synthetic wrappers, for which Synthetic != "", may share
|
|
|
|
// the same position as the function they wrap.
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Type() returns the function's Signature.
|
|
|
|
//
|
|
|
|
type Function struct {
|
2013-05-30 07:59:17 -06:00
|
|
|
name string
|
2013-07-26 23:27:48 -06:00
|
|
|
object types.Object // a declared *types.Func; nil for init, wrappers, etc.
|
|
|
|
method *types.Selection // info about provenance of synthetic methods [currently unused]
|
2013-05-17 14:25:48 -06:00
|
|
|
Signature *types.Signature
|
2013-05-17 15:02:47 -06:00
|
|
|
pos token.Pos
|
2013-05-30 07:59:17 -06:00
|
|
|
|
2013-07-03 15:57:20 -06:00
|
|
|
Synthetic string // provenance of synthetic function; "" for true source functions
|
2013-10-27 08:55:21 -06:00
|
|
|
syntax ast.Node // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode
|
2013-05-17 14:25:48 -06:00
|
|
|
Enclosing *Function // enclosing function if anon; nil if global
|
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 *Package // enclosing package; nil for shared funcs (wrappers and error.Error)
|
2013-05-17 14:25:48 -06:00
|
|
|
Prog *Program // enclosing program
|
|
|
|
Params []*Parameter // function parameters; for methods, includes receiver
|
|
|
|
FreeVars []*Capture // free variables whose values must be supplied by closure
|
|
|
|
Locals []*Alloc
|
|
|
|
Blocks []*BasicBlock // basic blocks of the function; nil => external
|
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
|
|
|
Recover *BasicBlock // optional; control transfers here after recovered panic
|
2013-05-17 14:25:48 -06:00
|
|
|
AnonFuncs []*Function // anonymous functions directly beneath this one
|
2013-11-07 08:08:51 -07:00
|
|
|
referrers []Instruction // referring instructions (iff Enclosing != nil)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
// The following fields are set transiently during building,
|
|
|
|
// then cleared.
|
|
|
|
currentBlock *BasicBlock // where to emit code
|
|
|
|
objects map[types.Object]Value // addresses of local variables
|
|
|
|
namedResults []*Alloc // tuple of named results
|
|
|
|
targets *targets // linked stack of branch targets
|
|
|
|
lblocks map[*ast.Object]*lblock // labelled blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
// An SSA basic block.
|
|
|
|
//
|
|
|
|
// The final element of Instrs is always an explicit transfer of
|
2013-10-08 10:31:39 -06:00
|
|
|
// control (If, Jump, Return or Panic).
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// A block may contain no Instructions only if it is unreachable,
|
|
|
|
// i.e. Preds is nil. Empty blocks are typically pruned.
|
|
|
|
//
|
|
|
|
// BasicBlocks and their Preds/Succs relation form a (possibly cyclic)
|
2013-12-05 07:50:18 -07:00
|
|
|
// graph independent of the SSA Value graph: the control-flow graph or
|
|
|
|
// CFG. It is illegal for multiple edges to exist between the same
|
|
|
|
// pair of blocks.
|
|
|
|
//
|
|
|
|
// Each BasicBlock is also a node in the dominator tree of the CFG.
|
|
|
|
// The tree may be navigated using Idom()/Dominees() and queried using
|
|
|
|
// Dominates().
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// The order of Preds and Succs are significant (to Phi and If
|
|
|
|
// instructions, respectively).
|
|
|
|
//
|
|
|
|
type BasicBlock struct {
|
|
|
|
Index int // index of this block within Func.Blocks
|
|
|
|
Comment string // optional label; no semantic significance
|
2013-06-13 12:43:35 -06:00
|
|
|
parent *Function // parent function
|
2013-05-17 14:25:48 -06:00
|
|
|
Instrs []Instruction // instructions in order
|
|
|
|
Preds, Succs []*BasicBlock // predecessors and successors
|
|
|
|
succs2 [2]*BasicBlock // initial space for Succs.
|
2013-12-05 07:50:18 -07:00
|
|
|
dom domInfo // dominator tree info
|
2013-05-17 14:25:48 -06:00
|
|
|
gaps int // number of nil Instrs (transient).
|
|
|
|
rundefers int // number of rundefers (transient)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pure values ----------------------------------------
|
|
|
|
|
2013-05-22 15:56:18 -06:00
|
|
|
// A Capture represents a free variable of the function to which it
|
|
|
|
// belongs.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-22 15:56:18 -06:00
|
|
|
// Captures are used to implement anonymous functions, whose free
|
|
|
|
// variables are lexically captured in a closure formed by
|
|
|
|
// MakeClosure. The referent of such a capture is an Alloc or another
|
|
|
|
// Capture and is considered a potentially escaping heap address, with
|
|
|
|
// pointer type.
|
|
|
|
//
|
|
|
|
// Captures are also used to implement bound method closures. Such a
|
|
|
|
// capture represents the receiver value and may be of any type that
|
|
|
|
// has concrete methods.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
// Pos() returns the position of the value that was captured, which
|
|
|
|
// belongs to an enclosing function.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
type Capture struct {
|
2013-06-13 12:43:35 -06:00
|
|
|
name string
|
|
|
|
typ types.Type
|
|
|
|
pos token.Pos
|
|
|
|
parent *Function
|
2013-05-17 14:25:48 -06:00
|
|
|
referrers []Instruction
|
2013-05-22 15:56:18 -06:00
|
|
|
|
|
|
|
// Transiently needed during building.
|
|
|
|
outer Value // the Value captured from the enclosing context.
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Parameter represents an input parameter of a function.
|
|
|
|
//
|
|
|
|
type Parameter struct {
|
2013-06-13 12:43:35 -06:00
|
|
|
name string
|
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
|
|
|
object types.Object // a *types.Var; nil for non-source locals
|
2013-06-13 12:43:35 -06:00
|
|
|
typ types.Type
|
|
|
|
pos token.Pos
|
|
|
|
parent *Function
|
2013-05-17 14:25:48 -06:00
|
|
|
referrers []Instruction
|
|
|
|
}
|
|
|
|
|
2013-07-16 11:50:08 -06:00
|
|
|
// A Const represents the value of a constant expression.
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
2014-01-13 14:45:46 -07:00
|
|
|
// The underlying type of a constant may be any boolean, numeric, or
|
|
|
|
// string type. In addition, a Const may represent the nil value of
|
|
|
|
// any reference type: interface, map, channel, pointer, slice, or
|
|
|
|
// function---but not "untyped nil".
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
// All source-level constant expressions are represented by a Const
|
2013-05-17 14:25:48 -06:00
|
|
|
// of equal type and value.
|
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
// Value holds the exact value of the constant, independent of its
|
2013-05-17 15:02:47 -06:00
|
|
|
// Type(), using the same representation as package go/exact uses for
|
2013-10-09 15:17:25 -06:00
|
|
|
// constants, or nil for a typed nil value.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-15 14:10:08 -06:00
|
|
|
// Pos() returns token.NoPos.
|
2013-05-30 07:59:17 -06:00
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// 42:int
|
|
|
|
// "hello":untyped string
|
|
|
|
// 3+4i:MyComplex
|
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
type Const struct {
|
2013-05-30 07:59:17 -06:00
|
|
|
typ types.Type
|
2013-05-17 14:25:48 -06:00
|
|
|
Value exact.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Global is a named Value holding the address of a package-level
|
|
|
|
// variable.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the position of the ast.ValueSpec.Names[*]
|
|
|
|
// identifier.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
type Global struct {
|
2013-07-11 12:12:30 -06:00
|
|
|
name string
|
|
|
|
object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
|
|
|
|
typ types.Type
|
|
|
|
pos token.Pos
|
2013-05-30 07:59:17 -06:00
|
|
|
|
|
|
|
Pkg *Package
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 11:31:05 -07:00
|
|
|
// A Builtin represents a specific use of a built-in function, e.g. len.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Builtins are immutable values. Builtins do not have addresses.
|
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
|
|
|
// Builtins can only appear in CallCommon.Func.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-09-23 16:18:35 -06:00
|
|
|
// Object() returns a *types.Builtin.
|
|
|
|
//
|
2014-01-07 11:31:05 -07:00
|
|
|
// Type() returns a *types.Signature representing the effective
|
|
|
|
// signature of the built-in for this call.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type Builtin struct {
|
2013-09-23 16:18:35 -06:00
|
|
|
object *types.Builtin // canonical types.Universe object for this built-in
|
2014-01-07 11:31:05 -07:00
|
|
|
sig *types.Signature
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value-defining instructions ----------------------------------------
|
|
|
|
|
|
|
|
// The Alloc instruction reserves space for a value of the given type,
|
|
|
|
// zero-initializes it, and yields its address.
|
|
|
|
//
|
|
|
|
// Alloc values are always addresses, and have pointer types, so the
|
|
|
|
// type of the allocated space is actually indirect(Type()).
|
|
|
|
//
|
|
|
|
// If Heap is false, Alloc allocates space in the function's
|
|
|
|
// activation record (frame); we refer to an Alloc(Heap=false) as a
|
|
|
|
// "local" alloc. Each local Alloc returns the same address each time
|
|
|
|
// it is executed within the same activation; the space is
|
|
|
|
// re-initialized to zero.
|
|
|
|
//
|
|
|
|
// If Heap is true, Alloc allocates space in the heap, and returns; we
|
|
|
|
// refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc
|
|
|
|
// returns a different address each time it is executed.
|
|
|
|
//
|
|
|
|
// When Alloc is applied to a channel, map or slice type, it returns
|
|
|
|
// the address of an uninitialized (nil) reference of that kind; store
|
|
|
|
// the result of MakeSlice, MakeMap or MakeChan in that location to
|
|
|
|
// instantiate these types.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
|
|
|
|
// or the ast.CallExpr.Lparen for a call to new() or for a call that
|
|
|
|
// allocates a varargs slice.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t0 = local int
|
|
|
|
// t1 = new int
|
|
|
|
//
|
|
|
|
type Alloc struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-08-01 12:06:10 -06:00
|
|
|
Comment string
|
|
|
|
Heap bool
|
|
|
|
index int // dense numbering; for lifting
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Phi instruction represents an SSA φ-node, which combines values
|
|
|
|
// that differ across incoming control-flow edges and yields a new
|
|
|
|
// value. Within a block, all φ-nodes must appear before all non-φ
|
|
|
|
// nodes.
|
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
// Pos() returns the position of the && or || for short-circuit
|
|
|
|
// control-flow joins, or that of the *Alloc for φ-nodes inserted
|
|
|
|
// during SSA renaming.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t2 = phi [0.start: t0, 1.if.then: t1, ...]
|
|
|
|
//
|
|
|
|
type Phi struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Comment string // a hint as to its purpose
|
|
|
|
Edges []Value // Edges[i] is value for Block().Preds[i]
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Call instruction represents a function or method call.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// The Call instruction yields the function result, if there is
|
|
|
|
// exactly one, or a tuple (empty or len>1) whose components are
|
|
|
|
// accessed via Extract.
|
|
|
|
//
|
|
|
|
// See CallCommon for generic function call documentation.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t2 = println(t0, t1)
|
|
|
|
// t4 = t3()
|
|
|
|
// t7 = invoke t5.Println(...t6)
|
|
|
|
//
|
|
|
|
type Call struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Call CallCommon
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The BinOp instruction yields the result of binary operation X Op Y.
|
|
|
|
//
|
|
|
|
// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t1 = t0 + 1:int
|
|
|
|
//
|
|
|
|
type BinOp struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
// One of:
|
|
|
|
// ADD SUB MUL QUO REM + - * / %
|
|
|
|
// AND OR XOR SHL SHR AND_NOT & | ^ << >> &~
|
|
|
|
// EQL LSS GTR NEQ LEQ GEQ == != < <= < >=
|
|
|
|
Op token.Token
|
|
|
|
X, Y Value
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The UnOp instruction yields the result of Op X.
|
2013-05-17 14:25:48 -06:00
|
|
|
// ARROW is channel receive.
|
|
|
|
// MUL is pointer indirection (load).
|
|
|
|
// XOR is bitwise complement.
|
|
|
|
// SUB is negation.
|
|
|
|
//
|
|
|
|
// If CommaOk and Op=ARROW, the result is a 2-tuple of the value above
|
|
|
|
// and a boolean indicating the success of the receive. The
|
|
|
|
// components of the tuple are accessed using Extract.
|
|
|
|
//
|
2013-08-27 09:18:31 -06:00
|
|
|
// Pos() returns the ast.UnaryExpr.OpPos or ast.RangeStmt.TokPos (for
|
|
|
|
// ranging over a channel), if explicit in the source.
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t0 = *x
|
|
|
|
// t2 = <-t1,ok
|
|
|
|
//
|
|
|
|
type UnOp struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^
|
|
|
|
X Value
|
|
|
|
CommaOk bool
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The ChangeType instruction applies to X a value-preserving type
|
|
|
|
// change to Type().
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Type changes are permitted:
|
|
|
|
// - between a named type and its underlying type.
|
|
|
|
// - between two named types of the same underlying type.
|
|
|
|
// - between (possibly named) pointers to identical base types.
|
|
|
|
// - between f(T) functions and (T) func f() methods.
|
|
|
|
// - from a bidirectional channel to a read- or write-channel,
|
|
|
|
// optionally adding/removing a name.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// This operation cannot fail dynamically.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
|
|
|
|
// from an explicit conversion in the source.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = changetype *int <- IntPtr (t0)
|
|
|
|
//
|
|
|
|
type ChangeType struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 15:02:47 -06:00
|
|
|
X Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Convert instruction yields the conversion of value X to type
|
2013-06-24 12:15:13 -06:00
|
|
|
// Type(). One or both of those types is basic (but possibly named).
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
|
|
|
// A conversion may change the value and representation of its operand.
|
|
|
|
// Conversions are permitted:
|
|
|
|
// - between real numeric types.
|
|
|
|
// - between complex numeric types.
|
|
|
|
// - between string and []byte or []rune.
|
2013-06-24 12:15:13 -06:00
|
|
|
// - between pointers and unsafe.Pointer.
|
|
|
|
// - between unsafe.Pointer and uintptr.
|
2013-05-17 15:02:47 -06:00
|
|
|
// - from (Unicode) integer to (UTF-8) string.
|
|
|
|
// A conversion may imply a type name change also.
|
|
|
|
//
|
|
|
|
// This operation cannot fail dynamically.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Conversions of untyped string/number/bool constants to a specific
|
|
|
|
// representation are eliminated during SSA construction.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
|
|
|
|
// from an explicit conversion in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
2013-05-17 15:02:47 -06:00
|
|
|
// t1 = convert []byte <- string (t0)
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
type Convert struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeInterface constructs a value of one interface type from a
|
|
|
|
// value of another interface type known to be assignable to it.
|
2013-07-26 19:49:27 -06:00
|
|
|
// This operation cannot fail.
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
2013-06-13 12:43:35 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
|
|
|
|
// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
|
|
|
|
// instruction arose from an explicit e.(T) operation; or token.NoPos
|
|
|
|
// otherwise.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = change interface interface{} <- I (t0)
|
|
|
|
//
|
|
|
|
type ChangeInterface struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeInterface constructs an instance of an interface type from a
|
2013-06-13 12:43:35 -06:00
|
|
|
// value of a concrete type.
|
|
|
|
//
|
2013-07-30 12:28:14 -06:00
|
|
|
// Use X.Type().MethodSet() to find the method-set of X, and
|
2013-07-30 14:36:58 -06:00
|
|
|
// Program.Method(m) to find the implementation of a method.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// To construct the zero value of an interface type T, use:
|
2013-07-16 11:50:08 -06:00
|
|
|
// NewConst(exact.MakeNil(), T, pos)
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
|
|
|
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
|
|
|
|
// from an explicit conversion in the source.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
2013-06-13 12:43:35 -06:00
|
|
|
// t1 = make interface{} <- int (42:int)
|
|
|
|
// t2 = make Stringer <- t0
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type MakeInterface struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-06-13 12:43:35 -06:00
|
|
|
X Value
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-22 15:56:18 -06:00
|
|
|
// The MakeClosure instruction yields a closure value whose code is
|
|
|
|
// Fn and whose free variables' values are supplied by Bindings.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Type() returns a (possibly named) *types.Signature.
|
|
|
|
//
|
2013-05-22 15:56:18 -06:00
|
|
|
// Pos() returns the ast.FuncLit.Type.Func for a function literal
|
|
|
|
// closure or the ast.SelectorExpr.Sel for a bound method closure.
|
2013-05-17 15:02:47 -06:00
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t0 = make closure anon@1.2 [x y z]
|
2013-05-22 15:56:18 -06:00
|
|
|
// t1 = make closure bound$(main.I).add [i]
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type MakeClosure struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Fn Value // always a *Function
|
|
|
|
Bindings []Value // values for each free variable in Fn.FreeVars
|
|
|
|
}
|
|
|
|
|
|
|
|
// The MakeMap instruction creates a new hash-table-based map object
|
|
|
|
// and yields a value of kind map.
|
|
|
|
//
|
|
|
|
// Type() returns a (possibly named) *types.Map.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
|
|
|
|
// the ast.CompositeLit.Lbrack if created by a literal.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = make map[string]int t0
|
2013-06-13 12:43:35 -06:00
|
|
|
// t1 = make StringIntMap t0
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type MakeMap struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Reserve Value // initial space reservation; nil => default
|
|
|
|
}
|
|
|
|
|
|
|
|
// The MakeChan instruction creates a new channel object and yields a
|
|
|
|
// value of kind chan.
|
|
|
|
//
|
|
|
|
// Type() returns a (possibly named) *types.Chan.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen for the make(chan) that
|
|
|
|
// created it.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t0 = make chan int 0
|
2013-06-13 12:43:35 -06:00
|
|
|
// t0 = make IntChan 0
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type MakeChan struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Size Value // int; size of buffer; zero => synchronous.
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The MakeSlice instruction yields a slice of length Len backed by a
|
|
|
|
// newly allocated array of length Cap.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Both Len and Cap must be non-nil Values of integer type.
|
|
|
|
//
|
|
|
|
// (Alloc(types.Array) followed by Slice will not suffice because
|
|
|
|
// Alloc can only create arrays of statically known length.)
|
|
|
|
//
|
|
|
|
// Type() returns a (possibly named) *types.Slice.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen for the make([]T) that
|
|
|
|
// created it.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
2013-06-13 12:43:35 -06:00
|
|
|
// t1 = make []string 1:int t0
|
|
|
|
// t1 = make StringSlice 1:int t0
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type MakeSlice struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Len Value
|
|
|
|
Cap Value
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Slice instruction yields a slice of an existing string, slice
|
|
|
|
// or *array X between optional integer bounds Low and High.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-08-19 13:38:30 -06:00
|
|
|
// Dynamically, this instruction panics if X evaluates to a nil *array
|
|
|
|
// pointer.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Type() returns string if the type of X was string, otherwise a
|
|
|
|
// *types.Slice with the same element type as X.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
|
|
|
|
// operation, the ast.CompositeLit.Lbrace if created by a literal, or
|
|
|
|
// NoPos if not explicit in the source (e.g. a variadic argument slice).
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = slice t0[1:]
|
|
|
|
//
|
|
|
|
type Slice struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // slice, string, or *array
|
|
|
|
Low, High Value // either may be nil
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The FieldAddr instruction yields the address of Field of *struct X.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// The field is identified by its index within the field list of the
|
|
|
|
// struct type of X.
|
|
|
|
//
|
2013-08-19 13:38:30 -06:00
|
|
|
// Dynamically, this instruction panics if X evaluates to a nil
|
|
|
|
// pointer.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Type() returns a (possibly named) *types.Pointer.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the position of the ast.SelectorExpr.Sel for the
|
|
|
|
// field, if explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = &t0.name [#1]
|
|
|
|
//
|
|
|
|
type FieldAddr struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // *struct
|
2013-06-24 12:15:13 -06:00
|
|
|
Field int // index into X.Type().Deref().(*types.Struct).Fields
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Field instruction yields the Field of struct X.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// The field is identified by its index within the field list of the
|
|
|
|
// struct type of X; by using numeric indices we avoid ambiguity of
|
|
|
|
// package-local identifiers and permit compact representations.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the position of the ast.SelectorExpr.Sel for the
|
|
|
|
// field, if explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = t0.name [#1]
|
|
|
|
//
|
|
|
|
type Field struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // struct
|
|
|
|
Field int // index into X.Type().(*types.Struct).Fields
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The IndexAddr instruction yields the address of the element at
|
|
|
|
// index Index of collection X. Index is an integer expression.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// The elements of maps and strings are not addressable; use Lookup or
|
|
|
|
// MapUpdate instead.
|
|
|
|
//
|
2013-08-19 13:38:30 -06:00
|
|
|
// Dynamically, this instruction panics if X evaluates to a nil *array
|
|
|
|
// pointer.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Type() returns a (possibly named) *types.Pointer.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
|
|
|
|
// explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t2 = &t0[t1]
|
|
|
|
//
|
|
|
|
type IndexAddr struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // slice or *array,
|
|
|
|
Index Value // numeric index
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Index instruction yields element Index of array X.
|
|
|
|
//
|
|
|
|
// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
|
|
|
|
// explicit in the source.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t2 = t0[t1]
|
|
|
|
//
|
|
|
|
type Index struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // array
|
|
|
|
Index Value // integer index
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Lookup instruction yields element Index of collection X, a map
|
|
|
|
// or string. Index is an integer expression if X is a string or the
|
|
|
|
// appropriate key type if X is a map.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// If CommaOk, the result is a 2-tuple of the value above and a
|
|
|
|
// boolean indicating the result of a map membership test for the key.
|
|
|
|
// The components of the tuple are accessed using Extract.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t2 = t0[t1]
|
|
|
|
// t5 = t3[t4],ok
|
|
|
|
//
|
|
|
|
type Lookup struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // string or map
|
|
|
|
Index Value // numeric or key-typed index
|
|
|
|
CommaOk bool // return a value,ok pair
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectState is a helper for Select.
|
|
|
|
// It represents one goal state and its corresponding communication.
|
|
|
|
//
|
|
|
|
type SelectState struct {
|
2013-12-17 16:45:01 -07:00
|
|
|
Dir types.ChanDir // direction of case (SendOnly or RecvOnly)
|
|
|
|
Chan Value // channel to use (for send or receive)
|
|
|
|
Send Value // value to send (for send)
|
|
|
|
Pos token.Pos // position of token.ARROW
|
|
|
|
DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 11:31:05 -07:00
|
|
|
// The Select instruction tests whether (or blocks until) one
|
2013-05-17 15:02:47 -06:00
|
|
|
// of the specified sent or received states is entered.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-06-24 12:15:13 -06:00
|
|
|
// Let n be the number of States for which Dir==RECV and T_i (0<=i<n)
|
|
|
|
// be the element type of each such state's Chan.
|
|
|
|
// Select returns an n+2-tuple
|
|
|
|
// (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)
|
|
|
|
// The tuple's components, described below, must be accessed via the
|
|
|
|
// Extract instruction.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// If Blocking, select waits until exactly one state holds, i.e. a
|
|
|
|
// channel becomes ready for the designated operation of sending or
|
|
|
|
// receiving; select chooses one among the ready states
|
|
|
|
// pseudorandomly, performs the send or receive operation, and sets
|
|
|
|
// 'index' to the index of the chosen channel.
|
|
|
|
//
|
|
|
|
// If !Blocking, select doesn't block if no states hold; instead it
|
|
|
|
// returns immediately with index equal to -1.
|
|
|
|
//
|
2013-06-24 12:15:13 -06:00
|
|
|
// If the chosen channel was used for a receive, the r_i component is
|
|
|
|
// set to the received value, where i is the index of that state among
|
|
|
|
// all n receive states; otherwise r_i has the zero value of type T_i.
|
|
|
|
// Note that the the receive index i is not the same as the state
|
|
|
|
// index index.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-06-24 12:15:13 -06:00
|
|
|
// The second component of the triple, recvOk, is a boolean whose value
|
2013-05-17 14:25:48 -06:00
|
|
|
// is true iff the selected operation was a receive and the receive
|
|
|
|
// successfully yielded a value.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.SelectStmt.Select.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
2014-01-07 11:31:05 -07:00
|
|
|
// t3 = select nonblocking [<-t0, t1<-t2]
|
2013-05-17 14:25:48 -06:00
|
|
|
// t4 = select blocking []
|
|
|
|
//
|
|
|
|
type Select struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-07-31 11:13:05 -06:00
|
|
|
States []*SelectState
|
2013-05-17 14:25:48 -06:00
|
|
|
Blocking bool
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Range instruction yields an iterator over the domain and range
|
|
|
|
// of X, which must be a string or map.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Elements are accessed via Next.
|
|
|
|
//
|
2013-06-24 12:15:13 -06:00
|
|
|
// Type() returns an opaque and degenerate "rangeIter" type.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.RangeStmt.For.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t0 = range "hello":string
|
|
|
|
//
|
|
|
|
type Range struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value // string or map
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Next instruction reads and advances the (map or string)
|
|
|
|
// iterator Iter and returns a 3-tuple value (ok, k, v). If the
|
|
|
|
// iterator is not exhausted, ok is true and k and v are the next
|
|
|
|
// elements of the domain and range, respectively. Otherwise ok is
|
|
|
|
// false and k and v are undefined.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Components of the tuple are accessed using Extract.
|
|
|
|
//
|
|
|
|
// The IsString field distinguishes iterators over strings from those
|
|
|
|
// over maps, as the Type() alone is insufficient: consider
|
|
|
|
// map[int]rune.
|
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
// Type() returns a *types.Tuple for the triple (ok, k, v).
|
|
|
|
// The types of k and/or v may be types.Invalid.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t1 = next t0
|
|
|
|
//
|
|
|
|
type Next struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Iter Value
|
|
|
|
IsString bool // true => string iterator; false => map iterator.
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The TypeAssert instruction tests whether interface value X has type
|
|
|
|
// AssertedType.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// If !CommaOk, on success it returns v, the result of the conversion
|
|
|
|
// (defined below); on failure it panics.
|
|
|
|
//
|
|
|
|
// If CommaOk: on success it returns a pair (v, true) where v is the
|
|
|
|
// result of the conversion; on failure it returns (z, false) where z
|
|
|
|
// is AssertedType's zero value. The components of the pair must be
|
|
|
|
// accessed using the Extract instruction.
|
|
|
|
//
|
|
|
|
// If AssertedType is a concrete type, TypeAssert checks whether the
|
|
|
|
// dynamic type in interface X is equal to it, and if so, the result
|
|
|
|
// of the conversion is a copy of the value in the interface.
|
|
|
|
//
|
|
|
|
// If AssertedType is an interface, TypeAssert checks whether the
|
|
|
|
// dynamic type of the interface is assignable to it, and if so, the
|
|
|
|
// result of the conversion is a copy of the interface value X.
|
2013-07-26 19:49:27 -06:00
|
|
|
// If AssertedType is a superinterface of X.Type(), the operation will
|
|
|
|
// fail iff the operand is nil. (Contrast with ChangeInterface, which
|
|
|
|
// performs no nil-check.)
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-30 07:59:17 -06:00
|
|
|
// Type() reflects the actual type of the result, possibly a
|
|
|
|
// 2-types.Tuple; AssertedType is the asserted type.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-06-13 12:43:35 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
|
|
|
|
// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
|
2013-07-31 11:13:05 -06:00
|
|
|
// instruction arose from an explicit e.(T) operation; or the
|
|
|
|
// ast.CaseClause.Case if the instruction arose from a case of a
|
|
|
|
// type-switch statement.
|
2013-06-13 12:43:35 -06:00
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// t1 = typeassert t0.(int)
|
|
|
|
// t3 = typeassert,ok t2.(T)
|
|
|
|
//
|
|
|
|
type TypeAssert struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
X Value
|
|
|
|
AssertedType types.Type
|
|
|
|
CommaOk bool
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Extract instruction yields component Index of Tuple.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// This is used to access the results of instructions with multiple
|
|
|
|
// return values, such as Call, TypeAssert, Next, UnOp(ARROW) and
|
|
|
|
// IndexExpr(Map).
|
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t1 = extract t0 #1
|
|
|
|
//
|
|
|
|
type Extract struct {
|
2013-10-14 11:48:34 -06:00
|
|
|
register
|
2013-05-17 14:25:48 -06:00
|
|
|
Tuple Value
|
|
|
|
Index int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instructions executed for effect. They do not yield a value. --------------------
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Jump instruction transfers control to the sole successor of its
|
|
|
|
// owning block.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// A Jump must be the last instruction of its containing BasicBlock.
|
|
|
|
//
|
|
|
|
// Pos() returns NoPos.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// jump done
|
|
|
|
//
|
|
|
|
type Jump struct {
|
|
|
|
anInstruction
|
|
|
|
}
|
|
|
|
|
|
|
|
// The If instruction transfers control to one of the two successors
|
|
|
|
// of its owning block, depending on the boolean Cond: the first if
|
|
|
|
// true, the second if false.
|
|
|
|
//
|
|
|
|
// An If instruction must be the last instruction of its containing
|
|
|
|
// BasicBlock.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns NoPos.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// if t0 goto done else body
|
|
|
|
//
|
|
|
|
type If struct {
|
|
|
|
anInstruction
|
|
|
|
Cond Value
|
|
|
|
}
|
|
|
|
|
2013-10-08 10:31:39 -06:00
|
|
|
// The Return instruction returns values and control back to the calling
|
2013-05-17 15:02:47 -06:00
|
|
|
// function.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// len(Results) is always equal to the number of results in the
|
|
|
|
// function's signature.
|
|
|
|
//
|
2013-10-08 10:31:39 -06:00
|
|
|
// If len(Results) > 1, Return returns a tuple value with the specified
|
2013-05-17 14:25:48 -06:00
|
|
|
// components which the caller must access using Extract instructions.
|
|
|
|
//
|
|
|
|
// There is no instruction to return a ready-made tuple like those
|
|
|
|
// returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or
|
|
|
|
// a tail-call to a function with multiple result parameters.
|
|
|
|
//
|
2013-10-08 10:31:39 -06:00
|
|
|
// Return must be the last instruction of its containing BasicBlock.
|
2013-05-17 14:25:48 -06:00
|
|
|
// Such a block has no successors.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.ReturnStmt.Return, if explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
2013-10-08 10:31:39 -06:00
|
|
|
// return
|
|
|
|
// return nil:I, 2:int
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-10-08 10:31:39 -06:00
|
|
|
type Return struct {
|
2013-05-17 14:25:48 -06:00
|
|
|
anInstruction
|
|
|
|
Results []Value
|
2013-05-17 15:02:47 -06:00
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The RunDefers instruction pops and invokes the entire stack of
|
|
|
|
// procedure calls pushed by Defer instructions in this function.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// It is legal to encounter multiple 'rundefers' instructions in a
|
|
|
|
// single control-flow path through a function; this is useful in
|
|
|
|
// the combined init() function, for example.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns NoPos.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// rundefers
|
|
|
|
//
|
|
|
|
type RunDefers struct {
|
|
|
|
anInstruction
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Panic instruction initiates a panic with value X.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// A Panic instruction must be the last instruction of its containing
|
|
|
|
// BasicBlock, which must have no successors.
|
|
|
|
//
|
|
|
|
// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction;
|
|
|
|
// they are treated as calls to a built-in function.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.CallExpr.Lparen if this panic was explicit
|
|
|
|
// in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// panic t0
|
|
|
|
//
|
|
|
|
type Panic struct {
|
|
|
|
anInstruction
|
2013-05-17 15:02:47 -06:00
|
|
|
X Value // an interface{}
|
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Go instruction creates a new goroutine and calls the specified
|
|
|
|
// function within it.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// See CallCommon for generic function call documentation.
|
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// Pos() returns the ast.GoStmt.Go.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// go println(t0, t1)
|
|
|
|
// go t3()
|
|
|
|
// go invoke t5.Println(...t6)
|
|
|
|
//
|
|
|
|
type Go struct {
|
|
|
|
anInstruction
|
|
|
|
Call CallCommon
|
2013-07-31 11:13:05 -06:00
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Defer instruction pushes the specified call onto a stack of
|
|
|
|
// functions to be called by a RunDefers instruction or by a panic.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// See CallCommon for generic function call documentation.
|
|
|
|
//
|
2013-07-31 11:13:05 -06:00
|
|
|
// Pos() returns the ast.DeferStmt.Defer.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// defer println(t0, t1)
|
|
|
|
// defer t3()
|
|
|
|
// defer invoke t5.Println(...t6)
|
|
|
|
//
|
|
|
|
type Defer struct {
|
|
|
|
anInstruction
|
|
|
|
Call CallCommon
|
2013-07-31 11:13:05 -06:00
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Send instruction sends X on channel Chan.
|
|
|
|
//
|
|
|
|
// Pos() returns the ast.SendStmt.Arrow, if explicit in the source.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// send t0 <- t1
|
|
|
|
//
|
|
|
|
type Send struct {
|
|
|
|
anInstruction
|
|
|
|
Chan, X Value
|
2013-05-17 15:02:47 -06:00
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The Store instruction stores Val at address Addr.
|
2013-05-17 14:25:48 -06:00
|
|
|
// Stores can be of arbitrary types.
|
|
|
|
//
|
2013-05-17 15:02:47 -06:00
|
|
|
// Pos() returns the ast.StarExpr.Star, if explicit in the source.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
// Example printed form:
|
|
|
|
// *x = y
|
|
|
|
//
|
|
|
|
type Store struct {
|
|
|
|
anInstruction
|
|
|
|
Addr Value
|
|
|
|
Val Value
|
2013-05-17 15:02:47 -06:00
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// The MapUpdate instruction updates the association of Map[Key] to
|
|
|
|
// Value.
|
|
|
|
//
|
2013-08-22 08:13:51 -06:00
|
|
|
// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
|
|
|
|
// if explicit in the source.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t0[t1] = t2
|
|
|
|
//
|
|
|
|
type MapUpdate struct {
|
|
|
|
anInstruction
|
|
|
|
Map Value
|
|
|
|
Key Value
|
|
|
|
Value Value
|
2013-05-17 15:02:47 -06:00
|
|
|
pos token.Pos
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-10-28 10:05:29 -06:00
|
|
|
// A DebugRef instruction maps a source-level expression Expr to the
|
2013-11-15 07:21:48 -07:00
|
|
|
// SSA value X that represents the value (!IsAddr) or address (IsAddr)
|
2013-10-28 10:05:29 -06:00
|
|
|
// of that expression.
|
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
|
|
|
//
|
|
|
|
// DebugRef is a pseudo-instruction: it has no dynamic effect.
|
|
|
|
//
|
2013-10-09 10:47:30 -06:00
|
|
|
// Pos() returns Expr.Pos(), the start position of the source-level
|
|
|
|
// expression. This is not the same as the "designated" token as
|
|
|
|
// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the
|
|
|
|
// position of the ("designated") Lparen token.
|
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
|
|
|
//
|
2013-10-28 10:05:29 -06:00
|
|
|
// If Expr is an *ast.Ident denoting a var or func, Object() returns
|
|
|
|
// the object; though this information can be obtained from the type
|
|
|
|
// checker, including it here greatly facilitates debugging.
|
|
|
|
// For non-Ident expressions, Object() returns nil.
|
|
|
|
//
|
|
|
|
// DebugRefs are generated only for functions built with debugging
|
|
|
|
// enabled; see Package.SetDebugMode().
|
|
|
|
//
|
|
|
|
// DebugRefs are not emitted for ast.Idents referring to constants or
|
|
|
|
// predeclared identifiers, since they are trivial and numerous.
|
|
|
|
// Nor are they emitted for ast.ParenExprs.
|
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
|
|
|
//
|
|
|
|
// (By representing these as instructions, rather than out-of-band,
|
|
|
|
// consistency is maintained during transformation passes by the
|
|
|
|
// ordinary SSA renaming machinery.)
|
|
|
|
//
|
2013-10-28 10:05:29 -06:00
|
|
|
// Example printed form:
|
|
|
|
// ; *ast.CallExpr @ 102:9 is t5
|
|
|
|
// ; var x float64 @ 109:72 is x
|
|
|
|
// ; address of *ast.CompositeLit @ 216:10 is t0
|
2013-10-27 08:55:21 -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
|
|
|
type DebugRef struct {
|
|
|
|
anInstruction
|
go.tools/ssa: record lvalue/rvalue distinction precisely in DebugRef.
A DebugRef associates a source expression E with an ssa.Value
V, but until now did not record whether V was the value or the
address of E. So, we would guess from the "pointerness" of
the Value, leading to confusion in some cases, e.g.
type N *N
var n N
n = &n // lvalue and rvalue are both pointers
Now we explicitly record 'IsAddress bool' in DebugRef, and
plumb this everywhere: through (*Function).ValueForExpr and
(*Program).VarValue, all the way to forming the pointer
analysis query.
Also:
- VarValue now treats each reference to a global distinctly,
just like it does for other vars. So:
var g int
func f() {
g = 1 // VarValue(g) == Const(1:int), !isAddress
print(g) // VarValue(g) == Global(g), isAddress
}
- DebugRefs are not emitted for references to predeclared
identifiers (nil, built-in).
- DebugRefs no longer prevent lifting of an Alloc var into a
register; now we update or discard the debug info.
- TestValueForExpr: improve coverage of ssa.EnclosingFunction
by putting expectations in methods and init funcs, not just
normal funcs.
- oracle: fix golden file broken by recent
(*types.Var).IsField change.
R=gri
CC=golang-dev
https://golang.org/cl/16610045
2013-10-24 16:31:50 -06:00
|
|
|
Expr ast.Expr // the referring expression (never *ast.ParenExpr)
|
2013-10-28 10:05:29 -06:00
|
|
|
object types.Object // the identity of the source var/func
|
go.tools/ssa: record lvalue/rvalue distinction precisely in DebugRef.
A DebugRef associates a source expression E with an ssa.Value
V, but until now did not record whether V was the value or the
address of E. So, we would guess from the "pointerness" of
the Value, leading to confusion in some cases, e.g.
type N *N
var n N
n = &n // lvalue and rvalue are both pointers
Now we explicitly record 'IsAddress bool' in DebugRef, and
plumb this everywhere: through (*Function).ValueForExpr and
(*Program).VarValue, all the way to forming the pointer
analysis query.
Also:
- VarValue now treats each reference to a global distinctly,
just like it does for other vars. So:
var g int
func f() {
g = 1 // VarValue(g) == Const(1:int), !isAddress
print(g) // VarValue(g) == Global(g), isAddress
}
- DebugRefs are not emitted for references to predeclared
identifiers (nil, built-in).
- DebugRefs no longer prevent lifting of an Alloc var into a
register; now we update or discard the debug info.
- TestValueForExpr: improve coverage of ssa.EnclosingFunction
by putting expectations in methods and init funcs, not just
normal funcs.
- oracle: fix golden file broken by recent
(*types.Var).IsField change.
R=gri
CC=golang-dev
https://golang.org/cl/16610045
2013-10-24 16:31:50 -06:00
|
|
|
IsAddr bool // Expr is addressable and X is the address it denotes
|
2013-10-28 10:05:29 -06:00
|
|
|
X Value // the value or address of Expr
|
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
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
// Embeddable mix-ins and helpers for common parts of other structs. -----------
|
|
|
|
|
2013-10-14 11:48:34 -06:00
|
|
|
// register is a mix-in embedded by all SSA values that are also
|
|
|
|
// instructions, i.e. virtual registers, and provides a uniform
|
|
|
|
// implementation of most of the Value interface: Value.Name() is a
|
|
|
|
// numbered register (e.g. "t0"); the other methods are field accessors.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-10-14 11:48:34 -06:00
|
|
|
// Temporary names are automatically assigned to each register on
|
2013-05-17 14:25:48 -06:00
|
|
|
// completion of building a function in SSA form.
|
|
|
|
//
|
|
|
|
// Clients must not assume that the 'id' value (and the Name() derived
|
|
|
|
// from it) is unique within a function. As always in this API,
|
|
|
|
// semantics are determined only by identity; names exist only to
|
|
|
|
// facilitate debugging.
|
|
|
|
//
|
2013-10-14 11:48:34 -06:00
|
|
|
type register struct {
|
2013-05-17 14:25:48 -06:00
|
|
|
anInstruction
|
|
|
|
num int // "name" of virtual register, e.g. "t0". Not guaranteed unique.
|
2013-05-30 07:59:17 -06:00
|
|
|
typ types.Type // type of virtual register
|
2013-05-17 15:02:47 -06:00
|
|
|
pos token.Pos // position of source expression, or NoPos
|
2013-05-17 14:25:48 -06:00
|
|
|
referrers []Instruction
|
|
|
|
}
|
|
|
|
|
|
|
|
// anInstruction is a mix-in embedded by all Instructions.
|
2013-11-07 08:08:51 -07:00
|
|
|
// It provides the implementations of the Block and setBlock methods.
|
2013-05-17 14:25:48 -06:00
|
|
|
type anInstruction struct {
|
2013-05-30 07:59:17 -06:00
|
|
|
block *BasicBlock // the basic block of this instruction
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CallCommon is contained by Go, Defer and Call to hold the
|
|
|
|
// common parts of a function or method call.
|
|
|
|
//
|
|
|
|
// Each CallCommon exists in one of two modes, function call and
|
|
|
|
// interface method invocation, or "call" and "invoke" for short.
|
|
|
|
//
|
2013-07-26 09:22:34 -06:00
|
|
|
// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon
|
2013-08-19 13:38:30 -06:00
|
|
|
// represents an ordinary function call of the value in Value,
|
|
|
|
// which may be a *Builtin, a *Function or any other value of kind
|
|
|
|
// 'func'.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2014-01-07 11:31:05 -07:00
|
|
|
// Value may be one of:
|
|
|
|
// (a) a *Function, indicating a statically dispatched call
|
|
|
|
// to a package-level function, an anonymous function, or
|
|
|
|
// a method of a named type.
|
|
|
|
// (b) a *MakeClosure, indicating an immediately applied
|
|
|
|
// function literal with free variables.
|
|
|
|
// (c) a *Builtin, indicating a statically dispatched call
|
|
|
|
// to a built-in function. StaticCallee returns nil.
|
|
|
|
// (d) any other value, indicating a dynamically dispatched
|
|
|
|
// function call.
|
|
|
|
// StaticCallee returns the identity of the callee in cases
|
|
|
|
// (a) and (b), nil otherwise.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-26 12:06:26 -06:00
|
|
|
// Args contains the arguments to the call. If Value is a method,
|
|
|
|
// Args[0] contains the receiver parameter.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t2 = println(t0, t1)
|
|
|
|
// go t3()
|
|
|
|
// defer t5(...t6)
|
|
|
|
//
|
2013-07-26 09:22:34 -06:00
|
|
|
// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon
|
2013-05-17 14:25:48 -06:00
|
|
|
// represents a dynamically dispatched call to an interface method.
|
2013-07-26 12:06:26 -06:00
|
|
|
// In this mode, Value is the interface value and Method is the
|
2013-08-19 13:38:30 -06:00
|
|
|
// interface's abstract method. Note: an abstract method may be
|
|
|
|
// shared by multiple interfaces due to embedding; Value.Type()
|
|
|
|
// provides the specific interface used for this call.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-26 12:06:26 -06:00
|
|
|
// Value is implicitly supplied to the concrete method implementation
|
2013-05-17 14:25:48 -06:00
|
|
|
// as the receiver parameter; in other words, Args[0] holds not the
|
2013-07-26 12:06:26 -06:00
|
|
|
// receiver but the first true argument.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// Example printed form:
|
|
|
|
// t1 = invoke t0.String()
|
|
|
|
// go invoke t3.Run(t2)
|
|
|
|
// defer invoke t4.Handle(...t5)
|
|
|
|
//
|
2014-01-07 11:31:05 -07:00
|
|
|
// For all calls to variadic functions (Signature().IsVariadic()),
|
|
|
|
// the last element of Args is a slice.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
type CallCommon struct {
|
2014-01-07 11:31:05 -07:00
|
|
|
Value Value // receiver (invoke mode) or func value (call mode)
|
|
|
|
Method *types.Func // abstract method (invoke mode)
|
|
|
|
Args []Value // actual parameters (in static method call, includes receiver)
|
|
|
|
pos token.Pos // position of CallExpr.Lparen, iff explicit in source
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvoke returns true if this call has "invoke" (not "call") mode.
|
|
|
|
func (c *CallCommon) IsInvoke() bool {
|
2013-07-26 09:22:34 -06:00
|
|
|
return c.Method != nil
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
func (c *CallCommon) Pos() token.Pos { return c.pos }
|
|
|
|
|
2013-06-24 12:15:13 -06:00
|
|
|
// Signature returns the signature of the called function.
|
|
|
|
//
|
|
|
|
// For an "invoke"-mode call, the signature of the interface method is
|
2013-06-26 10:38:08 -06:00
|
|
|
// returned.
|
|
|
|
//
|
|
|
|
// In either "call" or "invoke" mode, if the callee is a method, its
|
|
|
|
// receiver is represented by sig.Recv, not sig.Params().At(0).
|
2013-06-24 12:15:13 -06:00
|
|
|
//
|
|
|
|
func (c *CallCommon) Signature() *types.Signature {
|
2013-07-26 09:22:34 -06:00
|
|
|
if c.Method != nil {
|
|
|
|
return c.Method.Type().(*types.Signature)
|
2013-06-24 12:15:13 -06:00
|
|
|
}
|
2014-01-07 11:31:05 -07:00
|
|
|
return c.Value.Type().Underlying().(*types.Signature)
|
2013-06-24 12:15:13 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 11:31:05 -07:00
|
|
|
// StaticCallee returns the callee if this is a trivially static
|
|
|
|
// "call"-mode call to a function.
|
2013-05-17 14:25:48 -06:00
|
|
|
func (c *CallCommon) StaticCallee() *Function {
|
2013-07-26 12:06:26 -06:00
|
|
|
switch fn := c.Value.(type) {
|
2013-05-17 14:25:48 -06:00
|
|
|
case *Function:
|
|
|
|
return fn
|
|
|
|
case *MakeClosure:
|
|
|
|
return fn.Fn.(*Function)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description returns a description of the mode of this call suitable
|
|
|
|
// for a user interface, e.g. "static method call".
|
|
|
|
func (c *CallCommon) Description() string {
|
2013-07-26 12:06:26 -06:00
|
|
|
switch fn := c.Value.(type) {
|
2013-08-19 13:38:30 -06:00
|
|
|
case *Builtin:
|
|
|
|
return "built-in function call"
|
2013-05-17 14:25:48 -06:00
|
|
|
case *MakeClosure:
|
|
|
|
return "static function closure call"
|
|
|
|
case *Function:
|
2013-05-17 15:02:47 -06:00
|
|
|
if fn.Signature.Recv() != nil {
|
2013-05-17 14:25:48 -06:00
|
|
|
return "static method call"
|
|
|
|
}
|
|
|
|
return "static function call"
|
|
|
|
}
|
2013-08-19 13:38:30 -06:00
|
|
|
if c.IsInvoke() {
|
|
|
|
return "dynamic method call" // ("invoke" mode)
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
return "dynamic function call"
|
|
|
|
}
|
|
|
|
|
2013-06-24 12:15:13 -06:00
|
|
|
// The CallInstruction interface, implemented by *Go, *Defer and *Call,
|
|
|
|
// exposes the common parts of function calling instructions,
|
|
|
|
// yet provides a way back to the Value defined by *Call alone.
|
|
|
|
//
|
|
|
|
type CallInstruction interface {
|
|
|
|
Instruction
|
|
|
|
Common() *CallCommon // returns the common parts of the call
|
|
|
|
Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Call) Common() *CallCommon { return &s.Call }
|
|
|
|
func (s *Defer) Common() *CallCommon { return &s.Call }
|
|
|
|
func (s *Go) Common() *CallCommon { return &s.Call }
|
|
|
|
|
|
|
|
func (s *Call) Value() *Call { return s }
|
|
|
|
func (s *Defer) Value() *Call { return nil }
|
|
|
|
func (s *Go) Value() *Call { return nil }
|
|
|
|
|
2014-01-07 11:31:05 -07:00
|
|
|
func (v *Builtin) Type() types.Type { return v.sig }
|
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
|
|
|
func (v *Builtin) Name() string { return v.object.Name() }
|
2013-05-17 14:25:48 -06:00
|
|
|
func (*Builtin) Referrers() *[]Instruction { return nil }
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Builtin) Pos() token.Pos { return token.NoPos }
|
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
|
|
|
func (v *Builtin) Object() types.Object { return v.object }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Capture) Type() types.Type { return v.typ }
|
|
|
|
func (v *Capture) Name() string { return v.name }
|
2013-05-17 14:25:48 -06:00
|
|
|
func (v *Capture) Referrers() *[]Instruction { return &v.referrers }
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Capture) Pos() token.Pos { return v.pos }
|
2013-06-13 12:43:35 -06:00
|
|
|
func (v *Capture) Parent() *Function { return v.parent }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-11-15 07:21:48 -07:00
|
|
|
func (v *Global) Type() types.Type { return v.typ }
|
|
|
|
func (v *Global) Name() string { return v.name }
|
|
|
|
func (v *Global) Pos() token.Pos { return v.pos }
|
|
|
|
func (v *Global) Referrers() *[]Instruction { return nil }
|
|
|
|
func (v *Global) Token() token.Token { return token.VAR }
|
|
|
|
func (v *Global) Object() types.Object { return v.object }
|
|
|
|
func (v *Global) String() string { return v.RelString(nil) }
|
|
|
|
func (v *Global) Package() *Package { return v.Pkg }
|
|
|
|
func (v *Global) RelString(from *types.Package) string { return relString(v, from) }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-11-07 08:08:51 -07:00
|
|
|
func (v *Function) Name() string { return v.name }
|
|
|
|
func (v *Function) Type() types.Type { return v.Signature }
|
|
|
|
func (v *Function) Pos() token.Pos { return v.pos }
|
|
|
|
func (v *Function) Token() token.Token { return token.FUNC }
|
|
|
|
func (v *Function) Object() types.Object { return v.object }
|
2013-11-15 07:21:48 -07:00
|
|
|
func (v *Function) String() string { return v.RelString(nil) }
|
|
|
|
func (v *Function) Package() *Package { return v.Pkg }
|
2013-11-07 08:08:51 -07:00
|
|
|
func (v *Function) Referrers() *[]Instruction {
|
|
|
|
if v.Enclosing != nil {
|
|
|
|
return &v.referrers
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Parameter) Type() types.Type { return v.typ }
|
|
|
|
func (v *Parameter) Name() string { return v.name }
|
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
|
|
|
func (v *Parameter) Object() types.Object { return v.object }
|
2013-05-17 14:25:48 -06:00
|
|
|
func (v *Parameter) Referrers() *[]Instruction { return &v.referrers }
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Parameter) Pos() token.Pos { return v.pos }
|
2013-06-13 12:43:35 -06:00
|
|
|
func (v *Parameter) Parent() *Function { return v.parent }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Alloc) Type() types.Type { return v.typ }
|
2013-05-17 14:25:48 -06:00
|
|
|
func (v *Alloc) Referrers() *[]Instruction { return &v.referrers }
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *Alloc) Pos() token.Pos { return v.pos }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-10-14 11:48:34 -06:00
|
|
|
func (v *register) Type() types.Type { return v.typ }
|
|
|
|
func (v *register) setType(typ types.Type) { v.typ = typ }
|
|
|
|
func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) }
|
|
|
|
func (v *register) setNum(num int) { v.num = num }
|
|
|
|
func (v *register) Referrers() *[]Instruction { return &v.referrers }
|
|
|
|
func (v *register) Pos() token.Pos { return v.pos }
|
|
|
|
func (v *register) setPos(pos token.Pos) { v.pos = pos }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-06-13 12:43:35 -06:00
|
|
|
func (v *anInstruction) Parent() *Function { return v.block.parent }
|
2013-05-30 07:59:17 -06:00
|
|
|
func (v *anInstruction) Block() *BasicBlock { return v.block }
|
2013-11-07 08:08:51 -07:00
|
|
|
func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-11-15 07:21:48 -07:00
|
|
|
func (t *Type) Name() string { return t.object.Name() }
|
|
|
|
func (t *Type) Pos() token.Pos { return t.object.Pos() }
|
|
|
|
func (t *Type) Type() types.Type { return t.object.Type() }
|
|
|
|
func (t *Type) Token() token.Token { return token.TYPE }
|
|
|
|
func (t *Type) Object() types.Object { return t.object }
|
|
|
|
func (t *Type) String() string { return t.RelString(nil) }
|
|
|
|
func (t *Type) Package() *Package { return t.pkg }
|
|
|
|
func (t *Type) RelString(from *types.Package) string { return relString(t, from) }
|
|
|
|
|
|
|
|
func (c *NamedConst) Name() string { return c.object.Name() }
|
|
|
|
func (c *NamedConst) Pos() token.Pos { return c.object.Pos() }
|
|
|
|
func (c *NamedConst) String() string { return c.RelString(nil) }
|
|
|
|
func (c *NamedConst) Type() types.Type { return c.object.Type() }
|
|
|
|
func (c *NamedConst) Token() token.Token { return token.CONST }
|
|
|
|
func (c *NamedConst) Object() types.Object { return c.object }
|
|
|
|
func (c *NamedConst) Package() *Package { return c.pkg }
|
|
|
|
func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
// Func returns the package-level function of the specified name,
|
|
|
|
// or nil if not found.
|
|
|
|
//
|
|
|
|
func (p *Package) Func(name string) (f *Function) {
|
|
|
|
f, _ = p.Members[name].(*Function)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Var returns the package-level variable of the specified name,
|
|
|
|
// or nil if not found.
|
|
|
|
//
|
|
|
|
func (p *Package) Var(name string) (g *Global) {
|
|
|
|
g, _ = p.Members[name].(*Global)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Const returns the package-level constant of the specified name,
|
|
|
|
// or nil if not found.
|
|
|
|
//
|
2013-07-16 11:50:08 -06:00
|
|
|
func (p *Package) Const(name string) (c *NamedConst) {
|
|
|
|
c, _ = p.Members[name].(*NamedConst)
|
2013-05-17 14:25:48 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the package-level type of the specified name,
|
|
|
|
// or nil if not found.
|
|
|
|
//
|
|
|
|
func (p *Package) Type(name string) (t *Type) {
|
|
|
|
t, _ = p.Members[name].(*Type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
func (v *Call) Pos() token.Pos { return v.Call.pos }
|
2013-07-31 11:13:05 -06:00
|
|
|
func (s *Defer) Pos() token.Pos { return s.pos }
|
|
|
|
func (s *Go) Pos() token.Pos { return s.pos }
|
2013-05-17 15:02:47 -06:00
|
|
|
func (s *MapUpdate) Pos() token.Pos { return s.pos }
|
|
|
|
func (s *Panic) Pos() token.Pos { return s.pos }
|
2013-10-08 10:31:39 -06:00
|
|
|
func (s *Return) Pos() token.Pos { return s.pos }
|
2013-05-17 15:02:47 -06:00
|
|
|
func (s *Send) Pos() token.Pos { return s.pos }
|
|
|
|
func (s *Store) Pos() token.Pos { return s.pos }
|
|
|
|
func (s *If) Pos() token.Pos { return token.NoPos }
|
|
|
|
func (s *Jump) Pos() token.Pos { return token.NoPos }
|
|
|
|
func (s *RunDefers) Pos() token.Pos { return token.NoPos }
|
2013-07-31 11:13:05 -06:00
|
|
|
func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() }
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
// Operands.
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
func (v *Alloc) Operands(rands []*Value) []*Value {
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *BinOp) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X, &v.Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CallCommon) Operands(rands []*Value) []*Value {
|
2013-07-26 12:06:26 -06:00
|
|
|
rands = append(rands, &c.Value)
|
2013-05-17 14:25:48 -06:00
|
|
|
for i := range c.Args {
|
|
|
|
rands = append(rands, &c.Args[i])
|
|
|
|
}
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Go) Operands(rands []*Value) []*Value {
|
|
|
|
return s.Call.Operands(rands)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Call) Operands(rands []*Value) []*Value {
|
|
|
|
return s.Call.Operands(rands)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Defer) Operands(rands []*Value) []*Value {
|
|
|
|
return s.Call.Operands(rands)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ChangeInterface) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
func (v *ChangeType) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Convert) Operands(rands []*Value) []*Value {
|
2013-05-17 14:25:48 -06:00
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (s *DebugRef) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &s.X)
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
func (v *Extract) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.Tuple)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Field) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FieldAddr) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *If) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &s.Cond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Index) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X, &v.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *IndexAddr) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X, &v.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Jump) Operands(rands []*Value) []*Value {
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Lookup) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X, &v.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeChan) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeClosure) Operands(rands []*Value) []*Value {
|
|
|
|
rands = append(rands, &v.Fn)
|
|
|
|
for i := range v.Bindings {
|
|
|
|
rands = append(rands, &v.Bindings[i])
|
|
|
|
}
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeInterface) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeMap) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.Reserve)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MakeSlice) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.Len, &v.Cap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MapUpdate) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.Map, &v.Key, &v.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Next) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.Iter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Panic) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &s.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Phi) Operands(rands []*Value) []*Value {
|
|
|
|
for i := range v.Edges {
|
|
|
|
rands = append(rands, &v.Edges[i])
|
|
|
|
}
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Range) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
2013-10-08 10:31:39 -06:00
|
|
|
func (s *Return) Operands(rands []*Value) []*Value {
|
2013-05-17 14:25:48 -06:00
|
|
|
for i := range s.Results {
|
|
|
|
rands = append(rands, &s.Results[i])
|
|
|
|
}
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*RunDefers) Operands(rands []*Value) []*Value {
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Select) Operands(rands []*Value) []*Value {
|
|
|
|
for i := range v.States {
|
|
|
|
rands = append(rands, &v.States[i].Chan, &v.States[i].Send)
|
|
|
|
}
|
|
|
|
return rands
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Send) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &s.Chan, &s.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Slice) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X, &v.Low, &v.High)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &s.Addr, &s.Val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TypeAssert) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UnOp) Operands(rands []*Value) []*Value {
|
|
|
|
return append(rands, &v.X)
|
|
|
|
}
|