0% found this document useful (0 votes)
14 views24 pages

CD Chapter 6

Uploaded by

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

CD Chapter 6

Uploaded by

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

WCU

College of Engineering and Technology


Dep’t : Computer Science

Course: Compiler Design


Chapter 6: INTERMEDIATE CODE GENERATION

Instructor: Agegnehu Ashenafi (MSc.)


4.1 INTERMEDIATE LANGUAGES
There are three kinds of intermediate representations, they are
1. Syntax trees
2. Postfix notations
3. Three address code
1. Syntax trees
 A syntax tree depicts the natural hierarchical structure of a source program.
 A syntax tree for the assignment statement
4.1 INTERMEDIATE LANGUAGES
2. Postfix Notation
Postfix notation is a linearized representation of a syntax tree, it is a list of the
nodes of the tree in which a node appears immediately after its children.
The postfix notation for the syntax tree is

3. Three-Address Code
Three address code is a sequence of statements of the general form where x, y,
and z
are names, constants, or compiler-generated temporaries;
op stands for any operator, such as a fixed or floating-point arithmetic operator,
or a
logical operator on Boolean valued data.
The reason for the term "three-address code" is that each statement usually
contains
three addresses, two for the operands and one for the result. Example :
4.1. INTERMEDIATE LANGUAGES
Three address code for the syntax tree a := b* - c + b* -c is

4.2 IMPLEMENTATION OF THREE-ADDRESS STATEMENTS


A three address statement is an abstract form of intermediate code. There are three types of representations:
1. Quadruples
2. Triples
3. Indirect Triples
4.2 IMPLEMENTATION OF THREE-ADDRESS STATEMENTS
1. Quadruples
• A quadruple is a record structure with four fields, which we call op, arg1,
arg2, and result. The op field contains an internal code for the operator.
• The three-address statement x : = y op z is represented by placing y in arg 1,
z in arg2, and x in result.
• Statements with unary operators like x : = -y or x : = y do not use arg2.
• Operators like param use neither arg2 nor result.
• Conditional and unconditional jumps put the target labe1 in result.
• Quadruple for the statement a := b* - c + b* -c is
4.2 IMPLEMENTATION OF THREE-ADDRESS STATEMENTS
• The contents of fields arg1 , arg2, and result are normally pointers to the
symbol-table
• entries for the names represented by these fields. If so, temporary names
must be entered into the symbol table as they are created.
2. Triples
• Three-address statements can be represented by records with only three
fields: op,
• arg1 and arg2. Since three fields are used, this intermediate code format is
known as triples.
• The field’s arg1 and arg2, for the arguments of op, are either pointers to the
symbol table or pointers into the triple structure.
• 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.
4.2 IMPLEMENTATION OF THREE-ADDRESS STATEMENTS
3. Indirect triples
• Listing pointers to triples rather than listing the tuples themselves are called
indirect triples.
• Indirect triple representation of a := b* - c + b* -c

• Advantage: Indirect triples can save some space compared with quadruples, if
the same temporary value is used more than once.
4.3 ASSIGNMENT STATEMENTS
• Expressions can be of type Integer, real, array, and record.
• Translation scheme to produce three address code for assignments
4.3 ASSIGNMENT STATEMENTS
• The translation scheme shows how such symbol-table entries can be found.
• The lexeme for the name represented by id is given by attribute id.name.
• Operation lookup (id.name) checks if here is an entry for this occurrence of the name in
the symbol table. If so, a pointer to the entry is returned; otherwise, lookup returns nil to
indicate that no entry was found.
• E.place - the name that will hold the value of E.
• Newtemp generates a new temporary name each time a temporary is needed.
• Type Conversions within Assignment
• In practice, there would be many different types of Variables and constants, so the
compiler must either reject certain mixed-type operations or generate appropriate type
conversion instructions.
• Consider the grammar for assignment statements as above, Assume there are two types -
real and integer, with integers converted to real when necessary.
• The semantic rule for E.type associated with the Production EE + E is:
4.3 ASSIGNMENT STATEMENTS
• The complete semantic action for a production of the form E E1 + E2 is
4.4 BOOLEAN EXPRESSIONS
• Boolean expressions are composed of the boolean operators (and, or, and
not) applied to elements that are boolean variables or relational
expressions.
• In turn, relational expressions are of the form E1 relop E2 , where E1 and E2
are arithmetic expressions.
• In programming languages, boolean expressions have two primary purposes.
• They are used to compute logical values, but more often they are used as
conditional expressions in statements that alter the flow of control, such as if
then, if-then-else, or while-do statements.
• we consider boolean expressions generated by the following grammar:
4.4 BOOLEAN EXPRESSIONS
Methods of Translating Boolean Expressions
There are two principal methods of representing the value of a boolean
expression.
The first method is to encode true and false numerically.
1 is used to denote true and 0 to denote false.
The second principal method of implementing boolean expressions is by flow of
control, that is, representing the value of a boolean expression by a position
reached in a program
For example, given the expression E l or E2, if we determine that E1 is true, then
we can conclude that the entire expression is true without having to evaluate E2.
Numerical Representations
Expressions will be evaluated completely, from Left to right, in a manner similar
to arithmetic expressions.
For example, the translation for a or b and not c is the three-address sequence
4.4 BOOLEAN EXPRESSIONS
A relational expression such as a < b is equivalent to the conditional
statement if a < b then 1 else 0, which can be translated into the
three address code sequence.

A translation scheme for producing three-address code for boolean


expressions is shown in Fig. 8.20.
4.5. FLOW OF CONTROL STATEMENT

The following grammar generates the


flow of control statements, if-then,
if-then-else, and while-do statements.
Newlabel - returns a new symbolic label each time it is called
E.true - the labe1 to which control flows if E is true
E.false - the label to which control flows if E is false
S.next - a label that is attached to the first three address instruction to be
executed after the code for s.
If-Then
In translating the if-then statement S  if E then S1, a new label E.true is
created and
attached to the first three-address instruction generated for the statement as in
Fig, 8.22(a).
The code for E generates a jump to E.true if E is true and a jump to S.next if E is
false.
We therefore set E.false to S.next.
If-then-else
• In translating the if-then-else statement S if E
then S1 else S2. the code for the boolean
expression E has jumps out of it to the first
instruction of the code for S1 if E is true, and to
the first instruction of the code for S2 if E is
false, as illustrated in Fig. 8.22(b).
While - do statement
• In translating Swhile E do S1, A new label
S.begin is created and attached to the first
instruction generated for E. Another new label
E.true is attached to the first instruction for S1.
• The code for E generates a jump to this label if
E is true, a jump to S.next if E is false and set
E.false to be S.next. After the code for S1 we
place the instruction goto S.begin, which
causes a jump back to the beginning of the
code for the Boolean expression,
• Example
SYMBOL TABLE
A compiler uses a symbol table to keep track of scope and binding information about names.
The symbol table is searched every time a name is encountered in the source text. Changes to
the table occur if a new name or new information about an existing name is discovered.
A symbol-table mechanism must allow us to add new entries and find existing entries efficiently.
The two symbol-table mechanism presented in this section are linear lists and hash tables.
A linear list is the simplest to implement. but its performance is poor when the number of
entries are large.
Hashing schemes provide better performance for somewhat greater programming effort and
space overhead.
Symbol-Table Entries
Each entry in the symbol table is for the declaration of a name, The format of entries does not
have to be uniform, because the information saved about a name depends on the usage of the
name.
Each entry can be implemented as a record consisting of a sequence of consecutive words of
memory. To keep symbol-table records uniform, it may be convenient for some of the
information about a name to be kept outside the table entry, with only a pointer to this
information stored in the record.
 If there is a modest upper bound on the length of a
name, then the characters in the name can be stored
in the symbol-table entry, as in Fig. 7.32(a).
 If there is no limit on the length of a name, or if the
limit is rarely reached, the indirect scheme of Fig.
7.32(b) can be used.
 The data structures used for implementing the symbol
table are:
1. Linear list
2. Hash table
1. Linear list
 The simplest and easiest to implement data structure
for a symbol table is a linear list of records. New names
are added to the list in the order in which they are
encountered.
 The position of the end of the array is marked by the
pointer available.
 The search for a name proceeds backwards from the
end of the array to the beginning.
 When the name is located, the associated information
can be found in the words following next.
 If we reach the beginning of the array without finding
the name, a fault occurs – an expected name is not in
the table.
• To enter a name into symbol table,
we find out the hash value of the
name by applying suitable hash
function, which maps the name into
an integer between 0 to m-1, and
• Using the value generated by a hash
function as index in a hash table,
• we search the list of the symbol
table records built on that hash
index, if the name is not present in
that list we create a record for name
and insert it at the head of the list
built on that hash index.
• The retrieval of the information
associated with the name is done as
follows.
• First, the hash value of the name is
obtained and the list button on this
hash value is searched for getting
the information about the name.
Thank You !
…Question
?

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