annotate python/ir/module.py @ 231:521567d17388

simplify blink.c3
author Windel Bouwman
date Sat, 13 Jul 2013 20:20:44 +0200
parents 88a1e0baef65
children 63bb40758066
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
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
44 def findFunction(self, name):
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)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
49
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
50 def dump(self):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 158
diff changeset
51 print(self)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
52 for v in self.Variables:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
53 print(' ', v)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
54 for fn in self.Functions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
55 print(fn)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
56 for bb in fn.BasicBlocks:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
57 print(' ', bb)
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
58 for ins in bb.Instructions:
5a7d37d615ee Added function to IR
Windel Bouwman
parents: 171
diff changeset
59 print(' ', ins)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
60
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
61 def dumpgv(self, outf):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
62 outf.write('digraph G \n{\n')
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
63 for f in self.Functions:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
64 outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
65 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
66 contents = str(bb) + '\n'
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
67 contents += '\n'.join([str(i) for i in bb.Instructions])
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
68 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
69 for successor in bb.Successors:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
70 outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
71 # Draw dags if any:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
72 if hasattr(bb, 'dag'):
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
73 outf.write('{0} -> {1}\n'.format(id(bb), id(bb.dag)))
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
74 bb.dag.dumpgv(outf)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
75
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 172
diff changeset
76 outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
77 outf.write('}\n')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
78
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
79 # Analysis functions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
80 def check(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
81 """ Perform sanity check on module """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
82 for i in self.Instructions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
83 for t in i.defs:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
84 assert type(t) is Value, "def must be Value, not {0}".format(type(t))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
85 for t in i.uses:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
86 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
87