formal languages and compiler (cse322)introduction to compiler jungsik choi [email protected] formal...
TRANSCRIPT
![Page 1: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/1.jpg)
Introduction to CompilerJungsik Choi
Formal Languages and Compiler (CSE322)
2018. 3. 8
![Page 2: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/2.jpg)
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
![Page 3: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/3.jpg)
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
![Page 4: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/4.jpg)
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
![Page 5: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/5.jpg)
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
![Page 6: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/6.jpg)
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}
![Page 7: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/7.jpg)
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
![Page 8: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/8.jpg)
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>
![Page 9: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/9.jpg)
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
![Page 10: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/10.jpg)
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
![Page 11: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/11.jpg)
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
![Page 12: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/12.jpg)
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
![Page 13: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/13.jpg)
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
![Page 14: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/14.jpg)
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
![Page 15: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/15.jpg)
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
![Page 16: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/16.jpg)
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)
![Page 17: Formal Languages and Compiler (CSE322)Introduction to Compiler Jungsik Choi chjs@khu.ac.kr Formal Languages and Compiler (CSE322) 2018. 3. 8 2 •High level functions •Recognize](https://reader033.vdocuments.mx/reader033/viewer/2022050220/5f655eaaae0b096ba81348f8/html5/thumbnails/17.jpg)
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