Mercurial > lcfOS
comparison python/ir/basicblock.py @ 239:63bb40758066
added check
author | Windel Bouwman |
---|---|
date | Mon, 22 Jul 2013 17:57:25 +0200 |
parents | 1fa3e0050b49 |
children | ef683881c64e |
comparison
equal
deleted
inserted
replaced
238:90637d1bbfad | 239:63bb40758066 |
---|---|
1 | 1 |
2 class BasicBlock: | 2 class BasicBlock: |
3 """ Uninterrupted sequence of instructions. """ | 3 """ Uninterrupted sequence of instructions. """ |
4 def __init__(self, name): | 4 def __init__(self, name): |
5 self.name = name | 5 self.name = name |
6 self.instructions = [] | 6 self.instructions = [] |
7 | |
7 def __repr__(self): | 8 def __repr__(self): |
8 return 'BasicBlock {0}'.format(self.name) | 9 return 'BasicBlock {0}'.format(self.name) |
10 | |
9 def addInstruction(self, i): | 11 def addInstruction(self, i): |
10 i.parent = self | 12 i.parent = self |
11 self.instructions.append(i) | 13 self.instructions.append(i) |
12 addIns = addInstruction | 14 addIns = addInstruction |
13 | 15 |
14 def replaceInstruction(self, i1, i2): | 16 def replaceInstruction(self, i1, i2): |
15 idx = self.instructions.index(i1) | 17 idx = self.instructions.index(i1) |
16 i1.parent = None | 18 i1.parent = None |
17 i1.delete() | 19 i1.delete() |
18 i2.parent = self | 20 i2.parent = self |
19 self.instructions[idx] = i2 | 21 self.instructions[idx] = i2 |
20 | 22 |
21 def removeInstruction(self, i): | 23 def removeInstruction(self, i): |
22 i.parent = None | 24 i.parent = None |
23 self.instructions.remove(i) | 25 self.instructions.remove(i) |
24 | 26 |
25 def getInstructions(self): | 27 def getInstructions(self): |
26 return self.instructions | 28 return self.instructions |
27 | 29 |
28 def setInstructions(self, ins): | 30 def setInstructions(self, ins): |
32 for i in self.instructions: | 34 for i in self.instructions: |
33 i.parent = self | 35 i.parent = self |
34 Instructions = property(getInstructions, setInstructions) | 36 Instructions = property(getInstructions, setInstructions) |
35 | 37 |
36 def getLastIns(self): | 38 def getLastIns(self): |
37 return self.instructions[-1] | 39 return self.instructions[-1] |
38 LastInstruction = property(getLastIns) | 40 LastInstruction = property(getLastIns) |
41 | |
39 @property | 42 @property |
40 def Empty(self): | 43 def Empty(self): |
41 return len(self.instructions) == 0 | 44 return len(self.instructions) == 0 |
45 | |
42 @property | 46 @property |
43 def FirstInstruction(self): | 47 def FirstInstruction(self): |
44 return self.instructions[0] | 48 return self.instructions[0] |
45 FirstIns = FirstInstruction | 49 FirstIns = FirstInstruction |
46 | 50 |
47 def getSuccessors(self): | 51 def getSuccessors(self): |
48 if not self.Empty: | 52 if not self.Empty: |
49 i = self.LastInstruction | 53 i = self.LastInstruction |
50 return i.Targets | 54 return i.Targets |
51 return [] | 55 return [] |
52 Successors = property(getSuccessors) | 56 Successors = property(getSuccessors) |
53 | 57 |
54 def getPredecessors(self): | 58 def getPredecessors(self): |
55 preds = [] | 59 preds = [] |
56 for bb in self.parent.BasicBlocks: | 60 for bb in self.parent.BasicBlocks: |
57 if self in bb.Successors: | 61 if self in bb.Successors: |
58 preds.append(bb) | 62 preds.append(bb) |
59 return preds | 63 return preds |
60 Predecessors = property(getPredecessors) | 64 Predecessors = property(getPredecessors) |
61 | 65 |
66 def check(self): | |
67 for ins in self.Instructions: | |
68 ins.check() | |
69 |