JVM Series (1) - Variables

Keywords: Java Fragment jvm

Preface

I have been reading books about JVM for a long time. Although it is a bit difficult, I haven't given up so far. The purpose of this article is to serve as a small review of my study during this period. This chapter mainly analyses the data transfer relationship between local variable table and operand stack through several code fragments, focusing on iload, istore, iconst < n >, Iadd commands.

Early understanding

Several Concepts of JVM

  • Local Variable Table: Each stack frame contains a list of variables called the Local Variable Table.

  • Operator stack: Each stack frame contains an operand stack

  • iconst_0: Press the zero value of int into the operand stack

  • iload: Load a local variable onto the operand stack

  • istore: Store a value from the operand stack into a local variable table

  • iadd: Returns two elements at the top of the stack for addition, and adds the result stack

How to use dos to view bytecode files

First enter the directory of TestApp.class through dos, then run the command javap-c TestApp, you can see the compiled bytecode file.

Program Fragment I

  • Here is a java source code

public void test1(){      
    int c=0;              
}                         
  • Compiled bytecode

  public void test1();
    Code:
       0: iconst_0
       1: istore_1
       2: return
  • code analysis

Because test1() is an instance method, in the local variable table, the reference to the class where the index is 0 will be saved, so istore_1 will appear instead of istore_0.

Let's disassemble int c = 0, a constant 0, a variable C.

0: iconst_0   //Press constant 0 into the operand stack
1: istore_1   //Exit the stack, c=0
2: return      //Because it's void, there's no return value

Program Fragment II

  • The Java source code is as follows

public static void test2(){    
    int c=0;                   
}                              
  • Compiled bytecode file

  public static void test2();
    Code:
       0: iconst_0
       1: istore_0
       2: return
  • Analysis

Because test2() is a static method, no data is inserted into the local variable table. So what you see is istore_0 instead of istore_1 in program fragment 1. Other analyses are identical to program fragments

Program Fragment 3

  • java source code

public int test3(){         
    int c=0;                
    return c;               
}                           
  • Compiled bytecode

  public int test3();
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: ireturn
  • Analysis

0: iconst_0        //Press constant 0 stack
1: istore_1     //Exit the stack, and c=0
2: iload_1      //Press variable c onto the top of the stack
3: ireturn        //Returns the stack element

Program Fragment IV

  • Java source code

public int test4(int a,int b){       
    int c=0;                         
    return a+b;                      
}                                    
  • Compiled bytecode

  public int test4(int, int);
    Code:
       0: iconst_0
       1: istore_3
       2: iload_1
       3: iload_2
       4: iadd
       5: ireturn

* * analysis

Because test4(int a,int b) is an instance method, this is inserted where the local variable table index is 0.
Because test4(int a,int b) has two parameters, insert a at index 1 of local variables and B at index 2.

0: iconst_0        //Press constant 0 stack
1: istore_3        //Exit the stack, c=0, and store c at the location of index 3 in the local variable table
2: iload_1        //The variable stack indexed to 1 in the local variable table, i.e. a stack
3: iload_2        //The variable stack indexed to 2 in the local variable table, i.e. the b stack
4: iadd            //Two elements at the top of the stack are put out of the stack, added, and then the result is put into the stack (i.e. a,b out of the stack, a+b into the stack).
5: ireturn        //Returns the value of a+b

Program Fragment V

  • java source code

public int test5(int a,int b){    
    int c=0;                      
    c= a+b;                       
    return c;                     
}                                 
  • Compiled bytecode

  public int test5(int, int);
    Code:
       0: iconst_0
       1: istore_3
       2: iload_1
       3: iload_2
       4: iadd
       5: istore_3
       6: iload_3
       7: ireturn
  • Analysis

0: iconst_0        //Press constant 0 stack
1: istore_3        //Exit the stack, and c=0
2: iload_1        //Loading a variable stack with index 1 from a local variable table, i.e. a stack
3: iload_2        //Load the variable stack with index 2 from the local variable table, that is, the b stack
4: iadd            //Put the top two elements out of the stack, add them, and then press the results on the stack, and a+b on the stack.
5: istore_3        //Exit the top element of the stack and save it in the local variable table, i.e. c=a+b
6: iload_3        //Loading a variable stack with index 3 from a local variable table, i.e. c stack
7: ireturn        //Returns the top element of the stack, which returns c

Posted by Cypher87 on Thu, 18 Apr 2019 16:06:34 -0700