0% found this document useful (0 votes)
23 views

Chapter 5 Intermediate Code Generaration-1

Uploaded by

bhattdev7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Chapter 5 Intermediate Code Generaration-1

Uploaded by

bhattdev7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 5:

Intermediate Code Generation


Introduction
In the analysis-synthesis model of compiler ,
the front end translates a source program
into an intermediate representation from
which the back end generates target code.
What is intermediate code?
What is its importance?
Intermediate code:
During the translation of a source program
into the object code(target code) for a target
machine , a compiler may generate a middle
level language code, which is known as
intermediate code.
Reason/Benefits for using Intermediate Code
is
Because of machine independent intermediate
code, portability will be enhanced.
Retargeting is facilitated
A machine independent code optimizer can
be applied to intermediate representation.
Introduction
Intermediate code is the interface between
front end and back end in a compiler
Ideally the details of source language are
restricted to the front end and the details
of target machines to the back end
In this chapter we study intermediate
representations, static type checking and
intermediate code generation
Static Intermediate Code
Parser Check Code Generat
er Generator or
Front end Back end
Intermediate Languages
Syntax trees and postfix notion are two
kinds of intermediate representation.
A third method is Three-address Code.
A syntax tree depicts the natural hierarchical
structure of source program.
A DAG (Directed Acyclic Graph) gives the
same information but in more compact way
because common sub expression are
identified.
This way we can easily show the common
sub-expressions and then use that knowledge
during code generation
Example: a = b*-c + b*-c
Syntax tree VS DAG
assign assign

a + a +

* * *

b uminus b uminus b uminus

c c c
SDD for creating syntax tree
(Syntax Directed Definition)
Production Semantic Rules
1) S-> id:=E S.Node = mknode(‘assign’, mkleaf(id ,
id.place, E.nptr)
2) E -> E1+E2 E.Node = mknode(‘+’, E1.nptr,E2.nptr)
3) E -> E1*E2 E.Node = mknode(‘*’, E1.nptr,E2.nptr)
4) E -> -E1 E.Node = mkunode(‘uminus’, E1.nptr)
5) E-> (E1) E.node = E1.nptr
6) E -> id E.nptr = mkleaf(id, id.place)
Parse trees
Parse trees are usually generated as the next
step after lexical analysis (which turns the
source code into a series of tokens that can
be viewed as meaningful units, as opposed to
just a sequence of characters).

They are tree-like data structures that shows


how an input string of terminals (source code
tokens) has been generated by the grammar
of the language in question.
The root of the parse tree is the most
general symbol of the grammar - the start
symbol, and the interior nodes represent
non terminal symbols that the start symbol
expands to (can include the start symbol
itself), such
as expression, statement, term, function call.
Parse Tree:
The leaves are the terminals of the grammar,
the actual symbols which appear as
identifiers, Operators, keywords, and
constants in the language / input string.
Parse Tree
DAG
Draw a DAG for expression: a + a * (b – c) + (b
– c) * d.
x=(a+b)*(a+b+c)*(a+b+c+d)
Three Address Code
Explain quadruple, triple and indirect triple
with suitable example. (Dec 2023 Marks-07)
Explain various techniques used to represent
the three-address code with example.(May
2024 Marks-4)
Write three address code for a = b*-c + b*-c
(June 2023 Marks-07)
Three address code
In a three address code there is at most one
operator at the right side of an instruction
Example:
assign

a= b*-c + b*-c
a +

t1 = – c
* * t2 = b * t1
t3 = - c
uminus b uminus
t4 = b * t3
b
t5 = t2+ t4
c c a= t5
Forms of three address
instructions
 x = y op z (assignment statement for binary operation)
 x = op y (assignment statement for unary operation)
 x = y (copy statement)
 goto L (unconditional jump)
 if x relop y goto L (conditional jump)
 Procedure calls using:
 param x1
 param x2 ……
 call p,n
 y = call p,n
 x = y[i] and x[i] = y (indexed assigment)
 x = &y and x = *y and *x =y (address and pointer
assignment)
Data structures for three address
codes
Quadruples
Has four fields: op, arg1, arg2 and result
Triples
Temporaries are not used and instead
references to instructions are made
Indirect triples
In addition to triples we use a list of pointers to
triples
Quadruple
ƒ A quadruple is a record structure with four
fields: op,
arg1, arg2, and result
–The op field contains an internal code for an
operator – Statements with unary
operators do not use arg2
–ƒ The contents of fields arg1, arg2, and
result are
typically pointers to symbol table entries
– If so, temporaries must be entered into the
symbol
Example: a = b*-c + b*-c

t1 = – c
t2 = b * t1
t3 = - c
t4 = b * t3
t5 = t2+ t4
a= t5
Example: a = b*-c + b*-c
t1 = – c
t2 = b * t1
t3 = - c
t4 = b * t3
t5 = t2+ t4
a= t5
Triple
Triples:
- To avoid entering temporary names into the
symbol table, we might refer to a temporary
value by the position of the statement that
computes it.

- If we do so three-address statements can be


represented by records with only three fields:
op, arg1, arg2.
Indirect Triple
Indirect Triple:
- Another implementation of three-address
code that has been considered is that of
listing pointers to triple, rather than listing
the triples themselves.
- This implementation is naturally called
Indirect triples.
Indirect triple
Statements op arg 1 arg 2
(0) (14) (14) uminus c
(1) (15) (15) * b (14)
(2) (16) (16) uminus c
(3) (17) (17) * b (16)
(4) (18) (18) + (15) (17)
(5) (19) (19) assign a (18)
Three address code
Example t1 = minus c
t2 = b * t1
a =b * -c + b * -c t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5

Quadruples Triples Indirect Triples


op arg1 arg2 result op arg1 arg2 op op arg1 arg2
minus c t1 0 minus c (0) (14) 0 minus c
* b t1 t2 1 * b (0) (1) (1) 1 * b (0)
minus c t3 2 minus c (2) (2) 2 minus c
* b t3 t4 3 * b (2) b (2)
(3) (3) 3 *
+ t2 t4 t5 4 + (1) (3) (4) (4) 4 + (1) (3)
= t5 a 5 = a (4) (5) (5) 5 = a (4)
Annonated Parse Tree:
Annonated parse tree is a parse tree showing
the values of attributes at each node.
The process of computing the attributes
values at the nodes is called as Annonating or
decorating the parse tree.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy