Bottom-up evaluation of S

Download Report

Transcript Bottom-up evaluation of S

Compiler Designs and
Constructions
Chapter 10: Intermediate
Code Generation
Objectives:
Dr. Mohsen Chitsaz
Chapter 10: Intermediate
Code Generation
1
Intermediate Language (Code)

Why Intermediate Language and not the Target
Language?

Advantages of Intermediate Code:
Pascal
Fortran
C
My Compiler
Front-End
Back-End
Inter.
Code
---> 68000
---> 8086
Chapter 10: Intermediate
Code Generation
2
Definition:

Back-End:
Intermediate ---> Machine Code

Front-End:
Programming Languages ---> Intermediate
Chapter 10: Intermediate
Code Generation
3
Intermediate Code Generation
C:= A+B
What are the values of A and B?
A.Place
B.Place
C.Place
Translate:
Gen(Temp1 ‘:=‘ A.Place ‘+’ B.Place)
Gen(C.Place ‘:=‘ Temp1)
Chapter 10: Intermediate
Code Generation
4
Notation and Definition:
–
id.name
–
Lookup(id.name) Check Symbol Table for ‘id’
Returns Yes/No
E.Place
Name that holds the value of
Expression E
E.Code
Sequence of 3 address code
statements evaluating E
NewTemp()
Returns and appropriate
Temp name
–
–
–
id in Symbol Table
Chapter 10: Intermediate
Code Generation
5
Notation and Definition
–
Gen()
–
E.Mode
–
NextQuad
–
GetType(ID)
Backpatch (P,i)
–
–
–
Makelist (i)
Merge (P1, P2)
Generate Intermediate Code
into an output file ( .obj)
Types of E (Integer, …)
Gives address of next
(Gen())
Chapter 10: Intermediate
Code Generation
6
Instructions for running mICE





Download the ICE.zip from
http://faculty.frostburg.edu/chitsaz/ice.htm
Extract the files from ICE.zip to a directory
called mini.
Check if JVM ( Java Virtual Machine) is
installed on your machine.
From command prompt go to mini directory.
Recompile the mini.java & mice.java files on
your machine using the commands:
• javac mini.java
• javac mice.java
Chapter 10: Intermediate
Code Generation
7
Instruction for running mICE


Write your test program in a file and save
it in the mini directory.( eg. abc.jam )
Run the mini engine using the command:
• java mini


Give the name of your test program file
as the input file.
After running the mini engine,a file with
.out extension is added to your directory.
(eg abc.jam.out )
Chapter 10: Intermediate
Code Generation
8
Instruction for running mICE

Run the mice engine using
command:
• java mice
For the input file enter the
abc.jam.out file generated by mini
engine.
 You should see the output for the
test program on the screen.

Chapter 10: Intermediate
Code Generation
9
Program Example: abc.jam
1.
2.
3.
4.
5.
6.
Sys #0,,
Sys #-2,#69,
Sys #-2,#78,
Sys #-2,#68,
Sys #0,,
Hlt ,,
Output:
//Blank Line
//ASCII code for E
//ASCII code for N
//ASCII code for D
//Blank line
END
Chapter 10: Intermediate
Code Generation
10
Evaluation of Simple Expression
lefthandside

ID=righthandside
righthandside

expression
expression

simple_expression
simple_expression 
term
term

factor
factor

ID
Chapter 10: Intermediate
Code Generation
11
A=B
lefthandside
ID
=
righthandside
expression
simple_expression
term
factor
ID
Chapter 10: Intermediate
Code Generation
12
Evaluation of A=B

factor  ID
• { factor.place = ID }

term  factor
• { term.place = factor.place }

simple_expression  term
• { simple_expression.place = term.place }

expression  simple_expression
• { expression.place = simple_expression.place }
Chapter 10: Intermediate
Code Generation
13
Evaluation of A=B

righthandside  expression
• { righthandside.place = expression.place }

lefthandside  ID = righthandside
• { GEN( NextQuad “str” righthandside.place
“,” “,” ID.place ) }
Chapter 10: Intermediate
Code Generation
14
Type checking for A=B

factor  ID
• { factor.place = ID
factor.mode = GetType(ID) }

term  factor
• { term.place = factor.place
term.mode = factor.mode }

simple_expression  term
• { simple_expression.place = term.place
simple_expression.mode = term.mode }
Chapter 10: Intermediate
Code Generation
15
Type checking for A=B

expression  simple_expression
• {expression.place =
simple_expression.place
expression.mode =
simple_expression.mode }

righthandside  expression
• { righthandside.place = expression.place
righthandside.mode = expression.mode }
Chapter 10: Intermediate
Code Generation
16
Type checking for A=B

lefthandside  ID = righthandside
 { If (GetType(ID) == righthandside.mode )
GEN( NextQuad “str”
righthandside.place “,”“,” ID.place )
else
Error( NextQuad “,” Type error )
}
Chapter 10: Intermediate
Code Generation
17
Evaluation of numbers
factor  num
{ factor.place = ? num
factor.mode = integer }
Chapter 10: Intermediate
Code Generation
18
lefthandside
ID
=
A=B+C
righthandside
expression
simple_expression
simple_expression
addop
term
Simple_expression.place
term.place
term
factor
+
factor
ID
Chapter 10: Intermediate
Code Generation
ID
19
Evaluation of Simple Expression

addop  +
{ addop.op = ‘add’ }

addop  { addop.op = ‘sub’ }
Chapter 10: Intermediate
Code Generation
20
Evaluation of Simple Expression

simple_expression  simple_expression
addop.op term
if simple_expression.mode == term.mode
{simple_expression1.place = NewTemp()
GEN( NextQuad addop.op simple_expression.place
“,” term.place “,” simple_expression1.place )
else error( )
}
Chapter 10: Intermediate
Code Generation
21
Arithmetic Operation can be:
sub
mul
div
mod
Chapter 10: Intermediate
Code Generation
22
Mixed Mode Translation: (Type)
E1
E2
E1+E2
Real
Real
Real
Real
Int
Real
Int
Real
Real
Int
Int
Int
Chapter 10: Intermediate
Code Generation
23
Method1: Using Quadruples

Assumption:
Left association
 True=1
 False=0

Chapter 10: Intermediate
Code Generation
24
Translation of Boolean Expression

Example:
1:
2:
3:
4:
5:
a<b
IF a<b GOTO ( 4)
t1 = 0
GOTO (5 )
t1 = 1
IF a<b THEN 1 ELSE 0
Chapter 10: Intermediate
Code Generation
25
Translation of Boolean Expression
Example a<b>c
1 If a<b goto (4)
2
3
4
5
6
7
8
9
If t1=0
goto (5)
t1=1
if t1>c goto (8)
t2=0
goto (9)
t2=1
Chapter 10: Intermediate
Code Generation
26
Translation of Boolean Expression
Example a<b>c
1
2
3
4
5
6
7
8
9
JLT
STO
JMP
STO
JGT
STO
JMP
STO
a,b,#4
#0,,t1
,,#5
#1,,t1
t1,C,#8
#0,,t2
,,#9
#1,,t2
Chapter 10: Intermediate
Code Generation
27
expression
simple_expression
.place
relop
simple_expression
.place
.op
term
term
factor
factor
id
id
Chapter 10: Intermediate
Code Generation
28
Translation of Boolean Expression
expressionsimple_expression relop simple_expression
{ expression.place = NewTemp()
GEN(NextQuad relop.op simple_expression1.place
simple_expression2.place “#”NextQuad+2)
GEN(NextQuad “STO” #0“,”“,” expression.place)
GEN(NextQuad “JMP” “,”“,”“#”NextQuad+1)
GEN(NextQuad, “STO” #1“,”“,” expression.place)
}
Chapter 10: Intermediate
Code Generation
29
Translation of Boolean Expression
relop
relop
relop
relop
relop
relop






> { relop.op = JGT }
< { relop.op = JLT }
>= { relop.op = JGE }
<= { relop.op = JLE }
<> { relop.op = JNE }
== { relop.op = JEQ }
Chapter 10: Intermediate
Code Generation
30
factor1  not factor2
{ factor1.place = Newtemp()
GEN(NextQuad “NOT” factor2.place
“,”“,”factor1.place)
factor1  factor2 AND factor3
factor  factor OR factor
{ factor1.place = NewTemp()
GEN(NextQuad “AND” factor2.place
“,”factor3.place “,” factor1.place)
}
Chapter 10: Intermediate
Code Generation
31
Evaluation of If Statement
statement
if
(
expression
simple_expression
)
statement
simple_expression lefthandside
relop
ID=righthandside
Chapter 10: Intermediate
Code Generation
32
Evaluation of If Statement
If (a == b)
{
c=5;
d=2;
a=c
}
1
2
3
4
5
6
7
8
JEQ a,b,#4
STO #0,,t1
JMP ,,#5
STO #1,,t1
//if (t1) goto…
STO #5,,c
STO #2,,d
STO c,,a
Chapter 10: Intermediate
Code Generation
33
Evaluation of If Statement
If (a == b)
{
c=5;
d=2;
a=c
}
1
2
3
4
5
6
7
8
JEQ a,b,#4
STO #0,,t1
JMP ,,#5
STO #1,,t1
JNE t1,#0,__
STO #5,,c
STO #2,,d
STO c,,a
Chapter 10: Intermediate
Code Generation
//jump in false
34
Evaluation of If Statement
statement if (expression) statement
{ Backpatch (5,9) }
statementif(expression) statement
{Backpatch(5,NextQuad()}
Chapter 10: Intermediate
Code Generation
35
Evaluation of Nested If Statement
If (a<b)
{
a=2;
b=3;
if (a==c)
{
a=b;
c=2;
if (a>d)
a=d+2
}
}
Chapter 10: Intermediate
Code Generation
36
Evaluation of Nested If Statement
1
2
3
4
5
6
7
8
JLT a,b,#4
//a<b
STO #0,,t1
JMP ,,#5
STO #1,,t1
JNE t1,#0,#___
STO #2,,a
STO #3,,b
JEQ a,c,#11
//a==c
Chapter 10: Intermediate
Code Generation
12
5
exp_stack
37
Evaluation of Nested If Statement
9
10
11
12
13
14
15
16
17
STO #0,,t2
JMP ,,#12
STO #1,,t2
JNE t2,#0,#___
JLT a,d,#16
STO #0,,t3
JMP ,,#17
STO #1,,t3
…..
Chapter 10: Intermediate
Code Generation
38
Evaluation of Nested If Statement
1
2
3
4
5
6
7
8
JLT a,b,#4 //a<b
STO #0,,t1
JMP ,,#5
STO #1,,t1
JNE t1,#0,#17
STO #2,,a
STO #3,,b
JEQ a,c,#11
//a==c
Chapter 10: Intermediate
Code Generation
12
5
exp_stack
39
Evaluation of Nested If Statement
9
10
11
12
13
14
15
16
17
STO #0,,t2
JMP ,,#12
STO #1,,t2
JNE t2,#0, #17
JLT a,d,#16
STO #0,,t3
JMP ,,#17
STO #1,,t3
…..
Chapter 10: Intermediate
Code Generation
40
Evaluation of Nested If Statement
9
10
11
12
13
14
15
16
17
STO #0,,t2
JMP ,,#12
STO #1,,t1
JLT a,d,#16
JNE t2,#0,#17
STO #0,,t3
JMP ,,#17
STO #1,,t3
…..
Chapter 10: Intermediate
Code Generation
41
Evaluation of Nested If Statement
statement  if(expression) statement
{ BackPatch
(pop(eval_stack), NextQuad() )
}
Chapter 10: Intermediate
Code Generation
42
expressionsimple_expression relop simple_expression
{ expression.place = NewTemp()
GEN(NextQuad relop.op
simple_expression1.place
simple_expression2.place “#”NextQuad+2)
GEN(NextQuad “STO” #0“,”“,” expression.place)
GEN(NextQuad “JMP” “,”“,” “#”NextQuad+1)
GEN(NextQuad “STO” #1“,”“,” expression.place)
GEN(NextQuad “JNE” expression.place“,”#0“,”#)
}
Chapter 10: Intermediate
Code Generation
43
Evaluation of Nested If Statement
1.
2.
Set The Expflag to false
In your scanner if Nexttoken()=“if”
then set Expflag to true.
Chapter 10: Intermediate
Code Generation
44
3.
At the reduction of:
-expressionsimple_expression
-expressionsimple_expression Relop
simple_expression
If Expflag==true
{push Nextquad() on Exp_stack
insert a goto_ (JMP , , ___)
flag=false
}
Chapter 10: Intermediate
Code Generation
45
4.
when reduce production:
If (exp) statement then
(Backpatch with the top of Exp stack)
Chapter 10: Intermediate
Code Generation
46
Evaluation of WHILE Statement
while (a>b<c)
{
a=2;
while (a==b)
a=c
}
Chapter 10: Intermediate
Code Generation
47
Evaluation of WHILE Statement
1
2
3
4
5
6
7
8
JGT a,b,#4
STO #0,,t1
JMP ,,#5
STO #1,,t1
JLT t1,c,#8
STO #1,,t2
JMP ,,#9
STO #1,,t2
Chapter 10: Intermediate
Code Generation
48
9
10
11
12
13
14
15
16
JNE t2,#0,#_
STO #2,,a
JEQ a,b,#14 //while (a==b)
STO #0,,t3
JMP ,,#15
STO #1,,t3
JNE t3,#0,#_
STO c,,a
Chapter 10: Intermediate
Code Generation
49
17
18
19
JMP ,,#11
JMP ,,#1
…
//BackPatch(15,18)
//BackPatch(9,19)
Chapter 10: Intermediate
Code Generation
50