comparison python/ppci/target/instructionselector.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents python/target/instructionselector.py@d1ecc493384e
children 3bb7dcfe5529
comparison
equal deleted inserted replaced
341:4d204f6f7d4e 342:86b02c98a717
1 from ppci import ir
2 from ppci import irmach
3 from ppci.irmach import AbstractInstruction as makeIns
4 from .basetarget import Label
5
6 def genTemps():
7 n = 900
8 while True:
9 yield ir.Temp('t{}'.format(n))
10 n = n + 1
11
12
13 class InstructionSelector:
14 """
15 Base instruction selector. This class must be overridden by
16 backends.
17 """
18 def __init__(self):
19 self.temps = genTemps()
20
21 def newTmp(self):
22 return self.temps.__next__()
23
24
25 def munchFunction(self, f, frame):
26 # Entry point for instruction selection
27 assert isinstance(f, ir.Function)
28 self.targets = {}
29 # Enter a frame per function:
30 self.frame = frame
31 # First define labels:
32 for bb in f.Blocks:
33 itgt = makeIns(Label(ir.label_name(bb)))
34 self.targets[bb] = itgt
35 # Generate code for all blocks:
36 for bb in f.Blocks:
37 self.emit2(self.targets[bb])
38 for i in bb.Instructions:
39 self.munchStm(i)
40 self.munchStm(ir.Move(self.frame.rv, f.return_value))
41
42 def move(self, dst, src):
43 raise NotImplementedError('Not target implemented')
44
45 def emit(self, *args, **kwargs):
46 """ Abstract instruction emitter """
47 i = makeIns(*args, **kwargs)
48 return self.emit2(i)
49
50 def emit2(self, i):
51 self.frame.instructions.append(i)
52 return i
53
54 def munchStm(self, s):
55 """ Implement this in the target specific back-end """
56 raise NotImplementedError()
57
58 def munchExpr(self, e):
59 """ Implement this in the target specific back-end """
60 raise NotImplementedError()