Mercurial > lcfOS
comparison python/target/instructionselector.py @ 290:7b38782ed496
File moves
author | Windel Bouwman |
---|---|
date | Sun, 24 Nov 2013 11:24:15 +0100 |
parents | python/instructionselector.py@02385f62f250 |
children | 6753763d3bec |
comparison
equal
deleted
inserted
replaced
289:bd2593de3ff8 | 290:7b38782ed496 |
---|---|
1 import ir | |
2 import irmach | |
3 from irmach import AbstractInstruction as makeIns | |
4 import target | |
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 def munchFunction(self, f, frame): | |
25 # Entry point for instruction selection | |
26 assert isinstance(f, ir.Function) | |
27 self.targets = {} | |
28 # Enter a frame per function: | |
29 self.frame = frame | |
30 # First define labels: | |
31 for bb in f.Blocks: | |
32 itgt = makeIns(target.Label(bb.name)) | |
33 self.targets[bb] = itgt | |
34 # Generate code for all blocks: | |
35 for bb in f.Blocks: | |
36 self.emit2(self.targets[bb]) | |
37 for i in bb.Instructions: | |
38 self.munchStm(i) | |
39 self.munchStm(ir.Move(self.frame.rv, f.return_value)) | |
40 | |
41 def move(self, dst, src): | |
42 raise NotImplementedError('Not target implemented') | |
43 | |
44 def emit(self, *args, **kwargs): | |
45 """ Abstract instruction emitter """ | |
46 i = makeIns(*args, **kwargs) | |
47 return self.emit2(i) | |
48 | |
49 def emit2(self, i): | |
50 self.frame.instructions.append(i) | |
51 return i | |
52 | |
53 def munchStm(self, s): | |
54 """ Implement this in the target specific back-end """ | |
55 raise NotImplementedError() | |
56 | |
57 def munchExpr(self, e): | |
58 """ Implement this in the target specific back-end """ | |
59 raise NotImplementedError() |