annotate python/ir/function.py @ 270:cdc76d183bcc

first register allocator
author Windel Bouwman
date Mon, 19 Aug 2013 21:14:28 +0200
parents 5f8c04a8d26b
children cf7d5fb7d9c8
rev   line source
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
1 from .basicblock import Block
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents:
diff changeset
2
5a7d37d615ee Added function to IR
Windel Bouwman
parents:
diff changeset
3 class Function:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
4 def __init__(self, name):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
5 self.name = name
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
6 self.entry = Block('{}_entry'.format(name))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
7 self.epiloog = Block('{}_epilog'.format(name))
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
8
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
9 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
10 return 'Function {0}'.format(self.name)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
11
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
12 def addBB(self, bb):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
13 self.bbs.append(bb)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
14 bb.parent = self
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
15 addBasicBlock = addBB
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
16
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
17 def removeBasicBlock(self, bb):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
18 self.bbs.remove(bb)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
19 bb.parent = None
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
20
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
21 def getBBs(self):
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
22 bbs = [self.entry]
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
23 worklist = [self.entry]
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
24 while worklist:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
25 b = worklist.pop()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
26 for sb in b.Successors:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
27 if sb not in bbs:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
28 bbs.append(sb)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
29 worklist.append(sb)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
30 bbs.remove(self.entry)
270
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
31 if self.epiloog in bbs:
cdc76d183bcc first register allocator
Windel Bouwman
parents: 269
diff changeset
32 bbs.remove(self.epiloog)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
33 bbs.insert(0, self.entry)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
34 bbs.append(self.epiloog)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 256
diff changeset
35 return bbs
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
36
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
37 def findBasicBlock(self, name):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
38 for bb in self.bbs:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
39 if bb.name == name:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
40 return bb
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
41 raise KeyError(name)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
42
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 205
diff changeset
43 BasicBlocks = property(getBBs)
172
5a7d37d615ee Added function to IR
Windel Bouwman
parents:
diff changeset
44
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
45 @property
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
46 def Entry(self):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
47 return self.entry
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
48
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
49 def check(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
50 for bb in self.BasicBlocks:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
51 bb.check()
253
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
52
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
53 def call(self, *args):
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
54 varmap = {}
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
55 bb = self.Entry
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
56 ip = 0
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
57 while True:
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
58 i = bb.Instructions[ip]
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
59 ip += 1
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
60 return
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
61
74c6a20302d5 Added better logging
Windel Bouwman
parents: 239
diff changeset
62