28-Code Generation - Issues-11-07-2023
28-Code Generation - Issues-11-07-2023
2. Target program
3. Instruction selection
4. Register allocation
5. Evaluation order
Code Generator in Reality
• Heuristics
• Conservative estimates
1. Input and Output
2. IR and Target Code
2. IR and Target Code (Contd…)
3. Instruction Selection
• Factors determining the complexity of the target code
1. Level of the IR
• High-level
• translate each IR statement into a sequence of machine instructions using code templates
• Low-level
• generate more efficient code sequences
3. Instruction Selection (Contd…)
• Factors determining the complexity of the target code
2. Nature of the instruction-set architecture
• Uniformity and completeness of ISA affects the code E.g. floating-point operations are
done using separate registers.
• E.g: x = y + z a=b+c
d=a+e
LD R0, b // R0 = b
LD R0, y // R0 = y (load y into register R0) ADD R0, R0, c // R0 = R0 + c
ADD R0, R0, z // R0 = R0 + z (add z to R0 ) ST a, R0 // a = R0
LD R0, a // R0 = a Redundant
ST x, R0 // x = R0 (store R0 into x)
ADD R0, R0, e // R0 = R0 + e
ST d, R0 // d = R0
3. Instruction Selection (Contd…)
• Factors determining the complexity of the target code
3. Desired quality of the generated code
• Context and amount of information to process affects the code quality
LD R0, a // R0 = a
INC A ADD R0, R0, 1 // R0 = R0 + 1
ST a, R0 // a = R0