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