diff python/codegen/flowgraph.py @ 296:9417caea2eb3

Directorized some backend files
author Windel Bouwman
date Sun, 01 Dec 2013 13:36:58 +0100
parents python/flowgraph.py@9fca39eebe50
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/codegen/flowgraph.py	Sun Dec 01 13:36:58 2013 +0100
@@ -0,0 +1,46 @@
+from .graph import DiGraph, DiNode
+
+
+class FlowGraphNode(DiNode):
+    """ 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):
+        r = '{}'.format(self.ins)
+        if self.uses:
+            r += ' uses:' + ', '.join(str(u) for u in self.uses)
+        if self.defs:
+            r += ' defs:' + ', '.join(str(d) for d in self.defs)
+        return r
+
+
+class FlowGraph(DiGraph):
+    def __init__(self, instrs):
+        """ Create a flowgraph from a list of abstract instructions """
+        super().__init__()
+        self._map = {}
+        # Add nodes:
+        for ins in instrs:
+            n = FlowGraphNode(self, ins)
+            self._map[ins] = n
+            self.addNode(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