Java Virtual Machine

http://homepages.ius.edu/RWISMAN/C431/html/JVM.htm

Java Virtual Machine

powered by FreeFind

Modified: 10/19/2005 05:28:44

ONLINE RESOURCES

DOWNLOAD RESOURCES

OVERVIEW

The Java Virtual Machine (JVM) is an abstract computing machine based on a stack-oriented architecture with method access to a single, current frame.

Operands are on the stack, for example: 27 - 4

    


   Stack    
 
  
TOS 
21
34
15
  bipush 27
  bipush 4

    Stack  
 
  
  
  
TOS 
21
34
15
27
4
  isub


   Stack   
 
 
TOS
21
34
15
23

The following set of examples illustrate the organization of the JVM and the form code generation must produce in order to be compatible for execution. The approach will be to present Java source code and the corresponding assembly code for the JVM.

Two tools are very useful for our task of generating correct assembly code:

  • jasmin - an assembler that produces a class file for the JVM
  • jasper - which translates class file format byte-codes into an assembler file compatible with jasmin.

The JVM implements a pure stack-based architecture - generally there are no accessible registers, only access to the stack and the frame of the currently executing method.

It is important to recognize that stack and frame frame operations are independent; stack operations do not affect the frame except when moving data between the two.

  • Stack is accessed by the usual push and pop operations which only operate at the top of stack (TOS).
  • Frame is accessed by offsets from the start of the frame which allows random access.
      Frame  
this   0
 1
 2
 3
 4
21
34
15
-4
27
   Stack   
 
 
 
TOS
21
34
15
-4
27
Memory   
19
20
21
22
23
45
98
105
62
111

Stack and frame operations include:

  • bipush 100 - push byte 100 as integer to TOS.
  • iconst_5  - essentially the same as bipush 5, only defined for integers -1..5.
  • iload 1 - push the value from frame index 1 to TOS
  • istore 2 - pop the value from TOS to frame index 2.
  • aload 0 - 'this' object is at 0 of the current method frame and must be a reference. Push from frame index 0 to TOS.
  • astore 0 - pop from TOS to frame index 0.
  • dup - duplicates the TOS by pushing TOS.

Exercise - What is the result of each of the above on the Stack?

Exercise - What is the significance of this in object-oriented programming?

 

STACKS and FRAMES

A key insight is that the stack of the caller is the frame of thecallee (at least the parameter portion) when a method is called.

Consider the following example in Java and the corresponding JVM assembly:

CallerCallee
new B().m(11, 12);class B {
   void m(int a, int b) {}
                                     CallerStack
new B                             
bipush            11
bipush            12

invokevirtual   B/m(II)V
reference to B object
11
12
                                 Callee Frame
this                            
a            11
b            12
 
0reference to B object
111
212

Another example illustrates the interaction between methods using the stack to return results. The callee always leaves a method result at the TOS for the caller.

CallerCallee
int x;
x = new B().m(11, 12);
class B {
   int m(int a, int b) { return 13;}
                                     CallerStackbefore call
new B                             
bipush            11
bipush            12

invokevirtual   B/m(II)I
reference to B object
11
12

Caller Stack after call

 
13
TOS
                     Callee Frame
this
a            11    
b            12
0reference to B object
111
212
 
                    Callee Stack
.method public m(II)I    
   bipush   13   
   ireturn
.end method 
 
13

TOS

Exercise - What does the following mean?

  1. .method public m(II)I
  2. invokevirtual   C/m(II)I
  3. bipush   13
  4. ireturn

Example - Accessing a parameter.

  • Each class generates a separate class file when compiled.
  • Java classes ex1 and ex2 were in one file, ex1.java, which was compiled then disassembled using jasper.

Frame and parameter passing: note that method stack operations do not affect frame references of that method.

  • new ex2 - Line 16 of ex1 allocates new ex2 object and places reference on top of stack (TOS).
  • dup - duplicates the ex2 object on TOS.
  • invokespecial ex2/<init>()V - calls ex2 constructor, note that line 8
  • bipush 100 - pushes integer 100 to TOS.
  • invokevirtual ex2/f2(I)V - calls ex2 method f2,this is at frame offset 0, parameter n at offset 1, and local i at offset 2.
  • iload_1 - copies frame offset 1 (i.e. n) to TOS
  • istore_2 - copies TOS to local i at frame offset 2.
  1. public class ex1 {
  2.     public static void main(String [] args) {
  3.        new ex2().f2( 100 );
  4.     }
  5. }
  1. class ex2 {
  2.    public void f2( int n ) {           
  3.        int i;
  4.        i = n;
  5.    }
  6. }
Frame
0this = ex2
1n = 100
2i
  1. .source ex1.java
  2. .class public ex1
  3. .super java/lang/Object
     
  4. .method public <init>()V
  5. .limit stack 1
  6. .limit locals 1
  7. .line 1
  8.    aload_0                                ; 'this'
  9.    invokespecial java/lang/Object/<init>()V 
  10.    return
  11.  .end method
     
  12.  .method public static main([Ljava/lang/String;)V
  13.  .limit stack 2
  14.  .limit locals 1                            ;    STACK
  15.  .line 3                                     ;  ____________
  16.     new ex2                               ; | ex2 reference |
  17.     dup                                     ; | ex2 reference |
  18.     invokespecial ex2/<init>()V    ;  ____________
  19.     bipush  100                        ; | ex2 reference |
  20.     invokevirtual ex2/f2(I)V         ;| 100               |
     .line 4
  21.     return                                
  22.  .end method
  1.  .source ex1.java
  2.  .class ex2
  3.  .super java/lang/Object
     
  4.  .method <init>()V
  5.  .limit stack 1
  6.  .limit locals 1
  7.  .line 7
  8.    aload_0
  9.    invokespecial java/lang/Object/<init>()V
  10.    return
  11.  .end method
     
  12.  .method public f2(I)V
  13.  .limit stack 1
  14.  .limit locals 3
  15.  .line 9
  16.    iload_1                       ; n -> TOS
  17.    istore_2                      ; TOS -> i
  18.  .line 10
  19.    return
  20.  .end method
 

Exercise - What does the following mean?

  1. dup
  2. .limit stack 2
  3. istore 2
  4. iload 1

Exercise - Explain in .method public f2(I)V

  1. .limit stack 1
  2. .limit locals 3

 

Instance variables - instance variables (fields) are not part of the frame or stack but are accessed by this and field name.

Example - Returning a result and instance variables (fields).

  • The field j is defined in line 29.

    j = m;

    is equivalent to lines 45-47:

  1.        aload_0                               ; this
  2.        iload_2                                ; m
  3.        putfield              ex2/j I   ; j = m

Recall that this is at frame offset 0, and m at offset 2.

  • return j;

    is equivalent to lines 56-58:
     

    1.        aload_0              
    2.        getfield              ex2/j I
    3.        ireturn           
         

Where the return value is at TOS.

  1. public class ex1 {
  2.    public static void main(String [] args) {
  3.      new ex2().f2( 100, 200 );
  4.    }
  5. }
  1. class ex2 {
  2.    int j;
     
  3.    public int f2( int n, int m ) {      
  4.      int i = n;
  5.      j = m;
  6.      j = j + i;
  7.      return j;
  8.   }
  9. }
Frame
0this
1n
2m
3i

 

  1. .source               ex1.java
  2. .class                  public ex1
  3. .super                 java/lang/Object
     
  1. .method               public <init>()V
  2. .limit stack       1
  3. .limit locals       1
  4. .line                 1
  5.     aload_0               
  6.     invokespecial     java/lang/Object/<init>()V
  7.     return               
  8.  .end method             
     
  1.  .method               public static main([Ljava/lang/String;)V
  2.  .limit stack      3
  3.  .limit locals      1
  4.  .line               3
  5.     new                  ex2
  6.     dup                  
  7.     invokespecial      ex2/<init>()V
  8.     bipush                100
  9.     sipush                200
  10.     invokevirtual       ex2/f2(II)I
  11.     pop                  
  12.  .line                 4
  13.     return               
  14.  .end method             

 

  1.  .source                ex1.java
  2.  .class                   ex2
  3.  .super                  java/lang/Object
     
  4.  .field                    j I
     
  5.  .method               public <init>()V
  6.  .limit stack           1
  7.  .limit locals           1
  8.  .line                    10
  9.      aload_0              
  10.      invokespecial    java/lang/Object/<init>()V
  11.      return               
  12.  .end method
     
  13.  .method                public f2(II)I
  14.  .limit stack            3
  15.  .limit locals            4
  16.  .line                     13
  17.      iload_1              
  18.      istore_3             
  19.  .line                     14
  20.      aload_0              
  21.      iload_2              
  22.      putfield           ex2/j I
  23.  .line                     15
  24.      aload_0              
  25.      aload_0              
  26.      getfield             ex2/j I
  27.      iload_3              
  28.      iadd                 
  29.      putfield             ex2/j I
  30.  .line                      16
  31.      aload_0              
  32.      getfield              ex2/j I
  33.      ireturn              
  34.  .end method             

 

Exercise

  1. Explain lines 49-54. Specifically, where and what are the operands of line 53? Draw the stack to illustrate your answer.
  2. What is the effect of line 54?
  3. Explain lines 56-57.
  4. Why is local limit 4?
  5. Why is the stack limit 3?

 

FRAMES IN THE MiniJava COMPILER

The text presents a model for frames closely connected with later subjects of optimization and code production for an intermediate language.

We will first consider frame, class and method generation for the JVM architecture only; allowing us to examine many of the remaining steps in compilation in a simpler context before tackling the more general approach of the text.

Recall the SymbolTable package implementation which holds declaration information such as the name and type of variables.

Frames are only associated with methods and constructors, since MiniJava has only the default constructor, we are concerned only with frames for methods. The following classifies each declaration part of a MiniJava program as it relates to frames.

Class - Class data (i.e. name, parent, methods, globals) is recorded in the SymbolTable but is not part of the frame.

Class generation is straightforward consisting of:

  • Lines 26-28
    • optional source file name
    • required class name
    • parent class
  • Line 29
    • field names and type
  • Line 30
    • ex2 class constructor with 0 parameters.
  • Line 31
    • stack limit of 1 for one parameter in call to parent constructor at Line 34-35.
  • Line 32
    • number of parameters + locals + this - the frame size.
  1.  .source                ex1.java
  2.  .class                   ex2
  3.  .super                  java/lang/Object
     
  4.  .field                    j I
     
  5.  .method               public <init>()V
  6.  .limit stack           1
  7.  .limit locals           1
  8.  .line                    10
  9.      aload_0              
  10.      invokespecial    java/lang/Object/<init>()V
  11.      return               
  12.  .end method
class ex2 {

   int j;

Globals

Globals name (fields) and type are recorded in the SymbolTable but are not part of the frame. The example above illustrates how globals must be defined as part of the class.

Methods

  1.    public int f2( int n, int m ) {      
  2.      int i = n;
  3.      j = m;
  4.      j = j + i;
  5.      return j;
  6.   }
  7. }
  1.  .method                public f2(II)I
  2.  .limit stack            3
  3.  .limit locals            4
  4.  .line                     13
  5.      iload_1              
  6.      istore_3             
  7.  .line                     14
  8.      aload_0              
  9.      iload_2              
  10.      putfield           ex2/j I
  11.  .line                     15
  12.      aload_0              
  13.      aload_0              
  14.      getfield             ex2/j I
  15.      iload_3              
  16.      iadd                 
  17.      putfield             ex2/j I
  18.  .line                      16
  19.      aload_0              
  20.      getfield              ex2/j I
  21.      ireturn              
  22.  .end method             
Frame
0this
1n
2m
3i

 

A frame must be determined for each method, the essential data for compiling a method is:

  • method name
  • method type - for the return, MiniJava has void (return) or int (ireturn).
  • parameters - this is first, the order in the parameter determines frame location of others.
  • variables - assigned to a unique frame location following parameters.
  • .limit stack - maximum stack space required due to pushes, parameters, etc.; often the maximum number of parameters passed to any callee. It is safer to specify more than needed, specifying to little results in a runtime exception.
  • .limit locals - this + number of parameters + variables. As with the stack limit, it is allowed to set the limit above that needed, setting too low results in a runtime exception.

this

this is allocated first in the frame at location 0. Before an instance method call such as at Line 28, requires placing this at location 0 of the callee frame. The following constructs the frame for the putfield method that appears as:

  1.      aload_0               
  2.      bipush              12             
  3.      putfield           ex2/j I
Frame
0this
1 12

Parameters/Variables

Parameters are allocated in the frame immediately after this. Variables immediately follow parameters.

Caller - constructs the frame by placing the object passed to this followed by parameters on the stack; then invoke the method. Notice that values are represented as bytes, etc. and extended when copied to the frame where possible:

bipush 100 has the following description:

  • The immediate byte is sign-extended to an intvalue. That value is pushed onto the operand stack.

sipush 200

  • The immediate unsigned byte1 and byte2 values are assembled into an intermediate short where the value of the short is ( byte1 << 8) | byte2. The intermediate value is then sign-extended to an int value. That value is pushed onto the operand stack.
new ex2().f2( 100, 200 );
  1.     new                   ex2
  2.     bipush                100
  3.     sipush                200
  4.     invokevirtual       ex2/f2(II)I

Callee - defines the frame size by limit described above. Parameters and variable values are accessed from the frame.

  • frame - object references can be loaded from the frame to the TOS by the aload operation, integers by the iload. Stored from the TOS to the frame by the istore operation, etc.
  • stack - the stack is not required to be balanced at the method return, the return address seems to be part of the JVM architecture.
  • return - typed method returns (e.g. an int method) return the TOS.
  1.    public int f2( int n, int m ) {      
  2.      int i = n;
  3.      j = m;
  4.      return i + j;

 

  1.  .method                public f2(II)I
  2.  .limit stack            3
  3.  .limit locals            4
  4.  .line                     13
  5.      iload_1              
  6.      istore_3             
  7.  .line                     14
  8.      aload_0              
  9.      iload_2              
  10.      putfield           ex2/j I
  11.  .line                     15
  12.      aload_0              
  13.      getfield             ex2/j I
  14.      iload_3                   ; i
  15.      iadd                       ; TOS = i + j
  16.      ireturn                   ; returns TOS

DOWNLOAD RESOURCES

  • JVM Specifications - Download site

     

  • jasper - Download site

    jasper is a disassembler of Java class files to an assembly program. Basically the steps are:
     

    1. Compile the Java program file.
    2. Disassemble a class file to assembly, using example.class
      • Home: java -jar \jasper\jasper.jar example.class
         
      • IUS:    java -jar v:\common\user\C431\jasper.jar example.class

       

  • jasmin - Download site

    jasmin assembles a JVM assembly file into a class file that can be executed by the JVM.

  1. Compile the Java program file  to assembly language.
  2. Assemble a class file to assembly, using example.j
     
    • Home: java -jar \jasmin\jasmin.jar example.j
                java -cp . example.class
       
    • IUS:    java -jar v:\common\user\C431\jasmin.jar example.j
                java -cp . example.class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值