comparison python/ir/module.py @ 271:cf7d5fb7d9c8

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents 5ec7580976d9
children e64bae57cda8
comparison
equal deleted inserted replaced
270:cdc76d183bcc 271:cf7d5fb7d9c8
1 # IR-Structures: 1 # IR-Structures:
2 from .instruction import * 2 from .instruction import *
3 from .basicblock import Block 3 from .function import Block
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
10 self.variables = [] 10 self.variables = []
11 11
12 def __repr__(self): 12 def __repr__(self):
13 return 'IR-module [{0}]'.format(self.name) 13 return 'IR-module [{0}]'.format(self.name)
14 14
15 def getInstructions(self):
16 ins = []
17 for bb in self.BasicBlocks:
18 ins += bb.Instructions
19 return ins
20 Instructions = property(getInstructions)
21
22 def getBBs(self):
23 bbs = []
24 for f in self.Functions:
25 bbs += f.BasicBlocks
26 return bbs
27
28 BasicBlocks = property(getBBs)
29 def addFunc(self, f): 15 def addFunc(self, f):
30 self.funcs.append(f) 16 self.funcs.append(f)
17
31 addFunction = addFunc 18 addFunction = addFunc
32 19
33 def addVariable(self, v): 20 def addVariable(self, v):
34 self.variables.append(v) 21 self.variables.append(v)
35 22
36 def getVariables(self): 23 def getVariables(self):
37 return self.variables 24 return self.variables
25
38 Variables = property(getVariables) 26 Variables = property(getVariables)
39 27
40 def getFunctions(self): 28 def getFunctions(self):
41 return self.funcs 29 return self.funcs
30
42 Functions = property(getFunctions) 31 Functions = property(getFunctions)
43 32
44 def findFunction(self, name): 33 def findFunction(self, name):
45 for f in self.funcs: 34 for f in self.funcs:
46 if f.name == name: 35 if f.name == name:
47 return f 36 return f
48 raise KeyError(name) 37 raise KeyError(name)
38
49 getFunction = findFunction 39 getFunction = findFunction
50 40
51 def dump(self): 41 def dump(self):
52 print(self) 42 print(self)
53 for v in self.Variables: 43 for v in self.Variables:
67 contents = str(bb) + '\n' 57 contents = str(bb) + '\n'
68 contents += '\n'.join([str(i) for i in bb.Instructions]) 58 contents += '\n'.join([str(i) for i in bb.Instructions])
69 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents)) 59 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
70 for successor in bb.Successors: 60 for successor in bb.Successors:
71 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor))) 61 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
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 62
77 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry))) 63 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
78 outf.write('}\n') 64 outf.write('}\n')
79 65
80 # Analysis functions: 66 # Analysis functions:
81 def check(self): 67 def check(self):
82 """ Perform sanity check on module """ 68 """ Perform sanity check on module """
83 for i in self.Instructions:
84 pass
85 for f in self.Functions: 69 for f in self.Functions:
86 f.check() 70 f.check()
87 71
88 72