annotate 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
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):
171
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 = []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
7 def __repr__(self):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
8 return 'BasicBlock {0}'.format(self.name)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
9 def addInstruction(self, i):
173
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)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
12 addIns = addInstruction
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
13
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
14 def replaceInstruction(self, i1, i2):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
15 idx = self.instructions.index(i1)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
16 i1.parent = None
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
17 i1.delete()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
18 i2.parent = self
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
19 self.instructions[idx] = i2
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
20
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
21 def removeInstruction(self, i):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
22 i.parent = None
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
23 self.instructions.remove(i)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
24
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
25 def getInstructions(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
26 return self.instructions
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
27
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
28 def setInstructions(self, ins):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
29 for i in self.instructions:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
30 i.parent = None
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
31 self.instructions = ins
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
32 for i in self.instructions:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
33 i.parent = self
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
34 Instructions = property(getInstructions, setInstructions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
35
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
36 def getLastIns(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
37 return self.instructions[-1]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
38 LastInstruction = property(getLastIns)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
39 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
40 def Empty(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
41 return len(self.instructions) == 0
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 FirstInstruction(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
44 return self.instructions[0]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
45 FirstIns = FirstInstruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
46
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
47 def getSuccessors(self):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
48 if not self.Empty:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
49 i = self.LastInstruction
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
50 return i.Targets
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
51 return []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
52 Successors = property(getSuccessors)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
53
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
54 def getPredecessors(self):
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
55 preds = []
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
56 for bb in self.parent.BasicBlocks:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
57 if self in bb.Successors:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
58 preds.append(bb)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 174
diff changeset
59 return preds
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
60 Predecessors = property(getPredecessors)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
61