The concept of the Java compilation period
The process of converting source code to cost code during the compilation period will be more varied for Java.
The Java compiler can be divided into the following three types because of the different compiler types that result in different concepts:
This article mainly describes the Java front-end compiler workflow.
Javac Compiler compilation process
The Javac compiler itself is a front-end compiler implemented in the Java language, and the process of compiling Javac is as follows:
Parse and fill symbol table
This process consists of two parts: lexical parsing and filling symbol table
Lexical analysis is about to convert Java source code into tokens, and syntax analysis then constructs an abstract syntax tree based on tokens (ast,abstract Syntaxtrees). The symbol table is then populated based on the results of lexical parsing.
Plug-in annotation processing
JDK 1.6 begins with a set of plug-in annotation processor APIs that allow annotations to be processed during compilation, enabling the insertion of the annotation processor to access and modify arbitrary elements in the abstract syntax tree. If a plug-in annotation processor modifies the abstract syntax tree, the process of parsing and populating the symbol table is re-processed after the syntax tree has been modified.
Parsing and generating byte codes
This process mainly makes semantic analysis of the generated abstract syntax tree, and then generates the required bytecode. A semantic analysis can be divided into four steps:
- Label check
Label checks include: Whether a variable is declared before it is used, whether the variable type matches, and so on.
The callout check also includes an important optimization behavior--Constant folding:
int a = 1 + 2;
will be optimized toint a = 3;
- Data and control flow analysis
Data and control flow analysis are used to check the correctness of the context logic of the program, including whether the method has a return value for each execution path, whether the exception was thrown or caught, and so on.
- To solve the grammatical sugar
Java provides a lot of syntax sugar, common such as: generics, auto-boxing unboxing, variable-length parameters, For-each loops, assertion statements and so on. These syntactic sugars are reverted to simple basic syntax during the compilation period.
- Byte code generation
Bytecode generation the information generated from the previous steps is converted into bytecode to generate the class file.
In this process, the compiler automatically generates <init>
methods and methods to <clinit>
insert the syntax tree.
The compiler organizes the instance construction methods of the generated class by initializing the instance variables, statement blocks, calling the parent class instance constructor, and so on <init>
. The initialization of static variables, static statement blocks, and so on are organized to construct methods of generating classes <clinit>
.
Reference: "In-depth understanding of Java virtual machines"
Java compile-time front-end process