blob: 7cde827cec27044c3697d45ce63d30ded033e0e8 [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"
Alan Donovan542ffc72015-12-29 13:06:30 -050013 exact "go/constant"
Rob Pike83f21b92013-05-17 13:25:48 -070014 "go/token"
Alan Donovan542ffc72015-12-29 13:06:30 -050015 "go/types"
Rob Pike83f21b92013-05-17 13:25:48 -070016 "sync"
17
Andrew Gerrand5ebbcd12014-11-10 08:50:40 +110018 "golang.org/x/tools/go/types/typeutil"
Rob Pike83f21b92013-05-17 13:25:48 -070019)
20
21// A Program is a partial or complete Go program converted to SSA form.
Rob Pike83f21b92013-05-17 13:25:48 -070022type Program struct {
Alan Donovan1f29e742014-02-11 16:49:27 -050023 Fset *token.FileSet // position information for the files of this Program
24 imported map[string]*Package // all importable Packages, keyed by import path
25 packages map[*types.Package]*Package // all loaded Packages, keyed by object
26 mode BuilderMode // set of mode bits for SSA construction
Robert Griesemer3f8eecd2015-06-05 10:18:37 -070027 MethodSets typeutil.MethodSetCache // cache of type-checker's method-sets
Rob Pike83f21b92013-05-17 13:25:48 -070028
Alan Donovanf0116312014-12-29 13:20:22 -050029 methodsMu sync.Mutex // guards the following maps:
30 methodSets typeutil.Map // maps type to its concrete methodSet
31 runtimeTypes typeutil.Map // types for which rtypes are needed
32 canon typeutil.Map // type canonicalization map
33 bounds map[*types.Func]*Function // bounds for curried x.Method closures
34 thunks map[selectionKey]*Function // thunks for T.Method expressions
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//
Alan Donovan99577392015-03-05 14:35:50 -050042// Members also contains entries for "init" (the synthetic package
43// initializer) and "init#%d", the nth declared init function,
44// and unspecified other things too.
45//
Rob Pike83f21b92013-05-17 13:25:48 -070046type Package struct {
Alan Donovanf0116312014-12-29 13:20:22 -050047 Prog *Program // the owning program
Alan Donovanafcda552015-08-31 17:50:03 -040048 Pkg *types.Package // the corresponding go/types.Package
Alan Donovan99577392015-03-05 14:35:50 -050049 Members map[string]Member // all package members keyed by name (incl. init and init#%d)
Alan Donovanf0116312014-12-29 13:20:22 -050050 values map[types.Object]Value // package members (incl. types and methods), keyed by object
51 init *Function // Func("init"); the package's init function
Alan Donovanf9642692015-02-20 10:42:06 -050052 debug bool // include full debug info in this package
Rob Pike83f21b92013-05-17 13:25:48 -070053
Alan Donovan4d628a02013-06-03 14:15:19 -040054 // The following fields are set transiently, then cleared
55 // after building.
Alan Donovan80495532015-09-18 11:01:08 -040056 buildOnce sync.Once // ensures package building occurs once
57 ninit int32 // number of init functions
58 info *types.Info // package type information
59 files []*ast.File // package ASTs
Rob Pike83f21b92013-05-17 13:25:48 -070060}
61
Alan Donovan732dbe92013-07-16 13:50:08 -040062// A Member is a member of a Go package, implemented by *NamedConst,
Rob Pike83f21b92013-05-17 13:25:48 -070063// *Global, *Function, or *Type; they are created by package-level
64// const, var, func and type declarations respectively.
65//
66type Member interface {
Alan Donovan9fcd20e2013-11-15 09:21:48 -050067 Name() string // declared name of the package member
68 String() string // package-qualified name of the package member
69 RelString(*types.Package) string // like String, but relative refs are unqualified
70 Object() types.Object // typechecker's object for this member, if any
71 Pos() token.Pos // position of member's declaration, if known
72 Type() types.Type // type of the package member
73 Token() token.Token // token.{VAR,FUNC,CONST,TYPE}
Alan Donovanf9642692015-02-20 10:42:06 -050074 Package() *Package // the containing package
Rob Pike83f21b92013-05-17 13:25:48 -070075}
76
Alan Donovan341a07a2013-06-13 14:43:35 -040077// A Type is a Member of a Package representing a package-level named type.
Rob Pike83f21b92013-05-17 13:25:48 -070078type 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 Donovanf9642692015-02-20 10:42:06 -050083// A NamedConst is a Member of a Package representing a package-level
84// named constant.
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 Donovan9fcd20e2013-11-15 09:21:48 -050095 pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -070096}
97
Alan Donovanf3eb05b2014-01-13 16:45:46 -050098// A Value is an SSA value that can be referenced by an instruction.
Rob Pike83f21b92013-05-17 13:25:48 -070099type Value interface {
100 // Name returns the name of this value, and determines how
101 // this Value appears when used as an operand of an
102 // Instruction.
103 //
104 // This is the same as the source name for Parameters,
Alan Donovancc02c5b2014-06-11 14:04:45 -0400105 // Builtins, Functions, FreeVars, Globals.
Alan Donovan732dbe92013-07-16 13:50:08 -0400106 // For constants, it is a representation of the constant's value
Rob Pike83f21b92013-05-17 13:25:48 -0700107 // and type. For all other Values this is the name of the
108 // virtual register defined by the instruction.
109 //
110 // The name of an SSA Value is not semantically significant,
111 // and may not even be unique within a function.
112 Name() string
113
114 // If this value is an Instruction, String returns its
115 // disassembled form; otherwise it returns unspecified
116 // human-readable information about the Value, such as its
117 // kind, name and type.
118 String() string
119
120 // Type returns the type of this value. Many instructions
Alan Donovanbac70982013-10-29 11:07:09 -0400121 // (e.g. IndexAddr) change their behaviour depending on the
Rob Pike83f21b92013-05-17 13:25:48 -0700122 // types of their operands.
123 Type() types.Type
124
Alan Donovan04427c82014-06-11 13:14:06 -0400125 // Parent returns the function to which this Value belongs.
126 // It returns nil for named Functions, Builtin, Const and Global.
127 Parent() *Function
128
Rob Pike83f21b92013-05-17 13:25:48 -0700129 // Referrers returns the list of instructions that have this
130 // value as one of their operands; it may contain duplicates
131 // if an instruction has a repeated operand.
132 //
133 // Referrers actually returns a pointer through which the
134 // caller may perform mutations to the object's state.
135 //
Alan Donovan04427c82014-06-11 13:14:06 -0400136 // Referrers is currently only defined if Parent()!=nil,
Alan Donovancc02c5b2014-06-11 14:04:45 -0400137 // i.e. for the function-local values FreeVar, Parameter,
Alan Donovan04427c82014-06-11 13:14:06 -0400138 // Functions (iff anonymous) and all value-defining instructions.
Alan Donovanf1e5b032013-11-07 10:08:51 -0500139 // It returns nil for named Functions, Builtin, Const and Global.
Rob Pike83f21b92013-05-17 13:25:48 -0700140 //
141 // Instruction.Operands contains the inverse of this relation.
142 Referrers() *[]Instruction
143
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400144 // Pos returns the location of the AST token most closely
145 // associated with the operation that gave rise to this value,
146 // or token.NoPos if it was not explicit in the source.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400147 //
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400148 // For each ast.Node type, a particular token is designated as
149 // the closest location for the expression, e.g. the Lparen
150 // for an *ast.CallExpr. This permits a compact but
151 // approximate mapping from Values to source positions for use
152 // in diagnostic messages, for example.
153 //
154 // (Do not use this position to determine which Value
155 // corresponds to an ast.Expr; use Function.ValueForExpr
156 // instead. NB: it requires that the function was built with
157 // debug information.)
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400158 Pos() token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -0700159}
160
161// An Instruction is an SSA instruction that computes a new Value or
162// has some effect.
163//
164// An Instruction that defines a value (e.g. BinOp) also implements
165// the Value interface; an Instruction that only has an effect (e.g. Store)
166// does not.
167//
168type Instruction interface {
Alan Donovanf9642692015-02-20 10:42:06 -0500169 // String returns the disassembled form of this value.
Rob Pike83f21b92013-05-17 13:25:48 -0700170 //
Alan Donovanf9642692015-02-20 10:42:06 -0500171 // Examples of Instructions that are Values:
172 // "x + y" (BinOp)
Rob Pike83f21b92013-05-17 13:25:48 -0700173 // "len([])" (Call)
174 // Note that the name of the Value is not printed.
175 //
Alan Donovanf9642692015-02-20 10:42:06 -0500176 // Examples of Instructions that are not Values:
177 // "return x" (Return)
Rob Pike83f21b92013-05-17 13:25:48 -0700178 // "*y = x" (Store)
179 //
Alan Donovanf9642692015-02-20 10:42:06 -0500180 // (The separation Value.Name() from Value.String() is useful
181 // for some analyses which distinguish the operation from the
182 // value it defines, e.g., 'y = local int' is both an allocation
183 // of memory 'local int' and a definition of a pointer y.)
Rob Pike83f21b92013-05-17 13:25:48 -0700184 String() string
185
Alan Donovan341a07a2013-06-13 14:43:35 -0400186 // Parent returns the function to which this instruction
187 // belongs.
188 Parent() *Function
189
Rob Pike83f21b92013-05-17 13:25:48 -0700190 // Block returns the basic block to which this instruction
191 // belongs.
192 Block() *BasicBlock
193
Alan Donovanf1e5b032013-11-07 10:08:51 -0500194 // setBlock sets the basic block to which this instruction belongs.
195 setBlock(*BasicBlock)
Rob Pike83f21b92013-05-17 13:25:48 -0700196
197 // Operands returns the operands of this instruction: the
198 // set of Values it references.
199 //
200 // Specifically, it appends their addresses to rands, a
201 // user-provided slice, and returns the resulting slice,
202 // permitting avoidance of memory allocation.
203 //
Alan Donovan04427c82014-06-11 13:14:06 -0400204 // The operands are appended in undefined order, but the order
205 // is consistent for a given Instruction; the addresses are
206 // always non-nil but may point to a nil Value. Clients may
207 // store through the pointers, e.g. to effect a value
Rob Pike83f21b92013-05-17 13:25:48 -0700208 // renaming.
209 //
210 // Value.Referrers is a subset of the inverse of this
211 // relation. (Referrers are not tracked for all types of
212 // Values.)
213 Operands(rands []*Value) []*Value
214
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400215 // Pos returns the location of the AST token most closely
216 // associated with the operation that gave rise to this
217 // instruction, or token.NoPos if it was not explicit in the
218 // source.
Rob Pike87334f42013-05-17 14:02:47 -0700219 //
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400220 // For each ast.Node type, a particular token is designated as
221 // the closest location for the expression, e.g. the Go token
222 // for an *ast.GoStmt. This permits a compact but approximate
223 // mapping from Instructions to source positions for use in
224 // diagnostic messages, for example.
225 //
226 // (Do not use this position to determine which Instruction
227 // corresponds to an ast.Expr; see the notes for Value.Pos.
228 // This position may be used to determine which non-Value
229 // Instruction corresponds to some ast.Stmts, but not all: If
230 // and Jump instructions have no Pos(), for example.)
Rob Pike87334f42013-05-17 14:02:47 -0700231 Pos() token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -0700232}
233
Alan Donovan04427c82014-06-11 13:14:06 -0400234// A Node is a node in the SSA value graph. Every concrete type that
235// implements Node is also either a Value, an Instruction, or both.
236//
237// Node contains the methods common to Value and Instruction, plus the
238// Operands and Referrers methods generalized to return nil for
239// non-Instructions and non-Values, respectively.
240//
241// Node is provided to simplify SSA graph algorithms. Clients should
242// use the more specific and informative Value or Instruction
243// interfaces where appropriate.
244//
245type Node interface {
246 // Common methods:
247 String() string
248 Pos() token.Pos
249 Parent() *Function
250
251 // Partial methods:
252 Operands(rands []*Value) []*Value // nil for non-Instructions
253 Referrers() *[]Instruction // nil for non-Values
254}
255
Alan Donovanf9642692015-02-20 10:42:06 -0500256// Function represents the parameters, results, and code of a function
Rob Pike83f21b92013-05-17 13:25:48 -0700257// or method.
258//
259// If Blocks is nil, this indicates an external function for which no
Alan Donovanf3eb05b2014-01-13 16:45:46 -0500260// Go source code is available. In this case, FreeVars and Locals
Alan Donovanf9642692015-02-20 10:42:06 -0500261// are nil too. Clients performing whole-program analysis must
Rob Pike83f21b92013-05-17 13:25:48 -0700262// handle external functions specially.
263//
Alan Donovanb5016cb2013-12-05 09:50:18 -0500264// Blocks contains the function's control-flow graph (CFG).
Rob Pike83f21b92013-05-17 13:25:48 -0700265// Blocks[0] is the function entry point; block order is not otherwise
266// semantically significant, though it may affect the readability of
267// the disassembly.
Alan Donovanb5016cb2013-12-05 09:50:18 -0500268// To iterate over the blocks in dominance order, use DomPreorder().
Rob Pike83f21b92013-05-17 13:25:48 -0700269//
Alan Donovan2accef22013-10-14 15:38:56 -0400270// Recover is an optional second entry point to which control resumes
Peter Collingbournece1e99a2014-06-13 13:08:35 -0400271// after a recovered panic. The Recover block may contain only a return
272// statement, preceded by a load of the function's named return
273// parameters, if any.
Alan Donovan2accef22013-10-14 15:38:56 -0400274//
Alan Donovan04427c82014-06-11 13:14:06 -0400275// A nested function (Parent()!=nil) that refers to one or more
Alan Donovanf9642692015-02-20 10:42:06 -0500276// lexically enclosing local variables ("free variables") has FreeVars.
277// Such functions cannot be called directly but require a
Alan Donovan04427c82014-06-11 13:14:06 -0400278// value created by MakeClosure which, via its Bindings, supplies
279// values for these parameters.
Rob Pike83f21b92013-05-17 13:25:48 -0700280//
Rob Pike87334f42013-05-17 14:02:47 -0700281// If the function is a method (Signature.Recv() != nil) then the first
Rob Pike83f21b92013-05-17 13:25:48 -0700282// element of Params is the receiver parameter.
283//
Alan Donovan99577392015-03-05 14:35:50 -0500284// A Go package may declare many functions called "init".
285// For each one, Object().Name() returns "init" but Name() returns
286// "init#1", etc, in declaration order.
287//
Rob Pike87334f42013-05-17 14:02:47 -0700288// Pos() returns the declaring ast.FuncLit.Type.Func or the position
289// of the ast.FuncDecl.Name, if the function was explicit in the
Alan Donovan1fa3f782013-07-03 17:57:20 -0400290// source. Synthetic wrappers, for which Synthetic != "", may share
291// the same position as the function they wrap.
Alan Donovan149e0302014-08-01 14:44:37 -0400292// Syntax.Pos() always returns the position of the declaring "func" token.
Rob Pike87334f42013-05-17 14:02:47 -0700293//
Rob Pike83f21b92013-05-17 13:25:48 -0700294// Type() returns the function's Signature.
295//
296type Function struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400297 name string
Alan Donovanfec25222014-06-11 13:10:26 -0400298 object types.Object // a declared *types.Func or one of its wrappers
299 method *types.Selection // info about provenance of synthetic methods
Rob Pike83f21b92013-05-17 13:25:48 -0700300 Signature *types.Signature
Rob Pike87334f42013-05-17 14:02:47 -0700301 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400302
Alan Donovanf2db24a2014-07-11 10:50:09 +0100303 Synthetic string // provenance of synthetic function; "" for true source functions
304 syntax ast.Node // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode
305 parent *Function // enclosing function if anon; nil if global
306 Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error)
307 Prog *Program // enclosing program
308 Params []*Parameter // function parameters; for methods, includes receiver
309 FreeVars []*FreeVar // free variables whose values must be supplied by closure
310 Locals []*Alloc // local variables of this function
Rob Pike83f21b92013-05-17 13:25:48 -0700311 Blocks []*BasicBlock // basic blocks of the function; nil => external
Alan Donovan2accef22013-10-14 15:38:56 -0400312 Recover *BasicBlock // optional; control transfers here after recovered panic
Rob Pike83f21b92013-05-17 13:25:48 -0700313 AnonFuncs []*Function // anonymous functions directly beneath this one
Alan Donovan04427c82014-06-11 13:14:06 -0400314 referrers []Instruction // referring instructions (iff Parent() != nil)
Rob Pike83f21b92013-05-17 13:25:48 -0700315
316 // The following fields are set transiently during building,
317 // then cleared.
318 currentBlock *BasicBlock // where to emit code
319 objects map[types.Object]Value // addresses of local variables
320 namedResults []*Alloc // tuple of named results
Rob Pike83f21b92013-05-17 13:25:48 -0700321 targets *targets // linked stack of branch targets
322 lblocks map[*ast.Object]*lblock // labelled blocks
323}
324
Alan Donovanf9642692015-02-20 10:42:06 -0500325// BasicBlock represents an SSA basic block.
Rob Pike83f21b92013-05-17 13:25:48 -0700326//
327// The final element of Instrs is always an explicit transfer of
Alan Donovanf9642692015-02-20 10:42:06 -0500328// control (If, Jump, Return, or Panic).
Rob Pike83f21b92013-05-17 13:25:48 -0700329//
330// A block may contain no Instructions only if it is unreachable,
Alan Donovanf9642692015-02-20 10:42:06 -0500331// i.e., Preds is nil. Empty blocks are typically pruned.
Rob Pike83f21b92013-05-17 13:25:48 -0700332//
333// BasicBlocks and their Preds/Succs relation form a (possibly cyclic)
Alan Donovanb5016cb2013-12-05 09:50:18 -0500334// graph independent of the SSA Value graph: the control-flow graph or
335// CFG. It is illegal for multiple edges to exist between the same
336// pair of blocks.
337//
338// Each BasicBlock is also a node in the dominator tree of the CFG.
339// The tree may be navigated using Idom()/Dominees() and queried using
340// Dominates().
Rob Pike83f21b92013-05-17 13:25:48 -0700341//
Alan Donovanba9c8012014-03-11 18:24:39 -0400342// The order of Preds and Succs is significant (to Phi and If
Rob Pike83f21b92013-05-17 13:25:48 -0700343// instructions, respectively).
344//
345type BasicBlock struct {
Alan Donovanba9c8012014-03-11 18:24:39 -0400346 Index int // index of this block within Parent().Blocks
Rob Pike83f21b92013-05-17 13:25:48 -0700347 Comment string // optional label; no semantic significance
Alan Donovan341a07a2013-06-13 14:43:35 -0400348 parent *Function // parent function
Rob Pike83f21b92013-05-17 13:25:48 -0700349 Instrs []Instruction // instructions in order
350 Preds, Succs []*BasicBlock // predecessors and successors
Alan Donovanf9642692015-02-20 10:42:06 -0500351 succs2 [2]*BasicBlock // initial space for Succs
Alan Donovanb5016cb2013-12-05 09:50:18 -0500352 dom domInfo // dominator tree info
Alan Donovanf9642692015-02-20 10:42:06 -0500353 gaps int // number of nil Instrs (transient)
Rob Pike83f21b92013-05-17 13:25:48 -0700354 rundefers int // number of rundefers (transient)
355}
356
357// Pure values ----------------------------------------
358
Alan Donovancc02c5b2014-06-11 14:04:45 -0400359// A FreeVar represents a free variable of the function to which it
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400360// belongs.
Rob Pike83f21b92013-05-17 13:25:48 -0700361//
Alan Donovancc02c5b2014-06-11 14:04:45 -0400362// FreeVars are used to implement anonymous functions, whose free
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400363// variables are lexically captured in a closure formed by
Alan Donovancc02c5b2014-06-11 14:04:45 -0400364// MakeClosure. The value of such a free var is an Alloc or another
365// FreeVar and is considered a potentially escaping heap address, with
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400366// pointer type.
367//
Alan Donovancc02c5b2014-06-11 14:04:45 -0400368// FreeVars are also used to implement bound method closures. Such a
369// free var represents the receiver value and may be of any type that
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400370// has concrete methods.
Rob Pike83f21b92013-05-17 13:25:48 -0700371//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400372// Pos() returns the position of the value that was captured, which
373// belongs to an enclosing function.
374//
Alan Donovancc02c5b2014-06-11 14:04:45 -0400375type FreeVar struct {
Alan Donovan341a07a2013-06-13 14:43:35 -0400376 name string
377 typ types.Type
378 pos token.Pos
379 parent *Function
Rob Pike83f21b92013-05-17 13:25:48 -0700380 referrers []Instruction
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400381
382 // Transiently needed during building.
383 outer Value // the Value captured from the enclosing context.
Rob Pike83f21b92013-05-17 13:25:48 -0700384}
385
386// A Parameter represents an input parameter of a function.
387//
388type Parameter struct {
Alan Donovan341a07a2013-06-13 14:43:35 -0400389 name string
Alan Donovan55d678e2013-07-15 13:56:46 -0400390 object types.Object // a *types.Var; nil for non-source locals
Alan Donovan341a07a2013-06-13 14:43:35 -0400391 typ types.Type
392 pos token.Pos
393 parent *Function
Rob Pike83f21b92013-05-17 13:25:48 -0700394 referrers []Instruction
395}
396
Alan Donovan732dbe92013-07-16 13:50:08 -0400397// A Const represents the value of a constant expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700398//
Alan Donovanf3eb05b2014-01-13 16:45:46 -0500399// The underlying type of a constant may be any boolean, numeric, or
400// string type. In addition, a Const may represent the nil value of
Alan Donovanf9642692015-02-20 10:42:06 -0500401// any reference type---interface, map, channel, pointer, slice, or
Alan Donovanf3eb05b2014-01-13 16:45:46 -0500402// function---but not "untyped nil".
Rob Pike83f21b92013-05-17 13:25:48 -0700403//
Alan Donovan732dbe92013-07-16 13:50:08 -0400404// All source-level constant expressions are represented by a Const
Alan Donovanf9642692015-02-20 10:42:06 -0500405// of the same type and value.
Rob Pike83f21b92013-05-17 13:25:48 -0700406//
Alan Donovan732dbe92013-07-16 13:50:08 -0400407// Value holds the exact value of the constant, independent of its
Rob Pike87334f42013-05-17 14:02:47 -0700408// Type(), using the same representation as package go/exact uses for
Robert Griesemerf50f6c82013-10-09 14:17:25 -0700409// constants, or nil for a typed nil value.
Rob Pike83f21b92013-05-17 13:25:48 -0700410//
Alan Donovana399e262013-07-15 16:10:08 -0400411// Pos() returns token.NoPos.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400412//
Rob Pike83f21b92013-05-17 13:25:48 -0700413// Example printed form:
414// 42:int
415// "hello":untyped string
416// 3+4i:MyComplex
417//
Alan Donovan732dbe92013-07-16 13:50:08 -0400418type Const struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400419 typ types.Type
Rob Pike83f21b92013-05-17 13:25:48 -0700420 Value exact.Value
421}
422
423// A Global is a named Value holding the address of a package-level
424// variable.
425//
Rob Pike87334f42013-05-17 14:02:47 -0700426// Pos() returns the position of the ast.ValueSpec.Names[*]
427// identifier.
428//
Rob Pike83f21b92013-05-17 13:25:48 -0700429type Global struct {
Alan Donovanbc1f7242013-07-11 14:12:30 -0400430 name string
431 object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
432 typ types.Type
433 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400434
435 Pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -0700436}
437
Alan Donovand6eb8982014-01-07 13:31:05 -0500438// A Builtin represents a specific use of a built-in function, e.g. len.
Rob Pike83f21b92013-05-17 13:25:48 -0700439//
Rob Pike87334f42013-05-17 14:02:47 -0700440// Builtins are immutable values. Builtins do not have addresses.
Alan Donovan55d678e2013-07-15 13:56:46 -0400441// Builtins can only appear in CallCommon.Func.
Rob Pike83f21b92013-05-17 13:25:48 -0700442//
Alan Donovande23e2b2014-06-13 17:34:07 -0400443// Name() indicates the function: one of the built-in functions from the
444// Go spec (excluding "make" and "new") or one of these ssa-defined
445// intrinsics:
446//
447// // wrapnilchk returns ptr if non-nil, panics otherwise.
448// // (For use in indirection wrappers.)
449// func ssa:wrapnilchk(ptr *T, recvType, methodName string) *T
450//
451// Object() returns a *types.Builtin for built-ins defined by the spec,
452// nil for others.
Alan Donovan318b83e2013-09-23 18:18:35 -0400453//
Alan Donovand6eb8982014-01-07 13:31:05 -0500454// Type() returns a *types.Signature representing the effective
455// signature of the built-in for this call.
Rob Pike83f21b92013-05-17 13:25:48 -0700456//
457type Builtin struct {
Alan Donovande23e2b2014-06-13 17:34:07 -0400458 name string
459 sig *types.Signature
Rob Pike83f21b92013-05-17 13:25:48 -0700460}
461
462// Value-defining instructions ----------------------------------------
463
Alan Donovanf9642692015-02-20 10:42:06 -0500464// The Alloc instruction reserves space for a variable of the given type,
Rob Pike83f21b92013-05-17 13:25:48 -0700465// zero-initializes it, and yields its address.
466//
467// Alloc values are always addresses, and have pointer types, so the
Alan Donovanf9642692015-02-20 10:42:06 -0500468// type of the allocated variable is actually
469// Type().Underlying().(*types.Pointer).Elem().
Rob Pike83f21b92013-05-17 13:25:48 -0700470//
471// If Heap is false, Alloc allocates space in the function's
472// activation record (frame); we refer to an Alloc(Heap=false) as a
473// "local" alloc. Each local Alloc returns the same address each time
474// it is executed within the same activation; the space is
475// re-initialized to zero.
476//
Alan Donovanf9642692015-02-20 10:42:06 -0500477// If Heap is true, Alloc allocates space in the heap; we
Rob Pike83f21b92013-05-17 13:25:48 -0700478// refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc
479// returns a different address each time it is executed.
480//
481// When Alloc is applied to a channel, map or slice type, it returns
482// the address of an uninitialized (nil) reference of that kind; store
483// the result of MakeSlice, MakeMap or MakeChan in that location to
484// instantiate these types.
485//
Rob Pike87334f42013-05-17 14:02:47 -0700486// Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
Alan Donovan38cb4c02014-06-13 17:12:28 -0400487// or the ast.CallExpr.Rparen for a call to new() or for a call that
Rob Pike87334f42013-05-17 14:02:47 -0700488// allocates a varargs slice.
489//
Rob Pike83f21b92013-05-17 13:25:48 -0700490// Example printed form:
491// t0 = local int
492// t1 = new int
493//
494type Alloc struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400495 register
Alan Donovan5cc33ea2013-08-01 14:06:10 -0400496 Comment string
497 Heap bool
498 index int // dense numbering; for lifting
Rob Pike83f21b92013-05-17 13:25:48 -0700499}
500
Rob Pike87334f42013-05-17 14:02:47 -0700501// The Phi instruction represents an SSA φ-node, which combines values
502// that differ across incoming control-flow edges and yields a new
503// value. Within a block, all φ-nodes must appear before all non-φ
504// nodes.
505//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400506// Pos() returns the position of the && or || for short-circuit
507// control-flow joins, or that of the *Alloc for φ-nodes inserted
508// during SSA renaming.
Rob Pike83f21b92013-05-17 13:25:48 -0700509//
510// Example printed form:
Alan Donovanf9642692015-02-20 10:42:06 -0500511// t2 = phi [0: t0, 1: t1]
Rob Pike83f21b92013-05-17 13:25:48 -0700512//
513type Phi struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400514 register
Rob Pike83f21b92013-05-17 13:25:48 -0700515 Comment string // a hint as to its purpose
516 Edges []Value // Edges[i] is value for Block().Preds[i]
517}
518
Rob Pike87334f42013-05-17 14:02:47 -0700519// The Call instruction represents a function or method call.
Rob Pike83f21b92013-05-17 13:25:48 -0700520//
Alan Donovanf9642692015-02-20 10:42:06 -0500521// The Call instruction yields the function result if there is exactly
522// one. Otherwise it returns a tuple, the components of which are
Rob Pike83f21b92013-05-17 13:25:48 -0700523// accessed via Extract.
524//
525// See CallCommon for generic function call documentation.
526//
Rob Pike87334f42013-05-17 14:02:47 -0700527// Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
528//
Rob Pike83f21b92013-05-17 13:25:48 -0700529// Example printed form:
530// t2 = println(t0, t1)
531// t4 = t3()
532// t7 = invoke t5.Println(...t6)
533//
534type Call struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400535 register
Rob Pike83f21b92013-05-17 13:25:48 -0700536 Call CallCommon
537}
538
Rob Pike87334f42013-05-17 14:02:47 -0700539// The BinOp instruction yields the result of binary operation X Op Y.
540//
541// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700542//
543// Example printed form:
544// t1 = t0 + 1:int
545//
546type BinOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400547 register
Rob Pike83f21b92013-05-17 13:25:48 -0700548 // One of:
549 // ADD SUB MUL QUO REM + - * / %
Austin Clements25b95b42018-06-19 21:23:51 -0400550 // AND OR XOR SHL SHR AND_NOT & | ^ << >> &^
551 // EQL NEQ LSS LEQ GTR GEQ == != < <= < >=
Rob Pike83f21b92013-05-17 13:25:48 -0700552 Op token.Token
553 X, Y Value
554}
555
Rob Pike87334f42013-05-17 14:02:47 -0700556// The UnOp instruction yields the result of Op X.
Rob Pike83f21b92013-05-17 13:25:48 -0700557// ARROW is channel receive.
558// MUL is pointer indirection (load).
559// XOR is bitwise complement.
560// SUB is negation.
Alan Donovanba9c8012014-03-11 18:24:39 -0400561// NOT is logical negation.
Rob Pike83f21b92013-05-17 13:25:48 -0700562//
563// If CommaOk and Op=ARROW, the result is a 2-tuple of the value above
564// and a boolean indicating the success of the receive. The
565// components of the tuple are accessed using Extract.
566//
Alan Donovan538acf12014-12-29 16:49:20 -0500567// Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source.
568// For receive operations (ARROW) implicit in ranging over a channel,
569// Pos() returns the ast.RangeStmt.For.
570// For implicit memory loads (STAR), Pos() returns the position of the
571// most closely associated source-level construct; the details are not
572// specified.
Rob Pike87334f42013-05-17 14:02:47 -0700573//
Rob Pike83f21b92013-05-17 13:25:48 -0700574// Example printed form:
575// t0 = *x
576// t2 = <-t1,ok
577//
578type UnOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400579 register
Rob Pike83f21b92013-05-17 13:25:48 -0700580 Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^
581 X Value
582 CommaOk bool
583}
584
Rob Pike87334f42013-05-17 14:02:47 -0700585// The ChangeType instruction applies to X a value-preserving type
586// change to Type().
Rob Pike83f21b92013-05-17 13:25:48 -0700587//
Rob Pike87334f42013-05-17 14:02:47 -0700588// Type changes are permitted:
589// - between a named type and its underlying type.
590// - between two named types of the same underlying type.
591// - between (possibly named) pointers to identical base types.
Rob Pike87334f42013-05-17 14:02:47 -0700592// - from a bidirectional channel to a read- or write-channel,
593// optionally adding/removing a name.
Rob Pike83f21b92013-05-17 13:25:48 -0700594//
Rob Pike87334f42013-05-17 14:02:47 -0700595// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700596//
Rob Pike87334f42013-05-17 14:02:47 -0700597// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
598// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700599//
Rob Pike87334f42013-05-17 14:02:47 -0700600// Example printed form:
601// t1 = changetype *int <- IntPtr (t0)
602//
603type ChangeType struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400604 register
Rob Pike87334f42013-05-17 14:02:47 -0700605 X Value
606}
607
608// The Convert instruction yields the conversion of value X to type
Alan Donovan8097dad2013-06-24 14:15:13 -0400609// Type(). One or both of those types is basic (but possibly named).
Rob Pike87334f42013-05-17 14:02:47 -0700610//
611// A conversion may change the value and representation of its operand.
612// Conversions are permitted:
613// - between real numeric types.
614// - between complex numeric types.
615// - between string and []byte or []rune.
Alan Donovan8097dad2013-06-24 14:15:13 -0400616// - between pointers and unsafe.Pointer.
617// - between unsafe.Pointer and uintptr.
Rob Pike87334f42013-05-17 14:02:47 -0700618// - from (Unicode) integer to (UTF-8) string.
619// A conversion may imply a type name change also.
620//
621// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700622//
623// Conversions of untyped string/number/bool constants to a specific
624// representation are eliminated during SSA construction.
625//
Rob Pike87334f42013-05-17 14:02:47 -0700626// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
627// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700628//
Rob Pike87334f42013-05-17 14:02:47 -0700629// Example printed form:
630// t1 = convert []byte <- string (t0)
631//
632type Convert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400633 register
Rob Pike83f21b92013-05-17 13:25:48 -0700634 X Value
635}
636
637// ChangeInterface constructs a value of one interface type from a
638// value of another interface type known to be assignable to it.
Alan Donovanae801632013-07-26 21:49:27 -0400639// This operation cannot fail.
Rob Pike87334f42013-05-17 14:02:47 -0700640//
Alan Donovan341a07a2013-06-13 14:43:35 -0400641// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
642// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
643// instruction arose from an explicit e.(T) operation; or token.NoPos
644// otherwise.
645//
Rob Pike83f21b92013-05-17 13:25:48 -0700646// Example printed form:
647// t1 = change interface interface{} <- I (t0)
648//
649type ChangeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400650 register
Rob Pike83f21b92013-05-17 13:25:48 -0700651 X Value
652}
653
654// MakeInterface constructs an instance of an interface type from a
Alan Donovan341a07a2013-06-13 14:43:35 -0400655// value of a concrete type.
656//
Alan Donovan1f29e742014-02-11 16:49:27 -0500657// Use Program.MethodSets.MethodSet(X.Type()) to find the method-set
658// of X, and Program.Method(m) to find the implementation of a method.
Rob Pike83f21b92013-05-17 13:25:48 -0700659//
660// To construct the zero value of an interface type T, use:
Alan Donovan732dbe92013-07-16 13:50:08 -0400661// NewConst(exact.MakeNil(), T, pos)
Rob Pike87334f42013-05-17 14:02:47 -0700662//
663// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
664// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700665//
666// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400667// t1 = make interface{} <- int (42:int)
668// t2 = make Stringer <- t0
Rob Pike83f21b92013-05-17 13:25:48 -0700669//
670type MakeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400671 register
Alan Donovan341a07a2013-06-13 14:43:35 -0400672 X Value
Rob Pike83f21b92013-05-17 13:25:48 -0700673}
674
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400675// The MakeClosure instruction yields a closure value whose code is
676// Fn and whose free variables' values are supplied by Bindings.
Rob Pike83f21b92013-05-17 13:25:48 -0700677//
678// Type() returns a (possibly named) *types.Signature.
679//
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400680// Pos() returns the ast.FuncLit.Type.Func for a function literal
681// closure or the ast.SelectorExpr.Sel for a bound method closure.
Rob Pike87334f42013-05-17 14:02:47 -0700682//
Rob Pike83f21b92013-05-17 13:25:48 -0700683// Example printed form:
684// t0 = make closure anon@1.2 [x y z]
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400685// t1 = make closure bound$(main.I).add [i]
Rob Pike83f21b92013-05-17 13:25:48 -0700686//
687type MakeClosure struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400688 register
Rob Pike83f21b92013-05-17 13:25:48 -0700689 Fn Value // always a *Function
690 Bindings []Value // values for each free variable in Fn.FreeVars
691}
692
693// The MakeMap instruction creates a new hash-table-based map object
694// and yields a value of kind map.
695//
696// Type() returns a (possibly named) *types.Map.
697//
Rob Pike87334f42013-05-17 14:02:47 -0700698// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
699// the ast.CompositeLit.Lbrack if created by a literal.
700//
Rob Pike83f21b92013-05-17 13:25:48 -0700701// Example printed form:
702// t1 = make map[string]int t0
Alan Donovan341a07a2013-06-13 14:43:35 -0400703// t1 = make StringIntMap t0
Rob Pike83f21b92013-05-17 13:25:48 -0700704//
705type MakeMap struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400706 register
Rob Pike83f21b92013-05-17 13:25:48 -0700707 Reserve Value // initial space reservation; nil => default
Rob Pike83f21b92013-05-17 13:25:48 -0700708}
709
710// The MakeChan instruction creates a new channel object and yields a
711// value of kind chan.
712//
713// Type() returns a (possibly named) *types.Chan.
714//
Rob Pike87334f42013-05-17 14:02:47 -0700715// Pos() returns the ast.CallExpr.Lparen for the make(chan) that
716// created it.
717//
Rob Pike83f21b92013-05-17 13:25:48 -0700718// Example printed form:
719// t0 = make chan int 0
Alan Donovan341a07a2013-06-13 14:43:35 -0400720// t0 = make IntChan 0
Rob Pike83f21b92013-05-17 13:25:48 -0700721//
722type MakeChan struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400723 register
Rob Pike83f21b92013-05-17 13:25:48 -0700724 Size Value // int; size of buffer; zero => synchronous.
Rob Pike83f21b92013-05-17 13:25:48 -0700725}
726
Rob Pike87334f42013-05-17 14:02:47 -0700727// The MakeSlice instruction yields a slice of length Len backed by a
728// newly allocated array of length Cap.
Rob Pike83f21b92013-05-17 13:25:48 -0700729//
730// Both Len and Cap must be non-nil Values of integer type.
731//
732// (Alloc(types.Array) followed by Slice will not suffice because
Alan Donovanba9c8012014-03-11 18:24:39 -0400733// Alloc can only create arrays of constant length.)
Rob Pike83f21b92013-05-17 13:25:48 -0700734//
735// Type() returns a (possibly named) *types.Slice.
736//
Rob Pike87334f42013-05-17 14:02:47 -0700737// Pos() returns the ast.CallExpr.Lparen for the make([]T) that
738// created it.
739//
Rob Pike83f21b92013-05-17 13:25:48 -0700740// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400741// t1 = make []string 1:int t0
742// t1 = make StringSlice 1:int t0
Rob Pike83f21b92013-05-17 13:25:48 -0700743//
744type MakeSlice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400745 register
Rob Pike83f21b92013-05-17 13:25:48 -0700746 Len Value
747 Cap Value
Rob Pike83f21b92013-05-17 13:25:48 -0700748}
749
Rob Pike87334f42013-05-17 14:02:47 -0700750// The Slice instruction yields a slice of an existing string, slice
751// or *array X between optional integer bounds Low and High.
Rob Pike83f21b92013-05-17 13:25:48 -0700752//
Alan Donovan70722532013-08-19 15:38:30 -0400753// Dynamically, this instruction panics if X evaluates to a nil *array
754// pointer.
755//
Rob Pike83f21b92013-05-17 13:25:48 -0700756// Type() returns string if the type of X was string, otherwise a
757// *types.Slice with the same element type as X.
758//
Rob Pike87334f42013-05-17 14:02:47 -0700759// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
760// operation, the ast.CompositeLit.Lbrace if created by a literal, or
761// NoPos if not explicit in the source (e.g. a variadic argument slice).
762//
Rob Pike83f21b92013-05-17 13:25:48 -0700763// Example printed form:
764// t1 = slice t0[1:]
765//
766type Slice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400767 register
Alan Donovanb3dbe562014-02-05 17:54:51 -0500768 X Value // slice, string, or *array
769 Low, High, Max Value // each may be nil
Rob Pike83f21b92013-05-17 13:25:48 -0700770}
771
Rob Pike87334f42013-05-17 14:02:47 -0700772// The FieldAddr instruction yields the address of Field of *struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700773//
774// The field is identified by its index within the field list of the
775// struct type of X.
776//
Alan Donovan70722532013-08-19 15:38:30 -0400777// Dynamically, this instruction panics if X evaluates to a nil
778// pointer.
779//
Rob Pike83f21b92013-05-17 13:25:48 -0700780// Type() returns a (possibly named) *types.Pointer.
781//
Rob Pike87334f42013-05-17 14:02:47 -0700782// Pos() returns the position of the ast.SelectorExpr.Sel for the
783// field, if explicit in the source.
784//
Rob Pike83f21b92013-05-17 13:25:48 -0700785// Example printed form:
786// t1 = &t0.name [#1]
787//
788type FieldAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400789 register
Rob Pike83f21b92013-05-17 13:25:48 -0700790 X Value // *struct
Austin Clements25b95b42018-06-19 21:23:51 -0400791 Field int // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field)
Rob Pike83f21b92013-05-17 13:25:48 -0700792}
793
Rob Pike87334f42013-05-17 14:02:47 -0700794// The Field instruction yields the Field of struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700795//
796// The field is identified by its index within the field list of the
797// struct type of X; by using numeric indices we avoid ambiguity of
798// package-local identifiers and permit compact representations.
799//
Rob Pike87334f42013-05-17 14:02:47 -0700800// Pos() returns the position of the ast.SelectorExpr.Sel for the
801// field, if explicit in the source.
802//
Rob Pike83f21b92013-05-17 13:25:48 -0700803// Example printed form:
804// t1 = t0.name [#1]
805//
806type Field struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400807 register
Rob Pike83f21b92013-05-17 13:25:48 -0700808 X Value // struct
809 Field int // index into X.Type().(*types.Struct).Fields
810}
811
Rob Pike87334f42013-05-17 14:02:47 -0700812// The IndexAddr instruction yields the address of the element at
813// index Index of collection X. Index is an integer expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700814//
815// The elements of maps and strings are not addressable; use Lookup or
816// MapUpdate instead.
817//
Alan Donovan70722532013-08-19 15:38:30 -0400818// Dynamically, this instruction panics if X evaluates to a nil *array
819// pointer.
820//
Rob Pike83f21b92013-05-17 13:25:48 -0700821// Type() returns a (possibly named) *types.Pointer.
822//
Rob Pike87334f42013-05-17 14:02:47 -0700823// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
824// explicit in the source.
825//
Rob Pike83f21b92013-05-17 13:25:48 -0700826// Example printed form:
827// t2 = &t0[t1]
828//
829type IndexAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400830 register
Rob Pike83f21b92013-05-17 13:25:48 -0700831 X Value // slice or *array,
832 Index Value // numeric index
833}
834
Rob Pike87334f42013-05-17 14:02:47 -0700835// The Index instruction yields element Index of array X.
836//
837// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
838// explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700839//
840// Example printed form:
841// t2 = t0[t1]
842//
843type Index struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400844 register
Rob Pike83f21b92013-05-17 13:25:48 -0700845 X Value // array
846 Index Value // integer index
847}
848
Rob Pike87334f42013-05-17 14:02:47 -0700849// The Lookup instruction yields element Index of collection X, a map
850// or string. Index is an integer expression if X is a string or the
851// appropriate key type if X is a map.
Rob Pike83f21b92013-05-17 13:25:48 -0700852//
853// If CommaOk, the result is a 2-tuple of the value above and a
854// boolean indicating the result of a map membership test for the key.
855// The components of the tuple are accessed using Extract.
856//
Rob Pike87334f42013-05-17 14:02:47 -0700857// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
858//
Rob Pike83f21b92013-05-17 13:25:48 -0700859// Example printed form:
860// t2 = t0[t1]
861// t5 = t3[t4],ok
862//
863type Lookup struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400864 register
Rob Pike83f21b92013-05-17 13:25:48 -0700865 X Value // string or map
866 Index Value // numeric or key-typed index
867 CommaOk bool // return a value,ok pair
868}
869
870// SelectState is a helper for Select.
871// It represents one goal state and its corresponding communication.
872//
873type SelectState struct {
Robert Griesemer74d33a92013-12-17 15:45:01 -0800874 Dir types.ChanDir // direction of case (SendOnly or RecvOnly)
875 Chan Value // channel to use (for send or receive)
876 Send Value // value to send (for send)
877 Pos token.Pos // position of token.ARROW
878 DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
Rob Pike83f21b92013-05-17 13:25:48 -0700879}
880
Alan Donovand6eb8982014-01-07 13:31:05 -0500881// The Select instruction tests whether (or blocks until) one
Rob Pike87334f42013-05-17 14:02:47 -0700882// of the specified sent or received states is entered.
Rob Pike83f21b92013-05-17 13:25:48 -0700883//
Alan Donovan8097dad2013-06-24 14:15:13 -0400884// Let n be the number of States for which Dir==RECV and T_i (0<=i<n)
885// be the element type of each such state's Chan.
886// Select returns an n+2-tuple
887// (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)
888// The tuple's components, described below, must be accessed via the
889// Extract instruction.
Rob Pike83f21b92013-05-17 13:25:48 -0700890//
891// If Blocking, select waits until exactly one state holds, i.e. a
892// channel becomes ready for the designated operation of sending or
893// receiving; select chooses one among the ready states
894// pseudorandomly, performs the send or receive operation, and sets
895// 'index' to the index of the chosen channel.
896//
897// If !Blocking, select doesn't block if no states hold; instead it
898// returns immediately with index equal to -1.
899//
Alan Donovan8097dad2013-06-24 14:15:13 -0400900// If the chosen channel was used for a receive, the r_i component is
901// set to the received value, where i is the index of that state among
902// all n receive states; otherwise r_i has the zero value of type T_i.
Rob Pikea8c8f482014-05-19 09:48:30 -0700903// Note that the receive index i is not the same as the state
Alan Donovan8097dad2013-06-24 14:15:13 -0400904// index index.
Rob Pike83f21b92013-05-17 13:25:48 -0700905//
Alan Donovan8097dad2013-06-24 14:15:13 -0400906// The second component of the triple, recvOk, is a boolean whose value
Rob Pike83f21b92013-05-17 13:25:48 -0700907// is true iff the selected operation was a receive and the receive
908// successfully yielded a value.
909//
Rob Pike87334f42013-05-17 14:02:47 -0700910// Pos() returns the ast.SelectStmt.Select.
911//
Rob Pike83f21b92013-05-17 13:25:48 -0700912// Example printed form:
Alan Donovand6eb8982014-01-07 13:31:05 -0500913// t3 = select nonblocking [<-t0, t1<-t2]
Rob Pike83f21b92013-05-17 13:25:48 -0700914// t4 = select blocking []
915//
916type Select struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400917 register
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400918 States []*SelectState
Rob Pike83f21b92013-05-17 13:25:48 -0700919 Blocking bool
920}
921
Rob Pike87334f42013-05-17 14:02:47 -0700922// The Range instruction yields an iterator over the domain and range
923// of X, which must be a string or map.
Rob Pike83f21b92013-05-17 13:25:48 -0700924//
925// Elements are accessed via Next.
926//
Alan Donovan8097dad2013-06-24 14:15:13 -0400927// Type() returns an opaque and degenerate "rangeIter" type.
Rob Pike83f21b92013-05-17 13:25:48 -0700928//
Rob Pike87334f42013-05-17 14:02:47 -0700929// Pos() returns the ast.RangeStmt.For.
930//
Rob Pike83f21b92013-05-17 13:25:48 -0700931// Example printed form:
932// t0 = range "hello":string
933//
934type Range struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400935 register
Rob Pike83f21b92013-05-17 13:25:48 -0700936 X Value // string or map
937}
938
Rob Pike87334f42013-05-17 14:02:47 -0700939// The Next instruction reads and advances the (map or string)
940// iterator Iter and returns a 3-tuple value (ok, k, v). If the
941// iterator is not exhausted, ok is true and k and v are the next
942// elements of the domain and range, respectively. Otherwise ok is
943// false and k and v are undefined.
Rob Pike83f21b92013-05-17 13:25:48 -0700944//
945// Components of the tuple are accessed using Extract.
946//
947// The IsString field distinguishes iterators over strings from those
948// over maps, as the Type() alone is insufficient: consider
949// map[int]rune.
950//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400951// Type() returns a *types.Tuple for the triple (ok, k, v).
952// The types of k and/or v may be types.Invalid.
Rob Pike83f21b92013-05-17 13:25:48 -0700953//
954// Example printed form:
955// t1 = next t0
956//
957type Next struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400958 register
Rob Pike83f21b92013-05-17 13:25:48 -0700959 Iter Value
960 IsString bool // true => string iterator; false => map iterator.
961}
962
Rob Pike87334f42013-05-17 14:02:47 -0700963// The TypeAssert instruction tests whether interface value X has type
964// AssertedType.
Rob Pike83f21b92013-05-17 13:25:48 -0700965//
966// If !CommaOk, on success it returns v, the result of the conversion
967// (defined below); on failure it panics.
968//
969// If CommaOk: on success it returns a pair (v, true) where v is the
970// result of the conversion; on failure it returns (z, false) where z
971// is AssertedType's zero value. The components of the pair must be
972// accessed using the Extract instruction.
973//
974// If AssertedType is a concrete type, TypeAssert checks whether the
975// dynamic type in interface X is equal to it, and if so, the result
976// of the conversion is a copy of the value in the interface.
977//
978// If AssertedType is an interface, TypeAssert checks whether the
979// dynamic type of the interface is assignable to it, and if so, the
980// result of the conversion is a copy of the interface value X.
Alan Donovanae801632013-07-26 21:49:27 -0400981// If AssertedType is a superinterface of X.Type(), the operation will
982// fail iff the operand is nil. (Contrast with ChangeInterface, which
983// performs no nil-check.)
Rob Pike83f21b92013-05-17 13:25:48 -0700984//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400985// Type() reflects the actual type of the result, possibly a
986// 2-types.Tuple; AssertedType is the asserted type.
Rob Pike83f21b92013-05-17 13:25:48 -0700987//
Alan Donovan341a07a2013-06-13 14:43:35 -0400988// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
989// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400990// instruction arose from an explicit e.(T) operation; or the
991// ast.CaseClause.Case if the instruction arose from a case of a
992// type-switch statement.
Alan Donovan341a07a2013-06-13 14:43:35 -0400993//
Rob Pike83f21b92013-05-17 13:25:48 -0700994// Example printed form:
995// t1 = typeassert t0.(int)
996// t3 = typeassert,ok t2.(T)
997//
998type TypeAssert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400999 register
Rob Pike83f21b92013-05-17 13:25:48 -07001000 X Value
1001 AssertedType types.Type
1002 CommaOk bool
1003}
1004
Rob Pike87334f42013-05-17 14:02:47 -07001005// The Extract instruction yields component Index of Tuple.
Rob Pike83f21b92013-05-17 13:25:48 -07001006//
1007// This is used to access the results of instructions with multiple
1008// return values, such as Call, TypeAssert, Next, UnOp(ARROW) and
1009// IndexExpr(Map).
1010//
1011// Example printed form:
1012// t1 = extract t0 #1
1013//
1014type Extract struct {
Alan Donovan1ff34522013-10-14 13:48:34 -04001015 register
Rob Pike83f21b92013-05-17 13:25:48 -07001016 Tuple Value
1017 Index int
1018}
1019
1020// Instructions executed for effect. They do not yield a value. --------------------
1021
Rob Pike87334f42013-05-17 14:02:47 -07001022// The Jump instruction transfers control to the sole successor of its
1023// owning block.
Rob Pike83f21b92013-05-17 13:25:48 -07001024//
Rob Pike87334f42013-05-17 14:02:47 -07001025// A Jump must be the last instruction of its containing BasicBlock.
1026//
1027// Pos() returns NoPos.
Rob Pike83f21b92013-05-17 13:25:48 -07001028//
1029// Example printed form:
1030// jump done
1031//
1032type Jump struct {
1033 anInstruction
1034}
1035
1036// The If instruction transfers control to one of the two successors
1037// of its owning block, depending on the boolean Cond: the first if
1038// true, the second if false.
1039//
1040// An If instruction must be the last instruction of its containing
1041// BasicBlock.
1042//
Rob Pike87334f42013-05-17 14:02:47 -07001043// Pos() returns NoPos.
1044//
Rob Pike83f21b92013-05-17 13:25:48 -07001045// Example printed form:
1046// if t0 goto done else body
1047//
1048type If struct {
1049 anInstruction
1050 Cond Value
1051}
1052
Alan Donovan068f0172013-10-08 12:31:39 -04001053// The Return instruction returns values and control back to the calling
Rob Pike87334f42013-05-17 14:02:47 -07001054// function.
Rob Pike83f21b92013-05-17 13:25:48 -07001055//
1056// len(Results) is always equal to the number of results in the
1057// function's signature.
1058//
Alan Donovan068f0172013-10-08 12:31:39 -04001059// If len(Results) > 1, Return returns a tuple value with the specified
Rob Pike83f21b92013-05-17 13:25:48 -07001060// components which the caller must access using Extract instructions.
1061//
1062// There is no instruction to return a ready-made tuple like those
1063// returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or
1064// a tail-call to a function with multiple result parameters.
1065//
Alan Donovan068f0172013-10-08 12:31:39 -04001066// Return must be the last instruction of its containing BasicBlock.
Rob Pike83f21b92013-05-17 13:25:48 -07001067// Such a block has no successors.
1068//
Rob Pike87334f42013-05-17 14:02:47 -07001069// Pos() returns the ast.ReturnStmt.Return, if explicit in the source.
1070//
Rob Pike83f21b92013-05-17 13:25:48 -07001071// Example printed form:
Alan Donovan068f0172013-10-08 12:31:39 -04001072// return
1073// return nil:I, 2:int
Rob Pike83f21b92013-05-17 13:25:48 -07001074//
Alan Donovan068f0172013-10-08 12:31:39 -04001075type Return struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001076 anInstruction
1077 Results []Value
Rob Pike87334f42013-05-17 14:02:47 -07001078 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001079}
1080
Rob Pike87334f42013-05-17 14:02:47 -07001081// The RunDefers instruction pops and invokes the entire stack of
1082// procedure calls pushed by Defer instructions in this function.
Rob Pike83f21b92013-05-17 13:25:48 -07001083//
1084// It is legal to encounter multiple 'rundefers' instructions in a
1085// single control-flow path through a function; this is useful in
1086// the combined init() function, for example.
1087//
Rob Pike87334f42013-05-17 14:02:47 -07001088// Pos() returns NoPos.
1089//
Rob Pike83f21b92013-05-17 13:25:48 -07001090// Example printed form:
1091// rundefers
1092//
1093type RunDefers struct {
1094 anInstruction
1095}
1096
Rob Pike87334f42013-05-17 14:02:47 -07001097// The Panic instruction initiates a panic with value X.
Rob Pike83f21b92013-05-17 13:25:48 -07001098//
1099// A Panic instruction must be the last instruction of its containing
1100// BasicBlock, which must have no successors.
1101//
1102// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction;
1103// they are treated as calls to a built-in function.
1104//
Rob Pike87334f42013-05-17 14:02:47 -07001105// Pos() returns the ast.CallExpr.Lparen if this panic was explicit
1106// in the source.
1107//
Rob Pike83f21b92013-05-17 13:25:48 -07001108// Example printed form:
1109// panic t0
1110//
1111type Panic struct {
1112 anInstruction
Rob Pike87334f42013-05-17 14:02:47 -07001113 X Value // an interface{}
1114 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001115}
1116
Rob Pike87334f42013-05-17 14:02:47 -07001117// The Go instruction creates a new goroutine and calls the specified
1118// function within it.
Rob Pike83f21b92013-05-17 13:25:48 -07001119//
1120// See CallCommon for generic function call documentation.
1121//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001122// Pos() returns the ast.GoStmt.Go.
1123//
Rob Pike83f21b92013-05-17 13:25:48 -07001124// Example printed form:
1125// go println(t0, t1)
1126// go t3()
1127// go invoke t5.Println(...t6)
1128//
1129type Go struct {
1130 anInstruction
1131 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001132 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001133}
1134
Rob Pike87334f42013-05-17 14:02:47 -07001135// The Defer instruction pushes the specified call onto a stack of
1136// functions to be called by a RunDefers instruction or by a panic.
Rob Pike83f21b92013-05-17 13:25:48 -07001137//
1138// See CallCommon for generic function call documentation.
1139//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001140// Pos() returns the ast.DeferStmt.Defer.
1141//
Rob Pike83f21b92013-05-17 13:25:48 -07001142// Example printed form:
1143// defer println(t0, t1)
1144// defer t3()
1145// defer invoke t5.Println(...t6)
1146//
1147type Defer struct {
1148 anInstruction
1149 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001150 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001151}
1152
Rob Pike87334f42013-05-17 14:02:47 -07001153// The Send instruction sends X on channel Chan.
1154//
1155// Pos() returns the ast.SendStmt.Arrow, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001156//
1157// Example printed form:
1158// send t0 <- t1
1159//
1160type Send struct {
1161 anInstruction
1162 Chan, X Value
Rob Pike87334f42013-05-17 14:02:47 -07001163 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001164}
1165
Rob Pike87334f42013-05-17 14:02:47 -07001166// The Store instruction stores Val at address Addr.
Rob Pike83f21b92013-05-17 13:25:48 -07001167// Stores can be of arbitrary types.
1168//
Alan Donovan538acf12014-12-29 16:49:20 -05001169// Pos() returns the position of the source-level construct most closely
1170// associated with the memory store operation.
1171// Since implicit memory stores are numerous and varied and depend upon
1172// implementation choices, the details are not specified.
Rob Pike87334f42013-05-17 14:02:47 -07001173//
Rob Pike83f21b92013-05-17 13:25:48 -07001174// Example printed form:
1175// *x = y
1176//
1177type Store struct {
1178 anInstruction
1179 Addr Value
1180 Val Value
Rob Pike87334f42013-05-17 14:02:47 -07001181 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001182}
1183
Rob Pike87334f42013-05-17 14:02:47 -07001184// The MapUpdate instruction updates the association of Map[Key] to
1185// Value.
1186//
Alan Donovanc8a68902013-08-22 10:13:51 -04001187// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
1188// if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001189//
1190// Example printed form:
1191// t0[t1] = t2
1192//
1193type MapUpdate struct {
1194 anInstruction
1195 Map Value
1196 Key Value
1197 Value Value
Rob Pike87334f42013-05-17 14:02:47 -07001198 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001199}
1200
Alan Donovane2962652013-10-28 12:05:29 -04001201// A DebugRef instruction maps a source-level expression Expr to the
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001202// SSA value X that represents the value (!IsAddr) or address (IsAddr)
Alan Donovane2962652013-10-28 12:05:29 -04001203// of that expression.
Alan Donovan55d678e2013-07-15 13:56:46 -04001204//
1205// DebugRef is a pseudo-instruction: it has no dynamic effect.
1206//
Alan Donovane590cdb2013-10-09 12:47:30 -04001207// Pos() returns Expr.Pos(), the start position of the source-level
1208// expression. This is not the same as the "designated" token as
1209// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the
1210// position of the ("designated") Lparen token.
Alan Donovan55d678e2013-07-15 13:56:46 -04001211//
Alan Donovane2962652013-10-28 12:05:29 -04001212// If Expr is an *ast.Ident denoting a var or func, Object() returns
1213// the object; though this information can be obtained from the type
1214// checker, including it here greatly facilitates debugging.
1215// For non-Ident expressions, Object() returns nil.
1216//
1217// DebugRefs are generated only for functions built with debugging
Alan Donovane8afbfa2014-01-15 21:37:55 -05001218// enabled; see Package.SetDebugMode() and the GlobalDebug builder
1219// mode flag.
Alan Donovane2962652013-10-28 12:05:29 -04001220//
1221// DebugRefs are not emitted for ast.Idents referring to constants or
1222// predeclared identifiers, since they are trivial and numerous.
1223// Nor are they emitted for ast.ParenExprs.
Alan Donovan55d678e2013-07-15 13:56:46 -04001224//
1225// (By representing these as instructions, rather than out-of-band,
1226// consistency is maintained during transformation passes by the
1227// ordinary SSA renaming machinery.)
1228//
Alan Donovane2962652013-10-28 12:05:29 -04001229// Example printed form:
1230// ; *ast.CallExpr @ 102:9 is t5
1231// ; var x float64 @ 109:72 is x
1232// ; address of *ast.CompositeLit @ 216:10 is t0
Alan Donovanaa238622013-10-27 10:55:21 -04001233//
Alan Donovan55d678e2013-07-15 13:56:46 -04001234type DebugRef struct {
1235 anInstruction
Alan Donovan9f640c22013-10-24 18:31:50 -04001236 Expr ast.Expr // the referring expression (never *ast.ParenExpr)
Alan Donovane2962652013-10-28 12:05:29 -04001237 object types.Object // the identity of the source var/func
Alan Donovan9f640c22013-10-24 18:31:50 -04001238 IsAddr bool // Expr is addressable and X is the address it denotes
Alan Donovane2962652013-10-28 12:05:29 -04001239 X Value // the value or address of Expr
Alan Donovan55d678e2013-07-15 13:56:46 -04001240}
1241
Rob Pike83f21b92013-05-17 13:25:48 -07001242// Embeddable mix-ins and helpers for common parts of other structs. -----------
1243
Alan Donovan1ff34522013-10-14 13:48:34 -04001244// register is a mix-in embedded by all SSA values that are also
1245// instructions, i.e. virtual registers, and provides a uniform
1246// implementation of most of the Value interface: Value.Name() is a
1247// numbered register (e.g. "t0"); the other methods are field accessors.
Rob Pike83f21b92013-05-17 13:25:48 -07001248//
Alan Donovan1ff34522013-10-14 13:48:34 -04001249// Temporary names are automatically assigned to each register on
Rob Pike83f21b92013-05-17 13:25:48 -07001250// completion of building a function in SSA form.
1251//
1252// Clients must not assume that the 'id' value (and the Name() derived
1253// from it) is unique within a function. As always in this API,
1254// semantics are determined only by identity; names exist only to
1255// facilitate debugging.
1256//
Alan Donovan1ff34522013-10-14 13:48:34 -04001257type register struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001258 anInstruction
1259 num int // "name" of virtual register, e.g. "t0". Not guaranteed unique.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001260 typ types.Type // type of virtual register
Rob Pike87334f42013-05-17 14:02:47 -07001261 pos token.Pos // position of source expression, or NoPos
Rob Pike83f21b92013-05-17 13:25:48 -07001262 referrers []Instruction
1263}
1264
1265// anInstruction is a mix-in embedded by all Instructions.
Alan Donovanf1e5b032013-11-07 10:08:51 -05001266// It provides the implementations of the Block and setBlock methods.
Rob Pike83f21b92013-05-17 13:25:48 -07001267type anInstruction struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001268 block *BasicBlock // the basic block of this instruction
Rob Pike83f21b92013-05-17 13:25:48 -07001269}
1270
1271// CallCommon is contained by Go, Defer and Call to hold the
1272// common parts of a function or method call.
1273//
1274// Each CallCommon exists in one of two modes, function call and
1275// interface method invocation, or "call" and "invoke" for short.
1276//
Alan Donovan4da31df2013-07-26 11:22:34 -04001277// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon
Alan Donovan70722532013-08-19 15:38:30 -04001278// represents an ordinary function call of the value in Value,
1279// which may be a *Builtin, a *Function or any other value of kind
1280// 'func'.
Rob Pike83f21b92013-05-17 13:25:48 -07001281//
Alan Donovand6eb8982014-01-07 13:31:05 -05001282// Value may be one of:
1283// (a) a *Function, indicating a statically dispatched call
1284// to a package-level function, an anonymous function, or
1285// a method of a named type.
1286// (b) a *MakeClosure, indicating an immediately applied
1287// function literal with free variables.
1288// (c) a *Builtin, indicating a statically dispatched call
Alan Donovane8afbfa2014-01-15 21:37:55 -05001289// to a built-in function.
Alan Donovand6eb8982014-01-07 13:31:05 -05001290// (d) any other value, indicating a dynamically dispatched
1291// function call.
1292// StaticCallee returns the identity of the callee in cases
1293// (a) and (b), nil otherwise.
Rob Pike83f21b92013-05-17 13:25:48 -07001294//
Alan Donovan118786e2013-07-26 14:06:26 -04001295// Args contains the arguments to the call. If Value is a method,
1296// Args[0] contains the receiver parameter.
Rob Pike83f21b92013-05-17 13:25:48 -07001297//
1298// Example printed form:
1299// t2 = println(t0, t1)
1300// go t3()
1301// defer t5(...t6)
1302//
Alan Donovan4da31df2013-07-26 11:22:34 -04001303// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon
Rob Pike83f21b92013-05-17 13:25:48 -07001304// represents a dynamically dispatched call to an interface method.
Alan Donovan118786e2013-07-26 14:06:26 -04001305// In this mode, Value is the interface value and Method is the
Alan Donovan70722532013-08-19 15:38:30 -04001306// interface's abstract method. Note: an abstract method may be
1307// shared by multiple interfaces due to embedding; Value.Type()
1308// provides the specific interface used for this call.
Rob Pike83f21b92013-05-17 13:25:48 -07001309//
Alan Donovan118786e2013-07-26 14:06:26 -04001310// Value is implicitly supplied to the concrete method implementation
Rob Pike83f21b92013-05-17 13:25:48 -07001311// as the receiver parameter; in other words, Args[0] holds not the
Alan Donovan118786e2013-07-26 14:06:26 -04001312// receiver but the first true argument.
Rob Pike83f21b92013-05-17 13:25:48 -07001313//
Rob Pike83f21b92013-05-17 13:25:48 -07001314// Example printed form:
1315// t1 = invoke t0.String()
1316// go invoke t3.Run(t2)
1317// defer invoke t4.Handle(...t5)
1318//
Robert Griesemerebfa4ef2014-01-28 10:57:56 -08001319// For all calls to variadic functions (Signature().Variadic()),
Alan Donovand6eb8982014-01-07 13:31:05 -05001320// the last element of Args is a slice.
Rob Pike83f21b92013-05-17 13:25:48 -07001321//
1322type CallCommon struct {
Alan Donovand6eb8982014-01-07 13:31:05 -05001323 Value Value // receiver (invoke mode) or func value (call mode)
1324 Method *types.Func // abstract method (invoke mode)
1325 Args []Value // actual parameters (in static method call, includes receiver)
1326 pos token.Pos // position of CallExpr.Lparen, iff explicit in source
Rob Pike83f21b92013-05-17 13:25:48 -07001327}
1328
1329// IsInvoke returns true if this call has "invoke" (not "call") mode.
1330func (c *CallCommon) IsInvoke() bool {
Alan Donovan4da31df2013-07-26 11:22:34 -04001331 return c.Method != nil
Rob Pike83f21b92013-05-17 13:25:48 -07001332}
1333
Rob Pike87334f42013-05-17 14:02:47 -07001334func (c *CallCommon) Pos() token.Pos { return c.pos }
1335
Alan Donovan8097dad2013-06-24 14:15:13 -04001336// Signature returns the signature of the called function.
1337//
1338// For an "invoke"-mode call, the signature of the interface method is
Alan Donovanb68a0292013-06-26 12:38:08 -04001339// returned.
1340//
1341// In either "call" or "invoke" mode, if the callee is a method, its
1342// receiver is represented by sig.Recv, not sig.Params().At(0).
Alan Donovan8097dad2013-06-24 14:15:13 -04001343//
1344func (c *CallCommon) Signature() *types.Signature {
Alan Donovan4da31df2013-07-26 11:22:34 -04001345 if c.Method != nil {
1346 return c.Method.Type().(*types.Signature)
Alan Donovan8097dad2013-06-24 14:15:13 -04001347 }
Alan Donovand6eb8982014-01-07 13:31:05 -05001348 return c.Value.Type().Underlying().(*types.Signature)
Alan Donovan8097dad2013-06-24 14:15:13 -04001349}
1350
Alan Donovand6eb8982014-01-07 13:31:05 -05001351// StaticCallee returns the callee if this is a trivially static
1352// "call"-mode call to a function.
Rob Pike83f21b92013-05-17 13:25:48 -07001353func (c *CallCommon) StaticCallee() *Function {
Alan Donovan118786e2013-07-26 14:06:26 -04001354 switch fn := c.Value.(type) {
Rob Pike83f21b92013-05-17 13:25:48 -07001355 case *Function:
1356 return fn
1357 case *MakeClosure:
1358 return fn.Fn.(*Function)
1359 }
1360 return nil
1361}
1362
Rob Pike83f21b92013-05-17 13:25:48 -07001363// Description returns a description of the mode of this call suitable
Alan Donovanf9642692015-02-20 10:42:06 -05001364// for a user interface, e.g., "static method call".
Rob Pike83f21b92013-05-17 13:25:48 -07001365func (c *CallCommon) Description() string {
Alan Donovan118786e2013-07-26 14:06:26 -04001366 switch fn := c.Value.(type) {
Alan Donovan70722532013-08-19 15:38:30 -04001367 case *Builtin:
1368 return "built-in function call"
Rob Pike83f21b92013-05-17 13:25:48 -07001369 case *MakeClosure:
1370 return "static function closure call"
1371 case *Function:
Rob Pike87334f42013-05-17 14:02:47 -07001372 if fn.Signature.Recv() != nil {
Rob Pike83f21b92013-05-17 13:25:48 -07001373 return "static method call"
1374 }
1375 return "static function call"
1376 }
Alan Donovan70722532013-08-19 15:38:30 -04001377 if c.IsInvoke() {
1378 return "dynamic method call" // ("invoke" mode)
1379 }
Rob Pike83f21b92013-05-17 13:25:48 -07001380 return "dynamic function call"
1381}
1382
Alan Donovan8097dad2013-06-24 14:15:13 -04001383// The CallInstruction interface, implemented by *Go, *Defer and *Call,
Alan Donovan149e0302014-08-01 14:44:37 -04001384// exposes the common parts of function-calling instructions,
Alan Donovan8097dad2013-06-24 14:15:13 -04001385// yet provides a way back to the Value defined by *Call alone.
1386//
1387type CallInstruction interface {
1388 Instruction
1389 Common() *CallCommon // returns the common parts of the call
1390 Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer)
1391}
1392
1393func (s *Call) Common() *CallCommon { return &s.Call }
1394func (s *Defer) Common() *CallCommon { return &s.Call }
1395func (s *Go) Common() *CallCommon { return &s.Call }
1396
1397func (s *Call) Value() *Call { return s }
1398func (s *Defer) Value() *Call { return nil }
1399func (s *Go) Value() *Call { return nil }
1400
Alan Donovand6eb8982014-01-07 13:31:05 -05001401func (v *Builtin) Type() types.Type { return v.sig }
Alan Donovande23e2b2014-06-13 17:34:07 -04001402func (v *Builtin) Name() string { return v.name }
Rob Pike83f21b92013-05-17 13:25:48 -07001403func (*Builtin) Referrers() *[]Instruction { return nil }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001404func (v *Builtin) Pos() token.Pos { return token.NoPos }
Alan Donovande23e2b2014-06-13 17:34:07 -04001405func (v *Builtin) Object() types.Object { return types.Universe.Lookup(v.name) }
Alan Donovan04427c82014-06-11 13:14:06 -04001406func (v *Builtin) Parent() *Function { return nil }
Rob Pike83f21b92013-05-17 13:25:48 -07001407
Alan Donovancc02c5b2014-06-11 14:04:45 -04001408func (v *FreeVar) Type() types.Type { return v.typ }
1409func (v *FreeVar) Name() string { return v.name }
1410func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers }
1411func (v *FreeVar) Pos() token.Pos { return v.pos }
1412func (v *FreeVar) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001413
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001414func (v *Global) Type() types.Type { return v.typ }
1415func (v *Global) Name() string { return v.name }
Alan Donovan04427c82014-06-11 13:14:06 -04001416func (v *Global) Parent() *Function { return nil }
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001417func (v *Global) Pos() token.Pos { return v.pos }
1418func (v *Global) Referrers() *[]Instruction { return nil }
1419func (v *Global) Token() token.Token { return token.VAR }
1420func (v *Global) Object() types.Object { return v.object }
1421func (v *Global) String() string { return v.RelString(nil) }
1422func (v *Global) Package() *Package { return v.Pkg }
1423func (v *Global) RelString(from *types.Package) string { return relString(v, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001424
Alan Donovanf1e5b032013-11-07 10:08:51 -05001425func (v *Function) Name() string { return v.name }
1426func (v *Function) Type() types.Type { return v.Signature }
1427func (v *Function) Pos() token.Pos { return v.pos }
1428func (v *Function) Token() token.Token { return token.FUNC }
1429func (v *Function) Object() types.Object { return v.object }
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001430func (v *Function) String() string { return v.RelString(nil) }
1431func (v *Function) Package() *Package { return v.Pkg }
Alan Donovan04427c82014-06-11 13:14:06 -04001432func (v *Function) Parent() *Function { return v.parent }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001433func (v *Function) Referrers() *[]Instruction {
Alan Donovan04427c82014-06-11 13:14:06 -04001434 if v.parent != nil {
Alan Donovanf1e5b032013-11-07 10:08:51 -05001435 return &v.referrers
1436 }
1437 return nil
1438}
Rob Pike83f21b92013-05-17 13:25:48 -07001439
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001440func (v *Parameter) Type() types.Type { return v.typ }
1441func (v *Parameter) Name() string { return v.name }
Alan Donovan55d678e2013-07-15 13:56:46 -04001442func (v *Parameter) Object() types.Object { return v.object }
Rob Pike83f21b92013-05-17 13:25:48 -07001443func (v *Parameter) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001444func (v *Parameter) Pos() token.Pos { return v.pos }
Alan Donovan341a07a2013-06-13 14:43:35 -04001445func (v *Parameter) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001446
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001447func (v *Alloc) Type() types.Type { return v.typ }
Rob Pike83f21b92013-05-17 13:25:48 -07001448func (v *Alloc) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001449func (v *Alloc) Pos() token.Pos { return v.pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001450
Alan Donovan1ff34522013-10-14 13:48:34 -04001451func (v *register) Type() types.Type { return v.typ }
1452func (v *register) setType(typ types.Type) { v.typ = typ }
1453func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) }
1454func (v *register) setNum(num int) { v.num = num }
1455func (v *register) Referrers() *[]Instruction { return &v.referrers }
1456func (v *register) Pos() token.Pos { return v.pos }
1457func (v *register) setPos(pos token.Pos) { v.pos = pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001458
Alan Donovan341a07a2013-06-13 14:43:35 -04001459func (v *anInstruction) Parent() *Function { return v.block.parent }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001460func (v *anInstruction) Block() *BasicBlock { return v.block }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001461func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block }
Alan Donovan04427c82014-06-11 13:14:06 -04001462func (v *anInstruction) Referrers() *[]Instruction { return nil }
Rob Pike83f21b92013-05-17 13:25:48 -07001463
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001464func (t *Type) Name() string { return t.object.Name() }
1465func (t *Type) Pos() token.Pos { return t.object.Pos() }
1466func (t *Type) Type() types.Type { return t.object.Type() }
1467func (t *Type) Token() token.Token { return token.TYPE }
1468func (t *Type) Object() types.Object { return t.object }
1469func (t *Type) String() string { return t.RelString(nil) }
1470func (t *Type) Package() *Package { return t.pkg }
1471func (t *Type) RelString(from *types.Package) string { return relString(t, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001472
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001473func (c *NamedConst) Name() string { return c.object.Name() }
1474func (c *NamedConst) Pos() token.Pos { return c.object.Pos() }
1475func (c *NamedConst) String() string { return c.RelString(nil) }
1476func (c *NamedConst) Type() types.Type { return c.object.Type() }
1477func (c *NamedConst) Token() token.Token { return token.CONST }
1478func (c *NamedConst) Object() types.Object { return c.object }
1479func (c *NamedConst) Package() *Package { return c.pkg }
1480func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001481
1482// Func returns the package-level function of the specified name,
1483// or nil if not found.
1484//
1485func (p *Package) Func(name string) (f *Function) {
1486 f, _ = p.Members[name].(*Function)
1487 return
1488}
1489
1490// Var returns the package-level variable of the specified name,
1491// or nil if not found.
1492//
1493func (p *Package) Var(name string) (g *Global) {
1494 g, _ = p.Members[name].(*Global)
1495 return
1496}
1497
1498// Const returns the package-level constant of the specified name,
1499// or nil if not found.
1500//
Alan Donovan732dbe92013-07-16 13:50:08 -04001501func (p *Package) Const(name string) (c *NamedConst) {
1502 c, _ = p.Members[name].(*NamedConst)
Rob Pike83f21b92013-05-17 13:25:48 -07001503 return
1504}
1505
1506// Type returns the package-level type of the specified name,
1507// or nil if not found.
1508//
1509func (p *Package) Type(name string) (t *Type) {
1510 t, _ = p.Members[name].(*Type)
1511 return
1512}
1513
Rob Pike87334f42013-05-17 14:02:47 -07001514func (v *Call) Pos() token.Pos { return v.Call.pos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001515func (s *Defer) Pos() token.Pos { return s.pos }
1516func (s *Go) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001517func (s *MapUpdate) Pos() token.Pos { return s.pos }
1518func (s *Panic) Pos() token.Pos { return s.pos }
Alan Donovan068f0172013-10-08 12:31:39 -04001519func (s *Return) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001520func (s *Send) Pos() token.Pos { return s.pos }
1521func (s *Store) Pos() token.Pos { return s.pos }
1522func (s *If) Pos() token.Pos { return token.NoPos }
1523func (s *Jump) Pos() token.Pos { return token.NoPos }
1524func (s *RunDefers) Pos() token.Pos { return token.NoPos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001525func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() }
Rob Pike83f21b92013-05-17 13:25:48 -07001526
Rob Pike87334f42013-05-17 14:02:47 -07001527// Operands.
Rob Pike83f21b92013-05-17 13:25:48 -07001528
1529func (v *Alloc) Operands(rands []*Value) []*Value {
1530 return rands
1531}
1532
1533func (v *BinOp) Operands(rands []*Value) []*Value {
1534 return append(rands, &v.X, &v.Y)
1535}
1536
1537func (c *CallCommon) Operands(rands []*Value) []*Value {
Alan Donovan118786e2013-07-26 14:06:26 -04001538 rands = append(rands, &c.Value)
Rob Pike83f21b92013-05-17 13:25:48 -07001539 for i := range c.Args {
1540 rands = append(rands, &c.Args[i])
1541 }
1542 return rands
1543}
1544
1545func (s *Go) Operands(rands []*Value) []*Value {
1546 return s.Call.Operands(rands)
1547}
1548
1549func (s *Call) Operands(rands []*Value) []*Value {
1550 return s.Call.Operands(rands)
1551}
1552
1553func (s *Defer) Operands(rands []*Value) []*Value {
1554 return s.Call.Operands(rands)
1555}
1556
1557func (v *ChangeInterface) Operands(rands []*Value) []*Value {
1558 return append(rands, &v.X)
1559}
1560
Rob Pike87334f42013-05-17 14:02:47 -07001561func (v *ChangeType) Operands(rands []*Value) []*Value {
1562 return append(rands, &v.X)
1563}
1564
1565func (v *Convert) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001566 return append(rands, &v.X)
1567}
1568
Alan Donovan55d678e2013-07-15 13:56:46 -04001569func (s *DebugRef) Operands(rands []*Value) []*Value {
1570 return append(rands, &s.X)
1571}
1572
Rob Pike83f21b92013-05-17 13:25:48 -07001573func (v *Extract) Operands(rands []*Value) []*Value {
1574 return append(rands, &v.Tuple)
1575}
1576
1577func (v *Field) Operands(rands []*Value) []*Value {
1578 return append(rands, &v.X)
1579}
1580
1581func (v *FieldAddr) Operands(rands []*Value) []*Value {
1582 return append(rands, &v.X)
1583}
1584
1585func (s *If) Operands(rands []*Value) []*Value {
1586 return append(rands, &s.Cond)
1587}
1588
1589func (v *Index) Operands(rands []*Value) []*Value {
1590 return append(rands, &v.X, &v.Index)
1591}
1592
1593func (v *IndexAddr) Operands(rands []*Value) []*Value {
1594 return append(rands, &v.X, &v.Index)
1595}
1596
1597func (*Jump) Operands(rands []*Value) []*Value {
1598 return rands
1599}
1600
1601func (v *Lookup) Operands(rands []*Value) []*Value {
1602 return append(rands, &v.X, &v.Index)
1603}
1604
1605func (v *MakeChan) Operands(rands []*Value) []*Value {
1606 return append(rands, &v.Size)
1607}
1608
1609func (v *MakeClosure) Operands(rands []*Value) []*Value {
1610 rands = append(rands, &v.Fn)
1611 for i := range v.Bindings {
1612 rands = append(rands, &v.Bindings[i])
1613 }
1614 return rands
1615}
1616
1617func (v *MakeInterface) Operands(rands []*Value) []*Value {
1618 return append(rands, &v.X)
1619}
1620
1621func (v *MakeMap) Operands(rands []*Value) []*Value {
1622 return append(rands, &v.Reserve)
1623}
1624
1625func (v *MakeSlice) Operands(rands []*Value) []*Value {
1626 return append(rands, &v.Len, &v.Cap)
1627}
1628
1629func (v *MapUpdate) Operands(rands []*Value) []*Value {
1630 return append(rands, &v.Map, &v.Key, &v.Value)
1631}
1632
1633func (v *Next) Operands(rands []*Value) []*Value {
1634 return append(rands, &v.Iter)
1635}
1636
1637func (s *Panic) Operands(rands []*Value) []*Value {
1638 return append(rands, &s.X)
1639}
1640
1641func (v *Phi) Operands(rands []*Value) []*Value {
1642 for i := range v.Edges {
1643 rands = append(rands, &v.Edges[i])
1644 }
1645 return rands
1646}
1647
1648func (v *Range) Operands(rands []*Value) []*Value {
1649 return append(rands, &v.X)
1650}
1651
Alan Donovan068f0172013-10-08 12:31:39 -04001652func (s *Return) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001653 for i := range s.Results {
1654 rands = append(rands, &s.Results[i])
1655 }
1656 return rands
1657}
1658
1659func (*RunDefers) Operands(rands []*Value) []*Value {
1660 return rands
1661}
1662
1663func (v *Select) Operands(rands []*Value) []*Value {
1664 for i := range v.States {
1665 rands = append(rands, &v.States[i].Chan, &v.States[i].Send)
1666 }
1667 return rands
1668}
1669
1670func (s *Send) Operands(rands []*Value) []*Value {
1671 return append(rands, &s.Chan, &s.X)
1672}
1673
1674func (v *Slice) Operands(rands []*Value) []*Value {
Peter Collingbournecd36f522014-06-11 16:16:19 -04001675 return append(rands, &v.X, &v.Low, &v.High, &v.Max)
Rob Pike83f21b92013-05-17 13:25:48 -07001676}
1677
1678func (s *Store) Operands(rands []*Value) []*Value {
1679 return append(rands, &s.Addr, &s.Val)
1680}
1681
1682func (v *TypeAssert) Operands(rands []*Value) []*Value {
1683 return append(rands, &v.X)
1684}
1685
1686func (v *UnOp) Operands(rands []*Value) []*Value {
1687 return append(rands, &v.X)
1688}
Alan Donovan04427c82014-06-11 13:14:06 -04001689
1690// Non-Instruction Values:
1691func (v *Builtin) Operands(rands []*Value) []*Value { return rands }
Alan Donovancc02c5b2014-06-11 14:04:45 -04001692func (v *FreeVar) Operands(rands []*Value) []*Value { return rands }
Alan Donovan04427c82014-06-11 13:14:06 -04001693func (v *Const) Operands(rands []*Value) []*Value { return rands }
1694func (v *Function) Operands(rands []*Value) []*Value { return rands }
1695func (v *Global) Operands(rands []*Value) []*Value { return rands }
1696func (v *Parameter) Operands(rands []*Value) []*Value { return rands }