Mercurial > lcfOS
comparison python/ir/basicblock.py @ 219:1fa3e0050b49
Expanded ad hoc code generator
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 12:38:09 +0200 |
parents | d77cb5962cc5 |
children | 63bb40758066 |
comparison
equal
deleted
inserted
replaced
218:494828a7adf1 | 219:1fa3e0050b49 |
---|---|
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 def __repr__(self): | 7 def __repr__(self): |
8 return 'BasicBlock {0}'.format(self.name) | 8 return 'BasicBlock {0}'.format(self.name) |
9 def addInstruction(self, i): | 9 def addInstruction(self, i): |
10 i.parent = self | 10 i.parent = self |
11 self.instructions.append(i) | 11 self.instructions.append(i) |
12 addIns = addInstruction | 12 addIns = addInstruction |
13 | 13 |
14 def replaceInstruction(self, i1, i2): | 14 def replaceInstruction(self, i1, i2): |
15 idx = self.instructions.index(i1) | 15 idx = self.instructions.index(i1) |
16 i1.parent = None | 16 i1.parent = None |
17 i1.delete() | 17 i1.delete() |
18 i2.parent = self | 18 i2.parent = self |
19 self.instructions[idx] = i2 | 19 self.instructions[idx] = i2 |
20 | 20 |
21 def removeInstruction(self, i): | 21 def removeInstruction(self, i): |
22 i.parent = None | 22 i.parent = None |
23 self.instructions.remove(i) | 23 self.instructions.remove(i) |
24 | 24 |
25 def getInstructions(self): | 25 def getInstructions(self): |
26 return self.instructions | 26 return self.instructions |
27 | 27 |
28 def setInstructions(self, ins): | 28 def setInstructions(self, ins): |
29 for i in self.instructions: | 29 for i in self.instructions: |
30 i.parent = None | 30 i.parent = None |
31 self.instructions = ins | 31 self.instructions = ins |
32 for i in self.instructions: | 32 for i in self.instructions: |
33 i.parent = self | 33 i.parent = self |
34 Instructions = property(getInstructions, setInstructions) | 34 Instructions = property(getInstructions, setInstructions) |
35 | 35 |
36 def getLastIns(self): | 36 def getLastIns(self): |
37 return self.instructions[-1] | 37 return self.instructions[-1] |
38 LastInstruction = property(getLastIns) | 38 LastInstruction = property(getLastIns) |
39 @property | 39 @property |
40 def Empty(self): | 40 def Empty(self): |
41 return len(self.instructions) == 0 | 41 return len(self.instructions) == 0 |
42 @property | 42 @property |
43 def FirstInstruction(self): | 43 def FirstInstruction(self): |
44 return self.instructions[0] | 44 return self.instructions[0] |
45 FirstIns = FirstInstruction | 45 FirstIns = FirstInstruction |
46 def getSuccessors(self): | 46 |
47 def getSuccessors(self): | |
47 if not self.Empty: | 48 if not self.Empty: |
48 i = self.LastInstruction | 49 i = self.LastInstruction |
49 print(i) | |
50 return i.Targets | 50 return i.Targets |
51 return [] | 51 return [] |
52 Successors = property(getSuccessors) | 52 Successors = property(getSuccessors) |
53 def getPredecessors(self): | 53 |
54 def getPredecessors(self): | |
54 preds = [] | 55 preds = [] |
55 for bb in self.parent.BasicBlocks: | 56 for bb in self.parent.BasicBlocks: |
56 if self in bb.Successors: | 57 if self in bb.Successors: |
57 preds.append(bb) | 58 preds.append(bb) |
58 return preds | 59 return preds |
59 Predecessors = property(getPredecessors) | 60 Predecessors = property(getPredecessors) |
60 | 61 |