Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Compiler Construction Recap Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University."— Presentation transcript:

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

2 2 Test 25/02/2009 at 9:00 חומר פתוח Look at Ran’s webpage for previous tests Possible questions Extend IC Parsing …

3 3 Scanning CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … Issues in lexical analysis: Language changes: New keywords, New operators, New meta-language features, e.g., annotations // An example program class Hello { boolean state; static void main(string[] args) { Hello h = new Hello(); boolean s = h.rise(); Library.printb(s); h.setState(false); } boolean rise() { boolean oldState = state; state = true; return oldState; } void setState(boolean newState) { state = newState; } }

4 4 Parsing and AST CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … prog class_list class field_method_list fieldfield_method_list type ID(state) BOOLEAN method field_method_list … … parser uses stream of tokens and generate derivation tree Issues in syntax analysis: Grammars: LL(1), LR(0) Building LR(0) parsers Transition diagram Parse table Running automaton Conflict resolution Factoring Ambiguity TA1

5 5 Parsing and AST prog class_list class field_method_list fieldfield_method_list type ID(state) BOOLEAN method field_method_list … … Syntax tree built during parsing parser uses stream of token and generate derivation tree CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI … ProgAST ClassAST classList FieldAST[0] type:BoolType name:state MethodAST[0] MethodAST[1] MethodAST[2] … … methodList fieldList Should know difference between derivation tree and AST Know how to build AST from input

6 6 FieldsOrMethods ::= Field:field FieldsOrMethods:next {: RESULT = next; RESULT. addField(field); :} | Method:method FieldsOrMethods:next {: RESULT = next; RESULT.addMethod(method); :} | /* empty */ {: RESULT = new FieldsMethods(); :};

7 7 Questions Is the following grammar in LR(0) ? Build a parser for the grammar Run an input string using your parser

8 8 Question Is the following grammar is LR(0)? S -> B $ B -> id P | id ( E ] P -> epsilon | ( E ) E -> B | B,E A grammar with epsilon production is not LR(0)

9 9 Semantic analysis ProgAST ClassAST classList FieldAST[0] type:BoolType MethodAST[0] MethodAST[1] MethodAST[2] … … methodList fieldList Representing scopes Type-checking Semantic checks SymbolKindType HelloclassHello SymbolKindTypeProperties statefieldbooleaninstance mainmethodstring[]->voidstatic risemethodvoid->booleaninstance setStatemethodboolean->voidinstance SymbolKindType newStateparamint (Program) (Hello) (setState) …

10 10 Semantic conditions What is checked in compile-time and what is checked in runtime? EventC/R Program execution halts Break/continue inside a while statement Array index within bound In Java the cast statement (Integer)f is legal In Java method o.m(…) is illegal since m is private

11 11 Semantic conditions What is checked in compile-time and what is checked in runtime? EventC/R Program execution haltsR (undecidable in general) Break/continue inside a while statement C Array index within boundR (undecidable in general) In Java the cast statement (A)f is legal Depends: if A is sub-type of f then checked during runtime (raising exception), otherwise flagged as an error during compilation In Java method o.m(…) is illegal since m is private C

12 12 Question – extend IC Support Java override annotation inside comments // @Override Annotation is written above method to indicate it overrides a method in superclass Describe the phases in the compiler affected by the change and the changes themselves class A { void rise() {…} } class B extends A { // @Override void rise() {…} } class A { void rise() {…} } class B extends A { // @Override void ris() {…} } Legal program Illegal program

13 13 Answer The change affects the lexical analysis, syntax analysis and semantic analysis Does not affect later phases User semantic condition

14 14 Changes to scanner Add pattern for @Override inside comment state patterns Add Java code to action to comments – instead of not returning any token, we now return a token for the annotation What if we want to support multiple annotations in comments? boolean override=false; % // { override=false; yybegin(comment); } @Override { override=true; } \n { if (override) return new Token(…,override,…) }

15 15 Changes to parser and AST method  static type name params ‘{‘ mbody ‘}’ | type name params ‘{‘ mbody ‘}’ | OVERRIDE type name params ‘{‘ mbody ‘}’ Add a Boolean flag to the method AST node to indicate that the method is annotated

16 16 Changes to semantic analysis Suppose we have an override annotation above a method m in class A We check the following semantic conditions 1. class A extends a superclass (otherwise it does not make sense to override a method) 2. Traverse the superclasses of A by going up the class hierarchy until we find the first method m and check that it has the same signature as A.m If we fail to find such a method we report an error

17 17 2006 מועד ב, שאלה 3 class ByRefExample { static void main(string [] args) { int x = 5; increment(x); Library.println(x); } static void increment(int & y) { y = y + 1; } }

18 18 2006 מועד ב, שאלה 3 Treat by reference as address Lexer Add token for & Parser Add reference type Semantic analysis Compare the actual parameter type and the reference type Treat the scope checking

19 19 2006 מועד ב, שאלה 3 IR Add support for by reference Code generation Caller Add the pointer of the actual parameter to the stuck instead of the parameter Callee Treat the parameter as a pointer

20 20 Question Add constructors to IC Solution: treat the constructor as a function and call when object allocated Mark the constructor Check that every object has a constructor Call the constructor on allocation IR Code generation

21 21 Question Add break(exp) exp is from type int break should exit exp loop Cannot perform break(n) can be perform only inside n nested loops!!! int x=2; while(true) { break(x); } Can we generally check this in compile time?

22 22 Translation to IR Accept annotated AST and translate functions into lists of instructions Compute offsets for fields and virtual functions Issues: dispatch tables, weighted register allocation Question: give the method tables for Rectangle and Square class Shape { boolean isShape() {return true;} boolean isRectangle() {return false;} boolean isSquare() {return false;} double surfaceArea() {…} } class Rectangle extends Shape { double surfaceArea() {…} boolean isRectangle() {return true;} } class Square extends Rectangle { boolean isSquare() {return true;} }

23 23 Answer Shape_isShape Rectangle_isRectangle Shape_isSqaure Rectangle_surfaceArea Shape_isShape Rectangle_isRectangle Sqaure_isSqaure Rectangle_surfaceArea Method table for rectangleMethod table for square

24 24 Possible question Suppose we wish to provide type information during runtime like instanceof in Java x instanceof A returns true iff x is exactly of type A (in Java it can also be subtype of A) Describe the changes in runtime organization needed to support this operator and the translation to IR

25 25 Answer Use the pointers to the dispatch table as the type indicators Translate x instanceof A as Move x,R0 MoveField R0.0,R0 Compare R0,_DV_A If we want to support the Java operator represent the type hierarchy during runtime and generate code to search up the hierarchy Keep info for each type for its ancestors and check in constant time

26 26 Question – Activation record Run the program on the input and show the stack at after … Add a “new” function that allocates on the stack How to do it? Disadvantages Stack size is limited Live until the method returns

27 27 “To Callee-save or to Caller-save?” Callee-saved registers need only be saved when callee modifies their value Some conventions exist (cdecl) %eax, %ecx, %edx – caller save %ebx, %esi, %edi – callee save %esp – stack pointer %ebp – frame pointer Use %eax for return value

28 28 Register allocation Sethi Ullman can only handle expressions without side effect Can change the execution order without hurt the program semantics Possible question Extend Sethi Ullman to ternary trees Global register allocation Is it optimal? IR registers are treated as local variables When we have an actual spill we use the stuck

29 29 Translating call/return to IR TR[e1.foo(e2)] R1 := TR[e1] R2 := TR[e2] VirtualCall R1. c foo (x=R2),R Constant representing offset of method f in dispatch table of class type of e1


Download ppt "Compiler Construction Recap Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University."

Similar presentations


Ads by Google