Upload
bela
View
52
Download
0
Embed Size (px)
DESCRIPTION
Compiler Construction 0368-3133. Mooly Sagiv and Guy Golan-Gueta School of Computer Science Tel-Aviv University. Source text. txt. Executable code. exe. x86 executable. exe. IC Program. ic. Compiler. Frontend (analysis). Semantic Representation. Backend (synthesis). - PowerPoint PPT Presentation
Citation preview
Compiler ConstructionCompiler Construction0368-3133 0368-3133
Mooly Sagiv and Guy Golan-GuetaMooly Sagiv and Guy Golan-GuetaSchool of Computer ScienceSchool of Computer Science
Tel-Aviv UniversityTel-Aviv University
22
Executable
code
exe
Source
text
txt
Compiler
Semantic
Representation
Backend
(synthesis)
Frontend
(analysis)
ICProgram
ic
x86 executable
exeLexicalAnalysi
s
Syntax Analysi
s
Parsing
AST Symbol
Tableetc.
Inter.Rep.(IR)
CodeGeneration
33
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
44
50% exam50% exam50% project50% project
5 assignments – different weights5 assignments – different weightsPA1 - lexical analysisPA1 - lexical analysisPA2 - parsingPA2 - parsingPA3 - semantic analysisPA3 - semantic analysisPA4 - IR and IR loweringPA4 - IR and IR loweringPA5 - code generationPA5 - code generation
code checked both automatically and manuallycode checked both automatically and manually
GradingGrading
55
Teams of 2 or 3 studentsTeams of 2 or 3 students Email me before next recitationEmail me before next recitation
[email protected]@gmail.com 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 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
66
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
77
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)
88
Unsupported featuresUnsupported features
Access controlAccess controlEverything is publicEverything is public
InterfacesInterfacesMethod overloading Method overloading
(but still allow overriding)(but still allow overriding)ExceptionsExceptionsPackagesPackages
99
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) {...}
1010
IC program structureIC program structure
VariablesVariables can be declared anywhere in a method can be declared anywhere in a method 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[ ][ ]
1111
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
1212
SubtypingSubtyping
Inheritance induces subtyping relationInheritance induces subtyping relationType hierarchy gives acyclic graphType hierarchy gives acyclic graphSubtyping 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[]
1313
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
1414
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
1515
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
Methods can refer to the current object using Methods can refer to the current object using thisthis
1616
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
1717
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
1818
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
1919
Method invocation and inheritanceMethod invocation and inheritance
Example:Example:
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
2020
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
2121
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.}}
2222
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 Get acquainted with JavaGet acquainted with Java
J2SE 1.5 (or higher) (or higher) Eclipse IDE
Install and play with JFLex and Java CupInstall and play with JFLex and Java Cup
2323
Next weekNext week
Lexical analysisLexical analysisJFLexJFLex
Lexical analyzer generator in JavaLexical analyzer generator in Java
Explain PA1Explain PA1