Each time a new thread is started, the Java Virtual Machine assigns a java stack to it. The java stack saves the running status of the thread in frames. The Virtual Machine will only perform two operations on the java stack directly: the frame-based pressure stack or the outbound stack. When a thread calls a Java method, the VM will press a new stack in the java stack of the thread.
Stack frame:
Stack frames are composed of three parts: local variables, operand stacks, and stack data zones.
When a virtual machine calls a Java method, it obtains the local variables and the size of the operation data stack from the corresponding class information, and allocates the stack frame memory accordingly, and press it into the java stack.
1) local variables: The local variable area of the java stack frame is organized into an array counted from 0 in bytes.
2) operand Stack: Like a local variable, it is organized into an array of characters in length. Access through standard stack operations-pressure stacks and outbound stacks.
3) stack data zone: the java stack supports constant pool resolution, normal method return, and some information about the exception dispatching mechanism.
Example:
A Fibonacci sequenceCode, As follows:
Class Fibonacci {static void calcsequence () {long fibonum = 1; long a = 1; long B = 1; for (;) {fibonum = a + B; A = B; B = fibonum ;}}}
Script code:
0 lconst_1 // push long constant 1 1 lstore_0 // POP long into local vars 0 & 1: long a = 1; 2 lconst_1 // push long constant 1 3 lstore_2 // POP long into local vars 2 & 3: long B = 1; 4 lconst_1 // push long constant 1 5 lstore 4 // POP long into local vars 4 & 5: Long fibonum = 1; 7 lload_0 // push long from local vars 0 & 1 8 lload_2 // push long from local vars 2 & 3 9 Ladd // pop two longs, add them, push result10 lstore 4 // POP long into local vars 4 & 5: fibonum = A + B; 12 lload_2 // push long from local vars 2 & 313 lstore_0 // POP long into local vars 0 & 1: A = B; 14 lLoad 4 // push long from local vars 4 & 516 lstore_2 // POP long into local vars 2 & 3: B = fibonum; 17 goto 7 // jump back to offset 7: for (;;){}
Virtual Machine image:
1) initialization. Local variables: local variable. Operand Stack: operand stack.
2) allocate three variables: fibonum, A, and B. A local variable is a three-digit array.
3) lload_0: load the long type value from the local variable 0, and lload_2: load the long type value from the local variable 2.
4) Ladd: Execute the addition of int type.
5) istore 4: The Int type value pops up from the stack and stores it in a local variable at position 4.
References:
Deep Java Virtual Machine