[jvm] Dynamic link of virtual machine stack

Directory

        • 1. Description
        • 2. Code examples
        • 3. Generated bytecode
        • 4. Bytecode description
          • 4.1 Constant pool
          • 4.1 Method call
          • 4.3 Variables
          • 4.4 String
          • 4.5 Parent class Object
          • 4.6 System class
1. Description
  • 1. Method reference pointing to the runtime constant pool
  • 2. Each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool
  • 3. The purpose of including this reference is to support the code of the current method to implement dynamic linking (Dynamic Linking)
  • 4. When the java source file is compiled into the bytecode file, all variable and method references are saved in the constant pool of the class file as symbolic references (Symbolic Reference). For example: when describing a method that calls another method, it is represented by a symbolic reference pointing to the method in the constant pool
  • 5. The function of dynamic linking is to convert these symbol references into direct references to the calling method
2. Code examples
package com.learning.stack.dynamic_linking;

/**
 * @Author wangyouhui
 * @Description dynamic link
 **/
public class DynamicLinkingTest {
    int num = 10;

    public void methodOne(){
        System.out.println("Method 1 was executed");
        methodTwo();
        num + + ;
    }

    public void methodTwo(){
        System.out.println("Method 2 was executed");
    }
}
3. Generated bytecode
Classfile /F:/jdk-learning/jvm/target/classes/com/learning/stack/dynamic_linking/DynamicLinkingTest.class
  Last modified 2023-10-28; size 756 bytes
  MD5 checksum 96b3029ecd8f2d56b0fc46825936f83b
  Compiled from "DynamicLinkingTest.java"
public class com.learning.stack.dynamic_linking.DynamicLinkingTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref #9.#23 // java/lang/Object."<init>":()V
   #2 = Fieldref #8.#24 // com/learning/stack/dynamic_linking/DynamicLinkingTest.num:I
   #3 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
   #4 = String #27 // Method 1 is executed
   #5 = Methodref #28.#29 // java/io/PrintStream.println:(Ljava/lang/String;)V
   #6 = Methodref #8.#30 // com/learning/stack/dynamic_linking/DynamicLinkingTest.methodTwo:()V
   #7 = String #31 // Method 2 is executed
   #8 = Class #32 // com/learning/stack/dynamic_linking/DynamicLinkingTest
   #9 = Class #33 // java/lang/Object
  #10 = Utf8num
  #11 = Utf8 I
  #12 = Utf8 <init>
  #13 = Utf8 ()V
  #14 = Utf8 Code
  #15 = Utf8 LineNumberTable
  #16 = Utf8 LocalVariableTable
  #17 = Utf8 this
  #18 = Utf8 Lcom/learning/stack/dynamic_linking/DynamicLinkingTest;
  #19 = Utf8 methodOne
  #20 = Utf8 methodTwo
  #21 = Utf8 SourceFile
  #22 = Utf8 DynamicLinkingTest.java
  #23 = NameAndType #12:#13 // "<init>":()V
  #24 = NameAndType #10:#11 // num:I
  #25 = Class #34 // java/lang/System
  #26 = NameAndType #35:#36 // out:Ljava/io/PrintStream;
  #27 = Utf8 method 1 executed
  #28 = Class #37 // java/io/PrintStream
  #29 = NameAndType #38:#39 // println:(Ljava/lang/String;)V
  #30 = NameAndType #20:#13 // methodTwo:()V
  #31 = Utf8 method 2 is executed
  #32 = Utf8 com/learning/stack/dynamic_linking/DynamicLinkingTest
  #33 = Utf8 java/lang/Object
  #34 = Utf8 java/lang/System
  #35 = Utf8 out
  #36 = Utf8 Ljava/io/PrintStream;
  #37 = Utf8 java/io/PrintStream
  #38 = Utf8 println
  #39 = Utf8 (Ljava/lang/String;)V
{
  int num;
    descriptor: I
    flags:

  public com.learning.stack.dynamic_linking.DynamicLinkingTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1 // Method java/lang/Object."<init>":()V
         4: aload_0
         5: bipush 10
         7: putfield #2 // Field num:I
        10: return
      LineNumberTable:
        line 7: 0
        line 8: 4
      LocalVariableTable:
        Start Length Slot Name Signature
            0 11 0 this Lcom/learning/stack/dynamic_linking/DynamicLinkingTest;

  public void methodOne();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=1, args_size=1
         0: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc #4 // String method 1 is executed
         5: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: aload_0
         9: invokevirtual #6 // Method methodTwo:()V
        12: aload_0
        13: dup
        14: getfield #2 // Field num:I
        17: iconst_1
        18: iadd
        19: putfield #2 // Field num:I
        22: return
      LineNumberTable:
        line 11: 0
        line 12: 8
        line 13: 12
        line 14: 22
      LocalVariableTable:
        Start Length Slot Name Signature
            0 23 0 this Lcom/learning/stack/dynamic_linking/DynamicLinkingTest;

  public void methodTwo();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc #7 // String method 2 is executed
         5: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 17: 0
        line 18: 8
      LocalVariableTable:
        Start Length Slot Name Signature
            0 9 0 this Lcom/learning/stack/dynamic_linking/DynamicLinkingTest;
}
SourceFile: "DynamicLinkingTest.java"
4. Bytecode description
4.1 Constant Pool
Constant pool:
   #1 = Methodref #9.#23 // java/lang/Object."<init>":()V
   #2 = Fieldref #8.#24 // com/learning/stack/dynamic_linking/DynamicLinkingTest.num:I
   #3 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
   #4 = String #27 // Method 1 is executed
   #5 = Methodref #28.#29 // java/io/PrintStream.println:(Ljava/lang/String;)V
   #6 = Methodref #8.#30 // com/learning/stack/dynamic_linking/DynamicLinkingTest.methodTwo:()V
   #7 = String #31 // Method 2 is executed
   #8 = Class #32 // com/learning/stack/dynamic_linking/DynamicLinkingTest
   #9 = Class #33 // java/lang/Object
  #10 = Utf8num
  #11 = Utf8 I
  #12 = Utf8 <init>
  #13 = Utf8 ()V
  #14 = Utf8 Code
  #15 = Utf8 LineNumberTable
  #16 = Utf8 LocalVariableTable
  #17 = Utf8 this
  #18 = Utf8 Lcom/learning/stack/dynamic_linking/DynamicLinkingTest;
  #19 = Utf8 methodOne
  #20 = Utf8 methodTwo
  #21 = Utf8 SourceFile
  #22 = Utf8 DynamicLinkingTest.java
  #23 = NameAndType #12:#13 // "<init>":()V
  #24 = NameAndType #10:#11 // num:I
  #25 = Class #34 // java/lang/System
  #26 = NameAndType #35:#36 // out:Ljava/io/PrintStream;
  #27 = Utf8 method 1 executed
  #28 = Class #37 // java/io/PrintStream
  #29 = NameAndType #38:#39 // println:(Ljava/lang/String;)V
  #30 = NameAndType #20:#13 // methodTwo:()V
  #31 = Utf8 method 2 is executed
  #32 = Utf8 com/learning/stack/dynamic_linking/DynamicLinkingTest
  #33 = Utf8 java/lang/Object
  #34 = Utf8 java/lang/System
  #35 = Utf8 out
  #36 = Utf8 Ljava/io/PrintStream;
  #37 = Utf8 java/io/PrintStream
  #38 = Utf8 println
  #39 = Utf8 (Ljava/lang/String;)V
4.1 Method call
  • 1.methodOne method calls methodTwo method
9: invokevirtual #6 // Method methodTwo:()V
  • 2.invokevirtual calls virtual method
  • 3. #6 represents the Methodref method reference, pointing to #8.#30
 #6 = Methodref #8.#30 // com/learning/stack/dynamic_linking/DynamicLinkingTest.methodTwo:()V
  • 4. #8 means Class
 #8 = Class #32 // com/learning/stack/dynamic_linking/DynamicLinkingTest
  • 5. #32 means the class is com/learning/stack/dynamic_linking/DynamicLinkingTest
 #32 = Utf8 com/learning/stack/dynamic_linking/DynamicLinkingTest
  • 6. #30 indicates the name and return type
 #30 = NameAndType #20:#13 // methodTwo:()V
  • 7. #20 means the name is methodTwo
 #20 = Utf8 methodTwo
  • 8. #13 means the return type is void
 #13 = Utf8 ()V
4.3 Variables
  • 1. int num = 10;
#2 = Fieldref #8.#24 // com/learning/stack/dynamic_linking/DynamicLinkingTest.num:I
  • 2. Attribute reference, pointing to #8.#24
  • 3. #8 means Class
 #8 = Class #32 // com/learning/stack/dynamic_linking/DynamicLinkingTest
  • 4. #32 means the class is com/learning/stack/dynamic_linking/DynamicLinkingTest
 #32 = Utf8 com/learning/stack/dynamic_linking/DynamicLinkingTest
  • 5. #24 indicates name and type
 #24 = NameAndType #10:#11 // num:I
  • 6. #10 means the attribute name is num
 #10 = Utf8 num
  • 7. #11 indicates the type of attribute int
 #11 = Utf8 I
4.4 String
  • 1. #4 means string
 #4 = String #27 // Method 1 is executed
  • 2. #27 represents the Utf8 string “Method 1 is executed”
 #27 = Utf8 method 1 is executed
4.5 Parent Class Object
  • 1. #1 indicates method reference
 #1 = Methodref #9.#23 // java/lang/Object."<init>":()V
  • 2. #9 represents class
 #9 = Class #33 // java/lang/Object
  • 3. #33 means the class name is java/lang/Object
 #33 = Utf8 java/lang/Object
4.6 System class
  • 1. #3 represents attribute reference
 #3 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
  • 2. #25 represents class
 #25 = Class #34 // java/lang/System
  • 3. #34 indicates that the class name is java/lang/System
 #34 = Utf8 java/lang/System
  • 4. #26 indicates name and type
 #26 = NameAndType #35:#36 // out:Ljava/io/PrintStream;
  • 5. #35 means the name is out
 #35 = Utf8 out
  • 6. #36 indicates that the type is Ljava/io/PrintStream
 #36 = Utf8 Ljava/io/PrintStream;