blob: c7b550ec24edc0349f5808d93f241a10bf8d9b3e [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//
242// Blocks[0] is the function entry point; block order is not otherwise
243// semantically significant, though it may affect the readability of
244// the disassembly.
245//
Alan Donovan2accef22013-10-14 15:38:56 -0400246// Recover is an optional second entry point to which control resumes
247// after a recovered panic. The Recover block may contain only a load
248// of the function's named return parameters followed by a return of
249// the loaded values.
250//
Rob Pike83f21b92013-05-17 13:25:48 -0700251// A nested function that refers to one or more lexically enclosing
252// local variables ("free variables") has Capture parameters. Such
253// functions cannot be called directly but require a value created by
254// MakeClosure which, via its Bindings, supplies values for these
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400255// parameters.
Rob Pike83f21b92013-05-17 13:25:48 -0700256//
Rob Pike87334f42013-05-17 14:02:47 -0700257// If the function is a method (Signature.Recv() != nil) then the first
Rob Pike83f21b92013-05-17 13:25:48 -0700258// element of Params is the receiver parameter.
259//
Rob Pike87334f42013-05-17 14:02:47 -0700260// Pos() returns the declaring ast.FuncLit.Type.Func or the position
261// of the ast.FuncDecl.Name, if the function was explicit in the
Alan Donovan1fa3f782013-07-03 17:57:20 -0400262// source. Synthetic wrappers, for which Synthetic != "", may share
263// the same position as the function they wrap.
Rob Pike87334f42013-05-17 14:02:47 -0700264//
Rob Pike83f21b92013-05-17 13:25:48 -0700265// Type() returns the function's Signature.
266//
267type Function struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400268 name string
Robert Griesemer64ea46e2013-07-26 22:27:48 -0700269 object types.Object // a declared *types.Func; nil for init, wrappers, etc.
270 method *types.Selection // info about provenance of synthetic methods [currently unused]
Rob Pike83f21b92013-05-17 13:25:48 -0700271 Signature *types.Signature
Rob Pike87334f42013-05-17 14:02:47 -0700272 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400273
Alan Donovan1fa3f782013-07-03 17:57:20 -0400274 Synthetic string // provenance of synthetic function; "" for true source functions
Alan Donovanaa238622013-10-27 10:55:21 -0400275 syntax ast.Node // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode
Rob Pike83f21b92013-05-17 13:25:48 -0700276 Enclosing *Function // enclosing function if anon; nil if global
Alan Donovan87ced822013-10-23 17:07:52 -0400277 Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error)
Rob Pike83f21b92013-05-17 13:25:48 -0700278 Prog *Program // enclosing program
279 Params []*Parameter // function parameters; for methods, includes receiver
280 FreeVars []*Capture // free variables whose values must be supplied by closure
281 Locals []*Alloc
282 Blocks []*BasicBlock // basic blocks of the function; nil => external
Alan Donovan2accef22013-10-14 15:38:56 -0400283 Recover *BasicBlock // optional; control transfers here after recovered panic
Rob Pike83f21b92013-05-17 13:25:48 -0700284 AnonFuncs []*Function // anonymous functions directly beneath this one
Alan Donovanf1e5b032013-11-07 10:08:51 -0500285 referrers []Instruction // referring instructions (iff Enclosing != nil)
Rob Pike83f21b92013-05-17 13:25:48 -0700286
287 // The following fields are set transiently during building,
288 // then cleared.
289 currentBlock *BasicBlock // where to emit code
290 objects map[types.Object]Value // addresses of local variables
291 namedResults []*Alloc // tuple of named results
Rob Pike83f21b92013-05-17 13:25:48 -0700292 targets *targets // linked stack of branch targets
293 lblocks map[*ast.Object]*lblock // labelled blocks
294}
295
296// An SSA basic block.
297//
298// The final element of Instrs is always an explicit transfer of
Alan Donovan068f0172013-10-08 12:31:39 -0400299// control (If, Jump, Return or Panic).
Rob Pike83f21b92013-05-17 13:25:48 -0700300//
301// A block may contain no Instructions only if it is unreachable,
302// i.e. Preds is nil. Empty blocks are typically pruned.
303//
304// BasicBlocks and their Preds/Succs relation form a (possibly cyclic)
305// graph independent of the SSA Value graph. It is illegal for
306// multiple edges to exist between the same pair of blocks.
307//
308// The order of Preds and Succs are significant (to Phi and If
309// instructions, respectively).
310//
311type BasicBlock struct {
312 Index int // index of this block within Func.Blocks
313 Comment string // optional label; no semantic significance
Alan Donovan341a07a2013-06-13 14:43:35 -0400314 parent *Function // parent function
Rob Pike83f21b92013-05-17 13:25:48 -0700315 Instrs []Instruction // instructions in order
316 Preds, Succs []*BasicBlock // predecessors and successors
317 succs2 [2]*BasicBlock // initial space for Succs.
318 dom *domNode // node in dominator tree; optional.
319 gaps int // number of nil Instrs (transient).
320 rundefers int // number of rundefers (transient)
321}
322
323// Pure values ----------------------------------------
324
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400325// A Capture represents a free variable of the function to which it
326// belongs.
Rob Pike83f21b92013-05-17 13:25:48 -0700327//
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400328// Captures are used to implement anonymous functions, whose free
329// variables are lexically captured in a closure formed by
330// MakeClosure. The referent of such a capture is an Alloc or another
331// Capture and is considered a potentially escaping heap address, with
332// pointer type.
333//
334// Captures are also used to implement bound method closures. Such a
335// capture represents the receiver value and may be of any type that
336// has concrete methods.
Rob Pike83f21b92013-05-17 13:25:48 -0700337//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400338// Pos() returns the position of the value that was captured, which
339// belongs to an enclosing function.
340//
Rob Pike83f21b92013-05-17 13:25:48 -0700341type Capture struct {
Alan Donovan341a07a2013-06-13 14:43:35 -0400342 name string
343 typ types.Type
344 pos token.Pos
345 parent *Function
Rob Pike83f21b92013-05-17 13:25:48 -0700346 referrers []Instruction
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400347
348 // Transiently needed during building.
349 outer Value // the Value captured from the enclosing context.
Rob Pike83f21b92013-05-17 13:25:48 -0700350}
351
352// A Parameter represents an input parameter of a function.
353//
354type Parameter struct {
Alan Donovan341a07a2013-06-13 14:43:35 -0400355 name string
Alan Donovan55d678e2013-07-15 13:56:46 -0400356 object types.Object // a *types.Var; nil for non-source locals
Alan Donovan341a07a2013-06-13 14:43:35 -0400357 typ types.Type
358 pos token.Pos
359 parent *Function
Rob Pike83f21b92013-05-17 13:25:48 -0700360 referrers []Instruction
361}
362
Alan Donovan732dbe92013-07-16 13:50:08 -0400363// A Const represents the value of a constant expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700364//
Rob Pike87334f42013-05-17 14:02:47 -0700365// It may have a nil, boolean, string or numeric (integer, fraction or
366// complex) value, or a []byte or []rune conversion of a string
Alan Donovan732dbe92013-07-16 13:50:08 -0400367// constant.
Rob Pike87334f42013-05-17 14:02:47 -0700368//
Alan Donovan732dbe92013-07-16 13:50:08 -0400369// Consts may be of named types. A constant's underlying type can be
Rob Pike87334f42013-05-17 14:02:47 -0700370// a basic type, possibly one of the "untyped" types, or a slice type
Alan Donovan732dbe92013-07-16 13:50:08 -0400371// whose elements' underlying type is byte or rune. A nil constant can
Rob Pike87334f42013-05-17 14:02:47 -0700372// have any reference type: interface, map, channel, pointer, slice,
373// or function---but not "untyped nil".
Rob Pike83f21b92013-05-17 13:25:48 -0700374//
Alan Donovan732dbe92013-07-16 13:50:08 -0400375// All source-level constant expressions are represented by a Const
Rob Pike83f21b92013-05-17 13:25:48 -0700376// of equal type and value.
377//
Alan Donovan732dbe92013-07-16 13:50:08 -0400378// Value holds the exact value of the constant, independent of its
Rob Pike87334f42013-05-17 14:02:47 -0700379// Type(), using the same representation as package go/exact uses for
Robert Griesemerf50f6c82013-10-09 14:17:25 -0700380// constants, or nil for a typed nil value.
Rob Pike83f21b92013-05-17 13:25:48 -0700381//
Alan Donovana399e262013-07-15 16:10:08 -0400382// Pos() returns token.NoPos.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400383//
Rob Pike83f21b92013-05-17 13:25:48 -0700384// Example printed form:
385// 42:int
386// "hello":untyped string
387// 3+4i:MyComplex
388//
Alan Donovan732dbe92013-07-16 13:50:08 -0400389type Const struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400390 typ types.Type
Rob Pike83f21b92013-05-17 13:25:48 -0700391 Value exact.Value
392}
393
394// A Global is a named Value holding the address of a package-level
395// variable.
396//
Rob Pike87334f42013-05-17 14:02:47 -0700397// Pos() returns the position of the ast.ValueSpec.Names[*]
398// identifier.
399//
Rob Pike83f21b92013-05-17 13:25:48 -0700400type Global struct {
Alan Donovanbc1f7242013-07-11 14:12:30 -0400401 name string
402 object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
403 typ types.Type
404 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400405
406 Pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -0700407}
408
Rob Pike87334f42013-05-17 14:02:47 -0700409// A Builtin represents a built-in function, e.g. len.
Rob Pike83f21b92013-05-17 13:25:48 -0700410//
Rob Pike87334f42013-05-17 14:02:47 -0700411// Builtins are immutable values. Builtins do not have addresses.
Alan Donovan55d678e2013-07-15 13:56:46 -0400412// Builtins can only appear in CallCommon.Func.
Rob Pike83f21b92013-05-17 13:25:48 -0700413//
Alan Donovan318b83e2013-09-23 18:18:35 -0400414// Object() returns a *types.Builtin.
415//
416// Type() returns types.Typ[types.Invalid], since built-in functions
417// may have polymorphic or variadic types that are not expressible in
418// Go's type system.
Rob Pike83f21b92013-05-17 13:25:48 -0700419//
420type Builtin struct {
Alan Donovan318b83e2013-09-23 18:18:35 -0400421 object *types.Builtin // canonical types.Universe object for this built-in
Rob Pike83f21b92013-05-17 13:25:48 -0700422}
423
424// Value-defining instructions ----------------------------------------
425
426// The Alloc instruction reserves space for a value of the given type,
427// zero-initializes it, and yields its address.
428//
429// Alloc values are always addresses, and have pointer types, so the
430// type of the allocated space is actually indirect(Type()).
431//
432// If Heap is false, Alloc allocates space in the function's
433// activation record (frame); we refer to an Alloc(Heap=false) as a
434// "local" alloc. Each local Alloc returns the same address each time
435// it is executed within the same activation; the space is
436// re-initialized to zero.
437//
438// If Heap is true, Alloc allocates space in the heap, and returns; we
439// refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc
440// returns a different address each time it is executed.
441//
442// When Alloc is applied to a channel, map or slice type, it returns
443// the address of an uninitialized (nil) reference of that kind; store
444// the result of MakeSlice, MakeMap or MakeChan in that location to
445// instantiate these types.
446//
Rob Pike87334f42013-05-17 14:02:47 -0700447// Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
448// or the ast.CallExpr.Lparen for a call to new() or for a call that
449// allocates a varargs slice.
450//
Rob Pike83f21b92013-05-17 13:25:48 -0700451// Example printed form:
452// t0 = local int
453// t1 = new int
454//
455type Alloc struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400456 register
Alan Donovan5cc33ea2013-08-01 14:06:10 -0400457 Comment string
458 Heap bool
459 index int // dense numbering; for lifting
Rob Pike83f21b92013-05-17 13:25:48 -0700460}
461
Rob Pike87334f42013-05-17 14:02:47 -0700462// The Phi instruction represents an SSA φ-node, which combines values
463// that differ across incoming control-flow edges and yields a new
464// value. Within a block, all φ-nodes must appear before all non-φ
465// nodes.
466//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400467// Pos() returns the position of the && or || for short-circuit
468// control-flow joins, or that of the *Alloc for φ-nodes inserted
469// during SSA renaming.
Rob Pike83f21b92013-05-17 13:25:48 -0700470//
471// Example printed form:
472// t2 = phi [0.start: t0, 1.if.then: t1, ...]
473//
474type Phi struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400475 register
Rob Pike83f21b92013-05-17 13:25:48 -0700476 Comment string // a hint as to its purpose
477 Edges []Value // Edges[i] is value for Block().Preds[i]
478}
479
Rob Pike87334f42013-05-17 14:02:47 -0700480// The Call instruction represents a function or method call.
Rob Pike83f21b92013-05-17 13:25:48 -0700481//
482// The Call instruction yields the function result, if there is
483// exactly one, or a tuple (empty or len>1) whose components are
484// accessed via Extract.
485//
486// See CallCommon for generic function call documentation.
487//
Rob Pike87334f42013-05-17 14:02:47 -0700488// Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
489//
Rob Pike83f21b92013-05-17 13:25:48 -0700490// Example printed form:
491// t2 = println(t0, t1)
492// t4 = t3()
493// t7 = invoke t5.Println(...t6)
494//
495type Call struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400496 register
Rob Pike83f21b92013-05-17 13:25:48 -0700497 Call CallCommon
498}
499
Rob Pike87334f42013-05-17 14:02:47 -0700500// The BinOp instruction yields the result of binary operation X Op Y.
501//
502// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700503//
504// Example printed form:
505// t1 = t0 + 1:int
506//
507type BinOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400508 register
Rob Pike83f21b92013-05-17 13:25:48 -0700509 // One of:
510 // ADD SUB MUL QUO REM + - * / %
511 // AND OR XOR SHL SHR AND_NOT & | ^ << >> &~
512 // EQL LSS GTR NEQ LEQ GEQ == != < <= < >=
513 Op token.Token
514 X, Y Value
515}
516
Rob Pike87334f42013-05-17 14:02:47 -0700517// The UnOp instruction yields the result of Op X.
Rob Pike83f21b92013-05-17 13:25:48 -0700518// ARROW is channel receive.
519// MUL is pointer indirection (load).
520// XOR is bitwise complement.
521// SUB is negation.
522//
523// If CommaOk and Op=ARROW, the result is a 2-tuple of the value above
524// and a boolean indicating the success of the receive. The
525// components of the tuple are accessed using Extract.
526//
Alan Donovande47eba2013-08-27 11:18:31 -0400527// Pos() returns the ast.UnaryExpr.OpPos or ast.RangeStmt.TokPos (for
528// ranging over a channel), if explicit in the source.
Rob Pike87334f42013-05-17 14:02:47 -0700529//
Rob Pike83f21b92013-05-17 13:25:48 -0700530// Example printed form:
531// t0 = *x
532// t2 = <-t1,ok
533//
534type UnOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400535 register
Rob Pike83f21b92013-05-17 13:25:48 -0700536 Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^
537 X Value
538 CommaOk bool
539}
540
Rob Pike87334f42013-05-17 14:02:47 -0700541// The ChangeType instruction applies to X a value-preserving type
542// change to Type().
Rob Pike83f21b92013-05-17 13:25:48 -0700543//
Rob Pike87334f42013-05-17 14:02:47 -0700544// Type changes are permitted:
545// - between a named type and its underlying type.
546// - between two named types of the same underlying type.
547// - between (possibly named) pointers to identical base types.
548// - between f(T) functions and (T) func f() methods.
549// - from a bidirectional channel to a read- or write-channel,
550// optionally adding/removing a name.
Rob Pike83f21b92013-05-17 13:25:48 -0700551//
Rob Pike87334f42013-05-17 14:02:47 -0700552// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700553//
Rob Pike87334f42013-05-17 14:02:47 -0700554// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
555// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700556//
Rob Pike87334f42013-05-17 14:02:47 -0700557// Example printed form:
558// t1 = changetype *int <- IntPtr (t0)
559//
560type ChangeType struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400561 register
Rob Pike87334f42013-05-17 14:02:47 -0700562 X Value
563}
564
565// The Convert instruction yields the conversion of value X to type
Alan Donovan8097dad2013-06-24 14:15:13 -0400566// Type(). One or both of those types is basic (but possibly named).
Rob Pike87334f42013-05-17 14:02:47 -0700567//
568// A conversion may change the value and representation of its operand.
569// Conversions are permitted:
570// - between real numeric types.
571// - between complex numeric types.
572// - between string and []byte or []rune.
Alan Donovan8097dad2013-06-24 14:15:13 -0400573// - between pointers and unsafe.Pointer.
574// - between unsafe.Pointer and uintptr.
Rob Pike87334f42013-05-17 14:02:47 -0700575// - from (Unicode) integer to (UTF-8) string.
576// A conversion may imply a type name change also.
577//
578// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700579//
580// Conversions of untyped string/number/bool constants to a specific
581// representation are eliminated during SSA construction.
582//
Rob Pike87334f42013-05-17 14:02:47 -0700583// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
584// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700585//
Rob Pike87334f42013-05-17 14:02:47 -0700586// Example printed form:
587// t1 = convert []byte <- string (t0)
588//
589type Convert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400590 register
Rob Pike83f21b92013-05-17 13:25:48 -0700591 X Value
592}
593
594// ChangeInterface constructs a value of one interface type from a
595// value of another interface type known to be assignable to it.
Alan Donovanae801632013-07-26 21:49:27 -0400596// This operation cannot fail.
Rob Pike87334f42013-05-17 14:02:47 -0700597//
Alan Donovan341a07a2013-06-13 14:43:35 -0400598// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
599// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
600// instruction arose from an explicit e.(T) operation; or token.NoPos
601// otherwise.
602//
Rob Pike83f21b92013-05-17 13:25:48 -0700603// Example printed form:
604// t1 = change interface interface{} <- I (t0)
605//
606type ChangeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400607 register
Rob Pike83f21b92013-05-17 13:25:48 -0700608 X Value
609}
610
611// MakeInterface constructs an instance of an interface type from a
Alan Donovan341a07a2013-06-13 14:43:35 -0400612// value of a concrete type.
613//
Alan Donovan2a3a1292013-07-30 14:28:14 -0400614// Use X.Type().MethodSet() to find the method-set of X, and
Alan Donovan2f6855a2013-07-30 16:36:58 -0400615// Program.Method(m) to find the implementation of a method.
Rob Pike83f21b92013-05-17 13:25:48 -0700616//
617// To construct the zero value of an interface type T, use:
Alan Donovan732dbe92013-07-16 13:50:08 -0400618// NewConst(exact.MakeNil(), T, pos)
Rob Pike87334f42013-05-17 14:02:47 -0700619//
620// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
621// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700622//
623// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400624// t1 = make interface{} <- int (42:int)
625// t2 = make Stringer <- t0
Rob Pike83f21b92013-05-17 13:25:48 -0700626//
627type MakeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400628 register
Alan Donovan341a07a2013-06-13 14:43:35 -0400629 X Value
Rob Pike83f21b92013-05-17 13:25:48 -0700630}
631
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400632// The MakeClosure instruction yields a closure value whose code is
633// Fn and whose free variables' values are supplied by Bindings.
Rob Pike83f21b92013-05-17 13:25:48 -0700634//
635// Type() returns a (possibly named) *types.Signature.
636//
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400637// Pos() returns the ast.FuncLit.Type.Func for a function literal
638// closure or the ast.SelectorExpr.Sel for a bound method closure.
Rob Pike87334f42013-05-17 14:02:47 -0700639//
Rob Pike83f21b92013-05-17 13:25:48 -0700640// Example printed form:
641// t0 = make closure anon@1.2 [x y z]
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400642// t1 = make closure bound$(main.I).add [i]
Rob Pike83f21b92013-05-17 13:25:48 -0700643//
644type MakeClosure struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400645 register
Rob Pike83f21b92013-05-17 13:25:48 -0700646 Fn Value // always a *Function
647 Bindings []Value // values for each free variable in Fn.FreeVars
648}
649
650// The MakeMap instruction creates a new hash-table-based map object
651// and yields a value of kind map.
652//
653// Type() returns a (possibly named) *types.Map.
654//
Rob Pike87334f42013-05-17 14:02:47 -0700655// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
656// the ast.CompositeLit.Lbrack if created by a literal.
657//
Rob Pike83f21b92013-05-17 13:25:48 -0700658// Example printed form:
659// t1 = make map[string]int t0
Alan Donovan341a07a2013-06-13 14:43:35 -0400660// t1 = make StringIntMap t0
Rob Pike83f21b92013-05-17 13:25:48 -0700661//
662type MakeMap struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400663 register
Rob Pike83f21b92013-05-17 13:25:48 -0700664 Reserve Value // initial space reservation; nil => default
Rob Pike83f21b92013-05-17 13:25:48 -0700665}
666
667// The MakeChan instruction creates a new channel object and yields a
668// value of kind chan.
669//
670// Type() returns a (possibly named) *types.Chan.
671//
Rob Pike87334f42013-05-17 14:02:47 -0700672// Pos() returns the ast.CallExpr.Lparen for the make(chan) that
673// created it.
674//
Rob Pike83f21b92013-05-17 13:25:48 -0700675// Example printed form:
676// t0 = make chan int 0
Alan Donovan341a07a2013-06-13 14:43:35 -0400677// t0 = make IntChan 0
Rob Pike83f21b92013-05-17 13:25:48 -0700678//
679type MakeChan struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400680 register
Rob Pike83f21b92013-05-17 13:25:48 -0700681 Size Value // int; size of buffer; zero => synchronous.
Rob Pike83f21b92013-05-17 13:25:48 -0700682}
683
Rob Pike87334f42013-05-17 14:02:47 -0700684// The MakeSlice instruction yields a slice of length Len backed by a
685// newly allocated array of length Cap.
Rob Pike83f21b92013-05-17 13:25:48 -0700686//
687// Both Len and Cap must be non-nil Values of integer type.
688//
689// (Alloc(types.Array) followed by Slice will not suffice because
690// Alloc can only create arrays of statically known length.)
691//
692// Type() returns a (possibly named) *types.Slice.
693//
Rob Pike87334f42013-05-17 14:02:47 -0700694// Pos() returns the ast.CallExpr.Lparen for the make([]T) that
695// created it.
696//
Rob Pike83f21b92013-05-17 13:25:48 -0700697// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400698// t1 = make []string 1:int t0
699// t1 = make StringSlice 1:int t0
Rob Pike83f21b92013-05-17 13:25:48 -0700700//
701type MakeSlice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400702 register
Rob Pike83f21b92013-05-17 13:25:48 -0700703 Len Value
704 Cap Value
Rob Pike83f21b92013-05-17 13:25:48 -0700705}
706
Rob Pike87334f42013-05-17 14:02:47 -0700707// The Slice instruction yields a slice of an existing string, slice
708// or *array X between optional integer bounds Low and High.
Rob Pike83f21b92013-05-17 13:25:48 -0700709//
Alan Donovan70722532013-08-19 15:38:30 -0400710// Dynamically, this instruction panics if X evaluates to a nil *array
711// pointer.
712//
Rob Pike83f21b92013-05-17 13:25:48 -0700713// Type() returns string if the type of X was string, otherwise a
714// *types.Slice with the same element type as X.
715//
Rob Pike87334f42013-05-17 14:02:47 -0700716// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
717// operation, the ast.CompositeLit.Lbrace if created by a literal, or
718// NoPos if not explicit in the source (e.g. a variadic argument slice).
719//
Rob Pike83f21b92013-05-17 13:25:48 -0700720// Example printed form:
721// t1 = slice t0[1:]
722//
723type Slice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400724 register
Rob Pike83f21b92013-05-17 13:25:48 -0700725 X Value // slice, string, or *array
726 Low, High Value // either may be nil
727}
728
Rob Pike87334f42013-05-17 14:02:47 -0700729// The FieldAddr instruction yields the address of Field of *struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700730//
731// The field is identified by its index within the field list of the
732// struct type of X.
733//
Alan Donovan70722532013-08-19 15:38:30 -0400734// Dynamically, this instruction panics if X evaluates to a nil
735// pointer.
736//
Rob Pike83f21b92013-05-17 13:25:48 -0700737// Type() returns a (possibly named) *types.Pointer.
738//
Rob Pike87334f42013-05-17 14:02:47 -0700739// Pos() returns the position of the ast.SelectorExpr.Sel for the
740// field, if explicit in the source.
741//
Rob Pike83f21b92013-05-17 13:25:48 -0700742// Example printed form:
743// t1 = &t0.name [#1]
744//
745type FieldAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400746 register
Rob Pike83f21b92013-05-17 13:25:48 -0700747 X Value // *struct
Alan Donovan8097dad2013-06-24 14:15:13 -0400748 Field int // index into X.Type().Deref().(*types.Struct).Fields
Rob Pike83f21b92013-05-17 13:25:48 -0700749}
750
Rob Pike87334f42013-05-17 14:02:47 -0700751// The Field instruction yields the Field of struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700752//
753// The field is identified by its index within the field list of the
754// struct type of X; by using numeric indices we avoid ambiguity of
755// package-local identifiers and permit compact representations.
756//
Rob Pike87334f42013-05-17 14:02:47 -0700757// Pos() returns the position of the ast.SelectorExpr.Sel for the
758// field, if explicit in the source.
759//
Rob Pike83f21b92013-05-17 13:25:48 -0700760// Example printed form:
761// t1 = t0.name [#1]
762//
763type Field struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400764 register
Rob Pike83f21b92013-05-17 13:25:48 -0700765 X Value // struct
766 Field int // index into X.Type().(*types.Struct).Fields
767}
768
Rob Pike87334f42013-05-17 14:02:47 -0700769// The IndexAddr instruction yields the address of the element at
770// index Index of collection X. Index is an integer expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700771//
772// The elements of maps and strings are not addressable; use Lookup or
773// MapUpdate instead.
774//
Alan Donovan70722532013-08-19 15:38:30 -0400775// Dynamically, this instruction panics if X evaluates to a nil *array
776// pointer.
777//
Rob Pike83f21b92013-05-17 13:25:48 -0700778// Type() returns a (possibly named) *types.Pointer.
779//
Rob Pike87334f42013-05-17 14:02:47 -0700780// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
781// explicit in the source.
782//
Rob Pike83f21b92013-05-17 13:25:48 -0700783// Example printed form:
784// t2 = &t0[t1]
785//
786type IndexAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400787 register
Rob Pike83f21b92013-05-17 13:25:48 -0700788 X Value // slice or *array,
789 Index Value // numeric index
790}
791
Rob Pike87334f42013-05-17 14:02:47 -0700792// The Index instruction yields element Index of array X.
793//
794// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
795// explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700796//
797// Example printed form:
798// t2 = t0[t1]
799//
800type Index struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400801 register
Rob Pike83f21b92013-05-17 13:25:48 -0700802 X Value // array
803 Index Value // integer index
804}
805
Rob Pike87334f42013-05-17 14:02:47 -0700806// The Lookup instruction yields element Index of collection X, a map
807// or string. Index is an integer expression if X is a string or the
808// appropriate key type if X is a map.
Rob Pike83f21b92013-05-17 13:25:48 -0700809//
810// If CommaOk, the result is a 2-tuple of the value above and a
811// boolean indicating the result of a map membership test for the key.
812// The components of the tuple are accessed using Extract.
813//
Rob Pike87334f42013-05-17 14:02:47 -0700814// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
815//
Rob Pike83f21b92013-05-17 13:25:48 -0700816// Example printed form:
817// t2 = t0[t1]
818// t5 = t3[t4],ok
819//
820type Lookup struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400821 register
Rob Pike83f21b92013-05-17 13:25:48 -0700822 X Value // string or map
823 Index Value // numeric or key-typed index
824 CommaOk bool // return a value,ok pair
825}
826
827// SelectState is a helper for Select.
828// It represents one goal state and its corresponding communication.
829//
830type SelectState struct {
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400831 Dir ast.ChanDir // direction of case
832 Chan Value // channel to use (for send or receive)
833 Send Value // value to send (for send)
834 Pos token.Pos // position of token.ARROW
835 DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
Rob Pike83f21b92013-05-17 13:25:48 -0700836}
837
Rob Pike87334f42013-05-17 14:02:47 -0700838// The Select instruction tests whether (or blocks until) one or more
839// of the specified sent or received states is entered.
Rob Pike83f21b92013-05-17 13:25:48 -0700840//
Alan Donovan8097dad2013-06-24 14:15:13 -0400841// Let n be the number of States for which Dir==RECV and T_i (0<=i<n)
842// be the element type of each such state's Chan.
843// Select returns an n+2-tuple
844// (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)
845// The tuple's components, described below, must be accessed via the
846// Extract instruction.
Rob Pike83f21b92013-05-17 13:25:48 -0700847//
848// If Blocking, select waits until exactly one state holds, i.e. a
849// channel becomes ready for the designated operation of sending or
850// receiving; select chooses one among the ready states
851// pseudorandomly, performs the send or receive operation, and sets
852// 'index' to the index of the chosen channel.
853//
854// If !Blocking, select doesn't block if no states hold; instead it
855// returns immediately with index equal to -1.
856//
Alan Donovan8097dad2013-06-24 14:15:13 -0400857// If the chosen channel was used for a receive, the r_i component is
858// set to the received value, where i is the index of that state among
859// all n receive states; otherwise r_i has the zero value of type T_i.
860// Note that the the receive index i is not the same as the state
861// index index.
Rob Pike83f21b92013-05-17 13:25:48 -0700862//
Alan Donovan8097dad2013-06-24 14:15:13 -0400863// The second component of the triple, recvOk, is a boolean whose value
Rob Pike83f21b92013-05-17 13:25:48 -0700864// is true iff the selected operation was a receive and the receive
865// successfully yielded a value.
866//
Rob Pike87334f42013-05-17 14:02:47 -0700867// Pos() returns the ast.SelectStmt.Select.
868//
Rob Pike83f21b92013-05-17 13:25:48 -0700869// Example printed form:
870// t3 = select nonblocking [<-t0, t1<-t2, ...]
871// t4 = select blocking []
872//
873type Select struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400874 register
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400875 States []*SelectState
Rob Pike83f21b92013-05-17 13:25:48 -0700876 Blocking bool
877}
878
Rob Pike87334f42013-05-17 14:02:47 -0700879// The Range instruction yields an iterator over the domain and range
880// of X, which must be a string or map.
Rob Pike83f21b92013-05-17 13:25:48 -0700881//
882// Elements are accessed via Next.
883//
Alan Donovan8097dad2013-06-24 14:15:13 -0400884// Type() returns an opaque and degenerate "rangeIter" type.
Rob Pike83f21b92013-05-17 13:25:48 -0700885//
Rob Pike87334f42013-05-17 14:02:47 -0700886// Pos() returns the ast.RangeStmt.For.
887//
Rob Pike83f21b92013-05-17 13:25:48 -0700888// Example printed form:
889// t0 = range "hello":string
890//
891type Range struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400892 register
Rob Pike83f21b92013-05-17 13:25:48 -0700893 X Value // string or map
894}
895
Rob Pike87334f42013-05-17 14:02:47 -0700896// The Next instruction reads and advances the (map or string)
897// iterator Iter and returns a 3-tuple value (ok, k, v). If the
898// iterator is not exhausted, ok is true and k and v are the next
899// elements of the domain and range, respectively. Otherwise ok is
900// false and k and v are undefined.
Rob Pike83f21b92013-05-17 13:25:48 -0700901//
902// Components of the tuple are accessed using Extract.
903//
904// The IsString field distinguishes iterators over strings from those
905// over maps, as the Type() alone is insufficient: consider
906// map[int]rune.
907//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400908// Type() returns a *types.Tuple for the triple (ok, k, v).
909// The types of k and/or v may be types.Invalid.
Rob Pike83f21b92013-05-17 13:25:48 -0700910//
911// Example printed form:
912// t1 = next t0
913//
914type Next struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400915 register
Rob Pike83f21b92013-05-17 13:25:48 -0700916 Iter Value
917 IsString bool // true => string iterator; false => map iterator.
918}
919
Rob Pike87334f42013-05-17 14:02:47 -0700920// The TypeAssert instruction tests whether interface value X has type
921// AssertedType.
Rob Pike83f21b92013-05-17 13:25:48 -0700922//
923// If !CommaOk, on success it returns v, the result of the conversion
924// (defined below); on failure it panics.
925//
926// If CommaOk: on success it returns a pair (v, true) where v is the
927// result of the conversion; on failure it returns (z, false) where z
928// is AssertedType's zero value. The components of the pair must be
929// accessed using the Extract instruction.
930//
931// If AssertedType is a concrete type, TypeAssert checks whether the
932// dynamic type in interface X is equal to it, and if so, the result
933// of the conversion is a copy of the value in the interface.
934//
935// If AssertedType is an interface, TypeAssert checks whether the
936// dynamic type of the interface is assignable to it, and if so, the
937// result of the conversion is a copy of the interface value X.
Alan Donovanae801632013-07-26 21:49:27 -0400938// If AssertedType is a superinterface of X.Type(), the operation will
939// fail iff the operand is nil. (Contrast with ChangeInterface, which
940// performs no nil-check.)
Rob Pike83f21b92013-05-17 13:25:48 -0700941//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400942// Type() reflects the actual type of the result, possibly a
943// 2-types.Tuple; AssertedType is the asserted type.
Rob Pike83f21b92013-05-17 13:25:48 -0700944//
Alan Donovan341a07a2013-06-13 14:43:35 -0400945// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
946// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400947// instruction arose from an explicit e.(T) operation; or the
948// ast.CaseClause.Case if the instruction arose from a case of a
949// type-switch statement.
Alan Donovan341a07a2013-06-13 14:43:35 -0400950//
Rob Pike83f21b92013-05-17 13:25:48 -0700951// Example printed form:
952// t1 = typeassert t0.(int)
953// t3 = typeassert,ok t2.(T)
954//
955type TypeAssert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400956 register
Rob Pike83f21b92013-05-17 13:25:48 -0700957 X Value
958 AssertedType types.Type
959 CommaOk bool
960}
961
Rob Pike87334f42013-05-17 14:02:47 -0700962// The Extract instruction yields component Index of Tuple.
Rob Pike83f21b92013-05-17 13:25:48 -0700963//
964// This is used to access the results of instructions with multiple
965// return values, such as Call, TypeAssert, Next, UnOp(ARROW) and
966// IndexExpr(Map).
967//
968// Example printed form:
969// t1 = extract t0 #1
970//
971type Extract struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400972 register
Rob Pike83f21b92013-05-17 13:25:48 -0700973 Tuple Value
974 Index int
975}
976
977// Instructions executed for effect. They do not yield a value. --------------------
978
Rob Pike87334f42013-05-17 14:02:47 -0700979// The Jump instruction transfers control to the sole successor of its
980// owning block.
Rob Pike83f21b92013-05-17 13:25:48 -0700981//
Rob Pike87334f42013-05-17 14:02:47 -0700982// A Jump must be the last instruction of its containing BasicBlock.
983//
984// Pos() returns NoPos.
Rob Pike83f21b92013-05-17 13:25:48 -0700985//
986// Example printed form:
987// jump done
988//
989type Jump struct {
990 anInstruction
991}
992
993// The If instruction transfers control to one of the two successors
994// of its owning block, depending on the boolean Cond: the first if
995// true, the second if false.
996//
997// An If instruction must be the last instruction of its containing
998// BasicBlock.
999//
Rob Pike87334f42013-05-17 14:02:47 -07001000// Pos() returns NoPos.
1001//
Rob Pike83f21b92013-05-17 13:25:48 -07001002// Example printed form:
1003// if t0 goto done else body
1004//
1005type If struct {
1006 anInstruction
1007 Cond Value
1008}
1009
Alan Donovan068f0172013-10-08 12:31:39 -04001010// The Return instruction returns values and control back to the calling
Rob Pike87334f42013-05-17 14:02:47 -07001011// function.
Rob Pike83f21b92013-05-17 13:25:48 -07001012//
1013// len(Results) is always equal to the number of results in the
1014// function's signature.
1015//
Alan Donovan068f0172013-10-08 12:31:39 -04001016// If len(Results) > 1, Return returns a tuple value with the specified
Rob Pike83f21b92013-05-17 13:25:48 -07001017// components which the caller must access using Extract instructions.
1018//
1019// There is no instruction to return a ready-made tuple like those
1020// returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or
1021// a tail-call to a function with multiple result parameters.
1022//
Alan Donovan068f0172013-10-08 12:31:39 -04001023// Return must be the last instruction of its containing BasicBlock.
Rob Pike83f21b92013-05-17 13:25:48 -07001024// Such a block has no successors.
1025//
Rob Pike87334f42013-05-17 14:02:47 -07001026// Pos() returns the ast.ReturnStmt.Return, if explicit in the source.
1027//
Rob Pike83f21b92013-05-17 13:25:48 -07001028// Example printed form:
Alan Donovan068f0172013-10-08 12:31:39 -04001029// return
1030// return nil:I, 2:int
Rob Pike83f21b92013-05-17 13:25:48 -07001031//
Alan Donovan068f0172013-10-08 12:31:39 -04001032type Return struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001033 anInstruction
1034 Results []Value
Rob Pike87334f42013-05-17 14:02:47 -07001035 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001036}
1037
Rob Pike87334f42013-05-17 14:02:47 -07001038// The RunDefers instruction pops and invokes the entire stack of
1039// procedure calls pushed by Defer instructions in this function.
Rob Pike83f21b92013-05-17 13:25:48 -07001040//
1041// It is legal to encounter multiple 'rundefers' instructions in a
1042// single control-flow path through a function; this is useful in
1043// the combined init() function, for example.
1044//
Rob Pike87334f42013-05-17 14:02:47 -07001045// Pos() returns NoPos.
1046//
Rob Pike83f21b92013-05-17 13:25:48 -07001047// Example printed form:
1048// rundefers
1049//
1050type RunDefers struct {
1051 anInstruction
1052}
1053
Rob Pike87334f42013-05-17 14:02:47 -07001054// The Panic instruction initiates a panic with value X.
Rob Pike83f21b92013-05-17 13:25:48 -07001055//
1056// A Panic instruction must be the last instruction of its containing
1057// BasicBlock, which must have no successors.
1058//
1059// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction;
1060// they are treated as calls to a built-in function.
1061//
Rob Pike87334f42013-05-17 14:02:47 -07001062// Pos() returns the ast.CallExpr.Lparen if this panic was explicit
1063// in the source.
1064//
Rob Pike83f21b92013-05-17 13:25:48 -07001065// Example printed form:
1066// panic t0
1067//
1068type Panic struct {
1069 anInstruction
Rob Pike87334f42013-05-17 14:02:47 -07001070 X Value // an interface{}
1071 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001072}
1073
Rob Pike87334f42013-05-17 14:02:47 -07001074// The Go instruction creates a new goroutine and calls the specified
1075// function within it.
Rob Pike83f21b92013-05-17 13:25:48 -07001076//
1077// See CallCommon for generic function call documentation.
1078//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001079// Pos() returns the ast.GoStmt.Go.
1080//
Rob Pike83f21b92013-05-17 13:25:48 -07001081// Example printed form:
1082// go println(t0, t1)
1083// go t3()
1084// go invoke t5.Println(...t6)
1085//
1086type Go struct {
1087 anInstruction
1088 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001089 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001090}
1091
Rob Pike87334f42013-05-17 14:02:47 -07001092// The Defer instruction pushes the specified call onto a stack of
1093// functions to be called by a RunDefers instruction or by a panic.
Rob Pike83f21b92013-05-17 13:25:48 -07001094//
1095// See CallCommon for generic function call documentation.
1096//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001097// Pos() returns the ast.DeferStmt.Defer.
1098//
Rob Pike83f21b92013-05-17 13:25:48 -07001099// Example printed form:
1100// defer println(t0, t1)
1101// defer t3()
1102// defer invoke t5.Println(...t6)
1103//
1104type Defer struct {
1105 anInstruction
1106 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001107 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001108}
1109
Rob Pike87334f42013-05-17 14:02:47 -07001110// The Send instruction sends X on channel Chan.
1111//
1112// Pos() returns the ast.SendStmt.Arrow, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001113//
1114// Example printed form:
1115// send t0 <- t1
1116//
1117type Send struct {
1118 anInstruction
1119 Chan, X Value
Rob Pike87334f42013-05-17 14:02:47 -07001120 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001121}
1122
Rob Pike87334f42013-05-17 14:02:47 -07001123// The Store instruction stores Val at address Addr.
Rob Pike83f21b92013-05-17 13:25:48 -07001124// Stores can be of arbitrary types.
1125//
Rob Pike87334f42013-05-17 14:02:47 -07001126// Pos() returns the ast.StarExpr.Star, if explicit in the source.
Rob Pike87334f42013-05-17 14:02:47 -07001127//
Rob Pike83f21b92013-05-17 13:25:48 -07001128// Example printed form:
1129// *x = y
1130//
1131type Store struct {
1132 anInstruction
1133 Addr Value
1134 Val Value
Rob Pike87334f42013-05-17 14:02:47 -07001135 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001136}
1137
Rob Pike87334f42013-05-17 14:02:47 -07001138// The MapUpdate instruction updates the association of Map[Key] to
1139// Value.
1140//
Alan Donovanc8a68902013-08-22 10:13:51 -04001141// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
1142// if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001143//
1144// Example printed form:
1145// t0[t1] = t2
1146//
1147type MapUpdate struct {
1148 anInstruction
1149 Map Value
1150 Key Value
1151 Value Value
Rob Pike87334f42013-05-17 14:02:47 -07001152 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001153}
1154
Alan Donovane2962652013-10-28 12:05:29 -04001155// A DebugRef instruction maps a source-level expression Expr to the
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001156// SSA value X that represents the value (!IsAddr) or address (IsAddr)
Alan Donovane2962652013-10-28 12:05:29 -04001157// of that expression.
Alan Donovan55d678e2013-07-15 13:56:46 -04001158//
1159// DebugRef is a pseudo-instruction: it has no dynamic effect.
1160//
Alan Donovane590cdb2013-10-09 12:47:30 -04001161// Pos() returns Expr.Pos(), the start position of the source-level
1162// expression. This is not the same as the "designated" token as
1163// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the
1164// position of the ("designated") Lparen token.
Alan Donovan55d678e2013-07-15 13:56:46 -04001165//
Alan Donovane2962652013-10-28 12:05:29 -04001166// If Expr is an *ast.Ident denoting a var or func, Object() returns
1167// the object; though this information can be obtained from the type
1168// checker, including it here greatly facilitates debugging.
1169// For non-Ident expressions, Object() returns nil.
1170//
1171// DebugRefs are generated only for functions built with debugging
1172// enabled; see Package.SetDebugMode().
1173//
1174// DebugRefs are not emitted for ast.Idents referring to constants or
1175// predeclared identifiers, since they are trivial and numerous.
1176// Nor are they emitted for ast.ParenExprs.
Alan Donovan55d678e2013-07-15 13:56:46 -04001177//
1178// (By representing these as instructions, rather than out-of-band,
1179// consistency is maintained during transformation passes by the
1180// ordinary SSA renaming machinery.)
1181//
Alan Donovane2962652013-10-28 12:05:29 -04001182// Example printed form:
1183// ; *ast.CallExpr @ 102:9 is t5
1184// ; var x float64 @ 109:72 is x
1185// ; address of *ast.CompositeLit @ 216:10 is t0
Alan Donovanaa238622013-10-27 10:55:21 -04001186//
Alan Donovan55d678e2013-07-15 13:56:46 -04001187type DebugRef struct {
1188 anInstruction
Alan Donovan9f640c22013-10-24 18:31:50 -04001189 Expr ast.Expr // the referring expression (never *ast.ParenExpr)
Alan Donovane2962652013-10-28 12:05:29 -04001190 object types.Object // the identity of the source var/func
Alan Donovan9f640c22013-10-24 18:31:50 -04001191 IsAddr bool // Expr is addressable and X is the address it denotes
Alan Donovane2962652013-10-28 12:05:29 -04001192 X Value // the value or address of Expr
Alan Donovan55d678e2013-07-15 13:56:46 -04001193}
1194
Rob Pike83f21b92013-05-17 13:25:48 -07001195// Embeddable mix-ins and helpers for common parts of other structs. -----------
1196
Alan Donovan1ff34522013-10-14 13:48:34 -04001197// register is a mix-in embedded by all SSA values that are also
1198// instructions, i.e. virtual registers, and provides a uniform
1199// implementation of most of the Value interface: Value.Name() is a
1200// numbered register (e.g. "t0"); the other methods are field accessors.
Rob Pike83f21b92013-05-17 13:25:48 -07001201//
Alan Donovan1ff34522013-10-14 13:48:34 -04001202// Temporary names are automatically assigned to each register on
Rob Pike83f21b92013-05-17 13:25:48 -07001203// completion of building a function in SSA form.
1204//
1205// Clients must not assume that the 'id' value (and the Name() derived
1206// from it) is unique within a function. As always in this API,
1207// semantics are determined only by identity; names exist only to
1208// facilitate debugging.
1209//
Alan Donovan1ff34522013-10-14 13:48:34 -04001210type register struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001211 anInstruction
1212 num int // "name" of virtual register, e.g. "t0". Not guaranteed unique.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001213 typ types.Type // type of virtual register
Rob Pike87334f42013-05-17 14:02:47 -07001214 pos token.Pos // position of source expression, or NoPos
Rob Pike83f21b92013-05-17 13:25:48 -07001215 referrers []Instruction
1216}
1217
1218// anInstruction is a mix-in embedded by all Instructions.
Alan Donovanf1e5b032013-11-07 10:08:51 -05001219// It provides the implementations of the Block and setBlock methods.
Rob Pike83f21b92013-05-17 13:25:48 -07001220type anInstruction struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001221 block *BasicBlock // the basic block of this instruction
Rob Pike83f21b92013-05-17 13:25:48 -07001222}
1223
1224// CallCommon is contained by Go, Defer and Call to hold the
1225// common parts of a function or method call.
1226//
1227// Each CallCommon exists in one of two modes, function call and
1228// interface method invocation, or "call" and "invoke" for short.
1229//
Alan Donovan4da31df2013-07-26 11:22:34 -04001230// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon
Alan Donovan70722532013-08-19 15:38:30 -04001231// represents an ordinary function call of the value in Value,
1232// which may be a *Builtin, a *Function or any other value of kind
1233// 'func'.
Rob Pike83f21b92013-05-17 13:25:48 -07001234//
Alan Donovan118786e2013-07-26 14:06:26 -04001235// In the common case in which Value is a *Function, this indicates a
Rob Pike83f21b92013-05-17 13:25:48 -07001236// statically dispatched call to a package-level function, an
1237// anonymous function, or a method of a named type. Also statically
Alan Donovan118786e2013-07-26 14:06:26 -04001238// dispatched, but less common, Value may be a *MakeClosure, indicating
Rob Pike83f21b92013-05-17 13:25:48 -07001239// an immediately applied function literal with free variables. Any
Alan Donovan118786e2013-07-26 14:06:26 -04001240// other value of Value indicates a dynamically dispatched function
Rob Pike83f21b92013-05-17 13:25:48 -07001241// call. The StaticCallee method returns the callee in these cases.
1242//
Alan Donovan118786e2013-07-26 14:06:26 -04001243// Args contains the arguments to the call. If Value is a method,
1244// Args[0] contains the receiver parameter.
Rob Pike83f21b92013-05-17 13:25:48 -07001245//
1246// Example printed form:
1247// t2 = println(t0, t1)
1248// go t3()
1249// defer t5(...t6)
1250//
Alan Donovan4da31df2013-07-26 11:22:34 -04001251// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon
Rob Pike83f21b92013-05-17 13:25:48 -07001252// represents a dynamically dispatched call to an interface method.
Alan Donovan118786e2013-07-26 14:06:26 -04001253// In this mode, Value is the interface value and Method is the
Alan Donovan70722532013-08-19 15:38:30 -04001254// interface's abstract method. Note: an abstract method may be
1255// shared by multiple interfaces due to embedding; Value.Type()
1256// provides the specific interface used for this call.
Rob Pike83f21b92013-05-17 13:25:48 -07001257//
Alan Donovan118786e2013-07-26 14:06:26 -04001258// Value is implicitly supplied to the concrete method implementation
Rob Pike83f21b92013-05-17 13:25:48 -07001259// as the receiver parameter; in other words, Args[0] holds not the
Alan Donovan118786e2013-07-26 14:06:26 -04001260// receiver but the first true argument.
Rob Pike83f21b92013-05-17 13:25:48 -07001261//
Rob Pike83f21b92013-05-17 13:25:48 -07001262// Example printed form:
1263// t1 = invoke t0.String()
1264// go invoke t3.Run(t2)
1265// defer invoke t4.Handle(...t5)
1266//
1267// In both modes, HasEllipsis is true iff the last element of Args is
1268// a slice value containing zero or more arguments to a variadic
1269// function. (This is not semantically significant since the type of
1270// the called function is sufficient to determine this, but it aids
1271// readability of the printed form.)
1272//
1273type CallCommon struct {
Alan Donovan118786e2013-07-26 14:06:26 -04001274 Value Value // receiver (invoke mode) or func value (call mode)
1275 Method *types.Func // abstract method (invoke mode)
1276 Args []Value // actual parameters (in static method call, includes receiver)
Alan Donovan4da31df2013-07-26 11:22:34 -04001277 HasEllipsis bool // true iff last Args is a slice of '...' args (needed?)
1278 pos token.Pos // position of CallExpr.Lparen, iff explicit in source
Rob Pike83f21b92013-05-17 13:25:48 -07001279}
1280
1281// IsInvoke returns true if this call has "invoke" (not "call") mode.
1282func (c *CallCommon) IsInvoke() bool {
Alan Donovan4da31df2013-07-26 11:22:34 -04001283 return c.Method != nil
Rob Pike83f21b92013-05-17 13:25:48 -07001284}
1285
Rob Pike87334f42013-05-17 14:02:47 -07001286func (c *CallCommon) Pos() token.Pos { return c.pos }
1287
Alan Donovan8097dad2013-06-24 14:15:13 -04001288// Signature returns the signature of the called function.
1289//
1290// For an "invoke"-mode call, the signature of the interface method is
Alan Donovanb68a0292013-06-26 12:38:08 -04001291// returned.
1292//
1293// In either "call" or "invoke" mode, if the callee is a method, its
1294// receiver is represented by sig.Recv, not sig.Params().At(0).
Alan Donovan8097dad2013-06-24 14:15:13 -04001295//
Alan Donovanea8ba6f2013-07-03 15:10:49 -04001296// Signature returns nil for a call to a built-in function.
1297//
Alan Donovan8097dad2013-06-24 14:15:13 -04001298func (c *CallCommon) Signature() *types.Signature {
Alan Donovan4da31df2013-07-26 11:22:34 -04001299 if c.Method != nil {
1300 return c.Method.Type().(*types.Signature)
Alan Donovan8097dad2013-06-24 14:15:13 -04001301 }
Alan Donovan118786e2013-07-26 14:06:26 -04001302 sig, _ := c.Value.Type().Underlying().(*types.Signature) // nil for *Builtin
Alan Donovanea8ba6f2013-07-03 15:10:49 -04001303 return sig
Alan Donovan8097dad2013-06-24 14:15:13 -04001304}
1305
Rob Pike83f21b92013-05-17 13:25:48 -07001306// StaticCallee returns the called function if this is a trivially
1307// static "call"-mode call.
1308func (c *CallCommon) StaticCallee() *Function {
Alan Donovan118786e2013-07-26 14:06:26 -04001309 switch fn := c.Value.(type) {
Rob Pike83f21b92013-05-17 13:25:48 -07001310 case *Function:
1311 return fn
1312 case *MakeClosure:
1313 return fn.Fn.(*Function)
1314 }
1315 return nil
1316}
1317
Rob Pike83f21b92013-05-17 13:25:48 -07001318// Description returns a description of the mode of this call suitable
1319// for a user interface, e.g. "static method call".
1320func (c *CallCommon) Description() string {
Alan Donovan118786e2013-07-26 14:06:26 -04001321 switch fn := c.Value.(type) {
Alan Donovan70722532013-08-19 15:38:30 -04001322 case *Builtin:
1323 return "built-in function call"
Rob Pike83f21b92013-05-17 13:25:48 -07001324 case *MakeClosure:
1325 return "static function closure call"
1326 case *Function:
Rob Pike87334f42013-05-17 14:02:47 -07001327 if fn.Signature.Recv() != nil {
Rob Pike83f21b92013-05-17 13:25:48 -07001328 return "static method call"
1329 }
1330 return "static function call"
1331 }
Alan Donovan70722532013-08-19 15:38:30 -04001332 if c.IsInvoke() {
1333 return "dynamic method call" // ("invoke" mode)
1334 }
Rob Pike83f21b92013-05-17 13:25:48 -07001335 return "dynamic function call"
1336}
1337
Alan Donovan8097dad2013-06-24 14:15:13 -04001338// The CallInstruction interface, implemented by *Go, *Defer and *Call,
1339// exposes the common parts of function calling instructions,
1340// yet provides a way back to the Value defined by *Call alone.
1341//
1342type CallInstruction interface {
1343 Instruction
1344 Common() *CallCommon // returns the common parts of the call
1345 Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer)
1346}
1347
1348func (s *Call) Common() *CallCommon { return &s.Call }
1349func (s *Defer) Common() *CallCommon { return &s.Call }
1350func (s *Go) Common() *CallCommon { return &s.Call }
1351
1352func (s *Call) Value() *Call { return s }
1353func (s *Defer) Value() *Call { return nil }
1354func (s *Go) Value() *Call { return nil }
1355
Alan Donovan55d678e2013-07-15 13:56:46 -04001356func (v *Builtin) Type() types.Type { return v.object.Type() }
1357func (v *Builtin) Name() string { return v.object.Name() }
Rob Pike83f21b92013-05-17 13:25:48 -07001358func (*Builtin) Referrers() *[]Instruction { return nil }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001359func (v *Builtin) Pos() token.Pos { return token.NoPos }
Alan Donovan55d678e2013-07-15 13:56:46 -04001360func (v *Builtin) Object() types.Object { return v.object }
Rob Pike83f21b92013-05-17 13:25:48 -07001361
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001362func (v *Capture) Type() types.Type { return v.typ }
1363func (v *Capture) Name() string { return v.name }
Rob Pike83f21b92013-05-17 13:25:48 -07001364func (v *Capture) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001365func (v *Capture) Pos() token.Pos { return v.pos }
Alan Donovan341a07a2013-06-13 14:43:35 -04001366func (v *Capture) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001367
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001368func (v *Global) Type() types.Type { return v.typ }
1369func (v *Global) Name() string { return v.name }
1370func (v *Global) Pos() token.Pos { return v.pos }
1371func (v *Global) Referrers() *[]Instruction { return nil }
1372func (v *Global) Token() token.Token { return token.VAR }
1373func (v *Global) Object() types.Object { return v.object }
1374func (v *Global) String() string { return v.RelString(nil) }
1375func (v *Global) Package() *Package { return v.Pkg }
1376func (v *Global) RelString(from *types.Package) string { return relString(v, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001377
Alan Donovanf1e5b032013-11-07 10:08:51 -05001378func (v *Function) Name() string { return v.name }
1379func (v *Function) Type() types.Type { return v.Signature }
1380func (v *Function) Pos() token.Pos { return v.pos }
1381func (v *Function) Token() token.Token { return token.FUNC }
1382func (v *Function) Object() types.Object { return v.object }
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001383func (v *Function) String() string { return v.RelString(nil) }
1384func (v *Function) Package() *Package { return v.Pkg }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001385func (v *Function) Referrers() *[]Instruction {
1386 if v.Enclosing != nil {
1387 return &v.referrers
1388 }
1389 return nil
1390}
Rob Pike83f21b92013-05-17 13:25:48 -07001391
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001392func (v *Parameter) Type() types.Type { return v.typ }
1393func (v *Parameter) Name() string { return v.name }
Alan Donovan55d678e2013-07-15 13:56:46 -04001394func (v *Parameter) Object() types.Object { return v.object }
Rob Pike83f21b92013-05-17 13:25:48 -07001395func (v *Parameter) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001396func (v *Parameter) Pos() token.Pos { return v.pos }
Alan Donovan341a07a2013-06-13 14:43:35 -04001397func (v *Parameter) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001398
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001399func (v *Alloc) Type() types.Type { return v.typ }
Rob Pike83f21b92013-05-17 13:25:48 -07001400func (v *Alloc) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001401func (v *Alloc) Pos() token.Pos { return v.pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001402
Alan Donovan1ff34522013-10-14 13:48:34 -04001403func (v *register) Type() types.Type { return v.typ }
1404func (v *register) setType(typ types.Type) { v.typ = typ }
1405func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) }
1406func (v *register) setNum(num int) { v.num = num }
1407func (v *register) Referrers() *[]Instruction { return &v.referrers }
1408func (v *register) Pos() token.Pos { return v.pos }
1409func (v *register) setPos(pos token.Pos) { v.pos = pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001410
Alan Donovan341a07a2013-06-13 14:43:35 -04001411func (v *anInstruction) Parent() *Function { return v.block.parent }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001412func (v *anInstruction) Block() *BasicBlock { return v.block }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001413func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block }
Rob Pike83f21b92013-05-17 13:25:48 -07001414
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001415func (t *Type) Name() string { return t.object.Name() }
1416func (t *Type) Pos() token.Pos { return t.object.Pos() }
1417func (t *Type) Type() types.Type { return t.object.Type() }
1418func (t *Type) Token() token.Token { return token.TYPE }
1419func (t *Type) Object() types.Object { return t.object }
1420func (t *Type) String() string { return t.RelString(nil) }
1421func (t *Type) Package() *Package { return t.pkg }
1422func (t *Type) RelString(from *types.Package) string { return relString(t, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001423
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001424func (c *NamedConst) Name() string { return c.object.Name() }
1425func (c *NamedConst) Pos() token.Pos { return c.object.Pos() }
1426func (c *NamedConst) String() string { return c.RelString(nil) }
1427func (c *NamedConst) Type() types.Type { return c.object.Type() }
1428func (c *NamedConst) Token() token.Token { return token.CONST }
1429func (c *NamedConst) Object() types.Object { return c.object }
1430func (c *NamedConst) Package() *Package { return c.pkg }
1431func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001432
1433// Func returns the package-level function of the specified name,
1434// or nil if not found.
1435//
1436func (p *Package) Func(name string) (f *Function) {
1437 f, _ = p.Members[name].(*Function)
1438 return
1439}
1440
1441// Var returns the package-level variable of the specified name,
1442// or nil if not found.
1443//
1444func (p *Package) Var(name string) (g *Global) {
1445 g, _ = p.Members[name].(*Global)
1446 return
1447}
1448
1449// Const returns the package-level constant of the specified name,
1450// or nil if not found.
1451//
Alan Donovan732dbe92013-07-16 13:50:08 -04001452func (p *Package) Const(name string) (c *NamedConst) {
1453 c, _ = p.Members[name].(*NamedConst)
Rob Pike83f21b92013-05-17 13:25:48 -07001454 return
1455}
1456
1457// Type returns the package-level type of the specified name,
1458// or nil if not found.
1459//
1460func (p *Package) Type(name string) (t *Type) {
1461 t, _ = p.Members[name].(*Type)
1462 return
1463}
1464
Rob Pike87334f42013-05-17 14:02:47 -07001465func (v *Call) Pos() token.Pos { return v.Call.pos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001466func (s *Defer) Pos() token.Pos { return s.pos }
1467func (s *Go) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001468func (s *MapUpdate) Pos() token.Pos { return s.pos }
1469func (s *Panic) Pos() token.Pos { return s.pos }
Alan Donovan068f0172013-10-08 12:31:39 -04001470func (s *Return) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001471func (s *Send) Pos() token.Pos { return s.pos }
1472func (s *Store) Pos() token.Pos { return s.pos }
1473func (s *If) Pos() token.Pos { return token.NoPos }
1474func (s *Jump) Pos() token.Pos { return token.NoPos }
1475func (s *RunDefers) Pos() token.Pos { return token.NoPos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001476func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() }
Rob Pike83f21b92013-05-17 13:25:48 -07001477
Rob Pike87334f42013-05-17 14:02:47 -07001478// Operands.
Rob Pike83f21b92013-05-17 13:25:48 -07001479
1480func (v *Alloc) Operands(rands []*Value) []*Value {
1481 return rands
1482}
1483
1484func (v *BinOp) Operands(rands []*Value) []*Value {
1485 return append(rands, &v.X, &v.Y)
1486}
1487
1488func (c *CallCommon) Operands(rands []*Value) []*Value {
Alan Donovan118786e2013-07-26 14:06:26 -04001489 rands = append(rands, &c.Value)
Rob Pike83f21b92013-05-17 13:25:48 -07001490 for i := range c.Args {
1491 rands = append(rands, &c.Args[i])
1492 }
1493 return rands
1494}
1495
1496func (s *Go) Operands(rands []*Value) []*Value {
1497 return s.Call.Operands(rands)
1498}
1499
1500func (s *Call) Operands(rands []*Value) []*Value {
1501 return s.Call.Operands(rands)
1502}
1503
1504func (s *Defer) Operands(rands []*Value) []*Value {
1505 return s.Call.Operands(rands)
1506}
1507
1508func (v *ChangeInterface) Operands(rands []*Value) []*Value {
1509 return append(rands, &v.X)
1510}
1511
Rob Pike87334f42013-05-17 14:02:47 -07001512func (v *ChangeType) Operands(rands []*Value) []*Value {
1513 return append(rands, &v.X)
1514}
1515
1516func (v *Convert) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001517 return append(rands, &v.X)
1518}
1519
Alan Donovan55d678e2013-07-15 13:56:46 -04001520func (s *DebugRef) Operands(rands []*Value) []*Value {
1521 return append(rands, &s.X)
1522}
1523
Rob Pike83f21b92013-05-17 13:25:48 -07001524func (v *Extract) Operands(rands []*Value) []*Value {
1525 return append(rands, &v.Tuple)
1526}
1527
1528func (v *Field) Operands(rands []*Value) []*Value {
1529 return append(rands, &v.X)
1530}
1531
1532func (v *FieldAddr) Operands(rands []*Value) []*Value {
1533 return append(rands, &v.X)
1534}
1535
1536func (s *If) Operands(rands []*Value) []*Value {
1537 return append(rands, &s.Cond)
1538}
1539
1540func (v *Index) Operands(rands []*Value) []*Value {
1541 return append(rands, &v.X, &v.Index)
1542}
1543
1544func (v *IndexAddr) Operands(rands []*Value) []*Value {
1545 return append(rands, &v.X, &v.Index)
1546}
1547
1548func (*Jump) Operands(rands []*Value) []*Value {
1549 return rands
1550}
1551
1552func (v *Lookup) Operands(rands []*Value) []*Value {
1553 return append(rands, &v.X, &v.Index)
1554}
1555
1556func (v *MakeChan) Operands(rands []*Value) []*Value {
1557 return append(rands, &v.Size)
1558}
1559
1560func (v *MakeClosure) Operands(rands []*Value) []*Value {
1561 rands = append(rands, &v.Fn)
1562 for i := range v.Bindings {
1563 rands = append(rands, &v.Bindings[i])
1564 }
1565 return rands
1566}
1567
1568func (v *MakeInterface) Operands(rands []*Value) []*Value {
1569 return append(rands, &v.X)
1570}
1571
1572func (v *MakeMap) Operands(rands []*Value) []*Value {
1573 return append(rands, &v.Reserve)
1574}
1575
1576func (v *MakeSlice) Operands(rands []*Value) []*Value {
1577 return append(rands, &v.Len, &v.Cap)
1578}
1579
1580func (v *MapUpdate) Operands(rands []*Value) []*Value {
1581 return append(rands, &v.Map, &v.Key, &v.Value)
1582}
1583
1584func (v *Next) Operands(rands []*Value) []*Value {
1585 return append(rands, &v.Iter)
1586}
1587
1588func (s *Panic) Operands(rands []*Value) []*Value {
1589 return append(rands, &s.X)
1590}
1591
1592func (v *Phi) Operands(rands []*Value) []*Value {
1593 for i := range v.Edges {
1594 rands = append(rands, &v.Edges[i])
1595 }
1596 return rands
1597}
1598
1599func (v *Range) Operands(rands []*Value) []*Value {
1600 return append(rands, &v.X)
1601}
1602
Alan Donovan068f0172013-10-08 12:31:39 -04001603func (s *Return) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001604 for i := range s.Results {
1605 rands = append(rands, &s.Results[i])
1606 }
1607 return rands
1608}
1609
1610func (*RunDefers) Operands(rands []*Value) []*Value {
1611 return rands
1612}
1613
1614func (v *Select) Operands(rands []*Value) []*Value {
1615 for i := range v.States {
1616 rands = append(rands, &v.States[i].Chan, &v.States[i].Send)
1617 }
1618 return rands
1619}
1620
1621func (s *Send) Operands(rands []*Value) []*Value {
1622 return append(rands, &s.Chan, &s.X)
1623}
1624
1625func (v *Slice) Operands(rands []*Value) []*Value {
1626 return append(rands, &v.X, &v.Low, &v.High)
1627}
1628
1629func (s *Store) Operands(rands []*Value) []*Value {
1630 return append(rands, &s.Addr, &s.Val)
1631}
1632
1633func (v *TypeAssert) Operands(rands []*Value) []*Value {
1634 return append(rands, &v.X)
1635}
1636
1637func (v *UnOp) Operands(rands []*Value) []*Value {
1638 return append(rands, &v.X)
1639}