Java Reference
In-Depth Information
The predicate and loop body are shown abstractly in the above box, as each
could involve a nontrivial amount of generated code. However, the visitor is
recursively applied to the root of each of the subtrees, so that the constructs
already considered can trigger proper code generation.
11.4 The LHSVisitor
The visitors presented thus far are value-oriented , in the sense that when they
encounter a name in a program, they generate code to compute the value of that
name. In some cases, such as on the left-hand side of an assignment statement,
a name denotes its location instead of its value.
However, it is not the the case that all names appearing left of anassignment
operator denote locations and stores. For example, consider the assignment
statement a.b.c = 5,wherea, b,andc are object references. The only actual
store is to field c. The references to a and b are loads, not stores.
The code sketch for the LHSVisitor is shown in Figure 11.3. The con-
structor for the LHSVisitor is passed the instance of a MethodBodyVisitor that
is currently processing the AST. That instance can be used in the LHSVisitor
whenever a value needs to be generated. We use the active instance of a
MethodBodyVisitor rather than a new one, so that the state of the code gen-
erator is available. For example, the active instance of a MethodBodyVisitor
contains a reference to the postlude label for the current method. That label is
necessary to facilitate a clean method-return in the generated code.
For Java and the JVM, there are three cases that must be handled by the
LHSVisitor in terms of the type of AST node that can appear as the target of an
assignment statement, and these are discussed below.
11.4.1 Local References
/
Visitor code for Marker 54
/
procedure visit
( LocalReferencing n )
( new LocalStore( n . getType(), n . getLocation()))
call
set
S
tore
58
end
An assignment to a local name requires no further computation. Marker 58
sets the store instruction to be a local store, parameterized by the type of the
instruction (int, double, object reference, etc.) and the location associated
with the local reference. Recall that Marker 34 retrieves the store instruction
from the LHSVisitor after the left-hand side has been processed.
 
 
 
Search WWH ::




Custom Search