Modern Compiler Design


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Modern Compiler Design

  1. 1. Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University [email_address] Modern Compiler Design T1 - Overview
  2. 2. Who <ul><li>Greta Yorsh </li></ul><ul><li>Schrieber Open-space </li></ul><ul><li>Tel: 640-5358 </li></ul><ul><li>[email_address] </li></ul><ul><li>Wednesday 16:00-17:00 </li></ul><ul><li> </li></ul>
  3. 3. What Semantic Representation Backend (synthesis) Compiler Frontend (analysis) Executable code exe Source text txt
  4. 4. Say What? Semantic Representation Backend (synthesis) Compiler Frontend (analysis) Lexical Analysis Syntax Analysis Parsing AST Symbol Table etc. Inter. Rep. (IR) Code Gen. Executable code exe Source text txt Cool Language txt Executable code exe
  5. 5. How Lexical Analysis Syntax Analysis Parsing AST Symbol Table etc. Inter. Rep. (IR) Code Gen. flex bison C++ Cool GC MIPS assembly SPIM Cool Language txt Executable code exe
  6. 6. Project - a complete compiler <ul><li>Cool to MIPS assembly language </li></ul><ul><li>Implemented in C++ </li></ul><ul><li>Split into assignments </li></ul>
  7. 7. Grading <ul><li>50% exam </li></ul><ul><li>10% theoretical assignment </li></ul><ul><li>40% project </li></ul><ul><ul><li>5 assignments – different weights </li></ul></ul><ul><ul><li>code checked both automatically and manually </li></ul></ul>today exam teams PA1 PA2 TA1 PA3 PA4 PA5
  8. 8. Project Guidelines <ul><li>Teams of 3 students </li></ul><ul><ul><li>email me before next practice </li></ul></ul><ul><ul><li>team account </li></ul></ul><ul><li>In case of doubt – ask questions in the Forum </li></ul><ul><li>There is adequate time to complete assignments </li></ul><ul><li>Start early and please follow directions </li></ul><ul><li>Submit assignments on schedule </li></ul>
  9. 9. <ul><li>Goals: </li></ul><ul><ul><li>Understand the scope of the project </li></ul></ul><ul><ul><li>How to write a Cool program </li></ul></ul>Today Lexical Analysis Syntax Analysis Parsing AST Symbol Table etc. Inter. Rep. (IR) Code Gen. Cool Language txt Executable code exe
  10. 10. Cool Overview <ul><li>Classroom Object Oriented Language </li></ul><ul><li>Implementation of modern </li></ul><ul><ul><li>abstraction </li></ul></ul><ul><ul><li>static typing </li></ul></ul><ul><ul><li>reuse (inheritance) </li></ul></ul><ul><ul><li>memory management </li></ul></ul><ul><ul><li>and more… </li></ul></ul>
  11. 11. Good News <ul><li>No arrays </li></ul><ul><li>No floating point operations </li></ul><ul><li>No “static” modifier </li></ul><ul><li>No interfaces </li></ul><ul><li>No method overloading (but still allow overriding) </li></ul><ul><li>No exceptions </li></ul><ul><li>No packages </li></ul>
  12. 12. Better News <ul><li>Cool language is still rich enough for doing interesting things </li></ul>
  13. 13. A Simple Example <ul><li>class Point { </li></ul><ul><li>x : Int  0; </li></ul><ul><li>y : Int  0; </li></ul><ul><li>}; </li></ul><ul><li>Cool programs are sets of class definitions </li></ul><ul><li>class = a collection of attributes and methods </li></ul><ul><ul><li>A special class Main with a special method main </li></ul></ul><ul><ul><li>No separate notion of subroutine </li></ul></ul><ul><li>Instance of a class are objects </li></ul>
  14. 14. Cool Objects <ul><li>class Point { </li></ul><ul><li>x : Int  0; </li></ul><ul><li>y : Int ; (* use default value *) </li></ul><ul><li>}; </li></ul><ul><li>The expression “new Point” creates a new object of class Point </li></ul><ul><li>An object can be thought of as a struct (record) with a slot for each attribute </li></ul>0 0 x y
  15. 15. Methods <ul><li>class Point { </li></ul><ul><li>x : Int  0; </li></ul><ul><li>y : Int  0; </li></ul><ul><li>movePoint (newx : Int, newy : Int) : Point { </li></ul><ul><li>{ x  newx; </li></ul><ul><li>y  newy; </li></ul><ul><li>self; </li></ul><ul><li>} – close block expression </li></ul><ul><li>}; -- close method </li></ul><ul><li>}; -- close class </li></ul><ul><li>A class can also define methods for manipulating the attributes </li></ul><ul><li>Methods can refer to the current object using self </li></ul>
  16. 16. Information Hiding in Cool <ul><li>Methods are global (public) </li></ul><ul><li>Attributes are local to a class (private) </li></ul><ul><ul><li>they can only be accessed by the class’s methods </li></ul></ul><ul><li>Example </li></ul><ul><li>class Point { </li></ul><ul><li>… </li></ul><ul><li>getx () : Int { x }; </li></ul><ul><li>setx (newx : Int) : Int { x  newx }; </li></ul><ul><li>}; </li></ul>
  17. 17. Methods <ul><li>Each object knows how to access the code of a method </li></ul><ul><li>As if the object contains a slot pointing to the node </li></ul><ul><li>In reality implementations save space by sharing these pointers among instances of the same class </li></ul>0 0 x y * movePoint methods 0 0 x y * movePoint
  18. 18. Inheritance <ul><li>We can extend points to colored points using subclass (class hierarchy) </li></ul><ul><li>class ColorPoint inherits Points { </li></ul><ul><li>color : Int  0; </li></ul><ul><li>movePoint(newx : Int, newy : Int) : Point { </li></ul><ul><li>{ color  0; </li></ul><ul><li> x  newx; y  newy; </li></ul><ul><li>self; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>}; </li></ul>0 0 y color * movePoint 0 x
  19. 19. Cool Types <ul><li>Every class is a type </li></ul><ul><li>Base classes: </li></ul><ul><ul><li>Int </li></ul></ul><ul><ul><li>Bool (true, false) </li></ul></ul><ul><ul><li>String </li></ul></ul><ul><ul><li>Object (root of the class hierarchy) </li></ul></ul><ul><li>All variables must be declared </li></ul><ul><ul><li>compiler infers types for expressions </li></ul></ul>
  20. 20. Cool Type Checking <ul><li>x : A; </li></ul><ul><li>x  new B; </li></ul><ul><li>Is well typed if A is an ancestor of B in the class hierarchy </li></ul><ul><ul><li>anywhere an A is expected a B can be used </li></ul></ul><ul><li>Type safety </li></ul><ul><ul><li>A well-typed program cannot result in runtype type errors </li></ul></ul>
  21. 21. Method Invocation and Ingeritance <ul><li>Methods are invoked by dispatch </li></ul><ul><li>Understanding dispatch in the presence of inheritance is a subtle aspect of OO languages </li></ul><ul><li>p : Point ; </li></ul><ul><li>p  new ColorPoint; </li></ul><ul><li>p.movePoint(1,2); </li></ul><ul><ul><li>p has static type Point </li></ul></ul><ul><ul><li>p has dynamic type ColorPoint </li></ul></ul><ul><ul><li>p.movePoint must invoke the ColorPoint version </li></ul></ul>
  22. 22. Method Invokation <ul><li>Example: invoke one-argument method m </li></ul><ul><li>e.m(e’) </li></ul><ul><li>Eval e </li></ul><ul><li>Find class of e </li></ul><ul><li>Find code of m </li></ul><ul><li>Eval argument </li></ul><ul><li>Bind self and x </li></ul><ul><li>Run method </li></ul>
  23. 23. Other Expressions <ul><li>Expression language </li></ul><ul><ul><li>every expression has a type and a value </li></ul></ul><ul><ul><li>Loops: while E loop E pool </li></ul></ul><ul><ul><li>Conditionals: if E then E else E fi </li></ul></ul><ul><ul><li>Case statement: case E of x: Type  E; esac </li></ul></ul><ul><ul><li>Arithmetic operations </li></ul></ul><ul><ul><li>Logical operations </li></ul></ul><ul><ul><li>Assignment x  E </li></ul></ul>
  24. 24. Cool Memory Management <ul><li>Memory is allocated every time new is invoked </li></ul><ul><li>Memory is deallocated automatically when an object is not reachable anymore </li></ul><ul><ul><li>done by the garbage collector (GC) </li></ul></ul><ul><ul><li>There is a Cool GC </li></ul></ul>
  25. 25. Cool Language - Main features <ul><li>Object oriented </li></ul><ul><ul><li>Objects, virtual method calls, but no overloading </li></ul></ul><ul><li>Strongly typed </li></ul><ul><ul><li>Primitives for int, boolean, string </li></ul></ul><ul><ul><li>Reference types </li></ul></ul><ul><li>Dynamic allocation and Garbage Collection </li></ul><ul><ul><li>Heap allocation, automatic deallocation </li></ul></ul><ul><li>Many things are left out </li></ul><ul><ul><li>short time implementation </li></ul></ul><ul><li>Adapted with permission from Prof. Alex Aiken (Berkeley,Standford) </li></ul>