annotate python/ir/module.py @ 220:3f6c30a5d234

Major change in expression parsing to enable pointers and structs
author Windel Bouwman
date Sat, 06 Jul 2013 21:32:20 +0200
parents d77cb5962cc5
children 88a1e0baef65
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 = []
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
10 self.variables = []
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
11
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
12 def __repr__(self):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
13 return 'IR-module [{0}]'.format(self.name)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
14
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
15 def getInstructions(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
16 ins = []
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
17 for bb in self.BasicBlocks:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
18 ins += bb.Instructions
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
19 return ins
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
20 Instructions = property(getInstructions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
21
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
22 def getBBs(self):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
23 bbs = []
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
24 for f in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
25 bbs += f.BasicBlocks
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
26 return bbs
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
27
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
28 BasicBlocks = property(getBBs)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
29 def addFunc(self, f):
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
30 self.funcs.append(f)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
31 addFunction = addFunc
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
32
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
33 def addVariable(self, v):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
34 self.variables.append(v)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
35
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
36 def getVariables(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
37 return self.variables
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
38 Variables = property(getVariables)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
39
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
40 def getFunctions(self):
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
41 return self.funcs
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
42 Functions = property(getFunctions)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
43
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
44 def dump(self):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
45 print(self)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
46 for v in self.Variables:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
47 print(' ', v)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
48 for fn in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
49 print(fn)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
50 for bb in fn.BasicBlocks:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
51 print(' ', bb)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
52 for ins in bb.Instructions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
53 print(' ', ins)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
54
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
55 def dumpgv(self, outf):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
56 outf.write('digraph G \n{\n')
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
57 for f in self.Functions:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
58 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
59 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
60 contents = str(bb) + '\n'
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
61 contents += '\n'.join([str(i) for i in bb.Instructions])
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
62 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
63 for successor in bb.Successors:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
64 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
65 # Draw dags if any:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
66 if hasattr(bb, 'dag'):
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
67 outf.write('{0} -> {1}\n'.format(id(bb), id(bb.dag)))
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
68 bb.dag.dumpgv(outf)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
69
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
70 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
71 outf.write('}\n')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
72
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
73 # Analysis functions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
74 def check(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
75 """ Perform sanity check on module """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
76 for i in self.Instructions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
77 for t in i.defs:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
78 assert type(t) is Value, "def must be Value, not {0}".format(type(t))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
79 for t in i.uses:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
80 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
81