Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Java Programming/Byte Code

From Wikibooks, open books for an open world
<Java Programming

Java Byte Code is the language to which Java source is compiled and the Java Virtual Machine understands. Unlike compiled languages that have to be specifically compiled for each different type of computers, a Java program only needs to be converted to byte code once, after which it can run on any platform for which a Java Virtual Machine exists.

Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM). Bytecode files generally have a .class extension. It is not normally necessary for a Java programmer to know byte code, but it can be useful.



Other Languages

[edit |edit source]

There are a number of exciting new languages being created that also compile to Java byte code, such asGroovy.

GNAT
The GNU Ada-Compiler, is capable of compiling Ada into Java-style bytecode.
ftp://cs.nyu.edu/pub/gnat
JPython
Compiles Python to Java-style bytecode.
http://www.jpython.org/
Kawa
Compiles Scheme to Java-style bytecode.
http://www.gnu.org/software/kawa/


Example

[edit |edit source]

Consider the following Java code.

outer:for(inti=2;i<1000;i++){for(intj=2;j<i;j++){if(i%j==0)continueouter;}System.out.println(i);}

A Java compiler might translate the Java code above into byte code as follows, assuming the above was put in a method:

  Code:   0:   iconst_2   1:   istore_1   2:   iload_1   3:   sipush  1000   6:   if_icmpge       44   9:   iconst_2   10:  istore_2   11:  iload_2   12:  iload_1   13:  if_icmpge       31   16:  iload_1   17:  iload_2   18:  irem             # remainder   19:  ifne    25   22:  goto    38   25:  iinc    2, 1   28:  goto    11   31:  getstatic       #84; //Field java/lang/System.out:Ljava/io/PrintStream;   34:  iload_1   35:  invokevirtual   #85; //Method java/io/PrintStream.println:(I)V   38:  iinc    1, 1   41:  goto    2   44:  return

Example 2

[edit |edit source]

As an example we can write a simple Foo.java source:

publicclassFoo{publicstaticvoidmain(finalString[]args){System.out.println("This is a simple example of decompilation using javap");a();b();}publicstaticvoida(){System.out.println("Now we are calling a function...");}publicstaticvoidb(){System.out.println("...and now we are calling b");}}

Compile it and then move Foo.java to another directory or delete it if you wish. What can we do with javap and Foo.class ?

$javap Foo

produces this result:

Compiled from "Foo.java"public class Foo extends java.lang.Object {    public Foo();    public static void main(java.lang.String[]);    public static void a();    public static void b();}

As you can see the javac compiler doesn't strip any (public) variable name from the .class file. As a result the names of the functions, their parameters and types of return are exposed. (This is necessary in order for other classes to access them.)

Let's do a bit more, try:

$javap -c Foo
Compiled from "Foo.java"public class Foo extends java.lang.Object{public Foo();  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 This is a simple example of decompilation using javap   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V   8:   invokestatic    #5; //Method a:()V   11:  invokestatic    #6; //Method b:()V   14:  returnpublic static void a();  Code:   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;   3:   ldc             #7; //String Now we are calling a function...   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V   8:   returnpublic static void b();  Code:   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;   3:   ldc             #8; //String ...and now we are calling b   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V   8:   return}

The Java bytecodes

[edit |edit source]

See Oracle's Java Virtual Machine Specification[1] for more detailed descriptions

The manipulation of the operand stack is notated as [before]→[after], where [before] is the stack before the instruction is executed and [after] is the stack after the instruction is executed. A stack with the element 'b' on the top and element 'a' just after the top element is denoted 'a,b'.

MnemonicOpcode
(inhex)
Other bytesStack
[before]→[after]
Description
A
aaload32arrayref, index → valueloads onto the stack a reference from an array
aastore53arrayref, index, value →stores a reference into an array
aconst_null01→ nullpushes anull reference onto the stack
aload19index→ objectrefloads a reference onto the stack from a local variable#index
aload_02a→ objectrefloads a reference onto the stack from local variable 0
aload_12b→ objectrefloads a reference onto the stack from local variable 1
aload_22c→ objectrefloads a reference onto the stack from local variable 2
aload_32d→ objectrefloads a reference onto the stack from local variable 3
anewarraybdindexbyte1, indexbyte2count → arrayrefcreates a new array of references of lengthcount and component type identified by the class referenceindex (indexbyte1 << 8 + indexbyte2) in the constant pool
areturnb0objectref → [empty]returns a reference from a method
arraylengthbearrayref → lengthgets the length of an array
astore3aindexobjectref →stores a reference into a local variable#index
astore_04bobjectref →stores a reference into local variable 0
astore_14cobjectref →stores a reference into local variable 1
astore_24dobjectref →stores a reference into local variable 2
astore_34eobjectref →stores a reference into local variable 3
athrowbfobjectref → [empty], objectrefthrows an error or exception (notice that the rest of the stack is cleared, leaving only a reference to the Throwable)
B
baload33arrayref, index → valueloads a byte or Boolean value from an array
bastore54arrayref, index, value →stores a byte or Boolean value into an array
bipush10byte→ valuepushes abyte onto the stack as an integervalue
C
caload34arrayref, index → valueloads a char from an array
castore55arrayref, index, value →stores a char into an array
checkcastc0indexbyte1, indexbyte2objectref → objectrefchecks whether anobjectref is of a certain type, the class reference of which is in the constant pool atindex (indexbyte1 << 8 + indexbyte2)
D
d2f90value → resultconverts a double to a float
d2i8evalue → resultconverts a double to an int
d2l8fvalue → resultconverts a double to a long
dadd63value1, value2 → resultadds two doubles
daload31arrayref, index → valueloads a double from an array
dastore52arrayref, index, value →stores a double into an array
dcmpg98value1, value2 → resultcompares two doubles
dcmpl97value1, value2 → resultcompares two doubles
dconst_00e→ 0.0pushes the constant0.0 onto the stack
dconst_10f→ 1.0pushes the constant1.0 onto the stack
ddiv6fvalue1, value2 → resultdivides two doubles
dload18index→ valueloads a doublevalue from a local variable#index
dload_026→ valueloads a double from local variable 0
dload_127→ valueloads a double from local variable 1
dload_228→ valueloads a double from local variable 2
dload_329→ valueloads a double from local variable 3
dmul6bvalue1, value2 → resultmultiplies two doubles
dneg77value → resultnegates a double
drem73value1, value2 → resultgets the remainder from a division between two doubles
dreturnafvalue → [empty]returns a double from a method
dstore39indexvalue →stores a doublevalue into a local variable#index
dstore_047value →stores a double into local variable 0
dstore_148value →stores a double into local variable 1
dstore_249value →stores a double into local variable 2
dstore_34avalue →stores a double into local variable 3
dsub67value1, value2 → resultsubtracts a double from another
dup59value → value, valueduplicates the value on top of the stack
dup_x15avalue2, value1 → value1, value2, value1inserts a copy of the top value into the stack two values from the top
dup_x25bvalue3, value2, value1 → value1, value3, value2, value1inserts a copy of the top value into the stack two (if value2 is double or long it takes up the entry of value3, too) or three values (if value2 is neither double nor long) from the top
dup25c{value2, value1} → {value2, value1}, {value2, value1}duplicate top two stack words (two values, if value1 is not double nor long; a single value, if value1 is double or long)
dup2_x15dvalue3, {value2, value1} → {value2, value1}, value3, {value2, value1}duplicate two words and insert beneath third word (see explanation above)
dup2_x25e{value4, value3}, {value2, value1} → {value2, value1}, {value4, value3}, {value2, value1}duplicate two words and insert beneath fourth word
F
f2d8dvalue → resultconverts a float to a double
f2i8bvalue → resultconverts a float to an int
f2l8cvalue → resultconverts a float to a long
fadd62value1, value2 → resultadds two floats
faload30arrayref, index → valueloads a float from an array
fastore51arreyref, index, value →stores a float in an array
fcmpg96value1, value2 → resultcompares two floats
fcmpl95value1, value2 → resultcompares two floats
fconst_00b→ 0.0fpushes0.0f on the stack
fconst_10c→ 1.0fpushes1.0f on the stack
fconst_20d→ 2.0fpushes2.0f on the stack
fdiv6evalue1, value2 → resultdivides two floats
fload17index→ valueloads a floatvalue from a local variable#index
fload_022→ valueloads a floatvalue from local variable 0
fload_123→ valueloads a floatvalue from local variable 1
fload_224→ valueloads a floatvalue from local variable 2
fload_325→ valueloads a floatvalue from local variable 3
fmul6avalue1, value2 → resultmultiplies two floats
fneg76value → resultnegates a float
frem72value1, value2 → resultgets the remainder from a division between two floats
freturnaevalue → [empty]returns a float from method
fstore38indexvalue →stores a floatvalue into a local variable#index
fstore_043value →stores a floatvalue into local variable 0
fstore_144value →stores a floatvalue into local variable 1
fstore_245value →stores a floatvalue into local variable 2
fstore_346value →stores a floatvalue into local variable 3
fsub66value1, value2 → resultsubtracts two floats
G
getfieldb4index1, index2objectref → valuegets a fieldvalue of an objectobjectref, where the field is identified by field reference in the constant poolindex (index1 << 8 + index2)
getstaticb2index1, index2→ valuegets a static fieldvalue of a class, where the field is identified by field reference in the constant poolindex (index1 << 8 + index2)
gotoa7branchbyte1, branchbyte2[no change]goes to another instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
goto_wc8branchbyte1, branchbyte2, branchbyte3, branchbyte4[no change]goes to another instruction atbranchoffset (signed int constructed from unsigned bytesbranchbyte1 << 24 +branchbyte2 << 16 +branchbyte3 << 8 + branchbyte4)
I
i2b91value → resultconverts an int into a byte
i2c92value → resultconverts an int into a character
i2d87value → resultconverts an int into a double
i2f86value → resultconverts an int into a float
i2l85value → resultconverts an int into a long
i2s93value → resultconverts an int into a short
iadd60value1, value2 → resultadds two ints together
iaload2earrayref, index → valueloads an int from an array
iand7evalue1, value2 → resultperforms a logical and on two integers
iastore4farrayref, index, value →stores an int into an array
iconst_m102→ -1loads the int value -1 onto the stack
iconst_003→ 0loads the int value 0 onto the stack
iconst_104→ 1loads the int value 1 onto the stack
iconst_205→ 2loads the int value 2 onto the stack
iconst_306→ 3loads the int value 3 onto the stack
iconst_407→ 4loads the int value 4 onto the stack
iconst_508→ 5loads the int value 5 onto the stack
idiv6cvalue1, value2 → resultdivides two integers
if_acmpeqa5branchbyte1, branchbyte2value1, value2 →if references are equal, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_acmpnea6branchbyte1, branchbyte2value1, value2 →if references are not equal, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_icmpeq9fbranchbyte1, branchbyte2value1, value2 →if ints are equal, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_icmpnea0branchbyte1, branchbyte2value1, value2 →if ints are not equal, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_icmplta1branchbyte1, branchbyte2value1, value2 →ifvalue1 is less thanvalue2, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_icmpgea2branchbyte1, branchbyte2value1, value2 →ifvalue1 is greater than or equal tovalue2, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_icmpgta3branchbyte1, branchbyte2value1, value2 →ifvalue1 is greater thanvalue2, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
if_icmplea4branchbyte1, branchbyte2value1, value2 →ifvalue1 is less than or equal tovalue2, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifeq99branchbyte1, branchbyte2value →ifvalue is 0, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifne9abranchbyte1, branchbyte2value →ifvalue is not 0, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
iflt9bbranchbyte1, branchbyte2value →ifvalue is less than 0, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifge9cbranchbyte1, branchbyte2value →ifvalue is greater than or equal to 0, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifgt9dbranchbyte1, branchbyte2value →ifvalue is greater than 0, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifle9ebranchbyte1, branchbyte2value →ifvalue is less than or equal to 0, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifnonnullc7branchbyte1, branchbyte2value →ifvalue is not null, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
ifnullc6branchbyte1, branchbyte2value →ifvalue is null, branch to instruction atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2)
iinc84index, const[No change]increment local variable#index by signed byteconst
iload15index→ valueloads an intvalue from a variable#index
iload_01a→ valueloads an intvalue from variable 0
iload_11b→ valueloads an intvalue from variable 1
iload_21c→ valueloads an intvalue from variable 2
iload_31d→ valueloads an intvalue from variable 3
imul68value1, value2 → resultmultiply two integers
ineg74value → resultnegate int
instanceofc1indexbyte1, indexbyte2objectref → resultdetermines if an objectobjectref is of a given type, identified by class referenceindex in constant pool (indexbyte1 << 8 + indexbyte2)
invokedynamicbaindexbyte1, indexbyte2[arg1, arg2, ...] → resultinvokes a dynamic method and puts the result on the stack (might be void); the method is identified by method referenceindex in constant pool (indexbyte1 << 8 | indexbyte2)
invokeinterfaceb9indexbyte1, indexbyte2, count, 0objectref, [arg1, arg2, ...] →invokes an interface method on objectobjectref, where the interface method is identified by method referenceindex in constant pool (indexbyte1 << 8 + indexbyte2) andcount is the number of arguments to pop from the stack frame including the object on which the method is being called and must always be greater than or equal to 1
invokespecialb7indexbyte1, indexbyte2objectref, [arg1, arg2, ...] →invoke instance method on objectobjectref requiring special handling (instance initialization method, a private method, or a superclass method), where the method is identified by method referenceindex in constant pool (indexbyte1 << 8 + indexbyte2)
invokestaticb8indexbyte1, indexbyte2[arg1, arg2, ...] →invoke a static method, where the method is identified by method referenceindex in constant pool (indexbyte1 << 8 + indexbyte2)
invokevirtualb6indexbyte1, indexbyte2objectref, [arg1, arg2, ...] →invoke virtual method on objectobjectref, where the method is identified by method referenceindex in constant pool (indexbyte1 << 8 + indexbyte2)
ior80value1, value2 → resultlogical int or
irem70value1, value2 → resultlogical int remainder
ireturnacvalue → [empty]returns an integer from a method
ishl78value1, value2 → resultint shift left
ishr7avalue1, value2 → resultint shift right
istore36indexvalue →store intvalue into variable#index
istore_03bvalue →store intvalue into variable 0
istore_13cvalue →store intvalue into variable 1
istore_23dvalue →store intvalue into variable 2
istore_33evalue →store intvalue into variable 3
isub64value1, value2 → resultint subtract
iushr7cvalue1, value2 → resultint shift right
ixor82value1, value2 → resultint xor
J
jsra8branchbyte1, branchbyte2→ addressjump to subroutine atbranchoffset (signed short constructed from unsigned bytesbranchbyte1 << 8 + branchbyte2) and place the return address on the stack
jsr_wc9branchbyte1, branchbyte2, branchbyte3, branchbyte4→ addressjump to subroutine atbranchoffset (signed int constructed from unsigned bytesbranchbyte1 << 24 + branchbyte2 << 16 + branchbyte3 << 8 + branchbyte4) and place the return address on the stack
L
l2d8avalue → resultconverts a long to a double
l2f89value → resultconverts a long to a float
l2i88value → resultconverts a long to an int
ladd61value1, value2 → resultadd two longs
laload2farrayref, index → valueload a long from an array
land7fvalue1, value2 → resultbitwise and of two longs
lastore50arrayref, index, value →store a long to an array
lcmp94value1, value2 → resultcompares two longs values
lconst_009→ 0Lpushes the long 0 onto the stack
lconst_10a→ 1Lpushes the long 1 onto the stack
ldc12index→ valuepushes a constant#index from a constant pool (String, int, float or class type) onto the stack
ldc_w13indexbyte1, indexbyte2→ valuepushes a constant#index from a constant pool (String, int, float or class type) onto the stack (wideindex is constructed asindexbyte1 << 8 + indexbyte2)
ldc2_w14indexbyte1, indexbyte2→ valuepushes a constant#index from a constant pool (double or long) onto the stack (wideindex is constructed asindexbyte1 << 8 + indexbyte2)
ldiv6dvalue1, value2 → resultdivide two longs
lload16index→ valueload a long value from a local variable#index
lload_01e→ valueload a long value from a local variable 0
lload_11f→ valueload a long value from a local variable 1
lload_220→ valueload a long value from a local variable 2
lload_321→ valueload a long value from a local variable 3
lmul69value1, value2 → resultmultiplies two longs
lneg75value → resultnegates a long
lookupswitchab<0-3 bytes padding>, defaultbyte1, defaultbyte2, defaultbyte3, defaultbyte4, npairs1, npairs2, npairs3, npairs4, match-offset pairs...key →a target address is looked up from a table using a key and execution continues from the instruction at that address
lor81value1, value2 → resultbitwise or of two longs
lrem71value1, value2 → resultremainder of division of two longs
lreturnadvalue → [empty]returns a long value
lshl79value1, value2 → resultbitwise shift left of a longvalue1 byvalue2 positions
lshr7bvalue1, value2 → resultbitwise shift right of a longvalue1 byvalue2 positions
lstore37indexvalue →store a longvalue in a local variable#index
lstore_03fvalue →store a longvalue in a local variable 0
lstore_140value →store a longvalue in a local variable 1
lstore_241value →store a longvalue in a local variable 2
lstore_342value →store a longvalue in a local variable 3
lsub65value1, value2 → resultsubtract two longs
lushr7dvalue1, value2 → resultbitwise shift right of a longvalue1 byvalue2 positions, unsigned
lxor83value1, value2 → resultbitwise exclusive or of two longs
M
monitorenterc2objectref →enter monitor for object ("grab the lock" - start of synchronized() section)
monitorexitc3objectref →exit monitor for object ("release the lock" - end of synchronized() section)
multianewarrayc5indexbyte1, indexbyte2, dimensionscount1, [count2,...] → arrayrefcreate a new array ofdimensions dimensions with elements of type identified by class reference in constant poolindex (indexbyte1 << 8 + indexbyte2); the sizes of each dimension is identified bycount1, [count2, etc]
N
newbbindexbyte1, indexbyte2→ objectrefcreates new object of type identified by class reference in constant poolindex (indexbyte1 << 8 + indexbyte2)
newarraybcatypecount → arrayrefcreates new array withcount elements of primitive type identified byatype
nop00[No change]performs no operation
P
pop57value →discards the top value on the stack
pop258{value2, value1} →discards the top two values on the stack (or one value, if it is a double or long)
putfieldb5indexbyte1, indexbyte2objectref, value →set field tovalue in an objectobjectref, where the field is identified by a field referenceindex in constant pool (indexbyte1 << 8 + indexbyte2)
putstaticb3indexbyte1, indexbyte2value →set static field tovalue in a class, where the field is identified by a field referenceindex in constant pool (indexbyte1 << 8 + indexbyte2)
R
reta9index[No change]continue execution from address taken from a local variable#index (the asymmetry with jsr is intentional)
returnb1→ [empty]return void from method
S
saload35arrayref, index → valueload short from array
sastore56arrayref, index, value →store short to array
sipush11byte1, byte2→ valuepushes a signed integer (byte1 << 8 + byte2) onto the stack
swap5fvalue2, value1 → value1, value2swaps two top words on the stack (note that value1 and value2 must not be double or long)
T
W
Unused
impdep1fereserved for implementation-dependent operations within debuggers; should not appear in any class file
impdep2ffreserved for implementation-dependent operations within debuggers; should not appear in any class file
(no name)cb-fdthese values are currently unassigned for opcodes and are reserved for future use
xxxunusedxxxbathis opcode is reserved "for historical reasons"

References

[edit |edit source]
  1. Oracle's Java Virtual Machine Specification

External Links

[edit |edit source]


Invoking CJava Programming
Byte Code
Links
Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Byte_Code&oldid=4341688"
Category:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp