Mercurial > lcfOS
annotate python/ppci/codegen/graph.py @ 323:e9fe6988497c
Used burg for generating expressions
author | Windel Bouwman |
---|---|
date | Thu, 30 Jan 2014 19:03:24 +0100 |
parents | 2c9768114877 |
children | b00219172a42 |
rev | line source |
---|---|
269 | 1 |
2 class Graph: | |
3 """ | |
4 Generic graph base class. | |
5 Can dump to graphiz dot format for example! | |
6 """ | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
7 def __init__(self): |
277 | 8 self.nodes = set() |
9 self.edges = set() | |
323 | 10 self.adj_map = {} |
269 | 11 |
277 | 12 def addNode(self, n): |
13 self.nodes.add(n) | |
323 | 14 if n not in self.adj_map: |
15 self.adj_map[n] = set() | |
277 | 16 |
17 def delNode(self, n): | |
18 self.nodes.remove(n) | |
19 | |
269 | 20 def addEdge(self, n, m): |
21 """ Add an edge from n to m """ | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
22 self.edges.add((n, m)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
23 self.edges.add((m, n)) |
323 | 24 self.adj_map[n].add(m) |
25 self.adj_map[m].add(n) | |
277 | 26 |
27 def hasEdge(self, n, m): | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
28 return (n, m) in self.edges |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
29 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
30 def delEdge(self, n, m): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
31 self.edges.remove((n, m)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
32 self.edges.remove((m, n)) |
269 | 33 |
277 | 34 def adjecent(self, n): |
35 """ Return all nodes with edges to n """ | |
323 | 36 return self.adj_map[n] & self.nodes |
296 | 37 |
269 | 38 def to_dot(self, f): |
39 """ Generate graphviz dot representation """ | |
296 | 40 for n in self.nodes: |
312 | 41 print(' {} [label="{}" shape=box3d];'.format(id(n), n), file=f) |
269 | 42 for n, m in self.edges: |
312 | 43 print(' {} -> {};'.format(id(n), id(m)), file=f) |
269 | 44 |
45 | |
46 class Node: | |
47 """ | |
48 Node in a graph. | |
49 """ | |
50 def __init__(self, g): | |
51 self.g = g | |
296 | 52 self.addDegree = 0 # Hack to increase degree |
277 | 53 |
54 @property | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
55 def Adjecent(self): |
277 | 56 return self.g.adjecent(self) |
57 | |
58 @property | |
59 def Degree(self): | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
60 return len(self.Adjecent) + self.addDegree |
270 | 61 |
62 | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
63 class DiGraph(Graph): |
323 | 64 """ Directed graph. """ |
65 def __init__(self): | |
66 super().__init__() | |
67 self.suc_map = {} | |
68 self.pre_map = {} | |
69 | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
70 def addEdge(self, n, m): |
323 | 71 """ Add a directed edge from n to m """ |
72 assert n in self.nodes | |
73 assert m in self.nodes | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
74 self.edges.add((n, m)) |
323 | 75 self.suc_map[n].add(m) |
76 self.pre_map[m].add(n) | |
77 self.adj_map[n].add(m) | |
78 self.adj_map[m].add(n) | |
79 | |
80 def addNode(self, n): | |
81 super().addNode(n) | |
82 if n not in self.suc_map: | |
83 self.suc_map[n] = set() | |
84 if n not in self.pre_map: | |
85 self.pre_map[n] = set() | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
86 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
87 def hasEdge(self, n, m): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
88 return (n, m) in self.edges |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
89 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
90 def successors(self, n): |
323 | 91 return self.suc_map[n] & self.nodes |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
92 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
93 def predecessors(self, n): |
323 | 94 return self.pre_map[n] & self.nodes |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
95 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
96 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
97 class DiNode(Node): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
98 @property |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
99 def Succ(self): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
100 return self.g.successors(self) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
101 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
102 @property |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
103 def Pred(self): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
104 return self.g.predecessors(self) |