doc.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 defines a representation of the elements of Go programs
  5. // (packages, types, functions, variables and constants) using a
  6. // static single-assignment (SSA) form intermediate representation
  7. // (IR) for the bodies of functions.
  8. //
  9. // THIS INTERFACE IS EXPERIMENTAL AND IS LIKELY TO CHANGE.
  10. //
  11. // For an introduction to SSA form, see
  12. // http://en.wikipedia.org/wiki/Static_single_assignment_form.
  13. // This page provides a broader reading list:
  14. // http://www.dcs.gla.ac.uk/~jsinger/ssa.html.
  15. //
  16. // The level of abstraction of the SSA form is intentionally close to
  17. // the source language to facilitate construction of source analysis
  18. // tools. It is not intended for machine code generation.
  19. //
  20. // All looping, branching and switching constructs are replaced with
  21. // unstructured control flow. Higher-level control flow constructs
  22. // such as multi-way branch can be reconstructed as needed; see
  23. // ssautil.Switches() for an example.
  24. //
  25. // The simplest way to create the SSA representation of a package is
  26. // to load typed syntax trees using golang.org/x/tools/go/packages, then
  27. // invoke the ssautil.Packages helper function. See ExampleLoadPackages
  28. // and ExampleWholeProgram for examples.
  29. // The resulting ssa.Program contains all the packages and their
  30. // members, but SSA code is not created for function bodies until a
  31. // subsequent call to (*Package).Build or (*Program).Build.
  32. //
  33. // The builder initially builds a naive SSA form in which all local
  34. // variables are addresses of stack locations with explicit loads and
  35. // stores. Registerisation of eligible locals and φ-node insertion
  36. // using dominance and dataflow are then performed as a second pass
  37. // called "lifting" to improve the accuracy and performance of
  38. // subsequent analyses; this pass can be skipped by setting the
  39. // NaiveForm builder flag.
  40. //
  41. // The primary interfaces of this package are:
  42. //
  43. // - Member: a named member of a Go package.
  44. // - Value: an expression that yields a value.
  45. // - Instruction: a statement that consumes values and performs computation.
  46. // - Node: a Value or Instruction (emphasizing its membership in the SSA value graph)
  47. //
  48. // A computation that yields a result implements both the Value and
  49. // Instruction interfaces. The following table shows for each
  50. // concrete type which of these interfaces it implements.
  51. //
  52. // Value? Instruction? Member?
  53. // *Alloc ✔ ✔
  54. // *BinOp ✔ ✔
  55. // *Builtin ✔
  56. // *Call ✔ ✔
  57. // *ChangeInterface ✔ ✔
  58. // *ChangeType ✔ ✔
  59. // *Const ✔
  60. // *Convert ✔ ✔
  61. // *DebugRef ✔
  62. // *Defer ✔
  63. // *Extract ✔ ✔
  64. // *Field ✔ ✔
  65. // *FieldAddr ✔ ✔
  66. // *FreeVar ✔
  67. // *Function ✔ ✔ (func)
  68. // *Global ✔ ✔ (var)
  69. // *Go ✔
  70. // *If ✔
  71. // *Index ✔ ✔
  72. // *IndexAddr ✔ ✔
  73. // *Jump ✔
  74. // *Lookup ✔ ✔
  75. // *MakeChan ✔ ✔
  76. // *MakeClosure ✔ ✔
  77. // *MakeInterface ✔ ✔
  78. // *MakeMap ✔ ✔
  79. // *MakeSlice ✔ ✔
  80. // *MapUpdate ✔
  81. // *NamedConst ✔ (const)
  82. // *Next ✔ ✔
  83. // *Panic ✔
  84. // *Parameter ✔
  85. // *Phi ✔ ✔
  86. // *Range ✔ ✔
  87. // *Return ✔
  88. // *RunDefers ✔
  89. // *Select ✔ ✔
  90. // *Send ✔
  91. // *Slice ✔ ✔
  92. // *Store ✔
  93. // *Type ✔ (type)
  94. // *TypeAssert ✔ ✔
  95. // *UnOp ✔ ✔
  96. //
  97. // Other key types in this package include: Program, Package, Function
  98. // and BasicBlock.
  99. //
  100. // The program representation constructed by this package is fully
  101. // resolved internally, i.e. it does not rely on the names of Values,
  102. // Packages, Functions, Types or BasicBlocks for the correct
  103. // interpretation of the program. Only the identities of objects and
  104. // the topology of the SSA and type graphs are semantically
  105. // significant. (There is one exception: Ids, used to identify field
  106. // and method names, contain strings.) Avoidance of name-based
  107. // operations simplifies the implementation of subsequent passes and
  108. // can make them very efficient. Many objects are nonetheless named
  109. // to aid in debugging, but it is not essential that the names be
  110. // either accurate or unambiguous. The public API exposes a number of
  111. // name-based maps for client convenience.
  112. //
  113. // The ssa/ssautil package provides various utilities that depend only
  114. // on the public API of this package.
  115. //
  116. // TODO(adonovan): Consider the exceptional control-flow implications
  117. // of defer and recover().
  118. //
  119. // TODO(adonovan): write a how-to document for all the various cases
  120. // of trying to determine corresponding elements across the four
  121. // domains of source locations, ast.Nodes, types.Objects,
  122. // ssa.Values/Instructions.
  123. //
  124. package ssa // import "honnef.co/go/tools/ssa"