SlideShare ist ein Scribd-Unternehmen logo
1 von 20
1
Intermediate-Code Generation
Ms.Hiba Elsayed 2014
2
In the analysis-synthesis model of a compiler, the front end analyzes a source
program and creates an intermediate representation, from which the back end
generates target code. Ideally, details of the source language are confined to the
front end, and details of the target machine to the back end. With a suitably
defined intermediate representation, a compiler for language i and machine j
can then be built by combining the front end for language i with the back end for
machine j. This approach to creating suite of compilers can save a considerable
amount of effort: m x n compilers can be built by writing just m front ends and n
back ends.
Logical structure of a compiler front end
3
The choice or design of an intermediate representation varies from compiler to
compiler. An intermediate representation may either be an actual language or it
may consist of internal data structures that are shared by phases of the compiler. C
is a programming language, yet it is often used as an intermediate form because it
is flexible, it compiles into efficient machine code, and its compilers are widely
available. The original C++ compiler consisted of a front end that generated C,
treating a C compiler as a back end.
Intermediate Code
“Abstract” code generated from AST
• Simplicity and Portability
– Machine independent code.
– Enables common optimizations on intermediate code.
– Machine-dependent code optimizations postponed to last phase.
4
Intermediate Code Generation
Translate from abstract-syntax trees to intermediate codes.
• Generating a low-level intermediate representation with two properties:
**It should be easy to produce
** It should be easy to translate into the target machine
• One of the popular intermediate code is three-address code. A three address
code:
• Each statement contains at most 3 operands; in addition to “: =”,
i.e., assignment, at most one operator.
• An” easy” and “universal” format that can be translated into most
assembly languages.
5
some of the basic operations which in the source program, to change in the
Assembly language:
Intermediate Forms
• Stack machine code:
Code for a “postfix” stack machine.
• Two address code:
Code of the form “add r1, r2”
• Three address code:
Code of the form “add src1, src2, dest”
Quadruples and Triples: Representations for three-address code.
6
7
Three-Address Code
In three-address code, there is at most one operator on the right side of an instruction;
that is, no built-up arithmetic expressions are permitted. Thus a source-language
expression like x+y*z might be translated into the sequence of three-address instructions
where t1 and t2 are compiler-generated temporary names. This unraveling of multi-
operator arithmetic expressions and of nested flow-of-control statements makes three-address
code desirable for target-code generation and optimization, The use of names for the
intermediate values computed by a program allows three-address code to be rearranged
easily.
Quadruples
8
The description of three-address instructions specifies the components of each
type of instruction, but it does not specify the representation of these
instructions in a data structure. In a compiler, these instructions can be
implemented as objects or as records with fields for the operator and the
operands. Three such representations are called "quadruples," LLtriples,a"n d
"indirect triples."
A quadruple (or just "quad') has four fields, which we call op, arg,, arg2,and
result. The op field contains an internal code for the operator. For instance,the
three-address instruction x = y +x is represented by placing + in op, y in arg1, x in
arg2, and x in result. The following are some exceptions to this rule:
Triples
9
A triple has only three fields, which we call op, arg1 and arg2. Note that the result
field) is used primarily for temporary names. Using triples, we refer to the result of
an operation x op y by its position, rather than by an explicit temporary name.
Thus, instead of the temporary tl , a triple representation would refer to position (0).
Parenthesized numbers represent pointers into the triple structure itself.
The DAG and triple representations of expressions are equivalent. The
equivalence ends with expressions, since syntax-tree variants and three-address
code represent control flow quite differently.
Representation of three-address code with implicit destination argument.
Example: a := a + b * -c;
10
Triples Examples:
Representations of a + a * (b - c) + (b - c) * d
11
12
13
Quadruples Examples:
14
15
Static Single- Assignment Form
Static single-assignment form (SSA) is an intermediate representation that facilitates
certain code optimizations. Two distinctive aspects distinguish SSA from three-
address code. The first is that all assignments in SSA are to variables with distinct
names; hence the term static single-assigrnent . Below Figure shows the same
intermediate program in three-address code and in static single assignment form.
Note :that subscripts distinguish each definition of variables p and q in the SSA
representation.
Figure : Intermediate program in three-address code and SSA
16
The same variable may be defined in two different control-flow paths in a
program. For example, the source program
has two control-flow paths in which the variable x gets defined. If we use different
names for x in the true part and the false part of the conditional statement, then
which name should we use in the assignment y = x * a? Here is where the second
distinctive aspect of SSA comes into play. SSA uses a notational convention called
the 4-function to combine the two definitions of x:
Here, (xl, x2) has the value xl if the control flow passes through the true
part of the conditional and the value x2 if the control flow passes through the
false part. That is to say, the $-function returns the value of its argument that
corresponds to the control-flow path that was taken to get to the assignment
statement containing the -function.
Generating 3-address code
17
Generation of Postfix Code for Boolean
Expressions
18
Code Generation for Statements
19
20
Conditional Statements

Weitere ähnliche Inhalte

Was ist angesagt?

Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
Tech_MX
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
raosir123
 

Was ist angesagt? (20)

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilers
 
Design of a two pass assembler
Design of a two pass assemblerDesign of a two pass assembler
Design of a two pass assembler
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
phases of a compiler
 phases of a compiler phases of a compiler
phases of a compiler
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
Three Address code
Three Address code Three Address code
Three Address code
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Error detection recovery
Error detection recoveryError detection recovery
Error detection recovery
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Classification of Compilers
Classification of CompilersClassification of Compilers
Classification of Compilers
 
Code generation
Code generationCode generation
Code generation
 

Andere mochten auch

Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)
Tech_MX
 

Andere mochten auch (20)

Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)
 
Programming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresProgramming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwares
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
DFA Minimization
DFA MinimizationDFA Minimization
DFA Minimization
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Dfa vs nfa
Dfa vs nfaDfa vs nfa
Dfa vs nfa
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
Nfa vs dfa
Nfa vs dfaNfa vs dfa
Nfa vs dfa
 
optimization of DFA
optimization of DFAoptimization of DFA
optimization of DFA
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Translators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreterTranslators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreter
 
Language translator
Language translatorLanguage translator
Language translator
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 

Ähnlich wie Intermediate code- generation

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
 

Ähnlich wie Intermediate code- generation (20)

Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 
Assignment12
Assignment12Assignment12
Assignment12
 
Lecture 21 22
Lecture 21 22Lecture 21 22
Lecture 21 22
 
qb unit2 solve eem201.pdf
qb unit2 solve eem201.pdfqb unit2 solve eem201.pdf
qb unit2 solve eem201.pdf
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
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...
 
Unit1 cd
Unit1 cdUnit1 cd
Unit1 cd
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT Academy
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
C program
C programC program
C program
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
Impact of indentation in programming
Impact of indentation in programmingImpact of indentation in programming
Impact of indentation in programming
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
venkatesh.pptx
venkatesh.pptxvenkatesh.pptx
venkatesh.pptx
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Intermediate code- generation

  • 2. 2 In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target code. Ideally, details of the source language are confined to the front end, and details of the target machine to the back end. With a suitably defined intermediate representation, a compiler for language i and machine j can then be built by combining the front end for language i with the back end for machine j. This approach to creating suite of compilers can save a considerable amount of effort: m x n compilers can be built by writing just m front ends and n back ends. Logical structure of a compiler front end
  • 3. 3 The choice or design of an intermediate representation varies from compiler to compiler. An intermediate representation may either be an actual language or it may consist of internal data structures that are shared by phases of the compiler. C is a programming language, yet it is often used as an intermediate form because it is flexible, it compiles into efficient machine code, and its compilers are widely available. The original C++ compiler consisted of a front end that generated C, treating a C compiler as a back end. Intermediate Code “Abstract” code generated from AST • Simplicity and Portability – Machine independent code. – Enables common optimizations on intermediate code. – Machine-dependent code optimizations postponed to last phase.
  • 4. 4 Intermediate Code Generation Translate from abstract-syntax trees to intermediate codes. • Generating a low-level intermediate representation with two properties: **It should be easy to produce ** It should be easy to translate into the target machine • One of the popular intermediate code is three-address code. A three address code: • Each statement contains at most 3 operands; in addition to “: =”, i.e., assignment, at most one operator. • An” easy” and “universal” format that can be translated into most assembly languages.
  • 5. 5 some of the basic operations which in the source program, to change in the Assembly language:
  • 6. Intermediate Forms • Stack machine code: Code for a “postfix” stack machine. • Two address code: Code of the form “add r1, r2” • Three address code: Code of the form “add src1, src2, dest” Quadruples and Triples: Representations for three-address code. 6
  • 7. 7 Three-Address Code In three-address code, there is at most one operator on the right side of an instruction; that is, no built-up arithmetic expressions are permitted. Thus a source-language expression like x+y*z might be translated into the sequence of three-address instructions where t1 and t2 are compiler-generated temporary names. This unraveling of multi- operator arithmetic expressions and of nested flow-of-control statements makes three-address code desirable for target-code generation and optimization, The use of names for the intermediate values computed by a program allows three-address code to be rearranged easily.
  • 8. Quadruples 8 The description of three-address instructions specifies the components of each type of instruction, but it does not specify the representation of these instructions in a data structure. In a compiler, these instructions can be implemented as objects or as records with fields for the operator and the operands. Three such representations are called "quadruples," LLtriples,a"n d "indirect triples." A quadruple (or just "quad') has four fields, which we call op, arg,, arg2,and result. The op field contains an internal code for the operator. For instance,the three-address instruction x = y +x is represented by placing + in op, y in arg1, x in arg2, and x in result. The following are some exceptions to this rule:
  • 9. Triples 9 A triple has only three fields, which we call op, arg1 and arg2. Note that the result field) is used primarily for temporary names. Using triples, we refer to the result of an operation x op y by its position, rather than by an explicit temporary name. Thus, instead of the temporary tl , a triple representation would refer to position (0). Parenthesized numbers represent pointers into the triple structure itself. The DAG and triple representations of expressions are equivalent. The equivalence ends with expressions, since syntax-tree variants and three-address code represent control flow quite differently. Representation of three-address code with implicit destination argument. Example: a := a + b * -c;
  • 10. 10 Triples Examples: Representations of a + a * (b - c) + (b - c) * d
  • 11. 11
  • 12. 12
  • 14. 14
  • 15. 15 Static Single- Assignment Form Static single-assignment form (SSA) is an intermediate representation that facilitates certain code optimizations. Two distinctive aspects distinguish SSA from three- address code. The first is that all assignments in SSA are to variables with distinct names; hence the term static single-assigrnent . Below Figure shows the same intermediate program in three-address code and in static single assignment form. Note :that subscripts distinguish each definition of variables p and q in the SSA representation. Figure : Intermediate program in three-address code and SSA
  • 16. 16 The same variable may be defined in two different control-flow paths in a program. For example, the source program has two control-flow paths in which the variable x gets defined. If we use different names for x in the true part and the false part of the conditional statement, then which name should we use in the assignment y = x * a? Here is where the second distinctive aspect of SSA comes into play. SSA uses a notational convention called the 4-function to combine the two definitions of x: Here, (xl, x2) has the value xl if the control flow passes through the true part of the conditional and the value x2 if the control flow passes through the false part. That is to say, the $-function returns the value of its argument that corresponds to the control-flow path that was taken to get to the assignment statement containing the -function.
  • 18. Generation of Postfix Code for Boolean Expressions 18
  • 19. Code Generation for Statements 19