recap - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

recap

Description:

CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI ... scanner read text and generate token stream ... (Hello) (setState) 8. Semantic analysis. The roles of scopes ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 45
Provided by: RomanMa8
Category:
Tags: hello | recap

less

Transcript and Presenter's Notes

Title: recap


1
Winter 2007-2008Compiler ConstructionT11 Recap
Mooly Sagiv and Roman Manevich School of Computer
Science Tel-Aviv University
2
Exam 30/4/2008
  • Materials taught in class and recitations
  • Example exams on web-site
  • Last years exams in last years course
  • Popular types of questions
  • Introducing new features to language (IC)
  • Most reasonable Java features good candidates
  • Access control,
  • Exceptions,
  • Static fields
  • Object oriented-related features
  • Parsing related questions
  • Building LR(0) parser,
  • Resolving conflicts,
  • Running parser on input
  • Activation records concept level

3
Example program
// 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
4
Scanning
  • Issues in lexical analysis
  • Pattern matching conventions (longest match,
    priorities)
  • Running scanner automaton
  • 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
scanner read text and generate token stream
CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI
5
Parsing and AST
CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI
  • Issues in syntax analysis
  • Grammars LL(k), LR(k)
  • Building LR(0) parsers
  • Transition diagram
  • Parse table
  • Running automaton
  • Conflict resolution
  • Factoring
  • In parse table
  • Read TA1

parser uses stream of tokenand generate
derivation tree
prog
class_list
class
field_method_list
field
field_method_list
method
type
ID(state)
field_method_list

BOOLEAN

6
Parsing and AST
  • Should know difference between derivation
    tree and AST
  • Know how to build AST from input

CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI
parser uses stream of tokenand generate
derivation tree
prog
class_list
ProgAST
Syntax tree builtduring parsing
classList
class
ClassAST
methodList
fieldList
field_method_list
FieldAST0typeBoolTypenamestate
MethodAST0

field
field_method_list
MethodAST1

method
type
ID(state)
MethodAST2
field_method_list

BOOLEAN

7
Semantic analysis
  • Representing scopes
  • Type-checking
  • Semantic checks
  • Annotating AST

(Program)
ProgAST
(Hello)
classList
ClassAST
methodList
fieldList
FieldAST0typeBoolTypenamestate
MethodAST0

(setState)
MethodAST1


MethodAST2
8
Semantic analysis
  • The roles of scopes
  • Provide unique symbol for each identifier use
    symbols in next phases instead of identifiers
  • Disambiguate identifiers
  • Determine scope rules undeclared ids,
    double-declaration, declaration in wrong scope
  • Type-checking
  • Associate types with identifiers
  • Infer types for expressions
  • Check well-formed statements/classes etc.
  • Get to know type rule notations

9
Semantic conditions
  • What is checked in compile-time and what is
    checked in runtime?

10
Semantic conditions
  • What is checked in compile-time and what is
    checked in runtime?

11
More semantic conditions
class A class B extends A void foo()
B bArray new B10 A aArray
bArray A x new A() if () x new
B() aArray5x
(a) Explain why the assignment aArraybArray is
considered well-typed in Java.(b) Under what
conditions should/could the assignment
aArray5x lead to a runtime error? Explain.(c)
How does Java handle the problem with the
assignment aArray5x?
12
Answer
  • Since bArrayi is a subtype of aArrayi for
    every i
  • At the mentioned statement aArray points to an
    array of objects of type B. Therefore, when the
    condition does not hold, x points to an object of
    type A, and therefore the assignment is not
    type-safe
  • Java handles this by generating code to conduct
    type-checking during runtime. The generated code
    finds that the runtime type of x is X and the
    runtime type of the aArray is Y and checks that
    X is a subtype of X.If this condition doesnt
    hold it throws a ClassCastException

13
Possible question
  • Support Java override annotation inside comments
  • // _at_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

Legal program
Illegal program
class A void rise() class B extends A
// _at_Override void rise()
class A void rise() class B extends A
// _at_Override void raise()
14
Answer
  • The change affects the lexical analysis, syntax
    analysis and semantic analysis
  • It does not effect later phases since the
    annotation is meant to add a semantic condition
    by the user

15
Changes to scanner
  • Add pattern for _at_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 overridefalse //
overridefalse yybegin(comment)
_at_Override overridetrue \n if
(override) return new
Token(,override,)
16
Changes to parser and AST
  • Suppose we have rule
  • method ? static type name params mbody
    type name params mbody
  • Since that annotation is legal only for instance
    methods we rewrite the rule intomethod ? static
    type name params mbody type name params
    body OVERRIDE type name params
    mbody
  • We need to add a Boolean flag to the method AST
    node to indicate that the method is annotated

17
Changes to semantic analysis
  • Suppose we have an override annotation above a
    method m in class A
  • We check the following semantic condition using
    the following inform
  • We check that the class A extends a superclass
    (otherwise it does not make sense to override a
    method)
  • We check 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 we report an
    error
  • We use the following information
  • Symbol tables
  • Class hierarchy
  • Type table (for the types of methods)

18
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
  • Support extensions, e.g., translate switch
    statements
  • Question give 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
trueclass Square extends Rectangle
boolean isSquare() return true
19
Answer
Method table for rectangle
Method table for square
20
LIR translation
_DV_Hello _Hello_rise,_Hello_setState _Hello_ri
se Move this,R0 MoveField R0.0,R0 Move
R0,oldState Return oldState_Hello_setState
Move this,R0 Move newState,R1 MoveField
R1,newR0.0_ic_main __allocateObject(8),R0
MoveField _DV_Hello,R0.0 Move R0,h Move h,R0
VirtualCall R0.0(),R0 Move R0,s Library
__printb(s),Rdummy Move h,R0 VirtualCall
R0.1(newState0)
// 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
Suggestionplay around with microLIR
Compute methodand field offsets
methodToOffset
fieldToOffset
DVPtr 0
_Hello_rise 0
state 1
_Hello_setState1
Sometimes real offsets computed during (assembly)
code generation
21
Possible question
  • Suppose we wish to provide type information
    during runtime, e.g., to support operators like
    instanceof in Java
  • The operator returns true forx instanceof A 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
  • As a very restricted solution, we can avoid any
    changes to the runtime organization by using the
    pointers to the dispatch table as the type
    indicators
  • We translate x instanceof A asMove
    x,R0MoveField R0.0,R0Compare R0,_DV_A
  • The comparison is true iff the dispatch table of
    the object pointed-to by x is the dispatch table
    of class A, meaning that the object is of type A
  • If we want to support the Java operator we must
    represent the type hierarchy during runtime and
    generate code to search up the hierarchy(generati
    ng code with loops)

23
Code generation
  • Translate IR code to assembly
  • Not in exam
  • Run simple example and draw frame stacks in
    different points of execution
  • Interaction between register allocation and
    caller/callee save registers
  • Might be in exam
  • Activation records and call sequences
    conceptual level

24
Other issues
  • Liveness analysis

25
Sample questions 2007 a,b
26
???? (25 ??????) ????? ???????
  • ????? ?? ???????? ????? ??? ??? ????, ???
    ?????????, ?? ??? ????? ?????????.
  • ???? ??? ????? ????? ?? ????? ????? ?????? ??????
    ???? ??-??? ????????? ??
  • ????? ?????????, ????? ????? ???? ??????
    ??????????? ??????? ????? ??????.
  • ????? ??? ???? ?????? ??????, ?????? ?? ????
  • (5 ??') ???? ?? ??????? o ???? ????, ???? ????
    ?????? ??????? ?????? ?? ?????? ????.
  • (5 ??') ???? ?? ?????? x.ff, ?-f ????? ?????
    ???? ????? ????? ??-f ????? ????? ???? ??? ??
    ?????? A.
  • (5 ??') ???? ?? ???? ???? ????????? ???????????
    (dispatch table) ?? ????????? ??????? A ??? 16
    ???? ????? ???? ????????? ??????????? ?? ?????? B
    ?????? ?-A ??? 20 ????.
  • (5 ??') ????? ?? ?????? ???????? ?????? ??? ?????
    ???? ??????? ???????? ?????? ?? ??????? ???????
    ?-extern (???????? ???????? ?-extern ???? ???????
    ?????? ?????????? ???????).
  • (5 ??') ?????? ????? ?? ??? ????? eax ??????
    ??????.

27
?????
  • ?????? ????? ???? ????, ???? ?????? ?? ???? ????
    (garbage collector).
  • ?????? ????? ???? ????????? ???? ?????? ??????
    ???? ????? ?? ?????? ??????.
  • ?????? ????? ???? ????? ???? (??? ?????? ?? ???
    ??????), ???? ?????? ?????? ??????? ??????
    ????????? ??????? ????? ???????.
  • ?????? ????? ???? ????????? ???? ?-linking, ????
    ?????? ????? ????? ???????? ??????? ??????????
    ?????? ???? ?????? ???? ??????? ????????? ?????.
  • ?????? ????? ???? ????, ????? ????? ????????
    ?????, ???? ?????? ?????? ???????? ?caller-saved
    -

28
???? (20 ??????) ????? ??????
  • ???? ?????? ??? ??????? ?????? ?????? ?????? ?"?
    ????? assert
  • S ? assert C
  • S ? assert lp C rp
  • C ? P eq P P ? id id dot
    P
  • ?-tokens ???????? ?????? ??????? ?? ????????
    ???????? lp'(' rp')' eq''
  • dot'.' .
  • ??????? ?????? ?????? ?? ???? "assert(x.ay.b.c)"
    ????? "assert x.n.ny".?????? ????? LL(k)
    ???????? ?????? ????? ????? ????? ?? k ??????
    ????? (???? ?? ???????? ?????? ?????) ???? ??????
    ??? ?? ?????? ??? ?????? ?? ????? ?????? ??????
    ?????.
  • (3 ??') ??? ??? ?? k ????? ??-??? ???? ?????
    ?????? ?????? ????? ????? LL(k)? (??? tokens
    ????? ???? ?????? ??? ?????? ????? ??? ?????
    ??????) ??????.
  • (3 ??') ??? ???? ?????? ?? ??? k ?"? ?????
    ??????? ?? ??, ???? ????. ?? ??, ?????? ??? ??
    ???? ????? ???.
  • (4 ??') ???? ??? ?????? ????? ??-??? ???? ??????
    ?? P (??? ?????? ????????) ???? ???? ?-LR(0).
  • (10 ??') ???? ???? ???? ????? ???? ?? ???????
    ?-LR(0). ??? ?????? ???? ???? P'?id.??? ??
    ??????? ?????? (???????) ?? ?????? ??????? ???
    ???? ??????, ????? ???? ????? ?????? x.n.n"".

29
????? 2
  • ?????? ???? ?-LL(2), ??? ?????? ???? ?????? ???
    ???? S ? assert C ???? S ? assert lp C rp ????
    ?????? ?? ?-token ????, ????? ?? ??? ???? assert
    ?????? ??? ??? lp ?? token ???. ???-??, ???
    ?????? ??? ???? P ? id ???? P ? id dot P ????
    ????? ??? ?-token ????? id ??? dot ?? token ???.
    ??? ???? ?? ????? ?? ??? ?-tokens ????? ????.
  • ??, ???? ???? left-factoring ?"? ????? ?????
    ??????? ?? ?????? ?????? ????? S ? assert
    C'
  • C' ? C lp C rp
  • C ? P eq P
  • P ? id P'
  • P' ? e dot P
  • ??? ???? ?????? ??? ?????? ??????? ?-C' ??-P' ?"?
    ????? ?-token ?????? ????. ??? ??????
  • ??????? ???? ?-LL(1).
  • ?????? ?????? ????? ??? ??? ?????? ??? ?-LR(0)
    items ????? P ? id ? ?-P ? id ? dot P, ???? ????
    shift/reduce conflict, ???? ?????? ???? ????
    ?-LR(0).
  • ???? ????? ???? ??
  • P ? P' P ? P dot P'

  • P' ? id
  • (???? ?? ?????? ?????? ???? ??? ??? ???? ?????,
    ?? ?????? ?????? ??? ??? ??? ???? ??????.)

30
???? ???? ?'
31
???? ?-parser ?? x.n.n
32
???? 4 ????? ?????? ????????
???? ?????? ???? ?????? ?????? ????? ?? ???????
???????? ?? (????? ?????? ?? ???????? ?-liveness
analysis ????? ?????). ????-?? ?????? ?????
???????? ???? ???? ????? ?????? ???????.
enter x 8 / x, r1, r2 / y r1 / x, y,
r1 / z r2 / x, y, z / loop x x 1 /
x, y, z / y y z / x, y / z 2 /
x, y / if x 0 goto loop r1 y / r1
/ return / r1 /
  • (10 ??') ??? ?? ??? ??????? (?-interference
    graph) ?????? ?????? ????? ?? ??????? ???????.
  • (15 ??') ?????? ?? ???????? ?-graph coloring ??
    ??? ??????? ?????? ????? ?????? ??????? ?? ?????
    ????? ???????? r1, r2 ?-r3, ???? r1 ?-r2 ????????
    ???? ??????? ????????? ???? ???? ??.???-??
    ?????? ????????? ?? Briggs ?????? ????? ?????
    ((coalescing ??? ????? a ?-b ?????? ?????? ??
    ????? ?????? ???? ???? ?-k ????? ???? ???? ?????
    ?? ???? ?-k (????? ??????? k3).
  • ???????, ???????? ?-graph coloring, ??? ?????
    ????? ???? ?? ??????? ????? spill ?"? ?????????
    ????????-priority ?????? ??

priority (uo 10 ui) / deguo use def
outside of the loopui use def within the
loopdeg degree in the interference graph
33
????? ??? ???????
y
x
z
r1
r2
34
Spill priorities (uo 10 ui) / deg
35
????? ???? ???????? ??????
No simplification possibleTry coalescing yr1 or
zr2
only non-move related node deg3
y
x
z
r1
r2
36
????? ???? ???????? ??????
nodes of significant degree 1 (x) No simplification possibleTry coalescing zr2
yr1
x
z
r2
37
????? ???? ???????? ??????
yr1
can simplify
x
zr2
38
????? ???? ???????? ??????
can simplify
yr1
zr2
color stackx
39
????? ???? ???????? ??????
can simplify
zr2
color stack yr1 x
40
????? ???? ???????? ??????
color stackzr2 yr1 x
41
????? ???? ???????? ??????
pop and select
r2
zr2
color stack yr1 x
42
????? ???? ???????? ??????
r1
pop and select
yr1
r2
zr2
color stackx
43
????? ???? ???????? ??????
r1
yr1
pop and select
r3
r2
x
zr2
color stack
44
Good luckin the exam!
Write a Comment
User Comments (0)
About PowerShow.com