comparison python/ppci/codegen/flowgraph.py @ 301:6753763d3bec

merge codegen into ppci package
author Windel Bouwman
date Thu, 05 Dec 2013 17:02:38 +0100
parents python/codegen/flowgraph.py@9417caea2eb3
children b00219172a42
comparison
equal deleted inserted replaced
300:158068af716c 301:6753763d3bec
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