SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Unit-IV
A Simple Code Generator
Present By
S.Sathiya(BP150519)
Sacred Heart College(Autonomous)
Tirupattur
Simple Code Generator
Content……
 Introduction(Simple Code Generator)
 Register and Address Descriptors
A Code-Generation Algorithm
The Function getreg
Generating Code for Other Types of
Statements
Conditional Statements
Introduction
• Code Generator:
 Code generator can be considered as the
final phase of compilation.
 It generates a target code for a sequence
of three-address statement.
 It consider each statement in turn,
remembering if any of the operands of the
statement are currently in registers, and taking
advantage of that fact if possible.
Introduction
• We assume that computed results can be left in
registers an long as possible.
• Storing them only
a) if their register is needed for another
computation (or)
b) Just before a procedure call, jump, or
labeled statement
Condition (b)implies that everything must
be stored just before the end of a basic block.
Introduction
• The reason we must do so is that, after leaving a basic
block, we may be able to go to several different blocks,
or we may go to one particular block that can be
reached from several others.
• In either case, we cannot, without extra effort, assume
that a datum used by a block appears in the same
register no matter how control reached that block.
• Thus, to avoid a possible error, our simple code-
generator algorithm stores everything when moving
across basic block boundaries as well as when
procedure calls are made.
Introduction
• We can produce reasonable code for a three-
address statement a:=b+c
• If we generate the single instruction ADD Rj,Ri
with cost one
• If Ri contain b but c is in a memory location, we
can generate the sequence
Add c,Ri cost=2
(or)
Mov c,Rj
Add Rj, Ri cost=3
Register and Address Descriptors
• The code generator has to track both the
registers (for availability) and addresses
(location of values) while generating the code.
For both of them, the following two
descriptors are used:
Register descriptor
Address descriptor
Register descriptor
• Register descriptor is used to inform the code
generator about the availability of registers.
Register descriptor keeps track of values stored
in each register. Whenever a new register is
required during code generation, this
descriptor is consulted for register availability.
– Keep track of what is currently in each register.
– Initially all the registers are empty
Address descriptor
• Values of the names (identifiers) used in the program
might be stored at different locations while in
execution. Address descriptors are used to keep track of
memory locations where the values of identifiers are
stored. These locations may include CPU registers,
heaps, stacks, memory or a combination of the
mentioned locations.
– Keep track of location where current value of the name can be found
at runtime
– The location might be a register, stack, memory address or a set of
those
A Code-Generation Algorithm
• Basic blocks comprise of a sequence of three-
address instructions. Code generator takes
these sequence of instructions as input.
• For each three-address statement of the form
x:=y op z
we perform the following actions
I. Call function getReg, to decide the location
of L.
II. Determine the present location (register or
memory) of y by consulting the Address
Descriptor of y. If y is not presently in
register L, then generate the following
instruction to copy the value of y to L
MOV y’, L
where y’ represents the copied value of y.
III. Determine the present location of z using the
same method used in step 2 for y and generate
the following instruction:
OP z’, L
where z’ represents the copied value of z.
IV. Now L contains the value of y OP z, that is
intended to be assigned to x. So, if L is a register,
update its descriptor to indicate that it contains the
value of x. Update the descriptor of x to indicate
that it is stored at location L.
• If y and z has no further use, they can be given
back to the system.
The Function getreg
• Code generator uses getReg function to
determine the status of available registers and
the location of name values. getReg works as
follows:
• If variable Y is already in register R, it uses
that register.
• Else if some register R is available, it uses that
register.
• Else if both the above options are not possible,
it chooses a register that requires minimal
number of load and store instructions.
The Function getreg
1. If Y is in register (that holds no other values) and Y is
not live and has no next use after
X = Y op Z
then return register of Y for L.
2. Failing (1) return an empty register
3. Failing (2) if X has a next use in the block or op
requires register then get a register R, store its content
into M (by Mov R, M) and use it.
4. else select memory location X as L
Example
• For example, the assignment d := (a - b) + (a -
c) + (a - c) might be translated into the
following three-address code sequence:
• t1 = a – b
• t2 = a – c
• t3 = t1 + t2
• d = t3 + t2
Example
Stmt code reg desc addr desc
t1=a-b mov a,R0 R0 contains t1 t1 in R0
sub b,R0
t2=a-c mov a,R1 R0 contains t1 t1 in R0
sub c,R1 R1 contains t2 t2 in R1
t3=t1+t2 add R1,R0 R0 contains t3 t3 in R0
R1 contains t2 t2 in R1
d=t3+t2 add R1,R0 R0 contains d d in R0
mov R0,d d in R0 and
memory
Generating Code for Other Types of
Statements
• Indexed assignment
a:=b[i]
and
a[i]:=b
• Pointer assignment
a:=*p
and
*p:=a
Conditional Statements
• branch if value of R meets one of six
conditions negative, zero, positive, non-
negative, non-zero, non-positive
if X < Y goto Z Mov X, R0
Sub Y, R0
Jmp negative Z
• Condition codes: indicate whether last quantity
computed or loaded into a location is negative,
zero, or positive
Conditional Statements
• Compare instruction: sets the codes without actually
computing the value
• Cmp X, Y sets condition codes to positive if X > Y and so on
if X < Y goto Z Cmp X, Y
CJL Z
• maintain a condition code descriptor: tells the name that last
set the condition codes
X =Y + Z Mov Y,R0
if X < 0 goto L Add Z, R0
Mov R0, X
CJN L
Queries…….?

Weitere ähnliche Inhalte

Was ist angesagt?

Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMvikas dhakane
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocksishwarya516
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler designhmnasim15
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler DesignKuppusamy P
 
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 blocksMohammad Vaseem Akaram
 

Was ist angesagt? (20)

Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Run time storage
Run time storageRun time storage
Run time storage
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Loops in flow
Loops in flowLoops in flow
Loops in flow
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Code generation
Code generationCode generation
Code generation
 
Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
 
Code optimization
Code optimizationCode optimization
Code optimization
 
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
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Direct linking loaders
Direct linking loadersDirect linking loaders
Direct linking loaders
 
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
 

Andere mochten auch (17)

Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Declarations
DeclarationsDeclarations
Declarations
 
code optimization
code optimization code optimization
code optimization
 
Code generator
Code generatorCode generator
Code generator
 
1.5 problem solving using algebraic models
1.5 problem solving using algebraic models1.5 problem solving using algebraic models
1.5 problem solving using algebraic models
 
Case statements
Case statementsCase statements
Case statements
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Code optimization
Code optimization Code optimization
Code optimization
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Birch
BirchBirch
Birch
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Procedure calls(compilar)
Procedure  calls(compilar)Procedure  calls(compilar)
Procedure calls(compilar)
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Ähnlich wie Unit iv(simple code generator)

COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptssuserebb9821
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxssuserebb9821
 
Computer organization and architecture
Computer organization and architectureComputer organization and architecture
Computer organization and architectureSubesh Kumar Yadav
 
Computer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notesComputer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notesLakshmi Sarvani Videla
 
8051 addressing modes
8051 addressing modes8051 addressing modes
8051 addressing modessb108ec
 
Unit II arm 7 Instruction Set
Unit II arm 7 Instruction SetUnit II arm 7 Instruction Set
Unit II arm 7 Instruction SetDr. Pankaj Zope
 
Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...
Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...
Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...preethi3173
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Describe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdfDescribe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdfSALES97
 
Happy To Use SIMD
Happy To Use SIMDHappy To Use SIMD
Happy To Use SIMDWei-Ta Wang
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Gaditek
 
(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)Alveena Saleem
 

Ähnlich wie Unit iv(simple code generator) (20)

COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.ppt
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptx
 
Handout#10
Handout#10Handout#10
Handout#10
 
Computer organization and architecture
Computer organization and architectureComputer organization and architecture
Computer organization and architecture
 
MES_MODULE 2.pptx
MES_MODULE 2.pptxMES_MODULE 2.pptx
MES_MODULE 2.pptx
 
Computer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notesComputer Organization and 8085 microprocessor notes
Computer Organization and 8085 microprocessor notes
 
module-3.pptx
module-3.pptxmodule-3.pptx
module-3.pptx
 
8051 addressing modes
8051 addressing modes8051 addressing modes
8051 addressing modes
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Return Oriented Programming
Return Oriented ProgrammingReturn Oriented Programming
Return Oriented Programming
 
Unit II arm 7 Instruction Set
Unit II arm 7 Instruction SetUnit II arm 7 Instruction Set
Unit II arm 7 Instruction Set
 
Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...
Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...
Computer organisation and architecture jntuh 2rd year 2nd unit # central proc...
 
UNIT-3.pptx
UNIT-3.pptxUNIT-3.pptx
UNIT-3.pptx
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Instruction codes
Instruction codesInstruction codes
Instruction codes
 
Describe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdfDescribe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdf
 
Happy To Use SIMD
Happy To Use SIMDHappy To Use SIMD
Happy To Use SIMD
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)
 
(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)(246431835) instruction set principles (2) (1)
(246431835) instruction set principles (2) (1)
 
Pointer
PointerPointer
Pointer
 

Kürzlich hochgeladen

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Kürzlich hochgeladen (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Unit iv(simple code generator)

  • 1. Unit-IV A Simple Code Generator Present By S.Sathiya(BP150519) Sacred Heart College(Autonomous) Tirupattur
  • 2. Simple Code Generator Content……  Introduction(Simple Code Generator)  Register and Address Descriptors A Code-Generation Algorithm The Function getreg Generating Code for Other Types of Statements Conditional Statements
  • 3. Introduction • Code Generator:  Code generator can be considered as the final phase of compilation.  It generates a target code for a sequence of three-address statement.  It consider each statement in turn, remembering if any of the operands of the statement are currently in registers, and taking advantage of that fact if possible.
  • 4. Introduction • We assume that computed results can be left in registers an long as possible. • Storing them only a) if their register is needed for another computation (or) b) Just before a procedure call, jump, or labeled statement Condition (b)implies that everything must be stored just before the end of a basic block.
  • 5. Introduction • The reason we must do so is that, after leaving a basic block, we may be able to go to several different blocks, or we may go to one particular block that can be reached from several others. • In either case, we cannot, without extra effort, assume that a datum used by a block appears in the same register no matter how control reached that block. • Thus, to avoid a possible error, our simple code- generator algorithm stores everything when moving across basic block boundaries as well as when procedure calls are made.
  • 6. Introduction • We can produce reasonable code for a three- address statement a:=b+c • If we generate the single instruction ADD Rj,Ri with cost one • If Ri contain b but c is in a memory location, we can generate the sequence Add c,Ri cost=2 (or) Mov c,Rj Add Rj, Ri cost=3
  • 7. Register and Address Descriptors • The code generator has to track both the registers (for availability) and addresses (location of values) while generating the code. For both of them, the following two descriptors are used: Register descriptor Address descriptor
  • 8. Register descriptor • Register descriptor is used to inform the code generator about the availability of registers. Register descriptor keeps track of values stored in each register. Whenever a new register is required during code generation, this descriptor is consulted for register availability. – Keep track of what is currently in each register. – Initially all the registers are empty
  • 9. Address descriptor • Values of the names (identifiers) used in the program might be stored at different locations while in execution. Address descriptors are used to keep track of memory locations where the values of identifiers are stored. These locations may include CPU registers, heaps, stacks, memory or a combination of the mentioned locations. – Keep track of location where current value of the name can be found at runtime – The location might be a register, stack, memory address or a set of those
  • 10. A Code-Generation Algorithm • Basic blocks comprise of a sequence of three- address instructions. Code generator takes these sequence of instructions as input. • For each three-address statement of the form x:=y op z we perform the following actions
  • 11. I. Call function getReg, to decide the location of L. II. Determine the present location (register or memory) of y by consulting the Address Descriptor of y. If y is not presently in register L, then generate the following instruction to copy the value of y to L MOV y’, L where y’ represents the copied value of y.
  • 12. III. Determine the present location of z using the same method used in step 2 for y and generate the following instruction: OP z’, L where z’ represents the copied value of z. IV. Now L contains the value of y OP z, that is intended to be assigned to x. So, if L is a register, update its descriptor to indicate that it contains the value of x. Update the descriptor of x to indicate that it is stored at location L.
  • 13. • If y and z has no further use, they can be given back to the system.
  • 14. The Function getreg • Code generator uses getReg function to determine the status of available registers and the location of name values. getReg works as follows: • If variable Y is already in register R, it uses that register. • Else if some register R is available, it uses that register.
  • 15. • Else if both the above options are not possible, it chooses a register that requires minimal number of load and store instructions.
  • 16. The Function getreg 1. If Y is in register (that holds no other values) and Y is not live and has no next use after X = Y op Z then return register of Y for L. 2. Failing (1) return an empty register 3. Failing (2) if X has a next use in the block or op requires register then get a register R, store its content into M (by Mov R, M) and use it. 4. else select memory location X as L
  • 17. Example • For example, the assignment d := (a - b) + (a - c) + (a - c) might be translated into the following three-address code sequence: • t1 = a – b • t2 = a – c • t3 = t1 + t2 • d = t3 + t2
  • 18. Example Stmt code reg desc addr desc t1=a-b mov a,R0 R0 contains t1 t1 in R0 sub b,R0 t2=a-c mov a,R1 R0 contains t1 t1 in R0 sub c,R1 R1 contains t2 t2 in R1 t3=t1+t2 add R1,R0 R0 contains t3 t3 in R0 R1 contains t2 t2 in R1 d=t3+t2 add R1,R0 R0 contains d d in R0 mov R0,d d in R0 and memory
  • 19. Generating Code for Other Types of Statements • Indexed assignment a:=b[i] and a[i]:=b • Pointer assignment a:=*p and *p:=a
  • 20. Conditional Statements • branch if value of R meets one of six conditions negative, zero, positive, non- negative, non-zero, non-positive if X < Y goto Z Mov X, R0 Sub Y, R0 Jmp negative Z • Condition codes: indicate whether last quantity computed or loaded into a location is negative, zero, or positive
  • 21. Conditional Statements • Compare instruction: sets the codes without actually computing the value • Cmp X, Y sets condition codes to positive if X > Y and so on if X < Y goto Z Cmp X, Y CJL Z • maintain a condition code descriptor: tells the name that last set the condition codes X =Y + Z Mov Y,R0 if X < 0 goto L Add Z, R0 Mov R0, X CJN L