Code Generation Part I: Front-End Code Optimizer Code Generator
Code Generation Part I: Front-End Code Optimizer Code Generator
Code Generation Part I: Front-End Code Optimizer Code Generator
Chapter 9
Lexical error
Syntax error
Semantic error
Symbol Table
Code Generation
• Code produced by compiler must be correct
– Source-to-target program transformation should
be semantics preserving
• Code produced by compiler should be of
high quality
– Effective use of target machine resources
– Heuristic techniques should be used to generate
good but suboptimal code, because generating
optimal code is undecidable
1
4
op source, destination
2
7
Instruction Costs
• Machine is a simple, non-super-scalar processor
with fixed instruction costs
• Realistic machines have deep pipelines, I-cache,
D-cache, etc.
• Define the cost of instruction
= 1 + cost(source-mode) + cost(destination-mode)
Examples
Instruction Selection
• Instruction selection is important to obtain
efficient code
• Suppose we translate three-address code
x:=y+z
to: MOV y,R0
MOV a,R0
ADD z,R0 a:=a+1
ADD #1,R0
MOV R0,x
MOV R0,a
Cost = 6
Better Best
3
10
11
12
4
13
Example
t:=a*b t:=a*b
t:=t+a t:=t+a
t:=t/d t:=t/d
14
15
500: …
564: MOV R0,12(SP) Store return value
572: GOTO *SP Return to caller