This is a creation in Article, where the information may have evolved or changed.
Dog-comp
Abstract
This article describes the implementation process of a compiler. The source language chooses Minijava. Minijava is an object-oriented language that supports object-oriented features such as inheritance, object creation, and so on. Refer to the Tiger Book appendix for specific syntax.
Dog-comp is a Minijava compiler implemented with Golang and can now translate Minijava into C. The Dog-comp includes a front-end Lexer,parser,type-checking,codegen, and a minijava-ast-based optimization on the back end, based on the optimization of the control flow graph. To satisfy the Minijava object-oriented features, the Dog-comp also comes with a runtime that implements a garbage collector. Dog-comp can be used as a gadget for learning compiler and language optimizations.
Lexer
Although there is already a parser generator, Dog-comp uses the transfer graph (TDA) algorithm to implement lexer manually.
Parser
Dog-comp's parser use LL (1). The processing of exp and STM is grammatically rewritten, avoiding two semantics.
Elaborator
The compiler is a typical current structure, and the input of type checking is the output of parser, which is the AST of the source language.
CodeGen
In the process of code generation, the AST of the source language is translated into the AST of the C language, when the C-ast is printed and the translation is actually completed. But here are a few places to watch out for. To translate an object-oriented language into a language like C, we need to do some transformations in the middle, which are actually the implementation techniques of object-oriented language.
What is an object?
This is about what the object is. objects can have data and functions, so we can say that the object is a collection of data and functions that manipulate the data. In the C language, objects cannot be represented by just one structure, and there is the concept of a virtual function table. We can combine the data with a function table into a structure, and this structure is actually the Java object.
Flattening
The so-called flattening is the elimination of the Java class. Obviously, in C, all the functions are flat.
Garbage collector
Compiling the generated C files together with runtime.c compiles the executable file, which makes C a garbage collection feature. In order to support garbage collection, a number of fields must be added to the structure of C for GC to record the state of the object. For example, you need to record whether those fields in this object are references, whether the object is a normal object or an array object, and so on.
Optimization!
Dog-comp has made 4 optimizations at the source AST level.
Dead class Delete
Dead Code Removal
Algebra Simplification
Constant folding
Deadclass Elimination
As shown, the class FAC is a non-used category, and after the dead class is deleted, FAC is killed.
Deadcode Elimination
Program contains the dead code. Obviously, the else branch of the if is impossible to execute, and the last while is not possible. So after the dead code is deleted, these two pieces of code will disappear directly.
Here's a question to note: Should the while (true) {} and while (false) {} be treated the same? while (false) is obviously a dead code, but while (true) should be deleted? The answer is negative.
Algsimp
Algebra simplification is mainly for operations, such as I=ab0* (a+2)/m), the right expression can be directly optimized to 0.
Constfold
Constant folding is the operation of the constant at compile time.
There are many areas of code that can be optimized. After executing the constant collapse code, such as.
It can be observed here that, after the constant folding, there is actually a dead code generated, the program can continue to optimize. So the 4 optimization loops until the fixed point is reached.
Cfg
The control flow graph is a convenient intermediate representation for optimization. As is the control flow graph of the equal function in Linkedlist.java.
can use
dog-comp/$ ./dog-comp ../test/LinkedList.java -visualize svg 生成。