Java Reference
In-Depth Information
Type analysis in Figure 2.12 on page 49 attempts to reconcile the type
of the subtrees beneath a plus node by finding the least-general type t
that is suitable for the subtrees. The values used by the plus node are
converted to type t , and the result of the addition is also of type t .
Type analysis of an assign node in Figure 2.12 on page 49 attempts to
coerce the right subtree to match the type of the left subtree, so that the
valuetobestoredisappropriateforthe variable receiving that value.
The distinction between left and right values di
ersaswellforcode
generation in Figure 2.14 on page 52. Within a plus tree, identifiers are
assumed to represent their right values. For an assign node, identifiers
within the right subtree are right values, but the target of the assignment
is a left value.
ff
On the other hand, some nodes are treated similarly by almost every phase:
The plus and minus nodes of the ac language in Chapter 2 can be treated
identically except for the arithmetic operation performed on their sub-
trees.
Treatment of the if construct in most programming languages is very
similar to the ternary operator (e.g., ? in C and Java), even though the
specifics and nature of their syntax is very di
ff
erent.
It turns out there is no single node class hierarchy that is well suited to all
phases of compilation. While one hierarchy may be well suited to semantic
analysis, code generation or optimization phases becomes harder to write
when saddled with a class hierarchy that favors some other phase.
As a result, the node class hierarchy is relatively flat. Node management
is placed in a common superclass, say AbstractNode.Eachtypeofnode(if,
plus, etc.) is then a simple extension of AbstractNodewith enough construct-
specific functionality to allow the phases to do their work. Superclasses can be
introduced to simplify AST construction, by factoring common code between
node types. However, the resulting class hierarchy should not necessarily be
considered as a basis for designing the compiler's phases.
7.7.2 Visitor Pattern
The next issue to consider is the crafting of a compiler phase, in terms of
the classes available to host such code, and the node organization established
thus far. ASTs for Languages like Java contain
50 node types, and compilers
like the GNU Compiler Collection (GCC) have
200 phases. To manage
the relatively large space of potential phase and node interactions, modern
software engineering principles dictate that the code for a phase should be
 
 
Search WWH ::




Custom Search