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):
|
|
16 return '{}, use={}, def={}'.format(self.ins, self.uses, self.defs)
|
|
17
|
|
18
|
|
19 class FlowGraph(graph.Graph):
|
|
20 def __init__(self, instrs):
|
|
21 """ Create a flowgraph from a list of abstract instructions """
|
|
22 super().__init__()
|
|
23 self._map = {}
|
|
24 # Add nodes:
|
|
25 for ins in instrs:
|
|
26 n = self.newNode(ins)
|
|
27 self._map[ins] = n
|
|
28
|
|
29 # Make edges:
|
|
30 prev = None
|
|
31 for ins in instrs:
|
|
32 n = self._map[ins]
|
|
33 if prev:
|
|
34 self.addEdge(prev, n)
|
|
35 if ins.jumps:
|
|
36 prev = None
|
|
37 for j in ins.jumps:
|
|
38 to_n = self._map[j]
|
|
39 self.addEdge(n, to_n)
|
|
40 else:
|
|
41 prev = n
|
|
42
|
|
43 def newNode(self, ins):
|
|
44 """ Override new node to make flow graph node """
|
|
45 n = FlowGraphNode(self, ins)
|
|
46 self.nodes.append(n)
|
|
47 return n
|
|
48
|
|
49
|
|
50
|