blob: eab77a908bea104459d56e801e321dbe996f6034 [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 Clayton31e2a382011-01-30 20:03:56 +000013#include "lldb/Core/ConstString.h"
Greg Clayton64c84432011-01-21 22:02:52 +000014
15using namespace lldb;
16using namespace lldb_private;
17
18// ARM constants used during decoding
19#define REG_RD 0
20#define LDM_REGLIST 1
21#define PC_REG 15
22#define PC_REGLIST_BIT 0x8000
23
Johnny Chen251af6a2011-01-21 22:47:25 +000024#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +000025#define ARMv4T (1u << 1)
26#define ARMv5T (1u << 2)
27#define ARMv5TE (1u << 3)
28#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +000029#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +000030#define ARMv6K (1u << 6)
31#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +000032#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +000033#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +000034#define ARMvAll (0xffffffffu)
35
Johnny Chen7dc60e12011-01-24 19:46:32 +000036typedef enum
Greg Clayton64c84432011-01-21 22:02:52 +000037{
38 eEncodingA1,
39 eEncodingA2,
40 eEncodingA3,
41 eEncodingA4,
42 eEncodingA5,
43 eEncodingT1,
44 eEncodingT2,
45 eEncodingT3,
46 eEncodingT4,
47 eEncodingT5,
48} ARMEncoding;
49
Johnny Chen7dc60e12011-01-24 19:46:32 +000050typedef enum
51{
52 eSize16,
53 eSize32
54} ARMInstrSize;
55
Johnny Chen4baf2e32011-01-24 18:24:53 +000056// Typedef for the callback function used during the emulation.
Johnny Chen3c75c762011-01-22 00:47:08 +000057// Pass along (ARMEncoding)encoding as the callback data.
58typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding);
59
Johnny Chen7dc60e12011-01-24 19:46:32 +000060typedef struct
Greg Clayton64c84432011-01-21 22:02:52 +000061{
62 uint32_t mask;
63 uint32_t value;
64 uint32_t variants;
65 ARMEncoding encoding;
Johnny Chen7dc60e12011-01-24 19:46:32 +000066 ARMInstrSize size;
Greg Clayton64c84432011-01-21 22:02:52 +000067 EmulateCallback callback;
Johnny Chen4bee8ce2011-01-22 00:59:07 +000068 const char *name;
Johnny Chen7dc60e12011-01-24 19:46:32 +000069} ARMOpcode;
Greg Clayton64c84432011-01-21 22:02:52 +000070
Johnny Chen08c25e82011-01-31 18:02:28 +000071// Push Multiple Registers stores multiple registers to the stack, storing to
72// consecutive memory locations ending just below the address in SP, and updates
73// SP to point to the start of the stored data.
Greg Clayton64c84432011-01-21 22:02:52 +000074static bool
Johnny Chence1ca772011-01-25 01:13:00 +000075emulate_push (EmulateInstructionARM *emulator, ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +000076{
77#if 0
78 // ARM pseudo code...
79 if (ConditionPassed())
80 {
81 EncodingSpecificOperations();
82 NullCheckIfThumbEE(13);
83 address = SP - 4*BitCount(registers);
84
85 for (i = 0 to 14)
86 {
87 if (registers<i> == 1’)
88 {
89 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
90 MemA[address,4] = bits(32) UNKNOWN;
91 else
92 MemA[address,4] = R[i];
93 address = address + 4;
94 }
95 }
96
97 if (registers<15> == 1’) // Only possible for encoding A1 or A2
98 MemA[address,4] = PCStoreValue();
99
100 SP = SP - 4*BitCount(registers);
101 }
102#endif
103
104 bool success = false;
105 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
106 if (!success)
107 return false;
108
109 if (emulator->ConditionPassed())
110 {
111 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
112 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
113 if (!success)
114 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000115 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +0000116 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +0000117 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +0000118 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000119 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000120 // The M bit represents LR.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000121 if (Bits32(opcode, 8, 8))
Johnny Chenaedde1c2011-01-24 20:38:45 +0000122 registers |= 0x000eu;
123 // if BitCount(registers) < 1 then UNPREDICTABLE;
124 if (BitCount(registers) < 1)
125 return false;
126 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000127 case eEncodingT2:
128 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000129 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000130 // if BitCount(registers) < 2 then UNPREDICTABLE;
131 if (BitCount(registers) < 2)
132 return false;
133 break;
134 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000135 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000136 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000137 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000138 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000139 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000140 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000141 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000142 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000143 // Instead of return false, let's handle the following case as well,
144 // which amounts to pushing one reg onto the full descending stacks.
145 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000146 break;
147 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000148 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000149 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000150 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000151 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000152 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000153 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000154 default:
155 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000156 }
Johnny Chence1ca772011-01-25 01:13:00 +0000157 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000158 addr_t addr = sp - sp_offset;
159 uint32_t i;
160
161 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
162 for (i=0; i<15; ++i)
163 {
Johnny Chen108d5aa2011-01-26 01:00:55 +0000164 if (BitIsSet (registers, 1u << i))
Greg Clayton64c84432011-01-21 22:02:52 +0000165 {
166 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
167 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
168 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
169 if (!success)
170 return false;
171 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
172 return false;
173 addr += addr_byte_size;
174 }
175 }
176
Johnny Chen108d5aa2011-01-26 01:00:55 +0000177 if (BitIsSet (registers, 1u << 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000178 {
179 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000180 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton64c84432011-01-21 22:02:52 +0000181 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
182 if (!success)
183 return false;
184 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
185 return false;
186 }
187
188 context.type = EmulateInstruction::eContextAdjustStackPointer;
189 context.arg0 = eRegisterKindGeneric;
190 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000191 context.arg2 = -sp_offset;
Greg Clayton64c84432011-01-21 22:02:52 +0000192
193 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
194 return false;
195 }
196 return true;
197}
198
Johnny Chen5b442b72011-01-27 19:34:30 +0000199// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000200// ADD (SP plus immediate)
201static bool
202emulate_add_rd_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
203{
204#if 0
205 // ARM pseudo code...
206 if (ConditionPassed())
207 {
208 EncodingSpecificOperations();
209 (result, carry, overflow) = AddWithCarry(SP, imm32, 0’);
210 if d == 15 then
211 ALUWritePC(result); // setflags is always FALSE here
212 else
213 R[d] = result;
214 if setflags then
215 APSR.N = result<31>;
216 APSR.Z = IsZeroBit(result);
217 APSR.C = carry;
218 APSR.V = overflow;
219 }
220#endif
221
222 bool success = false;
223 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
224 if (!success)
225 return false;
226
227 if (emulator->ConditionPassed())
228 {
229 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
230 if (!success)
231 return false;
232 uint32_t Rd; // the destination register
233 uint32_t imm32;
234 switch (encoding) {
235 case eEncodingT1:
236 Rd = 7;
237 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
238 break;
239 case eEncodingA1:
240 Rd = Bits32(opcode, 15, 12);
241 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
242 break;
243 default:
244 return false;
245 }
246 addr_t sp_offset = imm32;
247 addr_t addr = sp + sp_offset; // a pointer to the stack area
248
249 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
250 eRegisterKindGeneric,
251 LLDB_REGNUM_GENERIC_SP,
252 sp_offset };
253
254 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
255 return false;
256 }
257 return true;
258}
259
Johnny Chen2ccad832011-01-28 19:57:25 +0000260// Set r7 or ip to the current stack pointer.
261// MOV (register)
262static bool
263emulate_mov_rd_sp (EmulateInstructionARM *emulator, ARMEncoding encoding)
264{
265#if 0
266 // ARM pseudo code...
267 if (ConditionPassed())
268 {
269 EncodingSpecificOperations();
270 result = R[m];
271 if d == 15 then
272 ALUWritePC(result); // setflags is always FALSE here
273 else
274 R[d] = result;
275 if setflags then
276 APSR.N = result<31>;
277 APSR.Z = IsZeroBit(result);
278 // APSR.C unchanged
279 // APSR.V unchanged
280 }
281#endif
282
283 bool success = false;
Johnny Chen1c13b622011-01-29 00:11:15 +0000284 //const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
285 //if (!success)
286 // return false;
Johnny Chen2ccad832011-01-28 19:57:25 +0000287
288 if (emulator->ConditionPassed())
289 {
290 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
291 if (!success)
292 return false;
293 uint32_t Rd; // the destination register
294 switch (encoding) {
295 case eEncodingT1:
296 Rd = 7;
297 break;
298 case eEncodingA1:
299 Rd = 12;
300 break;
301 default:
302 return false;
303 }
304 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
305 eRegisterKindGeneric,
306 LLDB_REGNUM_GENERIC_SP,
307 0 };
308
309 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp))
310 return false;
311 }
312 return true;
313}
314
Johnny Chen1c13b622011-01-29 00:11:15 +0000315// Move from high register (r8-r15) to low register (r0-r7).
316// MOV (register)
317static bool
318emulate_mov_low_high (EmulateInstructionARM *emulator, ARMEncoding encoding)
319{
320#if 0
321 // ARM pseudo code...
322 if (ConditionPassed())
323 {
324 EncodingSpecificOperations();
325 result = R[m];
326 if d == 15 then
327 ALUWritePC(result); // setflags is always FALSE here
328 else
329 R[d] = result;
330 if setflags then
331 APSR.N = result<31>;
332 APSR.Z = IsZeroBit(result);
333 // APSR.C unchanged
334 // APSR.V unchanged
335 }
336#endif
337
338 bool success = false;
339 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
340 if (!success)
341 return false;
342
343 if (emulator->ConditionPassed())
344 {
345 uint32_t Rm; // the source register
346 uint32_t Rd; // the destination register
347 switch (encoding) {
348 case eEncodingT1:
349 Rm = Bits32(opcode, 6, 3);
350 Rd = Bits32(opcode, 2, 1); // bits(7) == 0
351 break;
352 default:
353 return false;
354 }
355 int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
356 if (!success)
357 return false;
358
359 // The context specifies that Rm is to be moved into Rd.
360 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
361 eRegisterKindDWARF,
362 dwarf_r0 + Rm,
363 0 };
364
365 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value))
366 return false;
367 }
368 return true;
369}
370
Johnny Chen788e0552011-01-27 22:52:23 +0000371// PC relative immediate load into register, possibly followed by ADD (SP plus register).
372// LDR (literal)
373static bool
374emulate_ldr_rd_pc_rel (EmulateInstructionARM *emulator, ARMEncoding encoding)
375{
376#if 0
377 // ARM pseudo code...
378 if (ConditionPassed())
379 {
380 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
381 base = Align(PC,4);
382 address = if add then (base + imm32) else (base - imm32);
383 data = MemU[address,4];
384 if t == 15 then
385 if address<1:0> == 00 then LoadWritePC(data); else UNPREDICTABLE;
386 elsif UnalignedSupport() || address<1:0> = 00 then
387 R[t] = data;
388 else // Can only apply before ARMv7
389 if CurrentInstrSet() == InstrSet_ARM then
390 R[t] = ROR(data, 8*UInt(address<1:0>));
391 else
392 R[t] = bits(32) UNKNOWN;
393 }
394#endif
395
396 bool success = false;
397 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
398 if (!success)
399 return false;
400
401 if (emulator->ConditionPassed())
402 {
403 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
404 if (!success)
405 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000406
407 // PC relative immediate load context
408 EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset,
409 eRegisterKindGeneric,
410 LLDB_REGNUM_GENERIC_PC,
411 0};
Johnny Chen788e0552011-01-27 22:52:23 +0000412 uint32_t Rd; // the destination register
413 uint32_t imm32; // immediate offset from the PC
414 addr_t addr; // the PC relative address
415 uint32_t data; // the literal data value from the PC relative load
416 switch (encoding) {
417 case eEncodingT1:
418 Rd = Bits32(opcode, 10, 8);
419 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
420 addr = pc + 4 + imm32;
Johnny Chen809742e2011-01-28 00:32:27 +0000421 context.arg2 = 4 + imm32;
Johnny Chen788e0552011-01-27 22:52:23 +0000422 break;
423 default:
424 return false;
425 }
Johnny Chen809742e2011-01-28 00:32:27 +0000426 data = emulator->ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000427 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000428 return false;
Johnny Chen788e0552011-01-27 22:52:23 +0000429 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data))
430 return false;
431 }
432 return true;
433}
434
Johnny Chen5b442b72011-01-27 19:34:30 +0000435// An add operation to adjust the SP.
Johnny Chenfdd179e2011-01-31 20:09:28 +0000436// ADD (SP plus immediate)
437static bool
438emulate_add_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
439{
440#if 0
441 // ARM pseudo code...
442 if (ConditionPassed())
443 {
444 EncodingSpecificOperations();
445 (result, carry, overflow) = AddWithCarry(SP, imm32, 0’);
446 if d == 15 then // Can only occur for ARM encoding
447 ALUWritePC(result); // setflags is always FALSE here
448 else
449 R[d] = result;
450 if setflags then
451 APSR.N = result<31>;
452 APSR.Z = IsZeroBit(result);
453 APSR.C = carry;
454 APSR.V = overflow;
455 }
456#endif
457
458 bool success = false;
459 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
460 if (!success)
461 return false;
462
463 if (emulator->ConditionPassed())
464 {
465 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
466 if (!success)
467 return false;
468 uint32_t imm32; // the immediate operand
469 switch (encoding) {
470 case eEncodingT2:
471 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
472 break;
473 default:
474 return false;
475 }
476 addr_t sp_offset = imm32;
477 addr_t addr = sp + sp_offset; // the adjusted stack pointer value
478
479 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
480 eRegisterKindGeneric,
481 LLDB_REGNUM_GENERIC_SP,
482 sp_offset };
483
484 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
485 return false;
486 }
487 return true;
488}
489
490// An add operation to adjust the SP.
Johnny Chen5b442b72011-01-27 19:34:30 +0000491// ADD (SP plus register)
492static bool
493emulate_add_sp_rm (EmulateInstructionARM *emulator, ARMEncoding encoding)
494{
495#if 0
496 // ARM pseudo code...
497 if (ConditionPassed())
498 {
499 EncodingSpecificOperations();
500 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
501 (result, carry, overflow) = AddWithCarry(SP, shifted, 0’);
502 if d == 15 then
503 ALUWritePC(result); // setflags is always FALSE here
504 else
505 R[d] = result;
506 if setflags then
507 APSR.N = result<31>;
508 APSR.Z = IsZeroBit(result);
509 APSR.C = carry;
510 APSR.V = overflow;
511 }
512#endif
513
514 bool success = false;
515 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
516 if (!success)
517 return false;
518
519 if (emulator->ConditionPassed())
520 {
521 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
522 if (!success)
523 return false;
524 uint32_t Rm; // the second operand
525 switch (encoding) {
526 case eEncodingT2:
527 Rm = Bits32(opcode, 6, 3);
528 break;
529 default:
530 return false;
531 }
532 int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
533 if (!success)
534 return false;
535
536 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
537
538 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
539 eRegisterKindGeneric,
540 LLDB_REGNUM_GENERIC_SP,
541 reg_value };
542
543 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
544 return false;
545 }
546 return true;
547}
548
Johnny Chen0d0148e2011-01-28 02:26:08 +0000549// Set r7 to point to some ip offset.
550// SUB (immediate)
551static bool
552emulate_sub_r7_ip_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
553{
554#if 0
555 // ARM pseudo code...
556 if (ConditionPassed())
557 {
558 EncodingSpecificOperations();
559 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
560 if d == 15 then // Can only occur for ARM encoding
561 ALUWritePC(result); // setflags is always FALSE here
562 else
563 R[d] = result;
564 if setflags then
565 APSR.N = result<31>;
566 APSR.Z = IsZeroBit(result);
567 APSR.C = carry;
568 APSR.V = overflow;
569 }
570#endif
571
572 bool success = false;
573 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
574 if (!success)
575 return false;
576
577 if (emulator->ConditionPassed())
578 {
579 const addr_t ip = emulator->ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
580 if (!success)
581 return false;
582 uint32_t imm32;
583 switch (encoding) {
584 case eEncodingA1:
585 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
586 break;
587 default:
588 return false;
589 }
590 addr_t ip_offset = imm32;
591 addr_t addr = ip - ip_offset; // the adjusted ip value
592
593 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
594 eRegisterKindDWARF,
595 dwarf_r12,
596 -ip_offset };
597
598 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
599 return false;
600 }
601 return true;
602}
603
604// Set ip to point to some stack offset.
605// SUB (SP minus immediate)
606static bool
607emulate_sub_ip_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
608{
609#if 0
610 // ARM pseudo code...
611 if (ConditionPassed())
612 {
613 EncodingSpecificOperations();
614 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
615 if d == 15 then // Can only occur for ARM encoding
616 ALUWritePC(result); // setflags is always FALSE here
617 else
618 R[d] = result;
619 if setflags then
620 APSR.N = result<31>;
621 APSR.Z = IsZeroBit(result);
622 APSR.C = carry;
623 APSR.V = overflow;
624 }
625#endif
626
627 bool success = false;
628 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
629 if (!success)
630 return false;
631
632 if (emulator->ConditionPassed())
633 {
634 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
635 if (!success)
636 return false;
637 uint32_t imm32;
638 switch (encoding) {
639 case eEncodingA1:
640 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
641 break;
642 default:
643 return false;
644 }
645 addr_t sp_offset = imm32;
646 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
647
648 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
649 eRegisterKindGeneric,
650 LLDB_REGNUM_GENERIC_SP,
651 -sp_offset };
652
653 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
654 return false;
655 }
656 return true;
657}
658
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000659// A sub operation to adjust the SP -- allocate space for local storage.
660static bool
661emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
662{
663#if 0
664 // ARM pseudo code...
665 if (ConditionPassed())
666 {
667 EncodingSpecificOperations();
668 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
669 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +0000670 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000671 else
672 R[d] = result;
673 if setflags then
674 APSR.N = result<31>;
675 APSR.Z = IsZeroBit(result);
676 APSR.C = carry;
677 APSR.V = overflow;
678 }
679#endif
680
681 bool success = false;
682 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
683 if (!success)
684 return false;
685
686 if (emulator->ConditionPassed())
687 {
688 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
689 if (!success)
690 return false;
691 uint32_t imm32;
692 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +0000693 case eEncodingT1:
694 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +0000695 case eEncodingT2:
696 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
697 break;
698 case eEncodingT3:
699 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
700 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000701 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +0000702 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000703 break;
704 default:
705 return false;
706 }
707 addr_t sp_offset = imm32;
708 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
709
710 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
711 eRegisterKindGeneric,
712 LLDB_REGNUM_GENERIC_SP,
Johnny Chen5b442b72011-01-27 19:34:30 +0000713 -sp_offset };
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000714
715 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
716 return false;
717 }
718 return true;
719}
720
Johnny Chen08c25e82011-01-31 18:02:28 +0000721// A store operation to the stack that also updates the SP.
Johnny Chence1ca772011-01-25 01:13:00 +0000722static bool
723emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding)
724{
725#if 0
726 // ARM pseudo code...
727 if (ConditionPassed())
728 {
729 EncodingSpecificOperations();
730 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
731 address = if index then offset_addr else R[n];
732 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
733 if wback then R[n] = offset_addr;
734 }
735#endif
736
737 bool success = false;
738 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
739 if (!success)
740 return false;
741
742 if (emulator->ConditionPassed())
743 {
744 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
745 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
746 if (!success)
747 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000748 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +0000749 uint32_t imm12;
750 switch (encoding) {
751 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000752 Rt = Bits32(opcode, 15, 12);
753 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +0000754 break;
755 default:
756 return false;
757 }
758 addr_t sp_offset = imm12;
759 addr_t addr = sp - sp_offset;
760
761 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
Johnny Chen91d99862011-01-25 19:07:04 +0000762 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +0000763 {
Johnny Chen91d99862011-01-25 19:07:04 +0000764 context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number
765 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Johnny Chence1ca772011-01-25 01:13:00 +0000766 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
767 if (!success)
768 return false;
769 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
770 return false;
771 }
772 else
773 {
774 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
775 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
776 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
777 if (!success)
778 return false;
779 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
780 return false;
781 }
782
783 context.type = EmulateInstruction::eContextAdjustStackPointer;
784 context.arg0 = eRegisterKindGeneric;
785 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000786 context.arg2 = -sp_offset;
Johnny Chence1ca772011-01-25 01:13:00 +0000787
788 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
789 return false;
790 }
791 return true;
792}
793
Johnny Chen08c25e82011-01-31 18:02:28 +0000794// Vector Push stores multiple extension registers to the stack.
795// It also updates SP to point to the start of the stored data.
Johnny Chen799dfd02011-01-26 23:14:33 +0000796static bool
797emulate_vpush (EmulateInstructionARM *emulator, ARMEncoding encoding)
798{
799#if 0
800 // ARM pseudo code...
801 if (ConditionPassed())
802 {
803 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
804 address = SP - imm32;
805 SP = SP - imm32;
806 if single_regs then
807 for r = 0 to regs-1
808 MemA[address,4] = S[d+r]; address = address+4;
809 else
810 for r = 0 to regs-1
811 // Store as two word-aligned words in the correct order for current endianness.
812 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
813 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
814 address = address+8;
815 }
816#endif
817
818 bool success = false;
819 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
820 if (!success)
821 return false;
822
823 if (emulator->ConditionPassed())
824 {
825 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
826 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
827 if (!success)
828 return false;
829 bool single_regs;
830 uint32_t d; // UInt(Vd:D) starting register
831 uint32_t imm32; // stack offset
832 uint32_t regs; // number of registers
833 switch (encoding) {
834 case eEncodingT1:
835 case eEncodingA1:
836 single_regs = false;
837 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
838 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
839 // If UInt(imm8) is odd, see "FSTMX".
840 regs = Bits32(opcode, 7, 0) / 2;
841 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
842 if (regs == 0 || regs > 16 || (d + regs) > 32)
843 return false;
844 break;
845 case eEncodingT2:
846 case eEncodingA2:
847 single_regs = true;
848 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
849 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
850 regs = Bits32(opcode, 7, 0);
851 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
852 if (regs == 0 || regs > 16 || (d + regs) > 32)
853 return false;
854 break;
855 default:
856 return false;
857 }
858 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
859 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
860 addr_t sp_offset = imm32;
861 addr_t addr = sp - sp_offset;
862 uint32_t i;
863
864 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
865 for (i=d; i<regs; ++i)
866 {
867 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
868 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
869 // uint64_t to accommodate 64-bit registers.
870 uint64_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
871 if (!success)
872 return false;
873 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
874 return false;
875 addr += reg_byte_size;
876 }
877
878 context.type = EmulateInstruction::eContextAdjustStackPointer;
879 context.arg0 = eRegisterKindGeneric;
880 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000881 context.arg2 = -sp_offset;
Johnny Chen799dfd02011-01-26 23:14:33 +0000882
883 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
884 return false;
885 }
886 return true;
887}
888
Greg Clayton64c84432011-01-21 22:02:52 +0000889static ARMOpcode g_arm_opcodes[] =
890{
Johnny Chenfdd179e2011-01-31 20:09:28 +0000891 ///////////////////////////
892 // Prologue instructions //
893 ///////////////////////////
894
Johnny Chene4455022011-01-26 00:08:59 +0000895 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000896 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, "push <registers>" },
897 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, "push <register>" },
898
Johnny Chen5b442b72011-01-27 19:34:30 +0000899 // set r7 to point to a stack offset
Johnny Chenbcec3af2011-01-27 01:26:19 +0000900 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add r7, sp, #<const>" },
Johnny Chen2ccad832011-01-28 19:57:25 +0000901 { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, emulate_sub_r7_ip_imm, "sub r7, ip, #<const>"},
Johnny Chen5b442b72011-01-27 19:34:30 +0000902 // set ip to point to a stack offset
Johnny Chen2ccad832011-01-28 19:57:25 +0000903 { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, emulate_mov_rd_sp, "mov ip, sp" },
Johnny Chenbcec3af2011-01-27 01:26:19 +0000904 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add ip, sp, #<const>" },
Johnny Chen2ccad832011-01-28 19:57:25 +0000905 { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, emulate_sub_ip_sp_imm, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000906
907 // adjust the stack pointer
Johnny Chenbcec3af2011-01-27 01:26:19 +0000908 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +0000909
Johnny Chen2ccad832011-01-28 19:57:25 +0000910 // push one register
Johnny Chence1ca772011-01-25 01:13:00 +0000911 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chen788e0552011-01-27 22:52:23 +0000912 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +0000913
914 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000915 { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, emulate_vpush, "vpush.64 <list>"},
916 { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, emulate_vpush, "vpush.32 <list>"}
Greg Clayton64c84432011-01-21 22:02:52 +0000917};
918
Johnny Chen347320d2011-01-24 23:40:59 +0000919static ARMOpcode g_thumb_opcodes[] =
920{
Johnny Chenfdd179e2011-01-31 20:09:28 +0000921 ///////////////////////////
922 // Prologue instructions //
923 ///////////////////////////
924
Johnny Chene4455022011-01-26 00:08:59 +0000925 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000926 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, "push <registers>" },
927 { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, "push.w <registers>" },
928 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, "push.w <register>" },
Johnny Chen1c13b622011-01-29 00:11:15 +0000929 // move from high register to low register
930 { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, emulate_mov_low_high, "mov r0-r7, r8-r15" },
Johnny Chenbcec3af2011-01-27 01:26:19 +0000931
Johnny Chen5b442b72011-01-27 19:34:30 +0000932 // set r7 to point to a stack offset
Johnny Chen2ccad832011-01-28 19:57:25 +0000933 { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, emulate_add_rd_sp_imm, "add r7, sp, #imm" },
934 { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, emulate_mov_rd_sp, "mov r7, sp" },
Johnny Chen788e0552011-01-27 22:52:23 +0000935
936 // PC relative load into register (see also emulate_add_sp_rm)
937 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, emulate_ldr_rd_pc_rel, "ldr <Rd>, [PC, #imm]"},
Johnny Chen60c0d622011-01-25 23:49:39 +0000938
939 // adjust the stack pointer
Johnny Chen5b442b72011-01-27 19:34:30 +0000940 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, emulate_add_sp_rm, "add sp, <Rm>"},
Johnny Chen788e0552011-01-27 22:52:23 +0000941 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, emulate_sub_sp_imm, "add sp, sp, #imm"},
Johnny Chen5b442b72011-01-27 19:34:30 +0000942 { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, "sub.w sp, sp, #<const>"},
Johnny Chen788e0552011-01-27 22:52:23 +0000943 { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, "subw sp, sp, #imm12"},
Johnny Chen799dfd02011-01-26 23:14:33 +0000944
945 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000946 { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, emulate_vpush, "vpush.64 <list>"},
Johnny Chenfdd179e2011-01-31 20:09:28 +0000947 { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_vpush, "vpush.32 <list>"},
948
949 ///////////////////////////
950 // Epilogue instructions //
951 ///////////////////////////
952
953 { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, emulate_add_sp_imm, "add sp, #imm"}
Johnny Chen347320d2011-01-24 23:40:59 +0000954};
955
Greg Clayton64c84432011-01-21 22:02:52 +0000956static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
Johnny Chen347320d2011-01-24 23:40:59 +0000957static const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
Greg Clayton64c84432011-01-21 22:02:52 +0000958
Greg Clayton31e2a382011-01-30 20:03:56 +0000959bool
960EmulateInstructionARM::SetTargetTriple (const ConstString &triple)
961{
962 m_arm_isa = 0;
963 const char *triple_cstr = triple.GetCString();
964 if (triple_cstr)
965 {
966 const char *dash = ::strchr (triple_cstr, '-');
967 if (dash)
968 {
969 std::string arch (triple_cstr, dash);
970 const char *arch_cstr = arch.c_str();
971 if (strcasecmp(arch_cstr, "armv4t") == 0)
972 m_arm_isa = ARMv4T;
973 else if (strcasecmp(arch_cstr, "armv4") == 0)
974 m_arm_isa = ARMv4;
975 else if (strcasecmp(arch_cstr, "armv5tej") == 0)
976 m_arm_isa = ARMv5TEJ;
977 else if (strcasecmp(arch_cstr, "armv5te") == 0)
978 m_arm_isa = ARMv5TE;
979 else if (strcasecmp(arch_cstr, "armv5t") == 0)
980 m_arm_isa = ARMv5T;
981 else if (strcasecmp(arch_cstr, "armv6k") == 0)
982 m_arm_isa = ARMv6K;
983 else if (strcasecmp(arch_cstr, "armv6") == 0)
984 m_arm_isa = ARMv6;
985 else if (strcasecmp(arch_cstr, "armv6t2") == 0)
986 m_arm_isa = ARMv6T2;
987 else if (strcasecmp(arch_cstr, "armv7") == 0)
988 m_arm_isa = ARMv7;
989 else if (strcasecmp(arch_cstr, "armv8") == 0)
990 m_arm_isa = ARMv8;
991 }
992 }
993 return m_arm_isa != 0;
994}
995
996
Greg Clayton64c84432011-01-21 22:02:52 +0000997bool
998EmulateInstructionARM::ReadInstruction ()
999{
1000 bool success = false;
1001 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
1002 if (success)
1003 {
1004 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
1005 if (success)
1006 {
1007 Context read_inst_context = {eContextReadOpcode, 0, 0};
1008 if (m_inst_cpsr & MASK_CPSR_T)
1009 {
1010 m_inst_mode = eModeThumb;
1011 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
1012
1013 if (success)
1014 {
1015 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
1016 {
1017 m_inst.opcode_type = eOpcode16;
1018 m_inst.opcode.inst16 = thumb_opcode;
1019 }
1020 else
1021 {
1022 m_inst.opcode_type = eOpcode32;
1023 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
1024 }
1025 }
1026 }
1027 else
1028 {
1029 m_inst_mode = eModeARM;
1030 m_inst.opcode_type = eOpcode32;
1031 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
1032 }
1033 }
1034 }
1035 if (!success)
1036 {
1037 m_inst_mode = eModeInvalid;
1038 m_inst_pc = LLDB_INVALID_ADDRESS;
1039 }
1040 return success;
1041}
1042
1043uint32_t
1044EmulateInstructionARM::CurrentCond ()
1045{
1046 switch (m_inst_mode)
1047 {
1048 default:
1049 case eModeInvalid:
1050 break;
1051
1052 case eModeARM:
1053 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
1054
1055 case eModeThumb:
1056 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
1057 }
1058 return UINT32_MAX; // Return invalid value
1059}
1060bool
1061EmulateInstructionARM::ConditionPassed ()
1062{
1063 if (m_inst_cpsr == 0)
1064 return false;
1065
1066 const uint32_t cond = CurrentCond ();
1067
1068 if (cond == UINT32_MAX)
1069 return false;
1070
1071 bool result = false;
1072 switch (UnsignedBits(cond, 3, 1))
1073 {
1074 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
1075 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
1076 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
1077 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
1078 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
1079 case 5:
1080 {
1081 bool n = (m_inst_cpsr & MASK_CPSR_N);
1082 bool v = (m_inst_cpsr & MASK_CPSR_V);
1083 result = n == v;
1084 }
1085 break;
1086 case 6:
1087 {
1088 bool n = (m_inst_cpsr & MASK_CPSR_N);
1089 bool v = (m_inst_cpsr & MASK_CPSR_V);
1090 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
1091 }
1092 break;
1093 case 7:
1094 result = true;
1095 break;
1096 }
1097
1098 if (cond & 1)
1099 result = !result;
1100 return result;
1101}
1102
1103
1104bool
1105EmulateInstructionARM::EvaluateInstruction ()
1106{
1107 return false;
1108}