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