Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction 0368-3133 Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

Similar presentations


Presentation on theme: "Compiler Construction 0368-3133 Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University."— Presentation transcript:

1 Compiler Construction 0368-3133 Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University

2 2 Staff Instructor: Rina Zviel-Girshin Instructor: Rina Zviel-Girshin Grader: Paz Grimberg Grader: Paz Grimberg Technical Assistant: Ohad Shacham Technical Assistant: Ohad Shacham Schreiber Open-space (basement room 2) Schreiber Open-space (basement room 2) Wednesday 10:00 – 11:00 Wednesday 10:00 – 11:00 Phone #5358 Phone #5358

3 3 Administrative http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/wcc09.html http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/wcc09.html http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/wcc09.html Email: ohad.shacham@cs.tau.ac.il Email: ohad.shacham@cs.tau.ac.ilohad.shacham@cs.tau.ac.il Forum: https://forums.cs.tau.ac.il/viewforum.php?f=70 Forum: https://forums.cs.tau.ac.il/viewforum.php?f=70https://forums.cs.tau.ac.il/viewforum.php?f=70

4 4 Administrative There will be no recitation on November 4 th There will be no recitation on November 4 th Alternative recitation on one of the following: Alternative recitation on one of the following: November 20 November 20 November 27 November 27 December 4 December 4

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

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

7 7 How Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation JFlexJavaCupJava IC Program prog.ic x86 assembly prog.s x86 assembly prog.s libic.a (libic + gc) GNU assembler prog.o GNU linker prog.exe script / Ant

8 8 45% exam 45% exam 5% theoretical assignment 5% theoretical assignment 50% project 50% project 5 assignments – different weights 5 assignments – different weights code checked both automatically and manually code checked both automatically and manually Grading and schedule

9 9 Teams of 2 or 3 students Teams of 2 or 3 students Email me before next recitation Email me before next recitation List of members (first name, last name, id, username on nova) List of members (first name, last name, id, username on nova) Team-account user name Team-account user name There is adequate time to complete assignments There is adequate time to complete assignments Start early and please follow directions Start early and please follow directions Submission in your home directories Submission in your home directories Project guidelines

10 10 Goals: Goals: IC language overview IC language overview Understand the scope of the project Understand the scope of the project Today IC Language ic Executable code exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation

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

12 12 Unsupported features Access control Access control Everything is public Everything is public Interfaces Interfaces Method overloading (but still allow overriding) Method overloading (but still allow overriding) Exceptions Exceptions Packages Packages

13 13 IC program structure Program is sequence of class definitions Program is sequence of class definitions Class is sequence of fields and methods Class is sequence of fields and methods Static methods Static methods virtual methods virtual methods Exactly one main method static void main(string[] args) {...} Exactly one main method static void main(string[] args) {...}

14 14 IC program structure Variables can be declared anywhere in a method Variables can be declared anywhere in a method Check initialization before use Check initialization before use Object fields and Array elements are initialized Object fields and Array elements are initialized strings are primitive types strings are primitive types Arrays T[], T[][] Arrays T[], T[][]

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

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

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

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

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

20 20 class TestPoint { Point p; Point q; boolean test() { p = new Point(); q = p.movePoint(1,2); return p==q; }} class Point { int x; int y; Point movePoint(int newx, int newy) { x = newx; y = newy; return this; }} 00 xy p 12 xy p q Example 12 xy p this

21 21 Method implementation Each object knows how to access method code Each object knows how to access method code As if object contains slot pointing to the code As if object contains slot pointing to the code In reality implementations save space by sharing these pointers among instances of the same class In reality implementations save space by sharing these pointers among instances of the same class 00 xy * movePoint 00 xy * methods

22 22 Inheritance example We can extend points to colored points: We can extend points to colored points: class ColoredPoint extends Point { int color; Point movePoint(int newx, int newy) { color = 0; x = newx; y = newy; return this; }} 00 xy * movePoint methods 0 color

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

24 24 IC memory management Memory allocated every time new is used Memory allocated every time new is used Memory deallocated automatically by reclaiming unreachable objects Memory deallocated automatically by reclaiming unreachable objects Done by a garbage collector (GC) Done by a garbage collector (GC) Use off-the-shelf GC Use off-the-shelf GC 00 xy a … a = b 12 xy b 00 xy 12 xy b a unreachable object

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

26 26 For next week Split into teams Split into teams Send me email with team members and representative account Send me email with team members and representative account Read IC language specification Read IC language specification http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/doc/icspec.pdf http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/doc/icspec.pdf http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/doc/icspec.pdf Get acquainted with Java Get acquainted with Java J2SE 1.5 (or higher) J2SE 1.5 (or higher) J2SE 1.5 J2SE 1.5 Eclipse IDE Eclipse IDE Eclipse IDE Eclipse IDE Install and play with JFLex and Java Cup Install and play with JFLex and Java Cup

27 27 Next week Lexical analysis Lexical analysis JFLex JFLex Lexical analyzer generator in Java Lexical analyzer generator in Java Explain PA1 Explain PA1


Download ppt "Compiler Construction 0368-3133 Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University."

Similar presentations


Ads by Google