annotate python/flowgraph.py @ 282:c137f1fe3e65

Add codeship hook
author Windel Bouwman
date Fri, 15 Nov 2013 09:32:37 +0100
parents 9fca39eebe50
children
rev   line source
277
046017431c6a Started register allocator
Windel Bouwman
parents: 274
diff changeset
1 import graph
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
3
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
4 class FlowGraphNode(graph.DiNode):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
5 """ A node in the flow graph """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
6 def __init__(self, g, ins):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
7 super().__init__(g)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
8 self.ins = ins
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
9 self.uses = set(ins.src)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
10 self.defs = set(ins.dst)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11 self.live_in = set()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 self.live_out = set()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
14 def __repr__(self):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
15 r = '{}'.format(self.ins)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
16 if self.uses:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
17 r += ' uses:' + ', '.join(str(u) for u in self.uses)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
18 if self.defs:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
19 r += ' defs:' + ', '.join(str(d) for d in self.defs)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
20 return r
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
21
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
22
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
23 class FlowGraph(graph.DiGraph):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 def __init__(self, instrs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 """ Create a flowgraph from a list of abstract instructions """
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
26 super().__init__()
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 self._map = {}
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
28 # Add nodes:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
29 for ins in instrs:
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
30 n = FlowGraphNode(self, ins)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
31 self._map[ins] = n
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
32 self.addNode(n)
269
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