annotate python/instructionselector.py @ 269:5f8c04a8d26b

Towards better modularity
author Windel Bouwman
date Sun, 18 Aug 2013 17:43:18 +0200
parents
children ea93e0a7a31e
rev   line source
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
1
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2 import ir
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
3 import irmach
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
4
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
5 def genTemps():
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
6 n = 900
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
7 while True:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
8 yield 't{}'.format(n)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
9 n = n + 1
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
10
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11 class InstructionSelector:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
12 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13 Base instruction selector. This class must be overridden by
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
14 backends.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
15 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
16 def newTmp(self):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
17 return self.temps.__next__()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
18
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
19 def getTempReg(self, tmp):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
20 if tmp not in self.tempMap:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
21 self.tempMap[tmp] = self.newTmp()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
22 return self.tempMap[tmp]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
24 def munchProgram(self, p):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 # Entry point for instruction selection
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
26 self.temps = genTemps()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 assert isinstance(p, ir.Module)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
28 self.result = []
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
29 self.targets = {}
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
30 self.tempMap = {} # Mapping from temporaries to infinite register
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
31 for f in p.Functions:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
32 # First define labels:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
33 for bb in f.BasicBlocks:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
34 itgt = self.makeIns('{}:'.format(bb.name))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35 self.targets[bb] = itgt
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
36 for bb in f.BasicBlocks:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 self.emit2(self.targets[bb])
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 for i in bb.Instructions:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
39 self.munchStm(i)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 bb.machIns = self.result
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 return self.result
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43 def makeIns(self, *args, **kwargs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 return irmach.AbstractInstruction(*args, **kwargs)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46 def emit(self, *args, **kwargs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 """ Abstract instruction emitter """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48 i = self.makeIns(*args, **kwargs)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 return self.emit2(i)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 def emit2(self, i):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 self.result.append(i)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53 return i
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
54
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
55 def munchStm(self, s):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
56 raise NotImplementedError()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
57
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
58 def munchExpr(self, e):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
59 raise NotImplementedError()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
60