annotate python/ir/basicblock.py @ 177:460db5669efa

Added clean pass for IR
author Windel Bouwman
date Mon, 22 Apr 2013 23:54:54 +0200
parents 3eb06f5fb987
children d77cb5962cc5
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:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
3 """ Uninterrupted sequence of instructions. """
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
4 def __init__(self, name):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
5 self.name = name
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
6 self.instructions = []
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
7 def __repr__(self):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
8 return 'BasicBlock {0}'.format(self.name)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
9 def addInstruction(self, i):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
10 i.parent = self
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
11 self.instructions.append(i)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
12 addIns = addInstruction
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
13 def replaceInstruction(self, i1, i2):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
14 idx = self.instructions.index(i1)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
15 i1.parent = None
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
16 i1.delete()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
17 i2.parent = self
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
18 self.instructions[idx] = i2
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
19 def removeInstruction(self, i):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
20 i.parent = None
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
21 self.instructions.remove(i)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
22 def getInstructions(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
23 return self.instructions
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
24 def setInstructions(self, ins):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
25 for i in self.instructions:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
26 i.parent = None
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
27 self.instructions = ins
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
28 for i in self.instructions:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
29 i.parent = self
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
30 Instructions = property(getInstructions, setInstructions)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
31 def getLastIns(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
32 return self.instructions[-1]
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
33 LastInstruction = property(getLastIns)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
34 @property
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
35 def Empty(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
36 return len(self.instructions) == 0
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 @property
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
38 def FirstInstruction(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
39 return self.instructions[0]
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
40 FirstIns = FirstInstruction
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
41 def getSuccessors(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
42 if not self.Empty:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
43 i = self.LastInstruction
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
44 print(i)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
45 return i.Targets
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
46 return []
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
47 Successors = property(getSuccessors)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
48 def getPredecessors(self):
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
49 preds = []
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
50 for bb in self.parent.BasicBlocks:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
51 if self in bb.Successors:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
52 preds.append(bb)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
53 return preds
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
54 Predecessors = property(getPredecessors)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
55