SlideShare uma empresa Scribd logo
1 de 28
Intermediate Code Generation Part I Chapter 6 (1 st  ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
Intermediate Code Generation ,[object Object],[object Object],Front end Back end Intermediate code Target machine code
Intermediate Representations ,[object Object],[object Object],[object Object],[object Object]
Syntax-Directed Translation of Abstract Syntax Trees Production S     id :=   E E      E 1   +   E 2 E      E 1   *   E 2 E      -   E 1 E      (  E 1  ) E      id Semantic Rule S .nptr :=  mknode (‘:=’,  mkleaf ( id ,  id .entry),  E .nptr) E .nptr :=  mknode (‘+’,  E 1 .nptr,  E 2 .nptr) E .nptr :=  mknode (‘*’,  E 1 .nptr,  E 2 .nptr) E .nptr :=  mknode (‘uminus’,  E 1 .nptr) E .nptr :=  E 1 .nptr E .nptr :=  mkleaf ( id ,  id .entry)
Abstract Syntax Trees E .nptr * E .nptr E .nptr a b + E .nptr * a + b c E .nptr c E .nptr ( ) a * (b + c) Pro: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive
Abstract Syntax Trees versus DAGs := a + * uminus b c * uminus b c := a + * uminus b c Tree DAG a := b * -c + b * -c
Postfix Notation a := b * -c + b * -c a b c uminus * b c uminus * + assign iload 2 // push b iload 3 // push c ineg // uminus imul // * iload 2 // push b iload 3 // push c ineg // uminus imul // * iadd // + istore 1 // store a Bytecode (for example) Postfix notation represents operations on a stack Pro: easy to generate Cons: stack operations are more difficult to optimize
Three-Address Code a := b * -c + b * -c t1 := - c t2 := b * t1 t3 := - c t4 := b * t3 t5 := t2 + t4 a  := t5 Linearized representation of a syntax tree t1 := - c t2 := b * t1 t5 := t2 + t2 a  := t5 Linearized representation of a syntax DAG
Three-Address Statements ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Syntax-Directed Translation into Three-Address Code Synthesized attributes: S .code three-address code for  S S .begin label to start of  S  or nil S .after label to end of  S  or nil E .code three-address code for  E E .place a name holding the value of  E Productions S      id :=  E |  while   E   do   S E      E   +   E   |  E   *   E   |  -   E   |  (   E  )   |  id   |  num gen ( E .place ‘:=’  E 1 .place ‘+’  E 2 .place) t3 := t1 + t2 Code generation
Syntax-Directed Translation into Three-Address Code (cont’d) Productions S      id :=  E S      while   E   do   S 1 E      E 1   +   E 2 E      E 1   *   E 2 E      -   E 1 E      (   E 1  ) E      id E      num Semantic rules S .code :=  E .code ||  gen ( id .place ‘:=’  E .place);  S .begin :=  S .after := nil ( see next slide) E .place :=  newtemp (); E .code :=  E 1 .code ||  E 2 .code ||  gen ( E .place ‘:=’  E 1 .place ‘+’  E 2 .place) E .place :=  newtemp (); E .code :=  E 1 .code ||  E 2 .code ||  gen ( E .place ‘:=’  E 1 .place ‘*’  E 2 .place) E .place :=  newtemp (); E .code :=  E 1 .code ||  gen ( E .place ‘:=’ ‘uminus’  E 1 .place) E .place :=  E 1 .place E .code :=  E 1 .code E .place :=  id .name E .code := ‘’ E .place :=  newtemp (); E .code :=  gen ( E .place ‘:=’  num .value)
Syntax-Directed Translation into Three-Address Code (cont’d) Production S      while   E   do   S 1 Semantic rule S .begin :=  newlabel () S .after :=  newlabel () S .code :=  gen ( S .begin ‘:’) ||   E .code ||   gen (‘if’  E .place ‘=‘ ‘0’ ‘goto’  S .after) ||   S 1 .code ||   gen (‘goto’  S .begin) ||   gen ( S .after ‘:’) … if  E .place   =   0   goto  S .after S .code E .code goto  S .begin S .begin: S .after:
Example i := 2 * n + k while i do   i := i - k t1 := 2   t2 := t1 * n   t3 := t2 + k   i  := t3 L1: if i = 0 goto L2   t4 := i - k   i  := t4   goto L1 L2:
Implementation of Three-Address Statements: Quads Quads (quadruples) Pro: easy to rearrange code for global optimization Cons: lots of temporaries # Op Arg1 Arg2 Res (0) uminus c t1 (1) * b t1 t2 (2) uminus c t3 (3) * b t3 t4 (4) + t2 t4 t5 (5) := t5 a
Implementation of Three-Address Statements: Triples Triples Pro: temporaries are implicit Cons: difficult to rearrange code # Op Arg1 Arg2 (0) uminus c (1) * b (0) (2) uminus c (3) * b (2) (4) + (1) (3) (5) := a (4)
Implementation of Three-Address Stmts: Indirect Triples Triple container Pro: temporaries are implicit & easier to rearrange code Program # Op Arg1 Arg2 (14) uminus c (15) * b (14) (16) uminus c (17) * b (16) (18) + (15) (17) (19) := a (18) # Stmt (0) (14) (1) (15) (2) (16) (3) (17) (4) (18) (5) (19)
Names and Scopes ,[object Object],[object Object]
Symbol Tables for Scoping struct S { int a;   int b; } s; void swap(int& a, int& b) { int t;   t = a;   a = b;   b = t; } void somefunc() { …   swap(s.a, s.b);   … } We need a symbol table for the  fields  of struct S Need symbol table for  arguments and  locals  for each function Need symbol table for  global  variables and functions Check:  s  is global and has fields  a  and  b Using symbol tables we can generate  code to access  s  and its fields
Offset and Width for Runtime Allocation struct S { int a;   int b; } s; void swap(int& a, int& b) { int t;   t = a;   a = b;   b = t; } void somefunc() { …   swap(s.a, s.b);   … } Subroutine frame holds arguments  a  and  b  and local  t  at  offsets  0, 4, and 8 a b (0) (4) a b t (0) (4) (8) Subroutine frame fp[0]= fp[4]= fp[8]= The fields  a  and  b  of struct S are located at  offsets  0 and 4 from the start of S The  width  of S is 8 The  width  of the frame is 12
Example struct S { int a;   int b; } s; void swap(int& a, int& b) { int t;   t = a;   a = b;   b = t; } void foo() { …   swap(s.a, s.b);   … } a Trec S b s Tint Tfun swap a b t Tref prev=nil prev prev=nil prev Tfun foo swap foo globals (0) (0) (4) (8) (0) (4) Table nodes type nodes ( offset ) [ width ] [12] [0] [8] [8]
Hierarchical Symbol Table Operations ,[object Object],[object Object],[object Object],[object Object],[object Object]
Syntax-Directed Translation of Declarations in Scope Synthesized attributes: T .type pointer to type T .width storage width of type (bytes) E .place name of temp holding value of  E Productions P      D  ;  S D      D   ;   D   |  id :   T   |  proc   id ;  D  ;  S T      integer   |  real   |  array [ num ] of   T   |  ^   T   |  record   D   end S      S  ;  S   |   id :=  E   |   call id (  A  ) Global data to implement scoping: tblptr stack of pointers to tables offset stack of offset values Productions  (cont’d) E      E   +   E   |  E   *   E   |  -   E   |  (   E   )   |  id   |  E  ^   |  &  E     |  E  . id A      A  ,   E   |  E
Syntax-Directed Translation of Declarations in Scope (cont’d) P    {  t  :=  mktable (nil);  push ( t ,  tblptr );  push (0,  offset ) }   D  ;  S D      id :   T {  enter ( top ( tblptr) ,  id .name,  T .type,  top ( offset ));   top ( offset ) :=  top ( offset ) +  T .width } D      proc   id ;   {  t  :=  mktable ( top ( tblptr ));  push ( t ,  tblptr );  push (0,  offset ) }   D 1  ;  S {  t  :=  top ( tblptr );  addwidth ( t ,  top ( offset ));   pop ( tblptr );  pop ( offset );   enterproc ( top ( tblptr ),  id .name,  t ) } D      D 1   ;   D 2
Syntax-Directed Translation of Declarations in Scope (cont’d) T      integer {  T .type := ‘ integer’ ;  T .width := 4 } T      real {  T .type := ‘ real’ ;  T .width := 8 } T      array [ num ] of   T 1 {  T .type :=  array ( num .val,  T 1 .type);   T .width :=  num .val *  T 1 .width } T      ^   T 1 {  T .type :=  pointer ( T 1 .type);  T .width := 4 } T      record {  t  :=  mktable (nil);  push ( t ,  tblptr );  push (0,  offset ) }   D   end {  T .type :=  record ( top ( tblptr ));  T .width :=  top ( offset );   addwidth ( top ( tblptr ),  top ( offset ));  pop ( tblptr );  pop ( offset ) }
Example s: record   a: integer;   b: integer;   end; proc swap;   a: ^integer;   b: ^integer;   t: integer;   t := a^;   a^ := b^;   b^ := t; proc foo;   call swap(&s.a, &s.b); a Trec b s Tint Tfun swap a b t Tptr prev=nil prev prev=nil prev Tfun foo swap foo globals (0) (0) (4) (8) (0) (4) Table nodes type nodes ( offset ) [ width ] [12] [0] [8] [8]
Syntax-Directed Translation of Statements in Scope S      S  ;  S S      id :=  E {  p  :=  lookup ( top ( tblptr ),  id .name);   if   p  = nil  then   error ()   else if   p .level = 0  then  // global variable   emit ( id .place ‘:=’  E .place)   else  // local variable in subroutine frame   emit (fp[ p. offset] ‘:=’  E .place) }   s x y (0) (8) (12) Globals a b t (0) (4) (8) Subroutine frame fp[0]= fp[4]= fp[8]= …
Syntax-Directed Translation of Expressions in Scope E      E 1   +   E 2 {  E .place :=  newtemp ();   emit ( E .place ‘:=’  E 1 .place ‘+’  E 2 .place) } E      E 1   *   E 2 {  E .place :=  newtemp ();   emit ( E .place ‘:=’  E 1 .place ‘*’  E 2 .place) }   E      -   E 1   {  E .place :=  newtemp ();   emit ( E .place ‘:=’ ‘uminus’  E 1 .place) }   E      (   E 1   ) {  E .place :=  E 1 .place } E      id   {  p  :=  lookup ( top ( tblptr ),  id .name);   if   p  = nil  then  error ()   else if   p .level = 0  then  // global variable   E .place :=  id .place   else  // local variable in frame   E .place := fp[ p .offset] }
Syntax-Directed Translation of Expressions in Scope (cont’d) E      E 1  ^ {  E .place :=  newtemp ();   emit ( E .place ‘:=’  ‘ *’  E 1 .place) }   E      &  E 1 {  E .place :=  newtemp ();   emit ( E .place ‘:=’  ‘ &’  E 1 .place) }   E      id 1  . id 2 {  p  :=  lookup ( top ( tblptr ),  id 1 .name);   if   p  = nil  or   p .type != Trec  then   error ()   else   q  :=  lookup ( p .type.table,  id 2 .name);   if   q  = nil  then  error()   else if  p .level = 0  then  //  global variable   E .place :=  id 1 .place[ q .offset]   else  //  local variable in frame   E .place := fp[ p .offset+ q .offset] }

Mais conteúdo relacionado

Mais procurados

Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)bolovv
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocksJothi Lakshmi
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 

Mais procurados (20)

Ch9a
Ch9aCh9a
Ch9a
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Back patching
Back patchingBack patching
Back patching
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Three Address code
Three Address code Three Address code
Three Address code
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 

Semelhante a Intermediate Code Generation Part I Chapter 6

12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdfSHUJEHASSAN
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...venkatapranaykumarGa
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARDTia Ricci
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dagsindhu mathi
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPSPrasenjit Dey
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013ericupnorth
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework HelpC++ Homework Help
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdfHimoZZZ
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 

Semelhante a Intermediate Code Generation Part I Chapter 6 (20)

12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Ch2
Ch2Ch2
Ch2
 
Ch5b.ppt
Ch5b.pptCh5b.ppt
Ch5b.ppt
 
Array
ArrayArray
Array
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPS
 
Ch3
Ch3Ch3
Ch3
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
 
5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 

Mais de kinnarshah8888 (11)

Yuva Msp All
Yuva Msp AllYuva Msp All
Yuva Msp All
 
Yuva Msp Intro
Yuva Msp IntroYuva Msp Intro
Yuva Msp Intro
 
Ch6
Ch6Ch6
Ch6
 
Ch5a
Ch5aCh5a
Ch5a
 
Ch10
Ch10Ch10
Ch10
 
Ch7
Ch7Ch7
Ch7
 
Ch4b
Ch4bCh4b
Ch4b
 
Ch4a
Ch4aCh4a
Ch4a
 
Ch5b
Ch5bCh5b
Ch5b
 
Ch4c
Ch4cCh4c
Ch4c
 
Ch1
Ch1Ch1
Ch1
 

Último

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Intermediate Code Generation Part I Chapter 6

  • 1. Intermediate Code Generation Part I Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2.
  • 3.
  • 4. Syntax-Directed Translation of Abstract Syntax Trees Production S  id := E E  E 1 + E 2 E  E 1 * E 2 E  - E 1 E  ( E 1 ) E  id Semantic Rule S .nptr := mknode (‘:=’, mkleaf ( id , id .entry), E .nptr) E .nptr := mknode (‘+’, E 1 .nptr, E 2 .nptr) E .nptr := mknode (‘*’, E 1 .nptr, E 2 .nptr) E .nptr := mknode (‘uminus’, E 1 .nptr) E .nptr := E 1 .nptr E .nptr := mkleaf ( id , id .entry)
  • 5. Abstract Syntax Trees E .nptr * E .nptr E .nptr a b + E .nptr * a + b c E .nptr c E .nptr ( ) a * (b + c) Pro: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive
  • 6. Abstract Syntax Trees versus DAGs := a + * uminus b c * uminus b c := a + * uminus b c Tree DAG a := b * -c + b * -c
  • 7. Postfix Notation a := b * -c + b * -c a b c uminus * b c uminus * + assign iload 2 // push b iload 3 // push c ineg // uminus imul // * iload 2 // push b iload 3 // push c ineg // uminus imul // * iadd // + istore 1 // store a Bytecode (for example) Postfix notation represents operations on a stack Pro: easy to generate Cons: stack operations are more difficult to optimize
  • 8. Three-Address Code a := b * -c + b * -c t1 := - c t2 := b * t1 t3 := - c t4 := b * t3 t5 := t2 + t4 a := t5 Linearized representation of a syntax tree t1 := - c t2 := b * t1 t5 := t2 + t2 a := t5 Linearized representation of a syntax DAG
  • 9.
  • 10. Syntax-Directed Translation into Three-Address Code Synthesized attributes: S .code three-address code for S S .begin label to start of S or nil S .after label to end of S or nil E .code three-address code for E E .place a name holding the value of E Productions S  id := E | while E do S E  E + E | E * E | - E | ( E ) | id | num gen ( E .place ‘:=’ E 1 .place ‘+’ E 2 .place) t3 := t1 + t2 Code generation
  • 11. Syntax-Directed Translation into Three-Address Code (cont’d) Productions S  id := E S  while E do S 1 E  E 1 + E 2 E  E 1 * E 2 E  - E 1 E  ( E 1 ) E  id E  num Semantic rules S .code := E .code || gen ( id .place ‘:=’ E .place); S .begin := S .after := nil ( see next slide) E .place := newtemp (); E .code := E 1 .code || E 2 .code || gen ( E .place ‘:=’ E 1 .place ‘+’ E 2 .place) E .place := newtemp (); E .code := E 1 .code || E 2 .code || gen ( E .place ‘:=’ E 1 .place ‘*’ E 2 .place) E .place := newtemp (); E .code := E 1 .code || gen ( E .place ‘:=’ ‘uminus’ E 1 .place) E .place := E 1 .place E .code := E 1 .code E .place := id .name E .code := ‘’ E .place := newtemp (); E .code := gen ( E .place ‘:=’ num .value)
  • 12. Syntax-Directed Translation into Three-Address Code (cont’d) Production S  while E do S 1 Semantic rule S .begin := newlabel () S .after := newlabel () S .code := gen ( S .begin ‘:’) || E .code || gen (‘if’ E .place ‘=‘ ‘0’ ‘goto’ S .after) || S 1 .code || gen (‘goto’ S .begin) || gen ( S .after ‘:’) … if E .place = 0 goto S .after S .code E .code goto S .begin S .begin: S .after:
  • 13. Example i := 2 * n + k while i do i := i - k t1 := 2 t2 := t1 * n t3 := t2 + k i := t3 L1: if i = 0 goto L2 t4 := i - k i := t4 goto L1 L2:
  • 14. Implementation of Three-Address Statements: Quads Quads (quadruples) Pro: easy to rearrange code for global optimization Cons: lots of temporaries # Op Arg1 Arg2 Res (0) uminus c t1 (1) * b t1 t2 (2) uminus c t3 (3) * b t3 t4 (4) + t2 t4 t5 (5) := t5 a
  • 15. Implementation of Three-Address Statements: Triples Triples Pro: temporaries are implicit Cons: difficult to rearrange code # Op Arg1 Arg2 (0) uminus c (1) * b (0) (2) uminus c (3) * b (2) (4) + (1) (3) (5) := a (4)
  • 16. Implementation of Three-Address Stmts: Indirect Triples Triple container Pro: temporaries are implicit & easier to rearrange code Program # Op Arg1 Arg2 (14) uminus c (15) * b (14) (16) uminus c (17) * b (16) (18) + (15) (17) (19) := a (18) # Stmt (0) (14) (1) (15) (2) (16) (3) (17) (4) (18) (5) (19)
  • 17.
  • 18. Symbol Tables for Scoping struct S { int a; int b; } s; void swap(int& a, int& b) { int t; t = a; a = b; b = t; } void somefunc() { … swap(s.a, s.b); … } We need a symbol table for the fields of struct S Need symbol table for arguments and locals for each function Need symbol table for global variables and functions Check: s is global and has fields a and b Using symbol tables we can generate code to access s and its fields
  • 19. Offset and Width for Runtime Allocation struct S { int a; int b; } s; void swap(int& a, int& b) { int t; t = a; a = b; b = t; } void somefunc() { … swap(s.a, s.b); … } Subroutine frame holds arguments a and b and local t at offsets 0, 4, and 8 a b (0) (4) a b t (0) (4) (8) Subroutine frame fp[0]= fp[4]= fp[8]= The fields a and b of struct S are located at offsets 0 and 4 from the start of S The width of S is 8 The width of the frame is 12
  • 20. Example struct S { int a; int b; } s; void swap(int& a, int& b) { int t; t = a; a = b; b = t; } void foo() { … swap(s.a, s.b); … } a Trec S b s Tint Tfun swap a b t Tref prev=nil prev prev=nil prev Tfun foo swap foo globals (0) (0) (4) (8) (0) (4) Table nodes type nodes ( offset ) [ width ] [12] [0] [8] [8]
  • 21.
  • 22. Syntax-Directed Translation of Declarations in Scope Synthesized attributes: T .type pointer to type T .width storage width of type (bytes) E .place name of temp holding value of E Productions P  D ; S D  D ; D | id : T | proc id ; D ; S T  integer | real | array [ num ] of T | ^ T | record D end S  S ; S | id := E | call id ( A ) Global data to implement scoping: tblptr stack of pointers to tables offset stack of offset values Productions (cont’d) E  E + E | E * E | - E | ( E ) | id | E ^ | & E | E . id A  A , E | E
  • 23. Syntax-Directed Translation of Declarations in Scope (cont’d) P  { t := mktable (nil); push ( t , tblptr ); push (0, offset ) } D ; S D  id : T { enter ( top ( tblptr) , id .name, T .type, top ( offset )); top ( offset ) := top ( offset ) + T .width } D  proc id ; { t := mktable ( top ( tblptr )); push ( t , tblptr ); push (0, offset ) } D 1 ; S { t := top ( tblptr ); addwidth ( t , top ( offset )); pop ( tblptr ); pop ( offset ); enterproc ( top ( tblptr ), id .name, t ) } D  D 1 ; D 2
  • 24. Syntax-Directed Translation of Declarations in Scope (cont’d) T  integer { T .type := ‘ integer’ ; T .width := 4 } T  real { T .type := ‘ real’ ; T .width := 8 } T  array [ num ] of T 1 { T .type := array ( num .val, T 1 .type); T .width := num .val * T 1 .width } T  ^ T 1 { T .type := pointer ( T 1 .type); T .width := 4 } T  record { t := mktable (nil); push ( t , tblptr ); push (0, offset ) } D end { T .type := record ( top ( tblptr )); T .width := top ( offset ); addwidth ( top ( tblptr ), top ( offset )); pop ( tblptr ); pop ( offset ) }
  • 25. Example s: record a: integer; b: integer; end; proc swap; a: ^integer; b: ^integer; t: integer; t := a^; a^ := b^; b^ := t; proc foo; call swap(&s.a, &s.b); a Trec b s Tint Tfun swap a b t Tptr prev=nil prev prev=nil prev Tfun foo swap foo globals (0) (0) (4) (8) (0) (4) Table nodes type nodes ( offset ) [ width ] [12] [0] [8] [8]
  • 26. Syntax-Directed Translation of Statements in Scope S  S ; S S  id := E { p := lookup ( top ( tblptr ), id .name); if p = nil then error () else if p .level = 0 then // global variable emit ( id .place ‘:=’ E .place) else // local variable in subroutine frame emit (fp[ p. offset] ‘:=’ E .place) } s x y (0) (8) (12) Globals a b t (0) (4) (8) Subroutine frame fp[0]= fp[4]= fp[8]= …
  • 27. Syntax-Directed Translation of Expressions in Scope E  E 1 + E 2 { E .place := newtemp (); emit ( E .place ‘:=’ E 1 .place ‘+’ E 2 .place) } E  E 1 * E 2 { E .place := newtemp (); emit ( E .place ‘:=’ E 1 .place ‘*’ E 2 .place) } E  - E 1 { E .place := newtemp (); emit ( E .place ‘:=’ ‘uminus’ E 1 .place) } E  ( E 1 ) { E .place := E 1 .place } E  id { p := lookup ( top ( tblptr ), id .name); if p = nil then error () else if p .level = 0 then // global variable E .place := id .place else // local variable in frame E .place := fp[ p .offset] }
  • 28. Syntax-Directed Translation of Expressions in Scope (cont’d) E  E 1 ^ { E .place := newtemp (); emit ( E .place ‘:=’ ‘ *’ E 1 .place) } E  & E 1 { E .place := newtemp (); emit ( E .place ‘:=’ ‘ &’ E 1 .place) } E  id 1 . id 2 { p := lookup ( top ( tblptr ), id 1 .name); if p = nil or p .type != Trec then error () else q := lookup ( p .type.table, id 2 .name); if q = nil then error() else if p .level = 0 then // global variable E .place := id 1 .place[ q .offset] else // local variable in frame E .place := fp[ p .offset+ q .offset] }