Compiling Control Flow Statements

Compiling Control Flow
Statements
Mooly Sagiv
Tel Aviv University
[email protected]
and
Reinhard Wilhelm
Universitat des Saarlandes
[email protected]
April 21, 1997
{ Wilhelm/Maurer: Compiler Design {
Subjects
Syntax of control ow statements
Sequence of statements
Conditional Statements
Case statements
Iterative Statements
{ Wilhelm/Maurer: Compiler Design {
1
Syntax
Sequence of Statements
st1 st2 stn
Conditional Statements
if e then st if e then st else st case e of
:
1:
..
st1
st2
0
end
k
:
stk
Iterative Statements
while e do st od
repeat st until e
{ Wilhelm/Maurer: Compiler Design {
2
Sequence of statements
code
st1 st2 ) = code (st1 ) code (st2 )
(
control drops through from the last instruction
generated for st1 to the rst instruction generated
for st2.
st1 and st2 are compiled in the same Example: Assume that T yp(x) = T yp(y ) = int
code (x := 3 y := x + 1) = code (x := 3) code (y := x + 1) = codeL x codeR 3 sto i code (y := x + 1) = ldc a (x) codeR (3) sto i code (y := x + 1) = ldc a (x) ldc i 3 sto i code (y := x + 1) = ldc a (x) ldc i 3 sto i codeL y codeR (x + 1) sto i
= ldc a (x) ldc i 3 sto i ldc a (y ) codeR (x + 1) sto i
{ Wilhelm/Maurer: Compiler Design {
3
Conditional Statements
Machine ressources to implement control statements:
unconditional and conditional jumps
P-code Instructions for Branches
Inst.
Meaning
ujp q
fjp q
PC := q
if STORE SP ] = false
then PC := q
SP := SP ;1
Cond.
q 2 0 codemax ]
(b)
q 2 0 codemax ]
Result
()
fjp consumes the boolean value on top of the stack.
{ Wilhelm/Maurer: Compiler Design {
4
Conditional Statements
code if e then st code if e then st1 else st2 codeR e fjp l1 code st1
code st2 l2
(
)
= codeR e fjp l code st l :
(
)
=
ujp l2
l1
:
:
code R e
code R e
fjp
code st1
fjp
ujp
code st
code st2
Conditionals
doublesided
onesided
Problem: Forward references to labels
Solutions:
Generate symbolic labels in assembly code,
2{pass code generation,
\Back patching" target addresses when known.
{ Wilhelm/Maurer: Compiler Design {
5
Examples
Example1:
if
a>b
then
Example2:
if
x<5
c := a
then if
else
y<7
{ Wilhelm/Maurer: Compiler Design {
c := b
then
c := a
6
Case Statement
Restricted form: Initial section of the natural numbers
Input
case e of
:
1:
..
st1
st2
0
end
k
:
stk
Two Solutions
Generate nested conditionals
if e then st1
else if e then st2
= 0
..
= 1
else if e n then stk
else run-time error: unmatched case label Jump table: PC table e
=
:=
{ Wilhelm/Maurer: Compiler Design {
( )
7
P-Code Instructions for Jump Table
Instr.
ixj q
Meaning
PC
SP
:=
:=
STORE SP
SP ;
Cond.
]+
q
i
( )
Result
()
1
codeR
leaves value of selector
on top of the stack
indexed jump to end of
jumptable
e
neg i
ixj q
code st0
Jump behind end of case
ujp
code st1
ujp
..
code stk
q
ujp
ujp
..
ujp
ujp
{ Wilhelm/Maurer: Compiler Design {
Jumptable
8
Iterative Statements
code while e do st od l1 codeR e fjp l2 code st ujp l1
(
)
:
l2 :
Example:
x := 1
=
while x < do
4
x := x + 1
od
code repeat st until e l code st codeR e fjp l
(
:
)
code R e
fjp
code st
ujp
while e do st od
{ Wilhelm/Maurer: Compiler Design {
=
code st
code R e
fjp
repeat st until e
9