jvm-program compilation and code early (compile time) optimization

Source: Internet
Author: User

Early (compile-time) optimizations

One, Javac compiler

1.Javac Source code and debugging

The source code for Javac is placed in Jdk_src_home/langtools/src/shares/classes/com/sun/tools/javac, except for the JDK's own API, which only references jdk_src_home/ langtools/src/shares/classes/com/sun/* inside the code, debugging environment is easy to build up, because basically do not need to deal with dependencies.

The compilation process can be roughly divided into 3 processes:

(1) Parsing and filling symbol table procedure

(2) Annotation process processing of the plug-in annotation processor

(3) Analysis and byte code generation process

The entry for the Javac compilation action is the Com.sun.tools.javac.main,javacompikler class, and the code logic for the above 3 procedures is concentrated in the compiler () and Compiler2 () methods of the class.

2. Parse and fill symbol table

Parsing steps include lexical analysis and syntax analysis two processes

(1) Lexical and grammatical analysis

Lexical analysis is the source code of the byte stream into a token (token) collection, a single character is the smallest element of the program encoding process, and the markup is the smallest element of the compilation process.

In the Javac source code, the lexical analysis process is implemented by the Com.sun.tools.javac.parser.Scanner class.

Lexical analysis is the process of constructing abstract syntax tree based on token sequence, and abstract syntax is a kind of tree representation to describe the syntax structure of program code, each node of the syntax tree represents a grammatical structure in program code.

The parsing process is implemented by the Com.sun.tools.javac.parser.Parse class, which produces an abstract syntax tree with the Com.sun.tools.javac.tree.JTree class representation, after which the compiler basically no longer operates on the source code file. Subsequent operations are built on the abstract syntax tree.

(2) Fill symbol table

A symbol table is a table of symbolic addresses and symbolic information that consists of a set of symbols.

In parsing, the contents of the symbol table are used for parsing and generating intermediate code.

In the target code generation phase, when the symbol name is assigned to the address, the symbol table is the basis of the address assignment.

In the Javac source code, The process of populating the symbol table is implemented by the Com.sun.tools.javac.compiler.Enter class, where the exit is a pending list (ToDoList), containing the top node of the abstract syntax tree for each compilation unit and the top node of the Package-info-java.

3. Annotation processor

In the Javac source code, the initialization of the plug-in annotation processor is done in the Initprocessannotations () method, and its execution is done in the Processannotations () method. This method determines if there is a new annotation processor that needs to be executed, if any, A new Javacompiler object is generated by the doprocessing () method of the Com.sun.tools.javac.Processing.JavacProvcessingEnviroment class to process the subsequent steps of the compilation.

4. Semantic analysis and byte code generation

(1) Labeling check: The content includes whether the variable is declared before and after it is used, whether the data type between the variable and the assignment can match, etc. In the labeling check step, there is also an important action called constant folding.

Callout Check steps the implementation class in the Javac source code is the Com.sun.tools.javacComp.Attr class and the Com.sun.tools.javac.comp.Check class.

(2) Data and control analysis

Further verification of the context logic of the program, it can check whether the program local variables before and after the use of the assignment, whether each path of the method has a return value, whether all the checked exception is correctly out and so on.

In the Javac source code, the data and control flow analysis of the portal is the flow () method, the specific operation is done by the Com.sun.tools.javac.comp.Flow class.

(3) Solving grammatical sugars

Syntactic sugars (syntatic Sugar), also known as icing syntax, refers to a grammar that is added to a computer language, which has no effect on the function of the language, but is more convenient to use.

In the source code of Javac, the process of solving the syntactic sugars is triggered by the Desugar () method, which is done in the Com.dun.tools.javac.comp.TransTypes class and the Com.sun.tools.javac.comp.Lower class.

(4) Byte code generation

Bytecode generation is the last phase of the Javac compilation process, and in Javac source code there are com.sun.tolls.javac.jvm.Gen classes to complete.

after the syntax tree has been traversed and adjusted, the symbol table filled with all the required information is given to the Com.sun.tolls.javac.jvm.ClassWrite class, and the Witeclass () method of this class outputs the bytecode and generates the final class file. This concludes the entire compilation process.

Second, Java syntax sugar

1. Generics and type Erasure

C # inside generics, regardless of the program source code, the compiled IL, the value of the runtime of the CLR, is the actual existence of,list<int> and list<string> on two different slightly types, they are generated during the system run time, have their own virtual method table and, Type data, this implementation mechanism is called type bloat, and generics implemented based on this method are called true generics.

Generics in the Java language are not the same, it exists only in the program source code, in the compiled bytecode file, has been replaced with the original primitive type, and the corresponding place to insert the forced transformation code, so for the runtime of the Java language, arraylist<string > and arraylist<int> are the same class, so generics are actually a syntactic sugar in the Java language, and generic implementations in the Java language are called type Erasure, and generics based on this approach are called pseudo generics.

New properties such as Signature,localvariabletype are introduced in the virtual machine specification to solve the problem of parameter type recognition with generics.

2. Automatic packing, unpacking and looping

The automatic boxing and unpacking is converted into the corresponding box restoration method after compiling, and the traversal loop restores the code to the implementation of the iterator.

the "= =" Operation of the wrapper class does not automatically unpack without encountering arithmetic character arithmetic, and their equals () method does not handle the relationship of data transformation.

3. Conditional compilation

The Java language can be conditionally compiled by using an if statement with a constant condition.

The implementation of conditional compilation In the Java language is a syntactic sugar in the Java language, which, depending on the true and false of the Boolean constants, will erase the code that is not in the branch. This work will be implemented in the compiler release syntax sugar phase (Com.sun.tools.javac.comp.Lower Class).

jvm-program compilation and code early (compile time) optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.