JVM---virtual machine stack (operand stack)

Virtual machine stack-operand stack

Insert picture description here
concept

Operand stack: Operand Stack

  • In addition to the local variable table, each independent stack frame also contains a last-in first-out operand stack, which can also be called an expression stack (Expression Stack);
  • Operand stack, in the process of method execution, according to bytecode instructions, write data to the stack or extract data, that is, push and pop;
  • Some bytecode instructions push the value into the operand stack, and the rest of the bytecode instructions pull the operand out of the stack, and then push the result onto the stack after using them, such as performing operations such as copying, swapping, and summing;
  • The operand stack is mainly used to save the intermediate results of the calculation process, and at the same time as a temporary storage space for variables in the calculation process.
    Insert picture description here
  • The operand stack is a work area of ​​the JVM execution engine. When a method is first executed, a new stack frame will also be created. At this time, the operand stack of the method is empty;
  • Each operand stack will have a clear stack depth for storing values. The maximum depth required is defined at compile time and stored in the Code attribute of the method, which is the value of maxstack;
  • Any element in the stack can be any Java data type;
  • In the operand stack, the 32-bit type occupies one stack unit depth, and the 64-bit type occupies two stack unit depths;
  • The operand stack is not used to access the index for data access, but can only complete a data access through standard stacking and popping operations;
  • If the called method has a return value, its return value will be pushed into the operand stack of the current stack frame, and the next bytecode instruction that needs to be executed in the PC register is updated (the method is executed, return to Bytecode instructions of the previous method);
  • The data type of the elements in the operand stack must strictly match the sequence of bytecode instructions, which is verified by the compiler during the compiler, and it must be verified again in the data flow analysis phase of the class verification phase in the class loading process. |

Note: The interpretation engine of the Java virtual machine is a stack-based execution engine, where the stack refers to the operand stack.

Code explanation

Give a piece of code:

public void testAddOperation() {
    
    
    byte i = 15;
    int j = 8;
    int k = i + j;
}

Compiled into bytecode instructions:
Insert picture description here
byte, short, char, boolean are internally stored using int type.

From the above code, we can see that operands 15 and 8 are pushed into the stack by bipush, and the iadd method is used for the addition operation, where i stands for int, which is the addition operation of the int type.

The execution process is as follows:

  1. First execute the first statement, the PC register points to 0, that is, the instruction address is 0, and then use bipush to put operand 15 on the stack;
    Insert picture description here
  2. After execution, let PC +1 point to the next line of code. The next line of code is to store the elements of the operand stack to the location of local variable table 1. You can see that an element has been added to the local variable table;
    Insert picture description here

Why doesn't the local variable table start from 0?

  • In fact, the local variable table also starts from 0, but because the this pointer is stored at position 0, it is omitted directly.
  1. Then PC+1, which points to the next line. Let operand 8 also be put on the stack, and the store operation will be executed at the same time, and stored in the local variable table;
    Insert picture description hereInsert picture description here
  2. Then from the local variable table, put the data in the operand stack in turn;
    Insert picture description here
    Insert picture description here
  3. Then the two elements in the operand stack are added together and stored in the location of the local variable table 3;
    Insert picture description here
    Insert picture description here
  4. Finally, the location of the PC register points to 10, which is the return method, and the method is directly exited

Top of Stack Cache Technology

  • The zero-address instructions used by virtual machines based on the stack architecture are more compact, but it is necessary to use more stack and pop instructions to complete an operation, which also means that more instruction dispatch will be required ( instructiondispatch) times and memory read/write times;
  • Since the operands are stored in the memory, frequent memory read/write operations will inevitably affect the execution speed. In order to solve this problem, the designers of HotSpot JVM proposed the Top-of-Stack Cashing (Tos, Top-of-Stack Cashing) technology, which caches all the top-of-stack elements in the registers of the physical CPU, thereby reducing the read/write of the memory Times, improve the execution efficiency of the execution engine.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113810225