MIT Version - IU Oncourse

Spring 2010
Lecture 1: Introduction
Intro. to Computer Language Engineering
Course Administration info
info.
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 1
Outline
• Course Administration Information
• Introduction to computer language engineering
– Why do we need a compiler?
– What are compilers?
– Anatomy of a compiler
Saman Amarasinghe
2
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 2
Course Administration
•
•
•
•
•
•
Staff
Optional Text
Course Outline
The Project
j
Project Groups
Grading
Saman Amarasinghe
3
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 3
Reference Textbooks
Modern Compiler Implementation in Java (Tiger book)
A textbook tutorial on compiler
implementation, including
techniques for many language
features
•
Advanced Compiler Design and Implementation (Whale book)
Essentially a recipe book of
optimizations;
ti i ti
very complete
l t and
d
suited for industrial practitioners
and researchers.
•
Compilers: Principles,
Principles Techniques and Tools (Dragon book)
The classic
Th
l i compilers
il
textbook,
t tb k
although its front-end emphasis
reflects its age. New edition has
more optimization material.
•
Engineering a Compiler (Ark book)
A modern classroom textbook,
with increased emphasis on the
back-end and implementation
techniques
techniques.
•
Optimizing Compilers for Modern Architectures
A modern textbook that focuses
on optimizations including
parallelization and memory
hierarchy optimization
•
A.W. Appel
Cambridge University Press, 1998
ISBN 0-52158-388-8
Steven Muchnick
Morgan Kaufman Publishers, 1997
ISBN 1-55860-320-4
Aho, Lam, Sethi and Ullman
Addison-Wesley, 2006
ISBN 0321486811
Keith D. Cooper, Linda Torczon
Morgan Kaufman Publishers, 2003
ISBN 1-55860
55860-698
698-X
X
Randy Allen and Ken Kennedy
Morgan Kaufman Publishers, 2001
ISBN 1-55860-286-0
Saman Amarasinghe
5
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 4
The Project: The Five Segments

Lexical and Syntax Analysis
Semantic Analysis

Code Generation

Data-flow
Data
flow Analysis

Optimizations

Saman Amarasinghe
6
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 5
Each Segment...
• Segment Start
– Project Description
• Lectures
– 2 to 5 lectures
• Project Time
– (Design Document)
– (Project Checkpoint)
• Project Due
Saman Amarasinghe
7
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 6
Project Groups
• 1st project is an individual project
• Projects 2 to 5 are group projects consists of
3 to 4 students
• Grading
– All group members
b
(mostly)
(
tl ) gett the
th same grade
d
Saman Amarasinghe
8
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 7
Grades
• Compiler project
70%
• In-class Quizzes
30% (10% each)
• In-class mini-quizzes
10% (0.5% each)
Saman Amarasinghe
10
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 8
Grades for the Project
–
–
–
–
–
Scanner/Parser
S
Semantic
ti Checking
Ch ki
Code Generation
D t fl
Data-flow
Analysis
A l i
Optimizations
Saman Amarasinghe
5%
7 5%
7.5%
10%
7 5%
7.5%
30%
60%
11
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 9
Optimization Segment
• Making programs run fast
–
–
–
–
We provide a test set of applications
Figure-out what will make them run fast
Prioritize and implement the optimizations
Compiler derby at the end
• A “similar” application to the test set is provided the day before
• The compiler that produced the fastest code is the winner
• Do any optimizations you choose
– Including parallelization for multicores
• Grade is divided into:
– Documentation
• Justifyy yyour optimizations
p
and the selection p
process
6%
– Optimization Implementation
12%
– Derby performance
12%
• Producing correct code
30%
Saman Amarasinghe
12
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 10
The Quiz
• Three Quizzes
• In-Class Quiz
– 50 Minutes (be on time!)
– Open book, open notes
Saman Amarasinghe
13
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 11
Mini Quizzes
• You already got one.
one
• Given at the beginning of the class; Collected at
th end
the
d
• Collaboration is OK
• This is in lieu of time consuming problem sets
Saman Amarasinghe
14
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 12
Outline
• Course Administration Information
• Introduction to computer language engineering
– What are compilers?
– Why should we learn about them?
– Anatomy of a compiler
Saman Amarasinghe
15
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 13
Why Study Compilers?
• Compilers enable programming at a high level
language instead of machine instructions.
– Malleability
Malleability, Portability,
Portability Modularity,
Modularity Simplicity,
Simplicity
Programmer Productivity
– Also Efficiency and Performance
Saman Amarasinghe
16
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 14
Compilers Construction touches
many topics
t i in
i Computer
C
t Science
S i
• Theory
– Finite
Fi it State
St t A
Automata,
t
t G
Grammars and
dP
Parsing,
i
d
data-flow
t fl
• Algorithms
– Graph manipulation, dynamic programming
• Data structures
– Symbol tables, abstract syntax trees
• Systems
– Allocation and naming, multi-pass systems, compiler construction
• Computer Architecture
– Memory
M
hierarchy,
hi
h instruction
i t ti selection,
l ti
interlocks
i t l k and
d latencies,
l t
i
parallelism
ll li
• Security
– Detection of and Protection against vulnerabilities
• Software Engineering
– Software development environments, debugging
• Artificial Intelligence
– Heuristic based search for best optimizations
Saman Amarasinghe
17
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 15
Power of a Language
• Can use to describe any action
– Not tied to a “context”
• Many
M
ways to
t d
describe
ib the
th same action
ti
– Flexible
Saman Amarasinghe
18
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 16
How to instruct a computer
• How about natural languages?
–
–
–
–
English??
“Open the pod bay doors, Hal.”
“I am sorryDDave, I am afraid
f id I cannott do
d that”
th t”
We are not there yet!!
• Natural Languages:
– Powerful, but…
– Ambiguous
• Same expression describes many possible actions
Saman Amarasinghe
19
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 17
Programming Languages
• Properties
–
–
–
–
need
need
need
need
Saman Amarasinghe
to
to
to
to
be
be
be
be
precise
concise
expressive
at a high-level (lot of abstractions)
20
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 18
High-level Abstract Description
t Low-level
to
L
l
l Implementation
I
l
t ti
Details
D t il
My poll ratings are low,
lets invade a small nation
President
General
Cross the river and take
defensive positions
Sergeant
Forward march,
march turn left
Stop!, Shoot
Foot Soldier
Saman Amarasinghe
21
Figure by MIT OpenCourseWare.
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 19
1. How to instruct the computer
• Write a program using a programming language
– High-level Abstract Description
• Microprocessors talk in assembly language
– Low-level Implementation Details
Program
written
in a
Programming
Languages
Saman Amarasinghe
Compiler
p
22
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
Assembly
Language
Translation
6.035
©MIT Fall 1998
The Saylor Foundation 20
1. How to instruct the computer
• Input: High-level
High level programming language
• Output: Low-level assembly instructions
• Compiler
p
does the translation:
–
–
–
–
Read and understand the program
Preciselyy determine what actions it require
q
Figure-out how to faithfully carry-out those actions
Instruct the computer to carry out those actions
Saman Amarasinghe
23
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 21
Input to the Compiler
• Standard imperative language (Java,
(Java C,
C C++)
– State
• Variables,
Variables
• Structures,
y
• Arrays
– Computation
•
•
•
•
Expressions (arithmetic, logical, etc.)
Assignment statements
Control flow (conditionals, loops)
P
Procedures
d
Saman Amarasinghe
24
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 22
Output of the Compiler
• State
– Registers
– Memory with Flat Address S
Space
pace
• Machine code – load/store architecture
– Load, store instructions
– Arithmetic, logical operations on registers
– Branch instructions
Saman Amarasinghe
25
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 23
Example (input program)
int sumcalc(int a, int b, int N)
{
int i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
26
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 24
Example (Output assembly code)
sumcalc:
.L2:
.L3:
Saman Amarasinghe
.size
pushq
movq
movl
movl
movl
movl
movl
movl
movl
cmpl
l
jg
movl
leal
leaq
movq
movl
movq
cltd
idivl
movl
movl
u
imull
movl
incl
imull
addl
leaq
addl
movl
movl
imull
leaq
addl
leaq
i l
incl
jmp
movl
leave
ret
sumcalc, .-sumcalc
.section
.Lframe1:
.long
.LECIE1-.LSCIE1
.LSCIE1:.long
0x0
.byte
0x1
.string ""
.uleb128 0x1
.sleb128 -8
.byte
0x10
.byte
b t
0
0xc
.uleb128 0x7
.uleb128 0x8
.byte
0x90
.uleb128 0x1
.align
8
.LECIE1:.long
LECIE1: long
.LEFDE1
LEFDE1-.LASFDE1
LASFDE1
.long
.LASFDE1-.Lframe1
.quad
.LFB2
.quad
.LFE2-.LFB2
.byte
0x4
.long
.LCFI0-.LFB2
.byte
0 e
0xe
.uleb128 0x10
.byte
0x86
.uleb128 0x2
.byte
0x4
.long
.LCFI1-.LCFI0
.byte
0xd
.uleb128 0x6
.align
8
%rbp
%rsp, %rbp
%edi, -4(%rbp)
%esi, -8(%rbp)
%edx, -12(%rbp)
$0, -20(%rbp)
$0, -24(%rbp)
$0, -16(%rbp)
-16(%rbp), %eax
-12(%rbp),
12(% b ) %
%eax
.L3
-4(%rbp), %eax
0(,%rax,4), %edx
-8(%rbp), %rax
%rax, -40(%rbp)
%edx %eax
%edx,
-40(%rbp), %rcx
(%rcx)
%eax, -28(%rbp)
-28(%rbp), %edx
-16(%rbp),
6(% bp), %ed
%edx
-16(%rbp), %eax
%eax
%eax, %eax
%eax, %edx
-20(%rbp), %rax
%edx, (%rax)
-8(%rbp), %eax
%eax, %edx
-24(%rbp), %edx
-20(%rbp), %rax
%edx, (%rax)
-16(%rbp), %rax
(%
(%rax)
)
.L2
-20(%rbp), %eax
27
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 25
Mapping Time Continuum
C
Compilation
il ti
to
t Interpretation
I t
t ti
• Compile time
– Ex: C compiler
• Link
Li k time
ti
– Ex: Binary layout optimizer
• Load time
– Ex: JIT compiler
• Run time
– Ex: Java Interpreter
Saman Amarasinghe
28
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 26
Anatomy of a Computer
Program
written
in a
Programming
Languages
Saman Amarasinghe
p
Compiler
29
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
Assembly
Language
Translation
6.035
©MIT Fall 1998
The Saylor Foundation 27
Anatomy of a Computer
Program (character stream)
L i l Analyzer
Lexical
A l
(Scanner)
(S
)
Token Stream
Saman Amarasinghe
30
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 28
Lexical Analyzer (Scanner)
2 3 4
*
( 1 1
+ - 2 2 )
Num(234) mul_op lpar_op Num(11) add_op Num(-22) rpar_op
Saman Amarasinghe
31
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 29
Lexical Analyzer (Scanner)
2 3 4
*
( 1 1
+ - 2 2 )
Num(234) mul_op lpar_op Num(11) add_op Num(-22) rpar_op
18..23 + val#ue
V i bl names cannott hhave ‘#’ character
Variable
h
t
Not a number
Saman Amarasinghe
32
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 30
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Saman Amarasinghe
33
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 31
Syntax Analyzer (Parser)
num ‘*’ ‘(‘ num ‘‘+’’ num ‘)’
<expr>
<expr>
num
<op>
<expr>
*
<expr>
(
<expr> <op>
num
Saman Amarasinghe
+
34
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
)
<expr>
num
6.035
©MIT Fall 1998
The Saylor Foundation 32
Syntax Analyzer (Parser)
int * foo(i, j, k))
int i;
int j;
Extra parentheses
{
for(i=0; i j) {
fi(i>j)
Missing increment
return j;
Not an expression
}
Not a keyword
Saman Amarasinghe
35
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 33
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Semantic Analyzer
Intermediate Representation
Saman Amarasinghe
36
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 34
Semantic Analyzer
int * foo(i, j, k)
int i;
int j;
{
int x;
x = x + j + N;
return j;
}
Saman Amarasinghe
Type not declared
Mismatched return type
Uninitialized variable used
Undeclared variable
37
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 35
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Semantic Analyzer
Intermediate Representation
Code Optimizer
Optimized Intermediate Representation
Saman Amarasinghe
38
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 36
Optimizer
int sumcalc(int a, int b, int N)
{
int i;
int x, t, u, v;
x = 0;
u = ((a<<2)/b);
v = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + v + t*t;
v = v + u;
}
return x;
}
int sumcalc(int a, int b, int N)
{
int i;
int x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x+4*a/b*i+(i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
39
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 37
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Semantic Analyzer
Intermediate Representation
Code Optimizer
Optimized Intermediate Representation
Code Generator
Assembly code
Saman Amarasinghe
40
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 38
Code Generator
int sumcalc(int a, int b, int N)
{
int i;
int x, t, u, v;
x = 0;
u = ((a<<2)/b);
(( <<2)/b)
v = 0;
for(i = 0; i <= N; i++) {
t = i+1;
;
x = x + v + t*t;
v = v + u;
}
return
t
x;
}
Saman Amarasinghe
41
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
sumcalc:
xorl
xorl
movl
cmpl
jg
sall
.L5:
movl
cltd
idivl
leal
movl
imull
movl
imull
leal
movl
addl
cmpl
jle
.L7:
movl
ret
%r8d, %r8d
%ecx
%ecx, %ecx
%edx, %r9d
%edx, %r8d
.L7
$2, %edi
, %eax
%edi,
%esi
1(%rcx), %edx
%eax, %r10d
%ecx, %r10d
%edx, %ecx
%edx, %ecx
(%r10,%rcx), %eax
%edx, %ecx
%eax, %r8d
% 9
%r9d,
%
%edx
.L5
%r8d, %eax
6.035
©MIT Fall 1998
The Saylor Foundation 39
Program Translation
• Correct
– The actions requested by the program has to be
faithfully executed
• Efficient
– Intelligently and efficiently use the available resources to
carry out the requests
– (the word optimization is used loosely in the compiler
community – Optimizing compilers are never optimal)
Saman Amarasinghe
42
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 40
Efficient Execution
Cross the river and take
defensive positions
General
Sergeant
Foot Soldier
Figure by MIT OpenCourseWare.
Saman Amarasinghe
43
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 41
Efficient Execution
Cross the river and take
defensive positions
General
Where to cross the river? Use the
bridge upstream or surprise the enemy
by crossing downstream?
How do I minimize the casualties??
Sergeant
Foot Soldier
Saman Amarasinghe
44
Figure by MIT OpenCourseWare.
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 42
Efficient Execution
My poll ratings are low,
lets invade a small nation
President
Russia or Bermuda?
Or just stall for his poll
numbers to go up?
General
Figure by MIT OpenCourseWare.
Saman Amarasinghe
45
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 43
Efficient Execution
• Mapping from High to Low
– Simple mapping of a program to assembly language
produces inefficient execution
– Higher the level of abstraction  more inefficiency
• If not efficient
– High-level abstractions are useless
• Need
d to:
– provide a high level abstraction
– with performance of giving low-level instructions
Saman Amarasinghe
46
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 44
Efficient Execution help
i
increase
the
th llevell off abstraction
b t
ti
• Programming languages
– From C to OO-languages
with garbage collection
– Even more abstract
definitions
• Microprocessor
– From simple CISC to RISC to
VLIW to ….
Saman Amarasinghe
47
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 45
The Multicore Dilemma
• Superscalars
• Multicores
High
g Level Language
g g
Co
omp
ile
er??
Com
pile
er
High Level Language
Simple von Neumann Machine
Saman Amarasinghe
Harrd
warre
H
Hard
ware
w
Multiple exposed cores
48
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 46
The Multicore Dilemma
• Superscalars
• Multicores
Com
pile
er
High
g Level Language
g g
Parallel Language
Multiple exposed cores
Harrd
warre
H
Hard
ware
w
Saman Amarasinghe
Com
m
pilerr
Simple von Neumann Machine
49
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 47
Optimization Example
int sumcalc(int a, int b, int N)
{
int i;
int x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
50
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 48
pushq
%rbp
movq
%rsp, %rbp
movl
%edi, -4(%rbp)
movl
%esi, -8(%rbp)
movl
l
%
%edx,
d
-12(%rbp)
12(% b )
movl
$0, -20(%rbp)
movl
$0, -24(%rbp)
movl
$0, -16(%rbp)
.L2:
movl
-16(%rbp), %eax
cmpl
12(%rbp) %eax
12(%rbp),
jg
.L3
movl - -4(%rbp), %eax
leal
0(,%rax,4), %edx
leaq
-8(%rbp), %rax
movq
%rax
%rax, -40(%rbp)
40(%rbp)
movl
%edx, %eax
movq
-40(%rbp), %rcx
cltd
idivl
(%rcx)
movl
%eax,
, -28(%rbp)
(
p)
movl
-28(%rbp), %edx
imull
-16(%rbp), %edx
movl
-16(%rbp), %eax
incl
%eax
imull
%eax, %eax
addl
%eax, %edx
leaq
-20(%rbp), %rax
addl
%edx, (%rax)
movl
-8(%rbp), %eax
movl
%eax, %edx
i ll
imull
24(% b ) %
24(%rbp),
%edx
d
leaq - -20(%rbp), %rax
addl
%edx, (%rax)
leaq
-16(%rbp), %rax
incl
(%rax)
jmp
L2
.L3:
movl
-20(%rbp), %eax
leave
.
ret
Saman Amarasinghe
45
51
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 49
Lets Optimize...
int sumcalc(int a, int b, int N)
{
int i, x, y;
x = 0;
y = 0;
o ( = 0
0;
; i <= N;
; i++)
) {
for(i
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
52
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 50
Constant Propagation
int i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
Saman Amarasinghe
53
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 51
Constant Propagation
int i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
Saman Amarasinghe
54
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 52
Constant Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*0;
}
return x;
Saman Amarasinghe
55
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 53
Algebraic Simplification
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*0;
}
return x;
Saman Amarasinghe
56
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 54
Algebraic Simplification
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*0;
}
return x;
Saman Amarasinghe
57
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 55
Algebraic Simplification
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x;
}
return x;
Saman Amarasinghe
58
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 56
Copy Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x;
}
return x;
Saman Amarasinghe
59
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 57
Copy Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x;
}
return x;
Saman Amarasinghe
60
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 58
Copy Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
}
return x;
Saman Amarasinghe
61
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 59
Common Subexpression Elimination
int i
i, x
x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
}
return x;
Saman Amarasinghe
62
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 60
Common Subexpression Elimination
int i
i, x
x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
}
return x;
Saman Amarasinghe
63
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 61
Common Subexpression Elimination
int
i
t i
i, x, y, t;
t
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
64
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 62
Dead Code Elimination
int
i
t i
i, x, y, t
t;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
65
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 63
Dead Code Elimination
int
i
t i
i, x, y, t
t;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
66
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 64
Dead Code Elimination
int
i
t i
i, x, t;
t
x = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
67
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 65
Loop Invariant Removal
int
i
t i
i, x, t;
t
x = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
68
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 66
Loop Invariant Removal
int
i
t i
i, x, t;
t
x = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
69
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 67
Loop Invariant Removal
int
i
t i
i, x, t,
t u;
x = 0;
u = (4*a/b);
/
for(i = 0; i <= N; i++) {
t = i+1;
x = x + u*i + t*t;
}
return x;
Saman Amarasinghe
70
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 68
Strength Reduction
int
i
t i
i, x, t,
t u;
x = 0;
u = (4*a/b);
/
for(i = 0; i <= N; i++) {
t = i+1;
x = x + u*i + t*t;
}
return x;
Saman Amarasinghe
71
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 69
Strength Reduction
int
i
t i
i, x, t,
t u;
x = 0;
u = (4*a/b);
/
for(i = 0; i <= N; i++) {
t = i+1;
x = x + u*i + t*t;
}
return x;
Saman Amarasinghe
72
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 70
Strength Reduction
int i
i, x
x, t
t, u
u, v;
x = 0;
u = ((a<<2)/b);
v = 0;
for(i = 0; i <=
< N; i++) {
t = i+1;
x = x + v + t*t;
v = v + u;
}
return x;
Saman Amarasinghe
73
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 71
Register Allocation
fp
Local variable X
Local variable Y
Local variable I
Saman Amarasinghe
74
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 72
Register Allocation
fp
Local variable X
Local variable Y
Local variable I
$r8d
$r9d
$r10d
$ebx
$ecx
=
=
=
=
=
X
t
u
v
i
Saman Amarasinghe
75
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 73
Optimized Example
int sumcalc(int a, int b, int N)
{
int i, x, t, u, v;
x = 0;
u = ((a<<2)/b);
v = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + v + t*t;
v = v + u;
}
return x;
}
Saman Amarasinghe
76
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 74
Unoptimized Code
pushq
movq
movl
movl
movl
movl
movl
movl
.L2:
cmpl
movl
jg
movl
leal
leaq
movq
movl
movq
cltd
idi l
idivl
movl
movl
imull
movl
incl
imull
addl
leaq
addl
movl
movl
imull
leaq
addl
q
leaq
incl
jmp
.L3:
Optimized Code
xorl
xorl
movl
cmpl
jg
sall
%rbp
%rsp, %rbp
%edi, -4(%rbp)
4(%rbp)
%esi, -8(%rbp)
%edx, -12(%rbp)
$0, -20(%rbp)
$0, -24(%rbp)
$0, -16(%rbp)
-16(%rbp), %eax
-12(%rbp), %eax
.L3
L3
-4(%rbp), %eax
0(,%rax,4), %edx
-8(%rbp), %rax
%rax, -40(%rbp)
%edx, %eax
-40(%rbp), %rcx
.L5:
cltd
movl
idivl
leal
movl
imull
movl
imull
leal
movl
addl
cmpl
jl
jle
(%
(%rcx)
)
%eax, -28(%rbp)
-28(%rbp), %edx
-16(%rbp), %edx
-16(%rbp), %eax
%eax
%eax, %eax
%eax, %edx
-20(%rbp), %rax
%edx, (%rax)
-8(%rbp), %eax
%eax, %edx
-24(%rbp), %edx
-20(%rbp), %rax
%edx, (%rax)
-16(%rbp),
(
p), %rax
(%rax)
.L2
-20(%rbp), %eax
.L7:
%r8d, %r8d
%ecx, %ecx
%edx, %r9d
%edx, %r8d
.L7
$2, %edi
%edi, %eax
%esi
1(%rcx), %edx
%eax
%eax, %r10d
%ecx, %r10d
%edx, %ecx
%edx, %ecx
(%r10,%rcx), %eax
%edx, %ecx
%eax, %r8d
%r9d, %edx
.L5
L5
%r8d, %eax
ret
movl
leave
movl
ret
Inner Loop:
10*mov + 5*lea + 5*add/inc
+ 4*div/mul + 5*cmp/br/jmp
= 29 iinstructions
i
Execution time = 43 sec
Saman Amarasinghe
4*mov + 2*lea + 1*add/inc+
3*div/mul + 2*cmp/br/jmp
= 12 iinstructions
i
Execution time = 17 sec
77
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 75
Compilers Optimize Programs for…
•
•
•
•
•
•
Performance/Speed
Code Size
Power Consumption
Fast/Efficient
/
Compilation
p
Security/Reliability
Debugging
Saman Amarasinghe
78
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
6.035
©MIT Fall 1998
The Saylor Foundation 76
MIT OpenCourseWare
http://ocw.mit.edu
6.035 Computer Language Engineering
Spring 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/
Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 77