342
|
1
|
364
|
2 from ..basetarget import Instruction, LabelAddress
|
346
|
3 from ...bitfun import rotate_left
|
342
|
4
|
|
5 from .token import ArmToken
|
345
|
6 from .registers import R0, SP, ArmRegister
|
|
7
|
|
8
|
|
9 def encode_imm32(v):
|
|
10 """ Bundle 32 bit value into 4 bits rotation and 8 bits value
|
|
11 """
|
|
12 for i in range(0, 16):
|
|
13 v2 = rotate_left(v, i*2)
|
|
14 if (v2 & 0xFFFFFF00) == 0:
|
|
15 rotation = i
|
|
16 val = v2 & 0xFF
|
|
17 x = (rotation << 8) | val
|
|
18 return x
|
|
19 raise Exception("Invalid value {}".format(v))
|
342
|
20
|
|
21 # Instructions:
|
|
22
|
|
23 class ArmInstruction(Instruction):
|
|
24 def __init__(self):
|
|
25 self.token = ArmToken()
|
|
26
|
|
27
|
353
|
28 class ConstantData(ArmInstruction):
|
346
|
29 def __init__(self, v):
|
|
30 super().__init__()
|
353
|
31 assert isinstance(v, int)
|
346
|
32 self.v = v
|
|
33
|
353
|
34
|
364
|
35 class Dcd(ArmInstruction):
|
|
36 def __init__(self, v):
|
|
37 super().__init__()
|
|
38 assert isinstance(v, int) or isinstance(v, LabelAddress)
|
|
39 self.v = v
|
|
40
|
346
|
41 def encode(self):
|
364
|
42 if type(self.v) is int:
|
|
43 self.token[0:32] = self.v
|
|
44 else:
|
|
45 self.token[0:32] = 0
|
346
|
46 return self.token.encode()
|
|
47
|
364
|
48 def relocations(self):
|
|
49 if type(self.v) is LabelAddress:
|
|
50 return [(self.v.name, 'absaddr32')]
|
|
51 return []
|
|
52
|
350
|
53 def __repr__(self):
|
364
|
54 if type(self.v) is int:
|
|
55 return 'DCD {}'.format(hex(self.v))
|
|
56 else:
|
|
57 return 'DCD ={}'.format(self.v.name)
|
350
|
58
|
346
|
59
|
353
|
60 class Db(ConstantData):
|
|
61 def encode(self):
|
|
62 assert self.v < 256
|
|
63 return bytes([self.v])
|
|
64
|
|
65 def __repr__(self):
|
|
66 return 'DB {}'.format(hex(self.v))
|
|
67
|
|
68
|
346
|
69 def Mov(*args):
|
|
70 if len(args) == 2:
|
|
71 if isinstance(args[1], int):
|
|
72 return Mov1(*args)
|
|
73 elif isinstance(args[1], ArmRegister):
|
|
74 return Mov2(*args)
|
|
75 raise Exception()
|
|
76
|
|
77
|
|
78 class Mov1(ArmInstruction):
|
342
|
79 """ Mov Rd, imm16 """
|
|
80 def __init__(self, reg, imm):
|
|
81 super().__init__()
|
346
|
82 assert type(imm) is int
|
342
|
83 self.reg = reg
|
|
84 self.imm = imm
|
|
85
|
|
86 def encode(self):
|
346
|
87 self.token[0:12] = encode_imm32(self.imm)
|
345
|
88 self.token.Rd = self.reg.num
|
342
|
89 self.token[16:20] = 0
|
346
|
90 self.token[20] = 0 # Set flags
|
342
|
91 self.token[21:28] = 0b0011101
|
346
|
92 self.token.cond = AL
|
342
|
93 return self.token.encode()
|
|
94
|
|
95 def __repr__(self):
|
345
|
96 return 'Mov {}, {}'.format(self.reg, self.imm)
|
|
97
|
|
98
|
346
|
99 class Mov2(ArmInstruction):
|
|
100 def __init__(self, rd, rm):
|
|
101 super().__init__()
|
|
102 self.rd = rd
|
|
103 self.rm = rm
|
|
104
|
|
105 def encode(self):
|
|
106 self.token[0:4] = self.rm.num
|
|
107 self.token[4:12] = 0
|
|
108 self.token[12:16] = self.rd.num
|
|
109 self.token[16:20] = 0
|
|
110 self.token.S = 0
|
|
111 self.token[21:28] = 0xD
|
|
112 self.token.cond = AL
|
|
113 return self.token.encode()
|
|
114
|
350
|
115 def __repr__(self):
|
|
116 return 'MOV {}, {}'.format(self.rd, self.rm)
|
|
117
|
346
|
118
|
352
|
119 def Cmp(*args):
|
|
120 if len(args) == 2:
|
|
121 if isinstance(args[1], int):
|
|
122 return Cmp1(*args)
|
|
123 elif isinstance(args[1], ArmRegister):
|
|
124 return Cmp2(*args)
|
|
125 raise Exception()
|
|
126
|
|
127
|
|
128 class Cmp1(ArmInstruction):
|
|
129 """ CMP Rn, imm """
|
|
130 def __init__(self, reg, imm):
|
|
131 super().__init__()
|
|
132 assert type(imm) is int
|
|
133 self.reg = reg
|
|
134 self.imm = imm
|
|
135
|
|
136 def encode(self):
|
|
137 self.token[0:12] = encode_imm32(self.imm)
|
|
138 self.token.Rn = self.reg.num
|
|
139 self.token[20:28] = 0b00110101
|
|
140 self.token.cond = AL
|
|
141 return self.token.encode()
|
|
142
|
|
143 def __repr__(self):
|
|
144 return 'CMP {}, {}'.format(self.reg, self.imm)
|
|
145
|
|
146
|
|
147 class Cmp2(ArmInstruction):
|
|
148 """ CMP Rn, Rm """
|
|
149 def __init__(self, rn, rm):
|
|
150 super().__init__()
|
|
151 self.rn = rn
|
|
152 self.rm = rm
|
|
153
|
|
154 def encode(self):
|
|
155 self.token.Rn = self.rn.num
|
|
156 self.token.Rm = self.rm.num
|
|
157 self.token[7:16] = 0
|
|
158 self.token[20:28] = 0b10101
|
|
159 self.token.cond = AL
|
|
160 return self.token.encode()
|
|
161
|
|
162 def __repr__(self):
|
|
163 return 'CMP {}, {}'.format(self.rn, self.rm)
|
|
164
|
|
165
|
345
|
166 def Add(*args):
|
|
167 if len(args) == 3 and isinstance(args[0], ArmRegister) and \
|
|
168 isinstance(args[1], ArmRegister):
|
|
169 if isinstance(args[2], ArmRegister):
|
|
170 return Add1(args[0], args[1], args[2])
|
|
171 elif isinstance(args[2], int):
|
|
172 return Add2(args[0], args[1], args[2])
|
|
173 raise Exception()
|
|
174
|
|
175 def Sub(*args):
|
|
176 if len(args) == 3 and isinstance(args[0], ArmRegister) and \
|
|
177 isinstance(args[1], ArmRegister):
|
|
178 if isinstance(args[2], ArmRegister):
|
|
179 return Sub1(args[0], args[1], args[2])
|
|
180 elif isinstance(args[2], int):
|
|
181 return Sub2(args[0], args[1], args[2])
|
|
182 raise Exception()
|
|
183
|
352
|
184
|
346
|
185 def Mul(*args):
|
|
186 return Mul1(args[0], args[1], args[2])
|
|
187
|
|
188
|
354
|
189 class Mul1(ArmInstruction):
|
346
|
190 def __init__(self, rd, rn, rm):
|
|
191 super().__init__()
|
|
192 self.rd = rd
|
|
193 self.rn = rn
|
|
194 self.rm = rm
|
|
195
|
|
196 def encode(self):
|
|
197 self.token[0:4] = self.rn.num
|
|
198 self.token[4:8] = 0b1001
|
|
199 self.token[8:12] = self.rm.num
|
|
200 self.token[16:20] = self.rd.num
|
|
201 self.token.S = 0
|
|
202 self.token.cond = AL
|
|
203 return self.token.encode()
|
|
204
|
|
205
|
345
|
206 class OpRegRegReg(ArmInstruction):
|
|
207 """ add rd, rn, rm """
|
|
208 def __init__(self, rd, rn, rm, shift=0):
|
|
209 super().__init__()
|
|
210 self.rd = rd
|
|
211 self.rn = rn
|
|
212 self.rm = rm
|
|
213
|
|
214 def encode(self):
|
|
215 self.token[0:4] = self.rm.num
|
|
216 self.token[4] = 0
|
|
217 self.token[5:7] = 0
|
|
218 self.token[7:12] = 0 # Shift
|
|
219 self.token.Rd = self.rd.num
|
|
220 self.token.Rn = self.rn.num
|
|
221 self.token.S = 0 # Set flags
|
|
222 self.token[21:28] = self.opcode
|
|
223 self.token.cond = 0xE # Always!
|
|
224 return self.token.encode()
|
|
225
|
|
226 def __repr__(self):
|
354
|
227 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
|
345
|
228
|
|
229
|
|
230 class Add1(OpRegRegReg):
|
354
|
231 mnemonic = 'ADD'
|
345
|
232 opcode = 0b0000100
|
|
233
|
|
234
|
|
235 class Sub1(OpRegRegReg):
|
354
|
236 mnemonic = 'SUB'
|
345
|
237 opcode = 0b0000010
|
|
238
|
|
239
|
|
240 class Orr1(OpRegRegReg):
|
354
|
241 mnemonic = 'ORR'
|
345
|
242 opcode = 0b0001100
|
|
243
|
342
|
244
|
356
|
245 class And1(OpRegRegReg):
|
|
246 mnemonic = 'AND'
|
|
247 opcode = 0b0000000
|
|
248
|
|
249
|
|
250 class ShiftBase(ArmInstruction):
|
|
251 """ ? rd, rn, rm """
|
|
252 def __init__(self, rd, rn, rm):
|
|
253 super().__init__()
|
|
254 self.rd = rd
|
|
255 self.rn = rn
|
|
256 self.rm = rm
|
|
257
|
|
258 def encode(self):
|
|
259 self.token[0:4] = self.rn.num
|
|
260 self.token[4:8] = self.opcode
|
|
261 self.token[8:12] = self.rm.num
|
|
262 self.token[12:16] = self.rd.num
|
|
263 self.token.S = 0 # Set flags
|
|
264 self.token[21:28] = 0b1101
|
|
265 self.token.cond = 0xE # Always!
|
|
266 return self.token.encode()
|
|
267
|
|
268 def __repr__(self):
|
|
269 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
|
|
270
|
|
271
|
|
272 class Lsr1(ShiftBase):
|
|
273 mnemonic = 'LSR'
|
|
274 opcode = 0b0011
|
|
275
|
|
276
|
|
277 class Lsl1(ShiftBase):
|
|
278 mnemonic = 'LSL'
|
|
279 opcode = 0b0001
|
|
280
|
|
281
|
345
|
282 class OpRegRegImm(ArmInstruction):
|
|
283 """ add rd, rn, imm12 """
|
|
284 def __init__(self, rd, rn, imm):
|
|
285 super().__init__()
|
|
286 self.rd = rd
|
|
287 self.rn = rn
|
|
288 self.imm2 = encode_imm32(imm)
|
|
289 self.imm = imm
|
|
290
|
|
291 def encode(self):
|
|
292 self.token[0:12] = self.imm2
|
|
293 self.token.Rd = self.rd.num
|
|
294 self.token.Rn = self.rn.num
|
|
295 self.token.S = 0 # Set flags
|
|
296 self.token[21:28] = self.opcode
|
|
297 self.token.cond = 0xE # Always!
|
|
298 return self.token.encode()
|
|
299
|
|
300 def __repr__(self):
|
354
|
301 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.imm)
|
345
|
302
|
|
303
|
|
304 class Add2(OpRegRegImm):
|
354
|
305 mnemonic = 'ADD'
|
345
|
306 opcode = 0b0010100
|
|
307
|
|
308
|
|
309 class Sub2(OpRegRegImm):
|
354
|
310 mnemonic = 'SUB'
|
345
|
311 opcode = 0b0010010
|
|
312
|
|
313
|
|
314
|
|
315 # Branches:
|
|
316
|
|
317 class BranchBaseRoot(ArmInstruction):
|
|
318 def __init__(self, target):
|
|
319 super().__init__()
|
|
320 self.target = target
|
|
321
|
|
322 def encode(self):
|
|
323 self.token.cond = self.cond
|
|
324 self.token[24:28] = self.opcode
|
|
325 return self.token.encode()
|
|
326
|
|
327 def relocations(self):
|
|
328 return [(self.target, 'b_imm24')]
|
|
329
|
350
|
330 def __repr__(self):
|
|
331 mnemonic = self.__class__.__name__
|
|
332 return '{} {}'.format(mnemonic, self.target)
|
|
333
|
345
|
334
|
346
|
335 EQ, NE, CS, CC, MI, PL, VS, VC, HI, LS, GE, LT, GT, LE, AL = range(15)
|
|
336
|
345
|
337 class BranchBase(BranchBaseRoot):
|
|
338 opcode = 0b1010
|
|
339
|
|
340 class BranchLinkBase(BranchBaseRoot):
|
|
341 opcode = 0b1011
|
|
342
|
|
343 class Bl(BranchLinkBase):
|
346
|
344 cond = AL
|
345
|
345
|
|
346 class B(BranchBase):
|
346
|
347 cond = AL
|
345
|
348
|
|
349 class Beq(BranchBase):
|
346
|
350 cond = EQ
|
345
|
351
|
|
352 class Bgt(BranchBase):
|
346
|
353 cond = GT
|
345
|
354
|
360
|
355 class Bge(BranchBase):
|
|
356 cond = GE
|
|
357
|
345
|
358 class Ble(BranchBase):
|
346
|
359 cond = LE
|
|
360
|
|
361 class Blt(BranchBase):
|
|
362 cond = LT
|
|
363
|
352
|
364 class Bne(BranchBase):
|
|
365 cond = NE
|
346
|
366
|
|
367 # Memory:
|
|
368
|
|
369 def reg_list_to_mask(reg_list):
|
|
370 mask = 0
|
|
371 for reg in reg_list:
|
|
372 mask |= (1 << reg.num)
|
|
373 return mask
|
|
374
|
|
375
|
|
376 class Push(ArmInstruction):
|
|
377 def __init__(self, register_set):
|
|
378 super().__init__()
|
|
379 self.reg_list = register_set
|
|
380
|
|
381 def encode(self):
|
|
382 self.token.cond = AL
|
|
383 self.token[16:28] = 0b100100101101
|
|
384 reg_list = 0
|
|
385 self.token[0:16] = reg_list_to_mask(self.reg_list)
|
|
386 return self.token.encode()
|
|
387
|
350
|
388 def __repr__(self):
|
|
389 return 'PUSH {}'.format(self.reg_list)
|
|
390
|
|
391
|
346
|
392 class Pop(ArmInstruction):
|
|
393 def __init__(self, register_set):
|
|
394 super().__init__()
|
|
395 self.reg_list = register_set
|
|
396
|
|
397 def encode(self):
|
|
398 self.token.cond = AL
|
|
399 self.token[16:28] = 0b100010111101
|
|
400 self.token[0:16] = reg_list_to_mask(self.reg_list)
|
|
401 return self.token.encode()
|
345
|
402
|
350
|
403 def __repr__(self):
|
|
404 return 'POP {}'.format(self.reg_list)
|
|
405
|
345
|
406
|
346
|
407 def Ldr(*args):
|
350
|
408 """ Convenience function that creates the correct instruction """
|
|
409 if len(args) == 3:
|
|
410 if isinstance(args[1], ArmRegister):
|
|
411 return Ldr1(*args)
|
|
412 elif len(args) == 2:
|
|
413 if isinstance(args[1], ArmRegister):
|
|
414 return Ldr1(args[0], args[1], 0)
|
|
415 elif isinstance(args[1], str):
|
|
416 return Ldr3(*args)
|
346
|
417 raise Exception()
|
|
418
|
350
|
419
|
346
|
420 def Str(*args):
|
|
421 if len(args) == 3 and isinstance(args[1], ArmRegister):
|
|
422 return Str1(*args)
|
|
423 elif len(args) == 2 and isinstance(args[1], ArmRegister):
|
|
424 return Str1(args[0], args[1], 0)
|
|
425 raise Exception()
|
|
426
|
|
427
|
|
428 class LdrStrBase(ArmInstruction):
|
|
429 def __init__(self, rt, rn, offset):
|
|
430 super().__init__()
|
|
431 self.rt = rt
|
|
432 self.rn = rn
|
|
433 self.offset = offset
|
|
434
|
|
435 def encode(self):
|
|
436 self.token.cond = AL
|
|
437 self.token.Rn = self.rn.num
|
|
438 self.token[25:28] = self.opcode
|
|
439 self.token[20] = self.bit20
|
|
440 self.token[12:16] = self.rt.num
|
|
441 self.token[24] = 1 # Index
|
|
442 if self.offset >= 0:
|
|
443 self.token[23] = 1 # U == 1 'add'
|
|
444 self.token[0:12] = self.offset
|
|
445 else:
|
|
446 self.token[23] = 0
|
|
447 self.token[0:12] = -self.offset
|
|
448 return self.token.encode()
|
|
449
|
350
|
450 def __repr__(self):
|
|
451 return '{} {}, [{}, {}]'.format(self.mnemonic, self.rt, self.rn,
|
|
452 hex(self.offset))
|
346
|
453
|
354
|
454
|
346
|
455 class Str1(LdrStrBase):
|
|
456 opcode = 0b010
|
|
457 bit20 = 0
|
350
|
458 mnemonic = 'STR'
|
346
|
459
|
|
460
|
|
461 class Ldr1(LdrStrBase):
|
|
462 opcode = 0b010
|
|
463 bit20 = 1
|
350
|
464 mnemonic = 'LDR'
|
346
|
465
|
|
466
|
354
|
467 class Adr(ArmInstruction):
|
|
468 def __init__(self, rd, label):
|
|
469 super().__init__()
|
|
470 self.rd = rd
|
|
471 self.label = label
|
|
472
|
|
473 def __repr__(self):
|
|
474 return 'ADR {}, {}'.format(self.rd, self.label)
|
|
475
|
|
476 def relocations(self):
|
|
477 return [(self.label, 'adr_imm12')]
|
|
478
|
|
479 def encode(self):
|
|
480 self.token.cond = AL
|
|
481 self.token[0:12] = 0 # Filled by linker
|
|
482 self.token[12:16] = self.rd.num
|
|
483 self.token[16:20] = 0b1111
|
|
484 self.token[25] = 1
|
|
485 return self.token.encode()
|
|
486
|
|
487
|
346
|
488 class Ldr3(ArmInstruction):
|
350
|
489 """ Load PC relative constant value
|
|
490 LDR rt, label
|
|
491 encoding A1
|
|
492 """
|
346
|
493 def __init__(self, rt, label):
|
350
|
494 super().__init__()
|
346
|
495 self.rt = rt
|
|
496 self.label = label
|
|
497
|
350
|
498 def __repr__(self):
|
|
499 return 'LDR {}, {}'.format(self.rt, self.label)
|
|
500
|
|
501 def relocations(self):
|
|
502 return [(self.label, 'ldr_imm12')]
|
|
503
|
|
504 def encode(self):
|
|
505 self.token.cond = AL
|
|
506 self.token[0:12] = 0 # Filled by linker
|
|
507 self.token[12:16] = self.rt.num
|
|
508 self.token[16:23] = 0b0011111
|
|
509 self.token[24:28] = 0b0101
|
|
510 return self.token.encode()
|
362
|
511
|
|
512
|
|
513 class McrBase(ArmInstruction):
|
|
514 """ Mov arm register to coprocessor register """
|
|
515 def __init__(self, coproc, opc1, rt, crn, crm, opc2):
|
|
516 super().__init__()
|
|
517 self.coproc = coproc
|
|
518 self.opc1 = opc1
|
|
519 self.rt = rt
|
|
520 self.crn = crn
|
|
521 self.crm = crm
|
|
522 self.opc2 = opc2
|
|
523
|
|
524 def encode(self):
|
|
525 self.token[0:4] = self.crm
|
|
526 self.token[4] = 1
|
|
527 self.token[5:8] = self.opc2
|
|
528 self.token[8:12] = self.coproc
|
|
529 self.token[12:16] = self.rt.num
|
|
530 self.token[16:20] = self.crn
|
|
531 self.token[20] = self.b20
|
|
532 self.token[21:24] = self.opc1
|
|
533 self.token[24:28] = 0b1110
|
|
534 self.token.cond = AL
|
|
535 return self.token.encode()
|
|
536
|
|
537
|
|
538 class Mcr(McrBase):
|
|
539 b20 = 0
|
|
540
|
|
541
|
|
542 class Mrc(McrBase):
|
|
543 b20 = 1
|