comparison python/ir/module.py @ 253:74c6a20302d5

Added better logging
author Windel Bouwman
date Wed, 31 Jul 2013 17:57:03 +0200
parents 63bb40758066
children 04c19282a5aa
comparison
equal deleted inserted replaced
252:c4370696ccc7 253:74c6a20302d5
1 # IR-Structures: 1 # IR-Structures:
2 from .instruction import * 2 from .instruction import *
3 from .basicblock import BasicBlock 3 from .basicblock import BasicBlock
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
9 self.funcs = [] 9 self.funcs = []
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): 15 def getInstructions(self):
16 ins = [] 16 ins = []
17 for bb in self.BasicBlocks: 17 for bb in self.BasicBlocks:
18 ins += bb.Instructions 18 ins += bb.Instructions
19 return ins 19 return ins
20 Instructions = property(getInstructions) 20 Instructions = property(getInstructions)
21 21
22 def getBBs(self): 22 def getBBs(self):
23 bbs = [] 23 bbs = []
24 for f in self.Functions: 24 for f in self.Functions:
25 bbs += f.BasicBlocks 25 bbs += f.BasicBlocks
26 return bbs 26 return bbs
27 27
28 BasicBlocks = property(getBBs) 28 BasicBlocks = property(getBBs)
29 def addFunc(self, f): 29 def addFunc(self, f):
30 self.funcs.append(f) 30 self.funcs.append(f)
31 addFunction = addFunc 31 addFunction = addFunc
32 32
33 def addVariable(self, v): 33 def addVariable(self, v):
34 self.variables.append(v) 34 self.variables.append(v)
35 35
36 def getVariables(self): 36 def getVariables(self):
37 return self.variables 37 return self.variables
38 Variables = property(getVariables) 38 Variables = property(getVariables)
39 39
40 def getFunctions(self): 40 def getFunctions(self):
41 return self.funcs 41 return self.funcs
42 Functions = property(getFunctions) 42 Functions = property(getFunctions)
43 43
44 def findFunction(self, name): 44 def findFunction(self, name):
45 for f in self.funcs: 45 for f in self.funcs:
46 if f.name == name: 46 if f.name == name:
47 return f 47 return f
48 raise KeyError(name) 48 raise KeyError(name)
49 getFunction = findFunction
49 50
50 def dump(self): 51 def dump(self):
51 print(self) 52 print(self)
52 for v in self.Variables: 53 for v in self.Variables:
53 print(' ', v) 54 print(' ', v)
54 for fn in self.Functions: 55 for fn in self.Functions:
55 print(fn) 56 print(fn)
56 for bb in fn.BasicBlocks: 57 for bb in fn.BasicBlocks:
57 print(' ', bb) 58 print(' ', bb)
58 for ins in bb.Instructions: 59 for ins in bb.Instructions:
59 print(' ', ins) 60 print(' ', ins)
60 61
61 def dumpgv(self, outf): 62 def dumpgv(self, outf):
62 outf.write('digraph G \n{\n') 63 outf.write('digraph G \n{\n')
63 for f in self.Functions: 64 for f in self.Functions:
64 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f)) 65 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
65 for bb in f.BasicBlocks: 66 for bb in f.BasicBlocks:
66 contents = str(bb) + '\n' 67 contents = str(bb) + '\n'
74 bb.dag.dumpgv(outf) 75 bb.dag.dumpgv(outf)
75 76
76 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry))) 77 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
77 outf.write('}\n') 78 outf.write('}\n')
78 79
79 # Analysis functions: 80 # Analysis functions:
80 def check(self): 81 def check(self):
81 """ Perform sanity check on module """ 82 """ Perform sanity check on module """
82 for i in self.Instructions: 83 for i in self.Instructions:
83 for t in i.defs: 84 for t in i.defs:
84 assert type(t) is Value, "def must be Value, not {0}".format(type(t)) 85 assert type(t) is Value, "def must be Value, not {0}".format(type(t))
85 for t in i.uses: 86 for t in i.uses:
86 assert type(t) is Use, "use must be Value, not {0}".format(type(t)) 87 assert type(t) is Use, "use must be Value, not {0}".format(type(t))
87 for f in self.Functions: 88 for f in self.Functions:
88 f.check() 89 f.check()
89 90
90 91