You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.6 KiB

  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. // To construct an SSA-form program, call ssautil.CreateProgram on a
  26. // loader.Program, a set of type-checked packages created from
  27. // parsed Go source files. The resulting ssa.Program contains all the
  28. // packages and their members, but SSA code is not created for
  29. // function bodies until a subsequent call to (*Package).Build.
  30. //
  31. // The builder initially builds a naive SSA form in which all local
  32. // variables are addresses of stack locations with explicit loads and
  33. // stores. Registerisation of eligible locals and φ-node insertion
  34. // using dominance and dataflow are then performed as a second pass
  35. // called "lifting" to improve the accuracy and performance of
  36. // subsequent analyses; this pass can be skipped by setting the
  37. // NaiveForm builder flag.
  38. //
  39. // The primary interfaces of this package are:
  40. //
  41. // - Member: a named member of a Go package.
  42. // - Value: an expression that yields a value.
  43. // - Instruction: a statement that consumes values and performs computation.
  44. // - Node: a Value or Instruction (emphasizing its membership in the SSA value graph)
  45. //
  46. // A computation that yields a result implements both the Value and
  47. // Instruction interfaces. The following table shows for each
  48. // concrete type which of these interfaces it implements.
  49. //
  50. // Value? Instruction? Member?
  51. // *Alloc ✔ ✔
  52. // *BinOp ✔ ✔
  53. // *Builtin ✔
  54. // *Call ✔ ✔
  55. // *ChangeInterface ✔ ✔
  56. // *ChangeType ✔ ✔
  57. // *Const ✔
  58. // *Convert ✔ ✔
  59. // *DebugRef ✔
  60. // *Defer ✔
  61. // *Extract ✔ ✔
  62. // *Field ✔ ✔
  63. // *FieldAddr ✔ ✔
  64. // *FreeVar ✔
  65. // *Function ✔ ✔ (func)
  66. // *Global ✔ ✔ (var)
  67. // *Go ✔
  68. // *If ✔
  69. // *Index ✔ ✔
  70. // *IndexAddr ✔ ✔
  71. // *Jump ✔
  72. // *Lookup ✔ ✔
  73. // *MakeChan ✔ ✔
  74. // *MakeClosure ✔ ✔
  75. // *MakeInterface ✔ ✔
  76. // *MakeMap ✔ ✔
  77. // *MakeSlice ✔ ✔
  78. // *MapUpdate ✔
  79. // *NamedConst ✔ (const)
  80. // *Next ✔ ✔
  81. // *Panic ✔
  82. // *Parameter ✔
  83. // *Phi ✔ ✔
  84. // *Range ✔ ✔
  85. // *Return ✔
  86. // *RunDefers ✔
  87. // *Select ✔ ✔
  88. // *Send ✔
  89. // *Slice ✔ ✔
  90. // *Store ✔
  91. // *Type ✔ (type)
  92. // *TypeAssert ✔ ✔
  93. // *UnOp ✔ ✔
  94. //
  95. // Other key types in this package include: Program, Package, Function
  96. // and BasicBlock.
  97. //
  98. // The program representation constructed by this package is fully
  99. // resolved internally, i.e. it does not rely on the names of Values,
  100. // Packages, Functions, Types or BasicBlocks for the correct
  101. // interpretation of the program. Only the identities of objects and
  102. // the topology of the SSA and type graphs are semantically
  103. // significant. (There is one exception: Ids, used to identify field
  104. // and method names, contain strings.) Avoidance of name-based
  105. // operations simplifies the implementation of subsequent passes and
  106. // can make them very efficient. Many objects are nonetheless named
  107. // to aid in debugging, but it is not essential that the names be
  108. // either accurate or unambiguous. The public API exposes a number of
  109. // name-based maps for client convenience.
  110. //
  111. // The ssa/ssautil package provides various utilities that depend only
  112. // on the public API of this package.
  113. //
  114. // TODO(adonovan): Consider the exceptional control-flow implications
  115. // of defer and recover().
  116. //
  117. // TODO(adonovan): write a how-to document for all the various cases
  118. // of trying to determine corresponding elements across the four
  119. // domains of source locations, ast.Nodes, types.Objects,
  120. // ssa.Values/Instructions.
  121. //
  122. package ssa // import "golang.org/x/tools/go/ssa"