Compiler Construction: M Ikram Ul Haq 1
Compiler Construction: M Ikram Ul Haq 1
Compiler Construction: M Ikram Ul Haq 1
Compiler Construction
M Ikram Ul Haq 1
Compiler Construction
Roadmap
> Overview
> Front end
> Back end
> Multi-pass compilers
2
Compiler Construction
Roadmap
> Overview
> Front end
> Back end
> Multi-pass compilers
© Oscar Nierstrasz 3
Compiler Construction
Textbook
© Oscar Nierstrasz 4
Compiler Construction
© Oscar Nierstrasz 5
PS — Introduction
Compilers, Interpreters …
© O. Nierstrasz 1.6
Compiler Construction
What is a compiler?
© Oscar Nierstrasz 7
Compiler Construction
What is an interpreter?
© Oscar Nierstrasz 8
Compiler Construction
Why do we care?
© Oscar Nierstrasz 10
Compiler Construction
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
© Oscar Nierstrasz 11
Compiler Construction
A bit of history
© Oscar Nierstrasz 12
Compiler Construction
© Oscar Nierstrasz 13
Compiler Construction
Abstract view
© Oscar Nierstrasz 15
Compiler Construction
A fallacy!
© Oscar Nierstrasz 16
Compiler Construction
Roadmap
> Overview
> Front end
> Back end
> Multi-pass compilers
© Oscar Nierstrasz 17
Compiler Construction
Front end
Scanner
© Oscar Nierstrasz 19
Compiler Construction
Parser
Context-free grammars
Productio Result
n Given a grammar, valid
<goal> sentences can be
1 <expr> derived by repeated
2 <expr> <op> <term> substitution.
5 <expr> <op> y
7 <expr> - y To recognize a valid
2 <expr> <op> <term> - y sentence in some
4 <expr> <op> 2 - y
CFG, we reverse this
6
process and build up a
<expr> + 2 - y
parse.
3 <term> + 2 - y
5 x+2-y
© Oscar Nierstrasz 22
Compiler Construction
Parse trees
© Oscar Nierstrasz 23
Compiler Construction
© Oscar Nierstrasz 24
Compiler Construction
Roadmap
> Overview
> Front end
> Back end
> Multi-pass compilers
© Oscar Nierstrasz 25
Compiler Construction
Back end
Instruction selection
© Oscar Nierstrasz 27
Compiler Construction
Register allocation
Roadmap
> Overview
> Front end
> Back end
> Multi-pass compilers
© Oscar Nierstrasz 29
Compiler Construction
© Oscar Nierstrasz 30
Compiler Construction
© Oscar Nierstrasz 32
Compiler Construction
Compiler phases
Lex Break source file into individual words, or tokens
Parse Analyse the phrase structure of program
Parsing Actions Build a piece of abstract syntax tree for each phrase
Determine what each phrase means, relate uses of variables to their
Semantic Analysis
definitions, check types of expressions, request translation of each phrase
Place variables, function parameters, etc., into activation records (stack
Frame Layout
frames) in a machine-dependent way
Produce intermediate representation trees (IR trees), a notation that is not tied
Translate
to any particular source language or target machine
Hoist side effects out of expressions, and clean up conditional branches, for
Canonicalize
convenience of later phases
Group IR-tree nodes into clumps that correspond to actions of target-machine
Instruction Selection
instructions
Analyse sequence of instructions into control flow graph showing all possible
Control Flow Analysis
flows of control program might follow when it runs
Gather information about flow of data through variables of program; e.g.,
Data Flow Analysis liveness analysis calculates places where each variable holds a still-needed
(live) value
Choose registers for variables and temporary values; variables not
Register Allocation
simultaneously live can share same register
Code Emission Replace temporary names in each machine instruction with registers
© Oscar Nierstrasz 33
Compiler Construction
a := 5 + 3; b := (print(a,a—1),10a); print(b)
87
prints 80
© Oscar Nierstrasz 34
Compiler Construction
Tree representation
a := 5 + 3; b := (print(a,a—1),10a); print(b)
© Oscar Nierstrasz 35
Compiler Construction
© Oscar Nierstrasz 36
Compiler Construction
© Oscar Nierstrasz 37
Compiler Construction
© Oscar Nierstrasz 38