Code Generation
Code Generation
Intermediate Intermediate
code Code code Code
Front-End
Optimizer Generator
Lexical error
Syntax error
Semantic error
Symbol Table
Requirements
• Output code must be correct
• Must be of high quality; must make
effective use of resources
• Code generator must run efficiently
• Good Vs Optimal
Issues in the design of a Code
Generator
• Input to the Code Generator
• Target Programs
• Memory Management
• Instruction Selection
• Register Allocation
• Choice of evaluation order
Input to the Code Generator
• Input
– Intermediate representation produced by the
front end
• Postfix form, quads, syntax trees, DAGs
– Symbol table
– Intermediate code available
– Type checking done
– Input is free of errors
Target Programs
• Absolute machine language
• Relocatable machine language
– Expensive
– Flexible
• Assembly language
– Easier
Memory management
• Name in 3 address stmt refers to symbol
table (width, relative address)
• Static allocation Vs Stack allocation
• Labels have to be converted to addresses
• For each quad – machine instruction
address is tracked
• Backpatching
Instruction Selection
• Uniformity and completeness of instruction set
• Instruction speed
• For each type of statement a skeleton can be
maintained
x := y + z
MOV y, R0
ADD z, R0
MOV R0, x
Absolute M M 1
Register R R 0