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