Java Reference
In-Depth Information
FIGURE 4.6 The stack frame for an invocation of Locals.foo().
There are several regions of scope defined within the method foo() :
First, the method itself defines a region of scope, where the formal parameters t and
u are declared.
The method body defines a nested region of scope, where the local variable v is
declared.
Nested within the body are two blocks, each of which defines a region of scope:
{ In the rst region, the local variables w and x are declared.
{ In the second region, the local variables y and z are declared.
Because these two regions are disjoint, their locally declared variables may share locations
on the run-time stack frame. Thus, as illustrated in Figure 4.6, w and y share the same
location, and x and z share the same location.
During analysis, a context is created for each of these regions of scope: a MethodContext
for that region defined by the method (and in which the formal parameters t and u are
declared) and a LocalContext for each of the blocks.
The code for analyzing a JMethodDeclaration performs four steps:
1. It creates a new MethodContext , whose surroundingContext points back to the
previous ClassContext .
2. The first stack frame offset is 0; but if this is an instance method, then offset 0 must
be allocated to this , and the nextOffset is incremented to 1.
3. The formal parameters are declared as local variables and allocated consecutive offsets
in the stack frame.
4. It analyzes the method's body.
The code is straightforward:
publicJASTanalyze(Contextcontext){
this.context=newMethodContext(context,returnType);
if(!isStatic){
//Offset0isusedtoaddr"this".
this.context.nextOffset();
 
Search WWH ::




Custom Search