annotate python/ir/basicblock.py @ 251:6ed3d3a82a63

Added another c3 example. First import attempt
author Windel Bouwman
date Mon, 29 Jul 2013 20:23:13 +0200
parents ef683881c64e
children c4370696ccc7
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):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
5 self.name = name
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
6 self.instructions = []
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
7
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
8 def __repr__(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
9 return 'BasicBlock {0}'.format(self.name)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
10
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
11 def addInstruction(self, i):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
12 i.parent = self
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
13 self.instructions.append(i)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
14
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
15 def replaceInstruction(self, i1, i2):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
16 idx = self.instructions.index(i1)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
17 i1.parent = None
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
18 i1.delete()
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
19 i2.parent = self
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
20 self.instructions[idx] = i2
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
21
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
22 def removeInstruction(self, i):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
23 i.parent = None
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
24 self.instructions.remove(i)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
25
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
26 def getInstructions(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
27 return self.instructions
243
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
28 Instructions = property(getInstructions)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 177
diff changeset
29
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
30 def getLastIns(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
31 return self.instructions[-1]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
32 LastInstruction = property(getLastIns)
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
33
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
34 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
35 def Empty(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
36 return len(self.instructions) == 0
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
37
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
38 @property
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
39 def FirstInstruction(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
40 return self.instructions[0]
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
41
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
42 def getSuccessors(self):
243
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
43 if not self.Empty:
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
44 i = self.LastInstruction
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
45 return i.Targets
ef683881c64e Remove various files
Windel Bouwman
parents: 239
diff changeset
46 return []
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
47 Successors = property(getSuccessors)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
48
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
49 def getPredecessors(self):
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
50 preds = []
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
51 for bb in self.parent.BasicBlocks:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
52 if self in bb.Successors:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
53 preds.append(bb)
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
54 return preds
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 205
diff changeset
55 Predecessors = property(getPredecessors)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents:
diff changeset
56
239
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
57 def check(self):
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
58 for ins in self.Instructions:
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
59 ins.check()
63bb40758066 added check
Windel Bouwman
parents: 219
diff changeset
60