annotate python/ir/basicblock.py @ 252:c4370696ccc7

added optimize function
author Windel Bouwman
date Tue, 30 Jul 2013 17:57:46 +0200
parents ef683881c64e
children 04c19282a5aa
rev   line source
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
1
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
2 class BasicBlock:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
3 """ Uninterrupted sequence of instructions. """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
4 def __init__(self, name):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
5 self.name = name
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
6 self.instructions = []
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
7
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
8 def __repr__(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
9 return 'BasicBlock {0}'.format(self.name)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
10
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
11 def addInstruction(self, i):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
12 i.parent = self
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
13 self.instructions.append(i)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
14
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
15 def replaceInstruction(self, i1, i2):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
16 idx = self.instructions.index(i1)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
17 i1.parent = None
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
18 i1.delete()
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
19 i2.parent = self
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
20 self.instructions[idx] = i2
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
21
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
22 def removeInstruction(self, i):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
23 i.parent = None
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 243
diff changeset
24 i.delete()
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
25 self.instructions.remove(i)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
26
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
27 def getInstructions(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
28 return self.instructions
243
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
29 Instructions = property(getInstructions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
30
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
31 def getLastIns(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
32 return self.instructions[-1]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
33 LastInstruction = property(getLastIns)
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
34
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
35 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
36 def Empty(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
37 return len(self.instructions) == 0
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
38
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
39 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
40 def FirstInstruction(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
41 return self.instructions[0]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
42
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
43 def getSuccessors(self):
243
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
44 if not self.Empty:
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
45 i = self.LastInstruction
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
46 return i.Targets
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
47 return []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
48 Successors = property(getSuccessors)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
49
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
50 def getPredecessors(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
51 preds = []
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
52 for bb in self.parent.BasicBlocks:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
53 if self in bb.Successors:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
54 preds.append(bb)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
55 return preds
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
56 Predecessors = property(getPredecessors)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
57
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
58 def check(self):
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
59 for ins in self.Instructions:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
60 ins.check()
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
61