comparison python/ppci/codegen/codegen.py @ 312:2c9768114877

Added cool logging formatter
author Windel Bouwman
date Mon, 16 Dec 2013 17:58:15 +0100
parents 6753763d3bec
children 56e6ff84f646
comparison
equal deleted inserted replaced
311:ff665880a6b0 312:2c9768114877
1 from ..ir import Module 1 from ..ir import Module
2 from ..irutils import Verifier
2 from target import Target 3 from target import Target
3 from ppci import CompilerError 4 from ppci import CompilerError
4 from .canon import make as canonicalize 5 from .canon import make as canonicalize
5 from .registerallocator import RegisterAllocator 6 from .registerallocator import RegisterAllocator
7 import logging
6 8
7 9
8 class CodeGenerator: 10 class CodeGenerator:
9 """ Generic code generator """ 11 """ Generic code generator """
10 def __init__(self, target): 12 def __init__(self, target):
11 # TODO: schedule traces in better order. 13 # TODO: schedule traces in better order.
12 # This is optional! 14 # This is optional!
13 assert isinstance(target, Target), target 15 assert isinstance(target, Target), target
16 self.logger = logging.getLogger('codegen')
14 self.target = target 17 self.target = target
15 self.ins_sel = target.ins_sel 18 self.ins_sel = target.ins_sel
16 self.ra = RegisterAllocator() 19 self.ra = RegisterAllocator()
20 self.verifier = Verifier()
17 21
18 def generateFunc(self, irfunc, outs): 22 def generateFunc(self, irfunc, outs):
19 """ Generate code for one function into a frame """ 23 """ Generate code for one function into a frame """
24 self.logger.info('Generating code for {}'.format(irfunc.name))
20 # Create a frame for this function: 25 # Create a frame for this function:
21 frame = self.target.FrameClass(irfunc.name) 26 frame = self.target.FrameClass(irfunc.name)
22 27
23 # Canonicalize the intermediate language: 28 # Canonicalize the intermediate language:
24 canonicalize(irfunc, frame) 29 canonicalize(irfunc, frame)
30 self.logger.info('after canonicalize', extra={'irfunc':irfunc})
31 self.verifier.verify_function(irfunc)
25 self.ins_sel.munchFunction(irfunc, frame) 32 self.ins_sel.munchFunction(irfunc, frame)
33 self.logger.info('Selected instructions', extra={'ppci_frame':frame})
26 34
27 # Do register allocation: 35 # Do register allocation:
28 self.ra.allocFrame(frame) 36 self.ra.allocFrame(frame)
37 self.logger.info('Registers allocated, now adding final glue')
29 # TODO: Peep-hole here? 38 # TODO: Peep-hole here?
30 39
31 # Add label and return and stack adjustment: 40 # Add label and return and stack adjustment:
32 frame.EntryExitGlue3() 41 frame.EntryExitGlue3()
33 42
34 # Materialize the register allocated instructions into a stream of 43 # Materialize the register allocated instructions into a stream of
35 # real instructions. 44 # real instructions.
36 frame.lower_to(outs) 45 frame.lower_to(outs)
46 self.logger.info('Instructions materialized')
37 return frame 47 return frame
38 48
39 def generate(self, ircode, outs): 49 def generate(self, ircode, outs):
40 """ Generate code into output stream """ 50 """ Generate code into output stream """
41 assert isinstance(ircode, Module) 51 assert isinstance(ircode, Module)