Java Reference
In-Depth Information
While this visitor bears most of the responsibility of code generation,
some exceptional circumstances must be handled by the other visitors
described below.
LHSVisitor is responsible for generating code for the left-hand side of as-
signment statements. Recall from semantic analysis (Chapter 9) that
the meaning of a variable name changes across an assignment opera-
tor (e.g., = in Java). On the left side, a name means the address of the
variable; nearly everywhere else, a name means the value of the vari-
able. As another example, some languages (such as C
and Pascal)
include notation for reference parameters , which are transmitted using
their address instead of their value.
The LHSVisitorwill be directed to process portions of an AST in which
a name denotes its address instead of its value. Within such subtrees,
other namesmay refer to values, as dictated by a particular programming
language's semantics. The visitors call each other as needed to develop
the requisite addresses and values of names.
++
SignatureVisitor is responsible for visiting an AST subtree that corresponds
to a method definition or method invocation and developing the signa-
ture of the associatedmethod. The signature typically includes the name
of the method, a representation of the number and type of the method's
parameters, and the return type of the method.
This visitor is necessary when a method's signature is required, as the
code-generating visitor would otherwise generate code to invoke the
method. More detail about developing a method's type signature can be
found in Section 8.4.2 on page 294 and Section 8.7 on page 316.
The visitors facilitate organization of the code generator into sections of code
with related function. For eachAST construct of interest, we present algorithm-
style code in the form of a
method that illustrates one strategy for gen-
erating code for the construct. Exercises at the end of this chapter explore
alternative strategies.
At some point in each of the code-generation
visit
methods, actual code
must be emitted. The syntax and specification of such code depend on the
actual form of the VM code. To illustrate the principles of code generation
with concrete code sequences, we show the results of code generation using
instructions and directives from the Java Virtual Machine (JVM). Section 10.2
on page 397 describes the format of those instructions and provides resources
for further investigation of the JVM instruction set.
The coding convention within the visitors is to pass a visitor a node us-
ing the node's
visit
method. When a node accepts a visitor ,actionsare
performed that are appropriate to both the visitor and the node at hand. For
example, the action taken to visit a node's children at Marker 1 causes each
accept
 
Search WWH ::




Custom Search