# HG changeset patch # User Sigurd Meldgaard # Date 1278495743 -7200 # Node ID 4d4028b2d6c47307b5fa72be90e216070b8ad04d # Parent 4ad646307bd79c50a4d3bff19a29165cce8a4965 flow.py: added a parameter for going backwards diff -r 4ad646307bd7 -r 4d4028b2d6c4 pysmcl/flow.py --- 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():