annotate python/ir/module.py @ 202:f22b431f4113

Added arm add instruction
author Windel Bouwman
date Sat, 15 Jun 2013 10:02:50 +0200
parents 460db5669efa
children d77cb5962cc5
rev   line source
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
1 # IR-Structures:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
2 from .instruction import *
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
3 from .basicblock import BasicBlock
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
4
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
5 class Module:
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
6 """ Main container for a piece of code. """
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
7 def __init__(self, name):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
8 self.name = name
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
9 self.funcs = []
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
10 def __repr__(self):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
11 return 'IR-module [{0}]'.format(self.name)
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
12 def getInstructions(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
13 ins = []
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
14 for bb in self.BasicBlocks:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
15 ins += bb.Instructions
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
16 return ins
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
17 Instructions = property(getInstructions)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
18 def getBBs(self):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
19 bbs = []
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
20 for f in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
21 bbs += f.BasicBlocks
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
22 return bbs
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
23 BasicBlocks = property(getBBs)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
24 def addFunc(self, f):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
25 self.funcs.append(f)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
26 def getFuncs(self):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
27 return self.funcs
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
28 Functions = property(getFuncs)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
29 def dump(self):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
30 print(self)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
31 for fn in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
32 print(fn)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
33 for bb in fn.BasicBlocks:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
34 print(' ', bb)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
35 for ins in bb.Instructions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
36 print(' ', ins)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
37 print('END')
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
38 def dumpgv(self, outf):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
39 outf.write('digraph G \n{\n')
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
40 for f in self.Functions:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
41 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
42 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
43 contents = str(bb) + '\n'
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
44 contents += '\n'.join([str(i) for i in bb.Instructions])
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
45 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
46 for successor in bb.Successors:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
47 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
48 # Draw dags if any:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
49 if hasattr(bb, 'dag'):
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
50 outf.write('{0} -> {1}\n'.format(id(bb), id(bb.dag)))
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
51 bb.dag.dumpgv(outf)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
52
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
53 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
54 outf.write('}\n')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
55
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
56 # Analysis functions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
57 def check(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
58 """ Perform sanity check on module """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
59 for i in self.Instructions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
60 for t in i.defs:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
61 assert type(t) is Value, "def must be Value, not {0}".format(type(t))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
62 for t in i.uses:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
63 assert type(t) is Use, "use must be Value, not {0}".format(type(t))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
64