157
|
1 # IR-Structures:
|
171
|
2 from .instruction import *
|
268
|
3 from .basicblock 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 getInstructions(self):
|
171
|
16 ins = []
|
172
|
17 for bb in self.BasicBlocks:
|
171
|
18 ins += bb.Instructions
|
|
19 return ins
|
253
|
20 Instructions = property(getInstructions)
|
205
|
21
|
253
|
22 def getBBs(self):
|
258
|
23 bbs = []
|
|
24 for f in self.Functions:
|
|
25 bbs += f.BasicBlocks
|
|
26 return bbs
|
205
|
27
|
253
|
28 BasicBlocks = property(getBBs)
|
|
29 def addFunc(self, f):
|
258
|
30 self.funcs.append(f)
|
253
|
31 addFunction = addFunc
|
205
|
32
|
253
|
33 def addVariable(self, v):
|
205
|
34 self.variables.append(v)
|
|
35
|
253
|
36 def getVariables(self):
|
205
|
37 return self.variables
|
253
|
38 Variables = property(getVariables)
|
205
|
39
|
253
|
40 def getFunctions(self):
|
|
41 return self.funcs
|
|
42 Functions = property(getFunctions)
|
205
|
43
|
253
|
44 def findFunction(self, name):
|
230
|
45 for f in self.funcs:
|
|
46 if f.name == name:
|
|
47 return f
|
|
48 raise KeyError(name)
|
253
|
49 getFunction = findFunction
|
230
|
50
|
253
|
51 def dump(self):
|
170
|
52 print(self)
|
205
|
53 for v in self.Variables:
|
|
54 print(' ', v)
|
172
|
55 for fn in self.Functions:
|
|
56 print(fn)
|
|
57 for bb in fn.BasicBlocks:
|
|
58 print(' ', bb)
|
|
59 for ins in bb.Instructions:
|
|
60 print(' ', ins)
|
205
|
61
|
253
|
62 def dumpgv(self, outf):
|
171
|
63 outf.write('digraph G \n{\n')
|
173
|
64 for f in self.Functions:
|
174
|
65 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
|
173
|
66 for bb in f.BasicBlocks:
|
|
67 contents = str(bb) + '\n'
|
|
68 contents += '\n'.join([str(i) for i in bb.Instructions])
|
174
|
69 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
|
173
|
70 for successor in bb.Successors:
|
|
71 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
|
177
|
72 # Draw dags if any:
|
|
73 if hasattr(bb, 'dag'):
|
|
74 outf.write('{0} -> {1}\n'.format(id(bb), id(bb.dag)))
|
|
75 bb.dag.dumpgv(outf)
|
|
76
|
173
|
77 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
|
171
|
78 outf.write('}\n')
|
155
|
79
|
253
|
80 # Analysis functions:
|
|
81 def check(self):
|
239
|
82 """ Perform sanity check on module """
|
|
83 for i in self.Instructions:
|
268
|
84 pass
|
239
|
85 for f in self.Functions:
|
|
86 f.check()
|
|
87
|
171
|
88
|