SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Intermediate Code Generation Part II Chapter 6 (1 st  ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
Advanced Intermediate Code Generation Techniques ,[object Object],[object Object],[object Object],[object Object],[object Object]
Reusing Temporary Names Evaluate  E 1  into  t1   Evaluate  E 2  into  t2 t3 := t1 + t2 E 1   +   E 2 Modify  newtemp () to use a “stack”: Keep a counter  c , initialized to 0 Decrement counter on each use of a  $ i  in a three-address statement newtemp () returns temporary  $ c++  (that is,  c  is post incremented )   If  t1  no longer used, can reuse  t1 instead of using new temp  t3 generate
Reusing Temporary Names (cont’d) x := a * b + c * d - e * f $ 0  := a * b $ 1  := c * d $ 0  :=  $0  +  $1 $ 1  := e * f $ 0  :=  $0  -  $1 x  :=  $0 0 0 ->  0  -> 1 1 ->  1  -> 2 2 ->  0  -> 1 1 ->  1  -> 2 2 ->  0  -> 1 1 ->  0  -> 0 c  ->  c decr  ->  c incr Statement
Addressing Array Elements: One-Dimensional Arrays = base A  + ( i  -  low ) *  w = i  *  w  +  c where  c = base A  - low * w with  low =  10;  w =  4 A : array [10..20] of integer; … := A[i] t1 := c // c =  base A  -  10 * 4 t2 := i * 4 t3 := t1[t2] …  := t3
Addressing Array Elements: Multi-Dimensional Arrays A : array [1..2,1..3] of integer; low 1  =  1,  low 2  =  1,  n 1  =  2 , n 2  =  3 , w =  4 A[1,1] A[1,2] A[1,3] A[2,1] A[2,2] A[2,3] Row-major A[1,1] A[2,1] A[1,2] A[2,2] A[1,3] A[2,3] Column-major base A base A
Addressing Array Elements: Multi-Dimensional Arrays = base A  + (( i 1  -  low 1 ) *  n 2  +  i 2  -  low 2 )  * w =  (( i 1  *  n 2 )  + i 2 ) *  w  +  c where  c = base A  -  (( low 1  * n 2 ) +  low 2 ) *  w with  low 1  =  1;  low 2  =  1;  n 2  =  3;  w =  4 A : array [1..2,1..3] of integer;  (Row-major) … := A[i,j] t1 := i * 3 t1 := t1 + j t2 := c // c =  base A  -  (1 * 3 + 1) * 4 t3 := t1 * 4 t4 := t2[t3] …  := t4
Addressing Array Elements: Grammar S      L   :=   E E      E   +   E   |  (   E  )   |  L L      Elist  ]   |  id Elist      Elist  ,   E   |  id [  E Synthesized attributes: E .place name of temp holding value of  E Elist .array array name Elist .place name of temp holding index value Elist .ndim number of array dimensions L .place lvalue (=name of temp) L .offset index into array (=name of temp) null  indicates non-array simple  id
Addressing Array Elements S      L   :=   E {  if   L .offset =  null   then   emit ( L .place ‘:=’  E .place)   else   emit ( L .place[ L .offset] ‘:=’  E .place) }   E      E 1  +  E 2  {  E .place :=  newtemp ();   emit ( E .place ‘:=’  E 1 .place ‘+’  E 2 .place) }   E      (   E 1   ) {  E .place :=  E 1 .place } E      L {  if   L .offset =  null   then   E .place :=  L .place   else   E .place :=  newtemp ();   emit ( E .place ‘:=’  L .place[ L .offset] }
Addressing Array Elements L      Elist   ] {  L .place :=  newtemp ();   L .offset :=  newtemp ();   emit ( L .place ‘:=’  c ( Elist .array);   emit ( L .offset ‘:=’  Elist .place ‘*’  width ( Elist .array)) }   L      id   {  L .place :=  id .place;   L .offset :=  null  }   Elist      Elist 1   ,  E {  t  :=  newtemp ();  m  :=  Elist 1 .ndim + 1;    emit ( t  ‘:=’  Elist 1 .place ‘*’  limit ( Elist 1 .array,  m ));   emit ( t  ‘:=’  t  ‘+’  E .place);    Elist .array :=  Elist 1 .array;  Elist .place :=  t ;   Elist .ndim :=  m  } Elist      id   [  E {  Elist .array :=  id .place;  Elist .place :=  E .place;   Elist .ndim := 1 }
Translating Logical and Relational Expressions a or b and not c t1 := not c t2 := b and t1 t3 := a or t2 if a < b goto L1   t1 := 0   goto L2 L1: t1 := 1 L2: a < b
Translating Short-Circuit Expressions Using Backpatching E      E   or   M   E   |  E   and   M   E     |  not   E   |  (   E   )   |  id relop id   |  true |  false M       Synthesized attributes: E .truelist backpatch list for jumps on true E .falselist backpatch list for jumps on false M .quad location of current three-address quad
Backpatch Operations with Lists ,[object Object],[object Object],[object Object]
Backpatching with Lists: Example a < b or c < d and e < f 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _ 100: if a < b goto TRUE 101: goto 102 102: if c < d goto 104 103: goto FALSE 104: if e < f goto TRUE 105: goto FALSE backpatch
Backpatching with Lists: Translation Scheme M         {  M .quad :=  nextquad () } E      E 1   or   M   E 2 {  backpatch ( E 1 .falselist,  M .quad);   E .truelist :=  merge ( E 1 .truelist,  E 2 .truelist);   E .falselist :=  E 2 .falselist }   E      E 1   and   M   E 2 {  backpatch ( E 1 .truelist,  M .quad);   E .truelist :=  E 2 .truelist;   E .falselist :=  merge ( E 1 .falselist,  E 2 .falselist); } E      not   E 1  {  E .truelist :=  E 1 .falselist;   E .falselist :=  E 1 .truelist }   E      (   E 1   ) {  E .truelist :=  E 1 .truelist;   E .falselist :=  E 1 .falselist }
Backpatching with Lists: Translation Scheme (cont’d) E      id 1   relop id 2 {  E .truelist :=  makelist ( nextquad ());   E .falselist :=  makelist ( nextquad () + 1);   emit (‘ if ’  id 1 .place  relop .op  id 2 .place ‘ goto _ ’);   emit (‘ goto _ ’) }   E      true {  E .truelist :=  makelist ( nextquad ());   E .falselist := nil;   emit (‘ goto _ ’) } E      false {  E .falselist :=  makelist ( nextquad ());   E .truelist := nil;   emit (‘ goto _ ’) }
Flow-of-Control Statements and Backpatching: Grammar S      if   E   then   S   |  if   E   then   S   else   S     |  while   E  do  S   |  begin  L  end   |  A L      L   ;   S   |  S Synthesized attributes: S .nextlist backpatch list for jumps to the next statement after  S  (or nil) L .nextlist backpatch list for jumps to the next statement after  L  (or nil) S 1   ;  S 2   ;  S 3   ;  S 4   ;  S 5  … backpatch ( S 1 .nextlist, 200) backpatch ( S 2 .nextlist, 300) backpatch ( S 3 .nextlist, 400) backpatch ( S 4 .nextlist, 500) 100: Code for S1 200: Code for S2 300: Code for S3 400: Code for S4 500: Code for S5 Jumps out of S 1
Flow-of-Control Statements and Backpatching S     A {  S .nextlist := nil } S      begin  L  end {  S .nextlist :=  L .nextlist } S      if   E  then  M S 1 {  backpatch ( E .truelist,  M .quad);   S .nextlist :=  merge ( E .falselist,  S 1 .nextlist) } L     L 1   ;   M S {  backpatch ( L 1 .nextlist,  M .quad);   L .nextlist :=  S .nextlist; } L     S {  L .nextlist :=  S .nextlist; } M        {  M .quad :=  nextquad () } A    … Non-compound statements, e.g. assignment, function call
Flow-of-Control Statements and Backpatching (cont’d) S      if   E  then  M 1  S 1   N   else  M 2  S 2 {  backpatch ( E .truelist,  M 1 .quad);   backpatch ( E .falselist,  M 2 .quad);   S .nextlist :=  merge ( S 1 .nextlist,   merge ( N .nextlist,  S 2 .nextlist)) } S     while  M 1  E  do  M 2   S 1 {  backpatch ( S 1 ,nextlist,  M 1 .quad);   backpatch ( E .truelist,  M 2 .quad);   S .nextlist :=  E .falselist;   emit (‘ goto  M 1 .quad’) } N        {  N .nextlist :=  makelist ( nextquad ());   emit (‘ goto _ ’) }
Translating Procedure Calls S      call id (   Elist   ) Elist      Elist   ,   E   |  E call foo(a+1, b, 7) t1 := a + 1 t2 := 7 param t1 param b param t2 call foo 3
Translating Procedure Calls S      call id (   Elist   ) {  for  each item  p  on  queue   do   emit (‘ param ’  p );   emit (‘ call ’  id .place | queue |) } Elist      Elist   ,   E { append  E .place to the end of  queue  } Elist      E { initialize  queue  to contain only  E .place }

Weitere ähnliche Inhalte

Was ist angesagt?

Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocksJothi Lakshmi
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)bolovv
 

Was ist angesagt? (20)

Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Ch9c
Ch9cCh9c
Ch9c
 
Ch9a
Ch9aCh9a
Ch9a
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Ch5b
Ch5bCh5b
Ch5b
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Three Address code
Three Address code Three Address code
Three Address code
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Ch04
Ch04Ch04
Ch04
 
Ch06
Ch06Ch06
Ch06
 
Ch03
Ch03Ch03
Ch03
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
Chapter Eight(1)
Chapter Eight(1)Chapter Eight(1)
Chapter Eight(1)
 

Ähnlich wie Ch8b

Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)Siddhesh Pange
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdfSHUJEHASSAN
 
Admissions in India 2015
Admissions in India 2015Admissions in India 2015
Admissions in India 2015Edhole.com
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua tableShuai Yuan
 
Lesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemLesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemMitchell Wand
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in indiaEdhole.com
 
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptxAddressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptxAshishPandey502
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)Alexandru Radovici
 

Ähnlich wie Ch8b (20)

Presentation1.pdf
Presentation1.pdfPresentation1.pdf
Presentation1.pdf
 
Array
ArrayArray
Array
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
Admissions in India 2015
Admissions in India 2015Admissions in India 2015
Admissions in India 2015
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Ch5b.ppt
Ch5b.pptCh5b.ppt
Ch5b.ppt
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua table
 
Lesson 01 A Warmup Problem
Lesson 01 A Warmup ProblemLesson 01 A Warmup Problem
Lesson 01 A Warmup Problem
 
Array
ArrayArray
Array
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptxAddressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 
Ch3
Ch3Ch3
Ch3
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 

Mehr von 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
 
Ch2
Ch2Ch2
Ch2
 
Ch4b
Ch4bCh4b
Ch4b
 
Ch4a
Ch4aCh4a
Ch4a
 
Ch4c
Ch4cCh4c
Ch4c
 
Ch1
Ch1Ch1
Ch1
 

Kürzlich hochgeladen

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Kürzlich hochgeladen (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Ch8b

  • 1. Intermediate Code Generation Part II Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2.
  • 3. Reusing Temporary Names Evaluate E 1 into t1 Evaluate E 2 into t2 t3 := t1 + t2 E 1 + E 2 Modify newtemp () to use a “stack”: Keep a counter c , initialized to 0 Decrement counter on each use of a $ i in a three-address statement newtemp () returns temporary $ c++ (that is, c is post incremented ) If t1 no longer used, can reuse t1 instead of using new temp t3 generate
  • 4. Reusing Temporary Names (cont’d) x := a * b + c * d - e * f $ 0 := a * b $ 1 := c * d $ 0 := $0 + $1 $ 1 := e * f $ 0 := $0 - $1 x := $0 0 0 -> 0 -> 1 1 -> 1 -> 2 2 -> 0 -> 1 1 -> 1 -> 2 2 -> 0 -> 1 1 -> 0 -> 0 c -> c decr -> c incr Statement
  • 5. Addressing Array Elements: One-Dimensional Arrays = base A + ( i - low ) * w = i * w + c where c = base A - low * w with low = 10; w = 4 A : array [10..20] of integer; … := A[i] t1 := c // c = base A - 10 * 4 t2 := i * 4 t3 := t1[t2] … := t3
  • 6. Addressing Array Elements: Multi-Dimensional Arrays A : array [1..2,1..3] of integer; low 1 = 1, low 2 = 1, n 1 = 2 , n 2 = 3 , w = 4 A[1,1] A[1,2] A[1,3] A[2,1] A[2,2] A[2,3] Row-major A[1,1] A[2,1] A[1,2] A[2,2] A[1,3] A[2,3] Column-major base A base A
  • 7. Addressing Array Elements: Multi-Dimensional Arrays = base A + (( i 1 - low 1 ) * n 2 + i 2 - low 2 ) * w = (( i 1 * n 2 ) + i 2 ) * w + c where c = base A - (( low 1 * n 2 ) + low 2 ) * w with low 1 = 1; low 2 = 1; n 2 = 3; w = 4 A : array [1..2,1..3] of integer; (Row-major) … := A[i,j] t1 := i * 3 t1 := t1 + j t2 := c // c = base A - (1 * 3 + 1) * 4 t3 := t1 * 4 t4 := t2[t3] … := t4
  • 8. Addressing Array Elements: Grammar S  L := E E  E + E | ( E ) | L L  Elist ] | id Elist  Elist , E | id [ E Synthesized attributes: E .place name of temp holding value of E Elist .array array name Elist .place name of temp holding index value Elist .ndim number of array dimensions L .place lvalue (=name of temp) L .offset index into array (=name of temp) null indicates non-array simple id
  • 9. Addressing Array Elements S  L := E { if L .offset = null then emit ( L .place ‘:=’ E .place) else emit ( L .place[ L .offset] ‘:=’ E .place) } E  E 1 + E 2 { E .place := newtemp (); emit ( E .place ‘:=’ E 1 .place ‘+’ E 2 .place) } E  ( E 1 ) { E .place := E 1 .place } E  L { if L .offset = null then E .place := L .place else E .place := newtemp (); emit ( E .place ‘:=’ L .place[ L .offset] }
  • 10. Addressing Array Elements L  Elist ] { L .place := newtemp (); L .offset := newtemp (); emit ( L .place ‘:=’ c ( Elist .array); emit ( L .offset ‘:=’ Elist .place ‘*’ width ( Elist .array)) } L  id { L .place := id .place; L .offset := null } Elist  Elist 1 , E { t := newtemp (); m := Elist 1 .ndim + 1; emit ( t ‘:=’ Elist 1 .place ‘*’ limit ( Elist 1 .array, m )); emit ( t ‘:=’ t ‘+’ E .place); Elist .array := Elist 1 .array; Elist .place := t ; Elist .ndim := m } Elist  id [ E { Elist .array := id .place; Elist .place := E .place; Elist .ndim := 1 }
  • 11. Translating Logical and Relational Expressions a or b and not c t1 := not c t2 := b and t1 t3 := a or t2 if a < b goto L1 t1 := 0 goto L2 L1: t1 := 1 L2: a < b
  • 12. Translating Short-Circuit Expressions Using Backpatching E  E or M E | E and M E | not E | ( E ) | id relop id | true | false M   Synthesized attributes: E .truelist backpatch list for jumps on true E .falselist backpatch list for jumps on false M .quad location of current three-address quad
  • 13.
  • 14. Backpatching with Lists: Example a < b or c < d and e < f 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _ 100: if a < b goto TRUE 101: goto 102 102: if c < d goto 104 103: goto FALSE 104: if e < f goto TRUE 105: goto FALSE backpatch
  • 15. Backpatching with Lists: Translation Scheme M   { M .quad := nextquad () } E  E 1 or M E 2 { backpatch ( E 1 .falselist, M .quad); E .truelist := merge ( E 1 .truelist, E 2 .truelist); E .falselist := E 2 .falselist } E  E 1 and M E 2 { backpatch ( E 1 .truelist, M .quad); E .truelist := E 2 .truelist; E .falselist := merge ( E 1 .falselist, E 2 .falselist); } E  not E 1 { E .truelist := E 1 .falselist; E .falselist := E 1 .truelist } E  ( E 1 ) { E .truelist := E 1 .truelist; E .falselist := E 1 .falselist }
  • 16. Backpatching with Lists: Translation Scheme (cont’d) E  id 1 relop id 2 { E .truelist := makelist ( nextquad ()); E .falselist := makelist ( nextquad () + 1); emit (‘ if ’ id 1 .place relop .op id 2 .place ‘ goto _ ’); emit (‘ goto _ ’) } E  true { E .truelist := makelist ( nextquad ()); E .falselist := nil; emit (‘ goto _ ’) } E  false { E .falselist := makelist ( nextquad ()); E .truelist := nil; emit (‘ goto _ ’) }
  • 17. Flow-of-Control Statements and Backpatching: Grammar S  if E then S | if E then S else S | while E do S | begin L end | A L  L ; S | S Synthesized attributes: S .nextlist backpatch list for jumps to the next statement after S (or nil) L .nextlist backpatch list for jumps to the next statement after L (or nil) S 1 ; S 2 ; S 3 ; S 4 ; S 5 … backpatch ( S 1 .nextlist, 200) backpatch ( S 2 .nextlist, 300) backpatch ( S 3 .nextlist, 400) backpatch ( S 4 .nextlist, 500) 100: Code for S1 200: Code for S2 300: Code for S3 400: Code for S4 500: Code for S5 Jumps out of S 1
  • 18. Flow-of-Control Statements and Backpatching S  A { S .nextlist := nil } S  begin L end { S .nextlist := L .nextlist } S  if E then M S 1 { backpatch ( E .truelist, M .quad); S .nextlist := merge ( E .falselist, S 1 .nextlist) } L  L 1 ; M S { backpatch ( L 1 .nextlist, M .quad); L .nextlist := S .nextlist; } L  S { L .nextlist := S .nextlist; } M   { M .quad := nextquad () } A  … Non-compound statements, e.g. assignment, function call
  • 19. Flow-of-Control Statements and Backpatching (cont’d) S  if E then M 1 S 1 N else M 2 S 2 { backpatch ( E .truelist, M 1 .quad); backpatch ( E .falselist, M 2 .quad); S .nextlist := merge ( S 1 .nextlist, merge ( N .nextlist, S 2 .nextlist)) } S  while M 1 E do M 2 S 1 { backpatch ( S 1 ,nextlist, M 1 .quad); backpatch ( E .truelist, M 2 .quad); S .nextlist := E .falselist; emit (‘ goto M 1 .quad’) } N   { N .nextlist := makelist ( nextquad ()); emit (‘ goto _ ’) }
  • 20. Translating Procedure Calls S  call id ( Elist ) Elist  Elist , E | E call foo(a+1, b, 7) t1 := a + 1 t2 := 7 param t1 param b param t2 call foo 3
  • 21. Translating Procedure Calls S  call id ( Elist ) { for each item p on queue do emit (‘ param ’ p ); emit (‘ call ’ id .place | queue |) } Elist  Elist , E { append E .place to the end of queue } Elist  E { initialize queue to contain only E .place }