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

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents 5ec7580976d9
children e64bae57cda8
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 *
271
cf7d5fb7d9c8 Reorganization
Windel Bouwman
parents: 268
diff changeset
3 from .function import Block
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:
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
6 """ Main container for a piece of code. """
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
7 def __init__(self, name):
155
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
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
12 def __repr__(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
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
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
15 def addFunc(self, f):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 253
diff changeset
16 self.funcs.append(f)
271
cf7d5fb7d9c8 Reorganization
Windel Bouwman
parents: 268
diff changeset
17
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
18 addFunction = addFunc
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
19
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
20 def addVariable(self, v):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
21 self.variables.append(v)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
22
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
23 def getVariables(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
24 return self.variables
271
cf7d5fb7d9c8 Reorganization
Windel Bouwman
parents: 268
diff changeset
25
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
26 Variables = property(getVariables)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
27
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
28 def getFunctions(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
29 return self.funcs
271
cf7d5fb7d9c8 Reorganization
Windel Bouwman
parents: 268
diff changeset
30
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
31 Functions = property(getFunctions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
32
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
33 def findFunction(self, name):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
34 for f in self.funcs:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
35 if f.name == name:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
36 return f
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
37 raise KeyError(name)
271
cf7d5fb7d9c8 Reorganization
Windel Bouwman
parents: 268
diff changeset
38
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
39 getFunction = findFunction
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
40
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
41 def dump(self):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
42 print(self)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
43 for v in self.Variables:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
44 print(' ', v)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
45 for fn in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
46 print(fn)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
47 for bb in fn.BasicBlocks:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
48 print(' ', bb)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
49 for ins in bb.Instructions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
50 print(' ', ins)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
51
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
52 def dumpgv(self, outf):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
53 outf.write('digraph G \n{\n')
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
54 for f in self.Functions:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
55 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
56 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
57 contents = str(bb) + '\n'
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
58 contents += '\n'.join([str(i) for i in bb.Instructions])
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
59 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
60 for successor in bb.Successors:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
61 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
62
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
63 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
64 outf.write('}\n')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
65
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
66 # Analysis functions:
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
67 def check(self):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
68 """ Perform sanity check on module """
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
69 for f in self.Functions:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
70 f.check()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
71
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
72