Upload
uriel-petty
View
44
Download
1
Embed Size (px)
DESCRIPTION
Compiler Construction. Recap. 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. Exam. 18/02/2013 at 9:00 Past exams on my website חומר פתוח Possible questions Extend IC - PowerPoint PPT Presentation
Citation preview
Compiler Construction
Recap
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
Exam
• 18/02/2013 at 9:00• Past exams on my website• פתוח חומר• Possible questions
– Extend IC– Parsing– Register allocation– …
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 programclass 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
Parsing and ASTCLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI …
prog
class_list
class
field_method_list
field field_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
Parsing and AST
prog
class_list
class
field_method_list
field field_method_list
type ID(state)
BOOLEAN
method
field_method_list
…
…
Syntax tree builtduring parsing
Parser uses token stream, and generates derivation tree
CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI …
ProgAST
ClassAST
classList
FieldAST[0]type:BoolTypename:state
MethodAST[0]
MethodAST[1]
MethodAST[2]
…
…
methodListfieldList
Should know difference between derivation tree and AST Know how to build AST from input
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
Other possible questions
• Is the following grammar in LR(k)?• Build a parser for given grammar• Run an input string using your parser• …
9
Semantic analysis
ProgAST
ClassAST
classList
FieldAST[0]type:BoolType
MethodAST[0]
MethodAST[1]
MethodAST[2]
…
…
methodListfieldList
• Representing scopes• Type checking• Semantic checks
Symbol Kind Type
Hello class Hello
Symbol Kind Type Properties
state field boolean instance
main method string[]->void static
rise method void->boolean instance
setState method boolean->void instance
Symbol Kind Type
newState param int
(Program)
(Hello)
(setState)
…
10
Semantic conditions
• What is checked at compile time, and what is checked at runtime?
Event C/R
Program execution halts R (undecidable in general)
break/continue inside a while statement
C
Array index within bound R (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
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
Answer
• The change affects the lexical analysis, syntax analysis and semantic analysis
• Does not affect later phases– User-level semantic condition
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;%%<INITIAL> // { override=false; yybegin(comment); }<comment> @Override { override=true; }<comment> \n { if (override) return new Token(…,override,…) }
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
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.mIf we fail to find such a method, then we report an error
16
Question: IC language
Add constructors to IC (must be called)
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
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
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
Answer
Shape_isShape
Rectangle_isRectangle
Shape_isSqaure
Rectangle_surfaceArea
Shape_isShape
Rectangle_isRectangle
Sqaure_isSqaure
Rectangle_surfaceArea
Method table for rectangle Method table for square
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
Answer• Use the pointer to the dispatch table as the type indicator
• Translate x instanceof A asMove x,R0MoveField R0.0,R0Compare 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
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
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
Weighted reg. alloc. example
b
5 c
*
array access
+
a
base index
W=1
W=0 W=1
W=1W=1
W=2
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
ReminderR0 := TR[a+(b+(c*d))]
b
c d
*
+
+
aR0
R1
R2
Translation using all optimizationsshown until now uses 3 registers
R2
R1
left child first
b
c d
*
+
+
a
R0
Managed to save two registers
R0
R0
right child firstR0R0
27
Sethi Ullman• What type of tree is worst case for SU with respect to the tree’s height?
28
Sethi Ullman• What type of tree is best case for SU with respect to the tree’s height?
.
.
.
29
Sethi Ullman• What type of tree maximizes the ratio between registers allocated by
traversing the tree left-to-right and right-to-left?
.
.
.