277
|
1 import graph
|
269
|
2
|
|
3
|
|
4 class FlowGraphNode(graph.Node):
|
|
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
|
|
23 class FlowGraph(graph.Graph):
|
|
24 def __init__(self, instrs):
|
|
25 """ Create a flowgraph from a list of abstract instructions """
|
277
|
26 super().__init__(True)
|
269
|
27 self._map = {}
|
|
28 # Add nodes:
|
|
29 for ins in instrs:
|
|
30 n = self.newNode(ins)
|
|
31
|
|
32 # Make edges:
|
|
33 prev = None
|
|
34 for ins in instrs:
|
|
35 n = self._map[ins]
|
|
36 if prev:
|
|
37 self.addEdge(prev, n)
|
|
38 if ins.jumps:
|
|
39 prev = None
|
|
40 for j in ins.jumps:
|
|
41 to_n = self._map[j]
|
|
42 self.addEdge(n, to_n)
|
|
43 else:
|
|
44 prev = n
|
|
45
|
|
46 def newNode(self, ins):
|
|
47 """ Override new node to make flow graph node """
|
|
48 n = FlowGraphNode(self, ins)
|
277
|
49 self._map[ins] = n
|
|
50 self.addNode(n)
|
269
|
51 return n
|
|
52
|
|
53
|