Code Generation for ExpressionsTopAttribute  Grammars as a framework for semantic processingLowering Expression Sub-trees

Lowering Expression Sub-trees

  1. We can solidify our understanding of the run-time layout of memory (and get a head start on code-generation) by considering how to transform the trees produced by the parser to represent references to variables into trees that explicitly describe the address calculations and memory references involved.

  2. A reference to an identifier as a variable will be represented in the syntax tree by an Nrefvar node with an Nident node for the variable's name as its child.

  3. To actually access a variable in memory at run-time the hardware must add the displacement to the variable to the frame pointer for the method or pointer to the object in which the variable is stored.

  4. This leads to the following simple transformation for simple variable references:

  5. Similar transformations can be appled to more complex variables. Nsubs nodes can be transformed into trees that describe the subscript calculations required. In such a transformed tree:

  6. There will be several advantages to making these transformations. Because they replace special purpose tree nodes (Nident and Nsubs) with nodes types that would already be present in the tree (Nplus, Ntimes), they reduce the number of cases to be handled by later phases (code generation, optimization).

  7. There is a "hook" included in the format of the Nrefvar node to support later optimizations:


Computer Science 434
Department of Computer Science
Williams College

 Code Generation for ExpressionsTopAttribute  Grammars as a framework for semantic processingLowering Expression Sub-trees