annotate python/codegen.py @ 292:534b94b40aa8

Fixup reorganize
author Windel Bouwman
date Wed, 27 Nov 2013 08:06:42 +0100
parents 7b38782ed496
children
rev   line source
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
1 import ir
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
2 from target 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
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
6 import registerallocator
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
7
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
8
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
9 class CodeGenerator:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
10 """ Generic code generator """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
11 def __init__(self, target):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
12 # TODO: schedule traces in better order.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
13 # This is optional!
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
14 assert isinstance(target, Target), target
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
15 self.target = target
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
16 self.ins_sel = target.ins_sel
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
17 self.ra = registerallocator.RegisterAllocator()
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
18
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
19 def generateFunc(self, irfunc, outs):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
20 """ Generate code for one function into a frame """
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
21 # Cleanup function:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
22 transform.removeEmptyBlocks(irfunc)
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
23
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
24 # Create a frame for this function:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
25 frame = self.target.FrameClass(irfunc.name)
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
26
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
27 # Canonicalize the intermediate language:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
28 canon.make(irfunc, frame)
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
29 self.ins_sel.munchFunction(irfunc, frame)
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
30
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
31 # Do register allocation:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
32 self.ra.allocFrame(frame)
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
33 # TODO: Peep-hole here?
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
34
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
35 # Add label and return and stack adjustment:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
36 frame.EntryExitGlue3()
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
37
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
38 # Materialize assembly
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
39 # Materialize the register allocated instructions into a stream of
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
40 # real instructions.
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
41 frame.lower_to(outs)
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
42 return frame
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
43
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
44 def generate(self, ircode, outs):
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
45 assert isinstance(ircode, ir.Module)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
46 outs.selectSection('code')
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
47
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
48 # Munch program into a bunch of frames. One frame per function.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
49 # Each frame has a flat list of abstract instructions.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
50 # Generate code for all functions:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
51 self.frames = [self.generateFunc(func, outs) for func in ircode.Functions]
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
52 return self.frames