jvm2
TRANSCRIPT
JVM byte code jvm commands
Nick BovaSep 1 2013
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]
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
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
Special thanks
@Anton Arhipovhttp://arhipov.blogspot.com/
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
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/
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
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
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
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
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
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
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
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.
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)
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)
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.
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)
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)
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)
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)
Main bytecode uses
- Building a compiler- Static analysis- JVM bytecode instrumentation- ?
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
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
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
Type Conversion Instructions
• widening numeric conversions: i2l• narrowing numeric conversions: l2i
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
Operand Stack Management Instructions
• pop, dup, swap
Control Transfer Instructionsstructions
• Conditional branch: ifeq, ifneifge, ifnull, ifnonnull, if_icmpeq
• Compound conditional branch: tableswitch, lookupswitch.
• Unconditional branch: goto, jsr, ret.
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
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).
Throwing Exceptions
• An exception is thrown programmatically using the athrow instruction.