annotate python/codegenarm.py @ 211:99164160fb0b

Added another missing file
author Windel Bouwman
date Sat, 29 Jun 2013 10:10:45 +0200
parents
children 62386bcee1ba
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)))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
38 elif type(ins) is ir.Store:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
39 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
40 elif type(ins) is ir.Return:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
41 self.emit(arm.pop_ins(arm.RegisterSet({arm.r2, arm.r3, arm.pc})))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
42 elif type(ins) is ir.Load:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
43 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
44 elif type(ins) is ir.BinaryOperator:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
45 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
46 else:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
47 print(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
48 raise CompilerError('IR "{}" not covered'.format(ins))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
49
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
50