Mercurial > lcfOS
annotate python/target/instructionselector.py @ 340:c7cc54c0dfdf devel
Test featurebranch
author | Windel Bouwman |
---|---|
date | Sun, 23 Feb 2014 16:24:01 +0100 |
parents | d1ecc493384e |
children |
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 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
301
diff
changeset
|
24 |
275 | 25 def munchFunction(self, f, frame): |
269 | 26 # Entry point for instruction selection |
275 | 27 assert isinstance(f, ir.Function) |
269 | 28 self.targets = {} |
275 | 29 # Enter a frame per function: |
30 self.frame = frame | |
31 # First define labels: | |
32 for bb in f.Blocks: | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
301
diff
changeset
|
33 itgt = makeIns(target.Label(ir.label_name(bb))) |
275 | 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)) | |
269 | 41 |
275 | 42 def move(self, dst, src): |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
43 raise NotImplementedError('Not target implemented') |
269 | 44 |
45 def emit(self, *args, **kwargs): | |
46 """ Abstract instruction emitter """ | |
275 | 47 i = makeIns(*args, **kwargs) |
269 | 48 return self.emit2(i) |
49 | |
50 def emit2(self, i): | |
274 | 51 self.frame.instructions.append(i) |
269 | 52 return i |
53 | |
54 def munchStm(self, s): | |
274 | 55 """ Implement this in the target specific back-end """ |
269 | 56 raise NotImplementedError() |
57 | |
58 def munchExpr(self, e): | |
274 | 59 """ Implement this in the target specific back-end """ |
269 | 60 raise NotImplementedError() |