205
|
1 import struct, types
|
206
|
2 from target import Register, Instruction, Target, Imm8, Label, Imm3
|
219
|
3 from asmnodes import ASymbol, ANumber, AUnop, ABinop, ALabel
|
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)
|
219
|
39
|
|
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
|
205
|
198 class dcd_ins(ArmInstruction):
|
|
199 mnemonic = 'dcd'
|
|
200 def __init__(self, expr):
|
|
201 self.expr = expr
|
219
|
202
|
205
|
203 def encode(self):
|
|
204 return u32(self.expr)
|
202
|
205
|
219
|
206 def __repr__(self):
|
|
207 return 'DCD 0x{0:X}'.format(self.expr)
|
|
208
|
|
209
|
|
210
|
|
211 # Memory related
|
|
212
|
|
213 class LS_imm5_base(ArmInstruction):
|
|
214 """ ??? Rt, [Rn, imm5] """
|
225
|
215 operands = (Reg8Op, MemR8Rel)
|
212
|
216 def __init__(self, rt, memop):
|
|
217 assert memop.offset % 4 == 0
|
|
218 self.imm5 = memop.offset >> 2
|
|
219 self.rn = memop.basereg.num
|
225
|
220 self.rt = rt
|
219
|
221 self.memloc = memop
|
|
222 assert self.rn < 8
|
225
|
223 assert self.rt.num < 8
|
212
|
224
|
|
225 def encode(self):
|
|
226 Rn = self.rn
|
225
|
227 Rt = self.rt.num
|
212
|
228 imm5 = self.imm5
|
219
|
229
|
|
230 h = (self.opcode << 11) | (imm5 << 6) | (Rn << 3) | Rt
|
|
231 return u16(h)
|
|
232 def __repr__(self):
|
|
233 return '{} {}, {}'.format(self.mnemonic, self.rt, self.memloc)
|
|
234
|
|
235 @armtarget.instruction
|
|
236 class storeimm5_ins(LS_imm5_base):
|
|
237 mnemonic = 'STR'
|
|
238 opcode = 0xC
|
|
239
|
|
240 @armtarget.instruction
|
|
241 class loadimm5_ins(LS_imm5_base):
|
|
242 mnemonic = 'LDR'
|
|
243 opcode = 0xD
|
|
244
|
|
245 class ls_sp_base_imm8(ArmInstruction):
|
224
|
246 operands = (Reg8Op, MemSpRel)
|
219
|
247 def __init__(self, rt, memop):
|
|
248 self.rt = rt
|
|
249 self.offset = memop.offset
|
|
250
|
|
251 def encode(self):
|
|
252 rt = self.rt.num
|
|
253 assert rt < 8
|
|
254 imm8 = self.offset >> 2
|
|
255 assert imm8 < 256
|
|
256 h = (self.opcode << 8) | (rt << 8) | imm8
|
212
|
257 return u16(h)
|
|
258
|
219
|
259 def __repr__(self):
|
|
260 return '{} {}, [sp,#{}]'.format(self.mnemonic, self.rt, self.offset)
|
|
261
|
212
|
262 @armtarget.instruction
|
219
|
263 class ldr_pcrel(ArmInstruction):
|
|
264 """ ldr Rt, [PC, imm8], store value into memory """
|
212
|
265 mnemonic = 'ldr'
|
224
|
266 operands = (RegOp, MemPcRel)
|
219
|
267 def __init__(self, rt, label):
|
|
268 self.rt = rt
|
|
269 self.label = label
|
|
270 self.offset = 0
|
212
|
271
|
|
272 def encode(self):
|
219
|
273 rt = self.rt.num
|
|
274 assert rt < 8
|
|
275 imm8 = self.offset >> 2
|
|
276 assert imm8 < 256
|
|
277 h = (0x9 << 11) | (rt << 8) | imm8
|
212
|
278 return u16(h)
|
|
279
|
219
|
280 def __repr__(self):
|
232
|
281 return 'LDR {}, {}'.format(self.rt, self.label.name)
|
219
|
282
|
|
283 @armtarget.instruction
|
|
284 class ldr_sprel(ls_sp_base_imm8):
|
|
285 """ ldr Rt, [SP, imm8] """
|
|
286 mnemonic = 'LDR'
|
|
287 opcode = 0x98
|
|
288
|
|
289 @armtarget.instruction
|
|
290 class str_sprel(ls_sp_base_imm8):
|
|
291 """ str Rt, [SP, imm8] """
|
|
292 mnemonic = 'STR'
|
|
293 opcode = 0x90
|
|
294
|
212
|
295 @armtarget.instruction
|
202
|
296 class mov_ins(ArmInstruction):
|
|
297 """ mov Rd, imm8, move immediate value into register """
|
|
298 mnemonic = 'mov'
|
203
|
299 opcode = 4 # 00100 Rd(3) imm8
|
|
300 operands = (RegOp, Imm8)
|
205
|
301 irpattern = ir.ImmLoad
|
203
|
302 def __init__(self, rd, imm):
|
|
303 self.imm = imm.imm
|
|
304 self.r = rd.num
|
205
|
305
|
202
|
306 def encode(self):
|
|
307 rd = self.r
|
|
308 opcode = self.opcode
|
|
309 imm8 = self.imm
|
|
310 h = (opcode << 11) | (rd << 8) | imm8
|
|
311 return u16(h)
|
219
|
312 def __repr__(self):
|
|
313 return 'MOV {0}, xx?'.format(self.r)
|
232
|
314
|
|
315
|
203
|
316 @armtarget.instruction
|
|
317 class movregreg_ins(ArmInstruction):
|
|
318 """ mov Rd, Rm """
|
|
319 mnemonic = 'mov'
|
|
320 operands = (RegOp, RegOp)
|
|
321 def __init__(self, rd, rm):
|
|
322 self.rd = rd
|
|
323 self.rm = rm
|
|
324 def encode(self):
|
|
325 rd = self.rd.num
|
|
326 D = (rd & 0x8) >> 3
|
|
327 assert D < 2
|
|
328 rd = rd & 0x7
|
|
329 rm = self.rm.num
|
|
330 assert rm < 16
|
|
331 opcode = self.opcode
|
219
|
332 h = (1 << 14) | (3 << 9) | (D << 7) | (rm << 3) | rd
|
203
|
333 return u16(h)
|
|
334
|
219
|
335
|
|
336
|
|
337 # Arithmatics:
|
|
338
|
203
|
339 @armtarget.instruction
|
|
340 class addregregimm3_ins(ArmInstruction):
|
|
341 """ add Rd, Rn, imm3 """
|
|
342 mnemonic = 'add'
|
|
343 opcode = 3 # 00011
|
|
344 operands = (RegOp, RegOp, Imm3)
|
205
|
345 irpattern = 3
|
203
|
346 def __init__(self, rd, rn, imm3):
|
|
347 self.rd = rd
|
|
348 self.rn = rn
|
|
349 self.imm3 = imm3
|
|
350 def encode(self):
|
|
351 rd = self.rd.num
|
|
352 rn = self.rn.num
|
|
353 imm3 = self.imm3.imm
|
|
354 opcode = self.opcode
|
|
355 h = (opcode << 11) | (1 << 10) | (imm3 << 6) | (rn << 3) | rd
|
|
356 return u16(h)
|
|
357
|
219
|
358 class regregreg_base(ArmInstruction):
|
|
359 """ ??? Rd, Rn, Rm """
|
|
360 operands = (Reg8Op, Reg8Op, Reg8Op)
|
|
361 def __init__(self, rd, rn, rm):
|
|
362 self.rd = rd
|
|
363 self.rn = rn
|
|
364 self.rm = rm
|
|
365 def encode(self):
|
|
366 rd = self.rd.num
|
|
367 rn = self.rn.num
|
|
368 rm = self.rm.num
|
|
369 h = (self.opcode << 9) | (rm << 6) | (rn << 3) | rd
|
|
370 return u16(h)
|
|
371 def __repr__(self):
|
|
372 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
|
|
373
|
|
374 @armtarget.instruction
|
|
375 class addregs_ins(regregreg_base):
|
|
376 mnemonic = 'ADD'
|
|
377 opcode = 0b0001100
|
|
378
|
|
379 @armtarget.instruction
|
|
380 class subregs_ins(regregreg_base):
|
|
381 mnemonic = 'SUB'
|
|
382 opcode = 0b0001101
|
|
383
|
|
384 class regreg_base(ArmInstruction):
|
|
385 """ ??? Rdn, Rm """
|
|
386 operands = (Reg8Op, Reg8Op)
|
|
387 def __init__(self, rdn, rm):
|
|
388 self.rdn = rdn
|
|
389 self.rm = rm
|
|
390 def encode(self):
|
|
391 rdn = self.rdn.num
|
|
392 rm = self.rm.num
|
|
393 h = (self.opcode << 6) | (rm << 3) | rdn
|
|
394 return u16(h)
|
|
395 def __repr__(self):
|
|
396 return '{} {}, {}'.format(self.mnemonic, self.rdn, self.rm)
|
|
397
|
|
398 @armtarget.instruction
|
|
399 class andregs_ins(regreg_base):
|
|
400 mnemonic = 'AND'
|
|
401 opcode = 0b0100000000
|
|
402
|
|
403 @armtarget.instruction
|
|
404 class orrregs_ins(regreg_base):
|
|
405 mnemonic = 'ORR'
|
|
406 opcode = 0b0100001100
|
|
407
|
|
408 @armtarget.instruction
|
|
409 class cmp_ins(regreg_base):
|
|
410 mnemonic = 'CMP'
|
|
411 opcode = 0b0100001010
|
|
412
|
203
|
413 @armtarget.instruction
|
232
|
414 class lslregs_ins(regreg_base):
|
|
415 mnemonic = 'LSL'
|
|
416 opcode = 0b0100000010
|
|
417
|
|
418 @armtarget.instruction
|
203
|
419 class cmpregimm8_ins(ArmInstruction):
|
|
420 """ cmp Rn, imm8 """
|
|
421 mnemonic = 'cmp'
|
|
422 opcode = 5 # 00101
|
|
423 operands = (RegOp, Imm8)
|
|
424 def __init__(self, rn, imm):
|
|
425 self.rn = rn
|
|
426 self.imm = imm
|
|
427 def encode(self):
|
|
428 rn = self.rn.num
|
|
429 imm = self.imm.imm
|
|
430 opcode = self.opcode
|
|
431 h = (opcode << 11) | (rn << 8) | imm
|
|
432 return u16(h)
|
202
|
433
|
219
|
434 # Jumping:
|
218
|
435
|
|
436 @armtarget.instruction
|
205
|
437 class jmp_ins(ArmInstruction):
|
219
|
438 operands = (ALabel,)
|
205
|
439 mnemonic = 'jmp'
|
|
440 def __init__(self, target_label):
|
219
|
441 assert type(target_label) is ALabel
|
205
|
442 self.target = target_label
|
|
443 def fixUp(self):
|
|
444 pass
|
|
445 def encode(self):
|
219
|
446 h = 0 # TODO
|
205
|
447 return u16(h)
|
219
|
448 def __repr__(self):
|
|
449 return 'B {0}'.format(self.target.name)
|
|
450
|
|
451 @armtarget.instruction
|
|
452 class beq_ins(ArmInstruction):
|
|
453 operands = (ALabel,)
|
|
454 mnemonic = 'beq'
|
|
455 def __init__(self, target_label):
|
|
456 assert type(target_label) is ALabel
|
|
457 self.target = target_label
|
|
458 def fixUp(self):
|
|
459 pass
|
|
460 def encode(self):
|
|
461 h = 0 # TODO
|
|
462 return u16(h)
|
|
463 def __repr__(self):
|
|
464 return 'BEQ {0}'.format(self.target.name)
|
205
|
465
|
|
466 @armtarget.instruction
|
|
467 class push_ins(ArmInstruction):
|
206
|
468 operands = (RegisterSet,)
|
205
|
469 mnemonic = 'push'
|
|
470 def __init__(self, regs):
|
206
|
471 assert (type(regs),) == self.operands, (type(regs),)
|
205
|
472 self.regs = regs
|
206
|
473 def __repr__(self):
|
|
474 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
|
205
|
475 def encode(self):
|
206
|
476 reg_list = 0
|
|
477 M = 0
|
|
478 for n in self.regs.registerNumbers():
|
|
479 if n < 8:
|
|
480 reg_list |= (1 << n)
|
|
481 elif n == 14:
|
|
482 M = 1
|
|
483 else:
|
|
484 raise NotImplementedError('not implemented for this register')
|
|
485 h = (0x5a << 9) | (M << 8) | reg_list
|
|
486 return u16(h)
|
205
|
487
|
|
488 @armtarget.instruction
|
|
489 class pop_ins(ArmInstruction):
|
206
|
490 operands = (RegisterSet,)
|
205
|
491 mnemonic = 'pop'
|
|
492 def __init__(self, regs):
|
|
493 self.regs = regs
|
207
|
494 def __repr__(self):
|
|
495 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
|
205
|
496 def encode(self):
|
206
|
497 reg_list = 0
|
|
498 P = 0
|
|
499 for n in self.regs.registerNumbers():
|
|
500 if n < 8:
|
|
501 reg_list |= (1 << n)
|
|
502 elif n == 15:
|
|
503 P = 1
|
|
504 else:
|
|
505 raise NotImplementedError('not implemented for this register')
|
|
506 h = (0x5E << 9) | (P << 8) | reg_list
|
|
507 return u16(h)
|
205
|
508
|
|
509 @armtarget.instruction
|
202
|
510 class yield_ins(ArmInstruction):
|
|
511 operands = ()
|
|
512 mnemonic = 'yield'
|
|
513 def encode(self):
|
|
514 return u16(0xbf10)
|
|
515
|
206
|
516 armtarget.check()
|
|
517
|