Java Reference
In-Depth Information
arguments that correspond to the methods formal parameters, and all local variables in the
method body are allocated space in this stack frame. Also, all the method computations,
like that illustrated in Figure D.1, are carried out in the same stack frame. Computations
take place in the space above the arguments and local variables.
For example, consider the following instance method, add() .
intadd(intx,inty){
intz;
z=x+y;
returnz;
}
Now, say add() is a method defined in a class named Foo , and further assume that f is
a variable of type Foo . Consider the message expression
f.add(2,3);
When add() is invoked, a stack frame like that illustrated in Figure D.2 is pushed onto
the run-time stack.
FIGURE D.2 The stack frame for an invocation of add() .
Because add() is an instance method, the object itself, that is, this , must be passed as
an implicit argument in the method invocation; so this occupies the first location in the
stack frame at offset 0. Then the actual parameter values 2 and 3, for formal parameters
x and y , occupy the next two locations at offsets 1 and 2, respectively. The local variable
z is allocated space for its value at offset 3. Finally, two locations are allocated above the
parameters and local variable for the computation.
Here is a symbolic version 3 of the code produced for add() by our j-- compiler.
intadd(int,int);
Code:
Stack=2,Locals=4,Args_size=3
0:iload_1
1:iload_2
2:iadd
3:istore_3
3 Produced using javap-vFoo .
 
Search WWH ::




Custom Search