0% found this document useful (0 votes)
5 views18 pages

Lecture_19 Code Generation

The document discusses code generation in programming, including the code generation algorithm and design issues such as memory management, instruction selection, and register allocation. It provides an example of translating an assignment statement into three-address code and explains the concept of basic blocks and flow graphs in code structure. Additionally, it outlines rules for creating basic blocks and their significance in flow control within programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Lecture_19 Code Generation

The document discusses code generation in programming, including the code generation algorithm and design issues such as memory management, instruction selection, and register allocation. It provides an example of translating an assignment statement into three-address code and explains the concept of basic blocks and flow graphs in code structure. Additionally, it outlines rules for creating basic blocks and their significance in flow control within programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

CODE GENERATION

Topics to be Covered

 Code Generator

 Code Generation Algorithm: An Example

 Design Issues in Code Generation

 Basic Block & Flow Graph


Code Generator
Code Generation Algorithm
Code Generation : An Example
The assignment statement d:= (a-b) + (a-c) + (a-c) can be translated into the
following sequence of three address code:
t := a – b
u := a – c
v := t + u
d := v + u
Code sequence for the example is as follows:

Statement Code Generated Register Descriptor Address Descriptor


t:= a - b MOV a, R0 R0 contains t t in R0
SUB b, R0
u:= a - c MOV a, R1 R0 contains t t in R0
SUB c, R1 R1 contains u u in R1
v:= t + u ADD R1, R0 R0 contains v u in R1
R1 contains u v in R0
d:= v + u ADD R1, R0 R0 contains d d in R0
MOV R0, d d in R0 and memory
Design Issues in Code Generation
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
Design Issues in Code Generation
The following issues arise during the code generation phase:

1. Input to Code generator:

The input to the code generator is the intermediate code generated by


the front end, along with information in the symbol table that determines
the run-time addresses of the data objects denoted by the names in the
intermediate representation.

Intermediate codes may be represented mostly in quadruples, triples,


indirect triples, Postfix notation, syntax trees, DAGs, etc.

The code generation phase just proceeds on an assumption that the input
is free from all syntactic and semantic errors, the necessary type checking
has taken place and the type-conversion operators have been inserted
wherever necessary.
Design Issues in Code Generation
Design Issues in Code Generation
3. Memory management:
Mapping the names in the source program to the addresses of data
objects is done by the front end and the code generator.
A name in the three address statements refers to the symbol table entry
for the name.
Then from the symbol table entry, a relative address can be determined
for the name.

4. Instruction and Selection:

Selecting the best instructions will improve the efficiency of the


program.
It includes the instructions that should be complete and uniform.
Instruction speeds and machine idioms also play a major role when
efficiency is considered. But if we do not care about the efficiency of the
target program then instruction selection is straightforward.
Design Issues in Code Generation
5. Register Allocation:
Use of registers makes the computations faster in comparison to that of
memory, so efficient utilization of registers is important. The use of registers
are subdivided into two subproblems:

•During Register allocation – we select only those set of variables that will
reside in the registers at each point in the program.

•During a subsequent Register assignment phase, the specific register is


picked to access the variable.

•Evaluation order – The code generator decides the order in which the
instruction will be executed. The order of computations affects the efficiency
of the target code.

Note: Among many computational orders, some will require only fewer
registers to hold the intermediate results. However, picking the best order in
the general case is a difficult NP-complete problem.
Basic Block
Basic Block
Example Of Not A Basic Block-

Three Address Code for the expression If A<B then 1 else 0 is-

(1) if A<B goto (4)


(2) T1 = 0
(3) goto (5)
(4) T1 = 1
(5) print "Hello"

Here, the statements do not execute in a sequence one after the other.
Thus, they do not form a basic block.
Rules for creating Basic Block
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.
Basic Block : An Example
Basic Block : An Example
Now, the given code can be partitioned into two basic blocks as-
Flow Graph
A flow graph is a directed graph with flow control information added to the
basic blocks.
1) The basic blocks serve as nodes of the flow graph.
2) There is a directed edge from block B1 to block B2 if B2 appears
immediately after B1 in the code.
Example:
Draw a flow graph for the following Three Address Statements :
(1) PROD = 0
(2) I = 1
(3) T2 = addr(A) – 4
(4) T4 = addr(B) – 4
(5) T1 = 4 x I
(6) T3 = T2[T1]
(7) T5 = T4[T1]
(8) T6 = T3 x T5
(9) PROD = PROD + T6
(10) I = I + 1
(11) IF I <=20 GOTO (5)
Flow Graph
Firstly, we compute the basic blocks (already done above). Secondly, we assign the
flow control information.
THANK YOU

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