JVM-1. Compilation, jvm-1 Compilation

Source: Internet
Author: User
Tags java keywords

JVM-1. Compilation, jvm-1 Compilation
Directory 1. compiler Overview 2. compiler Composition 3. Example 4. a deep understanding of the JVM compiler 5. Syntax sugar 6. Supplement 1. compiler Overview 1. The essence of the compiler is the specification is transformed into another specification; it is easy for humans to understand machines. Javac converts the source code in the java file into the bytecode in the class file that can be recognized by the JVM, and then the JVM is converted into a machine language that can be recognized by the machine. 2. compiler-type front-end compiler :. java->. class; for example, Sun's javacJIT Compiler: runtime compiler, bytecode-> machine code; for example, Hotspot's C1 and C2 compiler AOT Compiler: Static advance compiler ,. java-> machine code 3. code optimization by the front-end compiler mainly involves program coding (helping programmers improve coding styles and improve coding efficiency), while JIT mainly focuses on performance optimization, in this way, the code in other languages can also be optimized. Many new Java syntax features are implemented through syntactic sugar without the support of the underlying virtual machine. Ii. compiler Composition 1. Lexical analyzer: generate a token stream. The Lexical analyzer reads each character in the source file, recognizes keywords, delimiters, and generates the token stream. The token stream includes: (1) java keywords: package, import, public, class, int, etc. (2) Custom Words: package name, class name, variable name, Method Name (3) Symbol: = ,;, +,-, *,/, %, {,}, etc. 2. syntax analyzer: generate a structured and operable syntax tree. In fact, the word in the token is used as a sentence. In some cases, it is called the abstract syntax tree (AST ). 3. semantic analyzer: generates an annotation syntax tree, which is closer to the syntax rules of the target language. Semantic analyzer performs a lot of processing on the basis of the syntax tree, including

1. Add the default parameter-free Constructor (without specifying any parameter constructor). In JVM, before the syntax analyzer and annotation processing, there is a step to fill the symbol table. The operations include adding the default no-argument constructor. 2. Processing annotation: After JDK1.6, the plug-in Annotation processor API is provided, it can be understood as a compiler plug-in. If the annotation processor modifies the syntax tree, the compiler performs loops (parsing + filling + annotation Processing). With the annotation processor, programmers can intervene in the compilation process. 3. annotation: Check the semantic validity and logic judgment • check whether the variable types in the syntax tree match (eg. string s = 1 + 2; // "=" the types at both ends do not match) • check whether access to variables, methods, or classes is valid (eg. one class cannot access the private method of another class) • Whether the variable has been declared and initialized before use • constant folding (eg. code: String s = "hello" + "world", after semantic analysis String s = "helloworld ") • derivation of parameter types of generic methods 4. Data and control flow analysis [compile-time analysis is similar to class-load analysis, but the verification scope is different. If the local variable does not have a flag in the memory, final verification can only be performed in the compilation phase.] • Is there a value before the local variable is used, JVM considers this step as a part. • deterministic value assignment of variables (eg. methods With return values must be determined with return values) • The final variable can only be assigned a value once, if a value is assigned during compilation, an error is returned. • whether all checked exceptions are thrown or captured. • all statements are executed. (The statements after return are not executed, apart from finally blocks) 5. Further semantic analysis • Remove permanent false code (eg. if (false) • automatic variable conversion (eg.int and Integer) • Remove syntactic sugar (eg. foreach is converted to a for loop, assert is converted to if, and the internal class is parsed into an external class associated with the external class) 6. Finally, converts the previously processed syntax tree to the final annotation syntax tree.
[Personal memory] apart from Annotation processing and syntactic sugar removal, it can be classified as follows: Class: adding a non-argument constructor; whether the access to variables, methods, and classes is legal fields: whether the variable types match; whether the final value is assigned multiple times; whether the constant folding method is external: whether the return value is correct; whether the exception is thrown or caught inside the method: whether the variable is declared and initialized before use; all statements are executed; remove permanent false code 4. Code Generator: generate bytecode (1) generate <init> () and <cinit> () Methods: Actually code convergence, including {}, static {}, variable initialization, and instance constructor that calls the parent class. (2) Other optimizations: Replace the string addition operation with the append () Operation of StringBuilder or StringBuffer. (3) generate bytecode 3. Example (1) source code to token
Package compile;/*** lexical */public class Cifa {int a; int c = a + 1 ;}

(2) syntax tree
package compile;public class Yufa {    int a;    private int c = a + 1;    public int getC() {        return c;    }    public void setC(int c) {        this.c = c;    }}

 

4. a deep understanding of the compiler in JVM 1. The division of the compiler in the book JVM is not the same, but it is generally the same. 2. parsing and filling symbol table parsing: lexical analysis and syntax analysis fill symbol table: symbol table is a table composed of a group of symbol addresses and symbol information; the information in the symbol table is used multiple times in the compilation phase. For example, semantic analysis is used for semantic check. In the bytecode generation phase, when an address is assigned to a symbol name, the symbol table is the basis for address allocation. 3. annotation processing 4. Semantic Analysis annotation checking data and control flow analysis decoding syntactic sugar 5. bytecode Generation 5. Syntactic sugar 1. Syntactic sugar: Add a syntax to a computer language, this syntax makes it easier for programmers to develop programs using languages, and enhances the readability of program code to avoid errors. However, this syntax does not affect the functions of the language. Java generic, variable-length parameters, automatic unpacking/packing, foreach loop, Conditional compilation, enumeration, internal classes, assertions, switches for enumeration and string, define and disable Resources in the try Statement (?) And so on, that is, the virtual machine itself does not support it. 2. Generic Type: pseudo-generic type. The essence of the type erasure generic type is parameterized type. Before the appearance of generic types, the common method is Object class + type conversion; java generics are implemented by type erasure, that is, pseudo generics. For example, List <String> and List <Integer> are classes at runtime, that is, List. type conversion is performed when use. The following two methods cannot be overloaded. public void method(List<Integer> list){} public void method(List<String> list){}Because the type is erased, some other attributes, such as Signature, are introduced to distinguish different parameterized types. Therefore, type erasure only deletes the bytecode in the Code attribute of the method, metadata still retains generic information, so parameterized types can be obtained through reflection. [This section is not in-depth.] 3. Automatic unpacking and automatic packing: intValue () and Integer. valueOf () variable length parameter: the parameter is an array foreach loop: iterator4. The initial task of conditional compiling C and C ++ is to solve the code dependency during compilation (for example, # include), but not in Java, because Java is a natural compilation method (instead of compiling Java files one by one, it inputs the top-level node of the syntax tree of all compilation units into the list to be processed before compiling, therefore, each file can provide symbolic information to each other. No Preprocessor is required. Implementation example:
Public static void main (String [] args) {if (true) {System. out. println ("True");} else {System. out. println ("False") ;}}after Compilation: public static void main (String args []) {System. out. println ("True ");}

Note: Only if + constants can be used for Conditional compilation. errors will be reported when the constant + while compilation is performed. The branch that cannot be reached is also completed in the syntactic sugar decoding phase; only block-level Conditional compilation can be implemented. 6. Supplement 1. Practice: plug-in Annotation processor. 2. Visitor Design Mode: decouples the data structure (such as the syntax tree) from the operations on the data structure. 3. Main reference: deep understanding of Java Virtual Machine

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.