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