annotate python/instructionselector.py @ 281:4496cae24d7f

Improved logview
author Windel Bouwman
date Sat, 02 Nov 2013 11:11:40 +0100
parents 02385f62f250
children
rev   line source
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
1 import ir
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
2 import irmach
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
3 from irmach import AbstractInstruction as makeIns
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
4 import target
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
5
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
6 def genTemps():
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
7 n = 900
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
8 while True:
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
9 yield ir.Temp('t{}'.format(n))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
10 n = n + 1
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
11
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
12
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
13 class InstructionSelector:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
14 """
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
15 Base instruction selector. This class must be overridden by
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
16 backends.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
17 """
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
18 def __init__(self):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
19 self.temps = genTemps()
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
20
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
21 def newTmp(self):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
22 return self.temps.__next__()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
23
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
24 def munchFunction(self, f, frame):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
25 # Entry point for instruction selection
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
26 assert isinstance(f, ir.Function)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
27 self.targets = {}
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
28 # Enter a frame per function:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
29 self.frame = frame
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
30 # First define labels:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
31 for bb in f.Blocks:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
32 itgt = makeIns(target.Label(bb.name))
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
33 self.targets[bb] = itgt
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
34 # Generate code for all blocks:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
35 for bb in f.Blocks:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
36 self.emit2(self.targets[bb])
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
37 for i in bb.Instructions:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
38 self.munchStm(i)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
39 self.munchStm(ir.Move(self.frame.rv, f.return_value))
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
40
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
41 def move(self, dst, src):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
42 raise NotImplementedError('Not target implemented')
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
43
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
44 def emit(self, *args, **kwargs):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
45 """ Abstract instruction emitter """
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
46 i = makeIns(*args, **kwargs)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
47 return self.emit2(i)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
48
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
49 def emit2(self, i):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
50 self.frame.instructions.append(i)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
51 return i
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
52
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
53 def munchStm(self, s):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
54 """ Implement this in the target specific back-end """
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
55 raise NotImplementedError()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
56
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
57 def munchExpr(self, e):
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 269
diff changeset
58 """ Implement this in the target specific back-end """
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
59 raise NotImplementedError()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents:
diff changeset
60