Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the compilers of JVM

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what JVM compilers are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Early (compile time) optimization

The compiler for JVM can be divided into three compilers:

Front-end compiler: the process of converting * .java to * .class. Such as Javac of Sun, incremental compiler of Eclipse JDT (ECJ)

JIT compiler: the process of converting bytecode into machine code, such as HotSpot VM's C1 and C2 compilers

AOT compiler: static advance compiler, the process of directly compiling * .java files into local machine code

1. Javac compiler

The Javac compiler itself is a program written in Java.

1), Javac source code and debugging

The source code of Javac is stored in JDK_SRC_HOME/langtools/src/share/slasses/com/sun/tools/javac

The compilation process can be roughly divided into three processes:

Process of parsing and filling symbol table

Annotation processing process of plug-in annotation processor

Analysis and bytecode generation process

The order of relationship and interaction among the three steps is as follows:

2), parsing and symbol filling table

The parsing step is completed by the parseFiles () method, and the parsing step includes two processes: lexical analysis and syntax analysis.

a. Lexical analysis and grammatical analysis

Lexical analysis: transform the character stream of the source code into a collection of tags (Token). A single character is the smallest element of the programming process, while the tag is the smallest element of the compilation process. Keywords, variable names, literals and operators can all become tags. In the source code of Javac, the lexical analysis process is implemented by the com.sun.tools.javac.parser.Scanner class.

Syntax analysis is the process of constructing abstract syntax tree based on Token sequence. Abstract syntax tree is a tree representation used to describe the syntax structure of program code. Each node of the syntax tree represents a syntax structure in the program code, such as packages, types, modifiers, interfaces, return values, and even code comments. The parsing process is implemented by the com.sun.tools.javac.parser.Parser class, and the abstract syntax tree produced at this stage is represented by the com.sun.tools.javac.tree.JCTree class. After this step, the compiler will basically no longer operate on the source file, and the subsequent operations are based on the abstract syntax tree.

b. Fill symbol table

Once the abstract syntax tree is complete, the next step is the process of populating the symbol table, the enterTrees () method. A symbol table is a table made up of a set of symbolic addresses and symbolic information, similar to the form of Kmurv value pairs in the hash table. The information registered in the symbol table is used at different stages of compilation. When assigning addresses to symbolic names, the symbol table is the basis for address allocation. The filling process is implemented by the com.sun.tools.javac.comp.Enter class

3), annotation processor

After JDK1.5, Java provides support for annotations that, like normal Java code, work at run time. With the standard API for compiler annotation processing, it is possible for our code to interfere with the behavior of the compiler. Since any element in the syntax tree, even code comments, can be accessed in the plug-in, there is a lot of room for the use of plug-in annotation processors.

4), semantic analysis and bytecode generation

The main task of semantic analysis is to examine the context-sensitive nature of structurally correct source programs, such as type review.

a. Dimension check

In the process of compiling Javac, the semantic analysis process is divided into two steps: annotation checking and data and control flow analysis, which are attribute () and flow () methods respectively.

The contents of the standard check steps include whether the variable has been declared before it is used and whether the data type between the variable and the assignment can match. In the standard check step, there is another important action called constant folding

Int a = 1 + 2

The literals "1", "2" and the operator "+" can still be seen on the syntax tree, but after being folded by constants, they will be folded to the literal "3". Because constant folding occurs during compilation, defining "aplom1 instruction 2" in the code does not increase the amount of computation of even one CPU instruction at run time rather than directly defining "axi3".

The implementation classes of the annotation check steps in the Javac source code are com.xun.tools.javac.comp.Attr and com.sun.tools.javac.comp.Check classes.

b. Data and control flow analysis

Data and control flow analysis can check out problems such as whether local variables of the program are assigned before use, whether each path of the method has a return value, and whether all checked exceptions are handled correctly.

Local variables are different from fields (instance variables, class variables). If there is no symbolic reference of CONSTANT_Fielddref_info in the constant pool, there will be no access flag information naturally. Therefore, declaring local variables as final has no effect on the runtime, and the invariance of variables is only guaranteed by the compiler during compilation. In the source code of Javac, the entry of data and control flow analysis is the flow () method, and the specific operation is done by the com.sun.tools.javac.comp.Flow class.

c. Interpretive sugar

Grammatical candy refers to adding a certain grammar to a computer language, which has no effect on the function of the language, but is more convenient for programmers to use.

Java is a "low-sugar language". The commonly used syntax sugars are generics, variable length parameters, automatic boxing / unpacking, and so on. These grammars are not supported by the virtual machine runtime, and they are restored to a simple basic syntax structure at compile time, a process called paraphrase candy. The process of interpreting sugar is triggered by the desuger () method.

d. Bytecode generation

Bytecode generation is the last stage of the Javac compilation process, which is completed by the com.sun.tools.javac.jvm,Gen class. The bytecode generation phase not only converts the information generated by the previous steps (syntax tree, symbol table) into bytecode into disk, but also adds and converts a small amount of code.

The instance constructor () method and the class constructor () method are added to the syntax tree at this stage, and the generation of these two constructors is actually a process of code convergence. The compiler will put the statement block ({} block for the instance constructor) For the class constructor, the operations such as static {} block, variable initialization (instance variables and class variables), calling the instance constructor of the parent class converge to the () and () methods, and ensure that the instance constructor of the parent class is executed first, then the variables are initialized, and finally the statement blocks are executed. The dynamics described above are implemented by the Gen.normalizeDefs () method.

After traversing and adjusting the syntax tree, the symbol table filled with all the required information is given to the com.sun.tools.javac.jvm.ClassWriter class, and the wrtieClass () method of this class outputs the bytecode to generate the final Class file

2. Java grammar the taste of sugar

1), generics and type erasure

Generics is a new feature of JDK1.5, which is essentially the application of parameterized types, that is, the data type being operated on is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.

Generics in the Java language are different. They only exist in the program source code. In the compiled bytecode file, they have been replaced with the original original type, and the forced transformation code is inserted in the corresponding place. Therefore, for the Java language at run time, ArrayList and ArrayList are the same class, so generics technology is actually a syntax candy of Java language. The implementation method of generics in Java language is called type erasure. Generics implemented based on this method are called pseudo generics.

Public static void main (String [] args) {Map map = new HashMap (); map.put ("hello", "Hello"); map.put ("how are you?", "have you eaten?"); System.out.println (map.get ("hello")); System.out.println (map.get ("how are you?"));}

Compile this Java code into a Class file, and then decompile it with the bytecode decompiler, the code is as follows:

Public static void main (String [] paramArrayOfString) {HashMap localHashMap = new HashMap (); localHashMap.put ("hello", "Hello"); localHashMap.put ("how are you?", "have you eaten?"); System.out.println ((String) localHashMap.get ("hello")); System.out.println ((String) localHashMap.get ("how are you?"));}

When generics encounter overloading:

Public static String method (List list) {System.out.println ("invoke method (List list)");} public static int method (List list) {System.out.println ("invoke method (List list)");}

This code cannot be compiled, so the parameters List and List are erased after compilation and become the same native type List. The erasure action causes the signature of the two methods to become exactly the same.

2), automatic boxing, unpacking and traversal cycle

Automatic boxing and unboxing are converted into corresponding wrapper and restore methods after compilation, and the traversal loop restores the code to the implementation of the iterator, which is why the traversal loop needs the class to be traversed to implement the Iterable interface, and the variable length parameter becomes an array type parameter when called.

Public static void main (String [] args) {Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; System.out.println (c = d); / / true System.out.println (e = = f); / / false System.out.println (c = = (a + b)); / / true System.out.println (c.equals (a + b)); / / true System.out.println (g = = (a + b)) / / true System.out.println (g.equals (a + b)); / / false}

The "= =" operations of wrapper classes do not unpack automatically without arithmetic operations, and their equals () methods do not handle the relationship of data transformation.

3), conditional compilation

The Java language uses if statements with constant conditions. The if statement in this code is different from other Java code. It will be run at the compilation stage, and the generated bytecode contains only the parts with correct conditions.

Public static void main (String [] args) {if (true) {System.out.println ("block 1");} else {System.out.println ("block 2");}}

The implementation of conditional compilation in Java language is also a syntax candy of Java language. According to the true or false value of Boolean constant, the compiler will eliminate the code blocks that are not established in the branch, which is realized in the paraphrase sugar stage.

There are many other language sweets in Java, such as inner classes, enumeration classes, assertion statements, switch support for enumerations and strings, defining and closing resources in try statements, and so on.

The above is all the content of this article "what are the compilers of JVM?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report