annotate python/ir/basicblock.py @ 239:63bb40758066

added check
author Windel Bouwman
date Mon, 22 Jul 2013 17:57:25 +0200
parents 1fa3e0050b49
children ef683881c64e
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)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
14 addIns = addInstruction
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
15
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
16 def replaceInstruction(self, i1, i2):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
17 idx = self.instructions.index(i1)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
18 i1.parent = None
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
19 i1.delete()
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
20 i2.parent = self
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
21 self.instructions[idx] = i2
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
22
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
23 def removeInstruction(self, i):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
24 i.parent = None
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
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
29
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
30 def setInstructions(self, ins):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
31 for i in self.instructions:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
32 i.parent = None
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
33 self.instructions = ins
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
34 for i in self.instructions:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
35 i.parent = self
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
36 Instructions = property(getInstructions, setInstructions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
37
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
38 def getLastIns(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
39 return self.instructions[-1]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
40 LastInstruction = property(getLastIns)
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
41
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
42 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
43 def Empty(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
44 return len(self.instructions) == 0
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
45
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
46 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
47 def FirstInstruction(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
48 return self.instructions[0]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
49 FirstIns = FirstInstruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
50
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
51 def getSuccessors(self):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
52 if not self.Empty:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
53 i = self.LastInstruction
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
54 return i.Targets
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
55 return []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
56 Successors = property(getSuccessors)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
57
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
58 def getPredecessors(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
59 preds = []
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
60 for bb in self.parent.BasicBlocks:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
61 if self in bb.Successors:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
62 preds.append(bb)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
63 return preds
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
64 Predecessors = property(getPredecessors)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
65
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
66 def check(self):
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
67 for ins in self.Instructions:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
68 ins.check()
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
69