Java ByteCode
Manuel Oriol June 8th, 2006
Java ByteCode Manuel Oriol June 8th, 2006 Byte Code? The Java - - PowerPoint PPT Presentation
Java ByteCode Manuel Oriol June 8th, 2006 Byte Code? The Java language is compiled into an intermediary form called byte code It ensures portability The byte code is a succession of instructions that manipulate a stack Each
Manuel Oriol June 8th, 2006
2
3
4
http://jakarta.apache.org/bcel/images/classfile.gif
5
6
7
CONSTANT_Class 7 CONSTANT_Fieldref 9 CONSTANT_Methodref 10 CONSTANT_InterfaceMethodref 11 CONSTANT_String 8 CONSTANT_Integer 3 CONSTANT_Float 4 CONSTANT_Long 5 CONSTANT_Double 6 CONSTANT_NameAndType 12 CONSTANT_Utf8 1
accessed from outside its package.
allowed.
specially when invoked by the invokespecial instruction.
0x0200 Is an interface, not a class.
be instantiated.
8
9
10
field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
11
method_info {
u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
12
Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; u2 exception_table_length; { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; }
13
14
15
signed byte
Unicode character
double-precision floating-point value
single-precision floating-point value
long integer
an instance of class <classname> (full path with /)
16
A method descriptor represents the parameters that the method takes and the value that it returns: MethodDescriptor: ( ParameterDescriptor* ) ReturnDescriptor A parameter descriptor represents a parameter passed to a method: ParameterDescriptor: FieldType A return descriptor represents the type of the value returned from a method. It is a series of characters generated by the grammar: ReturnDescriptor: FieldType V
17
18
CONSTANT_Class 7 CONSTANT_Fieldref 9 CONSTANT_Methodref 10 CONSTANT_InterfaceMethodref 11 CONSTANT_String 8 CONSTANT_Integer 3 CONSTANT_Float 4 CONSTANT_Long 5 CONSTANT_Double 6 CONSTANT_NameAndType 12 CONSTANT_Utf8 1
19
cp_info { u1 tag; u1 info[]; }
CONSTANT_Class_info { u1 tag; u2 name_index; } CONSTANT_Fieldref_info { u1 tag; u2 class_index; u2 name_and_type_index; } CONSTANT_Methodref_info { u1 tag; u2 class_index; u2 name_and_type_index; } CONSTANT_InterfaceMethodref_info { u1 tag; u2 class_index; u2 name_and_type_index; }
20
CONSTANT_Integer_info { u1 tag; u4 bytes; } CONSTANT_Float_info { u1 tag; u4 bytes; } CONSTANT_Long_info { u1 tag; u4 high_bytes; u4 low_bytes; } CONSTANT_Double_info { u1 tag; u4 high_bytes; u4 low_bytes; }
21
CONSTANT_String_info { u1 tag; u2 string_index; } CONSTANT_NameAndType_info { u1 tag; u2 name_index; u2 descriptor_index; } CONSTANT_Utf8_info { u1 tag; u2 length; u1 bytes[length]; }
22
23
24
b (boolean & byte), c, d, f, i, l, s
sipush, bipush, dconst_0, dconst_1, fconst_0,... fconst_2, iconst_0,..., iconst_5, lconst_0, lconst_1, sipush
d2f, d2i, d2l, f2d, f2i, f2l, i2b, i2c, i2d, i2f, i2l, i2s
f, i, l: fadd, fdiv...), dcmpg, dcmpl (f,l) (makes comparisons), iand, ior, ishl, ishr, iashr, ixor (also with l)
25
26
saload
lastore, sastore
arraylength
27
28
getfield, putfield, getstatic, putstatic
monitorenter, monitorexit
new, aconst_null
29
30
31
32
33
34
35
36
37
aload, aload_0, ..., aload_3, (same with d, i, f, l)
astore, astore_0, ..., aload_3, (same with d, i, f, l)
ldc, ldc_w, ldc2_w
38
39
40
41
42
43
44
45
46
public int test3(int); Signature: (I)I Code: 0: iconst_0 1: istore_2 2: iconst_0 3: istore_3 4: iload_3 5: bipush 10 7: if_icmpge 20 10: iload_2 11: iload_3 12: iadd 13: istore_2 14: iinc 3, 1 17: goto 4 20: iload_2 21: ireturn
47
public static void main(java.lang.String[]); Signature: ([Ljava/lang/String;)V Code: 0: new #2; //class Example 3: dup 4: invokespecial #3; //Method "<init>":()V 7: astore_1 8: invokestatic #4; //Method test1:()I 11: pop 12: aload_1 13: iconst_2 14: invokevirtual #5; //Method test2:(I)I 17: istore_2 18: aload_1 19: iload_2 20: invokevirtual #6; //Method test3:(I)I 23: pop 24: return
48
48
49
49
50
51
52
Manuel Oriol June 8th, 2006
54
55