SUBJECT CODE
TYPE THE SUBJECT NAME HERE
UNIT NO 4
RUNTIME ENVIRONMENT
4.5 ISSUES IN CODE GENERATION
III VI
CS8602
COMPILER DESIGN
CS8602
COMPILER DESIGN
Code▪ generator converts the intermediate representation of source code into a
form that can be readily executed by the machine. A code generator is expected to
generate the correct code. Designing of code generator should be done in such a
way so that it can be easily implemented, tested and maintained.
The following issues arise during the code generation phase:
1. Input to code generator
2. Target program
3. Memory management
4. Instruction selection
5. Register allocation
6. Evaluation order
CS8602
COMPILER DESIGN
▪ to code generator:
1. Input
• The input to the code generation consists of the intermediate representation of the
source program produced by front end , together with information in the symbol
table to determine run-time addresses of the data objects denoted by the names in
the intermediate representation.
• Intermediate representation can be :
a. Linear representation such as postfix notation
b. Three address representation such as quadruples
c. Virtual machine representation such as stack machine code
d. Graphical representations such as syntax trees and dags.
e. Prior to code generation, the front end must be scanned, parsed and translated
into intermediate representation along with necessary type checking. Therefore,
input to code generation is assumed to be error-free.
CS8602
COMPILER DESIGN
3. Memory management:
• Names in the source program are mapped to addresses of data objects in run-time
memory by the front end and code generator.
• It makes use of symbol table, that is, a name in a three-address statement refers to
a symbol-table entry for the name.
• Labels in three-address statements have to be converted to addresses o
instructions. For example,
j:gotoigenerates jump instruction as follows:
* if i < j, a backward jump instruction with target address equal to location of code for
quadruple i is generated.
* if i > j, the jump is forward. We must store on a list for quadruple i the location of the
first machine instruction generated for quadruple j. When i is processed, the machine
locations for all instructions that forward jumps to i are filled.
CS8602
COMPILER DESIGN
3. Memory management:
• Names in the source program are mapped to addresses of data objects in
run-time memory by the front end and code generator.
• It makes use of symbol table, that is, a name in a three-address statement
refers to a symbol-table entry for the name.
• Labels in three-address statements have to be converted to addresses of
instructions. For example,
j:gotoigenerates jump instruction as follows:
* if i < j, a backward jump instruction with target address equal to location of code
for quadruple i is generated.
* if i > j, the jump is forward. We must store on a list for quadruple i the location of
the first machine instruction generated for quadruple j. When i is processed, the
machine locations for all instructions that forward jumps to i are filled.
CS8602
COMPILER DESIGN
4. Instruction selection:
• The instructions of target machine should be complete and uniform.
• Instruction speeds and machine idioms are important factors when efficiency of targe
program
is considered.
• The quality of the generated code is determined by its speed and size.
• The former statement can be translated into the latter statement as shown below:
a:=b+c
d:=a+e (a)
MOV b,R0
ADD c,R0
MOV R0,a (b)
MOV a,R0
ADD e,R0
MOV R0,d
CS8602
COMPILER DESIGN
5. Register allocation
• Instructions involving register operands are shorter and faster than those involving
operands in memory. The use of registers is subdivided into two subproblems :
1. Register allocation - the set of variables that will reside in registers at a point in the
program is selected.
2. Register assignment - the specific register that a value picked•
3. Certain machine requires even-odd register pairs for some operands and results. Fo
example , consider the division instruction of the form :D x, y
where, x - dividend even register in even/odd register pair y-divisor
even register holds the remainder
odd register holds the quotient
6. Evaluation order
• The order in which the computations are performed can affect the efficiency of the targe
code.
Some computation orders require fewer registers to hold intermediate results than others.