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