
JVM Instructions. Dachuan Yu. Basics Stack-based Compactness & Efficiency JVM Instruction Set Compiling Java to JVM. Basics. Stack-Based most JVM instructions involve the operand stack transfer between operand stack and local variable load from constant pool
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.
Dachuan Yu
three rules about the stack
to guarantee that the code produced is verifiable
Opcodes indicate the constant value to push in 3 different ways
Opcode Operand(s) Description
Pushing dual-word constants onto the stack
Opcode Operand(s) Description
Pushing byte and short constants onto the stack
to an int and pushes it onto the stack
to an int and pushes it onto the stack
Pushing constant pool entries onto the stack
pool entry specified by indexbyte1
pushes single-word value from constant
pool entry specified by indexbyte1, 2
pushes dual-word value from constant
pool entry specified by indexbyte1, 2
Opcode Operand(s) Description
and put three down
and put four down
Opcode Operand(s) Description
vindex
Opcode Operand(s) Description
Opcode Operand(s) Description
Opcode Operand(s) Description
variable position vindex
constbyte1, constbyte2
Opcode Operand(s) Description
Opcode Operand(s) Description
IEEE 754 floating-point standard
Opcode Operand(s) Description
pushes reference
Accessing instance variables
of object to value
of object
Accessing class variables
of object to value
of object
Type checking
if cannot cast
else pushes false
Opcode Operand(s) Description
pushes objectref of new array
indexbyte2 pushes objectref of new array
Getting an array
Retrieving an array element
baload, caload, saload, laload, faload, daload, aaload
Storing to an array element
bastore, castore, sastore, lastore, fastore, dastore, aastore
if_icmpgt, if_icmpge
Unconditional branching
Opcode Operand(s) Description
branchbyte3, branchbyte4
in the current method
Table jumping
Opcode Operand(s) Description
defaultbyte2, defaultbyte3, defaultbyte4,
npairs1, npairs2, npairs3, npairs4,
case value/branch offset pairs...
defaultbyte2, defaultbyte3, defaultbyte4
lowbyte1, lowbyte2, lowbyte3, lowbyte4,
highbyte1, highbyte2, highbyte3, highbyte4,
branch offsets…
class 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
From to target type
19 23 23 <Class java.lang.ArithmeticException>
Miniature Subroutine
Opcode Operand(s) Description
branchbyte3, branchbyte4
in local variable index
class Surprise {
static int surpriseTheProgrammer(boolean bVal) {
while (bVal) {
try { return true; }
finally { break; }
}
return false;
}
}
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
18 astore_2
19 jsr 24
22 aload_2
23 athrow
24 astore_3
25 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
Opcode Operand(s) Description
(dynamic binding) instance method at constant pool index
local var0: objectref
local var1: arg1
local var2: arg2
local var3: arg3
local var4: ...
Opcode Operand(s) Description
(static binding) class method at constant pool index
local var0: arg1
local var1: arg2
local var2: arg3
local var3: ...
(<init>())
dynamic binding wouldn’t work
invoke superclass <init>()
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();
class 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();
}
}
class Superclass extends java.lang.Object {
Superclass();
void exampleMethod();
}
Method void exampleMethod()
0 aload_0
1 invokespecial #8 <Method void interestingMethod()>
4 return
we want the superclass’s version of someMethod()
Animal Dog CockerSpaniel
walk() walk()
about the method table offset
interface Friendly
class Dog
ptr to sayHello()
ptr to clone()
ptr to sayGoodbye()
ptr to woof()
class CockerSpaniel extends Dog
implements Friendly
class Cat
implements Friendly
ptr to clone()
ptr to clone()
ptr to woof()
ptr to sayHello()
ptr to sayHello()
ptr to sayGoodbye()
ptr to sayGoodbye()
faster
slower
Opcode Operand(s) Description