Javac Compiler early Optimizations

Source: Internet
Author: User

Early (compiler) optimizations for Java1. Javac Compiler1.1 Compilation steps

The compilation process can be divided into 3 steps:

    1. Parsing and filling symbol procedures
    2. annotation processing for the plug-in annotation processor
    3. Parsing and bytecode generation process
1.2 Parsing and filling symbols table

The parsing step contains two procedures for lexical analysis and syntax analysis.

1.2.1 Lexical and grammatical analysis

词法分析is to turn the character stream of the source code into a token collection, which is the smallest element of the compilation process.
语法分析Is the process of building an abstract syntax tree based on the token sequence.
抽象语法树is a tree representation that describes the syntax structure of a program code, and each node in the syntax tree represents a syntactic structure in program code, such as packages, types, modifiers, operators, interfaces, and so on.

1.2.2 Fill Symbol table

A symbol table is a table that consists of a set of symbolic addresses and symbolic information. The information registered in the symbol table is used at different stages of compilation. In semantic analysis, the contents of the symbol table are used for semantic inspection and intermediate code generation. 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.

1.3 Annotation Processor

The standard API, which provides a set of plug-in annotation processors in JDK1.6, handles annotations during compilation, and can be seen as a set of compiler plug-ins in which arbitrary elements in the abstract syntax tree can be read, modified, and added. If these plugins modify the syntax tree during annotation processing, the compiler will revert back to parsing and populating the symbol table until all the plug-in annotation processors have not modified the syntax tree, and each loop is called a round.

1.4 Semantic Analysis and byte code generation

The syntax tree can represent an abstraction of a properly structured source program, but there is no guarantee that the resource program is logical. Semantic analysis is a 主要任务 review of the context-sensitive nature of the structurally correct source program, such as type review.

1.4.1 Labeling Check

During the compilation process of Javac, the semantic analysis process is divided into 标注检查 数据及控制流分析 two steps.
The labeling check steps check for things such as 变量使用前是否已被声明、变量与赋值之间的数据类型是否能够匹配 .
In the labeling check step, there is also an important action called 变量折叠 . For example:
In the code, write the following:

int a = 1 + 2;

After the variable is collapsed, 1 + 2 it is collapsed into a literal 3 . So defining in code int a = 1 + 2 int a = 3 does not add to the computation of the program's run time, rather than the direct definition.

1.4.2 Data and Control flow analysis

Data and control flow analysis is a further validation of the context logic of the program, and he can check out whether the program local variables are assigned before they are used, whether each path of the method has a return value, and whether all of the checked exceptions are handled correctly.

1.4.3 to solve the grammatical sugar

语法糖(Syntactic Sugar), also called icing grammar, a term invented by the British computer scientist Peter J.landin, refers to a grammar that is added to a computer language that has no effect on the functionality of the language, but is more convenient for programmers to use.
The syntax sugar in Java will 编译阶段还原 go back to the simple underlying grammatical structure that this process becomes 解语法糖 .

1.4.4-byte code generation

Bytecode generation is the last phase of the Javac compilation process, and this phase of the compiler also makes a small amount of code additions and conversions. The instance constructor <init>() and class builder are <clinit>() added to the syntax tree at this stage. After the syntax tree has been traversed and adjusted, the final class file is generated.

2. Java syntax the taste of sugar2.1 Generics and type Erasure

The essence of generics is a parameterized type, that is, the data type being manipulated is specified as a parameter. This parameter type can be applied in the creation of classes, interfaces, and methods, which are referred to as generic classes, generic interfaces, and generic methods, respectively.
Generics in Java exist only in source code, erase generics at compile time, replace with original native type (Raw type, also called bare type), and insert forced transformation code where appropriate.

2.2 Automatic packing, unpacking and looping

the "= =" Operation of the wrapper class is in 不遇到算术运算的情况下不会自动拆箱 , their equals()方法不处理数据转型 .

Javac Compiler early Optimizations

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.