annotate python/ppci/codegen/codegen.py @ 377:9667d78ba79e

Switched to xml for project description
author Windel Bouwman
date Fri, 11 Apr 2014 15:47:50 +0200
parents 39bf68bf1891
children
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
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 348
diff changeset
3 from ..transform import RemoveAddZero
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 336
diff changeset
4 from ..target import Target
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
5 from .. import CompilerError
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
6 from .canon import make as canonicalize
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
7 from .registerallocator import RegisterAllocator
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
8 import logging
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
9
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
10
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
11 class CodeGenerator:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
12 """ Generic code generator """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
13 def __init__(self, target):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
14 # TODO: schedule traces in better order.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
15 # This is optional!
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
16 assert isinstance(target, Target), target
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
17 self.logger = logging.getLogger('codegen')
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
18 self.target = target
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
19 self.ins_sel = target.ins_sel
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
20 self.ra = RegisterAllocator()
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
21 self.verifier = Verifier()
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
22
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
23 def generateFunc(self, irfunc, outs):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
24 """ Generate code for one function into a frame """
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
25 self.logger.debug('Generating code for {}'.format(irfunc.name))
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
26 # 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
27 frame = self.target.FrameClass(ir.label_name(irfunc))
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
28
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
29 # Canonicalize the intermediate language:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
30 canonicalize(irfunc, frame)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 348
diff changeset
31 RemoveAddZero().run(irfunc)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
32 self.logger.debug('after canonicalize', extra={'irfunc': irfunc})
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 301
diff changeset
33 self.verifier.verify_function(irfunc)
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
34 self.ins_sel.munchFunction(irfunc, frame)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
35 self.logger.debug('Selected instructions', extra={'ppci_frame': frame})
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
36
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
37 # Do register allocation:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
38 self.ra.allocFrame(frame)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
39 self.logger.debug('Registers allocated, now adding final glue')
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
40 # TODO: Peep-hole here?
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
41
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
42 # Add label and return and stack adjustment:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
43 frame.EntryExitGlue3()
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
44
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
45 # Materialize the register allocated instructions into a stream of
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
46 # real instructions.
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
47 self.target.lower_frame_to_stream(frame, outs)
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 316
diff changeset
48 self.logger.debug('Instructions materialized')
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
49 return frame
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
50
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
51 def generate(self, ircode, outs):
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
52 """ 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
53 assert isinstance(ircode, ir.Module)
364
c49459768aaa Work on globals
Windel Bouwman
parents: 355
diff changeset
54 outs.select_section('data')
c49459768aaa Work on globals
Windel Bouwman
parents: 355
diff changeset
55 for global_variable in ircode.Variables:
c49459768aaa Work on globals
Windel Bouwman
parents: 355
diff changeset
56 self.target.emit_global(outs, ir.label_name(global_variable))
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
57 outs.select_section('code')
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
58
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
59 # Munch program into a bunch of frames. One frame per function.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
60 # Each frame has a flat list of abstract instructions.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
61 # Generate code for all functions:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
62 self.frames = [self.generateFunc(f, outs) for f in ircode.Functions]
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
63 return self.frames