blob: 78272c546a1f1bbdc30b7d0ea7547fa512662c9f [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 Donovane96c4e22018-08-10 12:49:16 -040013 "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 Donovane96c4e22018-08-10 12:49:16 -0400407// Value holds the value of the constant, independent of its Type(),
408// using go/constant representation, or nil for a typed nil value.
Rob Pike83f21b92013-05-17 13:25:48 -0700409//
Alan Donovana399e262013-07-15 16:10:08 -0400410// Pos() returns token.NoPos.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400411//
Rob Pike83f21b92013-05-17 13:25:48 -0700412// Example printed form:
413// 42:int
414// "hello":untyped string
415// 3+4i:MyComplex
416//
Alan Donovan732dbe92013-07-16 13:50:08 -0400417type Const struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400418 typ types.Type
Alan Donovane96c4e22018-08-10 12:49:16 -0400419 Value constant.Value
Rob Pike83f21b92013-05-17 13:25:48 -0700420}
421
422// A Global is a named Value holding the address of a package-level
423// variable.
424//
Rob Pike87334f42013-05-17 14:02:47 -0700425// Pos() returns the position of the ast.ValueSpec.Names[*]
426// identifier.
427//
Rob Pike83f21b92013-05-17 13:25:48 -0700428type Global struct {
Alan Donovanbc1f7242013-07-11 14:12:30 -0400429 name string
430 object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
431 typ types.Type
432 pos token.Pos
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400433
434 Pkg *Package
Rob Pike83f21b92013-05-17 13:25:48 -0700435}
436
Alan Donovand6eb8982014-01-07 13:31:05 -0500437// A Builtin represents a specific use of a built-in function, e.g. len.
Rob Pike83f21b92013-05-17 13:25:48 -0700438//
Rob Pike87334f42013-05-17 14:02:47 -0700439// Builtins are immutable values. Builtins do not have addresses.
Alan Donovan55d678e2013-07-15 13:56:46 -0400440// Builtins can only appear in CallCommon.Func.
Rob Pike83f21b92013-05-17 13:25:48 -0700441//
Alan Donovande23e2b2014-06-13 17:34:07 -0400442// Name() indicates the function: one of the built-in functions from the
443// Go spec (excluding "make" and "new") or one of these ssa-defined
444// intrinsics:
445//
446// // wrapnilchk returns ptr if non-nil, panics otherwise.
447// // (For use in indirection wrappers.)
448// func ssa:wrapnilchk(ptr *T, recvType, methodName string) *T
449//
450// Object() returns a *types.Builtin for built-ins defined by the spec,
451// nil for others.
Alan Donovan318b83e2013-09-23 18:18:35 -0400452//
Alan Donovand6eb8982014-01-07 13:31:05 -0500453// Type() returns a *types.Signature representing the effective
454// signature of the built-in for this call.
Rob Pike83f21b92013-05-17 13:25:48 -0700455//
456type Builtin struct {
Alan Donovande23e2b2014-06-13 17:34:07 -0400457 name string
458 sig *types.Signature
Rob Pike83f21b92013-05-17 13:25:48 -0700459}
460
461// Value-defining instructions ----------------------------------------
462
Alan Donovanf9642692015-02-20 10:42:06 -0500463// The Alloc instruction reserves space for a variable of the given type,
Rob Pike83f21b92013-05-17 13:25:48 -0700464// zero-initializes it, and yields its address.
465//
466// Alloc values are always addresses, and have pointer types, so the
Alan Donovanf9642692015-02-20 10:42:06 -0500467// type of the allocated variable is actually
468// Type().Underlying().(*types.Pointer).Elem().
Rob Pike83f21b92013-05-17 13:25:48 -0700469//
470// If Heap is false, Alloc allocates space in the function's
471// activation record (frame); we refer to an Alloc(Heap=false) as a
472// "local" alloc. Each local Alloc returns the same address each time
473// it is executed within the same activation; the space is
474// re-initialized to zero.
475//
Alan Donovanf9642692015-02-20 10:42:06 -0500476// If Heap is true, Alloc allocates space in the heap; we
Rob Pike83f21b92013-05-17 13:25:48 -0700477// refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc
478// returns a different address each time it is executed.
479//
480// When Alloc is applied to a channel, map or slice type, it returns
481// the address of an uninitialized (nil) reference of that kind; store
482// the result of MakeSlice, MakeMap or MakeChan in that location to
483// instantiate these types.
484//
Rob Pike87334f42013-05-17 14:02:47 -0700485// Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
Alan Donovan38cb4c02014-06-13 17:12:28 -0400486// or the ast.CallExpr.Rparen for a call to new() or for a call that
Rob Pike87334f42013-05-17 14:02:47 -0700487// allocates a varargs slice.
488//
Rob Pike83f21b92013-05-17 13:25:48 -0700489// Example printed form:
490// t0 = local int
491// t1 = new int
492//
493type Alloc struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400494 register
Alan Donovan5cc33ea2013-08-01 14:06:10 -0400495 Comment string
496 Heap bool
497 index int // dense numbering; for lifting
Rob Pike83f21b92013-05-17 13:25:48 -0700498}
499
Rob Pike87334f42013-05-17 14:02:47 -0700500// The Phi instruction represents an SSA φ-node, which combines values
501// that differ across incoming control-flow edges and yields a new
502// value. Within a block, all φ-nodes must appear before all non-φ
503// nodes.
504//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400505// Pos() returns the position of the && or || for short-circuit
506// control-flow joins, or that of the *Alloc for φ-nodes inserted
507// during SSA renaming.
Rob Pike83f21b92013-05-17 13:25:48 -0700508//
509// Example printed form:
Alan Donovanf9642692015-02-20 10:42:06 -0500510// t2 = phi [0: t0, 1: t1]
Rob Pike83f21b92013-05-17 13:25:48 -0700511//
512type Phi struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400513 register
Rob Pike83f21b92013-05-17 13:25:48 -0700514 Comment string // a hint as to its purpose
515 Edges []Value // Edges[i] is value for Block().Preds[i]
516}
517
Rob Pike87334f42013-05-17 14:02:47 -0700518// The Call instruction represents a function or method call.
Rob Pike83f21b92013-05-17 13:25:48 -0700519//
Alan Donovanf9642692015-02-20 10:42:06 -0500520// The Call instruction yields the function result if there is exactly
521// one. Otherwise it returns a tuple, the components of which are
Rob Pike83f21b92013-05-17 13:25:48 -0700522// accessed via Extract.
523//
524// See CallCommon for generic function call documentation.
525//
Rob Pike87334f42013-05-17 14:02:47 -0700526// Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
527//
Rob Pike83f21b92013-05-17 13:25:48 -0700528// Example printed form:
529// t2 = println(t0, t1)
530// t4 = t3()
531// t7 = invoke t5.Println(...t6)
532//
533type Call struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400534 register
Rob Pike83f21b92013-05-17 13:25:48 -0700535 Call CallCommon
536}
537
Rob Pike87334f42013-05-17 14:02:47 -0700538// The BinOp instruction yields the result of binary operation X Op Y.
539//
540// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700541//
542// Example printed form:
543// t1 = t0 + 1:int
544//
545type BinOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400546 register
Rob Pike83f21b92013-05-17 13:25:48 -0700547 // One of:
548 // ADD SUB MUL QUO REM + - * / %
Austin Clements25b95b42018-06-19 21:23:51 -0400549 // AND OR XOR SHL SHR AND_NOT & | ^ << >> &^
550 // EQL NEQ LSS LEQ GTR GEQ == != < <= < >=
Rob Pike83f21b92013-05-17 13:25:48 -0700551 Op token.Token
552 X, Y Value
553}
554
Rob Pike87334f42013-05-17 14:02:47 -0700555// The UnOp instruction yields the result of Op X.
Rob Pike83f21b92013-05-17 13:25:48 -0700556// ARROW is channel receive.
557// MUL is pointer indirection (load).
558// XOR is bitwise complement.
559// SUB is negation.
Alan Donovanba9c8012014-03-11 18:24:39 -0400560// NOT is logical negation.
Rob Pike83f21b92013-05-17 13:25:48 -0700561//
562// If CommaOk and Op=ARROW, the result is a 2-tuple of the value above
563// and a boolean indicating the success of the receive. The
564// components of the tuple are accessed using Extract.
565//
Alan Donovan538acf12014-12-29 16:49:20 -0500566// Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source.
567// For receive operations (ARROW) implicit in ranging over a channel,
568// Pos() returns the ast.RangeStmt.For.
569// For implicit memory loads (STAR), Pos() returns the position of the
570// most closely associated source-level construct; the details are not
571// specified.
Rob Pike87334f42013-05-17 14:02:47 -0700572//
Rob Pike83f21b92013-05-17 13:25:48 -0700573// Example printed form:
574// t0 = *x
575// t2 = <-t1,ok
576//
577type UnOp struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400578 register
Rob Pike83f21b92013-05-17 13:25:48 -0700579 Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^
580 X Value
581 CommaOk bool
582}
583
Rob Pike87334f42013-05-17 14:02:47 -0700584// The ChangeType instruction applies to X a value-preserving type
585// change to Type().
Rob Pike83f21b92013-05-17 13:25:48 -0700586//
Rob Pike87334f42013-05-17 14:02:47 -0700587// Type changes are permitted:
588// - between a named type and its underlying type.
589// - between two named types of the same underlying type.
590// - between (possibly named) pointers to identical base types.
Rob Pike87334f42013-05-17 14:02:47 -0700591// - from a bidirectional channel to a read- or write-channel,
592// optionally adding/removing a name.
Rob Pike83f21b92013-05-17 13:25:48 -0700593//
Rob Pike87334f42013-05-17 14:02:47 -0700594// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700595//
Rob Pike87334f42013-05-17 14:02:47 -0700596// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
597// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700598//
Rob Pike87334f42013-05-17 14:02:47 -0700599// Example printed form:
600// t1 = changetype *int <- IntPtr (t0)
601//
602type ChangeType struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400603 register
Rob Pike87334f42013-05-17 14:02:47 -0700604 X Value
605}
606
607// The Convert instruction yields the conversion of value X to type
Alan Donovan8097dad2013-06-24 14:15:13 -0400608// Type(). One or both of those types is basic (but possibly named).
Rob Pike87334f42013-05-17 14:02:47 -0700609//
610// A conversion may change the value and representation of its operand.
611// Conversions are permitted:
612// - between real numeric types.
613// - between complex numeric types.
614// - between string and []byte or []rune.
Alan Donovan8097dad2013-06-24 14:15:13 -0400615// - between pointers and unsafe.Pointer.
616// - between unsafe.Pointer and uintptr.
Rob Pike87334f42013-05-17 14:02:47 -0700617// - from (Unicode) integer to (UTF-8) string.
618// A conversion may imply a type name change also.
619//
620// This operation cannot fail dynamically.
Rob Pike83f21b92013-05-17 13:25:48 -0700621//
622// Conversions of untyped string/number/bool constants to a specific
623// representation are eliminated during SSA construction.
624//
Rob Pike87334f42013-05-17 14:02:47 -0700625// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
626// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700627//
Rob Pike87334f42013-05-17 14:02:47 -0700628// Example printed form:
629// t1 = convert []byte <- string (t0)
630//
631type Convert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400632 register
Rob Pike83f21b92013-05-17 13:25:48 -0700633 X Value
634}
635
636// ChangeInterface constructs a value of one interface type from a
637// value of another interface type known to be assignable to it.
Alan Donovanae801632013-07-26 21:49:27 -0400638// This operation cannot fail.
Rob Pike87334f42013-05-17 14:02:47 -0700639//
Alan Donovan341a07a2013-06-13 14:43:35 -0400640// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
641// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
642// instruction arose from an explicit e.(T) operation; or token.NoPos
643// otherwise.
644//
Rob Pike83f21b92013-05-17 13:25:48 -0700645// Example printed form:
646// t1 = change interface interface{} <- I (t0)
647//
648type ChangeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400649 register
Rob Pike83f21b92013-05-17 13:25:48 -0700650 X Value
651}
652
653// MakeInterface constructs an instance of an interface type from a
Alan Donovan341a07a2013-06-13 14:43:35 -0400654// value of a concrete type.
655//
Alan Donovan1f29e742014-02-11 16:49:27 -0500656// Use Program.MethodSets.MethodSet(X.Type()) to find the method-set
Nicholas Ng8542fc22017-05-03 23:14:54 +0100657// of X, and Program.MethodValue(m) to find the implementation of a method.
Rob Pike83f21b92013-05-17 13:25:48 -0700658//
659// To construct the zero value of an interface type T, use:
Alan Donovane96c4e22018-08-10 12:49:16 -0400660// NewConst(constant.MakeNil(), T, pos)
Rob Pike87334f42013-05-17 14:02:47 -0700661//
662// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
663// from an explicit conversion in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700664//
665// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400666// t1 = make interface{} <- int (42:int)
667// t2 = make Stringer <- t0
Rob Pike83f21b92013-05-17 13:25:48 -0700668//
669type MakeInterface struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400670 register
Alan Donovan341a07a2013-06-13 14:43:35 -0400671 X Value
Rob Pike83f21b92013-05-17 13:25:48 -0700672}
673
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400674// The MakeClosure instruction yields a closure value whose code is
675// Fn and whose free variables' values are supplied by Bindings.
Rob Pike83f21b92013-05-17 13:25:48 -0700676//
677// Type() returns a (possibly named) *types.Signature.
678//
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400679// Pos() returns the ast.FuncLit.Type.Func for a function literal
680// closure or the ast.SelectorExpr.Sel for a bound method closure.
Rob Pike87334f42013-05-17 14:02:47 -0700681//
Rob Pike83f21b92013-05-17 13:25:48 -0700682// Example printed form:
683// t0 = make closure anon@1.2 [x y z]
Alan Donovan8cdf1f12013-05-22 17:56:18 -0400684// t1 = make closure bound$(main.I).add [i]
Rob Pike83f21b92013-05-17 13:25:48 -0700685//
686type MakeClosure struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400687 register
Rob Pike83f21b92013-05-17 13:25:48 -0700688 Fn Value // always a *Function
689 Bindings []Value // values for each free variable in Fn.FreeVars
690}
691
692// The MakeMap instruction creates a new hash-table-based map object
693// and yields a value of kind map.
694//
695// Type() returns a (possibly named) *types.Map.
696//
Rob Pike87334f42013-05-17 14:02:47 -0700697// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
698// the ast.CompositeLit.Lbrack if created by a literal.
699//
Rob Pike83f21b92013-05-17 13:25:48 -0700700// Example printed form:
701// t1 = make map[string]int t0
Alan Donovan341a07a2013-06-13 14:43:35 -0400702// t1 = make StringIntMap t0
Rob Pike83f21b92013-05-17 13:25:48 -0700703//
704type MakeMap struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400705 register
Rob Pike83f21b92013-05-17 13:25:48 -0700706 Reserve Value // initial space reservation; nil => default
Rob Pike83f21b92013-05-17 13:25:48 -0700707}
708
709// The MakeChan instruction creates a new channel object and yields a
710// value of kind chan.
711//
712// Type() returns a (possibly named) *types.Chan.
713//
Rob Pike87334f42013-05-17 14:02:47 -0700714// Pos() returns the ast.CallExpr.Lparen for the make(chan) that
715// created it.
716//
Rob Pike83f21b92013-05-17 13:25:48 -0700717// Example printed form:
718// t0 = make chan int 0
Alan Donovan341a07a2013-06-13 14:43:35 -0400719// t0 = make IntChan 0
Rob Pike83f21b92013-05-17 13:25:48 -0700720//
721type MakeChan struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400722 register
Rob Pike83f21b92013-05-17 13:25:48 -0700723 Size Value // int; size of buffer; zero => synchronous.
Rob Pike83f21b92013-05-17 13:25:48 -0700724}
725
Rob Pike87334f42013-05-17 14:02:47 -0700726// The MakeSlice instruction yields a slice of length Len backed by a
727// newly allocated array of length Cap.
Rob Pike83f21b92013-05-17 13:25:48 -0700728//
729// Both Len and Cap must be non-nil Values of integer type.
730//
731// (Alloc(types.Array) followed by Slice will not suffice because
Alan Donovanba9c8012014-03-11 18:24:39 -0400732// Alloc can only create arrays of constant length.)
Rob Pike83f21b92013-05-17 13:25:48 -0700733//
734// Type() returns a (possibly named) *types.Slice.
735//
Rob Pike87334f42013-05-17 14:02:47 -0700736// Pos() returns the ast.CallExpr.Lparen for the make([]T) that
737// created it.
738//
Rob Pike83f21b92013-05-17 13:25:48 -0700739// Example printed form:
Alan Donovan341a07a2013-06-13 14:43:35 -0400740// t1 = make []string 1:int t0
741// t1 = make StringSlice 1:int t0
Rob Pike83f21b92013-05-17 13:25:48 -0700742//
743type MakeSlice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400744 register
Rob Pike83f21b92013-05-17 13:25:48 -0700745 Len Value
746 Cap Value
Rob Pike83f21b92013-05-17 13:25:48 -0700747}
748
Rob Pike87334f42013-05-17 14:02:47 -0700749// The Slice instruction yields a slice of an existing string, slice
750// or *array X between optional integer bounds Low and High.
Rob Pike83f21b92013-05-17 13:25:48 -0700751//
Alan Donovan70722532013-08-19 15:38:30 -0400752// Dynamically, this instruction panics if X evaluates to a nil *array
753// pointer.
754//
Rob Pike83f21b92013-05-17 13:25:48 -0700755// Type() returns string if the type of X was string, otherwise a
756// *types.Slice with the same element type as X.
757//
Rob Pike87334f42013-05-17 14:02:47 -0700758// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
759// operation, the ast.CompositeLit.Lbrace if created by a literal, or
760// NoPos if not explicit in the source (e.g. a variadic argument slice).
761//
Rob Pike83f21b92013-05-17 13:25:48 -0700762// Example printed form:
763// t1 = slice t0[1:]
764//
765type Slice struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400766 register
Alan Donovanb3dbe562014-02-05 17:54:51 -0500767 X Value // slice, string, or *array
768 Low, High, Max Value // each may be nil
Rob Pike83f21b92013-05-17 13:25:48 -0700769}
770
Rob Pike87334f42013-05-17 14:02:47 -0700771// The FieldAddr instruction yields the address of Field of *struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700772//
773// The field is identified by its index within the field list of the
774// struct type of X.
775//
Alan Donovan70722532013-08-19 15:38:30 -0400776// Dynamically, this instruction panics if X evaluates to a nil
777// pointer.
778//
Rob Pike83f21b92013-05-17 13:25:48 -0700779// Type() returns a (possibly named) *types.Pointer.
780//
Rob Pike87334f42013-05-17 14:02:47 -0700781// Pos() returns the position of the ast.SelectorExpr.Sel for the
782// field, if explicit in the source.
783//
Rob Pike83f21b92013-05-17 13:25:48 -0700784// Example printed form:
785// t1 = &t0.name [#1]
786//
787type FieldAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400788 register
Rob Pike83f21b92013-05-17 13:25:48 -0700789 X Value // *struct
Austin Clements25b95b42018-06-19 21:23:51 -0400790 Field int // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field)
Rob Pike83f21b92013-05-17 13:25:48 -0700791}
792
Rob Pike87334f42013-05-17 14:02:47 -0700793// The Field instruction yields the Field of struct X.
Rob Pike83f21b92013-05-17 13:25:48 -0700794//
795// The field is identified by its index within the field list of the
796// struct type of X; by using numeric indices we avoid ambiguity of
797// package-local identifiers and permit compact representations.
798//
Rob Pike87334f42013-05-17 14:02:47 -0700799// Pos() returns the position of the ast.SelectorExpr.Sel for the
800// field, if explicit in the source.
801//
Rob Pike83f21b92013-05-17 13:25:48 -0700802// Example printed form:
803// t1 = t0.name [#1]
804//
805type Field struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400806 register
Rob Pike83f21b92013-05-17 13:25:48 -0700807 X Value // struct
808 Field int // index into X.Type().(*types.Struct).Fields
809}
810
Rob Pike87334f42013-05-17 14:02:47 -0700811// The IndexAddr instruction yields the address of the element at
812// index Index of collection X. Index is an integer expression.
Rob Pike83f21b92013-05-17 13:25:48 -0700813//
814// The elements of maps and strings are not addressable; use Lookup or
815// MapUpdate instead.
816//
Alan Donovan70722532013-08-19 15:38:30 -0400817// Dynamically, this instruction panics if X evaluates to a nil *array
818// pointer.
819//
Rob Pike83f21b92013-05-17 13:25:48 -0700820// Type() returns a (possibly named) *types.Pointer.
821//
Rob Pike87334f42013-05-17 14:02:47 -0700822// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
823// explicit in the source.
824//
Rob Pike83f21b92013-05-17 13:25:48 -0700825// Example printed form:
826// t2 = &t0[t1]
827//
828type IndexAddr struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400829 register
Rob Pike83f21b92013-05-17 13:25:48 -0700830 X Value // slice or *array,
831 Index Value // numeric index
832}
833
Rob Pike87334f42013-05-17 14:02:47 -0700834// The Index instruction yields element Index of array X.
835//
836// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
837// explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -0700838//
839// Example printed form:
840// t2 = t0[t1]
841//
842type Index struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400843 register
Rob Pike83f21b92013-05-17 13:25:48 -0700844 X Value // array
845 Index Value // integer index
846}
847
Rob Pike87334f42013-05-17 14:02:47 -0700848// The Lookup instruction yields element Index of collection X, a map
849// or string. Index is an integer expression if X is a string or the
850// appropriate key type if X is a map.
Rob Pike83f21b92013-05-17 13:25:48 -0700851//
852// If CommaOk, the result is a 2-tuple of the value above and a
853// boolean indicating the result of a map membership test for the key.
854// The components of the tuple are accessed using Extract.
855//
Rob Pike87334f42013-05-17 14:02:47 -0700856// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
857//
Rob Pike83f21b92013-05-17 13:25:48 -0700858// Example printed form:
859// t2 = t0[t1]
860// t5 = t3[t4],ok
861//
862type Lookup struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400863 register
Rob Pike83f21b92013-05-17 13:25:48 -0700864 X Value // string or map
865 Index Value // numeric or key-typed index
866 CommaOk bool // return a value,ok pair
867}
868
869// SelectState is a helper for Select.
870// It represents one goal state and its corresponding communication.
871//
872type SelectState struct {
Robert Griesemer74d33a92013-12-17 15:45:01 -0800873 Dir types.ChanDir // direction of case (SendOnly or RecvOnly)
874 Chan Value // channel to use (for send or receive)
875 Send Value // value to send (for send)
876 Pos token.Pos // position of token.ARROW
877 DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
Rob Pike83f21b92013-05-17 13:25:48 -0700878}
879
Alan Donovand6eb8982014-01-07 13:31:05 -0500880// The Select instruction tests whether (or blocks until) one
Rob Pike87334f42013-05-17 14:02:47 -0700881// of the specified sent or received states is entered.
Rob Pike83f21b92013-05-17 13:25:48 -0700882//
Alan Donovan8097dad2013-06-24 14:15:13 -0400883// Let n be the number of States for which Dir==RECV and T_i (0<=i<n)
884// be the element type of each such state's Chan.
885// Select returns an n+2-tuple
886// (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)
887// The tuple's components, described below, must be accessed via the
888// Extract instruction.
Rob Pike83f21b92013-05-17 13:25:48 -0700889//
890// If Blocking, select waits until exactly one state holds, i.e. a
891// channel becomes ready for the designated operation of sending or
892// receiving; select chooses one among the ready states
893// pseudorandomly, performs the send or receive operation, and sets
894// 'index' to the index of the chosen channel.
895//
896// If !Blocking, select doesn't block if no states hold; instead it
897// returns immediately with index equal to -1.
898//
Alan Donovan8097dad2013-06-24 14:15:13 -0400899// If the chosen channel was used for a receive, the r_i component is
900// set to the received value, where i is the index of that state among
901// all n receive states; otherwise r_i has the zero value of type T_i.
Rob Pikea8c8f482014-05-19 09:48:30 -0700902// Note that the receive index i is not the same as the state
Alan Donovan8097dad2013-06-24 14:15:13 -0400903// index index.
Rob Pike83f21b92013-05-17 13:25:48 -0700904//
Alan Donovan8097dad2013-06-24 14:15:13 -0400905// The second component of the triple, recvOk, is a boolean whose value
Rob Pike83f21b92013-05-17 13:25:48 -0700906// is true iff the selected operation was a receive and the receive
907// successfully yielded a value.
908//
Rob Pike87334f42013-05-17 14:02:47 -0700909// Pos() returns the ast.SelectStmt.Select.
910//
Rob Pike83f21b92013-05-17 13:25:48 -0700911// Example printed form:
Alan Donovand6eb8982014-01-07 13:31:05 -0500912// t3 = select nonblocking [<-t0, t1<-t2]
Rob Pike83f21b92013-05-17 13:25:48 -0700913// t4 = select blocking []
914//
915type Select struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400916 register
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400917 States []*SelectState
Rob Pike83f21b92013-05-17 13:25:48 -0700918 Blocking bool
919}
920
Rob Pike87334f42013-05-17 14:02:47 -0700921// The Range instruction yields an iterator over the domain and range
922// of X, which must be a string or map.
Rob Pike83f21b92013-05-17 13:25:48 -0700923//
924// Elements are accessed via Next.
925//
Alan Donovan8097dad2013-06-24 14:15:13 -0400926// Type() returns an opaque and degenerate "rangeIter" type.
Rob Pike83f21b92013-05-17 13:25:48 -0700927//
Rob Pike87334f42013-05-17 14:02:47 -0700928// Pos() returns the ast.RangeStmt.For.
929//
Rob Pike83f21b92013-05-17 13:25:48 -0700930// Example printed form:
931// t0 = range "hello":string
932//
933type Range struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400934 register
Rob Pike83f21b92013-05-17 13:25:48 -0700935 X Value // string or map
936}
937
Rob Pike87334f42013-05-17 14:02:47 -0700938// The Next instruction reads and advances the (map or string)
939// iterator Iter and returns a 3-tuple value (ok, k, v). If the
940// iterator is not exhausted, ok is true and k and v are the next
941// elements of the domain and range, respectively. Otherwise ok is
942// false and k and v are undefined.
Rob Pike83f21b92013-05-17 13:25:48 -0700943//
944// Components of the tuple are accessed using Extract.
945//
946// The IsString field distinguishes iterators over strings from those
947// over maps, as the Type() alone is insufficient: consider
948// map[int]rune.
949//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400950// Type() returns a *types.Tuple for the triple (ok, k, v).
951// The types of k and/or v may be types.Invalid.
Rob Pike83f21b92013-05-17 13:25:48 -0700952//
953// Example printed form:
954// t1 = next t0
955//
956type Next struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400957 register
Rob Pike83f21b92013-05-17 13:25:48 -0700958 Iter Value
959 IsString bool // true => string iterator; false => map iterator.
960}
961
Rob Pike87334f42013-05-17 14:02:47 -0700962// The TypeAssert instruction tests whether interface value X has type
963// AssertedType.
Rob Pike83f21b92013-05-17 13:25:48 -0700964//
965// If !CommaOk, on success it returns v, the result of the conversion
966// (defined below); on failure it panics.
967//
968// If CommaOk: on success it returns a pair (v, true) where v is the
969// result of the conversion; on failure it returns (z, false) where z
970// is AssertedType's zero value. The components of the pair must be
971// accessed using the Extract instruction.
972//
973// If AssertedType is a concrete type, TypeAssert checks whether the
974// dynamic type in interface X is equal to it, and if so, the result
975// of the conversion is a copy of the value in the interface.
976//
977// If AssertedType is an interface, TypeAssert checks whether the
978// dynamic type of the interface is assignable to it, and if so, the
979// result of the conversion is a copy of the interface value X.
Alan Donovanae801632013-07-26 21:49:27 -0400980// If AssertedType is a superinterface of X.Type(), the operation will
981// fail iff the operand is nil. (Contrast with ChangeInterface, which
982// performs no nil-check.)
Rob Pike83f21b92013-05-17 13:25:48 -0700983//
Alan Donovan6c7ce1c2013-05-30 09:59:17 -0400984// Type() reflects the actual type of the result, possibly a
985// 2-types.Tuple; AssertedType is the asserted type.
Rob Pike83f21b92013-05-17 13:25:48 -0700986//
Alan Donovan341a07a2013-06-13 14:43:35 -0400987// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
988// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
Alan Donovanc28bf6e2013-07-31 13:13:05 -0400989// instruction arose from an explicit e.(T) operation; or the
990// ast.CaseClause.Case if the instruction arose from a case of a
991// type-switch statement.
Alan Donovan341a07a2013-06-13 14:43:35 -0400992//
Rob Pike83f21b92013-05-17 13:25:48 -0700993// Example printed form:
994// t1 = typeassert t0.(int)
995// t3 = typeassert,ok t2.(T)
996//
997type TypeAssert struct {
Alan Donovan1ff34522013-10-14 13:48:34 -0400998 register
Rob Pike83f21b92013-05-17 13:25:48 -0700999 X Value
1000 AssertedType types.Type
1001 CommaOk bool
1002}
1003
Rob Pike87334f42013-05-17 14:02:47 -07001004// The Extract instruction yields component Index of Tuple.
Rob Pike83f21b92013-05-17 13:25:48 -07001005//
1006// This is used to access the results of instructions with multiple
1007// return values, such as Call, TypeAssert, Next, UnOp(ARROW) and
1008// IndexExpr(Map).
1009//
1010// Example printed form:
1011// t1 = extract t0 #1
1012//
1013type Extract struct {
Alan Donovan1ff34522013-10-14 13:48:34 -04001014 register
Rob Pike83f21b92013-05-17 13:25:48 -07001015 Tuple Value
1016 Index int
1017}
1018
1019// Instructions executed for effect. They do not yield a value. --------------------
1020
Rob Pike87334f42013-05-17 14:02:47 -07001021// The Jump instruction transfers control to the sole successor of its
1022// owning block.
Rob Pike83f21b92013-05-17 13:25:48 -07001023//
Rob Pike87334f42013-05-17 14:02:47 -07001024// A Jump must be the last instruction of its containing BasicBlock.
1025//
1026// Pos() returns NoPos.
Rob Pike83f21b92013-05-17 13:25:48 -07001027//
1028// Example printed form:
1029// jump done
1030//
1031type Jump struct {
1032 anInstruction
1033}
1034
1035// The If instruction transfers control to one of the two successors
1036// of its owning block, depending on the boolean Cond: the first if
1037// true, the second if false.
1038//
1039// An If instruction must be the last instruction of its containing
1040// BasicBlock.
1041//
Rob Pike87334f42013-05-17 14:02:47 -07001042// Pos() returns NoPos.
1043//
Rob Pike83f21b92013-05-17 13:25:48 -07001044// Example printed form:
1045// if t0 goto done else body
1046//
1047type If struct {
1048 anInstruction
1049 Cond Value
1050}
1051
Alan Donovan068f0172013-10-08 12:31:39 -04001052// The Return instruction returns values and control back to the calling
Rob Pike87334f42013-05-17 14:02:47 -07001053// function.
Rob Pike83f21b92013-05-17 13:25:48 -07001054//
1055// len(Results) is always equal to the number of results in the
1056// function's signature.
1057//
Alan Donovan068f0172013-10-08 12:31:39 -04001058// If len(Results) > 1, Return returns a tuple value with the specified
Rob Pike83f21b92013-05-17 13:25:48 -07001059// components which the caller must access using Extract instructions.
1060//
1061// There is no instruction to return a ready-made tuple like those
1062// returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or
1063// a tail-call to a function with multiple result parameters.
1064//
Alan Donovan068f0172013-10-08 12:31:39 -04001065// Return must be the last instruction of its containing BasicBlock.
Rob Pike83f21b92013-05-17 13:25:48 -07001066// Such a block has no successors.
1067//
Rob Pike87334f42013-05-17 14:02:47 -07001068// Pos() returns the ast.ReturnStmt.Return, if explicit in the source.
1069//
Rob Pike83f21b92013-05-17 13:25:48 -07001070// Example printed form:
Alan Donovan068f0172013-10-08 12:31:39 -04001071// return
1072// return nil:I, 2:int
Rob Pike83f21b92013-05-17 13:25:48 -07001073//
Alan Donovan068f0172013-10-08 12:31:39 -04001074type Return struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001075 anInstruction
1076 Results []Value
Rob Pike87334f42013-05-17 14:02:47 -07001077 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001078}
1079
Rob Pike87334f42013-05-17 14:02:47 -07001080// The RunDefers instruction pops and invokes the entire stack of
1081// procedure calls pushed by Defer instructions in this function.
Rob Pike83f21b92013-05-17 13:25:48 -07001082//
1083// It is legal to encounter multiple 'rundefers' instructions in a
1084// single control-flow path through a function; this is useful in
1085// the combined init() function, for example.
1086//
Rob Pike87334f42013-05-17 14:02:47 -07001087// Pos() returns NoPos.
1088//
Rob Pike83f21b92013-05-17 13:25:48 -07001089// Example printed form:
1090// rundefers
1091//
1092type RunDefers struct {
1093 anInstruction
1094}
1095
Rob Pike87334f42013-05-17 14:02:47 -07001096// The Panic instruction initiates a panic with value X.
Rob Pike83f21b92013-05-17 13:25:48 -07001097//
1098// A Panic instruction must be the last instruction of its containing
1099// BasicBlock, which must have no successors.
1100//
1101// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction;
1102// they are treated as calls to a built-in function.
1103//
Rob Pike87334f42013-05-17 14:02:47 -07001104// Pos() returns the ast.CallExpr.Lparen if this panic was explicit
1105// in the source.
1106//
Rob Pike83f21b92013-05-17 13:25:48 -07001107// Example printed form:
1108// panic t0
1109//
1110type Panic struct {
1111 anInstruction
Rob Pike87334f42013-05-17 14:02:47 -07001112 X Value // an interface{}
1113 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001114}
1115
Rob Pike87334f42013-05-17 14:02:47 -07001116// The Go instruction creates a new goroutine and calls the specified
1117// function within it.
Rob Pike83f21b92013-05-17 13:25:48 -07001118//
1119// See CallCommon for generic function call documentation.
1120//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001121// Pos() returns the ast.GoStmt.Go.
1122//
Rob Pike83f21b92013-05-17 13:25:48 -07001123// Example printed form:
1124// go println(t0, t1)
1125// go t3()
1126// go invoke t5.Println(...t6)
1127//
1128type Go struct {
1129 anInstruction
1130 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001131 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001132}
1133
Rob Pike87334f42013-05-17 14:02:47 -07001134// The Defer instruction pushes the specified call onto a stack of
1135// functions to be called by a RunDefers instruction or by a panic.
Rob Pike83f21b92013-05-17 13:25:48 -07001136//
1137// See CallCommon for generic function call documentation.
1138//
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001139// Pos() returns the ast.DeferStmt.Defer.
1140//
Rob Pike83f21b92013-05-17 13:25:48 -07001141// Example printed form:
1142// defer println(t0, t1)
1143// defer t3()
1144// defer invoke t5.Println(...t6)
1145//
1146type Defer struct {
1147 anInstruction
1148 Call CallCommon
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001149 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001150}
1151
Rob Pike87334f42013-05-17 14:02:47 -07001152// The Send instruction sends X on channel Chan.
1153//
1154// Pos() returns the ast.SendStmt.Arrow, if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001155//
1156// Example printed form:
1157// send t0 <- t1
1158//
1159type Send struct {
1160 anInstruction
1161 Chan, X Value
Rob Pike87334f42013-05-17 14:02:47 -07001162 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001163}
1164
Rob Pike87334f42013-05-17 14:02:47 -07001165// The Store instruction stores Val at address Addr.
Rob Pike83f21b92013-05-17 13:25:48 -07001166// Stores can be of arbitrary types.
1167//
Alan Donovan538acf12014-12-29 16:49:20 -05001168// Pos() returns the position of the source-level construct most closely
1169// associated with the memory store operation.
1170// Since implicit memory stores are numerous and varied and depend upon
1171// implementation choices, the details are not specified.
Rob Pike87334f42013-05-17 14:02:47 -07001172//
Rob Pike83f21b92013-05-17 13:25:48 -07001173// Example printed form:
1174// *x = y
1175//
1176type Store struct {
1177 anInstruction
1178 Addr Value
1179 Val Value
Rob Pike87334f42013-05-17 14:02:47 -07001180 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001181}
1182
Rob Pike87334f42013-05-17 14:02:47 -07001183// The MapUpdate instruction updates the association of Map[Key] to
1184// Value.
1185//
Alan Donovanc8a68902013-08-22 10:13:51 -04001186// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
1187// if explicit in the source.
Rob Pike83f21b92013-05-17 13:25:48 -07001188//
1189// Example printed form:
1190// t0[t1] = t2
1191//
1192type MapUpdate struct {
1193 anInstruction
1194 Map Value
1195 Key Value
1196 Value Value
Rob Pike87334f42013-05-17 14:02:47 -07001197 pos token.Pos
Rob Pike83f21b92013-05-17 13:25:48 -07001198}
1199
Alan Donovane2962652013-10-28 12:05:29 -04001200// A DebugRef instruction maps a source-level expression Expr to the
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001201// SSA value X that represents the value (!IsAddr) or address (IsAddr)
Alan Donovane2962652013-10-28 12:05:29 -04001202// of that expression.
Alan Donovan55d678e2013-07-15 13:56:46 -04001203//
1204// DebugRef is a pseudo-instruction: it has no dynamic effect.
1205//
Alan Donovane590cdb2013-10-09 12:47:30 -04001206// Pos() returns Expr.Pos(), the start position of the source-level
1207// expression. This is not the same as the "designated" token as
1208// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the
1209// position of the ("designated") Lparen token.
Alan Donovan55d678e2013-07-15 13:56:46 -04001210//
Alan Donovane2962652013-10-28 12:05:29 -04001211// If Expr is an *ast.Ident denoting a var or func, Object() returns
1212// the object; though this information can be obtained from the type
1213// checker, including it here greatly facilitates debugging.
1214// For non-Ident expressions, Object() returns nil.
1215//
1216// DebugRefs are generated only for functions built with debugging
Alan Donovane8afbfa2014-01-15 21:37:55 -05001217// enabled; see Package.SetDebugMode() and the GlobalDebug builder
1218// mode flag.
Alan Donovane2962652013-10-28 12:05:29 -04001219//
1220// DebugRefs are not emitted for ast.Idents referring to constants or
1221// predeclared identifiers, since they are trivial and numerous.
1222// Nor are they emitted for ast.ParenExprs.
Alan Donovan55d678e2013-07-15 13:56:46 -04001223//
1224// (By representing these as instructions, rather than out-of-band,
1225// consistency is maintained during transformation passes by the
1226// ordinary SSA renaming machinery.)
1227//
Alan Donovane2962652013-10-28 12:05:29 -04001228// Example printed form:
1229// ; *ast.CallExpr @ 102:9 is t5
1230// ; var x float64 @ 109:72 is x
1231// ; address of *ast.CompositeLit @ 216:10 is t0
Alan Donovanaa238622013-10-27 10:55:21 -04001232//
Alan Donovan55d678e2013-07-15 13:56:46 -04001233type DebugRef struct {
1234 anInstruction
Alan Donovan9f640c22013-10-24 18:31:50 -04001235 Expr ast.Expr // the referring expression (never *ast.ParenExpr)
Alan Donovane2962652013-10-28 12:05:29 -04001236 object types.Object // the identity of the source var/func
Alan Donovan9f640c22013-10-24 18:31:50 -04001237 IsAddr bool // Expr is addressable and X is the address it denotes
Alan Donovane2962652013-10-28 12:05:29 -04001238 X Value // the value or address of Expr
Alan Donovan55d678e2013-07-15 13:56:46 -04001239}
1240
Rob Pike83f21b92013-05-17 13:25:48 -07001241// Embeddable mix-ins and helpers for common parts of other structs. -----------
1242
Alan Donovan1ff34522013-10-14 13:48:34 -04001243// register is a mix-in embedded by all SSA values that are also
1244// instructions, i.e. virtual registers, and provides a uniform
1245// implementation of most of the Value interface: Value.Name() is a
1246// numbered register (e.g. "t0"); the other methods are field accessors.
Rob Pike83f21b92013-05-17 13:25:48 -07001247//
Alan Donovan1ff34522013-10-14 13:48:34 -04001248// Temporary names are automatically assigned to each register on
Rob Pike83f21b92013-05-17 13:25:48 -07001249// completion of building a function in SSA form.
1250//
1251// Clients must not assume that the 'id' value (and the Name() derived
1252// from it) is unique within a function. As always in this API,
1253// semantics are determined only by identity; names exist only to
1254// facilitate debugging.
1255//
Alan Donovan1ff34522013-10-14 13:48:34 -04001256type register struct {
Rob Pike83f21b92013-05-17 13:25:48 -07001257 anInstruction
1258 num int // "name" of virtual register, e.g. "t0". Not guaranteed unique.
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001259 typ types.Type // type of virtual register
Rob Pike87334f42013-05-17 14:02:47 -07001260 pos token.Pos // position of source expression, or NoPos
Rob Pike83f21b92013-05-17 13:25:48 -07001261 referrers []Instruction
1262}
1263
1264// anInstruction is a mix-in embedded by all Instructions.
Alan Donovanf1e5b032013-11-07 10:08:51 -05001265// It provides the implementations of the Block and setBlock methods.
Rob Pike83f21b92013-05-17 13:25:48 -07001266type anInstruction struct {
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001267 block *BasicBlock // the basic block of this instruction
Rob Pike83f21b92013-05-17 13:25:48 -07001268}
1269
1270// CallCommon is contained by Go, Defer and Call to hold the
1271// common parts of a function or method call.
1272//
1273// Each CallCommon exists in one of two modes, function call and
1274// interface method invocation, or "call" and "invoke" for short.
1275//
Alan Donovan4da31df2013-07-26 11:22:34 -04001276// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon
Alan Donovan70722532013-08-19 15:38:30 -04001277// represents an ordinary function call of the value in Value,
1278// which may be a *Builtin, a *Function or any other value of kind
1279// 'func'.
Rob Pike83f21b92013-05-17 13:25:48 -07001280//
Alan Donovand6eb8982014-01-07 13:31:05 -05001281// Value may be one of:
1282// (a) a *Function, indicating a statically dispatched call
1283// to a package-level function, an anonymous function, or
1284// a method of a named type.
1285// (b) a *MakeClosure, indicating an immediately applied
1286// function literal with free variables.
1287// (c) a *Builtin, indicating a statically dispatched call
Alan Donovane8afbfa2014-01-15 21:37:55 -05001288// to a built-in function.
Alan Donovand6eb8982014-01-07 13:31:05 -05001289// (d) any other value, indicating a dynamically dispatched
1290// function call.
1291// StaticCallee returns the identity of the callee in cases
1292// (a) and (b), nil otherwise.
Rob Pike83f21b92013-05-17 13:25:48 -07001293//
Alan Donovan118786e2013-07-26 14:06:26 -04001294// Args contains the arguments to the call. If Value is a method,
1295// Args[0] contains the receiver parameter.
Rob Pike83f21b92013-05-17 13:25:48 -07001296//
1297// Example printed form:
1298// t2 = println(t0, t1)
1299// go t3()
1300// defer t5(...t6)
1301//
Alan Donovan4da31df2013-07-26 11:22:34 -04001302// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon
Rob Pike83f21b92013-05-17 13:25:48 -07001303// represents a dynamically dispatched call to an interface method.
Alan Donovan118786e2013-07-26 14:06:26 -04001304// In this mode, Value is the interface value and Method is the
Alan Donovan70722532013-08-19 15:38:30 -04001305// interface's abstract method. Note: an abstract method may be
1306// shared by multiple interfaces due to embedding; Value.Type()
1307// provides the specific interface used for this call.
Rob Pike83f21b92013-05-17 13:25:48 -07001308//
Alan Donovan118786e2013-07-26 14:06:26 -04001309// Value is implicitly supplied to the concrete method implementation
Rob Pike83f21b92013-05-17 13:25:48 -07001310// as the receiver parameter; in other words, Args[0] holds not the
Alan Donovan118786e2013-07-26 14:06:26 -04001311// receiver but the first true argument.
Rob Pike83f21b92013-05-17 13:25:48 -07001312//
Rob Pike83f21b92013-05-17 13:25:48 -07001313// Example printed form:
1314// t1 = invoke t0.String()
1315// go invoke t3.Run(t2)
1316// defer invoke t4.Handle(...t5)
1317//
Robert Griesemerebfa4ef2014-01-28 10:57:56 -08001318// For all calls to variadic functions (Signature().Variadic()),
Alan Donovand6eb8982014-01-07 13:31:05 -05001319// the last element of Args is a slice.
Rob Pike83f21b92013-05-17 13:25:48 -07001320//
1321type CallCommon struct {
Alan Donovand6eb8982014-01-07 13:31:05 -05001322 Value Value // receiver (invoke mode) or func value (call mode)
1323 Method *types.Func // abstract method (invoke mode)
1324 Args []Value // actual parameters (in static method call, includes receiver)
1325 pos token.Pos // position of CallExpr.Lparen, iff explicit in source
Rob Pike83f21b92013-05-17 13:25:48 -07001326}
1327
1328// IsInvoke returns true if this call has "invoke" (not "call") mode.
1329func (c *CallCommon) IsInvoke() bool {
Alan Donovan4da31df2013-07-26 11:22:34 -04001330 return c.Method != nil
Rob Pike83f21b92013-05-17 13:25:48 -07001331}
1332
Rob Pike87334f42013-05-17 14:02:47 -07001333func (c *CallCommon) Pos() token.Pos { return c.pos }
1334
Alan Donovan8097dad2013-06-24 14:15:13 -04001335// Signature returns the signature of the called function.
1336//
1337// For an "invoke"-mode call, the signature of the interface method is
Alan Donovanb68a0292013-06-26 12:38:08 -04001338// returned.
1339//
1340// In either "call" or "invoke" mode, if the callee is a method, its
1341// receiver is represented by sig.Recv, not sig.Params().At(0).
Alan Donovan8097dad2013-06-24 14:15:13 -04001342//
1343func (c *CallCommon) Signature() *types.Signature {
Alan Donovan4da31df2013-07-26 11:22:34 -04001344 if c.Method != nil {
1345 return c.Method.Type().(*types.Signature)
Alan Donovan8097dad2013-06-24 14:15:13 -04001346 }
Alan Donovand6eb8982014-01-07 13:31:05 -05001347 return c.Value.Type().Underlying().(*types.Signature)
Alan Donovan8097dad2013-06-24 14:15:13 -04001348}
1349
Alan Donovand6eb8982014-01-07 13:31:05 -05001350// StaticCallee returns the callee if this is a trivially static
1351// "call"-mode call to a function.
Rob Pike83f21b92013-05-17 13:25:48 -07001352func (c *CallCommon) StaticCallee() *Function {
Alan Donovan118786e2013-07-26 14:06:26 -04001353 switch fn := c.Value.(type) {
Rob Pike83f21b92013-05-17 13:25:48 -07001354 case *Function:
1355 return fn
1356 case *MakeClosure:
1357 return fn.Fn.(*Function)
1358 }
1359 return nil
1360}
1361
Rob Pike83f21b92013-05-17 13:25:48 -07001362// Description returns a description of the mode of this call suitable
Alan Donovanf9642692015-02-20 10:42:06 -05001363// for a user interface, e.g., "static method call".
Rob Pike83f21b92013-05-17 13:25:48 -07001364func (c *CallCommon) Description() string {
Alan Donovan118786e2013-07-26 14:06:26 -04001365 switch fn := c.Value.(type) {
Alan Donovan70722532013-08-19 15:38:30 -04001366 case *Builtin:
1367 return "built-in function call"
Rob Pike83f21b92013-05-17 13:25:48 -07001368 case *MakeClosure:
1369 return "static function closure call"
1370 case *Function:
Rob Pike87334f42013-05-17 14:02:47 -07001371 if fn.Signature.Recv() != nil {
Rob Pike83f21b92013-05-17 13:25:48 -07001372 return "static method call"
1373 }
1374 return "static function call"
1375 }
Alan Donovan70722532013-08-19 15:38:30 -04001376 if c.IsInvoke() {
1377 return "dynamic method call" // ("invoke" mode)
1378 }
Rob Pike83f21b92013-05-17 13:25:48 -07001379 return "dynamic function call"
1380}
1381
Alan Donovan8097dad2013-06-24 14:15:13 -04001382// The CallInstruction interface, implemented by *Go, *Defer and *Call,
Alan Donovan149e0302014-08-01 14:44:37 -04001383// exposes the common parts of function-calling instructions,
Alan Donovan8097dad2013-06-24 14:15:13 -04001384// yet provides a way back to the Value defined by *Call alone.
1385//
1386type CallInstruction interface {
1387 Instruction
1388 Common() *CallCommon // returns the common parts of the call
1389 Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer)
1390}
1391
1392func (s *Call) Common() *CallCommon { return &s.Call }
1393func (s *Defer) Common() *CallCommon { return &s.Call }
1394func (s *Go) Common() *CallCommon { return &s.Call }
1395
1396func (s *Call) Value() *Call { return s }
1397func (s *Defer) Value() *Call { return nil }
1398func (s *Go) Value() *Call { return nil }
1399
Alan Donovand6eb8982014-01-07 13:31:05 -05001400func (v *Builtin) Type() types.Type { return v.sig }
Alan Donovande23e2b2014-06-13 17:34:07 -04001401func (v *Builtin) Name() string { return v.name }
Rob Pike83f21b92013-05-17 13:25:48 -07001402func (*Builtin) Referrers() *[]Instruction { return nil }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001403func (v *Builtin) Pos() token.Pos { return token.NoPos }
Alan Donovande23e2b2014-06-13 17:34:07 -04001404func (v *Builtin) Object() types.Object { return types.Universe.Lookup(v.name) }
Alan Donovan04427c82014-06-11 13:14:06 -04001405func (v *Builtin) Parent() *Function { return nil }
Rob Pike83f21b92013-05-17 13:25:48 -07001406
Alan Donovancc02c5b2014-06-11 14:04:45 -04001407func (v *FreeVar) Type() types.Type { return v.typ }
1408func (v *FreeVar) Name() string { return v.name }
1409func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers }
1410func (v *FreeVar) Pos() token.Pos { return v.pos }
1411func (v *FreeVar) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001412
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001413func (v *Global) Type() types.Type { return v.typ }
1414func (v *Global) Name() string { return v.name }
Alan Donovan04427c82014-06-11 13:14:06 -04001415func (v *Global) Parent() *Function { return nil }
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001416func (v *Global) Pos() token.Pos { return v.pos }
1417func (v *Global) Referrers() *[]Instruction { return nil }
1418func (v *Global) Token() token.Token { return token.VAR }
1419func (v *Global) Object() types.Object { return v.object }
1420func (v *Global) String() string { return v.RelString(nil) }
1421func (v *Global) Package() *Package { return v.Pkg }
1422func (v *Global) RelString(from *types.Package) string { return relString(v, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001423
Alan Donovanf1e5b032013-11-07 10:08:51 -05001424func (v *Function) Name() string { return v.name }
1425func (v *Function) Type() types.Type { return v.Signature }
1426func (v *Function) Pos() token.Pos { return v.pos }
1427func (v *Function) Token() token.Token { return token.FUNC }
1428func (v *Function) Object() types.Object { return v.object }
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001429func (v *Function) String() string { return v.RelString(nil) }
1430func (v *Function) Package() *Package { return v.Pkg }
Alan Donovan04427c82014-06-11 13:14:06 -04001431func (v *Function) Parent() *Function { return v.parent }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001432func (v *Function) Referrers() *[]Instruction {
Alan Donovan04427c82014-06-11 13:14:06 -04001433 if v.parent != nil {
Alan Donovanf1e5b032013-11-07 10:08:51 -05001434 return &v.referrers
1435 }
1436 return nil
1437}
Rob Pike83f21b92013-05-17 13:25:48 -07001438
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001439func (v *Parameter) Type() types.Type { return v.typ }
1440func (v *Parameter) Name() string { return v.name }
Alan Donovan55d678e2013-07-15 13:56:46 -04001441func (v *Parameter) Object() types.Object { return v.object }
Rob Pike83f21b92013-05-17 13:25:48 -07001442func (v *Parameter) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001443func (v *Parameter) Pos() token.Pos { return v.pos }
Alan Donovan341a07a2013-06-13 14:43:35 -04001444func (v *Parameter) Parent() *Function { return v.parent }
Rob Pike83f21b92013-05-17 13:25:48 -07001445
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001446func (v *Alloc) Type() types.Type { return v.typ }
Rob Pike83f21b92013-05-17 13:25:48 -07001447func (v *Alloc) Referrers() *[]Instruction { return &v.referrers }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001448func (v *Alloc) Pos() token.Pos { return v.pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001449
Alan Donovan1ff34522013-10-14 13:48:34 -04001450func (v *register) Type() types.Type { return v.typ }
1451func (v *register) setType(typ types.Type) { v.typ = typ }
1452func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) }
1453func (v *register) setNum(num int) { v.num = num }
1454func (v *register) Referrers() *[]Instruction { return &v.referrers }
1455func (v *register) Pos() token.Pos { return v.pos }
1456func (v *register) setPos(pos token.Pos) { v.pos = pos }
Rob Pike83f21b92013-05-17 13:25:48 -07001457
Alan Donovan341a07a2013-06-13 14:43:35 -04001458func (v *anInstruction) Parent() *Function { return v.block.parent }
Alan Donovan6c7ce1c2013-05-30 09:59:17 -04001459func (v *anInstruction) Block() *BasicBlock { return v.block }
Alan Donovanf1e5b032013-11-07 10:08:51 -05001460func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block }
Alan Donovan04427c82014-06-11 13:14:06 -04001461func (v *anInstruction) Referrers() *[]Instruction { return nil }
Rob Pike83f21b92013-05-17 13:25:48 -07001462
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001463func (t *Type) Name() string { return t.object.Name() }
1464func (t *Type) Pos() token.Pos { return t.object.Pos() }
1465func (t *Type) Type() types.Type { return t.object.Type() }
1466func (t *Type) Token() token.Token { return token.TYPE }
1467func (t *Type) Object() types.Object { return t.object }
1468func (t *Type) String() string { return t.RelString(nil) }
1469func (t *Type) Package() *Package { return t.pkg }
1470func (t *Type) RelString(from *types.Package) string { return relString(t, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001471
Alan Donovan9fcd20e2013-11-15 09:21:48 -05001472func (c *NamedConst) Name() string { return c.object.Name() }
1473func (c *NamedConst) Pos() token.Pos { return c.object.Pos() }
1474func (c *NamedConst) String() string { return c.RelString(nil) }
1475func (c *NamedConst) Type() types.Type { return c.object.Type() }
1476func (c *NamedConst) Token() token.Token { return token.CONST }
1477func (c *NamedConst) Object() types.Object { return c.object }
1478func (c *NamedConst) Package() *Package { return c.pkg }
1479func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) }
Rob Pike83f21b92013-05-17 13:25:48 -07001480
1481// Func returns the package-level function of the specified name,
1482// or nil if not found.
1483//
1484func (p *Package) Func(name string) (f *Function) {
1485 f, _ = p.Members[name].(*Function)
1486 return
1487}
1488
1489// Var returns the package-level variable of the specified name,
1490// or nil if not found.
1491//
1492func (p *Package) Var(name string) (g *Global) {
1493 g, _ = p.Members[name].(*Global)
1494 return
1495}
1496
1497// Const returns the package-level constant of the specified name,
1498// or nil if not found.
1499//
Alan Donovan732dbe92013-07-16 13:50:08 -04001500func (p *Package) Const(name string) (c *NamedConst) {
1501 c, _ = p.Members[name].(*NamedConst)
Rob Pike83f21b92013-05-17 13:25:48 -07001502 return
1503}
1504
1505// Type returns the package-level type of the specified name,
1506// or nil if not found.
1507//
1508func (p *Package) Type(name string) (t *Type) {
1509 t, _ = p.Members[name].(*Type)
1510 return
1511}
1512
Rob Pike87334f42013-05-17 14:02:47 -07001513func (v *Call) Pos() token.Pos { return v.Call.pos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001514func (s *Defer) Pos() token.Pos { return s.pos }
1515func (s *Go) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001516func (s *MapUpdate) Pos() token.Pos { return s.pos }
1517func (s *Panic) Pos() token.Pos { return s.pos }
Alan Donovan068f0172013-10-08 12:31:39 -04001518func (s *Return) Pos() token.Pos { return s.pos }
Rob Pike87334f42013-05-17 14:02:47 -07001519func (s *Send) Pos() token.Pos { return s.pos }
1520func (s *Store) Pos() token.Pos { return s.pos }
1521func (s *If) Pos() token.Pos { return token.NoPos }
1522func (s *Jump) Pos() token.Pos { return token.NoPos }
1523func (s *RunDefers) Pos() token.Pos { return token.NoPos }
Alan Donovanc28bf6e2013-07-31 13:13:05 -04001524func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() }
Rob Pike83f21b92013-05-17 13:25:48 -07001525
Rob Pike87334f42013-05-17 14:02:47 -07001526// Operands.
Rob Pike83f21b92013-05-17 13:25:48 -07001527
1528func (v *Alloc) Operands(rands []*Value) []*Value {
1529 return rands
1530}
1531
1532func (v *BinOp) Operands(rands []*Value) []*Value {
1533 return append(rands, &v.X, &v.Y)
1534}
1535
1536func (c *CallCommon) Operands(rands []*Value) []*Value {
Alan Donovan118786e2013-07-26 14:06:26 -04001537 rands = append(rands, &c.Value)
Rob Pike83f21b92013-05-17 13:25:48 -07001538 for i := range c.Args {
1539 rands = append(rands, &c.Args[i])
1540 }
1541 return rands
1542}
1543
1544func (s *Go) Operands(rands []*Value) []*Value {
1545 return s.Call.Operands(rands)
1546}
1547
1548func (s *Call) Operands(rands []*Value) []*Value {
1549 return s.Call.Operands(rands)
1550}
1551
1552func (s *Defer) Operands(rands []*Value) []*Value {
1553 return s.Call.Operands(rands)
1554}
1555
1556func (v *ChangeInterface) Operands(rands []*Value) []*Value {
1557 return append(rands, &v.X)
1558}
1559
Rob Pike87334f42013-05-17 14:02:47 -07001560func (v *ChangeType) Operands(rands []*Value) []*Value {
1561 return append(rands, &v.X)
1562}
1563
1564func (v *Convert) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001565 return append(rands, &v.X)
1566}
1567
Alan Donovan55d678e2013-07-15 13:56:46 -04001568func (s *DebugRef) Operands(rands []*Value) []*Value {
1569 return append(rands, &s.X)
1570}
1571
Rob Pike83f21b92013-05-17 13:25:48 -07001572func (v *Extract) Operands(rands []*Value) []*Value {
1573 return append(rands, &v.Tuple)
1574}
1575
1576func (v *Field) Operands(rands []*Value) []*Value {
1577 return append(rands, &v.X)
1578}
1579
1580func (v *FieldAddr) Operands(rands []*Value) []*Value {
1581 return append(rands, &v.X)
1582}
1583
1584func (s *If) Operands(rands []*Value) []*Value {
1585 return append(rands, &s.Cond)
1586}
1587
1588func (v *Index) Operands(rands []*Value) []*Value {
1589 return append(rands, &v.X, &v.Index)
1590}
1591
1592func (v *IndexAddr) Operands(rands []*Value) []*Value {
1593 return append(rands, &v.X, &v.Index)
1594}
1595
1596func (*Jump) Operands(rands []*Value) []*Value {
1597 return rands
1598}
1599
1600func (v *Lookup) Operands(rands []*Value) []*Value {
1601 return append(rands, &v.X, &v.Index)
1602}
1603
1604func (v *MakeChan) Operands(rands []*Value) []*Value {
1605 return append(rands, &v.Size)
1606}
1607
1608func (v *MakeClosure) Operands(rands []*Value) []*Value {
1609 rands = append(rands, &v.Fn)
1610 for i := range v.Bindings {
1611 rands = append(rands, &v.Bindings[i])
1612 }
1613 return rands
1614}
1615
1616func (v *MakeInterface) Operands(rands []*Value) []*Value {
1617 return append(rands, &v.X)
1618}
1619
1620func (v *MakeMap) Operands(rands []*Value) []*Value {
1621 return append(rands, &v.Reserve)
1622}
1623
1624func (v *MakeSlice) Operands(rands []*Value) []*Value {
1625 return append(rands, &v.Len, &v.Cap)
1626}
1627
1628func (v *MapUpdate) Operands(rands []*Value) []*Value {
1629 return append(rands, &v.Map, &v.Key, &v.Value)
1630}
1631
1632func (v *Next) Operands(rands []*Value) []*Value {
1633 return append(rands, &v.Iter)
1634}
1635
1636func (s *Panic) Operands(rands []*Value) []*Value {
1637 return append(rands, &s.X)
1638}
1639
1640func (v *Phi) Operands(rands []*Value) []*Value {
1641 for i := range v.Edges {
1642 rands = append(rands, &v.Edges[i])
1643 }
1644 return rands
1645}
1646
1647func (v *Range) Operands(rands []*Value) []*Value {
1648 return append(rands, &v.X)
1649}
1650
Alan Donovan068f0172013-10-08 12:31:39 -04001651func (s *Return) Operands(rands []*Value) []*Value {
Rob Pike83f21b92013-05-17 13:25:48 -07001652 for i := range s.Results {
1653 rands = append(rands, &s.Results[i])
1654 }
1655 return rands
1656}
1657
1658func (*RunDefers) Operands(rands []*Value) []*Value {
1659 return rands
1660}
1661
1662func (v *Select) Operands(rands []*Value) []*Value {
1663 for i := range v.States {
1664 rands = append(rands, &v.States[i].Chan, &v.States[i].Send)
1665 }
1666 return rands
1667}
1668
1669func (s *Send) Operands(rands []*Value) []*Value {
1670 return append(rands, &s.Chan, &s.X)
1671}
1672
1673func (v *Slice) Operands(rands []*Value) []*Value {
Peter Collingbournecd36f522014-06-11 16:16:19 -04001674 return append(rands, &v.X, &v.Low, &v.High, &v.Max)
Rob Pike83f21b92013-05-17 13:25:48 -07001675}
1676
1677func (s *Store) Operands(rands []*Value) []*Value {
1678 return append(rands, &s.Addr, &s.Val)
1679}
1680
1681func (v *TypeAssert) Operands(rands []*Value) []*Value {
1682 return append(rands, &v.X)
1683}
1684
1685func (v *UnOp) Operands(rands []*Value) []*Value {
1686 return append(rands, &v.X)
1687}
Alan Donovan04427c82014-06-11 13:14:06 -04001688
1689// Non-Instruction Values:
1690func (v *Builtin) Operands(rands []*Value) []*Value { return rands }
Alan Donovancc02c5b2014-06-11 14:04:45 -04001691func (v *FreeVar) Operands(rands []*Value) []*Value { return rands }
Alan Donovan04427c82014-06-11 13:14:06 -04001692func (v *Const) Operands(rands []*Value) []*Value { return rands }
1693func (v *Function) Operands(rands []*Value) []*Value { return rands }
1694func (v *Global) Operands(rands []*Value) []*Value { return rands }
1695func (v *Parameter) Operands(rands []*Value) []*Value { return rands }