Java Reference
In-Depth Information
Code Source Comments
5 a=5 Push 5 on stack
sa Pop the stack, storing (s) the popped value in
register a
0k Reset precision to integer
la b=a+3.2 Load (l) register a, pushing its value on stack
5k
Set precision to float
3.2
Push 3.2 on stack
+
Add: 5 and 3.2 are popped from the stack and
their sum is pushed
sb
Pop the stack, storing the result in register b
0k
Reset precision to integer
lb pb
Push the value of the b register
p
Print the top-of-stack value
si
Pop the stack by storing into the i register
Figure 2.15: Code generated for the AST shown in Figure 2.9.
Even in this ad hoc code generator, one can see a principled approach. The
code sequences triggered by various AST nodes dovetail to carry out the
instructions of the input program. Although the task of code generation for
real programming languages and targets is more complex, the theme still holds
that pieces of individual code generation contribute to a larger e
ff
ect.
This finishes our tour of a compiler for the ac language. While each of
the phases becomes more involved as we move toward working with real pro-
gramming languages, the spirit of each phase remains the same. In the ensuing
chapters, we discuss how to automatemany of the tasks described in this chap-
ter. We develop skills necessary to craft a compiler's phases to accommodate
issues that arise when working with real programming languages.
 
Search WWH ::




Custom Search