Course Overview
-
Upload
aline-rodriquez -
Category
Documents
-
view
33 -
download
0
description
Transcript of Course Overview
![Page 1: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/1.jpg)
Course OverviewMooly Sagiv
[email protected] 31703-640-7606
Wed 10:00-12:00
html://www.math.tau.ac.il/~msagiv/courses/wcc02.htmlTextbook:Modern Compiler Implementation in C
Andrew AppelISBN 0-521-58390-X
![Page 2: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/2.jpg)
Outline• High level programming languages
• Interpreter vs. Compiler
• Abstract Machines
• Why study compilers?
• Main Compiler Phases
![Page 3: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/3.jpg)
High Level Programming Languages• Imperative
– Algol, PL1, Fortran, Pascal, Ada, Modula, and C– Closely related to “von Neumann” Computers
• Object-oriented – Simula, Smalltalk, Modula3, C++, Java, C#– Data abstraction and ‘evolutionary’
form of program development• Class An implementation of an abstract data type (data+code)• Objects Instances of a class• Fields Data (structure fields)• Methods Code (procedures/functions with overloading)• Inheritance Refining the functionality of a class with different fields
and methods
• Functional– Lisp, Scheme, ML, Miranda, Hope, Haskel
• Logic Programming– Prolog
![Page 4: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/4.jpg)
Other Languages• Hardware description languages
– VHDL
– The program describes Hardware components
– The compiler generates hardware layouts
• Shell-languages Shell, C-shell, REXX
– Include primitives constructs from the current software environment
• Graphics and Text processing TeX, LaTeX, postscript– The compiler generates page layouts
• Web/Internet
– HTML, MAWL, Telescript, JAVA
• Intermediate-languages
– P-Code, Java bytecode, IDL, CLR
![Page 5: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/5.jpg)
Interpreter• Input
– A program– An input for the program
• Output– The required output
interpreter
source-program
program’s input program’s input
![Page 6: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/6.jpg)
Example
C interpreter
int x;scanf(“%d”, &x);x = x + 1 ;printf(“%d”, x);
5 6
![Page 7: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/7.jpg)
Compiler• Input
– A program
• Output– An object program that reads the input and
writes the output
compiler
source-program
program’s input program’s inputobject-program
![Page 8: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/8.jpg)
Example
Sparc-cc-compiler
int x;scanf(“%d”, &x);x = x + 1 ;printf(“%d”, x);
5 6
add %fp,-8, %l1mov %l1, %o1call scanfld [%fp-8],%l0add %l0,1,%l0st %l0,[%fp-8] ld [%fp-8], %l1 mov %l1, %o1 call printf
assembler/linker
object-program
![Page 9: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/9.jpg)
Interpreter vs. Compiler
• Conceptually simpler (the definition of the programming language)
• Easier to port• Can provide more
specific error report• Normally faster
• More efficient– Compilation is done once
for all the inputs --- many computations can be performed at compile-time
– Sometimes evencompile-time + execution-time < interpretation-time
• Can report errors before input is given
![Page 10: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/10.jpg)
Interpreters provide specific error report• Input-program
• Input data y=0
scanf(“%d”, &y);if (y < 0)
x = 5;... if (y <= 0)
z = x + 1;
![Page 11: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/11.jpg)
Compilers are usually more efficient
Sparc-cc-compiler
scanf(“%d”, &x);y = 5 ;z = 7 ;x = x +y*z;printf(“%d”, x);
add %fp,-8, %l1 mov %l1, %o1call scanfmov 5, %l0st %l0,[%fp-12]mov 7,%l0st %l0,[%fp-16]ld [%fp-8], %l0ld [%fp-8],%l0add %l0, 35 ,%l0st %l0,[%fp-8] ld [%fp-8], %l1 mov %l1, %o1 call printf
![Page 12: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/12.jpg)
Compilers can provide errors beforeactual input is given
• Input-program
• Compiler-Output “line 4: improper pointer/integer combination: op =''
int a[100], x, y ;scanf(“%d”, y) ;if (y < 0)
/* line 4*/ y = a ;
![Page 13: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/13.jpg)
Compilers can provide errors beforeactual input is given
• Input-program
• Compiler-Output “line 88: x may be used before set''
scanf(“%”, y);if (y < 0)
x = 5;... if (y <= 0)/* line 88 */ z = x + 1;
![Page 14: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/14.jpg)
Abstract Machines• A compromise between compilers and interpreters• An intermediate program representation• The intermediate representation is interpreted• Example: Zurich P4 Pascal Compiler(1981)
• Other examples: Java bytecode, MS .NET• The intermediate code can be compiled
Pascal compiler
Pascal Program
P-code
interpreter program’s inputprogram’s input
![Page 15: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/15.jpg)
Why Study Compilers• Become a compiler writer
– New programming languages– New machines– New compilation modes: “just-in-time”
• Using some of the techniques in other contexts• Design a very big software program using a
reasonable effort• Learn applications of many CS results (formal
languages, decidability, graph algorithms, dynamic programming, ...
• Better understating of programming languages and machine architectures
• Become a better programmer
![Page 16: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/16.jpg)
Course Requirements
• Compiler Project 35%– Develop a Tiger Front-End in C
• Two parts:– Lex+Yacc (Chapter 2, 3, 4)– Semantic analysis (5, 12)
– Tight schedule– Bonus 10%
• Theoretical Exercises 15% • Final exam 50%
![Page 17: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/17.jpg)
Compiler Phases
• The compiler program is usually written as sequence of well defined phases
• The interfaces between the phases is well defined (another language)
• It is sometimes convenient to use auxiliary global information (e.g., symbol table)
• Advantages of the phase separation:– Modularity
– Simplicity
– Reusabilty
![Page 18: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/18.jpg)
Basic Compiler PhasesSource program (string)
Fin. Assembly
lexical analysis
syntax analysis
semantic analysis
Translate
Instruction selection
Register Allocation
Tokens
Abstract syntax tree
Intermediate representation
Assembly
Finite automata
Pushdown automata
Memory organization
graph algorithms
Dynamic programming
![Page 19: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/19.jpg)
Example:straight-line programming
Stm ::=Stm ; Stm //(CompoundStm)Stm ::=id := Exp // (AssignStm)Stm ::= print (ExpList) // (PrintStm)Exp ::= id // (IdExp)Exp ::= num // (NumExp)Exp ::= Exp Binop Exp // (OpExp)Exp ::= (Stm, Exp) // (EseqExp)ExpList ::= Exp, ExpList // (PairExpList)ExpList ::= Exp // (LastExpList)Binop ::= + // (Plus)Binop ::= - // (Minus)Binop ::= * // (Times)Binop ::= / // (Div)
![Page 20: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/20.jpg)
Example Input
a := 5 + 3;b := (
print(a, a-1), 10 * a);b := print(b)
![Page 21: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/21.jpg)
Questions
• How to check that a program is correct?
• How to internally represent the compiled program?
![Page 22: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/22.jpg)
Lexical Analysis
a\b := 5 + 3 ;\nb := (print(a, a-1), 10 * a) ;\nprint(b)
• Input string
• Tokens
id (“a”) assign num (5) + num(3) ;id(“b”) assign
print(id(“a”) , id(“a”) - num(1)), num(10) * id(“a”)) ;print(id(“b”))
![Page 23: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/23.jpg)
Syntax Analysis• Tokens
• Abstract Syntax tree
id (“a”) assign num (5) + num(3) ;id(“b”) assign
print(id(“a”) , id(“a”) - num(1)), num(10) * id(“a”)) ;print(id(“b”))
CompoundStm
AssignStmCompoundStm
id opExp
numExp numExpPlus
5 3
a
AssignStm
id eseqExp
PrintStm
opExp
b
![Page 24: Course Overview](https://reader030.fdocuments.in/reader030/viewer/2022032804/56812ad4550346895d8eb72f/html5/thumbnails/24.jpg)
Summary
• Phases drastically simplifies the problem of writing a good compiler
• The Textbook offers a reasonable partition into phases with interface definition (in C)
• Every week we will study a new compiler phase