annotate python/codegen.py @ 290:7b38782ed496

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