Your SlideShare is downloading. ×
  • Like
Jvm2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Jvm2

  • 308 views
Published

 

Published in Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
308
On SlideShare
0
From Embeds
0
Number of Embeds
6

Actions

Shares
Downloads
11
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. JVM byte code jvm commands Nick Bova Sep 1 2013
  • 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
  • 5. Special thanks @Anton Arhipov http://arhipov.blogspot.com/
  • 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
  • 9. Simple byte code example Method variables 0 Iconst_1 1 Iconst_2 2 iadd 3 Istore_0 4 Iload_0 0 2 1 0 Stack 1 2
  • 10. Simple byte code example Method variables 0 Iconst_1 1 Iconst_2 2 iadd 3 Istore_0 4 Iload_0 0 2 1 0 1 Stack 1 2
  • 11. Simple byte code example Method variables 0 Iconst_1 1 Iconst_2 2 iadd 3 Istore_0 4 Iload_0 0 2 1 2 0 1 Stack 1 2
  • 12. Simple byte code example Method variables 0 Iconst_1 1 Iconst_2 2 iadd 3 Istore_0 4 Iload_0 0 2 1 0 1+2 Stack 1 2
  • 13. Simple byte code example Method variables 0 Iconst_1 1 Iconst_2 2 iadd 3 Istore_0 4 Iload_0 0 2 1 0 3 Stack 1 2
  • 14. Simple byte code example Method variables 0 Iconst_1 1 Iconst_2 2 iadd 3 Istore_0 4 Iload_0 0 3 2 1 0 Stack 1 2
  • 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)
  • 23. Main bytecode uses - Building a compiler Static analysis JVM bytecode instrumentation ?
  • 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
  • 26. 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
  • 27. Type Conversion Instructions • widening numeric conversions: i2l • narrowing numeric conversions: l2i
  • 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
  • 29. Operand Stack Management Instructions • pop, dup, swap
  • 30. Control Transfer Instructionsstructions • Conditional branch: ifeq, ifneifge, ifnull, ifnonnull, if_icmpeq • Compound conditional branch: tableswitch, lookupswitch. • Unconditional branch: goto, jsr, ret.
  • 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).
  • 33. Throwing Exceptions • An exception is thrown programmatically using the athrow instruction.