157
|
1 # IR-Structures:
|
171
|
2 from .instruction import *
|
|
3 from .basicblock import BasicBlock
|
157
|
4
|
155
|
5 class Module:
|
170
|
6 """ Main container for a piece of code. """
|
155
|
7 def __init__(self, name):
|
|
8 self.name = name
|
172
|
9 self.funcs = []
|
158
|
10 def __repr__(self):
|
170
|
11 return 'IR-module [{0}]'.format(self.name)
|
155
|
12 def getInstructions(self):
|
171
|
13 ins = []
|
172
|
14 for bb in self.BasicBlocks:
|
171
|
15 ins += bb.Instructions
|
|
16 return ins
|
155
|
17 Instructions = property(getInstructions)
|
171
|
18 def getBBs(self):
|
172
|
19 bbs = []
|
|
20 for f in self.Functions:
|
|
21 bbs += f.BasicBlocks
|
|
22 return bbs
|
171
|
23 BasicBlocks = property(getBBs)
|
172
|
24 def addFunc(self, f):
|
|
25 self.funcs.append(f)
|
|
26 def getFuncs(self):
|
|
27 return self.funcs
|
|
28 Functions = property(getFuncs)
|
170
|
29 def dump(self):
|
|
30 print(self)
|
172
|
31 for fn in self.Functions:
|
|
32 print(fn)
|
|
33 for bb in fn.BasicBlocks:
|
|
34 print(' ', bb)
|
|
35 for ins in bb.Instructions:
|
|
36 print(' ', ins)
|
170
|
37 print('END')
|
171
|
38 def dumpgv(self, outf):
|
|
39 outf.write('digraph G \n{\n')
|
173
|
40 for f in self.Functions:
|
|
41 outf.write('{0} [label="{1}"]\n'.format(id(f), f))
|
|
42 for bb in f.BasicBlocks:
|
|
43 contents = str(bb) + '\n'
|
|
44 contents += '\n'.join([str(i) for i in bb.Instructions])
|
|
45 outf.write('{0} [label="{1}"];\n'.format(id(bb), contents))
|
|
46 for successor in bb.Successors:
|
|
47 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
|
|
48 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
|
171
|
49 outf.write('}\n')
|
155
|
50
|
171
|
51 # Analysis functions:
|
|
52 def check(self):
|
|
53 """ Perform sanity check on module """
|
|
54 for i in self.Instructions:
|
|
55 for t in i.defs:
|
|
56 assert type(t) is Value, "def must be Value, not {0}".format(type(t))
|
|
57 for t in i.uses:
|
|
58 assert type(t) is Value, "use must be Value, not {0}".format(type(t))
|
|
59
|