compile

command
Version: v0.0.0-...-e6da185 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 15 Imported by: 0

README

Go 编译器简介

cmd/compile 包含构成Go编译器的主包。编译器可以在逻辑上分为四个阶段,我们将简要介绍包含其代码的包列表。

我们有时可能会使用“前端”和“后端”这两个术语编译器。粗略地说,这些转化为前两个和后两个阶段我们将在这里列出的阶段。第三个术语,“中端”,通常指的是在第二阶段发生的大部分工作。

注意 go/* 系列包,比如 go/parser 和 go/types ,与编译器无关。由于编译器最初是用C编写的,开发go/* 包以使用 Go代码编写工具,例如 gofmt 和 vet

应该澄清的是,名称 “gc” 代表 “Go编译器”,并且具有与大写 “GC” 无关,它代表垃圾收集。

1. 解析

  • cmd/compile/internal/syntax(词法分析器,解析器,语法树)

在编译的第一阶段,源代码被 token 化(词法分析),解析(语法分析),并为每个源构造语法树文件。每个语法树都是相应源文件的精确表示对应于源的各种元素的节点,如表达式,声明和陈述。语法树还包括位置信息用于错误报告和调试信息的创建。

2. 类型检查与 AST 转换

  • cmd/compile/internal/gc(创建编译器AST,类型检查,AST转换)

gc 包中包含了一个从 C 语言开始编写的 AST 定义。它的所有代码都是根据它编写的,所以 gc 包必须做的第一件事就是将语法包的语法树转换为编译器的 AST 表示。这个额外的步骤可能会在将来重构。

然后对 AST 进行类型检查。第一步是名称解析和类型推断,它们确定哪个对象属于哪个标识符,以及每个表达式具有的类型。类型检查包括某些额外的检查,例如 “声明和未使用” 以及确定函数是否终止。

在 AST 上也进行了某些转换。一些节点基于类型信息被细化,例如从算术加法节点类型分割的字符串添加。其他一些例子是死代码消除,函数调用内联和转义分析。

3. 泛用型 SSA

  • cmd/compile/internal/gc (转换为SSA)
  • cmd/compile/internal/ssa (SSA 传递与规则)

在此阶段,AST将转换为静态单一分配(SSA)形式,这是一种具有特定属性的低级中间表示,可以更轻松地实现优化并最终从中生成机器代码。

在此转换期间,将应用函数内在函数。 这些是特殊功能,编译器已经教导它们根据具体情况用大量优化的代码替换。

在AST到SSA转换期间,某些节点也被降级为更简单的组件,因此编译器的其余部分可以使用它们。 例如,内置复制替换为内存移动,并且范围循环被重写为for循环。 其中一些目前发生在转换为SSA之前,由于历史原因,但长期计划是将所有这些都移到这里。

然后,应用一系列与机器无关的传递和规则。 这些不涉及任何单个计算机体系结构,因此可以在所有 GOARCH 变体上运行。

这些通用过程的一些示例包括消除死代码,删除不需要的零检查以及删除未使用的分支。通用重写规则主要涉及表达式,例如用常量值替换某些表达式,以及优化乘法和浮点运算。

4. 生成机器码

  • cmd/compile/internal/ssa (底层SSA和架构特定的传递)
  • cmd/internal/obj (生成机器码)

编译器的机器相关阶段以“底层”传递开始,该传递将通用值重写为其机器特定的变体。例如,在 amd64 存储器操作数上是可能的,因此可以组合许多加载存储操作。

请注意,较低的通道运行所有特定于机器的重写规则,因此它当前也应用了大量优化。

一旦SSA“降低”并且更加特定于目标体系结构,就会运行最终的代码优化过程。这包括另一个死代码消除传递,移动值更接近它们的使用,删除从未读取的局部变量,以及寄存器分配。

作为此步骤的一部分完成的其他重要工作包括堆栈框架布局,它将堆栈偏移分配给局部变量,以及指针活动分析,它计算每个 GC 安全点上的堆栈指针。

在SSA生成阶段结束时,Go 函数已转换为一系列 obj.Prog 指令。它们被传递给汇编程序(cmd/internal/obj),它将它们转换为机器代码并写出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。

进一步阅读

若需进一步了解 SSA 包的工作原理,请阅读 cmd/compile/internal/ssa/README.md.

Documentation

Overview

Compile, typically invoked as “go tool compile,” compiles a single Go package comprising the files named on the command line. It then writes a single object file named for the basename of the first source file with a .o suffix. The object file can then be combined with other objects into a package archive or passed directly to the linker (“go tool link”). If invoked with -pack, the compiler writes an archive directly, bypassing the intermediate object file.

The generated files contain type information about the symbols exported by the package and about types used by symbols imported by the package from other packages. It is therefore not necessary when compiling client C of package P to read the files of P's dependencies, only the compiled output of P.

Command Line

Usage:

go tool compile [flags] file...

The specified files must be Go source files and all part of the same package. The same compiler is used for all target operating systems and architectures. The GOOS and GOARCH environment variables set the desired target.

Flags:

-D path
	Set relative path for local imports.
-I dir1 -I dir2
	Search for imported packages in dir1, dir2, etc,
	after consulting $GOROOT/pkg/$GOOS_$GOARCH.
-L
	Show complete file path in error messages.
-N
	Disable optimizations.
-S
	Print assembly listing to standard output (code only).
-S -S
	Print assembly listing to standard output (code and data).
-V
	Print compiler version and exit.
-asmhdr file
	Write assembly header to file.
-buildid id
	Record id as the build id in the export metadata.
-blockprofile file
	Write block profile for the compilation to file.
-c int
	Concurrency during compilation. Set 1 for no concurrency (default is 1).
-complete
	Assume package has no non-Go components.
-cpuprofile file
	Write a CPU profile for the compilation to file.
-dynlink
	Allow references to Go symbols in shared libraries (experimental).
-e
	Remove the limit on the number of errors reported (default limit is 10).
-goversion string
	Specify required go tool version of the runtime.
	Exits when the runtime go version does not match goversion.
-h
	Halt with a stack trace at the first error detected.
-importcfg file
	Read import configuration from file.
	In the file, set importmap, packagefile to specify import resolution.
-importmap old=new
	Interpret import "old" as import "new" during compilation.
	The option may be repeated to add multiple mappings.
-installsuffix suffix
	Look for packages in $GOROOT/pkg/$GOOS_$GOARCH_suffix
	instead of $GOROOT/pkg/$GOOS_$GOARCH.
-l
	Disable inlining.
-lang version
	Set language version to compile, as in -lang=go1.12.
	Default is current version.
-largemodel
	Generate code that assumes a large memory model.
-linkobj file
	Write linker-specific object to file and compiler-specific
	object to usual output file (as specified by -o).
	Without this flag, the -o output is a combination of both
	linker and compiler input.
-m
	Print optimization decisions.
-memprofile file
	Write memory profile for the compilation to file.
-memprofilerate rate
	Set runtime.MemProfileRate for the compilation to rate.
-msan
	Insert calls to C/C++ memory sanitizer.
-mutexprofile file
	Write mutex profile for the compilation to file.
-nolocalimports
	Disallow local (relative) imports.
-o file
	Write object to file (default file.o or, with -pack, file.a).
-p path
	Set expected package import path for the code being compiled,
	and diagnose imports that would cause a circular dependency.
-pack
	Write a package (archive) file rather than an object file
-race
	Compile with race detector enabled.
-s
	Warn about composite literals that can be simplified.
-shared
	Generate code that can be linked into a shared library.
-traceprofile file
	Write an execution trace to file.
-trimpath prefix
	Remove prefix from recorded source file paths.

Flags related to debugging information:

-dwarf
	Generate DWARF symbols.
-dwarflocationlists
	Add location lists to DWARF in optimized mode.
-gendwarfinl int
	Generate DWARF inline info records (default 2).

Flags to debug the compiler itself:

-E
	Debug symbol export.
-K
	Debug missing line numbers.
-d list
	Print debug information about items in list. Try -d help for further information.
-live
	Debug liveness analysis.
-v
	Increase debug verbosity.
-%
	Debug non-static initializers.
-W
	Debug parse tree after type checking.
-f
	Debug stack frames.
-i
	Debug line number stack.
-j
	Debug runtime-initialized variables.
-r
	Debug generated wrappers.
-w
	Debug type checking.

Compiler Directives

The compiler accepts directives in the form of comments. To distinguish them from non-directive comments, directives require no space between the comment opening and the name of the directive. However, since they are comments, tools unaware of the directive convention or of a particular directive can skip over a directive like any other comment.

Line directives come in several forms:

//line :line
//line :line:col
//line filename:line
//line filename:line:col
/*line :line*/
/*line :line:col*/
/*line filename:line*/
/*line filename:line:col*/

In order to be recognized as a line directive, the comment must start with //line or /*line followed by a space, and must contain at least one colon. The //line form must start at the beginning of a line. A line directive specifies the source position for the character immediately following the comment as having come from the specified file, line and column: For a //line comment, this is the first character of the next line, and for a /*line comment this is the character position immediately following the closing */. If no filename is given, the recorded filename is empty if there is also no column number; otherwise it is the most recently recorded filename (actual filename or filename specified by previous line directive). If a line directive doesn't specify a column number, the column is "unknown" until the next directive and the compiler does not report column numbers for that range. The line directive text is interpreted from the back: First the trailing :ddd is peeled off from the directive text if ddd is a valid number > 0. Then the second :ddd is peeled off the same way if it is valid. Anything before that is considered the filename (possibly including blanks and colons). Invalid line or column values are reported as errors.

Examples:

//line foo.go:10      the filename is foo.go, and the line number is 10 for the next line
//line C:foo.go:10    colons are permitted in filenames, here the filename is C:foo.go, and the line is 10
//line  a:100 :10     blanks are permitted in filenames, here the filename is " a:100 " (excluding quotes)
/*line :10:20*/x      the position of x is in the current file with line number 10 and column number 20
/*line foo: 10 */     this comment is recognized as invalid line directive (extra blanks around line number)

Line directives typically appear in machine-generated code, so that compilers and debuggers will report positions in the original input to the generator.

The line directive is an historical special case; all other directives are of the form //go:name and must start at the beginning of a line, indicating that the directive is defined by the Go toolchain.

//go:noescape

The //go:noescape directive specifies that the next declaration in the file, which must be a func without a body (meaning that it has an implementation not written in Go) does not allow any of the pointers passed as arguments to escape into the heap or into the values returned from the function. This information can be used during the compiler's escape analysis of Go code calling the function.

//go:nosplit

The //go:nosplit directive specifies that the next function declared in the file must not include a stack overflow check. This is most commonly used by low-level runtime sources invoked at times when it is unsafe for the calling goroutine to be preempted.

//go:linkname localname [importpath.name]

The //go:linkname directive instructs the compiler to use “importpath.name” as the object file symbol name for the variable or function declared as “localname” in the source code. If the “importpath.name” argument is omitted, the directive uses the symbol's default object file symbol name and only has the effect of making the symbol accessible to other packages. Because this directive can subvert the type system and package modularity, it is only enabled in files that have imported "unsafe".

Source Files

Directories

Path Synopsis
internal
gc
ssa
x86

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL