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
2
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
3
Why Take this Course
Reason #2: nice balance of theory and practice
• Practice
• Apply theoretical notions to build a real compiler
4
Why Take this Course
Reason #3: programming experience
• write a large program which manipulates complex
data structures
• learn more about C++ and Intel x86
5
What are Compilers
• Translate information from one representation to
another
• Usually information = program
6
Examples
• Typical Compilers:
• VC, VC++, GCC, JavaC
• FORTRAN, Pascal, VB(?)
• Translators
• Word to PDF
• PDF to Postscript
7
In This Course
We will study typical compilation:
• from programs written in high-level languages to
low-level object code and machine code
8
Typical Compilation
High-level source code
Compiler
Low-level machine code
9
Source Code
int expr( int n )
{
int d;
d = 4*n*n*(n+1)*(n+1);
return d;
}
10
Source Code
• Optimized for human readability
• Matches human notions of grammar
• Uses named constructs such as variables and
procedures
11
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
12
ret
Assembly Code
Optimized for hardware
• Consists of machine instructions
• Uses registers and unnamed memory locations
• Much harder to understand by humans
13
How to Translate
Correctness:
the generated machine code must execute precisely
the same computation as the source code
14
How to Translate
• Is there a unique translation? No!
• Is there an algorithm for an “ideal translation”? No!
15
How to Translate
•Translation is a complex process
•source language and generated
code are very different
•Need to structure the translation
16