The Java compiler is generally divided into the front-end compiler, the JIT (just in time compiler) compiler, the AOT (Ahead of Time compiler) compiler three.
- Front-end compilers: compilers that compile Java files into class files, currently have the following two main, the Javac and the incremental compilers in Eclipse JDT (ECJ) provided by Sun
- JIT Compiler: Virtual machine backend run-time compiler, the process of converting bytecode into machine code. C1 provided in the HotSpot VM, C2 compiler
- AOT compiler: The process of converting a Java file directly to a local machine code. GNU Compiler for the Java (GCJ), Excelsior JET
* After Java 3, in order to enable other languages that are not compiled by Javac to be executed on the Java Virtual machine to enjoy the benefits of compiler optimization So, Java put all the compiler optimization work in the back-end timely compiler.
Sun Javac Compiler
The Sun javac compiler compilation process is divided into three steps, parsing the fill symbol table, the annotation processing of the plug-in annotation processor, the analysis and the bytecode production process
The **javac compiler's entry is the Com.sun.tools.javac.main.JavaCompiler class, and the main logic is concentrated in the compile and Compile2 methods
- Parsing the Fill symbol table
Lexical analysis: Transforming the character stream of a source code into a token collection Com.sun.tools.javac.parser.Scanner class for lexical analysis
Parsing: Constructing abstract syntax trees (ast,abstract Syntax tree) with tag sequences. The Eclipse AST View plugin can be used to view the abstract syntax tree
- Annotation processor
Java1.5 provides support for annotations (Annotations), which can be understood as a set of plug-ins for abstract syntax trees that can read, modify, and add operations to the abstract syntax tree directly.
If the syntax tree is modified during the parsing of annotations, the compiler returns to parsing and populating the symbol table until all the inserted annotation processors have not modified the syntax tree.
The Initprocessannotations () method in the Javaccompiler class initializes the annotation processor, Javacprocessingenvironment class doprocessing () method to generate a new javaccompiler to process the subsequent process of compilation.
- Semantic analysis and byte code generation
After parsing, the compiler gets the abstract syntax tree representation of the program, the syntax tree represents a properly structured source program abstraction, but cannot guarantee that the source program is logical, semantic analysis is to review the structure of the correct source program context.
Syntax analysis and bytecode generation are divided into four processes: annotation checking, data and control flow analysis, parsing of syntactic sugars, and bytecode generation .
annotation checking, and data and control flow analysis are primarily analytical checks of the context of the program.
To solve the syntactic sugar: in Java commonly used to solve the syntax of sugar is generic, in the Java Virtual machine is not supported generics. Grammar is only extended by syntactic sugar.