Java Reference
In-Depth Information
11
Code Generation for a
Virtual Machine
In this chapter, we take a final step in program translation by traversing an
abstract syntax tree (AST) and generating a form of code that is suitable for a
virtual machine . The construction of an AST (Chapter 7) and its subsequent
semantic processing (Chapters 8 and 9) have developed all of the information
that is necessary to translate a source program into some form of interpretable
or executable code. The AST serves well for expressing the structure and the
meaning of a source program. However, its design is purposefully abstract,
and thus independent of any particular architecture specification. Moreover,
the AST nicely represents the nested structure of programswritten in amodern
programming language, while the instructions executed by most architectures
are more linear in nature.
In Chapters 5 and 6, parsing techniques are presented that check an input
program's syntax based on a programming language's grammar. While the
grammar provides an automatic structure for regulating the parser's activity,
the translation of the source program into a suitable AST requires actions
that are inserted by hand. In this chapter, code generation is essentially the
inverse of the parsing process. A program's AST provides a structure that
can be traversed automatically, but the actions required to synthesize code are
formulated by hand.
Code-generation issues are discussed here and in Chapter 13, and the
di
ff
erences in treatment are as follows:
417
 
 
Search WWH ::




Custom Search