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:
|
174
|
41 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
|
173
|
42 for bb in f.BasicBlocks:
|
|
43 contents = str(bb) + '\n'
|
|
44 contents += '\n'.join([str(i) for i in bb.Instructions])
|
174
|
45 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
|
173
|
46 for successor in bb.Successors:
|
|
47 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
|
177
|
48 # Draw dags if any:
|
|
49 if hasattr(bb, 'dag'):
|
|
50 outf.write('{0} -> {1}\n'.format(id(bb), id(bb.dag)))
|
|
51 bb.dag.dumpgv(outf)
|
|
52
|
173
|
53 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
|
171
|
54 outf.write('}\n')
|
155
|
55
|
171
|
56 # Analysis functions:
|
|
57 def check(self):
|
|
58 """ Perform sanity check on module """
|
|
59 for i in self.Instructions:
|
|
60 for t in i.defs:
|
|
61 assert type(t) is Value, "def must be Value, not {0}".format(type(t))
|
|
62 for t in i.uses:
|
174
|
63 assert type(t) is Use, "use must be Value, not {0}".format(type(t))
|
171
|
64
|