Compiler Construction 0368-3133 Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv...
-
date post
22-Dec-2015 -
Category
Documents
-
view
218 -
download
1
Transcript of Compiler Construction 0368-3133 Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv...
Compiler ConstructionCompiler Construction0368-3133 0368-3133
Ran Shaham and Ohad ShachamRan Shaham and Ohad ShachamSchool of Computer ScienceSchool of Computer Science
Tel-Aviv UniversityTel-Aviv University
22
StaffStaff
Instructor: Ran ShahamInstructor: Ran ShahamTechnical Assistant: Ohad ShachamTechnical Assistant: Ohad Shacham
Schreiber Open-space (basement room 2)Schreiber Open-space (basement room 2)03 – 640 – 535803 – 640 – 5358Wednesday 16:00 – 17:00Wednesday 16:00 – 17:[email protected]@cs.tau.ac.ilwww.cs.tau.ac.il/research/ohad.shacham/www.cs.tau.ac.il/research/ohad.shacham/ForumForum
33
Generic compiler structureGeneric compiler structure
Executable
code
exe
Source
text
txt
Semantic
Representation
Backend
(synthesis)
Compiler
Frontend
(analysis)
44
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
55
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
66
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
77
Teams of 2 or 3 studentsTeams of 2 or 3 students Email me before next recitationEmail me before next recitation
Subject: Subject: Compilation - new project teamCompilation - new project team 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
In case of doubt – send me an emailIn case of doubt – send me an email There is adequate time to complete assignmentsThere is adequate time to complete assignments Start early and please follow directionsStart early and please follow directions
Project guidelinesProject guidelines
88
Goals:Goals:IC language overviewIC language overview
See See language specificationlanguage specification
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
99
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
Many things are left outMany things are left out Short implementation timeShort implementation time
Adapted with permission from Prof. Radu Rugina (Cornell University)Adapted with permission from Prof. Radu Rugina (Cornell University)
1010
Unsupported featuresUnsupported features
Access controlAccess controlEverything is publicEverything is public
InterfacesInterfacesMethod overloading Method overloading
(but still allow overriding)(but still allow overriding)ExceptionsExceptionsPackagesPackages
1111
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) {...}
1212
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[][]
1313
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
1414
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[]
1515
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
1616
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
1717
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
1818
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
1919
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
*movePointmethods
2020
Inheritance exampleInheritance exampleWe 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 0y color
*movePoint
0x
2121
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
2222
Method invocationMethod invocationExample: invoke method Example: invoke method p.movePoint(a+b,20/2)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
2323
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
2424
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.}}
2525
For next weekFor next week
Split into teamsSplit into teamsSend me email with team members and Send me email with team members and
representative accountrepresentative accountRead IC language specificationRead IC language specification
http://www.cs.tau.ac.il/research/ohad.shacham/wcc08/doc/icspec.pdfhttp://www.cs.tau.ac.il/research/ohad.shacham/wcc08/doc/icspec.pdf
Get acquainted with JavaGet acquainted with JavaJ2SE 1.5J2SE 1.5 (or higher) (or higher)Eclipse IDEEclipse IDE