My JVM study notes: runtime data area (3) detailed operand stack

Thanks to Mr. Song Hongkang from Shang Sigu for the JVM entry to the proficient course, and salute every teacher who teaches free courses attentively!
This set of tutorials are all my study notes after the course, to prevent forgetting, and send to everyone to share, thank you for viewing~

1. Last content

Last time we mentioned that the stack frame contains: local variable table, operand stack, dynamic link, method return address.
In the previous article, we focused on the local variable table, and those who are interested can check it out: My JVM study notes: runtime data area (2) Detailed local variable table
This article, let us focus on learning about the stack Other structures included in the frame memory: operand stack!

2. Detailed explanation of operand stack (Operand Stack)

2.1 Understanding of the stack:

The stack can be implemented using an array or a linked list, first-in-last-out (last-in, first-out), only the tail data can be added or deleted, the operation corresponds to only stacking and popping, and no direct manipulation of intermediate data!
Insert picture description here

2.2 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.

In the operand stack, during the execution of the method, according to bytecode instructions, data is written to or extracted from the stack, that is, push/pop.

  • Some bytecode instructions push values ​​onto the operand stack, and other bytecode instructions take the operand out of the stack. After using them, push the result onto the stack.
  • For example: perform operations such as copying, swapping, summing, etc.

Example: addition operation
Insert picture description here

Sample code:

public static void main(String[] args)
{
    
    
   int a = 8;
   int b = 15;
   int c = a + b;
}

Insert picture description here
Perform process analysis:

JVM is a stack-based running engine. All operations have only two operations: push and pop, and operations can be performed on the stack.

  1. When we define the variable a, the JVM will first push operand 8 onto the stack, and then pop the operand 8 into the location of the local variable table index1.
  2. When we define variable b, JVM will first push operand 15 onto the stack, and then pop operand 15 into the local variable table index2.
  3. Because all operations of the JVM are performed on the stack, when computing a+b, the JVM will divide the value of ab into the operand stack, and then perform the addition operation to get the result 23
  4. After the operation is completed, the operand 23 automatically becomes the top of the stack, and the stack operation is performed, and 23 is put into the position of the local variable table index3 (variable c), and the program ends.

Metaphorical understanding:
If you compare the local variable table to a shelf, there are two goods 8 and 15 on the shelf, and the shelf cannot directly process the goods, and the goods can only be transferred to the processing area (operating stack). , Put it into c shelf after processing operation.

to sum up:

  1. Operand stack,Mainly used to save the intermediate results of the calculation process, and as a temporary storage space for variables during the calculation process

  2. The operand stack is a work area of ​​the JVM execution engine. When a method is first executed, a new stack frame will be created, and the operand stack of this method is empty .

  3. 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 as the value of max_stack .

    note:

    • The size of the operand stack is not directly related to the size of the local variable table (it is determined that there will be at most several operands operated at the same time in this method)!
    • The size of the operand stack is determined at compile time and cannot be changed!
      Insert picture description here
  4. Any element in the stack can be any Java data type .

    • The 32bit type occupies a stack unit depth.
    • The 64bit type occupies two stack unit depths
  5. The operand stack is not used to access the index for data access, but only through standard push and pop operations to complete a data access.

  6. 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 to be executed in the PC register is updated.

    That is to say: if this method has a return value, just before the end of the method, the JVM will push the return value into the operand stack, and after returning, the return value can be directly taken out of the current stack and pushed into the operand stack of the external method .

    	public void test()
    		    {
          
          
    		        int a = getNum();
    		        int b = 0;
    		    }
    		
    		    public int getNum()
    		    {
          
          
    		        int b = 1;
    		        return b;
    		    }
    

    Insert picture description here

    The getNum method pushes the value of variable b into the operand stack and returns

    Insert picture description here

    Then when the assignment operation to a is executed, it can be loaded directly from the operand stack of the previous method

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 when completing an operation, which also means that more instruction dispatch will be required ( instruction dispatch) times and memory read/write times.

Since the operands are stored in memory, frequent memory read/write operations will inevitably affect the execution speed. To solve this problem, the designers of HotSpot JVM proposed Top-of-Stack Cashing (ToS, Top-of-Stack Cashing) technology, which caches all stack item elements in the registers of the physical CPU, thereby reducing memory read/write Times, improve the execution efficiency of the execution engine.

That is to say: because the operand stack is read and written very frequently, the jvm directly caches all the top elements of the stack in the cpu register to increase the access speed!

to sum up:

The operand stack is one of the core concepts of the JVM. We say that Java is a stack-based execution engine. The stack here refers to the operand stack, so we must understand this knowledge. Thank you for watching.

Guess you like

Origin blog.csdn.net/qq_42628989/article/details/107523139