diff python/flowgraph.py @ 269:5f8c04a8d26b

Towards better modularity
author Windel Bouwman
date Sun, 18 Aug 2013 17:43:18 +0200
parents
children ea93e0a7a31e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/flowgraph.py	Sun Aug 18 17:43:18 2013 +0200
@@ -0,0 +1,50 @@
+
+
+import graph
+
+class FlowGraphNode(graph.Node):
+    """ A node in the flow graph """
+    def __init__(self, g, ins):
+        super().__init__(g)
+        self.ins = ins
+        self.uses = set(ins.src)
+        self.defs = set(ins.dst)
+        self.live_in = set()
+        self.live_out = set()
+
+    def __repr__(self):
+        return '{}, use={}, def={}'.format(self.ins, self.uses, self.defs)
+
+
+class FlowGraph(graph.Graph):
+    def __init__(self, instrs):
+        """ Create a flowgraph from a list of abstract instructions """
+        super().__init__()
+        self._map = {}
+        # Add nodes:
+        for ins in instrs:
+            n = self.newNode(ins)
+            self._map[ins] = n
+
+        # Make edges:
+        prev = None
+        for ins in instrs:
+            n = self._map[ins]
+            if prev:
+                self.addEdge(prev, n)
+            if ins.jumps:
+                prev = None
+                for j in ins.jumps:
+                    to_n = self._map[j]
+                    self.addEdge(n, to_n)
+            else:
+                prev = n
+
+    def newNode(self, ins):
+        """ Override new node to make flow graph node """
+        n = FlowGraphNode(self, ins)
+        self.nodes.append(n)
+        return n
+
+
+