blob: a8a301deaf41544278f7e3533be0e57be2990e72 [file] [log] [blame]
Alan Donovan713699d2013-08-27 18:49:13 -04001// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Rob Pike83f21b92013-05-17 13:25:48 -07005package ssa
6
7// This package defines a high-level intermediate representation for
8// Go programs using static single-assignment (SSA) form.
9
10import (
11 "fmt"
12 "go/ast"
13 "go/token"
14 "sync"
15
16 "code.google.com/p/go.tools/go/exact"
17 "code.google.com/p/go.tools/go/types"
Alan Donovan4df74772013-07-10 18:01:11 -040018 "code.google.com/p/go.tools/go/types/typemap"
Alan Donovanbe28dbb2013-05-31 16:14:13 -040019 "code.google.com/p/go.tools/importer"
Rob Pike83f21b92013-05-17 13:25:48 -070020)
21
22// A Program is a partial or complete Go program converted to SSA form.
Rob Pike83f21b92013-05-17 13:25:48 -070023//
24type Program struct {
Alan Donovan3f2f9a72013-09-06 18:13:57 -040025 Fset *token.FileSet // position information for the files of this Program
26 imported map[string]*Package // all importable Packages, keyed by import path
27 packages map[*types.Package]*Package // all loaded Packages, keyed by object
Alan Donovan318b83e2013-09-23 18:18:35 -040028 builtins map[*types.Builtin]*Builtin // all built-in functions, keyed by typechecker objects.
Alan Donovan3f2f9a72013-09-06 18:13:57 -040029 mode BuilderMode // set of mode bits for SSA construction
Rob Pike83f21b92013-05-17 13:25:48 -070030
Alan Donovanf1d4d012013-06-14 15:50:37 -040031 methodsMu sync.Mutex // guards the following maps:
Alan Donovan2a3a1292013-07-30 14:28:14 -040032 methodSets typemap.M // maps type to its concrete methodSet
Alan Donovan4da31df2013-07-26 11:22:34 -040033 boundMethodWrappers map[*types.Func]*Function // wrappers for curried x.Method closures
Alan Donovanf1d4d012013-06-14 15:50:37 -040034 ifaceMethodWrappers map[*types.Func]*Function // wrappers for curried I.Method functions
Rob Pike83f21b92013-05-17 13:25:48 -070035}
36
37// A Package is a single analyzed Go package containing Members for
38// all package-level functions, variables, constants and types it
39// declares. These may be accessed directly via Members, or via the
40// type-specific accessor methods Func, Type, Var and Const.
41//
42type Package struct {
Alan Donovan87ced822013-10-23 17:07:52 -040043 Prog *Program // the owning program
44 Object *types.Package // the type checker's package object for this package
45 Members map[string]Member // all package members keyed by name
46 methodSets []types.Type // types whose method sets are included in this package
47 values map[types.Object]Value // package members (incl. types and methods), keyed by object
48 init *Function // Func("init"); the package's init function
49 debug bool // include full debug info in this package.
Rob Pike83f21b92013-05-17 13:25:48 -070050
Alan Donovan4d628a02013-06-03 14:15:19 -040051 // The following fields are set transiently, then cleared
52 // after building.
Alan Donovan87ced822013-10-23 17:07:52 -040053 started int32 // atomically tested and set at start of build phase
54 ninit int32 // number of init functions
55 info *importer.PackageInfo // package ASTs and type information
56 needRTTI typemap.M // types for which runtime type info is needed
Rob Pike83f21b92013-05-17 13:25:48 -070057}
58
Alan Donovan732dbe92013-07-16 13:50:08 -040059// A Member is a member of a Go package, implemented by *NamedConst,
Rob Pike83f21b92013-05-17 13:25:48 -070060// *Global, *Function, or *Type; they are created by package-level
61// const, var, func and type declarations respectively.
62//
63type Member interface {
Alan Donovan9fcd20e2013-11-15 09:21:48 -050064 Name() string // declared name of the package member
65 String() string // package-qualified name of the package member
66 RelString(*types.Package) string // like String, but relative refs are unqualified
67 Object() types.Object // typechecker's object for this member, if any
68 Pos() token.Pos // position of member's declaration, if known
69 Type() types.Type // type of the package member
70 Token() token.Token // token.{VAR,FUNC,CONST,TYPE}
71 Package() *Package // returns the containing package. (TODO: rename Pkg)
Rob Pike83f21b92013-05-17 13:25:48 -070072}
73
Alan Donovan341a07a2013-06-13 14:43:35 -040074// A Type is a Member of a Package representing a package-level named type.
75//
76// Type() returns a *types.Named.
Rob Pike83f21b92013-05-17 13:25:48 -070077//
78type Type struct {
Alan Donovanbc1f7242013-07-11 14:12:30 -040079 object *types.TypeName
Alan Donovan9fcd20e2013-11-15 09:21:48 -050080 pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -070081}
82
Alan Donovan732dbe92013-07-16 13:50:08 -040083// A NamedConst is a Member of Package representing a package-level
84// named constant value.
Rob Pike83f21b92013-05-17 13:25:48 -070085//
Rob Pike87334f42013-05-17 14:02:47 -070086// Pos() returns the position of the declaring ast.ValueSpec.Names[*]
87// identifier.
88//
Alan Donovan732dbe92013-07-16 13:50:08 -040089// NB: a NamedConst is not a Value; it contains a constant Value, which
Rob Pike87334f42013-05-17 14:02:47 -070090// it augments with the name and position of its 'const' declaration.
91//
Alan Donovan732dbe92013-07-16 13:50:08 -040092type NamedConst struct {
Alan Donovanbc1f7242013-07-11 14:12:30 -040093 object *types.Const
Alan Donovan732dbe92013-07-16 13:50:08 -040094 Value *Const
Alan Donovanbc1f7242013-07-11 14:12:30 -040095 pos token.Pos
Alan Donovan9fcd20e2013-11-15 09:21:48 -050096 pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -070097}
98
99// An SSA value that can be referenced by an instruction.
100type Value interface {
101 // Name returns the name of this value, and determines how
102 // this Value appears when used as an operand of an
103 // Instruction.
104 //
105 // This is the same as the source name for Parameters,
Alan Donovanbac70982013-10-29 11:07:09 -0400106 // Builtins, Functions, Captures, Globals.
Alan Donovan732dbe92013-07-16 13:50:08 -0400107 // For constants, it is a representation of the constant's value
Rob Pike83f21b92013-05-17 13:25:48 -0700108 // and type. For all other Values this is the name of the
109 // virtual register defined by the instruction.
110 //
111 // The name of an SSA Value is not semantically significant,
112 // and may not even be unique within a function.
113 Name() string
114
115 // If this value is an Instruction, String returns its
116 // disassembled form; otherwise it returns unspecified
117 // human-readable information about the Value, such as its
118 // kind, name and type.
119 String() string
120
121 // Type returns the type of this value. Many instructions
Alan Donovanbac70982013-10-29 11:07:09 -0400122 // (e.g. IndexAddr) change their behaviour depending on the
Rob Pike83f21b92013-05-17 13:25:48 -0700123 // types of their operands.
124 Type() types.Type
125
126 // Referrers returns the list of instructions that have this
127 // value as one of their operands; it may contain duplicates
128 // if an instruction has a repeated operand.
129 //
130 // Referrers actually returns a pointer through which the
131 // caller may perform mutations to the object's state.
132 //
133 // Referrers is currently only defined for the function-local
Alan Donovanf1e5b032013-11-07 10:08:51 -0500134 // values Capture, Parameter, Functions (iff anonymous) and
135 // all value-defining instructions.
136 // It returns nil for named Functions, Builtin, Const and Global.
Rob Pike83f21b92013-05-17 13:25:48 -0700137 //
138 // Instruction.Operands contains the inverse of this relation.
139 Referrers() *[]Instruction
140
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400141 // Pos returns the location of the AST token most closely
142 // associated with the operation that gave rise to this value,
143 // or token.NoPos if it was not explicit in the source.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400144 //
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400145 // For each ast.Node type, a particular token is designated as
146 // the closest location for the expression, e.g. the Lparen
147 // for an *ast.CallExpr. This permits a compact but
148 // approximate mapping from Values to source positions for use
149 // in diagnostic messages, for example.
150 //
151 // (Do not use this position to determine which Value
152 // corresponds to an ast.Expr; use Function.ValueForExpr
153 // instead. NB: it requires that the function was built with
154 // debug information.)
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400155 //
156 Pos() token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -0700157}
158
159// An Instruction is an SSA instruction that computes a new Value or
160// has some effect.
161//
162// An Instruction that defines a value (e.g. BinOp) also implements
163// the Value interface; an Instruction that only has an effect (e.g. Store)
164// does not.
165//
166type Instruction interface {
167 // String returns the disassembled form of this value. e.g.
168 //
169 // Examples of Instructions that define a Value:
170 // e.g. "x + y" (BinOp)
171 // "len([])" (Call)
172 // Note that the name of the Value is not printed.
173 //
174 // Examples of Instructions that do define (are) Values:
Alan Donovan068f0172013-10-08 12:31:39 -0400175 // e.g. "return x" (Return)
Rob Pike83f21b92013-05-17 13:25:48 -0700176 // "*y = x" (Store)
177 //
178 // (This separation is useful for some analyses which
179 // distinguish the operation from the value it
180 // defines. e.g. 'y = local int' is both an allocation of
181 // memory 'local int' and a definition of a pointer y.)
182 String() string
183
Alan Donovan341a07a2013-06-13 14:43:35 -0400184 // Parent returns the function to which this instruction
185 // belongs.
186 Parent() *Function
187
Rob Pike83f21b92013-05-17 13:25:48 -0700188 // Block returns the basic block to which this instruction
189 // belongs.
190 Block() *BasicBlock
191
Alan Donovanf1e5b032013-11-07 10:08:51 -0500192 // setBlock sets the basic block to which this instruction belongs.
193 setBlock(*BasicBlock)
Rob Pike83f21b92013-05-17 13:25:48 -0700194
195 // Operands returns the operands of this instruction: the
196 // set of Values it references.
197 //
198 // Specifically, it appends their addresses to rands, a
199 // user-provided slice, and returns the resulting slice,
200 // permitting avoidance of memory allocation.
201 //
202 // The operands are appended in undefined order; the addresses
203 // are always non-nil but may point to a nil Value. Clients
204 // may store through the pointers, e.g. to effect a value
205 // renaming.
206 //
207 // Value.Referrers is a subset of the inverse of this
208 // relation. (Referrers are not tracked for all types of
209 // Values.)
210 Operands(rands []*Value) []*Value
211
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400212 // Pos returns the location of the AST token most closely
213 // associated with the operation that gave rise to this
214 // instruction, or token.NoPos if it was not explicit in the
215 // source.
Rob Pike87334f42013-05-17 14:02:47 -0700216 //
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400217 // For each ast.Node type, a particular token is designated as
218 // the closest location for the expression, e.g. the Go token
219 // for an *ast.GoStmt. This permits a compact but approximate
220 // mapping from Instructions to source positions for use in
221 // diagnostic messages, for example.
222 //
223 // (Do not use this position to determine which Instruction
224 // corresponds to an ast.Expr; see the notes for Value.Pos.
225 // This position may be used to determine which non-Value
226 // Instruction corresponds to some ast.Stmts, but not all: If
227 // and Jump instructions have no Pos(), for example.)
Rob Pike87334f42013-05-17 14:02:47 -0700228 //
229 Pos() token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -0700230}
231
232// Function represents the parameters, results and code of a function
233// or method.
234//
235// If Blocks is nil, this indicates an external function for which no
236// Go source code is available. In this case, Captures and Locals
237// will be nil too. Clients performing whole-program analysis must
238// handle external functions specially.
239//
240// Functions are immutable values; they do not have addresses.
241//
Alan Donovanb5016cb2013-12-05 09:50:18 -0500242// Blocks contains the function's control-flow graph (CFG).
Rob Pike83f21b92013-05-17 13:25:48 -0700243// Blocks[0] is the function entry point; block order is not otherwise
244// semantically significant, though it may affect the readability of
245// the disassembly.
Alan Donovanb5016cb2013-12-05 09:50:18 -0500246// To iterate over the blocks in dominance order, use DomPreorder().
Rob Pike83f21b92013-05-17 13:25:48 -0700247//
Alan Donovan2accef22013-10-14 15:38:56 -0400248// Recover is an optional second entry point to which control resumes
249// after a recovered panic. The Recover block may contain only a load
250// of the function's named return parameters followed by a return of
251// the loaded values.
252//
Rob Pike83f21b92013-05-17 13:25:48 -0700253// A nested function that refers to one or more lexically enclosing
254// local variables ("free variables") has Capture parameters. Such
255// functions cannot be called directly but require a value created by
256// MakeClosure which, via its Bindings, supplies values for these
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400257// parameters.
Rob Pike83f21b92013-05-17 13:25:48 -0700258//
Rob Pike87334f42013-05-17 14:02:47 -0700259// If the function is a method (Signature.Recv() != nil) then the first
Rob Pike83f21b92013-05-17 13:25:48 -0700260// element of Params is the receiver parameter.
261//
Rob Pike87334f42013-05-17 14:02:47 -0700262// Pos() returns the declaring ast.FuncLit.Type.Func or the position
263// of the ast.FuncDecl.Name, if the function was explicit in the
Alan Donovan1fa3f782013-07-03 17:57:20 -0400264// source. Synthetic wrappers, for which Synthetic != "", may share
265// the same position as the function they wrap.
Rob Pike87334f42013-05-17 14:02:47 -0700266//
Rob Pike83f21b92013-05-17 13:25:48 -0700267// Type() returns the function's Signature.
268//
269type Function struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400270 name string
Robert Griesemer64ea46e2013-07-26 22:27:48 -0700271 object types.Object // a declared *types.Func; nil for init, wrappers, etc.
272 method *types.Selection // info about provenance of synthetic methods [currently unused]
Rob Pike83f21b92013-05-17 13:25:48 -0700273 Signature *types.Signature
Rob Pike87334f42013-05-17 14:02:47 -0700274 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400275
Alan Donovan1fa3f782013-07-03 17:57:20 -0400276 Synthetic string // provenance of synthetic function; "" for true source functions
Alan Donovanaa238622013-10-27 10:55:21 -0400277 syntax ast.Node // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode
Rob Pike83f21b92013-05-17 13:25:48 -0700278 Enclosing *Function // enclosing function if anon; nil if global
Alan Donovan87ced822013-10-23 17:07:52 -0400279 Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error)
Rob Pike83f21b92013-05-17 13:25:48 -0700280 Prog *Program // enclosing program
281 Params []*Parameter // function parameters; for methods, includes receiver
282 FreeVars []*Capture // free variables whose values must be supplied by closure
283 Locals []*Alloc
284 Blocks []*BasicBlock // basic blocks of the function; nil => external
Alan Donovan2accef22013-10-14 15:38:56 -0400285 Recover *BasicBlock // optional; control transfers here after recovered panic
Rob Pike83f21b92013-05-17 13:25:48 -0700286 AnonFuncs []*Function // anonymous functions directly beneath this one
Alan Donovanf1e5b032013-11-07 10:08:51 -0500287 referrers []Instruction // referring instructions (iff Enclosing != nil)
Rob Pike83f21b92013-05-17 13:25:48 -0700288
289 // The following fields are set transiently during building,
290 // then cleared.
291 currentBlock *BasicBlock // where to emit code
292 objects map[types.Object]Value // addresses of local variables
293 namedResults []*Alloc // tuple of named results
Rob Pike83f21b92013-05-17 13:25:48 -0700294 targets *targets // linked stack of branch targets
295 lblocks map[*ast.Object]*lblock // labelled blocks
296}
297
298// An SSA basic block.
299//
300// The final element of Instrs is always an explicit transfer of
Alan Donovan068f0172013-10-08 12:31:39 -0400301// control (If, Jump, Return or Panic).
Rob Pike83f21b92013-05-17 13:25:48 -0700302//
303// A block may contain no Instructions only if it is unreachable,
304// i.e. Preds is nil. Empty blocks are typically pruned.
305//
306// BasicBlocks and their Preds/Succs relation form a (possibly cyclic)
Alan Donovanb5016cb2013-12-05 09:50:18 -0500307// graph independent of the SSA Value graph: the control-flow graph or
308// CFG. It is illegal for multiple edges to exist between the same
309// pair of blocks.
310//
311// Each BasicBlock is also a node in the dominator tree of the CFG.
312// The tree may be navigated using Idom()/Dominees() and queried using
313// Dominates().
Rob Pike83f21b92013-05-17 13:25:48 -0700314//
315// The order of Preds and Succs are significant (to Phi and If
316// instructions, respectively).
317//
318type BasicBlock struct {
319 Index int // index of this block within Func.Blocks
320 Comment string // optional label; no semantic significance
Alan Donovan341a07a2013-06-13 14:43:35 -0400321 parent *Function // parent function
Rob Pike83f21b92013-05-17 13:25:48 -0700322 Instrs []Instruction // instructions in order
323 Preds, Succs []*BasicBlock // predecessors and successors
324 succs2 [2]*BasicBlock // initial space for Succs.
Alan Donovanb5016cb2013-12-05 09:50:18 -0500325 dom domInfo // dominator tree info
Rob Pike83f21b92013-05-17 13:25:48 -0700326 gaps int // number of nil Instrs (transient).
327 rundefers int // number of rundefers (transient)
328}
329
330// Pure values ----------------------------------------
331
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400332// A Capture represents a free variable of the function to which it
333// belongs.
Rob Pike83f21b92013-05-17 13:25:48 -0700334//
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400335// Captures are used to implement anonymous functions, whose free
336// variables are lexically captured in a closure formed by
337// MakeClosure. The referent of such a capture is an Alloc or another
338// Capture and is considered a potentially escaping heap address, with
339// pointer type.
340//
341// Captures are also used to implement bound method closures. Such a
342// capture represents the receiver value and may be of any type that
343// has concrete methods.
Rob Pike83f21b92013-05-17 13:25:48 -0700344//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400345// Pos() returns the position of the value that was captured, which
346// belongs to an enclosing function.
347//
Rob Pike83f21b92013-05-17 13:25:48 -0700348type Capture struct {
Alan Donovan341a07a2013-06-13 14:43:35 -0400349 name string
350 typ types.Type
351 pos token.Pos
352 parent *Function
Rob Pike83f21b92013-05-17 13:25:48 -0700353 referrers []Instruction
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400354
355 // Transiently needed during building.
356 outer Value // the Value captured from the enclosing context.
Rob Pike83f21b92013-05-17 13:25:48 -0700357}
358
359// A Parameter represents an input parameter of a function.
360//
361type Parameter struct {
Alan Donovan341a07a2013-06-13 14:43:35 -0400362 name string
Alan Donovan55d678e2013-07-15 13:56:46 -0400363 object types.Object // a *types.Var; nil for non-source locals
Alan Donovan341a07a2013-06-13 14:43:35 -0400364 typ types.Type
365 pos token.Pos
366 parent *Function
Rob Pike83f21b92013-05-17 13:25:48 -0700367 referrers []Instruction
368}
369
Alan Donovan732dbe92013-07-16 13:50:08 -0400370// A Const represents the value of a constant expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700371//
Rob Pike87334f42013-05-17 14:02:47 -0700372// It may have a nil, boolean, string or numeric (integer, fraction or
373// complex) value, or a []byte or []rune conversion of a string
Alan Donovan732dbe92013-07-16 13:50:08 -0400374// constant.
Rob Pike87334f42013-05-17 14:02:47 -0700375//
Alan Donovan732dbe92013-07-16 13:50:08 -0400376// Consts may be of named types. A constant's underlying type can be
Rob Pike87334f42013-05-17 14:02:47 -0700377// a basic type, possibly one of the "untyped" types, or a slice type
Alan Donovan732dbe92013-07-16 13:50:08 -0400378// whose elements' underlying type is byte or rune. A nil constant can
Rob Pike87334f42013-05-17 14:02:47 -0700379// have any reference type: interface, map, channel, pointer, slice,
380// or function---but not "untyped nil".
Rob Pike83f21b92013-05-17 13:25:48 -0700381//
Alan Donovan732dbe92013-07-16 13:50:08 -0400382// All source-level constant expressions are represented by a Const
Rob Pike83f21b92013-05-17 13:25:48 -0700383// of equal type and value.
384//
Alan Donovan732dbe92013-07-16 13:50:08 -0400385// Value holds the exact value of the constant, independent of its
Rob Pike87334f42013-05-17 14:02:47 -0700386// Type(), using the same representation as package go/exact uses for
Robert Griesemerf50f6c82013-10-09 14:17:25 -0700387// constants, or nil for a typed nil value.
Rob Pike83f21b92013-05-17 13:25:48 -0700388//
Alan Donovana399e262013-07-15 16:10:08 -0400389// Pos() returns token.NoPos.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400390//
Rob Pike83f21b92013-05-17 13:25:48 -0700391// Example printed form:
392// 42:int
393// "hello":untyped string
394// 3+4i:MyComplex
395//
Alan Donovan732dbe92013-07-16 13:50:08 -0400396type Const struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400397 typ types.Type
Rob Pike83f21b92013-05-17 13:25:48 -0700398 Value exact.Value
399}
400
401// A Global is a named Value holding the address of a package-level
402// variable.
403//
Rob Pike87334f42013-05-17 14:02:47 -0700404// Pos() returns the position of the ast.ValueSpec.Names[*]
405// identifier.
406//
Rob Pike83f21b92013-05-17 13:25:48 -0700407type Global struct {
Alan Donovanbc1f7242013-07-11 14:12:30 -0400408 name string
409 object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
410 typ types.Type
411 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400412
413 Pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -0700414}
415
Rob Pike87334f42013-05-17 14:02:47 -0700416// A Builtin represents a built-in function, e.g. len.
Rob Pike83f21b92013-05-17 13:25:48 -0700417//
Rob Pike87334f42013-05-17 14:02:47 -0700418// Builtins are immutable values. Builtins do not have addresses.
Alan Donovan55d678e2013-07-15 13:56:46 -0400419// Builtins can only appear in CallCommon.Func.
Rob Pike83f21b92013-05-17 13:25:48 -0700420//
Alan Donovan318b83e2013-09-23 18:18:35 -0400421// Object() returns a *types.Builtin.
422//
423// Type() returns types.Typ[types.Invalid], since built-in functions
424// may have polymorphic or variadic types that are not expressible in
425// Go's type system.
Rob Pike83f21b92013-05-17 13:25:48 -0700426//
427type Builtin struct {
Alan Donovan318b83e2013-09-23 18:18:35 -0400428 object *types.Builtin // canonical types.Universe object for this built-in
Rob Pike83f21b92013-05-17 13:25:48 -0700429}
430
431// Value-defining instructions ----------------------------------------
432
433// The Alloc instruction reserves space for a value of the given type,
434// zero-initializes it, and yields its address.
435//
436// Alloc values are always addresses, and have pointer types, so the
437// type of the allocated space is actually indirect(Type()).
438//
439// If Heap is false, Alloc allocates space in the function's
440// activation record (frame); we refer to an Alloc(Heap=false) as a
441// "local" alloc. Each local Alloc returns the same address each time
442// it is executed within the same activation; the space is
443// re-initialized to zero.
444//
445// If Heap is true, Alloc allocates space in the heap, and returns; we
446// refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc
447// returns a different address each time it is executed.
448//
449// When Alloc is applied to a channel, map or slice type, it returns
450// the address of an uninitialized (nil) reference of that kind; store
451// the result of MakeSlice, MakeMap or MakeChan in that location to
452// instantiate these types.
453//
Rob Pike87334f42013-05-17 14:02:47 -0700454// Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
455// or the ast.CallExpr.Lparen for a call to new() or for a call that
456// allocates a varargs slice.
457//
Rob Pike83f21b92013-05-17 13:25:48 -0700458// Example printed form:
459// t0 = local int
460// t1 = new int
461//
462type Alloc struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400463 register
Alan Donovan5cc33ea2013-08-01 14:06:10 -0400464 Comment string
465 Heap bool
466 index int // dense numbering; for lifting
Rob Pike83f21b92013-05-17 13:25:48 -0700467}
468
Rob Pike87334f42013-05-17 14:02:47 -0700469// The Phi instruction represents an SSA φ-node, which combines values
470// that differ across incoming control-flow edges and yields a new
471// value. Within a block, all φ-nodes must appear before all non-φ
472// nodes.
473//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400474// Pos() returns the position of the && or || for short-circuit
475// control-flow joins, or that of the *Alloc for φ-nodes inserted
476// during SSA renaming.
Rob Pike83f21b92013-05-17 13:25:48 -0700477//
478// Example printed form:
479// t2 = phi [0.start: t0, 1.if.then: t1, ...]
480//
481type Phi struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400482 register
Rob Pike83f21b92013-05-17 13:25:48 -0700483 Comment string // a hint as to its purpose
484 Edges []Value // Edges[i] is value for Block().Preds[i]
485}
486
Rob Pike87334f42013-05-17 14:02:47 -0700487// The Call instruction represents a function or method call.
Rob Pike83f21b92013-05-17 13:25:48 -0700488//
489// The Call instruction yields the function result, if there is
490// exactly one, or a tuple (empty or len>1) whose components are
491// accessed via Extract.
492//
493// See CallCommon for generic function call documentation.
494//
Rob Pike87334f42013-05-17 14:02:47 -0700495// Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
496//
Rob Pike83f21b92013-05-17 13:25:48 -0700497// Example printed form:
498// t2 = println(t0, t1)
499// t4 = t3()
500// t7 = invoke t5.Println(...t6)
501//
502type Call struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400503 register
Rob Pike83f21b92013-05-17 13:25:48 -0700504 Call CallCommon
505}
506
Rob Pike87334f42013-05-17 14:02:47 -0700507// The BinOp instruction yields the result of binary operation X Op Y.
508//
509// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700510//
511// Example printed form:
512// t1 = t0 + 1:int
513//
514type BinOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400515 register
Rob Pike83f21b92013-05-17 13:25:48 -0700516 // One of:
517 // ADD SUB MUL QUO REM + - * / %
518 // AND OR XOR SHL SHR AND_NOT & | ^ << >> &~
519 // EQL LSS GTR NEQ LEQ GEQ == != < <= < >=
520 Op token.Token
521 X, Y Value
522}
523
Rob Pike87334f42013-05-17 14:02:47 -0700524// The UnOp instruction yields the result of Op X.
Rob Pike83f21b92013-05-17 13:25:48 -0700525// ARROW is channel receive.
526// MUL is pointer indirection (load).
527// XOR is bitwise complement.
528// SUB is negation.
529//
530// If CommaOk and Op=ARROW, the result is a 2-tuple of the value above
531// and a boolean indicating the success of the receive. The
532// components of the tuple are accessed using Extract.
533//
Alan Donovande47eba2013-08-27 11:18:31 -0400534// Pos() returns the ast.UnaryExpr.OpPos or ast.RangeStmt.TokPos (for
535// ranging over a channel), if explicit in the source.
Rob Pike87334f42013-05-17 14:02:47 -0700536//
Rob Pike83f21b92013-05-17 13:25:48 -0700537// Example printed form:
538// t0 = *x
539// t2 = <-t1,ok
540//
541type UnOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400542 register
Rob Pike83f21b92013-05-17 13:25:48 -0700543 Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^
544 X Value
545 CommaOk bool
546}
547
Rob Pike87334f42013-05-17 14:02:47 -0700548// The ChangeType instruction applies to X a value-preserving type
549// change to Type().
Rob Pike83f21b92013-05-17 13:25:48 -0700550//
Rob Pike87334f42013-05-17 14:02:47 -0700551// Type changes are permitted:
552// - between a named type and its underlying type.
553// - between two named types of the same underlying type.
554// - between (possibly named) pointers to identical base types.
555// - between f(T) functions and (T) func f() methods.
556// - from a bidirectional channel to a read- or write-channel,
557// optionally adding/removing a name.
Rob Pike83f21b92013-05-17 13:25:48 -0700558//
Rob Pike87334f42013-05-17 14:02:47 -0700559// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700560//
Rob Pike87334f42013-05-17 14:02:47 -0700561// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
562// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700563//
Rob Pike87334f42013-05-17 14:02:47 -0700564// Example printed form:
565// t1 = changetype *int <- IntPtr (t0)
566//
567type ChangeType struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400568 register
Rob Pike87334f42013-05-17 14:02:47 -0700569 X Value
570}
571
572// The Convert instruction yields the conversion of value X to type
Alan Donovan8097dad2013-06-24 14:15:13 -0400573// Type(). One or both of those types is basic (but possibly named).
Rob Pike87334f42013-05-17 14:02:47 -0700574//
575// A conversion may change the value and representation of its operand.
576// Conversions are permitted:
577// - between real numeric types.
578// - between complex numeric types.
579// - between string and []byte or []rune.
Alan Donovan8097dad2013-06-24 14:15:13 -0400580// - between pointers and unsafe.Pointer.
581// - between unsafe.Pointer and uintptr.
Rob Pike87334f42013-05-17 14:02:47 -0700582// - from (Unicode) integer to (UTF-8) string.
583// A conversion may imply a type name change also.
584//
585// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700586//
587// Conversions of untyped string/number/bool constants to a specific
588// representation are eliminated during SSA construction.
589//
Rob Pike87334f42013-05-17 14:02:47 -0700590// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
591// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700592//
Rob Pike87334f42013-05-17 14:02:47 -0700593// Example printed form:
594// t1 = convert []byte <- string (t0)
595//
596type Convert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400597 register
Rob Pike83f21b92013-05-17 13:25:48 -0700598 X Value
599}
600
601// ChangeInterface constructs a value of one interface type from a
602// value of another interface type known to be assignable to it.
Alan Donovanae801632013-07-26 21:49:27 -0400603// This operation cannot fail.
Rob Pike87334f42013-05-17 14:02:47 -0700604//
Alan Donovan341a07a2013-06-13 14:43:35 -0400605// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
606// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
607// instruction arose from an explicit e.(T) operation; or token.NoPos
608// otherwise.
609//
Rob Pike83f21b92013-05-17 13:25:48 -0700610// Example printed form:
611// t1 = change interface interface{} <- I (t0)
612//
613type ChangeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400614 register
Rob Pike83f21b92013-05-17 13:25:48 -0700615 X Value
616}
617
618// MakeInterface constructs an instance of an interface type from a
Alan Donovan341a07a2013-06-13 14:43:35 -0400619// value of a concrete type.
620//
Alan Donovan2a3a1292013-07-30 14:28:14 -0400621// Use X.Type().MethodSet() to find the method-set of X, and
Alan Donovan2f6855a2013-07-30 16:36:58 -0400622// Program.Method(m) to find the implementation of a method.
Rob Pike83f21b92013-05-17 13:25:48 -0700623//
624// To construct the zero value of an interface type T, use:
Alan Donovan732dbe92013-07-16 13:50:08 -0400625// NewConst(exact.MakeNil(), T, pos)
Rob Pike87334f42013-05-17 14:02:47 -0700626//
627// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
628// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700629//
630// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400631// t1 = make interface{} <- int (42:int)
632// t2 = make Stringer <- t0
Rob Pike83f21b92013-05-17 13:25:48 -0700633//
634type MakeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400635 register
Alan Donovan341a07a2013-06-13 14:43:35 -0400636 X Value
Rob Pike83f21b92013-05-17 13:25:48 -0700637}
638
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400639// The MakeClosure instruction yields a closure value whose code is
640// Fn and whose free variables' values are supplied by Bindings.
Rob Pike83f21b92013-05-17 13:25:48 -0700641//
642// Type() returns a (possibly named) *types.Signature.
643//
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400644// Pos() returns the ast.FuncLit.Type.Func for a function literal
645// closure or the ast.SelectorExpr.Sel for a bound method closure.
Rob Pike87334f42013-05-17 14:02:47 -0700646//
Rob Pike83f21b92013-05-17 13:25:48 -0700647// Example printed form:
648// t0 = make closure anon@1.2 [x y z]
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400649// t1 = make closure bound$(main.I).add [i]
Rob Pike83f21b92013-05-17 13:25:48 -0700650//
651type MakeClosure struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400652 register
Rob Pike83f21b92013-05-17 13:25:48 -0700653 Fn Value // always a *Function
654 Bindings []Value // values for each free variable in Fn.FreeVars
655}
656
657// The MakeMap instruction creates a new hash-table-based map object
658// and yields a value of kind map.
659//
660// Type() returns a (possibly named) *types.Map.
661//
Rob Pike87334f42013-05-17 14:02:47 -0700662// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
663// the ast.CompositeLit.Lbrack if created by a literal.
664//
Rob Pike83f21b92013-05-17 13:25:48 -0700665// Example printed form:
666// t1 = make map[string]int t0
Alan Donovan341a07a2013-06-13 14:43:35 -0400667// t1 = make StringIntMap t0
Rob Pike83f21b92013-05-17 13:25:48 -0700668//
669type MakeMap struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400670 register
Rob Pike83f21b92013-05-17 13:25:48 -0700671 Reserve Value // initial space reservation; nil => default
Rob Pike83f21b92013-05-17 13:25:48 -0700672}
673
674// The MakeChan instruction creates a new channel object and yields a
675// value of kind chan.
676//
677// Type() returns a (possibly named) *types.Chan.
678//
Rob Pike87334f42013-05-17 14:02:47 -0700679// Pos() returns the ast.CallExpr.Lparen for the make(chan) that
680// created it.
681//
Rob Pike83f21b92013-05-17 13:25:48 -0700682// Example printed form:
683// t0 = make chan int 0
Alan Donovan341a07a2013-06-13 14:43:35 -0400684// t0 = make IntChan 0
Rob Pike83f21b92013-05-17 13:25:48 -0700685//
686type MakeChan struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400687 register
Rob Pike83f21b92013-05-17 13:25:48 -0700688 Size Value // int; size of buffer; zero => synchronous.
Rob Pike83f21b92013-05-17 13:25:48 -0700689}
690
Rob Pike87334f42013-05-17 14:02:47 -0700691// The MakeSlice instruction yields a slice of length Len backed by a
692// newly allocated array of length Cap.
Rob Pike83f21b92013-05-17 13:25:48 -0700693//
694// Both Len and Cap must be non-nil Values of integer type.
695//
696// (Alloc(types.Array) followed by Slice will not suffice because
697// Alloc can only create arrays of statically known length.)
698//
699// Type() returns a (possibly named) *types.Slice.
700//
Rob Pike87334f42013-05-17 14:02:47 -0700701// Pos() returns the ast.CallExpr.Lparen for the make([]T) that
702// created it.
703//
Rob Pike83f21b92013-05-17 13:25:48 -0700704// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400705// t1 = make []string 1:int t0
706// t1 = make StringSlice 1:int t0
Rob Pike83f21b92013-05-17 13:25:48 -0700707//
708type MakeSlice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400709 register
Rob Pike83f21b92013-05-17 13:25:48 -0700710 Len Value
711 Cap Value
Rob Pike83f21b92013-05-17 13:25:48 -0700712}
713
Rob Pike87334f42013-05-17 14:02:47 -0700714// The Slice instruction yields a slice of an existing string, slice
715// or *array X between optional integer bounds Low and High.
Rob Pike83f21b92013-05-17 13:25:48 -0700716//
Alan Donovan70722532013-08-19 15:38:30 -0400717// Dynamically, this instruction panics if X evaluates to a nil *array
718// pointer.
719//
Rob Pike83f21b92013-05-17 13:25:48 -0700720// Type() returns string if the type of X was string, otherwise a
721// *types.Slice with the same element type as X.
722//
Rob Pike87334f42013-05-17 14:02:47 -0700723// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
724// operation, the ast.CompositeLit.Lbrace if created by a literal, or
725// NoPos if not explicit in the source (e.g. a variadic argument slice).
726//
Rob Pike83f21b92013-05-17 13:25:48 -0700727// Example printed form:
728// t1 = slice t0[1:]
729//
730type Slice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400731 register
Rob Pike83f21b92013-05-17 13:25:48 -0700732 X Value // slice, string, or *array
733 Low, High Value // either may be nil
734}
735
Rob Pike87334f42013-05-17 14:02:47 -0700736// The FieldAddr instruction yields the address of Field of *struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700737//
738// The field is identified by its index within the field list of the
739// struct type of X.
740//
Alan Donovan70722532013-08-19 15:38:30 -0400741// Dynamically, this instruction panics if X evaluates to a nil
742// pointer.
743//
Rob Pike83f21b92013-05-17 13:25:48 -0700744// Type() returns a (possibly named) *types.Pointer.
745//
Rob Pike87334f42013-05-17 14:02:47 -0700746// Pos() returns the position of the ast.SelectorExpr.Sel for the
747// field, if explicit in the source.
748//
Rob Pike83f21b92013-05-17 13:25:48 -0700749// Example printed form:
750// t1 = &t0.name [#1]
751//
752type FieldAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400753 register
Rob Pike83f21b92013-05-17 13:25:48 -0700754 X Value // *struct
Alan Donovan8097dad2013-06-24 14:15:13 -0400755 Field int // index into X.Type().Deref().(*types.Struct).Fields
Rob Pike83f21b92013-05-17 13:25:48 -0700756}
757
Rob Pike87334f42013-05-17 14:02:47 -0700758// The Field instruction yields the Field of struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700759//
760// The field is identified by its index within the field list of the
761// struct type of X; by using numeric indices we avoid ambiguity of
762// package-local identifiers and permit compact representations.
763//
Rob Pike87334f42013-05-17 14:02:47 -0700764// Pos() returns the position of the ast.SelectorExpr.Sel for the
765// field, if explicit in the source.
766//
Rob Pike83f21b92013-05-17 13:25:48 -0700767// Example printed form:
768// t1 = t0.name [#1]
769//
770type Field struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400771 register
Rob Pike83f21b92013-05-17 13:25:48 -0700772 X Value // struct
773 Field int // index into X.Type().(*types.Struct).Fields
774}
775
Rob Pike87334f42013-05-17 14:02:47 -0700776// The IndexAddr instruction yields the address of the element at
777// index Index of collection X. Index is an integer expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700778//
779// The elements of maps and strings are not addressable; use Lookup or
780// MapUpdate instead.
781//
Alan Donovan70722532013-08-19 15:38:30 -0400782// Dynamically, this instruction panics if X evaluates to a nil *array
783// pointer.
784//
Rob Pike83f21b92013-05-17 13:25:48 -0700785// Type() returns a (possibly named) *types.Pointer.
786//
Rob Pike87334f42013-05-17 14:02:47 -0700787// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
788// explicit in the source.
789//
Rob Pike83f21b92013-05-17 13:25:48 -0700790// Example printed form:
791// t2 = &t0[t1]
792//
793type IndexAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400794 register
Rob Pike83f21b92013-05-17 13:25:48 -0700795 X Value // slice or *array,
796 Index Value // numeric index
797}
798
Rob Pike87334f42013-05-17 14:02:47 -0700799// The Index instruction yields element Index of array X.
800//
801// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
802// explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700803//
804// Example printed form:
805// t2 = t0[t1]
806//
807type Index struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400808 register
Rob Pike83f21b92013-05-17 13:25:48 -0700809 X Value // array
810 Index Value // integer index
811}
812
Rob Pike87334f42013-05-17 14:02:47 -0700813// The Lookup instruction yields element Index of collection X, a map
814// or string. Index is an integer expression if X is a string or the
815// appropriate key type if X is a map.
Rob Pike83f21b92013-05-17 13:25:48 -0700816//
817// If CommaOk, the result is a 2-tuple of the value above and a
818// boolean indicating the result of a map membership test for the key.
819// The components of the tuple are accessed using Extract.
820//
Rob Pike87334f42013-05-17 14:02:47 -0700821// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
822//
Rob Pike83f21b92013-05-17 13:25:48 -0700823// Example printed form:
824// t2 = t0[t1]
825// t5 = t3[t4],ok
826//
827type Lookup struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400828 register
Rob Pike83f21b92013-05-17 13:25:48 -0700829 X Value // string or map
830 Index Value // numeric or key-typed index
831 CommaOk bool // return a value,ok pair
832}
833
834// SelectState is a helper for Select.
835// It represents one goal state and its corresponding communication.
836//
837type SelectState struct {
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400838 Dir ast.ChanDir // direction of case
839 Chan Value // channel to use (for send or receive)
840 Send Value // value to send (for send)
841 Pos token.Pos // position of token.ARROW
842 DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
Rob Pike83f21b92013-05-17 13:25:48 -0700843}
844
Rob Pike87334f42013-05-17 14:02:47 -0700845// The Select instruction tests whether (or blocks until) one or more
846// of the specified sent or received states is entered.
Rob Pike83f21b92013-05-17 13:25:48 -0700847//
Alan Donovan8097dad2013-06-24 14:15:13 -0400848// Let n be the number of States for which Dir==RECV and T_i (0<=i<n)
849// be the element type of each such state's Chan.
850// Select returns an n+2-tuple
851// (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)
852// The tuple's components, described below, must be accessed via the
853// Extract instruction.
Rob Pike83f21b92013-05-17 13:25:48 -0700854//
855// If Blocking, select waits until exactly one state holds, i.e. a
856// channel becomes ready for the designated operation of sending or
857// receiving; select chooses one among the ready states
858// pseudorandomly, performs the send or receive operation, and sets
859// 'index' to the index of the chosen channel.
860//
861// If !Blocking, select doesn't block if no states hold; instead it
862// returns immediately with index equal to -1.
863//
Alan Donovan8097dad2013-06-24 14:15:13 -0400864// If the chosen channel was used for a receive, the r_i component is
865// set to the received value, where i is the index of that state among
866// all n receive states; otherwise r_i has the zero value of type T_i.
867// Note that the the receive index i is not the same as the state
868// index index.
Rob Pike83f21b92013-05-17 13:25:48 -0700869//
Alan Donovan8097dad2013-06-24 14:15:13 -0400870// The second component of the triple, recvOk, is a boolean whose value
Rob Pike83f21b92013-05-17 13:25:48 -0700871// is true iff the selected operation was a receive and the receive
872// successfully yielded a value.
873//
Rob Pike87334f42013-05-17 14:02:47 -0700874// Pos() returns the ast.SelectStmt.Select.
875//
Rob Pike83f21b92013-05-17 13:25:48 -0700876// Example printed form:
877// t3 = select nonblocking [<-t0, t1<-t2, ...]
878// t4 = select blocking []
879//
880type Select struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400881 register
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400882 States []*SelectState
Rob Pike83f21b92013-05-17 13:25:48 -0700883 Blocking bool
884}
885
Rob Pike87334f42013-05-17 14:02:47 -0700886// The Range instruction yields an iterator over the domain and range
887// of X, which must be a string or map.
Rob Pike83f21b92013-05-17 13:25:48 -0700888//
889// Elements are accessed via Next.
890//
Alan Donovan8097dad2013-06-24 14:15:13 -0400891// Type() returns an opaque and degenerate "rangeIter" type.
Rob Pike83f21b92013-05-17 13:25:48 -0700892//
Rob Pike87334f42013-05-17 14:02:47 -0700893// Pos() returns the ast.RangeStmt.For.
894//
Rob Pike83f21b92013-05-17 13:25:48 -0700895// Example printed form:
896// t0 = range "hello":string
897//
898type Range struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400899 register
Rob Pike83f21b92013-05-17 13:25:48 -0700900 X Value // string or map
901}
902
Rob Pike87334f42013-05-17 14:02:47 -0700903// The Next instruction reads and advances the (map or string)
904// iterator Iter and returns a 3-tuple value (ok, k, v). If the
905// iterator is not exhausted, ok is true and k and v are the next
906// elements of the domain and range, respectively. Otherwise ok is
907// false and k and v are undefined.
Rob Pike83f21b92013-05-17 13:25:48 -0700908//
909// Components of the tuple are accessed using Extract.
910//
911// The IsString field distinguishes iterators over strings from those
912// over maps, as the Type() alone is insufficient: consider
913// map[int]rune.
914//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400915// Type() returns a *types.Tuple for the triple (ok, k, v).
916// The types of k and/or v may be types.Invalid.
Rob Pike83f21b92013-05-17 13:25:48 -0700917//
918// Example printed form:
919// t1 = next t0
920//
921type Next struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400922 register
Rob Pike83f21b92013-05-17 13:25:48 -0700923 Iter Value
924 IsString bool // true => string iterator; false => map iterator.
925}
926
Rob Pike87334f42013-05-17 14:02:47 -0700927// The TypeAssert instruction tests whether interface value X has type
928// AssertedType.
Rob Pike83f21b92013-05-17 13:25:48 -0700929//
930// If !CommaOk, on success it returns v, the result of the conversion
931// (defined below); on failure it panics.
932//
933// If CommaOk: on success it returns a pair (v, true) where v is the
934// result of the conversion; on failure it returns (z, false) where z
935// is AssertedType's zero value. The components of the pair must be
936// accessed using the Extract instruction.
937//
938// If AssertedType is a concrete type, TypeAssert checks whether the
939// dynamic type in interface X is equal to it, and if so, the result
940// of the conversion is a copy of the value in the interface.
941//
942// If AssertedType is an interface, TypeAssert checks whether the
943// dynamic type of the interface is assignable to it, and if so, the
944// result of the conversion is a copy of the interface value X.
Alan Donovanae801632013-07-26 21:49:27 -0400945// If AssertedType is a superinterface of X.Type(), the operation will
946// fail iff the operand is nil. (Contrast with ChangeInterface, which
947// performs no nil-check.)
Rob Pike83f21b92013-05-17 13:25:48 -0700948//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400949// Type() reflects the actual type of the result, possibly a
950// 2-types.Tuple; AssertedType is the asserted type.
Rob Pike83f21b92013-05-17 13:25:48 -0700951//
Alan Donovan341a07a2013-06-13 14:43:35 -0400952// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
953// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400954// instruction arose from an explicit e.(T) operation; or the
955// ast.CaseClause.Case if the instruction arose from a case of a
956// type-switch statement.
Alan Donovan341a07a2013-06-13 14:43:35 -0400957//
Rob Pike83f21b92013-05-17 13:25:48 -0700958// Example printed form:
959// t1 = typeassert t0.(int)
960// t3 = typeassert,ok t2.(T)
961//
962type TypeAssert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400963 register
Rob Pike83f21b92013-05-17 13:25:48 -0700964 X Value
965 AssertedType types.Type
966 CommaOk bool
967}
968
Rob Pike87334f42013-05-17 14:02:47 -0700969// The Extract instruction yields component Index of Tuple.
Rob Pike83f21b92013-05-17 13:25:48 -0700970//
971// This is used to access the results of instructions with multiple
972// return values, such as Call, TypeAssert, Next, UnOp(ARROW) and
973// IndexExpr(Map).
974//
975// Example printed form:
976// t1 = extract t0 #1
977//
978type Extract struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400979 register
Rob Pike83f21b92013-05-17 13:25:48 -0700980 Tuple Value
981 Index int
982}
983
984// Instructions executed for effect. They do not yield a value. --------------------
985
Rob Pike87334f42013-05-17 14:02:47 -0700986// The Jump instruction transfers control to the sole successor of its
987// owning block.
Rob Pike83f21b92013-05-17 13:25:48 -0700988//
Rob Pike87334f42013-05-17 14:02:47 -0700989// A Jump must be the last instruction of its containing BasicBlock.
990//
991// Pos() returns NoPos.
Rob Pike83f21b92013-05-17 13:25:48 -0700992//
993// Example printed form:
994// jump done
995//
996type Jump struct {
997 anInstruction
998}
999
1000// The If instruction transfers control to one of the two successors
1001// of its owning block, depending on the boolean Cond: the first if
1002// true, the second if false.
1003//
1004// An If instruction must be the last instruction of its containing
1005// BasicBlock.
1006//
Rob Pike87334f42013-05-17 14:02:47 -07001007// Pos() returns NoPos.
1008//
Rob Pike83f21b92013-05-17 13:25:48 -07001009// Example printed form:
1010// if t0 goto done else body
1011//
1012type If struct {
1013 anInstruction
1014 Cond Value
1015}
1016
Alan Donovan068f0172013-10-08 12:31:39 -04001017// The Return instruction returns values and control back to the calling
Rob Pike87334f42013-05-17 14:02:47 -07001018// function.
Rob Pike83f21b92013-05-17 13:25:48 -07001019//
1020// len(Results) is always equal to the number of results in the
1021// function's signature.
1022//
Alan Donovan068f0172013-10-08 12:31:39 -04001023// If len(Results) > 1, Return returns a tuple value with the specified
Rob Pike83f21b92013-05-17 13:25:48 -07001024// components which the caller must access using Extract instructions.
1025//
1026// There is no instruction to return a ready-made tuple like those
1027// returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or
1028// a tail-call to a function with multiple result parameters.
1029//
Alan Donovan068f0172013-10-08 12:31:39 -04001030// Return must be the last instruction of its containing BasicBlock.
Rob Pike83f21b92013-05-17 13:25:48 -07001031// Such a block has no successors.
1032//
Rob Pike87334f42013-05-17 14:02:47 -07001033// Pos() returns the ast.ReturnStmt.Return, if explicit in the source.
1034//
Rob Pike83f21b92013-05-17 13:25:48 -07001035// Example printed form:
Alan Donovan068f0172013-10-08 12:31:39 -04001036// return
1037// return nil:I, 2:int
Rob Pike83f21b92013-05-17 13:25:48 -07001038//
Alan Donovan068f0172013-10-08 12:31:39 -04001039type Return struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001040 anInstruction
1041 Results []Value
Rob Pike87334f42013-05-17 14:02:47 -07001042 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001043}
1044
Rob Pike87334f42013-05-17 14:02:47 -07001045// The RunDefers instruction pops and invokes the entire stack of
1046// procedure calls pushed by Defer instructions in this function.
Rob Pike83f21b92013-05-17 13:25:48 -07001047//
1048// It is legal to encounter multiple 'rundefers' instructions in a
1049// single control-flow path through a function; this is useful in
1050// the combined init() function, for example.
1051//
Rob Pike87334f42013-05-17 14:02:47 -07001052// Pos() returns NoPos.
1053//
Rob Pike83f21b92013-05-17 13:25:48 -07001054// Example printed form:
1055// rundefers
1056//
1057type RunDefers struct {
1058 anInstruction
1059}
1060
Rob Pike87334f42013-05-17 14:02:47 -07001061// The Panic instruction initiates a panic with value X.
Rob Pike83f21b92013-05-17 13:25:48 -07001062//
1063// A Panic instruction must be the last instruction of its containing
1064// BasicBlock, which must have no successors.
1065//
1066// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction;
1067// they are treated as calls to a built-in function.
1068//
Rob Pike87334f42013-05-17 14:02:47 -07001069// Pos() returns the ast.CallExpr.Lparen if this panic was explicit
1070// in the source.
1071//
Rob Pike83f21b92013-05-17 13:25:48 -07001072// Example printed form:
1073// panic t0
1074//
1075type Panic struct {
1076 anInstruction
Rob Pike87334f42013-05-17 14:02:47 -07001077 X Value // an interface{}
1078 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001079}
1080
Rob Pike87334f42013-05-17 14:02:47 -07001081// The Go instruction creates a new goroutine and calls the specified
1082// function within it.
Rob Pike83f21b92013-05-17 13:25:48 -07001083//
1084// See CallCommon for generic function call documentation.
1085//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001086// Pos() returns the ast.GoStmt.Go.
1087//
Rob Pike83f21b92013-05-17 13:25:48 -07001088// Example printed form:
1089// go println(t0, t1)
1090// go t3()
1091// go invoke t5.Println(...t6)
1092//
1093type Go struct {
1094 anInstruction
1095 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001096 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001097}
1098
Rob Pike87334f42013-05-17 14:02:47 -07001099// The Defer instruction pushes the specified call onto a stack of
1100// functions to be called by a RunDefers instruction or by a panic.
Rob Pike83f21b92013-05-17 13:25:48 -07001101//
1102// See CallCommon for generic function call documentation.
1103//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001104// Pos() returns the ast.DeferStmt.Defer.
1105//
Rob Pike83f21b92013-05-17 13:25:48 -07001106// Example printed form:
1107// defer println(t0, t1)
1108// defer t3()
1109// defer invoke t5.Println(...t6)
1110//
1111type Defer struct {
1112 anInstruction
1113 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001114 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001115}
1116
Rob Pike87334f42013-05-17 14:02:47 -07001117// The Send instruction sends X on channel Chan.
1118//
1119// Pos() returns the ast.SendStmt.Arrow, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001120//
1121// Example printed form:
1122// send t0 <- t1
1123//
1124type Send struct {
1125 anInstruction
1126 Chan, X Value
Rob Pike87334f42013-05-17 14:02:47 -07001127 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001128}
1129
Rob Pike87334f42013-05-17 14:02:47 -07001130// The Store instruction stores Val at address Addr.
Rob Pike83f21b92013-05-17 13:25:48 -07001131// Stores can be of arbitrary types.
1132//
Rob Pike87334f42013-05-17 14:02:47 -07001133// Pos() returns the ast.StarExpr.Star, if explicit in the source.
Rob Pike87334f42013-05-17 14:02:47 -07001134//
Rob Pike83f21b92013-05-17 13:25:48 -07001135// Example printed form:
1136// *x = y
1137//
1138type Store struct {
1139 anInstruction
1140 Addr Value
1141 Val Value
Rob Pike87334f42013-05-17 14:02:47 -07001142 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001143}
1144
Rob Pike87334f42013-05-17 14:02:47 -07001145// The MapUpdate instruction updates the association of Map[Key] to
1146// Value.
1147//
Alan Donovanc8a68902013-08-22 10:13:51 -04001148// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
1149// if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001150//
1151// Example printed form:
1152// t0[t1] = t2
1153//
1154type MapUpdate struct {
1155 anInstruction
1156 Map Value
1157 Key Value
1158 Value Value
Rob Pike87334f42013-05-17 14:02:47 -07001159 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001160}
1161
Alan Donovane2962652013-10-28 12:05:29 -04001162// A DebugRef instruction maps a source-level expression Expr to the
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001163// SSA value X that represents the value (!IsAddr) or address (IsAddr)
Alan Donovane2962652013-10-28 12:05:29 -04001164// of that expression.
Alan Donovan55d678e2013-07-15 13:56:46 -04001165//
1166// DebugRef is a pseudo-instruction: it has no dynamic effect.
1167//
Alan Donovane590cdb2013-10-09 12:47:30 -04001168// Pos() returns Expr.Pos(), the start position of the source-level
1169// expression. This is not the same as the "designated" token as
1170// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the
1171// position of the ("designated") Lparen token.
Alan Donovan55d678e2013-07-15 13:56:46 -04001172//
Alan Donovane2962652013-10-28 12:05:29 -04001173// If Expr is an *ast.Ident denoting a var or func, Object() returns
1174// the object; though this information can be obtained from the type
1175// checker, including it here greatly facilitates debugging.
1176// For non-Ident expressions, Object() returns nil.
1177//
1178// DebugRefs are generated only for functions built with debugging
1179// enabled; see Package.SetDebugMode().
1180//
1181// DebugRefs are not emitted for ast.Idents referring to constants or
1182// predeclared identifiers, since they are trivial and numerous.
1183// Nor are they emitted for ast.ParenExprs.
Alan Donovan55d678e2013-07-15 13:56:46 -04001184//
1185// (By representing these as instructions, rather than out-of-band,
1186// consistency is maintained during transformation passes by the
1187// ordinary SSA renaming machinery.)
1188//
Alan Donovane2962652013-10-28 12:05:29 -04001189// Example printed form:
1190// ; *ast.CallExpr @ 102:9 is t5
1191// ; var x float64 @ 109:72 is x
1192// ; address of *ast.CompositeLit @ 216:10 is t0
Alan Donovanaa238622013-10-27 10:55:21 -04001193//
Alan Donovan55d678e2013-07-15 13:56:46 -04001194type DebugRef struct {
1195 anInstruction
Alan Donovan9f640c22013-10-24 18:31:50 -04001196 Expr ast.Expr // the referring expression (never *ast.ParenExpr)
Alan Donovane2962652013-10-28 12:05:29 -04001197 object types.Object // the identity of the source var/func
Alan Donovan9f640c22013-10-24 18:31:50 -04001198 IsAddr bool // Expr is addressable and X is the address it denotes
Alan Donovane2962652013-10-28 12:05:29 -04001199 X Value // the value or address of Expr
Alan Donovan55d678e2013-07-15 13:56:46 -04001200}
1201
Rob Pike83f21b92013-05-17 13:25:48 -07001202// Embeddable mix-ins and helpers for common parts of other structs. -----------
1203
Alan Donovan1ff34522013-10-14 13:48:34 -04001204// register is a mix-in embedded by all SSA values that are also
1205// instructions, i.e. virtual registers, and provides a uniform
1206// implementation of most of the Value interface: Value.Name() is a
1207// numbered register (e.g. "t0"); the other methods are field accessors.
Rob Pike83f21b92013-05-17 13:25:48 -07001208//
Alan Donovan1ff34522013-10-14 13:48:34 -04001209// Temporary names are automatically assigned to each register on
Rob Pike83f21b92013-05-17 13:25:48 -07001210// completion of building a function in SSA form.
1211//
1212// Clients must not assume that the 'id' value (and the Name() derived
1213// from it) is unique within a function. As always in this API,
1214// semantics are determined only by identity; names exist only to
1215// facilitate debugging.
1216//
Alan Donovan1ff34522013-10-14 13:48:34 -04001217type register struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001218 anInstruction
1219 num int // "name" of virtual register, e.g. "t0". Not guaranteed unique.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001220 typ types.Type // type of virtual register
Rob Pike87334f42013-05-17 14:02:47 -07001221 pos token.Pos // position of source expression, or NoPos
Rob Pike83f21b92013-05-17 13:25:48 -07001222 referrers []Instruction
1223}
1224
1225// anInstruction is a mix-in embedded by all Instructions.
Alan Donovanf1e5b032013-11-07 10:08:51 -05001226// It provides the implementations of the Block and setBlock methods.
Rob Pike83f21b92013-05-17 13:25:48 -07001227type anInstruction struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001228 block *BasicBlock // the basic block of this instruction
Rob Pike83f21b92013-05-17 13:25:48 -07001229}
1230
1231// CallCommon is contained by Go, Defer and Call to hold the
1232// common parts of a function or method call.
1233//
1234// Each CallCommon exists in one of two modes, function call and
1235// interface method invocation, or "call" and "invoke" for short.
1236//
Alan Donovan4da31df2013-07-26 11:22:34 -04001237// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon
Alan Donovan70722532013-08-19 15:38:30 -04001238// represents an ordinary function call of the value in Value,
1239// which may be a *Builtin, a *Function or any other value of kind
1240// 'func'.
Rob Pike83f21b92013-05-17 13:25:48 -07001241//
Alan Donovan118786e2013-07-26 14:06:26 -04001242// In the common case in which Value is a *Function, this indicates a
Rob Pike83f21b92013-05-17 13:25:48 -07001243// statically dispatched call to a package-level function, an
1244// anonymous function, or a method of a named type. Also statically
Alan Donovan118786e2013-07-26 14:06:26 -04001245// dispatched, but less common, Value may be a *MakeClosure, indicating
Rob Pike83f21b92013-05-17 13:25:48 -07001246// an immediately applied function literal with free variables. Any
Alan Donovan118786e2013-07-26 14:06:26 -04001247// other value of Value indicates a dynamically dispatched function
Rob Pike83f21b92013-05-17 13:25:48 -07001248// call. The StaticCallee method returns the callee in these cases.
1249//
Alan Donovan118786e2013-07-26 14:06:26 -04001250// Args contains the arguments to the call. If Value is a method,
1251// Args[0] contains the receiver parameter.
Rob Pike83f21b92013-05-17 13:25:48 -07001252//
1253// Example printed form:
1254// t2 = println(t0, t1)
1255// go t3()
1256// defer t5(...t6)
1257//
Alan Donovan4da31df2013-07-26 11:22:34 -04001258// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon
Rob Pike83f21b92013-05-17 13:25:48 -07001259// represents a dynamically dispatched call to an interface method.
Alan Donovan118786e2013-07-26 14:06:26 -04001260// In this mode, Value is the interface value and Method is the
Alan Donovan70722532013-08-19 15:38:30 -04001261// interface's abstract method. Note: an abstract method may be
1262// shared by multiple interfaces due to embedding; Value.Type()
1263// provides the specific interface used for this call.
Rob Pike83f21b92013-05-17 13:25:48 -07001264//
Alan Donovan118786e2013-07-26 14:06:26 -04001265// Value is implicitly supplied to the concrete method implementation
Rob Pike83f21b92013-05-17 13:25:48 -07001266// as the receiver parameter; in other words, Args[0] holds not the
Alan Donovan118786e2013-07-26 14:06:26 -04001267// receiver but the first true argument.
Rob Pike83f21b92013-05-17 13:25:48 -07001268//
Rob Pike83f21b92013-05-17 13:25:48 -07001269// Example printed form:
1270// t1 = invoke t0.String()
1271// go invoke t3.Run(t2)
1272// defer invoke t4.Handle(...t5)
1273//
1274// In both modes, HasEllipsis is true iff the last element of Args is
1275// a slice value containing zero or more arguments to a variadic
1276// function. (This is not semantically significant since the type of
1277// the called function is sufficient to determine this, but it aids
1278// readability of the printed form.)
1279//
1280type CallCommon struct {
Alan Donovan118786e2013-07-26 14:06:26 -04001281 Value Value // receiver (invoke mode) or func value (call mode)
1282 Method *types.Func // abstract method (invoke mode)
1283 Args []Value // actual parameters (in static method call, includes receiver)
Alan Donovan4da31df2013-07-26 11:22:34 -04001284 HasEllipsis bool // true iff last Args is a slice of '...' args (needed?)
1285 pos token.Pos // position of CallExpr.Lparen, iff explicit in source
Rob Pike83f21b92013-05-17 13:25:48 -07001286}
1287
1288// IsInvoke returns true if this call has "invoke" (not "call") mode.
1289func (c *CallCommon) IsInvoke() bool {
Alan Donovan4da31df2013-07-26 11:22:34 -04001290 return c.Method != nil
Rob Pike83f21b92013-05-17 13:25:48 -07001291}
1292
Rob Pike87334f42013-05-17 14:02:47 -07001293func (c *CallCommon) Pos() token.Pos { return c.pos }
1294
Alan Donovan8097dad2013-06-24 14:15:13 -04001295// Signature returns the signature of the called function.
1296//
1297// For an "invoke"-mode call, the signature of the interface method is
Alan Donovanb68a0292013-06-26 12:38:08 -04001298// returned.
1299//
1300// In either "call" or "invoke" mode, if the callee is a method, its
1301// receiver is represented by sig.Recv, not sig.Params().At(0).
Alan Donovan8097dad2013-06-24 14:15:13 -04001302//
Alan Donovanea8ba6f2013-07-03 15:10:49 -04001303// Signature returns nil for a call to a built-in function.
1304//
Alan Donovan8097dad2013-06-24 14:15:13 -04001305func (c *CallCommon) Signature() *types.Signature {
Alan Donovan4da31df2013-07-26 11:22:34 -04001306 if c.Method != nil {
1307 return c.Method.Type().(*types.Signature)
Alan Donovan8097dad2013-06-24 14:15:13 -04001308 }
Alan Donovan118786e2013-07-26 14:06:26 -04001309 sig, _ := c.Value.Type().Underlying().(*types.Signature) // nil for *Builtin
Alan Donovanea8ba6f2013-07-03 15:10:49 -04001310 return sig
Alan Donovan8097dad2013-06-24 14:15:13 -04001311}
1312
Rob Pike83f21b92013-05-17 13:25:48 -07001313// StaticCallee returns the called function if this is a trivially
1314// static "call"-mode call.
1315func (c *CallCommon) StaticCallee() *Function {
Alan Donovan118786e2013-07-26 14:06:26 -04001316 switch fn := c.Value.(type) {
Rob Pike83f21b92013-05-17 13:25:48 -07001317 case *Function:
1318 return fn
1319 case *MakeClosure:
1320 return fn.Fn.(*Function)
1321 }
1322 return nil
1323}
1324
Rob Pike83f21b92013-05-17 13:25:48 -07001325// Description returns a description of the mode of this call suitable
1326// for a user interface, e.g. "static method call".
1327func (c *CallCommon) Description() string {
Alan Donovan118786e2013-07-26 14:06:26 -04001328 switch fn := c.Value.(type) {
Alan Donovan70722532013-08-19 15:38:30 -04001329 case *Builtin:
1330 return "built-in function call"
Rob Pike83f21b92013-05-17 13:25:48 -07001331 case *MakeClosure:
1332 return "static function closure call"
1333 case *Function:
Rob Pike87334f42013-05-17 14:02:47 -07001334 if fn.Signature.Recv() != nil {
Rob Pike83f21b92013-05-17 13:25:48 -07001335 return "static method call"
1336 }
1337 return "static function call"
1338 }
Alan Donovan70722532013-08-19 15:38:30 -04001339 if c.IsInvoke() {
1340 return "dynamic method call" // ("invoke" mode)
1341 }
Rob Pike83f21b92013-05-17 13:25:48 -07001342 return "dynamic function call"
1343}
1344
Alan Donovan8097dad2013-06-24 14:15:13 -04001345// The CallInstruction interface, implemented by *Go, *Defer and *Call,
1346// exposes the common parts of function calling instructions,
1347// yet provides a way back to the Value defined by *Call alone.
1348//
1349type CallInstruction interface {
1350 Instruction
1351 Common() *CallCommon // returns the common parts of the call
1352 Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer)
1353}
1354
1355func (s *Call) Common() *CallCommon { return &s.Call }
1356func (s *Defer) Common() *CallCommon { return &s.Call }
1357func (s *Go) Common() *CallCommon { return &s.Call }
1358
1359func (s *Call) Value() *Call { return s }
1360func (s *Defer) Value() *Call { return nil }
1361func (s *Go) Value() *Call { return nil }
1362
Alan Donovan55d678e2013-07-15 13:56:46 -04001363func (v *Builtin) Type() types.Type { return v.object.Type() }
1364func (v *Builtin) Name() string { return v.object.Name() }
Rob Pike83f21b92013-05-17 13:25:48 -07001365func (*Builtin) Referrers() *[]Instruction { return nil }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001366func (v *Builtin) Pos() token.Pos { return token.NoPos }
Alan Donovan55d678e2013-07-15 13:56:46 -04001367func (v *Builtin) Object() types.Object { return v.object }
Rob Pike83f21b92013-05-17 13:25:48 -07001368
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001369func (v *Capture) Type() types.Type { return v.typ }
1370func (v *Capture) Name() string { return v.name }
Rob Pike83f21b92013-05-17 13:25:48 -07001371func (v *Capture) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001372func (v *Capture) Pos() token.Pos { return v.pos }
Alan Donovan341a07a2013-06-13 14:43:35 -04001373func (v *Capture) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001374
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001375func (v *Global) Type() types.Type { return v.typ }
1376func (v *Global) Name() string { return v.name }
1377func (v *Global) Pos() token.Pos { return v.pos }
1378func (v *Global) Referrers() *[]Instruction { return nil }
1379func (v *Global) Token() token.Token { return token.VAR }
1380func (v *Global) Object() types.Object { return v.object }
1381func (v *Global) String() string { return v.RelString(nil) }
1382func (v *Global) Package() *Package { return v.Pkg }
1383func (v *Global) RelString(from *types.Package) string { return relString(v, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001384
Alan Donovanf1e5b032013-11-07 10:08:51 -05001385func (v *Function) Name() string { return v.name }
1386func (v *Function) Type() types.Type { return v.Signature }
1387func (v *Function) Pos() token.Pos { return v.pos }
1388func (v *Function) Token() token.Token { return token.FUNC }
1389func (v *Function) Object() types.Object { return v.object }
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001390func (v *Function) String() string { return v.RelString(nil) }
1391func (v *Function) Package() *Package { return v.Pkg }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001392func (v *Function) Referrers() *[]Instruction {
1393 if v.Enclosing != nil {
1394 return &v.referrers
1395 }
1396 return nil
1397}
Rob Pike83f21b92013-05-17 13:25:48 -07001398
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001399func (v *Parameter) Type() types.Type { return v.typ }
1400func (v *Parameter) Name() string { return v.name }
Alan Donovan55d678e2013-07-15 13:56:46 -04001401func (v *Parameter) Object() types.Object { return v.object }
Rob Pike83f21b92013-05-17 13:25:48 -07001402func (v *Parameter) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001403func (v *Parameter) Pos() token.Pos { return v.pos }
Alan Donovan341a07a2013-06-13 14:43:35 -04001404func (v *Parameter) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001405
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001406func (v *Alloc) Type() types.Type { return v.typ }
Rob Pike83f21b92013-05-17 13:25:48 -07001407func (v *Alloc) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001408func (v *Alloc) Pos() token.Pos { return v.pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001409
Alan Donovan1ff34522013-10-14 13:48:34 -04001410func (v *register) Type() types.Type { return v.typ }
1411func (v *register) setType(typ types.Type) { v.typ = typ }
1412func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) }
1413func (v *register) setNum(num int) { v.num = num }
1414func (v *register) Referrers() *[]Instruction { return &v.referrers }
1415func (v *register) Pos() token.Pos { return v.pos }
1416func (v *register) setPos(pos token.Pos) { v.pos = pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001417
Alan Donovan341a07a2013-06-13 14:43:35 -04001418func (v *anInstruction) Parent() *Function { return v.block.parent }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001419func (v *anInstruction) Block() *BasicBlock { return v.block }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001420func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block }
Rob Pike83f21b92013-05-17 13:25:48 -07001421
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001422func (t *Type) Name() string { return t.object.Name() }
1423func (t *Type) Pos() token.Pos { return t.object.Pos() }
1424func (t *Type) Type() types.Type { return t.object.Type() }
1425func (t *Type) Token() token.Token { return token.TYPE }
1426func (t *Type) Object() types.Object { return t.object }
1427func (t *Type) String() string { return t.RelString(nil) }
1428func (t *Type) Package() *Package { return t.pkg }
1429func (t *Type) RelString(from *types.Package) string { return relString(t, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001430
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001431func (c *NamedConst) Name() string { return c.object.Name() }
1432func (c *NamedConst) Pos() token.Pos { return c.object.Pos() }
1433func (c *NamedConst) String() string { return c.RelString(nil) }
1434func (c *NamedConst) Type() types.Type { return c.object.Type() }
1435func (c *NamedConst) Token() token.Token { return token.CONST }
1436func (c *NamedConst) Object() types.Object { return c.object }
1437func (c *NamedConst) Package() *Package { return c.pkg }
1438func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001439
1440// Func returns the package-level function of the specified name,
1441// or nil if not found.
1442//
1443func (p *Package) Func(name string) (f *Function) {
1444 f, _ = p.Members[name].(*Function)
1445 return
1446}
1447
1448// Var returns the package-level variable of the specified name,
1449// or nil if not found.
1450//
1451func (p *Package) Var(name string) (g *Global) {
1452 g, _ = p.Members[name].(*Global)
1453 return
1454}
1455
1456// Const returns the package-level constant of the specified name,
1457// or nil if not found.
1458//
Alan Donovan732dbe92013-07-16 13:50:08 -04001459func (p *Package) Const(name string) (c *NamedConst) {
1460 c, _ = p.Members[name].(*NamedConst)
Rob Pike83f21b92013-05-17 13:25:48 -07001461 return
1462}
1463
1464// Type returns the package-level type of the specified name,
1465// or nil if not found.
1466//
1467func (p *Package) Type(name string) (t *Type) {
1468 t, _ = p.Members[name].(*Type)
1469 return
1470}
1471
Rob Pike87334f42013-05-17 14:02:47 -07001472func (v *Call) Pos() token.Pos { return v.Call.pos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001473func (s *Defer) Pos() token.Pos { return s.pos }
1474func (s *Go) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001475func (s *MapUpdate) Pos() token.Pos { return s.pos }
1476func (s *Panic) Pos() token.Pos { return s.pos }
Alan Donovan068f0172013-10-08 12:31:39 -04001477func (s *Return) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001478func (s *Send) Pos() token.Pos { return s.pos }
1479func (s *Store) Pos() token.Pos { return s.pos }
1480func (s *If) Pos() token.Pos { return token.NoPos }
1481func (s *Jump) Pos() token.Pos { return token.NoPos }
1482func (s *RunDefers) Pos() token.Pos { return token.NoPos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001483func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() }
Rob Pike83f21b92013-05-17 13:25:48 -07001484
Rob Pike87334f42013-05-17 14:02:47 -07001485// Operands.
Rob Pike83f21b92013-05-17 13:25:48 -07001486
1487func (v *Alloc) Operands(rands []*Value) []*Value {
1488 return rands
1489}
1490
1491func (v *BinOp) Operands(rands []*Value) []*Value {
1492 return append(rands, &v.X, &v.Y)
1493}
1494
1495func (c *CallCommon) Operands(rands []*Value) []*Value {
Alan Donovan118786e2013-07-26 14:06:26 -04001496 rands = append(rands, &c.Value)
Rob Pike83f21b92013-05-17 13:25:48 -07001497 for i := range c.Args {
1498 rands = append(rands, &c.Args[i])
1499 }
1500 return rands
1501}
1502
1503func (s *Go) Operands(rands []*Value) []*Value {
1504 return s.Call.Operands(rands)
1505}
1506
1507func (s *Call) Operands(rands []*Value) []*Value {
1508 return s.Call.Operands(rands)
1509}
1510
1511func (s *Defer) Operands(rands []*Value) []*Value {
1512 return s.Call.Operands(rands)
1513}
1514
1515func (v *ChangeInterface) Operands(rands []*Value) []*Value {
1516 return append(rands, &v.X)
1517}
1518
Rob Pike87334f42013-05-17 14:02:47 -07001519func (v *ChangeType) Operands(rands []*Value) []*Value {
1520 return append(rands, &v.X)
1521}
1522
1523func (v *Convert) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001524 return append(rands, &v.X)
1525}
1526
Alan Donovan55d678e2013-07-15 13:56:46 -04001527func (s *DebugRef) Operands(rands []*Value) []*Value {
1528 return append(rands, &s.X)
1529}
1530
Rob Pike83f21b92013-05-17 13:25:48 -07001531func (v *Extract) Operands(rands []*Value) []*Value {
1532 return append(rands, &v.Tuple)
1533}
1534
1535func (v *Field) Operands(rands []*Value) []*Value {
1536 return append(rands, &v.X)
1537}
1538
1539func (v *FieldAddr) Operands(rands []*Value) []*Value {
1540 return append(rands, &v.X)
1541}
1542
1543func (s *If) Operands(rands []*Value) []*Value {
1544 return append(rands, &s.Cond)
1545}
1546
1547func (v *Index) Operands(rands []*Value) []*Value {
1548 return append(rands, &v.X, &v.Index)
1549}
1550
1551func (v *IndexAddr) Operands(rands []*Value) []*Value {
1552 return append(rands, &v.X, &v.Index)
1553}
1554
1555func (*Jump) Operands(rands []*Value) []*Value {
1556 return rands
1557}
1558
1559func (v *Lookup) Operands(rands []*Value) []*Value {
1560 return append(rands, &v.X, &v.Index)
1561}
1562
1563func (v *MakeChan) Operands(rands []*Value) []*Value {
1564 return append(rands, &v.Size)
1565}
1566
1567func (v *MakeClosure) Operands(rands []*Value) []*Value {
1568 rands = append(rands, &v.Fn)
1569 for i := range v.Bindings {
1570 rands = append(rands, &v.Bindings[i])
1571 }
1572 return rands
1573}
1574
1575func (v *MakeInterface) Operands(rands []*Value) []*Value {
1576 return append(rands, &v.X)
1577}
1578
1579func (v *MakeMap) Operands(rands []*Value) []*Value {
1580 return append(rands, &v.Reserve)
1581}
1582
1583func (v *MakeSlice) Operands(rands []*Value) []*Value {
1584 return append(rands, &v.Len, &v.Cap)
1585}
1586
1587func (v *MapUpdate) Operands(rands []*Value) []*Value {
1588 return append(rands, &v.Map, &v.Key, &v.Value)
1589}
1590
1591func (v *Next) Operands(rands []*Value) []*Value {
1592 return append(rands, &v.Iter)
1593}
1594
1595func (s *Panic) Operands(rands []*Value) []*Value {
1596 return append(rands, &s.X)
1597}
1598
1599func (v *Phi) Operands(rands []*Value) []*Value {
1600 for i := range v.Edges {
1601 rands = append(rands, &v.Edges[i])
1602 }
1603 return rands
1604}
1605
1606func (v *Range) Operands(rands []*Value) []*Value {
1607 return append(rands, &v.X)
1608}
1609
Alan Donovan068f0172013-10-08 12:31:39 -04001610func (s *Return) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001611 for i := range s.Results {
1612 rands = append(rands, &s.Results[i])
1613 }
1614 return rands
1615}
1616
1617func (*RunDefers) Operands(rands []*Value) []*Value {
1618 return rands
1619}
1620
1621func (v *Select) Operands(rands []*Value) []*Value {
1622 for i := range v.States {
1623 rands = append(rands, &v.States[i].Chan, &v.States[i].Send)
1624 }
1625 return rands
1626}
1627
1628func (s *Send) Operands(rands []*Value) []*Value {
1629 return append(rands, &s.Chan, &s.X)
1630}
1631
1632func (v *Slice) Operands(rands []*Value) []*Value {
1633 return append(rands, &v.X, &v.Low, &v.High)
1634}
1635
1636func (s *Store) Operands(rands []*Value) []*Value {
1637 return append(rands, &s.Addr, &s.Val)
1638}
1639
1640func (v *TypeAssert) Operands(rands []*Value) []*Value {
1641 return append(rands, &v.X)
1642}
1643
1644func (v *UnOp) Operands(rands []*Value) []*Value {
1645 return append(rands, &v.X)
1646}