Mercurial > lcfOS
annotate python/ppci/codegen/graph.py @ 305:0615b5308710
Updated docs
author | Windel Bouwman |
---|---|
date | Fri, 06 Dec 2013 13:50:38 +0100 |
parents | 6753763d3bec |
children | 2c9768114877 |
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() | |
269 | 10 |
277 | 11 def addNode(self, n): |
12 self.nodes.add(n) | |
13 | |
14 def delNode(self, n): | |
15 self.nodes.remove(n) | |
16 | |
269 | 17 def addEdge(self, n, m): |
18 """ Add an edge from n to m """ | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
19 self.edges.add((n, m)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
20 self.edges.add((m, n)) |
277 | 21 |
22 def hasEdge(self, n, m): | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
23 return (n, m) in self.edges |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
24 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
25 def delEdge(self, n, m): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
26 self.edges.remove((n, m)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
27 self.edges.remove((m, n)) |
269 | 28 |
277 | 29 def adjecent(self, n): |
30 """ Return all nodes with edges to n """ | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
31 # TODO: this can be optimized for speed: |
277 | 32 return set(m for m in self.nodes if self.hasEdge(n, m)) |
296 | 33 |
269 | 34 def to_dot(self, f): |
35 """ Generate graphviz dot representation """ | |
296 | 36 for n in self.nodes: |
37 print('{} [label="{}" shape=box3d];'.format(id(n), n), file=f) | |
269 | 38 for n, m in self.edges: |
39 print('{} -> {};'.format(id(n), id(m)), file=f) | |
40 | |
41 | |
42 class Node: | |
43 """ | |
44 Node in a graph. | |
45 """ | |
46 def __init__(self, g): | |
47 self.g = g | |
296 | 48 self.addDegree = 0 # Hack to increase degree |
277 | 49 |
50 @property | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
51 def Adjecent(self): |
277 | 52 return self.g.adjecent(self) |
53 | |
54 @property | |
55 def Degree(self): | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
56 return len(self.Adjecent) + self.addDegree |
270 | 57 |
58 | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
59 class DiGraph(Graph): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
60 """ |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
61 Directed graph. |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
62 """ |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
63 def addEdge(self, n, m): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
64 """ Add an edge from n to m """ |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
65 self.edges.add((n, m)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
66 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
67 def hasEdge(self, n, m): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
68 return (n, m) in self.edges |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
69 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
70 def successors(self, n): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
71 return set(m for m in self.nodes if self.hasEdge(n, m)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
72 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
73 def predecessors(self, n): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
74 return set(m for m in self.nodes if self.hasEdge(m, n)) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
75 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
76 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
77 class DiNode(Node): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
78 @property |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
79 def Succ(self): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
80 return self.g.successors(self) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
81 |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
82 @property |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
83 def Pred(self): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
84 return self.g.predecessors(self) |