Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../lib/golang/src/cmd/compile
File: README.md
<!---
[0] Fix | Delete
// Copyright 2018 The Go Authors. All rights reserved.
[1] Fix | Delete
// Use of this source code is governed by a BSD-style
[2] Fix | Delete
// license that can be found in the LICENSE file.
[3] Fix | Delete
-->
[4] Fix | Delete
[5] Fix | Delete
## Introduction to the Go compiler
[6] Fix | Delete
[7] Fix | Delete
`cmd/compile` contains the main packages that form the Go compiler. The compiler
[8] Fix | Delete
may be logically split in four phases, which we will briefly describe alongside
[9] Fix | Delete
the list of packages that contain their code.
[10] Fix | Delete
[11] Fix | Delete
You may sometimes hear the terms "front-end" and "back-end" when referring to
[12] Fix | Delete
the compiler. Roughly speaking, these translate to the first two and last two
[13] Fix | Delete
phases we are going to list here. A third term, "middle-end", often refers to
[14] Fix | Delete
much of the work that happens in the second phase.
[15] Fix | Delete
[16] Fix | Delete
Note that the `go/*` family of packages, such as `go/parser` and
[17] Fix | Delete
`go/types`, are mostly unused by the compiler. Since the compiler was
[18] Fix | Delete
initially written in C, the `go/*` packages were developed to enable
[19] Fix | Delete
writing tools working with Go code, such as `gofmt` and `vet`.
[20] Fix | Delete
However, over time the compiler's internal APIs have slowly evolved to
[21] Fix | Delete
be more familiar to users of the `go/*` packages.
[22] Fix | Delete
[23] Fix | Delete
It should be clarified that the name "gc" stands for "Go compiler", and has
[24] Fix | Delete
little to do with uppercase "GC", which stands for garbage collection.
[25] Fix | Delete
[26] Fix | Delete
### 1. Parsing
[27] Fix | Delete
[28] Fix | Delete
* `cmd/compile/internal/syntax` (lexer, parser, syntax tree)
[29] Fix | Delete
[30] Fix | Delete
In the first phase of compilation, source code is tokenized (lexical analysis),
[31] Fix | Delete
parsed (syntax analysis), and a syntax tree is constructed for each source
[32] Fix | Delete
file.
[33] Fix | Delete
[34] Fix | Delete
Each syntax tree is an exact representation of the respective source file, with
[35] Fix | Delete
nodes corresponding to the various elements of the source such as expressions,
[36] Fix | Delete
declarations, and statements. The syntax tree also includes position information
[37] Fix | Delete
which is used for error reporting and the creation of debugging information.
[38] Fix | Delete
[39] Fix | Delete
### 2. Type checking
[40] Fix | Delete
[41] Fix | Delete
* `cmd/compile/internal/types2` (type checking)
[42] Fix | Delete
[43] Fix | Delete
The types2 package is a port of `go/types` to use the syntax package's
[44] Fix | Delete
AST instead of `go/ast`.
[45] Fix | Delete
[46] Fix | Delete
### 3. IR construction ("noding")
[47] Fix | Delete
[48] Fix | Delete
* `cmd/compile/internal/types` (compiler types)
[49] Fix | Delete
* `cmd/compile/internal/ir` (compiler AST)
[50] Fix | Delete
* `cmd/compile/internal/typecheck` (AST transformations)
[51] Fix | Delete
* `cmd/compile/internal/noder` (create compiler AST)
[52] Fix | Delete
[53] Fix | Delete
The compiler middle end uses its own AST definition and representation of Go
[54] Fix | Delete
types carried over from when it was written in C. All of its code is written in
[55] Fix | Delete
terms of these, so the next step after type checking is to convert the syntax
[56] Fix | Delete
and types2 representations to ir and types. This process is referred to as
[57] Fix | Delete
"noding."
[58] Fix | Delete
[59] Fix | Delete
There are currently two noding implementations:
[60] Fix | Delete
[61] Fix | Delete
1. irgen (aka "-G=3" or sometimes "noder2") is the implementation used starting
[62] Fix | Delete
with Go 1.18, and
[63] Fix | Delete
[64] Fix | Delete
2. Unified IR is another, in-development implementation (enabled with
[65] Fix | Delete
`GOEXPERIMENT=unified`), which also implements import/export and inlining.
[66] Fix | Delete
[67] Fix | Delete
Up through Go 1.18, there was a third noding implementation (just
[68] Fix | Delete
"noder" or "-G=0"), which directly converted the pre-type-checked
[69] Fix | Delete
syntax representation into IR and then invoked package typecheck's
[70] Fix | Delete
type checker. This implementation was removed after Go 1.18, so now
[71] Fix | Delete
package typecheck is only used for IR transformations.
[72] Fix | Delete
[73] Fix | Delete
### 4. Middle end
[74] Fix | Delete
[75] Fix | Delete
* `cmd/compile/internal/deadcode` (dead code elimination)
[76] Fix | Delete
* `cmd/compile/internal/inline` (function call inlining)
[77] Fix | Delete
* `cmd/compile/internal/devirtualize` (devirtualization of known interface method calls)
[78] Fix | Delete
* `cmd/compile/internal/escape` (escape analysis)
[79] Fix | Delete
[80] Fix | Delete
Several optimization passes are performed on the IR representation:
[81] Fix | Delete
dead code elimination, (early) devirtualization, function call
[82] Fix | Delete
inlining, and escape analysis.
[83] Fix | Delete
[84] Fix | Delete
### 5. Walk
[85] Fix | Delete
[86] Fix | Delete
* `cmd/compile/internal/walk` (order of evaluation, desugaring)
[87] Fix | Delete
[88] Fix | Delete
The final pass over the IR representation is "walk," which serves two purposes:
[89] Fix | Delete
[90] Fix | Delete
1. It decomposes complex statements into individual, simpler statements,
[91] Fix | Delete
introducing temporary variables and respecting order of evaluation. This step
[92] Fix | Delete
is also referred to as "order."
[93] Fix | Delete
[94] Fix | Delete
2. It desugars higher-level Go constructs into more primitive ones. For example,
[95] Fix | Delete
`switch` statements are turned into binary search or jump tables, and
[96] Fix | Delete
operations on maps and channels are replaced with runtime calls.
[97] Fix | Delete
[98] Fix | Delete
### 6. Generic SSA
[99] Fix | Delete
[100] Fix | Delete
* `cmd/compile/internal/ssa` (SSA passes and rules)
[101] Fix | Delete
* `cmd/compile/internal/ssagen` (converting IR to SSA)
[102] Fix | Delete
[103] Fix | Delete
In this phase, IR is converted into Static Single Assignment (SSA) form, a
[104] Fix | Delete
lower-level intermediate representation with specific properties that make it
[105] Fix | Delete
easier to implement optimizations and to eventually generate machine code from
[106] Fix | Delete
it.
[107] Fix | Delete
[108] Fix | Delete
During this conversion, function intrinsics are applied. These are special
[109] Fix | Delete
functions that the compiler has been taught to replace with heavily optimized
[110] Fix | Delete
code on a case-by-case basis.
[111] Fix | Delete
[112] Fix | Delete
Certain nodes are also lowered into simpler components during the AST to SSA
[113] Fix | Delete
conversion, so that the rest of the compiler can work with them. For instance,
[114] Fix | Delete
the copy builtin is replaced by memory moves, and range loops are rewritten into
[115] Fix | Delete
for loops. Some of these currently happen before the conversion to SSA due to
[116] Fix | Delete
historical reasons, but the long-term plan is to move all of them here.
[117] Fix | Delete
[118] Fix | Delete
Then, a series of machine-independent passes and rules are applied. These do not
[119] Fix | Delete
concern any single computer architecture, and thus run on all `GOARCH` variants.
[120] Fix | Delete
These passes include dead code elimination, removal of
[121] Fix | Delete
unneeded nil checks, and removal of unused branches. The generic rewrite rules
[122] Fix | Delete
mainly concern expressions, such as replacing some expressions with constant
[123] Fix | Delete
values, and optimizing multiplications and float operations.
[124] Fix | Delete
[125] Fix | Delete
### 7. Generating machine code
[126] Fix | Delete
[127] Fix | Delete
* `cmd/compile/internal/ssa` (SSA lowering and arch-specific passes)
[128] Fix | Delete
* `cmd/internal/obj` (machine code generation)
[129] Fix | Delete
[130] Fix | Delete
The machine-dependent phase of the compiler begins with the "lower" pass, which
[131] Fix | Delete
rewrites generic values into their machine-specific variants. For example, on
[132] Fix | Delete
amd64 memory operands are possible, so many load-store operations may be combined.
[133] Fix | Delete
[134] Fix | Delete
Note that the lower pass runs all machine-specific rewrite rules, and thus it
[135] Fix | Delete
currently applies lots of optimizations too.
[136] Fix | Delete
[137] Fix | Delete
Once the SSA has been "lowered" and is more specific to the target architecture,
[138] Fix | Delete
the final code optimization passes are run. This includes yet another dead code
[139] Fix | Delete
elimination pass, moving values closer to their uses, the removal of local
[140] Fix | Delete
variables that are never read from, and register allocation.
[141] Fix | Delete
[142] Fix | Delete
Other important pieces of work done as part of this step include stack frame
[143] Fix | Delete
layout, which assigns stack offsets to local variables, and pointer liveness
[144] Fix | Delete
analysis, which computes which on-stack pointers are live at each GC safe point.
[145] Fix | Delete
[146] Fix | Delete
At the end of the SSA generation phase, Go functions have been transformed into
[147] Fix | Delete
a series of obj.Prog instructions. These are passed to the assembler
[148] Fix | Delete
(`cmd/internal/obj`), which turns them into machine code and writes out the
[149] Fix | Delete
final object file. The object file will also contain reflect data, export data,
[150] Fix | Delete
and debugging information.
[151] Fix | Delete
[152] Fix | Delete
### Further reading
[153] Fix | Delete
[154] Fix | Delete
To dig deeper into how the SSA package works, including its passes and rules,
[155] Fix | Delete
head to [cmd/compile/internal/ssa/README.md](internal/ssa/README.md).
[156] Fix | Delete
[157] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function