annotate python/ir/module.py @ 268:5ec7580976d9

Op naar tree-IR
author Windel Bouwman
date Wed, 14 Aug 2013 20:12:40 +0200
parents 04c19282a5aa
children cf7d5fb7d9c8
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 *
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 258
diff changeset
3 from .basicblock 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 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
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
20 Instructions = property(getInstructions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
21
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
22 def getBBs(self):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 253
diff changeset
23 bbs = []
04c19282a5aa Added register allocator
Windel Bouwman
parents: 253
diff changeset
24 for f in self.Functions:
04c19282a5aa Added register allocator
Windel Bouwman
parents: 253
diff changeset
25 bbs += f.BasicBlocks
04c19282a5aa Added register allocator
Windel Bouwman
parents: 253
diff changeset
26 return bbs
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 BasicBlocks = property(getBBs)
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
29 def addFunc(self, f):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 253
diff changeset
30 self.funcs.append(f)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
31 addFunction = addFunc
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 addVariable(self, v):
205
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
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
36 def getVariables(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
37 return self.variables
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
38 Variables = property(getVariables)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
39
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
40 def getFunctions(self):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
41 return self.funcs
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
42 Functions = property(getFunctions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
43
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
44 def findFunction(self, name):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
45 for f in self.funcs:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
46 if f.name == name:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
47 return f
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
48 raise KeyError(name)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
49 getFunction = findFunction
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
50
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
51 def dump(self):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
52 print(self)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
53 for v in self.Variables:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
54 print(' ', v)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
55 for fn in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
56 print(fn)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
57 for bb in fn.BasicBlocks:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
58 print(' ', bb)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
59 for ins in bb.Instructions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
60 print(' ', ins)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
61
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
62 def dumpgv(self, outf):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
63 outf.write('digraph G \n{\n')
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
64 for f in self.Functions:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
65 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
66 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
67 contents = str(bb) + '\n'
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
68 contents += '\n'.join([str(i) for i in bb.Instructions])
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
69 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
70 for successor in bb.Successors:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
71 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
72 # Draw dags if any:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
73 if hasattr(bb, 'dag'):
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
74 outf.write('{0} -> {1}\n'.format(id(bb), id(bb.dag)))
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
75 bb.dag.dumpgv(outf)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
76
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
77 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
78 outf.write('}\n')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
79
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
80 # Analysis functions:
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
81 def check(self):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
82 """ Perform sanity check on module """
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
83 for i in self.Instructions:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 258
diff changeset
84 pass
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
85 for f in self.Functions:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
86 f.check()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
87
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
88