Mercurial > lcfOS
comparison python/codegen/codegen.py @ 296:9417caea2eb3
Directorized some backend files
author | Windel Bouwman |
---|---|
date | Sun, 01 Dec 2013 13:36:58 +0100 |
parents | python/codegen.py@534b94b40aa8 |
children |
comparison
equal
deleted
inserted
replaced
295:917eab04b8b7 | 296:9417caea2eb3 |
---|---|
1 import ir | |
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, ir.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 |