gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/simpler/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 var _ Instruction = (*Sigma)(nil) 508 var _ Value = (*Sigma)(nil) 509 510 type Sigma struct { 511 register 512 X Value 513 Branch bool 514 } 515 516 func (p *Sigma) Value() Value { 517 v := p.X 518 for { 519 sigma, ok := v.(*Sigma) 520 if !ok { 521 break 522 } 523 v = sigma 524 } 525 return v 526 } 527 528 func (p *Sigma) String() string { 529 return fmt.Sprintf("σ [%s.%t]", relName(p.X, p), p.Branch) 530 } 531 532 // The Phi instruction represents an SSA φ-node, which combines values 533 // that differ across incoming control-flow edges and yields a new 534 // value. Within a block, all φ-nodes must appear before all non-φ 535 // nodes. 536 // 537 // Pos() returns the position of the && or || for short-circuit 538 // control-flow joins, or that of the *Alloc for φ-nodes inserted 539 // during SSA renaming. 540 // 541 // Example printed form: 542 // t2 = phi [0: t0, 1: t1] 543 // 544 type Phi struct { 545 register 546 Comment string // a hint as to its purpose 547 Edges []Value // Edges[i] is value for Block().Preds[i] 548 } 549 550 // The Call instruction represents a function or method call. 551 // 552 // The Call instruction yields the function result if there is exactly 553 // one. Otherwise it returns a tuple, the components of which are 554 // accessed via Extract. 555 // 556 // See CallCommon for generic function call documentation. 557 // 558 // Pos() returns the ast.CallExpr.Lparen, if explicit in the source. 559 // 560 // Example printed form: 561 // t2 = println(t0, t1) 562 // t4 = t3() 563 // t7 = invoke t5.Println(...t6) 564 // 565 type Call struct { 566 register 567 Call CallCommon 568 } 569 570 // The BinOp instruction yields the result of binary operation X Op Y. 571 // 572 // Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source. 573 // 574 // Example printed form: 575 // t1 = t0 + 1:int 576 // 577 type BinOp struct { 578 register 579 // One of: 580 // ADD SUB MUL QUO REM + - * / % 581 // AND OR XOR SHL SHR AND_NOT & | ^ << >> &~ 582 // EQL LSS GTR NEQ LEQ GEQ == != < <= < >= 583 Op token.Token 584 X, Y Value 585 } 586 587 // The UnOp instruction yields the result of Op X. 588 // ARROW is channel receive. 589 // MUL is pointer indirection (load). 590 // XOR is bitwise complement. 591 // SUB is negation. 592 // NOT is logical negation. 593 // 594 // If CommaOk and Op=ARROW, the result is a 2-tuple of the value above 595 // and a boolean indicating the success of the receive. The 596 // components of the tuple are accessed using Extract. 597 // 598 // Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source. 599 // For receive operations (ARROW) implicit in ranging over a channel, 600 // Pos() returns the ast.RangeStmt.For. 601 // For implicit memory loads (STAR), Pos() returns the position of the 602 // most closely associated source-level construct; the details are not 603 // specified. 604 // 605 // Example printed form: 606 // t0 = *x 607 // t2 = <-t1,ok 608 // 609 type UnOp struct { 610 register 611 Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^ 612 X Value 613 CommaOk bool 614 } 615 616 // The ChangeType instruction applies to X a value-preserving type 617 // change to Type(). 618 // 619 // Type changes are permitted: 620 // - between a named type and its underlying type. 621 // - between two named types of the same underlying type. 622 // - between (possibly named) pointers to identical base types. 623 // - from a bidirectional channel to a read- or write-channel, 624 // optionally adding/removing a name. 625 // 626 // This operation cannot fail dynamically. 627 // 628 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose 629 // from an explicit conversion in the source. 630 // 631 // Example printed form: 632 // t1 = changetype *int <- IntPtr (t0) 633 // 634 type ChangeType struct { 635 register 636 X Value 637 } 638 639 // The Convert instruction yields the conversion of value X to type 640 // Type(). One or both of those types is basic (but possibly named). 641 // 642 // A conversion may change the value and representation of its operand. 643 // Conversions are permitted: 644 // - between real numeric types. 645 // - between complex numeric types. 646 // - between string and []byte or []rune. 647 // - between pointers and unsafe.Pointer. 648 // - between unsafe.Pointer and uintptr. 649 // - from (Unicode) integer to (UTF-8) string. 650 // A conversion may imply a type name change also. 651 // 652 // This operation cannot fail dynamically. 653 // 654 // Conversions of untyped string/number/bool constants to a specific 655 // representation are eliminated during SSA construction. 656 // 657 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose 658 // from an explicit conversion in the source. 659 // 660 // Example printed form: 661 // t1 = convert []byte <- string (t0) 662 // 663 type Convert struct { 664 register 665 X Value 666 } 667 668 // ChangeInterface constructs a value of one interface type from a 669 // value of another interface type known to be assignable to it. 670 // This operation cannot fail. 671 // 672 // Pos() returns the ast.CallExpr.Lparen if the instruction arose from 673 // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the 674 // instruction arose from an explicit e.(T) operation; or token.NoPos 675 // otherwise. 676 // 677 // Example printed form: 678 // t1 = change interface interface{} <- I (t0) 679 // 680 type ChangeInterface struct { 681 register 682 X Value 683 } 684 685 // MakeInterface constructs an instance of an interface type from a 686 // value of a concrete type. 687 // 688 // Use Program.MethodSets.MethodSet(X.Type()) to find the method-set 689 // of X, and Program.Method(m) to find the implementation of a method. 690 // 691 // To construct the zero value of an interface type T, use: 692 // NewConst(exact.MakeNil(), T, pos) 693 // 694 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose 695 // from an explicit conversion in the source. 696 // 697 // Example printed form: 698 // t1 = make interface{} <- int (42:int) 699 // t2 = make Stringer <- t0 700 // 701 type MakeInterface struct { 702 register 703 X Value 704 } 705 706 // The MakeClosure instruction yields a closure value whose code is 707 // Fn and whose free variables' values are supplied by Bindings. 708 // 709 // Type() returns a (possibly named) *types.Signature. 710 // 711 // Pos() returns the ast.FuncLit.Type.Func for a function literal 712 // closure or the ast.SelectorExpr.Sel for a bound method closure. 713 // 714 // Example printed form: 715 // t0 = make closure anon@1.2 [x y z] 716 // t1 = make closure bound$(main.I).add [i] 717 // 718 type MakeClosure struct { 719 register 720 Fn Value // always a *Function 721 Bindings []Value // values for each free variable in Fn.FreeVars 722 } 723 724 // The MakeMap instruction creates a new hash-table-based map object 725 // and yields a value of kind map. 726 // 727 // Type() returns a (possibly named) *types.Map. 728 // 729 // Pos() returns the ast.CallExpr.Lparen, if created by make(map), or 730 // the ast.CompositeLit.Lbrack if created by a literal. 731 // 732 // Example printed form: 733 // t1 = make map[string]int t0 734 // t1 = make StringIntMap t0 735 // 736 type MakeMap struct { 737 register 738 Reserve Value // initial space reservation; nil => default 739 } 740 741 // The MakeChan instruction creates a new channel object and yields a 742 // value of kind chan. 743 // 744 // Type() returns a (possibly named) *types.Chan. 745 // 746 // Pos() returns the ast.CallExpr.Lparen for the make(chan) that 747 // created it. 748 // 749 // Example printed form: 750 // t0 = make chan int 0 751 // t0 = make IntChan 0 752 // 753 type MakeChan struct { 754 register 755 Size Value // int; size of buffer; zero => synchronous. 756 } 757 758 // The MakeSlice instruction yields a slice of length Len backed by a 759 // newly allocated array of length Cap. 760 // 761 // Both Len and Cap must be non-nil Values of integer type. 762 // 763 // (Alloc(types.Array) followed by Slice will not suffice because 764 // Alloc can only create arrays of constant length.) 765 // 766 // Type() returns a (possibly named) *types.Slice. 767 // 768 // Pos() returns the ast.CallExpr.Lparen for the make([]T) that 769 // created it. 770 // 771 // Example printed form: 772 // t1 = make []string 1:int t0 773 // t1 = make StringSlice 1:int t0 774 // 775 type MakeSlice struct { 776 register 777 Len Value 778 Cap Value 779 } 780 781 // The Slice instruction yields a slice of an existing string, slice 782 // or *array X between optional integer bounds Low and High. 783 // 784 // Dynamically, this instruction panics if X evaluates to a nil *array 785 // pointer. 786 // 787 // Type() returns string if the type of X was string, otherwise a 788 // *types.Slice with the same element type as X. 789 // 790 // Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice 791 // operation, the ast.CompositeLit.Lbrace if created by a literal, or 792 // NoPos if not explicit in the source (e.g. a variadic argument slice). 793 // 794 // Example printed form: 795 // t1 = slice t0[1:] 796 // 797 type Slice struct { 798 register 799 X Value // slice, string, or *array 800 Low, High, Max Value // each may be nil 801 } 802 803 // The FieldAddr instruction yields the address of Field of *struct X. 804 // 805 // The field is identified by its index within the field list of the 806 // struct type of X. 807 // 808 // Dynamically, this instruction panics if X evaluates to a nil 809 // pointer. 810 // 811 // Type() returns a (possibly named) *types.Pointer. 812 // 813 // Pos() returns the position of the ast.SelectorExpr.Sel for the 814 // field, if explicit in the source. 815 // 816 // Example printed form: 817 // t1 = &t0.name [#1] 818 // 819 type FieldAddr struct { 820 register 821 X Value // *struct 822 Field int // index into X.Type().Deref().(*types.Struct).Fields 823 } 824 825 // The Field instruction yields the Field of struct X. 826 // 827 // The field is identified by its index within the field list of the 828 // struct type of X; by using numeric indices we avoid ambiguity of 829 // package-local identifiers and permit compact representations. 830 // 831 // Pos() returns the position of the ast.SelectorExpr.Sel for the 832 // field, if explicit in the source. 833 // 834 // Example printed form: 835 // t1 = t0.name [#1] 836 // 837 type Field struct { 838 register 839 X Value // struct 840 Field int // index into X.Type().(*types.Struct).Fields 841 } 842 843 // The IndexAddr instruction yields the address of the element at 844 // index Index of collection X. Index is an integer expression. 845 // 846 // The elements of maps and strings are not addressable; use Lookup or 847 // MapUpdate instead. 848 // 849 // Dynamically, this instruction panics if X evaluates to a nil *array 850 // pointer. 851 // 852 // Type() returns a (possibly named) *types.Pointer. 853 // 854 // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if 855 // explicit in the source. 856 // 857 // Example printed form: 858 // t2 = &t0[t1] 859 // 860 type IndexAddr struct { 861 register 862 X Value // slice or *array, 863 Index Value // numeric index 864 } 865 866 // The Index instruction yields element Index of array X. 867 // 868 // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if 869 // explicit in the source. 870 // 871 // Example printed form: 872 // t2 = t0[t1] 873 // 874 type Index struct { 875 register 876 X Value // array 877 Index Value // integer index 878 } 879 880 // The Lookup instruction yields element Index of collection X, a map 881 // or string. Index is an integer expression if X is a string or the 882 // appropriate key type if X is a map. 883 // 884 // If CommaOk, the result is a 2-tuple of the value above and a 885 // boolean indicating the result of a map membership test for the key. 886 // The components of the tuple are accessed using Extract. 887 // 888 // Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source. 889 // 890 // Example printed form: 891 // t2 = t0[t1] 892 // t5 = t3[t4],ok 893 // 894 type Lookup struct { 895 register 896 X Value // string or map 897 Index Value // numeric or key-typed index 898 CommaOk bool // return a value,ok pair 899 } 900 901 // SelectState is a helper for Select. 902 // It represents one goal state and its corresponding communication. 903 // 904 type SelectState struct { 905 Dir types.ChanDir // direction of case (SendOnly or RecvOnly) 906 Chan Value // channel to use (for send or receive) 907 Send Value // value to send (for send) 908 Pos token.Pos // position of token.ARROW 909 DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode] 910 } 911 912 // The Select instruction tests whether (or blocks until) one 913 // of the specified sent or received states is entered. 914 // 915 // Let n be the number of States for which Dir==RECV and T_i (0<=i<n) 916 // be the element type of each such state's Chan. 917 // Select returns an n+2-tuple 918 // (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1) 919 // The tuple's components, described below, must be accessed via the 920 // Extract instruction. 921 // 922 // If Blocking, select waits until exactly one state holds, i.e. a 923 // channel becomes ready for the designated operation of sending or 924 // receiving; select chooses one among the ready states 925 // pseudorandomly, performs the send or receive operation, and sets 926 // 'index' to the index of the chosen channel. 927 // 928 // If !Blocking, select doesn't block if no states hold; instead it 929 // returns immediately with index equal to -1. 930 // 931 // If the chosen channel was used for a receive, the r_i component is 932 // set to the received value, where i is the index of that state among 933 // all n receive states; otherwise r_i has the zero value of type T_i. 934 // Note that the receive index i is not the same as the state 935 // index index. 936 // 937 // The second component of the triple, recvOk, is a boolean whose value 938 // is true iff the selected operation was a receive and the receive 939 // successfully yielded a value. 940 // 941 // Pos() returns the ast.SelectStmt.Select. 942 // 943 // Example printed form: 944 // t3 = select nonblocking [<-t0, t1<-t2] 945 // t4 = select blocking [] 946 // 947 type Select struct { 948 register 949 States []*SelectState 950 Blocking bool 951 } 952 953 // The Range instruction yields an iterator over the domain and range 954 // of X, which must be a string or map. 955 // 956 // Elements are accessed via Next. 957 // 958 // Type() returns an opaque and degenerate "rangeIter" type. 959 // 960 // Pos() returns the ast.RangeStmt.For. 961 // 962 // Example printed form: 963 // t0 = range "hello":string 964 // 965 type Range struct { 966 register 967 X Value // string or map 968 } 969 970 // The Next instruction reads and advances the (map or string) 971 // iterator Iter and returns a 3-tuple value (ok, k, v). If the 972 // iterator is not exhausted, ok is true and k and v are the next 973 // elements of the domain and range, respectively. Otherwise ok is 974 // false and k and v are undefined. 975 // 976 // Components of the tuple are accessed using Extract. 977 // 978 // The IsString field distinguishes iterators over strings from those 979 // over maps, as the Type() alone is insufficient: consider 980 // map[int]rune. 981 // 982 // Type() returns a *types.Tuple for the triple (ok, k, v). 983 // The types of k and/or v may be types.Invalid. 984 // 985 // Example printed form: 986 // t1 = next t0 987 // 988 type Next struct { 989 register 990 Iter Value 991 IsString bool // true => string iterator; false => map iterator. 992 } 993 994 // The TypeAssert instruction tests whether interface value X has type 995 // AssertedType. 996 // 997 // If !CommaOk, on success it returns v, the result of the conversion 998 // (defined below); on failure it panics. 999 // 1000 // If CommaOk: on success it returns a pair (v, true) where v is the 1001 // result of the conversion; on failure it returns (z, false) where z 1002 // is AssertedType's zero value. The components of the pair must be 1003 // accessed using the Extract instruction. 1004 // 1005 // If AssertedType is a concrete type, TypeAssert checks whether the 1006 // dynamic type in interface X is equal to it, and if so, the result 1007 // of the conversion is a copy of the value in the interface. 1008 // 1009 // If AssertedType is an interface, TypeAssert checks whether the 1010 // dynamic type of the interface is assignable to it, and if so, the 1011 // result of the conversion is a copy of the interface value X. 1012 // If AssertedType is a superinterface of X.Type(), the operation will 1013 // fail iff the operand is nil. (Contrast with ChangeInterface, which 1014 // performs no nil-check.) 1015 // 1016 // Type() reflects the actual type of the result, possibly a 1017 // 2-types.Tuple; AssertedType is the asserted type. 1018 // 1019 // Pos() returns the ast.CallExpr.Lparen if the instruction arose from 1020 // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the 1021 // instruction arose from an explicit e.(T) operation; or the 1022 // ast.CaseClause.Case if the instruction arose from a case of a 1023 // type-switch statement. 1024 // 1025 // Example printed form: 1026 // t1 = typeassert t0.(int) 1027 // t3 = typeassert,ok t2.(T) 1028 // 1029 type TypeAssert struct { 1030 register 1031 X Value 1032 AssertedType types.Type 1033 CommaOk bool 1034 } 1035 1036 // The Extract instruction yields component Index of Tuple. 1037 // 1038 // This is used to access the results of instructions with multiple 1039 // return values, such as Call, TypeAssert, Next, UnOp(ARROW) and 1040 // IndexExpr(Map). 1041 // 1042 // Example printed form: 1043 // t1 = extract t0 #1 1044 // 1045 type Extract struct { 1046 register 1047 Tuple Value 1048 Index int 1049 } 1050 1051 // Instructions executed for effect. They do not yield a value. -------------------- 1052 1053 // The Jump instruction transfers control to the sole successor of its 1054 // owning block. 1055 // 1056 // A Jump must be the last instruction of its containing BasicBlock. 1057 // 1058 // Pos() returns NoPos. 1059 // 1060 // Example printed form: 1061 // jump done 1062 // 1063 type Jump struct { 1064 anInstruction 1065 } 1066 1067 // The If instruction transfers control to one of the two successors 1068 // of its owning block, depending on the boolean Cond: the first if 1069 // true, the second if false. 1070 // 1071 // An If instruction must be the last instruction of its containing 1072 // BasicBlock. 1073 // 1074 // Pos() returns NoPos. 1075 // 1076 // Example printed form: 1077 // if t0 goto done else body 1078 // 1079 type If struct { 1080 anInstruction 1081 Cond Value 1082 } 1083 1084 // The Return instruction returns values and control back to the calling 1085 // function. 1086 // 1087 // len(Results) is always equal to the number of results in the 1088 // function's signature. 1089 // 1090 // If len(Results) > 1, Return returns a tuple value with the specified 1091 // components which the caller must access using Extract instructions. 1092 // 1093 // There is no instruction to return a ready-made tuple like those 1094 // returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or 1095 // a tail-call to a function with multiple result parameters. 1096 // 1097 // Return must be the last instruction of its containing BasicBlock. 1098 // Such a block has no successors. 1099 // 1100 // Pos() returns the ast.ReturnStmt.Return, if explicit in the source. 1101 // 1102 // Example printed form: 1103 // return 1104 // return nil:I, 2:int 1105 // 1106 type Return struct { 1107 anInstruction 1108 Results []Value 1109 pos token.Pos 1110 } 1111 1112 // The RunDefers instruction pops and invokes the entire stack of 1113 // procedure calls pushed by Defer instructions in this function. 1114 // 1115 // It is legal to encounter multiple 'rundefers' instructions in a 1116 // single control-flow path through a function; this is useful in 1117 // the combined init() function, for example. 1118 // 1119 // Pos() returns NoPos. 1120 // 1121 // Example printed form: 1122 // rundefers 1123 // 1124 type RunDefers struct { 1125 anInstruction 1126 } 1127 1128 // The Panic instruction initiates a panic with value X. 1129 // 1130 // A Panic instruction must be the last instruction of its containing 1131 // BasicBlock, which must have no successors. 1132 // 1133 // NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction; 1134 // they are treated as calls to a built-in function. 1135 // 1136 // Pos() returns the ast.CallExpr.Lparen if this panic was explicit 1137 // in the source. 1138 // 1139 // Example printed form: 1140 // panic t0 1141 // 1142 type Panic struct { 1143 anInstruction 1144 X Value // an interface{} 1145 pos token.Pos 1146 } 1147 1148 // The Go instruction creates a new goroutine and calls the specified 1149 // function within it. 1150 // 1151 // See CallCommon for generic function call documentation. 1152 // 1153 // Pos() returns the ast.GoStmt.Go. 1154 // 1155 // Example printed form: 1156 // go println(t0, t1) 1157 // go t3() 1158 // go invoke t5.Println(...t6) 1159 // 1160 type Go struct { 1161 anInstruction 1162 Call CallCommon 1163 pos token.Pos 1164 } 1165 1166 // The Defer instruction pushes the specified call onto a stack of 1167 // functions to be called by a RunDefers instruction or by a panic. 1168 // 1169 // See CallCommon for generic function call documentation. 1170 // 1171 // Pos() returns the ast.DeferStmt.Defer. 1172 // 1173 // Example printed form: 1174 // defer println(t0, t1) 1175 // defer t3() 1176 // defer invoke t5.Println(...t6) 1177 // 1178 type Defer struct { 1179 anInstruction 1180 Call CallCommon 1181 pos token.Pos 1182 } 1183 1184 // The Send instruction sends X on channel Chan. 1185 // 1186 // Pos() returns the ast.SendStmt.Arrow, if explicit in the source. 1187 // 1188 // Example printed form: 1189 // send t0 <- t1 1190 // 1191 type Send struct { 1192 anInstruction 1193 Chan, X Value 1194 pos token.Pos 1195 } 1196 1197 // The Store instruction stores Val at address Addr. 1198 // Stores can be of arbitrary types. 1199 // 1200 // Pos() returns the position of the source-level construct most closely 1201 // associated with the memory store operation. 1202 // Since implicit memory stores are numerous and varied and depend upon 1203 // implementation choices, the details are not specified. 1204 // 1205 // Example printed form: 1206 // *x = y 1207 // 1208 type Store struct { 1209 anInstruction 1210 Addr Value 1211 Val Value 1212 pos token.Pos 1213 } 1214 1215 // The BlankStore instruction is emitted for assignments to the blank 1216 // identifier. 1217 // 1218 // BlankStore is a pseudo-instruction: it has no dynamic effect. 1219 // 1220 // Pos() returns NoPos. 1221 // 1222 // Example printed form: 1223 // _ = t0 1224 // 1225 type BlankStore struct { 1226 anInstruction 1227 Val Value 1228 } 1229 1230 // The MapUpdate instruction updates the association of Map[Key] to 1231 // Value. 1232 // 1233 // Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack, 1234 // if explicit in the source. 1235 // 1236 // Example printed form: 1237 // t0[t1] = t2 1238 // 1239 type MapUpdate struct { 1240 anInstruction 1241 Map Value 1242 Key Value 1243 Value Value 1244 pos token.Pos 1245 } 1246 1247 // A DebugRef instruction maps a source-level expression Expr to the 1248 // SSA value X that represents the value (!IsAddr) or address (IsAddr) 1249 // of that expression. 1250 // 1251 // DebugRef is a pseudo-instruction: it has no dynamic effect. 1252 // 1253 // Pos() returns Expr.Pos(), the start position of the source-level 1254 // expression. This is not the same as the "designated" token as 1255 // documented at Value.Pos(). e.g. CallExpr.Pos() does not return the 1256 // position of the ("designated") Lparen token. 1257 // 1258 // If Expr is an *ast.Ident denoting a var or func, Object() returns 1259 // the object; though this information can be obtained from the type 1260 // checker, including it here greatly facilitates debugging. 1261 // For non-Ident expressions, Object() returns nil. 1262 // 1263 // DebugRefs are generated only for functions built with debugging 1264 // enabled; see Package.SetDebugMode() and the GlobalDebug builder 1265 // mode flag. 1266 // 1267 // DebugRefs are not emitted for ast.Idents referring to constants or 1268 // predeclared identifiers, since they are trivial and numerous. 1269 // Nor are they emitted for ast.ParenExprs. 1270 // 1271 // (By representing these as instructions, rather than out-of-band, 1272 // consistency is maintained during transformation passes by the 1273 // ordinary SSA renaming machinery.) 1274 // 1275 // Example printed form: 1276 // ; *ast.CallExpr @ 102:9 is t5 1277 // ; var x float64 @ 109:72 is x 1278 // ; address of *ast.CompositeLit @ 216:10 is t0 1279 // 1280 type DebugRef struct { 1281 anInstruction 1282 Expr ast.Expr // the referring expression (never *ast.ParenExpr) 1283 object types.Object // the identity of the source var/func 1284 IsAddr bool // Expr is addressable and X is the address it denotes 1285 X Value // the value or address of Expr 1286 } 1287 1288 // Embeddable mix-ins and helpers for common parts of other structs. ----------- 1289 1290 // register is a mix-in embedded by all SSA values that are also 1291 // instructions, i.e. virtual registers, and provides a uniform 1292 // implementation of most of the Value interface: Value.Name() is a 1293 // numbered register (e.g. "t0"); the other methods are field accessors. 1294 // 1295 // Temporary names are automatically assigned to each register on 1296 // completion of building a function in SSA form. 1297 // 1298 // Clients must not assume that the 'id' value (and the Name() derived 1299 // from it) is unique within a function. As always in this API, 1300 // semantics are determined only by identity; names exist only to 1301 // facilitate debugging. 1302 // 1303 type register struct { 1304 anInstruction 1305 num int // "name" of virtual register, e.g. "t0". Not guaranteed unique. 1306 typ types.Type // type of virtual register 1307 pos token.Pos // position of source expression, or NoPos 1308 referrers []Instruction 1309 } 1310 1311 // anInstruction is a mix-in embedded by all Instructions. 1312 // It provides the implementations of the Block and setBlock methods. 1313 type anInstruction struct { 1314 block *BasicBlock // the basic block of this instruction 1315 } 1316 1317 // CallCommon is contained by Go, Defer and Call to hold the 1318 // common parts of a function or method call. 1319 // 1320 // Each CallCommon exists in one of two modes, function call and 1321 // interface method invocation, or "call" and "invoke" for short. 1322 // 1323 // 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon 1324 // represents an ordinary function call of the value in Value, 1325 // which may be a *Builtin, a *Function or any other value of kind 1326 // 'func'. 1327 // 1328 // Value may be one of: 1329 // (a) a *Function, indicating a statically dispatched call 1330 // to a package-level function, an anonymous function, or 1331 // a method of a named type. 1332 // (b) a *MakeClosure, indicating an immediately applied 1333 // function literal with free variables. 1334 // (c) a *Builtin, indicating a statically dispatched call 1335 // to a built-in function. 1336 // (d) any other value, indicating a dynamically dispatched 1337 // function call. 1338 // StaticCallee returns the identity of the callee in cases 1339 // (a) and (b), nil otherwise. 1340 // 1341 // Args contains the arguments to the call. If Value is a method, 1342 // Args[0] contains the receiver parameter. 1343 // 1344 // Example printed form: 1345 // t2 = println(t0, t1) 1346 // go t3() 1347 // defer t5(...t6) 1348 // 1349 // 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon 1350 // represents a dynamically dispatched call to an interface method. 1351 // In this mode, Value is the interface value and Method is the 1352 // interface's abstract method. Note: an abstract method may be 1353 // shared by multiple interfaces due to embedding; Value.Type() 1354 // provides the specific interface used for this call. 1355 // 1356 // Value is implicitly supplied to the concrete method implementation 1357 // as the receiver parameter; in other words, Args[0] holds not the 1358 // receiver but the first true argument. 1359 // 1360 // Example printed form: 1361 // t1 = invoke t0.String() 1362 // go invoke t3.Run(t2) 1363 // defer invoke t4.Handle(...t5) 1364 // 1365 // For all calls to variadic functions (Signature().Variadic()), 1366 // the last element of Args is a slice. 1367 // 1368 type CallCommon struct { 1369 Value Value // receiver (invoke mode) or func value (call mode) 1370 Method *types.Func // abstract method (invoke mode) 1371 Args []Value // actual parameters (in static method call, includes receiver) 1372 pos token.Pos // position of CallExpr.Lparen, iff explicit in source 1373 } 1374 1375 // IsInvoke returns true if this call has "invoke" (not "call") mode. 1376 func (c *CallCommon) IsInvoke() bool { 1377 return c.Method != nil 1378 } 1379 1380 func (c *CallCommon) Pos() token.Pos { return c.pos } 1381 1382 // Signature returns the signature of the called function. 1383 // 1384 // For an "invoke"-mode call, the signature of the interface method is 1385 // returned. 1386 // 1387 // In either "call" or "invoke" mode, if the callee is a method, its 1388 // receiver is represented by sig.Recv, not sig.Params().At(0). 1389 // 1390 func (c *CallCommon) Signature() *types.Signature { 1391 if c.Method != nil { 1392 return c.Method.Type().(*types.Signature) 1393 } 1394 return c.Value.Type().Underlying().(*types.Signature) 1395 } 1396 1397 // StaticCallee returns the callee if this is a trivially static 1398 // "call"-mode call to a function. 1399 func (c *CallCommon) StaticCallee() *Function { 1400 switch fn := c.Value.(type) { 1401 case *Function: 1402 return fn 1403 case *MakeClosure: 1404 return fn.Fn.(*Function) 1405 } 1406 return nil 1407 } 1408 1409 // Description returns a description of the mode of this call suitable 1410 // for a user interface, e.g., "static method call". 1411 func (c *CallCommon) Description() string { 1412 switch fn := c.Value.(type) { 1413 case *Builtin: 1414 return "built-in function call" 1415 case *MakeClosure: 1416 return "static function closure call" 1417 case *Function: 1418 if fn.Signature.Recv() != nil { 1419 return "static method call" 1420 } 1421 return "static function call" 1422 } 1423 if c.IsInvoke() { 1424 return "dynamic method call" // ("invoke" mode) 1425 } 1426 return "dynamic function call" 1427 } 1428 1429 // The CallInstruction interface, implemented by *Go, *Defer and *Call, 1430 // exposes the common parts of function-calling instructions, 1431 // yet provides a way back to the Value defined by *Call alone. 1432 // 1433 type CallInstruction interface { 1434 Instruction 1435 Common() *CallCommon // returns the common parts of the call 1436 Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer) 1437 } 1438 1439 func (s *Call) Common() *CallCommon { return &s.Call } 1440 func (s *Defer) Common() *CallCommon { return &s.Call } 1441 func (s *Go) Common() *CallCommon { return &s.Call } 1442 1443 func (s *Call) Value() *Call { return s } 1444 func (s *Defer) Value() *Call { return nil } 1445 func (s *Go) Value() *Call { return nil } 1446 1447 func (v *Builtin) Type() types.Type { return v.sig } 1448 func (v *Builtin) Name() string { return v.name } 1449 func (*Builtin) Referrers() *[]Instruction { return nil } 1450 func (v *Builtin) Pos() token.Pos { return token.NoPos } 1451 func (v *Builtin) Object() types.Object { return types.Universe.Lookup(v.name) } 1452 func (v *Builtin) Parent() *Function { return nil } 1453 1454 func (v *FreeVar) Type() types.Type { return v.typ } 1455 func (v *FreeVar) Name() string { return v.name } 1456 func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers } 1457 func (v *FreeVar) Pos() token.Pos { return v.pos } 1458 func (v *FreeVar) Parent() *Function { return v.parent } 1459 1460 func (v *Global) Type() types.Type { return v.typ } 1461 func (v *Global) Name() string { return v.name } 1462 func (v *Global) Parent() *Function { return nil } 1463 func (v *Global) Pos() token.Pos { return v.pos } 1464 func (v *Global) Referrers() *[]Instruction { return nil } 1465 func (v *Global) Token() token.Token { return token.VAR } 1466 func (v *Global) Object() types.Object { return v.object } 1467 func (v *Global) String() string { return v.RelString(nil) } 1468 func (v *Global) Package() *Package { return v.Pkg } 1469 func (v *Global) RelString(from *types.Package) string { return relString(v, from) } 1470 1471 func (v *Function) Name() string { return v.name } 1472 func (v *Function) Type() types.Type { return v.Signature } 1473 func (v *Function) Pos() token.Pos { return v.pos } 1474 func (v *Function) Token() token.Token { return token.FUNC } 1475 func (v *Function) Object() types.Object { return v.object } 1476 func (v *Function) String() string { return v.RelString(nil) } 1477 func (v *Function) Package() *Package { return v.Pkg } 1478 func (v *Function) Parent() *Function { return v.parent } 1479 func (v *Function) Referrers() *[]Instruction { 1480 if v.parent != nil { 1481 return &v.referrers 1482 } 1483 return nil 1484 } 1485 1486 func (v *Parameter) Type() types.Type { return v.typ } 1487 func (v *Parameter) Name() string { return v.name } 1488 func (v *Parameter) Object() types.Object { return v.object } 1489 func (v *Parameter) Referrers() *[]Instruction { return &v.referrers } 1490 func (v *Parameter) Pos() token.Pos { return v.pos } 1491 func (v *Parameter) Parent() *Function { return v.parent } 1492 1493 func (v *Alloc) Type() types.Type { return v.typ } 1494 func (v *Alloc) Referrers() *[]Instruction { return &v.referrers } 1495 func (v *Alloc) Pos() token.Pos { return v.pos } 1496 1497 func (v *register) Type() types.Type { return v.typ } 1498 func (v *register) setType(typ types.Type) { v.typ = typ } 1499 func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) } 1500 func (v *register) setNum(num int) { v.num = num } 1501 func (v *register) Referrers() *[]Instruction { return &v.referrers } 1502 func (v *register) Pos() token.Pos { return v.pos } 1503 func (v *register) setPos(pos token.Pos) { v.pos = pos } 1504 1505 func (v *anInstruction) Parent() *Function { return v.block.parent } 1506 func (v *anInstruction) Block() *BasicBlock { return v.block } 1507 func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block } 1508 func (v *anInstruction) Referrers() *[]Instruction { return nil } 1509 1510 func (t *Type) Name() string { return t.object.Name() } 1511 func (t *Type) Pos() token.Pos { return t.object.Pos() } 1512 func (t *Type) Type() types.Type { return t.object.Type() } 1513 func (t *Type) Token() token.Token { return token.TYPE } 1514 func (t *Type) Object() types.Object { return t.object } 1515 func (t *Type) String() string { return t.RelString(nil) } 1516 func (t *Type) Package() *Package { return t.pkg } 1517 func (t *Type) RelString(from *types.Package) string { return relString(t, from) } 1518 1519 func (c *NamedConst) Name() string { return c.object.Name() } 1520 func (c *NamedConst) Pos() token.Pos { return c.object.Pos() } 1521 func (c *NamedConst) String() string { return c.RelString(nil) } 1522 func (c *NamedConst) Type() types.Type { return c.object.Type() } 1523 func (c *NamedConst) Token() token.Token { return token.CONST } 1524 func (c *NamedConst) Object() types.Object { return c.object } 1525 func (c *NamedConst) Package() *Package { return c.pkg } 1526 func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) } 1527 1528 // Func returns the package-level function of the specified name, 1529 // or nil if not found. 1530 // 1531 func (p *Package) Func(name string) (f *Function) { 1532 f, _ = p.Members[name].(*Function) 1533 return 1534 } 1535 1536 // Var returns the package-level variable of the specified name, 1537 // or nil if not found. 1538 // 1539 func (p *Package) Var(name string) (g *Global) { 1540 g, _ = p.Members[name].(*Global) 1541 return 1542 } 1543 1544 // Const returns the package-level constant of the specified name, 1545 // or nil if not found. 1546 // 1547 func (p *Package) Const(name string) (c *NamedConst) { 1548 c, _ = p.Members[name].(*NamedConst) 1549 return 1550 } 1551 1552 // Type returns the package-level type of the specified name, 1553 // or nil if not found. 1554 // 1555 func (p *Package) Type(name string) (t *Type) { 1556 t, _ = p.Members[name].(*Type) 1557 return 1558 } 1559 1560 func (v *Call) Pos() token.Pos { return v.Call.pos } 1561 func (s *Defer) Pos() token.Pos { return s.pos } 1562 func (s *Go) Pos() token.Pos { return s.pos } 1563 func (s *MapUpdate) Pos() token.Pos { return s.pos } 1564 func (s *Panic) Pos() token.Pos { return s.pos } 1565 func (s *Return) Pos() token.Pos { return s.pos } 1566 func (s *Send) Pos() token.Pos { return s.pos } 1567 func (s *Store) Pos() token.Pos { return s.pos } 1568 func (s *BlankStore) Pos() token.Pos { return token.NoPos } 1569 func (s *If) Pos() token.Pos { return token.NoPos } 1570 func (s *Jump) Pos() token.Pos { return token.NoPos } 1571 func (s *RunDefers) Pos() token.Pos { return token.NoPos } 1572 func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() } 1573 1574 // Operands. 1575 1576 func (v *Alloc) Operands(rands []*Value) []*Value { 1577 return rands 1578 } 1579 1580 func (v *BinOp) Operands(rands []*Value) []*Value { 1581 return append(rands, &v.X, &v.Y) 1582 } 1583 1584 func (c *CallCommon) Operands(rands []*Value) []*Value { 1585 rands = append(rands, &c.Value) 1586 for i := range c.Args { 1587 rands = append(rands, &c.Args[i]) 1588 } 1589 return rands 1590 } 1591 1592 func (s *Go) Operands(rands []*Value) []*Value { 1593 return s.Call.Operands(rands) 1594 } 1595 1596 func (s *Call) Operands(rands []*Value) []*Value { 1597 return s.Call.Operands(rands) 1598 } 1599 1600 func (s *Defer) Operands(rands []*Value) []*Value { 1601 return s.Call.Operands(rands) 1602 } 1603 1604 func (v *ChangeInterface) Operands(rands []*Value) []*Value { 1605 return append(rands, &v.X) 1606 } 1607 1608 func (v *ChangeType) Operands(rands []*Value) []*Value { 1609 return append(rands, &v.X) 1610 } 1611 1612 func (v *Convert) Operands(rands []*Value) []*Value { 1613 return append(rands, &v.X) 1614 } 1615 1616 func (s *DebugRef) Operands(rands []*Value) []*Value { 1617 return append(rands, &s.X) 1618 } 1619 1620 func (v *Extract) Operands(rands []*Value) []*Value { 1621 return append(rands, &v.Tuple) 1622 } 1623 1624 func (v *Field) Operands(rands []*Value) []*Value { 1625 return append(rands, &v.X) 1626 } 1627 1628 func (v *FieldAddr) Operands(rands []*Value) []*Value { 1629 return append(rands, &v.X) 1630 } 1631 1632 func (s *If) Operands(rands []*Value) []*Value { 1633 return append(rands, &s.Cond) 1634 } 1635 1636 func (v *Index) Operands(rands []*Value) []*Value { 1637 return append(rands, &v.X, &v.Index) 1638 } 1639 1640 func (v *IndexAddr) Operands(rands []*Value) []*Value { 1641 return append(rands, &v.X, &v.Index) 1642 } 1643 1644 func (*Jump) Operands(rands []*Value) []*Value { 1645 return rands 1646 } 1647 1648 func (v *Lookup) Operands(rands []*Value) []*Value { 1649 return append(rands, &v.X, &v.Index) 1650 } 1651 1652 func (v *MakeChan) Operands(rands []*Value) []*Value { 1653 return append(rands, &v.Size) 1654 } 1655 1656 func (v *MakeClosure) Operands(rands []*Value) []*Value { 1657 rands = append(rands, &v.Fn) 1658 for i := range v.Bindings { 1659 rands = append(rands, &v.Bindings[i]) 1660 } 1661 return rands 1662 } 1663 1664 func (v *MakeInterface) Operands(rands []*Value) []*Value { 1665 return append(rands, &v.X) 1666 } 1667 1668 func (v *MakeMap) Operands(rands []*Value) []*Value { 1669 return append(rands, &v.Reserve) 1670 } 1671 1672 func (v *MakeSlice) Operands(rands []*Value) []*Value { 1673 return append(rands, &v.Len, &v.Cap) 1674 } 1675 1676 func (v *MapUpdate) Operands(rands []*Value) []*Value { 1677 return append(rands, &v.Map, &v.Key, &v.Value) 1678 } 1679 1680 func (v *Next) Operands(rands []*Value) []*Value { 1681 return append(rands, &v.Iter) 1682 } 1683 1684 func (s *Panic) Operands(rands []*Value) []*Value { 1685 return append(rands, &s.X) 1686 } 1687 1688 func (v *Sigma) Operands(rands []*Value) []*Value { 1689 return append(rands, &v.X) 1690 } 1691 1692 func (v *Phi) Operands(rands []*Value) []*Value { 1693 for i := range v.Edges { 1694 rands = append(rands, &v.Edges[i]) 1695 } 1696 return rands 1697 } 1698 1699 func (v *Range) Operands(rands []*Value) []*Value { 1700 return append(rands, &v.X) 1701 } 1702 1703 func (s *Return) Operands(rands []*Value) []*Value { 1704 for i := range s.Results { 1705 rands = append(rands, &s.Results[i]) 1706 } 1707 return rands 1708 } 1709 1710 func (*RunDefers) Operands(rands []*Value) []*Value { 1711 return rands 1712 } 1713 1714 func (v *Select) Operands(rands []*Value) []*Value { 1715 for i := range v.States { 1716 rands = append(rands, &v.States[i].Chan, &v.States[i].Send) 1717 } 1718 return rands 1719 } 1720 1721 func (s *Send) Operands(rands []*Value) []*Value { 1722 return append(rands, &s.Chan, &s.X) 1723 } 1724 1725 func (v *Slice) Operands(rands []*Value) []*Value { 1726 return append(rands, &v.X, &v.Low, &v.High, &v.Max) 1727 } 1728 1729 func (s *Store) Operands(rands []*Value) []*Value { 1730 return append(rands, &s.Addr, &s.Val) 1731 } 1732 1733 func (s *BlankStore) Operands(rands []*Value) []*Value { 1734 return append(rands, &s.Val) 1735 } 1736 1737 func (v *TypeAssert) Operands(rands []*Value) []*Value { 1738 return append(rands, &v.X) 1739 } 1740 1741 func (v *UnOp) Operands(rands []*Value) []*Value { 1742 return append(rands, &v.X) 1743 } 1744 1745 // Non-Instruction Values: 1746 func (v *Builtin) Operands(rands []*Value) []*Value { return rands } 1747 func (v *FreeVar) Operands(rands []*Value) []*Value { return rands } 1748 func (v *Const) Operands(rands []*Value) []*Value { return rands } 1749 func (v *Function) Operands(rands []*Value) []*Value { return rands } 1750 func (v *Global) Operands(rands []*Value) []*Value { return rands } 1751 func (v *Parameter) Operands(rands []*Value) []*Value { return rands }