5.1 Issues in Code Generation
5.1 Issues in 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
• Linearized rep -Postfix form, three address
statements –quads
• Virtual machine rep –stack machine code
• Graphical rep -syntax trees, DAGs
– Front end together with information in the Symbol table
–used to determine runtime addresses of the data
objects.
– Intermediate code available –values of names
appearing in the intermediate language represented
by bits, integers, reals and pointers
– Type checking done
– Input is free of errors
Target Programs
• Absolute machine language –placed in a fixed memory
location and immediately executed.
• Relocatable machine language – subprograms to be
compiled separately.
– A set of relocatable object modules can be linked
together and loaded for execution by al inking loader.
– Expensive for linking and loading
– Flexible
• Assembly language
– It can generate symbolic instructions and use the macro facilities
of the assembler to help generate code.
– Easier
Memory management
• Name in 3 address stmt refers to symbol table (width,
relative address).
• During declaration symbol table entries are created.
• Static allocation Vs Stack allocation
• Labels in 3 address stmt have to be converted to
addresses
• For each quad – machine instruction address is tracked
• Backpatching
• j: goto i
• i<j – backward jump
• i>j – forward jump
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