comparison python/ir/module.py @ 172:5a7d37d615ee

Added function to IR
author Windel Bouwman
date Thu, 04 Apr 2013 17:58:37 +0200
parents 3eb9b9e2958d
children c1d2b6b9f9a7
comparison
equal deleted inserted replaced
171:3eb9b9e2958d 172:5a7d37d615ee
4 4
5 class Module: 5 class Module:
6 """ Main container for a piece of code. """ 6 """ Main container for a piece of code. """
7 def __init__(self, name): 7 def __init__(self, name):
8 self.name = name 8 self.name = name
9 self.bbs = [] 9 self.funcs = []
10 def __repr__(self): 10 def __repr__(self):
11 return 'IR-module [{0}]'.format(self.name) 11 return 'IR-module [{0}]'.format(self.name)
12 def getInstructions(self): 12 def getInstructions(self):
13 ins = [] 13 ins = []
14 for bb in self.bbs: 14 for bb in self.BasicBlocks:
15 ins += bb.Instructions 15 ins += bb.Instructions
16 return ins 16 return ins
17 Instructions = property(getInstructions) 17 Instructions = property(getInstructions)
18 def addBB(self, bb):
19 self.bbs.append(bb)
20 def getBBs(self): 18 def getBBs(self):
21 return self.bbs 19 bbs = []
20 for f in self.Functions:
21 bbs += f.BasicBlocks
22 return bbs
22 BasicBlocks = property(getBBs) 23 BasicBlocks = property(getBBs)
24 def addFunc(self, f):
25 self.funcs.append(f)
26 def getFuncs(self):
27 return self.funcs
28 Functions = property(getFuncs)
23 def dump(self): 29 def dump(self):
24 print(self) 30 print(self)
25 for i in self.Instructions: 31 for fn in self.Functions:
26 print(i, 'live vars:', list(i.live_in), 'uses', list(i.uses), 'defs', list(i.defs)) 32 print(fn)
33 for bb in fn.BasicBlocks:
34 print(' ', bb)
35 for ins in bb.Instructions:
36 print(' ', ins)
27 print('END') 37 print('END')
28 def dumpgv(self, outf): 38 def dumpgv(self, outf):
29 outf.write('digraph G \n{\n') 39 outf.write('digraph G \n{\n')
30 for i in self.Instructions: 40 for i in self.Instructions:
31 outf.write('{0} [label="{1}"];\n'.format(id(i), i)) 41 outf.write('{0} [label="{1}"];\n'.format(id(i), i))
44 def analyze(self): 54 def analyze(self):
45 # Determine pred and succ: 55 # Determine pred and succ:
46 def link(a, b): 56 def link(a, b):
47 a.succ.add(b) 57 a.succ.add(b)
48 b.pred.add(a) 58 b.pred.add(a)
49 for bb in self.bbs: 59 for bb in self.BasicBlocks:
50 if not bb.Empty: 60 if not bb.Empty:
51 if len(bb.Instructions) > 1: 61 if len(bb.Instructions) > 1:
52 for i1, i2 in zip(bb.Instructions[:-1], bb.Instructions[1:]): 62 for i1, i2 in zip(bb.Instructions[:-1], bb.Instructions[1:]):
53 link(i1, i2) 63 link(i1, i2)
54 else: 64 else: