Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract...
-
Upload
maude-dorothy-dorsey -
Category
Documents
-
view
223 -
download
4
Transcript of Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract...
![Page 1: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/1.jpg)
Compiler course
1. Introduction
![Page 2: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/2.jpg)
Outline
• Scope of the course
• Disciplines involved in it
• Abstract view for a compiler
• Front-end and back-end tasks
• Modules
![Page 3: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/3.jpg)
Course scope
• Aim:– To learn techniques of a modern compiler
• Main reference:– Compilers – Principles, Techniques and Tools, Second
Edition by Alfred V. Aho, Ravi Sethi, Jeffery D. Ullman
• Supplementary references:– Modern compiler construction in Java 2nd edition
– Advanced Compiler Design and Implementation by Muchnick
![Page 4: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/4.jpg)
Subjects
• Lexical analysis (Scanning)• Syntax Analysis (Parsing)• Syntax Directed Translation• Intermediate Code Generation• Run-time environments• Code Generation• Machine Independent Optimization
![Page 5: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/5.jpg)
Grading policy
• Midterm (4-5 points)
• Final exam (7-9 points)
• Home-works (2-3 points)
• Term project (4-5 points)
![Page 6: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/6.jpg)
Compiler learning
• Isn’t it an old discipline?– Yes, it is a well-established discipline– Algorithms, methods and techniques are researched and
developed in early stages of computer science growth– There are many compilers around and many tools to
generate them automatically
• So, why we need to learn it?– Although you may never write a full compiler– But the techniques we learn is useful in many tasks like
writing an interpreter for a scripting language, validation checking for forms and so on
![Page 7: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/7.jpg)
Terminology
• Compiler:– a program that translates an executable program in one
language into an executable program in another language
– we expect the program produced by the compiler to be better, in some way, than the original
• Interpreter:– a program that reads an executable program and
produces the results of running that program– usually, this involves executing the source program in
some fashion• Our course is mainly about compilers but many of
the same issues arise in interpreters
![Page 8: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/8.jpg)
Disciplines involved
• Algorithms
• Languages and machines
• Operating systems
• Computer architectures
![Page 9: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/9.jpg)
Abstract view
• Recognizes legal (and illegal) programs• Generate correct code• Manage storage of all variables and code• Agreement on format for object (or
assembly) code
CompilerSourcecode
Machinecode
errors
![Page 10: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/10.jpg)
Front-end, Back-end division
• Front end maps legal code into IR• Back end maps IR onto target machine• Simplify retargeting• Allows multiple front ends• Multiple passes -> better code
Front endSourcecode
Machinecode
errors
IRBack end
![Page 11: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/11.jpg)
Front end
• Recognize legal code
• Report errors
• Produce IR
• Preliminary storage maps
ScannerSourcecode
IR
errors
tokensParser
![Page 12: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/12.jpg)
Front end
• Scanner:– Maps characters into tokens – the basic unit of syntax
• x = x + y becomes <id, x> = <id, x> + <id, y>– Typical tokens: number, id, +, -, *, /, do, end– Eliminate white space (tabs, blanks, comments)
• A key issue is speed so instead of using a tool like LEX it sometimes needed to write your own scanner
ScannerSourcecode
IR
errors
tokensParser
![Page 13: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/13.jpg)
Front end
• Parser:– Recognize context-free syntax– Guide context-sensitive analysis– Construct IR– Produce meaningful error messages– Attempt error correction
• There are parser generators like YACC which automates much of the work
ScannerSourcecode
IR
errors
tokensParser
![Page 14: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/14.jpg)
Front end
• Context free grammars are used to represent programming language syntaxes:
<expr> ::= <expr> <op> <term> | <term>
<term> ::= <number> | <id><op> ::= + | -
![Page 15: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/15.jpg)
Front end
• A parser tries to map a program to the syntactic elements defined in the grammar
• A parse can be represented by a tree called a parse or syntax tree
![Page 16: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/16.jpg)
Front end
• A parse tree can be represented more compactly referred to as Abstract Syntax Tree (AST)
• AST is often used as IR between front end and back end
![Page 17: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/17.jpg)
Back end
• Translate IR into target machine code• Choose instructions for each IR operation• Decide what to keep in registers at each
point• Ensure conformance with system interfaces
InstructionselectionIR
Machine code
errors
RegisterAllocation
![Page 18: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/18.jpg)
Back end
• Produce compact fast code
• Use available addressing modes
InstructionselectionIR
Machine code
errors
RegisterAllocation
![Page 19: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/19.jpg)
Back end
• Have a value in a register when used
• Limited resources
• Optimal allocation is difficult
InstructionselectionIR
Machine code
errors
RegisterAllocation
![Page 20: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/20.jpg)
Traditional three pass compiler
• Code improvement analyzes and change IR
• Goal is to reduce runtime
Front endSourcecode
Machinecode
errors
IRBack end
Middleend
IR
![Page 21: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/21.jpg)
Middle end (optimizer)
• Modern optimizers are usually built as a set of passes
• Typical passes– Constant propagation– Common sub-expression elimination– Redundant store elimination– Dead code elimination
![Page 22: Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.](https://reader036.fdocuments.net/reader036/viewer/2022062422/56649eb55503460f94bbd766/html5/thumbnails/22.jpg)
Readings
• Chapter 1 of the book