annotate python/ppci/codegen/codegen.py @ 331:a78b41ff6ad2

Added better recipe files
author Windel Bouwman
date Fri, 07 Feb 2014 12:39:59 +0100
parents 56e6ff84f646
children 6f4753202b9a
rev   line source
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 296
diff changeset
1 from ..ir import Module
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
2 from ..irutils import Verifier
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
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 """
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
24 self.logger.info('Generating code for {}'.format(irfunc.name))
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
25 # Create a frame for this function:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
26 frame = self.target.FrameClass(irfunc.name)
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)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 312
diff changeset
30 self.logger.info('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)
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 312
diff changeset
33 self.logger.info('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)
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
37 self.logger.info('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)
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
46 self.logger.info('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 """
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 296
diff changeset
51 assert isinstance(ircode, 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