0% found this document useful (0 votes)
4 views

Lecture 209

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 209

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Compiler

Construction
Three-Address Code
 Three-address code is
attractive for several reasons:

2
Three-Address Code
 absence of destructive operators
gives the compiler freedom to
reuse names and values
 three-address code is reasonably
compact: operations are 1 to 2
bytes; addresses are 4 bytes

3
Three-Address Code
 absence of destructive operators
gives the compiler freedom to
reuse names and values
 three-address code is reasonably
compact: operations are 1 to 2
bytes; addresses are 4 bytes

4
Three-Address Code
 many modern processors
implement three-address
operations, a three-address code
models their properties well

5
Syntax-directed Translation
 We now consider syntax-
directed translation schemes
using three-address code for
various programming
constructs
 We start with the assignment
statement
6
Syntax-directed Translation
 We now consider syntax-
directed translation schemes
using three-address code for
various programming
constructs
 We start with the assignment
statement
7
Production translation scheme
S → id = E { p = lookup(id.name);
emit( p, ‘=’, E.place); }

E → E1 + E2 { E.place = newtemp();
emit( E.place, ‘=’, E1.place,
‘+’, E2.place); }
E → E1  E2 { E.place = newtemp();
emit( E.place, ‘=’, E1.place,
‘’, E2.place); }

8
Production translation scheme
E → – E1 { E.place = newtemp();
emit( E.place, ‘=’, ‘–’ ,
E1.place); }

E → ( E1 ) { E.place = E1.place; }

E → id { p = lookup(id.name);
emit( E.place, ‘=’, p ); }

9
Assignment Statement
 The tranlation scheme uses a
symbol table for identifiers
and temporaries
 Every time the parser
encounters an identifier, it
installs it in the symbol table.

10
Assignment Statement
 The tranlation scheme uses a
symbol table for identifiers
and temporaries
 Every time the parser
encounters an identifier, it
installs it in the symbol table.

11
Assignment Statement
 The symbol table can be
implemented as a hash table
or using some other efficient
data structure for table.

12
Assignment Statement
 The routine lookup(name)
checks if there an entry for the
name in the symbol table
 If the name is found, the
routine returns a pointer to
entry.
13
Assignment Statement
 The routine lookup(name)
checks if there an entry for the
name in the symbol table
 If the name is found, the
routine returns a pointer to
entry.
14
Assignment Statement
 The routine newtemp()
returns a new temporary in
response to successive calls
 Temporaries can be placed in
the symbol table

15
Assignment Statement
 The routine emit()
generates a three-address
statement which can either be
held in memory or written to a
file

16
Example
Here is the bottom-up parse of
the assignment statement
a = b*-c + b*-c
and the syntax-directed
translation into three-address
code
17
a = b*-c + b*-c
Parser action attribute code
id=id  –id + id  –id
id=E1  –id + id  –id E1.place = b
id=E1  –E2 + id  –id E2.place = c

id=E1  E2 + id  –id E2.place = t1 t1 = – c

id=E1 + id  –id E1.place = t2 t2 = bt1

18
Parser action attribute code
id=E1 + E2  –id E2.place = b
id=E1 + E2  –E3 E3.place = c
id=E1 + E2  E3 E3.place = t3 t3 = – c

id=E1 + E2 E2.place = t4 t4 = bt3


id=E1 E1.place = t5 t5 = t2+t4
S a = t5

19
a = b*-c + b*-c

t1 = –c
t2 = b  t1
t3 = –c
t4 = b  t3
t5 = t2 + t4
a = t5
20
Representing Linear Codes
 Three-address codes are often
implemented as a set of
quadruples
 Each quadruple has four fields
• an operator
• two operands (or sources)
• a destination
21
Representing Linear Codes
 Three-address codes are often
implemented as a set of
quadruples
 Each quadruple has four fields
• an operator
• two operands (or sources)
• a destination
22
Simple Array of Quadruples
Target Op Arg1 Arg2
t1 ← 2
t2 ← y
t3  t1 t2
t4 ← x
t5 – t4 t3
23
Array of Pointers to quads
 t1 ← 2
 t2 ← y
 t3  t 1 t2
 t4 ← x
 t5 – t4 t3
24
Linked List of quads
  t1 ← 2
 t2 ← y
 t3  t 1 t2
 t4 ← x
 t5 – t4 t3
25
Flow-of-Control Statements
S→ if E then S1
| if E then S1 else S2
| while E do S1
where E is a boolean
expression

26
Flow-of-Control Statements
 Consider the statement
if c < d then
x = y + z
else
x = y – z
 One possible 3-address code
could be
27
if c < d then
x = y + z
else
x = y – z

if c < d goto L1
goto L2
L1: x = y + z
goto L3
L2: x = y – z
L3: nop
28
Flow-of-Control Statements
 we will assume that a three-
address statement can be
symbolically labeled
 the function newlabel() returns
a new symbolic label each
time it is called
29
Flow-of-Control Statements
 we will assume that a three-
address statement can be
symbolically labeled
 the function newlabel() returns
a new symbolic label each
time it is called
30
Three-Address Statement Types
 Prior to proceeding with flow-
of-control construct, here are
the types of three-Address
statements that we will use

31
Three-Address Statement Types
Assignment statement
x = y op z
where op is a binary
arithmetic or logical operation

32
Three-Address Statement Types
Assignment statement
x = op y
where op is a unary
operation, e.g., unary minus,
logical negation, shift
operators
33
Three-Address Statement Types
Copy statement
x = y
where value of y is assigned
to x

34
Three-Address Statement Types
Unconditional jump
goto L
The three-address statement
with label L is executed next

35
Three-Address Statement Types
Conditional jump
if x relop y goto L
where relop is <, =, >=, etc.
If x stands in relation relop
to y, execute statement with
label L, next otherwise
36
Three-Address Statement Types
Indexed assignment
a) x = y[i]
b) x[i] = y
In a), set x to value in location
i memory units beyond
location y.
37
Three-Address Statement Types
Indexed assignment
a) x = y[i]
b) x[i] = y
In b), set contents of location
i memory units beyond x to
y.
38
Array of Quadruples
Label Target Op Arg1 Arg2
L1 if_lt c d
L2 goto
L1: x + y z
L3 goto
L2: x – y z
L3: nop
39
Flow-of-Control Statements
 We associate with a boolean
expression E two labels; E.true
and E.false
 The control flows to E.true if
the expression evaluates to
true, to E.false otherwise

40
Flow-of-Control Statements
 We associate with a boolean
expression E two labels; E.true
and E.false
 The control flows to E.true if
the expression evaluates to
true, to E.false otherwise

41
S → if E then S1
to E.true
E.code
to E.false
E.true:
S1.code

E.false: .......

42

You might also like