annotate python/codegenarm.py @ 232:e621e3ba78d2

Added left shift instruction
author Windel Bouwman
date Sun, 14 Jul 2013 11:50:58 +0200
parents 1c7364bd74c7
children 83781bd10fdb
rev   line source
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
1 import ir
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
2 from asmnodes import ALabel, AComment
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
3 import cortexm3 as arm
211
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:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
7 """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
8 Simple code generator
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
9 Ad hoc implementation
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
10 """
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
11 def __init__(self, out):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
12 self.outs = out
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
13
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
14 def emit(self, item):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
15 self.outs.emit(item)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
16
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
17 def generate(self, ircode):
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
18 assert isinstance(ircode, ir.Module)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
19 print('ARM code generation')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
20 self.outs.selectSection('data')
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
21
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
22 for gvar in ircode.Variables:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
23 self.emit(ALabel(gvar.name))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
24 # TODO: use initial value:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
25 self.emit(arm.dcd_ins(0))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
26
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
27 self.imms = [] # list with immediates relative to PC.
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
28 self.outs.selectSection('code')
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
29
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
30 # Manually inserted startup code:
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
31 self.emit(arm.dcd_ins(0x20000678)) # initial stack ptr
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
32 self.emit(arm.dcd_ins(0x08000401)) # reset vector
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
33
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
34 for f in ircode.Functions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
35 # Add global variable addresses to immediate list:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
36 for gvar in ircode.Variables:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
37 pass #self.imms.append((
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
38
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
39 self.stack_frame = []
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
40 self.emit(ALabel(f.name))
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
41 # Save some registers:
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
42 self.emit(arm.push_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6,arm.r7,arm.lr})))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
43 for bb in f.BasicBlocks:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
44 self.emit(ALabel(bb.name))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
45 for ins in bb.Instructions:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
46 self.generateInstruction(ins)
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
47
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
48 self.outs.align(4)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
49 while self.imms:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
50 l, v = self.imms.pop()
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
51 self.emit(ALabel(l))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
52 self.emit(arm.dcd_ins(v))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
53 self.outs.align(4)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
54
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
55 # Helper functions:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
56 def getStack(self, v):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
57 off = self.stack_frame.index(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
58 return off * 4
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
59 def addStack(self, v):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
60 self.stack_frame.append(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
61 return self.getStack(v)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
62 def getGlobal(self, r, g):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
63 _global_address = g.name + '__global'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
64 self.emit(arm.ldr_pcrel(r, ALabel(_global_address)))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
65 def loadStack(self, reg, val):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
66 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
67 def comment(self, txt):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
68 self.emit(AComment(txt))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
69
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
70 def generateInstruction(self, ins):
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
71 self.comment(str(ins))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
72 if type(ins) is ir.Branch:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
73 tgt = ALabel(ins.target.name)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
74 self.emit(arm.jmp_ins(tgt))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
75 elif type(ins) is ir.ImmLoad:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
76 lname = ins.target.name + '_ivalue'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
77 self.emit(arm.ldr_pcrel(arm.r0, ALabel(lname)))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
78 self.imms.append((lname, ins.value))
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
79 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target))))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
80 elif type(ins) is ir.Store:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
81 # Load value in r0:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
82 self.loadStack(arm.r0, ins.value)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
83 # store in memory:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
84 # TODO: split globals and locals??
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
85 #self.getGlobal(arm.r1, ins.location)
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
86 self.loadStack(arm.r1, ins.location)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
87 self.emit(arm.storeimm5_ins(arm.r0, arm.MemR8Rel(arm.r1, 0)))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
88 elif type(ins) is ir.Load:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
89 # TODO: differ global and local??
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
90 #self.getGlobal(arm.r0, ins.location)
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
91 self.loadStack(arm.r0, ins.location)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
92 self.emit(arm.loadimm5_ins(arm.r0, arm.MemR8Rel(arm.r0, 0)))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
93 # Store value on stack:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
94 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.value))))
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
95 elif type(ins) is ir.BinaryOperator:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
96 # Load operands:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
97 self.loadStack(arm.r0, ins.value1)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
98 self.loadStack(arm.r1, ins.value2)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
99 # do operation:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
100 if ins.operation == '+':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
101 self.emit(arm.addregs_ins(arm.r0, arm.r0, arm.r1))
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
102 elif ins.operation == '<<':
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
103 self.emit(arm.lslregs_ins(arm.r0, arm.r1))
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
104 elif ins.operation == '|':
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
105 self.emit(arm.orrregs_ins(arm.r0, arm.r1))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
106 else:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
107 print('operation not implemented', ins.operation)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
108 # Store value back:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
109 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.result))))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
110 elif type(ins) is ir.Return:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
111 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 211
diff changeset
112 elif type(ins) is ir.ConditionalBranch:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
113 self.loadStack(arm.r0, ins.a)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
114 self.loadStack(arm.r1, ins.b)
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
115 self.emit(arm.cmp_ins(arm.r1, arm.r0))
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
116 tgt_yes = ALabel(ins.lab1.name)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
117 if ins.cond == '==':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
118 self.emit(arm.beq_ins(tgt_yes))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
119 else:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
120 print('TODO', ins.cond)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
121 tgt_no = ALabel(ins.lab2.name)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
122 self.emit(arm.jmp_ins(tgt_no))
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 219
diff changeset
123 elif type(ins) is ir.Alloc:
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
124 # Local variables are added to stack
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 219
diff changeset
125 self.addStack(ins.value)
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
126 else:
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
127 raise CompilerError('IR "{}" not covered'.format(ins))
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
128
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
129