CD Unit 6
CD Unit 6
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
• 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.