Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

9 JVM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

JVM and Jasmin

Dr. Nguyen Hua Phung


Faculty of CSE
HCMUT
Outline
• Our compiler
• Java Virtual Machine
– Data types
– Operand stack
– Local variable array
– Instructions

CSE - HCMUT JVM and Jasmin 2


Our Compiler
source code
Scanner Ö
Sequence of tokens
Parser Ö
AST
Semantic analyzer Ö
Symbol
table AST
Code generation
Jasmin code (*.j)
Java Bytecode Assembler
Java Byte code (*.class)
Java Virtual Machine

CSE - HCMUT JVM and Jasmin 3


Java Programming Environment

From [1]

CSE - HCMUT JVM and Jasmin 4


source programs Java programs

Our Compiler

Java Compiler
Java Byte Code
Assembler

Java virtual machine

CSE - HCMUT JVM and Jasmin 5


Why Jasmin ?
• Jasmin is a Java assembler
– adopts a one-to-one mapping
– operation codes are represented by mnemonic
– Example:
.line 4
public class VD { iconst_0
public void main(String[] args) { istore_2
.line 5
int a,b; iload_2
b = 0; iconst_2
imul
a = b * 2 + 40; bipush 40
} iadd
istore_1
}
CSE - HCMUT JVM and Jasmin 6
Java Byte Code
ca fe ba be 00 00 00 31 00 1b 0a 00 05 00 0e 09 00 0f 00 10 0a 00 11 00 12 07 00 13 07 00 14 01
00 06 3c 69 6e 69 74 3e 01 00 03 28 29 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e 75 6d 62
65 72 54 61 62 6c 65 01 00 04 6d 61 69 6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74
72 69 6e 67 3b 29 56 01 00 0a 53 6f 75 72 63 65 46 69 6c 65 01 00 07 56 44 2e 6a 61 76 61 0c 00
06 00 07 07 00 15 0c 00 16 00 17 07 00 18 0c 00 19 00 1a 01 00 02 56 44 01 00 10 6a 61 76 61 2f
6c 61 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00
03 6f 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a
61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 01 00 07 70 72 69 6e 74 6c 6e 01 00 04 28
49 29 56 00 21 00 04 00 05 00 00 00 00 00 02 00 01 00 06 00 07 00 01 00 08 00 00 00 1d 00 01 00
01 00 00 00 05 2a b7 00 01 b1 00 00 00 01 00 09 00 00 00 06 00 01 00 00 00 01 00 09 00 0a 00 0b
00 01 00 08 00 00 00 35 00 02 00 03 00 00 00 11 03 3d 1c 05 68 10 28 60 3c b2 00 02 1b b6 00 03
b1 00 00 00 01 00 09 00 00 00 12 00 04 00 00 00 04 00 02 00 05 00 09 00 06 00 10 00 07 00 01 00
0c 00 00 00 02 00 0d

CSE - HCMUT JVM and Jasmin 7


Outline
• Our compiler
• Java Virtual Machine
– Data types
– Operand stack
– Local variable array
– Instructions

CSE - HCMUT JVM and Jasmin 8


Java Virtual Machine
BKIT programs Java programs

compiling compiling

Java bytecode
running running

Java Virtual Java Virtual Java Virtual


Machine for Machine for Machine for
Linux Windows your television
Linux Windows Your
Television

CSE - HCMUT JVM and Jasmin 9


JVM = stack-based machine
• A stack for each method
• The stack is used to store operands and results of an
expression.
• It is also used to pass argument and receive returned
value.
• Code generation for a stack-based machine is easier
than that for a register-based one.

CSE - HCMUT JVM and Jasmin 10


Internal Architecture of JVM

From [1]

CSE - HCMUT JVM and Jasmin 11


Method Area and Heap

From [1]

CSE - HCMUT JVM and Jasmin 12


Java Stacks

From [1]

CSE - HCMUT JVM and Jasmin 13


Outline
• Our compiler
• Java Virtual Machine
– Data types
– Operand stack
– Local variable array
– Instructions

CSE - HCMUT JVM and Jasmin 14


Data Types
Type Range Description
boolean {0,1} Z
byte -27 to 27 - 1, inclusive B
short -215 to 215 – 1, inclusive S
int -231 to 231 – 1, inclusive I
long -263 to 263 – 1, inclusive L
char 16 bit usigned Unicode (0 to 216 -1) C
float 32-bit IEEE 754 single-precision float F
double 64-bit IEEE 754 double-precision float D
returnAddress address of an opcode within the same method
class reference Lclass-name;
interface reference Linter-name;
array reference [[..[component-
type
void V
CSE - HCMUT JVM and Jasmin 15
Example
Java language type JVM description
Object Ljava/lang/Object;
String Ljava/lang/String;
String [] [Ljava/lang/String;
int [] [I
float [] [] [[F
void main(String [] args) ([Ljava/lang/String;)V
int gcd(int a,int b) (II)I
char foo(float a,Object b) (FLjava/lang/Object;)C

CSE - HCMUT JVM and Jasmin 16


Example (cont’d)
public class GetType {
public static void main(String [] args) {
Object a = new Object();
int [] b = new int[10];
float[][] c = new float[2][3];
String d = “csds”;
System.out.println(“The class name of a is ”+ a.getClass());
System.out.println(“The class name of b is ” + b.getClass());
System.out.println(“The class name of c is ” + c.getClass());
System.out.println(“The class name of d is ” + d.getClass());
}
}

CSE - HCMUT JVM and Jasmin 17


Example (cont’d)
• boolean, byte, char and short are implemented as int
public class IntTypes { .method public static
public static void main([Ljava/lang/String;)V
...
main(String argv[]) { .line 3
boolean z = true; iconst_1
istore_1
byte b = 1; .line 4
short s = 2; iconst_1
char c = 'a'; istore_2
.line 5
} iconst_2
} istore_3
.line 6
bipush 97
istore_4
Label0:
.line 8
return
.end method

CSE - HCMUT JVM and Jasmin 18


Outline
• Our compiler
• Java Virtual Machine
– Data types
– Operand stack
– Local variable array
– Instructions

CSE - HCMUT JVM and Jasmin 19


Operand Stack
• Accessed by pushing and popping values
– storing operands and receiving the operations’ results
– passing arguments and receiving method results
• Integral expression:
a = b * 2 + 40;
• Jasmin code
iload_2 // load variable 2 onto op stack
iconst_2 // push constant 2 onto op stack
imul // pop 2 values on top of stack, multiple them and push the result
onto stack
bipush 4. // push 40 onto stack
iadd // pop 2 values on top of stack, calculate and push the result onto
stack
istore_1 // pop the value on top of stack and assign it to variable 1
CSE - HCMUT JVM and Jasmin 20
Outline
• Our compiler
• Java Virtual Machine
– Data types
– Operand stack
– Local variable array
– Instructions

CSE - HCMUT JVM and Jasmin 21


Local Variable Array
1. A new local variable array is created each time a
method is called
2. Local variables addressed by indexing, starting from 0
3. Instance methods:
– slot 0 given to this
– Parameters (if any) given consecutive indices, starting from 1
– The indices allocated to the other variables in any order
4. Class methods:
– Parameters (if any) given consecutive indices, starting from 0
– The indices allocated to the other variables in any order
5. One slot can hold a value of boolean, byte, char, short,
int, float,reference and returnAdrress
6. One pair of slots can hold a value of long and double
From [2]

CSE - HCMUT JVM and Jasmin 22


Example 1
public static void foo() { .line 7
int a,b,c; iconst_1
istore_0 // a
a = 1; .line 8
b = 2; iconst_2
istore_1 // b
c = (a + b) * 3;
.line 9
} iload_0
iload_1
iadd
iconst_3
imul
istore_2 // c

CSE - HCMUT JVM and Jasmin 23


Example 2
public void foo() {
int a,b,c; .var 0 is this LVD2; from Label0 to Label1
.line 7
a = 1; iconst_1
b = 2; istore_1 // a
.line 8
c = (a + b) * 3;
iconst_2
} istore_2 // b
.line 9
iload_1
iload_2
iadd
iconst_3
imul
istore_3 // c

CSE - HCMUT JVM and Jasmin 24


Example 3
.line 6
iconst_1
public void foo() { istore_1 // a
int a = 1; .line 7
long b = 2; ldc2_w 2
lstore_2 // 2,3 for b
int c = 3; .line 8
long d = (a + b) * c; iconst_3
istore 4 // c
}
.line 9
iload_1
i2l // conversion
lload_2
ladd
iload 4
i2l // conversion
lmul
lstore 5 // 5,6 for d
CSE - HCMUT JVM and Jasmin 25
Outline
• Our compiler
• Java Virtual Machine
– Data types
– Operand stack
– Local variable array
– Instructions

CSE - HCMUT JVM and Jasmin 26


Jasmin Instructions
1. Arithmetic Instructions
2. Load and store instructions
3. Control transfer instructions
4. Type conversion instructions
5. Operand stack management instructions
6. Object creation and manipulation
7. Method invocation instructions
8. Throwing instructions (not used)
9. Implementing finally (not used)
10. Synchronisation (not used)

CSE - HCMUT JVM and Jasmin 27


Arithmetic Instructions
• Add: iadd, ladd, fadd, dadd.
• Subtract: isub, lsub, fsub, dsub.
• Multiply: imul, lmul, fmul, dmul.
• Divide: idiv, ldiv, fdiv, ddiv.
• Remainder: irem, lrem, frem, drem.
• Negate: ineg, lneg, fneg, dneg.
• Shift: ishl, ishr, iushr, lshl, lshr, lushr.
• Bitwise OR: ior, lor.
• Bitwise AND: iand, land.
• Bitwise exclusive OR: ixor, lxor.
• Local variable increment: iinc.
• Comparison: dcmpg, dcmpl, fcmpg, fcmpl, lcmp.
From ($3.11.3,[3])
CSE - HCMUT JVM and Jasmin 28
Load and Store
• Load a local variable onto the operand stack:
iload, iload_<n>, Þ n:0..3, used for int, boolean, byte, char or short
lload, lload_<n>, Þ n:0..3, used for long
fload, fload_<n>, Þ n:0..3, used for float
dload, dload_<n>, Þ n:0..3, used for double
aload, aload_<n>, Þ n:0..3, used for a reference
Taload. Þ T:b,s,i,l,f,d,c,a

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


istore, istore_<n>, n:0..3, used for int, boolean, byte, char or short
lstore, lstore_<n>, n:0..3, used for long
fstore, fstore_<n>, n:0..3, used for float
dstore, dstore_<n>, n:0..3, used for double
astore, astore_<n>, n:0..3, used for a reference and returnAddress
Tastore. Þ T:b,s,i,l,f,d,c,a From ($11.3.2,[3])

CSE - HCMUT JVM and Jasmin 29


Load and Store (cont’d)
• Load a constant onto the operand stack:
bipush, Þ for an integer constant from -27 to 27 - 1
sipush, Þ for an integer constant from -215 to 215 - 1
ldc, Þ for a constant that is an integer, float or a quoted string
ldc_w,
ldc2_w, Þ for a constant that is a long or a double
aconst_null, Þ for a null
iconst_m1, Þ for -1
iconst_<i>, Þ for 0,…,5
lconst_<l>, Þ for 0,1
fconst_<f>, Þ for 0.0,1.0 and 2.0
dconst_<d>. Þ for 0.0,1.0

From ($11.3.2,[3])

CSE - HCMUT JVM and Jasmin 30


Example 4
.line 6 .line 10
int a = 1 ; iconst_1 iload_1
istore_1 iload_2
int b = 100; .line 7 imul
int c = 1000; bipush 100 iload_3
istore_2 iadd
int d = 40000; .line 8 iload 4
int e = a * b + c – d; sipush 1000 isub
istore_3 istore 5
.line 9
ldc 40000
istore 4

CSE - HCMUT JVM and Jasmin 31


Example 5
float a = 1.0F ; .line 6 .line 10
fconst_1 fload_1
float b = 2.0F; fstore_1 fload_2
.line 7 fmul
float c = 3.0F; fconst_2 fload_3
float d = 4.0F; fstore_2 fadd
.line 8 fload 4
float e = a * b + c - d; ldc 3.0 fsub
fstore_3 fstore 5
.line 9
ldc 4.0
fstore 4

CSE - HCMUT JVM and Jasmin 32


Example 6
a[0] = 100; .line 8
aload_0 // push address of array referred by a
b = a[1]; iconst_0 // push 0
bipush 100 // push 100
iastore // a[0] = 100

.line 9
aload_0 // push address of array referred by a
iconst_1 // push 1
iaload // pop a and 1, push a[1]
istore_1 // store to b

CSE - HCMUT JVM and Jasmin 33


Control Transfer Instructions
• Unconditional branch:
goto, goto_w, jsr, jsr_w, ret.
• Conditional branch:
ifeq, iflt, ifle, ifne, ifgt, ifge, Þ compare an integer to zero
ifnull, ifnonnull, Þ compare a reference to null
if_icmpeq, if_icmpne, if_icmplt, if_icmpgt, if_icmple,
if_icmpge, Þ compare two integers
if_acmpeq, if_acmpne. Þ compare two references
• Compound conditional branch:
tableswitch, lookupswitch.
From ($3.11.7,[3])

CSE - HCMUT JVM and Jasmin 34


Example 7
int a,b,c; .line 7
iload_0 // push a
if (a > b) iload_1 // push b
if_icmple Label0
c = 1; .line 8
else iconst_1
istore_2 // c = 1
c = 2; goto Label1
Label0:
.line 10
iconst_2
istore_2 // c = 2
Label1:

CSE - HCMUT JVM and Jasmin 35


Example 8
float a,b; int c; .line 7
fload_0 // push a
if (a > b) fload_1 // push b
fcmpl // pop a,b, push 1 if a > b, 0 otherwise
c = 1; ifle Label0 // goto Label0 if top <= 0
else .line 8
iconst_1
c = 2; istore_2
goto Label1
Label0:
.line 10
iconst_2
istore_2
Label1:

CSE - HCMUT JVM and Jasmin 36


fcmpg and fcmpl

1 if value 1 > value 2


value 2
value 1 if value 1 == value 2
0

If either is NaN, fcmpg pushes 1


and fcmpl pushes -1
-1 if value 1 < value 2

CSE - HCMUT JVM and Jasmin 37


Type Conversion Instructions
• i2l, i2f, i2d, l2f, l2d, and f2d.
• Only i2f is used in MP compiler

.line 6
iconst_1
int b = 1; istore_0
.line 7
float a = b; iload_0
i2f
fstore_1

CSE - HCMUT JVM and Jasmin 38


Operand Stack Management
Instructions
• dup duplicate the stack top operand
• pop remove the stack top operand

value
value value value
… … … …

dup pop
used when translating a = b = … used when translating 1;

• others: pop2, dup2, swap,…

CSE - HCMUT JVM and Jasmin 39


Example 10
.line 7
int a,b,c; iconst_1
dup
a = b = c = 1; istore_2
dup
istore_1
istore_0

int a,b,c; .line 7


iconst_1
1 + (a = 2); iconst_2
dup
istore_0
In MC, not in Java iadd
pop

CSE - HCMUT JVM and Jasmin 40


Object Creation and Manipulation
• Create a new class instance: new.
• Create a new array: newarray, anewarray,
multianewarray.
• Access fields of classes (static fields, known as class
variables) and fields of class instances (non-static fields,
known as instance variables): getfield, putfield, getstatic,
putstatic.
• Load an array component onto the operand stack:
baload, caload, saload, iaload, laload, faload, daload,
aaload.
• Store a value from the operand stack as an array
component: bastore, castore, sastore, iastore, lastore,
fastore, dastore, aastore.
• …
CSE - HCMUT JVM and Jasmin 41
Example 11
BKOOL: .line 6
bipush 10
a:integer[10]; newarray int
astore_0
a[0] = a[1] + 2; .line 7
aload_0
iconst_0
aload_0
iconst_1
iaload
Java: iconst_2
int a[] = new int [10]; iadd
iastore
a[0] = a[1] + 2;

CSE - HCMUT JVM and Jasmin 42


Field Instructions
• getstatic
• pustatic
<field_spec> <descriptor>
• getfield
• putfield
• E.g.
getstatic java.lang.System.out Ljava/io/PrintStream;
class name field type
field name

CSE - HCMUT JVM and Jasmin 43


Example 12
new VD12
public class VD12 { dup
static int a; invokespecial VD12/<init>()V
int b; astore_1
iconst_1
static VD12 c;
putstatic VD12.a I
VD12 d; aload_1
public static void getstatic VD12.a I
main(String[] arg) { iconst_1
VD12 e; iadd
e = new VD12(); putfield VD12.b I
aload_1
a = 1;
new VD12
e.b = a + 1; dup
e.d = new VD12(); invokespecial VD12/<init>()V
c = e.d; putfield VD12.d LVD12;
} aload_1
} getfield VD12.d LVD12;
putstatic VD12.c LVD12;

CSE - HCMUT JVM and Jasmin 44


Method Invocation Instructions
• invokestatic
• invokevirtual <method-spec>
• invokespecial
– the constructor method <init>
– a private method
– a method in a super class
• invokeinterface <method-spec> <num-args>
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

class name method name type desc

CSE - HCMUT JVM and Jasmin 45


Method Invocation Instructions
(cont’d)
arg n arg n
… …
arg 2 arg 1
arg 1 result (if any) objref result (if any)
… … … …

invokestatic invokevirtual/invokespecial

• invokevirtual: based on the real type of objref


• invokestatic: based on the static class

CSE - HCMUT JVM and Jasmin 46


Example 13
public class VD13 {
public static void main(String[] arg) {
goo(new VD13());
}
float foo(int a, float b) {
return a + b;
}
static void goo(VD13 x){
x.foo(1,2.3F);
}
}
CSE - HCMUT JVM and Jasmin 47
Example 13 (cont’d)
public static void main(String[] arg) {
goo(new VD13());
} .method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
.var 0 is arg0 [Ljava/lang/String; from Label0 to Label1

.line 3
new VD13
dup
invokespecial VD13/<init>()V
invokestatic VD13/goo(LVD13;)V
objref
.line 4
objref return

.end method

CSE - HCMUT JVM and Jasmin 48


Example 13 (cont’d)
.method static goo(LVD13;)V
static void goo(VD13 x) { .limit stack 3
x.foo(1,2.3F); .limit locals 1
} .var 0 is arg0 LVD13; from Label0 to Label1

.line 9
aload_0
2.3 iconst_1
ldc 2.3
1 invokevirtual VD13/foo(IF)F
objref pop
3.3
Label1:
.line 10
return

.end method

CSE - HCMUT JVM and Jasmin 49


Method Return
• All methods in Java are terminated by a return instruction
– return void
– ireturn int,short,char,boolean, byte
– freturn float
– lreturn long
– dreturn double
– areturn reference

CSE - HCMUT JVM and Jasmin 50


Example 13 (cont’d)
float foo(int a, float b) { .method foo(IF)F
.limit stack 2
return a + b; .limit locals 3
.var 0 is this LVD13; from Label0 to Label1
} .var 1 is arg0 I from Label0 to Label1
.var 2 is arg1 F from Label0 to Label1

Label0:
iload_1
i2f
fload_2
fadd
b Label1:
ab freturn
a+
.end method

CSE - HCMUT JVM and Jasmin 51


Jasmin Directives
Ø .source <source.java>
Ø .class <the current class>
Ø .super <the super class>
Ø .limit
Ø .method <the method description>
Ø .field <the field description>
Ø .end
Ø .var <the variable description>
Ø .line <the line number in source code>

CSE - HCMUT JVM and Jasmin 52


Example 14
public class VD14 { .source VD14.java
int a; .class public VD14
static int b; .super java/lang/Object

.field a I
public static void .field static b I
main(String[] arg) {
(new
VD14()).foo(1,2.3F);
}

float foo(int a, float b) {


return a * b;
}

CSE - HCMUT JVM and Jasmin 53


Example 14 (cont’d)
public class VD14 { .method public <init>()V
int a; .limit stack 1
static int b; .limit locals 1
.var 0 is this LVD14; from Label0 to Label1
public static void Label0:
main(String[] arg) { .line 1
(new aload_0
VD14()).foo(1,2.3F); invokespecial java/lang/Object/<init>()V
} Label1:
return
float foo(int a, float b) {
.end method
return a * b;
}

CSE - HCMUT JVM and Jasmin 54


Example 14 (cont’d)
public class VD14 { .method public static main([Ljava/lang/String;)V
int a; .limit stack 3
.limit locals 1
static int b; .var 0 is arg0 [Ljava/lang/String; from Label0 to
Label1
public static void
Label0:
main(String[] arg) { .line 5
(new new VD14
VD14()).foo(1,2.3F); dup
} invokespecial VD14/<init>()V
iconst_1
ldc 2.3
float foo(int a, float b) { invokevirtual VD14/foo(IF)F
return a * b; pop
Label1:
} .line 6
return
}
.end method

CSE - HCMUT JVM and Jasmin 55


Example 14 (cont’d)
public class VD14 { .method foo(IF)F
int a; .limit stack 2
static int b; .limit locals 3
.var 0 is this LVD14; from Label0 to Label1
.var 1 is arg0 I from Label0 to Label1
public static void .var 2 is arg1 F from Label0 to Label1
main(String[] arg) {
(new Label0:
VD14()).foo(1,2.3F); .line 8
} iload_1
i2f
float foo(int a, float b) { fload_2
fmul
return a * b;
Label1:
} freturn

} .end method

CSE - HCMUT JVM and Jasmin 56


References
[1] Bill Venner, Inside the Java Virtual Machine,
http://www.artima.com/insidejvm/ed2/
[2] J.Xue, Prog. Lang. and Compiler, http://www.cse.unsw.edu.au/~cs3131
[3] Java Virtual Machine Specification, http://java.sun.com/docs/books/vmspec/
[4] Jasmin Home Page, http://jasmin.sourceforge.net/

CSE - HCMUT JVM and Jasmin 57

You might also like