JVM learning - (f) front-end compilation and optimization

JVM learning - (f) front-end compilation and optimization

The general said, "compile" is relatively broad concept that can refer to three main aspects:
1️⃣ front-end compiler. It refers to the .java file into a .class file conversion process.
2️⃣ time compiler. It refers to the process of operation of the byte code into machine code in turn.
3️⃣ advance compiler. Directly compiled into binary code associated with the target set of machine instructions.
Here mainly refers to the front-end compiler, due here for the optimization of efficiency measures is basically no, optimize coding efficiency is aimed primarily at programmers and language user happiness.

A .javac compiler

javac compiler itself except for using a standard library than JDK, it refers only to the src / share / classes / com / sun / * code, from the view of the overall structure javac code, the compilation process can be divided into a preparation and three processes, namely:
1️⃣ preparation process: initializing the annotation plug-in processor.
Parsing process 2️⃣ symbol table and fill:
1. lexical, syntactic analysis, flow into the character set of markers, the abstract syntax tree structure. 2. Fill the symbol table, the symbol address and the sign signal is generated.
Annotation process 3️⃣ annotation plug-processor: annotation plug-in execution stage of the processor. Only here you can diy.
4️⃣ bytecode analysis and generation process, comprising:
1. Check the label. 2. Flow control flow analysis and data. 3. syntactic sugar solution. 4. bytecode generation.
* NOTE: If the step 3 has generated new symbol, need to restart from step 2.
The entire compilation process can be summarized as:
Preparation process: the annotation plug-in initialization processor - 1.1 Process: lexical analysis, syntax analysis - 1.2 Process: input to the symbol table - Process 2: annotation process executed - Procedure 3.1: Labeling - 3.2 Process: Data flow - 3.3 process: Solutions syntactic sugar - 3.4 process: generating bytecode

Resolving the symbol table and fill

① morphology, syntax analysis
lexical analysis refers to the character stream into source code marker collection process. Character is the smallest element when writing programs, and the mark is the smallest element compile time.
The parsing process is a sequence of tokens of the abstract syntax tree configuration.
Abstract syntax tree is a code syntax used to describe the tree structure of a program representation, each node represents a grammatical structure of the program code.
② filled symbol table
symbol table is a set of data structures and the symbol address information composed of symbols. Output is a product of the process pending list.

2. annotation processor

After jdk6 designed a set of standard API called "plug-in annotation processors" may be advanced to a particular compiler code annotation processing. It can be seen as a set of compiler plug, when the plug-in operation, allowing read, modify, add any element of the abstract syntax tree.

3. Semantic analysis generates bytecode

Semantic checks: abstract syntax tree for a context check the correct source correlation properties, such as: type checking, checking control flow, data flow checking.
① check label
includes a front has already been declared variables, data type and assignment between variables match, which also includes a small optimization is called constant folding.
② data and control flow analysis
which is to verify that the program logic further context. Checking whether, for example, local variables assigned before use, whether each path has a return value of the method, if all are properly checked exception processing and the like, can be seen as the basic functions of data and control flow during loading with class similar analysis.
For example, with a final modified local variables, this expression with ordinary local variables in the class file are the same, no effect on the operation of, and therefore should be noted that this type of error in the data and control flow analysis.
Solutions syntactic sugar ③
④ bytecode generated
just in front of the respective steps of the generated information (the number of grammar, the symbol table) into bytecode instructions written to disk, the compiler also add a small amount of code and conversion work.
Such as () and a method of generating class constructor method strength constructor.

II. Syntactic sugar

Syntactic sugar can not provide substantial improvements, but they can increase efficiency, improve the rigor or grammar, or the code can reduce the chance of error.

1. Generic

Generic nature of the parameter or the parameter type of polymorphism.
①java and C # generic
java generic called "generic types of wiper style", C # selected "instantiated generic formula." Inside C # generics either inside the program source code, compiled intermediate language representation or CLR runtime inside are real. java language generic only present in the source code, all of the generic will be replaced in the original bytecode bare type.
Comparison between the two:
1️⃣C # generics, whether the use of the effect is still operational efficiency is the overall leader in the generic java (mainly because of java generics will lead to countless structure of entitlements wrapper classes).
The only advantage is to achieve 2️⃣java generics generics only need to make improvements on the javac compiler. Where the historical factors of java (complete backward compatibility) resulted in java generics only in this way can be used.
3️⃣java generic so that can not be run on generic type information, make some code becomes very tedious.
②java elegant object-oriented code type of loss
when a generic encounter overloaded, if the parameters are both generic and return values are the same, this code will not compile executed because generic types are compiled after It is wiped, resulting in two identical method can not be overloaded.
However, if the two different types of return value but can be overloaded because of the addition of different return values can coexist in such a class file, the Signature method to store a code signature byte level characteristic, which comprises the return value. Only in jdk6 can compile.
③ entry box with automatic traversal cycle
should be noted that not the entry box of "==" packaging operation if the situation is not encountered arithmetic operation, and data type conversion process will not equals.
* Comparative try to use the integer equals, because only compare = [-128,127] data.
④ conditional compilation
Condition is not satisfied in the code will be removed during compilation, the byte code does not exist in this code.

Released six original articles · won praise 2 · Views 281

Guess you like

Origin blog.csdn.net/jacobbbbbbbb/article/details/104228064