Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Virtual Machine: Instruction Set Cheng-Chia Chen.

Similar presentations


Presentation on theme: "1 Java Virtual Machine: Instruction Set Cheng-Chia Chen."— Presentation transcript:

1 1 Java Virtual Machine: Instruction Set Cheng-Chia Chen

2 2 2 Java Program class SumI { public static void main (String[] args) { int count=10; int sum =0; for (int index=1;index<count;index++) sum=sum+index; } // method main } // class SumI

3 3 3 Java ByteCode Method void main(java.lang.String[]) 0 bipush 10// byte push 10 to stack (0x10) 2 istore_1// load 10 (top of stack) to count 3 iconst_0// push 0 to stack 4 istore_2// load 0 (top of stack to sum 5 iconst_1// push 1 to stack 6 istore_3// load 1 (top of stack) to index 7 goto 17// go to 17

4 4 3 Java ByteCode 10 iload_2// load sum to stack 11 iload_3// load index to stack 12 iadd// add 13 istore_2// store “top of stack” to sum 14 iinc 3 1// index ++ 17 iload_3// load index to stack 18 iload_1// load count to stack 19 if_icmplt 10// if index < count goto 10 22 return

5 5 import java.io.*; class ReadFile { public static void main (String[] args) { try { FileInputStream in = new FileInputStream(args[0]); int c; while((c = in.read()) != -1) System.out.println(c); in.close(); } catch(Exception e) { e.printStackTrace(); } } // method main } // class ReadFile

6 6 JVM Instruction Sets l Stack and Local Variable Operations l Type Conversion l Integer Arithmetic l Logic and Bit manipulation l Floating Point Arithmetic l Objects and Arrays l Control Flow l Exceptions l Finally Clauses l Method Invocation and Return l Thread Synchronization

7 7 Menmonics for instruction type i for an int operation, l for long, s for short, b for byte, c for char, f for float, d for double, and a for reference/array. l EX: iload, lload, fload, dload, aload,…

8 8 Load and Store Instructions l transfer values between the local variables and the operand stack l load: localVar  Stack l store: Stack  localVar l [i | l | f | d | a]load u8,// or wide load u16 l [i | l | f | d | a]load_, // n=0..3 l [i | l | f | d | a]store u8, // or wide load u16 l [i | l | f | d | a]store_, // n=0..3

9 9 Push Constants onto stack iconst_ x »x =m1..5; push int x onto stack. fconst_ x »x =0..2; push float x onto stack. l lconst_x ; dconst_x : »x =0 or 1; push long (or double) x onto stack l aconst_null: push null onto stack. l bipush s8; sipush s16, »push (int) s8 [or (int) s16] onto stack

10 10 Push Constants onto stack l ldc u8; ldc_w u16, ldc2_w u16, »push single word (or double worlds for ldc2) from constant_pool entry u8 (or u16) »ldc/ldc_w can be used to push byte/short/char/int/float/String constants.

11 11 Generic Stack Operations l nop // do nothing l pop, pop2, // pop top (two) words l dup, dup2, // duplicate top (two) words l swap. // swap top two words l dup_x1, dup_x2 »duplicate top words and put 1 ( 2 ) down »w3, w2, w1 => w3,w1,w2,w1 or (w1,w3,w2,w1) l dup2_x1, dup2_x2, »w4, w3, w2, w1 =>w4, w2,w1, w3,w2,w1 or (w2,w1,w4,w3,w2,w1)

12 12 Integer and floating point Arithmetic Instructions l Add: iadd, ladd, fadd, dadd. l Subtract: isub, lsub, fsub, dsub. l Multiply: imul, lmul, fmul, dmul. l Divide: idiv, ldiv, fdiv, ddiv. l Remainder: irem, lrem, frem, drem. l Negate: ineg, lneg, fneg, dneg. l Local variable increment: »iinc u8 s8 // add s8 to lcalvar[u8]. »wide iinc u16 s16 // add s16 to local var at u16

13 13 Shift and Bitwise operations l Shift: ishl, ishr, iushr, lshl, lshr, lushr. »ishl : … int2, int1 => …, int2<<(int1 & 0x001f) »lushr: … long2, int1=> …, long2 >>> (int1 & 0x003f) l Bitwise OR: ior, lor. l Bitwise AND: iand, land. l Bitwise exclusive OR: ixor, lxor.

14 14 Comparison operations l dcmpg, dcmpl, »…d1,d2=>… int; (d1 -1; d2=d1=> 0; d1>d2 =>1 ) or NaN=>(g =>1; l=>-1) l fcmpg, fcmpl, // like above l lcmp. »… long1, long2 => … int k where » k = -1, 0 or 1 depending on long1 long2.

15 15 Type Conversion Instructions l widening conversions: »int  long  float  double : »i2l, i2f, i2d, l2f, l2d, f2d. »// sign extension+round-to-nearest mode l Narrowing conversions: »double  float  long  int : »d2f, d2l, d2i, f2l, f2i, l2i // truncate+ sign extension+ rtn mode »int  short, byte, char: »i2s, i2c, i2b // truncate+ sign extension l Note: byte, short, char  int are done automaticaly.

16 16 Object Creation and Field Access l new »Create a new class instance on the heap, »and push a reference: l Field access: »getfield fieldRefIndex; putfield fieldRefIndex, »… objRef  … value; …objRef, value  … » getstatic fieldRefIndex putstatic fieldRefIndex » …  value; …value  … l Type Checking: »instanceof class_index : … objRef  … RltVal »checkcast class_index: …obj1  … obj1 if obj1 can be casted to class type, o/w throw CastException

17 17 Array Creation l newarray aType: »pop length, allocate new primitive array of type given by atype, push the reference. »aType : (z,c,f,d,b,s,i,l) ->(4,5,6,7,8,9,10,11) »… u16 -> arrayRef l anewarray Class_index, »pop length, allocate new array of type given by class_index, push the reference l multianewarray type_index dim:u8. »pop dim number of array lengths, allocate multi- dimensional array of type class_index, pop reference. »new int[10][20] ==> p10, p20, multianewarray [[i 2.

18 18 Array Component Access l array component  stack : »baload, caload, saload, iaload, »… arrayRef, index:s32  … (int) array[index] »laload, faload, daload, aaload. l stack  array component »bastore, castore, sastore, iastore, »… arrayRef, index:s32, val  … »lastore, fastore, dastore, aastore. l Get the length of array: arraylength »… arrayRef  … (int)array.length

19 19 Control flow Instructions 1. Conditional Branches l Conditional branch: »unary comparsions –ifeq, iflt, ifle, ifne, ifgt, ifge, // =0?, <0?,… –ifnull, ifnonnull, // null or not null »binary comparisons –if_icmpeq, if_icmpne, if_icmplt, if_icmpgt, if_icmple, if_icmpge, –if_acmpeq, if_acmpne. // compare reference l For comparison of long, double and float »use dcmpg, dcmpl, fcmpg, fcmpl, lcmp and »unary comparisons on int.

20 20 Control flow Instructions : 2.Compound Conditional Branches l lookupswitch defaultOffset #pairs {case offset} #pairs : »all (blue) fields are int32 values »case must be in ascending order »e.g.: lookupswitch lab0 3 2 lab1 10 lab2 13 lab3 l tableswitch defOffset lowCase highCase offset high-low+1. »goto offset[top-low+1] iff low ≤ top ≤ high »else goto defOffset. »e.g.: tableswitch lab0 11 14 lab11 lab12 lab13 lab14.

21 21 Control flow Instructions : Unconditional branches l goto s16; goto_w s32 »pc = pc + s16 (or s32). // for implementing finally clauses // intraMethod subroutine call l jsr s16, jsr_w s32, »push return addr and jumpToSubroutine pc+s16 for a finally block »…  … returnAddr l ret u8; widen ret u16, »return to addr given by localVar u8 or u16.

22 22 Method Invocation and Return Instructions l invokevirtual : for normal methods l invokeInterface: »for interface methods // search of implementation is needed l invokespecial: »for private method, superclass method and instance initialization (constructors) methods l invokestatic : for static methods l format: opcode u16 // methodRefIndex l return : »ireturn, lreturn, freturn, dreturn, areturn »return : for void, instance, class (or interface) initialization methods

23 23 Exceptions related Instructions l Throw Exception: »athrow »can also be thrown by other instructions if abnormal condition detected. »… ExcObj  … Implementing finally l jsr s16, jsr_w s32, »jumpToSubroutine pc+s16 for a finally block l ret u8; widen ret u16, »return to addr given by localVar u8 or u16.

24 24 Synchronization l via monitor construct: »monitorenter, monitorexit l monitorenter »pop objectref, acquire the lock associated with the object. »… ObjRef  … l monitorexit »pop objectref, release the lock associate with objectref » … ObjRef  …

25 25 Classes needing special support of JVM l Reflection: » java.lang.reflect.* and java.lang. Class. l Loading and creation of a class or interface. »ClassLoader. l Linking and initialization of a class or interface. l Security: »java.security.** and other classes such as SecurityManage r. Multithreading, such as the class Thread. Weak references: java.lang.ref.* l …


Download ppt "1 Java Virtual Machine: Instruction Set Cheng-Chia Chen."

Similar presentations


Ads by Google