annotate 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
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
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
4 from .canon import make as canonicalize
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
5 from .registerallocator import RegisterAllocator
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
6
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
7
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
8 class CodeGenerator:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
9 """ Generic code generator """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
10 def __init__(self, target):
290
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!
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
13 assert isinstance(target, Target), target
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
14 self.target = target
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
15 self.ins_sel = target.ins_sel
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
16 self.ra = RegisterAllocator()
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
17
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
18 def generateFunc(self, irfunc, outs):
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
19 """ Generate code for one function into a frame """
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
20 # Create a frame for this function:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
21 frame = self.target.FrameClass(irfunc.name)
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
22
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
23 # Canonicalize the intermediate language:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
24 canonicalize(irfunc, frame)
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
25 self.ins_sel.munchFunction(irfunc, frame)
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
26
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
27 # Do register allocation:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
28 self.ra.allocFrame(frame)
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
29 # TODO: Peep-hole here?
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
30
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
31 # Add label and return and stack adjustment:
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
32 frame.EntryExitGlue3()
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
33
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
34 # Materialize the register allocated instructions into a stream of
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
35 # real instructions.
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
36 frame.lower_to(outs)
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
37 return frame
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
38
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
39 def generate(self, ircode, outs):
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
40 """ Generate code into output stream """
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
41 assert isinstance(ircode, ir.Module)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
42 outs.selectSection('code')
210
67b0feafe5ae Added missing file
Windel Bouwman
parents:
diff changeset
43
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
44 # Munch program into a bunch of frames. One frame per function.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
45 # Each frame has a flat list of abstract instructions.
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
46 # Generate code for all functions:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 292
diff changeset
47 self.frames = [self.generateFunc(f, outs) for f in ircode.Functions]
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
48 return self.frames