Compiler Construction 0368-3133 Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.

Slides:



Advertisements
Similar presentations
Air Force Institute of Technology Electrical and Computer Engineering
Advertisements

Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms The C Programming Language.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Compilation Encapsulation Or: Why Every Component Should Just Do Its Damn Job.
Compiler Construction Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Imperative Programming
EECE 310: Software Engineering Lecture 2: Understanding Objects in Java and Types.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Code Generation III. PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 –
Basic Semantics Associating meaning with language entities.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Winter Compiler Construction Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
Object Oriented Programming Lecture 2: BallWorld.
Memory Management in Java Mr. Gerb Computer Science 4.
Design issues for Object-Oriented Languages
C11, Implications of Inheritance
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 User-Defined Methods.
Names and Attributes Names are a key programming language feature
Winter Compiler Construction
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C Basics.
An Introduction to Java – Part I, language basics
Programming Languages and Paradigms
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Presentation transcript:

Compiler Construction Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2 Staff Instructor: Ran Shaham Instructor: Ran Shaham Technical Assistant: Ohad Shacham Technical Assistant: Ohad Shacham Schreiber Open-space (basement room 2) Schreiber Open-space (basement room 2) 03 – 640 – – 640 – 5358 Wednesday 16:00 – 17:00 Wednesday 16:00 – 17: Forum Forum

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

4 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

5 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

6 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

7 Teams of 2 or 3 students Teams of 2 or 3 students me before next recitation me before next recitation Subject: Compilation - new project team Subject: Compilation - new project team 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 In case of doubt – send me an In case of doubt – send me an 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 Project guidelines

8 Goals: Goals: IC language overview IC language overview See language specification See language specificationlanguage specificationlanguage specification 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

9 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 Many things are left out Many things are left out Short implementation time Short implementation time Adapted with permission from Prof. Radu Rugina (Cornell University) Adapted with permission from Prof. Radu Rugina (Cornell University)

10 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

11 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) {...}

12 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[][]

13 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

14 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[]

15 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

16 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

17 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

18 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

19 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

20 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 ycolor * movePoint 0 x

21 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

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

23 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

24 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.}

25 For next week Split into teams Split into teams Send me with team members and representative account Send me with team members and representative account Read IC language specification Read IC language specification 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