Home >Backend Development >Golang >How to implement bootstrapping in golang?
Golang implements bootstrapping: first install [Go 1.4] or higher; then use the existing Go tool chain to create a basic version of the [Go 1.5] tool chain; and finally use it to build [ go_bootstrap] and the rest of the standard library and standard components.
Golang’s method of implementing bootstrapping:
Bootstrapping
(Bootstrapping) This is The process of "writing a compiler (or assembler) for the target programming language to be compiled". Generally speaking, bootstrapping has several advantages, such as:
is used to test the language being bootstrapped;
supports the use of usually more Write compilers for high-level languages that provide more high-level abstractions;
compilers can also benefit from any improvements at the language level.
As mentioned earlier, Google began its efforts to remove C code from the Go source tree a year ago. The conversion plan is divided into 5 steps:
Phase 1 - Develop a translator from C language to Go language, and translate the existing C compiler into Go language. This stage takes advantage of the fact that the original compiler did not make extensive use of some features that are difficult to port to the Go language, such as macros, unions, and pointer arithmetic.
Phase 2 - Convert the compiler's source tree to get a Go language compiler, but it is relatively primitive and in C style.
Phase 3 - Convert the previously obtained compiler into a program that conforms to Go language habits, mainly by identifying packages, adding documentation and unit tests.
Stage 4 - Optimizing the compiler, solving compiler and CPU memory usage issues, and possibly introducing parallelization. In addition, an attempt is made to introduce a new intermediate representation between the architecture-independent unordered trees (Node*s) and the architecture-dependent ordered lists (Prog*s) used today, with the goal of improving the compiler's ability to eliminate redundant Optimization capabilities in situations such as nil checks and bounds checks.
Phase 5 - Replace the front end with the latest version of go/parser and go/types.
Russ mentioned that they also considered some alternatives, but they were eliminated based on various factors, which were described in this document a year ago.
Bootstrapping of Go
The bootstrapping of the compiler usually leads to the "chicken or the egg" problem, and a way must be provided to compile our The language to create.
The situation with Go is that to build Go 1.5, you must first install Go 1.4 or higher, and then use the existing Go toolchain to create a base version of the Go 1.5 toolchain. Once you have the Go 1.5 toolchain compiled (Go 1.4), you can use it to build itself, and you can further use it to build go_bootstrap and the rest of the standard library and standard components. This process adds an intermediate step - the resulting toolchain is then used to build itself, which can be applied to any future Go version.
To learn more about Go’s plan to implement bootstrapping, InfoQ interviewed Russ.
Achieving bootstrapping seems to be a big milestone for the Go language. In the evolution of language, why did you decide to do this at this stage? Can you explain it in detail?
Go is a good general-purpose language, but it was designed to be used when writing large-scale, highly concurrent server-side software, like those running on Google's servers. If bootstrapping is implemented earlier, the Go compiler will be the first large-scale Go language program. This will have a negative impact on the language design and will take us away from the real goal.
There are also some technical reasons for not implementing bootstrapping earlier, such as portability, compiling from source code is easier than bootstrapping, and we can also have a stable compiler implementation as early as possible.
Using Go to build Go, compared with using C, in which specific areas do you think there are obvious improvements?
Ken Thompson once said to me that writing programs in Go feels easier than using C. One reason is that Go eliminates several common types of C bugs, such as dangling pointers, memory leaks, buffer overflows, stack overflows during deep recursion, misuse of void*, and unexpected numeric conversions.
The standard Go toolchain supports modularity, unit testing and performance analysis better than any standard C toolchain, but what excites me most is when modifying internal APIs or refactoring. The prospect of applying automated program rewrites (such as gofix).
In the "Go 1.3 Compiler Overhaul" document, you describe a 5-step process for migrating an existing compiler from C to Go. What steps have been completed so far? When are the remaining steps planned to be completed?
For the Go project, it is more important to convert the language's runtime from C to Go, so we did this first. Now we are back to the compiler.
From a documentation perspective, we are currently in Phase 2. The translator is complete and helped us convert the runtime. We are applying this to the compiler. We hope to complete the transition to the Go 1.5 compiler. Cleanup work will be carried out in projects after Go 1.5.
Related learning recommendations: Go language tutorial
The above is the detailed content of How to implement bootstrapping in golang?. For more information, please follow other related articles on the PHP Chinese website!