JVM_03 Runtime data area 1_Stack_Operand Stack_Operand Stack

Operand Stack (Operand Stack)

Stack: can be implemented using an array or a linked list

  • In addition to the local variable table, each independent stack frame also contains a last-in, first-out operand stack, which can also become an expression stack
    operand stack. During the execution of the method, it is transferred to the stack according to the bytecode instructions. Write data or extract data, that is, push or pop

    • Some bytecode instructions push the value onto the operand stack, and the rest of the bytecode instructions take the operand out of the stack, and then push the result onto the stack after using them. (Such as bytecode instruction bipush operation)
    • For example: perform operations such as copying, swapping, summing, etc.
      Insert picture description here
2.4.1 Overview
  • 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.

  • The operand stack is a work area of ​​the JVM execution engine. When a method starts to execute, a new stack frame will also be created. The operand stack of this method is empty

  • Each operand stack will have a clear stack depth for storing values. The maximum depth required is defined by the compiler and stored in the code attribute of the method as the value of max_stack.

  • 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 depth units
  • The operand stack is not used to access the index for data access, but only through push and pop operations of the standard brick to complete a data access

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

  • 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 compilation, and it must be verified again in the data flow analysis phase of the class verification phase in the class loading process.

  • In addition, we say that the interpretation engine of the Java virtual machine is a stack-based execution engine, where the stack refers to the operand stack.

  • Combine the above figure and the following figure to see the execution process of a method (stack frame)

①15 into the stack; ②store 15, 15 into the local variable table

Insert picture description here
③Press 8; ④Store 8, 8 into the local variable table;

Insert picture description here

**⑤Get the data with index 1 and 2 from the local variable table and put it on the operand stack; ⑥iadd addition operation, 8 and 15 pop out of the stack
**
Insert picture description here

**⑦iadd operation result 23 is pushed onto the stack; ⑧ 23 is stored in the local variable table index 3
**

Insert picture description here

Top-of-Stack Cashing (ToS)
  • 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 ( instruction dispatch) 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 a stack top cache technology, which caches all the top elements of the stack in the CPU registers in the room, so as to reduce the number of reads/writes to the memory and improve the execution efficiency of the epidemic.

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/114581355