Docsity

IR Taxonomy - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:5-90
L
Le
ec
ct
tu
ur
re
e
3
35
5
IR Taxonomy
IRs fall into three organizational categories:
1. Graphical IRs encode the compilerโ€™s knowledge in a graph.
2. Linear IRs resemble pseudo-code for some abstract machine
3. Hybrid IRs combine elements of both graphical (structural) and linear IRs
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 x*2+x*2*y
=
a +
*
b
-
c
*
b
-
c
+
*
x 2
*
y
*
2
x
pf3

Partial preview of the text

Download IR Taxonomy - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Le Leccttuurree 3 355

IR Taxonomy

IRs fall into three organizational categories:

  1. Graphical IRs encode the compilerโ€™s knowledge in a graph.
  2. Linear IRs resemble pseudo-code for some abstract machine
  3. Hybrid IRs combine elements of both graphical (structural) and linear IRs

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**