

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


IR Taxonomy
IRs fall into three organizational categories:
Graphical IRs
Parse trees are graphs that represent source-code form of the program. The structure of the tree corresponds to the syntax of the source code. Parse trees are used primarily in discussion of parsing and in attribute grammar systems where they are the primary IR In most other applications, compilers use one of the more concise alternatives. An abstract syntax tree (AST) retains the essential structure of the parse tree but eliminates extraneous nodes. Here, for example, is the AST for the expression a = b-c + b-c.** Notice how all derivation related information has been removed.
ASTs have been used in many practical compiler systems such as source-to-source systems, automatic parallelization tools, pretty-printing etc.
AST is more concise than a parse tree. It faithfully retains the structure of the original source code. Consider the AST for x2+x2y*
a (^) +
b -**
c
b -
c
x (^2)
*** y x 2**
The AST contains two distinct copies of x2.* A directed acyclic graph (DAG) is a contraction of the AST that avoids duplication.
If the value of x does not change between uses of x2* , the compiler can generate code that evaluates the subtree once and uses the result twice.
The task of building AST fits neatly into an ad hoc-syntax-directed translation scheme. Assume that the compiler has routines mknode and mkleaf for creating tree nodes. The following rules can be attached to the expression grammar to create AST.
Production Semantic Rule E โ E1 + E2 E.nptr = mknode(โ+โ, E1.nptr, E2.nptr) E โ E1 โ E2 E.nptr = mknode(โโโ, E1.nptr, E2.nptr) E โ โ E1 E.nptr = mknode(โโโ, E1.nptr) E โ ( E1 ) E.nptr = E1.nptr E โ num E.nptr = mkleaf(โnumโ, num.val)
The following table shows the same rules using YACC syntax.
Production Semantic Rule (yacc) E โ E1 + E2 $$.nptr = mknode(โ+โ, $1.nptr, $3.nptr)
E โ E1 โ E2 $$.nptr = mknode(โโโ, $1.nptr, $3.nptr) E โ โ E1 $$.nptr = mknode(โโโ, $1.nptr) E โ ( E1 ) $$.nptr = $1.nptr E โ num $$.nptr = mkleaf(โnumโ, $1.val)
We will use another IR, called three-address code , for actual code generation The semantic rules for generating three-address code for common programming languages constructs are similar to those for AST.
Linear IRs
The alternative to graphical IR is a linear IR. An assembly-language program is a form of linear code. It consists of a sequence of instructions that execute in order of appearance
*** y x 2**