SlideShare a Scribd company logo
Code Generation
- Principles of Compiler Design
Presenter:
PRABU.R
BP150504, II MSC CS,
PG Department of Computer Science,
Sacred Heart College, Tirupattur.
AGENDA
• Introduction
• Issues in the Design of a Code Generation.
Introduction
• It is the final phase in the compiler model.
• Input as a Intermediate representation of the
source program and output as equivalent
target program.
Int. code Int. codeSrc
Progm
Front
end
Code
Optimizer
Code
Generator
Symbol
Table
Target
Progm
Issues in Design of Code Gen.
1. Input to the Code Generator
2. Target Program
3. Memory Management
4. Instruction Section
5. Register Allocation
6. Choices of Evaluation Order
7. Approaches to Code Generation
1. Input to the Code Generator
• The input to the code gen. consists of intermediate
representation of the source program
• Which is produced by the front end, together with info in the
symbol table, used to determine the run time address of
objects denoted by the names in intermediate representation.
• Three several choices for the IL :
– Linear representation such as postfix notation
– Three-address code such as quadruples.
– Graphical representation such as syntax tree and dags.
2. Target Program
• Output of code gen is Target Program.
• Like Intermediate Code, this output also takes many forms:
– Absolute Machine Language
– Relocatable Machine Language
– Assembly Language.
• Producing an absolute ML has the advantage that it can be
placed in a fixed location in memory and immediately
executed.
• Compilers such as WATFIV and PL/C, produces Absolute code.
2. Cont..
• On the other hand while producing relocatable ML, we gain
flexibility that we can call other programs from an object
module.
• Where as while producing assembly language program,
process of code generation is easier.
• We can generate symbolic instructions and use the macro
facilities of the assembler to help generate code.
3. Memory Management
• Mapping names in the source program to addresses of data
objects in run time memory is done cooperatively by the front
end and the code generator.
• We assumed names in a Three address statement refers to
the names in symbol table entry for the name.
• If Machine code is being generated, labels in three address
statement have to be converted to addresses of instruction.
• This process is analogous in Back patching.
4. Instruction Selection
• The nature of instruction set of the target ML determines the
difficulty of instruction selection.
X:=y+z
MOV y,R0
ADD z,R0
MOV R0,x
5. Register Allocation
• The use of register allocation is often subdivided into two sub
problems:
– During register allocation
– During a subsequent register assignment phase.
• Certain machines register pairs
Multiplication: M x,y
Division: D x,y
t:=a+b
t:=t*b
t:=a/b
6. Choices of Evaluation Order
• The order in which computations are performed can affect
the efficiency of the target code.
• Some computation orders requires fewer registers to hold
intermediate results than others.
• Picking a best order is another difficult, NP-complete problem.
7. Approaches to Code Generation
• The most important criterion for a code generator is that it
produce correct code.
• Correctness takes on significance because of the no of special
cases that a code generator might face.
• Given the premium on correctness, designing a code gen so it
can be easily implemented, tested and maintained is an
important design deal.
THANK YOU

More Related Content

What's hot

Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 
Code generator
Code generatorCode generator
Code generator
Tech_MX
 

What's hot (20)

Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Design of a two pass assembler
Design of a two pass assemblerDesign of a two pass assembler
Design of a two pass assembler
 
Pass Structure of Assembler
Pass Structure of AssemblerPass Structure of Assembler
Pass Structure of Assembler
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Assemblers
AssemblersAssemblers
Assemblers
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Memory Reference Instructions
Memory Reference InstructionsMemory Reference Instructions
Memory Reference Instructions
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Code generator
Code generatorCode generator
Code generator
 
Macro Processor
Macro ProcessorMacro Processor
Macro Processor
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 

Viewers also liked

Open and closed thermodynamic system
Open and closed thermodynamic systemOpen and closed thermodynamic system
Open and closed thermodynamic system
physics101
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
bolovv
 

Viewers also liked (20)

Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Normalization
NormalizationNormalization
Normalization
 
Ch8
Ch8Ch8
Ch8
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Mca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementMca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction management
 
Normalization
NormalizationNormalization
Normalization
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Mca ii-dbms- u-i-introductory concepts of dbms
Mca ii-dbms- u-i-introductory concepts of dbmsMca ii-dbms- u-i-introductory concepts of dbms
Mca ii-dbms- u-i-introductory concepts of dbms
 
The dag representation of basic blocks
The dag representation of basic blocksThe dag representation of basic blocks
The dag representation of basic blocks
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Open and closed thermodynamic system
Open and closed thermodynamic systemOpen and closed thermodynamic system
Open and closed thermodynamic system
 
Hybrid Cloud Computing (IBM System z)
Hybrid Cloud Computing (IBM System z)Hybrid Cloud Computing (IBM System z)
Hybrid Cloud Computing (IBM System z)
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
System software and operating system
System software and operating systemSystem software and operating system
System software and operating system
 
Design And Implementation Of A Bangla Compiler
Design And Implementation Of A Bangla CompilerDesign And Implementation Of A Bangla Compiler
Design And Implementation Of A Bangla Compiler
 
Crytography
CrytographyCrytography
Crytography
 
code optimization
code optimization code optimization
code optimization
 

Similar to Code Generation

1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx
venkatapranaykumarGa
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
riddhi viradiya
 

Similar to Code Generation (20)

Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
Chapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course MaterialChapter-1.pptx compiler Design Course Material
Chapter-1.pptx compiler Design Course Material
 
Assembler
AssemblerAssembler
Assembler
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
System software 5th unit
System software 5th unitSystem software 5th unit
System software 5th unit
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Valuable Information on Lexical Analysis in Compiler Design
Valuable Information on Lexical Analysis in Compiler DesignValuable Information on Lexical Analysis in Compiler Design
Valuable Information on Lexical Analysis in Compiler Design
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Embedded computing platform design
Embedded computing platform designEmbedded computing platform design
Embedded computing platform design
 

Recently uploaded

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 

Code Generation

  • 1. Code Generation - Principles of Compiler Design Presenter: PRABU.R BP150504, II MSC CS, PG Department of Computer Science, Sacred Heart College, Tirupattur.
  • 2. AGENDA • Introduction • Issues in the Design of a Code Generation.
  • 3. Introduction • It is the final phase in the compiler model. • Input as a Intermediate representation of the source program and output as equivalent target program. Int. code Int. codeSrc Progm Front end Code Optimizer Code Generator Symbol Table Target Progm
  • 4. Issues in Design of Code Gen. 1. Input to the Code Generator 2. Target Program 3. Memory Management 4. Instruction Section 5. Register Allocation 6. Choices of Evaluation Order 7. Approaches to Code Generation
  • 5. 1. Input to the Code Generator • The input to the code gen. consists of intermediate representation of the source program • Which is produced by the front end, together with info in the symbol table, used to determine the run time address of objects denoted by the names in intermediate representation. • Three several choices for the IL : – Linear representation such as postfix notation – Three-address code such as quadruples. – Graphical representation such as syntax tree and dags.
  • 6. 2. Target Program • Output of code gen is Target Program. • Like Intermediate Code, this output also takes many forms: – Absolute Machine Language – Relocatable Machine Language – Assembly Language. • Producing an absolute ML has the advantage that it can be placed in a fixed location in memory and immediately executed. • Compilers such as WATFIV and PL/C, produces Absolute code.
  • 7. 2. Cont.. • On the other hand while producing relocatable ML, we gain flexibility that we can call other programs from an object module. • Where as while producing assembly language program, process of code generation is easier. • We can generate symbolic instructions and use the macro facilities of the assembler to help generate code.
  • 8. 3. Memory Management • Mapping names in the source program to addresses of data objects in run time memory is done cooperatively by the front end and the code generator. • We assumed names in a Three address statement refers to the names in symbol table entry for the name. • If Machine code is being generated, labels in three address statement have to be converted to addresses of instruction. • This process is analogous in Back patching.
  • 9. 4. Instruction Selection • The nature of instruction set of the target ML determines the difficulty of instruction selection. X:=y+z MOV y,R0 ADD z,R0 MOV R0,x
  • 10. 5. Register Allocation • The use of register allocation is often subdivided into two sub problems: – During register allocation – During a subsequent register assignment phase. • Certain machines register pairs Multiplication: M x,y Division: D x,y t:=a+b t:=t*b t:=a/b
  • 11. 6. Choices of Evaluation Order • The order in which computations are performed can affect the efficiency of the target code. • Some computation orders requires fewer registers to hold intermediate results than others. • Picking a best order is another difficult, NP-complete problem.
  • 12. 7. Approaches to Code Generation • The most important criterion for a code generator is that it produce correct code. • Correctness takes on significance because of the no of special cases that a code generator might face. • Given the premium on correctness, designing a code gen so it can be easily implemented, tested and maintained is an important design deal.