当前位置:网站首页>11. Execution engine

11. Execution engine

2020-12-07 17:18:50 Irving the procedural ape

1. Executive engine overview and basic execution process

1.1 summary

In the previous post , Have been to Class loading , And the runtime data area of the loaded class , however our java The program is running in jvm Medium , Our bytecode instructions are not directly recognized by the operating system , This is the time The executive engine is on the scene .

1605707526358

  1. The execution engine is Java One of the components of the virtual machine core .“ virtual machine ” It's a comparison with “ The physical machine ” The concept of , Both machines have code execution capabilities , The difference is that the execution engine of the physical machine is built directly on the processor 、 cache 、 Instruction set and operating system level , The execution engine of virtual machine is realized by software itself , Therefore, the architecture of instruction set and execution engine can be customized without physical constraints , Be able to execute instruction set formats that are not directly supported by hardware .
  2. JVM The main task of bytecode is to load bytecode into it , But bytecode can't run directly on the operating system , Because bytecode instructions are not equivalent to local machine instructions , It contains only a few things that can be JVM Bytecode instruction identified 、 The symbol table , And other supporting information .
  3. that , If you want to make a Java The program runs , Execution engine (Execution Engine) The task is to interpret bytecode instructions / It can only be compiled to the local machine instructions on the corresponding platform . Simply speaking ,JVM The execution engine in acts as a translator to translate a high-level language into a machine language .

1605707645273

1.2 The basic execution process

We learned earlier that Unique to threads Program counter , Write down a line Address of instruction , Provided to the execution engine to execute ,

  1. What kind of bytecode instructions the execution engine needs to execute depends entirely on PC register .
  2. Every time after an instruction operation has been executed ,PC The register will update the address of the next instruction to be executed .
  3. Of course, the method is in the process of execution , It is possible for the execution engine to accurately locate the object reference stored in the local variable table Java Object instance information in the heap , And locate the type information of the target object through the metadata pointer in the object header .
  4. From the outside , be-all Java The execution engine input of the virtual machine 、 Handle 、 The output is consistent : The input is a bytecode binary stream , The processing is bytecode parsing execution 、 The equivalent of just in time compilation , The output is the execution process .

Sketch Map :

1605707898021

2. java The process of code compilation and execution

2.1 Java Code interpretation and execution and Compile implementation

Before most of the program code is converted into the object code of the physical machine or the instruction set executed by the virtual machine , You need to go through the steps in the figure below :

1605708749056

  1. The orange part in the front is the process of compiling and generating bytecode file (javac Instructions java File transfer class file ), and JVM irrelevant
  2. Back green ( Explain to perform ) And blue ( Just in time compilation ) It's just JVM The process of execution , Corresponding to the interpreter and compiler in the execution engine

What is an interpreter ? What is? JIT compiler ?

  • ** Interpreter : **

When Java The virtual machine will start according to Predefined specifications The bytecode is executed line by line , Put the contents of each bytecode file “ translate ” Execute local machine instructions for the corresponding platform .

  • JIT(Just In Time Compiler) compiler

The virtual machine will source code directly compile Become a machine language related to the local machine platform .

In the beginning java when , We call it java The language is Semi compiled and semi interpreted language , This is because jvm in That's what the execution engine does ,

  1. JDK1.0 Time , take Java Language orientation is “ Explain to perform ” It's more accurate . later ,Java We also developed compilers that can generate native code directly .
  2. Now? JVM In execution Java Code time , Usually, interpretation execution and compilation execution are combined .
  3. After translating bytecode into local code , You can do a cache operation , Stored in the method area JIT In the code cache

jvm Execution process summary chart :

1605709533579

2.2 Machine code , Instructions , assembly language

Machine code

  1. All kinds of instructions expressed in binary code , It's called machine code ( It's just a bunch of 010101, Can be directly cpu perform ). Start , People use it to write programs , This is machine language .
  2. Machine language can be understood and accepted by computers , But it's so different from people's language , It's not easy to be understood and remembered by people , And programming with it is easy to make mistakes .
  3. Once the program written with it is input into the computer ,CPU Read directly to run , So compared with programs written in other languages , Fastest execution .
  4. Machine instructions and CPU Closely related , So different kinds of CPU The corresponding machine instructions are different .

Instructions

  1. Because the machine code is made by 0 and 1 A binary sequence of components , The readability is really poor , So people invented instructions .

  2. An instruction is a machine code specific to 0 and 1 Sequence , Reduced to the corresponding instruction ( It is usually abbreviated in English , Such as mov,inc etc. ), It's a little more readable

    An instruction may represent a large series of machine codes with continuous functions

  3. Because of different hardware platforms , Perform the same operation , The corresponding machine code may be different , So the same instruction from different hardware platforms ( such as mov), The corresponding machine code may be different .

  4. Different hardware platforms , The instructions they support , There is a difference . So the instructions that each platform supports , Call it platform specific Instruction set .

    • x86 Instruction set , The corresponding is x86 Architecture platform
    • ARM Instruction set , The corresponding is ARM Architecture platform

assembly language

  1. Because the readability of the instructions is still too poor , So people invented assembly language .
  2. In assembly language , Use mnemonics (Mnemonics) An operation code that replaces a machine instruction , Using address symbols (Symbol) Or labelling (Label) The address of an instruction or operand . On different hardware platforms , Assembly languages correspond to different machine language instruction sets , Conversion to machine instructions by assembly process .
  3. Because computers only know machine code , So the program written in assembly language must be translated ( assembly ) Machine code , Computers can recognize and execute .

High-level language

  1. In order to make it easier for computer users to program , Later, various high-level computer languages appeared .(C,C++,JAVA)
  2. High level language is better than machine language 、 Assembly language is closer to human language when a computer executes a program written in a high-level language , Still need to interpret and compile the program into the machine's instruction code . The program that completes this process is called an interpreter or compiler .
  3. High level languages are not directly translated into Machine instructions
    • First, you need to go through the compilation phase , Translate high level language into assembly language
    • And then through the assembly phase , Translate the generated assembly language into machine instructions

C The flow of language :

  1. The build process : Is to read the source program ( Character stream ), Analyze the morphology and grammar of it , Convert high-level language instructions into functional equivalent assembly code
  2. Assembly process : In fact, it refers to the process of translating assembly language code into target machine instructions .

1605710567268

JAVA The language is similar , The first thing that will be written is JAVA The file is compiled into a bytecode file ( Also known as front-end compilation , Half of the compilation is done , reduce jvm The workload of compiling ), This is the bytecode instruction that we often see before , Here's a description of bytecode :

  1. Bytecode is an intermediate state ( Middle ) The binary code of ( file ), It's more abstract than machine code , You need to perform engine translation to become machine code
  2. Bytecode is mainly used to realize specific software operation and software environment 、 Nothing to do with the hardware environment .
  3. Bytecode is implemented through compilers and virtual machines . The compiler compiles the source code into bytecode , Virtual machines on specific platforms translate bytecodes into instructions that can be executed directly .

3. Interpreter

3.1 Why there's a bytecode middle code

I said before , jvm The execution engine in can put Bytecode Instructions for interpretation and execution , That's the same thing , Why? The interpreter cannot execute directly java Source code ,

Theoretically, it can

1605712315799

The picture above shows , java Language is coming jvm The first step , Will also java The source code has been compiled , Become class Bytecode , This step will be greatly saved jvm The job of ,javac take java The language is compiled into a lower level instruction form , And it is optimized in the process of compiling ( character string , Constant optimization ),

3.2 The function of the interpreter

  1. JVM The original intention of designers is simply to satisfy Java Program implementation cross platform features , Therefore, avoid using static compilation to generate local machine instructions directly , Thus, the idea of implementing interpreter executing program with line by line interpretation bytecode is born .
  2. The interpreter's real role is a runtime “ translator ”, Put the contents of bytecode file “ translate ” Execute local machine instructions for the corresponding platform .
  3. When a bytecode instruction is interpreted and executed , And then according to PC The next bytecode instruction recorded in the register that needs to be executed performs an interpretation operation .

3.3 The classification of interpreters

  1. stay Java In the development history of , There are two sets of interpretation actuators , That is, the old bytecode interpreter and the template interpreter now widely used .
    • Bytecode interpreter simulates bytecode execution through pure software code , Very inefficient .
    • The template interpreter associates each bytecode with a template function , The template function directly generates the machine code when the bytecode is executed , This greatly improves the performance of the interpreter .(Map Key value pair ?)

Now in the HotSpot VM in , The interpreter is mainly composed of Interpreter Module and Code Module composition .

  • Interpreter modular : The core function of the interpreter is realized
  • Code modular : Used to manage HotSpot VM Local machine instructions generated at runtime

3.4 The status quo of the interpreter

  1. Because the interpreter is very simple in design and Implementation , So in addition to Java Beyond language , Many high-level languages are also based on interpreter execution , such as Python、Perl、Ruby etc. . But today , Interpreter based execution has fallen into a synonym for inefficiency , And often by some C/C++ What programmers make fun of .
  2. To solve this problem ,JVM The platform supports a technique called just in time compilation . The purpose of just in time compilation is to avoid functions being interpreted and executed , It's the whole machine , Every time a function is executed , Just execute the compiled machine code , In this way, the efficiency of execution can be greatly improved .
  3. But anyway , Interpreter based execution mode still makes an indelible contribution to the development of intermediate language .

Now , The interpreter is also an integral part of the execution engine , Even though it compiles instantly There are many advantages , But it also has its disadvantages , Need to be supplemented by the interpreter , Two together , It is the best ( Yida makes money )

4. JIT compiler

4.1 . summary

  1. HotSpot VM It is one of the representative works of high performance virtual machine on the market . It uses an interpreter and just in time compiler architecture .
  2. stay Java When the virtual machine is running , Interpreters and just in time compilers can work together , Learn from each other , Try to choose the most appropriate way to balance the time it takes to compile native code and the time to directly interpret and execute code .
  3. In today's ,Java The performance of the program has been completely changed , Have reached the point where you can and C/C++ The program is higher and lower .

What then? We have more advanced JIT compiler , And keep the interpreter ?

  1. such as JRockit VM There's no interpreter inside , Bytecodes are all compiled and executed by just in time compilers . JRockit Virtual machines cut down the interpreter , Just in time compilers . That's because JRockit Only deployed on the server , There is usually time for him to compile instructions , It's not very demanding for response , After the completion of the compiler in time , Will provide better performance
  2. When the program starts , The interpreter can work immediately , Fast response , Save compilation time , Execute now . The compiler wants to work , Compiling code into native code , It takes a certain amount of execution time , But after compiling it into local code , High execution efficiency .
  3. Even though JRockit VM The execution performance of the program in will be very efficient , But the program must take longer to compile when it starts . For server applications , Starting time is not the focus , But for those scenarios that are focused on startup time , Perhaps we need to use the interpreter and the instant compiler architecture to trade off a balance point .
  4. When an interpreter coexists with an instant compiler , stay Java When the virtual machine starts , The interpreter works first , You don't have to wait for the instant compiler to complete its compilation before executing , This saves a lot of unnecessary compilation time . as time goes on , Compilers work , Compile more and more code into local code , Get more execution efficiency .
  5. meanwhile , Explain execution when radical optimization by the compiler doesn't work , As a compiler “ Escape door ”.

summary :

  1. When the virtual machine starts , The interpreter works first , You don't have to wait for the instant compiler to complete its compilation before executing , This saves a lot of unnecessary <.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012071713131611.html