Alan Donovan | 713699d | 2013-08-27 18:49:13 -0400 | [diff] [blame] | 1 | // 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 Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 5 | package ssa |
| 6 | |
| 7 | // This package defines a high-level intermediate representation for |
| 8 | // Go programs using static single-assignment (SSA) form. |
| 9 | |
| 10 | import ( |
| 11 | "fmt" |
| 12 | "go/ast" |
| 13 | "go/token" |
| 14 | "sync" |
| 15 | |
| 16 | "code.google.com/p/go.tools/go/exact" |
| 17 | "code.google.com/p/go.tools/go/types" |
Alan Donovan | 4df7477 | 2013-07-10 18:01:11 -0400 | [diff] [blame] | 18 | "code.google.com/p/go.tools/go/types/typemap" |
Alan Donovan | be28dbb | 2013-05-31 16:14:13 -0400 | [diff] [blame] | 19 | "code.google.com/p/go.tools/importer" |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // A Program is a partial or complete Go program converted to SSA form. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 23 | // |
| 24 | type Program struct { |
Alan Donovan | 3f2f9a7 | 2013-09-06 18:13:57 -0400 | [diff] [blame] | 25 | Fset *token.FileSet // position information for the files of this Program |
| 26 | imported map[string]*Package // all importable Packages, keyed by import path |
| 27 | packages map[*types.Package]*Package // all loaded Packages, keyed by object |
Alan Donovan | 318b83e | 2013-09-23 18:18:35 -0400 | [diff] [blame] | 28 | builtins map[*types.Builtin]*Builtin // all built-in functions, keyed by typechecker objects. |
Alan Donovan | 3f2f9a7 | 2013-09-06 18:13:57 -0400 | [diff] [blame] | 29 | mode BuilderMode // set of mode bits for SSA construction |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 30 | |
Alan Donovan | f1d4d01 | 2013-06-14 15:50:37 -0400 | [diff] [blame] | 31 | methodsMu sync.Mutex // guards the following maps: |
Alan Donovan | 2a3a129 | 2013-07-30 14:28:14 -0400 | [diff] [blame] | 32 | methodSets typemap.M // maps type to its concrete methodSet |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 33 | boundMethodWrappers map[*types.Func]*Function // wrappers for curried x.Method closures |
Alan Donovan | f1d4d01 | 2013-06-14 15:50:37 -0400 | [diff] [blame] | 34 | ifaceMethodWrappers map[*types.Func]*Function // wrappers for curried I.Method functions |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 35 | } |
| 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 | // |
| 42 | type Package struct { |
Alan Donovan | 87ced82 | 2013-10-23 17:07:52 -0400 | [diff] [blame] | 43 | Prog *Program // the owning program |
| 44 | Object *types.Package // the type checker's package object for this package |
| 45 | Members map[string]Member // all package members keyed by name |
| 46 | methodSets []types.Type // types whose method sets are included in this package |
| 47 | values map[types.Object]Value // package members (incl. types and methods), keyed by object |
| 48 | init *Function // Func("init"); the package's init function |
| 49 | debug bool // include full debug info in this package. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 50 | |
Alan Donovan | 4d628a0 | 2013-06-03 14:15:19 -0400 | [diff] [blame] | 51 | // The following fields are set transiently, then cleared |
| 52 | // after building. |
Alan Donovan | 87ced82 | 2013-10-23 17:07:52 -0400 | [diff] [blame] | 53 | started int32 // atomically tested and set at start of build phase |
| 54 | ninit int32 // number of init functions |
| 55 | info *importer.PackageInfo // package ASTs and type information |
| 56 | needRTTI typemap.M // types for which runtime type info is needed |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 59 | // A Member is a member of a Go package, implemented by *NamedConst, |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 60 | // *Global, *Function, or *Type; they are created by package-level |
| 61 | // const, var, func and type declarations respectively. |
| 62 | // |
| 63 | type Member interface { |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 64 | Name() string // declared name of the package member |
| 65 | String() string // package-qualified name of the package member |
| 66 | RelString(*types.Package) string // like String, but relative refs are unqualified |
| 67 | Object() types.Object // typechecker's object for this member, if any |
| 68 | Pos() token.Pos // position of member's declaration, if known |
| 69 | Type() types.Type // type of the package member |
| 70 | Token() token.Token // token.{VAR,FUNC,CONST,TYPE} |
| 71 | Package() *Package // returns the containing package. (TODO: rename Pkg) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 74 | // A Type is a Member of a Package representing a package-level named type. |
| 75 | // |
| 76 | // Type() returns a *types.Named. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 77 | // |
| 78 | type Type struct { |
Alan Donovan | bc1f724 | 2013-07-11 14:12:30 -0400 | [diff] [blame] | 79 | object *types.TypeName |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 80 | pkg *Package |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 83 | // A NamedConst is a Member of Package representing a package-level |
| 84 | // named constant value. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 85 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 86 | // Pos() returns the position of the declaring ast.ValueSpec.Names[*] |
| 87 | // identifier. |
| 88 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 89 | // NB: a NamedConst is not a Value; it contains a constant Value, which |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 90 | // it augments with the name and position of its 'const' declaration. |
| 91 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 92 | type NamedConst struct { |
Alan Donovan | bc1f724 | 2013-07-11 14:12:30 -0400 | [diff] [blame] | 93 | object *types.Const |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 94 | Value *Const |
Alan Donovan | bc1f724 | 2013-07-11 14:12:30 -0400 | [diff] [blame] | 95 | pos token.Pos |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 96 | pkg *Package |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // An SSA value that can be referenced by an instruction. |
| 100 | type Value interface { |
| 101 | // Name returns the name of this value, and determines how |
| 102 | // this Value appears when used as an operand of an |
| 103 | // Instruction. |
| 104 | // |
| 105 | // This is the same as the source name for Parameters, |
Alan Donovan | bac7098 | 2013-10-29 11:07:09 -0400 | [diff] [blame] | 106 | // Builtins, Functions, Captures, Globals. |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 107 | // For constants, it is a representation of the constant's value |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 108 | // and type. For all other Values this is the name of the |
| 109 | // virtual register defined by the instruction. |
| 110 | // |
| 111 | // The name of an SSA Value is not semantically significant, |
| 112 | // and may not even be unique within a function. |
| 113 | Name() string |
| 114 | |
| 115 | // If this value is an Instruction, String returns its |
| 116 | // disassembled form; otherwise it returns unspecified |
| 117 | // human-readable information about the Value, such as its |
| 118 | // kind, name and type. |
| 119 | String() string |
| 120 | |
| 121 | // Type returns the type of this value. Many instructions |
Alan Donovan | bac7098 | 2013-10-29 11:07:09 -0400 | [diff] [blame] | 122 | // (e.g. IndexAddr) change their behaviour depending on the |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 123 | // types of their operands. |
| 124 | Type() types.Type |
| 125 | |
| 126 | // Referrers returns the list of instructions that have this |
| 127 | // value as one of their operands; it may contain duplicates |
| 128 | // if an instruction has a repeated operand. |
| 129 | // |
| 130 | // Referrers actually returns a pointer through which the |
| 131 | // caller may perform mutations to the object's state. |
| 132 | // |
| 133 | // Referrers is currently only defined for the function-local |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 134 | // values Capture, Parameter, Functions (iff anonymous) and |
| 135 | // all value-defining instructions. |
| 136 | // It returns nil for named Functions, Builtin, Const and Global. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 137 | // |
| 138 | // Instruction.Operands contains the inverse of this relation. |
| 139 | Referrers() *[]Instruction |
| 140 | |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 141 | // Pos returns the location of the AST token most closely |
| 142 | // associated with the operation that gave rise to this value, |
| 143 | // or token.NoPos if it was not explicit in the source. |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 144 | // |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 145 | // For each ast.Node type, a particular token is designated as |
| 146 | // the closest location for the expression, e.g. the Lparen |
| 147 | // for an *ast.CallExpr. This permits a compact but |
| 148 | // approximate mapping from Values to source positions for use |
| 149 | // in diagnostic messages, for example. |
| 150 | // |
| 151 | // (Do not use this position to determine which Value |
| 152 | // corresponds to an ast.Expr; use Function.ValueForExpr |
| 153 | // instead. NB: it requires that the function was built with |
| 154 | // debug information.) |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 155 | // |
| 156 | Pos() token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // An Instruction is an SSA instruction that computes a new Value or |
| 160 | // has some effect. |
| 161 | // |
| 162 | // An Instruction that defines a value (e.g. BinOp) also implements |
| 163 | // the Value interface; an Instruction that only has an effect (e.g. Store) |
| 164 | // does not. |
| 165 | // |
| 166 | type Instruction interface { |
| 167 | // String returns the disassembled form of this value. e.g. |
| 168 | // |
| 169 | // Examples of Instructions that define a Value: |
| 170 | // e.g. "x + y" (BinOp) |
| 171 | // "len([])" (Call) |
| 172 | // Note that the name of the Value is not printed. |
| 173 | // |
| 174 | // Examples of Instructions that do define (are) Values: |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 175 | // e.g. "return x" (Return) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 176 | // "*y = x" (Store) |
| 177 | // |
| 178 | // (This separation is useful for some analyses which |
| 179 | // distinguish the operation from the value it |
| 180 | // defines. e.g. 'y = local int' is both an allocation of |
| 181 | // memory 'local int' and a definition of a pointer y.) |
| 182 | String() string |
| 183 | |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 184 | // Parent returns the function to which this instruction |
| 185 | // belongs. |
| 186 | Parent() *Function |
| 187 | |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 188 | // Block returns the basic block to which this instruction |
| 189 | // belongs. |
| 190 | Block() *BasicBlock |
| 191 | |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 192 | // setBlock sets the basic block to which this instruction belongs. |
| 193 | setBlock(*BasicBlock) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 194 | |
| 195 | // Operands returns the operands of this instruction: the |
| 196 | // set of Values it references. |
| 197 | // |
| 198 | // Specifically, it appends their addresses to rands, a |
| 199 | // user-provided slice, and returns the resulting slice, |
| 200 | // permitting avoidance of memory allocation. |
| 201 | // |
| 202 | // The operands are appended in undefined order; the addresses |
| 203 | // are always non-nil but may point to a nil Value. Clients |
| 204 | // may store through the pointers, e.g. to effect a value |
| 205 | // renaming. |
| 206 | // |
| 207 | // Value.Referrers is a subset of the inverse of this |
| 208 | // relation. (Referrers are not tracked for all types of |
| 209 | // Values.) |
| 210 | Operands(rands []*Value) []*Value |
| 211 | |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 212 | // Pos returns the location of the AST token most closely |
| 213 | // associated with the operation that gave rise to this |
| 214 | // instruction, or token.NoPos if it was not explicit in the |
| 215 | // source. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 216 | // |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 217 | // For each ast.Node type, a particular token is designated as |
| 218 | // the closest location for the expression, e.g. the Go token |
| 219 | // for an *ast.GoStmt. This permits a compact but approximate |
| 220 | // mapping from Instructions to source positions for use in |
| 221 | // diagnostic messages, for example. |
| 222 | // |
| 223 | // (Do not use this position to determine which Instruction |
| 224 | // corresponds to an ast.Expr; see the notes for Value.Pos. |
| 225 | // This position may be used to determine which non-Value |
| 226 | // Instruction corresponds to some ast.Stmts, but not all: If |
| 227 | // and Jump instructions have no Pos(), for example.) |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 228 | // |
| 229 | Pos() token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Function represents the parameters, results and code of a function |
| 233 | // or method. |
| 234 | // |
| 235 | // If Blocks is nil, this indicates an external function for which no |
| 236 | // Go source code is available. In this case, Captures and Locals |
| 237 | // will be nil too. Clients performing whole-program analysis must |
| 238 | // handle external functions specially. |
| 239 | // |
| 240 | // Functions are immutable values; they do not have addresses. |
| 241 | // |
| 242 | // Blocks[0] is the function entry point; block order is not otherwise |
| 243 | // semantically significant, though it may affect the readability of |
| 244 | // the disassembly. |
| 245 | // |
Alan Donovan | 2accef2 | 2013-10-14 15:38:56 -0400 | [diff] [blame] | 246 | // Recover is an optional second entry point to which control resumes |
| 247 | // after a recovered panic. The Recover block may contain only a load |
| 248 | // of the function's named return parameters followed by a return of |
| 249 | // the loaded values. |
| 250 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 251 | // A nested function that refers to one or more lexically enclosing |
| 252 | // local variables ("free variables") has Capture parameters. Such |
| 253 | // functions cannot be called directly but require a value created by |
| 254 | // MakeClosure which, via its Bindings, supplies values for these |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 255 | // parameters. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 256 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 257 | // If the function is a method (Signature.Recv() != nil) then the first |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 258 | // element of Params is the receiver parameter. |
| 259 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 260 | // Pos() returns the declaring ast.FuncLit.Type.Func or the position |
| 261 | // of the ast.FuncDecl.Name, if the function was explicit in the |
Alan Donovan | 1fa3f78 | 2013-07-03 17:57:20 -0400 | [diff] [blame] | 262 | // source. Synthetic wrappers, for which Synthetic != "", may share |
| 263 | // the same position as the function they wrap. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 264 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 265 | // Type() returns the function's Signature. |
| 266 | // |
| 267 | type Function struct { |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 268 | name string |
Robert Griesemer | 64ea46e | 2013-07-26 22:27:48 -0700 | [diff] [blame] | 269 | object types.Object // a declared *types.Func; nil for init, wrappers, etc. |
| 270 | method *types.Selection // info about provenance of synthetic methods [currently unused] |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 271 | Signature *types.Signature |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 272 | pos token.Pos |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 273 | |
Alan Donovan | 1fa3f78 | 2013-07-03 17:57:20 -0400 | [diff] [blame] | 274 | Synthetic string // provenance of synthetic function; "" for true source functions |
Alan Donovan | aa23862 | 2013-10-27 10:55:21 -0400 | [diff] [blame] | 275 | syntax ast.Node // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 276 | Enclosing *Function // enclosing function if anon; nil if global |
Alan Donovan | 87ced82 | 2013-10-23 17:07:52 -0400 | [diff] [blame] | 277 | Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 278 | Prog *Program // enclosing program |
| 279 | Params []*Parameter // function parameters; for methods, includes receiver |
| 280 | FreeVars []*Capture // free variables whose values must be supplied by closure |
| 281 | Locals []*Alloc |
| 282 | Blocks []*BasicBlock // basic blocks of the function; nil => external |
Alan Donovan | 2accef2 | 2013-10-14 15:38:56 -0400 | [diff] [blame] | 283 | Recover *BasicBlock // optional; control transfers here after recovered panic |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 284 | AnonFuncs []*Function // anonymous functions directly beneath this one |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 285 | referrers []Instruction // referring instructions (iff Enclosing != nil) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 286 | |
| 287 | // The following fields are set transiently during building, |
| 288 | // then cleared. |
| 289 | currentBlock *BasicBlock // where to emit code |
| 290 | objects map[types.Object]Value // addresses of local variables |
| 291 | namedResults []*Alloc // tuple of named results |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 292 | targets *targets // linked stack of branch targets |
| 293 | lblocks map[*ast.Object]*lblock // labelled blocks |
| 294 | } |
| 295 | |
| 296 | // An SSA basic block. |
| 297 | // |
| 298 | // The final element of Instrs is always an explicit transfer of |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 299 | // control (If, Jump, Return or Panic). |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 300 | // |
| 301 | // A block may contain no Instructions only if it is unreachable, |
| 302 | // i.e. Preds is nil. Empty blocks are typically pruned. |
| 303 | // |
| 304 | // BasicBlocks and their Preds/Succs relation form a (possibly cyclic) |
| 305 | // graph independent of the SSA Value graph. It is illegal for |
| 306 | // multiple edges to exist between the same pair of blocks. |
| 307 | // |
| 308 | // The order of Preds and Succs are significant (to Phi and If |
| 309 | // instructions, respectively). |
| 310 | // |
| 311 | type BasicBlock struct { |
| 312 | Index int // index of this block within Func.Blocks |
| 313 | Comment string // optional label; no semantic significance |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 314 | parent *Function // parent function |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 315 | Instrs []Instruction // instructions in order |
| 316 | Preds, Succs []*BasicBlock // predecessors and successors |
| 317 | succs2 [2]*BasicBlock // initial space for Succs. |
| 318 | dom *domNode // node in dominator tree; optional. |
| 319 | gaps int // number of nil Instrs (transient). |
| 320 | rundefers int // number of rundefers (transient) |
| 321 | } |
| 322 | |
| 323 | // Pure values ---------------------------------------- |
| 324 | |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 325 | // A Capture represents a free variable of the function to which it |
| 326 | // belongs. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 327 | // |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 328 | // Captures are used to implement anonymous functions, whose free |
| 329 | // variables are lexically captured in a closure formed by |
| 330 | // MakeClosure. The referent of such a capture is an Alloc or another |
| 331 | // Capture and is considered a potentially escaping heap address, with |
| 332 | // pointer type. |
| 333 | // |
| 334 | // Captures are also used to implement bound method closures. Such a |
| 335 | // capture represents the receiver value and may be of any type that |
| 336 | // has concrete methods. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 337 | // |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 338 | // Pos() returns the position of the value that was captured, which |
| 339 | // belongs to an enclosing function. |
| 340 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 341 | type Capture struct { |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 342 | name string |
| 343 | typ types.Type |
| 344 | pos token.Pos |
| 345 | parent *Function |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 346 | referrers []Instruction |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 347 | |
| 348 | // Transiently needed during building. |
| 349 | outer Value // the Value captured from the enclosing context. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // A Parameter represents an input parameter of a function. |
| 353 | // |
| 354 | type Parameter struct { |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 355 | name string |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 356 | object types.Object // a *types.Var; nil for non-source locals |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 357 | typ types.Type |
| 358 | pos token.Pos |
| 359 | parent *Function |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 360 | referrers []Instruction |
| 361 | } |
| 362 | |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 363 | // A Const represents the value of a constant expression. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 364 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 365 | // It may have a nil, boolean, string or numeric (integer, fraction or |
| 366 | // complex) value, or a []byte or []rune conversion of a string |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 367 | // constant. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 368 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 369 | // Consts may be of named types. A constant's underlying type can be |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 370 | // a basic type, possibly one of the "untyped" types, or a slice type |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 371 | // whose elements' underlying type is byte or rune. A nil constant can |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 372 | // have any reference type: interface, map, channel, pointer, slice, |
| 373 | // or function---but not "untyped nil". |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 374 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 375 | // All source-level constant expressions are represented by a Const |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 376 | // of equal type and value. |
| 377 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 378 | // Value holds the exact value of the constant, independent of its |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 379 | // Type(), using the same representation as package go/exact uses for |
Robert Griesemer | f50f6c8 | 2013-10-09 14:17:25 -0700 | [diff] [blame] | 380 | // constants, or nil for a typed nil value. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 381 | // |
Alan Donovan | a399e26 | 2013-07-15 16:10:08 -0400 | [diff] [blame] | 382 | // Pos() returns token.NoPos. |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 383 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 384 | // Example printed form: |
| 385 | // 42:int |
| 386 | // "hello":untyped string |
| 387 | // 3+4i:MyComplex |
| 388 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 389 | type Const struct { |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 390 | typ types.Type |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 391 | Value exact.Value |
| 392 | } |
| 393 | |
| 394 | // A Global is a named Value holding the address of a package-level |
| 395 | // variable. |
| 396 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 397 | // Pos() returns the position of the ast.ValueSpec.Names[*] |
| 398 | // identifier. |
| 399 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 400 | type Global struct { |
Alan Donovan | bc1f724 | 2013-07-11 14:12:30 -0400 | [diff] [blame] | 401 | name string |
| 402 | object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard |
| 403 | typ types.Type |
| 404 | pos token.Pos |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 405 | |
| 406 | Pkg *Package |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 409 | // A Builtin represents a built-in function, e.g. len. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 410 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 411 | // Builtins are immutable values. Builtins do not have addresses. |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 412 | // Builtins can only appear in CallCommon.Func. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 413 | // |
Alan Donovan | 318b83e | 2013-09-23 18:18:35 -0400 | [diff] [blame] | 414 | // Object() returns a *types.Builtin. |
| 415 | // |
| 416 | // Type() returns types.Typ[types.Invalid], since built-in functions |
| 417 | // may have polymorphic or variadic types that are not expressible in |
| 418 | // Go's type system. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 419 | // |
| 420 | type Builtin struct { |
Alan Donovan | 318b83e | 2013-09-23 18:18:35 -0400 | [diff] [blame] | 421 | object *types.Builtin // canonical types.Universe object for this built-in |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Value-defining instructions ---------------------------------------- |
| 425 | |
| 426 | // The Alloc instruction reserves space for a value of the given type, |
| 427 | // zero-initializes it, and yields its address. |
| 428 | // |
| 429 | // Alloc values are always addresses, and have pointer types, so the |
| 430 | // type of the allocated space is actually indirect(Type()). |
| 431 | // |
| 432 | // If Heap is false, Alloc allocates space in the function's |
| 433 | // activation record (frame); we refer to an Alloc(Heap=false) as a |
| 434 | // "local" alloc. Each local Alloc returns the same address each time |
| 435 | // it is executed within the same activation; the space is |
| 436 | // re-initialized to zero. |
| 437 | // |
| 438 | // If Heap is true, Alloc allocates space in the heap, and returns; we |
| 439 | // refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc |
| 440 | // returns a different address each time it is executed. |
| 441 | // |
| 442 | // When Alloc is applied to a channel, map or slice type, it returns |
| 443 | // the address of an uninitialized (nil) reference of that kind; store |
| 444 | // the result of MakeSlice, MakeMap or MakeChan in that location to |
| 445 | // instantiate these types. |
| 446 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 447 | // Pos() returns the ast.CompositeLit.Lbrace for a composite literal, |
| 448 | // or the ast.CallExpr.Lparen for a call to new() or for a call that |
| 449 | // allocates a varargs slice. |
| 450 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 451 | // Example printed form: |
| 452 | // t0 = local int |
| 453 | // t1 = new int |
| 454 | // |
| 455 | type Alloc struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 456 | register |
Alan Donovan | 5cc33ea | 2013-08-01 14:06:10 -0400 | [diff] [blame] | 457 | Comment string |
| 458 | Heap bool |
| 459 | index int // dense numbering; for lifting |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 462 | // The Phi instruction represents an SSA φ-node, which combines values |
| 463 | // that differ across incoming control-flow edges and yields a new |
| 464 | // value. Within a block, all φ-nodes must appear before all non-φ |
| 465 | // nodes. |
| 466 | // |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 467 | // Pos() returns the position of the && or || for short-circuit |
| 468 | // control-flow joins, or that of the *Alloc for φ-nodes inserted |
| 469 | // during SSA renaming. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 470 | // |
| 471 | // Example printed form: |
| 472 | // t2 = phi [0.start: t0, 1.if.then: t1, ...] |
| 473 | // |
| 474 | type Phi struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 475 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 476 | Comment string // a hint as to its purpose |
| 477 | Edges []Value // Edges[i] is value for Block().Preds[i] |
| 478 | } |
| 479 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 480 | // The Call instruction represents a function or method call. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 481 | // |
| 482 | // The Call instruction yields the function result, if there is |
| 483 | // exactly one, or a tuple (empty or len>1) whose components are |
| 484 | // accessed via Extract. |
| 485 | // |
| 486 | // See CallCommon for generic function call documentation. |
| 487 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 488 | // Pos() returns the ast.CallExpr.Lparen, if explicit in the source. |
| 489 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 490 | // Example printed form: |
| 491 | // t2 = println(t0, t1) |
| 492 | // t4 = t3() |
| 493 | // t7 = invoke t5.Println(...t6) |
| 494 | // |
| 495 | type Call struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 496 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 497 | Call CallCommon |
| 498 | } |
| 499 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 500 | // The BinOp instruction yields the result of binary operation X Op Y. |
| 501 | // |
| 502 | // Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 503 | // |
| 504 | // Example printed form: |
| 505 | // t1 = t0 + 1:int |
| 506 | // |
| 507 | type BinOp struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 508 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 509 | // One of: |
| 510 | // ADD SUB MUL QUO REM + - * / % |
| 511 | // AND OR XOR SHL SHR AND_NOT & | ^ << >> &~ |
| 512 | // EQL LSS GTR NEQ LEQ GEQ == != < <= < >= |
| 513 | Op token.Token |
| 514 | X, Y Value |
| 515 | } |
| 516 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 517 | // The UnOp instruction yields the result of Op X. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 518 | // ARROW is channel receive. |
| 519 | // MUL is pointer indirection (load). |
| 520 | // XOR is bitwise complement. |
| 521 | // SUB is negation. |
| 522 | // |
| 523 | // If CommaOk and Op=ARROW, the result is a 2-tuple of the value above |
| 524 | // and a boolean indicating the success of the receive. The |
| 525 | // components of the tuple are accessed using Extract. |
| 526 | // |
Alan Donovan | de47eba | 2013-08-27 11:18:31 -0400 | [diff] [blame] | 527 | // Pos() returns the ast.UnaryExpr.OpPos or ast.RangeStmt.TokPos (for |
| 528 | // ranging over a channel), if explicit in the source. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 529 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 530 | // Example printed form: |
| 531 | // t0 = *x |
| 532 | // t2 = <-t1,ok |
| 533 | // |
| 534 | type UnOp struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 535 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 536 | Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^ |
| 537 | X Value |
| 538 | CommaOk bool |
| 539 | } |
| 540 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 541 | // The ChangeType instruction applies to X a value-preserving type |
| 542 | // change to Type(). |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 543 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 544 | // Type changes are permitted: |
| 545 | // - between a named type and its underlying type. |
| 546 | // - between two named types of the same underlying type. |
| 547 | // - between (possibly named) pointers to identical base types. |
| 548 | // - between f(T) functions and (T) func f() methods. |
| 549 | // - from a bidirectional channel to a read- or write-channel, |
| 550 | // optionally adding/removing a name. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 551 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 552 | // This operation cannot fail dynamically. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 553 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 554 | // Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 555 | // from an explicit conversion in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 556 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 557 | // Example printed form: |
| 558 | // t1 = changetype *int <- IntPtr (t0) |
| 559 | // |
| 560 | type ChangeType struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 561 | register |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 562 | X Value |
| 563 | } |
| 564 | |
| 565 | // The Convert instruction yields the conversion of value X to type |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 566 | // Type(). One or both of those types is basic (but possibly named). |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 567 | // |
| 568 | // A conversion may change the value and representation of its operand. |
| 569 | // Conversions are permitted: |
| 570 | // - between real numeric types. |
| 571 | // - between complex numeric types. |
| 572 | // - between string and []byte or []rune. |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 573 | // - between pointers and unsafe.Pointer. |
| 574 | // - between unsafe.Pointer and uintptr. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 575 | // - from (Unicode) integer to (UTF-8) string. |
| 576 | // A conversion may imply a type name change also. |
| 577 | // |
| 578 | // This operation cannot fail dynamically. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 579 | // |
| 580 | // Conversions of untyped string/number/bool constants to a specific |
| 581 | // representation are eliminated during SSA construction. |
| 582 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 583 | // Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 584 | // from an explicit conversion in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 585 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 586 | // Example printed form: |
| 587 | // t1 = convert []byte <- string (t0) |
| 588 | // |
| 589 | type Convert struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 590 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 591 | X Value |
| 592 | } |
| 593 | |
| 594 | // ChangeInterface constructs a value of one interface type from a |
| 595 | // value of another interface type known to be assignable to it. |
Alan Donovan | ae80163 | 2013-07-26 21:49:27 -0400 | [diff] [blame] | 596 | // This operation cannot fail. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 597 | // |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 598 | // Pos() returns the ast.CallExpr.Lparen if the instruction arose from |
| 599 | // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the |
| 600 | // instruction arose from an explicit e.(T) operation; or token.NoPos |
| 601 | // otherwise. |
| 602 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 603 | // Example printed form: |
| 604 | // t1 = change interface interface{} <- I (t0) |
| 605 | // |
| 606 | type ChangeInterface struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 607 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 608 | X Value |
| 609 | } |
| 610 | |
| 611 | // MakeInterface constructs an instance of an interface type from a |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 612 | // value of a concrete type. |
| 613 | // |
Alan Donovan | 2a3a129 | 2013-07-30 14:28:14 -0400 | [diff] [blame] | 614 | // Use X.Type().MethodSet() to find the method-set of X, and |
Alan Donovan | 2f6855a | 2013-07-30 16:36:58 -0400 | [diff] [blame] | 615 | // Program.Method(m) to find the implementation of a method. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 616 | // |
| 617 | // To construct the zero value of an interface type T, use: |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 618 | // NewConst(exact.MakeNil(), T, pos) |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 619 | // |
| 620 | // Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 621 | // from an explicit conversion in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 622 | // |
| 623 | // Example printed form: |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 624 | // t1 = make interface{} <- int (42:int) |
| 625 | // t2 = make Stringer <- t0 |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 626 | // |
| 627 | type MakeInterface struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 628 | register |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 629 | X Value |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 632 | // The MakeClosure instruction yields a closure value whose code is |
| 633 | // Fn and whose free variables' values are supplied by Bindings. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 634 | // |
| 635 | // Type() returns a (possibly named) *types.Signature. |
| 636 | // |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 637 | // Pos() returns the ast.FuncLit.Type.Func for a function literal |
| 638 | // closure or the ast.SelectorExpr.Sel for a bound method closure. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 639 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 640 | // Example printed form: |
| 641 | // t0 = make closure anon@1.2 [x y z] |
Alan Donovan | 8cdf1f1 | 2013-05-22 17:56:18 -0400 | [diff] [blame] | 642 | // t1 = make closure bound$(main.I).add [i] |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 643 | // |
| 644 | type MakeClosure struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 645 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 646 | Fn Value // always a *Function |
| 647 | Bindings []Value // values for each free variable in Fn.FreeVars |
| 648 | } |
| 649 | |
| 650 | // The MakeMap instruction creates a new hash-table-based map object |
| 651 | // and yields a value of kind map. |
| 652 | // |
| 653 | // Type() returns a (possibly named) *types.Map. |
| 654 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 655 | // Pos() returns the ast.CallExpr.Lparen, if created by make(map), or |
| 656 | // the ast.CompositeLit.Lbrack if created by a literal. |
| 657 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 658 | // Example printed form: |
| 659 | // t1 = make map[string]int t0 |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 660 | // t1 = make StringIntMap t0 |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 661 | // |
| 662 | type MakeMap struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 663 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 664 | Reserve Value // initial space reservation; nil => default |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // The MakeChan instruction creates a new channel object and yields a |
| 668 | // value of kind chan. |
| 669 | // |
| 670 | // Type() returns a (possibly named) *types.Chan. |
| 671 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 672 | // Pos() returns the ast.CallExpr.Lparen for the make(chan) that |
| 673 | // created it. |
| 674 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 675 | // Example printed form: |
| 676 | // t0 = make chan int 0 |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 677 | // t0 = make IntChan 0 |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 678 | // |
| 679 | type MakeChan struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 680 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 681 | Size Value // int; size of buffer; zero => synchronous. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 684 | // The MakeSlice instruction yields a slice of length Len backed by a |
| 685 | // newly allocated array of length Cap. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 686 | // |
| 687 | // Both Len and Cap must be non-nil Values of integer type. |
| 688 | // |
| 689 | // (Alloc(types.Array) followed by Slice will not suffice because |
| 690 | // Alloc can only create arrays of statically known length.) |
| 691 | // |
| 692 | // Type() returns a (possibly named) *types.Slice. |
| 693 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 694 | // Pos() returns the ast.CallExpr.Lparen for the make([]T) that |
| 695 | // created it. |
| 696 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 697 | // Example printed form: |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 698 | // t1 = make []string 1:int t0 |
| 699 | // t1 = make StringSlice 1:int t0 |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 700 | // |
| 701 | type MakeSlice struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 702 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 703 | Len Value |
| 704 | Cap Value |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 707 | // The Slice instruction yields a slice of an existing string, slice |
| 708 | // or *array X between optional integer bounds Low and High. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 709 | // |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 710 | // Dynamically, this instruction panics if X evaluates to a nil *array |
| 711 | // pointer. |
| 712 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 713 | // Type() returns string if the type of X was string, otherwise a |
| 714 | // *types.Slice with the same element type as X. |
| 715 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 716 | // Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice |
| 717 | // operation, the ast.CompositeLit.Lbrace if created by a literal, or |
| 718 | // NoPos if not explicit in the source (e.g. a variadic argument slice). |
| 719 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 720 | // Example printed form: |
| 721 | // t1 = slice t0[1:] |
| 722 | // |
| 723 | type Slice struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 724 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 725 | X Value // slice, string, or *array |
| 726 | Low, High Value // either may be nil |
| 727 | } |
| 728 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 729 | // The FieldAddr instruction yields the address of Field of *struct X. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 730 | // |
| 731 | // The field is identified by its index within the field list of the |
| 732 | // struct type of X. |
| 733 | // |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 734 | // Dynamically, this instruction panics if X evaluates to a nil |
| 735 | // pointer. |
| 736 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 737 | // Type() returns a (possibly named) *types.Pointer. |
| 738 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 739 | // Pos() returns the position of the ast.SelectorExpr.Sel for the |
| 740 | // field, if explicit in the source. |
| 741 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 742 | // Example printed form: |
| 743 | // t1 = &t0.name [#1] |
| 744 | // |
| 745 | type FieldAddr struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 746 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 747 | X Value // *struct |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 748 | Field int // index into X.Type().Deref().(*types.Struct).Fields |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 751 | // The Field instruction yields the Field of struct X. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 752 | // |
| 753 | // The field is identified by its index within the field list of the |
| 754 | // struct type of X; by using numeric indices we avoid ambiguity of |
| 755 | // package-local identifiers and permit compact representations. |
| 756 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 757 | // Pos() returns the position of the ast.SelectorExpr.Sel for the |
| 758 | // field, if explicit in the source. |
| 759 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 760 | // Example printed form: |
| 761 | // t1 = t0.name [#1] |
| 762 | // |
| 763 | type Field struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 764 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 765 | X Value // struct |
| 766 | Field int // index into X.Type().(*types.Struct).Fields |
| 767 | } |
| 768 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 769 | // The IndexAddr instruction yields the address of the element at |
| 770 | // index Index of collection X. Index is an integer expression. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 771 | // |
| 772 | // The elements of maps and strings are not addressable; use Lookup or |
| 773 | // MapUpdate instead. |
| 774 | // |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 775 | // Dynamically, this instruction panics if X evaluates to a nil *array |
| 776 | // pointer. |
| 777 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 778 | // Type() returns a (possibly named) *types.Pointer. |
| 779 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 780 | // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if |
| 781 | // explicit in the source. |
| 782 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 783 | // Example printed form: |
| 784 | // t2 = &t0[t1] |
| 785 | // |
| 786 | type IndexAddr struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 787 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 788 | X Value // slice or *array, |
| 789 | Index Value // numeric index |
| 790 | } |
| 791 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 792 | // The Index instruction yields element Index of array X. |
| 793 | // |
| 794 | // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if |
| 795 | // explicit in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 796 | // |
| 797 | // Example printed form: |
| 798 | // t2 = t0[t1] |
| 799 | // |
| 800 | type Index struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 801 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 802 | X Value // array |
| 803 | Index Value // integer index |
| 804 | } |
| 805 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 806 | // The Lookup instruction yields element Index of collection X, a map |
| 807 | // or string. Index is an integer expression if X is a string or the |
| 808 | // appropriate key type if X is a map. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 809 | // |
| 810 | // If CommaOk, the result is a 2-tuple of the value above and a |
| 811 | // boolean indicating the result of a map membership test for the key. |
| 812 | // The components of the tuple are accessed using Extract. |
| 813 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 814 | // Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source. |
| 815 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 816 | // Example printed form: |
| 817 | // t2 = t0[t1] |
| 818 | // t5 = t3[t4],ok |
| 819 | // |
| 820 | type Lookup struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 821 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 822 | X Value // string or map |
| 823 | Index Value // numeric or key-typed index |
| 824 | CommaOk bool // return a value,ok pair |
| 825 | } |
| 826 | |
| 827 | // SelectState is a helper for Select. |
| 828 | // It represents one goal state and its corresponding communication. |
| 829 | // |
| 830 | type SelectState struct { |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 831 | Dir ast.ChanDir // direction of case |
| 832 | Chan Value // channel to use (for send or receive) |
| 833 | Send Value // value to send (for send) |
| 834 | Pos token.Pos // position of token.ARROW |
| 835 | DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode] |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 838 | // The Select instruction tests whether (or blocks until) one or more |
| 839 | // of the specified sent or received states is entered. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 840 | // |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 841 | // Let n be the number of States for which Dir==RECV and T_i (0<=i<n) |
| 842 | // be the element type of each such state's Chan. |
| 843 | // Select returns an n+2-tuple |
| 844 | // (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1) |
| 845 | // The tuple's components, described below, must be accessed via the |
| 846 | // Extract instruction. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 847 | // |
| 848 | // If Blocking, select waits until exactly one state holds, i.e. a |
| 849 | // channel becomes ready for the designated operation of sending or |
| 850 | // receiving; select chooses one among the ready states |
| 851 | // pseudorandomly, performs the send or receive operation, and sets |
| 852 | // 'index' to the index of the chosen channel. |
| 853 | // |
| 854 | // If !Blocking, select doesn't block if no states hold; instead it |
| 855 | // returns immediately with index equal to -1. |
| 856 | // |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 857 | // If the chosen channel was used for a receive, the r_i component is |
| 858 | // set to the received value, where i is the index of that state among |
| 859 | // all n receive states; otherwise r_i has the zero value of type T_i. |
| 860 | // Note that the the receive index i is not the same as the state |
| 861 | // index index. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 862 | // |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 863 | // The second component of the triple, recvOk, is a boolean whose value |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 864 | // is true iff the selected operation was a receive and the receive |
| 865 | // successfully yielded a value. |
| 866 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 867 | // Pos() returns the ast.SelectStmt.Select. |
| 868 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 869 | // Example printed form: |
| 870 | // t3 = select nonblocking [<-t0, t1<-t2, ...] |
| 871 | // t4 = select blocking [] |
| 872 | // |
| 873 | type Select struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 874 | register |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 875 | States []*SelectState |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 876 | Blocking bool |
| 877 | } |
| 878 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 879 | // The Range instruction yields an iterator over the domain and range |
| 880 | // of X, which must be a string or map. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 881 | // |
| 882 | // Elements are accessed via Next. |
| 883 | // |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 884 | // Type() returns an opaque and degenerate "rangeIter" type. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 885 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 886 | // Pos() returns the ast.RangeStmt.For. |
| 887 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 888 | // Example printed form: |
| 889 | // t0 = range "hello":string |
| 890 | // |
| 891 | type Range struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 892 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 893 | X Value // string or map |
| 894 | } |
| 895 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 896 | // The Next instruction reads and advances the (map or string) |
| 897 | // iterator Iter and returns a 3-tuple value (ok, k, v). If the |
| 898 | // iterator is not exhausted, ok is true and k and v are the next |
| 899 | // elements of the domain and range, respectively. Otherwise ok is |
| 900 | // false and k and v are undefined. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 901 | // |
| 902 | // Components of the tuple are accessed using Extract. |
| 903 | // |
| 904 | // The IsString field distinguishes iterators over strings from those |
| 905 | // over maps, as the Type() alone is insufficient: consider |
| 906 | // map[int]rune. |
| 907 | // |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 908 | // Type() returns a *types.Tuple for the triple (ok, k, v). |
| 909 | // The types of k and/or v may be types.Invalid. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 910 | // |
| 911 | // Example printed form: |
| 912 | // t1 = next t0 |
| 913 | // |
| 914 | type Next struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 915 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 916 | Iter Value |
| 917 | IsString bool // true => string iterator; false => map iterator. |
| 918 | } |
| 919 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 920 | // The TypeAssert instruction tests whether interface value X has type |
| 921 | // AssertedType. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 922 | // |
| 923 | // If !CommaOk, on success it returns v, the result of the conversion |
| 924 | // (defined below); on failure it panics. |
| 925 | // |
| 926 | // If CommaOk: on success it returns a pair (v, true) where v is the |
| 927 | // result of the conversion; on failure it returns (z, false) where z |
| 928 | // is AssertedType's zero value. The components of the pair must be |
| 929 | // accessed using the Extract instruction. |
| 930 | // |
| 931 | // If AssertedType is a concrete type, TypeAssert checks whether the |
| 932 | // dynamic type in interface X is equal to it, and if so, the result |
| 933 | // of the conversion is a copy of the value in the interface. |
| 934 | // |
| 935 | // If AssertedType is an interface, TypeAssert checks whether the |
| 936 | // dynamic type of the interface is assignable to it, and if so, the |
| 937 | // result of the conversion is a copy of the interface value X. |
Alan Donovan | ae80163 | 2013-07-26 21:49:27 -0400 | [diff] [blame] | 938 | // If AssertedType is a superinterface of X.Type(), the operation will |
| 939 | // fail iff the operand is nil. (Contrast with ChangeInterface, which |
| 940 | // performs no nil-check.) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 941 | // |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 942 | // Type() reflects the actual type of the result, possibly a |
| 943 | // 2-types.Tuple; AssertedType is the asserted type. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 944 | // |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 945 | // Pos() returns the ast.CallExpr.Lparen if the instruction arose from |
| 946 | // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 947 | // instruction arose from an explicit e.(T) operation; or the |
| 948 | // ast.CaseClause.Case if the instruction arose from a case of a |
| 949 | // type-switch statement. |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 950 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 951 | // Example printed form: |
| 952 | // t1 = typeassert t0.(int) |
| 953 | // t3 = typeassert,ok t2.(T) |
| 954 | // |
| 955 | type TypeAssert struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 956 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 957 | X Value |
| 958 | AssertedType types.Type |
| 959 | CommaOk bool |
| 960 | } |
| 961 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 962 | // The Extract instruction yields component Index of Tuple. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 963 | // |
| 964 | // This is used to access the results of instructions with multiple |
| 965 | // return values, such as Call, TypeAssert, Next, UnOp(ARROW) and |
| 966 | // IndexExpr(Map). |
| 967 | // |
| 968 | // Example printed form: |
| 969 | // t1 = extract t0 #1 |
| 970 | // |
| 971 | type Extract struct { |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 972 | register |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 973 | Tuple Value |
| 974 | Index int |
| 975 | } |
| 976 | |
| 977 | // Instructions executed for effect. They do not yield a value. -------------------- |
| 978 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 979 | // The Jump instruction transfers control to the sole successor of its |
| 980 | // owning block. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 981 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 982 | // A Jump must be the last instruction of its containing BasicBlock. |
| 983 | // |
| 984 | // Pos() returns NoPos. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 985 | // |
| 986 | // Example printed form: |
| 987 | // jump done |
| 988 | // |
| 989 | type Jump struct { |
| 990 | anInstruction |
| 991 | } |
| 992 | |
| 993 | // The If instruction transfers control to one of the two successors |
| 994 | // of its owning block, depending on the boolean Cond: the first if |
| 995 | // true, the second if false. |
| 996 | // |
| 997 | // An If instruction must be the last instruction of its containing |
| 998 | // BasicBlock. |
| 999 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1000 | // Pos() returns NoPos. |
| 1001 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1002 | // Example printed form: |
| 1003 | // if t0 goto done else body |
| 1004 | // |
| 1005 | type If struct { |
| 1006 | anInstruction |
| 1007 | Cond Value |
| 1008 | } |
| 1009 | |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1010 | // The Return instruction returns values and control back to the calling |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1011 | // function. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1012 | // |
| 1013 | // len(Results) is always equal to the number of results in the |
| 1014 | // function's signature. |
| 1015 | // |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1016 | // If len(Results) > 1, Return returns a tuple value with the specified |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1017 | // components which the caller must access using Extract instructions. |
| 1018 | // |
| 1019 | // There is no instruction to return a ready-made tuple like those |
| 1020 | // returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or |
| 1021 | // a tail-call to a function with multiple result parameters. |
| 1022 | // |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1023 | // Return must be the last instruction of its containing BasicBlock. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1024 | // Such a block has no successors. |
| 1025 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1026 | // Pos() returns the ast.ReturnStmt.Return, if explicit in the source. |
| 1027 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1028 | // Example printed form: |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1029 | // return |
| 1030 | // return nil:I, 2:int |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1031 | // |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1032 | type Return struct { |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1033 | anInstruction |
| 1034 | Results []Value |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1035 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1038 | // The RunDefers instruction pops and invokes the entire stack of |
| 1039 | // procedure calls pushed by Defer instructions in this function. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1040 | // |
| 1041 | // It is legal to encounter multiple 'rundefers' instructions in a |
| 1042 | // single control-flow path through a function; this is useful in |
| 1043 | // the combined init() function, for example. |
| 1044 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1045 | // Pos() returns NoPos. |
| 1046 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1047 | // Example printed form: |
| 1048 | // rundefers |
| 1049 | // |
| 1050 | type RunDefers struct { |
| 1051 | anInstruction |
| 1052 | } |
| 1053 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1054 | // The Panic instruction initiates a panic with value X. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1055 | // |
| 1056 | // A Panic instruction must be the last instruction of its containing |
| 1057 | // BasicBlock, which must have no successors. |
| 1058 | // |
| 1059 | // NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction; |
| 1060 | // they are treated as calls to a built-in function. |
| 1061 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1062 | // Pos() returns the ast.CallExpr.Lparen if this panic was explicit |
| 1063 | // in the source. |
| 1064 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1065 | // Example printed form: |
| 1066 | // panic t0 |
| 1067 | // |
| 1068 | type Panic struct { |
| 1069 | anInstruction |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1070 | X Value // an interface{} |
| 1071 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1074 | // The Go instruction creates a new goroutine and calls the specified |
| 1075 | // function within it. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1076 | // |
| 1077 | // See CallCommon for generic function call documentation. |
| 1078 | // |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 1079 | // Pos() returns the ast.GoStmt.Go. |
| 1080 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1081 | // Example printed form: |
| 1082 | // go println(t0, t1) |
| 1083 | // go t3() |
| 1084 | // go invoke t5.Println(...t6) |
| 1085 | // |
| 1086 | type Go struct { |
| 1087 | anInstruction |
| 1088 | Call CallCommon |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 1089 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1092 | // The Defer instruction pushes the specified call onto a stack of |
| 1093 | // functions to be called by a RunDefers instruction or by a panic. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1094 | // |
| 1095 | // See CallCommon for generic function call documentation. |
| 1096 | // |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 1097 | // Pos() returns the ast.DeferStmt.Defer. |
| 1098 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1099 | // Example printed form: |
| 1100 | // defer println(t0, t1) |
| 1101 | // defer t3() |
| 1102 | // defer invoke t5.Println(...t6) |
| 1103 | // |
| 1104 | type Defer struct { |
| 1105 | anInstruction |
| 1106 | Call CallCommon |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 1107 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1110 | // The Send instruction sends X on channel Chan. |
| 1111 | // |
| 1112 | // Pos() returns the ast.SendStmt.Arrow, if explicit in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1113 | // |
| 1114 | // Example printed form: |
| 1115 | // send t0 <- t1 |
| 1116 | // |
| 1117 | type Send struct { |
| 1118 | anInstruction |
| 1119 | Chan, X Value |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1120 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1123 | // The Store instruction stores Val at address Addr. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1124 | // Stores can be of arbitrary types. |
| 1125 | // |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1126 | // Pos() returns the ast.StarExpr.Star, if explicit in the source. |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1127 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1128 | // Example printed form: |
| 1129 | // *x = y |
| 1130 | // |
| 1131 | type Store struct { |
| 1132 | anInstruction |
| 1133 | Addr Value |
| 1134 | Val Value |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1135 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1138 | // The MapUpdate instruction updates the association of Map[Key] to |
| 1139 | // Value. |
| 1140 | // |
Alan Donovan | c8a6890 | 2013-08-22 10:13:51 -0400 | [diff] [blame] | 1141 | // Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack, |
| 1142 | // if explicit in the source. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1143 | // |
| 1144 | // Example printed form: |
| 1145 | // t0[t1] = t2 |
| 1146 | // |
| 1147 | type MapUpdate struct { |
| 1148 | anInstruction |
| 1149 | Map Value |
| 1150 | Key Value |
| 1151 | Value Value |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1152 | pos token.Pos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Alan Donovan | e296265 | 2013-10-28 12:05:29 -0400 | [diff] [blame] | 1155 | // A DebugRef instruction maps a source-level expression Expr to the |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 1156 | // SSA value X that represents the value (!IsAddr) or address (IsAddr) |
Alan Donovan | e296265 | 2013-10-28 12:05:29 -0400 | [diff] [blame] | 1157 | // of that expression. |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1158 | // |
| 1159 | // DebugRef is a pseudo-instruction: it has no dynamic effect. |
| 1160 | // |
Alan Donovan | e590cdb | 2013-10-09 12:47:30 -0400 | [diff] [blame] | 1161 | // Pos() returns Expr.Pos(), the start position of the source-level |
| 1162 | // expression. This is not the same as the "designated" token as |
| 1163 | // documented at Value.Pos(). e.g. CallExpr.Pos() does not return the |
| 1164 | // position of the ("designated") Lparen token. |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1165 | // |
Alan Donovan | e296265 | 2013-10-28 12:05:29 -0400 | [diff] [blame] | 1166 | // If Expr is an *ast.Ident denoting a var or func, Object() returns |
| 1167 | // the object; though this information can be obtained from the type |
| 1168 | // checker, including it here greatly facilitates debugging. |
| 1169 | // For non-Ident expressions, Object() returns nil. |
| 1170 | // |
| 1171 | // DebugRefs are generated only for functions built with debugging |
| 1172 | // enabled; see Package.SetDebugMode(). |
| 1173 | // |
| 1174 | // DebugRefs are not emitted for ast.Idents referring to constants or |
| 1175 | // predeclared identifiers, since they are trivial and numerous. |
| 1176 | // Nor are they emitted for ast.ParenExprs. |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1177 | // |
| 1178 | // (By representing these as instructions, rather than out-of-band, |
| 1179 | // consistency is maintained during transformation passes by the |
| 1180 | // ordinary SSA renaming machinery.) |
| 1181 | // |
Alan Donovan | e296265 | 2013-10-28 12:05:29 -0400 | [diff] [blame] | 1182 | // Example printed form: |
| 1183 | // ; *ast.CallExpr @ 102:9 is t5 |
| 1184 | // ; var x float64 @ 109:72 is x |
| 1185 | // ; address of *ast.CompositeLit @ 216:10 is t0 |
Alan Donovan | aa23862 | 2013-10-27 10:55:21 -0400 | [diff] [blame] | 1186 | // |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1187 | type DebugRef struct { |
| 1188 | anInstruction |
Alan Donovan | 9f640c2 | 2013-10-24 18:31:50 -0400 | [diff] [blame] | 1189 | Expr ast.Expr // the referring expression (never *ast.ParenExpr) |
Alan Donovan | e296265 | 2013-10-28 12:05:29 -0400 | [diff] [blame] | 1190 | object types.Object // the identity of the source var/func |
Alan Donovan | 9f640c2 | 2013-10-24 18:31:50 -0400 | [diff] [blame] | 1191 | IsAddr bool // Expr is addressable and X is the address it denotes |
Alan Donovan | e296265 | 2013-10-28 12:05:29 -0400 | [diff] [blame] | 1192 | X Value // the value or address of Expr |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1193 | } |
| 1194 | |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1195 | // Embeddable mix-ins and helpers for common parts of other structs. ----------- |
| 1196 | |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 1197 | // register is a mix-in embedded by all SSA values that are also |
| 1198 | // instructions, i.e. virtual registers, and provides a uniform |
| 1199 | // implementation of most of the Value interface: Value.Name() is a |
| 1200 | // numbered register (e.g. "t0"); the other methods are field accessors. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1201 | // |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 1202 | // Temporary names are automatically assigned to each register on |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1203 | // completion of building a function in SSA form. |
| 1204 | // |
| 1205 | // Clients must not assume that the 'id' value (and the Name() derived |
| 1206 | // from it) is unique within a function. As always in this API, |
| 1207 | // semantics are determined only by identity; names exist only to |
| 1208 | // facilitate debugging. |
| 1209 | // |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 1210 | type register struct { |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1211 | anInstruction |
| 1212 | num int // "name" of virtual register, e.g. "t0". Not guaranteed unique. |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1213 | typ types.Type // type of virtual register |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1214 | pos token.Pos // position of source expression, or NoPos |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1215 | referrers []Instruction |
| 1216 | } |
| 1217 | |
| 1218 | // anInstruction is a mix-in embedded by all Instructions. |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 1219 | // It provides the implementations of the Block and setBlock methods. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1220 | type anInstruction struct { |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1221 | block *BasicBlock // the basic block of this instruction |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | // CallCommon is contained by Go, Defer and Call to hold the |
| 1225 | // common parts of a function or method call. |
| 1226 | // |
| 1227 | // Each CallCommon exists in one of two modes, function call and |
| 1228 | // interface method invocation, or "call" and "invoke" for short. |
| 1229 | // |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 1230 | // 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 1231 | // represents an ordinary function call of the value in Value, |
| 1232 | // which may be a *Builtin, a *Function or any other value of kind |
| 1233 | // 'func'. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1234 | // |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1235 | // In the common case in which Value is a *Function, this indicates a |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1236 | // statically dispatched call to a package-level function, an |
| 1237 | // anonymous function, or a method of a named type. Also statically |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1238 | // dispatched, but less common, Value may be a *MakeClosure, indicating |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1239 | // an immediately applied function literal with free variables. Any |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1240 | // other value of Value indicates a dynamically dispatched function |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1241 | // call. The StaticCallee method returns the callee in these cases. |
| 1242 | // |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1243 | // Args contains the arguments to the call. If Value is a method, |
| 1244 | // Args[0] contains the receiver parameter. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1245 | // |
| 1246 | // Example printed form: |
| 1247 | // t2 = println(t0, t1) |
| 1248 | // go t3() |
| 1249 | // defer t5(...t6) |
| 1250 | // |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 1251 | // 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1252 | // represents a dynamically dispatched call to an interface method. |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1253 | // In this mode, Value is the interface value and Method is the |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 1254 | // interface's abstract method. Note: an abstract method may be |
| 1255 | // shared by multiple interfaces due to embedding; Value.Type() |
| 1256 | // provides the specific interface used for this call. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1257 | // |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1258 | // Value is implicitly supplied to the concrete method implementation |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1259 | // as the receiver parameter; in other words, Args[0] holds not the |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1260 | // receiver but the first true argument. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1261 | // |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1262 | // Example printed form: |
| 1263 | // t1 = invoke t0.String() |
| 1264 | // go invoke t3.Run(t2) |
| 1265 | // defer invoke t4.Handle(...t5) |
| 1266 | // |
| 1267 | // In both modes, HasEllipsis is true iff the last element of Args is |
| 1268 | // a slice value containing zero or more arguments to a variadic |
| 1269 | // function. (This is not semantically significant since the type of |
| 1270 | // the called function is sufficient to determine this, but it aids |
| 1271 | // readability of the printed form.) |
| 1272 | // |
| 1273 | type CallCommon struct { |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1274 | Value Value // receiver (invoke mode) or func value (call mode) |
| 1275 | Method *types.Func // abstract method (invoke mode) |
| 1276 | Args []Value // actual parameters (in static method call, includes receiver) |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 1277 | HasEllipsis bool // true iff last Args is a slice of '...' args (needed?) |
| 1278 | pos token.Pos // position of CallExpr.Lparen, iff explicit in source |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | // IsInvoke returns true if this call has "invoke" (not "call") mode. |
| 1282 | func (c *CallCommon) IsInvoke() bool { |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 1283 | return c.Method != nil |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1286 | func (c *CallCommon) Pos() token.Pos { return c.pos } |
| 1287 | |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 1288 | // Signature returns the signature of the called function. |
| 1289 | // |
| 1290 | // For an "invoke"-mode call, the signature of the interface method is |
Alan Donovan | b68a029 | 2013-06-26 12:38:08 -0400 | [diff] [blame] | 1291 | // returned. |
| 1292 | // |
| 1293 | // In either "call" or "invoke" mode, if the callee is a method, its |
| 1294 | // receiver is represented by sig.Recv, not sig.Params().At(0). |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 1295 | // |
Alan Donovan | ea8ba6f | 2013-07-03 15:10:49 -0400 | [diff] [blame] | 1296 | // Signature returns nil for a call to a built-in function. |
| 1297 | // |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 1298 | func (c *CallCommon) Signature() *types.Signature { |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 1299 | if c.Method != nil { |
| 1300 | return c.Method.Type().(*types.Signature) |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 1301 | } |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1302 | sig, _ := c.Value.Type().Underlying().(*types.Signature) // nil for *Builtin |
Alan Donovan | ea8ba6f | 2013-07-03 15:10:49 -0400 | [diff] [blame] | 1303 | return sig |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 1304 | } |
| 1305 | |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1306 | // StaticCallee returns the called function if this is a trivially |
| 1307 | // static "call"-mode call. |
| 1308 | func (c *CallCommon) StaticCallee() *Function { |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1309 | switch fn := c.Value.(type) { |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1310 | case *Function: |
| 1311 | return fn |
| 1312 | case *MakeClosure: |
| 1313 | return fn.Fn.(*Function) |
| 1314 | } |
| 1315 | return nil |
| 1316 | } |
| 1317 | |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1318 | // Description returns a description of the mode of this call suitable |
| 1319 | // for a user interface, e.g. "static method call". |
| 1320 | func (c *CallCommon) Description() string { |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1321 | switch fn := c.Value.(type) { |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 1322 | case *Builtin: |
| 1323 | return "built-in function call" |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1324 | case *MakeClosure: |
| 1325 | return "static function closure call" |
| 1326 | case *Function: |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1327 | if fn.Signature.Recv() != nil { |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1328 | return "static method call" |
| 1329 | } |
| 1330 | return "static function call" |
| 1331 | } |
Alan Donovan | 7072253 | 2013-08-19 15:38:30 -0400 | [diff] [blame] | 1332 | if c.IsInvoke() { |
| 1333 | return "dynamic method call" // ("invoke" mode) |
| 1334 | } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1335 | return "dynamic function call" |
| 1336 | } |
| 1337 | |
Alan Donovan | 8097dad | 2013-06-24 14:15:13 -0400 | [diff] [blame] | 1338 | // The CallInstruction interface, implemented by *Go, *Defer and *Call, |
| 1339 | // exposes the common parts of function calling instructions, |
| 1340 | // yet provides a way back to the Value defined by *Call alone. |
| 1341 | // |
| 1342 | type CallInstruction interface { |
| 1343 | Instruction |
| 1344 | Common() *CallCommon // returns the common parts of the call |
| 1345 | Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer) |
| 1346 | } |
| 1347 | |
| 1348 | func (s *Call) Common() *CallCommon { return &s.Call } |
| 1349 | func (s *Defer) Common() *CallCommon { return &s.Call } |
| 1350 | func (s *Go) Common() *CallCommon { return &s.Call } |
| 1351 | |
| 1352 | func (s *Call) Value() *Call { return s } |
| 1353 | func (s *Defer) Value() *Call { return nil } |
| 1354 | func (s *Go) Value() *Call { return nil } |
| 1355 | |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1356 | func (v *Builtin) Type() types.Type { return v.object.Type() } |
| 1357 | func (v *Builtin) Name() string { return v.object.Name() } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1358 | func (*Builtin) Referrers() *[]Instruction { return nil } |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1359 | func (v *Builtin) Pos() token.Pos { return token.NoPos } |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1360 | func (v *Builtin) Object() types.Object { return v.object } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1361 | |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1362 | func (v *Capture) Type() types.Type { return v.typ } |
| 1363 | func (v *Capture) Name() string { return v.name } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1364 | func (v *Capture) Referrers() *[]Instruction { return &v.referrers } |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1365 | func (v *Capture) Pos() token.Pos { return v.pos } |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 1366 | func (v *Capture) Parent() *Function { return v.parent } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1367 | |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 1368 | func (v *Global) Type() types.Type { return v.typ } |
| 1369 | func (v *Global) Name() string { return v.name } |
| 1370 | func (v *Global) Pos() token.Pos { return v.pos } |
| 1371 | func (v *Global) Referrers() *[]Instruction { return nil } |
| 1372 | func (v *Global) Token() token.Token { return token.VAR } |
| 1373 | func (v *Global) Object() types.Object { return v.object } |
| 1374 | func (v *Global) String() string { return v.RelString(nil) } |
| 1375 | func (v *Global) Package() *Package { return v.Pkg } |
| 1376 | func (v *Global) RelString(from *types.Package) string { return relString(v, from) } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1377 | |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 1378 | func (v *Function) Name() string { return v.name } |
| 1379 | func (v *Function) Type() types.Type { return v.Signature } |
| 1380 | func (v *Function) Pos() token.Pos { return v.pos } |
| 1381 | func (v *Function) Token() token.Token { return token.FUNC } |
| 1382 | func (v *Function) Object() types.Object { return v.object } |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 1383 | func (v *Function) String() string { return v.RelString(nil) } |
| 1384 | func (v *Function) Package() *Package { return v.Pkg } |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 1385 | func (v *Function) Referrers() *[]Instruction { |
| 1386 | if v.Enclosing != nil { |
| 1387 | return &v.referrers |
| 1388 | } |
| 1389 | return nil |
| 1390 | } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1391 | |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1392 | func (v *Parameter) Type() types.Type { return v.typ } |
| 1393 | func (v *Parameter) Name() string { return v.name } |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1394 | func (v *Parameter) Object() types.Object { return v.object } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1395 | func (v *Parameter) Referrers() *[]Instruction { return &v.referrers } |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1396 | func (v *Parameter) Pos() token.Pos { return v.pos } |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 1397 | func (v *Parameter) Parent() *Function { return v.parent } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1398 | |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1399 | func (v *Alloc) Type() types.Type { return v.typ } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1400 | func (v *Alloc) Referrers() *[]Instruction { return &v.referrers } |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1401 | func (v *Alloc) Pos() token.Pos { return v.pos } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1402 | |
Alan Donovan | 1ff3452 | 2013-10-14 13:48:34 -0400 | [diff] [blame] | 1403 | func (v *register) Type() types.Type { return v.typ } |
| 1404 | func (v *register) setType(typ types.Type) { v.typ = typ } |
| 1405 | func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) } |
| 1406 | func (v *register) setNum(num int) { v.num = num } |
| 1407 | func (v *register) Referrers() *[]Instruction { return &v.referrers } |
| 1408 | func (v *register) Pos() token.Pos { return v.pos } |
| 1409 | func (v *register) setPos(pos token.Pos) { v.pos = pos } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1410 | |
Alan Donovan | 341a07a | 2013-06-13 14:43:35 -0400 | [diff] [blame] | 1411 | func (v *anInstruction) Parent() *Function { return v.block.parent } |
Alan Donovan | 6c7ce1c | 2013-05-30 09:59:17 -0400 | [diff] [blame] | 1412 | func (v *anInstruction) Block() *BasicBlock { return v.block } |
Alan Donovan | f1e5b03 | 2013-11-07 10:08:51 -0500 | [diff] [blame] | 1413 | func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1414 | |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 1415 | func (t *Type) Name() string { return t.object.Name() } |
| 1416 | func (t *Type) Pos() token.Pos { return t.object.Pos() } |
| 1417 | func (t *Type) Type() types.Type { return t.object.Type() } |
| 1418 | func (t *Type) Token() token.Token { return token.TYPE } |
| 1419 | func (t *Type) Object() types.Object { return t.object } |
| 1420 | func (t *Type) String() string { return t.RelString(nil) } |
| 1421 | func (t *Type) Package() *Package { return t.pkg } |
| 1422 | func (t *Type) RelString(from *types.Package) string { return relString(t, from) } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1423 | |
Alan Donovan | 9fcd20e | 2013-11-15 09:21:48 -0500 | [diff] [blame^] | 1424 | func (c *NamedConst) Name() string { return c.object.Name() } |
| 1425 | func (c *NamedConst) Pos() token.Pos { return c.object.Pos() } |
| 1426 | func (c *NamedConst) String() string { return c.RelString(nil) } |
| 1427 | func (c *NamedConst) Type() types.Type { return c.object.Type() } |
| 1428 | func (c *NamedConst) Token() token.Token { return token.CONST } |
| 1429 | func (c *NamedConst) Object() types.Object { return c.object } |
| 1430 | func (c *NamedConst) Package() *Package { return c.pkg } |
| 1431 | func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1432 | |
| 1433 | // Func returns the package-level function of the specified name, |
| 1434 | // or nil if not found. |
| 1435 | // |
| 1436 | func (p *Package) Func(name string) (f *Function) { |
| 1437 | f, _ = p.Members[name].(*Function) |
| 1438 | return |
| 1439 | } |
| 1440 | |
| 1441 | // Var returns the package-level variable of the specified name, |
| 1442 | // or nil if not found. |
| 1443 | // |
| 1444 | func (p *Package) Var(name string) (g *Global) { |
| 1445 | g, _ = p.Members[name].(*Global) |
| 1446 | return |
| 1447 | } |
| 1448 | |
| 1449 | // Const returns the package-level constant of the specified name, |
| 1450 | // or nil if not found. |
| 1451 | // |
Alan Donovan | 732dbe9 | 2013-07-16 13:50:08 -0400 | [diff] [blame] | 1452 | func (p *Package) Const(name string) (c *NamedConst) { |
| 1453 | c, _ = p.Members[name].(*NamedConst) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1454 | return |
| 1455 | } |
| 1456 | |
| 1457 | // Type returns the package-level type of the specified name, |
| 1458 | // or nil if not found. |
| 1459 | // |
| 1460 | func (p *Package) Type(name string) (t *Type) { |
| 1461 | t, _ = p.Members[name].(*Type) |
| 1462 | return |
| 1463 | } |
| 1464 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1465 | func (v *Call) Pos() token.Pos { return v.Call.pos } |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 1466 | func (s *Defer) Pos() token.Pos { return s.pos } |
| 1467 | func (s *Go) Pos() token.Pos { return s.pos } |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1468 | func (s *MapUpdate) Pos() token.Pos { return s.pos } |
| 1469 | func (s *Panic) Pos() token.Pos { return s.pos } |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1470 | func (s *Return) Pos() token.Pos { return s.pos } |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1471 | func (s *Send) Pos() token.Pos { return s.pos } |
| 1472 | func (s *Store) Pos() token.Pos { return s.pos } |
| 1473 | func (s *If) Pos() token.Pos { return token.NoPos } |
| 1474 | func (s *Jump) Pos() token.Pos { return token.NoPos } |
| 1475 | func (s *RunDefers) Pos() token.Pos { return token.NoPos } |
Alan Donovan | c28bf6e | 2013-07-31 13:13:05 -0400 | [diff] [blame] | 1476 | func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() } |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1477 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1478 | // Operands. |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1479 | |
| 1480 | func (v *Alloc) Operands(rands []*Value) []*Value { |
| 1481 | return rands |
| 1482 | } |
| 1483 | |
| 1484 | func (v *BinOp) Operands(rands []*Value) []*Value { |
| 1485 | return append(rands, &v.X, &v.Y) |
| 1486 | } |
| 1487 | |
| 1488 | func (c *CallCommon) Operands(rands []*Value) []*Value { |
Alan Donovan | 118786e | 2013-07-26 14:06:26 -0400 | [diff] [blame] | 1489 | rands = append(rands, &c.Value) |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1490 | for i := range c.Args { |
| 1491 | rands = append(rands, &c.Args[i]) |
| 1492 | } |
| 1493 | return rands |
| 1494 | } |
| 1495 | |
| 1496 | func (s *Go) Operands(rands []*Value) []*Value { |
| 1497 | return s.Call.Operands(rands) |
| 1498 | } |
| 1499 | |
| 1500 | func (s *Call) Operands(rands []*Value) []*Value { |
| 1501 | return s.Call.Operands(rands) |
| 1502 | } |
| 1503 | |
| 1504 | func (s *Defer) Operands(rands []*Value) []*Value { |
| 1505 | return s.Call.Operands(rands) |
| 1506 | } |
| 1507 | |
| 1508 | func (v *ChangeInterface) Operands(rands []*Value) []*Value { |
| 1509 | return append(rands, &v.X) |
| 1510 | } |
| 1511 | |
Rob Pike | 87334f4 | 2013-05-17 14:02:47 -0700 | [diff] [blame] | 1512 | func (v *ChangeType) Operands(rands []*Value) []*Value { |
| 1513 | return append(rands, &v.X) |
| 1514 | } |
| 1515 | |
| 1516 | func (v *Convert) Operands(rands []*Value) []*Value { |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1517 | return append(rands, &v.X) |
| 1518 | } |
| 1519 | |
Alan Donovan | 55d678e | 2013-07-15 13:56:46 -0400 | [diff] [blame] | 1520 | func (s *DebugRef) Operands(rands []*Value) []*Value { |
| 1521 | return append(rands, &s.X) |
| 1522 | } |
| 1523 | |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1524 | func (v *Extract) Operands(rands []*Value) []*Value { |
| 1525 | return append(rands, &v.Tuple) |
| 1526 | } |
| 1527 | |
| 1528 | func (v *Field) Operands(rands []*Value) []*Value { |
| 1529 | return append(rands, &v.X) |
| 1530 | } |
| 1531 | |
| 1532 | func (v *FieldAddr) Operands(rands []*Value) []*Value { |
| 1533 | return append(rands, &v.X) |
| 1534 | } |
| 1535 | |
| 1536 | func (s *If) Operands(rands []*Value) []*Value { |
| 1537 | return append(rands, &s.Cond) |
| 1538 | } |
| 1539 | |
| 1540 | func (v *Index) Operands(rands []*Value) []*Value { |
| 1541 | return append(rands, &v.X, &v.Index) |
| 1542 | } |
| 1543 | |
| 1544 | func (v *IndexAddr) Operands(rands []*Value) []*Value { |
| 1545 | return append(rands, &v.X, &v.Index) |
| 1546 | } |
| 1547 | |
| 1548 | func (*Jump) Operands(rands []*Value) []*Value { |
| 1549 | return rands |
| 1550 | } |
| 1551 | |
| 1552 | func (v *Lookup) Operands(rands []*Value) []*Value { |
| 1553 | return append(rands, &v.X, &v.Index) |
| 1554 | } |
| 1555 | |
| 1556 | func (v *MakeChan) Operands(rands []*Value) []*Value { |
| 1557 | return append(rands, &v.Size) |
| 1558 | } |
| 1559 | |
| 1560 | func (v *MakeClosure) Operands(rands []*Value) []*Value { |
| 1561 | rands = append(rands, &v.Fn) |
| 1562 | for i := range v.Bindings { |
| 1563 | rands = append(rands, &v.Bindings[i]) |
| 1564 | } |
| 1565 | return rands |
| 1566 | } |
| 1567 | |
| 1568 | func (v *MakeInterface) Operands(rands []*Value) []*Value { |
| 1569 | return append(rands, &v.X) |
| 1570 | } |
| 1571 | |
| 1572 | func (v *MakeMap) Operands(rands []*Value) []*Value { |
| 1573 | return append(rands, &v.Reserve) |
| 1574 | } |
| 1575 | |
| 1576 | func (v *MakeSlice) Operands(rands []*Value) []*Value { |
| 1577 | return append(rands, &v.Len, &v.Cap) |
| 1578 | } |
| 1579 | |
| 1580 | func (v *MapUpdate) Operands(rands []*Value) []*Value { |
| 1581 | return append(rands, &v.Map, &v.Key, &v.Value) |
| 1582 | } |
| 1583 | |
| 1584 | func (v *Next) Operands(rands []*Value) []*Value { |
| 1585 | return append(rands, &v.Iter) |
| 1586 | } |
| 1587 | |
| 1588 | func (s *Panic) Operands(rands []*Value) []*Value { |
| 1589 | return append(rands, &s.X) |
| 1590 | } |
| 1591 | |
| 1592 | func (v *Phi) Operands(rands []*Value) []*Value { |
| 1593 | for i := range v.Edges { |
| 1594 | rands = append(rands, &v.Edges[i]) |
| 1595 | } |
| 1596 | return rands |
| 1597 | } |
| 1598 | |
| 1599 | func (v *Range) Operands(rands []*Value) []*Value { |
| 1600 | return append(rands, &v.X) |
| 1601 | } |
| 1602 | |
Alan Donovan | 068f017 | 2013-10-08 12:31:39 -0400 | [diff] [blame] | 1603 | func (s *Return) Operands(rands []*Value) []*Value { |
Rob Pike | 83f21b9 | 2013-05-17 13:25:48 -0700 | [diff] [blame] | 1604 | for i := range s.Results { |
| 1605 | rands = append(rands, &s.Results[i]) |
| 1606 | } |
| 1607 | return rands |
| 1608 | } |
| 1609 | |
| 1610 | func (*RunDefers) Operands(rands []*Value) []*Value { |
| 1611 | return rands |
| 1612 | } |
| 1613 | |
| 1614 | func (v *Select) Operands(rands []*Value) []*Value { |
| 1615 | for i := range v.States { |
| 1616 | rands = append(rands, &v.States[i].Chan, &v.States[i].Send) |
| 1617 | } |
| 1618 | return rands |
| 1619 | } |
| 1620 | |
| 1621 | func (s *Send) Operands(rands []*Value) []*Value { |
| 1622 | return append(rands, &s.Chan, &s.X) |
| 1623 | } |
| 1624 | |
| 1625 | func (v *Slice) Operands(rands []*Value) []*Value { |
| 1626 | return append(rands, &v.X, &v.Low, &v.High) |
| 1627 | } |
| 1628 | |
| 1629 | func (s *Store) Operands(rands []*Value) []*Value { |
| 1630 | return append(rands, &s.Addr, &s.Val) |
| 1631 | } |
| 1632 | |
| 1633 | func (v *TypeAssert) Operands(rands []*Value) []*Value { |
| 1634 | return append(rands, &v.X) |
| 1635 | } |
| 1636 | |
| 1637 | func (v *UnOp) Operands(rands []*Value) []*Value { |
| 1638 | return append(rands, &v.X) |
| 1639 | } |