| 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 |
|
| 5 |
package ir |
| 6 |
|
| 7 |
// This package defines a high-level intermediate representation for |
| 8 |
// Go programs using static single-assignment (SSA) form. |
| 9 |
|
| 10 |
import ( |
| 11 |
"fmt" |
| 12 |
"go/ast" |
| 13 |
"go/constant" |
| 14 |
"go/token" |
| 15 |
"go/types" |
| 16 |
"iter" |
| 17 |
"math/big" |
| 18 |
"reflect" |
| 19 |
"slices" |
| 20 |
"strings" |
| 21 |
"sync" |
| 22 |
"unsafe" |
| 23 |
|
| 24 |
"honnef.co/go/tools/go/types/typeutil" |
| 25 |
"honnef.co/go/tools/internal/xtools-internal/typeparams" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
// Replace CompositeValue with only constant values with AggregateConst. Currently disabled because it breaks field |
| 30 |
// tracking in U1000. |
| 31 |
doSimplifyConstantCompositeValues = false |
| 32 |
) |
| 33 |
|
| 34 |
type ID int |
| 35 |
|
| 36 |
// A Program is a partial or complete Go program converted to IR form. |
| 37 |
type Program struct { |
| 38 |
Fset *token.FileSet // position information for the files of this Program |
| 39 |
imported map[string]*Package // all importable Packages, keyed by import path |
| 40 |
packages map[*types.Package]*Package // all created Packages |
| 41 |
mode BuilderMode // set of mode bits for IR construction |
| 42 |
MethodSets typeutil.MethodSetCache // cache of type-checker's method-sets |
| 43 |
|
| 44 |
canon *canonizer // type canonicalization map |
| 45 |
ctxt *types.Context // cache for type checking instantiations |
| 46 |
|
| 47 |
methodsMu sync.Mutex |
| 48 |
methodSets typeutil.Map[*methodSet] // maps type to its concrete *methodSet |
| 49 |
|
| 50 |
// memoization of whether a type refers to type parameters |
| 51 |
hasParamsMu sync.Mutex |
| 52 |
hasParams typeparams.Free |
| 53 |
|
| 54 |
// set of concrete types used as MakeInterface operands |
| 55 |
makeInterfaceTypesMu sync.Mutex |
| 56 |
makeInterfaceTypes map[types.Type]unit // (may contain redundant identical types) |
| 57 |
|
| 58 |
// objectMethods is a memoization of objectMethod |
| 59 |
// to avoid creation of duplicate methods from type information. |
| 60 |
objectMethodsMu sync.Mutex |
| 61 |
objectMethods map[*types.Func]*Function |
| 62 |
|
| 63 |
noReturn func(*types.Func) bool // (optional) predicate that decides whether a given call cannot return |
| 64 |
} |
| 65 |
|
| 66 |
// A Package is a single analyzed Go package containing Members for |
| 67 |
// all package-level functions, variables, constants and types it |
| 68 |
// declares. These may be accessed directly via Members, or via the |
| 69 |
// type-specific accessor methods Func, Type, Var and Const. |
| 70 |
// |
| 71 |
// Members also contains entries for "init" (the synthetic package |
| 72 |
// initializer) and "init#%d", the nth declared init function, |
| 73 |
// and unspecified other things too. |
| 74 |
type Package struct { |
| 75 |
Prog *Program // the owning program |
| 76 |
Pkg *types.Package // the corresponding go/types.Package |
| 77 |
Members map[string]Member // all package members keyed by name (incl. init and init#%d) |
| 78 |
Functions []*Function // all functions, excluding anonymous ones |
| 79 |
values map[types.Object]Member // mapping of package objects to members (incl. methods). Contains *NamedConst, *Global, *Function (values but not types) |
| 80 |
init *Function // Func("init"); the package's init function |
| 81 |
debug bool // include full debug info in this package |
| 82 |
syntax bool // package was loaded from syntax |
| 83 |
|
| 84 |
// The following fields are set transiently, then cleared |
| 85 |
// after building. |
| 86 |
buildOnce sync.Once // ensures package building occurs once |
| 87 |
ninit int32 // number of init functions |
| 88 |
info *types.Info // package type information |
| 89 |
files []*ast.File // package ASTs |
| 90 |
created []*Function // members created as a result of building this package (includes declared functions, wrappers) |
| 91 |
initVersion map[ast.Expr]string // goversion to use for each global var init expr |
| 92 |
} |
| 93 |
|
| 94 |
// A Member is a member of a Go package, implemented by *NamedConst, |
| 95 |
// *Global, *Function, or *Type; they are created by package-level |
| 96 |
// const, var, func and type declarations respectively. |
| 97 |
type Member interface { |
| 98 |
Name() string // declared name of the package member |
| 99 |
String() string // package-qualified name of the package member |
| 100 |
RelString(*types.Package) string // like String, but relative refs are unqualified |
| 101 |
Object() types.Object // typechecker's object for this member, if any |
| 102 |
Type() types.Type // type of the package member |
| 103 |
Token() token.Token // token.{VAR,FUNC,CONST,TYPE} |
| 104 |
Package() *Package // the containing package |
| 105 |
} |
| 106 |
|
| 107 |
// A Type is a Member of a Package representing a package-level named type. |
| 108 |
type Type struct { |
| 109 |
object *types.TypeName |
| 110 |
pkg *Package |
| 111 |
} |
| 112 |
|
| 113 |
// A NamedConst is a Member of a Package representing a package-level |
| 114 |
// named constant. |
| 115 |
// |
| 116 |
// Pos() returns the position of the declaring ast.ValueSpec.Names[*] |
| 117 |
// identifier. |
| 118 |
// |
| 119 |
// NB: a NamedConst is not a Value; it contains a constant Value, which |
| 120 |
// it augments with the name and position of its 'const' declaration. |
| 121 |
type NamedConst struct { |
| 122 |
object *types.Const |
| 123 |
Value *Const |
| 124 |
pkg *Package |
| 125 |
} |
| 126 |
|
| 127 |
// A Value is an IR value that can be referenced by an instruction. |
| 128 |
type Value interface { |
| 129 |
// Name returns the name of this value, and determines how |
| 130 |
// this Value appears when used as an operand of an |
| 131 |
// Instruction. |
| 132 |
// |
| 133 |
// This is the same as the source name for Parameters, |
| 134 |
// Builtins, Functions, FreeVars, Globals. |
| 135 |
// For constants, it is a representation of the constant's value |
| 136 |
// and type. For all other Values this is the name of the |
| 137 |
// virtual register defined by the instruction. |
| 138 |
// |
| 139 |
// The name of an IR Value is not semantically significant, |
| 140 |
// and may not even be unique within a function. |
| 141 |
Name() string |
| 142 |
|
| 143 |
// If this value is an Instruction, String returns its |
| 144 |
// disassembled form; otherwise it returns unspecified |
| 145 |
// human-readable information about the Value, such as its |
| 146 |
// kind, name and type. |
| 147 |
String() string |
| 148 |
|
| 149 |
// Type returns the type of this value. Many instructions |
| 150 |
// (e.g. IndexAddr) change their behaviour depending on the |
| 151 |
// types of their operands. |
| 152 |
Type() types.Type |
| 153 |
|
| 154 |
// Parent returns the function to which this Value belongs. |
| 155 |
// It returns nil for named Functions, Builtin and Global. |
| 156 |
Parent() *Function |
| 157 |
|
| 158 |
// Referrers returns the list of instructions that have this |
| 159 |
// value as one of their operands; it may contain duplicates |
| 160 |
// if an instruction has a repeated operand. |
| 161 |
// |
| 162 |
// Referrers actually returns a pointer through which the |
| 163 |
// caller may perform mutations to the object's state. |
| 164 |
// |
| 165 |
// Referrers is currently only defined if Parent()!=nil, |
| 166 |
// i.e. for the function-local values FreeVar, Parameter, |
| 167 |
// Functions (iff anonymous) and all value-defining instructions. |
| 168 |
// It returns nil for named Functions, Builtin and Global. |
| 169 |
// |
| 170 |
// Instruction.Operands contains the inverse of this relation. |
| 171 |
Referrers() *[]Instruction |
| 172 |
|
| 173 |
Operands(rands []*Value) []*Value // nil for non-Instructions |
| 174 |
|
| 175 |
// Source returns the AST node responsible for creating this |
| 176 |
// value. A single AST node may be responsible for more than one |
| 177 |
// value, and not all values have an associated AST node. |
| 178 |
// |
| 179 |
// Do not use this method to find a Value given an ast.Expr; use |
| 180 |
// ValueForExpr instead. |
| 181 |
Source() ast.Node |
| 182 |
|
| 183 |
// Pos returns Source().Pos() if Source is not nil, else it |
| 184 |
// returns token.NoPos. |
| 185 |
Pos() token.Pos |
| 186 |
} |
| 187 |
|
| 188 |
// An Instruction is an IR instruction that computes a new Value or |
| 189 |
// has some effect. |
| 190 |
// |
| 191 |
// An Instruction that defines a value (e.g. BinOp) also implements |
| 192 |
// the Value interface; an Instruction that only has an effect (e.g. Store) |
| 193 |
// does not. |
| 194 |
type Instruction interface { |
| 195 |
setSource(ast.Node) |
| 196 |
setID(ID) |
| 197 |
|
| 198 |
Comment() string |
| 199 |
|
| 200 |
// String returns the disassembled form of this value. |
| 201 |
// |
| 202 |
// Examples of Instructions that are Values: |
| 203 |
// "x + y" (BinOp) |
| 204 |
// "len([])" (Call) |
| 205 |
// Note that the name of the Value is not printed. |
| 206 |
// |
| 207 |
// Examples of Instructions that are not Values: |
| 208 |
// "return x" (Return) |
| 209 |
// "*y = x" (Store) |
| 210 |
// |
| 211 |
// (The separation Value.Name() from Value.String() is useful |
| 212 |
// for some analyses which distinguish the operation from the |
| 213 |
// value it defines, e.g., 'y = local int' is both an allocation |
| 214 |
// of memory 'local int' and a definition of a pointer y.) |
| 215 |
String() string |
| 216 |
|
| 217 |
// ID returns the ID of this instruction. IDs are unique within a single |
| 218 |
// function and are densely numbered, but may contain gaps. |
| 219 |
// Globally, instructions are identified by their addresses. However, |
| 220 |
// IDs exist to facilitate efficient storage of mappings between |
| 221 |
// instructions and data when analysing functions. |
| 222 |
// |
| 223 |
// NB: IDs are allocated late in the IR construction process and |
| 224 |
// are not available to early stages of said process. |
| 225 |
ID() ID |
| 226 |
|
| 227 |
// Parent returns the function to which this instruction |
| 228 |
// belongs. |
| 229 |
Parent() *Function |
| 230 |
|
| 231 |
// Block returns the basic block to which this instruction |
| 232 |
// belongs. |
| 233 |
Block() *BasicBlock |
| 234 |
|
| 235 |
// setBlock sets the basic block to which this instruction belongs. |
| 236 |
setBlock(*BasicBlock) |
| 237 |
|
| 238 |
// Operands returns the operands of this instruction: the |
| 239 |
// set of Values it references. |
| 240 |
// |
| 241 |
// Specifically, it appends their addresses to rands, a |
| 242 |
// user-provided slice, and returns the resulting slice, |
| 243 |
// permitting avoidance of memory allocation. |
| 244 |
// |
| 245 |
// The operands are appended in undefined order, but the order |
| 246 |
// is consistent for a given Instruction; the addresses are |
| 247 |
// always non-nil but may point to a nil Value. Clients may |
| 248 |
// store through the pointers, e.g. to effect a value |
| 249 |
// renaming. |
| 250 |
// |
| 251 |
// Value.Referrers is a subset of the inverse of this |
| 252 |
// relation. (Referrers are not tracked for all types of |
| 253 |
// Values.) |
| 254 |
Operands(rands []*Value) []*Value |
| 255 |
|
| 256 |
Referrers() *[]Instruction // nil for non-Values |
| 257 |
|
| 258 |
// Source returns the AST node responsible for creating this |
| 259 |
// instruction. A single AST node may be responsible for more than |
| 260 |
// one instruction, and not all instructions have an associated |
| 261 |
// AST node. |
| 262 |
Source() ast.Node |
| 263 |
|
| 264 |
// Pos returns Source().Pos() if Source is not nil, else it |
| 265 |
// returns token.NoPos. |
| 266 |
Pos() token.Pos |
| 267 |
} |
| 268 |
|
| 269 |
// A Node is a node in the IR value graph. Every concrete type that |
| 270 |
// implements Node is also either a Value, an Instruction, or both. |
| 271 |
// |
| 272 |
// Node contains the methods common to Value and Instruction, plus the |
| 273 |
// Operands and Referrers methods generalized to return nil for |
| 274 |
// non-Instructions and non-Values, respectively. |
| 275 |
// |
| 276 |
// Node is provided to simplify IR graph algorithms. Clients should |
| 277 |
// use the more specific and informative Value or Instruction |
| 278 |
// interfaces where appropriate. |
| 279 |
type Node interface { |
| 280 |
// Common methods: |
| 281 |
String() string |
| 282 |
Source() ast.Node |
| 283 |
Pos() token.Pos |
| 284 |
Parent() *Function |
| 285 |
|
| 286 |
// Partial methods: |
| 287 |
Operands(rands []*Value) []*Value // nil for non-Instructions |
| 288 |
Referrers() *[]Instruction // nil for non-Values |
| 289 |
} |
| 290 |
|
| 291 |
// Function represents the parameters, results, and code of a function |
| 292 |
// or method. |
| 293 |
// |
| 294 |
// If Blocks is nil, this indicates an external function for which no |
| 295 |
// Go source code is available. In this case, FreeVars, Locals, and |
| 296 |
// Params are nil too. Clients performing whole-program analysis must |
| 297 |
// handle external functions specially. |
| 298 |
// |
| 299 |
// Blocks contains the function's control-flow graph (CFG). |
| 300 |
// Blocks[0] is the function entry point; block order is not otherwise |
| 301 |
// semantically significant, though it may affect the readability of |
| 302 |
// the disassembly. |
| 303 |
// To iterate over the blocks in dominance order, use DomPreorder(). |
| 304 |
// |
| 305 |
// Recover is an optional second entry point to which control resumes |
| 306 |
// after a recovered panic. The Recover block may contain only a return |
| 307 |
// statement, preceded by a load of the function's named return |
| 308 |
// parameters, if any. |
| 309 |
// |
| 310 |
// A nested function (Parent()!=nil) that refers to one or more |
| 311 |
// lexically enclosing local variables ("free variables") has FreeVars. |
| 312 |
// Such functions cannot be called directly but require a |
| 313 |
// value created by MakeClosure which, via its Bindings, supplies |
| 314 |
// values for these parameters. |
| 315 |
// |
| 316 |
// If the function is a method (Signature.Recv() != nil) then the first |
| 317 |
// element of Params is the receiver parameter. |
| 318 |
// |
| 319 |
// A Go package may declare many functions called "init". |
| 320 |
// For each one, Object().Name() returns "init" but Name() returns |
| 321 |
// "init#1", etc, in declaration order. |
| 322 |
// |
| 323 |
// Pos() returns the declaring ast.FuncLit.Type.Func or the position |
| 324 |
// of the ast.FuncDecl.Name, if the function was explicit in the |
| 325 |
// source. Synthetic wrappers, for which Synthetic != "", may share |
| 326 |
// the same position as the function they wrap. |
| 327 |
// Syntax.Pos() always returns the position of the declaring "func" token. |
| 328 |
// |
| 329 |
// When the operand of a range statement is an iterator function, |
| 330 |
// the loop body is transformed into a synthetic anonymous function |
| 331 |
// that is passed as the yield argument in a call to the iterator. |
| 332 |
// In that case, Function.Source() is the ast.RangeStmt. |
| 333 |
// and Function.Syntax is the ast.RangeStmt. |
| 334 |
// |
| 335 |
// Synthetic functions, for which Synthetic != "", are functions |
| 336 |
// that do not appear in the source AST. These include: |
| 337 |
// - method wrappers, |
| 338 |
// - thunks, |
| 339 |
// - bound functions, |
| 340 |
// - empty functions built from loaded type information, |
| 341 |
// - yield functions created from range-over-func loops, |
| 342 |
// - package init functions, and |
| 343 |
// - instantiations of generic functions. |
| 344 |
// |
| 345 |
// Synthetic wrapper functions may share the same position |
| 346 |
// as the function they wrap. |
| 347 |
// |
| 348 |
// Type() returns the function's Signature. |
| 349 |
// |
| 350 |
// A generic function is a function or method that has uninstantiated type |
| 351 |
// parameters (TypeParams() != nil). Consider a hypothetical generic |
| 352 |
// method, (*Map[K,V]).Get. It may be instantiated with all |
| 353 |
// non-parameterized types as (*Map[string,int]).Get or with |
| 354 |
// parameterized types as (*Map[string,U]).Get, where U is a type parameter. |
| 355 |
// In both instantiations, Origin() refers to the instantiated generic |
| 356 |
// method, (*Map[K,V]).Get, TypeParams() refers to the parameters [K,V] of |
| 357 |
// the generic method. TypeArgs() refers to [string,U] or [string,int], |
| 358 |
// respectively, and is nil in the generic method. |
| 359 |
type Function struct { |
| 360 |
name string |
| 361 |
object *types.Func // symbol for declared function (nil for FuncLit or synthetic init) |
| 362 |
method *selection // info about provenance of synthetic methods; thunk => non-nil |
| 363 |
Signature *types.Signature |
| 364 |
pos token.Pos |
| 365 |
syntax ast.Node |
| 366 |
|
| 367 |
// source information |
| 368 |
Synthetic string // provenance of synthetic function; "" for true source functions |
| 369 |
info *types.Info // type annotations (if syntax != nil) |
| 370 |
goversion string // Go version of syntax (NB: init is special) |
| 371 |
|
| 372 |
parent *Function // enclosing function if anon; nil if global |
| 373 |
Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error) |
| 374 |
Prog *Program // enclosing program |
| 375 |
|
| 376 |
exprToValue map[ast.Expr]struct { |
| 377 |
v Value |
| 378 |
isAddr bool |
| 379 |
} |
| 380 |
|
| 381 |
buildshared *task // wait for a shared function to be done building (may be nil if <=1 builder ever needs to wait) |
| 382 |
|
| 383 |
// These fields are populated only when the function body is built: |
| 384 |
|
| 385 |
Params []*Parameter // function parameters; for methods, includes receiver |
| 386 |
FreeVars []*FreeVar // free variables whose values must be supplied by closure |
| 387 |
Locals []*Alloc // frame-allocated variables of this function |
| 388 |
Blocks []*BasicBlock // basic blocks of the function; nil => external |
| 389 |
Recover *BasicBlock // optional; control transfers here after recovered panic |
| 390 |
AnonFuncs []*Function // anonymous functions (from FuncLit, RangeStmt) directly beneath this one |
| 391 |
referrers []Instruction // referring instructions (iff Parent() != nil) |
| 392 |
anonIdx int32 // position of a nested function in parent's AnonFuncs. fn.Parent()!=nil => fn.Parent().AnonFunc[fn.anonIdx] == fn. |
| 393 |
|
| 394 |
SCCs []*SCC // SCC DAG in reverse topological order |
| 395 |
|
| 396 |
recvtypeparams *types.TypeParamList // receiver type parameters of this function. recvtypeparams.Len() > 0 => method on generic or instance of generic type |
| 397 |
recvtypeargs []types.Type // type arguments that instantiated recvtypeparams. len(recvtypeargs) > 0 => method on instance of generic type |
| 398 |
typeparams *types.TypeParamList // type parameters of this function. typeparams.Len() > 0 => generic or instance of generic function or method |
| 399 |
typeargs []types.Type // type arguments that instantiated typeparams. len(typeargs) > 0 => instance of generic function or method |
| 400 |
topLevelOrigin *Function // the origin function if this is an instance of a source function. nil if Parent()!=nil. |
| 401 |
generic *generic // instances of this function, if generic |
| 402 |
|
| 403 |
// The following fields are cleared after building. |
| 404 |
build buildFunc // algorithm to build function body (nil => built) |
| 405 |
currentBlock *BasicBlock // where to emit code |
| 406 |
vars map[*types.Var]Value // addresses of local variables |
| 407 |
results []*Alloc // result allocations of the current function |
| 408 |
returnVars []*types.Var // variables for a return statement. Either results or for range-over-func a parent's results |
| 409 |
targets *targets // linked stack of branch targets |
| 410 |
lblocks map[*types.Label]*lblock // labelled blocks |
| 411 |
subst *subster // type parameter substitutions (if non-nil) |
| 412 |
jump *types.Var // synthetic variable for the yield state (non-nil => range-over-func) |
| 413 |
deferstack *types.Var // synthetic variable holding enclosing ssa:deferstack() |
| 414 |
source *Function // nearest enclosing source function |
| 415 |
exits []*exit // exits of the function that need to be resolved |
| 416 |
uniq int64 // source of unique ints within the source tree while building |
| 417 |
liftableBlockMap BlockMap[liftableBlockDesc] |
| 418 |
|
| 419 |
blocksets [4]BlockSet |
| 420 |
} |
| 421 |
|
| 422 |
// BasicBlock represents an IR basic block. |
| 423 |
// |
| 424 |
// The final element of Instrs is always an explicit transfer of |
| 425 |
// control (If, Jump, Return, Panic, or Unreachable). |
| 426 |
// |
| 427 |
// A block may contain no Instructions only if it is unreachable, |
| 428 |
// i.e., Preds is nil. Empty blocks are typically pruned. |
| 429 |
// |
| 430 |
// BasicBlocks and their Preds/Succs relation form a (possibly cyclic) |
| 431 |
// graph independent of the IR Value graph: the control-flow graph or |
| 432 |
// CFG. It is illegal for multiple edges to exist between the same |
| 433 |
// pair of blocks. |
| 434 |
// |
| 435 |
// Each BasicBlock is also a node in the dominator tree of the CFG. |
| 436 |
// The tree may be navigated using Idom()/Dominees() and queried using |
| 437 |
// Dominates(). |
| 438 |
// |
| 439 |
// The order of Preds and Succs is significant (to Phi and If |
| 440 |
// instructions, respectively). |
| 441 |
type BasicBlock struct { |
| 442 |
Index int // index of this block within Parent().Blocks |
| 443 |
Comment string // optional label; no semantic significance |
| 444 |
parent *Function // parent function |
| 445 |
Instrs []Instruction // instructions in order |
| 446 |
Preds, Succs []*BasicBlock // predecessors and successors |
| 447 |
SCC *SCC // strongly connected component |
| 448 |
succs2 [2]*BasicBlock // initial space for Succs |
| 449 |
dom domInfo // dominator tree info |
| 450 |
gaps int // number of nil Instrs (transient) |
| 451 |
rundefers int // number of rundefers (transient) |
| 452 |
} |
| 453 |
|
| 454 |
type SCC struct { |
| 455 |
Index int |
| 456 |
Blocks []*BasicBlock |
| 457 |
Preds, Succs []*SCC |
| 458 |
reachable big.Int |
| 459 |
} |
| 460 |
|
| 461 |
// Pure values ---------------------------------------- |
| 462 |
|
| 463 |
// A FreeVar represents a free variable of the function to which it |
| 464 |
// belongs. |
| 465 |
// |
| 466 |
// FreeVars are used to implement anonymous functions, whose free |
| 467 |
// variables are lexically captured in a closure formed by |
| 468 |
// MakeClosure. The value of such a free var is an Alloc or another |
| 469 |
// FreeVar and is considered a potentially escaping heap address, with |
| 470 |
// pointer type. |
| 471 |
// |
| 472 |
// FreeVars are also used to implement bound method closures. Such a |
| 473 |
// free var represents the receiver value and may be of any type that |
| 474 |
// has concrete methods. |
| 475 |
// |
| 476 |
// Pos() returns the position of the value that was captured, which |
| 477 |
// belongs to an enclosing function. |
| 478 |
type FreeVar struct { |
| 479 |
node |
| 480 |
|
| 481 |
name string |
| 482 |
typ types.Type |
| 483 |
parent *Function |
| 484 |
referrers []Instruction |
| 485 |
|
| 486 |
// Transiently needed during building. |
| 487 |
outer Value // the Value captured from the enclosing context. |
| 488 |
} |
| 489 |
|
| 490 |
// A Parameter represents an input parameter of a function. |
| 491 |
type Parameter struct { |
| 492 |
node |
| 493 |
|
| 494 |
name string |
| 495 |
object *types.Var // non-nil |
| 496 |
typ types.Type |
| 497 |
parent *Function |
| 498 |
referrers []Instruction |
| 499 |
} |
| 500 |
|
| 501 |
// A Const represents a value known at build time. |
| 502 |
// |
| 503 |
// Consts include true constants of boolean, numeric, and string types, as |
| 504 |
// defined by the Go spec; these are represented by a non-nil Value field. |
| 505 |
// |
| 506 |
// Consts also include the "zero" value of any type, of which the nil values |
| 507 |
// of various pointer-like types are a special case; these are represented |
| 508 |
// by a nil Value field. |
| 509 |
// |
| 510 |
// Example printed form: |
| 511 |
// |
| 512 |
// 42:int |
| 513 |
// "hello":untyped string |
| 514 |
// 3+4i:MyComplex |
| 515 |
// nil:*int |
| 516 |
// nil:[]string |
| 517 |
// [3]int{}:[3]int |
| 518 |
// struct{x string}{}:struct{x string} |
| 519 |
// 0:interface{int|int64} |
| 520 |
// nil:interface{bool|int} // no go/constant representation |
| 521 |
type Const struct { |
| 522 |
node |
| 523 |
|
| 524 |
typ types.Type |
| 525 |
Value constant.Value |
| 526 |
} |
| 527 |
|
| 528 |
type AggregateConst struct { |
| 529 |
node |
| 530 |
|
| 531 |
typ types.Type |
| 532 |
Values []Value |
| 533 |
} |
| 534 |
|
| 535 |
type CompositeValue struct { |
| 536 |
register |
| 537 |
|
| 538 |
// Bitmap records which elements were explicitly provided. For example, [4]byte{2: x} would have a bitmap of 0010. |
| 539 |
Bitmap big.Int |
| 540 |
// The number of bits set in Bitmap |
| 541 |
// TODO(dh): remove NumSet. we can just query the bitmap. |
| 542 |
NumSet int |
| 543 |
// Dense list of values in the composite literal. Omitted elements are filled in with zero values. |
| 544 |
Values []Value |
| 545 |
} |
| 546 |
|
| 547 |
type Constant interface { |
| 548 |
Value |
| 549 |
aConstant() |
| 550 |
RelString(*types.Package) string |
| 551 |
} |
| 552 |
|
| 553 |
func (*Const) aConstant() {} |
| 554 |
func (*AggregateConst) aConstant() {} |
| 555 |
|
| 556 |
// A Global is a named Value holding the address of a package-level |
| 557 |
// variable. |
| 558 |
// |
| 559 |
// Pos() returns the position of the ast.ValueSpec.Names[*] |
| 560 |
// identifier. |
| 561 |
type Global struct { |
| 562 |
node |
| 563 |
|
| 564 |
name string |
| 565 |
object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard |
| 566 |
typ types.Type |
| 567 |
|
| 568 |
Pkg *Package |
| 569 |
} |
| 570 |
|
| 571 |
// A Builtin represents a specific use of a built-in function, e.g. len. |
| 572 |
// |
| 573 |
// Builtins are immutable values. Builtins do not have addresses. |
| 574 |
// Builtins can only appear in CallCommon.Func. |
| 575 |
// |
| 576 |
// Name() indicates the function: one of the built-in functions from the |
| 577 |
// Go spec (excluding "make" and "new") or one of these ir-defined |
| 578 |
// intrinsics: |
| 579 |
// |
| 580 |
// // wrapnilchk returns ptr if non-nil, panics otherwise. |
| 581 |
// // (For use in indirection wrappers.) |
| 582 |
// func ir:wrapnilchk(ptr *T, recvType, methodName string) *T |
| 583 |
// |
| 584 |
// // noreturnWasPanic returns true if the previously called |
| 585 |
// // function panicked, false if it exited the process. |
| 586 |
// func ir:noreturnWasPanic() bool |
| 587 |
// |
| 588 |
// Object() returns a *types.Builtin for built-ins defined by the spec, |
| 589 |
// nil for others. |
| 590 |
// |
| 591 |
// Type() returns a *types.Signature representing the effective |
| 592 |
// signature of the built-in for this call. |
| 593 |
type Builtin struct { |
| 594 |
node |
| 595 |
|
| 596 |
name string |
| 597 |
sig *types.Signature |
| 598 |
} |
| 599 |
|
| 600 |
// Value-defining instructions ---------------------------------------- |
| 601 |
|
| 602 |
// The Alloc instruction reserves space for a variable of the given type, |
| 603 |
// zero-initializes it, and yields its address. |
| 604 |
// |
| 605 |
// Alloc values are always addresses, and have pointer types, so the |
| 606 |
// type of the allocated variable is actually |
| 607 |
// Type().Underlying().(*types.Pointer).Elem(). |
| 608 |
// |
| 609 |
// If Heap is false, Alloc zero-initializes the same local variable in |
| 610 |
// the call frame and returns its address; in this case the Alloc must |
| 611 |
// be present in Function.Locals. We call this a "local" alloc. |
| 612 |
// |
| 613 |
// If Heap is true, Alloc allocates a new zero-initialized variable |
| 614 |
// each time the instruction is executed. We call this a "new" alloc. |
| 615 |
// |
| 616 |
// When Alloc is applied to a channel, map or slice type, it returns |
| 617 |
// the address of an uninitialized (nil) reference of that kind; store |
| 618 |
// the result of MakeSlice, MakeMap or MakeChan in that location to |
| 619 |
// instantiate these types. |
| 620 |
// |
| 621 |
// Pos() returns the ast.CompositeLit.Lbrace for a composite literal, |
| 622 |
// or the ast.CallExpr.Rparen for a call to new() or for a call that |
| 623 |
// allocates a varargs slice. |
| 624 |
// |
| 625 |
// Example printed form: |
| 626 |
// |
| 627 |
// t0 = local int |
| 628 |
// t1 = new int |
| 629 |
type Alloc struct { |
| 630 |
register |
| 631 |
Heap bool |
| 632 |
index int // dense numbering; for lifting |
| 633 |
} |
| 634 |
|
| 635 |
// The Phi instruction represents an SSA φ-node, which combines values |
| 636 |
// that differ across incoming control-flow edges and yields a new |
| 637 |
// value. Within a block, all φ-nodes must appear before all non-φ |
| 638 |
// nodes. |
| 639 |
// |
| 640 |
// Pos() returns the position of the && or || for short-circuit |
| 641 |
// control-flow joins, or that of the *Alloc for φ-nodes inserted |
| 642 |
// during SSA renaming. |
| 643 |
// |
| 644 |
// Example printed form: |
| 645 |
// |
| 646 |
// t2 = phi [0: t0, 1: t1] |
| 647 |
type Phi struct { |
| 648 |
register |
| 649 |
Edges []Value // Edges[i] is value for Block().Preds[i] |
| 650 |
|
| 651 |
live bool // used during lifting |
| 652 |
} |
| 653 |
|
| 654 |
// The Call instruction represents a function or method call. |
| 655 |
// |
| 656 |
// The Call instruction yields the function result if there is exactly |
| 657 |
// one. Otherwise it returns a tuple, the components of which are |
| 658 |
// accessed via Extract. |
| 659 |
// |
| 660 |
// See CallCommon for generic function call documentation. |
| 661 |
// |
| 662 |
// Pos() returns the ast.CallExpr.Lparen, if explicit in the source. |
| 663 |
// |
| 664 |
// Example printed form: |
| 665 |
// |
| 666 |
// t2 = println(t0, t1) |
| 667 |
// t4 = t3() |
| 668 |
// t7 = invoke t5.Println(...t6) |
| 669 |
type Call struct { |
| 670 |
register |
| 671 |
Call CallCommon |
| 672 |
} |
| 673 |
|
| 674 |
// The BinOp instruction yields the result of binary operation X Op Y. |
| 675 |
// |
| 676 |
// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source. |
| 677 |
// |
| 678 |
// Example printed form: |
| 679 |
// |
| 680 |
// t1 = t0 + 1:int |
| 681 |
type BinOp struct { |
| 682 |
register |
| 683 |
// One of: |
| 684 |
// ADD SUB MUL QUO REM + - * / % |
| 685 |
// AND OR XOR SHL SHR AND_NOT & | ^ << >> &^ |
| 686 |
// EQL NEQ LSS LEQ GTR GEQ == != < <= < >= |
| 687 |
Op token.Token |
| 688 |
X, Y Value |
| 689 |
} |
| 690 |
|
| 691 |
// The UnOp instruction yields the result of Op X. |
| 692 |
// XOR is bitwise complement. |
| 693 |
// SUB is negation. |
| 694 |
// NOT is logical negation. |
| 695 |
// |
| 696 |
// Example printed form: |
| 697 |
// |
| 698 |
// t0 = ^x |
| 699 |
type UnOp struct { |
| 700 |
register |
| 701 |
Op token.Token // One of: NOT SUB XOR ! - ^ |
| 702 |
X Value |
| 703 |
} |
| 704 |
|
| 705 |
// The Load instruction loads a value from a memory address. |
| 706 |
// |
| 707 |
// For implicit memory loads, Pos() returns the position of the |
| 708 |
// most closely associated source-level construct; the details are not |
| 709 |
// specified. |
| 710 |
// |
| 711 |
// Example printed form: |
| 712 |
// |
| 713 |
// t2 = Load <int> t1 |
| 714 |
type Load struct { |
| 715 |
register |
| 716 |
X Value |
| 717 |
} |
| 718 |
|
| 719 |
// The ChangeType instruction applies to X a value-preserving type |
| 720 |
// change to Type(). |
| 721 |
// |
| 722 |
// Type changes are permitted: |
| 723 |
// - between a named type and its underlying type. |
| 724 |
// - between two named types of the same underlying type. |
| 725 |
// - between (possibly named) pointers to identical base types. |
| 726 |
// - from a bidirectional channel to a read- or write-channel, |
| 727 |
// optionally adding/removing a name. |
| 728 |
// - between a type (t) and an instance of the type (tσ), i.e. |
| 729 |
// Type() == σ(X.Type()) (or X.Type()== σ(Type())) where |
| 730 |
// σ is the type substitution of Parent().TypeParams by |
| 731 |
// Parent().TypeArgs. |
| 732 |
// |
| 733 |
// This operation cannot fail dynamically. |
| 734 |
// |
| 735 |
// Type changes may to be to or from a type parameter (or both). All |
| 736 |
// types in the type set of X.Type() have a value-preserving type |
| 737 |
// change to all types in the type set of Type(). |
| 738 |
// |
| 739 |
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 740 |
// from an explicit conversion in the source. |
| 741 |
// |
| 742 |
// Example printed form: |
| 743 |
// |
| 744 |
// t1 = changetype *int <- IntPtr (t0) |
| 745 |
type ChangeType struct { |
| 746 |
register |
| 747 |
X Value |
| 748 |
} |
| 749 |
|
| 750 |
// The Convert instruction yields the conversion of value X to type |
| 751 |
// Type(). One or both of those types is basic (but possibly named). |
| 752 |
// |
| 753 |
// A conversion may change the value and representation of its operand. |
| 754 |
// Conversions are permitted: |
| 755 |
// - between real numeric types. |
| 756 |
// - between complex numeric types. |
| 757 |
// - between string and []byte or []rune. |
| 758 |
// - between pointers and unsafe.Pointer. |
| 759 |
// - between unsafe.Pointer and uintptr. |
| 760 |
// - from (Unicode) integer to (UTF-8) string. |
| 761 |
// |
| 762 |
// A conversion may imply a type name change also. |
| 763 |
// |
| 764 |
// Conversions may to be to or from a type parameter. All types in |
| 765 |
// the type set of X.Type() can be converted to all types in the type |
| 766 |
// set of Type(). |
| 767 |
// |
| 768 |
// This operation cannot fail dynamically. |
| 769 |
// |
| 770 |
// Conversions of untyped string/number/bool constants to a specific |
| 771 |
// representation are eliminated during IR construction. |
| 772 |
// |
| 773 |
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 774 |
// from an explicit conversion in the source. |
| 775 |
// |
| 776 |
// Example printed form: |
| 777 |
// |
| 778 |
// t1 = convert []byte <- string (t0) |
| 779 |
type Convert struct { |
| 780 |
register |
| 781 |
X Value |
| 782 |
} |
| 783 |
|
| 784 |
// The MultiConvert instruction yields the conversion of value X to type |
| 785 |
// Type(). Either X.Type() or Type() must be a type parameter. Each |
| 786 |
// type in the type set of X.Type() can be converted to each type in the |
| 787 |
// type set of Type(). |
| 788 |
// |
| 789 |
// See the documentation for Convert, ChangeType, SliceToArray, and SliceToArrayPointer |
| 790 |
// for the conversions that are permitted. |
| 791 |
// |
| 792 |
// This operation can fail dynamically (see SliceToArrayPointer). |
| 793 |
// |
| 794 |
// Example printed form: |
| 795 |
// |
| 796 |
// t1 = multiconvert D <- S (t0) [*[2]rune <- []rune | string <- []rune] |
| 797 |
type MultiConvert struct { |
| 798 |
register |
| 799 |
X Value |
| 800 |
from, to types.Type |
| 801 |
} |
| 802 |
|
| 803 |
// ChangeInterface constructs a value of one interface type from a |
| 804 |
// value of another interface type known to be assignable to it. |
| 805 |
// This operation cannot fail. |
| 806 |
// |
| 807 |
// Pos() returns the ast.CallExpr.Lparen if the instruction arose from |
| 808 |
// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the |
| 809 |
// instruction arose from an explicit e.(T) operation; or token.NoPos |
| 810 |
// otherwise. |
| 811 |
// |
| 812 |
// Example printed form: |
| 813 |
// |
| 814 |
// t1 = change interface interface{} <- I (t0) |
| 815 |
type ChangeInterface struct { |
| 816 |
register |
| 817 |
X Value |
| 818 |
} |
| 819 |
|
| 820 |
// The SliceToArrayPointer instruction yields the conversion of slice X to |
| 821 |
// array pointer. |
| 822 |
// |
| 823 |
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 824 |
// from an explicit conversion in the source. |
| 825 |
// |
| 826 |
// Conversion may to be to or from a type parameter. All types in |
| 827 |
// the type set of X.Type() must be a slice types that can be converted to |
| 828 |
// all types in the type set of Type() which must all be pointer to array |
| 829 |
// types. |
| 830 |
// |
| 831 |
// This operation can fail dynamically if the length of the slice is less |
| 832 |
// than the length of the array. |
| 833 |
// |
| 834 |
// Example printed form: |
| 835 |
// |
| 836 |
// t1 = slice to array pointer *[4]byte <- []byte (t0) |
| 837 |
type SliceToArrayPointer struct { |
| 838 |
register |
| 839 |
X Value |
| 840 |
} |
| 841 |
|
| 842 |
// The SliceToArray instruction yields the conversion of slice X to |
| 843 |
// array. |
| 844 |
// |
| 845 |
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 846 |
// from an explicit conversion in the source. |
| 847 |
// |
| 848 |
// Example printed form: |
| 849 |
// |
| 850 |
// t2 = SliceToArray <[4]byte> t1 |
| 851 |
type SliceToArray struct { |
| 852 |
register |
| 853 |
X Value |
| 854 |
} |
| 855 |
|
| 856 |
// MakeInterface constructs an instance of an interface type from a |
| 857 |
// value of a concrete type. |
| 858 |
// |
| 859 |
// Use Program.MethodSets.MethodSet(X.Type()) to find the method-set |
| 860 |
// of X, and Program.MethodValue(m) to find the implementation of a method. |
| 861 |
// |
| 862 |
// To construct the zero value of an interface type T, use: |
| 863 |
// |
| 864 |
// NewConst(constant.MakeNil(), T, pos) |
| 865 |
// |
| 866 |
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose |
| 867 |
// from an explicit conversion in the source. |
| 868 |
// |
| 869 |
// Example printed form: |
| 870 |
// |
| 871 |
// t1 = make interface{} <- int (42:int) |
| 872 |
// t1 = make Stringer <- t0 |
| 873 |
type MakeInterface struct { |
| 874 |
register |
| 875 |
X Value |
| 876 |
} |
| 877 |
|
| 878 |
// The MakeClosure instruction yields a closure value whose code is |
| 879 |
// Fn and whose free variables' values are supplied by Bindings. |
| 880 |
// |
| 881 |
// Type() returns a (possibly named) *types.Signature. |
| 882 |
// |
| 883 |
// Pos() returns the ast.FuncLit.Type.Func for a function literal |
| 884 |
// closure or the ast.SelectorExpr.Sel for a bound method closure. |
| 885 |
// |
| 886 |
// Example printed form: |
| 887 |
// |
| 888 |
// t0 = make closure [email protected] [x y z] |
| 889 |
// t1 = make closure bound$(main.I).add [i] |
| 890 |
type MakeClosure struct { |
| 891 |
register |
| 892 |
Fn Value // always a *Function |
| 893 |
Bindings []Value // values for each free variable in Fn.FreeVars |
| 894 |
} |
| 895 |
|
| 896 |
// The MakeMap instruction creates a new hash-table-based map object |
| 897 |
// and yields a value of kind map. |
| 898 |
// |
| 899 |
// Type() returns a (possibly named) *types.Map. |
| 900 |
// |
| 901 |
// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or |
| 902 |
// the ast.CompositeLit.Lbrack if created by a literal. |
| 903 |
// |
| 904 |
// Example printed form: |
| 905 |
// |
| 906 |
// t1 = make map[string]int t0 |
| 907 |
// t1 = make StringIntMap t0 |
| 908 |
type MakeMap struct { |
| 909 |
register |
| 910 |
Reserve Value // initial space reservation; nil => default |
| 911 |
} |
| 912 |
|
| 913 |
// The MakeChan instruction creates a new channel object and yields a |
| 914 |
// value of kind chan. |
| 915 |
// |
| 916 |
// Type() returns a (possibly named) *types.Chan. |
| 917 |
// |
| 918 |
// Pos() returns the ast.CallExpr.Lparen for the make(chan) that |
| 919 |
// created it. |
| 920 |
// |
| 921 |
// Example printed form: |
| 922 |
// |
| 923 |
// t1 = make chan int t0 |
| 924 |
// t1 = make IntChan t0 |
| 925 |
type MakeChan struct { |
| 926 |
register |
| 927 |
Size Value // int; size of buffer; zero => synchronous. |
| 928 |
} |
| 929 |
|
| 930 |
// The MakeSlice instruction yields a slice of length Len backed by a |
| 931 |
// newly allocated array of length Cap. |
| 932 |
// |
| 933 |
// Both Len and Cap must be non-nil Values of integer type. |
| 934 |
// |
| 935 |
// (Alloc(types.Array) followed by Slice will not suffice because |
| 936 |
// Alloc can only create arrays of constant length.) |
| 937 |
// |
| 938 |
// Type() returns a (possibly named) *types.Slice. |
| 939 |
// |
| 940 |
// Pos() returns the ast.CallExpr.Lparen for the make([]T) that |
| 941 |
// created it. |
| 942 |
// |
| 943 |
// Example printed form: |
| 944 |
// |
| 945 |
// t2 = make []string t1 t0 |
| 946 |
// t2 = make StringSlice t1 t0 |
| 947 |
type MakeSlice struct { |
| 948 |
register |
| 949 |
Len Value |
| 950 |
Cap Value |
| 951 |
} |
| 952 |
|
| 953 |
// The Slice instruction yields a slice of an existing string, slice |
| 954 |
// or *array X between optional integer bounds Low and High. |
| 955 |
// |
| 956 |
// Dynamically, this instruction panics if X evaluates to a nil *array |
| 957 |
// pointer. |
| 958 |
// |
| 959 |
// Type() returns string if the type of X was string, otherwise a |
| 960 |
// *types.Slice with the same element type as X. |
| 961 |
// |
| 962 |
// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice |
| 963 |
// operation, the ast.CompositeLit.Lbrace if created by a literal, or |
| 964 |
// NoPos if not explicit in the source (e.g. a variadic argument slice). |
| 965 |
// |
| 966 |
// Example printed form: |
| 967 |
// |
| 968 |
// t2 = slice t0[t1:] |
| 969 |
type Slice struct { |
| 970 |
register |
| 971 |
X Value // slice, string, or *array |
| 972 |
Low, High, Max Value // each may be nil |
| 973 |
} |
| 974 |
|
| 975 |
// The FieldAddr instruction yields the address of Field of *struct X. |
| 976 |
// |
| 977 |
// The field is identified by its index within the field list of the |
| 978 |
// struct type of X. |
| 979 |
// |
| 980 |
// Dynamically, this instruction panics if X evaluates to a nil |
| 981 |
// pointer. |
| 982 |
// |
| 983 |
// Type() returns a (possibly named) *types.Pointer. |
| 984 |
// |
| 985 |
// Pos() returns the position of the ast.SelectorExpr.Sel for the |
| 986 |
// field, if explicit in the source. For implicit selections, returns |
| 987 |
// the position of the inducing explicit selection. If produced for a |
| 988 |
// struct literal S{f: e}, it returns the position of the colon; for |
| 989 |
// S{e} it returns the start of expression e. |
| 990 |
// |
| 991 |
// Example printed form: |
| 992 |
// |
| 993 |
// t1 = &t0.name [#1] |
| 994 |
type FieldAddr struct { |
| 995 |
register |
| 996 |
X Value // *struct |
| 997 |
Field int // index into CoreType(CoreType(X.Type()).(*types.Pointer).Elem()).(*types.Struct).Fields |
| 998 |
} |
| 999 |
|
| 1000 |
// The Field instruction yields the Field of struct X. |
| 1001 |
// |
| 1002 |
// The field is identified by its index within the field list of the |
| 1003 |
// struct type of X; by using numeric indices we avoid ambiguity of |
| 1004 |
// package-local identifiers and permit compact representations. |
| 1005 |
// |
| 1006 |
// Pos() returns the position of the ast.SelectorExpr.Sel for the |
| 1007 |
// field, if explicit in the source. For implicit selections, returns |
| 1008 |
// the position of the inducing explicit selection. |
| 1009 |
// |
| 1010 |
// Example printed form: |
| 1011 |
// |
| 1012 |
// t1 = t0.name [#1] |
| 1013 |
type Field struct { |
| 1014 |
register |
| 1015 |
X Value // struct |
| 1016 |
Field int // index into CoreType(X.Type()).(*types.Struct).Fields |
| 1017 |
} |
| 1018 |
|
| 1019 |
// The IndexAddr instruction yields the address of the element at |
| 1020 |
// index Index of collection X. Index is an integer expression. |
| 1021 |
// |
| 1022 |
// The elements of maps and strings are not addressable; use Lookup (map), |
| 1023 |
// Index (string), or MapUpdate instead. |
| 1024 |
// |
| 1025 |
// Dynamically, this instruction panics if X evaluates to a nil *array |
| 1026 |
// pointer. |
| 1027 |
// |
| 1028 |
// Type() returns a (possibly named) *types.Pointer. |
| 1029 |
// |
| 1030 |
// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if |
| 1031 |
// explicit in the source. |
| 1032 |
// |
| 1033 |
// Example printed form: |
| 1034 |
// |
| 1035 |
// t2 = &t0[t1] |
| 1036 |
type IndexAddr struct { |
| 1037 |
register |
| 1038 |
X Value // *array, slice or type parameter with types array, *array, or slice. |
| 1039 |
Index Value // numeric index |
| 1040 |
} |
| 1041 |
|
| 1042 |
// The Index instruction yields element Index of collection X, an array, |
| 1043 |
// string or type parameter containing an array, a string, a pointer to an, |
| 1044 |
// array or a slice. |
| 1045 |
// |
| 1046 |
// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if |
| 1047 |
// explicit in the source. |
| 1048 |
// |
| 1049 |
// Example printed form: |
| 1050 |
// |
| 1051 |
// t2 = t0[t1] |
| 1052 |
type Index struct { |
| 1053 |
register |
| 1054 |
X Value // array, string or type parameter with types array, *array, slice, or string. |
| 1055 |
Index Value // integer index |
| 1056 |
} |
| 1057 |
|
| 1058 |
// The MapLookup instruction yields element Index of collection X, a map. |
| 1059 |
// Index is the appropriate key type. |
| 1060 |
// |
| 1061 |
// If CommaOk, the result is a 2-tuple of the value above and a |
| 1062 |
// boolean indicating the result of a map membership test for the key. |
| 1063 |
// The components of the tuple are accessed using Extract. |
| 1064 |
// |
| 1065 |
// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source. |
| 1066 |
// |
| 1067 |
// Example printed form: |
| 1068 |
// |
| 1069 |
// t2 = t0[t1] |
| 1070 |
// t5 = t3[t4],ok |
| 1071 |
type MapLookup struct { |
| 1072 |
register |
| 1073 |
X Value // map |
| 1074 |
Index Value // key-typed index |
| 1075 |
CommaOk bool // return a value,ok pair |
| 1076 |
} |
| 1077 |
|
| 1078 |
// The StringLookup instruction yields element Index of collection X, a string. |
| 1079 |
// Index is an integer expression. |
| 1080 |
// |
| 1081 |
// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source. |
| 1082 |
// |
| 1083 |
// Example printed form: |
| 1084 |
// |
| 1085 |
// t3 = StringLookup <uint8> t2 t1 |
| 1086 |
type StringLookup struct { |
| 1087 |
register |
| 1088 |
X Value // string |
| 1089 |
Index Value // numeric index |
| 1090 |
} |
| 1091 |
|
| 1092 |
// SelectState is a helper for Select. |
| 1093 |
// It represents one goal state and its corresponding communication. |
| 1094 |
type SelectState struct { |
| 1095 |
Dir types.ChanDir // direction of case (SendOnly or RecvOnly) |
| 1096 |
Chan Value // channel to use (for send or receive) |
| 1097 |
Send Value // value to send (for send) |
| 1098 |
Pos token.Pos // position of token.ARROW |
| 1099 |
DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode] |
| 1100 |
} |
| 1101 |
|
| 1102 |
// The Select instruction tests whether (or blocks until) one |
| 1103 |
// of the specified sent or received states is entered. |
| 1104 |
// |
| 1105 |
// Let n be the number of States for which Dir==RECV and Tᵢ (0 ≤ i < n) |
| 1106 |
// be the element type of each such state's Chan. |
| 1107 |
// Select returns an n+2-tuple |
| 1108 |
// |
| 1109 |
// (index int, recvOk bool, râ‚€ Tâ‚€, ... râ‚™-1 Tâ‚™-1) |
| 1110 |
// |
| 1111 |
// The tuple's components, described below, must be accessed via the |
| 1112 |
// Extract instruction. |
| 1113 |
// |
| 1114 |
// If Blocking, select waits until exactly one state holds, i.e. a |
| 1115 |
// channel becomes ready for the designated operation of sending or |
| 1116 |
// receiving; select chooses one among the ready states |
| 1117 |
// pseudorandomly, performs the send or receive operation, and sets |
| 1118 |
// 'index' to the index of the chosen channel. |
| 1119 |
// |
| 1120 |
// If !Blocking, select doesn't block if no states hold; instead it |
| 1121 |
// returns immediately with index equal to -1. |
| 1122 |
// |
| 1123 |
// If the chosen channel was used for a receive, the ráµ¢ component is |
| 1124 |
// set to the received value, where i is the index of that state among |
| 1125 |
// all n receive states; otherwise ráµ¢ has the zero value of type Táµ¢. |
| 1126 |
// Note that the receive index i is not the same as the state |
| 1127 |
// index index. |
| 1128 |
// |
| 1129 |
// The second component of the triple, recvOk, is a boolean whose value |
| 1130 |
// is true iff the selected operation was a receive and the receive |
| 1131 |
// successfully yielded a value. |
| 1132 |
// |
| 1133 |
// Pos() returns the ast.SelectStmt.Select. |
| 1134 |
// |
| 1135 |
// Example printed form: |
| 1136 |
// |
| 1137 |
// t3 = select nonblocking [<-t0, t1<-t2] |
| 1138 |
// t4 = select blocking [] |
| 1139 |
type Select struct { |
| 1140 |
register |
| 1141 |
States []*SelectState |
| 1142 |
Blocking bool |
| 1143 |
} |
| 1144 |
|
| 1145 |
// The Range instruction yields an iterator over the domain and range |
| 1146 |
// of X, which must be a string or map. |
| 1147 |
// |
| 1148 |
// Elements are accessed via Next. |
| 1149 |
// |
| 1150 |
// Type() returns an opaque and degenerate "rangeIter" type. |
| 1151 |
// |
| 1152 |
// Pos() returns the ast.RangeStmt.For. |
| 1153 |
// |
| 1154 |
// Example printed form: |
| 1155 |
// |
| 1156 |
// t0 = range "hello":string |
| 1157 |
type Range struct { |
| 1158 |
register |
| 1159 |
X Value // string or map |
| 1160 |
} |
| 1161 |
|
| 1162 |
// The Next instruction reads and advances the (map or string) |
| 1163 |
// iterator Iter and returns a 3-tuple value (ok, k, v). If the |
| 1164 |
// iterator is not exhausted, ok is true and k and v are the next |
| 1165 |
// elements of the domain and range, respectively. Otherwise ok is |
| 1166 |
// false and k and v are undefined. |
| 1167 |
// |
| 1168 |
// Components of the tuple are accessed using Extract. |
| 1169 |
// |
| 1170 |
// The IsString field distinguishes iterators over strings from those |
| 1171 |
// over maps, as the Type() alone is insufficient: consider |
| 1172 |
// map[int]rune. |
| 1173 |
// |
| 1174 |
// Type() returns a *types.Tuple for the triple (ok, k, v). |
| 1175 |
// The types of k and/or v may be types.Invalid. |
| 1176 |
// |
| 1177 |
// Example printed form: |
| 1178 |
// |
| 1179 |
// t1 = next t0 |
| 1180 |
type Next struct { |
| 1181 |
register |
| 1182 |
Iter Value |
| 1183 |
IsString bool // true => string iterator; false => map iterator. |
| 1184 |
} |
| 1185 |
|
| 1186 |
// The TypeAssert instruction tests whether interface value X has type |
| 1187 |
// AssertedType. |
| 1188 |
// |
| 1189 |
// If !CommaOk, on success it returns v, the result of the conversion |
| 1190 |
// (defined below); on failure it panics. |
| 1191 |
// |
| 1192 |
// If CommaOk: on success it returns a pair (v, true) where v is the |
| 1193 |
// result of the conversion; on failure it returns (z, false) where z |
| 1194 |
// is AssertedType's zero value. The components of the pair must be |
| 1195 |
// accessed using the Extract instruction. |
| 1196 |
// |
| 1197 |
// If Underlying: tests whether interface value X has the underlying |
| 1198 |
// type AssertedType. |
| 1199 |
// |
| 1200 |
// If AssertedType is a concrete type, TypeAssert checks whether the |
| 1201 |
// dynamic type in interface X is equal to it, and if so, the result |
| 1202 |
// of the conversion is a copy of the value in the interface. |
| 1203 |
// |
| 1204 |
// If AssertedType is an interface, TypeAssert checks whether the |
| 1205 |
// dynamic type of the interface is assignable to it, and if so, the |
| 1206 |
// result of the conversion is a copy of the interface value X. |
| 1207 |
// If AssertedType is a superinterface of X.Type(), the operation will |
| 1208 |
// fail iff the operand is nil. (Contrast with ChangeInterface, which |
| 1209 |
// performs no nil-check.) |
| 1210 |
// |
| 1211 |
// Type() reflects the actual type of the result, possibly a |
| 1212 |
// 2-types.Tuple; AssertedType is the asserted type. |
| 1213 |
// |
| 1214 |
// Depending on the TypeAssert's purpose, Pos may return: |
| 1215 |
// - the ast.CallExpr.Lparen of an explicit T(e) conversion; |
| 1216 |
// - the ast.TypeAssertExpr.Lparen of an explicit e.(T) operation; |
| 1217 |
// - the ast.CaseClause.Case of a case of a type-switch statement; |
| 1218 |
// - the Ident(m).NamePos of an interface method value i.m |
| 1219 |
// (for which TypeAssert may be used to effect the nil check). |
| 1220 |
// |
| 1221 |
// Example printed form: |
| 1222 |
// |
| 1223 |
// t1 = typeassert t0.(int) |
| 1224 |
// t3 = typeassert,ok t2.(T) |
| 1225 |
type TypeAssert struct { |
| 1226 |
register |
| 1227 |
X Value |
| 1228 |
AssertedType types.Type |
| 1229 |
CommaOk bool |
| 1230 |
} |
| 1231 |
|
| 1232 |
// The Extract instruction yields component Index of Tuple. |
| 1233 |
// |
| 1234 |
// This is used to access the results of instructions with multiple |
| 1235 |
// return values, such as Call, TypeAssert, Next, Recv, |
| 1236 |
// MapLookup and others. |
| 1237 |
// |
| 1238 |
// Example printed form: |
| 1239 |
// |
| 1240 |
// t1 = extract t0 #1 |
| 1241 |
type Extract struct { |
| 1242 |
register |
| 1243 |
Tuple Value |
| 1244 |
Index int |
| 1245 |
} |
| 1246 |
|
| 1247 |
// Instructions executed for effect. They do not yield a value. -------------------- |
| 1248 |
|
| 1249 |
// The Jump instruction transfers control to the sole successor of its |
| 1250 |
// owning block. |
| 1251 |
// |
| 1252 |
// A Jump must be the last instruction of its containing BasicBlock. |
| 1253 |
// |
| 1254 |
// Pos() returns NoPos. |
| 1255 |
// |
| 1256 |
// Example printed form: |
| 1257 |
// |
| 1258 |
// jump done |
| 1259 |
type Jump struct { |
| 1260 |
anInstruction |
| 1261 |
} |
| 1262 |
|
| 1263 |
// The Unreachable pseudo-instruction signals that execution cannot |
| 1264 |
// continue after the preceding function call because it terminates |
| 1265 |
// the process. |
| 1266 |
// |
| 1267 |
// An Unreachable instruction must be the last instruction of its |
| 1268 |
// containing BasicBlock, which must have no successors. |
| 1269 |
// |
| 1270 |
// Example printed form: |
| 1271 |
// |
| 1272 |
// Unreachable |
| 1273 |
type Unreachable struct { |
| 1274 |
anInstruction |
| 1275 |
} |
| 1276 |
|
| 1277 |
// The If instruction transfers control to one of the two successors |
| 1278 |
// of its owning block, depending on the boolean Cond: the first if |
| 1279 |
// true, the second if false. |
| 1280 |
// |
| 1281 |
// An If instruction must be the last instruction of its containing |
| 1282 |
// BasicBlock. |
| 1283 |
// |
| 1284 |
// Pos() returns the *ast.IfStmt, if explicit in the source. |
| 1285 |
// |
| 1286 |
// Example printed form: |
| 1287 |
// |
| 1288 |
// if t0 goto done else body |
| 1289 |
type If struct { |
| 1290 |
anInstruction |
| 1291 |
Cond Value |
| 1292 |
} |
| 1293 |
|
| 1294 |
type ConstantSwitch struct { |
| 1295 |
anInstruction |
| 1296 |
Tag Value |
| 1297 |
// Constant branch conditions. A nil Value denotes the (implicit |
| 1298 |
// or explicit) default branch. |
| 1299 |
Conds []Value |
| 1300 |
} |
| 1301 |
|
| 1302 |
type TypeSwitch struct { |
| 1303 |
register |
| 1304 |
Tag Value |
| 1305 |
Conds []types.Type |
| 1306 |
} |
| 1307 |
|
| 1308 |
// The Return instruction returns values and control back to the calling |
| 1309 |
// function. |
| 1310 |
// |
| 1311 |
// len(Results) is always equal to the number of results in the |
| 1312 |
// function's signature. |
| 1313 |
// |
| 1314 |
// If len(Results) > 1, Return returns a tuple value with the specified |
| 1315 |
// components which the caller must access using Extract instructions. |
| 1316 |
// |
| 1317 |
// There is no instruction to return a ready-made tuple like those |
| 1318 |
// returned by a "value,ok"-mode TypeAssert, MapLookup or Recv or |
| 1319 |
// a tail-call to a function with multiple result parameters. |
| 1320 |
// |
| 1321 |
// Return must be the last instruction of its containing BasicBlock. |
| 1322 |
// Such a block has no successors. |
| 1323 |
// |
| 1324 |
// Pos() returns the ast.ReturnStmt.Return, if explicit in the source. |
| 1325 |
// |
| 1326 |
// Example printed form: |
| 1327 |
// |
| 1328 |
// return |
| 1329 |
// return t0, t1 |
| 1330 |
type Return struct { |
| 1331 |
anInstruction |
| 1332 |
Results []Value |
| 1333 |
} |
| 1334 |
|
| 1335 |
// The RunDefers instruction pops and invokes the entire stack of |
| 1336 |
// procedure calls pushed by Defer instructions in this function. |
| 1337 |
// |
| 1338 |
// It is legal to encounter multiple 'rundefers' instructions in a |
| 1339 |
// single control-flow path through a function; this is useful in |
| 1340 |
// the combined init() function, for example. |
| 1341 |
// |
| 1342 |
// Pos() returns NoPos. |
| 1343 |
// |
| 1344 |
// Example printed form: |
| 1345 |
// |
| 1346 |
// rundefers |
| 1347 |
type RunDefers struct { |
| 1348 |
anInstruction |
| 1349 |
} |
| 1350 |
|
| 1351 |
// The Panic instruction initiates a panic with value X. |
| 1352 |
// |
| 1353 |
// A Panic instruction must be the last instruction of its containing |
| 1354 |
// BasicBlock, which must have no successors. |
| 1355 |
// |
| 1356 |
// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction; |
| 1357 |
// they are treated as calls to a built-in function. |
| 1358 |
// |
| 1359 |
// Pos() returns the ast.CallExpr.Lparen if this panic was explicit |
| 1360 |
// in the source. |
| 1361 |
// |
| 1362 |
// Example printed form: |
| 1363 |
// |
| 1364 |
// panic t0 |
| 1365 |
type Panic struct { |
| 1366 |
anInstruction |
| 1367 |
X Value // an interface{} |
| 1368 |
} |
| 1369 |
|
| 1370 |
// The Go instruction creates a new goroutine and calls the specified |
| 1371 |
// function within it. |
| 1372 |
// |
| 1373 |
// See CallCommon for generic function call documentation. |
| 1374 |
// |
| 1375 |
// Pos() returns the ast.GoStmt.Go. |
| 1376 |
// |
| 1377 |
// Example printed form: |
| 1378 |
// |
| 1379 |
// go println(t0, t1) |
| 1380 |
// go t3() |
| 1381 |
// go invoke t5.Println(...t6) |
| 1382 |
type Go struct { |
| 1383 |
anInstruction |
| 1384 |
Call CallCommon |
| 1385 |
} |
| 1386 |
|
| 1387 |
// The Defer instruction pushes the specified call onto a stack of |
| 1388 |
// functions to be called by a RunDefers instruction or by a panic. |
| 1389 |
// |
| 1390 |
// If DeferStack != nil, it indicates the defer list that the defer is |
| 1391 |
// added to. Defer list values come from the Builtin function |
| 1392 |
// ssa:deferstack. Calls to ssa:deferstack() produces the defer stack |
| 1393 |
// of the current function frame. DeferStack allows for deferring into an |
| 1394 |
// alternative function stack than the current function. |
| 1395 |
// |
| 1396 |
// See CallCommon for generic function call documentation. |
| 1397 |
// |
| 1398 |
// Pos() returns the ast.DeferStmt.Defer. |
| 1399 |
// |
| 1400 |
// Example printed form: |
| 1401 |
// |
| 1402 |
// defer println(t0, t1) |
| 1403 |
// defer t3() |
| 1404 |
// defer invoke t5.Println(...t6) |
| 1405 |
type Defer struct { |
| 1406 |
anInstruction |
| 1407 |
Call CallCommon |
| 1408 |
DeferStack Value // stack of deferred functions (from ssa:deferstack() intrinsic) onto which this function is pushed |
| 1409 |
} |
| 1410 |
|
| 1411 |
// The Send instruction sends X on channel Chan. |
| 1412 |
// |
| 1413 |
// Pos() returns the ast.SendStmt.Arrow, if explicit in the source. |
| 1414 |
// |
| 1415 |
// Example printed form: |
| 1416 |
// |
| 1417 |
// send t0 <- t1 |
| 1418 |
type Send struct { |
| 1419 |
anInstruction |
| 1420 |
Chan, X Value |
| 1421 |
} |
| 1422 |
|
| 1423 |
// The Recv instruction receives from channel Chan. |
| 1424 |
// |
| 1425 |
// If CommaOk, the result is a 2-tuple of the value above |
| 1426 |
// and a boolean indicating the success of the receive. The |
| 1427 |
// components of the tuple are accessed using Extract. |
| 1428 |
// |
| 1429 |
// Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source. |
| 1430 |
// For receive operations implicit in ranging over a channel, |
| 1431 |
// Pos() returns the ast.RangeStmt.For. |
| 1432 |
// |
| 1433 |
// Example printed form: |
| 1434 |
// |
| 1435 |
// t2 = Recv <int> t1 |
| 1436 |
// t3 = Recv <(int, bool)> t1 |
| 1437 |
type Recv struct { |
| 1438 |
register |
| 1439 |
Chan Value |
| 1440 |
CommaOk bool |
| 1441 |
} |
| 1442 |
|
| 1443 |
// The Store instruction stores Val at address Addr. |
| 1444 |
// Stores can be of arbitrary types. |
| 1445 |
// |
| 1446 |
// Pos() returns the position of the source-level construct most closely |
| 1447 |
// associated with the memory store operation. |
| 1448 |
// Since implicit memory stores are numerous and varied and depend upon |
| 1449 |
// implementation choices, the details are not specified. |
| 1450 |
// |
| 1451 |
// Example printed form: |
| 1452 |
// |
| 1453 |
// *x = y |
| 1454 |
type Store struct { |
| 1455 |
anInstruction |
| 1456 |
Addr Value |
| 1457 |
Val Value |
| 1458 |
} |
| 1459 |
|
| 1460 |
// The BlankStore instruction is emitted for assignments to the blank |
| 1461 |
// identifier. |
| 1462 |
// |
| 1463 |
// BlankStore is a pseudo-instruction: it has no dynamic effect. |
| 1464 |
// |
| 1465 |
// Pos() returns NoPos. |
| 1466 |
// |
| 1467 |
// Example printed form: |
| 1468 |
// |
| 1469 |
// BlankStore t1 |
| 1470 |
type BlankStore struct { |
| 1471 |
anInstruction |
| 1472 |
Val Value |
| 1473 |
} |
| 1474 |
|
| 1475 |
// The MapUpdate instruction updates the association of Map[Key] to |
| 1476 |
// Value. |
| 1477 |
// |
| 1478 |
// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack, |
| 1479 |
// if explicit in the source. |
| 1480 |
// |
| 1481 |
// Example printed form: |
| 1482 |
// |
| 1483 |
// t0[t1] = t2 |
| 1484 |
type MapUpdate struct { |
| 1485 |
anInstruction |
| 1486 |
Map Value |
| 1487 |
Key Value |
| 1488 |
Value Value |
| 1489 |
} |
| 1490 |
|
| 1491 |
// A debugRef instruction maps a source-level expression Expr to the |
| 1492 |
// IR value X that represents the value (!IsAddr) or address (IsAddr) |
| 1493 |
// of that expression. |
| 1494 |
// |
| 1495 |
// debugRef is a pseudo-instruction: it has no dynamic effect. |
| 1496 |
// |
| 1497 |
// Pos() returns Expr.Pos(), the start position of the source-level |
| 1498 |
// expression. This is not the same as the "designated" token as |
| 1499 |
// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the |
| 1500 |
// position of the ("designated") Lparen token. |
| 1501 |
// |
| 1502 |
// If Expr is an *ast.Ident denoting a var or func, Object() returns |
| 1503 |
// the object; though this information can be obtained from the type |
| 1504 |
// checker, including it here greatly facilitates debugging. |
| 1505 |
// For non-Ident expressions, Object() returns nil. |
| 1506 |
// |
| 1507 |
// DebugRefs are generated only for functions built with debugging |
| 1508 |
// enabled; see Package.SetDebugMode() and the GlobalDebug builder |
| 1509 |
// mode flag. |
| 1510 |
// |
| 1511 |
// DebugRefs are not emitted for ast.Idents referring to constants or |
| 1512 |
// predeclared identifiers, since they are trivial and numerous. |
| 1513 |
// Nor are they emitted for ast.ParenExprs. |
| 1514 |
// |
| 1515 |
// (By representing these as instructions, rather than out-of-band, |
| 1516 |
// consistency is maintained during transformation passes by the |
| 1517 |
// ordinary SSA renaming machinery.) |
| 1518 |
// |
| 1519 |
// Example printed form: |
| 1520 |
// |
| 1521 |
// ; *ast.CallExpr @ 102:9 is t5 |
| 1522 |
// ; var x float64 @ 109:72 is x |
| 1523 |
// ; address of *ast.CompositeLit @ 216:10 is t0 |
| 1524 |
type debugRef struct { |
| 1525 |
anInstruction |
| 1526 |
Expr ast.Expr // the referring expression (never *ast.ParenExpr) |
| 1527 |
object types.Object // the identity of the source var/func |
| 1528 |
IsAddr bool // Expr is addressable and X is the address it denotes |
| 1529 |
X Value // the value or address of Expr |
| 1530 |
} |
| 1531 |
|
| 1532 |
// Embeddable mix-ins and helpers for common parts of other structs. ----------- |
| 1533 |
|
| 1534 |
// register is a mix-in embedded by all IR values that are also |
| 1535 |
// instructions, i.e. virtual registers, and provides a uniform |
| 1536 |
// implementation of most of the Value interface: Value.Name() is a |
| 1537 |
// numbered register (e.g. "t0"); the other methods are field accessors. |
| 1538 |
// |
| 1539 |
// Temporary names are automatically assigned to each register on |
| 1540 |
// completion of building a function in IR form. |
| 1541 |
type register struct { |
| 1542 |
anInstruction |
| 1543 |
typ types.Type // type of virtual register |
| 1544 |
referrers []Instruction |
| 1545 |
} |
| 1546 |
|
| 1547 |
type node struct { |
| 1548 |
source ast.Node |
| 1549 |
} |
| 1550 |
|
| 1551 |
func (n *node) setSource(source ast.Node) { n.source = source } |
| 1552 |
func (n *node) Source() ast.Node { return n.source } |
| 1553 |
|
| 1554 |
func (n *node) Pos() token.Pos { |
| 1555 |
if n.source != nil { |
| 1556 |
return n.source.Pos() |
| 1557 |
} |
| 1558 |
return token.NoPos |
| 1559 |
} |
| 1560 |
|
| 1561 |
// anInstruction is a mix-in embedded by all Instructions. |
| 1562 |
// It provides the implementations of the Block and setBlock methods. |
| 1563 |
type anInstruction struct { |
| 1564 |
node |
| 1565 |
id ID |
| 1566 |
block *BasicBlock // the basic block of this instruction |
| 1567 |
comment string |
| 1568 |
} |
| 1569 |
|
| 1570 |
func (instr *anInstruction) setID(id ID) { |
| 1571 |
instr.id = id |
| 1572 |
} |
| 1573 |
|
| 1574 |
func (instr anInstruction) ID() ID { |
| 1575 |
return instr.id |
| 1576 |
} |
| 1577 |
|
| 1578 |
func (instr anInstruction) Comment() string { |
| 1579 |
return instr.comment |
| 1580 |
} |
| 1581 |
|
| 1582 |
// CallCommon is contained by Go, Defer and Call to hold the |
| 1583 |
// common parts of a function or method call. |
| 1584 |
// |
| 1585 |
// Each CallCommon exists in one of two modes, function call and |
| 1586 |
// interface method invocation, or "call" and "invoke" for short. |
| 1587 |
// |
| 1588 |
// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon |
| 1589 |
// represents an ordinary function call of the value in Value, |
| 1590 |
// which may be a *Builtin, a *Function or any other value of kind |
| 1591 |
// 'func'. |
| 1592 |
// |
| 1593 |
// Value may be one of: |
| 1594 |
// |
| 1595 |
// (a) a *Function, indicating a statically dispatched call |
| 1596 |
// to a package-level function, an anonymous function, or |
| 1597 |
// a method of a named type. |
| 1598 |
// (b) a *MakeClosure, indicating an immediately applied |
| 1599 |
// function literal with free variables. |
| 1600 |
// (c) a *Builtin, indicating a statically dispatched call |
| 1601 |
// to a built-in function. |
| 1602 |
// (d) any other value, indicating a dynamically dispatched |
| 1603 |
// function call. |
| 1604 |
// |
| 1605 |
// StaticCallee returns the identity of the callee in cases |
| 1606 |
// (a) and (b), nil otherwise. |
| 1607 |
// |
| 1608 |
// Args contains the arguments to the call. If Value is a method, |
| 1609 |
// Args[0] contains the receiver parameter. |
| 1610 |
// |
| 1611 |
// Example printed form: |
| 1612 |
// |
| 1613 |
// t2 = println(t0, t1) |
| 1614 |
// go t3() |
| 1615 |
// defer t5(...t6) |
| 1616 |
// |
| 1617 |
// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon |
| 1618 |
// represents a dynamically dispatched call to an interface method. |
| 1619 |
// In this mode, Value is the interface value and Method is the |
| 1620 |
// interface's abstract method. The interface value may be a type |
| 1621 |
// parameter. Note: an interface method may be shared by multiple |
| 1622 |
// interfaces due to embedding; Value.Type() provides the specific |
| 1623 |
// interface used for this call. |
| 1624 |
// |
| 1625 |
// Value is implicitly supplied to the concrete method implementation |
| 1626 |
// as the receiver parameter; in other words, Args[0] holds not the |
| 1627 |
// receiver but the first true argument. |
| 1628 |
// |
| 1629 |
// Example printed form: |
| 1630 |
// |
| 1631 |
// t1 = invoke t0.String() |
| 1632 |
// go invoke t3.Run(t2) |
| 1633 |
// defer invoke t4.Handle(...t5) |
| 1634 |
// |
| 1635 |
// For all calls to variadic functions (Signature().Variadic()), |
| 1636 |
// the last element of Args is a slice. |
| 1637 |
type CallCommon struct { |
| 1638 |
Value Value // receiver (invoke mode) or func value (call mode) |
| 1639 |
Method *types.Func // interface method (invoke mode) |
| 1640 |
Args []Value // actual parameters (in static method call, includes receiver) |
| 1641 |
|
| 1642 |
source ast.Node |
| 1643 |
} |
| 1644 |
|
| 1645 |
// IsInvoke returns true if this call has "invoke" (not "call") mode. |
| 1646 |
func (c *CallCommon) IsInvoke() bool { |
| 1647 |
return c.Method != nil |
| 1648 |
} |
| 1649 |
|
| 1650 |
func (c *CallCommon) Pos() token.Pos { |
| 1651 |
if c.source != nil { |
| 1652 |
return c.source.Pos() |
| 1653 |
} |
| 1654 |
return token.NoPos |
| 1655 |
} |
| 1656 |
|
| 1657 |
// Signature returns the signature of the called function. |
| 1658 |
// |
| 1659 |
// For an "invoke"-mode call, the signature of the interface method is |
| 1660 |
// returned. |
| 1661 |
// |
| 1662 |
// In either "call" or "invoke" mode, if the callee is a method, its |
| 1663 |
// receiver is represented by sig.Recv, not sig.Params().At(0). |
| 1664 |
func (c *CallCommon) Signature() *types.Signature { |
| 1665 |
if c.Method != nil { |
| 1666 |
return c.Method.Type().(*types.Signature) |
| 1667 |
} |
| 1668 |
return typeutil.CoreType(c.Value.Type()).(*types.Signature) |
| 1669 |
} |
| 1670 |
|
| 1671 |
// StaticCallee returns the callee if this is a trivially static |
| 1672 |
// "call"-mode call to a function. |
| 1673 |
func (c *CallCommon) StaticCallee() *Function { |
| 1674 |
switch fn := c.Value.(type) { |
| 1675 |
case *Function: |
| 1676 |
return fn |
| 1677 |
case *MakeClosure: |
| 1678 |
return fn.Fn.(*Function) |
| 1679 |
} |
| 1680 |
return nil |
| 1681 |
} |
| 1682 |
|
| 1683 |
// Description returns a description of the mode of this call suitable |
| 1684 |
// for a user interface, e.g., "static method call". |
| 1685 |
func (c *CallCommon) Description() string { |
| 1686 |
switch fn := c.Value.(type) { |
| 1687 |
case *Builtin: |
| 1688 |
return "built-in function call" |
| 1689 |
case *MakeClosure: |
| 1690 |
return "static function closure call" |
| 1691 |
case *Function: |
| 1692 |
if fn.Signature.Recv() != nil { |
| 1693 |
return "static method call" |
| 1694 |
} |
| 1695 |
return "static function call" |
| 1696 |
} |
| 1697 |
if c.IsInvoke() { |
| 1698 |
return "dynamic method call" // ("invoke" mode) |
| 1699 |
} |
| 1700 |
return "dynamic function call" |
| 1701 |
} |
| 1702 |
|
| 1703 |
// The CallInstruction interface, implemented by *Go, *Defer and *Call, |
| 1704 |
// exposes the common parts of function-calling instructions, |
| 1705 |
// yet provides a way back to the Value defined by *Call alone. |
| 1706 |
type CallInstruction interface { |
| 1707 |
Instruction |
| 1708 |
Common() *CallCommon // returns the common parts of the call |
| 1709 |
Value() *Call |
| 1710 |
} |
| 1711 |
|
| 1712 |
func (s *Call) Common() *CallCommon { return &s.Call } |
| 1713 |
func (s *Defer) Common() *CallCommon { return &s.Call } |
| 1714 |
func (s *Go) Common() *CallCommon { return &s.Call } |
| 1715 |
|
| 1716 |
func (s *Call) setSource(source ast.Node) { s.source = source; s.Call.source = source } |
| 1717 |
func (s *Defer) setSource(source ast.Node) { s.source = source; s.Call.source = source } |
| 1718 |
func (s *Go) setSource(source ast.Node) { s.source = source; s.Call.source = source } |
| 1719 |
|
| 1720 |
func (s *Call) Value() *Call { return s } |
| 1721 |
func (s *Defer) Value() *Call { return nil } |
| 1722 |
func (s *Go) Value() *Call { return nil } |
| 1723 |
|
| 1724 |
func (v *Builtin) Type() types.Type { return v.sig } |
| 1725 |
func (v *Builtin) Name() string { return v.name } |
| 1726 |
func (*Builtin) Referrers() *[]Instruction { return nil } |
| 1727 |
func (v *Builtin) Pos() token.Pos { return token.NoPos } |
| 1728 |
func (v *Builtin) Object() types.Object { return types.Universe.Lookup(v.name) } |
| 1729 |
func (v *Builtin) Parent() *Function { return nil } |
| 1730 |
|
| 1731 |
func (v *FreeVar) Type() types.Type { return v.typ } |
| 1732 |
func (v *FreeVar) Name() string { return v.name } |
| 1733 |
func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers } |
| 1734 |
func (v *FreeVar) Parent() *Function { return v.parent } |
| 1735 |
|
| 1736 |
func (v *Global) Type() types.Type { return v.typ } |
| 1737 |
func (v *Global) Name() string { return v.name } |
| 1738 |
func (v *Global) Parent() *Function { return nil } |
| 1739 |
func (v *Global) Referrers() *[]Instruction { return nil } |
| 1740 |
func (v *Global) Token() token.Token { return token.VAR } |
| 1741 |
func (v *Global) Object() types.Object { return v.object } |
| 1742 |
func (v *Global) String() string { return v.RelString(nil) } |
| 1743 |
func (v *Global) Package() *Package { return v.Pkg } |
| 1744 |
func (v *Global) RelString(from *types.Package) string { return relString(v, from) } |
| 1745 |
|
| 1746 |
func (v *Function) Name() string { return v.name } |
| 1747 |
func (v *Function) Type() types.Type { return v.Signature } |
| 1748 |
func (v *Function) Token() token.Token { return token.FUNC } |
| 1749 |
func (v *Function) Object() types.Object { |
| 1750 |
if v.object != nil { |
| 1751 |
return types.Object(v.object) |
| 1752 |
} |
| 1753 |
return nil |
| 1754 |
} |
| 1755 |
func (v *Function) String() string { return v.RelString(nil) } |
| 1756 |
func (v *Function) Package() *Package { return v.Pkg } |
| 1757 |
func (v *Function) Parent() *Function { return v.parent } |
| 1758 |
func (v *Function) Referrers() *[]Instruction { |
| 1759 |
if v.parent != nil { |
| 1760 |
return &v.referrers |
| 1761 |
} |
| 1762 |
return nil |
| 1763 |
} |
| 1764 |
|
| 1765 |
// Returns returns an iterator over all blocks containing return statements. |
| 1766 |
func (v *Function) Returns() iter.Seq[*BasicBlock] { |
| 1767 |
return func(yield func(*BasicBlock) bool) { |
| 1768 |
for _, b := range v.Blocks { |
| 1769 |
if _, ok := b.Control().(*Return); ok { |
| 1770 |
if !yield(b) { |
| 1771 |
return |
| 1772 |
} |
| 1773 |
} |
| 1774 |
} |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
// Exits returns an iterator over all blocks that have no successors. |
| 1779 |
func (v *Function) Exits() iter.Seq[*BasicBlock] { |
| 1780 |
return func(yield func(*BasicBlock) bool) { |
| 1781 |
for _, b := range v.Blocks { |
| 1782 |
if len(b.Succs) == 0 { |
| 1783 |
if !yield(b) { |
| 1784 |
return |
| 1785 |
} |
| 1786 |
} |
| 1787 |
} |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
// Nodes returns an iterator over the basic block indices. |
| 1792 |
func (v *Function) Nodes() iter.Seq[int] { |
| 1793 |
return func(yield func(int) bool) { |
| 1794 |
for i := range v.Blocks { |
| 1795 |
if !yield(i) { |
| 1796 |
return |
| 1797 |
} |
| 1798 |
} |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
// NumNodes returns the number of basic blocks in the graph. |
| 1803 |
func (v *Function) NumNodes() int { return len(v.Blocks) } |
| 1804 |
|
| 1805 |
// Out returns an iterator over the successor block indices of a given node. |
| 1806 |
func (v *Function) Out(node int) iter.Seq[int] { |
| 1807 |
return func(yield func(int) bool) { |
| 1808 |
for _, succ := range v.Blocks[node].Succs { |
| 1809 |
if !yield(succ.Index) { |
| 1810 |
return |
| 1811 |
} |
| 1812 |
} |
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
func (v *Parameter) Type() types.Type { return v.typ } |
| 1817 |
func (v *Parameter) Name() string { return v.name } |
| 1818 |
func (v *Parameter) Object() types.Object { return v.object } |
| 1819 |
func (v *Parameter) Referrers() *[]Instruction { return &v.referrers } |
| 1820 |
func (v *Parameter) Pos() token.Pos { return v.object.Pos() } |
| 1821 |
func (v *Parameter) Parent() *Function { return v.parent } |
| 1822 |
|
| 1823 |
// TypeParams are the function's type parameters if generic or the |
| 1824 |
// type parameters that were instantiated if fn is an instantiation. |
| 1825 |
// |
| 1826 |
// Specifically, the resulting list behaves like: |
| 1827 |
// |
| 1828 |
// func f // [] |
| 1829 |
// func f[P] // [P] |
| 1830 |
// func (T) m // [] |
| 1831 |
// func (T) m[P] // [P] |
| 1832 |
// func (T[P]) m // [P] |
| 1833 |
// func (T[P]) m[Q] // [P (index=0), Q (index=0)] |
| 1834 |
// |
| 1835 |
// Note that receiver type parameters precede other type parameters. |
| 1836 |
// Also, type parameters may have the same index if they come from |
| 1837 |
// different source type parameter lists. |
| 1838 |
func (fn *Function) TypeParams() *types.TypeParamList { |
| 1839 |
return consTypeParamLists(fn.recvtypeparams, fn.typeparams) |
| 1840 |
} |
| 1841 |
|
| 1842 |
func consTypeParamLists(l, r *types.TypeParamList) *types.TypeParamList { |
| 1843 |
if l.Len() == 0 { |
| 1844 |
return r |
| 1845 |
} |
| 1846 |
if r.Len() == 0 { |
| 1847 |
return l |
| 1848 |
} |
| 1849 |
|
| 1850 |
tpars := make([]*types.TypeParam, l.Len()+r.Len()) |
| 1851 |
for i := range l.Len() { |
| 1852 |
tpars[i] = l.At(i) |
| 1853 |
} |
| 1854 |
for i := range r.Len() { |
| 1855 |
tpars[i+l.Len()] = r.At(i) |
| 1856 |
} |
| 1857 |
// This logic unsafely assumes (and asserts) that the layout of the |
| 1858 |
// TypeParamList is identical to that of a slice of TypeParams. This |
| 1859 |
// is a hack while we work on getting a constructor for TypeParamList |
| 1860 |
// approved (see go.dev/issue/79603). |
| 1861 |
t := reflect.TypeFor[types.TypeParamList]() |
| 1862 |
if t.NumField() != 1 { |
| 1863 |
panic("TypeParamList has unexpected fields") |
| 1864 |
} |
| 1865 |
if f := t.Field(0); f.Offset != 0 || f.Type != reflect.TypeFor[[]*types.TypeParam]() { |
| 1866 |
panic("TypeParamList field is not []*TypeParam") |
| 1867 |
} |
| 1868 |
return (*types.TypeParamList)(unsafe.Pointer(&tpars)) |
| 1869 |
} |
| 1870 |
|
| 1871 |
// TypeArgs are the types that TypeParams() were instantiated by to create fn |
| 1872 |
// from fn.Origin(). |
| 1873 |
// |
| 1874 |
// Specifically, the resulting slice behaves like: |
| 1875 |
// |
| 1876 |
// f // [] |
| 1877 |
// f[int] // [int] |
| 1878 |
// T.m // [] |
| 1879 |
// T.m[int] // [int] |
| 1880 |
// T[int].m // [int] |
| 1881 |
// T[int].m[uint] // [int, uint] |
| 1882 |
// |
| 1883 |
// Note that receiver type arguments precede other type arguments. |
| 1884 |
func (fn *Function) TypeArgs() []types.Type { |
| 1885 |
return slices.Concat(fn.recvtypeargs, fn.typeargs) |
| 1886 |
} |
| 1887 |
|
| 1888 |
// Origin returns the generic function from which fn was instantiated, |
| 1889 |
// or nil if fn is not an instantiation. |
| 1890 |
func (fn *Function) Origin() *Function { |
| 1891 |
if fn.parent != nil && fn.parent.hasTypeArgs() { |
| 1892 |
// Nested functions are BUILT at a different time than their instances. |
| 1893 |
// Build declared package if not yet BUILT. This is not an expected use |
| 1894 |
// case, but is simple and robust. |
| 1895 |
fn.declaredPackage().Build() |
| 1896 |
} |
| 1897 |
return origin(fn) |
| 1898 |
} |
| 1899 |
|
| 1900 |
// hasTypeParams returns whether fn has any type parameters |
| 1901 |
func (fn *Function) hasTypeParams() bool { |
| 1902 |
return fn.recvtypeparams.Len()+fn.typeparams.Len() > 0 |
| 1903 |
} |
| 1904 |
|
| 1905 |
// hasTypeArgs returns whether fn has any type arguments |
| 1906 |
func (fn *Function) hasTypeArgs() bool { |
| 1907 |
return len(fn.recvtypeargs)+len(fn.typeargs) > 0 |
| 1908 |
} |
| 1909 |
|
| 1910 |
// subrtargs returns fn's receiver type parameters substituted with receiver type arguments |
| 1911 |
func (fn *Function) subrtargs(m *types.Func) []types.Type { |
| 1912 |
return fn.subst.types(receiverTypeArgs(m)) |
| 1913 |
} |
| 1914 |
|
| 1915 |
// subtargs returns fn's type parameters substituted with (possibly implied) type arguments |
| 1916 |
func (fn *Function) subtargs(id *ast.Ident) []types.Type { |
| 1917 |
return fn.subst.types(instanceArgs(fn.info, id)) |
| 1918 |
} |
| 1919 |
|
| 1920 |
// targstr returns a comma-separated string of the types in targs |
| 1921 |
func targstr(targs []types.Type) string { |
| 1922 |
var sb strings.Builder |
| 1923 |
if len(targs) > 0 { |
| 1924 |
sb.WriteString("[") |
| 1925 |
for i := range targs { |
| 1926 |
if i > 0 { |
| 1927 |
sb.WriteString(", ") |
| 1928 |
} |
| 1929 |
sb.WriteString(targs[i].String()) |
| 1930 |
} |
| 1931 |
sb.WriteString("]") |
| 1932 |
} |
| 1933 |
return sb.String() |
| 1934 |
} |
| 1935 |
|
| 1936 |
// origin is the function that fn is an instantiation of. Returns nil if fn is |
| 1937 |
// not an instantiation. |
| 1938 |
// |
| 1939 |
// Precondition: fn and the origin function are done building. |
| 1940 |
func origin(fn *Function) *Function { |
| 1941 |
if fn.parent != nil && fn.parent.hasTypeArgs() { |
| 1942 |
return origin(fn.parent).AnonFuncs[fn.anonIdx] |
| 1943 |
} |
| 1944 |
return fn.topLevelOrigin |
| 1945 |
} |
| 1946 |
|
| 1947 |
func (v *Alloc) Type() types.Type { return v.typ } |
| 1948 |
func (v *Alloc) Referrers() *[]Instruction { return &v.referrers } |
| 1949 |
|
| 1950 |
func (v *register) Type() types.Type { return v.typ } |
| 1951 |
func (v *register) setType(typ types.Type) { v.typ = typ } |
| 1952 |
func (v *register) Name() string { return fmt.Sprintf("t%d", v.id) } |
| 1953 |
func (v *register) Referrers() *[]Instruction { return &v.referrers } |
| 1954 |
|
| 1955 |
func (v *anInstruction) Parent() *Function { return v.block.parent } |
| 1956 |
func (v *anInstruction) Block() *BasicBlock { return v.block } |
| 1957 |
func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block } |
| 1958 |
func (v *anInstruction) Referrers() *[]Instruction { return nil } |
| 1959 |
|
| 1960 |
func (t *Type) Name() string { return t.object.Name() } |
| 1961 |
func (t *Type) Pos() token.Pos { return t.object.Pos() } |
| 1962 |
func (t *Type) Type() types.Type { return t.object.Type() } |
| 1963 |
func (t *Type) Token() token.Token { return token.TYPE } |
| 1964 |
func (t *Type) Object() types.Object { return t.object } |
| 1965 |
func (t *Type) String() string { return t.RelString(nil) } |
| 1966 |
func (t *Type) Package() *Package { return t.pkg } |
| 1967 |
func (t *Type) RelString(from *types.Package) string { return relString(t, from) } |
| 1968 |
|
| 1969 |
func (c *NamedConst) Name() string { return c.object.Name() } |
| 1970 |
func (c *NamedConst) Pos() token.Pos { return c.object.Pos() } |
| 1971 |
func (c *NamedConst) String() string { return c.RelString(nil) } |
| 1972 |
func (c *NamedConst) Type() types.Type { return c.object.Type() } |
| 1973 |
func (c *NamedConst) Token() token.Token { return token.CONST } |
| 1974 |
func (c *NamedConst) Object() types.Object { return c.object } |
| 1975 |
func (c *NamedConst) Package() *Package { return c.pkg } |
| 1976 |
func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) } |
| 1977 |
|
| 1978 |
func (v *Function) Pos() token.Pos { return v.pos } |
| 1979 |
func (v *Function) Source() ast.Node { return v.syntax } |
| 1980 |
|
| 1981 |
func (d *debugRef) Object() types.Object { return d.object } |
| 1982 |
|
| 1983 |
// Func returns the package-level function of the specified name, |
| 1984 |
// or nil if not found. |
| 1985 |
func (p *Package) Func(name string) (f *Function) { |
| 1986 |
f, _ = p.Members[name].(*Function) |
| 1987 |
return |
| 1988 |
} |
| 1989 |
|
| 1990 |
// Var returns the package-level variable of the specified name, |
| 1991 |
// or nil if not found. |
| 1992 |
func (p *Package) Var(name string) (g *Global) { |
| 1993 |
g, _ = p.Members[name].(*Global) |
| 1994 |
return |
| 1995 |
} |
| 1996 |
|
| 1997 |
// Const returns the package-level constant of the specified name, |
| 1998 |
// or nil if not found. |
| 1999 |
func (p *Package) Const(name string) (c *NamedConst) { |
| 2000 |
c, _ = p.Members[name].(*NamedConst) |
| 2001 |
return |
| 2002 |
} |
| 2003 |
|
| 2004 |
// Type returns the package-level type of the specified name, |
| 2005 |
// or nil if not found. |
| 2006 |
func (p *Package) Type(name string) (t *Type) { |
| 2007 |
t, _ = p.Members[name].(*Type) |
| 2008 |
return |
| 2009 |
} |
| 2010 |
|
| 2011 |
func (s *debugRef) Pos() token.Pos { return s.Expr.Pos() } |
| 2012 |
|
| 2013 |
// Operands. |
| 2014 |
|
| 2015 |
func (v *Alloc) Operands(rands []*Value) []*Value { |
| 2016 |
return rands |
| 2017 |
} |
| 2018 |
|
| 2019 |
func (v *BinOp) Operands(rands []*Value) []*Value { |
| 2020 |
return append(rands, &v.X, &v.Y) |
| 2021 |
} |
| 2022 |
|
| 2023 |
func (c *CallCommon) Operands(rands []*Value) []*Value { |
| 2024 |
rands = append(rands, &c.Value) |
| 2025 |
for i := range c.Args { |
| 2026 |
rands = append(rands, &c.Args[i]) |
| 2027 |
} |
| 2028 |
return rands |
| 2029 |
} |
| 2030 |
|
| 2031 |
func (s *Go) Operands(rands []*Value) []*Value { |
| 2032 |
return s.Call.Operands(rands) |
| 2033 |
} |
| 2034 |
|
| 2035 |
func (s *Call) Operands(rands []*Value) []*Value { |
| 2036 |
return s.Call.Operands(rands) |
| 2037 |
} |
| 2038 |
|
| 2039 |
func (s *Defer) Operands(rands []*Value) []*Value { |
| 2040 |
return append(s.Call.Operands(rands), &s.DeferStack) |
| 2041 |
} |
| 2042 |
|
| 2043 |
func (v *ChangeInterface) Operands(rands []*Value) []*Value { |
| 2044 |
return append(rands, &v.X) |
| 2045 |
} |
| 2046 |
|
| 2047 |
func (v *ChangeType) Operands(rands []*Value) []*Value { |
| 2048 |
return append(rands, &v.X) |
| 2049 |
} |
| 2050 |
|
| 2051 |
func (v *Convert) Operands(rands []*Value) []*Value { |
| 2052 |
return append(rands, &v.X) |
| 2053 |
} |
| 2054 |
|
| 2055 |
func (v *MultiConvert) Operands(rands []*Value) []*Value { |
| 2056 |
return append(rands, &v.X) |
| 2057 |
} |
| 2058 |
|
| 2059 |
func (v *SliceToArrayPointer) Operands(rands []*Value) []*Value { |
| 2060 |
return append(rands, &v.X) |
| 2061 |
} |
| 2062 |
|
| 2063 |
func (v *SliceToArray) Operands(rands []*Value) []*Value { |
| 2064 |
return append(rands, &v.X) |
| 2065 |
} |
| 2066 |
|
| 2067 |
func (s *debugRef) Operands(rands []*Value) []*Value { |
| 2068 |
return append(rands, &s.X) |
| 2069 |
} |
| 2070 |
|
| 2071 |
func (v *Extract) Operands(rands []*Value) []*Value { |
| 2072 |
return append(rands, &v.Tuple) |
| 2073 |
} |
| 2074 |
|
| 2075 |
func (v *Field) Operands(rands []*Value) []*Value { |
| 2076 |
return append(rands, &v.X) |
| 2077 |
} |
| 2078 |
|
| 2079 |
func (v *FieldAddr) Operands(rands []*Value) []*Value { |
| 2080 |
return append(rands, &v.X) |
| 2081 |
} |
| 2082 |
|
| 2083 |
func (s *If) Operands(rands []*Value) []*Value { |
| 2084 |
return append(rands, &s.Cond) |
| 2085 |
} |
| 2086 |
|
| 2087 |
func (s *ConstantSwitch) Operands(rands []*Value) []*Value { |
| 2088 |
rands = append(rands, &s.Tag) |
| 2089 |
for i := range s.Conds { |
| 2090 |
rands = append(rands, &s.Conds[i]) |
| 2091 |
} |
| 2092 |
return rands |
| 2093 |
} |
| 2094 |
|
| 2095 |
func (s *TypeSwitch) Operands(rands []*Value) []*Value { |
| 2096 |
rands = append(rands, &s.Tag) |
| 2097 |
return rands |
| 2098 |
} |
| 2099 |
|
| 2100 |
func (v *Index) Operands(rands []*Value) []*Value { |
| 2101 |
return append(rands, &v.X, &v.Index) |
| 2102 |
} |
| 2103 |
|
| 2104 |
func (v *IndexAddr) Operands(rands []*Value) []*Value { |
| 2105 |
return append(rands, &v.X, &v.Index) |
| 2106 |
} |
| 2107 |
|
| 2108 |
func (*Jump) Operands(rands []*Value) []*Value { |
| 2109 |
return rands |
| 2110 |
} |
| 2111 |
|
| 2112 |
func (*Unreachable) Operands(rands []*Value) []*Value { |
| 2113 |
return rands |
| 2114 |
} |
| 2115 |
|
| 2116 |
func (v *MapLookup) Operands(rands []*Value) []*Value { |
| 2117 |
return append(rands, &v.X, &v.Index) |
| 2118 |
} |
| 2119 |
|
| 2120 |
func (v *StringLookup) Operands(rands []*Value) []*Value { |
| 2121 |
return append(rands, &v.X, &v.Index) |
| 2122 |
} |
| 2123 |
|
| 2124 |
func (v *MakeChan) Operands(rands []*Value) []*Value { |
| 2125 |
return append(rands, &v.Size) |
| 2126 |
} |
| 2127 |
|
| 2128 |
func (v *MakeClosure) Operands(rands []*Value) []*Value { |
| 2129 |
rands = append(rands, &v.Fn) |
| 2130 |
for i := range v.Bindings { |
| 2131 |
rands = append(rands, &v.Bindings[i]) |
| 2132 |
} |
| 2133 |
return rands |
| 2134 |
} |
| 2135 |
|
| 2136 |
func (v *MakeInterface) Operands(rands []*Value) []*Value { |
| 2137 |
return append(rands, &v.X) |
| 2138 |
} |
| 2139 |
|
| 2140 |
func (v *MakeMap) Operands(rands []*Value) []*Value { |
| 2141 |
return append(rands, &v.Reserve) |
| 2142 |
} |
| 2143 |
|
| 2144 |
func (v *MakeSlice) Operands(rands []*Value) []*Value { |
| 2145 |
return append(rands, &v.Len, &v.Cap) |
| 2146 |
} |
| 2147 |
|
| 2148 |
func (v *MapUpdate) Operands(rands []*Value) []*Value { |
| 2149 |
return append(rands, &v.Map, &v.Key, &v.Value) |
| 2150 |
} |
| 2151 |
|
| 2152 |
func (v *Next) Operands(rands []*Value) []*Value { |
| 2153 |
return append(rands, &v.Iter) |
| 2154 |
} |
| 2155 |
|
| 2156 |
func (s *Panic) Operands(rands []*Value) []*Value { |
| 2157 |
return append(rands, &s.X) |
| 2158 |
} |
| 2159 |
|
| 2160 |
func (v *Phi) Operands(rands []*Value) []*Value { |
| 2161 |
for i := range v.Edges { |
| 2162 |
rands = append(rands, &v.Edges[i]) |
| 2163 |
} |
| 2164 |
return rands |
| 2165 |
} |
| 2166 |
|
| 2167 |
func (v *Range) Operands(rands []*Value) []*Value { |
| 2168 |
return append(rands, &v.X) |
| 2169 |
} |
| 2170 |
|
| 2171 |
func (s *Return) Operands(rands []*Value) []*Value { |
| 2172 |
for i := range s.Results { |
| 2173 |
rands = append(rands, &s.Results[i]) |
| 2174 |
} |
| 2175 |
return rands |
| 2176 |
} |
| 2177 |
|
| 2178 |
func (*RunDefers) Operands(rands []*Value) []*Value { |
| 2179 |
return rands |
| 2180 |
} |
| 2181 |
|
| 2182 |
func (v *Select) Operands(rands []*Value) []*Value { |
| 2183 |
for i := range v.States { |
| 2184 |
rands = append(rands, &v.States[i].Chan, &v.States[i].Send) |
| 2185 |
} |
| 2186 |
return rands |
| 2187 |
} |
| 2188 |
|
| 2189 |
func (s *Send) Operands(rands []*Value) []*Value { |
| 2190 |
return append(rands, &s.Chan, &s.X) |
| 2191 |
} |
| 2192 |
|
| 2193 |
func (recv *Recv) Operands(rands []*Value) []*Value { |
| 2194 |
return append(rands, &recv.Chan) |
| 2195 |
} |
| 2196 |
|
| 2197 |
func (v *Slice) Operands(rands []*Value) []*Value { |
| 2198 |
return append(rands, &v.X, &v.Low, &v.High, &v.Max) |
| 2199 |
} |
| 2200 |
|
| 2201 |
func (s *Store) Operands(rands []*Value) []*Value { |
| 2202 |
return append(rands, &s.Addr, &s.Val) |
| 2203 |
} |
| 2204 |
|
| 2205 |
func (s *BlankStore) Operands(rands []*Value) []*Value { |
| 2206 |
return append(rands, &s.Val) |
| 2207 |
} |
| 2208 |
|
| 2209 |
func (v *TypeAssert) Operands(rands []*Value) []*Value { |
| 2210 |
return append(rands, &v.X) |
| 2211 |
} |
| 2212 |
|
| 2213 |
func (v *UnOp) Operands(rands []*Value) []*Value { |
| 2214 |
return append(rands, &v.X) |
| 2215 |
} |
| 2216 |
|
| 2217 |
func (v *Load) Operands(rands []*Value) []*Value { |
| 2218 |
return append(rands, &v.X) |
| 2219 |
} |
| 2220 |
|
| 2221 |
func (v *AggregateConst) Operands(rands []*Value) []*Value { |
| 2222 |
for i := range v.Values { |
| 2223 |
rands = append(rands, &v.Values[i]) |
| 2224 |
} |
| 2225 |
return rands |
| 2226 |
} |
| 2227 |
|
| 2228 |
func (v *CompositeValue) Operands(rands []*Value) []*Value { |
| 2229 |
for i := range v.Values { |
| 2230 |
rands = append(rands, &v.Values[i]) |
| 2231 |
} |
| 2232 |
return rands |
| 2233 |
} |
| 2234 |
|
| 2235 |
// Non-Instruction Values: |
| 2236 |
func (v *Builtin) Operands(rands []*Value) []*Value { return rands } |
| 2237 |
func (v *FreeVar) Operands(rands []*Value) []*Value { return rands } |
| 2238 |
func (v *Const) Operands(rands []*Value) []*Value { return rands } |
| 2239 |
func (v *Function) Operands(rands []*Value) []*Value { return rands } |
| 2240 |
func (v *Global) Operands(rands []*Value) []*Value { return rands } |
| 2241 |
func (v *Parameter) Operands(rands []*Value) []*Value { return rands } |