Compiler
Autor:
Aho et al.
Folie: 1
Compiler
Compilerbau
Michael Leuschel
http://stups.hhu.de/w/Compilerbau,_WiSe_15
Aufbau
Softwaretechnik
Compiler
Autor:
Aho et al.
Sicherheitskritische
Systeme
5 KP
WS
Model Checking
5 KP
SS
Programmiersprachen
Einführung in die
logische
Programmierung
5 KP
Compilerbau
5 KP
Funktionale
Programmierung
5 KP
WS
Dynamische
Programmiersprachen
5 KP
SS
Programmiersprachen
II
5 KP
SS
Bücher I
Compiler
Autor:
Aho et al.
• Aho, Lam, Sethi, Ullman: Compiler,
2008, Pearson
• Errata:
– http://www.stups.uni-duesseldorf.de/
~leuschel/drachen.php
Bücher II
Compiler
• Kompilerbau:
Autor:
Aho et al.
– Andrew W. Appel, Modern Compiler Implementation in
Java (2nd edition), Cambridge University Press, 2002.
– http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=052182060x
• Anderes Hintergrundmaterial:
– Watt & Brown, Programming Language Processors in
Java, Prentice-Hall, 2000.
– Cooper, Torczon, Engineering a Compiler, Elsevier,
2004.
Compiler
Kapitel 1
Einleitung
Folie: 5
Compiler
Kapitel 1
Einleitung
Autor:
Aho et al.
Programmiersprachen
Compiler
Autor:
Aho et al.
Folie: 6
• Maschinen- und
Assemblersprachen
• Höhere Programmiersprachen
– übernehmen Speicherverwaltung,
Typkonsistenz, parallele
Ausführung,...
– höhere Produktivität, weniger Fehler
– gute Übersetzung wichtig um
Hardware effektiv zu nutzen
Compiler
Autor:
Aho et al.
Folie: 7
Ausführung eines
Programms
Compiler
Autor:
Aho et al.
Vorteile eines Compilers
(1.1.2)
• (viel) schnellere Ausführung
• Gesamter Code wird überprüft
Folie: 8
• Quellkode/Interpreter muss nicht
ausgeliefert werden
Compiler
Autor:
Aho et al.
Folie: 9
Vorteile eines Interpreters
(1.1.2)
• Keine Kompilationszeit
• Bessere Fehlerdiagnose
(Debugging)
• Dynamische Sprachkonstrukte:
– Introspektion/Reflection
– Modifikation des Programms zur
Laufzeit möglich
Hybridcompiler
Compiler
Kapitel 1
Einleitung
Folie: 10
manchmal:
JIT Compiler
Autor:
Aho et al.
Compiler
Autor:
Aho et al.
Folie: 11
Vorteile eines HybridCompilers
• kompakter Zwischenkode
• gegenüber Compiler:
– Platformunabhängig, bessere
Fehlerdiagnose, dynamische
Konstrukte bedingt möglich
• gegenüber Interpreter
– Gesamter Code überprüft, schneller
Compiler im Kontext
Compiler
Kapitel 1
Einleitung
Folie: 12
Autor:
Aho et al.
Einige bekannte Compiler
Compiler
Kapitel 1
https://gcc.gnu.org/
Einleitung
http://clang.llvm.org http://llvm.org
Folie: 13
javac
Java Hotspot
Autor:
Aho et al.
Microsoft Visual C++
https://code.google.com/p/v8/
Warum Compiler studieren ?
Compiler
Kapitel 1
Einleitung
Folie: 14
• Ein Standardwerkzeug eines
Informatikers
• Domain specific languages (DSLs)
– Vielleicht müssen Sie einen Compiler
schreiben
• Studium eines mittelgroßen
Softwaresystems
• Interessante Theorie und Algorithmen
– Nützlich für viele andere Anwendungen
Autor:
Aho et al.
Should I learn compilers?
Compiler
Kapitel 1
Einleitung
Folie: 15
Autor:
Aho et al.
http://stackoverflow.com/questions/733093/when-should-i-learn-compilers
Compilerbau:
Ein komplexes Unterfangen
Compiler
position = initial + rate*60
Kapitel 1
Einleitung
Folie: 16
Autor:
Aho et al.
?
Beispiel
Compiler
Autor:
Aho et al.
Folie: 17
#define CUBE(x) (x)*(x)*(x)
int main() {
int i = 0;
int x = 2;
int sum = 0;
while (i++ < 100) {
sum += CUBE(x);
}
printf("The sum is %d\n", sum);
}
https://en.wikibooks.org/wiki/Introduction_to_Programming_Languages/Compiled_Programs
Compiler
Autor:
Aho et al.
Folie: 18
#define CUBE(x) (x)*(x)*(x)
int main() {
int i = 0;
int x = 2;
int sum = 0;
while (i++ < 100) {
sum += CUBE(x);
}
printf("The sum is %d\n", sum);
}
Simple1.java
Compiler
Kapitel 1
public class Simple1 {
Einleitung
Folie: 19
Autor:
Aho et al.
public static void main(String args[])
{
int x = 3;
int y = 5;
int res = x + x*y + 2;
System.out.print("Result: ");
System.out.println(res);
}
}
Tokenized Simple1.java
Compiler
Kapitel 1
Einleitung
Folie: 20
Autor:
Aho et al.
LINE 2
PUBLIC_SYMBOL
CLASS_SYMBOL
IDENTIFIER: Simple1
LEFT_BRACE
LINE 4
PUBLIC_SYMBOL
STATIC_SYMBOL
VOID_SYMBOL
IDENTIFIER: main
LEFT_PARENTHESIS
IDENTIFIER: String
IDENTIFIER: args
LEFT_BRACKET
RIGHT_BRACKET
RIGHT_PARENTHESIS
LEFT_BRACE
LINE 6
INT_SYMBOL
IDENTIFIER: x
EQUALS
INTCONST: 3
SEMICOLON
LINE 7
INT_SYMBOL
IDENTIFIER:
EQUALS
INTCONST: 5
SEMICOLON
LINE 8
INT_SYMBOL
IDENTIFIER:
EQUALS
IDENTIFIER:
PLUS
IDENTIFIER:
STAR
IDENTIFIER:
PLUS
INTCONST: 2
SEMICOLON
LINE 9
IDENTIFIER:
DOT
IDENTIFIER: out
DOT
IDENTIFIER: print
y
LEFT_PARENTHESIS
STRINGCONST: "Result:
"
RIGHT_PARENTHESIS
SEMICOLON
LINE 10
res
IDENTIFIER: System
DOT
x
IDENTIFIER: out
DOT
x
IDENTIFIER: println
LEFT_PARENTHESIS
y
IDENTIFIER: res
RIGHT_PARENTHESIS
SEMICOLON
LINE 11
RIGHT_BRACE
System LINE 12
RIGHT_BRACE
Syntax tree for Simple1.java
(part)
Compiler
Kapitel 1
Einleitung
Folie: 21
Note: “+” is treated
as right associative
(normally it is left
associative!)
Autor:
Aho et al.
javap -c Simple1
Compiler
Bytecode
public class Simple1 extends java.lang.Object{
public Simple1();
Code:
0:
aload_0
1:
invokespecial
#1; //Method java/lang/Object."<init>":()V
4:
return
Kapitel 1
Einleitung
Folie: 22
Autor:
Aho et al.
public static void main(java.lang.String[]);
Code:
0:
iconst_3
1:
istore_1
2:
iconst_5
3:
istore_2
4:
iload_1
5:
iload_1
6:
iload_2
7:
imul
8:
iadd
9:
iconst_2
10: iadd
11: istore_3
12: getstatic
#2; //Field java/lang/System.out:Ljava/io/PrintStream;
15: ldc
#3; //String Result:
17: invokevirtual
#4; //Method java/io/PrintStream.print:(Ljava/lang/
String;)V
20: getstatic
#2; //Field java/lang/System.out:Ljava/io/PrintStream;
23: iload_3
24: invokevirtual
#5; //Method java/io/PrintStream.println:(I)V
27: return
}
Compiler Architektur
Compiler
position = initial + rate*60
Kapitel 1
Einleitung
Folie: 23
Autor:
Aho et al.
?
Compiler
Kapitel 1
Front End
(Analyse)
Einleitung
Folie: 24
Back End
(Synthese)
Autor:
Aho et al.
Beispiel
Compiler
•
Übersetzung von:
Kapitel 1
Einleitung
Folie: 25
position = initial + rate*60
Autor:
Aho et al.
Compiler
Kapitel 1
Einleitung
Folie: 26
Autor:
Aho et al.
Programmiersprachen:
Einige Konzepte (1.6)
Compiler
Kapitel 1
Einleitung
Folie: 27
• Statisch: zur Kompilierzeit
• Dynamisch: zur Laufzeit
• Gültigkeitsbereich (scope) eines Namens
• Statisch (lexikalisch) oder dynamisch
• Speicherort eines Namens
• Statisch oder dynamisch bestimmbar
• Umgebung verknüpft Namen mit Speicherort
• Wert eines Speicherorts
Autor:
Aho et al.
• Statisch oder
dynamisch
Gültigkeitsbereich :
Blockstruktur (1.6)
Compiler
Kapitel 1
Einleitung
Folie: 28
Autor:
Aho et al.
• Programmiersprachen mit Blockstruktur:
• erlauben Verschachtelung von Blöcken
Programmiersprachen:
Einige Konzepte II
Compiler
Kapitel 1
Einleitung
Folie: 29
Autor:
Aho et al.
• Bezeichner
• Zeichenstring der auf eine Entität
verweist (zB Datenobjekt, Prozedur,
Klasse oder Type)
• Bezeichner sind Namen (Namen sind
nicht immer Bezeichner, zB x.y)
• Variable eines Speicherorts
• Verweist auf einen bestimmten
Speicherort
Programmiersprachen:
Einige Konzepte III
Compiler
Kapitel 1
Einleitung
Folie: 30
Autor:
Aho et al.
• Deklaration
• Geben Typ an
• int i
• Definition
• Geben Wert an
• i = 7
Gültigkeitsbereich
Compiler
Kapitel 1
Einleitung
Folie: 31
• Der Gültigkeitsbereich (scope) einer Deklaration von x
ist der Kontext, in dem Verwendungen von x auf diese
Deklaration verweisen. Eine Sprache verwendet einen
statischen oder lexikalischen Gültigkeitsbereich, wenn
der Gültigkeitsbereich einer Deklaration aus dem
Programmtext abzulesen ist. Anderenfalls nutzt die
Sprache einen dynamischen Gültigkeitsbereich
• Lebensdauer:
– muss mindestens den Gültigkeitsbereich abdecken
– kann aber länger sein (zB static Variablen)
Autor:
Aho et al.
Statischer Gültigkeitsbereich
Blockstruktur
Compiler
Kapitel 1
Einleitung
Folie: 32
Autor:
Aho et al.
Dynamischer Gültigkeitsbereich
Compiler
Kapitel 1
Einleitung
Folie: 33
Autor:
Aho et al.
• Objekt-Orientierte Programmierung:
Vererbung
• Klasse C mit methode m()
• Unterklasse D von C mit eigenem m()
• Objekt x der Klasse C, Aufruf x.m()
• Dynamische Programmiersprachen
• Tcl, Python, Ruby
Übung
Compiler
Kapitel 1
Einleitung
Folie: 34
Autor:
Aho et al.
w=13
x=11
y=13
z=11
w=9
x=7
y=13
z=11
Werte für w,x,y,z ?
Übung
Compiler
Kapitel 1
Einleitung
Folie: 35
int w: B1 - B3 - B4
x: B1-B2-B4
y: B1-B5
z: B1-B2-B5
int x: B2-B3
z: B2
int w,x: B3
int w,x: B4
int y,z: B5
Autor:
Aho et al.
Gültigkeitsbereiche der zwölf Deklarationen?
Andere Themen in 1.6
Compiler
•
– public
– private
– protected
Kapitel 1
Einleitung
Folie: 36
Zugriffskontrolle
•
Parameterübergabe
– call by value
– call by reference
– call by name
– Aliasing
– Selber durchlesen
Autor:
Aho et al.
Zusammenfassung
Compiler
Kapitel 1
Einleitung
Folie: 37
Autor:
Aho et al.
• Compiler, Interpreter, Hyrbridcompiler
• Compilerphasen
• Compiler: Abfolge von Phasen
• Trennung: Back-End, Front-End
• Theorie und Praxis: Modellierung im
Compilerdesign (Automaten, Grammatiken,
reguläre Ausdrücke, Bäume)
Zusammenfassung
Compiler
Kapitel 1
Einleitung
Folie: 38
Autor:
Aho et al.
• Compiler, Interpreter, Hyrbridcompiler
• Compilerphasen
• Compiler: Abfolge von Phasen
• Trennung: Back-End, Front-End
• Einige Programmiersprachenkonzepte
• Statisch / Dynamisch
• Gültigkeitsbereich (scope)
• Umgebung