SlideShare a Scribd company logo
UNIT III
INTERMEDIATE CODE
GENERATION
Mrs.D.Jena Catherine Bel,
Assistant Professor, CSE
Velammal Engineering College
DAG – Construction
• A dag for a basic block has following labels on the nodes
• leaves are labeled by unique identifiers, either variable
names or constants
• interior nodes are labeled by an operator symbol
• nodes are also optionally given a sequence of identifiers for
labels
Mrs.D.Jena
Catherine
Bel
2
DAG – Construction
• Algorithm for constructing a DAG
• Input : A basic block.
• Output : DAG with
i). Label for each node; identifier for leaf node, operator for interior node.
ii). For each node a list of attached identifiers.
Do the steps (1) to (3) for each statement in the given basic block.
The statements may be of the form (i). A := B op C, (ii). A := op B, (iii) A := B.
• Step 1:
• If NODE (B) is undefined create leaf labeled B and let the NODE (B) be this node.
• In case (i), do the same for C.
• Step 2:
• In case(i)
• determine whether there is a node labeled ‘op’ with left NODE(B), right NODE(C)
• If not create such a node. Let ‘n’ be the node found or created.
In case (ii), find whether a node labeled ‘op’ with only one child NODE(B) is available or not.
• If not create such a node and let ‘n’ be the node found or created.
• In case (iii), let ‘n’ be the NODE (B).
• Step 3:
• Append A to the list of attached identifiers for node found in step 2. Finally set NODE (A) to n.
Mrs.D.Jena
Catherine
Bel
3
DAG – Construction
• Example: Consider the following Basic Block.
(1) t1 := 4*i
(2) t2 := a [ t1 ]
(3) t3 := 4*i
(4) t4 :=b [ t3 ]
(5) t5 := t2*t4
(6) t6 := prod +t5
(7) prod := t6
(8) t7 := i+1
(9) i:= t7
(10) if i<=20 goto (3)
•
Mrs.D.Jena
Catherine
Bel
4
DAG – Construction
t1
*
4 I
DAGconstruction
DAGfor the statement1
n3
n1 n2
t2
[ ]
a 4
t1
*
I
n5
n4 n1
n3
n2
For the first two
statements
Mrs.D.Jena
Catherine
Bel
5
DAG – Construction
• For first three statements
t2
[]
a 4
t1,t3
*
I
n5
n4 n1
n3
n2
Mrs.D.Jena
Catherine
Bel
6
DAG – Construction
• First four statements
t2
[ ]
a 4
t1, t3
*
I
t4
[ ]
n5
n4 n1
n3
n2
n6
n7
b
Mrs.D.Jena
Catherine
Bel
7
DAG – Construction
prod0
4
b
a 20
t4
t5
t6, prod
t7, i
(1)
t1, t3
t2
+
<=
[]
*
[]
*
+
i0
Mrs.D.Jena
Catherine
Bel
8
Application of DAG
•Automatic detection of common sub expressions.
•The values, which can be used outside the block can also be identified;
•Reconstruction of the simplified list of quadruples after eliminating
common sub expressions and assignments like A := B unless absolutely
necessary.
•
DAG – Construction
Mrs.D.Jena
Catherine
Bel
9
CODE GENERATION FROM DAG
Advantage
• Rearrange the order of the final computation sequence
to obtain an optimal code
Consider the Basic block for T4 := (A+B) – (E – (C + D))
T1 := A + B
T2 := C + D
T3 := E - T2
T4 := T1 - T3
T4
T1
T3
T2
Mrs.D.Jena
Catherine
Bel
10
CODE GENERATION FROM DAG
• Corresponding object code is
MOV A, R0
ADD B, R0
MOV C, R1
ADD D, R1
MOV R0, T1
MOV E, R0
SUB R1, R0
MOV T1, R1
SUB R0, R1
MOV R1, T4
Mrs.D.Jena
Catherine
Bel
11
CODE GENERATION FROM DAG
• Algorithm to find out the optimal ordering
While unlisted interior nodes remain do
Begin
Select an unlisted node n, all whose parents have been listed;
list n;
While the left most child “m” of “n” has no unlisted parents and is
not a leaf do begin
list m;
n := m;
End;
End;
Mrs.D.Jena
Catherine
Bel
12
CODE GENERATION FROM DAG
• The algorithm produces the optimal order in reverse. The optimal
order produced for the above DAG is as follows.
T2 := C + D
T3 := E - T2
T1 := A + B
T4 := T1 - T3
The generated code is given below.
MOV C, R0
ADD D, R0
MOV E, R1
SUB R0, R1
MOV A, R0
ADD B, R0
SUB R1, R0
MOV R0, T4
Mrs.D.Jena
Catherine
Bel
13
CODE GENERATION FROM LABELED TREE
• In labeling algorithm
• labels each node of the tree with an integer
• denotes the fewest number of registers required to evaluate the tree with no stores of
intermediate results (no need of temporary names).
Begin /* labeling algorithm */
If “n” is a leaf
then if “n” is the left most child of its parent then LABEL(n) := 1
else LABEL(n) := 0;
else begin
let n1, n2, … nk be the children of n ordered by their LABELs so that LABEL(n1) >=
LABEL(n2) >=… >= LABEL(nk);
LABEL(n) := max ( LABEL(ni) + i – 1 )
1<i<l
end;
End
If nodes are binary
Max (l1 , l2) if l1 ≠ l2
l1 + 1 if l1 = l2
Label (n) =
Mrs.D.Jena
Catherine
Bel
14
CODE GENERATION FROM LABELED
TREE
T1 := A + B
T2 := C + D
T3 := E - T2
T4 := T1 - T3
Mrs.D.Jena
Catherine
Bel
15
CODE GENERATION FROM LABELED
TREE
• Algorithm for code generation from a labeled tree
• Uses a recursive procedure named GENCODE(n), which
generates the code to evaluate the node n into a register.
• RSTACK is the register stack used by the procedure, which is initially
having all the available registers.
• TSTACK is the stack of temporary names used by the GENCODE
procedure.
• The notation X STACK is used to pop the top stack and assign
the value popped to X.
• The notation STACK X denotes push X onto STACK. TOP(STACK)
refers the value on the top of the stack.
Mrs.D.Jena
Catherine
Bel
16
CODE GENERATION FROM LABELED
TREE
• Procedure GENCODE (n)
Begin
/* case 0 */
if “n” is a left leaf representing the operand NAME and “n” is the left most child of its parent then
print ‘MOV’ || NAME || ‘,’ || TOP(RSTACK);
if “n” is an interior node with operator OP and children n1 and n2 then
• if LABEL(n2) = 0 then begin /* case 1 */
let NAME be the operand represented by n2;
GENCODE (n1);
print OP || NAME || ‘,’ || TOP(RSTACK)
end
else if 1 <= LABEL(n1) < LABEL(n2) and LABEL(n1) < r then
begin /* case 2 */
SWAP (RSTACK);
GENCODE (n2);
R =pop( RSTACK); /* n2 is evaluated in register R */
GENCODE (n1);
print OP || R || ‘,’ || TOP(RSTACK);
push(RSTACK , R);
SWAP (RSTACK);
end
Mrs.D.Jena
Catherine
Bel
17
CODE GENERATION FROM LABELED
TREE
else if 1 <= LABEL(n2) <= LABEL(n1) and LABEL(n2) < r then
begin /* case 3 */
GENCODE (n1);
R =pop( RSTACK); /* n1 is evaluated in register R */
GENCODE (n2);
print OP || TOP(RSTACK) ||’,’ || R
• push(RSTACK, R);
• end
else begin/*case 4, both labels are >total no. of registers */
GENCODE (n2);
T=pop( TSTACK);
print ‘MOV’ || TOP(RSTACK) ||’,’ || T
GENCODE (n1);
push(TSTACK, T);
print OP || T ||’,’ || TOP(RSTACK)
end;
end;
•
Mrs.D.Jena
Catherine
Bel
18
CODE GENERATION FROM LABELED
TREE
.
GENCODE(T4) (case2) [R0 R1]
GENCODE(T3) (case3) [R1 R0]
GENCODE(E) (case0) [R1 R0]
Print MOV E, R1
GENCODE(T2) (case1) [R0]
GENCODE(C) (case0) [R0]
Print MOV C, R0
Print ADD D, R0
Print SUB R0, R1
GENCODE(T1) (case1) [R0]
GENCODE(A) (case 0) [R0]
Print MOV A, R0
Print ADD B, R0
Print SUB R1, R0
Mrs.D.Jena
Catherine
Bel
19
Ad

More Related Content

What's hot (20)

Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
Piyush Rochwani
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
JeeSa Sultana
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
Dattatray Gandhmal
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
Dattatray Gandhmal
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
valuebound
 
Computer registers
Computer registersComputer registers
Computer registers
DeepikaT13
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
SomnathMore3
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
vinaykhimsuriya1
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
Self employed
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Stack
StackStack
Stack
Zaid Shabbir
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Binary codes
Binary codesBinary codes
Binary codes
Revathi Subramaniam
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
hardwired control unit ppt
hardwired control unit ppthardwired control unit ppt
hardwired control unit ppt
SushmithaAcharya7
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
Piyush Rochwani
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
JeeSa Sultana
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
valuebound
 
Computer registers
Computer registersComputer registers
Computer registers
DeepikaT13
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
vinaykhimsuriya1
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
Self employed
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 

Similar to Compiler Design Unit 3 (20)

Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
indhu mathi
 
Ch8a
Ch8aCh8a
Ch8a
kinnarshah8888
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Reetesh Gupta
 
Introduction to Algorithms- Design and analysis of Algorithms
Introduction to Algorithms- Design and analysis of AlgorithmsIntroduction to Algorithms- Design and analysis of Algorithms
Introduction to Algorithms- Design and analysis of Algorithms
premanandmit
 
Merge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering studentsMerge sort-algorithm for computer science engineering students
Merge sort-algorithm for computer science engineering students
University of Science and Technology Chitttagong
 
All homework and exams in one file.pdf
All homework and exams in one file.pdfAll homework and exams in one file.pdf
All homework and exams in one file.pdf
Ann Wera
 
course information of design analysis of alg
course information of design analysis of algcourse information of design analysis of alg
course information of design analysis of alg
Bobby Pra A
 
Introduction
IntroductionIntroduction
Introduction
pilavare
 
Chapter 09-Trees
Chapter 09-TreesChapter 09-Trees
Chapter 09-Trees
MuhammadBakri13
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
rediet43
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
MuhammadUmerIhtisham
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
Pradeep Bisht
 
Recurrences
RecurrencesRecurrences
Recurrences
Ala' Mohammad
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
Introduction of Algorithm.pdf
Introduction of Algorithm.pdfIntroduction of Algorithm.pdf
Introduction of Algorithm.pdf
LaxmiMobile1
 
b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
SyedAhsan232061
 
l1.ppt
l1.pptl1.ppt
l1.ppt
ImXaib
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
indhu mathi
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Reetesh Gupta
 
Introduction to Algorithms- Design and analysis of Algorithms
Introduction to Algorithms- Design and analysis of AlgorithmsIntroduction to Algorithms- Design and analysis of Algorithms
Introduction to Algorithms- Design and analysis of Algorithms
premanandmit
 
All homework and exams in one file.pdf
All homework and exams in one file.pdfAll homework and exams in one file.pdf
All homework and exams in one file.pdf
Ann Wera
 
course information of design analysis of alg
course information of design analysis of algcourse information of design analysis of alg
course information of design analysis of alg
Bobby Pra A
 
Introduction
IntroductionIntroduction
Introduction
pilavare
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
rediet43
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
MuhammadUmerIhtisham
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
Pradeep Bisht
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
Introduction of Algorithm.pdf
Introduction of Algorithm.pdfIntroduction of Algorithm.pdf
Introduction of Algorithm.pdf
LaxmiMobile1
 
l1.ppt
l1.pptl1.ppt
l1.ppt
ImXaib
 
Ad

More from Jena Catherine Bel D (10)

Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
Jena Catherine Bel D
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
Jena Catherine Bel D
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
Jena Catherine Bel D
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
Jena Catherine Bel D
 
Theory of Computation Unit 5
Theory of Computation Unit 5Theory of Computation Unit 5
Theory of Computation Unit 5
Jena Catherine Bel D
 
Theory of Computation Unit 4
Theory of Computation Unit 4Theory of Computation Unit 4
Theory of Computation Unit 4
Jena Catherine Bel D
 
Theory of Computation Unit 3
Theory of Computation Unit 3Theory of Computation Unit 3
Theory of Computation Unit 3
Jena Catherine Bel D
 
Theory of Computation Unit 2
Theory of Computation Unit 2Theory of Computation Unit 2
Theory of Computation Unit 2
Jena Catherine Bel D
 
Theory of Computation Unit 1
Theory of Computation Unit 1Theory of Computation Unit 1
Theory of Computation Unit 1
Jena Catherine Bel D
 
Automata
AutomataAutomata
Automata
Jena Catherine Bel D
 
Ad

Recently uploaded (20)

SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
David Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And PythonDavid Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And Python
David Boutry
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
David Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And PythonDavid Boutry - Specializes In AWS, Microservices And Python
David Boutry - Specializes In AWS, Microservices And Python
David Boutry
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 

Compiler Design Unit 3

  • 1. UNIT III INTERMEDIATE CODE GENERATION Mrs.D.Jena Catherine Bel, Assistant Professor, CSE Velammal Engineering College
  • 2. DAG – Construction • A dag for a basic block has following labels on the nodes • leaves are labeled by unique identifiers, either variable names or constants • interior nodes are labeled by an operator symbol • nodes are also optionally given a sequence of identifiers for labels Mrs.D.Jena Catherine Bel 2
  • 3. DAG – Construction • Algorithm for constructing a DAG • Input : A basic block. • Output : DAG with i). Label for each node; identifier for leaf node, operator for interior node. ii). For each node a list of attached identifiers. Do the steps (1) to (3) for each statement in the given basic block. The statements may be of the form (i). A := B op C, (ii). A := op B, (iii) A := B. • Step 1: • If NODE (B) is undefined create leaf labeled B and let the NODE (B) be this node. • In case (i), do the same for C. • Step 2: • In case(i) • determine whether there is a node labeled ‘op’ with left NODE(B), right NODE(C) • If not create such a node. Let ‘n’ be the node found or created. In case (ii), find whether a node labeled ‘op’ with only one child NODE(B) is available or not. • If not create such a node and let ‘n’ be the node found or created. • In case (iii), let ‘n’ be the NODE (B). • Step 3: • Append A to the list of attached identifiers for node found in step 2. Finally set NODE (A) to n. Mrs.D.Jena Catherine Bel 3
  • 4. DAG – Construction • Example: Consider the following Basic Block. (1) t1 := 4*i (2) t2 := a [ t1 ] (3) t3 := 4*i (4) t4 :=b [ t3 ] (5) t5 := t2*t4 (6) t6 := prod +t5 (7) prod := t6 (8) t7 := i+1 (9) i:= t7 (10) if i<=20 goto (3) • Mrs.D.Jena Catherine Bel 4
  • 5. DAG – Construction t1 * 4 I DAGconstruction DAGfor the statement1 n3 n1 n2 t2 [ ] a 4 t1 * I n5 n4 n1 n3 n2 For the first two statements Mrs.D.Jena Catherine Bel 5
  • 6. DAG – Construction • For first three statements t2 [] a 4 t1,t3 * I n5 n4 n1 n3 n2 Mrs.D.Jena Catherine Bel 6
  • 7. DAG – Construction • First four statements t2 [ ] a 4 t1, t3 * I t4 [ ] n5 n4 n1 n3 n2 n6 n7 b Mrs.D.Jena Catherine Bel 7
  • 8. DAG – Construction prod0 4 b a 20 t4 t5 t6, prod t7, i (1) t1, t3 t2 + <= [] * [] * + i0 Mrs.D.Jena Catherine Bel 8
  • 9. Application of DAG •Automatic detection of common sub expressions. •The values, which can be used outside the block can also be identified; •Reconstruction of the simplified list of quadruples after eliminating common sub expressions and assignments like A := B unless absolutely necessary. • DAG – Construction Mrs.D.Jena Catherine Bel 9
  • 10. CODE GENERATION FROM DAG Advantage • Rearrange the order of the final computation sequence to obtain an optimal code Consider the Basic block for T4 := (A+B) – (E – (C + D)) T1 := A + B T2 := C + D T3 := E - T2 T4 := T1 - T3 T4 T1 T3 T2 Mrs.D.Jena Catherine Bel 10
  • 11. CODE GENERATION FROM DAG • Corresponding object code is MOV A, R0 ADD B, R0 MOV C, R1 ADD D, R1 MOV R0, T1 MOV E, R0 SUB R1, R0 MOV T1, R1 SUB R0, R1 MOV R1, T4 Mrs.D.Jena Catherine Bel 11
  • 12. CODE GENERATION FROM DAG • Algorithm to find out the optimal ordering While unlisted interior nodes remain do Begin Select an unlisted node n, all whose parents have been listed; list n; While the left most child “m” of “n” has no unlisted parents and is not a leaf do begin list m; n := m; End; End; Mrs.D.Jena Catherine Bel 12
  • 13. CODE GENERATION FROM DAG • The algorithm produces the optimal order in reverse. The optimal order produced for the above DAG is as follows. T2 := C + D T3 := E - T2 T1 := A + B T4 := T1 - T3 The generated code is given below. MOV C, R0 ADD D, R0 MOV E, R1 SUB R0, R1 MOV A, R0 ADD B, R0 SUB R1, R0 MOV R0, T4 Mrs.D.Jena Catherine Bel 13
  • 14. CODE GENERATION FROM LABELED TREE • In labeling algorithm • labels each node of the tree with an integer • denotes the fewest number of registers required to evaluate the tree with no stores of intermediate results (no need of temporary names). Begin /* labeling algorithm */ If “n” is a leaf then if “n” is the left most child of its parent then LABEL(n) := 1 else LABEL(n) := 0; else begin let n1, n2, … nk be the children of n ordered by their LABELs so that LABEL(n1) >= LABEL(n2) >=… >= LABEL(nk); LABEL(n) := max ( LABEL(ni) + i – 1 ) 1<i<l end; End If nodes are binary Max (l1 , l2) if l1 ≠ l2 l1 + 1 if l1 = l2 Label (n) = Mrs.D.Jena Catherine Bel 14
  • 15. CODE GENERATION FROM LABELED TREE T1 := A + B T2 := C + D T3 := E - T2 T4 := T1 - T3 Mrs.D.Jena Catherine Bel 15
  • 16. CODE GENERATION FROM LABELED TREE • Algorithm for code generation from a labeled tree • Uses a recursive procedure named GENCODE(n), which generates the code to evaluate the node n into a register. • RSTACK is the register stack used by the procedure, which is initially having all the available registers. • TSTACK is the stack of temporary names used by the GENCODE procedure. • The notation X STACK is used to pop the top stack and assign the value popped to X. • The notation STACK X denotes push X onto STACK. TOP(STACK) refers the value on the top of the stack. Mrs.D.Jena Catherine Bel 16
  • 17. CODE GENERATION FROM LABELED TREE • Procedure GENCODE (n) Begin /* case 0 */ if “n” is a left leaf representing the operand NAME and “n” is the left most child of its parent then print ‘MOV’ || NAME || ‘,’ || TOP(RSTACK); if “n” is an interior node with operator OP and children n1 and n2 then • if LABEL(n2) = 0 then begin /* case 1 */ let NAME be the operand represented by n2; GENCODE (n1); print OP || NAME || ‘,’ || TOP(RSTACK) end else if 1 <= LABEL(n1) < LABEL(n2) and LABEL(n1) < r then begin /* case 2 */ SWAP (RSTACK); GENCODE (n2); R =pop( RSTACK); /* n2 is evaluated in register R */ GENCODE (n1); print OP || R || ‘,’ || TOP(RSTACK); push(RSTACK , R); SWAP (RSTACK); end Mrs.D.Jena Catherine Bel 17
  • 18. CODE GENERATION FROM LABELED TREE else if 1 <= LABEL(n2) <= LABEL(n1) and LABEL(n2) < r then begin /* case 3 */ GENCODE (n1); R =pop( RSTACK); /* n1 is evaluated in register R */ GENCODE (n2); print OP || TOP(RSTACK) ||’,’ || R • push(RSTACK, R); • end else begin/*case 4, both labels are >total no. of registers */ GENCODE (n2); T=pop( TSTACK); print ‘MOV’ || TOP(RSTACK) ||’,’ || T GENCODE (n1); push(TSTACK, T); print OP || T ||’,’ || TOP(RSTACK) end; end; • Mrs.D.Jena Catherine Bel 18
  • 19. CODE GENERATION FROM LABELED TREE . GENCODE(T4) (case2) [R0 R1] GENCODE(T3) (case3) [R1 R0] GENCODE(E) (case0) [R1 R0] Print MOV E, R1 GENCODE(T2) (case1) [R0] GENCODE(C) (case0) [R0] Print MOV C, R0 Print ADD D, R0 Print SUB R0, R1 GENCODE(T1) (case1) [R0] GENCODE(A) (case 0) [R0] Print MOV A, R0 Print ADD B, R0 Print SUB R1, R0 Mrs.D.Jena Catherine Bel 19
  翻译: