Mercurial > lcfOS
annotate python/ppci/codegen/flowgraph.py @ 313:04cf4d26a3bc
Added constant function
author | Windel Bouwman |
---|---|
date | Wed, 18 Dec 2013 18:02:26 +0100 |
parents | 6753763d3bec |
children | b00219172a42 |
rev | line source |
---|---|
296 | 1 from .graph import DiGraph, DiNode |
269 | 2 |
3 | |
296 | 4 class FlowGraphNode(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 | |
296 | 23 class FlowGraph(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 |