Download presentation
Presentation is loading. Please wait.
Published byCurtis Ellis Modified over 8 years ago
1
Compiler Construction Recap
2
2 Announcements PA4: extension until end of exam period – Not a single day more, for any reason! PA5: bonus exercise – Will be posted immediately after semester ends
3
3 Exam 18/02/2013 at 9:00 Past exams on my website חומר פתוח Possible questions – Extend IC – Parsing – Register allocation – …
4
4 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; } }
5
5 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 … … Issues in syntax analysis: Grammars: LL(1), LR(0) Ambiguity Parser uses token stream, and generates derivation tree
6
6 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 token stream, and generates 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
7
7 Question: Parsing 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 productions is not LR(0)
8
8 Other possible questions Is the following grammar in LR(k)? Build a parser for given grammar Run an input string using your parser …
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 at compile time, and what is checked at 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 subtype of f then checked at runtime (potentially raising exception); otherwise flagged as an error during compilation In Java, call o.m(…) is illegal since m is private C
11
11 Question: IC language 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
12
12 Answer The change affects the lexical analysis, syntax analysis and semantic analysis Does not affect later phases – User-level semantic condition
13
13 Changes to scanner Add pattern for @Override inside comment state patterns Add Java action code to comments: Instead of not returning any token, we now return a token for the annotation boolean override=false; % // { override=false; yybegin(comment); } @Override { override=true; } \n { if (override) return new Token(…,override,…) }
14
14 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
15
15 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, then we report an error
16
16 Question: IC language Add constructors to IC (must be called)
17
Answer Treat the constructor as a function, and call when object allocated – Lexical analysis: nothing – Parsing: AST node for constructor – Semantic analysis: Check that every class has a constructor Actual/formal compatibility – IR/code generation: Call the constructor on allocation
18
18 Translation to IR Accept annotated AST and translate functions into lists of instructions – Compute offsets for fields and virtual methods – Issues: dispatch tables, weighted register allocation
19
Question: IR 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;} }
20
20 Answer Shape_isShape Rectangle_isRectangle Shape_isSqaure Rectangle_surfaceArea Shape_isShape Rectangle_isRectangle Sqaure_isSqaure Rectangle_surfaceArea Method table for rectangleMethod table for square
21
21 Question: IR Suppose we wish to provide type information at runtime – Similar to 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
22
22 Answer Use the pointer to the dispatch table as the type indicator 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 at runtime and generate code to search up the hierarchy – Keep ancestor info for each type to enable constant-time checking
23
23 Register allocation Sethi Ullman – can only handle expressions without side effect Global register allocation IR registers are treated as local variables When we have an actual spill we use the stack
24
24 Weighted register allocation Can save registers by reordering subtree computations Label each node with its weight – Weight = number of registers needed – Leaf weight known – Internal node weight w(left) > w(right) then w = left w(right) > w(left) then w = right w(right) = w(left) then w = left + 1 Choose heavier child as first to be translated Have to check that there are no side effects
25
25 Weighted reg. alloc. example b 5c * array access + a baseindex W=1W=0W=1 W=2 Phase 1: - check absence of side-effects in expression tree - assign weight to each AST node R0 := TR[ a+b[5*c] ]
26
26 Reminder R0 := TR[a+(b+(c*d))] b cd * + + a R0 R1 R2 Translation using all optimizations shown until now uses 3 registers R2 R1 left child first b cd * + + a R0 Managed to save two registers R0 right child first R0
27
27 Sethi Ullman What type of tree is worst case for SU with respect to the tree’s height?
28
28 Sethi Ullman What type of tree is best case for SU with respect to the tree’s height?......
29
29 Sethi Ullman What type of tree maximizes the ratio between registers allocated by traversing the tree left-to-right and right-to-left?......
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.