Virtual machine byte code execution engine
1. All Java Virtual machine execution engines are consistent: the input is a bytecode file, processing is the efficient process of byte code parsing, the output is the execution results.
2. Runtime stack frame structure:
A stack frame is a data structure that supports the method invocation and execution of a virtual machine, which is the stack element of the virtual machine stack in the data area when the VM is running. Stack frames Store the method's local variable table, operand stack, dynamic link and method return address information, each method call from the beginning of the call to the completion of the execution of a stack frame in the virtual machine stack from the stack into the process. The conceptual structure of the stack frame is as follows:
Here is a detailed explanation of the functions and data structures of each part of the stack frame:
① Local Variables table: Store the method parameters and local variables defined within the method (note that the data structure of the local variable table is not a stack). The local variable table takes the variable slot slot as the basic unit, and the slot length is not specified, and for some long data to occupy multiple slots, it is allocated a contiguous slot space in a high-alignment manner.
How do virtual machines use local variable tables? Here's how:
② operand stack: is a post-in, first-out stack that stores the operands and operation results of the bytecode instruction. at the beginning of the execution of the method, the operand stack of this method is empty , during the execution of the method, various bytecode instructions will write to the operand stack and extract the contents , that is, into the stack/stack operation.
③ dynamic Link: Saves the base address of the stack frame.
④ method Return Address: Save the return address.
3. Method invocation mechanism for Java virtual machines:
Method invocation differs from method execution, and the only task for a method invocation is to determine the version of the called Method , which is the method that is called, which temporarily does not involve the method's execution.
① parsing: All method calls in the target method in the class file is a constant pool of symbolic references, during the parsing phase of the class loading , some of these symbolic references will be converted to direct references, this parsing can be established as a prerequisite: The method has a deterministic version of the call before it actually runs, and the transfer version of the method is immutable at run time . The invocation of such a method is called parsing.
② allocation:
Java is an object-oriented language because Java has three basic features of object-oriented: inheritance, encapsulation, and polymorphism. In this section we are concerned with how overloading and overwriting are implemented, and how to find the right target method.
⑴ Static allocation:
The dispatch action that relies on a static type (that is, the type that the skin type, which is used to declare the variable) to locate the execution version of the method is called a static dispatch. A typical function of static dispatch is method overloading. That is, the virtual machine is overloaded by the static type of the parameter instead of the actual type (you might have a reference to a parent class to the object of a subclass, where the subclass is the actual type referenced by the parent class) to judge the basis. For example:
The result of its execution is: Hello, guy!
Hello, guy!.
static dispatch occurs during the compile phase , so statically dispatched actions are not actually done by the virtual machine. Sometimes the overloaded version is not unique, and this happens when you use the literal as the argument, the literal does not display the static type, this time to follow the "nearest principle" to determine the overloaded version. For example, there are two methods of void F (int) and void F (double) in the code, and now I make the method call F (1), then the f (int) method will be called, and if there is no f (int) method in the code, the compiler will choose the f (double) method to execute. This is not in detail.
⑵ Dynamic Dispatch:
Dynamic allocation is very important in relation to coverage. First look at the following code:
It outputs the following results:
The result is not surprising to us, but how does a virtual machine know which method to call?
We use the JAVAP tool to output its bytecode for this class of. class files, and we find that both method calls in the class file are the same invokevirtual #22, which means that the instructions and parameters are identical, but their execution results are not the same. This is going to start with the polymorphic lookup process of the invokevirtual directive, the runtime parsing process of the Invokevirtual directive is broadly divided into the following steps:
So the result of the example above is self-evident. We call this process of executing a version at run time based on the actual type determination method called dynamic dispatch.
⑶ single Dispatch and multiple dispatch:
The parameters of the method's recipient and method are collectively referred to as the method's volume, according to how many kinds of parcels the allocation is based on, the allocation is divided into single and multiple allocations, the single dispatch selects the target method based on one volume, and multiple allocations select the target method based on multiple parcels.
The static assignment of the Java language is a multi-dispatch type, and dynamic dispatch is a single-dispatch type.
⑷ implementation of dynamic dispatch of virtual machines:
The dispatch process described earlier, as an analysis of the virtual machine conceptual model, is basically sufficient, and it solves the problem of what the virtual machine "will do" during the dispatch process. But the virtual machine "How to Do", the virtual opportunities are different.
Because dynamic dispatch is a very frequent action, and the dynamically dispatched method version selection process requires the runtime to search for the appropriate target method in the class's method metadata, the actual implementation of the virtual machine is based on performance considerations, most of which do not really carry out such frequent searches. In the face of this situation, the most commonly used "stability optimization" method is to create a virtual method table for the class in the method area , corresponding to this, the interface method table is used when Invokeinterface executes, and the virtual method table index is used instead of the metadata lookup to improve the performance . The virtual method table structure is as follows:
Virtual method Table Structure
The virtual method table holds the actual entry address of each method. If a method is not overridden in a subclass, then the address entry in the virtual method table of the subclass and the address entry for the same method as the parent class are consistent, pointing to the implementation portal of the parent class, and if the subclass overrides this method, the address in the subclass method table will be replaced with the entry address of the subclass implementation version.
For the convenience of the program, with the same signature method, the parent class and the subclass of the virtual method table should have the same index number, so when the type transformation, only need to change the method table lookup, you can from the different virtual method table by index to convert the required entry address.
The method table is typically initialized at the connection stage of the class load, and after the class's variable initial value is prepared, the virtual opportunity initializes the method table for that class.
4. Stack-based byte-code interpretation execution engine:
This section describes how a virtual machine executes bytecode directives in a method.
① Stack-based instruction set and register-based instruction set:
The Java compiler outputs the instruction stream, which is basically a stack-based instruction set structure. The instructions in the instruction flow are mostly 0 address directives (0 address instructions are the 0 operand addresses in the instruction), and they depend on the operand stack to work. Another set of commonly used instruction set structures that correspond to this is the register-based instruction set structure. Take a simple example to illustrate the difference: use both to calculate the two:
The stack-based command assembly is this:
Two iconst_1 instructions will continuously put two constant 1 into the stack, iadd instruction to stack the top two values out of the stack, add, and then put the results back to the top of the stack, and finally istore_0 the value of the top of the stack back into the No. 0 slot of the local variable table.
The register-based instruction set may be this:
The MOV instruction sets the EAX register to 1 and then eax plus 1 to save back to EAX.
Comparison of the advantages and disadvantages of the two instruction set structures:
② Stack-based interpreter execution process:
Examine this piece of code:
Use JAVAP to view his bytecode instructions as follows:
The code, the operand stack, and the local variable table change in the execution process as follows:
It is worth mentioning that in the generated bytecode directive, there is no information about the "a", "B", "C" identifiers, the bytecode instruction is only the local variable table, operand operation in the operation of the number of instructions, it is how to know the calculation (a+b) * What are the corresponding operands of the local variable table when C is taken? We can see that the instruction generated when int a=100 is Istore_1,int b=200 is generated when Istore_2,int c=300 is generated istore_3, that is, although the variable identifier is not explicitly corresponding to the slot in the local variable table, But it already uses the default way of declaring the variables in the order in which they are declared (and the order in which they are passed).
Similarly, in C, C + + and other high-level language programs generated by the EXE file, after disassembly we can see for an int a=100; statement produced by the assembly language instruction is 003513BE mov dword ptr [a],64h , he said put 10 0 stored in a double word of the address indicated by PTR [a], in the instruction that the machine eventually runs, it is a specific address, and there is no information about identifier a.
In-depth understanding of the JVM notes---the eighth chapter