annotate python/flowgraph.py @ 269:5f8c04a8d26b

Towards better modularity
author Windel Bouwman
date Sun, 18 Aug 2013 17:43:18 +0200
parents
children ea93e0a7a31e
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):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
16 return '{}, use={}, def={}'.format(self.ins, self.uses, self.defs)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
17
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
18
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
19 class FlowGraph(graph.Graph):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
20 def __init__(self, instrs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
21 """ Create a flowgraph from a list of abstract instructions """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
22 super().__init__()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23 self._map = {}
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 # Add nodes:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 for ins in instrs:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
26 n = self.newNode(ins)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 self._map[ins] = n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
28
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
29 # Make edges:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
30 prev = None
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
31 for ins in instrs:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
32 n = self._map[ins]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
33 if prev:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
34 self.addEdge(prev, n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35 if ins.jumps:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
36 prev = None
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 for j in ins.jumps:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 to_n = self._map[j]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
39 self.addEdge(n, to_n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 else:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 prev = n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43 def newNode(self, ins):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 """ Override new node to make flow graph node """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45 n = FlowGraphNode(self, ins)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46 self.nodes.append(n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 return n
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50