LatJUG. Java Bytecode Fundamentals
Upcoming SlideShare
Loading in...5
×
 

LatJUG. Java Bytecode Fundamentals

on

  • 1,803 views

Java Bytecode Fundamentals

Java Bytecode Fundamentals
Anton Arhipov [Estonia]
http://goo.gl/GO6Mm

Java User Group Latvia
March 2011
http://goo.gl/77MhT

Statistics

Views

Total Views
1,803
Slideshare-icon Views on SlideShare
1,742
Embed Views
61

Actions

Likes
6
Downloads
67
Comments
0

5 Embeds 61

http://jujo00obo2o234ungd3t8qjfcjrs3o6k-a-sites-opensocial.googleusercontent.com 47
http://www.jug.lv 7
https://jujo00obo2o234ungd3t8qjfcjrs3o6k-a-sites-opensocial.googleusercontent.com 3
http://jug.lv 3
http://jugpreview.tumblr.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial LicenseCC Attribution-NonCommercial License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    LatJUG. Java Bytecode Fundamentals LatJUG. Java Bytecode Fundamentals Presentation Transcript

    • Java BytecodeFundamentals JUG.LV 2011, Riga
    • whoamiAnton ArhipovZeroTurnaroundJRebel http://arhipov.blogspot.com @antonarhipov @javarebel
    • who-are-you?
    • 1+2
    • +1+2 1 2
    • +1+2 1 212+
    • +1+2 1 212+
    • +1+2 1 212+ PUSH 1 1
    • +1+2 1 212+ PUSH 1 PUSH 2 2 1
    • +1+2 1 212+ PUSH 1 PUSH 2 3 ADD
    • +1+2 1 212+ ICONST_1 ICONST_2 3 IADD
    • ?=1+2
    • Byte Code One-byte instructions 256 possible opcodes ~200 in use
    • Byte Code One-byte instructions 256 possible opcodes ~200 in use
    • The Master Plan javap Stack Machine Objects and Methods Flow Control
    • javap Java class file disassembler Used with no options shows class structure only Methods, superclass, interfaces, etc -c – shows the bytecode -private – shows all classes and members -s – prints internal types signatures -l – prints lines numbers and local variable tables
    • C:workjugclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: returnpublic static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    • C:workjugclasses>javap Hello -verboseCompiled from "Hello.java“public class Hello extends java.lang.Object SourceFile: "Hello.java" minor version: 0 major version: 50 Constant pool:const #1 = Method #6.#20; // java/lang/Object."<init>":()Vconst #2 = Field #21.#22; // java/lang/System.out:Ljava/io/PrintStream;const #3 = String #23; // Hello, World!const #4 = Method #24.#25; // java/io/PrintStream.println:(Ljava/lang/String;)Vconst #5 = class #26; // Helloconst #6 = class #27; // java/lang/Objectconst #7 = Asciz <init>;const #8 = Asciz ()V;
    • C:workjugclasses>javap Hello -verbose…public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LHello;
    • C:workjugclasses>javap Hello -verbose…public static void main(java.lang.String[]); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return LineNumberTable: line 4: 0 line 5: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 args [Ljava/lang/String;
    • Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation Frame: Operand stack Array of local variables
    • Frame
    • public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • 0 1 2 3 4 aload_0 getfield 00 02 areturnpublic java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • 0 1 2 3 4 2A B4 00 02 B0public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • Stack Operations dup A pop B swap dup_x1 dup_x2
    • Stack Operations dup A pop A swap B dup_x1 dup_x2
    • Stack Operations dup A pop B swap dup_x1 dup_x2
    • Stack Operations dup B pop A swap dup_x1 dup_x2
    • Stack Operations dup B pop A swap B dup_x1 dup_x2
    • Stack Operations dup B pop A swap B dup_x1 B dup_x2 A
    • Local Variables
    • Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variables The tablepublic int calculate(int); Code: maps Stack=2, Locals=2, Args_size=2 numbers to … names LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2 Sized explicitly … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variables Local Variables Stackvar value depth value ldc "Hello"0 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Local Variables Stackvar value depth value ldc "Hello"0 0 "Hello" astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 1 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 "Hello" astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables & Stack load Local Variables Stack Table store
    • Object Initializationnew <init> Instance initialization method <clinit> Class and interface initialization method
    • Object Initialization: static {} static {}; Code: 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
    • Object Initialization: static {} <clinit> static {}; Code: 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
    • Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
    • Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
    • Object Initialization: {}
    • Object Initialization: {}
    • Object Initialization: {} public Initializer(int); Code: 0: aload_0 1: invokespecial #1; // ..<init> 4: aload_0 5: iconst_1 6: putfield #2; //Field a:I 9: aload_0 10: iconst_2 11: putfield #3; //Field c:I 14: aload_0 15: iload_1 16: putfield #4; //Field b:I 19: return
    • Method Invocation invokestatic invokeinterface invokevirtual invokespecial invokedynamic
    • Parameter Passing
    • Parameter Passing parameter return value
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 this 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2; //createRandomValue()1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 this 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 this 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 X 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; // Integer.intValue:() 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 1 3 7: invokespecial #3; //incValue 10: areturn 2 X 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 X+1 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; //Integer.valueOf 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; //incValue 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 objectref 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 objectref 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Local Variables public java.lang.Object execute();var value 0: aload_0 Stack 1: invokespecial #2;1 4: astore_1 depth value2 5: aload_0 6: aload_1 1 objectref 3 7: invokespecial #3; 10: areturn 2 3 private java.lang.Integer incValue(java.lang.Integer); 4var value 0: aload_11 1: invokevirtual #8; 5 4: iconst_1 62 5: iadd 3 6: invokestatic #7; 9: areturn
    • Flow Control
    • Flow Control GOTO
    • Stack depth valuepublic int decide(int); Code: 1 0: iload_1 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 0: iload_1 1 a 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 0: iload_1 1 10 1: bipush 3: if_icmpge 8 10 2 a 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 0: iload_1 1 10 1: bipush 3: if_icmpge 8 10 2 a 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 1 0: iload_1 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 0: iload_1 1 100 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 0: iload_1 1 100 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • Stack depth valuepublic int decide(int); Code: 1 0: iload_1 1: bipush 10 2 3: if_icmpge 8 6: iconst_0 3 7: ireturn 8: bipush 10010: ireturn
    • ant.arhipov@gmail.comhttp://arhipov.blogspot.com@antonarhipov@javarebel