500 likes | 641 Views
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
E N D
Compiler Designs and Constructions Chapter 10: Intermediate Code Generation Objectives: Dr. Mohsen Chitsaz Chapter 10: Intermediate Code Generation
Intermediate Language (Code) • Why Intermediate Language and not the Target Language? • Advantages of Intermediate Code: Pascal Back-End Fortran Inter. Code ---> 68000 ---> 8086 C My Compiler Front-End Chapter 10: Intermediate Code Generation
Definition: • Back-End: Intermediate ---> Machine Code • Front-End: Programming Languages ---> Intermediate Chapter 10: Intermediate Code Generation
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
Notation and Definition: Chapter 10: Intermediate Code Generation
Notation and Definition Chapter 10: Intermediate Code Generation
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
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
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
Program Example: abc.jam • Sys #0,, //Blank Line • Sys #-2,#69, //ASCII code for E • Sys #-2,#78, //ASCII code for N • Sys #-2,#68, //ASCII code for D • Sys #0,, //Blank line • Hlt ,, Output: END Chapter 10: Intermediate Code Generation
Evaluation of Simple Expression lefthandside ID=righthandside righthandside expression expression simple_expression simple_expression term term factor factor ID Chapter 10: Intermediate Code Generation
A=B lefthandside righthandside ID = expression simple_expression term factor ID Chapter 10: Intermediate Code Generation
Evaluation ofA=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
Evaluation ofA=B • righthandside expression • { righthandside.place = expression.place } • lefthandside ID = righthandside • { GEN( NextQuad “str” righthandside.place “,” “,” ID.place ) } Chapter 10: Intermediate Code Generation
Type checking forA=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
Type checking forA=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
Type checking forA=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
Evaluation of numbers factor num { factor.place = ? num factor.mode = integer } Chapter 10: Intermediate Code Generation
lefthandside A=B+C = ID righthandside expression simple_expression simple_expression addop term Simple_expression.place term.place factor term + ID factor ID Chapter 10: Intermediate Code Generation
Evaluation of Simple Expression • addop + { addop.op = ‘add’ } • addop - { addop.op = ‘sub’ } Chapter 10: Intermediate Code Generation
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
Arithmetic Operation can be: Chapter 10: Intermediate Code Generation
Mixed Mode Translation: (Type) Chapter 10: Intermediate Code Generation
Method1: Using Quadruples • Assumption: • Left association • True=1 • False=0 Chapter 10: Intermediate Code Generation
Translation of Boolean Expression • Example: a < b 1: IF a<b GOTO ( 4) 2: t1 = 0 3: GOTO (5 ) 4: t1 = 1 5: IF a<b THEN 1 ELSE 0 Chapter 10: Intermediate Code Generation
Translation of Boolean Expression Example a<b>c 1 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 Chapter 10: Intermediate Code Generation
Translation of Boolean Expression Example a<b>c 1 JLT a,b,#4 2 STO #0,,t1 3 JMP ,,#5 4 STO #1,,t1 5 JGT t1,C,#8 6 STO #0,,t2 7 JMP ,,#9 8 STO #1,,t2 9 Chapter 10: Intermediate Code Generation
expression simple_expression simple_expression relop .place .op .place term term factor factor id id Chapter 10: Intermediate Code Generation
Translation of Boolean Expression 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) } Chapter 10: Intermediate Code Generation
Translation of Boolean Expression relop > { relop.op = JGT } relop < { relop.op = JLT } relop >= { relop.op = JGE } relop <= { relop.op = JLE } relop <> { relop.op = JNE } relop == { relop.op = JEQ } Chapter 10: Intermediate Code Generation
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
Evaluation of If Statement statement ) expression if statement ( lefthandside simple_expression simple_expression relop ID=righthandside Chapter 10: Intermediate Code Generation
If (a == b) { c=5; d=2; a=c } JEQ a,b,#4 STO #0,,t1 JMP ,,#5 STO #1,,t1 //if (t1) goto… STO #5,,c STO #2,,d STO c,,a Evaluation of If Statement Chapter 10: Intermediate Code Generation
Evaluation of If Statement If (a == b) { c=5; d=2; a=c } JEQ a,b,#4 STO #0,,t1 JMP ,,#5 STO #1,,t1 JNE t1,#0,__ //jump in false STO #5,,c STO #2,,d STO c,,a Chapter 10: Intermediate Code Generation
Evaluation of If Statement statement if (expression) statement { Backpatch (5,9) } statementif(expression) statement {Backpatch(5,NextQuad()} Chapter 10: Intermediate Code Generation
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
Evaluation of Nested If Statement • 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 12 5 exp_stack Chapter 10: Intermediate Code Generation
Evaluation of Nested If Statement • 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
Evaluation of Nested If Statement • 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 12 5 exp_stack Chapter 10: Intermediate Code Generation
Evaluation of Nested If Statement • 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
Evaluation of Nested If Statement • 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
Evaluation of Nested If Statement statement if(expression) statement { BackPatch (pop(eval_stack), NextQuad() ) } Chapter 10: Intermediate Code Generation
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“,”#) } Chapter 10: Intermediate Code Generation
Evaluation of Nested If Statement • Set The Expflag to false • In your scanner if Nexttoken()=“if” then set Expflag to true. Chapter 10: Intermediate Code Generation
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 } Chapter 10: Intermediate Code Generation
when reduce production: If (exp) statement then (Backpatch with the top of Exp stack) Chapter 10: Intermediate Code Generation
Evaluation of WHILE Statement while (a>b<c) { a=2; while (a==b) a=c } Chapter 10: Intermediate Code Generation
Evaluation of WHILE Statement • 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
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
JMP ,,#11 • JMP ,,#1 //BackPatch(15,18) • … //BackPatch(9,19) Chapter 10: Intermediate Code Generation