2. 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 – bova.mykola@gmail.com
3. 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
4. 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
6. Books
1) Inside the Java Virtual Machine
Bill Venners
2) Programming for the Java™ Virtual Machine
Joshua Engel
3) The Well-Grounded Java Developer
Vital techniques of Java 7 and polyglot
programming.
Benjamin J. Evans
Martijn Verburg
7. Articles (1)
1) JVM Internals
Douglas Q. Hawkins
http://www.dougqh.net/
3) Lifting The Veil – Reading Java Byte Code
Alexander Shopov
3) Java bytecode:
Understanding bytecode makes you a better programmer
Peter Haggar
http://www.ibm.com/developerworks/ibm/library/ithaggar_bytecode/
8. Articles (2)
4) Looking "Under the Hood" with javap
by Corey McGlone
http://www.javaranch.com/journal/200408/ScjpTip
Line-javap.html
5) Understanding JVM Internals
http://www.cubrid.org/blog/devplatform/understanding-jvm-internals/
6) Java Bytecode Fundamentals
http://arhipov.blogspot.com/2011/01/javabytecode-fundamentals.html
15. 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.
16. 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)
17. 3. No loop constructs
There’s no built-in support for while, for, foreach loops.
Compilers implement these using jump
instructions.
(Example in file HelloWorld_FOR.javap)
18. 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.
19. 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)
20. 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)
21. 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)
22. 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)
24. 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
25. 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
28. 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
31. 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 an
instance 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
32. Method Invocation and Return
Instructions (invokedynamic)
javac won’t emit invokedynamic
There is no direct Java language support for invokedynamic in Java 7—
no Java
expression will be directly compiled into an invokedynamic bytecode
by javac. Java 8
is expected to add more language constructs (such as default methods)
that will
make 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
when
targeting the Java 7 VM (but some clever Java frameworks have found
ways to make
it work for them too).