Compiler
Construction
LECTURE 1
Why Take this Course
Reason # 1: understand compilers and languages
understand the code structure
understand language semantics
understand relation between source code and
generated machine code
become a better programmer
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
2
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Why Take this Course
Reason #2: nice balance of theory
and practice
Theory
mathematical models: regular
expressions, automata, grammars,
graphs
algorithms that use these models
Practice
Apply theoretical notions to build a
real compiler
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
3
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Why Take this Course
Reason #3: programming experience
write a large program which manipulates
complex data structures
learn more about C++ and Intel x86
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
4
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
What are Compilers
Translate information from one representation to another
Usually, information = program
Typical Compilers:
VC, VC++, GCC, JavaC
FORTRAN, Pascal, VB
Translators
Word to PDF
PDF to Postscript
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
5
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
In This Course
We will study typical compilation:
from programs written in high-level
languages to low-level object code and
machine code
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
6
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Typical Compilation
High-level
High-level source
source code
code
Compiler
Low-level
Low-level machine
machine code
code
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
7
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Source Code
int expr( int n )
{
int d;
d = 4*n*n*(n+1)*(n+1);
return d;
}
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
8
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Source Code
Optimized for human readability
Matches human notions of grammar
Uses named constructs such as variables
and procedures
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
9
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Assembly Code
.globl _expr
_expr:
imull %eax,%edx
pushl %ebp
movl 8(%ebp),%eax
movl %esp,%ebp
incl %eax
subl $24,%esp
imull %eax,%edx
movl 8(%ebp),%eax
movl %edx,-4(%ebp)
movl %eax,%edx
movl -4(%ebp),%edx
leal 0(,%edx,4),%eax
movl %edx,%eax
movl %eax,%edx
jmp L2
imull 8(%ebp),%edx
.align 4
movl 8(%ebp),%eax
L2:
incl %eax
leave
ret
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
10
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
Assembly Code
Optimized for hardware
Consists of machine instructions
Uses registers and unnamed memory
locations
Much harder to understand by humans
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
11
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
How to Translate
Correctness:
the generated machine code must
execute precisely the same
computation as the source code
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
12
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
How to Translate
Is there a unique translation? No!
Is there an algorithm for an “ideal
translation”? No!
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
13
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN
How to Translate
Translation is a complex process
source language and generated code
are very different
Need to structure the translation
RECOMMANDED TEXT BOOK: COMPILERS-PRINCIPLES
14
TECHNIQUES AND TOOLS BY AHO, SETHI AND ULLMAN