50
Chapter 10: Intermediate Code Generation 1 Compiler Designs and Constructions Chapter 10: Intermediate Code Generation Objectives: Dr. Mohsen Chitsaz

Compiler Designs and Constructions

Embed Size (px)

DESCRIPTION

Compiler Designs and Constructions. Chapter 10: Intermediate Code Generation Objectives: Dr. Mohsen Chitsaz. Intermediate Language (Code). Why Intermediate Language and not the Target Language? Advantages of Intermediate Code:. Pascal. Back-End. Fortran. Inter. Code. ---> 68000 - PowerPoint PPT Presentation

Citation preview

Page 1: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

1

Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation Objectives:

Dr. Mohsen Chitsaz

Page 2: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

2

Intermediate Language (Code)

Why Intermediate Language and not the Target Language?

Advantages of Intermediate Code:

Inter. Code

Pascal

Fortran

C

My Compiler

Back-End

---> 68000---> 8086

Front-End

Page 3: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

3

Definition:

Back-End: Intermediate ---> Machine Code

Front-End: Programming Languages ---> Intermediate

Page 4: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

4

Intermediate Code Generation

C:= A+B

What are the values of A and B?

A.PlaceB.PlaceC.Place

Translate:Gen(Temp1 ‘:=‘ A.Place ‘+’ B.Place)Gen(C.Place ‘:=‘ Temp1)

Page 5: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

5

Notation and Definition:

– id.name id in Symbol Table

– 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

Page 6: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

6

Notation and Definition

– Gen() Generate Intermediate Code into an output file ( .obj)

– E.Mode Types of E (Integer, …)

– NextQuad

– GetType(ID)

Gives address of next (Gen())

– Backpatch (P,i)

– Makelist (i)– Merge (P1, P2)

Page 7: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

7

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

Page 8: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

8

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 )

Page 9: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

9

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.

Page 10: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

10

Program Example: abc.jam

1. Sys #0,, //Blank Line2. Sys #-2,#69, //ASCII code for E3. Sys #-2,#78, //ASCII code for N4. Sys #-2,#68, //ASCII code for D5. Sys #0,, //Blank line6. Hlt ,,

Output: END

Page 11: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

11

Evaluation of Simple Expression

lefthandside ID=righthandside

righthandside expression

expression simple_expression

simple_expression term

term factor

factor ID

Page 12: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

12

lefthandside

ID

factor

term

simple_expression

expression

righthandsideID =

A=B

Page 13: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

13

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 }

Evaluation of A=B

Page 14: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

14

righthandside expression• { righthandside.place = expression.place }

lefthandside ID = righthandside• { GEN( NextQuad “str” righthandside.place

“,” “,” ID.place ) }

Evaluation of A=B

Page 15: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

15

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 }

Type checking for A=B

Page 16: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

16

expression simple_expression• {expression.place =

simple_expression.place expression.mode = simple_expression.mode }

righthandside expression• { righthandside.place = expression.place

righthandside.mode = expression.mode }

Type checking for A=B

Page 17: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

17

lefthandside ID = righthandside { If (GetType(ID) == righthandside.mode )

GEN( NextQuad “str” righthandside.place “,”“,” ID.place )

else Error( NextQuad “,” Type error )

}

Type checking for A=B

Page 18: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

18

factor num{ factor.place = ? num factor.mode = integer }

Evaluation of numbers

Page 19: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

19

ID

factor

term

simple_expression

expression

righthandsideID =

lefthandside

addop

+

simple_expression

ID

factor

term

Simple_expression.place term.place

A=B+C

Page 20: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

20

addop +{ addop.op = ‘add’ }

addop -{ addop.op = ‘sub’ }

Evaluation of Simple Expression

Page 21: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

21

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( )}

Evaluation of Simple Expression

Page 22: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

22

Arithmetic Operation can be:

submuldivmod

Page 23: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

23

Mixed Mode Translation: (Type)

E1 E2 E1+E2

Real Real Real

Real Int Real

Int Real Real

Int Int Int

Page 24: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

24

Method1: Using Quadruples

Assumption: Left association True=1 False=0

Page 25: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

25

Translation of Boolean Expression

Example: a < b1: IF a<b GOTO ( 4)2: t1 = 03: GOTO (5 )4: t1 = 15:

IF a<b THEN 1 ELSE 0

Page 26: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

26

Translation of Boolean Expression

Example a<b>c1 If a<b goto (4)

2 If t1=0

3 goto (5)4 t1=1

5 if t1>c goto (8)

6 t2=0

7 goto (9)8 t2=1

9

Page 27: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

27

Translation of Boolean Expression

Example a<b>c 1 JLT a,b,#4

2 STO #0,,t1

3 JMP ,,#54 STO #1,,t1

5 JGT t1,C,#8

6 STO #0,,t2

7 JMP ,,#98 STO #1,,t2

9

Page 28: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

28

expression

simple_expression relop simple_expression

term

factor

id

term

factor

id

.place .op .place

Page 29: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

29

expressionsimple_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)

}

Translation of Boolean Expression

Page 30: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

30

relop > { relop.op = JGT }relop < { relop.op = JLT }relop >= { relop.op = JGE }relop <= { relop.op = JLE }relop <> { relop.op = JNE }relop == { relop.op = JEQ }

Translation of Boolean Expression

Page 31: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

31

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

Page 32: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

32

Evaluation of If Statementstatement

if ( expression ) statement

simple_expression

relop

simple_expression lefthandside

ID=righthandside

Page 33: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

33

If (a == b){

c=5;d=2;a=c

}

1 JEQ a,b,#42 STO #0,,t1

3 JMP ,,#54 STO #1,,t1

5 //if (t1) goto…

6 STO #5,,c7 STO #2,,d8 STO c,,a

Evaluation of If Statement

Page 34: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

34

If (a == b){

c=5;d=2;a=c

}

1 JEQ a,b,#42 STO #0,,t1

3 JMP ,,#54 STO #1,,t1

5 JNE t1,#0,__ //jump in false

6 STO #5,,c7 STO #2,,d8 STO c,,a

Evaluation of If Statement

Page 35: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

35

statement if (expression) statement{ Backpatch (5,9) }

statementif(expression) statement

{Backpatch(5,NextQuad()}

Evaluation of If Statement

Page 36: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

36

If (a<b){ a=2;

b=3; if (a==c)

{ a=b;c=2;if (a>d) a=d+2

}}

Evaluation of Nested If Statement

Page 37: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

37

1 JLT a,b,#4 //a<b2 STO #0,,t1

3 JMP ,,#54 STO #1,,t1

5 JNE t1,#0,#___6 STO #2,,a7 STO #3,,b8 JEQ a,c,#11 //a==c

12

5

exp_stack

Evaluation of Nested If Statement

Page 38: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

38

9 STO #0,,t2

10 JMP ,,#1211 STO #1,,t2

12 JNE t2,#0,#___13 JLT a,d,#1614 STO #0,,t3

15 JMP ,,#1716 STO #1,,t317 …..

Evaluation of Nested If Statement

Page 39: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

39

Evaluation of Nested If Statement

1 JLT a,b,#4//a<b2 STO #0,,t13 JMP ,,#54 STO #1,,t15 JNE t1,#0,#17

6 STO #2,,a7 STO #3,,b8 JEQ a,c,#11 //a==c

12

5

exp_stack

Page 40: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

40

9 STO #0,,t2

10 JMP ,,#1211 STO #1,,t2

12 JNE t2,#0, #1713 JLT a,d,#1614 STO #0,,t3

15 JMP ,,#1716 STO #1,,t317 …..

Evaluation of Nested If Statement

Page 41: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

41

9 STO #0,,t210 JMP ,,#1211 STO #1,,t112 JLT a,d,#1613 JNE t2,#0,#1714 STO #0,,t315 JMP ,,#1716 STO #1,,t317 …..

Evaluation of Nested If Statement

Page 42: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

42

statement if(expression) statement{ BackPatch

(pop(eval_stack), NextQuad() )}

Evaluation of Nested If Statement

Page 43: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

43

expressionsimple_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“,”#)

}

Page 44: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

44

1. Set The Expflag to false2. In your scanner if Nexttoken()=“if”

then set Expflag to true.

Evaluation of Nested If Statement

Page 45: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

45

3. At the reduction of:-expressionsimple_expression-expressionsimple_expression Relop

simple_expression

If Expflag==true{push Nextquad() on Exp_stack insert a goto_ (JMP , , ___) flag=false}

Page 46: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

46

4. when reduce production:

If (exp) statement then(Backpatch with the top of Exp stack)

Page 47: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

47

while (a>b<c){

a=2;while (a==b)

a=c}

Evaluation of WHILE Statement

Page 48: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

48

1 JGT a,b,#42 STO #0,,t1

3 JMP ,,#54 STO #1,,t1

5 JLT t1,c,#8

6 STO #1,,t2

7 JMP ,,#98 STO #1,,t2

Evaluation of WHILE Statement

Page 49: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

49

9 JNE t2,#0,#_10 STO #2,,a11 JEQ a,b,#14 //while (a==b)12 STO #0,,t3

13 JMP ,,#1514 STO #1,,t3

15 JNE t3,#0,#_16 STO c,,a

Page 50: Compiler Designs and Constructions

Chapter 10: Intermediate Code Generation

50

17 JMP ,,#1118 JMP ,,#1 //BackPatch(15,18)19 … //BackPatch(9,19)