Mercurial > lcfOS
annotate python/target/instructionselector.py @ 305:0615b5308710
Updated docs
author | Windel Bouwman |
---|---|
date | Fri, 06 Dec 2013 13:50:38 +0100 |
parents | 6753763d3bec |
children | d1ecc493384e |
rev | line source |
---|---|
301 | 1 from ppci import ir |
2 from ppci import irmach | |
3 from ppci.irmach import AbstractInstruction as makeIns | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
4 import target |
269 | 5 |
6 def genTemps(): | |
7 n = 900 | |
8 while True: | |
275 | 9 yield ir.Temp('t{}'.format(n)) |
269 | 10 n = n + 1 |
11 | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
12 |
269 | 13 class InstructionSelector: |
14 """ | |
15 Base instruction selector. This class must be overridden by | |
16 backends. | |
17 """ | |
275 | 18 def __init__(self): |
19 self.temps = genTemps() | |
20 | |
269 | 21 def newTmp(self): |
22 return self.temps.__next__() | |
23 | |
275 | 24 def munchFunction(self, f, frame): |
269 | 25 # Entry point for instruction selection |
275 | 26 assert isinstance(f, ir.Function) |
269 | 27 self.targets = {} |
275 | 28 # Enter a frame per function: |
29 self.frame = frame | |
30 # First define labels: | |
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 | 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)) | |
269 | 40 |
275 | 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 | 43 |
44 def emit(self, *args, **kwargs): | |
45 """ Abstract instruction emitter """ | |
275 | 46 i = makeIns(*args, **kwargs) |
269 | 47 return self.emit2(i) |
48 | |
49 def emit2(self, i): | |
274 | 50 self.frame.instructions.append(i) |
269 | 51 return i |
52 | |
53 def munchStm(self, s): | |
274 | 54 """ Implement this in the target specific back-end """ |
269 | 55 raise NotImplementedError() |
56 | |
57 def munchExpr(self, e): | |
274 | 58 """ Implement this in the target specific back-end """ |
269 | 59 raise NotImplementedError() |