The JVM stack frame operation and operand stack local variable table

Pre-knowledge

Primer

Design mode register

As far as we known or arm x86 instruction set, its operation based on the data registers are. For example, to perform two additions will need to operate these two numbers are then fed into the two registers to perform an addition operation, which is consistent with our programming language for cognitive, easier to understand.

Stack-based design patterns

The clock is stack-based design data stored in the stack, the stack will need to use the data from the stack, and executes the corresponding operations.

For example, a process executed a = b + c bytecode operand stack and local variable table of changes in the JVM as shown in FIG.

Stored in the local variable table A, b, c three local variables, b and c, respectively, the first stack
figure 1

The top of the stack to save the number two stack performs addition operation, the result to the top of the stack, the top of the stack after the stack number assigned to a
figure 2

A simple example

In the previous section we learned how to stack and local variable table is with the completion of an addition operation, we will list the local variable-depth study of this section.

How to view the local variable table?

We can see by the way decompile class file local variable table, but here is more recommended to use IDEA's jclasslib plug (direct search there) byte code view, because its design is more humane, more friendly.

Examples of the method of local variable table

We know that in an instance method, we can directly access member variables or functions instance, without the need to be referenced by this, how this is achieved?
Direct hands-on problem like this to write a test class, decompile it results naturally clear.
First, to a simple class

public class T {

    private int a = 0;

    public void add(int b,int c){
        a = b + c;
    }
}

Second, the class selected, and then select the showbytecode with jclasslib in view of the
image 3

Results are as follows:
Figure 4

Methods of selecting the add method of our attention, to observe the figure highlighted the part
Figure 5

The original, JVM in the compiled code, sneaking add in the local variable table in a this reference (cited saved Obviously this instance), this is our reason why member variable instance can access method, prove the following
Image 6

FIG brief explanation follows in section code:
0) aload_0 to refer to this reference stack (aload_0 upcoming local variable table index 0 is pressed onto the operand stack)
. 1) iload_1 parameter b stack (local variable in the the index is an integer of 1 to press the operand stack)
2) iload_2 parameter stack c

At this time, the contents of the stack are (0 top of the stack)
0.c
1.B
2.this

. 3) the iadd to stack two numbers are added, and save the results to the top of the stack, the stack contents at this time is
0.b + C
1.this

. 4). PutField two values of the stack a stack, a first value (b + c) assigning a corresponding member variable to the second value (this) (yes, yes even have to perform a two-assignment Ci pop operations)
PutField is described below (in FIG highlighted Notes):
putfield
address: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5 .putfield

in conclusion

基于栈的指令集系统可以很方便的做到平台无关性(x86、arm),但也降低了性能,这也是为啥Java性能比C低原因。(操作寄存器快,还是操作栈快?哈哈)

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160190.htm