[pypy-svn] rev 1507 - pypy/trunk/src/pypy/translator

jriehl at codespeak.net jriehl at codespeak.net
Wed Oct 1 18:58:34 CEST 2003


Author: jriehl
Date: Wed Oct  1 18:58:33 2003
New Revision: 1507

Modified:
   pypy/trunk/src/pypy/translator/flowmodel.py
Log:
Adding additional graph utility methods to the flow model.

Modified: pypy/trunk/src/pypy/translator/flowmodel.py
==============================================================================
--- pypy/trunk/src/pypy/translator/flowmodel.py	(original)
+++ pypy/trunk/src/pypy/translator/flowmodel.py	Wed Oct  1 18:58:33 2003
@@ -4,13 +4,33 @@
 of the classes in this module the translator parts will only look 
 into the attributes defined here. 
 """
-class BasicBlock:
+class FlowNode:
+    def getedges(self):
+        raise NotImplementedError, "Abstract base class"
+
+    def flatten(self):
+        nodedict = self.visit(lambda x: None)
+        return nodedict.keys()
+
+    def visit(self, fn, _visited = None):
+        if _visited is None:
+            _visited = {}
+        _visited[self] = fn(self)
+        for targetnode in self.getedges():
+            if not _visited.has_key(targetnode):
+                targetnode.visit(fn, _visited)
+        return _visited
+
+class BasicBlock(FlowNode):
     def __init__(self, input_args, locals, operations, branch=None):
         self.input_args = input_args
         self.locals = locals
         self.operations = operations
         self.branch = branch
 
+    def getedges(self):
+        return [self.branch]
+
     def closeblock(self, branch):
         self.operations = tuple(self.operations)  # should no longer change
         self.branch = branch
@@ -54,27 +74,36 @@
     def __repr__(self):
         return "%s(%s) -> %s" % (self.opname, ", ".join(map(str, self.args)), self.result)
 
-class Branch:
+class Branch(FlowNode):
     def __init__(self, args=None, target=None):
         self.set(args, target)
 
+    def getedges(self):
+        return [self.target]
+
     def set(self, args, target):
         self.args = args     # list of variables
         self.target = target # basic block instance
 
-class ConditionalBranch:
+class ConditionalBranch(FlowNode):
     def __init__(self, condition=None, ifbranch=None, elsebranch=None):
         self.set(condition, ifbranch, elsebranch)
 
+    def getedges(self):
+        return [self.ifbranch, self.elsebranch]
+
     def set(self, condition, ifbranch, elsebranch):
         self.condition = condition
         self.ifbranch = ifbranch
         self.elsebranch = elsebranch
 
-class EndBranch:
+class EndBranch(FlowNode):
     def __init__(self, returnvalue):
         self.returnvalue = returnvalue
 
+    def getedges(self):
+        return []
+
 class FunctionGraph:
     def __init__(self, startblock, functionname):
         self.startblock = startblock
@@ -82,3 +111,6 @@
 
     def get_args(self):
         return self.startblock.input_args
+
+    def flatten(self):
+        return self.startblock.flatten()


More information about the Pypy-commit mailing list