From 5a04f18fd2711e24c91f097a5ba92edbafdf5770 Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 7 Aug 2018 12:56:12 +0800 Subject: [PATCH 1/7] partially finished --- ...fficial Introduction to the Go Compiler.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sources/tech/20180427 An Official Introduction to the Go Compiler.md b/sources/tech/20180427 An Official Introduction to the Go Compiler.md index 78dbe23cd7..5be11f8bbe 100644 --- a/sources/tech/20180427 An Official Introduction to the Go Compiler.md +++ b/sources/tech/20180427 An Official Introduction to the Go Compiler.md @@ -5,45 +5,69 @@ ## Introduction to the Go compiler +## Go编译器介绍 + `cmd/compile` contains the main packages that form the Go compiler. The compiler may be logically split in four phases, which we will briefly describe alongside the list of packages that contain their code. +`cmd/compile` 包含构成 Go 编译器主要的包。编译器在逻辑上可以被分为四个阶段,我们将简要介绍这几个阶段以及包含相应代码的包的列表。 + You may sometimes hear the terms "front-end" and "back-end" when referring to the compiler. Roughly speaking, these translate to the first two and last two phases we are going to list here. A third term, "middle-end", often refers to much of the work that happens in the second phase. +在谈到编译器时,有时可能会听到“前端”和“后端”这两个术语。粗略地说,这些对应于我们将在此列出的前两个和后两个阶段。第三个术语“中间端”通常指的是第二阶段执行的大部分工作。 + Note that the `go/*` family of packages, such as `go/parser` and `go/types`, have no relation to the compiler. Since the compiler was initially written in C, the `go/*` packages were developed to enable writing tools working with Go code, such as `gofmt` and `vet`. +请注意,`go/parser` 和 `go/types` 等 `go/*` 系列包与编译器无关。由于编译器最初是用C编写的,所以这些 `go/*` 包被开发出来以便于能够写出和 `Go` 代码一起工作的工具,例如 `gofmt` 和 `vet`。 + It should be clarified that the name "gc" stands for "Go compiler", and has little to do with uppercase GC, which stands for garbage collection. +需要澄清的是,名称“gc”代表“Go 编译器”,与大写 GC 无关,后者代表垃圾收集。 + ### 1. Parsing +### 1. 解析 + * `cmd/compile/internal/syntax` (lexer, parser, syntax tree) +* `cmd/compile/internal/syntax` (词法分析器、解析器、语法树) + In the first phase of compilation, source code is tokenized (lexical analysis), parsed (syntactic analyses), and a syntax tree is constructed for each source file. +在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指token,它是一组预定义、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等)。 + Each syntax tree is an exact representation of the respective source file, with nodes corresponding to the various elements of the source such as expressions, declarations, and statements. The syntax tree also includes position information which is used for error reporting and the creation of debugging information. +每棵语法树都是相应源文件的确切表示,其中节点对应于源文件的各种元素,例如表达式,声明和语句。语法树还包括位置信息,用于错误报告和创建调试信息。 + ### 2. Type-checking and AST transformations +### 2. 类型检查和AST变形 + * `cmd/compile/internal/gc` (create compiler AST, type checking, AST transformations) +* `cmd/compile/internal/gc` (创建编译器AST,类型检查,AST变形) + The gc package includes an AST definition carried over from when it was written in C. All of its code is written in terms of it, so the first thing that the gc package must do is convert the syntax package's syntax tree to the compiler's AST representation. This extra step may be refactored away in the future. +gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是根据该 AST 编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构(译注:AST,Abstract Syntax Tree,抽象语法树,用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。 + The AST is then type-checked. The first steps are name resolution and type inference, which determine which object belongs to which identifier, and what type each expression has. Type-checking includes certain extra checks, such as From 412fe5556e531c1f1cc8dd7380930bc2810d7c04 Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 8 Aug 2018 12:54:04 +0800 Subject: [PATCH 2/7] partially finished --- ...fficial Introduction to the Go Compiler.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sources/tech/20180427 An Official Introduction to the Go Compiler.md b/sources/tech/20180427 An Official Introduction to the Go Compiler.md index 5be11f8bbe..bafe14c1e9 100644 --- a/sources/tech/20180427 An Official Introduction to the Go Compiler.md +++ b/sources/tech/20180427 An Official Introduction to the Go Compiler.md @@ -74,40 +74,62 @@ type each expression has. Type-checking includes certain extra checks, such as "declared and not used" as well as determining whether or not a function terminates. +然后对 AST 进行类型检查。第一步是名字解析和类型推断,它们确定哪个对象属于哪个标识符,以及每个表达式具有的类型。类型检查包括特定的额外检查,例如“声明但未使用”以及确定函数是否会终止。 + Certain transformations are also done on the AST. Some nodes are refined based on type information, such as string additions being split from the arithmetic addition node type. Some other examples are dead code elimination, function call inlining, and escape analysis. +特定转换也基于 AST 上完成。一些节点被基于类型信息而细化,例如把字符串加法从算术加法的节点类型中拆分出来。其他一些例子是死代码消除,函数调用内联和逃逸分析(译注:逃逸分析是一种分析指针有效范围的方法)。 + ### 3. Generic SSA +### 3. 通用SSA + * `cmd/compile/internal/gc` (converting to SSA) + +* `cmd/compile/internal/gc` (转换成 SSA) + * `cmd/compile/internal/ssa` (SSA passes and rules) +* `cmd/compile/internal/ssa` (SSA 相关的遍和规则) + +(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即pass;最后一遍之前所有的遍数得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法)。 In this phase, the AST is converted into Static Single Assignment (SSA) form, a lower-level intermediate representation with specific properties that make it easier to implement optimizations and to eventually generate machine code from it. +在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码(译注:静态单赋值形式SSA是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义,补充如何轻松实现优化)。 + During this conversion, function intrinsics are applied. These are special functions that the compiler has been taught to replace with heavily optimized code on a case-by-case basis. +在这个转换过程中,将完成内置函数的处理。 这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。 + Certain nodes are also lowered into simpler components during the AST to SSA conversion, so that the rest of the compiler can work with them. For instance, the copy builtin is replaced by memory moves, and range loops are rewritten into for loops. Some of these currently happen before the conversion to SSA due to historical reasons, but the long-term plan is to move all of them here. +在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环(range需要翻译!!!)。由于历史原因,这里面有些目前在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 + Then, a series of machine-independent passes and rules are applied. These do not concern any single computer architecture, and thus run on all `GOARCH` variants. +然后,一系列机器无关的规则和遍会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。 + Some examples of these generic passes include dead code elimination, removal of unneeded nil checks, and removal of unused branches. The generic rewrite rules mainly concern expressions, such as replacing some expressions with constant values, and optimizing multiplications and float operations. +这类通用的遍的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 + ### 4. Generating machine code * `cmd/compile/internal/ssa` (SSA lowering and arch-specific passes) From 942f0024d887771061a1fb0f78176a637011c35b Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 9 Aug 2018 12:22:05 +0800 Subject: [PATCH 3/7] finished for the 1st pass. --- ...fficial Introduction to the Go Compiler.md | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/sources/tech/20180427 An Official Introduction to the Go Compiler.md b/sources/tech/20180427 An Official Introduction to the Go Compiler.md index bafe14c1e9..50db21cc86 100644 --- a/sources/tech/20180427 An Official Introduction to the Go Compiler.md +++ b/sources/tech/20180427 An Official Introduction to the Go Compiler.md @@ -116,7 +116,7 @@ the copy builtin is replaced by memory moves, and range loops are rewritten into for loops. Some of these currently happen before the conversion to SSA due to historical reasons, but the long-term plan is to move all of them here. -在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环(range需要翻译!!!)。由于历史原因,这里面有些目前在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 +在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环。由于历史原因,这里面有些目前在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 Then, a series of machine-independent passes and rules are applied. These do not concern any single computer architecture, and thus run on all `GOARCH` variants. @@ -132,44 +132,63 @@ values, and optimizing multiplications and float operations. ### 4. Generating machine code +### 4. 生成机器码 + * `cmd/compile/internal/ssa` (SSA lowering and arch-specific passes) + +* `cmd/compile/internal/ssa` (SSA 低级化和体系结构特定的遍) + * `cmd/internal/obj` (machine code generation) +* `cmd/internal/obj` (机器代码生成) + The machine-dependent phase of the compiler begins with the "lower" pass, which rewrites generic values into their machine-specific variants. For example, on amd64 memory operands are possible, so many load-store operations may be combined. +编译器中机器相关的阶段开始于“低级”的遍,该阶段将通用变量改写为它们的机器相关变形形式。例如,在 amd64 体系结构中操作数可以在内存中,这样许多装载-存储操作就可以被合并。 + Note that the lower pass runs all machine-specific rewrite rules, and thus it currently applies lots of optimizations too. +注意低级的遍运行所有机器特定的重写规则,因此它也应用了很多优化。 + Once the SSA has been "lowered" and is more specific to the target architecture, the final code optimization passes are run. This includes yet another dead code elimination pass, moving values closer to their uses, the removal of local variables that are never read from, and register allocation. +一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的遍了。这包含了另外一个死代码消除的遍,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及寄存器分配。 + Other important pieces of work done as part of this step include stack frame layout, which assigns stack offsets to local variables, and pointer liveness analysis, which computes which on-stack pointers are live at each GC safe point. +本步骤中完成的其它重要工作包括堆栈布局,它将指定局部变量在堆栈中的偏移位置,以及指针活性分析,后者计算每个垃圾收集安全点上的哪些堆栈指针仍然是活动的(译注:不活动的指针就可以……)。 + At the end of the SSA generation phase, Go functions have been transformed into a series of obj.Prog instructions. These are passed to the assembler (`cmd/internal/obj`), which turns them into machine code and writes out the final object file. The object file will also contain reflect data, export data, and debugging information. +在 SSA 生成阶段结束时,Go 函数已被转换为一系列 obj.Prog 指令。它们被传递给汇编程序(`cmd/internal/obj`),后者将它们转换为机器代码并输出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。 + ### Further reading +### 后续读物 + To dig deeper into how the SSA package works, including its passes and rules, head to `cmd/compile/internal/ssa/README.md`. - +要深入了解 SSA 包的工作方式,包括其遍和规则,请转到 cmd/compile/internal/ssa/README.md。 -------------------------------------------------------------------------------- via: https://github.com/golang/go/blob/master/src/cmd/compile/README.md 作者:[mvdan ][a] -译者:[译者ID](https://github.com/译者ID) +译者:[stephenxs](https://github.com/stephenxs) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From de34465256be35ea0b1087366d7c06342db8e098 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 13 Aug 2018 13:01:27 +0800 Subject: [PATCH 4/7] phase 2 --- ...fficial Introduction to the Go Compiler.md | 105 ++---------------- 1 file changed, 9 insertions(+), 96 deletions(-) diff --git a/sources/tech/20180427 An Official Introduction to the Go Compiler.md b/sources/tech/20180427 An Official Introduction to the Go Compiler.md index 50db21cc86..d54fb61906 100644 --- a/sources/tech/20180427 An Official Introduction to the Go Compiler.md +++ b/sources/tech/20180427 An Official Introduction to the Go Compiler.md @@ -3,142 +3,55 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -## Introduction to the Go compiler - ## Go编译器介绍 -`cmd/compile` contains the main packages that form the Go compiler. The compiler -may be logically split in four phases, which we will briefly describe alongside -the list of packages that contain their code. - `cmd/compile` 包含构成 Go 编译器主要的包。编译器在逻辑上可以被分为四个阶段,我们将简要介绍这几个阶段以及包含相应代码的包的列表。 -You may sometimes hear the terms "front-end" and "back-end" when referring to -the compiler. Roughly speaking, these translate to the first two and last two -phases we are going to list here. A third term, "middle-end", often refers to -much of the work that happens in the second phase. - 在谈到编译器时,有时可能会听到“前端”和“后端”这两个术语。粗略地说,这些对应于我们将在此列出的前两个和后两个阶段。第三个术语“中间端”通常指的是第二阶段执行的大部分工作。 -Note that the `go/*` family of packages, such as `go/parser` and `go/types`, -have no relation to the compiler. Since the compiler was initially written in C, -the `go/*` packages were developed to enable writing tools working with Go code, -such as `gofmt` and `vet`. - 请注意,`go/parser` 和 `go/types` 等 `go/*` 系列包与编译器无关。由于编译器最初是用C编写的,所以这些 `go/*` 包被开发出来以便于能够写出和 `Go` 代码一起工作的工具,例如 `gofmt` 和 `vet`。 -It should be clarified that the name "gc" stands for "Go compiler", and has -little to do with uppercase GC, which stands for garbage collection. - 需要澄清的是,名称“gc”代表“Go 编译器”,与大写 GC 无关,后者代表垃圾收集。 -### 1. Parsing - ### 1. 解析 -* `cmd/compile/internal/syntax` (lexer, parser, syntax tree) - * `cmd/compile/internal/syntax` (词法分析器、解析器、语法树) -In the first phase of compilation, source code is tokenized (lexical analysis), -parsed (syntactic analyses), and a syntax tree is constructed for each source -file. - -在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指token,它是一组预定义、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等)。 - -Each syntax tree is an exact representation of the respective source file, with -nodes corresponding to the various elements of the source such as expressions, -declarations, and statements. The syntax tree also includes position information -which is used for error reporting and the creation of debugging information. +在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指token,它是一组预定义、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等;语法树,以及下文提到的AST,Abstract Syntax Tree,抽象语法树,是指用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。 每棵语法树都是相应源文件的确切表示,其中节点对应于源文件的各种元素,例如表达式,声明和语句。语法树还包括位置信息,用于错误报告和创建调试信息。 -### 2. Type-checking and AST transformations - ### 2. 类型检查和AST变形 -* `cmd/compile/internal/gc` (create compiler AST, type checking, AST transformations) - * `cmd/compile/internal/gc` (创建编译器AST,类型检查,AST变形) -The gc package includes an AST definition carried over from when it was written -in C. All of its code is written in terms of it, so the first thing that the gc -package must do is convert the syntax package's syntax tree to the compiler's -AST representation. This extra step may be refactored away in the future. - -gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是根据该 AST 编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构(译注:AST,Abstract Syntax Tree,抽象语法树,用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。 - -The AST is then type-checked. The first steps are name resolution and type -inference, which determine which object belongs to which identifier, and what -type each expression has. Type-checking includes certain extra checks, such as -"declared and not used" as well as determining whether or not a function -terminates. +gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是基于该 AST 编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构。 然后对 AST 进行类型检查。第一步是名字解析和类型推断,它们确定哪个对象属于哪个标识符,以及每个表达式具有的类型。类型检查包括特定的额外检查,例如“声明但未使用”以及确定函数是否会终止。 -Certain transformations are also done on the AST. Some nodes are refined based -on type information, such as string additions being split from the arithmetic -addition node type. Some other examples are dead code elimination, function call -inlining, and escape analysis. - 特定转换也基于 AST 上完成。一些节点被基于类型信息而细化,例如把字符串加法从算术加法的节点类型中拆分出来。其他一些例子是死代码消除,函数调用内联和逃逸分析(译注:逃逸分析是一种分析指针有效范围的方法)。 -### 3. Generic SSA - ### 3. 通用SSA -* `cmd/compile/internal/gc` (converting to SSA) - * `cmd/compile/internal/gc` (转换成 SSA) -* `cmd/compile/internal/ssa` (SSA passes and rules) +* `cmd/compile/internal/ssa` (SSA 相关的pass和规则) -* `cmd/compile/internal/ssa` (SSA 相关的遍和规则) +(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即pass;最后一遍之前所有的遍数得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义,补充如何轻松实现优化)。 -(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即pass;最后一遍之前所有的遍数得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法)。 - -In this phase, the AST is converted into Static Single Assignment (SSA) form, a -lower-level intermediate representation with specific properties that make it -easier to implement optimizations and to eventually generate machine code from -it. - -在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码(译注:静态单赋值形式SSA是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义,补充如何轻松实现优化)。 - -During this conversion, function intrinsics are applied. These are special -functions that the compiler has been taught to replace with heavily optimized -code on a case-by-case basis. +在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码(译注:)。 在这个转换过程中,将完成内置函数的处理。 这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。 -Certain nodes are also lowered into simpler components during the AST to SSA -conversion, so that the rest of the compiler can work with them. For instance, -the copy builtin is replaced by memory moves, and range loops are rewritten into -for loops. Some of these currently happen before the conversion to SSA due to -historical reasons, but the long-term plan is to move all of them here. +在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环。由于历史原因,目前这里面有些在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 -在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环。由于历史原因,这里面有些目前在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 +然后,一系列机器无关的规则和pass会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。 -Then, a series of machine-independent passes and rules are applied. These do not -concern any single computer architecture, and thus run on all `GOARCH` variants. - -然后,一系列机器无关的规则和遍会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。 - -Some examples of these generic passes include dead code elimination, removal of -unneeded nil checks, and removal of unused branches. The generic rewrite rules -mainly concern expressions, such as replacing some expressions with constant -values, and optimizing multiplications and float operations. - -这类通用的遍的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 - -### 4. Generating machine code +这类通用的pass的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 ### 4. 生成机器码 -* `cmd/compile/internal/ssa` (SSA lowering and arch-specific passes) - -* `cmd/compile/internal/ssa` (SSA 低级化和体系结构特定的遍) - -* `cmd/internal/obj` (machine code generation) +* `cmd/compile/internal/ssa` (SSA 低级化和体系结构特定的pass) * `cmd/internal/obj` (机器代码生成) From 31b1575838197c2a43bdf1541dd9ba2254db9e84 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 20 Aug 2018 12:24:05 +0800 Subject: [PATCH 5/7] finish --- ...fficial Introduction to the Go Compiler.md | 45 ++++--------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/sources/tech/20180427 An Official Introduction to the Go Compiler.md b/sources/tech/20180427 An Official Introduction to the Go Compiler.md index d54fb61906..4c65b59521 100644 --- a/sources/tech/20180427 An Official Introduction to the Go Compiler.md +++ b/sources/tech/20180427 An Official Introduction to the Go Compiler.md @@ -35,11 +35,11 @@ gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义 * `cmd/compile/internal/gc` (转换成 SSA) -* `cmd/compile/internal/ssa` (SSA 相关的pass和规则) +* `cmd/compile/internal/ssa` (SSA 相关的 pass 和规则) -(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即pass;最后一遍之前所有的遍数得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义,补充如何轻松实现优化)。 +(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即 pass;最后一遍之前所有的 pass 得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义)。 -在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码(译注:)。 +在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码。 在这个转换过程中,将完成内置函数的处理。 这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。 @@ -47,7 +47,7 @@ gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义 然后,一系列机器无关的规则和pass会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。 -这类通用的pass的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 +这类通用的 pass 的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 ### 4. 生成机器码 @@ -55,46 +55,19 @@ gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义 * `cmd/internal/obj` (机器代码生成) -The machine-dependent phase of the compiler begins with the "lower" pass, which -rewrites generic values into their machine-specific variants. For example, on -amd64 memory operands are possible, so many load-store operations may be combined. +编译器中机器相关的阶段开始于“低级”的 pass,该阶段将通用变量改写为它们的机器相关变形形式。例如,在 amd64 体系结构中操作数可以在内存中,这样许多装载-存储操作就可以被合并。 -编译器中机器相关的阶段开始于“低级”的遍,该阶段将通用变量改写为它们的机器相关变形形式。例如,在 amd64 体系结构中操作数可以在内存中,这样许多装载-存储操作就可以被合并。 +注意低级的 pass 运行所有机器特定的重写规则,因此它也应用了很多优化。 -Note that the lower pass runs all machine-specific rewrite rules, and thus it -currently applies lots of optimizations too. +一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的 pass 了。这包含了另外一个死代码消除的 pass,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及寄存器分配。 -注意低级的遍运行所有机器特定的重写规则,因此它也应用了很多优化。 - -Once the SSA has been "lowered" and is more specific to the target architecture, -the final code optimization passes are run. This includes yet another dead code -elimination pass, moving values closer to their uses, the removal of local -variables that are never read from, and register allocation. - -一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的遍了。这包含了另外一个死代码消除的遍,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及寄存器分配。 - -Other important pieces of work done as part of this step include stack frame -layout, which assigns stack offsets to local variables, and pointer liveness -analysis, which computes which on-stack pointers are live at each GC safe point. - -本步骤中完成的其它重要工作包括堆栈布局,它将指定局部变量在堆栈中的偏移位置,以及指针活性分析,后者计算每个垃圾收集安全点上的哪些堆栈指针仍然是活动的(译注:不活动的指针就可以……)。 - -At the end of the SSA generation phase, Go functions have been transformed into -a series of obj.Prog instructions. These are passed to the assembler -(`cmd/internal/obj`), which turns them into machine code and writes out the -final object file. The object file will also contain reflect data, export data, -and debugging information. +本步骤中完成的其它重要工作包括堆栈布局,它将指定局部变量在堆栈中的偏移位置,以及指针活性分析,后者计算每个垃圾收集安全点上的哪些堆栈上的指针仍然是活动的。 在 SSA 生成阶段结束时,Go 函数已被转换为一系列 obj.Prog 指令。它们被传递给汇编程序(`cmd/internal/obj`),后者将它们转换为机器代码并输出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。 -### Further reading - ### 后续读物 -To dig deeper into how the SSA package works, including its passes and rules, -head to `cmd/compile/internal/ssa/README.md`. - -要深入了解 SSA 包的工作方式,包括其遍和规则,请转到 cmd/compile/internal/ssa/README.md。 +要深入了解 SSA 包的工作方式,包括它的 pass 和规则,请转到 cmd/compile/internal/ssa/README.md。 -------------------------------------------------------------------------------- From f989619e6cf8ab20332ac05d49a1fb964ad8c77c Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 20 Aug 2018 12:27:49 +0800 Subject: [PATCH 6/7] translated. --- ...fficial Introduction to the Go Compiler.md | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 translated/tech/20180427 An Official Introduction to the Go Compiler.md diff --git a/translated/tech/20180427 An Official Introduction to the Go Compiler.md b/translated/tech/20180427 An Official Introduction to the Go Compiler.md new file mode 100644 index 0000000000..4c65b59521 --- /dev/null +++ b/translated/tech/20180427 An Official Introduction to the Go Compiler.md @@ -0,0 +1,82 @@ + +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +## Go编译器介绍 + +`cmd/compile` 包含构成 Go 编译器主要的包。编译器在逻辑上可以被分为四个阶段,我们将简要介绍这几个阶段以及包含相应代码的包的列表。 + +在谈到编译器时,有时可能会听到“前端”和“后端”这两个术语。粗略地说,这些对应于我们将在此列出的前两个和后两个阶段。第三个术语“中间端”通常指的是第二阶段执行的大部分工作。 + +请注意,`go/parser` 和 `go/types` 等 `go/*` 系列包与编译器无关。由于编译器最初是用C编写的,所以这些 `go/*` 包被开发出来以便于能够写出和 `Go` 代码一起工作的工具,例如 `gofmt` 和 `vet`。 + +需要澄清的是,名称“gc”代表“Go 编译器”,与大写 GC 无关,后者代表垃圾收集。 + +### 1. 解析 + +* `cmd/compile/internal/syntax` (词法分析器、解析器、语法树) + +在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指token,它是一组预定义、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等;语法树,以及下文提到的AST,Abstract Syntax Tree,抽象语法树,是指用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。 + +每棵语法树都是相应源文件的确切表示,其中节点对应于源文件的各种元素,例如表达式,声明和语句。语法树还包括位置信息,用于错误报告和创建调试信息。 + +### 2. 类型检查和AST变形 + +* `cmd/compile/internal/gc` (创建编译器AST,类型检查,AST变形) + +gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是基于该 AST 编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构。 + +然后对 AST 进行类型检查。第一步是名字解析和类型推断,它们确定哪个对象属于哪个标识符,以及每个表达式具有的类型。类型检查包括特定的额外检查,例如“声明但未使用”以及确定函数是否会终止。 + +特定转换也基于 AST 上完成。一些节点被基于类型信息而细化,例如把字符串加法从算术加法的节点类型中拆分出来。其他一些例子是死代码消除,函数调用内联和逃逸分析(译注:逃逸分析是一种分析指针有效范围的方法)。 + +### 3. 通用SSA + +* `cmd/compile/internal/gc` (转换成 SSA) + +* `cmd/compile/internal/ssa` (SSA 相关的 pass 和规则) + +(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即 pass;最后一遍之前所有的 pass 得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义)。 + +在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码。 + +在这个转换过程中,将完成内置函数的处理。 这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。 + +在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环。由于历史原因,目前这里面有些在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 + +然后,一系列机器无关的规则和pass会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。 + +这类通用的 pass 的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 + +### 4. 生成机器码 + +* `cmd/compile/internal/ssa` (SSA 低级化和体系结构特定的pass) + +* `cmd/internal/obj` (机器代码生成) + +编译器中机器相关的阶段开始于“低级”的 pass,该阶段将通用变量改写为它们的机器相关变形形式。例如,在 amd64 体系结构中操作数可以在内存中,这样许多装载-存储操作就可以被合并。 + +注意低级的 pass 运行所有机器特定的重写规则,因此它也应用了很多优化。 + +一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的 pass 了。这包含了另外一个死代码消除的 pass,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及寄存器分配。 + +本步骤中完成的其它重要工作包括堆栈布局,它将指定局部变量在堆栈中的偏移位置,以及指针活性分析,后者计算每个垃圾收集安全点上的哪些堆栈上的指针仍然是活动的。 + +在 SSA 生成阶段结束时,Go 函数已被转换为一系列 obj.Prog 指令。它们被传递给汇编程序(`cmd/internal/obj`),后者将它们转换为机器代码并输出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。 + +### 后续读物 + +要深入了解 SSA 包的工作方式,包括它的 pass 和规则,请转到 cmd/compile/internal/ssa/README.md。 + +-------------------------------------------------------------------------------- + +via: https://github.com/golang/go/blob/master/src/cmd/compile/README.md + +作者:[mvdan ][a] +译者:[stephenxs](https://github.com/stephenxs) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://github.com/mvdan From 3baea08d8497b82dfaa813634f08bbd7fc65ac6b Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 20 Aug 2018 12:28:21 +0800 Subject: [PATCH 7/7] moved to translated dir. --- ...fficial Introduction to the Go Compiler.md | 82 ------------------- 1 file changed, 82 deletions(-) delete mode 100644 sources/tech/20180427 An Official Introduction to the Go Compiler.md diff --git a/sources/tech/20180427 An Official Introduction to the Go Compiler.md b/sources/tech/20180427 An Official Introduction to the Go Compiler.md deleted file mode 100644 index 4c65b59521..0000000000 --- a/sources/tech/20180427 An Official Introduction to the Go Compiler.md +++ /dev/null @@ -1,82 +0,0 @@ - -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -## Go编译器介绍 - -`cmd/compile` 包含构成 Go 编译器主要的包。编译器在逻辑上可以被分为四个阶段,我们将简要介绍这几个阶段以及包含相应代码的包的列表。 - -在谈到编译器时,有时可能会听到“前端”和“后端”这两个术语。粗略地说,这些对应于我们将在此列出的前两个和后两个阶段。第三个术语“中间端”通常指的是第二阶段执行的大部分工作。 - -请注意,`go/parser` 和 `go/types` 等 `go/*` 系列包与编译器无关。由于编译器最初是用C编写的,所以这些 `go/*` 包被开发出来以便于能够写出和 `Go` 代码一起工作的工具,例如 `gofmt` 和 `vet`。 - -需要澄清的是,名称“gc”代表“Go 编译器”,与大写 GC 无关,后者代表垃圾收集。 - -### 1. 解析 - -* `cmd/compile/internal/syntax` (词法分析器、解析器、语法树) - -在编译的第一阶段,源代码被标记化(词法分析),解析(语法分析),并为每个源文件构造语法树(译注:这里标记指token,它是一组预定义、能够识别的字符串,通常由名字和值构成,其中名字一般是词法的类别,如标识符、关键字、分隔符、操作符、文字和注释等;语法树,以及下文提到的AST,Abstract Syntax Tree,抽象语法树,是指用树来表达程序设计语言的语法结构,通常叶子节点是操作数,其它节点是操作码)。 - -每棵语法树都是相应源文件的确切表示,其中节点对应于源文件的各种元素,例如表达式,声明和语句。语法树还包括位置信息,用于错误报告和创建调试信息。 - -### 2. 类型检查和AST变形 - -* `cmd/compile/internal/gc` (创建编译器AST,类型检查,AST变形) - -gc 包中包含一个继承自(早期)C 语言实现的版本的 AST 定义。所有代码都是基于该 AST 编写的,所以 gc 包必须做的第一件事就是将 syntax 包(定义)的语法树转换为编译器的 AST 表示法。这个额外步骤可能会在将来重构。 - -然后对 AST 进行类型检查。第一步是名字解析和类型推断,它们确定哪个对象属于哪个标识符,以及每个表达式具有的类型。类型检查包括特定的额外检查,例如“声明但未使用”以及确定函数是否会终止。 - -特定转换也基于 AST 上完成。一些节点被基于类型信息而细化,例如把字符串加法从算术加法的节点类型中拆分出来。其他一些例子是死代码消除,函数调用内联和逃逸分析(译注:逃逸分析是一种分析指针有效范围的方法)。 - -### 3. 通用SSA - -* `cmd/compile/internal/gc` (转换成 SSA) - -* `cmd/compile/internal/ssa` (SSA 相关的 pass 和规则) - -(译注:许多常见高级语言的编译器无法通过一次扫描源代码或 AST 就完成所有编译工作,取而代之的做法是多次扫描,每次完成一部分工作,并将输出结果作为下次扫描的输入,直到最终产生目标代码。这里每次扫描称作一遍,即 pass;最后一遍之前所有的 pass 得到的结果都可称作中间表示法,本文中 AST、SSA 等都属于中间表示法。SSA,静态单赋值形式,是中间表示法的一种性质,它要求每个变量只被赋值一次且在使用前被定义)。 - -在此阶段,AST 将被转换为静态单赋值形式(SSA)形式,这是一种具有特定属性的低级中间表示法,可以更轻松地实现优化并最终从它生成机器代码。 - -在这个转换过程中,将完成内置函数的处理。 这些是特殊的函数,编译器被告知逐个分析这些函数并决定是否用深度优化的代码替换它们(译注:内置函数指由语言本身定义的函数,通常编译器的处理方式是使用相应实现函数的指令序列代替对函数的调用指令,有点类似内联函数)。 - -在 AST 转化成 SSA 的过程中,特定节点也被低级化为更简单的组件,以便于剩余的编译阶段可以基于它们工作。例如,内建的拷贝被替换为内存移动,range循环被改写为for循环。由于历史原因,目前这里面有些在转化到 SSA 之前发生,但长期计划则是把它们都移到这里(转化 SSA)。 - -然后,一系列机器无关的规则和pass会被执行。这些并不考虑特定计算机体系结构,因此对所有 `GOARCH` 变量的值都会运行。 - -这类通用的 pass 的一些例子包括,死代码消除,移除不必要的空指针检查,以及移除无用的分支等。通用改写规则主要考虑表达式,例如将一些表达式替换为常量,优化乘法和浮点操作。 - -### 4. 生成机器码 - -* `cmd/compile/internal/ssa` (SSA 低级化和体系结构特定的pass) - -* `cmd/internal/obj` (机器代码生成) - -编译器中机器相关的阶段开始于“低级”的 pass,该阶段将通用变量改写为它们的机器相关变形形式。例如,在 amd64 体系结构中操作数可以在内存中,这样许多装载-存储操作就可以被合并。 - -注意低级的 pass 运行所有机器特定的重写规则,因此它也应用了很多优化。 - -一旦 SSA 被“低级化”并且更具体地针对目标体系结构,就要运行最终代码优化的 pass 了。这包含了另外一个死代码消除的 pass,它将变量移动到更靠近它们使用的地方,移除从来没有被读过的局部变量,以及寄存器分配。 - -本步骤中完成的其它重要工作包括堆栈布局,它将指定局部变量在堆栈中的偏移位置,以及指针活性分析,后者计算每个垃圾收集安全点上的哪些堆栈上的指针仍然是活动的。 - -在 SSA 生成阶段结束时,Go 函数已被转换为一系列 obj.Prog 指令。它们被传递给汇编程序(`cmd/internal/obj`),后者将它们转换为机器代码并输出最终的目标文件。目标文件还将包含反射数据,导出数据和调试信息。 - -### 后续读物 - -要深入了解 SSA 包的工作方式,包括它的 pass 和规则,请转到 cmd/compile/internal/ssa/README.md。 - --------------------------------------------------------------------------------- - -via: https://github.com/golang/go/blob/master/src/cmd/compile/README.md - -作者:[mvdan ][a] -译者:[stephenxs](https://github.com/stephenxs) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://github.com/mvdan