jvm instructions basics –stack-based –compactness & efficiency jvm instruction set...

44
JVM Instructions • Basics – Stack-based – Compactness & Efficiency • JVM Instruction Set – Compiling Java to JVM Dachuan Yu

Post on 19-Dec-2015

234 views

Category:

Documents


4 download

TRANSCRIPT

JVM Instructions

• Basics– Stack-based– Compactness & Efficiency

• JVM Instruction Set– Compiling Java to JVM

Dachuan Yu

Basics

• Stack-Based– most JVM instructions involve the operand stack

– transfer between operand stack and local variable

– load from constant pool

– rules to guarantee verifiability

• Compactness and Efficiency– some operands taken from the operand stack

– operands integrated in opcodes

– type integrated in opcodes

– not orthogonal

Compiling Statements and Expressions

three rules about the stackto guarantee that the code produced is verifiable

• statement:– empty stack empty stack

• expression– place an additional element on top of the stack

• compound expression/statement– evaluate subexpressions first

– leaving the result of each on the stack

– used as operands to the compound exp/stm

JVM Instruction Set

• Stack and Local Variable Operations

• Type Conversion

• Integer Arithmetic

• Logic

• Floating-Point Arithmetic

• Objects and Arrays

• Control Flow

• Exceptions

• Finally Clauses

• Method Invocation and Return

• Reserved

Stack and Local Variable Operations

• Pushing Constants onto the StackOpcodes indicate the constant value to push in 3 different ways

– implicit in the opcode itself

– follows the opcode in the bytecode stream as an operand

– taken from the constant pool

• Generic Stack Operations• Pushing Local Variables onto the Stack• Popping to Local Variables• The wide Instruction

Pushing single-word constants onto the stackOpcode Operand(s) Description

• iconst_m1 (none) pushes int -1 onto the stack

• iconst_0 (none) pushes int 0 onto the stack

• iconst_1 (none) pushes int 1 onto the stack

• iconst_2 (none) pushes int 2 onto the stack

• iconst_3 (none) pushes int 3 onto the stack

• iconst_4 (none) pushes int 4 onto the stack

• iconst_5 (none) pushes int 5 onto the stack

• fconst_0 (none) pushes float 0 onto the stack

• fconst_1 (none) pushes float 1 onto the stack

• fconst_2 (none) pushes float 2 onto the stack

Pushing dual-word constants onto the stack• lconst_0 (none) pushes long 0 onto the stack

• lconst_1 (none) pushes long 1 onto the stack

• dconst_0 (none) pushes double 0 onto the stack

• dconst_1 (none) pushes double 1 onto the stack

Pushing a null reference onto the stackOpcode Operand(s) Description

• aconst_null (none) pushes a null object reference

Pushing byte and short constants onto the stack• bipush byte1 expands byte1 (a byte type)

to an int and pushes it onto the stack• sipush byte1,byte2 expands byte1, byte2 (a short type)

to an int and pushes it onto the stack

Pushing constant pool entries onto the stack• ldc indexbyte1 pushes single-word value from constant

pool entry specified by indexbyte1• ldc_w indexbyte1, indexbyte2

pushes single-word value from constant

pool entry specified by indexbyte1, 2• ldc2_w indexbyte1, indexbyte2

pushes dual-word value from constant

pool entry specified by indexbyte1, 2

Stack manipulation

Opcode Operand(s) Description

• nop (none) do nothing

• pop (none) pop the top word from the operand stack

• pop2 (none) pop the top two words

• swap (none) swap the top two words

• dup (none) duplicate top word

• dup2 (none) duplicate top two words

• dup_x1 (none) duplicate top word and put two down

• dup_x2 (none) duplicate top word and put three down

• dup2_x1 (none) duplicate top two words

and put three down

• dup2_x2 (none) duplicate top two words

and put four down

Pushing local variables onto the stack

Opcode Operand(s) Description

• iload vindex pushes int from local variable position

vindex

• iload_0 (none) pushes int from local variable position 0

• iload_1 (none) pushes int from local variable position 1

• iload_2 (none) pushes int from local variable position 2

• iload_3 (none) pushes int from local variable position 3

• Similar cases for float, long, double and object reference.

• Similar cases for popping from the stack into local variables (-store).

The wide IntructionOpcode Operand(s) Description

• wide iload, indexbyte1, indexbyte2

• wide lload, indexbyte1, indexbyte2

• wide fload, indexbyte1, indexbyte2

• wide dload, indexbyte1, indexbyte2

• wide aload, indexbyte1, indexbyte2

• wide istore, indexbyte1, indexbyte2

• wide lstore, indexbyte1, indexbyte2

• wide fstore, indexbyte1, indexbyte2

• wide dstore, indexbyte1, indexbyte2

• wide astore, indexbyte1, indexbyte2

• wide iinc ...

• wide ret ...

Type ConversionOpcode Operand(s) Description

• i2l (none)

• i2f (none)

• i2d (none)

• l2i (none)

• l2f (none)

• l2d (none)

• f2i (none)

• f2l (none)

• f2d (none)

• d2i (none)

• d2l (none)

• d2f (none)

• i2b (none)

• i2c (none)

• i2s (none)

Integer ArithmeticOpcode Operand(s) Description

• iadd (none)

• isub (none)

• imul (none)

• idiv (none)

• irem (none)

• ineg (none)

• ladd (none)

• lsub (none)

• lmul (none)

• ldiv (none)

• lrem (none)

• lneg (none)

• iinc vindex, const adds const to an int at local

variable position vindex

• wide iinc, indexbyte1, indexbyte2,

constbyte1, constbyte2

LogicOpcode Operand(s) Description

• ishl (none) shifts int left

• ishr (none) arithmetic shifts int right

• iushr (none) logical shifts int right

• iand (none) boolean ANDs two ints

• ior (none) boolean ORs two ints

• ixor (none) boolean XORs two ints

• lshl (none)

• lshr (none)

• lushr (none)

• land (none)

• lor (none)

• lxor (none)

Floating-Point Arithmetic

Opcode Operand(s) Description

• fadd (none)

• fsub (none)

• fmul (none)

• fdiv (none)

• frem (none)

• fneg (none)

• dadd (none)

• dsub (none)

• dmul (none)

• ddiv (none)

• drem (none)

• dneg (none)

IEEE 754 floating-point standard

Objects and Arrays• create and manipulate objects and arrays

– involve the heap

– most of them refer to entries in the constant pool

• a refresher on objects and arrays– memory is allocated on the garbage-collected heap only as objects

– only object references and primitive types can reside on stack

– all objects are instantiated/accessed with the same set of opcodes

– except for arrays -- handled by special opcodes

• opcodes for objects• opcodes for arrays

Object creationOpcode Operand(s) Description

• new indexbyte1,indexbyte2 creates a new object on the heap,

pushes reference

Accessing instance variables• putfield indexbyte1,indexbyte2 set field, indicated by index,

of object to value• getfield indexbyte1,indexbyte2 pushes field, indicated by index,

of object

Accessing class variables• putstatic indexbyte1,indexbyte2 set field, indicated by index,

of object to value• getstatic indexbyte1,indexbyte2 pushes field, indicated by index,

of object

Type checking• checkcast indexbyte1,indexbyte2 throws ClassCastException

if cannot cast• instanceof indexbyte1,indexbyte2 pushes true if succeed,

else pushes false

Create new arraysOpcode Operand(s) Description

• newarrray atype pops length allocate new array of “atype”,

pushes objectref of new array

• anewarrray indexbyte1, pops length allocate new array of objects,

indexbyte2 pushes objectref of new array

• multianewarray indexbyte1, indexbyte2, dimensions

Getting an array• arraylength (none)

Retrieving an array element• iaload (none)

baload, caload, saload, laload, faload, daload, aaload

Storing to an array element• iastore (none)

bastore, castore, sastore, lastore, fastore, dastore, aastore

Control Flow

• Dealing with the following Java source codes– if, if-else, while, do-while, for and switch

• Conditional Branching• Unconditional Branching• Conditional Branching with Tables• Others

– exceptions

– finally clauses

– invoking and returning from methods

Conditional Branching• Integer comparison with zero

– ifeq, ifne, iflt, ifle, ifgt, ifge

• comparison of two integers– if_icmpeq, if_icmpne, if_icmplt, if_icmple,

if_icmpgt, if_icmpge

• comparison of longs, floats, and doubles– lcmp

– fcmpg, fcmpl, dcmpg, dcmpl

– handling NaN

Conditional Branching cont’d

• Object reference comparison with null– ifnull, ifnonnull

• comparison of two object reference– if_acmpeq, if_acmpne

Unconditional BranchingUnconditional branching

Opcode Operand(s) Description

• goto branchbyte1, branchbyte2 branch to offset

• goto_w branchbyte1, branchbyte2, branch to offset

branchbyte3, branchbyte4

– JVM adds the offset to the current pc register

– resulting address must contain an instruction

in the current method

Conditional Branching with Tables

Table jumpingOpcode Operand(s) Description

• lookupswitch <0-3 byte pad>defaultbyte1,

defaultbyte2, defaultbyte3, defaultbyte4,

npairs1, npairs2, npairs3, npairs4,

case value/branch offset pairs...

• tableswitch <0-3 byte pad>defaultbyte1,

defaultbyte2, defaultbyte3, defaultbyte4

lowbyte1, lowbyte2, lowbyte3, lowbyte4,

highbyte1, highbyte2, highbyte3, highbyte4,

branch offsets…

– lookupswitch is more general purpose

– tableswitch is more efficient

Exceptions

example the exception table throw - athrow catch

exception thrownsearch the exception tableif match, continue execution at specified offsetif not, pop current stack frame and rethrow

Exception example - Java codeclass OverflowException extends Exception {}

class DivideByZeroException extends Exception {}

class NitPickyMath {

static int remainder(int dividend, int divisor)

throws OverflowException, DivideByZeroException {

if ((dividend == Integer.MIN_VALUE) &&

(divisor == -1)) {

throw new OverflowException();

}

try {

return dividend % divisor;

}

catch (ArithmeticException e) {

throw new DivideByZeroException();

}

}

}

Method int remainder(int, int)

0 iload_0

1 ldc #1 <Integer -2147483648>

3 if_icmpne 19

6 iload_1

7 iconst_m1

8 if_icmpne 19

11 new #4 <Class OverflowException>

14 dup

15 invokespecial #10 <Method OverflowException()>

18 athrow

19 iload_0

20 iload_1

21 irem

22 ireturn

23 pop

24 new #2 <Class DivideByZeroException>

27 dup

28 invokespecial #9 <Method DivideByZeroException()>

31 athrow

Exception example - byte code

Exception Table• one entry for each “catch”

– start point;

– end point;

– pc offset to jump to;

– constant pool index of the exception class

From to target type

19 23 23 <Class java.lang.ArithmeticException>

pc is within the range exception class is as specified

Finally Clauses

• opcodes for finally clausesMiniature Subroutine

Opcode Operand(s) Description

• jsr branchbyte1, branchbyte2 pushes return address, do branch

• jsr_w branchbyte1, branchbyte2,

branchbyte3, branchbyte4

• ret index returns to the address stored

in local variable index

• wide ret, indexbyte1, indexbyte2

• surprising behaviors

class Surprise {

static int surpriseTheProgrammer(boolean bVal) {

while (bVal) {

try { return true; }

finally { break; }

}

return false;

}

}

• finally clause may include return, break, continue statements

• finally clause may throw exceptions

Asymmetrical Invocation and Return

Asymmetrical Invocation and Return

class Nostalgia {

static int giveMeThatOldFashionedBoolean(boolean bVal) {

try {

if (bVal) {

return 1;

}

return 0;

}

finally {

System.out.println("Got old fashioned.");

}

}

}

Method int giveMeThatOldFashionedBoolean(boolean)

0 iload_0

1 ifeq 11

4 iconst_1

5 istore_1

6 jsr 24

9 iload_1

10 ireturn

11 iconst_0

12 istore_1

13 jsr 24

16 iload_1

17 ireturn

Asymmetrical Invocation and Return

18 astore_219 jsr 2422 aload_223 athrow

24 astore_325 getstatic #7 <Field java.io.PrintStream out>28 ldc #1 <String "Got old fashioned.">30 invokevirtual #8 <Method void println(java.lang.String)>33 ret 3

Exception table: from to target type 0 18 18 any

Method Invocation and Return

• four instructions• invokevirtual

• invokestatic

• invokespecial

• invokeinterface

• arguments are passed as local variables• invoke on:

• object class name

• method name

• method descriptor

• constant pool stores symbolic reference

Opcode Operand(s) Description

• invokevirtual indexbyte1, indexbyte2 pop objectref and args, invoke

(dynamic binding) instance method at constant pool index

• pop object reference and arguments

• (possibly) resolve

• verifications– the referenced method exists

– access is legal

invokevirtual

local var0: objectref

local var1: arg1

local var2: arg2

local var3: arg3

local var4: ...

Opcode Operand(s) Description

• invokevirtual indexbyte1, indexbyte2 pop args, invoke

(static binding) class method at constant pool index

• pop arguments

• (possibly) resolve

• verifications– the referenced method exists

– access is legal

invokestatic

local var0: arg1

local var1: arg2

local var2: arg3

local var3: ...

The invokespecial Instruction

• instance initialization methods

(<init>())

• private methods

• methods invoked with the super keyword

dynamic binding wouldn’t work

invokespecial and <init>()

• <init>() is invoked when a new instance is created

• subclass <init>() need to be able to

invoke superclass <init>()

invokespecial and Private Methods

• Subclass may declare an instance method

with the same signature

as a private instance method in a superclass

Superclass Subclass

private void foo() void foo()

void callfoo(){

foo();

}Subclass me = new Subclass();me.callfoo();

Invokespecial and Private Methodsclass Superclass {

private void interestingMethod() {

System.out.println("Superclass's interesting method.");

}

void exampleMethod() {

interestingMethod();

}

}

class Subclass extends Superclass {

void interestingMethod() {

System.out.println("Subclass's interesting method.");

}

public static void main(String args[]) {

Subclass me = new Subclass();

me.exampleMethod();

}

}

Invokespecial and Private Methods

class Superclass extends java.lang.Object {

Superclass();

void exampleMethod();

}

Method void exampleMethod()

0 aload_0

1 invokespecial #8 <Method void interestingMethod()>

4 return

invokespecial and super

• when calling super.someMethod()

we want the superclass’s version of someMethod()• ACC_SUPER (set by compiler)

– static binding

– special dynamic binding

Animal Dog CockerSpaniel

walk() walk()

• recompile issue

The invokeinterface Instruction

• Performs same function as invokevirtual– invokes instance methods

– uses dynamic binding

• can't make as many assumptions

about the method table offset– Given a class reference, a method will always occupy

the same position in the method table

– this is not true given an interface reference

Method Table Offset Issue

class Dog

ptr to clone()

ptr to woof()

class CockerSpaniel extends Dog

implements Friendly

ptr to clone()

ptr to woof()

ptr to sayHello()

ptr to sayGoodbye()

ptr to sayHello()

ptr to sayGoodbye()

interface Friendly

ptr to sayHello()

ptr to sayGoodbye()

class Cat

implements Friendly

ptr to clone()

Invocation Instructions and Speed

• invokespecial & invokestatic • invokevirtual• invokeinterface

faster

slower

Returning from Methods

Opcode Operand(s) Description

• ireturn (none) pop int, push onto stack of calling method and return

• lreturn (none) pop long, ...

• freturn (none) pop float, ...

• dreturn (none) pop double, ...

• areturn (none) pop object reference, ...

• return (none) return void

Reserved

• impdep1 254(0xfe)

• impdep2 255(oxff)

• breakpoint 202(oxca)