[pypy-svn] rev 1470 - pypy/trunk/src/pypy/translator/test

hpk at codespeak.net hpk at codespeak.net
Tue Sep 30 17:20:03 CEST 2003


Author: hpk
Date: Tue Sep 30 17:20:02 2003
New Revision: 1470

Added:
   pypy/trunk/src/pypy/translator/test/make_dot.py
Modified:
   pypy/trunk/src/pypy/translator/test/buildpyxmodule.py
   pypy/trunk/src/pypy/translator/test/test_sourcegen.py
Log:
beginnings of a tool to visualize the control-flow



Modified: pypy/trunk/src/pypy/translator/test/buildpyxmodule.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/buildpyxmodule.py	(original)
+++ pypy/trunk/src/pypy/translator/test/buildpyxmodule.py	Tue Sep 30 17:20:02 2003
@@ -53,7 +53,8 @@
         sys.path.pop(0)
     finally:
         os.chdir(str(lastdir))
-        dirpath.rmtree()
+        if not debug:
+            dirpath.rmtree()
     return testmodule
 
 def make_c_from_pyxfile(pyxfile):

Added: pypy/trunk/src/pypy/translator/test/make_dot.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/translator/test/make_dot.py	Tue Sep 30 17:20:02 2003
@@ -0,0 +1,120 @@
+#<MROgraph.py>
+ 
+"""
+"""
+
+import autopath
+from pypy.translator.controlflow import *
+import os
+
+class Source:
+    def __init__(self):
+        self.lines = []
+
+    def getsource(self):
+        content = "\n".join(self.lines)
+
+        return """digraph test { 
+            node [fontname=Times];
+            edge [fontname=Times];
+        %(content)s
+        }
+        """ % locals()
+
+    def putnode(self, name, node):
+        self.lines.append('%(name)s [shape=box, label=%(name)s];' % locals())
+
+    def putedge(self, name, othername):
+        self.lines.append('%(name)s -> %(othername)s;' % locals())
+
+#class Node:
+#    def __init__(self, **kwargs):
+#        self.__dict__.update(kwargs)
+#
+#class NodeFunctionGraph(Node):
+#    def dotsource(self):
+
+class DotGen:
+    def __init__(self):
+        self.graph = {} # node1 : [node1,node2,node3] }
+        self.names = {} # obj : string 
+        self.blocknum = 0
+        self.branchnum = 0
+
+    def get_source(self, obj):
+        self.dispatch(obj)
+        source = Source()
+        for obj,edges in self.graph.items():
+            objname = self.names[obj]
+            source.putnode(objname, obj)
+            for edge in edges:
+                source.putedge(objname, edge)
+        return source.getsource()
+
+    def dispatch(self, obj):
+        try:
+            return self.names[obj]
+        except KeyError:
+            pass
+        typename = obj.__class__.__name__
+        method = getattr(self, "gen_%s" % typename, None)
+        if method is not None:
+            return method(obj)
+
+        raise ValueError, "unknown type for %r" % typename
+
+    def append_edge(self, obj, otherobj):
+        points_to = self.graph.setdefault(obj, [])
+        points_to.append(otherobj)
+
+    def gen_FunctionGraph(self, obj):
+        name = obj.functionname
+        self.names[obj] = name
+        self.append_edge(obj, self.dispatch(obj.startblock))
+        return name
+
+    def gen_BasicBlock(self, obj):
+        name = "block%s" % self.blocknum
+        self.blocknum += 1
+        self.names[obj] = name
+        self.append_edge(obj, self.dispatch(obj.branch))
+        return name
+
+    def gen_Branch(self, obj):
+        name = "branch%s" % self.branchnum
+        self.branchnum += 1
+        self.names[obj] = name
+        self.append_edge(obj, self.dispatch(obj.target))
+        return name
+
+    def gen_ConditionalBranch(self, obj):
+        name = "Condbranch%s" % self.branchnum
+        self.branchnum += 1
+        self.names[obj] = name
+        self.append_edge(obj, self.dispatch(obj.ifbranch))
+        self.append_edge(obj, self.dispatch(obj.elsebranch))
+        return name
+
+    def gen_EndBranch(self, obj):
+        name = "endbranch%s" % self.branchnum
+        self.branchnum += 1
+        self.names[obj] = name
+        self.graph[obj] = [] 
+        return name
+        #self.append_edge(self.dispatch(obj.target))
+
+def make_ps(fun):
+    dotgen = DotGen()
+   
+    from vpath.local import Path
+    from vpath.adapter.process import exec_cmd
+    dest = Path('/tmp/testgraph.dot')
+    dest.write(dotgen.get_source(fun))
+    psdest = dest.newsuffix('.ps')
+    out = exec_cmd('dot -Tps %s' % str(dest))
+    psdest.write(out)
+    print "wrote", psdest
+
+#if __name__ == '__main__':
+    
+        

Modified: pypy/trunk/src/pypy/translator/test/test_sourcegen.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_sourcegen.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_sourcegen.py	Tue Sep 30 17:20:02 2003
@@ -6,6 +6,7 @@
 from pypy.translator.controlflow import *
 
 from pypy.translator.test.buildpyxmodule import make_module_from_pyxstring
+#from pypy.translator.test.make_dot import make_ps
 
 class TestCase(test.IntTestCase):
     def test_simple_func(self):
@@ -83,6 +84,8 @@
                                 [], headerbranch)
 
         fun = FunctionGraph(startblock, "f")
+        #make_ps(fun)
+        
         result = GenPyrex(fun).emitcode()
         mod = make_module_from_pyxstring(result)
         self.assertEquals(mod.f(42), 0)


More information about the Pypy-commit mailing list