Debian Codegraph @

go/ir/doc.go

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