0% found this document useful (0 votes)
18 views27 pages

CD Unit 6

Compiler design

Uploaded by

Sneha Giri
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)
18 views27 pages

CD Unit 6

Compiler design

Uploaded by

Sneha Giri
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/ 27

System Software

Unit-6
Code Generation
Issues in Design of a Code generator
In the code generation phase, various issues can
arises:
1.Input to the code generator
2.Target program
3.Memory management
4.Instruction selection
5.Register allocation
6.Evaluation order
1.Input to the code generator
• The input to the code generator contains the
intermediate representation of the source program and
the information of the symbol table. The source
program is produced by the front end.
• Intermediate representation has the several choices:
a) Postfix notation
b) Syntax tree
c) Three address code
• We assume front end produces low-level intermediate
representation i.e. values of names in it can directly
manipulated by the machine instructions.
• The code generation phase needs complete error-free
intermediate code as an input requires.
2. Target program:
• The target program is the output of the code
generator. The output can be:
• a) Assembly language: It allows subprogram
to be separately compiled.
• b) Relocatable machine language: It makes
the process of code generation easier.
• c) Absolute machine language: It can be
placed in a fixed location in memory and can
be executed immediately.
3. Memory management
• During code generation process the symbol table entries
have to be mapped to actual addresses and levels have to
be mapped to instruction address.
• Mapping name in the source program to address of data is
co-operating done by the front end and code generator.
• Local variables are stack allocation in the activation record
while global variables are in static area.

4. Instruction selection:
• Nature of instruction set of the target machine should be
complete and uniform.
• When you consider the efficiency of target machine then
the instruction speed and machine idioms are important
factors.
• The quality of the generated code can be determined by its
speed and size.
5. Register allocation

• Register can be accessed faster than memory. The


instructions involving operands in register are
shorter and faster than those involving in
memory operand.
• The following sub problems arise when we use
registers:
• Register allocation: In register allocation, we
select the set of variables that will reside in
register.
• Register assignment: In Register assignment, we
pick the register that contains variable.
• 6. Evaluation order
• The efficiency of the target code can be
affected by the order in which the
computations are performed. Some
computation orders need fewer registers to
hold results of intermediate than others.
Basic block
• Basic block is a set of statements that always
executes in a sequence one after the other.
• The characteristics of basic blocks are-
1. They do not contain any kind of jump
statements in them.
2. There is no possibility of branching or getting
halt in the middle.
3. All the statements execute in the same order
they appear.
4. They do not lose the flow control of the
program.
Example Of Basic Block-

Three Address Code for the expression a = b + c + d is-

• Here,
• All the statements execute in a sequence one after the
other.
• Thus, they form a basic block.
• Three Address Code for the expression If A<B
then 1 else 0 is-

• Here,
• The statements do not execute in a sequence
one after the other.
• Thus, they do not form a basic block.
Partitioning Intermediate Code Into Basic Blocks-
• Any given code can be partitioned into basic blocks using
the following rules-
• Rule-01: Determining Leaders-
Following statements of the code are called as Leaders–
• First statement of the code.
• Statement that is a target of the conditional or
unconditional goto statement.
• Statement that appears immediately after a goto
statement.

• Rule-02: Determining Basic Blocks-


• All the statements that follow the leader (including the
leader) till the next leader appears form one basic block.
• The first statement of the code is called as the first leader.
• The block containing the first leader is called as Initial block.
Flow Graphs-

• A flow graph is a directed graph with flow


control information added to the basic blocks.
• The basic blocks serve as nodes of the flow
graph.
• There is a directed edge from block B1 to
block B2 if B2 appears immediately after B1 in
the code.
• Consider the code fragment of source code which
computes the dot product of two vectors a and b
of length 20.write the 3 address code computing
dot product.
begin
PROD := 0;
I := 1;
do begin
PROD = PROD +a[I]*b[I];
I=I+1;
End
While I<=20
End.
begin
PROD := 0;
I := 1;
•REFER
do begin
PROD = PROD +a[I]*b[I];
I=I=1;
CLASS
End
While I<=20
End.
Notes
Optimization of Basic Blocks:

• There are two type of basic block


optimization. These are as follows:
1.Structure-Preserving Transformations
2.Algebraic Transformations
1. Structure preserving
transformations:
The primary Structure-Preserving
Transformation on basic blocks is as follows:
• Common sub-expression elimination
• Dead code elimination
• Renaming of temporary variables
• Interchange of two independent adjacent
statements
(a) Common sub-expression elimination:
• In the common sub-expression, you don't
need to be computed it over and over again.
Instead of this you can compute it once and
kept in store from where it's referenced when
encountered again.
(b) Dead-code elimination
• It is possible that a program contains a large
amount of dead code.
• This can be caused when once declared and
defined once and forget to remove them in
this case they serve no purpose.
• Suppose the statement x:= y + z appears in a
block and x is dead symbol that means it will
never subsequently used. Then without
changing the value of the basic block you can
safely remove this statement.
(c) Renaming temporary variables
• A statement t:= b + c can be changed to u:= b
+ c where t is a temporary variable and u is a
new temporary variable. All the instance of t
can be replaced with the u without changing
the basic block value.
(d) Interchange of statement
Suppose a block has the following two adjacent
statements:
1.t1 : = b + c
2.t2 : = x + y
• These two statements can be interchanged
without affecting the value of block when
value of t1 does not affect the value of t2.
Peephole Optimization

• Peephole optimization is a type of Code


Optimization performed on a small part of the code. It is
performed on the very small set of instructions in a
segment of code.

• It basically works on the theory of replacement in which a


part of code is replaced by shorter and faster code without
change in output.

• Peephole is the machine dependent optimization.


The objective of peephole optimization is:
1. To improve performance
2. To reduce memory footprint
3. To reduce code size

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