cool hit counter Javac's implementation process_Intefrankly

Javac's implementation process


It mainly introduces the process and principles of Javac implementation.

First figure out what is Javac?

Javac is a compiler that converts one language to another language specification. The role of the compiler is the process of converting source code that conforms to the java language specification into a bytecode file that the JVM virtual machine can recognize.

For java. The javac task is to convert the java source code into binary code that jvm can recognize. Convert .java files to .class files, these .class files are also known as bytecode files that only the JVM can recognize.

Rationale of the compilation process.

1, lexical analysis: first read the source code in accordance with the bytes, and then find the syntax keywords defined (if/else/for, etc.), and then determine which keywords are in line with the java language specification, after finishing the analysis to return some normalized Token stream

2. Syntax analysis: analysis of Token streams. The keywords are analyzed to find out if they are combined together to conform to the java language specification, and after this analysis an abstract syntax tree that conforms to the java specification is produced. An abstract syntax tree is a structured grammatical expression that serves to put lexical laws together in a structured form.

Java's syntax tree makes java source code more structured, and each node in the syntax tree is a JCTree instance

The JCTree class has three important properties: in fact, these three properties are well understood and appear in order to distinguish each node in the tree. Defining these three properties allows you to find the hierarchy and location of the nodes in the tree very quickly.

  1. TreeTag: Each syntax number node is represented by a shaping constant, and each node value is executed by adding one to the previous node, the vertex node TopLevel is 1, then the immediately following Import node is added one to TopLevel, which equals 2
  2. pos: integer. indicate the location
  3. type: indicates the type of the node

3. Semantic analysis: the generated abstract structure tree is further analyzed to convert the complex syntactic structure into a simple, easy to understand and read syntactic structure. For example, converting an enhanced for loop foreach to a for loop structure. A more concrete abstract structure tree is produced after semantic analysis.

Steps in the implementation of semantic analysis.

Semantic analysis is mainly done in the Enter class, which has two main steps

(1) All symbols appearing in the class are entered into the class's own symbol table, and all class symbols, class parameter lists, superclass symbols, and inherited interface type symbols are stored in the list of unprocessed symbols (since the class has references in other classes in addition to its own symbols, they are sorted)

(2) In the list of unprocessed symbols, parsing all class symbols into their respective class symbols is done in the MemberEnter.complete() class. (Parse the syntax tree and add all symbols to the symbol table)

In the Enter class resolution step, there is a secondary operation.

  • Adding a default constructor.
  • Then the next step is to process the annotations: this is done in the JavaProcessingEnvironment class.
  • Data flow analysis, done in the Flow class.  

     Steps for data flow analysis implementation.

     1, check whether the variables are assigned before use, in addition to the 8 basic data types, there are String types and other object references need to be assigned before use.

     2. Variables modified with final will not be reassigned, and will report an error if copied repeatedly; also if the variable is a static member variable it must be assigned at the time of definition

     3. Analysis of method return value types

     4. All Checked Exceptions must be thrown or caught upwards.

     5, all statements will be executed, this analysis is whether there are statements after the return statement, because the statements after the return will not be executed.

  • The final step of the semantic parser, further processing the syntax tree, solves the problem
    •    Eliminate useless code, e.g., blocks of code where the if condition is always false will not be executed  
    • Unlocking syntactic sugar: to put it bluntly, converting, for example, an enhanced foreach loop to a for loop
    • Automatic type conversion of variables: e.g., between int and Integer types

4, byte code: after the above three steps, java source code can be converted into java virtual machine (JVM) can be other byte code files.

Code Generator.

  •    Converts the source code into a command form that conforms to the JVM syntax specification. All JVM operations are based on stack operations, so all operations are done on the incoming and outgoing stacks.
  • Output the bytecode in the form of JVM file organization to a bytecode file with the class suffix

Entire business process.

The basics of javac are described here. Those interested can study the source code of Javac, which can be downloaded from OpenJdk.


Recommended>>
1、CCTV Finances The Conversation The future of blockchain will create far more value than the internet
2、Nine drawbacks in blockchain networks
3、Create a whole industry chain ecosystem of Chinese hemp to meet a better tomorrow of Chinese hemp pass
4、Apple confirms no service bug in iPhone 7 will repair for free
5、Google bans mining apps from the Chrome Extensions store

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号