41
Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Embed Size (px)

Citation preview

Page 1: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Context-sensitive Analysis, II

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

Page 2: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Evaluation Methods

Dynamic, dependence-based methods• Build the parse tree• Build the dependence graph• Topological sort the dependence graph• Define attributes in topological order

Rule-based methods (treewalk)

• Analyze rules at compiler-generation time• Determine a fixed (static) ordering• Evaluate nodes in that order

Oblivious methods (passes, dataflow)

• Ignore rules & parse tree• Pick a convenient order (at design time) & use it

Page 3: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

Page 4: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

Number

Sign List

BitList

BitList

Bit

1

0

1

pos:val:

pos:val:

pos:val:

pos:val:

pos:val:

pos: 0val:

val:

neg:

For “–101”

Page 5: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

Number

Sign List

BitList

BitList

Bit

1

0

1

pos: 1val: 0

pos: 0val: 1

pos: 2val: 4

pos: 2val: 4

pos: 1val: 4

pos: 0val: 5

val: –5

neg: true

For “–101”

Inherited Attributes

Page 6: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

Number

Sign List

BitList

BitList

Bit

1

0

1

pos: 1val: 0

pos: 0val: 1

pos: 2val: 4

pos: 2val: 4

pos: 1val: 4

pos: 0val: 5

val: –5

neg: true

For “–101”

Synthesized attributes

Page 7: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

Number

Sign List

BitList

BitList

Bit

1

0

1

pos: 1val: 0

pos: 0val: 1

pos: 2val: 4

pos: 2val: 4

pos: 1val: 4

pos: 0val: 5

val: –5

neg: true

For “–101”

Synthesized attributes

Page 8: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

Number

Sign List

BitList

BitList

Bit

1

0

1

pos: 1val: 0

pos: 0val: 1

pos: 2val: 4

pos: 2val: 4

pos: 1val: 4

pos: 0val: 5

val: –5

neg: true

For “–101”

& then peel away the parse tree ...

If we show the computation ...

Page 9: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Back to the Example

1

0

1

pos: 1val: 0

pos: 0val: 1

pos: 2val: 4

pos: 2val: 4

pos: 1val: 4

pos: 0val: 5

val: –5

neg: true

For “–101”

All that is left is the attribute dependence graph.

This succinctly represents the flow of values in the problem instance.

The dynamic methods sort this graph to find independent values, then work along graph edges.

The rule-based methods try to discover “good” orders by analyzing the rules.

The oblivious methods ignore the structure of this graph.

The dependence graph must be acyclic

Page 10: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circularity

We can only evaluate acyclic instances• We can prove that some grammars can only generate

instances with acyclic dependence graphs• Largest such class is “strongly non-circular” grammars

(SNC )• SNC grammars can be tested in polynomial time• Failing the SNC test is not conclusive

Many evaluation methods discover circularity dynamically Bad property for a compiler to have

SNC grammars were first defined by Kennedy & Warren

Page 11: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

A Circular Attribute Grammar

Productions Attribution RulesNumber → List List. a ← 0

List0 → List1 Bit List1.a ← List0.a + 1List0.b ← List1.bList1.c ← List1.b + Bit.val

| B it List0.b ← List0.a + List0.c + Bit.val

B it → 0 Bit.val ← 0| 1 Bit.val ← 2Bit.pos

Page 12: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos

Page 13: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos

Page 14: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos

Page 15: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos

Page 16: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos

Here is the circularity …

Page 17: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos

Here is the circularity …

Page 18: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

An Extended Example

Grammar for a basic block (§ 4.3.3)

Let’s estimate cycle counts

• Each operation has a COST

• Add them, bottom up

• Assume a load per value

• Assume no reuse

Simple problem for an AG

Hey, this looks useful !

Page 19: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

An Extended Example (continued) Block0

→ Block1 Assign Block0.cost ← Block1.cost + Assign.cost

⎯ Assign Block0.cost ← Assign.cost

Assign → I dent = Exp r ; Assign.cost ← COST(stor ) e + Exp .r cost

Expr0 → Expr1 + Term Expr0.cost ← Expr1.cost + COST(add) + Ter .mcost

⎯ Expr1 – Term Expr0.cost ← Expr1.cost + COST(add) + Ter .mcost

⎯ Term Expr0.cost ← Ter .mcostTerm0 → Term1 * Factor Term0.cost ← Term1.cost +

COST(mult ) + Factor.cost⎯ Term1 / Factor Term0.cost ← Term1.cost +

COST(div) + Factor.cost⎯ Factor Term0.cost ← Factor.cost

Factor → ( Exp r ) Factor.cost ← Exp .r cost⎯ Number Factor.cost ← COST(loadI )⎯ I dentif ier Factor.cost ← COST(load)

These are all synthesized attributes !

Values flow from rhs to lhs in prod’ns

Page 20: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

An Extended Example (continued)

Properties of the example grammar• All attributes are synthesized S-attributed grammar

• Rules can be evaluated bottom-up in a single pass Good fit to bottom-up, shift/reduce parser

• Easily understood solution• Seems to fit the problem well

What about an improvement?• Values are loaded only once per block (not at each use)• Need to track which values have been already loaded

Page 21: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Adding load tracking• Need sets Before and After for each production• Must be initialized, updated, and passed around the tree

A Better Execution Model

Factor → ( Expr ) Factor.cost ← Expr.cost ;Expr.Befor e ← Factor.Befor e ;Factor.Af te r ← Expr.After

⎯ Number Factor.cost ← COST(loadi) ;Factor.Af te r ← Factor.Before

⎯ Identifier If (Identifie .r name ∉ Factor.Befor )e then Factor.cost ← COST(load); Factor.Af te r ← Factor.Before ∪ Identifie .r name else Factor.cost ← 0 Factor.Af te r ← Factor.Before

This looks more complex!

Page 22: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

• Load tracking adds complexity• But, most of it is in the “copy rules”• Every production needs rules to copy Before & After

A sample production

These copy rules multiply rapidlyEach creates an instance of the setLots of work, lots of space, lots of rules to write

A Better Execution Model

Expr0 → Expr1 + Term Expr0 .cos t ← Expr1.cos t + COST(ad )d + Term.cost ;Expr1.Before ← Expr0 .Before ;Term.Before ← Expr1.Afte ;rExpr0 .Afte r ← Term.After

Page 23: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

What about accounting for finite register sets?• Before & After must be of limited size• Adds complexity to FactorIdentifier • Requires more complex initialization

Jump from tracking loads to tracking registers is small• Copy rules are already in place• Some local code to perform the allocation

Next class Curing these problems with ad-hoc syntax-directed

translation

An Even Better Model

Page 24: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Remember the Example from Last Lecture?

Grammar for a basic block (§ 4.3.3)

Let’s estimate cycle counts

• Each operation has a COST

• Add them, bottom up

• Assume a load per value

• Assume no reuse

Simple problem for an AG

Hey, this looks useful !

Page 25: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

And Its Extensions

Tracking loads • Introduced Before and After sets to record loads• Added ≥ 2 copy rules per production

Serialized evaluation into execution order

• Made the whole attribute grammar large & cumbersome

Finite register set• Complicated one production (Factor Identifier) • Needed a little fancier initialization• Changes were quite limited

Why is one change hard and the other easy?

Page 26: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

The Moral of the Story

• Non-local computation needed lots of supporting rules• Complex local computation was relatively easy

The Problems• Copy rules increase cognitive overhead• Copy rules increase space requirements

Need copies of attributes Can use pointers, for even more cognitive overhead

• Result is an attributed tree (somewhat subtle points) Must build the parse tree Either search tree for answers or copy them to the root

Page 27: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Addressing the Problem

If you gave this problem to a chief programmer in COMP 314

• Introduce a central repository for facts

• Table of names Field in table for loaded/not loaded state

• Avoids all the copy rules, allocation & storage headaches

• All inter-assignment attribute flow is through table Clean, efficient implementation Good techniques for implementing the table (hashing, §

B.3) When its done, information is in the table ! Cures most of the problems

• Unfortunately, this design violates the functional paradigm Do we care?

Rice’s team programming course…

Page 28: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

The Realist’s Alternative

Ad-hoc syntax-directed translation• Associate a snippet of code with each production• At each reduction, the corresponding snippet runs• Allowing arbitrary code provides complete flexibility

Includes ability to do tasteless & bad things

To make this work• Need names for attributes of each symbol on lhs & rhs

Typically, one attribute passed through parser + arbitrary code (structures, globals, statics, …)

Yacc introduced $$, $1, $2, … $n, left to right

• Need an evaluation scheme Fits nicely into LR(1) parsing algorithm

Page 29: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Extra Slides Start Here

Page 30: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Reworking the Example (with load tracking)

Block0 → Block1 Assign⎯ Assign cost ← 0;

Assign → Iden = t Ex pr ; cost← cos + t COST(store);Expr0 → Expr1 + Term cost← cos + t COST(add);

⎯ Expr1 – Term cost← cos + t COST(sub);⎯ Term

Term0 → Term1 * Factor cost← cos + t COST(mult);⎯ Term1 / Factor cost← cos + t COST(div);⎯ Factor

Factor → ( Ex pr )⎯ Number cost← cos + t COST(loadi);⎯ Identifier { i← hash(Identifie )r ;

if (Table[i].load ed= false) the n{ cost ← cos + t COST(load); Table[i].load ed← true; }}

This looks

cleaner &simpler !

Page 31: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Example — Building a Parse Tree

• Assume constructors for each node• Assume stack holds pointers to nodes• Assume yacc syntax

Goal → Expr $$ = $1;

Ex pr → Ex pr + T e r m $$ = MakeAddNode($1,$3);

| Ex pr – T e r m $$ = MakeSubNode($1,$3);

| T e rm $$ = $1;

T e rm → T e rm * Fa c t o r $$ = MakeMulNode($1,$3);

| T e rm / Fa c t o r $$ = MakeDivNode($1,$3);

| Fa c t o r $$ = $1;

Fa c t o r → ( Ex pr ) $$ = $1;

| n u m b e r $$ = MakeNumNode(token);

| id $$ = MakeIdNode(token);

Page 32: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Reality

Most parsers are based on this ad-hoc style of context-sensitive analysis

Advantages• Addresses the shortcomings of the AG paradigm• Efficient, flexible

Disadvantages• Must write the code with little assistance• Programmer deals directly with the details

Most parser generators support a yacc-like notation

Page 33: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Typical Uses

• Building a symbol table Enter declaration information as processed At end of declaration syntax, do some post processing Use table to check errors as parsing progresses

• Simple error checking/type checking Define before use lookup on reference Dimension, type, ... check as encountered Type conformability of expression bottom-up walk Procedure interfaces are harder

Build a representation for parameter list & types Create list of sites to check Check offline, or handle the cases for arbitrary

orderings

assumes table is global

Page 34: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Is This Really “Ad-hoc” ?

Relationship between practice and attribute grammars

Similarities• Both rules & actions associated with productions• Application order determined by tools, not author• (Somewhat) abstract names for symbols

Differences• Actions applied as a unit; not true for AG rules• Anything goes in ad-hoc actions; AG rules are functional• AG rules are higher level than ad-hoc actions

Page 35: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Limitations

• Forced to evaluate in a given order: postorder Left to right only Bottom up only

• Implications Declarations before uses Context information cannot be passed down

How do you know what rule you are called from within? Example: cannot pass bit position from right down

Could you use globals? In this case we could get the position from the left,

which is not much help (and it requires initialization)

Page 36: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Limitations

Can often rewrite the problem to fit S-attributed model

Number Sign List

$$ $1 x $2

Sign + $$ 1

| - $$ -1

List0 List1 Bit $$ 2 x $1 + $2

| Bit $$ $1

Bit 0 $$ 0

| 1 $$ 1

Remember, I warned you that I picked the attribution rules to highlight features of attribute grammars, rather than to show you the most efficient way to compute the answer!

Page 37: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Alternative Strategy

• Build Abstract Syntax Tree Use tree walk routines Use “visitor” design pattern to add functionality

TreeNodeVisitor

VisitAssignment(AssignmentNode)

VisitVariableRef(VariableRefNode)

TypeCheckVisitor

VisitAssignment(AssignmentNode)

VisitVariableRef(VariableRefNode)

AnalysisVisitor

VisitAssignment(AssignmentNode)

VisitVariableRef(VariableRefNode)

Page 38: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Visitor Treewalk I

TreeNode

Accept(NodeVisitor)

AssignmentNode

Accept(NodeVisitor v)

v.VisitAssignment(this)

VariableRefNode

Accept(NodeVisitor v)

v.VisitVariableRef(this)

• Parallel structure of tree: Separates treewalk code from node handling code Facilitates processing change without change to tree

structure

Page 39: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Visitor Treewalk II

VisitAssignment(aNodePtr)

// preprocess assignment

(aNodePtr->rhs)->Accept(this);

// postprocess rhs info;

(aNodePtr->lhs)->Accept(this);

// postprocess assignment;

To start the process:

AnalysisVisitor a; treeRoot->Accept(a);

Refers to current visitor!

Page 40: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Summary: Strategies for Context-Sensitive Analysis• Attribute Grammars

Pros: Formal, powerful, can deal with propagation strategies

Cons: Too many copy rules, no global tables, works on parse tree

• Postorder Code Execution Pros: Simple and functional, can be specified in grammar

(Yacc) but does not require parse tree Cons: Rigid evaluation order, no context inheritance

• Generalized Tree Walk Pros: Full power and generality, operates on abstract

syntax tree (using Visitor pattern) Cons: Requires specific code for each tree node type, more

complicated

Page 41: Context-sensitive Analysis, II Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice

Circular grammar

Number

Sign List

BitList

BitList

Bit

1

0

1 For “–101”

a: 0b:c:

a:b:c:

a:b:c:

val:

val:

val:

Number List

List.a 0

List0 List1

BitList1.a List0.a + 1List0.b List1.b

List1.c List1.b + Bit.val

| Bit List0.b List0.a + List0.c + Bit.val

Bit 0 Bit.val 0

| 1 Bit.val 2Bit.pos