annotate python/ppci/codegen/codegen.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents d1ecc493384e
children 3bb7dcfe5529
rev   line source
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 334
diff changeset
1 from .. import ir
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
2 from ..irutils import Verifier
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 336
diff changeset
3 from ..target import Target
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
4 from ppci import CompilerError
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
5 from .canon import make as canonicalize
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
6 from .registerallocator import RegisterAllocator
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
7 import logging
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
8
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
9
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
10 class CodeGenerator:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
11 """ Generic code generator """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
12 def __init__(self, target):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
13 # TODO: schedule traces in better order.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
14 # This is optional!
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
15 assert isinstance(target, Target), target
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
16 self.logger = logging.getLogger('codegen')
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
17 self.target = target
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
18 self.ins_sel = target.ins_sel
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
19 self.ra = RegisterAllocator()
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
20 self.verifier = Verifier()
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
21
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
22 def generateFunc(self, irfunc, outs):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
23 """ Generate code for one function into a frame """
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
24 self.logger.debug('Generating code for {}'.format(irfunc.name))
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
25 # Create a frame for this function:
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 334
diff changeset
26 frame = self.target.FrameClass(ir.label_name(irfunc))
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
27
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
28 # Canonicalize the intermediate language:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
29 canonicalize(irfunc, frame)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
30 self.logger.debug('after canonicalize', extra={'irfunc': irfunc})
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
31 self.verifier.verify_function(irfunc)
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
32 self.ins_sel.munchFunction(irfunc, frame)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
33 self.logger.debug('Selected instructions', extra={'ppci_frame': frame})
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
34
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
35 # Do register allocation:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
36 self.ra.allocFrame(frame)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
37 self.logger.debug('Registers allocated, now adding final glue')
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
38 # TODO: Peep-hole here?
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
39
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
40 # Add label and return and stack adjustment:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
41 frame.EntryExitGlue3()
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
42
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
43 # Materialize the register allocated instructions into a stream of
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
44 # real instructions.
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
45 frame.lower_to(outs)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
46 self.logger.debug('Instructions materialized')
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
47 return frame
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
48
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
49 def generate(self, ircode, outs):
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
50 """ Generate code into output stream """
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 334
diff changeset
51 assert isinstance(ircode, ir.Module)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
52 outs.selectSection('code')
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
53
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
54 # Munch program into a bunch of frames. One frame per function.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
55 # Each frame has a flat list of abstract instructions.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
56 # Generate code for all functions:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
57 self.frames = [self.generateFunc(f, outs) for f in ircode.Functions]
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
58 return self.frames