comparison python/codegen/flowgraph.py @ 296:9417caea2eb3

Directorized some backend files
author Windel Bouwman
date Sun, 01 Dec 2013 13:36:58 +0100
parents python/flowgraph.py@9fca39eebe50
children
comparison
equal deleted inserted replaced
295:917eab04b8b7 296:9417caea2eb3
1 from .graph import DiGraph, DiNode
2
3
4 class FlowGraphNode(DiNode):
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):
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
21
22
23 class FlowGraph(DiGraph):
24 def __init__(self, instrs):
25 """ Create a flowgraph from a list of abstract instructions """
26 super().__init__()
27 self._map = {}
28 # Add nodes:
29 for ins in instrs:
30 n = FlowGraphNode(self, ins)
31 self._map[ins] = n
32 self.addNode(n)
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