Mercurial > lcfOS
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 | 1 import graph |
269 | 2 |
3 | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
4 class FlowGraphNode(graph.DiNode): |
269 | 5 """ A node in the flow graph """ |
6 def __init__(self, g, ins): | |
7 super().__init__(g) | |
8 self.ins = ins | |
9 self.uses = set(ins.src) | |
10 self.defs = set(ins.dst) | |
11 self.live_in = set() | |
12 self.live_out = set() | |
13 | |
14 def __repr__(self): | |
274 | 15 r = '{}'.format(self.ins) |
16 if self.uses: | |
17 r += ' uses:' + ', '.join(str(u) for u in self.uses) | |
18 if self.defs: | |
19 r += ' defs:' + ', '.join(str(d) for d in self.defs) | |
20 return r | |
269 | 21 |
22 | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
23 class FlowGraph(graph.DiGraph): |
269 | 24 def __init__(self, instrs): |
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 | 27 self._map = {} |
28 # Add nodes: | |
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 | 33 |
34 # Make edges: | |
35 prev = None | |
36 for ins in instrs: | |
37 n = self._map[ins] | |
38 if prev: | |
39 self.addEdge(prev, n) | |
40 if ins.jumps: | |
41 prev = None | |
42 for j in ins.jumps: | |
43 to_n = self._map[j] | |
44 self.addEdge(n, to_n) | |
45 else: | |
46 prev = n | |
47 | |
48 |