0% found this document useful (0 votes)
54 views27 pages

17-Three Address Code

The document discusses three-address code, highlighting its compactness and the absence of destructive operators, which allows for efficient name and value reuse in compilers. It details syntax-directed translation schemes for programming constructs, particularly focusing on assignment statements and the use of a symbol table for identifiers and temporaries. Additionally, it describes the representation of three-address codes as quadruples and outlines various statement types including assignment, jumps, and indexed assignments.

Uploaded by

Zunaira Ikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views27 pages

17-Three Address Code

The document discusses three-address code, highlighting its compactness and the absence of destructive operators, which allows for efficient name and value reuse in compilers. It details syntax-directed translation schemes for programming constructs, particularly focusing on assignment statements and the use of a symbol table for identifiers and temporaries. Additionally, it describes the representation of three-address codes as quadruples and outlines various statement types including assignment, jumps, and indexed assignments.

Uploaded by

Zunaira Ikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Three-Address Code

 Three-address code is
attractive for several reasons:

1
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

2
Syntax-directed Translation
 We now consider syntax-
directed translation schemes
using three-address code for
various programming
constructs
 We start with the assignment
statement
3
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); }

4
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 ); }

5
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.

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

7
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.
8
Assignment Statement
 The routine newtemp()
returns a new temporary in
response to successive calls
 Temporaries can be placed in
the symbol table

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

10
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
11
AST: a = b*-c + b*-c
=
a +

* *
b - b -

c c

12
a = b*-c + b*-c
Parser action attribute code
id=id  –id + id  –
id
id=E1  –id + id  – E1.place = b
id
id=E1  –E2 + id  – E2.place = c
id
id=E1  E2 + id  – E2.place = t1 = – c
id t1
id=E1 + id  –id E1.place = t2 = 13
t2 bt1
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

14
a = b*-c + b*-c

t1 = –c
t2 = b  t1
t3 = –c
t4 = b  t3
t5 = t2 + t4
a = t5
15
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
16
Simple Array of Quadruples
Target Op Arg1 Arg2
t1 ← 2
t2 ← y
t3  t1 t2
t4 ← x
t5 – t4 t3
17
Array of Pointers to quads
 t1 ← 2
 t2 ← y
 t3  t 1 t2
 t4 ← x
 t5 – t4 t3
18
Linked List of quads
  t1 ← 2
 t2 ← y
 t3  t 1 t2
 t4 ← x
 t5 – t4 t3
19
Three-Address Statement Types
Assignment statement
x = y op z
where op is a binary
arithmetic or logical operation

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

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

23
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
24
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.
25
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.
26
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
27

You might also like