Professional Documents
Culture Documents
COMPILER DESIGN
UNIT I
Reference Books
2
3
Program Execution: Layered
Architecture
temp = v[k];
High Level Language v[k] = v[k+1];
Program
v[k+1] = temp;
Compiler
lw $15, 0($2)
Assembly Language
Program lw $16, 4($2)
sw $16, 0($2)
Assembler + sw $15, 4($2)
Linker + Loader 0000 1001 1100 0110 1010 1111 0101 1000
Machine Language 1010 1111 0101 1000 0000 1001 1100 0110
Program 1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111
Machine Interpretation
Control Signal
Specification ALUOP[0:3] <= InstReg[9:11] & MASK
°
°
8-Jan- 4
15
What is a compiler?
5
Programming Languages
Machine Architecture
Language theory
Algorithms
Software Engineering
Why do we care for Compiler Design?
time-to-market
Optimization criteria for silicon compilation
query
Cost depends upon relative sizes of tables,
availability of indexes
Generate proper sequence of operations
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
A bit of history
17
18
Compilers:
• Compilers are sometimes classified as the
following depending on how they have been
constructed or on what function they are
supposed to perform
– single-pass
– multi-pass
– load-and-go
– Debugging
– optimizing
The Analysis-Synthesis Model of
Compilation
– Analysis
– Synthesis
The Analysis-Synthesis Model of
Compilation
Analysis Part
• The analysis part breaks up the
source program into constituent pieces
• creates an intermediate representation of the
source program.
Synthesis Part
The synthesis part constructs the desired target
program from the intermediate representation
The Analysis-Synthesis Model of
Compilation:
1.Structure Editors
2.Pretty Printers
3.Static Checkers
4.Interpreters
Context of a Compiler
26
– Linear Analysis:
– Hierarchical Analysis:
– Semantic Analysis:
28
Analysis of the Source Program:
• Linear Analysis:
• Hierarchical Analysis:
37
Syntax Analysis or
Hierarchical Analysis
(Parsing):
• The hierarchical structure of a program
is usually expressed by recursive rules.
38
Syntax Analysis or
Hierarchical Analysis
(Parsing):
• Any identifier is an expression.
• Any number is an expression
• If expression1 and expression2 are expressions,
then so are
– Expression1 + expression2
– Expression1 * expression2
– (Expression1 )
Analysis of the Source Program:
• Semantic Analysis:
• We can of intermediate
think this
representation as a program for an abstract
machine.
Intermediate Code Generation:
• This intermediate representation should
have two important properties;
– it should be easy to produce,
• MOVF id3, r2
• MULF #60.0, r2
• MOVF id2, r1
• ADDF r2, r1
• MOVF r1, id1
Code Generation
• The first and second operands of each
instruction specify a source and destination,
respectively.
Preprocessors
Assemblers
Two pass assembly
Loaders & Link editors
Cousins of the compiler- Preprocessors
74
• Macro processing
• File inclusion
First pass
Find all identifiers and their storage location and store in
symbol table
Identifier Address
a 0
b 4
Second pass
Translate each operation code into the sequence of bits
Relocatable machine code
Cousins of the compiler- Loaders and Link-Editors
77
Loader
Taking and altering relocatable address machine codes
Link-editors
External references
Library file, routines by system, any other program
Grouping of Phases
78
Based on generation
Imperative
Classification Declarative
of Prog lang
Von Neumann
Object oriented
Scripting
Based on Generations
86
Imperative
In this classification the program specifies how a
computation is to be done.
eg C, C++, C#, Java.
Declarative
In this specification, program specifies what
computation is to be done
Functional languages like ML and Haskell and
constraint logic languages such as Prolog
Von Neumann Languages
88
at compile time
Environment & States
95
1. The environment is the mapping from names to
locations in the store.(ie mapping from names to
variables)
2. The state is a mapping from locations in store to
their values
Two declarations of name i
96
Static Scope & Block Structure
97
access
Dynamic Scope
100
101
Call by Value
Call by Reference