009 early (compiler) optimizations

Source: Internet
Author: User

Javac the process of compilingIt can be roughly divided into 3 processes, namely:
    • Parse and populate symbol table procedures.
    • annotation processing for the plug-in annotation processor.  
    • Parsing and bytecode generation process.
The relationship between these 3 steps and the Order of interaction

1. Parsing and filling symbol table ① Lexical analysis is the transformation of the source code character stream into a token collection, a single character is the smallest element of the program writing process, and the tag is the smallest element of the compilation process, the keyword, variable name, literal, operator can be tags, In the Javac source code, the lexical analysis process is implemented by the Com.sun.tools.javac.parser.Scanner class.
Parsing is the process of constructing an abstract syntax tree based on the token sequence, abstract Syntax tree,ast is a tree representation of the syntax structure of a program code, Each node of the syntax tree represents a syntactic structure (Construct) in program code, such as packages, types, modifiers, operators, interfaces, return values, and even code comments, all of which can be a syntactic structure.
The ② symbol table (symbol table) is a table of symbolic addresses and symbolic information that the reader can imagine as a form of k-v value pairs in a Chenhashi table (in fact, the symbol table is not necessarily a hash table, it can be an ordered symbol table, a tree symbol table, a stack structure symbol table, etc.). The information that is 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 checking (such as checking that the use of a name is consistent with the original description) 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.

2. Note Processorplug-in annotation processorAny element in the abstract syntax tree can be read, modified, and added. If these plug-ins 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

3, semantic analysis and bytecode generation syntax analysis, the compiler obtains the program code Abstract Syntax tree representation, the syntax tree can represent a properly structured source program abstraction, but can not guarantee that the source program is logical. The main task of semantic analysis is to examine the structure of the correct source program in context, such as type review.

The semantic analysis process is divided into two steps: labeling inspection and data and control flow analysis.
The ① callout Check Labeling check procedure includes whether a variable is declared before it is used, whether the data type between the variable and the assignment can match, and so on.

② data and control flow analysis data and control flow analysis is a further verification of the program context logic, it can check whether the program local variables are assigned before use, whether each path of the method has a return value, whether all of the detected anomalies are handled correctly and so on. Compile time data and control flow analysis and class loading data and control flow analysis of the purpose is basically consistent, but the calibration range is different, there are some check items only in the compilation period or run time can be carried out.

Declaring a local variable as final has no effect on the run time, and the invariant of the variable is only guaranteed by the compiler during compilation.

③ syntax sugar (syntactic Sugar), also known as icing grammar, 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 for programmers to use. In general, using syntactic sugars can increase the readability of the program, thus reducing the chance of code errors.

④ byte code generation

From for notes (Wiz)

009 early (compiler) 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.