annotate python/codegenarm.py @ 212:62386bcee1ba

Added parser combinator lib
author Windel Bouwman
date Sun, 30 Jun 2013 19:00:41 +0200
parents 99164160fb0b
children 494828a7adf1
rev   line source
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
1 import ir
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
2 from asmnodes import ALabel
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
3 import arm_cm3 as arm
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
4 from ppci import CompilerError
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
5
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
6 class ArmCodeGenerator:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
7 """ Simple code generator """
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
8 def __init__(self, out):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
9 self.outs = out
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
10
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
11 def emit(self, item):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
12 self.outs.emit(item)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
13
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
14 def generate(self, ircode):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
15 assert isinstance(ircode, ir.Module)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
16 print('ARM code generation')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
17 self.outs.selectSection('data')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
18
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
19 for gvar in ircode.Variables:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
20 self.emit(ALabel(gvar.name))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
21 # TODO: use initial value:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
22 self.emit(arm.dcd_ins(0))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
23
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
24 self.outs.selectSection('code')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
25 for f in ircode.Functions:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
26 self.emit(ALabel(f.name))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
27 self.emit(arm.push_ins(arm.RegisterSet({arm.r2, arm.r3,arm.lr})))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
28 for bb in f.BasicBlocks:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
29 self.emit(ALabel(bb.name))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
30 for ins in bb.Instructions:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
31 self.generateInstruction(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
32
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
33 def generateInstruction(self, ins):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
34 if type(ins) is ir.Branch:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
35 self.emit(arm.jmp_ins(ins.target))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
36 elif type(ins) is ir.ImmLoad and ins.value < 255:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
37 self.emit(arm.mov_ins(arm.r0, arm.Imm8(ins.value)))
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
38 # determine stack frame..
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
39 self.emit(arm.mov_ins(arm.r1, arm.Imm8(9)))
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
40 #self.emit(arm.
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
41 elif type(ins) is ir.ImmLoad and ins.value < (2**32):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
42 print(ins)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
43 elif type(ins) is ir.Store:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
44 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
45 elif type(ins) is ir.Return:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
46 self.emit(arm.pop_ins(arm.RegisterSet({arm.r2, arm.r3, arm.pc})))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
47 elif type(ins) is ir.Load:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
48 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
49 elif type(ins) is ir.BinaryOperator:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
50 print(ins)
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
51 elif type(ins) is ir.ConditionalBranch:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
52 print(ins)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
53 else:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
54 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
55 raise CompilerError('IR "{}" not covered'.format(ins))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
56
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
57