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

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

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Compiler ConstructionCompiler Construction0368-3133 0368-3133

Rina Zviel-Girshin and Ohad ShachamRina Zviel-Girshin and Ohad ShachamSchool of Computer ScienceSchool of Computer Science

Tel-Aviv UniversityTel-Aviv University

22

StaffStaff

Instructor: Rina Zviel-GirshinInstructor: Rina Zviel-Girshin Grader: Paz GrimbergGrader: Paz Grimberg Technical Assistant: Ohad ShachamTechnical Assistant: Ohad Shacham

Schreiber Open-space (basement room 2)Schreiber Open-space (basement room 2) Wednesday 10:00 – 11:00Wednesday 10:00 – 11:00 Phone #5358Phone #5358

33

AdministrativeAdministrative

http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/wcc09.htmlhttp://www.cs.tau.ac.il/research/ohad.shacham/wcc09/wcc09.html Email:Email: [email protected]@cs.tau.ac.il Forum:Forum: https://forums.cs.tau.ac.il/viewforum.php?f=70https://forums.cs.tau.ac.il/viewforum.php?f=70

44

AdministrativeAdministrative

There will be no recitation on November 4There will be no recitation on November 4 thth

Alternative recitation on one of the following:Alternative recitation on one of the following: November 20November 20 November 27November 27 December 4December 4

55

Generic compiler structureGeneric compiler structure

Executable

code

exe

Source

text

txt

Semantic

Representation

Backend

(synthesis)

Compiler

Frontend

(analysis)

66

Executable

code

exe

Source

text

txt

Semantic

Representation

Backend

(synthesis)

Compiler

Frontend

(analysis)

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compilerIC compiler

77

HowHowLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

JFlex JavaCup Java

ICProgram

prog.ic

x86 assembly

prog.s

x86 assembly

prog.s

libic.a(libic + gc)

GNU assembler

prog.o GNUlinker prog.exe

script / Ant

88

45% exam45% exam5% theoretical assignment 5% theoretical assignment 50% project50% project

5 assignments – different weights5 assignments – different weightscode checked both automatically and manuallycode checked both automatically and manually

Grading and scheduleGrading and schedule

99

Teams of 2 or 3 studentsTeams of 2 or 3 students Email me before next recitationEmail me before next recitation

List of members (first name, last name, id, username on List of members (first name, last name, id, username on nova)nova)

Team-account user nameTeam-account user name

There is adequate time to complete assignmentsThere is adequate time to complete assignments Start early and please follow directionsStart early and please follow directions

Submission in your home directoriesSubmission in your home directories

Project guidelinesProject guidelines

1010

Goals:Goals: IC language overviewIC language overview Understand the scope of the projectUnderstand the scope of the project

TodayToday

IC

Language

ic

Executable

code

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

1111

IC language - main featuresIC language - main features Strongly-typedStrongly-typed

Primitive types for int, boolean, stringPrimitive types for int, boolean, string Reference typesReference types

Object orientedObject oriented Objects, virtual method callsObjects, virtual method calls InheritanceInheritance

Memory managementMemory management Dynamic heap allocation of objects and arraysDynamic heap allocation of objects and arrays Automatic deallocation (garbage collection)Automatic deallocation (garbage collection) Runtime safety checksRuntime safety checks

Null dereferenceNull dereference Division by 0Division by 0 Array access out of boundsArray access out of bounds

Adapted with permission from Prof. Radu Rugina (Cornell Adapted with permission from Prof. Radu Rugina (Cornell University)University)

1212

Unsupported featuresUnsupported features

Access controlAccess controlEverything is publicEverything is public

InterfacesInterfacesMethod overloading Method overloading

(but still allow overriding)(but still allow overriding)ExceptionsExceptionsPackagesPackages

1313

IC program structureIC program structure

Program is sequence of Program is sequence of classclass definitions definitionsClass is sequence of Class is sequence of fieldsfields and and methodsmethods

Static methods Static methods virtualvirtual methods methods Exactly one Exactly one mainmain method methodstatic void main(string[] args) {...}static void main(string[] args) {...}

1414

IC program structureIC program structure

VariablesVariables can be declared anywhere in a can be declared anywhere in a methodmethod Check initialization before useCheck initialization before use

Object fields and Array elements are initializedObject fields and Array elements are initialized

stringsstrings are primitive types are primitive types ArraysArrays T[], T[][] T[], T[][]

1515

IC typesIC types Every class is a typeEvery class is a type Primitive types:Primitive types:

int :int : 1,-1,2,-2,…1,-1,2,-2,… boolean : true, falseboolean : true, false string : “hello”string : “hello”

References : References : nullnull Arrays : Arrays : int [] x = new int[5]; x.length==5;int [] x = new int[5]; x.length==5;

All variables must be declaredAll variables must be declared compiler infers types for expressionscompiler infers types for expressions

Type-safetyType-safety Well-typed programs do not result in runtime type errorsWell-typed programs do not result in runtime type errors

1616

SubtypingSubtyping

Inheritance induces subtyping relationInheritance induces subtyping relationType hierarchy gives acyclic graph (forest)Type hierarchy gives acyclic graph (forest)Subtyping rules:Subtyping rules:

A extends B {…}A extends B {…}

A ≤ BA ≤ B A ≤ AA ≤ A

A ≤ B B ≤ CA ≤ B B ≤ C

A ≤ CA ≤ C nullnull ≤ A ≤ A

Subtyping does not extend to array typesSubtyping does not extend to array typesA subtype of B then A[] A subtype of B then A[] is notis not a subtype of B[] a subtype of B[]

1717

ExpressionsExpressions

Expression languageExpression language Every expression has a type and a valueEvery expression has a type and a value Loops:Loops: whilewhile ((exprexpr)) {{ stmt stmt }} Conditionals:Conditionals: ifif ((exprexpr)) stmt stmt elseelse stmt stmt Arithmetic operators: Arithmetic operators: + - * / %+ - * / % Relational compatison: Relational compatison: < > == <= >=< > == <= >= Logical operators: Logical operators: && ||&& || Unary operators: Unary operators: ! -! - Assignment:Assignment: xx = = expr expr break, continuebreak, continue

1818

ObjectsObjects

Instances of classes are Instances of classes are objectsobjectsclass Point {class Point {

int x; // initialized to 0int x; // initialized to 0

int y;int y;}}

new Point()new Point() allocates object of class allocates object of class PointPoint on heap and initializes fieldson heap and initializes fields No argumentsNo arguments

An object can be thought of as a struct (record) An object can be thought of as a struct (record) with a slot for each fieldwith a slot for each field

0 0x y

1919

MethodsMethodsclass Point {class Point {int x;int x;int y;int y;Point movePoint(int newx, int newy) {Point movePoint(int newx, int newy) {

x = newx;x = newx;y = newy;y = newy;return return thisthis;;

}} -- close method -- close method}} -- close class -- close class A class can also define methods to manipulate fieldsA class can also define methods to manipulate fields Methods can refer to the current object using Methods can refer to the current object using thisthis

2020

class TestPoint {class TestPoint {Point p;Point p;Point q;Point q;boolean test() {boolean test() {

p = new Point();p = new Point();q = q = pp.movePoint(1,2);.movePoint(1,2);return p==q;return p==q;

}}}}

class Point {class Point {int x;int x;int y;int y;Point movePoint(int newx, int newy) {Point movePoint(int newx, int newy) {

x = newx;x = newx;y = newy;y = newy;return return thisthis;;

} } }}

0 0x y

p

1 2x y

pq

ExampleExample

1 2x y

pthis

2121

Method implementationMethod implementation

Each object knows how to access method codeEach object knows how to access method code As if object contains slot pointing to the codeAs if object contains slot pointing to the code

In reality implementations save space by sharing these In reality implementations save space by sharing these pointers among instances of the same classpointers among instances of the same class

0 0x y

*movePoint

0 0x y

*movePoint

methods

2222

Inheritance exampleInheritance example We can extend points to colored points:We can extend points to colored points:

class ColoredPoint extends Point {class ColoredPoint extends Point {int color;int color;Point movePoint(int newx, int newy) {Point movePoint(int newx, int newy) {

color = 0;color = 0;x = newx;x = newx;y = newy;y = newy;return this;return this;

}}}}

0 0x y

*movePoint

methods0color

2323

Method invocation and inheritanceMethod invocation and inheritance

Methods are invoked by dispatchMethods are invoked by dispatch Understanding dispatch in the presence of Understanding dispatch in the presence of

inheritance is a subtle aspect of OO languagesinheritance is a subtle aspect of OO languages

Point p;Point p;

p = new ColoredPoint();p = new ColoredPoint();

p.movePoint(1,2);p.movePoint(1,2); pp has has staticstatic type type PointPoint pp has has dynamicdynamic type type ColoredPointColoredPoint p.movePointp.movePoint invokes invokes ColoredPointColoredPoint

implementationimplementation

2424

IC memory managementIC memory management

Memory allocated every time Memory allocated every time newnew is used is used Memory deallocated automatically by Memory deallocated automatically by

reclaiming reclaiming unreachableunreachable objects objectsDone by a garbage collector (GC) Done by a garbage collector (GC) Use off-the-shelf GCUse off-the-shelf GC

0 0x y

a…a = b

1 2x y

b

0 0x y

1 2x y

ba

unreachableobject

2525

Library functionsLibrary functions libic provides:libic provides:

I/O operationsI/O operations datatype conversionsdatatype conversions system level-operationssystem level-operations

class Library {class Library { static void println(string s); static void println(string s); // prints string s followed by a newline.// prints string s followed by a newline. static void print(string s); static void print(string s); // prints string s. // prints string s. static void printi(int i); static void printi(int i); // prints integer i.// prints integer i. static void printb(boolean b); static void printb(boolean b); // prints boolean b.// prints boolean b. static int readi(); static int readi(); // reads one character from the input.// reads one character from the input. static string readln(); static string readln(); // reads one line from the input.// reads one line from the input. static boolean eof(); static boolean eof(); // checks end-of-file on standard input.// checks end-of-file on standard input. static int stoi(string s, int n); static int stoi(string s, int n); // returns the integer that s represents// 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); static string itos(int i); // returns a string representation of i.// returns a string representation of i. static int[] stoa(string s);static int[] stoa(string s); // an array with the ascii codes of chars in s.// an array with the ascii codes of chars in s. static string atos(int[] a);static string atos(int[] a); // builds a string from the ascii codes in a.// builds a string from the ascii codes in a. static int random(int n); static int random(int n); // returns a random number between 0 and n-1.// returns a random number between 0 and n-1. static int time(); static int time(); // number of milliseconds since program start.// number of milliseconds since program start. static void exit(int n); static void exit(int n); // terminates the program with exit code n.// terminates the program with exit code n.}}

2626

For next weekFor next week

Split into teamsSplit into teams Send me email with team members and Send me email with team members and

representative accountrepresentative account Read IC language specificationRead IC language specification

http://www.cs.tau.ac.il/research/ohad.shacham/wcc09/doc/icspec.pdfhttp://www.cs.tau.ac.il/research/ohad.shacham/wcc09/doc/icspec.pdf

Get acquainted with JavaGet acquainted with Java J2SE 1.5J2SE 1.5 (or higher) (or higher) Eclipse IDEEclipse IDE

Install and play with JFLex and Java CupInstall and play with JFLex and Java Cup

2727

Next weekNext week

Lexical analysisLexical analysisJFLexJFLex

Lexical analyzer generator in JavaLexical analyzer generator in Java

Explain PA1Explain PA1