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
|
345
|
232 class OpRegRegImm(ArmInstruction):
|
|
233 """ add rd, rn, imm12 """
|
|
234 def __init__(self, rd, rn, imm):
|
|
235 super().__init__()
|
|
236 self.rd = rd
|
|
237 self.rn = rn
|
|
238 self.imm2 = encode_imm32(imm)
|
|
239 self.imm = imm
|
|
240
|
|
241 def encode(self):
|
|
242 self.token[0:12] = self.imm2
|
|
243 self.token.Rd = self.rd.num
|
|
244 self.token.Rn = self.rn.num
|
|
245 self.token.S = 0 # Set flags
|
|
246 self.token[21:28] = self.opcode
|
|
247 self.token.cond = 0xE # Always!
|
|
248 return self.token.encode()
|
|
249
|
|
250 def __repr__(self):
|
354
|
251 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.imm)
|
345
|
252
|
|
253
|
|
254 class Add2(OpRegRegImm):
|
354
|
255 mnemonic = 'ADD'
|
345
|
256 opcode = 0b0010100
|
|
257
|
|
258
|
|
259 class Sub2(OpRegRegImm):
|
354
|
260 mnemonic = 'SUB'
|
345
|
261 opcode = 0b0010010
|
|
262
|
|
263
|
|
264
|
|
265 # Branches:
|
|
266
|
|
267 class BranchBaseRoot(ArmInstruction):
|
|
268 def __init__(self, target):
|
|
269 super().__init__()
|
|
270 self.target = target
|
|
271
|
|
272 def encode(self):
|
|
273 self.token.cond = self.cond
|
|
274 self.token[24:28] = self.opcode
|
|
275 return self.token.encode()
|
|
276
|
|
277 def relocations(self):
|
|
278 return [(self.target, 'b_imm24')]
|
|
279
|
350
|
280 def __repr__(self):
|
|
281 mnemonic = self.__class__.__name__
|
|
282 return '{} {}'.format(mnemonic, self.target)
|
|
283
|
345
|
284
|
346
|
285 EQ, NE, CS, CC, MI, PL, VS, VC, HI, LS, GE, LT, GT, LE, AL = range(15)
|
|
286
|
345
|
287 class BranchBase(BranchBaseRoot):
|
|
288 opcode = 0b1010
|
|
289
|
|
290 class BranchLinkBase(BranchBaseRoot):
|
|
291 opcode = 0b1011
|
|
292
|
|
293 class Bl(BranchLinkBase):
|
346
|
294 cond = AL
|
345
|
295
|
|
296 class B(BranchBase):
|
346
|
297 cond = AL
|
345
|
298
|
|
299 class Beq(BranchBase):
|
346
|
300 cond = EQ
|
345
|
301
|
|
302 class Bgt(BranchBase):
|
346
|
303 cond = GT
|
345
|
304
|
|
305 class Ble(BranchBase):
|
346
|
306 cond = LE
|
|
307
|
|
308 class Blt(BranchBase):
|
|
309 cond = LT
|
|
310
|
352
|
311 class Bne(BranchBase):
|
|
312 cond = NE
|
346
|
313
|
|
314 # Memory:
|
|
315
|
|
316 def reg_list_to_mask(reg_list):
|
|
317 mask = 0
|
|
318 for reg in reg_list:
|
|
319 mask |= (1 << reg.num)
|
|
320 return mask
|
|
321
|
|
322
|
|
323 class Push(ArmInstruction):
|
|
324 def __init__(self, register_set):
|
|
325 super().__init__()
|
|
326 self.reg_list = register_set
|
|
327
|
|
328 def encode(self):
|
|
329 self.token.cond = AL
|
|
330 self.token[16:28] = 0b100100101101
|
|
331 reg_list = 0
|
|
332 self.token[0:16] = reg_list_to_mask(self.reg_list)
|
|
333 return self.token.encode()
|
|
334
|
350
|
335 def __repr__(self):
|
|
336 return 'PUSH {}'.format(self.reg_list)
|
|
337
|
|
338
|
346
|
339 class Pop(ArmInstruction):
|
|
340 def __init__(self, register_set):
|
|
341 super().__init__()
|
|
342 self.reg_list = register_set
|
|
343
|
|
344 def encode(self):
|
|
345 self.token.cond = AL
|
|
346 self.token[16:28] = 0b100010111101
|
|
347 self.token[0:16] = reg_list_to_mask(self.reg_list)
|
|
348 return self.token.encode()
|
345
|
349
|
350
|
350 def __repr__(self):
|
|
351 return 'POP {}'.format(self.reg_list)
|
|
352
|
345
|
353
|
346
|
354 def Ldr(*args):
|
350
|
355 """ Convenience function that creates the correct instruction """
|
|
356 if len(args) == 3:
|
|
357 if isinstance(args[1], ArmRegister):
|
|
358 return Ldr1(*args)
|
|
359 elif len(args) == 2:
|
|
360 if isinstance(args[1], ArmRegister):
|
|
361 return Ldr1(args[0], args[1], 0)
|
|
362 elif isinstance(args[1], str):
|
|
363 return Ldr3(*args)
|
346
|
364 raise Exception()
|
|
365
|
350
|
366
|
346
|
367 def Str(*args):
|
|
368 if len(args) == 3 and isinstance(args[1], ArmRegister):
|
|
369 return Str1(*args)
|
|
370 elif len(args) == 2 and isinstance(args[1], ArmRegister):
|
|
371 return Str1(args[0], args[1], 0)
|
|
372 raise Exception()
|
|
373
|
|
374
|
|
375 class LdrStrBase(ArmInstruction):
|
|
376 def __init__(self, rt, rn, offset):
|
|
377 super().__init__()
|
|
378 self.rt = rt
|
|
379 self.rn = rn
|
|
380 self.offset = offset
|
|
381
|
|
382 def encode(self):
|
|
383 self.token.cond = AL
|
|
384 self.token.Rn = self.rn.num
|
|
385 self.token[25:28] = self.opcode
|
|
386 self.token[20] = self.bit20
|
|
387 self.token[12:16] = self.rt.num
|
|
388 self.token[24] = 1 # Index
|
|
389 if self.offset >= 0:
|
|
390 self.token[23] = 1 # U == 1 'add'
|
|
391 self.token[0:12] = self.offset
|
|
392 else:
|
|
393 self.token[23] = 0
|
|
394 self.token[0:12] = -self.offset
|
|
395 return self.token.encode()
|
|
396
|
350
|
397 def __repr__(self):
|
|
398 return '{} {}, [{}, {}]'.format(self.mnemonic, self.rt, self.rn,
|
|
399 hex(self.offset))
|
346
|
400
|
354
|
401
|
346
|
402 class Str1(LdrStrBase):
|
|
403 opcode = 0b010
|
|
404 bit20 = 0
|
350
|
405 mnemonic = 'STR'
|
346
|
406
|
|
407
|
|
408 class Ldr1(LdrStrBase):
|
|
409 opcode = 0b010
|
|
410 bit20 = 1
|
350
|
411 mnemonic = 'LDR'
|
346
|
412
|
|
413
|
354
|
414 class Adr(ArmInstruction):
|
|
415 def __init__(self, rd, label):
|
|
416 super().__init__()
|
|
417 self.rd = rd
|
|
418 self.label = label
|
|
419
|
|
420 def __repr__(self):
|
|
421 return 'ADR {}, {}'.format(self.rd, self.label)
|
|
422
|
|
423 def relocations(self):
|
|
424 return [(self.label, 'adr_imm12')]
|
|
425
|
|
426 def encode(self):
|
|
427 self.token.cond = AL
|
|
428 self.token[0:12] = 0 # Filled by linker
|
|
429 self.token[12:16] = self.rd.num
|
|
430 self.token[16:20] = 0b1111
|
|
431 self.token[25] = 1
|
|
432 return self.token.encode()
|
|
433
|
|
434
|
346
|
435 class Ldr3(ArmInstruction):
|
350
|
436 """ Load PC relative constant value
|
|
437 LDR rt, label
|
|
438 encoding A1
|
|
439 """
|
346
|
440 def __init__(self, rt, label):
|
350
|
441 super().__init__()
|
346
|
442 self.rt = rt
|
|
443 self.label = label
|
|
444
|
350
|
445 def __repr__(self):
|
|
446 return 'LDR {}, {}'.format(self.rt, self.label)
|
|
447
|
|
448 def relocations(self):
|
|
449 return [(self.label, 'ldr_imm12')]
|
|
450
|
|
451 def encode(self):
|
|
452 self.token.cond = AL
|
|
453 self.token[0:12] = 0 # Filled by linker
|
|
454 self.token[12:16] = self.rt.num
|
|
455 self.token[16:23] = 0b0011111
|
|
456 self.token[24:28] = 0b0101
|
|
457 return self.token.encode()
|