of 41
7/29/2019 intro to compiler design
1/41
Compiler Construction
1. IntroductionProf. O. NierstraszFall Semester 2008
7/29/2019 intro to compiler design
2/41
Compiler Construction
Oscar Nierstrasz
Compiler Construction
LecturerProf. Oscar [email protected]. 14/103Tel. 031 631.4618
Assistant Toon VerwaestLectures IWI 003, Wednesdays @ 10h15-12h00Exercises IWI 003, Wednesdays @ 12h00-13h00
WWW www.iam.unibe.ch/~scg/Teaching/CC/
2
7/29/2019 intro to compiler design
3/41
Oscar Nierstrasz
Compiler Construction
Roadmap
> Overview> Front end> Back end> Multi-pass compilers
3
7/29/2019 intro to compiler design
4/41
Oscar Nierstrasz
Compiler Construction
Roadmap
> Overview> Front end> Back end> Multi-pass compilers
4
7/29/2019 intro to compiler design
5/41
Oscar Nierstrasz
Compiler Construction
Textbook
> Andrew W. Appel, Modern compiler implementationin Java(Second edition), Cambridge University Press,New York, NY, USA, 2002, with Jens Palsberg.
Thanks to Jens Palsberg and Tony Hoskingfor their kind permission to reuse and adaptthe CS132 and CS502 lecture notes.
http://www.cs.ucla.edu/~palsberg/http://www.cs.purdue.edu/homes/hosking/
5
7/29/2019 intro to compiler design
6/41
Other recommended sources
> Compilers: Principles, Techniques, andTools, Aho, Sethi and Ullman http://dragonbook.stanford.edu/
> Parsing Techniques, Grune and Jacobs http://www.cs.vu.nl/~dick/PT2Ed.html
> Advanced Compiler Design andImplementation, Muchnik
Oscar Nierstrasz
Compiler Construction
6
7/29/2019 intro to compiler design
7/41
Schedule
Oscar Nierstrasz
Compiler Construction
7
1 17-Sep-08 Introduction
2 24-Sep-08 Lexical Analysis
3 1-Oct-08 Parsing
4 8-Oct-08 Parsing in Practice
5 15-Oct-08 Semantic Analysis
6 22-Oct-08 Intermediate Representation
7 29-Oct-08 Code Generation
8 5-Nov-08 Introduction to SSA [Marcus Denker]
9 12-Nov-08 Optimization [Marcus Denker]
10 19-Nov-08 The PyPy tool chain [Toon Verwaest]
11 26-Nov-08 PEGs, Packrats and Executable Grammars
12 3-Dec-08 Domain Specific Languages [Lukas Renggli]
13 10-Dec-08 Program Transformation
14 17-Dec-08 Final Exam
7/29/2019 intro to compiler design
8/41
Compilers, Interpreters
O. Nierstrasz
PS Introduction
1.8
7/29/2019 intro to compiler design
9/41
Oscar Nierstrasz
Compiler Construction
What is a compiler?
a program that translates an executable
program in one language into an
executableprogram in another language
9
7/29/2019 intro to compiler design
10/41
Oscar Nierstrasz
Compiler Construction
What is an interpreter?
a program that reads an
executableprogram and produces
the results of running that program
10
7/29/2019 intro to compiler design
11/41
Oscar Nierstrasz
Compiler Construction
Why do we care?
artificialintelligence
greedy algorithmslearning algorithms
algorithms
graph algorithmsunion-finddynamic programming
theoryDFAs for scanningparser generatorslattice theory for analysis
systems
allocation and naminglocalitysynchronization
architecture
pipeline managementhierarchy managementinstruction set use
Compiler construction
is a microcosm of
computer science
Inside a compiler, all these things come together11
7/29/2019 intro to compiler design
12/41
Oscar Nierstrasz
Compiler Construction
Isnt it a solved problem?
> Machines are constantly changing Changes in architecture changes in compilers new features pose new problems changing costs lead to different concerns old solutions need re-engineering
> Innovations in compilers should prompt changes inarchitecture New languages and features
12
7/29/2019 intro to compiler design
13/41
Oscar Nierstrasz
Compiler Construction
What qualities are important in a compiler?
1. Correct code 2. Output runs fast 3. Compiler runs fast 4.
Compile time proportional to program size
5. Support for separate compilation 6. Good diagnostics for syntax errors 7. Works well with the debugger 8. Good diagnostics for flow anomalies 9. Cross language calls 10.Consistent, predictable optimization
13
7/29/2019 intro to compiler design
14/41
Oscar Nierstrasz
Compiler Construction
A bit of history
> 1952: First compiler (linker/loader) written by GraceHopper for A-0 programming language
> 1957: First complete compiler for FORTRAN by JohnBackus and team
> 1960:COBOL compilers for multiple architectures> 1962: First self-hosting compiler for LISP
14
7/29/2019 intro to compiler design
15/41
Oscar Nierstrasz
Compiler Construction
A compiler was originally a program that
compiled subroutines [a link-loader].
When in 1954 the combination algebraic
compiler came into use, or rather intomisuse, the meaning of the term had already
shifted into the present one. Bauer and Eickel [1975]
15
7/29/2019 intro to compiler design
16/41
Oscar Nierstrasz
Compiler Construction
Abstract view
recognize legal (and illegal) programs generate correct code manage storage of all variables and code agree on format for object (or assembly) code
Bigstepupfromassemblerhigherlevelnotations16
7/29/2019 intro to compiler design
17/41
Oscar Nierstrasz
Compiler Construction
Traditionaltwopasscompiler
intermediate representation (IR) front end maps legal code into IR back end maps IR onto target machine simplify retargeting allows multiple front ends
multiple passes better code
17
7/29/2019 intro to compiler design
18/41
Oscar Nierstrasz
Compiler Construction
Afallacy!
Front-end, IR and back-end must encodeknowledge needed for all nm combinations!
18
7/29/2019 intro to compiler design
19/41
Oscar Nierstrasz
Compiler Construction
Roadmap
> Overview> Front end> Back end> Multi-pass compilers
19
7/29/2019 intro to compiler design
20/41
Oscar Nierstrasz
Compiler Construction
Front end
recognize legal code report errors produce IR preliminary storage map shape code for the back end
Much of front end construction can be automated20
7/29/2019 intro to compiler design
21/41
Oscar Nierstrasz
Compiler Construction
Scanner
map characters to tokenscharacter string value for a token is a lexemeeliminate white space
x = x + y = + 21
7/29/2019 intro to compiler design
22/41
Oscar Nierstrasz
Compiler Construction
Parser
recognize context-free syntax guide context-sensitive analysis construct IR(s) produce meaningful error messages attempt error correction
Parser generators mechanize much of the work22
7/29/2019 intro to compiler design
23/41
Oscar Nierstrasz
Compiler Construction
Context-free grammars
1. := 2. := 3. | 4. := number5. | id6. := +7. | -
Context-free syntax
is specified with a
grammar, usually inBackus-Naur form
(BNF)
A grammar G = (S,N,T,P)Sis the start-symbolNis a set of non-terminal symbolsTis a set of terminal symbolsPis a set of productions P: N(NT)*
23
7/29/2019 intro to compiler design
24/41
Oscar Nierstrasz
Compiler Construction
Deriving valid sentences
Production Result
1 2 5 y7 - y2 - y4 2 - y6 + 2 - y3 + 2 - y5 x + 2 - y
Given a grammar, valid
sentences can be
derived by repeated
substitution.
To recognize a valid
sentence in some
CFG, we reverse thisprocess and build up a
parse.
24
7/29/2019 intro to compiler design
25/41
Oscar Nierstrasz
Compiler Construction
Parse trees
A parse can be represented by a
tree called a parseor syntaxtree.
Obviously, this contains a lotof unnecessary information
25
7/29/2019 intro to compiler design
26/41
Oscar Nierstrasz
Compiler Construction
Abstract syntax trees
So, compilers often use an abstractsyntaxtree(AST).
ASTs are often
used as an IR.
26
7/29/2019 intro to compiler design
27/41
Oscar Nierstrasz
Compiler Construction
Roadmap
> Overview> Front end> Back end> Multi-pass compilers
27
7/29/2019 intro to compiler design
28/41
Oscar Nierstrasz
Compiler Construction
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
Automation has been less successful here
28
7/29/2019 intro to compiler design
29/41
Oscar Nierstrasz
Compiler Construction
Instruction selection
produce compact, fast code use available addressing modes pattern matching problem
ad hoc techniques tree pattern matching string pattern matching dynamic programming
29
7/29/2019 intro to compiler design
30/41
Oscar Nierstrasz
Compiler Construction
Register allocation
have value in a register when used limited resources changes instruction choices can move loads and stores optimal allocation is difficult
Modern allocators often use an analogy to graph coloring30
7/29/2019 intro to compiler design
31/41
Oscar Nierstrasz
Compiler Construction
Roadmap
> Overview> Front end> Back end> Multi-pass compilers
31
7/29/2019 intro to compiler design
32/41
Oscar Nierstrasz
Compiler Construction
Traditional three-pass compiler
analyzes and changes IR goal is to reduce runtime must preserve values
32
7/29/2019 intro to compiler design
33/41
Oscar Nierstrasz
Compiler Construction
Optimizer (middle end)
Modern optimizers are usually built as a set of passes
constant propagation and folding code motion reduction of operator strength common sub-expression elimination redundant store elimination dead code elimination
33
7/29/2019 intro to compiler design
34/41
Oscar Nierstrasz
Compiler Construction
The MiniJava compiler
34
7/29/2019 intro to compiler design
35/41
Oscar Nierstrasz
Compiler Construction
Compiler phases
Lex Break source file into individual words, or tokensParse
Analyse the phrase structure of program Parsing Actions Build a piece of abstract syntax treefor each phrase Semantic Analysis
Determine what each phrase means, relate uses of variables to their definitions,check types of expressions, request translation of each phrase
Frame LayoutPlace variables, function parameters, etc., into activation records (stack frames)in a machine-dependent way
TranslateProduce intermediate representation trees(IR trees), a notation that is not tiedto any particular source language or target machine
CanonicalizeHoist side effects out of expressions, and clean up conditional branches, forconvenience of later phases
Instruction SelectionGroup IR-tree nodes into clumps that correspond to actions of target-machineinstructions
Control Flow AnalysisAnalyse sequence of instructions into control flow graphshowing all possibleflows of control program might follow when it runs
Data Flow AnalysisGather information about flow of data through variables of program; e.g.,
liveness analysiscalculates places where each variable holds a still-needed(live) value
Register AllocationChoose registers for variables and temporary values; variables notsimultaneously live can share same register
Code Emission Replace temporary names in each machine instruction with registers 35
7/29/2019 intro to compiler design
36/41
Oscar Nierstrasz
Compiler Construction
Astraight-lineprogramminglanguage(no loops or conditionals):
Stm Stm ; Stm CompoundStmStm id := Exp AssignStm Stm print ( ExpList ) PrintStmExp id IdExp Exp num NumExp Exp Exp Binop Exp OpExp Exp ( Stm , Exp ) EseqExpExpList Exp , ExpList PairExpListExpList Exp LastExpListBinop + PlusBinop - MinusBinop TimesBinop / Div
a := 5 + 3; b := (print(a,a1),10a); print(b)prints 8 780
36
7/29/2019 intro to compiler design
37/41
Oscar Nierstrasz
Compiler Construction
Tree representation
a := 5 + 3; b := (print(a,a1),10
a); print(b)
37
7/29/2019 intro to compiler design
38/41
Oscar Nierstrasz
Compiler Construction
Java classes for trees
abstract class Stm {}class CompoundStm extends Stm {
Stm stm1, stm2;CompoundStm(Stm s1, Stm s2){stm1=s1; stm2=s2;}
}class AssignStm extends Stm {
String id; Exp exp;AssignStm(String i, Exp e) {id=i; exp=e;}
}class PrintStm extends Stm {
ExpList exps;PrintStm(ExpList e) {exps=e;}
}abstract class Exp {}class IdExp extends Exp {
String id;IdExp(String i) {id=i;}
}
class NumExp extends Exp {int num;NumExp(int n) {num=n;}
}class OpExp extends Exp {
Exp left, right; int oper;final static int Plus=1,Minus=2,Times=3,Div=4;OpExp(Exp l, int o, Exp r) {left=l; oper=o; right=r;}
}class EseqExp extends Exp {
Stm stm; Exp exp;EseqExp(Stm s, Exp e) {stm=s; exp=e;}
}abstract class ExpList {}class PairExpList extends ExpList {
Exp head; ExpList tail;public PairExpList(Exp h, ExpList t) {head=h; tail=t;}
}class LastExpList extends ExpList {
Exp head; public LastExpList(Exp h) {head=h;}
}38
7/29/2019 intro to compiler design
39/41
Oscar Nierstrasz
Compiler Construction
What you should know!
What is the difference between a compiler and aninterpreter?
What are important qualities of compilers? Why are compilers commonly split into multiple passes? What are the typical responsibilities of the different parts
of a modern compiler? How are context-free grammars specified? What is abstract about an abstract syntax tree? What is intermediate representation and what is it for? Why is optimization a separate activity?
39
7/29/2019 intro to compiler design
40/41
Oscar Nierstrasz
Compiler Construction
Can you answer these questions?
Is Java compiled or interpreted? What about Smalltalk?Ruby? PHP? Are you sure?
What are the key differences between modern compilersand compilers written in the 1970s?
Why is it hard for compilers to generate good errormessages?
What is context-free about a context-free grammar?
40
7/29/2019 intro to compiler design
41/41
Oscar Nierstrasz
Compiler Construction
License
> http://creativecommons.org/licenses/by-sa/2.5/
Attribution-ShareAlike 2.5
You are free:
to copy, distribute, display, and perform the work
to make derivative works to make commercial use of the workUnder the following conditions:
Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting
work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.
41