11.1 Preparation for Code Generation

11.2 Generation of Directives

11.3 Simple Code Generation from AST's

11.4 Standard Code Generation Strategies

11.5 Code Generator Generators

11.6 Register Allocation

11.7 A Better Code Generation Algorithm from AST's

11.8 Code Generation from DAG's

11.9 Retargetable Code Generation

11.10 Peephole Optimization

11.11 Summary

Web References

Exercises

11.8 Code Generation from DAG's

The optimal code generation algorithm is an NP-complete problem. Directed acyclic graphs allow a code Generation heuristic:

    Put out code for each node immediately after code for its children has been emitted as far as possible because then the results are more apt to still be available, say in a register.

To prepare the list of DAG nodes to compute (that is, the list for which code is to be emitted), start at the root of the right-most subtree. Put this node on the list, L, and continue by adding a left-most node to the list after all its parents are already on the list. Then generate code for the nodes in L by starting at the end of L and proceeding to the beginning.

    Example 10
Generating code from a DAG

Thus, we would put out code for node 8 first, then node 9, etc.

Another way to put out good code from DAG's is to break the DAG up into trees and to use a code generation algorithm which is optimal on trees. (Of course, it takes time to break up the DAG.)

    Example 11
Breaking DAG's into trees