annotate python/flowgraph.py @ 274:ea93e0a7a31e

Move docs
author Windel Bouwman
date Wed, 04 Sep 2013 17:35:06 +0200
parents 5f8c04a8d26b
children 046017431c6a
rev   line source
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
1
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
3 import graph
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
4
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
5 class FlowGraphNode(graph.Node):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
6 """ A node in the flow graph """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
7 def __init__(self, g, ins):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
8 super().__init__(g)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
9 self.ins = ins
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
10 self.uses = set(ins.src)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11 self.defs = set(ins.dst)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 self.live_in = set()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13 self.live_out = set()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
14
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
15 def __repr__(self):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
16 r = '{}'.format(self.ins)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
17 if self.uses:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
18 r += ' uses:' + ', '.join(str(u) for u in self.uses)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
19 if self.defs:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
20 r += ' defs:' + ', '.join(str(d) for d in self.defs)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
21 return r
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
22
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 class FlowGraph(graph.Graph):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 def __init__(self, instrs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
26 """ Create a flowgraph from a list of abstract instructions """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 super().__init__()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
28 self._map = {}
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
29 # Add nodes:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
30 for ins in instrs:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
31 n = self.newNode(ins)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
32 self._map[ins] = n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
33
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
34 # Make edges:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35 prev = None
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
36 for ins in instrs:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 n = self._map[ins]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 if prev:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
39 self.addEdge(prev, n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 if ins.jumps:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 prev = None
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42 for j in ins.jumps:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43 to_n = self._map[j]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 self.addEdge(n, to_n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45 else:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46 prev = n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48 def newNode(self, ins):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 """ Override new node to make flow graph node """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50 n = FlowGraphNode(self, ins)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 self.nodes.append(n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 return n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
54
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
55