171
|
1
|
|
2 class BasicBlock:
|
174
|
3 """ Uninterrupted sequence of instructions. """
|
171
|
4 def __init__(self, name):
|
|
5 self.name = name
|
|
6 self.instructions = []
|
|
7 def __repr__(self):
|
173
|
8 return 'BasicBlock {0}'.format(self.name)
|
|
9 def addInstruction(self, i):
|
|
10 i.parent = self
|
171
|
11 self.instructions.append(i)
|
173
|
12 addIns = addInstruction
|
|
13 def replaceInstruction(self, i1, i2):
|
|
14 idx = self.instructions.index(i1)
|
|
15 i1.parent = None
|
174
|
16 i1.delete()
|
173
|
17 i2.parent = self
|
|
18 self.instructions[idx] = i2
|
|
19 def removeInstruction(self, i):
|
|
20 i.parent = None
|
|
21 self.instructions.remove(i)
|
171
|
22 def getInstructions(self):
|
|
23 return self.instructions
|
174
|
24 def setInstructions(self, ins):
|
|
25 for i in self.instructions:
|
|
26 i.parent = None
|
|
27 self.instructions = ins
|
|
28 for i in self.instructions:
|
|
29 i.parent = self
|
|
30 Instructions = property(getInstructions, setInstructions)
|
171
|
31 def getLastIns(self):
|
|
32 return self.instructions[-1]
|
174
|
33 LastInstruction = property(getLastIns)
|
171
|
34 @property
|
|
35 def Empty(self):
|
|
36 return len(self.instructions) == 0
|
|
37 @property
|
173
|
38 def FirstInstruction(self):
|
171
|
39 return self.instructions[0]
|
173
|
40 FirstIns = FirstInstruction
|
|
41 def getSuccessors(self):
|
|
42 if not self.Empty:
|
174
|
43 i = self.LastInstruction
|
177
|
44 print(i)
|
173
|
45 return i.Targets
|
|
46 return []
|
|
47 Successors = property(getSuccessors)
|
174
|
48 def getPredecessors(self):
|
177
|
49 preds = []
|
|
50 for bb in self.parent.BasicBlocks:
|
|
51 if self in bb.Successors:
|
|
52 preds.append(bb)
|
|
53 return preds
|
174
|
54 Predecessors = property(getPredecessors)
|
171
|
55
|