So let's go ahead and look at the contents of a stack frame. 2
Here's an illustration of the stack frame of a method. 3
It contains three things. 4
A local with Eberts array and Upperton stack and also a reference to that runtime constant of the class 5
of the current method. 6
Look already was array as you may guess is an array that stores all the local variables and method since 7
Midhat parameters are also local variables. 8
They also get stored here. 9
In fact method barometers are stored before local variables. 10
And the idea if this method is an instance method then the element in the zero next will be a reference 11
. 12
That is a reference to the current object on the heap. 13
And this is needed for accessing any instance do from within them either. 14
But if the method is a static method then the zero element would be the first method parameter as a 15
method to some sort before local variables that are declared in the body. 16
I pop from local variables data that I read will also include the return address which is nothing but 17
the memory address of the next instruction and the calling method. 18
So if this method completes its execution successfully without any errors then the program counter is 19
updated with this return address. 20
Then GBM execution engine will exit give the instruction at that memory address. 21
Now it's coming to open in-stock. 22
It is similar to general purpose registers. 23
In native you. 24
It is a central focus of JVM instruction set as values are generally pushed onto it before they are 25
used. 26
Note is that we are using the word push here. 27
And that's because our patent stack is a stack most byte code instructions involved moving values to 28
and fro between the local variables array and the output in stack are put in stack is also used to receive 29
any values that are written by methods invoked from the current method. 30
Here's an example to illustrate how operant stack is used on the left we have a simple code fragment 31
which initializes variables Y and Z and then assigns some to the variable x on the right. 32
We have the corresponding bytecode instructions. 33
Let's not have you want this bytecode instructions mean the first two instructions are generated due 34
to the first statement and y equals to 1. 35
The first instruction I Konst underscore one simply pushes the constant value one on the top of the 36
open in-stock. 37
So the suffix one indicates a constant value. 38
One second instruction I store underscore one BOP's the value on the top of the operand stuck here. 39
I and I start indicates the value to be part of any data type. 40
The bottom line is it's stored in the local variables array at the end it's position 1. 41
So suffixed one indicates the next position and the local variables are in. 42
Next the third and fourth instructions are similar to forestall instructions and the corresponding The 43
second statement in is equal to three. 44
So the third instruction I cost underscore three pushes constant three on the opera in-stock. 45
When the fourth instruction I start underscore to boost the value on the top of the operand stack and 46
store it in the local variables array at index position 2. 47
At this point the all put in stack is empty. 48
When the local variables array has values 1 and 3 No the third statement is X equals Y policy in order 49
to perform this operation. 50
Venia to transfer the values from the local variables are you on to the operand stack. 51
So the next two instructions I Laura underscore one. 52
And I Lord underskirt boo boo. 53
Exactly that I should underscore one takes the value add and mix position 1 in the local variables array 54
and pushes it onto the top of the operand stock. 55
Similarly I Lorena's go too big to evaluate inexpertly shouldn't do in the local variables array and 56
pushes it onto the top of the operand stack. 57
Next the instruction I add Boster and values on top of the operand stack and adds them on pushes the 58
result back onto the upper end stack. 59
Once again I and I add refers to integrate R-Type that is a boo or loose on the operand stack or off 60
and did type since the sum is assigned to the variable x. 61
We have the instruction eyes toward underskirt 3 which we know pops up and right now on the top of the 62
operand stack which basically is the sum unstarted at the end ex-politician 3 in the local variables 63
. 64
So store instruction is basically moving the value from the open and stack to the local variables array 65
. 66
Well the load instruction is doing the exact opposite. 67
So with that the local variables are the highest of value corresponding to Y and position 1 the value 68
of Z at index 2. 69
And finally the value of x at index 3 no index zero in this guess is reserved for the method parameter 70
. 71
And that's because this particular code fragment was part of the main method which we know has only 72
a single parameter which is a String Theory. 73
So if the code was inside some other static method that did not have any method parameters then our 74
variable values y z on X would have been stored in expositions 0 1 and 2 respectively. 75
So as you can see GBM uses are put in stack just like the way a CPO uses registers. 76
So everything is done via the open stack. 77
That's about stack frame. 78
And we could also review these few simple bytecode instructions next to a very simple example. 79
We will also explore a few other important bytecode instructions