Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Bytecode Crash Course [Code One 2019]

Java bytecode lies at the foundation of the the entire Java ecosystem. Regardless of what language and tools you use, if you run on the JVM, you’re using Java bytecode. Although understanding bytecode is not a requirement for all developers, a deeper understanding of how your code runs on the JVM can make understanding and resolving certain types of issues much easier. Understanding bytecode also opens the door to more-advanced techniques such as bytecode instrumentation via JVMTI and writing your own Java agent. This session starts at the very beginning and covers all the basics, with a heavy emphasis on examples.

  • Login to see the comments

  • Be the first to like this

Java Bytecode Crash Course [Code One 2019]

  1. 1. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website at http://www.oracle.com/investor. All information in this presentation is current as of September 2019 and Oracle undertakes no duty to update any statement in light of new information or future events. Safe Harbor Copyright © 2019 Oracle and/or its affiliates.
  2. 2. Java Bytecode Crash Course [DEV4312] Principal Member of Technical Staff Java Platform Group September 16, 2019 David Buck Copyright © 2019 Oracle and/or its affiliates.
  3. 3. JVM Sustaining Engineer OpenJDK 8 Update Project Maintainer JavaOne Rock Star Co-author of Oracle WebLogic Server 11g 構築・運用ガイド @DavidBuckJP https://blogs.oracle.com/buck/ Who am I? David Buck (left)
  4. 4. Agenda Introduction Foundational Concepts Examples
  5. 5. Introduction
  6. 6. Why?
  7. 7. Why? lingua franca of Java platform
  8. 8. Why? lingua franca of Java platform javap output
  9. 9. Why? lingua franca of Java platform javap output foundation for future study bridge methods type eraser debugging
  10. 10. javap -v option to “disassemble” bytecode -p option to see private methods use latest version
  11. 11. Disclaimer
  12. 12. Foundational Concepts
  13. 13. What is bytecode? byte-sized opcodes Only 256 possible opcodes operands can be 8 or 16 bit
  14. 14. stack machine
  15. 15. stack machine push 1 1
  16. 16. stack machine push 2 1 2
  17. 17. stack machine add 3
  18. 18. stack machine Convenient abstract away CPU register details free liveness analysis
  19. 19. stack machine Not that rare RPN notation
  20. 20. stack machine Not that rare RPN notation PostScript
  21. 21. stack machine Not that rare RPN notation PostScript Forth
  22. 22. local variables store / load commands used to access method arguments
  23. 23. Examples
  24. 24. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } }
  25. 25. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return
  26. 26. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return
  27. 27. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return
  28. 28. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this ?
  29. 29. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this ? 0
  30. 30. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 0
  31. 31. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 0 0
  32. 32. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 0 0 100
  33. 33. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 0
  34. 34. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 1
  35. 35. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 1
  36. 36. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 2
  37. 37. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 3
  38. 38. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 100
  39. 39. Let’s go for a spin! void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this 100
  40. 40. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } }
  41. 41. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } }
  42. 42. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: return
  43. 43. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: return
  44. 44. data “categories” Size 1 boolean byte char short int float reference returnAddress Size 2 long double
  45. 45. int spin void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this i int 1 int 0
  46. 46. Double Spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: returnStackLocals this i (1 of 2) d1 (2 of 2) d1 (1 of 2) i (2 of 2) d0 (2 of 2) d0 (1 of 2)
  47. 47. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: returnStackLocals this i (1 of 2) d1 (2 of 2) d1 (1 of 2) i (2 of 2) d0 (2 of 2) d0 (1 of 2)
  48. 48. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: returnStackLocals this i (1 of 2) d1 (2 of 2) d1 (1 of 2) i (2 of 2) d0 (2 of 2) d0 (1 of 2)
  49. 49. constant pool loads
  50. 50. constant pool loads Category 1 ldc ldc_w Category 2 ldc2_w
  51. 51. constant pool loads Category 1 ldc ldc_w Category 2 ldc2_w
  52. 52. constant pool loads Category 1 ldc ldc_w Category 2 ldc2_w
  53. 53. constant pool loads Category 1 ldc ldc_w Category 2 ldc2_w
  54. 54. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: returnStackLocals this i (1 of 2) d1 (2 of 2) d1 (1 of 2) i (2 of 2) d0 (2 of 2) d0 (1 of 2)
  55. 55. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: returnStackLocals this i (1 of 2) d1 (2 of 2) d1 (1 of 2) i (2 of 2) d0 (2 of 2) d0 (1 of 2)
  56. 56. but some types are more equal than others…opcode byte short int long float double char reference Tipush bipush sipush Tconst iconst lconst fconst dconst aconst Tload iload lload fload dload aload Tstore istore lstore fstore dstore astore Tinc iinc Taload baload saload iaload laload faload daload caload aaload Tastore bastore sastore iastore lastore fastore dastore castore aastore Tadd iadd ladd fadd dadd Tsub isub lsub fsub dsub Tmul imul lmul fmul dmul Tdiv idiv ldiv fdiv ddiv Trem irem lrem frem drem Tneg ineg lneg fneg dneg Tshl ishl lshl Tshr ishr lshr Tushr iushr lushr Tand iand land Tor ior lor Txor ixor lxor i2T i2b i2s i2l i2f i2d l2T l2i l2f l2d f2T f2i f2l f2d d2T d2i d2l d2f Tcmp lcmp Tcmpl fcmpl dcmpl Tcmpg fcmpg dcmpg if_TcmpOP if_icmpOP if_acmpOP Treturn ireturn lreturn freturn dreturn areturn
  57. 57. but some types are more equal than others… opcode byte short int long float double char reference Tipush bipush sipush Tconst iconst lconst fconst dconst aconst Tload iload lload fload dload aload Tstore istore lstore fstore dstore astore Tinc iinc Taload baload saload iaload laload faload daload caload aaload Tastore bastore sastore iastore lastore fastore dastore castore aastore Tadd iadd ladd fadd dadd Tsub isub lsub fsub dsub Tmul imul lmul fmul dmul Tdiv idiv ldiv fdiv ddiv Trem irem lrem frem drem Tneg ineg lneg fneg dneg Tshl ishl lshl Tshr ishr lshr Tushr iushr lushr Tand iand land Tor ior lor Txor ixor lxor i2T i2b i2s i2l i2f i2d l2T l2i l2f l2d f2T f2i f2l f2d d2T d2i d2l d2f Tcmp lcmp Tcmpl fcmpl dcmpl Tcmpg fcmpg dcmpg if_TcmpOP if_icmpOP if_acmpOP Treturn ireturn lreturn freturn dreturn areturn
  58. 58. but some types are more equal than others… Actual type Computational type Category Boolean int 1 byte int 1 char int 1 short int 1 int int 1 float float 1 reference reference 1 returnAddress returnAddress 1 long long 2 double double 2
  59. 59. int spin void spin() { int i; for (i = 0; i < 100; i++) { ;// empty } } void spin(); Code: stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 14 8: iinc 1, 1 11: goto 2 14: return StackLocals this i int 1 int 0
  60. 60. double spin void dspin() { double i; for (i = 0.0; i < 100.0; i++) { ; //empty } } void dspin(); stack=4, locals=3, args_size=1 0: dconst_0 1: dstore_1 2: dload_1 3: ldc2_w #2 // double 100.0d 6: dcmpg 7: ifge 17 10: dload_1 11: dconst_1 12: dadd 13: dstore_1 14: goto 2 17: returnStackLocals this i (1 of 2) d1 (2 of 2) d1 (1 of 2) i (2 of 2) d0 (2 of 2) d0 (1 of 2)
  61. 61. short spin void sspin() { short i; for (i = 0; i < 100; i++) { ; //empty } }
  62. 62. short spin void sspin() { short i; for (i = 0; i < 100; i++) { ; //empty } }
  63. 63. short spin void sspin() { short i; for (i = 0; i < 100; i++) { ; //empty } } void sspin(); stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 16 8: iload_1 9: iconst_1 10: iadd 11: i2s 12: istore_1 13: goto 2 16: returnStackLocals this i int 1 int 0
  64. 64. short spin void sspin() { short i; for (i = 0; i < 100; i++) { ; //empty } } void sspin(); stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 16 8: iload_1 9: iconst_1 10: iadd 11: i2s 12: istore_1 13: goto 2 16: returnStackLocals this i int 1 int 0
  65. 65. short spin void sspin() { short i; for (i = 0; i < 100; i++) { ; //empty } } void sspin(); stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 16 8: iload_1 9: iconst_1 10: iadd 11: i2s 12: istore_1 13: goto 2 16: returnStackLocals this i int 1 int 0
  66. 66. short spin void sspin() { short i; for (i = 0; i < 100; i++) { ; //empty } } void sspin(); stack=2, locals=2, args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 16 8: iload_1 9: iconst_1 10: iadd 11: i2s 12: istore_1 13: goto 2 16: returnStackLocals this i int 1 int 0
  67. 67. constant pool fun void useManyNumeric() { int i = 100; int j = 1000000; long l1 = 1; long l2 = 0xffffffff; double d = 2.2; // ...do some calculations... } void useManyNumeric(); 0: bipush 100 2: istore_1 3: ldc #2 // int 1000000 5: istore_2 6: lconst_1 7: lstore_3 8: ldc2_w #3 // long -1l 11: lstore 5 13: ldc2_w #5 // double 2.2d 16: dstore 7 18: return
  68. 68. constant pool fun void useManyNumeric() { int i = 100; int j = 1000000; long l1 = 1; long l2 = 0xffffffff; double d = 2.2; // ...do some calculations... } void useManyNumeric(); 0: bipush 100 2: istore_1 3: ldc #2 // int 1000000 5: istore_2 6: lconst_1 7: lstore_3 8: ldc2_w #3 // long -1l 11: lstore 5 13: ldc2_w #5 // double 2.2d 16: dstore 7 18: return
  69. 69. constant pool fun void useManyNumeric() { int i = 100; int j = 1000000; long l1 = 1; long l2 = 0xffffffff; double d = 2.2; // ...do some calculations... } void useManyNumeric(); 0: bipush 100 2: istore_1 3: ldc #2 // int 1000000 5: istore_2 6: lconst_1 7: lstore_3 8: ldc2_w #3 // long -1l 11: lstore 5 13: ldc2_w #5 // double 2.2d 16: dstore 7 18: return
  70. 70. constant pool fun void useManyNumeric() { int i = 100; int j = 1000000; long l1 = 1; long l2 = 0xffffffff; double d = 2.2; // ...do some calculations... } void useManyNumeric(); 0: bipush 100 2: istore_1 3: ldc #2 // int 1000000 5: istore_2 6: lconst_1 7: lstore_3 8: ldc2_w #3 // long -1l 11: lstore 5 13: ldc2_w #5 // double 2.2d 16: dstore 7 18: return
  71. 71. constant pool fun void useManyNumeric() { int i = 100; int j = 1000000; long l1 = 1; long l2 = 0xffffffff; double d = 2.2; // ...do some calculations... } void useManyNumeric(); 0: bipush 100 2: istore_1 3: ldc #2 // int 1000000 5: istore_2 6: lconst_1 7: lstore_3 8: ldc2_w #3 // long -1l 11: lstore 5 13: ldc2_w #5 // double 2.2d 16: dstore 7 18: return
  72. 72. Constant Pool Fun void useManyNumeric() { int i = 100; int j = 1000000; long l1 = 1; long l2 = 0xffffffff; double d = 2.2; // ...do some calculations... } void useManyNumeric(); 0: bipush 100 2: istore_1 3: ldc #2 // int 1000000 5: istore_2 6: lconst_1 7: lstore_3 8: ldc2_w #3 // long -1l 11: lstore 5 13: ldc2_w #5 // double 2.2d 16: dstore 7 18: return
  73. 73. argument passing (virtual) int addTwo(int i, int j) { return i + j; }
  74. 74. argument passing (virtual) int addTwo(int i, int j) { return i + j; } int addTwo(int, int); stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn
  75. 75. argument passing (virtual) int addTwo(int i, int j) { return i + j; } int addTwo(int, int); stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn
  76. 76. argument passing (virtual) int addTwo(int i, int j) { return i + j; } int addTwo(int, int); stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn StackLocals this i j
  77. 77. argument passing (virtual) int addTwo(int i, int j) { return i + j; } int addTwo(int, int); stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn StackLocals this i i j
  78. 78. argument passing (virtual) int addTwo(int i, int j) { return i + j; } int addTwo(int, int); stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn StackLocals this i i j j
  79. 79. argument passing (virtual) int addTwo(int i, int j) { return i + j; } int addTwo(int, int); stack=2, locals=3, args_size=3 0: iload_1 1: iload_2 2: iadd 3: ireturn StackLocals this i i+j j
  80. 80. argument passing (static) static int addTwoStatic(int i, int j) { return i + j; }
  81. 81. argument passing (static) static int addTwoStatic(int i, int j) { return i + j; } static int addTwoStatic(int, int); flags: ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: iadd 3: ireturn
  82. 82. argument passing (static) static int addTwoStatic(int i, int j) { return i + j; } static int addTwoStatic(int, int); flags: ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: iadd 3: ireturn
  83. 83. argument passing (static) static int addTwoStatic(int i, int j) { return i + j; } static int addTwoStatic(int, int); flags: ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: iadd 3: ireturn
  84. 84. argument passing (static) static int addTwoStatic(int i, int j) { return i + j; } static int addTwoStatic(int, int); flags: ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: iadd 3: ireturn StackLocals i j int 1 int 0
  85. 85. method call (virtual) int add12and13() { return addTwo(12, 13); } int add12and13(); Code: stack=3, locals=1, args_size=1 0: aload_0 1: bipush 12 3: bipush 13 5: invokevirtual #2 // Method addTwo:(II)I 8: ireturn StackLocals this
  86. 86. method call (virtual) int add12and13() { return addTwo(12, 13); } int add12and13(); Code: stack=3, locals=1, args_size=1 0: aload_0 1: bipush 12 3: bipush 13 5: invokevirtual #2 // Method addTwo:(II)I 8: ireturn StackLocals this this
  87. 87. method call (virtual) int add12and13() { return addTwo(12, 13); } int add12and13(); Code: stack=3, locals=1, args_size=1 0: aload_0 1: bipush 12 3: bipush 13 5: invokevirtual #2 // Method addTwo:(II)I 8: ireturn StackLocals this this 12
  88. 88. method call (virtual) int add12and13() { return addTwo(12, 13); } int add12and13(); Code: stack=3, locals=1, args_size=1 0: aload_0 1: bipush 12 3: bipush 13 5: invokevirtual #2 // Method addTwo:(II)I 8: ireturn StackLocals this this 12 13
  89. 89. method call (virtual) int add12and13() { return addTwo(12, 13); } int add12and13(); Code: stack=3, locals=1, args_size=1 0: aload_0 1: bipush 12 3: bipush 13 5: invokevirtual #2 // Method addTwo:(II)I 8: ireturn StackLocals this 25
  90. 90. method call (static) int add12and13_static() { return addTwoStatic(12, 13); } int add12and13_static(); Code: stack=2, locals=1, args_size=1 0: bipush 12 2: bipush 13 4: invokestatic #3 // Method addTwoStatic:(II)I 7: ireturn StackLocals 25
  91. 91. method call (static) int add12and13_static() { return addTwoStatic(12, 13); } int add12and13_static(); Code: stack=2, locals=1, args_size=1 0: bipush 12 2: bipush 13 4: invokestatic #3 // Method addTwoStatic:(II)I 7: ireturn StackLocals 12
  92. 92. method call (static) int add12and13_static() { return addTwoStatic(12, 13); } int add12and13_static(); Code: stack=2, locals=1, args_size=1 0: bipush 12 2: bipush 13 4: invokestatic #3 // Method addTwoStatic:(II)I 7: ireturn StackLocals 12 13
  93. 93. method call (static) int add12and13_static() { return addTwoStatic(12, 13); } int add12and13_static(); Code: stack=2, locals=1, args_size=1 0: bipush 12 2: bipush 13 4: invokestatic #3 // Method addTwoStatic:(II)I 7: ireturn StackLocals 25
  94. 94. method call (private) class Near { int it; public int getItNear() { return getIt(); } private int getIt() { return it; } }
  95. 95. method call (private) class Near { int it; public int getItNear() { return getIt(); } private int getIt() { return it; } } public int getItNear(); stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #3 // Method getIt:()I 4: ireturn
  96. 96. method call (super class) class Far extends Near { int getItFar() { return super.getItNear(); } }
  97. 97. method call (private) class Far extends Near { int getItFar() { return super.getItNear(); } } int getItFar(); Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #3 // Method Examples3_7$Near.getItNaer: ()I 4: ireturn
  98. 98. types of calls invokevirtual invokestatic invokeinterface invokespecial invokedynamic
  99. 99. types of calls invokevirtual invokestatic invokeinterface invokespecial invokedynamic
  100. 100. types of calls
  101. 101. types of calls invokevirtual instance method this pointer virtual lookup
  102. 102. types of calls invokevirtual instance method this pointer virtual lookup invokestatic class method no this pointer static linking
  103. 103. types of calls invokevirtual instance method this pointer virtual lookup invokestatic class method no this pointer static linking invokeinterface interface method this pointer virtual lookup
  104. 104. types of calls invokevirtual instance method this pointer virtual lookup invokestatic class method no this pointer static linking invokeinterface interface method this pointer virtual lookup invokespecial Everything else constructors super class private this pointer static linking
  105. 105. When two Objects love each other very much… Object create() { return new Object(); } StackLocals
  106. 106. When two Objects love each other very much… Object create() { return new Object(); } java.lang.Object create(); stack=2, locals=1, args_size=1 0: new #2 // class java/lang/Object 3: dup 4: invokespecial #1 // Method java/lang/Object."<init>":( )V 7: areturn StackLocals
  107. 107. When two Objects love each other very much… Object create() { return new Object(); } java.lang.Object create(); stack=2, locals=1, args_size=1 0: new #2 // class java/lang/Object 3: dup 4: invokespecial #1 // Method java/lang/Object."<init>":( )V 7: areturn StackLocals baby
  108. 108. When two Objects love each other very much… Object create() { return new Object(); } java.lang.Object create(); stack=2, locals=1, args_size=1 0: new #2 // class java/lang/Object 3: dup 4: invokespecial #1 // Method java/lang/Object."<init>":( )V 7: areturn StackLocals baby baby
  109. 109. When two Objects love each other very much… Object create() { return new Object(); } java.lang.Object create(); stack=2, locals=1, args_size=1 0: new #2 // class java/lang/Object 3: dup 4: invokespecial #1 // Method java/lang/Object."<init>":( )V 7: areturn StackLocals baby
  110. 110. field access int i void setIt(int value) { i = value; } int getIt() { return i; } StackLocals
  111. 111. field access (set) int i void setIt(int value) { i = value; } int getIt() { return i; } void setIt(int); stack=2, locals=2, args_size=2 0: aload_0 1: iload_1 2: putfield #6 // Field i:I 5: return StackLocals
  112. 112. field access (set) int i void setIt(int value) { i = value; } int getIt() { return i; } void setIt(int); stack=2, locals=2, args_size=2 0: aload_0 1: iload_1 2: putfield #6 // Field i:I 5: return StackLocals this
  113. 113. field access (set) int i void setIt(int value) { i = value; } int getIt() { return i; } void setIt(int); stack=2, locals=2, args_size=2 0: aload_0 1: iload_1 2: putfield #6 // Field i:I 5: return StackLocals this value
  114. 114. field access (set) int i void setIt(int value) { i = value; } int getIt() { return i; } void setIt(int); stack=2, locals=2, args_size=2 0: aload_0 1: iload_1 2: putfield #6 // Field i:I 5: return StackLocals
  115. 115. field access (get) int i void setIt(int value) { i = value; } int getIt() { return i; } int getIt(); stack=1, locals=1, args_size=1 0: aload_0 1: getfield #6 // Field i:I 4: ireturn StackLocals
  116. 116. array opcodes newarray (primitive type) anewarray multianewarray Tastore (e.g. iastore) Taload (e.g. faload)
  117. 117. switch statement (table) int chooseNear(int i) { switch (i) { case 0: return 0; case 1: return 1; case 2: return 2; default: return -1; } }
  118. 118. switch statement (table) int chooseNear(int i) { switch (i) { case 0: return 0; case 1: return 1; case 2: return 2; default: return -1; } } int chooseNear(int); 0: iload_1 1: tableswitch { // 0 to 2 0: 28 1: 30 2: 32 default: 34 } 28: iconst_0 29: ireturn 30: iconst_1 31: ireturn 32: iconst_2 33: ireturn 34: iconst_m1 35: ireturn
  119. 119. switch statement (sparse) int chooseFar(int i) { switch (i) { case -100: return -1; case 0: return 0; case 100: return 1; default: return -1; } }
  120. 120. switch statement (sparse) int chooseFar(int i) { switch (i) { case -100: return -1; case 0: return 0; case 100: return 1; default: return -1; } } int chooseFar(int); 0: iload_1 1: lookupswitch { // 3 -100: 36 0: 38 100: 40 default: 42 } 36: iconst_m1 37: ireturn 38: iconst_0 39: ireturn 40: iconst_1 41: ireturn 42: iconst_m1 43: ireturn
  121. 121. untyped stack operations pop pop2 dup dup2 dup_x1 dup2_x1 dup_x2 dup2_x2 swap
  122. 122. Exceptions void catchOne(); stack=2, locals=2, args_size=1 0: aload_0 1: invokevirtual #4 // Method tryItOut:()V 4: goto 13 7: astore_1 8: aload_0 9: aload_1 10: invokevirtual #5 // Method handleExc:(Ljava/lang/Exception;)V 13: return Exception table: from to target type 0 4 7 Class TestExc
  123. 123. Exceptions void catchOne() { try { tryItOut(); } catch (TestExc e) { handleExc(e); } }
  124. 124. Exceptions void catchOne(); stack=2, locals=2, args_size=1 0: aload_0 1: invokevirtual #4 // Method tryItOut:()V 4: goto 13 7: astore_1 8: aload_0 9: aload_1 10: invokevirtual #5 // Method handleExc:(Ljava/lang/Exception;)V 13: return Exception table: from to target type 0 4 7 Class TestExc
  125. 125. Exceptions void catchOne(); stack=2, locals=2, args_size=1 0: aload_0 1: invokevirtual #4 // Method tryItOut:()V 4: goto 13 7: astore_1 8: aload_0 9: aload_1 10: invokevirtual #5 // Method handleExc:(Ljava/lang/Exception;)V 13: return Exception table: from to target type 0 4 7 Class TestExc
  126. 126. Exceptions void catchOne(); stack=2, locals=2, args_size=1 0: aload_0 1: invokevirtual #4 // Method tryItOut:()V 4: goto 13 7: astore_1 8: aload_0 9: aload_1 10: invokevirtual #5 // Method handleExc:(Ljava/lang/Exception;)V 13: return Exception table: from to target type 0 4 7 Class TestExc
  127. 127. about finally jsr / ret removed in jdk7 (cass file ver. 51) now finally block code is just copied to each possible exit point
  128. 128. Q&A
  129. 129. Thank You!!!
  130. 130. Session Survey Help us make the content even better. Please complete the session survey in the Mobile App. Copyright © 2019 Oracle and/or its affiliates.
  131. 131. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website at http://www.oracle.com/investor. All information in this presentation is current as of September 2019 and Oracle undertakes no duty to update any statement in light of new information or future events. Safe Harbor Copyright © 2019 Oracle and/or its affiliates.

×