SlideShare a Scribd company logo
1 of 8
Download to read offline
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 1
HANDOUT#10
Aim:
Implementation of Toy-code Generator
Theory:
The major issues in code generation for expressions are as follows:
1. Determination of an evaluation order for the operators in an expression.
2. Selection of instructions to be used in target program.
3. Use of registers and handling of partial results.
The evaluation order of operations depends on operator precedence in an obvious
way-an operator which precedes its left and right neighbors must be evaluated
before either of them. Hence, a feasible evaluation order is the order in which
operators are reduced during a bottom up parse, or the reverse of the order in
which operators are produced during a top down parse.
The choice of an instruction to be used in the target code depends on the following:
1. The type and length of each operand.
2. The addressability of each operand, i.e. where the operand is located and
how it can be accessed
The addressability of an operand indicates where an operand is located and how it
can be accessed.
A partial result is the value of some subexpression computed while evaluating an
expression. In the interest of efficiency, partial results are maintained in CPU
registers as far as possible. However, some of them have to be moved to memory if
the number of results exceeds the number of available CPU registers. An
improvement issue in the code generation is when and how to move partial results
between memory and CPU registers, and how to know which partial result is
contained in a register. We use a register descriptor to maintain information for the
latter purpose.
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 2
Operand descriptors
An operand descriptor has the following fields:
1. Attributes: Contains the subfields type and length.
2. Addressability: Specifies where the operand is located, and how it can be
accessed, It has the following two subfields:
a) Addressability code: This code takes the values 'M' (operand is in
memory),and 'R' (operand in register).Other addressability such as
address in a register('AR') and address in memory('AM') are also
possible
b) Address: Address of a memory word or a CPU register.
An operand descriptor is built for every operand participating in an expression.
Example
The code generated for the expression a * b is as follows:
MOVER AREG, A
MULT AREG, B
Three operand descriptors are used during code generation. Assuming a, b to be
integers occupying 1 memory word, these are:
Descriptor for a
Register Descriptors
A register descriptor has two fields
1. Status: Contains the code free or occupied to indicate register
status.
2. Operand descriptor #: If status = occupied, this field contains the
descriptor # for the operand contained in the register.
Register descriptors are stored in an array called Register_descriptor. One register
descriptor exists for each CPU register.
Example
(int, 1) M,addr(a)
(int, 1) M,addr(b)
(int, 1) R, addr(AREG)
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 3
The register descriptor for AREG after generating code for a*b in example would
be
This indicates that register AREG contains the operand described by descriptor #3.
Generating an instruction
When an operator opi is reduced by the parser, the function codegen is called with
opi and descriptors of its operands as parameters.
• A single instruction can be generated to evaluate opi if the descriptors
indicate that one operand is in a register and the other is in memory.
• If both operands are in memory, an instruction is generated to move one
of them into a register. This is followed by an instruction to evaluate opi.
Saving partial results
If all registers are occupied (i.e. they contain partial results) when operator opi is to
be evaluated, a register r is freed by copying its contents into a temporary location
in the memory. A register r is now used to evaluate operator opi. An array temp is
declared in the target program to hold partial results. A partial result is always
stored in the next free entry of temp. When partial result is moved to a temporary
location, the descriptor of the partial result must change. The operand descriptor #
field of the register descriptor is used to achieve this.
Example
Consider the expression a*b + c*d. After generating code for a*b, the operand and
register descriptors would be as shown in Example 8 and 9. After the partial result
a*b is moved to a temporary location, say temp [1], the operand descriptors must
become
Occupied #3
(int, 1) M,addr(a)
(int, 1) M,addr(b)
(int, 1) M,addr(temp[1])
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 4
to indicate that the value of the operand described by operand descriptor #3 (viz.
a*b) has been moved to memory location temp[1].
Figure 1 shows the complete code generation routine.
codegen(operator, opd1, opd2)
{
if opd1.addressability_code = ‘R’ /* Code generation -- case 1 */
if operator = ‘+’ generate ‘ADD AREG, opd2’ ;
/* Analogous code for other operators */
else if opd2.addressability_code = ‘R’ /* Code generation --
case 2 */
if operator = ‘+’ generate ‘ADD AREG, opd1’ ;
/* Analogous code for other operators */
else /* Code generation -- case 3 */
if Register_descr.status = ‘Occupied’ /*save partial results */
generate ( ‘MOVEM AREG, Temp[j]’ );
j = j + 1;
Operand_descr[Register_descr. Operand_descriptor#]
= (<type>, (M, Addr (Temp[j])))
/* Generate code */
generate ‘MOVER AREG, opd1’ ;
if operator = ‘+’ generate ‘ADD AREG, opd2’ ;
/* Analogous code for other operators */
/* common part – Create a new descriptor
Saying operand value is in register AREG */
i = i + 1;
Operand_descr[i] = (<type>,(‘R’, Addr(AREG)));
Register_descr = (‘Occupied’, i);
return i;
}
Figure 1: Code generation routine
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 5
Example
Code generation steps for the expression a*b+c*d are shown in figure 2, where the
superscript of an NT shows the operand descriptor # used as its attribute, and the
notation <id>v represents the token <id> constructed for symbol v.
Step No. Parsing action Code generation action
1 <id>a → F1
Build descriptor # 1
2 F1
→ T1
-
3 <id>b → F2
Build descriptor # 2
4 T1
* F2
→ T3
Generate MOVER AREG, A
MULT AREG, B
Build descriptor # 3
5 T3
→ E3
-
6 <id>c → F4
Build descriptor # 4
7 F4
→ T4
-
8 <id>d → F5
Build descriptor # 5
9 T4
* F5
→ T6
Generate MOVEM AREG,
TEMP_I
MOVER AREG, C
MULT AREG, D
Build descriptor # 6
10 E3
+ T6
→ E7
Generate ADD AREG, TEMP_I
Figure 2: Code generation actions for a*b+c*d
Figure 3(a) shows the parse tree and operand and register descriptor after step 8.
Figure 3(b) shows the parse tree and descriptor after step 9.
System Programming
Walchand Institute of Technology
Figure
Figure 4(a) shows the code generated for this string using the code genera
routine.
Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur
(a)
(b)
Figure 3: Code generation actions
a) shows the code generated for this string using the code genera
Sunita M. Dol, CSE Dept
Page 6
a) shows the code generated for this string using the code generation
System Programming
Walchand Institute of Technology
Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur
(a)
(b)
Sunita M. Dol, CSE Dept
Page 7
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 8
Figure 4: Illustration for temporary location usage
Figure 4(b) shows the code for the expression resulting from the reuse of temp[1].
Input:
Enter expression: a+b*c+d
Output:
Postfix exp is: abc*d++
MOVER AREG b
MULT c
ADD d
MOVEM AREG u
MOVER AREG a
ADD u
Conclusion:
The major issues in code generation for expressions are as follows:
1. Determination of an evaluation order for the operators in an expression
depends on operator precedence.
2. Selection of instructions to be used in target program depends on the type
& length of each operand and the addressability of each operand
3. Register descriptor is considered for the use of registers and handling of
partial results.
The Toy Code generator is implemented in C-language

More Related Content

What's hot

What's hot (20)

Handout#11
Handout#11Handout#11
Handout#11
 
Handout#07
Handout#07Handout#07
Handout#07
 
Handout#04
Handout#04Handout#04
Handout#04
 
Handout#06
Handout#06Handout#06
Handout#06
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#01
Handout#01Handout#01
Handout#01
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
 
C programming notes
C programming notesC programming notes
C programming notes
 
Oops
OopsOops
Oops
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 

Similar to Handout#10

Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
 
ITEC582-Chapter 12.pptx
ITEC582-Chapter 12.pptxITEC582-Chapter 12.pptx
ITEC582-Chapter 12.pptxSabaNaeem26
 
assembler_full_slides.ppt
assembler_full_slides.pptassembler_full_slides.ppt
assembler_full_slides.pptAshwini864432
 
Examinable Question and answer system programming
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programmingMakerere university
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdpPradeep Kumar TS
 
Intermediate Representation in Compiler Construction
Intermediate Representation in Compiler ConstructionIntermediate Representation in Compiler Construction
Intermediate Representation in Compiler Constructiontheizm1
 
Effisiensi prog atmel
Effisiensi prog atmelEffisiensi prog atmel
Effisiensi prog atmelrm_dhozooo
 
16-bit Microprocessor Design (2005)
16-bit Microprocessor Design (2005)16-bit Microprocessor Design (2005)
16-bit Microprocessor Design (2005)Susam Pal
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoMark John Lado, MIT
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 

Similar to Handout#10 (20)

Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
 
ITEC582-Chapter 12.pptx
ITEC582-Chapter 12.pptxITEC582-Chapter 12.pptx
ITEC582-Chapter 12.pptx
 
assembler_full_slides.ppt
assembler_full_slides.pptassembler_full_slides.ppt
assembler_full_slides.ppt
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Systolic, Transposed & Semi-Parallel Architectures and Programming
Systolic, Transposed & Semi-Parallel Architectures and ProgrammingSystolic, Transposed & Semi-Parallel Architectures and Programming
Systolic, Transposed & Semi-Parallel Architectures and Programming
 
Examinable Question and answer system programming
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programming
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
instruction codes
instruction codesinstruction codes
instruction codes
 
Instruction codes
Instruction codesInstruction codes
Instruction codes
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
 
Intermediate Representation in Compiler Construction
Intermediate Representation in Compiler ConstructionIntermediate Representation in Compiler Construction
Intermediate Representation in Compiler Construction
 
Effisiensi prog atmel
Effisiensi prog atmelEffisiensi prog atmel
Effisiensi prog atmel
 
instruction
instruction instruction
instruction
 
A0220105
A0220105A0220105
A0220105
 
16-bit Microprocessor Design (2005)
16-bit Microprocessor Design (2005)16-bit Microprocessor Design (2005)
16-bit Microprocessor Design (2005)
 
MPMC UNIT-2.pdf
MPMC UNIT-2.pdfMPMC UNIT-2.pdf
MPMC UNIT-2.pdf
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John Lado
 
Doc8453
Doc8453Doc8453
Doc8453
 
Cao
CaoCao
Cao
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 

More from Sunita Milind Dol (18)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment5
Assignment5Assignment5
Assignment5
 
Assignment3
Assignment3Assignment3
Assignment3
 
Assignment2
Assignment2Assignment2
Assignment2
 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 

Handout#10

  • 1. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 1 HANDOUT#10 Aim: Implementation of Toy-code Generator Theory: The major issues in code generation for expressions are as follows: 1. Determination of an evaluation order for the operators in an expression. 2. Selection of instructions to be used in target program. 3. Use of registers and handling of partial results. The evaluation order of operations depends on operator precedence in an obvious way-an operator which precedes its left and right neighbors must be evaluated before either of them. Hence, a feasible evaluation order is the order in which operators are reduced during a bottom up parse, or the reverse of the order in which operators are produced during a top down parse. The choice of an instruction to be used in the target code depends on the following: 1. The type and length of each operand. 2. The addressability of each operand, i.e. where the operand is located and how it can be accessed The addressability of an operand indicates where an operand is located and how it can be accessed. A partial result is the value of some subexpression computed while evaluating an expression. In the interest of efficiency, partial results are maintained in CPU registers as far as possible. However, some of them have to be moved to memory if the number of results exceeds the number of available CPU registers. An improvement issue in the code generation is when and how to move partial results between memory and CPU registers, and how to know which partial result is contained in a register. We use a register descriptor to maintain information for the latter purpose.
  • 2. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 2 Operand descriptors An operand descriptor has the following fields: 1. Attributes: Contains the subfields type and length. 2. Addressability: Specifies where the operand is located, and how it can be accessed, It has the following two subfields: a) Addressability code: This code takes the values 'M' (operand is in memory),and 'R' (operand in register).Other addressability such as address in a register('AR') and address in memory('AM') are also possible b) Address: Address of a memory word or a CPU register. An operand descriptor is built for every operand participating in an expression. Example The code generated for the expression a * b is as follows: MOVER AREG, A MULT AREG, B Three operand descriptors are used during code generation. Assuming a, b to be integers occupying 1 memory word, these are: Descriptor for a Register Descriptors A register descriptor has two fields 1. Status: Contains the code free or occupied to indicate register status. 2. Operand descriptor #: If status = occupied, this field contains the descriptor # for the operand contained in the register. Register descriptors are stored in an array called Register_descriptor. One register descriptor exists for each CPU register. Example (int, 1) M,addr(a) (int, 1) M,addr(b) (int, 1) R, addr(AREG)
  • 3. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 3 The register descriptor for AREG after generating code for a*b in example would be This indicates that register AREG contains the operand described by descriptor #3. Generating an instruction When an operator opi is reduced by the parser, the function codegen is called with opi and descriptors of its operands as parameters. • A single instruction can be generated to evaluate opi if the descriptors indicate that one operand is in a register and the other is in memory. • If both operands are in memory, an instruction is generated to move one of them into a register. This is followed by an instruction to evaluate opi. Saving partial results If all registers are occupied (i.e. they contain partial results) when operator opi is to be evaluated, a register r is freed by copying its contents into a temporary location in the memory. A register r is now used to evaluate operator opi. An array temp is declared in the target program to hold partial results. A partial result is always stored in the next free entry of temp. When partial result is moved to a temporary location, the descriptor of the partial result must change. The operand descriptor # field of the register descriptor is used to achieve this. Example Consider the expression a*b + c*d. After generating code for a*b, the operand and register descriptors would be as shown in Example 8 and 9. After the partial result a*b is moved to a temporary location, say temp [1], the operand descriptors must become Occupied #3 (int, 1) M,addr(a) (int, 1) M,addr(b) (int, 1) M,addr(temp[1])
  • 4. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 4 to indicate that the value of the operand described by operand descriptor #3 (viz. a*b) has been moved to memory location temp[1]. Figure 1 shows the complete code generation routine. codegen(operator, opd1, opd2) { if opd1.addressability_code = ‘R’ /* Code generation -- case 1 */ if operator = ‘+’ generate ‘ADD AREG, opd2’ ; /* Analogous code for other operators */ else if opd2.addressability_code = ‘R’ /* Code generation -- case 2 */ if operator = ‘+’ generate ‘ADD AREG, opd1’ ; /* Analogous code for other operators */ else /* Code generation -- case 3 */ if Register_descr.status = ‘Occupied’ /*save partial results */ generate ( ‘MOVEM AREG, Temp[j]’ ); j = j + 1; Operand_descr[Register_descr. Operand_descriptor#] = (<type>, (M, Addr (Temp[j]))) /* Generate code */ generate ‘MOVER AREG, opd1’ ; if operator = ‘+’ generate ‘ADD AREG, opd2’ ; /* Analogous code for other operators */ /* common part – Create a new descriptor Saying operand value is in register AREG */ i = i + 1; Operand_descr[i] = (<type>,(‘R’, Addr(AREG))); Register_descr = (‘Occupied’, i); return i; } Figure 1: Code generation routine
  • 5. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 5 Example Code generation steps for the expression a*b+c*d are shown in figure 2, where the superscript of an NT shows the operand descriptor # used as its attribute, and the notation <id>v represents the token <id> constructed for symbol v. Step No. Parsing action Code generation action 1 <id>a → F1 Build descriptor # 1 2 F1 → T1 - 3 <id>b → F2 Build descriptor # 2 4 T1 * F2 → T3 Generate MOVER AREG, A MULT AREG, B Build descriptor # 3 5 T3 → E3 - 6 <id>c → F4 Build descriptor # 4 7 F4 → T4 - 8 <id>d → F5 Build descriptor # 5 9 T4 * F5 → T6 Generate MOVEM AREG, TEMP_I MOVER AREG, C MULT AREG, D Build descriptor # 6 10 E3 + T6 → E7 Generate ADD AREG, TEMP_I Figure 2: Code generation actions for a*b+c*d Figure 3(a) shows the parse tree and operand and register descriptor after step 8. Figure 3(b) shows the parse tree and descriptor after step 9.
  • 6. System Programming Walchand Institute of Technology Figure Figure 4(a) shows the code generated for this string using the code genera routine. Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur (a) (b) Figure 3: Code generation actions a) shows the code generated for this string using the code genera Sunita M. Dol, CSE Dept Page 6 a) shows the code generated for this string using the code generation
  • 7. System Programming Walchand Institute of Technology Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur (a) (b) Sunita M. Dol, CSE Dept Page 7
  • 8. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 8 Figure 4: Illustration for temporary location usage Figure 4(b) shows the code for the expression resulting from the reuse of temp[1]. Input: Enter expression: a+b*c+d Output: Postfix exp is: abc*d++ MOVER AREG b MULT c ADD d MOVEM AREG u MOVER AREG a ADD u Conclusion: The major issues in code generation for expressions are as follows: 1. Determination of an evaluation order for the operators in an expression depends on operator precedence. 2. Selection of instructions to be used in target program depends on the type & length of each operand and the addressability of each operand 3. Register descriptor is considered for the use of registers and handling of partial results. The Toy Code generator is implemented in C-language