Академический Документы
Профессиональный Документы
Культура Документы
Rupesh Nasre.
Compiler Design
QEEE
Aug 2016
Languages
Languages
Humans communicate
Birds communicate
Animals communicate
Aliens?
Programming Languages
4
Source: google images
Programming Languages
filetype:pdf, link:
Using a calculator
Compiler
I know English
7
Source: google images
Jobs of a Compiler
Maintain correctness
Be efficient
,
8
Good Language
I got books but more than that I got your letter.
,
I have books, but I got your letter got more than that.
,
I have books, but that's more than I got your letter.
,
I have books, but I have received your letter is more than.
stewardesses
typewriter
skepticisms
quine
Programs as Data
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
10
Language matters!
12
Source: Thinking Fast and Slow, Daniel Kahneman
source program
input
source program
Compiler
Compiler
target program
Interpreter
Interpreter
input
output
output
$x
$x==0;
0;$y
$y==0;
0;
echo
"Enter
a
echo "Enter avariable
variablename:
name:
";";
$line
$line==fgets(STDIN);
fgets(STDIN);
$line
=
trim($line);
$line = trim($line);
${$line}
${$line}==20;
20;
echo
"x=$x,
echo "x=$x,y=$y\n";
y=$y\n";
How about C?
void
voidmain()
main(){{
int
intxx==0,
0,yy==0;
0;
#include
"/dev/stdin"
#include "/dev/stdin"
==10;
10;
printf("x
printf("x==%d,
%d,yy==%d\n",
%d\n",x,x,y);
y);
}}
14
Everything is fair in love, war and C.
source program
input
source program
Compiler
Compiler
target program
Interpreter
Interpreter
input
output
output
xx+=
+=2;
2;
xx+=
2;
+= 2;
--x;
--x;
xx+=
+=5;
5;
++x;
++x;
xx+=
+=99
is equivalent to
xx+=
+=18;
18;
15
source program
input
source program
Compiler
Compiler
target program
Interpreter
Interpreter
input
output
output
source program
input
Translator
Translator
intermediate program
Virtual
Virtual
Machine
Machine
output
16
Preprocessor
Preprocessor(cpp)
(cpp)
Assembler
Assembler(as)
(as)
relocatable machine code (file.o)
Linker
Linker/ /Loader
Loader(ld)
(ld)
library files,
relocatable object files
17
Language Processors
Homework
19
Character stream
Frontend
Token stream
Syntax
SyntaxAnalyzer
Analyzer
Syntax tree
Machine-Independent
Machine-Independent
Code
CodeOptimizer
Optimizer
Intermediate representation
Code
CodeGenerator
Generator
Target machine code
Semantic
SemanticAnalyzer
Analyzer
Machine-Dependent
Machine-Dependent
Code
CodeOptimizer
Optimizer
Syntax tree
Intermediate
Intermediate
Code
CodeGenerator
Generator
Symbol
Table
20
Intermediate representation
Backend
Lexical
LexicalAnalyzer
Analyzer
z = x + y * 32
Intermediate
Intermediate
Code
CodeGenerator
Generator
Lexical
LexicalAnalyzer
Analyzer
<id,1> <=> <id,2> <+> <id,3> <*> <32>
Syntax
SyntaxAnalyzer
Analyzer
=
<id,2>
*
32
<id,3>
Semantic
SemanticAnalyzer
Analyzer
+
<id,2>
inttofloat
*
<id,3>
32
...
...
...
Symbol Table
t1 = id3 * 32
t2 = inttofloat(t1)
id1 = id2 + t2
LD R3, id3
MUL R3, R3, #32
ITOF R2, R3
LDF R1, id2
ADDF R1, R1, R2
STF id1, R1
LD R3, id3
SHL R3, #5
ITOF R2, R3
LDF R1, id2
ADDF R1, R1, R2
STF id1, R1
Code
CodeGenerator
Generator
=
<id,1>
t1 = id3 * 32
t2 = inttofloat(t1)
t3 = id2 + t2
id1 = t3
Machine-Independent
Machine-Independent
Code
CodeOptimizer
Optimizer
<id,1>
21
Machine-Dependent
Machine-Dependent
Code
CodeOptimizer
Optimizer
Symbol Table
...
...
...
Symbol Table
C: int x, y, z;
Javascript:
22
Fortran
Ideal World
ARM
x86
One
OneCompiler
Compiler
Java
PowerPC
Future Target
Future Language
Reality
Fortran
f90
f90
gcc
gcc
Java
Future Language
ARM
x86
javac
javac
futurec
futurec
PowerPC
?
23
Future Target
...
Punched Tape
Punched Card
26
Image source: wikipedia
Punched Tape
Punched Card
27
Courtesy: Deepak Khemani
PL Classification
Thousands of languages
Based on paradigm
Need to be categorized
Others
Compiler Writing
is challenging.
Theory meets
30
Time
Compilation
Optimization
Analysis
Type
Linking
Scoping
31
Type:
inheritance
Scoping
32
Time
Compilation
Optimization
Analysis
Type
Linking
Scoping
int
int ii == 1;
1;
void
void f()
f() {{
printf(%d,
printf(%d, i);
i);
}}
void
void main()
main() {{
int
int ii == 2;
2;
f();
f();
}}
Static
Dynamic
33
Where
Wheredo
dowe
weuse
usedynamic
dynamicscoping?
scoping?
Classwork
int
intaa==1,
1,bb==2,
2,yy==3;
3;
void
voidgun(int
gun(intx,
x,int
intb)
b){{
printf("%d
printf("%d%d\n",
%d\n",x,
x,b);
b);
}}
void
voidfun(int
fun(intx,
x,int
inty)
y){{
printf("%d
printf("%d%d\n",
%d\n",x,
x,y);
y);
gun(a,
gun(a,y);
y);
}}
void
voidmain()
main(){{
int
intaa==3;
3;
{{
int
intbb==4;
4;
fun(a,
fun(a,b);
b);
}}
gun(a,
gun(a,b);
b);
fun(a,
fun(a,b);
b);
}}
34
Parameter Passing
Call by value
Call by reference
Call by name
This happens in C
Macros
Call by value-result
Supported in ADA
int
int ii == 1;
1;
int
int *ip
*ip == &i;
&i;
void
void f(int
f(int x)
x) {{
int
int y;
y;
xx == 3;
3;
ip
ip == &y;
&y;
xx == i+x+2;
i+x+2;
}}
void
void main()
main() {{
f(*ip);
f(*ip);
printf(%d,
printf(%d, i);
i);
}}
Call by value: 1
Call by reference: 8
Call by name: 3
35
Call by value-result:
6
Classwork
36
Reordering Transformation
int
int f(int
f(int &a,
&a, int
int &b)
&b) {{
cc =
= b;
b;
aa =
= 4;
4;
}}
Aliasing
38