annotate python/instructionselector.py @ 274:ea93e0a7a31e

Move docs
author Windel Bouwman
date Wed, 04 Sep 2013 17:35:06 +0200
parents 5f8c04a8d26b
children 6f2423df0675
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)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
28 self.frames = []
269
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:
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
32 # Enter a frame per function:
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
33 self.frame = self.newFrame(f.name)
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
34 self.frames.append(self.frame)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
35 # First define labels:
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
36 for bb in f.Blocks:
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
37 itgt = self.makeIns('{}:'.format(bb.name))
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
38 self.targets[bb] = itgt
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
39 for bb in f.Blocks:
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40 self.emit2(self.targets[bb])
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
41 for i in bb.Instructions:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
42 self.munchStm(i)
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
43 #bb.machIns = self.result
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
44 return self.frames
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
46 def makeIns(self, *args, **kwargs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 return irmach.AbstractInstruction(*args, **kwargs)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 def emit(self, *args, **kwargs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
50 """ Abstract instruction emitter """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 i = self.makeIns(*args, **kwargs)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52 return self.emit2(i)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
54 def emit2(self, i):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
55 self.frame.instructions.append(i)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
56 return i
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
57
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
58 def newFrame(self, name):
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
59 raise NotImplementedError()
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
60
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
61 def munchStm(self, s):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
62 """ Implement this in the target specific back-end """
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
63 raise NotImplementedError()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
64
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
65 def munchExpr(self, e):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
66 """ Implement this in the target specific back-end """
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
67 raise NotImplementedError()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
68