1
CSE 450: Compilers
K. Stirewalt
Code Generation
Part 1: Generating 3address code
Topics:
– Language of 3address code
– Translation of various kinds of statements into 3address
code
– Data storage/layout and arrays
Readings: Sections 7.1 – 7.3 of the Dragon Book
CSE 450: Compilers
K. Stirewalt
Structure
of a
Compiler
Source Language
Target Language
Semantic Analyzer
Syntax Analyzer
Lexical Analyzer
Front
End
Code Optimizer
Target Code Generator
Back
End
Int. Code Generator
Intermediate Code
CSE 450: Compilers
K. Stirewalt
Today!
Source Language
Target Language
Semantic Analyzer
Syntax Analyzer
Lexical Analyzer
Front
End
Code Optimizer
Target Code Generator
Back
End
Int. Code Generator
Intermediate Code
CSE 450: Compilers
K. Stirewalt
Threeaddress code
A form of intermediate code with at most one
operator on the right
hand side of an
assignment statement
E.g., a source expression like x+y+z might be
translated into the sequence of 3
address
instructions:
t
1
= y * z
t
2
= x + t
1
Useful representation for optimization and for
register allocation
CSE 450: Compilers
K. Stirewalt
Forms of 3address instructions
Assignments
– Form 1:
x = y op z
where op is a binary
arithmetic or logical operation and x, y, and
z are addresses
– Form 2:
x = op y
where op is a unary
arithmetic or logical operation and x and y
are addresses
Copy instructions:
of the form
x = y
Unconditional jumps
:
goto
L
CSE 450: Compilers
K. Stirewalt
3address instructions (continued)
Conditional jumps
:
– Form 1:
if
x
goto
L
– Form 2:
ifFalse
x
goto
L
– Form 3:
if
x relop y
goto
L
Indexed copy instructions
:
–
x = y[i]
–
x[i] = y
Address and pointer instructions:
–
x =
y
–
x =
*
y
–
*
x = y
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
CSE 450: Compilers
K. Stirewalt
3address instructions (continued)
Procedure calls:
involve a combination of
three kinds of instruction:
–
param
x
stores the contents of address x on the
system stack
–
call
p, n
invokes procedure
p
using the last
n
elements on the stack as actual parameters
–
y =
call
p, n
is same except with the ability to
store a returned value into address
y
–
return
y
returns to caller, optionally passing y as
a return value.
CSE 450: Compilers
K. Stirewalt
Example of a procedure call
Consider function
foo
of two integer parameters
that returns an integer.
We could translate a SpartanAda statement of
the form:
y := foo(x, y);
into the 3
address instructions:
param x
param y
y = call foo, 2
CSE 450: Compilers
K. Stirewalt
Example
Consider the C
 language statement:
do i = i+1; while (a[i] < v);
We could translate this into:
L:
t
1
= i + 1
i = t
1
t
2
= i * 8
t
3
= a[t
2
]
if t
3
< v goto L
The multiplication of i by 8 accounts for the
elements of a each taking 8 bytes of space
CSE 450: Compilers
K. Stirewalt
Formally defining translations
To define the translation described by the
previous example we must develop an SDD
Problem: The translation is subtle and complex
Solution:
– Define the translation one language feature at a
time
– Requires careful design of a framework of
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 STIREWALT
 Array, K. Stirewalt, Compilers K. Stirewalt

Click to edit the document details