diff python/interferencegraph.py @ 279:2ccd57b1d78c

Fix register allocator to do burn2 OK
author Windel Bouwman
date Sat, 12 Oct 2013 09:56:23 +0200
parents 9fca39eebe50
children 02385f62f250
line wrap: on
line diff
--- a/python/interferencegraph.py	Sun Sep 29 14:08:15 2013 +0200
+++ b/python/interferencegraph.py	Sat Oct 12 09:56:23 2013 +0200
@@ -6,9 +6,10 @@
         super().__init__(g)
         self.temps = [varname]
         self.moves = set()
+        self.color = None
 
     def __repr__(self):
-        return 'IGN<{}>'.format(self.temps)
+        return '{}({})'.format(self.temps, self.color)
 
 
 class InterferenceGraph(graph.Graph):
@@ -52,12 +53,24 @@
                     n2 = self.getNode(tmp2)
                     self.addEdge(n1, n2)
 
+    def to_dot(self, f):
+        """ Generate graphviz dot representation """
+        for node in self.nodes:
+            print('{} [label="{}" shape=box3d];'.format(id(node), node), file=f)
+        for n, m in self.edges:
+            print('{} -> {};'.format(id(n), id(m)), file=f)
+
+    def to_txt(self):
+        for node in self.nodes:
+            print('{} interferes: {}'.format(node, node.Adjecent))
+
     def getNode(self, tmp):
         # Linear search
         # TODO: can be improved for speed!
         for n in self.nodes:
             if tmp in n.temps:
                 return n
+        print('new one!')
         n = InterferenceGraphNode(self, tmp)
         self.addNode(n)
         return n
@@ -66,7 +79,15 @@
         """ Combine n and m into n """
         n.temps.extend(m.temps)
         n.moves.update(m.moves)
-        for t in m.Adjecent:
-            self.addEdge(n, t)
+        # Reroute all edges:
+        e1 = [e for e in self.edges if e[0] is m]
+        e2 = [e for e in self.edges if e[1] is m]
+        for e in e1:
+            self.edges.remove(e)
+            self.addEdge(n, e[1])
+        for e in e2:
+            self.edges.remove(e)
+            self.addEdge(n, e[0])
+        # Remove node m:
         self.delNode(m)