SlideShare uma empresa Scribd logo
1 de 25
Presented by
Ramchandra Regmi
Roll No. IT/12/09
6th
semester
Sub:- Compiler design
Mizoram University
Introduction
Intermediate code is the interface between front end
and back end in a compiler
Ideally the details of source language are confined to
the front end and the details of target machines to the
back end.
Parser
Static
Checker
Intermediate
Code Generator
Code
Generator
Front end Back end
Intermediate code
Although a source program can be translated directly
into the target language. Some benefits of using a
machine-independent intermediate form are-
1) Retargeting is Facilitated: A compiler for a different
machine can be created by attaching a back end for
the new machine to an existing front end.
2) A machine-independent code optimizer can be applied
to the intermediate representation.
CONT….
Why intermediate code ??
4 source
language
3 target
machines
4 front ends+
4*3 optimizers+
4*3 code generators
4 front ends+
1 optimizers+
3 code generators
4 source
language
3 target
machines
Intermediate code
optimizer
Different type of Intermediate code
Intermediate code must be easy to produce and easy to
translate machine code.
A short of universal assembly language.
Should not contain any machine specific
parameters(register, address, etc.)
The type of the intermediate code deployed is based on the
application. They are-
1) Quadruples, Triples, Indirect Triples, Abstract
Syntax tree are the classical form used for machine
independent optimizations and machine code generation.
2) Static Single Assignment(SSA) is a recent form and
enables more effective for conditional constant
propagation and global constant variables.
3) Program Dependence Graph(PDG) is useful in
automatic parallelization, instruction scheduling and
software pipelining.
Three address code
Three address code is built from two concept- address and
instructions. OR
In object oriented terms, these concepts correspond to
classes, and the various kinds of addresses and instructions
correspond to appropriate subclasses.
An address can be one of the following-
i) A name- For the convenience, we allow source- program
names to appear as address in three –address code. In an
implementation, a source name is replace by the pointer to
its symbol table entry.
ii)A constant- various type of constants and variables.
iii)A compiler-generated temporary- Its useful, especially
in optimizing compilers, to create a distinct name each time
temporary is needed.
Cont.…..
Three address code is a generic form and can be
implemented as quadruples, triples , indirect triples, tree
or DAG.
And instruction are very simple i.e.
a=b+c , x=-y, if a>b goto L1 , x=y etc.
Here, LHS is the target and RHS has at most two source
and one operator.
Example- a+b*c-d/(b*c)
t1= b*c
t2=a+t1
t3=b*c
t4=d/t3
t5=t2-t4
Cont.……
Quadruples:- Its also called quad for simplicity, uses a
record structure with four fields namely, OP, ARG1,
ARG2, and RESULT.
Triples:- it’s a alternative representation of three-
address statements, which saves one completes field
present in the quadruples. This avoid entering
temporary names into the symbol table, an obvious
optimization in space.
Indirect Triples:- another implementation of three
address code maintains array of pointers to triples
rather than listing the triples themselves. This
implementation is called indirect triples because of
the nature to reference triples indirectly.
Cont.…
Advantage of indirect triples
1) The pointer are smaller than the triples and hence
move faster. And this could be used for quads and
many other recording applications(e.g Sorting large
records).
2)Since the triples do not move, the reference they
contain to past result remain accurate.
Cont..
1 t1= b*c
2 t2=a+t1
3 t3=b*c
4 t4=d/t3
5 t5=t2-t4
3 address
op arg1 arg2 Result
* b c t1
+ a t1 t2
* b c t3
/ d t3 t4
- t2 t4 t5
Quadruples
0
1
2
3
4
op arg1 arg2
* b c
+ a (0)
* b c
/ d (2)
- (1) (3)
0
1
2
3
4
Triples
op arg1 arg2
* b c
+ a (10)
* b c
/ d (12)
- (11) (13)
Indirect Triples
(10)
(11)
(12)
(13)
(14)
(10)
(11)
(12)
(13)
(14)
STMT
0
1
2
3
4
Cont.….
+
a
*
b
c b c
*
d
/ +
a
*
b
c
d
/
Syntax tree
DAG
Instruction of 3-address code-1
1. Assignment instructions
a=b biop c, a= uop b, and a=b(copy)
Where,
i) biop is any binary arithmetic, logical or relational operator.
ii) uop is any unary arithmetic (-, shift, conversion) or logical operator (~).
Conversion operators are useful for converting integers to floating point
numbers, etc.
2. Jump instructions
goto l (unconditional jump to l),
If t goto l(if t is true then jump to l),
If a relop b goto l (jump to l if a relational operation b is true).
Where,
L is the label of the next three address instruction to be executed.
t is a Boolean variable either 0 or 1.
a and b are either variable or constants .
Cont.….
3. Functions
func begin <name> (beginning of the function)
func end (end of function )
param p (place a value parameter p on stack)
refparam p (place a reference parameters p on stack).
call f, n (call the function f with n parameters )
return (return rom a function).
return a(return from a function with a value a )
4. Index copy instructions
a=b[i] (a is set to contents)
where, b is usually the base address of an array.
a[i]=b (ith location of array a set to b).
 Pointer assignments
a= &b (a is set to the address of b, i.e. a points to b).
*a= b (contents (contents(a) is set of contents(b))).
1.Operation with expressions
Translation of Expressions
Attributes S.code and E.code denote the three address code
respectively and attribute E.addr(temp) denotes the
address that will hold value of E.
When E (E1), the translation of E is the same as that of
sub-expression E1.
If E1 is computed into E1.addr and E2 is computed E2.addr,
then E1+E2 translate into t=E1.addr+E2.addr, where t is
temporary name and then E.addr set to t.
The translation of E -E1 is similar, the rules create a new
temporary for E and generate an instruction to perform the
unary minus operation.
Finally, production of E id=E; generates instructions that
assign the value of expression E to identifier id. Top.get
determine the address of the identifier represented by id.
And an assignement to the address top.get(id.lexeme) for
instance of id.
Cont.
2.Incremental Translation
Cod attribute can be quite long stings so instead of
building up E.code we can arrange generate only the
three address instructions.
In incremental approach, gen not only constructs a
three address instructions , it appends the instruction
to the sequence of instructions generated so far.
The sequence may either be retained in memory for
further processing or it may be output incrementally.
Cont…..
3. Addressing Array Elements
Generally array elements are start from o,1,2,…..,n-1.
If the width of each array element is w , then the ith
of
element of array A begins with location.
base+i*w Where
base is relative address(A[0]).
The relative address A[i1][i2] is
base + i1*w +i2*w2
Alternatively,
base + (i1+n2+i2)w
Where n number of element in array.
Cont..
Layouts for a two-dimensional array:
4. Translation of array reference
Cont..
1. L.addr denotes a temporary that is used while
computing the offset for the array reference by
summing the terms ij * wj .
2. L.array is a pointer to the symbol table entry for a
array name , l.array.base is used to determine the
actual l-value of an array reference after all the index
expressions are analyzed.
3. L.typw is the type of the subarray generated by L. for
any type t, we assume that width is given by t.width.
For any array type t , suppose that t.elem gives the
element type.
example of c-program
int a[10], b[10], dot_prod, i;
int * a1;
int *b1;
dot_prod=0;
a1=a;
b1=b;
For(i=0; i<10; i++)
dot_prod + = *a1++ * *b1++;
Intermediate code:-
dot_prod=0;
a1= &a
b1=&b
i=0
L1: if (i>=10) goto l2:
t3=*a1
t4=a1+1
a1=t4
t5=*b1
t6=b1+1
b1=t6
t7= t3*t5
t8=dot_prod +t7
dot_prod=t8
t9=i+1
i=t9
goto L1
L2:
Reference :-
1) Principles of compiler design -A.V. Aho . J.D.Ullman
Pearson Education.
2). video Lecture on Intermediate code generation (
https://youtu.be/EpAzj7zXrbk) by Prof. Y.N.
Srikanth,Department of Computer Science and
Engineering,IISc Bangalore.
3). Compiler design by Rajesh K. Maurya.
Intermediate code generation

Mais conteúdo relacionado

Mais procurados

Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Instruction Formats
Instruction FormatsInstruction Formats
Instruction FormatsRaaviKapoor
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler DesignKuppusamy P
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
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
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 

Mais procurados (20)

COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Code generation
Code generationCode generation
Code generation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Instruction Formats
Instruction FormatsInstruction Formats
Instruction Formats
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
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
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Code generator
Code generatorCode generator
Code generator
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 

Destaque (20)

Run time
Run timeRun time
Run time
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Dfa vs nfa
Dfa vs nfaDfa vs nfa
Dfa vs nfa
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
optimization of DFA
optimization of DFAoptimization of DFA
optimization of DFA
 
Nfa vs dfa
Nfa vs dfaNfa vs dfa
Nfa vs dfa
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
DFA Minimization
DFA MinimizationDFA Minimization
DFA Minimization
 
Programming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwaresProgramming languages,compiler,interpreter,softwares
Programming languages,compiler,interpreter,softwares
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Language translator
Language translatorLanguage translator
Language translator
 
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
 

Semelhante a Intermediate code generation

Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfRAnwarpasha
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course materialgadisaAdamu
 
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
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)Sourov Kumar Ron
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representationsahmed51236
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)bolovv
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 

Semelhante a Intermediate code generation (20)

Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
 
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...
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 
Ch8a
Ch8aCh8a
Ch8a
 
CC Week 11.ppt
CC Week 11.pptCC Week 11.ppt
CC Week 11.ppt
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Assignment12
Assignment12Assignment12
Assignment12
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Pointers
PointersPointers
Pointers
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 

Último

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 

Último (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

Intermediate code generation

  • 1. Presented by Ramchandra Regmi Roll No. IT/12/09 6th semester Sub:- Compiler design Mizoram University
  • 2. Introduction Intermediate code is the interface between front end and back end in a compiler Ideally the details of source language are confined to the front end and the details of target machines to the back end. Parser Static Checker Intermediate Code Generator Code Generator Front end Back end Intermediate code
  • 3. Although a source program can be translated directly into the target language. Some benefits of using a machine-independent intermediate form are- 1) Retargeting is Facilitated: A compiler for a different machine can be created by attaching a back end for the new machine to an existing front end. 2) A machine-independent code optimizer can be applied to the intermediate representation. CONT….
  • 4. Why intermediate code ?? 4 source language 3 target machines 4 front ends+ 4*3 optimizers+ 4*3 code generators 4 front ends+ 1 optimizers+ 3 code generators 4 source language 3 target machines Intermediate code optimizer
  • 5. Different type of Intermediate code Intermediate code must be easy to produce and easy to translate machine code. A short of universal assembly language. Should not contain any machine specific parameters(register, address, etc.) The type of the intermediate code deployed is based on the application. They are- 1) Quadruples, Triples, Indirect Triples, Abstract Syntax tree are the classical form used for machine independent optimizations and machine code generation. 2) Static Single Assignment(SSA) is a recent form and enables more effective for conditional constant propagation and global constant variables. 3) Program Dependence Graph(PDG) is useful in automatic parallelization, instruction scheduling and software pipelining.
  • 6. Three address code Three address code is built from two concept- address and instructions. OR In object oriented terms, these concepts correspond to classes, and the various kinds of addresses and instructions correspond to appropriate subclasses. An address can be one of the following- i) A name- For the convenience, we allow source- program names to appear as address in three –address code. In an implementation, a source name is replace by the pointer to its symbol table entry. ii)A constant- various type of constants and variables. iii)A compiler-generated temporary- Its useful, especially in optimizing compilers, to create a distinct name each time temporary is needed.
  • 7. Cont.….. Three address code is a generic form and can be implemented as quadruples, triples , indirect triples, tree or DAG. And instruction are very simple i.e. a=b+c , x=-y, if a>b goto L1 , x=y etc. Here, LHS is the target and RHS has at most two source and one operator. Example- a+b*c-d/(b*c) t1= b*c t2=a+t1 t3=b*c t4=d/t3 t5=t2-t4
  • 8. Cont.…… Quadruples:- Its also called quad for simplicity, uses a record structure with four fields namely, OP, ARG1, ARG2, and RESULT. Triples:- it’s a alternative representation of three- address statements, which saves one completes field present in the quadruples. This avoid entering temporary names into the symbol table, an obvious optimization in space. Indirect Triples:- another implementation of three address code maintains array of pointers to triples rather than listing the triples themselves. This implementation is called indirect triples because of the nature to reference triples indirectly.
  • 9. Cont.… Advantage of indirect triples 1) The pointer are smaller than the triples and hence move faster. And this could be used for quads and many other recording applications(e.g Sorting large records). 2)Since the triples do not move, the reference they contain to past result remain accurate.
  • 10. Cont.. 1 t1= b*c 2 t2=a+t1 3 t3=b*c 4 t4=d/t3 5 t5=t2-t4 3 address op arg1 arg2 Result * b c t1 + a t1 t2 * b c t3 / d t3 t4 - t2 t4 t5 Quadruples 0 1 2 3 4
  • 11. op arg1 arg2 * b c + a (0) * b c / d (2) - (1) (3) 0 1 2 3 4 Triples op arg1 arg2 * b c + a (10) * b c / d (12) - (11) (13) Indirect Triples (10) (11) (12) (13) (14) (10) (11) (12) (13) (14) STMT 0 1 2 3 4 Cont.….
  • 12. + a * b c b c * d / + a * b c d / Syntax tree DAG
  • 13. Instruction of 3-address code-1 1. Assignment instructions a=b biop c, a= uop b, and a=b(copy) Where, i) biop is any binary arithmetic, logical or relational operator. ii) uop is any unary arithmetic (-, shift, conversion) or logical operator (~). Conversion operators are useful for converting integers to floating point numbers, etc. 2. Jump instructions goto l (unconditional jump to l), If t goto l(if t is true then jump to l), If a relop b goto l (jump to l if a relational operation b is true). Where, L is the label of the next three address instruction to be executed. t is a Boolean variable either 0 or 1. a and b are either variable or constants .
  • 14. Cont.…. 3. Functions func begin <name> (beginning of the function) func end (end of function ) param p (place a value parameter p on stack) refparam p (place a reference parameters p on stack). call f, n (call the function f with n parameters ) return (return rom a function). return a(return from a function with a value a ) 4. Index copy instructions a=b[i] (a is set to contents) where, b is usually the base address of an array. a[i]=b (ith location of array a set to b).  Pointer assignments a= &b (a is set to the address of b, i.e. a points to b). *a= b (contents (contents(a) is set of contents(b))).
  • 16. Attributes S.code and E.code denote the three address code respectively and attribute E.addr(temp) denotes the address that will hold value of E. When E (E1), the translation of E is the same as that of sub-expression E1. If E1 is computed into E1.addr and E2 is computed E2.addr, then E1+E2 translate into t=E1.addr+E2.addr, where t is temporary name and then E.addr set to t. The translation of E -E1 is similar, the rules create a new temporary for E and generate an instruction to perform the unary minus operation. Finally, production of E id=E; generates instructions that assign the value of expression E to identifier id. Top.get determine the address of the identifier represented by id. And an assignement to the address top.get(id.lexeme) for instance of id. Cont.
  • 18. Cod attribute can be quite long stings so instead of building up E.code we can arrange generate only the three address instructions. In incremental approach, gen not only constructs a three address instructions , it appends the instruction to the sequence of instructions generated so far. The sequence may either be retained in memory for further processing or it may be output incrementally. Cont…..
  • 19. 3. Addressing Array Elements Generally array elements are start from o,1,2,…..,n-1. If the width of each array element is w , then the ith of element of array A begins with location. base+i*w Where base is relative address(A[0]). The relative address A[i1][i2] is base + i1*w +i2*w2 Alternatively, base + (i1+n2+i2)w Where n number of element in array.
  • 20. Cont.. Layouts for a two-dimensional array:
  • 21. 4. Translation of array reference
  • 22. Cont.. 1. L.addr denotes a temporary that is used while computing the offset for the array reference by summing the terms ij * wj . 2. L.array is a pointer to the symbol table entry for a array name , l.array.base is used to determine the actual l-value of an array reference after all the index expressions are analyzed. 3. L.typw is the type of the subarray generated by L. for any type t, we assume that width is given by t.width. For any array type t , suppose that t.elem gives the element type.
  • 23. example of c-program int a[10], b[10], dot_prod, i; int * a1; int *b1; dot_prod=0; a1=a; b1=b; For(i=0; i<10; i++) dot_prod + = *a1++ * *b1++; Intermediate code:- dot_prod=0; a1= &a b1=&b i=0 L1: if (i>=10) goto l2: t3=*a1 t4=a1+1 a1=t4 t5=*b1 t6=b1+1 b1=t6 t7= t3*t5 t8=dot_prod +t7 dot_prod=t8 t9=i+1 i=t9 goto L1 L2:
  • 24. Reference :- 1) Principles of compiler design -A.V. Aho . J.D.Ullman Pearson Education. 2). video Lecture on Intermediate code generation ( https://youtu.be/EpAzj7zXrbk) by Prof. Y.N. Srikanth,Department of Computer Science and Engineering,IISc Bangalore. 3). Compiler design by Rajesh K. Maurya.