269
|
1
|
|
2
|
|
3 import graph
|
|
4
|
|
5 class FlowGraphNode(graph.Node):
|
|
6 """ A node in the flow graph """
|
|
7 def __init__(self, g, ins):
|
|
8 super().__init__(g)
|
|
9 self.ins = ins
|
|
10 self.uses = set(ins.src)
|
|
11 self.defs = set(ins.dst)
|
|
12 self.live_in = set()
|
|
13 self.live_out = set()
|
|
14
|
|
15 def __repr__(self):
|
274
|
16 r = '{}'.format(self.ins)
|
|
17 if self.uses:
|
|
18 r += ' uses:' + ', '.join(str(u) for u in self.uses)
|
|
19 if self.defs:
|
|
20 r += ' defs:' + ', '.join(str(d) for d in self.defs)
|
|
21 return r
|
269
|
22
|
|
23
|
|
24 class FlowGraph(graph.Graph):
|
|
25 def __init__(self, instrs):
|
|
26 """ Create a flowgraph from a list of abstract instructions """
|
|
27 super().__init__()
|
|
28 self._map = {}
|
|
29 # Add nodes:
|
|
30 for ins in instrs:
|
|
31 n = self.newNode(ins)
|
|
32 self._map[ins] = 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
|
|
47
|
|
48 def newNode(self, ins):
|
|
49 """ Override new node to make flow graph node """
|
|
50 n = FlowGraphNode(self, ins)
|
|
51 self.nodes.append(n)
|
|
52 return n
|
|
53
|
|
54
|
|
55
|