SlideShare a Scribd company logo
Composing Source-to-Source
Data-Flow Transformations with
Dependent Dynamic Rewrite Rules
Program Transformation 2004–2005

Eelco Visser
Institute of Information & Computing Sciences
Utrecht University,
The Netherlands

March 3, 2005
Outline

1

Data-flow transformation strategies

2

Dependencies in data-flow transformation rules

3

Generic data-flow transformation strategies

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Part I
Data-Flow Transformation Strategies

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Flow-Sensitive Constant Propagation

(x := 3;
y := x + 1;
if foo(x) then
(y := 2 * x;
x := y - 2)
else
(x := y;
y := 23);
z := x + y)

http://www.strategoxt.org

(x := 3;
y := 4;
if foo(3) then
(y := 6;
x := 4)
else
(x := 4;
y := 23);
z := 4 + y)

Composing Source-to-Source Data-Flow Transformations with
x := 3
x := 3
x -> 3
y := x + 1
y := 4
x -> 3
y -> 4
if foo(x)
if foo(3)
x -> 3
y -> 4
y := 2 * x
y := 6

x -> 3
y -> 4
x := y
x := 4

x -> 3
y -> 6

x -> 4
y -> 4

x := y - 2
x := 4

y := 23
y := 23

x -> 4
y -> 6

x -> 4
y -> 23

x -> 4
y z := x + y
z := 4 + y
Dependent dynamic rules
Strategy for Basic Constant Propagation
prop-const = PropConst <+ prop-const-assign
<+ prop-const-declare <+ prop-const-let <+ prop-const-if
<+ prop-const-while <+ (all(prop-const); try(EvalBinOp))
prop-const-assign =
|[ x := <prop-const => e> ]|
; if <is-value> e
then rules( PropConst.x : |[ x ]| -> |[ e ]| )
else rules( PropConst.x :- |[ x ]| ) end
prop-const-declare =
|[ var x := <prop-const => e> ]|
; if <is-value> e
then rules( PropConst+x : |[ x ]| -> |[ e ]| )
else rules( PropConst+x :- |[ x ]| ) end
prop-const-let =
?|[ let d* in e* end ]|; {| PropConst : all(prop-const) |}
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Intersection of Rule Sets

prop-const-if =
|[ if <prop-const> then <id> else <id> ]|
; (|[ if <id> then <prop-const> else <id> ]|
/PropConst |[ if <id> then <id> else <prop-const> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Intersection of Rule Sets

x
let var x := 1 var y := z 1
var z := 3 var a := 4 1
in x := x + z;
4
a := 5;
4
if y then
(y := y + 5;
4
z := 8)
4
else
(x := a + 21;
26
y := x + 1;
26
z := a + z);
26
b := a + z;
z := z + x end
-

y
-

z
3
3
3

a
4
4
5

b
-

-

3
8

5
5

-

27
27
-

3
3
8
8
8
8

5
5
5
5
5
5

13
13

http://www.strategoxt.org

let var x := 1 var y := z
var z := 3 var a := 4
in x := 4;
a := 5;
if y then
(y := y + 5;
z := 8)
else
(x := 26;
y := 27;
z := 8);
b := 13;
z := 8 + x end

Composing Source-to-Source Data-Flow Transformations with
Fixed-Point Intersection of Rule Sets
let var w := 20 var x := 20
var y := 20 var z := 10
in while SomethingUnknown()
(if x = 20 then w := 20
if y = 20 then x := 20
if z = 20 then y := 20
w; x; y; z end

do
else w := 10;
else x := 10;
else y := 10);

1
2

let var w := 20 var x := 20
var y := 20 var z := 10
in while SomethingUnknown() do
(if x = 20 then w := 20 else w := 10;
if y = 20 then x := 20 else x := 10;
y := 10);
w; x; y; 10 end

http://www.strategoxt.org

3
4

w
20
20
20
20
20
-

x
20
20
20
-

y
20
10
10
10
10
-

z
10
10
10
10
10
10
10
10
10

Composing Source-to-Source Data-Flow Transformations with
Fixed-Point Intersection of Rule Sets

prop-const-while =
?|[ while e1 do e2 ]|
; (/PropConst* |[ while <prop-const> do <prop-const> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Unreachable Code Elimination

let var x := 0 var y := 0
in x := 10;
while A do
(if x = 10
then dosomething()
else (dosomethingelse();
x := x + 1));
y := x
end

http://www.strategoxt.org

let var x := 0
var y := 0
in x := 10;
while A do
dosomething();
y := 10
end

Composing Source-to-Source Data-Flow Transformations with
Unreachable Code Elimination
prop-const-if =
|[ if <prop-const> then <id> else <id> ]|
; (EvalIf; prop-const
<+ (|[ if <id> then <prop-const> else <id> ]|
/PropConst
|[ if <id> then <id> else <prop-const> ]|))
prop-const-while =
?|[ while e1 do e2 ]|
; (|[ while <prop-const> do <id> ]|; EvalWhile
<+ (/PropConst*
|[ while <prop-const> do <prop-const> ]|))
EvalIf : |[ if
EvalIf : |[ if
where
EvalWhile : |[

0 then e1 else e2 ]| -> |[ e2 ]|
i then e1 else e2 ]| -> |[ e1 ]|
<not(eq)>(|[ i ]|, |[ 0 ]|)
while 0 do e ]| -> |[ () ]|

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Dead Code Elimination

(x := foo(b);
y := bar(h);
a := c + 23;
if 4 > x then
(d := b + a;
g := 4 + y)
else
(b := 2;
a := y + 3;
a := 4 + x);
print(a))

{c,b}
{x,c}
{x,c}
{x,a}
{a}
{a}
{x}
{x}
{x}
{a}

http://www.strategoxt.org

(x := foo(b);
a := c + 23;
if not(4> x) then

a := 4 + x;
print(a))

Composing Source-to-Source Data-Flow Transformations with
Dead Code Elimination
dce = VarNeeded <+ ElimAssign <+ dce-assign
<+ dce-seq <+ dce-if <+ dce-while <+ all(dce)
ElimAssign :
|[ x := e ]| -> |[ () ]|
where <not(Needed)> |[ x ]|
VarNeeded =
?|[ x ]|
; rules(Needed : |[ x ]|)
dce-assign =
?|[ x := e ]|
; rules(Needed :- |[ x ]|)
; |[ <id> := <dce> ]|

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Dead Code Elimination – Control-Flow

dce-seq =
|[ (<* reverse-filter(dce; not(?|[ () ]|)) >) ]|
dce-if =
(|[ if <id> then <dce> else <id> ]|
Needed/ |[ if <id> then <id> else <dce> ]|)
; |[ if <dce> then <id> else <id> ]|
; try(ElimIf)
dce-while =
|[ while <id> do <id> ]|
; (Needed/* |[ while <dce> do <dce> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Part II
Dependencies in Data-Flow Transformation Rules

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation
Replace copies x produced by assignments of the form x := y by
original y
a := b;
c := d + a

http://www.strategoxt.org

a := b;
c := d + b

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation
Replace copies x produced by assignments of the form x := y by
original y
a := b;
c := d + b

a := b;
c := d + a
First attempt using dynamic rules (wrong)

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Insufficient Dependencies
(a := b;
b := foo();
c := d + a)

(a := b;
b := foo();
c := d + b)

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Insufficient Dependencies
(a := b;
b := foo();
c := d + a)

(a := b;
b := foo();
c := d + b)

Problem: rule not undefined when variable in rhs changed
Solution: undefine rule when any of its variables is modified
copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Free Variable Capture
let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(a)
end
end

let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(b) // wrong!
end
end

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Free Variable Capture
let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(a)
end
end

let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(b) // wrong!
end
end

Problem: rule not undefined when variables become shadowed
Solution: undefine rule locally when some variable shadowed
copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (1)
let var a := bar()
in let var b := foo()
in a := b
end;
print(a)
end

let var a := bar()
in let var b := foo()
in a := b
end;
print(b) // wrong!
end

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (1)
let var a := bar()
in let var b := foo()
in a := b
end;
print(a)
end

let var a := bar()
in let var b := foo()
in a := b
end;
print(b) // wrong!
end

Problem: rule not undefined when a variable goes out of scope
Solution: (re)define rule in local scope
copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (2)
let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(a)

let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(c) // ok!

end

end

copy-prop-assign = ?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else rules( CopyProp.x :- |[ x ]| ) end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (2)
let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(a)

let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(c) // ok!

end

end

Problem: definition in local scope is too restricted
Solution: (re)define rule in innermost scope of all variables
involved
copy-prop-assign = ?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else rules( CopyProp.x :- |[ x ]| ) end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination
(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
a + b;
a + c;
1;
(a + c) + (a + b))

http://www.strategoxt.org

⇒

(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
x;
a + c;
1;
(a + c) + (a + b))

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination
(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
a + b;
a + c;
1;
(a + c) + (a + b))

⇒

(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
x;
a + c;
1;
(a + c) + (a + b))

Assignment
x := e
Propagation rule
|[ e ]| -> |[ x ]|
Dependencies in common-subexpression elimination
all variables in assignment x := e

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination
cse = cse-assign <+ (all(cse); try(ReplaceExp))
cse-assign =
|[ x := <cse => e> ]|
; where(<undefine-subexpressions> |[ x ]|)
; if <not(is-subterm(||[ x ]|))> |[ e ]| then
rules(ReplaceExp : |[ e ]| -> |[ x ]|)
; where(<register-subexpressions(|e)> |[ x := e ]|)
end
register-subexpressions(|e) =
get-vars; map({y : ?|[ y ]|
; rules(UsedInExp :+ |[ y ]| -> e)})
undefine-subexpressions =
bagof-UsedInExp; map({?e; rules(ReplaceExp :- |[ e ]|)})
get-vars = collect({?|[ x ]|})
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Dependent Dynamic Rules

Declare rule dependencies
R.lab : p1 -> p2
depends on [(lab1,dep1),...,(labn,depn)]
Undefine all rules depending on dep
undefine-R(|dep)
Locally undefine all rules depending on dep
new-R(|lab, dep)
and label current scope with lab

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation – Assignments
copy-prop =
repeat1(CopyProp)
<+ copy-prop-assign
<+ copy-prop-declare
<+ copy-prop-let <+ copy-prop-if <+ copy-prop-while
<+ all(copy-prop)
copy-prop-declare =
|[ var x ta := <copy-prop => e> ]|
; where( new-CopyProp(|x, x) )
; where( try(<copy-prop-assign-aux> |[ x := e ]|) )
copy-prop-assign =
|[ x := <copy-prop => e> ]|
; where( undefine-CopyProp(|x) )
; where( try(copy-prop-assign-aux) )

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation – Propagation Rule

copy-prop-assign-aux =
? |[ x := y ]|
; where( <not(eq)>(x,y) )
; where( innermost-scope-CopyProp => z )
; rules(
CopyProp.z : |[ x ]| -> |[ y ]|
depends on [(x,x), (y,y)]
)
innermost-scope-CopyProp =
get-var-names => vars
; innermost-scope-CopyProp(elem-of(|vars))

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation – Control-Flow

copy-prop-let =
|[ let <*id> in <*id> end ]|
; {| CopyProp : all(copy-prop) |}
copy-prop-if =
|[ if <copy-prop> then <id> else <id> ]|
; ( |[ if <id> then <copy-prop> else <id> ]|
/CopyProp |[ if <id> then <id> else <copy-prop> ]|)
copy-prop-while =
|[ while <id> do <id> ]|
; (/CopyProp* |[ while <copy-prop> do <copy-prop> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination – Assignments

cse =
cse-assign <+ cse-vardec <+ cse-let <+ cse-if
<+ cse-while <+ all(cse); try(CSE)
cse-vardec =
|[ var x ta := <cse => e> ]|
; new-CSE(|x, x)
; where( try(<cse-assign-aux> |[ x := e ]|) )
cse-assign =
|[ x := <cse => e> ]|
; undefine-CSE(|x)
; where(try(cse-assign-aux))

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination – Propagation

cse-assign-aux =
? |[ x := e ]|
; where( <not(oncetd(?|[ x ]|)); pure> |[ e ]| )
; where( get-var-names; map(!(<id>,<id>)) => xs )
; where( innermost-scope-CSE => z )
; rules( CSE.z : |[ e ]| -> |[ x ]| depends on xs )
pure =
?|[ i ]| + ?|[ x ]| + |[ <bo:id>(<pure>, <pure>) ]|
innermost-scope-CSE =
get-var-names => vars
; innermost-scope-CSE(where(<elem>(<id>, vars)))

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination – Control-Flow

cse-let =
|[ let <*id> in <*id> end ]|
; {| CSE : all(cse) |}
cse-if =
|[ if <cse> then <id> else <id> ]|
; ( |[ if <id> then <cse> else <id> ]|
/CSE |[ if <id> then <id> else <cse> ]|)
cse-while =
|[ while <id> do <id> ]|
; (/CSE* |[ while <cse> do <cse> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Part III
Generic Data-Flow Transformation Strategies

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Data-Flow Transformation Strategies

Data-flow transformation strategies are similar
Factor out underlying strategy
Requires generalization over combinators
new-dynamic-rules(|Rs,x,x)
undefine-dynamic-rules(|Rs,x)
/~Rs1~Rs2/

Allows very concise specifications for specific transformations
Combination of transformations

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Strategy – Framework
forward-prop(transform, before, after | Rs1, Rs2, Rs3) =
<conc>(Rs1, Rs2, Rs3) => RsSc;
<conc>(Rs1, Rs2) => RsDf;
let
fp = prop-assign <+ prop-declare <+ prop-let
<+ prop-if <+ prop-while
<+ transform(fp)
<+ (before; all(fp); after)
prop-assign = ...
prop-declare = ...
prop-let = ...
prop-if = ...
prop-while = ...
in fp
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Strategy – Assignments
prop-assign =
|[ <id> := <fp> ]|
; (transform(fp)
<+ before
; ?|[ x := e ]|
; undefine-dynamic-rules(|RsDf,x)
; after)
prop-declare =
|[ var <id> := <fp> ]|
; (transform(fp)
<+ before; ?|[ var x := e ]|
; new-dynamic-rules(|RsSc,x,x);after)
prop-let =
?|[ let d* in e* end ]|
; (transform(fp)
<+ {|~RsSc : before; all(fp); after |})
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Strategy – Control Flow

prop-if =
|[ if <fp> then <id> else <id> ]|
; (transform(fp)
<+ before
; (|[ if <id> then <fp> else <id> ]|
/~Rs1~Rs2/ |[ if <id> then <id> else <fp> ]|)
; after)
prop-while =
?|[ while e1 do e2 ]|
; (transform(fp)
<+ before
; /~Rs1~Rs2/* |[ while <fp> do <fp> ]|
; after)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: Constant Propagation
prop-const = forward-prop(prop-const-transform, id,
prop-const-after | ["PropConst"],[],[])
prop-const-transform(recur) =
EvalFor <+ EvalIf; recur
<+ |[ while <recur> do <id> ]|; EvalWhile
prop-const-after =
try(prop-const-assign <+ prop-const-declare
<+ PropConst <+ EvalBinOp)
prop-const-assign =
?|[ x := e ]|; where( <is-value> e )
; rules( PropConst.x : |[ x ]| -> |[ e ]|
depends on [(x,x)] )
prop-const-declare =
?|[var x ta := e]|; where(<prop-const-assign>|[x := e]|)
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: Copy Propagation
copy-prop =
forward-prop(no-transform,id,copy-prop-after
|["CopyProp"],[],[])
copy-prop-after =
try(copy-prop-assign <+ copy-prop-declare
<+ repeat1(CopyProp))
copy-prop-declare =
? |[ var x ta := e ]|
; where(try(<copy-prop-assign> |[ x := e ]|))
copy-prop-assign =
? |[ x := y ]|
; where( <not(eq)> (x, y) )
; where( get-var-dependencies => xs )
; where( innermost-scope-CopyProp => z )
; rules( CopyProp.z : |[ x ]| -> |[ y ]| depends on xs )
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: Common-Subexpression Elimination

cse =
forward-prop(no-transform, id, cse-after|["CSE"],[],[])
cse-after =
try(cse-assign <+ cse-declare <+ CSE)
cse-declare =
?|[ var x := e ]|; where( <cse-assign> |[ x := e ]| )
cse-assign
; where(
; where(
; where(
; rules(

= ?|[ x := e ]|
<pure-and-not-trivial(|x)> |[ e ]| )
get-var-dependencies => xs )
innermost-scope-CSE => z )
CSE.z : |[ e ]| -> |[ x ]| depends on xs )

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: A Super Optimizer
super-opt =
forward-prop(
prop-const-transform
, bvr-before
, bvr-after
; copy-prop-after
; prop-const-after
; cse-after
| ["PropConst", "CopyProp", "CSE"], [], ["RenameVar"])
Combination of
constant propagation
copy propagation
common-subexpression elimination
bound variable renaming
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with

More Related Content

PPTX
R Language Introduction
KEY
Presentation R basic teaching module
PPTX
Language R
PDF
3 R Tutorial Data Structure
PDF
Introduction to R programming
PPTX
Data analysis with R
PDF
R Programming: Export/Output Data In R
PPTX
R language introduction
R Language Introduction
Presentation R basic teaching module
Language R
3 R Tutorial Data Structure
Introduction to R programming
Data analysis with R
R Programming: Export/Output Data In R
R language introduction

What's hot (20)

PDF
R Programming: Learn To Manipulate Strings In R
PDF
Introduction to R Programming
PDF
R Programming: Importing Data In R
PDF
4 R Tutorial DPLYR Apply Function
PPTX
R programming language
PPT
R programming by ganesh kavhar
PDF
R Programming: Mathematical Functions In R
PDF
RDataMining slides-regression-classification
PDF
Next Generation Programming in R
PDF
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
PDF
Vasia Kalavri – Training: Gelly School
PDF
Data Analysis and Programming in R
PPTX
Merge Multiple CSV in single data frame using R
PPTX
R language
PPTX
The Very ^ 2 Basics of R
KEY
R for Pirates. ESCCONF October 27, 2011
PDF
R statistics with mongo db
PPTX
2. R-basics, Vectors, Arrays, Matrices, Factors
PDF
Introduction to R
PDF
Cocoaheads Meetup / Alex Zimin / Swift magic
R Programming: Learn To Manipulate Strings In R
Introduction to R Programming
R Programming: Importing Data In R
4 R Tutorial DPLYR Apply Function
R programming language
R programming by ganesh kavhar
R Programming: Mathematical Functions In R
RDataMining slides-regression-classification
Next Generation Programming in R
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Vasia Kalavri – Training: Gelly School
Data Analysis and Programming in R
Merge Multiple CSV in single data frame using R
R language
The Very ^ 2 Basics of R
R for Pirates. ESCCONF October 27, 2011
R statistics with mongo db
2. R-basics, Vectors, Arrays, Matrices, Factors
Introduction to R
Cocoaheads Meetup / Alex Zimin / Swift magic
Ad

Similar to Dependent dynamic rules (20)

PDF
Scoped dynamic rewrite rules
PDF
Compiler Construction | Lecture 10 | Data-Flow Analysis
PDF
Composing Source-to-Source Data-Flow Transformations with Rewriting Strategie...
PDF
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
PPTX
Building .NET Apps using Couchbase Lite
KEY
Let's build a parser!
PDF
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
PDF
Online partial evaluation
PDF
Compiler Construction | Lecture 11 | Monotone Frameworks
PDF
Data science at the command line
PPTX
Software engineering
PDF
NSC #2 - D2 06 - Richard Johnson - SAGEly Advice
PDF
Relaxing global-as-view in mediated data integration from linked data
PPTX
Pyretic - A new programmer friendly language for SDN
PDF
SLE2015: Distributed ATL
PPTX
Google cloud Dataflow & Apache Flink
PDF
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
PPTX
Introduction to Apache Flink
PDF
Refactoring to Macros with Clojure
PDF
Clojure+ClojureScript Webapps
Scoped dynamic rewrite rules
Compiler Construction | Lecture 10 | Data-Flow Analysis
Composing Source-to-Source Data-Flow Transformations with Rewriting Strategie...
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Building .NET Apps using Couchbase Lite
Let's build a parser!
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Online partial evaluation
Compiler Construction | Lecture 11 | Monotone Frameworks
Data science at the command line
Software engineering
NSC #2 - D2 06 - Richard Johnson - SAGEly Advice
Relaxing global-as-view in mediated data integration from linked data
Pyretic - A new programmer friendly language for SDN
SLE2015: Distributed ATL
Google cloud Dataflow & Apache Flink
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
Introduction to Apache Flink
Refactoring to Macros with Clojure
Clojure+ClojureScript Webapps
Ad

More from Eelco Visser (20)

PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
PDF
CS4200 2019 | Lecture 4 | Syntactic Services
PDF
CS4200 2019 | Lecture 3 | Parsing
PDF
CS4200 2019 | Lecture 2 | syntax-definition
PDF
CS4200 2019 Lecture 1: Introduction
PDF
A Direct Semantics of Declarative Disambiguation Rules
PDF
Declarative Type System Specification with Statix
PDF
Compiler Construction | Lecture 17 | Beyond Compiler Construction
PDF
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
PDF
Compiler Construction | Lecture 15 | Memory Management
PDF
Compiler Construction | Lecture 14 | Interpreters
PDF
Compiler Construction | Lecture 13 | Code Generation
PDF
Compiler Construction | Lecture 12 | Virtual Machines
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Compiler Construction | Lecture 6 | Introduction to Static Analysis
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
Compiler Construction | Lecture 4 | Parsing
PDF
Compiler Construction | Lecture 3 | Syntactic Editor Services
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 Lecture 1: Introduction
A Direct Semantics of Declarative Disambiguation Rules
Declarative Type System Specification with Statix
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 3 | Syntactic Editor Services

Recently uploaded (20)

PPTX
1. Introduction to Computer Programming.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Machine Learning_overview_presentation.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Approach and Philosophy of On baking technology
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Tartificialntelligence_presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
1. Introduction to Computer Programming.pptx
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
A comparative analysis of optical character recognition models for extracting...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine Learning_overview_presentation.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Approach and Philosophy of On baking technology
SOPHOS-XG Firewall Administrator PPT.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
Tartificialntelligence_presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf

Dependent dynamic rules

  • 1. Composing Source-to-Source Data-Flow Transformations with Dependent Dynamic Rewrite Rules Program Transformation 2004–2005 Eelco Visser Institute of Information & Computing Sciences Utrecht University, The Netherlands March 3, 2005
  • 2. Outline 1 Data-flow transformation strategies 2 Dependencies in data-flow transformation rules 3 Generic data-flow transformation strategies http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 3. Part I Data-Flow Transformation Strategies http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 4. Flow-Sensitive Constant Propagation (x := 3; y := x + 1; if foo(x) then (y := 2 * x; x := y - 2) else (x := y; y := 23); z := x + y) http://www.strategoxt.org (x := 3; y := 4; if foo(3) then (y := 6; x := 4) else (x := 4; y := 23); z := 4 + y) Composing Source-to-Source Data-Flow Transformations with
  • 5. x := 3 x := 3 x -> 3 y := x + 1 y := 4 x -> 3 y -> 4 if foo(x) if foo(3) x -> 3 y -> 4 y := 2 * x y := 6 x -> 3 y -> 4 x := y x := 4 x -> 3 y -> 6 x -> 4 y -> 4 x := y - 2 x := 4 y := 23 y := 23 x -> 4 y -> 6 x -> 4 y -> 23 x -> 4 y z := x + y z := 4 + y
  • 7. Strategy for Basic Constant Propagation prop-const = PropConst <+ prop-const-assign <+ prop-const-declare <+ prop-const-let <+ prop-const-if <+ prop-const-while <+ (all(prop-const); try(EvalBinOp)) prop-const-assign = |[ x := <prop-const => e> ]| ; if <is-value> e then rules( PropConst.x : |[ x ]| -> |[ e ]| ) else rules( PropConst.x :- |[ x ]| ) end prop-const-declare = |[ var x := <prop-const => e> ]| ; if <is-value> e then rules( PropConst+x : |[ x ]| -> |[ e ]| ) else rules( PropConst+x :- |[ x ]| ) end prop-const-let = ?|[ let d* in e* end ]|; {| PropConst : all(prop-const) |} http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 8. Intersection of Rule Sets prop-const-if = |[ if <prop-const> then <id> else <id> ]| ; (|[ if <id> then <prop-const> else <id> ]| /PropConst |[ if <id> then <id> else <prop-const> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 9. Intersection of Rule Sets x let var x := 1 var y := z 1 var z := 3 var a := 4 1 in x := x + z; 4 a := 5; 4 if y then (y := y + 5; 4 z := 8) 4 else (x := a + 21; 26 y := x + 1; 26 z := a + z); 26 b := a + z; z := z + x end - y - z 3 3 3 a 4 4 5 b - - 3 8 5 5 - 27 27 - 3 3 8 8 8 8 5 5 5 5 5 5 13 13 http://www.strategoxt.org let var x := 1 var y := z var z := 3 var a := 4 in x := 4; a := 5; if y then (y := y + 5; z := 8) else (x := 26; y := 27; z := 8); b := 13; z := 8 + x end Composing Source-to-Source Data-Flow Transformations with
  • 10. Fixed-Point Intersection of Rule Sets let var w := 20 var x := 20 var y := 20 var z := 10 in while SomethingUnknown() (if x = 20 then w := 20 if y = 20 then x := 20 if z = 20 then y := 20 w; x; y; z end do else w := 10; else x := 10; else y := 10); 1 2 let var w := 20 var x := 20 var y := 20 var z := 10 in while SomethingUnknown() do (if x = 20 then w := 20 else w := 10; if y = 20 then x := 20 else x := 10; y := 10); w; x; y; 10 end http://www.strategoxt.org 3 4 w 20 20 20 20 20 - x 20 20 20 - y 20 10 10 10 10 - z 10 10 10 10 10 10 10 10 10 Composing Source-to-Source Data-Flow Transformations with
  • 11. Fixed-Point Intersection of Rule Sets prop-const-while = ?|[ while e1 do e2 ]| ; (/PropConst* |[ while <prop-const> do <prop-const> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 12. Unreachable Code Elimination let var x := 0 var y := 0 in x := 10; while A do (if x = 10 then dosomething() else (dosomethingelse(); x := x + 1)); y := x end http://www.strategoxt.org let var x := 0 var y := 0 in x := 10; while A do dosomething(); y := 10 end Composing Source-to-Source Data-Flow Transformations with
  • 13. Unreachable Code Elimination prop-const-if = |[ if <prop-const> then <id> else <id> ]| ; (EvalIf; prop-const <+ (|[ if <id> then <prop-const> else <id> ]| /PropConst |[ if <id> then <id> else <prop-const> ]|)) prop-const-while = ?|[ while e1 do e2 ]| ; (|[ while <prop-const> do <id> ]|; EvalWhile <+ (/PropConst* |[ while <prop-const> do <prop-const> ]|)) EvalIf : |[ if EvalIf : |[ if where EvalWhile : |[ 0 then e1 else e2 ]| -> |[ e2 ]| i then e1 else e2 ]| -> |[ e1 ]| <not(eq)>(|[ i ]|, |[ 0 ]|) while 0 do e ]| -> |[ () ]| http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 14. Dead Code Elimination (x := foo(b); y := bar(h); a := c + 23; if 4 > x then (d := b + a; g := 4 + y) else (b := 2; a := y + 3; a := 4 + x); print(a)) {c,b} {x,c} {x,c} {x,a} {a} {a} {x} {x} {x} {a} http://www.strategoxt.org (x := foo(b); a := c + 23; if not(4> x) then a := 4 + x; print(a)) Composing Source-to-Source Data-Flow Transformations with
  • 15. Dead Code Elimination dce = VarNeeded <+ ElimAssign <+ dce-assign <+ dce-seq <+ dce-if <+ dce-while <+ all(dce) ElimAssign : |[ x := e ]| -> |[ () ]| where <not(Needed)> |[ x ]| VarNeeded = ?|[ x ]| ; rules(Needed : |[ x ]|) dce-assign = ?|[ x := e ]| ; rules(Needed :- |[ x ]|) ; |[ <id> := <dce> ]| http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 16. Dead Code Elimination – Control-Flow dce-seq = |[ (<* reverse-filter(dce; not(?|[ () ]|)) >) ]| dce-if = (|[ if <id> then <dce> else <id> ]| Needed/ |[ if <id> then <id> else <dce> ]|) ; |[ if <dce> then <id> else <id> ]| ; try(ElimIf) dce-while = |[ while <id> do <id> ]| ; (Needed/* |[ while <dce> do <dce> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 17. Part II Dependencies in Data-Flow Transformation Rules http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 18. Copy Propagation Replace copies x produced by assignments of the form x := y by original y a := b; c := d + a http://www.strategoxt.org a := b; c := d + b Composing Source-to-Source Data-Flow Transformations with
  • 19. Copy Propagation Replace copies x produced by assignments of the form x := y by original y a := b; c := d + b a := b; c := d + a First attempt using dynamic rules (wrong) copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 20. Problem: Insufficient Dependencies (a := b; b := foo(); c := d + a) (a := b; b := foo(); c := d + b) copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 21. Problem: Insufficient Dependencies (a := b; b := foo(); c := d + a) (a := b; b := foo(); c := d + b) Problem: rule not undefined when variable in rhs changed Solution: undefine rule when any of its variables is modified copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 22. Problem: Free Variable Capture let var a := bar() var b := baz() in a := b; let var b := foo() in print(a) end end let var a := bar() var b := baz() in a := b; let var b := foo() in print(b) // wrong! end end copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 23. Problem: Free Variable Capture let var a := bar() var b := baz() in a := b; let var b := foo() in print(a) end end let var a := bar() var b := baz() in a := b; let var b := foo() in print(b) // wrong! end end Problem: rule not undefined when variables become shadowed Solution: undefine rule locally when some variable shadowed copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 24. Problem: Escaping Variables (1) let var a := bar() in let var b := foo() in a := b end; print(a) end let var a := bar() in let var b := foo() in a := b end; print(b) // wrong! end copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 25. Problem: Escaping Variables (1) let var a := bar() in let var b := foo() in a := b end; print(a) end let var a := bar() in let var b := foo() in a := b end; print(b) // wrong! end Problem: rule not undefined when a variable goes out of scope Solution: (re)define rule in local scope copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 26. Problem: Escaping Variables (2) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(a) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(c) // ok! end end copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 27. Problem: Escaping Variables (2) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(a) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(c) // ok! end end Problem: definition in local scope is too restricted Solution: (re)define rule in innermost scope of all variables involved copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 28. Common-Subexpression Elimination (x y z a z := := := := := a + b; a + b; a + c; 1; (a + c) + (a + b)) http://www.strategoxt.org ⇒ (x y z a z := := := := := a + b; x; a + c; 1; (a + c) + (a + b)) Composing Source-to-Source Data-Flow Transformations with
  • 29. Common-Subexpression Elimination (x y z a z := := := := := a + b; a + b; a + c; 1; (a + c) + (a + b)) ⇒ (x y z a z := := := := := a + b; x; a + c; 1; (a + c) + (a + b)) Assignment x := e Propagation rule |[ e ]| -> |[ x ]| Dependencies in common-subexpression elimination all variables in assignment x := e http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 30. Common-Subexpression Elimination cse = cse-assign <+ (all(cse); try(ReplaceExp)) cse-assign = |[ x := <cse => e> ]| ; where(<undefine-subexpressions> |[ x ]|) ; if <not(is-subterm(||[ x ]|))> |[ e ]| then rules(ReplaceExp : |[ e ]| -> |[ x ]|) ; where(<register-subexpressions(|e)> |[ x := e ]|) end register-subexpressions(|e) = get-vars; map({y : ?|[ y ]| ; rules(UsedInExp :+ |[ y ]| -> e)}) undefine-subexpressions = bagof-UsedInExp; map({?e; rules(ReplaceExp :- |[ e ]|)}) get-vars = collect({?|[ x ]|}) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 31. Dependent Dynamic Rules Declare rule dependencies R.lab : p1 -> p2 depends on [(lab1,dep1),...,(labn,depn)] Undefine all rules depending on dep undefine-R(|dep) Locally undefine all rules depending on dep new-R(|lab, dep) and label current scope with lab http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 32. Copy Propagation – Assignments copy-prop = repeat1(CopyProp) <+ copy-prop-assign <+ copy-prop-declare <+ copy-prop-let <+ copy-prop-if <+ copy-prop-while <+ all(copy-prop) copy-prop-declare = |[ var x ta := <copy-prop => e> ]| ; where( new-CopyProp(|x, x) ) ; where( try(<copy-prop-assign-aux> |[ x := e ]|) ) copy-prop-assign = |[ x := <copy-prop => e> ]| ; where( undefine-CopyProp(|x) ) ; where( try(copy-prop-assign-aux) ) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 33. Copy Propagation – Propagation Rule copy-prop-assign-aux = ? |[ x := y ]| ; where( <not(eq)>(x,y) ) ; where( innermost-scope-CopyProp => z ) ; rules( CopyProp.z : |[ x ]| -> |[ y ]| depends on [(x,x), (y,y)] ) innermost-scope-CopyProp = get-var-names => vars ; innermost-scope-CopyProp(elem-of(|vars)) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 34. Copy Propagation – Control-Flow copy-prop-let = |[ let <*id> in <*id> end ]| ; {| CopyProp : all(copy-prop) |} copy-prop-if = |[ if <copy-prop> then <id> else <id> ]| ; ( |[ if <id> then <copy-prop> else <id> ]| /CopyProp |[ if <id> then <id> else <copy-prop> ]|) copy-prop-while = |[ while <id> do <id> ]| ; (/CopyProp* |[ while <copy-prop> do <copy-prop> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 35. Common-Subexpression Elimination – Assignments cse = cse-assign <+ cse-vardec <+ cse-let <+ cse-if <+ cse-while <+ all(cse); try(CSE) cse-vardec = |[ var x ta := <cse => e> ]| ; new-CSE(|x, x) ; where( try(<cse-assign-aux> |[ x := e ]|) ) cse-assign = |[ x := <cse => e> ]| ; undefine-CSE(|x) ; where(try(cse-assign-aux)) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 36. Common-Subexpression Elimination – Propagation cse-assign-aux = ? |[ x := e ]| ; where( <not(oncetd(?|[ x ]|)); pure> |[ e ]| ) ; where( get-var-names; map(!(<id>,<id>)) => xs ) ; where( innermost-scope-CSE => z ) ; rules( CSE.z : |[ e ]| -> |[ x ]| depends on xs ) pure = ?|[ i ]| + ?|[ x ]| + |[ <bo:id>(<pure>, <pure>) ]| innermost-scope-CSE = get-var-names => vars ; innermost-scope-CSE(where(<elem>(<id>, vars))) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 37. Common-Subexpression Elimination – Control-Flow cse-let = |[ let <*id> in <*id> end ]| ; {| CSE : all(cse) |} cse-if = |[ if <cse> then <id> else <id> ]| ; ( |[ if <id> then <cse> else <id> ]| /CSE |[ if <id> then <id> else <cse> ]|) cse-while = |[ while <id> do <id> ]| ; (/CSE* |[ while <cse> do <cse> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 38. Part III Generic Data-Flow Transformation Strategies http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 39. Generic Data-Flow Transformation Strategies Data-flow transformation strategies are similar Factor out underlying strategy Requires generalization over combinators new-dynamic-rules(|Rs,x,x) undefine-dynamic-rules(|Rs,x) /~Rs1~Rs2/ Allows very concise specifications for specific transformations Combination of transformations http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 40. Generic Strategy – Framework forward-prop(transform, before, after | Rs1, Rs2, Rs3) = <conc>(Rs1, Rs2, Rs3) => RsSc; <conc>(Rs1, Rs2) => RsDf; let fp = prop-assign <+ prop-declare <+ prop-let <+ prop-if <+ prop-while <+ transform(fp) <+ (before; all(fp); after) prop-assign = ... prop-declare = ... prop-let = ... prop-if = ... prop-while = ... in fp end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 41. Generic Strategy – Assignments prop-assign = |[ <id> := <fp> ]| ; (transform(fp) <+ before ; ?|[ x := e ]| ; undefine-dynamic-rules(|RsDf,x) ; after) prop-declare = |[ var <id> := <fp> ]| ; (transform(fp) <+ before; ?|[ var x := e ]| ; new-dynamic-rules(|RsSc,x,x);after) prop-let = ?|[ let d* in e* end ]| ; (transform(fp) <+ {|~RsSc : before; all(fp); after |}) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 42. Generic Strategy – Control Flow prop-if = |[ if <fp> then <id> else <id> ]| ; (transform(fp) <+ before ; (|[ if <id> then <fp> else <id> ]| /~Rs1~Rs2/ |[ if <id> then <id> else <fp> ]|) ; after) prop-while = ?|[ while e1 do e2 ]| ; (transform(fp) <+ before ; /~Rs1~Rs2/* |[ while <fp> do <fp> ]| ; after) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 43. Instantation: Constant Propagation prop-const = forward-prop(prop-const-transform, id, prop-const-after | ["PropConst"],[],[]) prop-const-transform(recur) = EvalFor <+ EvalIf; recur <+ |[ while <recur> do <id> ]|; EvalWhile prop-const-after = try(prop-const-assign <+ prop-const-declare <+ PropConst <+ EvalBinOp) prop-const-assign = ?|[ x := e ]|; where( <is-value> e ) ; rules( PropConst.x : |[ x ]| -> |[ e ]| depends on [(x,x)] ) prop-const-declare = ?|[var x ta := e]|; where(<prop-const-assign>|[x := e]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 44. Instantation: Copy Propagation copy-prop = forward-prop(no-transform,id,copy-prop-after |["CopyProp"],[],[]) copy-prop-after = try(copy-prop-assign <+ copy-prop-declare <+ repeat1(CopyProp)) copy-prop-declare = ? |[ var x ta := e ]| ; where(try(<copy-prop-assign> |[ x := e ]|)) copy-prop-assign = ? |[ x := y ]| ; where( <not(eq)> (x, y) ) ; where( get-var-dependencies => xs ) ; where( innermost-scope-CopyProp => z ) ; rules( CopyProp.z : |[ x ]| -> |[ y ]| depends on xs ) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 45. Instantation: Common-Subexpression Elimination cse = forward-prop(no-transform, id, cse-after|["CSE"],[],[]) cse-after = try(cse-assign <+ cse-declare <+ CSE) cse-declare = ?|[ var x := e ]|; where( <cse-assign> |[ x := e ]| ) cse-assign ; where( ; where( ; where( ; rules( = ?|[ x := e ]| <pure-and-not-trivial(|x)> |[ e ]| ) get-var-dependencies => xs ) innermost-scope-CSE => z ) CSE.z : |[ e ]| -> |[ x ]| depends on xs ) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 46. Instantation: A Super Optimizer super-opt = forward-prop( prop-const-transform , bvr-before , bvr-after ; copy-prop-after ; prop-const-after ; cse-after | ["PropConst", "CopyProp", "CSE"], [], ["RenameVar"]) Combination of constant propagation copy propagation common-subexpression elimination bound variable renaming http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with