Post on 22-Jul-2020
Introduction to CompilerJungsik Choi
chjs@khu.ac.kr
Formal Languages and Compiler (CSE322)
2018. 3. 8
2
• High level functions• Recognize legal program, generate correct code (OS & linker can accept)
• Manage the storage of all variables and code
• Two passes• Use an intermediate representation (IR)
• Front end maps legal source code into IR 𝑂(𝑛) or 𝑂(𝑛 log 𝑛)
• Back end maps IR into target machine code typically NP-complete
• Admits multiple front ends & multiple passes (better code)
Traditional Two-pass Compiler
Front End Back EndTarget
ProgramSource
Program
IR
Errors
Compiler
3
• Responsibilities• Recognize legal (& illegal) programs
• Report errors in a useful way
• Produce IR & preliminary storage map
• Shape the code for the back end
• Much of front end construction can be automated
Front End
SourceProgram
IR
Errors
Scanner Parsertokens
4
• Scanner (Lexical Analysis)• Maps character stream into words (basic units of syntax)
• sum=x+y; becomes <id,sum> <eq,=> <id,x> <add,+> <id,y> <sc,;>
• word ≅ lexeme, part of speech ≅ token type
• Typical tokens include number, identifier, keyword, operator• Scanner eliminates white space and comments
• Produced by automatic scanner generator
Front End - Scanner
SourceProgram
IR
Errors
Scanner Parsertokens
5
• Parser• Recognizes context-free syntax
• Guides context-sensitive (“semantic”) analysis
• E.g. type checking
• Builds IR for source program
• Produced by automatic parser generators
Front End - Parser
SourceProgram
IR
Errors
Scanner Parsertokens
6
• Context-free syntax can be put to better use
• This grammar defines simple expressions with addition & subtraction over “number” and ‘id”
• This grammar, like many, falls in a class called “context-free grammars”, abbreviated CFG
Front End – Example (1)
1.goal → expr
2.expr → expr op term
3. | term
4.term → number
5. | id
6.op → +
7. | -
S = goal
T = {number, id, +, -}
NT = {goal, expr, term, op}
P = {1, 2, 3, 4, 5, 6, 7}
7
• A parse can be represented by a tree (parse tree or syntax tree)
Front End – Example (2)
1.goal → expr
2.expr → expr op term
3. | term
4.term → number
5. | id
6.op → +
7. | -
goal
expr
opexpr term
opexpr term
term
<id,x>
+ <number,2>
− <id,y>
𝑥 + 2 − 𝑦
• This contains a lot of unneeded information
8
• Compilers often use an Abstract Syntax Tree (AST)• AST summarizes grammatical structure, without including
detail about the derivation
• This is much more concise
• ASTs are one kind of intermediate representation (IR)
Front End – Example (3)
−
+
<id,x> <number,2>
<id,y>
9
• Responsibilities• Translate IR into target machined code
• Choose instructions to implement each IR operation
• Decide which values to keep in registers
• Find optimal order of instruction execution
• Ensure conformance with system interfaces
• Automation has been less successful in the back end
Back End
IR
Errors
InstructionSelection
IR TargetProgram
InstructionScheduling
RegisterAllocation
IR
10
• Instruction Selection• Produce fast, compact code
• Take advantage of target features such as addressing modes
• Usually viewed as a pattern matching problem
• ad hoc methods, pattern matching, dynamic programming
Back End – Instruction Selection
IR
Errors
InstructionSelection
TargetProgram
InstructionScheduling
RegisterAllocation
IR IR
11
• Instruction Scheduling• Avoid hardware stalls and interlocks
• Use all functional units productively
• Can increase lifetime of variables (changing the allocation)
• Optimal scheduling is NP-Complete in nearly all cases
• Heuristic techniques are well developed
Back End – Instruction Scheduling
IR
Errors
InstructionSelection
TargetProgram
InstructionScheduling
RegisterAllocation
IR IR
12
• Register Allocation• Have each value in a register when it is used
• Manage a limited set of resources
• Can change instruction choices & insert LOADs & STOREs
• Optimal allocation is NP-Complete
• Compilers approximate solutions to NP-Complete problems
Back End – Register Allocation
IR
Errors
InstructionSelection
TargetProgram
InstructionScheduling
RegisterAllocation
IR IR
13
• Code Optimizations• Analyzes IR and rewrites (or transforms) IR
• Primary goal is to reduce
• Execution time, Space usage, Power consumption, …
• Must preserve “meaning” of the code
Optimizing Compiler
Front End Back EndTarget
ProgramSource
Program
IR
Errors
Compiler
Middle EndIR
14
• Simple Treewalk for initial code
• Peephole matching for desired code
Instruction Selection Example
load 4 r5loadAO r0,r5 r6loadI 8 r7loadAO r0,r7 r8mult r6,r8 r9
loadAI r0,4 r5loadAI r0,8 r6mult r5,r6 r7
IDENT<a, ARP, 4>
IDENT<b, ARP, 4>
×
Tree Treewalk Code Desired Code
15
• Schedule Instructions considering• Latency
• Dependences
• Generate fast executing code
Instruction Scheduling Example
a: loadAI r0,@w r1b: add r1,r1 r1c: loadAI r0,@x r2d: mult r1,r2 r1
a
b
d
c
The Code The Precedence Graph
16
• Instruction selection assume infinite # of registers (virtual registers)
• Mapping virtual registers to physical registers
• Sometimes need register spill/fill code
Register Allocation Example
loadI 4 r1loadAO r0,r1 r2loadI 8 r3loadAO r0,r3 r4mult r2,r4 r5
6 virtual registers
loadI 4 r1loadAO r0,r1 r1loadI 8 r2loadAO r0,r2 r2mult r1,r2 r2
3 physical registers
(only r5 is used later) (r5 is renamed with r2)
17
• Front End: Process high-level programming language
• Middle End: Apply optimization for speed, power, space, …
• Back End: Produce machine-level assembly code
Summary
Front End Back EndTarget
ProgramSource
Program
IR
Errors
Compiler
Middle EndIR