The stack frame structure
A stack frame is a data structure that is pushed onto the stack as the method is called. Only the top stack frame is valid for the execution engine. A stack frame includes a local variable table, operand stack, dynamic linkage, method return address, and so on
Local variable scale
Each slot can be a 32-bit memory space used to store Boolean, byte, CHAR, short, int, float, Reference, or returnAddress data. For 64-bit data, only longs and doubles occupy two slots, and reference has no specified length.
Slot can be reused to save stack frame space, but reuse may have additional side effects.
The operand stack
The operand stack is designed to provide operands for bytecode instructions, and it starts out empty, as instructions write or data to the operand stack. Each element of the operand stack can be any Java data type, including long and double, but the depth of the operand stack cannot exceed the maximum value set in the max_stacks data item.
Dynamic connection
Each stack frame contains a reference to the method that the stack frame belongs to in the runtime constant pool, and this reference is held to support dynamic concatenation during method calls. As described in Java Virtual Machine Series and other File Structures, the constant pool holds a large number of symbolic references, and the method invocation instructions in bytecode take symbolic references to methods in the constant pool as arguments. Some of these symbolic references are converted to direct references when the class is loaded or used for the first time as static parsing. The other part is converted to a direct reference during each run, and this part becomes the dynamic join.
Method return address
The return address of the method is the address of the bytecode instruction that continues execution after the method returns.
The method call
Method invocation is not equal to method execution. The only task of method invocation stage is to determine the version of the called method (that is, which method to call), and the instruction execution inside the method is not involved.
parsing
Some of these symbolic references are converted to direct references during the parsing phase of the class load, provided that the method has a determinable invocation version before it is actually run and is immutable at run time. Calls to such methods are called parsing.
These include static methods, private methods, instance constructors, and superclass methods
The dispatch
A resolution call is a static process that is determined at compile time, whereas a dispatch call can be static or dynamic
Static dispatching
Static dispatch applies to method overloading and occurs at compile time, as in
public class StaticDispatch{
static abstract class Human{}
static class Man extends Human{}
static class Woman extends Human{}
public void sayHello(Human guy){
System.out.println("hello,guy");
}
public void sayHello(Man guy){
System.out.println("hello,gentleman");
}
public void sayHello(Woman guy){
System.out.println("hello,llady");
}
public static void main(String[] args) {
Human man = new Man();
Human woman = new Woman();
StaticDispatch sr = newStaticDispatch(); sr.sayHello(man); sr.sayHello(woman); }}Copy the code
Output result:
hello,guy! hello,guy!
In the main method, Human becomes the static type of the variable, and Man and Woman become the actual type of the variable. Which overloaded version of the method to use is entirely dependent on the number of data types passed in as long as the receiver of the method has identified the object SR. At compile time, the statically typed virtual machine is known, so the static type is used to call the method.
Dynamic dispatch
Dynamic dispatch is related to the rewriting of polymorphism
public class StaticDispatch{
static abstract class Human{
protected abstact void sayHello(a);
}
static class Man extends Human{
@Override
protected void sayHello(a) {
System.out.println("man says hello"); }}static class Woman extends Human{
@Override
protected void sayHello(a) {
System.out.println("woman says hello"); }}public static void main(String[] args) {
Human man = new Man();
Human woman = new Woman();
man.sayHello();
woman.sayHello();
man = newWoman(); man.sayHello(woman); }}Copy the code
Result output:
man says hello
woman says hello
woman says hello
This result is not surprising, as it shows that the virtual machine dispatches the version of method execution based on the actual type.
What about the implementation of dynamic dispatch? Is the use of virtual method table to achieve, the specific first keep, such as after the time to fill……
Bytecode parsing execution
How does a virtual machine execute bytecode instructions in a method
Parsing performed
Many Java virtual machine execution engines have a choice between parsing execution (through a parser) and compiler execution (through a just-in-time compiler that generates native code execution) when executing Java code. Most program code passes through the following steps before it reaches the object code of the physical machine or the instruction set executed by the virtual machine.
Like C/C++, lexical analysis, syntax analysis, and subsequent optimizers and generators can be independent of the execution engine. In the case of the Java language, you can choose to implement part of it (to the abstract syntax tree) as a semi-independent compiler. The Java compiler completes the process of program code through lexical analysis, syntax analysis to abstract syntax tree, and then traverses the syntax tree to generate linear bytecode instruction stream.
Stack – based instruction set and register – based instruction set
By stack-based and register-based, we mean that instructions operate on stacks or registers. The expression of stack-based instruction set is:
iconst_1
iadd
istore_0
The register-based instruction set is expressed as:
mov eax, 1
add eax, 1
The instruction set based on stack is only the instruction set implemented by virtual machine for portability, and virtual machine can not escape the instruction set based on register, so the realization of the instruction set based on stack, or rely on the realization of the instruction set based on register. No one objects to this……