jvm2

33
JVM byte code jvm commands Nick Bova Sep 1 2013

Upload: mykola-bova

Post on 10-May-2015

344 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Jvm2

JVM byte code jvm commands

Nick BovaSep 1 2013

Page 2: Jvm2

About myself

• In IT since 2000• 6 years with mainframes• Refactoring FinExpert virtual machine and finex programming

language• Many assemblers in institute

• Skype – mykola_bova• Twitter – mykola_bova• Facebook - FB/myk.bova• LinkedIn – ua.linkedin.com/in/mykbova• E-mail – [email protected]

Page 3: Jvm2

Why JVM byte code?

• A key to JVM internals and Java itself• Practical understanding –key for solving difficult problems• A way to understand how it “really” works

Page 4: Jvm2

What will be / will not be here?

• JVM and JVM byte code (JVM spec)• “Touch” JVM byte code on practice (reJ,

ClassEditor)

• Libraries for byte code manipulation? – No• Am I JVM / JVM byte code expert? - No

Page 5: Jvm2

Special thanks

@Anton Arhipovhttp://arhipov.blogspot.com/

Page 6: Jvm2

Books

1) Inside the Java Virtual MachineBill Venners2) Programming for the Java™ Virtual MachineJoshua Engel3) The Well-Grounded Java DeveloperVital techniques of Java 7 and polyglot

programming.Benjamin J. EvansMartijn Verburg

Page 7: Jvm2

Articles (1)

1) JVM InternalsDouglas Q. Hawkinshttp://www.dougqh.net/3) Lifting The Veil – Reading Java Byte CodeAlexander Shopov3) Java bytecode:Understanding bytecode makes you a better programmerPeter Haggarhttp://www.ibm.com/developerworks/ibm/library/it-

haggar_bytecode/

Page 8: Jvm2

Articles (2)

4) Looking "Under the Hood" with javapby Corey McGlonehttp://www.javaranch.com/journal/200408/ScjpTipLine-

javap.html5) Understanding JVM Internalshttp://www.cubrid.org/blog/dev-platform/

understanding-jvm-internals/6) Java Bytecode Fundamentalshttp://arhipov.blogspot.com/2011/01/java-bytecode-

fundamentals.html

Page 9: Jvm2

Simple byte code example

3

1

2

0

4

Iconst_1

Iconst_2

iadd

Istore_0

Iload_0

Stack

2

1

0

Method variables

0 1 2

Page 10: Jvm2

Simple byte code example

3

1

2

0

4

Iconst_1

Iconst_2

iadd

Istore_0

Iload_0

1

2

1

0

Method variables

Stack

0 1 2

Page 11: Jvm2

Simple byte code example

3

1

2

0

4

Iconst_1

Iconst_2

iadd

Istore_0

Iload_0 2

1

2

1

0

Method variables

Stack

0 1 2

Page 12: Jvm2

Simple byte code example

3

1

2

0

4

Iconst_1

Iconst_2

iadd

Istore_0

Iload_0

1 + 2

2

1

0

Method variables

Stack

0 1 2

Page 13: Jvm2

Simple byte code example

3

1

2

0

4

Iconst_1

Iconst_2

iadd

Istore_0

Iload_0

3

2

1

0

Method variables

Stack

0 1 2

Page 14: Jvm2

Simple byte code example

3

1

2

0

4

Iconst_1

Iconst_2

iadd

Istore_0

Iload_0

2

1

0

3

Method variables

Stack

0 1 2

Page 15: Jvm2

1. No variables

Bytecode employs an Assembly-like register stack known as the locals stack to hold variables.

Values of fields, functions and of binary operations (+, -, * ..) are held in a stack known as the operand stack.

Page 16: Jvm2

2. No binary logical operators

No built-in support for &&, ||, ^

Compilers implement these using jump instructions.

(Examples in files HelloWorld_AND.javap and HelloWorld_OR.javap)

Page 17: Jvm2

3. No loop constructs

There’s no built-in support for while, for, for-each loops.

Compilers implement these using jump instructions.

(Example in file HelloWorld_FOR.javap)

Page 18: Jvm2

4. No String support

Like in C, there’s no built-in support for strings, only char arrays.

Compilers usually use StringBuilder to compensate.No penalty for concatenating different data types(Example in file HelloWorld_STRING.javap)JavaC uses java.lang.StringBuilder to combine (+)strings.

Different overloads of the .append() method are used to concat different data types.

Page 19: Jvm2

5. Only 4 primitive types

Bytecode only operates on 4 primitives types int, float, double, long vs. the 8 Java primitives.

char, bool, byte, short are treated as ints(Example in file HelloWorld_INT.javap)

Page 20: Jvm2

6. Only 4 primitive types

Bytecode only operates on 4 primitives types Compilers will add synthetic $this fields.

If you’re not making calls to your outer-class - don’t forget to add a static modifier.

(Example in file HelloWorld_NESTED.javap)

Page 21: Jvm2

8 Using nested classes (2)?

Bytecode only operates on 4 primitives types • Try and avoid implicitly creating bridge

methods by invoking private members (use protected)

(Example in file HelloWorld_BRIDGE.javap)

Page 22: Jvm2

9. Boxing and unboxing

Boxing is added by the Java/Scala compiler. There’s no such concept in bytecode or in the

JVM.

Watch out for NullPointerExceptions(Example in file HelloWorld_BOXING.javap)

Page 23: Jvm2

Main bytecode uses

- Building a compiler- Static analysis- JVM bytecode instrumentation- ?

Page 24: Jvm2

Different types are supported in the JVM instruction set

- opcode - Taload- byte - baload- short - saload- int - iaload- long - laload- float - faload- double - daload- char - caload- reference - aaload

Page 25: Jvm2

Load and Store Instructions

• Load a local variable onto the operand stack: iload

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

• Load a constant on to the operand stack: ldc• Gain access to more local variables using a

wider index, or to a larger immediate operand: wide

Page 26: Jvm2

Arithmetic Instructions• Add: iadd• Subtract: isub• Multiply: imul• Divide: idiv• Remainder: irem• Negate: ineg• Shift: ishl• Bitwise OR: ior• Bitwise AND: iand• Bitwise exclusive OR: ixor• Local variable increment: iinc.• Comparison: dcmpg

Page 27: Jvm2

Type Conversion Instructions

• widening numeric conversions: i2l• narrowing numeric conversions: l2i

Page 28: Jvm2

Object Creation and Manipulation• Create a new class instance: new• Create a new array: newarray• Access fields of classes and fields of class instances :

getfield, putfield, getstatic, putstatic• Load an array component onto the operand stack:

baload• Store a value from the operand stack as an array

component: bastore• Get the length of array: arraylength.• Check properties of class instances or arrays:

instanceof, checkcast

Page 29: Jvm2

Operand Stack Management Instructions

• pop, dup, swap

Page 30: Jvm2

Control Transfer Instructionsstructions

• Conditional branch: ifeq, ifneifge, ifnull, ifnonnull, if_icmpeq

• Compound conditional branch: tableswitch, lookupswitch.

• Unconditional branch: goto, jsr, ret.

Page 31: Jvm2

Method Invocation and Return Instructions

• invokevirtual invokes an instance method of an object, dispatching on the

(virtual) type of the object• invokeinterface invokes an interface method• invokespecial invokes an instance method requiring special

handling, whether aninstance initialization method, a private method, or a

superclass method.• invokestatic invokes a class (static) method in a named

class.• The method return instructions, which are distinguished by

return type: ireturn

Page 32: Jvm2

Method Invocation and Return Instructions (invokedynamic)

javac won’t emit invokedynamicThere is no direct Java language support for invokedynamic in Java 7—no

Javaexpression will be directly compiled into an invokedynamic bytecode by

javac. Java 8is expected to add more language constructs (such as default methods)

that willmake use of the dynamic capabilities.Instead, invokedynamic is an improvement that is squarely targeted at

non-Java languages.The bytecode has been added for dynamic languages to make use of whentargeting the Java 7 VM (but some clever Java frameworks have found

ways to makeit work for them too).

Page 33: Jvm2

Throwing Exceptions

• An exception is thrown programmatically using the athrow instruction.