3. Introduction
• It is the final phase in the compiler model.
• Input as a Intermediate representation of the
source program and output as equivalent
target program.
Int. code Int. codeSrc
Progm
Front
end
Code
Optimizer
Code
Generator
Symbol
Table
Target
Progm
4. Issues in Design of Code Gen.
1. Input to the Code Generator
2. Target Program
3. Memory Management
4. Instruction Section
5. Register Allocation
6. Choices of Evaluation Order
7. Approaches to Code Generation
5. 1. Input to the Code Generator
• The input to the code gen. consists of intermediate
representation of the source program
• Which is produced by the front end, together with info in the
symbol table, used to determine the run time address of
objects denoted by the names in intermediate representation.
• Three several choices for the IL :
– Linear representation such as postfix notation
– Three-address code such as quadruples.
– Graphical representation such as syntax tree and dags.
6. 2. Target Program
• Output of code gen is Target Program.
• Like Intermediate Code, this output also takes many forms:
– Absolute Machine Language
– Relocatable Machine Language
– Assembly Language.
• Producing an absolute ML has the advantage that it can be
placed in a fixed location in memory and immediately
executed.
• Compilers such as WATFIV and PL/C, produces Absolute code.
7. 2. Cont..
• On the other hand while producing relocatable ML, we gain
flexibility that we can call other programs from an object
module.
• Where as while producing assembly language program,
process of code generation is easier.
• We can generate symbolic instructions and use the macro
facilities of the assembler to help generate code.
8. 3. Memory Management
• Mapping names in the source program to addresses of data
objects in run time memory is done cooperatively by the front
end and the code generator.
• We assumed names in a Three address statement refers to
the names in symbol table entry for the name.
• If Machine code is being generated, labels in three address
statement have to be converted to addresses of instruction.
• This process is analogous in Back patching.
9. 4. Instruction Selection
• The nature of instruction set of the target ML determines the
difficulty of instruction selection.
X:=y+z
MOV y,R0
ADD z,R0
MOV R0,x
10. 5. Register Allocation
• The use of register allocation is often subdivided into two sub
problems:
– During register allocation
– During a subsequent register assignment phase.
• Certain machines register pairs
Multiplication: M x,y
Division: D x,y
t:=a+b
t:=t*b
t:=a/b
11. 6. Choices of Evaluation Order
• The order in which computations are performed can affect
the efficiency of the target code.
• Some computation orders requires fewer registers to hold
intermediate results than others.
• Picking a best order is another difficult, NP-complete problem.
12. 7. Approaches to Code Generation
• The most important criterion for a code generator is that it
produce correct code.
• Correctness takes on significance because of the no of special
cases that a code generator might face.
• Given the premium on correctness, designing a code gen so it
can be easily implemented, tested and maintained is an
important design deal.