ssa.go 56 KB

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