Java Reference
In-Depth Information
While many nodes fall into the Computingcategory, the strategy for generating
code for such nodes can be simply stated as follows:
Marker 28 calls for the generation of code for each child (processed left
to right) of the Computing node. The generated code evaluates each of
the operands required to compute the operation for node n .
After evaluation, the operands' results can be found in the resource
allocated when they were visited. For a zero-address target, such as the
JVM, the operands' results are available at the TOS.
Marker 29 sets aside space for the value computed by node n .Forsome
VMs, the location may be implicit, such as the TOS or in a predetermined
register. For example, the JVM obtains an instruction's operands from
the TOS and leaves the result of the instruction (if any) on TOS. Other
targets require management of registers or other local storage.
Marker 30 emits code to compute the value of the expression associated
with AST node n . That code will reference the values of n 's operands,
each already evaluated (recursively) into its own result-holding local
storage. The code generated on behalf of node n 's operation may be
a single instruction, a sequence of instructions, or an invocation of a
runtime method to realize the operation.
AST
JVM Instructions
ComputeIsh
plus
;
Code emitted for left child
iload 5
n
;
Code emitted for right child
sipush 250
;
Code for the Computing node
iadd
ConstantIsh
LocalReferencing
a
250
The box above shows an AST for the binary plus node. The code generated for
the JVMby visiting each of n 's two children leaves a value on the stack for each
child. The iadd instruction pops the top two values, computes their sum, and
pushes the result onto the stack. The type of instruction, integer addition, is
determined by the type associated with the Computingnode during semantic
analysis (Section 8.8 on page 323).
 
Search WWH ::




Custom Search