Java virtual machine instruction set
Download
1 / 25

Java Virtual Machine: Instruction Set - PowerPoint PPT Presentation


  • 119 Views
  • Uploaded on

Java Virtual Machine: Instruction Set. Cheng-Chia Chen. 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. 2. Java ByteCode.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Java Virtual Machine: Instruction Set' - ray


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Java virtual machine instruction set

Java Virtual Machine: Instruction Set

Cheng-Chia Chen


Java program
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

2


Java bytecode
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

3


Java bytecode1
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

3


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


Jvm instruction sets
JVM Instruction Sets

  • Stack and Local Variable Operations

  • Type Conversion

  • Integer Arithmetic

  • Logic and Bit manipulation

  • Floating Point Arithmetic

  • Objects and Arrays

  • Control Flow

  • Exceptions

  • Finally Clauses

  • Method Invocation and Return

  • Thread Synchronization


Menmonics for instruction type
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.

  • EX: iload, lload, fload, dload, aload,…


Load and store instructions
Load and Store Instructions

  • transfer values between the local variables and the operand stack

  • load: localVar  Stack

  • store: Stack  localVar

  • [i | l | f | d | a]load u8,// or wide load u16

  • [i | l | f | d | a]load_<n>, // n=0..3

  • [i | l | f | d | a]store u8, // or wide load u16

  • [i | l | f | d | a]store_<n>, // n=0..3


Push constants onto stack
Push Constants onto stack

  • iconst_x

    • x=m1..5; push int x onto stack.

  • fconst_x

    • x=0..2; push float x onto stack.

  • lconst_x ; dconst_x :

    • x=0 or 1; push long (or double) x onto stack

  • aconst_null: push null onto stack.

  • bipush s8;sipush s16,

    • push (int) s8 [or (int) s16] onto stack


Push constants onto stack1
Push Constants onto stack

  • 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.


Generic stack operations
Generic Stack Operations

  • nop // do nothing

  • pop, pop2, // pop top (two) words

  • dup, dup2, // duplicate top (two) words

  • swap. // swap top two words

  • dup_x1, dup_x2

    • duplicate top words and put 1 ( 2 ) down

    • w3, w2, w1 => w3,w1,w2,w1 or (w1,w3,w2,w1)

  • dup2_x1, dup2_x2,

    • w4, w3, w2, w1 =>w4, w2,w1, w3,w2,w1 or (w2,w1,w4,w3,w2,w1)


Integer and floating point arithmetic instructions
Integer and floating pointArithmetic 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.

  • Local variable increment:

    • iinc u8 s8 // add s8 to lcalvar[u8].

    • wide iinc u16 s16 // add s16 to local var at u16


Shift and bitwise operations
Shift and Bitwise operations

  • Shift: ishl, ishr, iushr, lshl, lshr, lushr.

    • ishl : … int2, int1 => … , int2<<(int1 & 0x001f)

    • lushr: … long2, int1=> … , long2 >>> (int1 & 0x003f)

  • Bitwise OR: ior, lor.

  • Bitwise AND: iand, land.

  • Bitwise exclusive OR: ixor, lxor.


Comparison operations
Comparison operations

  • dcmpg, dcmpl,

    • …d1,d2=>… int; (d1 < d2 =>-1; d2=d1=> 0; d1>d2 =>1 ) or NaN=>(g =>1; l=>-1)

  • fcmpg, fcmpl, // like above

  • lcmp.

    • … long1, long2 => … int k where

    • k = -1, 0 or 1 depending on long1 <, = or > long2.


Type conversion instructions
Type Conversion Instructions

  • widening conversions:

    • int  long  float  double :

    • i2l, i2f, i2d, l2f, l2d, f2d.

    • // sign extension+round-to-nearest mode

  • 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

  • Note: byte, short, char  int are done automaticaly.


Object creation and field access
Object Creation and Field Access

  • new

    • Create a new class instance on the heap,

    • and push a reference:

  • Field access:

    • getfield fieldRefIndex;putfield fieldRefIndex,

    • … objRef  … value; …objRef, value …

    • getstaticfieldRefIndexputstatic fieldRefIndex

    • …  value; …value  …

  • Type Checking:

    • instanceof class_index : … objRef  … RltVal

    • checkcast class_index: …obj1  … obj1 if obj1 can be casted to class type, o/w throw CastException


Array creation
Array Creation

  • 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

  • anewarray Class_index,

    • pop length, allocate new array of type given by class_index, push the reference

  • 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.


Array component access
Array Component Access

  • array component  stack :

    • baload, caload, saload, iaload,

    • … arrayRef, index:s32  … (int) array[index]

    • laload, faload, daload, aaload.

  • stack  array component

    • bastore, castore, sastore, iastore,

    • … arrayRef, index:s32, val  …

    • lastore, fastore, dastore, aastore.

  • Get the length of array:arraylength

    • … arrayRef  … (int)array.length


Control flow instructions 1 conditional branches
Control flow Instructions1. Conditional Branches

  • 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

  • For comparison of long, double and float

    • use dcmpg, dcmpl, fcmpg, fcmpl, lcmp and

    • unary comparisons on int.


Control flow instructions 2 compound conditional branches
Control flow Instructions :2.Compound Conditional Branches

  • lookupswitch <0-3bytepads for word alignment> defaultOffset#pairs {caseoffset}#pairs:

    • all (blue) fields are int32 values

    • case must be in ascending order

    • e.g.: lookupswitch lab0 3 2 lab1 10 lab2 13 lab3

  • tableswitch <0-3pads> 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.


Control flow instructions unconditional branches
Control flow Instructions: Unconditional branches

  • goto s16;goto_w s32

    • pc = pc + s16 (or s32).

      // for implementing finally clauses

      // intraMethod subroutine call

  • jsr s16,jsr_w s32,

    • push return addr and jumpToSubroutine pc+s16 for a finally block

    • …  … returnAddr

  • ret u8; widen ret u16,

    • return to addr given by localVar u8 or u16 .


Method invocation and return instructions
Method Invocation and Return Instructions

  • invokevirtual : for normal methods

  • invokeInterface:

    • for interface methods // search of implementation is needed

  • invokespecial:

    • for private method, superclass method and instance initialization (constructors) methods

  • invokestatic : for static methods

  • format: opcode u16// methodRefIndex

  • return :

    • ireturn, lreturn, freturn, dreturn, areturn

    • return : for void, instance, class (or interface) initialization methods


Exceptions related instructions
Exceptions related Instructions

  • Throw Exception:

    • athrow

    • can also be thrown by other instructions if abnormal condition detected.

    • … ExcObj  …

  • Implementing finally

  • jsr s16,jsr_w s32,

    • jumpToSubroutine pc+s16 for a finally block

  • ret u8; widen ret u16,

    • return to addr given by localVar u8 or u16 .


Synchronization
Synchronization

  • via monitor construct:

    • monitorenter, monitorexit

  • monitorenter

    • pop objectref, acquire the lock associated with the object.

    • … ObjRef  …

  • monitorexit

    • pop objectref, release the lock associate with objectref

    • … ObjRef  …


Classes needing special support of jvm
Classes needing special support of JVM

  • Reflection:

    • java.lang.reflect.* and java.lang.Class.

  • Loading and creation of a class or interface.

    • ClassLoader.

  • Linking and initialization of a class or interface.

  • Security:

    • java.security.** and other classes such as SecurityManager.

  • Multithreading, such as the class Thread.

  • Weak references: java.lang.ref.*


ad