SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
guest9f8315
 

Tendances (20)

Code generation
Code generationCode generation
Code generation
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Run time storage
Run time storageRun time storage
Run time storage
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 

En vedette

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

En vedette (18)

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
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
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
 
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
 

Similaire à 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
 

Similaire à 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
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
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
 

Dernier

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
vu2urc
 

Dernier (20)

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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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...
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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...
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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
 
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...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 

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