jvm2
TRANSCRIPT
![Page 1: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/1.jpg)
JVM byte code jvm commands
Nick BovaSep 1 2013
![Page 2: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/5.jpg)
Special thanks
@Anton Arhipovhttp://arhipov.blogspot.com/
![Page 6: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/23.jpg)
Main bytecode uses
- Building a compiler- Static analysis- JVM bytecode instrumentation- ?
![Page 24: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/27.jpg)
Type Conversion Instructions
• widening numeric conversions: i2l• narrowing numeric conversions: l2i
![Page 28: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/29.jpg)
Operand Stack Management Instructions
• pop, dup, swap
![Page 30: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/30.jpg)
Control Transfer Instructionsstructions
• Conditional branch: ifeq, ifneifge, ifnull, ifnonnull, if_icmpeq
• Compound conditional branch: tableswitch, lookupswitch.
• Unconditional branch: goto, jsr, ret.
![Page 31: Jvm2](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/554f3ed3b4c90572088b51a6/html5/thumbnails/33.jpg)
Throwing Exceptions
• An exception is thrown programmatically using the athrow instruction.