changeset 338:4d4028b2d6c4

flow.py: added a parameter for going backwards
author Sigurd Meldgaard <stm@daimi.au.dk>
date Wed, 07 Jul 2010 11:42:23 +0200
parents 4ad646307bd7
children a7d868a5577a
files pysmcl/flow.py
diffstat 1 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/flow.py	Wed Jul 07 11:38:40 2010 +0200
+++ b/pysmcl/flow.py	Wed Jul 07 11:42:23 2010 +0200
@@ -6,13 +6,13 @@
 from pysmcl.pretty_print import PrettyPrinter
 
 
-def analyze(function, join, combine, key, init, distribute=None):
+def analyze(function, join, combine, key, init, forward=True, distribute=None):
     """
-    Doing a simple intra procedural forward analysis using the
-    iterative worklist algorithm. Parametrized by the functions join
-    and combine, and with the value initial.  The lattice-points are
-    stored in each statement-node in_values and out_values, under the
-    *key*.
+    Doing a simple intra procedural forward (or backwards) analysis
+    using the iterative worklist algorithm. Parametrized by the
+    functions join and combine, and with the value initial.  The
+    lattice-points are stored in each statement-node in_values and
+    out_values, under the *key*.
 
     function (FunctionDef) The function to analyze
 
@@ -31,8 +31,12 @@
     """
 
     def default_distribute(x):
+        if forward:
             for child in x.children:
                 child.in_values[key] = x.out_values[key]
+        else: #Backwards analysis
+            for parent in x.parents:
+                parent.in_values[key] = x.out_values[key]
 
     if distribute is None:
         distribute = default_distribute
@@ -55,11 +59,18 @@
     while len(worklist) != 0:
         x = worklist.pop(0)
         oldout = x.out_values[key]
-        x.out_values[key] = combine(x, join(x.parents))
-        if oldout != x.out_values[key]:
-            distribute(x)
-            for child in x.children:
-                worklist.append(child)
+        if forward:
+            x.out_values[key] = combine(x, join(x.parents))
+            if oldout != x.out_values[key]:
+                distribute(x)
+                for child in x.children:
+                    worklist.append(child)
+        else: # Backwards analysis
+            x.out_values[key] = combine(x, join(x.children))
+            if oldout != x.out_values[key]:
+                distribute(x)
+                for parent in x.parents:
+                    worklist.append(parent)
 
 
 class Flow():