JVM stack frame

 

There are stored in the stack frame
  • Local variable table
  • Operand stack (or expression stack)
  • Dynamic linking (or method reference to runtime constant pool)
  • Method return address (or definition when the method exits normally or when it exits abnormally)
  • Some additional information

 

First, the local variable table
It is a set of variable value storage space for storing method parameters and local variables defined within the method. When the Java program is compiled into a Class file, the maximum capacity of the local variable table that the method needs to allocate is determined in the max_locals data item of the Code property of the method.
  • The most basic storage unit of the local variable table is Slot (variable slot)
  • The local variable table stores various basic data types (8 types), reference types (reference) and returnAddress types known at compile time
  • In the local variable table, types within 32 bits occupy only one slot (including returnAddress), and types of 64 bits (long and double) occupy 2 slots
  • byte, short, char are converted to int before storage, boolean is also converted to int, 0 means false, non-zero means true.
  • Local variable table is also called local variable array or local variable table
  • The required capacity of the local variable table is determined at compile time and stored in the maximum local variables data item of the Code property of the method. The size of the local variable table will not change during the execution of the method

 

Run the command: javap -verbose to execute the .class file

 

Second, the operand stack
The stack can be implemented using arrays or linked lists. The operand stack is also called the expression stack

  • 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 execution, a new stack frame will be created accordingly. 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 at compile time and stored in the Code property 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 a stack unit depth
  • The operand stack does not use the index access method for data access, but can only complete a data access through standard push and pop operations.
 
During the execution of the method, according to the bytecode instructions, write data to the stack or extract data, that is, on / off the stack
Some bytecode instructions push the value onto the operand stack, and the remaining bytecode instructions pull the operand off the stack. After using them, push the result onto the stack.

 

Examples

public  void testAddOperation () {
     byte i = 15 ;
     int j = 8 ;
     int k = i + j; 
} 
// corresponding code, namely byte code instruction 
 0 bipush 15 
 2 istore_1
  3 bipush 8 
 5 istore_2
  6 iload_1
  7 iload_2
  8 iadd
  9 istore_3
 10 return

Steps

 

 

Three, dynamic link
Dynamic linking is also referred to as a method reference to the runtime constant pool
Each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool. The purpose of including this reference is to support the code of the current method to achieve dynamic linking (Dynamic Linking) such as: invokeddynamic instruction.
When the Java source file is compiled into a bytecode file, all variable and method references are stored as symbol references (Symbolic Refrence) in the constant pool of the class file. For example: when describing a method that calls another method, it is represented by a symbol reference that points to the method in the constant pool, then the role of dynamic linking is to convert these symbol references into direct references to the calling method.
 

 

Four, method return address
  • Store the value of the pc register calling this method
  • There are two ways to end a method: normal execution is completed; an unhandled exception occurs, and abnormal exit.
  • No matter which way to exit, after the method exits, it returns to the position where the method is called. When the method exits normally, the value of the caller's pc counter is used as the return address, that is, the address of the next instruction of the instruction that called the method. When exiting by exception, the return address is determined by the exception table, and this part of the information is generally not saved in the stack frame.
 
In essence, the exit of the method is the process of popping the current stack frame. At this point, you need to restore the local variable table of the upper-level method, the operand stack, push the return value into the caller stack frame's operand stack, set the PC register value, etc., so that the caller method continues to execute.
The difference between the normal completion exit and the abnormal completion exit is that exiting through the abnormal completion exit will not generate any return value to its upper caller.
 
When a method starts executing, there are only two ways to exit the method:
1. When the execution engine encounters a bytecode instruction (return) returned by any method, the return value will be passed to the upper method caller, referred to as normal completion exit;
    Which return instruction a method needs to use after the normal call is completed also depends on the actual data type of the method return value.
    In bytecode instructions, the return instruction includes ireturn (when the return value is boolean, byte, char, short, and int types), lretrun, freturn, dreturn, and areturn, and there is a return instruction for the method and instance declared as void Initialization methods, class and interface initialization methods are used.
2. An exception was encountered during the execution of the method, and this exception was not handled within the method, that is, as long as no matching exception handler was found in the exception table of this method, the method would exit. Abbreviated as abnormal completion of export.

 

Guess you like

Origin www.cnblogs.com/caoxb/p/12735566.html