Winter 2007 2008 Compiler Construction 0368 3133 Mooly

  • Slides: 26
Download presentation
Winter 2007 -2008 Compiler Construction 0368 -3133 Mooly Sagiv and Roman Manevich School of

Winter 2007 -2008 Compiler Construction 0368 -3133 Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University

Who Roman Manevich Schreiber Open-space (basement) Tel: 640 -5358 rumster@post. tau. ac. il Wednesday

Who Roman Manevich Schreiber Open-space (basement) Tel: 640 -5358 rumster@post. tau. ac. il Wednesday 16: 00 -17: 00 http: //www. cs. tau. ac. il/~rumster/wcc 07/ 2

Mailing list and forum n n n Mailing list web interface: http: //listserv. tau.

Mailing list and forum n n n Mailing list web interface: http: //listserv. tau. ac. il/archives/cs 0368 -3133 -02. html http: //listserv. tau. ac. il/archives/cs 0368 -3133 -03. html http: //listserv. tau. ac. il/archives/cs 0368 -3133 -04. html Activate e-mail by going to: https: //www. tau. ac. il/newuser/ Forwarding, changing email address: https: //www. tau. ac. il/forward/ http: //www. ims. tau. ac. il/tal/ Not registered? Mail to listserv@listserv. tau. ac. il with the line: subscribe CS 0368 -3133 -0 X Real Name where X is your recitation course number (2/3/4) Forum: http//: www. cs. tau. ac. il/system/forums/viewforum. php? f=55 3

Generic compiler structure Compiler txt Source text exe Frontend Semantic Backend (analysis) Representation (synthesis)

Generic compiler structure Compiler txt Source text exe Frontend Semantic Backend (analysis) Representation (synthesis) Executable code 4

IC compiler Compiler txt Source exe Frontend Semantic Backend (analysis) Representation (synthesis) text Executable

IC compiler Compiler txt Source exe Frontend Semantic Backend (analysis) Representation (synthesis) text Executable code ic IC Program Lexical Analysis Syntax Analysis Parsing AST Symbol Table etc. Inter. Rep. (IR) Code Generation exe x 86 executable 5

How Lexical Analysis prog. ic Syntax Analysis AST Parsing IC Program JFlex Java. Cup

How Lexical Analysis prog. ic Syntax Analysis AST Parsing IC Program JFlex Java. Cup Symbol Table etc. Inter. Rep. (IR) Code Generation prog. s x 86 assembly Java prog. s x 86 assembly GNU assembler prog. o libic. a (libic + gc) script / Ant GNU linker prog. exe 6

Grading and schedule n n n 45% 50% exam 5% theoretical assignment 50% project

Grading and schedule n n n 45% 50% exam 5% theoretical assignment 50% project n tea ms n 24/10 today 4 assignments + 1 bonus – different weights code checked both automatically and manually tentative schedule PA 1 31/10 23/1 30/1 PA 2 6/2 13/2 PA 3 20/2 27/2 PA 4 5/3 12/3 7/3 PA 5 19/3 26/3 ? ? /? ? 14/3 poll ? ? /? ? ? ? /? ? semester ends exam 7

Project guidelines n Teams of 2 or 3 students n Email me before next

Project guidelines n Teams of 2 or 3 students n Email me before next recitation n n n Subject: Compilation - new project team List of members (first name, last name, id, username on nova) Team-account user name In case of doubt – ask questions in the forum There is adequate time to complete assignments Start early and please follow directions 8

Today ic Lexical Analysis Syntax Analysis Parsing IC AST Symbol Table etc. Inter. Rep.

Today ic Lexical Analysis Syntax Analysis Parsing IC AST Symbol Table etc. Inter. Rep. (IR) exe Executable code Language n Code Generation Goals: n IC language overview n n See language specification Understand the scope of the project 9

IC language - main features n Strongly-typed n n n Object oriented n n

IC language - main features n Strongly-typed n n n Object oriented n n n Primitive types for int, boolean, string Reference types Objects, virtual method calls Inheritance Memory management n n n Dynamic heap allocation of obejcts and arrays Automatic deallocation (garbage collection) Runtime safety checks n n Many things are left out n n Null dereference Division by 0 Array access out of bounds Short implementation time Adapted with permission from Prof. Radu Rugina (Cornell University) 10

Unsupported features n Access control n n n n Everything is public Interfaces Downcasting

Unsupported features n Access control n n n n Everything is public Interfaces Downcasting Method overloading (but still allow overriding) Exceptions Packages Multiple source files 11

IC program structure n n Program is sequence of class definitions Class is sequence

IC program structure n n Program is sequence of class definitions Class is sequence of fields and methods n n n Variables can be declared anywhere in a method n n n Static methods and virtual methods Exactly one main method static void main(string[] args) {. . . } Check initialization before use Strings are primitive types Arrays T[], T[][] 12

IC types n n Every class is a type Primitive types: n n n

IC types n n Every class is a type Primitive types: n n n References : null Arrays : int [] x = new int[5]; All variables must be declared n n int : 1, -1, 2, -2, … boolean : true, false string : “hello” x. length==5; compiler infers types for expressions Type-safety n Well-typed programs do not result in runtime type errors 13

Subtyping n Inheritance induces subtyping relation n n Type hierarchy gives acyclic graph (forest)

Subtyping n Inheritance induces subtyping relation n n Type hierarchy gives acyclic graph (forest) Subtyping rules: A extends B {…} A≤B n A≤B A≤A B≤C A≤C null ≤ A Subtyping does not extend to array types n A subtype of B then A[] is not a subtype of B[] 14

Expressions n Expression language n n n n Every expression has a type and

Expressions n Expression language n n n n Every expression has a type and a value Loops: while (expr) { stmt } Conditionals: if (expr) stmt else stmt Arithmetic operators: + - * / % Relational compatison: < > == <= >= Logical operators: && || Unary operators: ! Assignment: x = expr 15

Objects n Instances of classes are objects class Point { int x; // initialized

Objects n Instances of classes are objects class Point { int x; // initialized to 0 int y; } n new Point() allocates object of class Point on heap and initializes fields n n No arguments An object can be thought of as a struct (record) with a slot for each field 0 x 0 y 16

Methods class Point { int x; int y; Point move. Point(int newx, int newy)

Methods class Point { int x; int y; Point move. Point(int newx, int newy) { x = newx; y = newy; return this; } -- close method } -- close class n A class can also define methods to manipulate fields n Methods can refer to the current object using this 17

Example class Test. Point { Point p; Point q; boolean test() { p =

Example class Test. Point { Point p; Point q; boolean test() { p = new Point(); q = p. move. Point(1, 2); return p==q; } } p p q class Point { int x; int y; Point move. Point(int newx, int newy) { x = newx; y = newy; p return this; this } } 0 x 0 y 1 x 2 y 18

Method implementation n n Each object knows how to access method code As if

Method implementation n n Each object knows how to access method code As if object contains slot pointing to the code 0 x n 0 y * move. Point In reality implementations save space by sharing these pointers among instances of the same class 0 x 0 y methods * move. Point 19

Inheritance example n We can extend points to colored points: class Colored. Point extends

Inheritance example n We can extend points to colored points: class Colored. Point extends Point { int color; Point move. Point(int newx, int newy) { color = 0; x = newx; y = newy; return this; } } 0 x 0 y 0 color * move. Point 20

Method invocation and inheritance n n Methods are invoked by dispatch Understanding dispatch in

Method invocation and inheritance n n Methods are invoked by dispatch Understanding dispatch in the presence of inheritance is a subtle aspect of OO languages Point p; p = new Colored. Point(); p. move. Point(1, 2); n n n p has static type Point p has dynamic type Colored. Point p. move. Point invokes Colored. Point implementation 21

Method invocation n Example: invoke method p. move. Point(a+b, 20/2) 1. Evaluate p 2.

Method invocation n Example: invoke method p. move. Point(a+b, 20/2) 1. Evaluate p 2. Find class of p 3. Find code of move. Point 4. Evaluate arguments a+b, 20/2 5. Bind p to this 6. Bind actual arguments to formal arguments 7. Run method code 22

IC memory management n n Memory allocated every time new is used Memory deallocated

IC memory management n n Memory allocated every time new is used Memory deallocated automatically by reclaiming unreachable objects n n Done by a garbage collector (GC) Use off-the-shelf GC … a = b a unreachable object 0 x 0 y b 1 x 2 y 0 x 0 y b a 1 x 2 y 23

Library functions n libic provides: n n n I/O operations datatype conversions system level-operations

Library functions n libic provides: n n n I/O operations datatype conversions system level-operations class Library { static void println(string s); static void printi(int i); static void printb(boolean b); static int readi(); static string readln(); static boolean eof(); static int stoi(string s, int n); static static string itos(int i); int[] stoa(string s); string atos(int[] a); int random(int i); int time(); void exit(int i); // // // // prints string s followed by a newline. prints string s. prints integer i. prints boolean b. reads one character from the input. reads one line from the input. checks end-of-file on standard input. returns the integer that s represents or n of s is not an integer. returns a string representation of i. an array with the ascii codes of chars in s. builds a string from the ascii codes in a. returns a random number between 0 and n-1. number of milliseconds since program start. terminates the program with exit code n. } 24

For next week n Split into teams n n Read IC language specification n

For next week n Split into teams n n Read IC language specification n n Send me email with team members and representative account http: //www. cs. tau. ac. il/~rumster/wcc 07/icspec. pdf Get acquainted with Java n n n J 2 SE 1. 5 (or higher) Eclipse IDE More helpful links on web-site 25

See you next week 26

See you next week 26