Lecture 08
Lecture 08
12/8/2024 1
Summary of Front End
Front
Abstract Syntax Tree w/Attributes End
Intermediate-code Generator
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 2
Component-Based Approach to Building
Compilers
Source program Source program
in Language-1 in Language-2
Intermediate-code Optimizer
4
Intermediate Representation (IR)
•Why IR ?
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 5
Without IR
C SPARC
Pascal HP PA
FORTRAN x86
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 6
With IR
C SPARC
Pascal HP PA
IR
FORTRAN x86
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 7
With IR
Pascal Common ?
IR Backend
FORTRAN
C++
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 8
Advantages of Using an Intermediate
Language
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 9
Issues in Designing an IR
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 10
Issues in Designing an IR
a + a*(b-c) (b-c) * d
a * (b-c)
(b-c)
Value Number Method for Constructing
DAG’s
*
A
B C
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 19
Postfix Notation (PN)
A mathematical notation wherein every
operator follows all of its operands.
Examples:
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 20
Postfix Notation (PN) – Cont’d
Form Rules:
1. If E is a variable/constant, the PN of E is E
itself
2. If E is an expression of the form E1 op E2, the
PN of E is E1’E2’op (E1’ and E2’ are the PN of E1
and E2, respectively.)
3. If E is a parenthesized expression of form
(E1), the PN of E is the same as the PN of E1.
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 21
Three Address Code
• In three-address code, there is at most
one operator on the right side of an
instruction;
• That is, no built-up arithmetic expressions
are permitted.
• source-language expression: x + y * z
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 23
Three Address Code
The general form
x := y op z
x,y,and z are names, constants,
compiler-generated temporaries
op stands for any operator such as +,-,…
x*5-y might be translated as
t1 := x * 5
t2 := t1 - y
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 24
Syntax-Directed Translation Into
Three-Address
Temporary
• In general, when generating three-address
statements, the compiler has to create new temporary
variables (temporaries) as needed.
• We use a function newtemp( ) that returns a new
temporary each time it is called.
• Recall Topic-2: when talking about this topic
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 25
Syntax-Directed Translation Into
Three-Address
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 26
Syntax tree vs. Three address code
12/8/2024 \course\cpeg621-10F\Topic-1a.ppt 27
DAG vs. Three address code