comparison python/ppci/codegen/codegen.py @ 301:6753763d3bec

merge codegen into ppci package
author Windel Bouwman
date Thu, 05 Dec 2013 17:02:38 +0100
parents python/codegen/codegen.py@9417caea2eb3
children 2c9768114877
comparison
equal deleted inserted replaced
300:158068af716c 301:6753763d3bec
1 from ..ir import Module
2 from target import Target
3 from ppci import CompilerError
4 from .canon import make as canonicalize
5 from .registerallocator import RegisterAllocator
6
7
8 class CodeGenerator:
9 """ Generic code generator """
10 def __init__(self, target):
11 # TODO: schedule traces in better order.
12 # This is optional!
13 assert isinstance(target, Target), target
14 self.target = target
15 self.ins_sel = target.ins_sel
16 self.ra = RegisterAllocator()
17
18 def generateFunc(self, irfunc, outs):
19 """ Generate code for one function into a frame """
20 # Create a frame for this function:
21 frame = self.target.FrameClass(irfunc.name)
22
23 # Canonicalize the intermediate language:
24 canonicalize(irfunc, frame)
25 self.ins_sel.munchFunction(irfunc, frame)
26
27 # Do register allocation:
28 self.ra.allocFrame(frame)
29 # TODO: Peep-hole here?
30
31 # Add label and return and stack adjustment:
32 frame.EntryExitGlue3()
33
34 # Materialize the register allocated instructions into a stream of
35 # real instructions.
36 frame.lower_to(outs)
37 return frame
38
39 def generate(self, ircode, outs):
40 """ Generate code into output stream """
41 assert isinstance(ircode, Module)
42 outs.selectSection('code')
43
44 # Munch program into a bunch of frames. One frame per function.
45 # Each frame has a flat list of abstract instructions.
46 # Generate code for all functions:
47 self.frames = [self.generateFunc(f, outs) for f in ircode.Functions]
48 return self.frames