Java Reference
In-Depth Information
/
Visitor code for Marker
3
/
procedure
visit
(MethodDeclaring
md
)
sigVisitor
←
new
SignatureVisitor()
call
md
.
accept(
sigVisitor
)
signature
←
sigVisitor
.
getSignature()
19
call
emit
M
ethod
N
ame
(
signature
)
20
(
md
.
getLocals(),
md
.
getStack())
call
emit
M
ethod
A
lloc
21
postludeLabel
← gen
L
abel
()
bodyVisitor
←
new
MethodBodyVisitor(
postludeLabel
)
md
.
getBody()
.
accept(
bodyVisitor
)
22
call
emit
M
ethod
P
ostlude
(
postludeLabel
)
23
end
Method signature:
The code beginning atMarker
19
runs the SignatureVisitor
on node
md
to develop its declared signature. The signature includes the
name of themethod, the types of its parameters, and its return type. Such
information is needed to define or call the method represented by
md
.
Method prelude:
Before code is generated for the contents of a method, the
compiler must generate the method's
prelude
. Thepreludecodees-
tablishes a runtime context for executing the method. Marker
20
be-
gins the prelude by emitting code based on the method's full signature.
Marker
21
generates code that allocates space needed by the method
during its execution. Such space typically includes the method's
local
variables
and stack space for method calls and intermediate computa-
tions. The space required for local variables can be determined from
the method's
symbol table
as discussed in Chapter 8. The space re-
quired for the runtime stack depends on the method's operations and
how deeply they push operands before popping them. Java's language
definition contains rules that ensure that the stack is used consistently
and predictably within a method. A simple form of data flow analysis
(Exercise 67 on page 649) can determine the maximum stack depth for a
method.
Method body code:
We are now in a position to generate the main portion of
a method's code. The execution of certain constructs or exceptions may
cause the code at some point to conclude the method's execution. We
therefore generate a label for the method's
postlude
code and pass that to
the visitor that generates the method body code. This allows the visitor
to jump to the postlude sequence if the method should cease execution.