Mercurial > lcfOS
comparison python/codegen.py @ 292:534b94b40aa8
Fixup reorganize
author | Windel Bouwman |
---|---|
date | Wed, 27 Nov 2013 08:06:42 +0100 |
parents | 7b38782ed496 |
children |
comparison
equal
deleted
inserted
replaced
290:7b38782ed496 | 292:534b94b40aa8 |
---|---|
1 import ir | 1 import ir |
2 import target | 2 from target import Target |
3 from ppci import CompilerError | 3 from ppci import CompilerError |
4 import transform | 4 import transform |
5 import canon | 5 import canon |
6 import registerallocator | |
6 | 7 |
7 | 8 |
8 # TODO: this class could be target independent: | |
9 class CodeGenerator: | 9 class CodeGenerator: |
10 def __init__(self, outs, target): | 10 """ Generic code generator """ |
11 def __init__(self, target): | |
11 # TODO: schedule traces in better order. | 12 # TODO: schedule traces in better order. |
12 # This is optional! | 13 # This is optional! |
13 assert isinstance(tg, target.Target) | 14 assert isinstance(target, Target), target |
14 self.target = target | 15 self.target = target |
15 self.ins_sel = ArmInstructionSelector() | 16 self.ins_sel = target.ins_sel |
16 self.ra = registerallocator.RegisterAllocator() | 17 self.ra = registerallocator.RegisterAllocator() |
17 self.outs = outs | |
18 self.outs.getSection('code').address = 0x08000000 | |
19 self.outs.getSection('data').address = 0x20000000 | |
20 | 18 |
21 def generateFunc(self, irfunc): | 19 def generateFunc(self, irfunc, outs): |
22 """ Generate code for one function into a frame """ | 20 """ Generate code for one function into a frame """ |
23 # Cleanup function: | 21 # Cleanup function: |
24 transform.removeEmptyBlocks(irfunc) | 22 transform.removeEmptyBlocks(irfunc) |
25 | 23 |
26 # Create a frame for this function: | 24 # Create a frame for this function: |
27 frame = ArmFrame(irfunc.name) | 25 frame = self.target.FrameClass(irfunc.name) |
28 | 26 |
29 # Canonicalize the intermediate language: | 27 # Canonicalize the intermediate language: |
30 canon.make(irfunc, frame) | 28 canon.make(irfunc, frame) |
31 self.ins_sel.munchFunction(irfunc, frame) | 29 self.ins_sel.munchFunction(irfunc, frame) |
32 | 30 |
33 # Do register allocation: | 31 # Do register allocation: |
34 self.ra.allocFrame(frame) | 32 self.ra.allocFrame(frame) |
35 # TODO: Peep-hole here? | 33 # TODO: Peep-hole here? |
36 | 34 |
37 # Can we materialize here?? | |
38 | |
39 # Add label and return and stack adjustment: | 35 # Add label and return and stack adjustment: |
40 frame.EntryExitGlue3() | 36 frame.EntryExitGlue3() |
41 | 37 |
42 # Materialize assembly | 38 # Materialize assembly |
43 # Materialize the register allocated instructions into a stream of | 39 # Materialize the register allocated instructions into a stream of |
44 # real instructions. | 40 # real instructions. |
45 frame.lower_to(self.outs) | 41 frame.lower_to(outs) |
46 return frame | 42 return frame |
47 | 43 |
48 def generate(self, ircode): | 44 def generate(self, ircode, outs): |
49 assert isinstance(ircode, ir.Module) | 45 assert isinstance(ircode, ir.Module) |
50 self.outs.selectSection('code') | 46 outs.selectSection('code') |
51 # assembly glue to make it work: | |
52 # TODO: this must be in source code, not in compiler | |
53 self.outs.emit(arm.dcd_ins(Imm32(0x20000678))) # initial SP | |
54 self.outs.emit(arm.dcd_ins(Imm32(0x08000009))) # reset vector | |
55 self.outs.emit(arm.b_ins(LabelRef('main'))) | |
56 | 47 |
57 # Munch program into a bunch of frames. One frame per function. | 48 # Munch program into a bunch of frames. One frame per function. |
58 # Each frame has a flat list of abstract instructions. | 49 # Each frame has a flat list of abstract instructions. |
59 # Generate code for all functions: | 50 # Generate code for all functions: |
60 self.frames = [self.generateFunc(func) for func in ircode.Functions] | 51 self.frames = [self.generateFunc(func, outs) for func in ircode.Functions] |
61 | |
62 # TODO: fixup references, do this in another way? | |
63 self.outs.backpatch() | |
64 self.outs.backpatch() # Why two times? | |
65 return self.frames | 52 return self.frames |