157
|
1 # IR-Structures:
|
171
|
2 from .instruction import *
|
271
|
3 from .function import Block
|
157
|
4
|
155
|
5 class Module:
|
253
|
6 """ Main container for a piece of code. """
|
|
7 def __init__(self, name):
|
155
|
8 self.name = name
|
172
|
9 self.funcs = []
|
205
|
10 self.variables = []
|
|
11
|
253
|
12 def __repr__(self):
|
|
13 return 'IR-module [{0}]'.format(self.name)
|
205
|
14
|
253
|
15 def addFunc(self, f):
|
258
|
16 self.funcs.append(f)
|
271
|
17
|
253
|
18 addFunction = addFunc
|
205
|
19
|
253
|
20 def addVariable(self, v):
|
205
|
21 self.variables.append(v)
|
|
22
|
253
|
23 def getVariables(self):
|
205
|
24 return self.variables
|
271
|
25
|
253
|
26 Variables = property(getVariables)
|
205
|
27
|
253
|
28 def getFunctions(self):
|
|
29 return self.funcs
|
271
|
30
|
253
|
31 Functions = property(getFunctions)
|
205
|
32
|
253
|
33 def findFunction(self, name):
|
230
|
34 for f in self.funcs:
|
|
35 if f.name == name:
|
|
36 return f
|
|
37 raise KeyError(name)
|
271
|
38
|
253
|
39 getFunction = findFunction
|
230
|
40
|
253
|
41 def dump(self):
|
170
|
42 print(self)
|
205
|
43 for v in self.Variables:
|
|
44 print(' ', v)
|
172
|
45 for fn in self.Functions:
|
|
46 print(fn)
|
|
47 for bb in fn.BasicBlocks:
|
|
48 print(' ', bb)
|
|
49 for ins in bb.Instructions:
|
|
50 print(' ', ins)
|
205
|
51
|
253
|
52 def dumpgv(self, outf):
|
171
|
53 outf.write('digraph G \n{\n')
|
173
|
54 for f in self.Functions:
|
174
|
55 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
|
173
|
56 for bb in f.BasicBlocks:
|
|
57 contents = str(bb) + '\n'
|
|
58 contents += '\n'.join([str(i) for i in bb.Instructions])
|
174
|
59 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
|
173
|
60 for successor in bb.Successors:
|
|
61 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
|
177
|
62
|
173
|
63 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
|
171
|
64 outf.write('}\n')
|
155
|
65
|
253
|
66 # Analysis functions:
|
|
67 def check(self):
|
239
|
68 """ Perform sanity check on module """
|
|
69 for f in self.Functions:
|
|
70 f.check()
|
|
71
|
171
|
72
|