Java compile-time front-end process

Source: Internet
Author: User

The concept of the Java compilation period

The process of converting source code to cost code during the compilation period will be more varied for Java.
The Java compiler can be divided into the following three types because of the different compiler types that result in different concepts:

    • Front-end compilers

      The front-end compiler transforms the Java source code into bytecode, and the common front-end compiler includes the Javac compiler, the ECJ compiler (Eclipse JDT).

    • Instant (Jit,justin time) compiler

      The instant compiler transforms the bytecode into the cost of machine code, and the common instant compiler includes the C1, C2 compiler of the hotspot virtual machine.

    • AOT (Aheadof time) compiler

      The AOT compiler directly changes the Java source code to local machine code, and the common AOT compiler includes the GCJ compiler (GNU Compiler for the Java)

This article mainly describes the Java front-end compiler workflow.

Javac Compiler compilation process

The Javac compiler itself is a front-end compiler implemented in the Java language, and the process of compiling Javac is as follows:

  • Parse and fill symbol table

    This process consists of two parts: lexical parsing and filling symbol table
    Lexical analysis is about to convert Java source code into tokens, and syntax analysis then constructs an abstract syntax tree based on tokens (ast,abstract Syntaxtrees). The symbol table is then populated based on the results of lexical parsing.

  • Plug-in annotation processing

    JDK 1.6 begins with a set of plug-in annotation processor APIs that allow annotations to be processed during compilation, enabling the insertion of the annotation processor to access and modify arbitrary elements in the abstract syntax tree. If a plug-in annotation processor modifies the abstract syntax tree, the process of parsing and populating the symbol table is re-processed after the syntax tree has been modified.

  • Parsing and generating byte codes

    This process mainly makes semantic analysis of the generated abstract syntax tree, and then generates the required bytecode. A semantic analysis can be divided into four steps:
    • Label check
      Label checks include: Whether a variable is declared before it is used, whether the variable type matches, and so on.
      The callout check also includes an important optimization behavior--Constant folding:
      int a = 1 + 2;will be optimized toint a = 3;
    • Data and control flow analysis
      Data and control flow analysis are used to check the correctness of the context logic of the program, including whether the method has a return value for each execution path, whether the exception was thrown or caught, and so on.
    • To solve the grammatical sugar
      Java provides a lot of syntax sugar, common such as: generics, auto-boxing unboxing, variable-length parameters, For-each loops, assertion statements and so on. These syntactic sugars are reverted to simple basic syntax during the compilation period.
    • Byte code generation
      Bytecode generation the information generated from the previous steps is converted into bytecode to generate the class file.
      In this process, the compiler automatically generates <init> methods and methods to <clinit> insert the syntax tree.
      The compiler organizes the instance construction methods of the generated class by initializing the instance variables, statement blocks, calling the parent class instance constructor, and so on <init> . The initialization of static variables, static statement blocks, and so on are organized to construct methods of generating classes <clinit> .



Reference: "In-depth understanding of Java virtual machines"

Java compile-time front-end process

Related Article

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.