java bytecode I ++ ++ j

Operand stack

During operand stack for execution of bytecode instructions, just as a general purpose register in the CPU. Most JVM bytecode popping the respective operation, stack, copy, exchange, or perform the operation, so that various data of production and consumption. Thus, in the byte code, the command to move frequently between the value of the local variable table and the operand stack. For example, a simple interaction results in variable initialization operand stack where two bytecodes impact.

int i;

The following bytecode compiled from:

0: iconst_0 // 0 will push to the top of the operand stack.

1: istore_1 // pop top stack operands from and stored to a local variable

 

Integer JVM byte code of the push instruction (iconst, bipush, sipush, ldc)

java in shaping the common length indicates the range:

byte ranges from -128 to 127, 1 byte (-2 to 7 th to 7 th -12) 
Short ranges from -32768 to 32767, 2 bytes (- 15 th to 15 th -1) 2 2
int ranges (-2147483648 to 2147483647), power 31 4 bytes (31 - 2 to the power of 2 -1)
Long a in the range (~ -9,223,372,036,854,774,808 9223372036854774807), 8 bytes (63 -2 to 63 th -12 th) of

When the value of int -1 to 5: int ranging from 0 to 5 JVM uses iconst_0, iconst_1, iconst_2, iconst_3, iconst_4, iconst_5 the constant instruction onto the stack, using iconst_m1 the constant instruction value onto the stack -1 the

value of -128 to 127 using bipush instruction (bipush 127),

the value -32768 to 32767 using sipush instruction (sipush 32767)
The value -2147483648 2147483647 ~ employed ldc instruction (ldc # 2; // int 2147483647 value obtained from the constant pool). 

java stack

Thread - the thread's stack, the stack frame - method, thread execution method is to stack frame from the stack, the stack of the process.
Stack frame: three parts: local variables (variables + parameters in the method), and other data operand stack

①int i = 0;
    i = i++;
②int i = 0;
    i = ++i;
③int i = 0;
    int j = 0;
    j = i++ + i++; 
④ int i = 0;
     int j = 0;
     j = i++ + i++ + i++;

Bytecode

Stack

before->after

Description

iconst_0

->0

Loads the int value 0 onto the stack

istore_1

value->

Store int value into variable 1

istore_2

value->

Store int value into variable 2

iinc

No change

Increment local variable #index by signed byte const

iload_1

->value

Loads an int value from variable 1

iadd

value 1,value 2->result

Adds 2 ints together

 















Description Two caveats:
①iinc is operating parameter, but this is ignored, abbreviated as Iinc, this operation corresponds to from the add operation, and the operation does not change any stack;
②iadd after the operation to retain only result in result in a stack.
 
Next, the main program is a four byte code:
①iconst_0        ②iconst_0        ③iconst_0       ④iconst_0
    istore_1 istore_1 istore_1 istore_1
    iload_1             iinc 1,1              iconst_0          iconst_0
    iinc 1,1             iload_1              istore_2           istore_2
    istore_1            istore_1            iload_1             iload_1
                                                      iinc 1,1 1,1 iinc
                                                      iload_1             iload_1
                                                      iinc 1,1 1,1 iinc
                                                      iadd iadd
                                                      istore_2            iload_1
                                                                               iinc 1.1
                                                                               iadd
                                                                               istore_2
 

Guess you like

Origin www.cnblogs.com/erinchen/p/11610323.html