Mercurial > lcfOS
annotate python/cortexm3.py @ 271:cf7d5fb7d9c8
Reorganization
author | Windel Bouwman |
---|---|
date | Tue, 20 Aug 2013 18:56:02 +0200 |
parents | 5ec7580976d9 |
children | 6f2423df0675 |
rev | line source |
---|---|
261 | 1 import struct |
2 import types | |
235 | 3 from target import Register, Instruction, Target, Imm8, Label, Imm3, LabelRef, Imm32 |
234 | 4 from asmnodes import ASymbol, ANumber, AUnop, ABinop |
202 | 5 from ppci import CompilerError |
205 | 6 import ir |
202 | 7 |
218 | 8 # TODO: encode this in DSL (domain specific language) |
9 | |
202 | 10 def u16(h): |
11 return struct.pack('<H', h) | |
12 | |
205 | 13 def u32(x): |
14 return struct.pack('<I', x) | |
15 | |
202 | 16 armtarget = Target('arm') |
17 | |
18 class ArmReg(Register): | |
19 def __init__(self, num, name): | |
20 super().__init__(name) | |
21 self.num = num | |
206 | 22 def __repr__(self): |
23 return self.name | |
202 | 24 |
203 | 25 class RegOp: |
26 def __init__(self, num): | |
206 | 27 assert num < 16 |
203 | 28 self.num = num |
29 | |
30 @classmethod | |
31 def Create(cls, vop): | |
32 if type(vop) is ASymbol: | |
33 name = vop.name | |
34 regs = {} | |
35 for r in armtarget.registers: | |
36 regs[r.name] = r | |
37 if name in regs: | |
38 r = regs[name] | |
39 return cls(r.num) | |
234 | 40 |
219 | 41 class Reg8Op: |
42 def __init__(self, num): | |
43 assert num < 8 | |
44 self.num = num | |
45 | |
46 @classmethod | |
47 def Create(cls, vop): | |
48 if type(vop) is ASymbol: | |
49 name = vop.name | |
50 regs = {} | |
51 for r in armtarget.registers: | |
52 regs[r.name] = r | |
53 if name in regs: | |
54 r = regs[name] | |
55 if r.num < 8: | |
56 return cls(r.num) | |
203 | 57 |
206 | 58 def getRegNum(n): |
59 for r in armtarget.registers: | |
60 if r.num == n: | |
61 return r | |
203 | 62 |
206 | 63 def getRegisterRange(n1, n2): |
64 regs = [] | |
65 if n1.num < n2.num: | |
66 for n in range(n1.num, n2.num + 1): | |
67 r = getRegNum(n) | |
68 assert r | |
69 regs.append(r) | |
70 return regs | |
203 | 71 |
224 | 72 def isRegOffset(regname, x, y): |
73 if type(x) is ASymbol and type(y) is ANumber and x.name.upper() == regname: | |
74 return y.number | |
75 elif type(y) is ASymbol and type(x) is ANumber and y.name.upper() == regname: | |
76 return x.number | |
77 | |
78 | |
79 class MemRegXRel: | |
80 def __init__(self, offset): | |
81 assert offset % 4 == 0 | |
212 | 82 self.offset = offset |
83 | |
219 | 84 def __repr__(self): |
224 | 85 return '[{}, #{}]'.format(self.regname, self.offset) |
219 | 86 |
212 | 87 @classmethod |
88 def Create(cls, vop): | |
89 if type(vop) is AUnop and vop.operation == '[]': | |
90 vop = vop.arg # descent | |
224 | 91 offset = isRegOffset(cls.regname, vop.arg1, vop.arg2) |
92 if type(offset) is int: | |
93 if offset % 4 == 0: | |
223 | 94 offset = vop.arg2.number |
95 return cls(offset) | |
224 | 96 elif type(vop) is ASymbol and vop.name.upper() == self.regname: |
223 | 97 return cls(0) |
98 | |
224 | 99 class MemSpRel(MemRegXRel): |
100 regname = 'SP' | |
101 | |
102 class MemPcRel(MemRegXRel): | |
103 regname = 'PC' | |
104 | |
225 | 105 class MemR8Rel: |
219 | 106 def __init__(self, basereg, offset): |
107 assert type(basereg) is ArmReg | |
108 self.basereg = basereg | |
109 self.offset = offset | |
110 | |
111 def __repr__(self): | |
112 return '[{}, #{}]'.format(self.basereg, self.offset) | |
113 | |
114 @classmethod | |
115 def Create(cls, vop): | |
116 if type(vop) is AUnop and vop.operation == '[]': | |
117 vop = vop.arg # descent | |
118 if type(vop) is ABinop: | |
119 if vop.op == '+' and type(vop.arg1) is ASymbol and type(vop.arg2) is ANumber: | |
120 offset = vop.arg2.number | |
121 if offset > 120: | |
122 return | |
123 basereg = Reg8Op.Create(vop.arg1) | |
124 if not basereg: | |
125 return | |
126 else: | |
127 return | |
128 elif type(vop) is ASymbol: | |
129 offset = 0 | |
130 basereg = Reg8Op.Create(vop) | |
131 if not basereg: | |
132 return | |
133 else: | |
134 return | |
135 return cls(getRegNum(basereg.num), offset) | |
212 | 136 |
205 | 137 class RegisterSet: |
138 def __init__(self, regs): | |
206 | 139 assert type(regs) is set |
140 self.regs = regs | |
141 def __repr__(self): | |
142 return ','.join([str(r) for r in self.regs]) | |
143 @classmethod | |
144 def Create(cls, vop): | |
145 assert type(vop) is AUnop and vop.operation == '{}' | |
146 assert type(vop.arg) is list | |
147 regs = set() | |
148 for arg in vop.arg: | |
149 if type(arg) is ASymbol: | |
150 reg = RegOp.Create(arg) | |
151 if not reg: | |
152 return | |
153 regs.add(reg) | |
154 elif type(arg) is ABinop and arg.op == '-': | |
155 reg1 = RegOp.Create(arg.arg1) | |
156 reg2 = RegOp.Create(arg.arg2) | |
157 if not reg1: | |
158 return | |
159 if not reg2: | |
160 return | |
161 for r in getRegisterRange(reg1, reg2): | |
162 regs.add(r) | |
163 else: | |
164 raise Exception('Cannot be') | |
165 return cls(regs) | |
166 | |
167 def registerNumbers(self): | |
168 return [r.num for r in self.regs] | |
205 | 169 |
202 | 170 # 8 bit registers: |
205 | 171 r0 = ArmReg(0, 'r0') |
172 armtarget.registers.append(r0) | |
206 | 173 r1 = ArmReg(1, 'r1') |
174 armtarget.registers.append(r1) | |
175 r2 = ArmReg(2, 'r2') | |
176 armtarget.registers.append(r2) | |
177 r3 = ArmReg(3, 'r3') | |
178 armtarget.registers.append(r3) | |
202 | 179 r4 = ArmReg(4, 'r4') |
180 armtarget.registers.append(r4) | |
203 | 181 r5 = ArmReg(5, 'r5') |
182 armtarget.registers.append(r5) | |
183 r6 = ArmReg(6, 'r6') | |
184 armtarget.registers.append(r6) | |
185 r7 = ArmReg(7, 'r7') | |
186 armtarget.registers.append(r7) | |
206 | 187 # Other registers: |
188 # TODO | |
189 sp = ArmReg(13, 'sp') | |
190 armtarget.registers.append(sp) | |
191 lr = ArmReg(14, 'lr') | |
192 armtarget.registers.append(lr) | |
193 pc = ArmReg(15, 'pc') | |
194 armtarget.registers.append(pc) | |
202 | 195 |
196 class ArmInstruction(Instruction): | |
197 pass | |
198 | |
235 | 199 |
200 @armtarget.instruction | |
205 | 201 class dcd_ins(ArmInstruction): |
202 mnemonic = 'dcd' | |
235 | 203 operands = (Imm32,) |
205 | 204 def __init__(self, expr): |
237 | 205 if isinstance(expr, Imm32): |
206 self.expr = expr.imm | |
207 self.label = None | |
208 elif isinstance(expr, LabelRef): | |
209 self.expr = 0 | |
210 self.label = expr | |
211 else: | |
212 raise NotImplementedError() | |
213 | |
214 def resolve(self, f): | |
215 if self.label: | |
216 self.expr = f(self.label.name) | |
219 | 217 |
205 | 218 def encode(self): |
219 return u32(self.expr) | |
202 | 220 |
219 | 221 def __repr__(self): |
222 return 'DCD 0x{0:X}'.format(self.expr) | |
223 | |
224 | |
225 | |
226 # Memory related | |
227 | |
228 class LS_imm5_base(ArmInstruction): | |
229 """ ??? Rt, [Rn, imm5] """ | |
225 | 230 operands = (Reg8Op, MemR8Rel) |
212 | 231 def __init__(self, rt, memop): |
232 assert memop.offset % 4 == 0 | |
233 self.imm5 = memop.offset >> 2 | |
234 self.rn = memop.basereg.num | |
225 | 235 self.rt = rt |
219 | 236 self.memloc = memop |
237 assert self.rn < 8 | |
225 | 238 assert self.rt.num < 8 |
212 | 239 |
240 def encode(self): | |
241 Rn = self.rn | |
225 | 242 Rt = self.rt.num |
212 | 243 imm5 = self.imm5 |
219 | 244 |
245 h = (self.opcode << 11) | (imm5 << 6) | (Rn << 3) | Rt | |
246 return u16(h) | |
247 def __repr__(self): | |
248 return '{} {}, {}'.format(self.mnemonic, self.rt, self.memloc) | |
249 | |
250 @armtarget.instruction | |
251 class storeimm5_ins(LS_imm5_base): | |
252 mnemonic = 'STR' | |
253 opcode = 0xC | |
254 | |
255 @armtarget.instruction | |
256 class loadimm5_ins(LS_imm5_base): | |
257 mnemonic = 'LDR' | |
258 opcode = 0xD | |
259 | |
260 class ls_sp_base_imm8(ArmInstruction): | |
224 | 261 operands = (Reg8Op, MemSpRel) |
219 | 262 def __init__(self, rt, memop): |
263 self.rt = rt | |
264 self.offset = memop.offset | |
265 | |
266 def encode(self): | |
267 rt = self.rt.num | |
268 assert rt < 8 | |
269 imm8 = self.offset >> 2 | |
270 assert imm8 < 256 | |
271 h = (self.opcode << 8) | (rt << 8) | imm8 | |
212 | 272 return u16(h) |
273 | |
219 | 274 def __repr__(self): |
275 return '{} {}, [sp,#{}]'.format(self.mnemonic, self.rt, self.offset) | |
276 | |
236 | 277 def align(x, m): |
278 while ((x % m) != 0): | |
279 x = x + 1 | |
280 return x | |
281 | |
212 | 282 @armtarget.instruction |
219 | 283 class ldr_pcrel(ArmInstruction): |
284 """ ldr Rt, [PC, imm8], store value into memory """ | |
212 | 285 mnemonic = 'ldr' |
235 | 286 operands = (RegOp, LabelRef) |
219 | 287 def __init__(self, rt, label): |
235 | 288 assert isinstance(label, LabelRef) |
219 | 289 self.rt = rt |
290 self.label = label | |
291 self.offset = 0 | |
212 | 292 |
234 | 293 def resolve(self, f): |
235 | 294 la = f(self.label.name) |
236 | 295 sa = align(self.address + 2, 4) |
296 self.offset = (la - sa) | |
235 | 297 if self.offset < 0: |
298 self.offset = 0 | |
234 | 299 |
212 | 300 def encode(self): |
219 | 301 rt = self.rt.num |
302 assert rt < 8 | |
303 imm8 = self.offset >> 2 | |
304 assert imm8 < 256 | |
235 | 305 assert imm8 >= 0 |
219 | 306 h = (0x9 << 11) | (rt << 8) | imm8 |
212 | 307 return u16(h) |
308 | |
219 | 309 def __repr__(self): |
232 | 310 return 'LDR {}, {}'.format(self.rt, self.label.name) |
219 | 311 |
312 @armtarget.instruction | |
313 class ldr_sprel(ls_sp_base_imm8): | |
314 """ ldr Rt, [SP, imm8] """ | |
315 mnemonic = 'LDR' | |
316 opcode = 0x98 | |
317 | |
318 @armtarget.instruction | |
319 class str_sprel(ls_sp_base_imm8): | |
320 """ str Rt, [SP, imm8] """ | |
321 mnemonic = 'STR' | |
322 opcode = 0x90 | |
323 | |
212 | 324 @armtarget.instruction |
202 | 325 class mov_ins(ArmInstruction): |
326 """ mov Rd, imm8, move immediate value into register """ | |
327 mnemonic = 'mov' | |
203 | 328 opcode = 4 # 00100 Rd(3) imm8 |
329 operands = (RegOp, Imm8) | |
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 | |
262 | 503 |
237 | 504 @armtarget.instruction |
505 class beq_ins(cond_base_ins): | |
219 | 506 mnemonic = 'beq' |
237 | 507 cond = 0 |
508 | |
262 | 509 |
237 | 510 @armtarget.instruction |
262 | 511 class bne_ins(cond_base_ins): |
237 | 512 mnemonic = 'bne' |
513 cond = 1 | |
205 | 514 |
262 | 515 |
516 @armtarget.instruction | |
517 class blt_ins(cond_base_ins): | |
518 mnemonic = 'blt' | |
519 cond = 0b1011 | |
520 | |
521 | |
522 @armtarget.instruction | |
523 class blt_ins(cond_base_ins): | |
524 mnemonic = 'bgt' | |
525 cond = 0b1100 | |
526 | |
527 | |
205 | 528 @armtarget.instruction |
529 class push_ins(ArmInstruction): | |
206 | 530 operands = (RegisterSet,) |
205 | 531 mnemonic = 'push' |
532 def __init__(self, regs): | |
206 | 533 assert (type(regs),) == self.operands, (type(regs),) |
205 | 534 self.regs = regs |
206 | 535 def __repr__(self): |
536 return '{0} {{{1}}}'.format(self.mnemonic, self.regs) | |
205 | 537 def encode(self): |
206 | 538 reg_list = 0 |
539 M = 0 | |
540 for n in self.regs.registerNumbers(): | |
541 if n < 8: | |
542 reg_list |= (1 << n) | |
543 elif n == 14: | |
544 M = 1 | |
545 else: | |
546 raise NotImplementedError('not implemented for this register') | |
547 h = (0x5a << 9) | (M << 8) | reg_list | |
548 return u16(h) | |
205 | 549 |
550 @armtarget.instruction | |
551 class pop_ins(ArmInstruction): | |
206 | 552 operands = (RegisterSet,) |
205 | 553 mnemonic = 'pop' |
554 def __init__(self, regs): | |
555 self.regs = regs | |
207 | 556 def __repr__(self): |
557 return '{0} {{{1}}}'.format(self.mnemonic, self.regs) | |
205 | 558 def encode(self): |
206 | 559 reg_list = 0 |
560 P = 0 | |
561 for n in self.regs.registerNumbers(): | |
562 if n < 8: | |
563 reg_list |= (1 << n) | |
564 elif n == 15: | |
565 P = 1 | |
566 else: | |
567 raise NotImplementedError('not implemented for this register') | |
568 h = (0x5E << 9) | (P << 8) | reg_list | |
569 return u16(h) | |
205 | 570 |
571 @armtarget.instruction | |
202 | 572 class yield_ins(ArmInstruction): |
573 operands = () | |
574 mnemonic = 'yield' | |
575 def encode(self): | |
576 return u16(0xbf10) | |
577 | |
206 | 578 armtarget.check() |
579 |