On the JVM, bytecode

Talk about the JVM, recently cracked jar in the study, have experience in this area to guide us trouble thinking ..

reference

Reference 1
command organizing

Common commands

1
2
3

Decompile class file `` view all the information
javap -verbose [class]

Local variable type

Special character representation Types of description
c char char type
i int int type
l long long type
s short short type
b byte byte type
f float float type
with boolean Boolean
a reference Quote

Methods Information

1
2
3
4
5
L1
LOCALVARIABLE this LMain; L0 L1 0
LOCALVARIABLE a I L0 L1 1
MAXSTACK = 0
MAXLOCALS = 2

Local variable parameters LOCALVARIABLE
local table size MAXLOCALS
operand stack size MAXSTACK

instruction

Operation Instructions

Arithmetic instructions for two 操作数栈values of a particular operation is performed, and the result is stored back to 操作栈the top.

  • Addition instruction: iadd, ladd, fadd, dadd
  • Subtraction instruction: isub, lsub, fsub, dsub
  • Multiply instructions: imul, lmul, fmul, dmul
  • Division instructions: idiv, ldiv, fdiv, ddiv
  • Remainder instruction: irem, lrem, frem, drem
  • Invert instructions: ineg, leng, fneg, dneg
  • Motion commands: ishl, ishr, iushr, lshl, lshr, lushr
  • Bitwise or instructions: ior, lor
  • Bitwise AND instruction: iand, land
  • Bitwise XOR instruction: ixor, lxor
  • Increment local variable instruction: iinc
  • Compare instruction: dcmpg, dcmpl, fcmpg, fcmpl, lcmp

Load and store instructions

For 局部变量表and 操作数栈transferred back and forth between the

  • A local variable is loaded into the instruction operand stack comprising: iload, iload_ <n>, lload, lload_ <n>, float, fload_ <n>, dload, dload_ <n>, aload, aload_ <n>.
  • Storing a value from the stack into local variable target operand instructions: istore, istore_ <n>, lstore, lstore_ <n>, fstore, fstore_ <n>, dstore, dstore_ <n>, astore, astore_ <n>
  • The constant is loaded into the operand stack instructions: bipush, sipush, ldc, ldc_w, ldc2_w, aconst_null, iconst_ml, iconst_ <i>, lconst_ <l>, fconst_ <f>, dconst_ <d>
  • Access the local variable table index instruction: wide
    portion of instructions ending angle brackets represent a set of instructions, such as iload_ <i>, it represents iload_0, iload_1 the like, these are the set of instructions with a general-purpose instruction operand.

Operation Instructions

Of two 操作数栈values on a specific operation is performed, and the result is stored back to 操作栈the top

  • Addition instruction: iadd, ladd, fadd, dadd
  • Subtraction instruction: isub, lsub, fsub, dsub
  • Multiply instructions: imul, lmul, fmul, dmul
  • Division instructions: idiv, ldiv, fdiv, ddiv
  • Remainder instruction: irem, lrem, frem, drem
  • Invert instructions: ineg, leng, fneg, dneg
  • Motion commands: ishl, ishr, iushr, lshl, lshr, lushr
  • Bitwise or instructions: ior, lor
  • Bitwise AND instruction: iand, land
  • Bitwise XOR instruction: ixor, lxor
  • Increment local variable instruction: iinc
  • Compare instruction: dcmpg, dcmpl, fcmpg, fcmpl, lcmp

Type Conversion

The two Java virtual machine numeric type conversion

JVM support a wide conversion types (small type conversion to a wide range of types):

  • int type to long, float, double type
  • long type to float, double type
  • float to double type

Object creation and operation

Although class instances and arrays are objects, Java virtual machines that use different bytecode instructions and the operation for creating class instances and arrays.

  • Command to create an instance of: new
  • Create an array of command: newarray, anewarray, multianewarray
  • Access field of the instruction: getfield, putfield, getstatic, putstatic
  • The array element is loaded into the operand stack instructions: baload, caload, saload, iaload, laload, faload, daload, aaload
  • The operand stack is the value stored in the array element is performed: bastore, castore, castore, sastore, iastore, fastore, dastore, aastore
  • Instruction fetch array length: arraylength
  • Examples of type checking instructions: instanceof, checkcast

Management instruction operand stack

Direct operation instruction operand stack: pop, pop2, dup, dup2, dup_x1, dup2_x1, dup_x2, dup2_x2 and swap

NOTE: onto stack (DUP) / pop stack (POP)

Control transfer instruction

  • Conditional branch: ifeq, if a large column  talk JVM, bytecode lt, ifle, ifne, ifgt, ifge, ifnull, ifnotnull, if_cmpeq, if_icmpne, if_icmlt, if_icmpgt etc.
  • Complex conditional branch: tableswitch, lookupswitch
  • Unconditional branch: goto, goto_w, jsr, jsr_w, ret

Method call and return instructions

  • invokevirtual instruction: call object instance method, is dispatched (assigned virtual machine) based on the actual type of the object.
  • invokeinterface instruction: call interface method at runtime object that implements this interface to search for a method of finding the right method call.
  • invokespecial: calls require special treatment instance method, instance initialization method comprising, parent private methods and methods
  • invokestatic: call the class method (static)
  • Method return instruction is a return type distinguishing value, comprising ireturn (return value is boolean, byte, char, short, and int), lreturn, freturn, drturn and areturn, another return for void method, instance initialization method, classes, and i interface class initialization method used.

Synchronize

Eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// class version 52.0 (52)
// access flags 0x21
public class Main {

// compiled from: Main.java

// access flags 0x11
public final I simpleField = 100

// access flags 0x0
<init>()V
L0
LINENUMBER 12 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
L1
LINENUMBER 10 L1
ALOAD 0
BIPUSH 100
PUTFIELD Main.simpleField : I
L2
LINENUMBER 13 L2
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
BIPUSH 100
INVOKEVIRTUAL java/io/PrintStream.print (I)V
L3
LINENUMBER 14 L3
RETURN
L4
LOCALVARIABLE this LMain; L0 L4 0
MAXSTACK = 2
MAXLOCALS = 1

// access flags 0x9
public static main([Ljava/lang/String;)V
L0
LINENUMBER 16 L0
NEW Main
DUP
INVOKESPECIAL Main.<init> ()V
ASTORE 1
L1
LINENUMBER 17 L1
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
LDC "-->"
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
L2
LINENUMBER 19 L2
RETURN
L3
LOCALVARIABLE args [Ljava/lang/String; L0 L3 0
LOCALVARIABLE main LMain; L1 L3 1
MAXSTACK = 2
MAXLOCALS = 2

// access flags 0x21
public synchronized a(I)V throws java/lang/Exception
L0
LINENUMBER 23 L0
RETURN
L1
LOCALVARIABLE this LMain; L0 L1 0
LOCALVARIABLE a I L0 L1 1
MAXSTACK = 0
MAXLOCALS = 2
}

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11692153.html