1 / 50

Compiler Designs and Constructions

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

Download Presentation

Compiler Designs and Constructions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. Definition: • Back-End: Intermediate ---> Machine Code • Front-End: Programming Languages ---> Intermediate Chapter 10: Intermediate Code Generation

  4. 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

  5. Notation and Definition: Chapter 10: Intermediate Code Generation

  6. Notation and Definition 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 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 ) 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. Chapter 10: Intermediate Code Generation

  10. 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

  11. Evaluation of Simple Expression lefthandside  ID=righthandside righthandside  expression expression  simple_expression simple_expression  term term  factor factor  ID Chapter 10: Intermediate Code Generation

  12. A=B lefthandside righthandside ID = expression simple_expression term factor ID Chapter 10: Intermediate Code Generation

  13. 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

  14. Evaluation ofA=B • righthandside  expression • { righthandside.place = expression.place } • lefthandside  ID = righthandside • { GEN( NextQuad “str” righthandside.place “,” “,” ID.place ) } Chapter 10: Intermediate Code Generation

  15. 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

  16. 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

  17. 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

  18. Evaluation of numbers factor  num { factor.place = ? num factor.mode = integer } Chapter 10: Intermediate Code Generation

  19. 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

  20. Evaluation of Simple Expression • addop  + { addop.op = ‘add’ } • addop  - { addop.op = ‘sub’ } Chapter 10: Intermediate Code Generation

  21. 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

  22. Arithmetic Operation can be: Chapter 10: Intermediate Code Generation

  23. Mixed Mode Translation: (Type) Chapter 10: Intermediate Code Generation

  24. Method1: Using Quadruples • Assumption: • Left association • True=1 • False=0 Chapter 10: Intermediate Code Generation

  25. 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

  26. 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

  27. 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

  28. expression simple_expression simple_expression relop .place .op .place term term factor factor id id Chapter 10: Intermediate Code Generation

  29. 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

  30. 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

  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) } Chapter 10: Intermediate Code Generation

  32. Evaluation of If Statement statement ) expression if statement ( lefthandside simple_expression simple_expression relop ID=righthandside Chapter 10: Intermediate Code Generation

  33. 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

  34. 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

  35. Evaluation of If Statement statement if (expression) statement { Backpatch (5,9) } statementif(expression) statement {Backpatch(5,NextQuad()} Chapter 10: Intermediate Code Generation

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. Evaluation of Nested If Statement statement  if(expression) statement { BackPatch (pop(eval_stack), NextQuad() ) } Chapter 10: Intermediate Code Generation

  43. 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

  44. 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

  45. 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

  46. when reduce production: If (exp) statement then (Backpatch with the top of Exp stack) Chapter 10: Intermediate Code Generation

  47. Evaluation of WHILE Statement while (a>b<c) { a=2; while (a==b) a=c } Chapter 10: Intermediate Code Generation

  48. 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

  49. 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

  50. JMP ,,#11 • JMP ,,#1 //BackPatch(15,18) • … //BackPatch(9,19) Chapter 10: Intermediate Code Generation

More Related