JVM---virtual machine stack (dynamic link and method return address)

Virtual machine stack-dynamic link

Insert picture description here

  • Dynamic link, method return address, additional information: some places are called frame data area;
  • Each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool. The purpose of this reference is to support the code of the current method to achieve dynamic linking (Dynamic Linking), such as: invoke instruction;
  • When Java source files are compiled into bytecode files, all variables and method references are stored as symbolic references in the constant pool of the class file.

For example: when a method calls another method, it is represented by the symbolic references pointing to the method in the constant pool, then the function of dynamic linking is to convert these symbolic references into direct references to the calling method (that is, the method needs to run The information is stored in the constant pool. When the method is executed, it needs to go to the constant pool to get it, and the method to get it is dynamic linking).
Insert picture description here
Why do we need a runtime constant pool?

Because different methods may call constants or methods, storing a copy in the constant pool can save space. The function of the constant pool is to provide some symbols and constants to facilitate the identification of instructions.

Method return address

The method return address stores the value of the pc register that called the method.

There are two ways to end a method:

  • Normal execution completed;
  • An unhandled exception occurred and abnormal exit occurred.

No matter which way to exit, after the method exits, it will return to the place where the method was 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; while exiting through an exception, the return address must be determined by the exception table, which is generally not in the stack frame Save this part of the information.

  • After a method is called normally, which return instruction needs to be used depends on the actual data type of the return value of the method;
  • In bytecode instructions, return instructions include ireturn (used when the return value is boolean, byte, char, short, and int), lreturn (Long type), freturn (Float type), dreturn (Double type), and areturn. In addition, there is a method where the return instruction is declared as void, the instance initialization method, and the initialization method of the class and interface is used.

Method execution encountered an exception:

  • Encountered an exception (Exception) during the execution of the method, and this exception is not handled in the method, that is to say, if no matching exception handler is found in the exception table of this method, the method will exit;
  • During the execution of the method, the exception handling when an exception is thrown is stored in an exception handling table, so that it is convenient to find the exception handling code when an exception occurs;
  • Exiting through an abnormal completion exit will not generate any return value to his upper caller.
    Insert picture description here
    Essentially, the exit of the method is the process of popping the current stack frame. At this point, it is necessary to restore the local variable table and operand stack of the upper method, push the return value into the operand stack of the caller's stack frame, set the PC register value, etc., so that the caller method can continue to execute.

Some additional information

The stack frame is also allowed to carry some additional information related to the implementation of the Java virtual machine. For example: information that supports program debugging.

Guess you like

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