Mercurial > lcfOS
annotate python/cortexm3.py @ 258:04c19282a5aa
Added register allocator
author | Windel Bouwman |
---|---|
date | Mon, 05 Aug 2013 19:46:11 +0200 |
parents | 6ed3d3a82a63 |
children | 444b9df2ed99 |
rev | line source |
---|---|
205 | 1 import struct, types |
235 | 2 from target import Register, Instruction, Target, Imm8, Label, Imm3, LabelRef, Imm32 |
234 | 3 from asmnodes import ASymbol, ANumber, AUnop, ABinop |
202 | 4 from ppci import CompilerError |
205 | 5 import ir |
202 | 6 |
218 | 7 # TODO: encode this in DSL (domain specific language) |
8 | |
202 | 9 def u16(h): |
10 return struct.pack('<H', h) | |
11 | |
205 | 12 def u32(x): |
13 return struct.pack('<I', x) | |
14 | |
202 | 15 armtarget = Target('arm') |
16 | |
17 class ArmReg(Register): | |
18 def __init__(self, num, name): | |
19 super().__init__(name) | |
20 self.num = num | |
206 | 21 def __repr__(self): |
22 return self.name | |
202 | 23 |
203 | 24 class RegOp: |
25 def __init__(self, num): | |
206 | 26 assert num < 16 |
203 | 27 self.num = num |
28 | |
29 @classmethod | |
30 def Create(cls, vop): | |
31 if type(vop) is ASymbol: | |
32 name = vop.name | |
33 regs = {} | |
34 for r in armtarget.registers: | |
35 regs[r.name] = r | |
36 if name in regs: | |
37 r = regs[name] | |
38 return cls(r.num) | |
234 | 39 |
219 | 40 class Reg8Op: |
41 def __init__(self, num): | |
42 assert num < 8 | |
43 self.num = num | |
44 | |
45 @classmethod | |
46 def Create(cls, vop): | |
47 if type(vop) is ASymbol: | |
48 name = vop.name | |
49 regs = {} | |
50 for r in armtarget.registers: | |
51 regs[r.name] = r | |
52 if name in regs: | |
53 r = regs[name] | |
54 if r.num < 8: | |
55 return cls(r.num) | |
203 | 56 |
206 | 57 def getRegNum(n): |
58 for r in armtarget.registers: | |
59 if r.num == n: | |
60 return r | |
203 | 61 |
206 | 62 def getRegisterRange(n1, n2): |
63 regs = [] | |
64 if n1.num < n2.num: | |
65 for n in range(n1.num, n2.num + 1): | |
66 r = getRegNum(n) | |
67 assert r | |
68 regs.append(r) | |
69 return regs | |
203 | 70 |
224 | 71 def isRegOffset(regname, x, y): |
72 if type(x) is ASymbol and type(y) is ANumber and x.name.upper() == regname: | |
73 return y.number | |
74 elif type(y) is ASymbol and type(x) is ANumber and y.name.upper() == regname: | |
75 return x.number | |
76 | |
77 | |
78 class MemRegXRel: | |
79 def __init__(self, offset): | |
80 assert offset % 4 == 0 | |
212 | 81 self.offset = offset |
82 | |
219 | 83 def __repr__(self): |
224 | 84 return '[{}, #{}]'.format(self.regname, self.offset) |
219 | 85 |
212 | 86 @classmethod |
87 def Create(cls, vop): | |
88 if type(vop) is AUnop and vop.operation == '[]': | |
89 vop = vop.arg # descent | |
224 | 90 offset = isRegOffset(cls.regname, vop.arg1, vop.arg2) |
91 if type(offset) is int: | |
92 if offset % 4 == 0: | |
223 | 93 offset = vop.arg2.number |
94 return cls(offset) | |
224 | 95 elif type(vop) is ASymbol and vop.name.upper() == self.regname: |
223 | 96 return cls(0) |
97 | |
224 | 98 class MemSpRel(MemRegXRel): |
99 regname = 'SP' | |
100 | |
101 class MemPcRel(MemRegXRel): | |
102 regname = 'PC' | |
103 | |
225 | 104 class MemR8Rel: |
219 | 105 def __init__(self, basereg, offset): |
106 assert type(basereg) is ArmReg | |
107 self.basereg = basereg | |
108 self.offset = offset | |
109 | |
110 def __repr__(self): | |
111 return '[{}, #{}]'.format(self.basereg, self.offset) | |
112 | |
113 @classmethod | |
114 def Create(cls, vop): | |
115 if type(vop) is AUnop and vop.operation == '[]': | |
116 vop = vop.arg # descent | |
117 if type(vop) is ABinop: | |
118 if vop.op == '+' and type(vop.arg1) is ASymbol and type(vop.arg2) is ANumber: | |
119 offset = vop.arg2.number | |
120 if offset > 120: | |
121 return | |
122 basereg = Reg8Op.Create(vop.arg1) | |
123 if not basereg: | |
124 return | |
125 else: | |
126 return | |
127 elif type(vop) is ASymbol: | |
128 offset = 0 | |
129 basereg = Reg8Op.Create(vop) | |
130 if not basereg: | |
131 return | |
132 else: | |
133 return | |
134 return cls(getRegNum(basereg.num), offset) | |
212 | 135 |
205 | 136 class RegisterSet: |
137 def __init__(self, regs): | |
206 | 138 assert type(regs) is set |
139 self.regs = regs | |
140 def __repr__(self): | |
141 return ','.join([str(r) for r in self.regs]) | |
142 @classmethod | |
143 def Create(cls, vop): | |
144 assert type(vop) is AUnop and vop.operation == '{}' | |
145 assert type(vop.arg) is list | |
146 regs = set() | |
147 for arg in vop.arg: | |
148 if type(arg) is ASymbol: | |
149 reg = RegOp.Create(arg) | |
150 if not reg: | |
151 return | |
152 regs.add(reg) | |
153 elif type(arg) is ABinop and arg.op == '-': | |
154 reg1 = RegOp.Create(arg.arg1) | |
155 reg2 = RegOp.Create(arg.arg2) | |
156 if not reg1: | |
157 return | |
158 if not reg2: | |
159 return | |
160 for r in getRegisterRange(reg1, reg2): | |
161 regs.add(r) | |
162 else: | |
163 raise Exception('Cannot be') | |
164 return cls(regs) | |
165 | |
166 def registerNumbers(self): | |
167 return [r.num for r in self.regs] | |
205 | 168 |
202 | 169 # 8 bit registers: |
205 | 170 r0 = ArmReg(0, 'r0') |
171 armtarget.registers.append(r0) | |
206 | 172 r1 = ArmReg(1, 'r1') |
173 armtarget.registers.append(r1) | |
174 r2 = ArmReg(2, 'r2') | |
175 armtarget.registers.append(r2) | |
176 r3 = ArmReg(3, 'r3') | |
177 armtarget.registers.append(r3) | |
202 | 178 r4 = ArmReg(4, 'r4') |
179 armtarget.registers.append(r4) | |
203 | 180 r5 = ArmReg(5, 'r5') |
181 armtarget.registers.append(r5) | |
182 r6 = ArmReg(6, 'r6') | |
183 armtarget.registers.append(r6) | |
184 r7 = ArmReg(7, 'r7') | |
185 armtarget.registers.append(r7) | |
206 | 186 # Other registers: |
187 # TODO | |
188 sp = ArmReg(13, 'sp') | |
189 armtarget.registers.append(sp) | |
190 lr = ArmReg(14, 'lr') | |
191 armtarget.registers.append(lr) | |
192 pc = ArmReg(15, 'pc') | |
193 armtarget.registers.append(pc) | |
202 | 194 |
195 class ArmInstruction(Instruction): | |
196 pass | |
197 | |
235 | 198 |
199 @armtarget.instruction | |
205 | 200 class dcd_ins(ArmInstruction): |
201 mnemonic = 'dcd' | |
235 | 202 operands = (Imm32,) |
205 | 203 def __init__(self, expr): |
237 | 204 if isinstance(expr, Imm32): |
205 self.expr = expr.imm | |
206 self.label = None | |
207 elif isinstance(expr, LabelRef): | |
208 self.expr = 0 | |
209 self.label = expr | |
210 else: | |
211 raise NotImplementedError() | |
212 | |
213 def resolve(self, f): | |
214 if self.label: | |
215 self.expr = f(self.label.name) | |
219 | 216 |
205 | 217 def encode(self): |
218 return u32(self.expr) | |
202 | 219 |
219 | 220 def __repr__(self): |
221 return 'DCD 0x{0:X}'.format(self.expr) | |
222 | |
223 | |
224 | |
225 # Memory related | |
226 | |
227 class LS_imm5_base(ArmInstruction): | |
228 """ ??? Rt, [Rn, imm5] """ | |
225 | 229 operands = (Reg8Op, MemR8Rel) |
212 | 230 def __init__(self, rt, memop): |
231 assert memop.offset % 4 == 0 | |
232 self.imm5 = memop.offset >> 2 | |
233 self.rn = memop.basereg.num | |
225 | 234 self.rt = rt |
219 | 235 self.memloc = memop |
236 assert self.rn < 8 | |
225 | 237 assert self.rt.num < 8 |
212 | 238 |
239 def encode(self): | |
240 Rn = self.rn | |
225 | 241 Rt = self.rt.num |
212 | 242 imm5 = self.imm5 |
219 | 243 |
244 h = (self.opcode << 11) | (imm5 << 6) | (Rn << 3) | Rt | |
245 return u16(h) | |
246 def __repr__(self): | |
247 return '{} {}, {}'.format(self.mnemonic, self.rt, self.memloc) | |
248 | |
249 @armtarget.instruction | |
250 class storeimm5_ins(LS_imm5_base): | |
251 mnemonic = 'STR' | |
252 opcode = 0xC | |
253 | |
254 @armtarget.instruction | |
255 class loadimm5_ins(LS_imm5_base): | |
256 mnemonic = 'LDR' | |
257 opcode = 0xD | |
258 | |
259 class ls_sp_base_imm8(ArmInstruction): | |
224 | 260 operands = (Reg8Op, MemSpRel) |
219 | 261 def __init__(self, rt, memop): |
262 self.rt = rt | |
263 self.offset = memop.offset | |
264 | |
265 def encode(self): | |
266 rt = self.rt.num | |
267 assert rt < 8 | |
268 imm8 = self.offset >> 2 | |
269 assert imm8 < 256 | |
270 h = (self.opcode << 8) | (rt << 8) | imm8 | |
212 | 271 return u16(h) |
272 | |
219 | 273 def __repr__(self): |
274 return '{} {}, [sp,#{}]'.format(self.mnemonic, self.rt, self.offset) | |
275 | |
236 | 276 def align(x, m): |
277 while ((x % m) != 0): | |
278 x = x + 1 | |
279 return x | |
280 | |
212 | 281 @armtarget.instruction |
219 | 282 class ldr_pcrel(ArmInstruction): |
283 """ ldr Rt, [PC, imm8], store value into memory """ | |
212 | 284 mnemonic = 'ldr' |
235 | 285 operands = (RegOp, LabelRef) |
219 | 286 def __init__(self, rt, label): |
235 | 287 assert isinstance(label, LabelRef) |
219 | 288 self.rt = rt |
289 self.label = label | |
290 self.offset = 0 | |
212 | 291 |
234 | 292 def resolve(self, f): |
235 | 293 la = f(self.label.name) |
236 | 294 sa = align(self.address + 2, 4) |
295 self.offset = (la - sa) | |
235 | 296 if self.offset < 0: |
297 self.offset = 0 | |
234 | 298 |
212 | 299 def encode(self): |
219 | 300 rt = self.rt.num |
301 assert rt < 8 | |
302 imm8 = self.offset >> 2 | |
303 assert imm8 < 256 | |
235 | 304 assert imm8 >= 0 |
219 | 305 h = (0x9 << 11) | (rt << 8) | imm8 |
212 | 306 return u16(h) |
307 | |
219 | 308 def __repr__(self): |
232 | 309 return 'LDR {}, {}'.format(self.rt, self.label.name) |
219 | 310 |
311 @armtarget.instruction | |
312 class ldr_sprel(ls_sp_base_imm8): | |
313 """ ldr Rt, [SP, imm8] """ | |
314 mnemonic = 'LDR' | |
315 opcode = 0x98 | |
316 | |
317 @armtarget.instruction | |
318 class str_sprel(ls_sp_base_imm8): | |
319 """ str Rt, [SP, imm8] """ | |
320 mnemonic = 'STR' | |
321 opcode = 0x90 | |
322 | |
212 | 323 @armtarget.instruction |
202 | 324 class mov_ins(ArmInstruction): |
325 """ mov Rd, imm8, move immediate value into register """ | |
326 mnemonic = 'mov' | |
203 | 327 opcode = 4 # 00100 Rd(3) imm8 |
328 operands = (RegOp, Imm8) | |
205 | 329 irpattern = ir.ImmLoad |
203 | 330 def __init__(self, rd, imm): |
331 self.imm = imm.imm | |
332 self.r = rd.num | |
205 | 333 |
202 | 334 def encode(self): |
335 rd = self.r | |
336 opcode = self.opcode | |
337 imm8 = self.imm | |
338 h = (opcode << 11) | (rd << 8) | imm8 | |
339 return u16(h) | |
219 | 340 def __repr__(self): |
341 return 'MOV {0}, xx?'.format(self.r) | |
232 | 342 |
343 | |
203 | 344 |
219 | 345 |
346 | |
347 # Arithmatics: | |
348 | |
203 | 349 @armtarget.instruction |
350 class addregregimm3_ins(ArmInstruction): | |
351 """ add Rd, Rn, imm3 """ | |
352 mnemonic = 'add' | |
353 opcode = 3 # 00011 | |
354 operands = (RegOp, RegOp, Imm3) | |
205 | 355 irpattern = 3 |
203 | 356 def __init__(self, rd, rn, imm3): |
357 self.rd = rd | |
358 self.rn = rn | |
359 self.imm3 = imm3 | |
360 def encode(self): | |
361 rd = self.rd.num | |
362 rn = self.rn.num | |
363 imm3 = self.imm3.imm | |
364 opcode = self.opcode | |
365 h = (opcode << 11) | (1 << 10) | (imm3 << 6) | (rn << 3) | rd | |
366 return u16(h) | |
367 | |
219 | 368 class regregreg_base(ArmInstruction): |
369 """ ??? Rd, Rn, Rm """ | |
370 operands = (Reg8Op, Reg8Op, Reg8Op) | |
371 def __init__(self, rd, rn, rm): | |
372 self.rd = rd | |
373 self.rn = rn | |
374 self.rm = rm | |
375 def encode(self): | |
376 rd = self.rd.num | |
377 rn = self.rn.num | |
378 rm = self.rm.num | |
379 h = (self.opcode << 9) | (rm << 6) | (rn << 3) | rd | |
380 return u16(h) | |
381 def __repr__(self): | |
382 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm) | |
383 | |
384 @armtarget.instruction | |
385 class addregs_ins(regregreg_base): | |
386 mnemonic = 'ADD' | |
387 opcode = 0b0001100 | |
388 | |
389 @armtarget.instruction | |
390 class subregs_ins(regregreg_base): | |
391 mnemonic = 'SUB' | |
392 opcode = 0b0001101 | |
393 | |
394 class regreg_base(ArmInstruction): | |
395 """ ??? Rdn, Rm """ | |
396 operands = (Reg8Op, Reg8Op) | |
397 def __init__(self, rdn, rm): | |
398 self.rdn = rdn | |
399 self.rm = rm | |
400 def encode(self): | |
401 rdn = self.rdn.num | |
402 rm = self.rm.num | |
403 h = (self.opcode << 6) | (rm << 3) | rdn | |
404 return u16(h) | |
405 def __repr__(self): | |
406 return '{} {}, {}'.format(self.mnemonic, self.rdn, self.rm) | |
407 | |
408 @armtarget.instruction | |
258 | 409 class movregreg_ins(regreg_base): |
410 """ mov Rd, Rm """ | |
411 mnemonic = 'mov' | |
412 opcode = 0 | |
413 | |
414 @armtarget.instruction | |
219 | 415 class andregs_ins(regreg_base): |
416 mnemonic = 'AND' | |
417 opcode = 0b0100000000 | |
418 | |
419 @armtarget.instruction | |
420 class orrregs_ins(regreg_base): | |
421 mnemonic = 'ORR' | |
422 opcode = 0b0100001100 | |
423 | |
424 @armtarget.instruction | |
425 class cmp_ins(regreg_base): | |
426 mnemonic = 'CMP' | |
427 opcode = 0b0100001010 | |
428 | |
203 | 429 @armtarget.instruction |
232 | 430 class lslregs_ins(regreg_base): |
431 mnemonic = 'LSL' | |
432 opcode = 0b0100000010 | |
433 | |
434 @armtarget.instruction | |
203 | 435 class cmpregimm8_ins(ArmInstruction): |
436 """ cmp Rn, imm8 """ | |
437 mnemonic = 'cmp' | |
438 opcode = 5 # 00101 | |
439 operands = (RegOp, Imm8) | |
440 def __init__(self, rn, imm): | |
441 self.rn = rn | |
442 self.imm = imm | |
443 def encode(self): | |
444 rn = self.rn.num | |
445 imm = self.imm.imm | |
446 opcode = self.opcode | |
447 h = (opcode << 11) | (rn << 8) | imm | |
448 return u16(h) | |
202 | 449 |
219 | 450 # Jumping: |
218 | 451 |
238 | 452 def wrap_negative(x, bits): |
453 b = struct.unpack('<I', struct.pack('<i', x))[0] | |
454 mask = (1 << bits) - 1 | |
455 return b & mask | |
456 | |
237 | 457 class jumpBase_ins(ArmInstruction): |
458 operands = (LabelRef,) | |
205 | 459 def __init__(self, target_label): |
237 | 460 assert type(target_label) is LabelRef |
205 | 461 self.target = target_label |
237 | 462 self.offset = 0 |
463 | |
464 def resolve(self, f): | |
465 la = f(self.target.name) | |
238 | 466 sa = self.address + 4 |
237 | 467 self.offset = (la - sa) |
238 | 468 #if self.offset < 0: |
469 # # TODO: handle negative jump | |
470 # self.offset = 0 | |
237 | 471 |
219 | 472 def __repr__(self): |
237 | 473 return '{} {}'.format(self.mnemonic, self.target.name) |
219 | 474 |
475 @armtarget.instruction | |
237 | 476 class b_ins(jumpBase_ins): |
477 mnemonic = 'B' | |
478 def encode(self): | |
238 | 479 imm11 = wrap_negative(self.offset >> 1, 11) |
480 h = (0b11100 << 11) | imm11 # | 1 # 1 to enable thumb mode | |
237 | 481 return u16(h) |
482 | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
483 @armtarget.instruction |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
484 class bl_ins(jumpBase_ins): |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
485 mnemonic = 'BL' |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
486 def encode(self): |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
487 imm32 = wrap_negative(self.offset >> 1, 32) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
488 imm11 = imm32 & 0x7FF |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
489 imm10 = (imm32 >> 11) & 0x3FF |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
490 j1 = 1 # TODO: what do these mean? |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
491 j2 = 1 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
492 s = (imm32 >> 24) & 0x1 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
493 h1 = (0b11110 << 11) | (s << 10) | imm10 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
494 h2 = (0b1101 << 12) | (j1 << 13) | (j2 << 11) | imm11 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
495 return u16(h1) + u16(h2) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
238
diff
changeset
|
496 |
237 | 497 class cond_base_ins(jumpBase_ins): |
498 def encode(self): | |
238 | 499 imm8 = wrap_negative(self.offset >> 1, 8) |
237 | 500 h = (0b1101 << 12) | (self.cond << 8) | imm8 |
501 return u16(h) | |
502 | |
503 @armtarget.instruction | |
504 class beq_ins(cond_base_ins): | |
219 | 505 mnemonic = 'beq' |
237 | 506 cond = 0 |
507 | |
508 @armtarget.instruction | |
509 class beq_ins(cond_base_ins): | |
510 mnemonic = 'bne' | |
511 cond = 1 | |
205 | 512 |
513 @armtarget.instruction | |
514 class push_ins(ArmInstruction): | |
206 | 515 operands = (RegisterSet,) |
205 | 516 mnemonic = 'push' |
517 def __init__(self, regs): | |
206 | 518 assert (type(regs),) == self.operands, (type(regs),) |
205 | 519 self.regs = regs |
206 | 520 def __repr__(self): |
521 return '{0} {{{1}}}'.format(self.mnemonic, self.regs) | |
205 | 522 def encode(self): |
206 | 523 reg_list = 0 |
524 M = 0 | |
525 for n in self.regs.registerNumbers(): | |
526 if n < 8: | |
527 reg_list |= (1 << n) | |
528 elif n == 14: | |
529 M = 1 | |
530 else: | |
531 raise NotImplementedError('not implemented for this register') | |
532 h = (0x5a << 9) | (M << 8) | reg_list | |
533 return u16(h) | |
205 | 534 |
535 @armtarget.instruction | |
536 class pop_ins(ArmInstruction): | |
206 | 537 operands = (RegisterSet,) |
205 | 538 mnemonic = 'pop' |
539 def __init__(self, regs): | |
540 self.regs = regs | |
207 | 541 def __repr__(self): |
542 return '{0} {{{1}}}'.format(self.mnemonic, self.regs) | |
205 | 543 def encode(self): |
206 | 544 reg_list = 0 |
545 P = 0 | |
546 for n in self.regs.registerNumbers(): | |
547 if n < 8: | |
548 reg_list |= (1 << n) | |
549 elif n == 15: | |
550 P = 1 | |
551 else: | |
552 raise NotImplementedError('not implemented for this register') | |
553 h = (0x5E << 9) | (P << 8) | reg_list | |
554 return u16(h) | |
205 | 555 |
556 @armtarget.instruction | |
202 | 557 class yield_ins(ArmInstruction): |
558 operands = () | |
559 mnemonic = 'yield' | |
560 def encode(self): | |
561 return u16(0xbf10) | |
562 | |
206 | 563 armtarget.check() |
564 |