annotate python/ppci/codegen/flowgraph.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents 6753763d3bec
children b00219172a42
rev   line source
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 278
diff changeset
1 from .graph import DiGraph, DiNode
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
3
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 278
diff changeset
4 class FlowGraphNode(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
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 278
diff changeset
23 class FlowGraph(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