255
|
1 import logging
|
211
|
2 import ir
|
249
|
3 from target import Label, Comment, Alignment, LabelRef, Imm32, DebugInfo
|
218
|
4 import cortexm3 as arm
|
211
|
5 from ppci import CompilerError
|
261
|
6 import irmach
|
211
|
7
|
262
|
8
|
|
9 class InstructionSelector:
|
268
|
10 def newTmp(self):
|
|
11 return 't999'
|
|
12
|
|
13 def munchProgram(self, p):
|
|
14 assert isinstance(p, ir.Module)
|
|
15 self.result = []
|
|
16 for f in p.Functions:
|
|
17 for bb in f.BasicBlocks:
|
|
18 for i in bb.Instructions:
|
|
19 self.munchStm(i)
|
|
20 return self.result
|
|
21
|
|
22 def emit(self, *args, **kwargs):
|
|
23 """ Abstract instruction emitter """
|
|
24 i = irmach.AbstractInstruction(*args, **kwargs)
|
|
25 self.result.append(i)
|
|
26
|
|
27 def munchStm(self, s):
|
|
28 raise NotImplementedError()
|
|
29
|
|
30 def munchExpr(self, e):
|
|
31 raise NotImplementedError()
|
262
|
32
|
|
33
|
|
34 class RegisterAllocator:
|
268
|
35 """ Target independent register allocator """
|
262
|
36 pass
|
|
37
|
|
38
|
268
|
39 class ArmInstructionSelector(InstructionSelector):
|
|
40 def munchExpr(self, e):
|
|
41 if isinstance(e, ir.Alloc):
|
|
42 return 0
|
|
43 elif isinstance(e, ir.Binop) and e.operation == '+':
|
|
44 a = self.munchExpr(e.value1)
|
|
45 b = self.munchExpr(e.value2)
|
|
46 d = self.newTmp()
|
|
47 self.emit('add %d0, %s0, %s1', dst=[d], src=[a, b])
|
|
48 return d
|
|
49 elif isinstance(e, ir.Binop) and e.operation == '|':
|
|
50 a = self.munchExpr(e.value1)
|
|
51 b = self.munchExpr(e.value2)
|
|
52 d = self.newTmp()
|
|
53 self.emit('orrrr %d0, %s0, %s1', dst=[d], src=[a, b])
|
|
54 return d
|
|
55 elif isinstance(e, ir.Binop) and e.operation == '<<':
|
|
56 a = self.munchExpr(e.value1)
|
|
57 b = self.munchExpr(e.value2)
|
|
58 d = self.newTmp()
|
|
59 self.emit('lsl %d0, %s0, %s1', dst=[d], src=[a, b])
|
|
60 return d
|
|
61 elif isinstance(e, ir.Binop) and e.operation == '*':
|
|
62 a = self.munchExpr(e.value1)
|
|
63 b = self.munchExpr(e.value2)
|
|
64 d = self.newTmp()
|
|
65 self.emit('mylll %d0, %s0, %s1', dst=[d], src=[a, b])
|
|
66 return d
|
|
67 elif isinstance(e, ir.Const):
|
|
68 d = self.newTmp()
|
|
69 if e.value < 256:
|
|
70 self.emit('ldr %d0, {}'.format(e.value), dst=[d])
|
|
71 else:
|
|
72 self.emit('ldrpcrel TODO')
|
|
73 return d
|
|
74 elif isinstance(e, ir.Mem):
|
|
75 # Load from memory
|
|
76 loc = self.munchExpr(e.e)
|
|
77 d = self.newTmp()
|
|
78 self.emit('ldr %d0, [%s0]', src=[loc], dst=[d])
|
|
79 return d
|
|
80 elif isinstance(e, ir.Temp):
|
|
81 return e
|
|
82 else:
|
|
83 raise NotImplementedError('--> {}'.format(e))
|
|
84
|
|
85 def munchStm(self, s):
|
|
86 if isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem):
|
|
87 memloc = self.munchExpr(s.dst.e)
|
|
88 val = self.munchExpr(s.src)
|
|
89 self.emit('str [%s0], %s1')
|
|
90 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Temp):
|
|
91 val = self.munchExpr(s.src)
|
|
92 self.emit('str %d0, %s0', dst=[s.dst], src=[val])
|
|
93 elif isinstance(s, ir.Return):
|
|
94 self.emit('ret')
|
|
95 elif isinstance(s, ir.Jump):
|
|
96 self.emit('jmp {}'.format(s))
|
|
97 elif isinstance(s, ir.CJump):
|
|
98 self.munchExpr(s.a)
|
|
99 self.munchExpr(s.b)
|
|
100 self.emit('jmp {}'.format(s))
|
|
101 else:
|
|
102 raise NotImplementedError('--> {}'.format(s))
|
|
103
|
|
104
|
211
|
105 class ArmCodeGenerator:
|
268
|
106 def __init__(self, outs):
|
|
107 self.ins_sel = ArmInstructionSelector()
|
|
108 self.outs = outs
|
|
109 self.outs.getSection('code').address = 0x08000000
|
|
110 self.outs.getSection('data').address = 0x20000000
|
|
111
|
|
112 def generate(self, ircode):
|
|
113 self.ins_sel.munchProgram(ircode)
|
|
114
|
|
115
|
|
116 class ArmCodeGenerator_old:
|
219
|
117 """
|
|
118 Simple code generator
|
|
119 Ad hoc implementation
|
|
120 """
|
211
|
121 def __init__(self, out):
|
|
122 self.outs = out
|
258
|
123 self.logger = logging.getLogger('codegenarm')
|
211
|
124
|
|
125 def emit(self, item):
|
|
126 self.outs.emit(item)
|
|
127
|
|
128 def generate(self, ircode):
|
|
129 assert isinstance(ircode, ir.Module)
|
255
|
130 self.logger.info('Generating arm code for {}'.format(ircode.name))
|
261
|
131 self.available_regs = {arm.r3, arm.r4, arm.r5, arm.r6, arm.r7}
|
258
|
132 self.regmap = {}
|
250
|
133 # TODO: get these from linker descriptor?
|
|
134 self.outs.getSection('code').address = 0x08000000
|
|
135 self.outs.getSection('data').address = 0x20000000
|
211
|
136 self.outs.selectSection('data')
|
|
137
|
|
138 for gvar in ircode.Variables:
|
235
|
139 self.emit(Label(gvar.name))
|
211
|
140 # TODO: use initial value:
|
235
|
141 self.dcd(0)
|
211
|
142
|
219
|
143 self.imms = [] # list with immediates relative to PC.
|
211
|
144 self.outs.selectSection('code')
|
232
|
145
|
|
146 # Manually inserted startup code:
|
235
|
147 self.dcd(0x20000678) # initial stack ptr
|
237
|
148 # TODO: use label here:
|
|
149 #self.emit(arm.dcd_ins(LabelRef('reset'))) # reset vector
|
|
150 self.dcd(0x08000009) # reset vector, lsb indicates thumb mode
|
259
|
151 self.emit(arm.bl_ins(LabelRef('main')))
|
232
|
152
|
237
|
153 self.emit(Label('reset'))
|
211
|
154 for f in ircode.Functions:
|
240
|
155 self.localVars = []
|
219
|
156 # Add global variable addresses to immediate list:
|
|
157 for gvar in ircode.Variables:
|
|
158 pass #self.imms.append((
|
|
159
|
|
160 self.stack_frame = []
|
234
|
161 self.emit(Label(f.name))
|
218
|
162 # Save some registers:
|
261
|
163 self.emit(arm.push_ins(arm.RegisterSet({arm.r3, arm.r4, arm.r5, arm.r6,arm.r7,arm.lr})))
|
211
|
164 for bb in f.BasicBlocks:
|
234
|
165 self.emit(Label(bb.name))
|
211
|
166 for ins in bb.Instructions:
|
|
167 self.generateInstruction(ins)
|
|
168
|
235
|
169 self.align()
|
219
|
170 while self.imms:
|
|
171 l, v = self.imms.pop()
|
234
|
172 self.emit(Label(l))
|
235
|
173 self.dcd(v)
|
|
174 self.align()
|
237
|
175 self.outs.backpatch()
|
|
176 self.outs.backpatch()
|
258
|
177 codesize = self.outs.getSection('code').Size
|
|
178 self.logger.info('Generated {} bytes code'.format(codesize))
|
219
|
179
|
235
|
180 def dcd(self, x):
|
|
181 self.emit(arm.dcd_ins(Imm32(x)))
|
|
182
|
|
183 def align(self):
|
|
184 self.outs.emit(Alignment(4))
|
249
|
185
|
225
|
186 # Helper functions:
|
219
|
187 def getStack(self, v):
|
|
188 off = self.stack_frame.index(v)
|
|
189 return off * 4
|
249
|
190
|
219
|
191 def addStack(self, v):
|
|
192 self.stack_frame.append(v)
|
|
193 return self.getStack(v)
|
249
|
194
|
219
|
195 def getGlobal(self, r, g):
|
|
196 _global_address = g.name + '__global'
|
235
|
197 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
|
249
|
198
|
225
|
199 def loadStack(self, reg, val):
|
|
200 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
|
249
|
201
|
258
|
202 def getreg(self, v):
|
|
203 if not v in self.regmap:
|
|
204 self.regmap[v] = self.available_regs.pop()
|
|
205 return self.regmap[v]
|
|
206
|
|
207 def freereg(self, v, ins):
|
|
208 if v.lastUse(ins):
|
|
209 r = self.regmap.pop(v)
|
|
210 assert r not in self.regmap.values()
|
|
211 self.available_regs.add(r)
|
|
212
|
225
|
213 def comment(self, txt):
|
234
|
214 self.emit(Comment(txt))
|
219
|
215
|
249
|
216 def debugInfo(self, loc):
|
|
217 if loc:
|
|
218 self.emit(DebugInfo(loc))
|
|
219
|
211
|
220 def generateInstruction(self, ins):
|
232
|
221 self.comment(str(ins))
|
249
|
222 if hasattr(ins, 'debugLoc'):
|
|
223 self.debugInfo(ins.debugLoc)
|
211
|
224 if type(ins) is ir.Branch:
|
237
|
225 tgt = LabelRef(ins.target.name)
|
|
226 self.emit(arm.b_ins(tgt))
|
219
|
227 elif type(ins) is ir.ImmLoad:
|
|
228 lname = ins.target.name + '_ivalue'
|
258
|
229 r0 = self.getreg(ins.target)
|
|
230 self.emit(arm.ldr_pcrel(r0, LabelRef(lname)))
|
219
|
231 self.imms.append((lname, ins.value))
|
211
|
232 elif type(ins) is ir.Store:
|
219
|
233 # Load value in r0:
|
258
|
234 r0 = self.getreg(ins.value)
|
219
|
235 # store in memory:
|
232
|
236 # TODO: split globals and locals??
|
|
237 #self.getGlobal(arm.r1, ins.location)
|
240
|
238 # Horrible hack with localVars
|
|
239 if ins.location in self.localVars:
|
243
|
240 # The value was alloc'ed
|
258
|
241 self.emit(arm.str_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
|
240
|
242 else:
|
258
|
243 r1 = self.getreg(ins.location)
|
|
244 self.emit(arm.storeimm5_ins(r0, arm.MemR8Rel(r1, 0)))
|
259
|
245 self.freereg(ins.location, ins)
|
258
|
246 self.freereg(ins.value, ins)
|
211
|
247 elif type(ins) is ir.Load:
|
232
|
248 # TODO: differ global and local??
|
|
249 #self.getGlobal(arm.r0, ins.location)
|
258
|
250 r0 = self.getreg(ins.value)
|
240
|
251 if ins.location in self.localVars:
|
258
|
252 self.emit(arm.ldr_sprel(r0, arm.MemSpRel(self.getStack(ins.location))))
|
240
|
253 else:
|
258
|
254 r2 = self.getreg(ins.location)
|
|
255 self.emit(arm.loadimm5_ins(r0, arm.MemR8Rel(r2, 0)))
|
259
|
256 self.freereg(ins.location, ins)
|
211
|
257 elif type(ins) is ir.BinaryOperator:
|
219
|
258 # Load operands:
|
258
|
259 r0 = self.getreg(ins.value1)
|
|
260 r1 = self.getreg(ins.value2)
|
|
261 r2 = self.getreg(ins.result)
|
219
|
262 # do operation:
|
|
263 if ins.operation == '+':
|
258
|
264 self.emit(arm.addregs_ins(r2, r0, r1))
|
232
|
265 elif ins.operation == '<<':
|
258
|
266 self.emit(arm.movregreg_ins(r2, r0))
|
|
267 self.emit(arm.lslregs_ins(r2, r1))
|
232
|
268 elif ins.operation == '|':
|
258
|
269 self.emit(arm.movregreg_ins(r2, r0))
|
|
270 self.emit(arm.orrregs_ins(r2, r1))
|
219
|
271 else:
|
237
|
272 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
|
258
|
273 self.freereg(ins.value1, ins)
|
|
274 self.freereg(ins.value2, ins)
|
259
|
275 elif type(ins) is ir.Call:
|
|
276 # TODO: prep parameters:
|
|
277 self.emit(arm.bl_ins(LabelRef(ins.callee.name)))
|
219
|
278 elif type(ins) is ir.Return:
|
261
|
279 self.emit(arm.pop_ins(arm.RegisterSet({arm.r3, arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
|
212
|
280 elif type(ins) is ir.ConditionalBranch:
|
258
|
281 r0 = self.getreg(ins.a)
|
|
282 r1 = self.getreg(ins.b)
|
|
283 self.emit(arm.cmp_ins(r1, r0))
|
262
|
284 tgt_yes = LabelRef(ins.lab1.name)
|
219
|
285 if ins.cond == '==':
|
|
286 self.emit(arm.beq_ins(tgt_yes))
|
262
|
287 elif ins.cond == '<':
|
|
288 self.emit(arm.blt_ins(tgt_yes))
|
|
289 elif ins.cond == '>':
|
|
290 self.emit(arm.bgt_ins(tgt_yes))
|
219
|
291 else:
|
237
|
292 raise NotImplementedError('"{}" not covered'.format(ins.cond))
|
262
|
293 tgt_no = LabelRef(ins.lab2.name)
|
|
294 self.emit(arm.b_ins(tgt_no))
|
258
|
295 self.freereg(ins.a, ins)
|
|
296 self.freereg(ins.b, ins)
|
222
|
297 elif type(ins) is ir.Alloc:
|
232
|
298 # Local variables are added to stack
|
222
|
299 self.addStack(ins.value)
|
240
|
300 self.localVars.append(ins.value)
|
|
301 # load address into variable:
|
211
|
302 else:
|
237
|
303 raise NotImplementedError('IR "{}" not covered'.format(ins))
|
211
|
304
|
|
305
|