blob: 2f6f8cdd18ca3d9e2e34e9fa6a336c92e285a362 [file] [log] [blame]
Greg Clayton64c84432011-01-21 22:02:52 +00001//===-- EmulateInstructionARM.cpp -------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "EmulateInstructionARM.h"
Johnny Chen8584c922011-01-26 01:18:52 +000011#include "ARMDefines.h"
Johnny Chen4baf2e32011-01-24 18:24:53 +000012#include "ARMUtils.h"
Greg Clayton64c84432011-01-21 22:02:52 +000013
14using namespace lldb;
15using namespace lldb_private;
16
17// ARM constants used during decoding
18#define REG_RD 0
19#define LDM_REGLIST 1
20#define PC_REG 15
21#define PC_REGLIST_BIT 0x8000
22
Johnny Chen251af6a2011-01-21 22:47:25 +000023#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +000024#define ARMv4T (1u << 1)
25#define ARMv5T (1u << 2)
26#define ARMv5TE (1u << 3)
27#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +000028#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +000029#define ARMv6K (1u << 6)
30#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +000031#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +000032#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +000033#define ARMvAll (0xffffffffu)
34
Johnny Chen7dc60e12011-01-24 19:46:32 +000035typedef enum
Greg Clayton64c84432011-01-21 22:02:52 +000036{
37 eEncodingA1,
38 eEncodingA2,
39 eEncodingA3,
40 eEncodingA4,
41 eEncodingA5,
42 eEncodingT1,
43 eEncodingT2,
44 eEncodingT3,
45 eEncodingT4,
46 eEncodingT5,
47} ARMEncoding;
48
Johnny Chen7dc60e12011-01-24 19:46:32 +000049typedef enum
50{
51 eSize16,
52 eSize32
53} ARMInstrSize;
54
Johnny Chen4baf2e32011-01-24 18:24:53 +000055// Typedef for the callback function used during the emulation.
Johnny Chen3c75c762011-01-22 00:47:08 +000056// Pass along (ARMEncoding)encoding as the callback data.
57typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding);
58
Johnny Chen7dc60e12011-01-24 19:46:32 +000059typedef struct
Greg Clayton64c84432011-01-21 22:02:52 +000060{
61 uint32_t mask;
62 uint32_t value;
63 uint32_t variants;
64 ARMEncoding encoding;
Johnny Chen7dc60e12011-01-24 19:46:32 +000065 ARMInstrSize size;
Greg Clayton64c84432011-01-21 22:02:52 +000066 EmulateCallback callback;
Johnny Chen4bee8ce2011-01-22 00:59:07 +000067 const char *name;
Johnny Chen7dc60e12011-01-24 19:46:32 +000068} ARMOpcode;
Greg Clayton64c84432011-01-21 22:02:52 +000069
70static bool
Johnny Chence1ca772011-01-25 01:13:00 +000071emulate_push (EmulateInstructionARM *emulator, ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +000072{
73#if 0
74 // ARM pseudo code...
75 if (ConditionPassed())
76 {
77 EncodingSpecificOperations();
78 NullCheckIfThumbEE(13);
79 address = SP - 4*BitCount(registers);
80
81 for (i = 0 to 14)
82 {
83 if (registers<i> == 1’)
84 {
85 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
86 MemA[address,4] = bits(32) UNKNOWN;
87 else
88 MemA[address,4] = R[i];
89 address = address + 4;
90 }
91 }
92
93 if (registers<15> == 1’) // Only possible for encoding A1 or A2
94 MemA[address,4] = PCStoreValue();
95
96 SP = SP - 4*BitCount(registers);
97 }
98#endif
99
100 bool success = false;
101 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
102 if (!success)
103 return false;
104
105 if (emulator->ConditionPassed())
106 {
107 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
108 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
109 if (!success)
110 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000111 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +0000112 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +0000113 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +0000114 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000115 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000116 // The M bit represents LR.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000117 if (Bits32(opcode, 8, 8))
Johnny Chenaedde1c2011-01-24 20:38:45 +0000118 registers |= 0x000eu;
119 // if BitCount(registers) < 1 then UNPREDICTABLE;
120 if (BitCount(registers) < 1)
121 return false;
122 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000123 case eEncodingT2:
124 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000125 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000126 // if BitCount(registers) < 2 then UNPREDICTABLE;
127 if (BitCount(registers) < 2)
128 return false;
129 break;
130 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000131 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000132 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000133 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000134 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000135 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000136 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000137 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000138 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000139 // Instead of return false, let's handle the following case as well,
140 // which amounts to pushing one reg onto the full descending stacks.
141 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000142 break;
143 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000144 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000145 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000146 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000147 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000148 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000149 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000150 default:
151 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000152 }
Johnny Chence1ca772011-01-25 01:13:00 +0000153 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000154 addr_t addr = sp - sp_offset;
155 uint32_t i;
156
157 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
158 for (i=0; i<15; ++i)
159 {
Johnny Chen108d5aa2011-01-26 01:00:55 +0000160 if (BitIsSet (registers, 1u << i))
Greg Clayton64c84432011-01-21 22:02:52 +0000161 {
162 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
163 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
164 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
165 if (!success)
166 return false;
167 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
168 return false;
169 addr += addr_byte_size;
170 }
171 }
172
Johnny Chen108d5aa2011-01-26 01:00:55 +0000173 if (BitIsSet (registers, 1u << 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000174 {
175 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000176 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton64c84432011-01-21 22:02:52 +0000177 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
178 if (!success)
179 return false;
180 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
181 return false;
182 }
183
184 context.type = EmulateInstruction::eContextAdjustStackPointer;
185 context.arg0 = eRegisterKindGeneric;
186 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000187 context.arg2 = -sp_offset;
Greg Clayton64c84432011-01-21 22:02:52 +0000188
189 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
190 return false;
191 }
192 return true;
193}
194
Johnny Chen5b442b72011-01-27 19:34:30 +0000195// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000196// ADD (SP plus immediate)
197static bool
198emulate_add_rd_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
199{
200#if 0
201 // ARM pseudo code...
202 if (ConditionPassed())
203 {
204 EncodingSpecificOperations();
205 (result, carry, overflow) = AddWithCarry(SP, imm32, 0’);
206 if d == 15 then
207 ALUWritePC(result); // setflags is always FALSE here
208 else
209 R[d] = result;
210 if setflags then
211 APSR.N = result<31>;
212 APSR.Z = IsZeroBit(result);
213 APSR.C = carry;
214 APSR.V = overflow;
215 }
216#endif
217
218 bool success = false;
219 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
220 if (!success)
221 return false;
222
223 if (emulator->ConditionPassed())
224 {
225 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
226 if (!success)
227 return false;
228 uint32_t Rd; // the destination register
229 uint32_t imm32;
230 switch (encoding) {
231 case eEncodingT1:
232 Rd = 7;
233 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
234 break;
235 case eEncodingA1:
236 Rd = Bits32(opcode, 15, 12);
237 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
238 break;
239 default:
240 return false;
241 }
242 addr_t sp_offset = imm32;
243 addr_t addr = sp + sp_offset; // a pointer to the stack area
244
245 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
246 eRegisterKindGeneric,
247 LLDB_REGNUM_GENERIC_SP,
248 sp_offset };
249
250 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
251 return false;
252 }
253 return true;
254}
255
Johnny Chen2ccad832011-01-28 19:57:25 +0000256// Set r7 or ip to the current stack pointer.
257// MOV (register)
258static bool
259emulate_mov_rd_sp (EmulateInstructionARM *emulator, ARMEncoding encoding)
260{
261#if 0
262 // ARM pseudo code...
263 if (ConditionPassed())
264 {
265 EncodingSpecificOperations();
266 result = R[m];
267 if d == 15 then
268 ALUWritePC(result); // setflags is always FALSE here
269 else
270 R[d] = result;
271 if setflags then
272 APSR.N = result<31>;
273 APSR.Z = IsZeroBit(result);
274 // APSR.C unchanged
275 // APSR.V unchanged
276 }
277#endif
278
279 bool success = false;
Johnny Chen1c13b622011-01-29 00:11:15 +0000280 //const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
281 //if (!success)
282 // return false;
Johnny Chen2ccad832011-01-28 19:57:25 +0000283
284 if (emulator->ConditionPassed())
285 {
286 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
287 if (!success)
288 return false;
289 uint32_t Rd; // the destination register
290 switch (encoding) {
291 case eEncodingT1:
292 Rd = 7;
293 break;
294 case eEncodingA1:
295 Rd = 12;
296 break;
297 default:
298 return false;
299 }
300 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
301 eRegisterKindGeneric,
302 LLDB_REGNUM_GENERIC_SP,
303 0 };
304
305 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp))
306 return false;
307 }
308 return true;
309}
310
Johnny Chen1c13b622011-01-29 00:11:15 +0000311// Move from high register (r8-r15) to low register (r0-r7).
312// MOV (register)
313static bool
314emulate_mov_low_high (EmulateInstructionARM *emulator, ARMEncoding encoding)
315{
316#if 0
317 // ARM pseudo code...
318 if (ConditionPassed())
319 {
320 EncodingSpecificOperations();
321 result = R[m];
322 if d == 15 then
323 ALUWritePC(result); // setflags is always FALSE here
324 else
325 R[d] = result;
326 if setflags then
327 APSR.N = result<31>;
328 APSR.Z = IsZeroBit(result);
329 // APSR.C unchanged
330 // APSR.V unchanged
331 }
332#endif
333
334 bool success = false;
335 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
336 if (!success)
337 return false;
338
339 if (emulator->ConditionPassed())
340 {
341 uint32_t Rm; // the source register
342 uint32_t Rd; // the destination register
343 switch (encoding) {
344 case eEncodingT1:
345 Rm = Bits32(opcode, 6, 3);
346 Rd = Bits32(opcode, 2, 1); // bits(7) == 0
347 break;
348 default:
349 return false;
350 }
351 int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
352 if (!success)
353 return false;
354
355 // The context specifies that Rm is to be moved into Rd.
356 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
357 eRegisterKindDWARF,
358 dwarf_r0 + Rm,
359 0 };
360
361 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value))
362 return false;
363 }
364 return true;
365}
366
Johnny Chen788e0552011-01-27 22:52:23 +0000367// PC relative immediate load into register, possibly followed by ADD (SP plus register).
368// LDR (literal)
369static bool
370emulate_ldr_rd_pc_rel (EmulateInstructionARM *emulator, ARMEncoding encoding)
371{
372#if 0
373 // ARM pseudo code...
374 if (ConditionPassed())
375 {
376 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
377 base = Align(PC,4);
378 address = if add then (base + imm32) else (base - imm32);
379 data = MemU[address,4];
380 if t == 15 then
381 if address<1:0> == 00 then LoadWritePC(data); else UNPREDICTABLE;
382 elsif UnalignedSupport() || address<1:0> = 00 then
383 R[t] = data;
384 else // Can only apply before ARMv7
385 if CurrentInstrSet() == InstrSet_ARM then
386 R[t] = ROR(data, 8*UInt(address<1:0>));
387 else
388 R[t] = bits(32) UNKNOWN;
389 }
390#endif
391
392 bool success = false;
393 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
394 if (!success)
395 return false;
396
397 if (emulator->ConditionPassed())
398 {
399 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
400 if (!success)
401 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000402
403 // PC relative immediate load context
404 EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset,
405 eRegisterKindGeneric,
406 LLDB_REGNUM_GENERIC_PC,
407 0};
Johnny Chen788e0552011-01-27 22:52:23 +0000408 uint32_t Rd; // the destination register
409 uint32_t imm32; // immediate offset from the PC
410 addr_t addr; // the PC relative address
411 uint32_t data; // the literal data value from the PC relative load
412 switch (encoding) {
413 case eEncodingT1:
414 Rd = Bits32(opcode, 10, 8);
415 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
416 addr = pc + 4 + imm32;
Johnny Chen809742e2011-01-28 00:32:27 +0000417 context.arg2 = 4 + imm32;
Johnny Chen788e0552011-01-27 22:52:23 +0000418 break;
419 default:
420 return false;
421 }
Johnny Chen809742e2011-01-28 00:32:27 +0000422 data = emulator->ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000423 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000424 return false;
Johnny Chen788e0552011-01-27 22:52:23 +0000425 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data))
426 return false;
427 }
428 return true;
429}
430
Johnny Chen5b442b72011-01-27 19:34:30 +0000431// An add operation to adjust the SP.
432// ADD (SP plus register)
433static bool
434emulate_add_sp_rm (EmulateInstructionARM *emulator, ARMEncoding encoding)
435{
436#if 0
437 // ARM pseudo code...
438 if (ConditionPassed())
439 {
440 EncodingSpecificOperations();
441 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
442 (result, carry, overflow) = AddWithCarry(SP, shifted, 0’);
443 if d == 15 then
444 ALUWritePC(result); // setflags is always FALSE here
445 else
446 R[d] = result;
447 if setflags then
448 APSR.N = result<31>;
449 APSR.Z = IsZeroBit(result);
450 APSR.C = carry;
451 APSR.V = overflow;
452 }
453#endif
454
455 bool success = false;
456 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
457 if (!success)
458 return false;
459
460 if (emulator->ConditionPassed())
461 {
462 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
463 if (!success)
464 return false;
465 uint32_t Rm; // the second operand
466 switch (encoding) {
467 case eEncodingT2:
468 Rm = Bits32(opcode, 6, 3);
469 break;
470 default:
471 return false;
472 }
473 int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
474 if (!success)
475 return false;
476
477 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
478
479 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
480 eRegisterKindGeneric,
481 LLDB_REGNUM_GENERIC_SP,
482 reg_value };
483
484 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
485 return false;
486 }
487 return true;
488}
489
Johnny Chen0d0148e2011-01-28 02:26:08 +0000490// Set r7 to point to some ip offset.
491// SUB (immediate)
492static bool
493emulate_sub_r7_ip_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
494{
495#if 0
496 // ARM pseudo code...
497 if (ConditionPassed())
498 {
499 EncodingSpecificOperations();
500 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
501 if d == 15 then // Can only occur for ARM encoding
502 ALUWritePC(result); // setflags is always FALSE here
503 else
504 R[d] = result;
505 if setflags then
506 APSR.N = result<31>;
507 APSR.Z = IsZeroBit(result);
508 APSR.C = carry;
509 APSR.V = overflow;
510 }
511#endif
512
513 bool success = false;
514 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
515 if (!success)
516 return false;
517
518 if (emulator->ConditionPassed())
519 {
520 const addr_t ip = emulator->ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
521 if (!success)
522 return false;
523 uint32_t imm32;
524 switch (encoding) {
525 case eEncodingA1:
526 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
527 break;
528 default:
529 return false;
530 }
531 addr_t ip_offset = imm32;
532 addr_t addr = ip - ip_offset; // the adjusted ip value
533
534 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
535 eRegisterKindDWARF,
536 dwarf_r12,
537 -ip_offset };
538
539 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
540 return false;
541 }
542 return true;
543}
544
545// Set ip to point to some stack offset.
546// SUB (SP minus immediate)
547static bool
548emulate_sub_ip_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
549{
550#if 0
551 // ARM pseudo code...
552 if (ConditionPassed())
553 {
554 EncodingSpecificOperations();
555 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
556 if d == 15 then // Can only occur for ARM encoding
557 ALUWritePC(result); // setflags is always FALSE here
558 else
559 R[d] = result;
560 if setflags then
561 APSR.N = result<31>;
562 APSR.Z = IsZeroBit(result);
563 APSR.C = carry;
564 APSR.V = overflow;
565 }
566#endif
567
568 bool success = false;
569 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
570 if (!success)
571 return false;
572
573 if (emulator->ConditionPassed())
574 {
575 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
576 if (!success)
577 return false;
578 uint32_t imm32;
579 switch (encoding) {
580 case eEncodingA1:
581 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
582 break;
583 default:
584 return false;
585 }
586 addr_t sp_offset = imm32;
587 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
588
589 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
590 eRegisterKindGeneric,
591 LLDB_REGNUM_GENERIC_SP,
592 -sp_offset };
593
594 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
595 return false;
596 }
597 return true;
598}
599
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000600// A sub operation to adjust the SP -- allocate space for local storage.
601static bool
602emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
603{
604#if 0
605 // ARM pseudo code...
606 if (ConditionPassed())
607 {
608 EncodingSpecificOperations();
609 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
610 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +0000611 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000612 else
613 R[d] = result;
614 if setflags then
615 APSR.N = result<31>;
616 APSR.Z = IsZeroBit(result);
617 APSR.C = carry;
618 APSR.V = overflow;
619 }
620#endif
621
622 bool success = false;
623 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
624 if (!success)
625 return false;
626
627 if (emulator->ConditionPassed())
628 {
629 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
630 if (!success)
631 return false;
632 uint32_t imm32;
633 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +0000634 case eEncodingT1:
635 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +0000636 case eEncodingT2:
637 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
638 break;
639 case eEncodingT3:
640 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
641 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000642 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +0000643 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000644 break;
645 default:
646 return false;
647 }
648 addr_t sp_offset = imm32;
649 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
650
651 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
652 eRegisterKindGeneric,
653 LLDB_REGNUM_GENERIC_SP,
Johnny Chen5b442b72011-01-27 19:34:30 +0000654 -sp_offset };
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000655
656 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
657 return false;
658 }
659 return true;
660}
661
662// A store operation to the stacks that also updates the SP.
Johnny Chence1ca772011-01-25 01:13:00 +0000663static bool
664emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding)
665{
666#if 0
667 // ARM pseudo code...
668 if (ConditionPassed())
669 {
670 EncodingSpecificOperations();
671 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
672 address = if index then offset_addr else R[n];
673 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
674 if wback then R[n] = offset_addr;
675 }
676#endif
677
678 bool success = false;
679 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
680 if (!success)
681 return false;
682
683 if (emulator->ConditionPassed())
684 {
685 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
686 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
687 if (!success)
688 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000689 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +0000690 uint32_t imm12;
691 switch (encoding) {
692 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000693 Rt = Bits32(opcode, 15, 12);
694 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +0000695 break;
696 default:
697 return false;
698 }
699 addr_t sp_offset = imm12;
700 addr_t addr = sp - sp_offset;
701
702 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
Johnny Chen91d99862011-01-25 19:07:04 +0000703 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +0000704 {
Johnny Chen91d99862011-01-25 19:07:04 +0000705 context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number
706 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Johnny Chence1ca772011-01-25 01:13:00 +0000707 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
708 if (!success)
709 return false;
710 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
711 return false;
712 }
713 else
714 {
715 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
716 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
717 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
718 if (!success)
719 return false;
720 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
721 return false;
722 }
723
724 context.type = EmulateInstruction::eContextAdjustStackPointer;
725 context.arg0 = eRegisterKindGeneric;
726 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000727 context.arg2 = -sp_offset;
Johnny Chence1ca772011-01-25 01:13:00 +0000728
729 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
730 return false;
731 }
732 return true;
733}
734
Johnny Chen799dfd02011-01-26 23:14:33 +0000735static bool
736emulate_vpush (EmulateInstructionARM *emulator, ARMEncoding encoding)
737{
738#if 0
739 // ARM pseudo code...
740 if (ConditionPassed())
741 {
742 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
743 address = SP - imm32;
744 SP = SP - imm32;
745 if single_regs then
746 for r = 0 to regs-1
747 MemA[address,4] = S[d+r]; address = address+4;
748 else
749 for r = 0 to regs-1
750 // Store as two word-aligned words in the correct order for current endianness.
751 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
752 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
753 address = address+8;
754 }
755#endif
756
757 bool success = false;
758 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
759 if (!success)
760 return false;
761
762 if (emulator->ConditionPassed())
763 {
764 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
765 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
766 if (!success)
767 return false;
768 bool single_regs;
769 uint32_t d; // UInt(Vd:D) starting register
770 uint32_t imm32; // stack offset
771 uint32_t regs; // number of registers
772 switch (encoding) {
773 case eEncodingT1:
774 case eEncodingA1:
775 single_regs = false;
776 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
777 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
778 // If UInt(imm8) is odd, see "FSTMX".
779 regs = Bits32(opcode, 7, 0) / 2;
780 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
781 if (regs == 0 || regs > 16 || (d + regs) > 32)
782 return false;
783 break;
784 case eEncodingT2:
785 case eEncodingA2:
786 single_regs = true;
787 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
788 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
789 regs = Bits32(opcode, 7, 0);
790 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
791 if (regs == 0 || regs > 16 || (d + regs) > 32)
792 return false;
793 break;
794 default:
795 return false;
796 }
797 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
798 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
799 addr_t sp_offset = imm32;
800 addr_t addr = sp - sp_offset;
801 uint32_t i;
802
803 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
804 for (i=d; i<regs; ++i)
805 {
806 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
807 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
808 // uint64_t to accommodate 64-bit registers.
809 uint64_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
810 if (!success)
811 return false;
812 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
813 return false;
814 addr += reg_byte_size;
815 }
816
817 context.type = EmulateInstruction::eContextAdjustStackPointer;
818 context.arg0 = eRegisterKindGeneric;
819 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000820 context.arg2 = -sp_offset;
Johnny Chen799dfd02011-01-26 23:14:33 +0000821
822 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
823 return false;
824 }
825 return true;
826}
827
Greg Clayton64c84432011-01-21 22:02:52 +0000828static ARMOpcode g_arm_opcodes[] =
829{
Johnny Chene4455022011-01-26 00:08:59 +0000830 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000831 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, "push <registers>" },
832 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, "push <register>" },
833
Johnny Chen5b442b72011-01-27 19:34:30 +0000834 // set r7 to point to a stack offset
Johnny Chenbcec3af2011-01-27 01:26:19 +0000835 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add r7, sp, #<const>" },
Johnny Chen2ccad832011-01-28 19:57:25 +0000836 { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, emulate_sub_r7_ip_imm, "sub r7, ip, #<const>"},
Johnny Chen5b442b72011-01-27 19:34:30 +0000837 // set ip to point to a stack offset
Johnny Chen2ccad832011-01-28 19:57:25 +0000838 { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, emulate_mov_rd_sp, "mov ip, sp" },
Johnny Chenbcec3af2011-01-27 01:26:19 +0000839 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add ip, sp, #<const>" },
Johnny Chen2ccad832011-01-28 19:57:25 +0000840 { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, emulate_sub_ip_sp_imm, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000841
842 // adjust the stack pointer
Johnny Chenbcec3af2011-01-27 01:26:19 +0000843 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +0000844
Johnny Chen2ccad832011-01-28 19:57:25 +0000845 // push one register
Johnny Chence1ca772011-01-25 01:13:00 +0000846 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chen788e0552011-01-27 22:52:23 +0000847 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +0000848
849 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000850 { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, emulate_vpush, "vpush.64 <list>"},
851 { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, emulate_vpush, "vpush.32 <list>"}
Greg Clayton64c84432011-01-21 22:02:52 +0000852};
853
Johnny Chen347320d2011-01-24 23:40:59 +0000854static ARMOpcode g_thumb_opcodes[] =
855{
Johnny Chene4455022011-01-26 00:08:59 +0000856 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000857 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, "push <registers>" },
858 { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, "push.w <registers>" },
859 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, "push.w <register>" },
Johnny Chen1c13b622011-01-29 00:11:15 +0000860 // move from high register to low register
861 { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, emulate_mov_low_high, "mov r0-r7, r8-r15" },
Johnny Chenbcec3af2011-01-27 01:26:19 +0000862
Johnny Chen5b442b72011-01-27 19:34:30 +0000863 // set r7 to point to a stack offset
Johnny Chen2ccad832011-01-28 19:57:25 +0000864 { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, emulate_add_rd_sp_imm, "add r7, sp, #imm" },
865 { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, emulate_mov_rd_sp, "mov r7, sp" },
Johnny Chen788e0552011-01-27 22:52:23 +0000866
867 // PC relative load into register (see also emulate_add_sp_rm)
868 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, emulate_ldr_rd_pc_rel, "ldr <Rd>, [PC, #imm]"},
Johnny Chen60c0d622011-01-25 23:49:39 +0000869
870 // adjust the stack pointer
Johnny Chen5b442b72011-01-27 19:34:30 +0000871 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, emulate_add_sp_rm, "add sp, <Rm>"},
Johnny Chen788e0552011-01-27 22:52:23 +0000872 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, emulate_sub_sp_imm, "add sp, sp, #imm"},
Johnny Chen5b442b72011-01-27 19:34:30 +0000873 { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, "sub.w sp, sp, #<const>"},
Johnny Chen788e0552011-01-27 22:52:23 +0000874 { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, "subw sp, sp, #imm12"},
Johnny Chen799dfd02011-01-26 23:14:33 +0000875
876 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000877 { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, emulate_vpush, "vpush.64 <list>"},
878 { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_vpush, "vpush.32 <list>"}
Johnny Chen347320d2011-01-24 23:40:59 +0000879};
880
Greg Clayton64c84432011-01-21 22:02:52 +0000881static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
Johnny Chen347320d2011-01-24 23:40:59 +0000882static const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
Greg Clayton64c84432011-01-21 22:02:52 +0000883
884bool
885EmulateInstructionARM::ReadInstruction ()
886{
887 bool success = false;
888 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
889 if (success)
890 {
891 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
892 if (success)
893 {
894 Context read_inst_context = {eContextReadOpcode, 0, 0};
895 if (m_inst_cpsr & MASK_CPSR_T)
896 {
897 m_inst_mode = eModeThumb;
898 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
899
900 if (success)
901 {
902 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
903 {
904 m_inst.opcode_type = eOpcode16;
905 m_inst.opcode.inst16 = thumb_opcode;
906 }
907 else
908 {
909 m_inst.opcode_type = eOpcode32;
910 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
911 }
912 }
913 }
914 else
915 {
916 m_inst_mode = eModeARM;
917 m_inst.opcode_type = eOpcode32;
918 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
919 }
920 }
921 }
922 if (!success)
923 {
924 m_inst_mode = eModeInvalid;
925 m_inst_pc = LLDB_INVALID_ADDRESS;
926 }
927 return success;
928}
929
930uint32_t
931EmulateInstructionARM::CurrentCond ()
932{
933 switch (m_inst_mode)
934 {
935 default:
936 case eModeInvalid:
937 break;
938
939 case eModeARM:
940 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
941
942 case eModeThumb:
943 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
944 }
945 return UINT32_MAX; // Return invalid value
946}
947bool
948EmulateInstructionARM::ConditionPassed ()
949{
950 if (m_inst_cpsr == 0)
951 return false;
952
953 const uint32_t cond = CurrentCond ();
954
955 if (cond == UINT32_MAX)
956 return false;
957
958 bool result = false;
959 switch (UnsignedBits(cond, 3, 1))
960 {
961 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
962 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
963 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
964 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
965 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
966 case 5:
967 {
968 bool n = (m_inst_cpsr & MASK_CPSR_N);
969 bool v = (m_inst_cpsr & MASK_CPSR_V);
970 result = n == v;
971 }
972 break;
973 case 6:
974 {
975 bool n = (m_inst_cpsr & MASK_CPSR_N);
976 bool v = (m_inst_cpsr & MASK_CPSR_V);
977 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
978 }
979 break;
980 case 7:
981 result = true;
982 break;
983 }
984
985 if (cond & 1)
986 result = !result;
987 return result;
988}
989
990
991bool
992EmulateInstructionARM::EvaluateInstruction ()
993{
994 return false;
995}