blob: f45091b5aa5f0015fd48922861bb74d3b07b03e0 [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;
187 context.arg2 = sp_offset;
188
189 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
190 return false;
191 }
192 return true;
193}
194
Johnny Chenbcec3af2011-01-27 01:26:19 +0000195// Adjust r7 or ip to point to saved value residing within the stack.
196// 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 Chen4c0e0bc2011-01-25 22:45:28 +0000256// A sub operation to adjust the SP -- allocate space for local storage.
257static bool
258emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding)
259{
260#if 0
261 // ARM pseudo code...
262 if (ConditionPassed())
263 {
264 EncodingSpecificOperations();
265 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
266 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +0000267 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000268 else
269 R[d] = result;
270 if setflags then
271 APSR.N = result<31>;
272 APSR.Z = IsZeroBit(result);
273 APSR.C = carry;
274 APSR.V = overflow;
275 }
276#endif
277
278 bool success = false;
279 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
280 if (!success)
281 return false;
282
283 if (emulator->ConditionPassed())
284 {
285 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
286 if (!success)
287 return false;
288 uint32_t imm32;
289 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +0000290 case eEncodingT1:
291 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +0000292 case eEncodingT2:
293 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
294 break;
295 case eEncodingT3:
296 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
297 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000298 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +0000299 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000300 break;
301 default:
302 return false;
303 }
304 addr_t sp_offset = imm32;
305 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
306
307 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
308 eRegisterKindGeneric,
309 LLDB_REGNUM_GENERIC_SP,
310 sp_offset };
311
312 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
313 return false;
314 }
315 return true;
316}
317
318// A store operation to the stacks that also updates the SP.
Johnny Chence1ca772011-01-25 01:13:00 +0000319static bool
320emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding)
321{
322#if 0
323 // ARM pseudo code...
324 if (ConditionPassed())
325 {
326 EncodingSpecificOperations();
327 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
328 address = if index then offset_addr else R[n];
329 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
330 if wback then R[n] = offset_addr;
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 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
342 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
343 if (!success)
344 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000345 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +0000346 uint32_t imm12;
347 switch (encoding) {
348 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000349 Rt = Bits32(opcode, 15, 12);
350 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +0000351 break;
352 default:
353 return false;
354 }
355 addr_t sp_offset = imm12;
356 addr_t addr = sp - sp_offset;
357
358 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
Johnny Chen91d99862011-01-25 19:07:04 +0000359 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +0000360 {
Johnny Chen91d99862011-01-25 19:07:04 +0000361 context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number
362 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Johnny Chence1ca772011-01-25 01:13:00 +0000363 uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
364 if (!success)
365 return false;
366 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
367 return false;
368 }
369 else
370 {
371 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
372 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
373 const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
374 if (!success)
375 return false;
376 if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
377 return false;
378 }
379
380 context.type = EmulateInstruction::eContextAdjustStackPointer;
381 context.arg0 = eRegisterKindGeneric;
382 context.arg1 = LLDB_REGNUM_GENERIC_SP;
383 context.arg2 = sp_offset;
384
385 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
386 return false;
387 }
388 return true;
389}
390
Johnny Chen799dfd02011-01-26 23:14:33 +0000391static bool
392emulate_vpush (EmulateInstructionARM *emulator, ARMEncoding encoding)
393{
394#if 0
395 // ARM pseudo code...
396 if (ConditionPassed())
397 {
398 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
399 address = SP - imm32;
400 SP = SP - imm32;
401 if single_regs then
402 for r = 0 to regs-1
403 MemA[address,4] = S[d+r]; address = address+4;
404 else
405 for r = 0 to regs-1
406 // Store as two word-aligned words in the correct order for current endianness.
407 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
408 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
409 address = address+8;
410 }
411#endif
412
413 bool success = false;
414 const uint32_t opcode = emulator->OpcodeAsUnsigned (&success);
415 if (!success)
416 return false;
417
418 if (emulator->ConditionPassed())
419 {
420 const uint32_t addr_byte_size = emulator->GetAddressByteSize();
421 const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
422 if (!success)
423 return false;
424 bool single_regs;
425 uint32_t d; // UInt(Vd:D) starting register
426 uint32_t imm32; // stack offset
427 uint32_t regs; // number of registers
428 switch (encoding) {
429 case eEncodingT1:
430 case eEncodingA1:
431 single_regs = false;
432 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
433 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
434 // If UInt(imm8) is odd, see "FSTMX".
435 regs = Bits32(opcode, 7, 0) / 2;
436 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
437 if (regs == 0 || regs > 16 || (d + regs) > 32)
438 return false;
439 break;
440 case eEncodingT2:
441 case eEncodingA2:
442 single_regs = true;
443 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
444 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
445 regs = Bits32(opcode, 7, 0);
446 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
447 if (regs == 0 || regs > 16 || (d + regs) > 32)
448 return false;
449 break;
450 default:
451 return false;
452 }
453 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
454 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
455 addr_t sp_offset = imm32;
456 addr_t addr = sp - sp_offset;
457 uint32_t i;
458
459 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
460 for (i=d; i<regs; ++i)
461 {
462 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
463 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
464 // uint64_t to accommodate 64-bit registers.
465 uint64_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
466 if (!success)
467 return false;
468 if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
469 return false;
470 addr += reg_byte_size;
471 }
472
473 context.type = EmulateInstruction::eContextAdjustStackPointer;
474 context.arg0 = eRegisterKindGeneric;
475 context.arg1 = LLDB_REGNUM_GENERIC_SP;
476 context.arg2 = sp_offset;
477
478 if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
479 return false;
480 }
481 return true;
482}
483
Greg Clayton64c84432011-01-21 22:02:52 +0000484static ARMOpcode g_arm_opcodes[] =
485{
Johnny Chene4455022011-01-26 00:08:59 +0000486 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000487 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, "push <registers>" },
488 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, "push <register>" },
489
490 // adjust r7 to point to a stack offset
491 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add r7, sp, #<const>" },
492 // adjust ip to point to a stack offset
493 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add ip, sp, #<const>" },
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000494
495 // adjust the stack pointer
Johnny Chenbcec3af2011-01-27 01:26:19 +0000496 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +0000497
498 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chenbcec3af2011-01-27 01:26:19 +0000499 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, "str Rt, [sp, #-<imm12>]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +0000500
501 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000502 { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, emulate_vpush, "vpush.64 <list>"},
503 { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, emulate_vpush, "vpush.32 <list>"}
Greg Clayton64c84432011-01-21 22:02:52 +0000504};
505
Johnny Chen347320d2011-01-24 23:40:59 +0000506static ARMOpcode g_thumb_opcodes[] =
507{
Johnny Chene4455022011-01-26 00:08:59 +0000508 // push register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000509 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, "push <registers>" },
510 { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, "push.w <registers>" },
511 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, "push.w <register>" },
512
513 // adjust r7 to point to a stack offset
514 { 0xffffff00, 0x000af00, ARMvAll, eEncodingT1, eSize16, emulate_add_rd_sp_imm, "add r7, sp, #<imm>" },
Johnny Chen60c0d622011-01-25 23:49:39 +0000515
516 // adjust the stack pointer
Johnny Chenbcec3af2011-01-27 01:26:19 +0000517 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, emulate_sub_sp_imm, "sub{s} sp, sp, #<imm>"},
518 { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, "sub{s}.w sp, sp, #<const>"},
519 { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, "subw sp, sp, #<imm12>"},
Johnny Chen799dfd02011-01-26 23:14:33 +0000520
521 // vector push consecutive extension register(s)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000522 { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, emulate_vpush, "vpush.64 <list>"},
523 { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_vpush, "vpush.32 <list>"}
Johnny Chen347320d2011-01-24 23:40:59 +0000524};
525
Greg Clayton64c84432011-01-21 22:02:52 +0000526static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
Johnny Chen347320d2011-01-24 23:40:59 +0000527static const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
Greg Clayton64c84432011-01-21 22:02:52 +0000528
529bool
530EmulateInstructionARM::ReadInstruction ()
531{
532 bool success = false;
533 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
534 if (success)
535 {
536 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
537 if (success)
538 {
539 Context read_inst_context = {eContextReadOpcode, 0, 0};
540 if (m_inst_cpsr & MASK_CPSR_T)
541 {
542 m_inst_mode = eModeThumb;
543 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
544
545 if (success)
546 {
547 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
548 {
549 m_inst.opcode_type = eOpcode16;
550 m_inst.opcode.inst16 = thumb_opcode;
551 }
552 else
553 {
554 m_inst.opcode_type = eOpcode32;
555 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
556 }
557 }
558 }
559 else
560 {
561 m_inst_mode = eModeARM;
562 m_inst.opcode_type = eOpcode32;
563 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
564 }
565 }
566 }
567 if (!success)
568 {
569 m_inst_mode = eModeInvalid;
570 m_inst_pc = LLDB_INVALID_ADDRESS;
571 }
572 return success;
573}
574
575uint32_t
576EmulateInstructionARM::CurrentCond ()
577{
578 switch (m_inst_mode)
579 {
580 default:
581 case eModeInvalid:
582 break;
583
584 case eModeARM:
585 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
586
587 case eModeThumb:
588 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
589 }
590 return UINT32_MAX; // Return invalid value
591}
592bool
593EmulateInstructionARM::ConditionPassed ()
594{
595 if (m_inst_cpsr == 0)
596 return false;
597
598 const uint32_t cond = CurrentCond ();
599
600 if (cond == UINT32_MAX)
601 return false;
602
603 bool result = false;
604 switch (UnsignedBits(cond, 3, 1))
605 {
606 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
607 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
608 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
609 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
610 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
611 case 5:
612 {
613 bool n = (m_inst_cpsr & MASK_CPSR_N);
614 bool v = (m_inst_cpsr & MASK_CPSR_V);
615 result = n == v;
616 }
617 break;
618 case 6:
619 {
620 bool n = (m_inst_cpsr & MASK_CPSR_N);
621 bool v = (m_inst_cpsr & MASK_CPSR_V);
622 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
623 }
624 break;
625 case 7:
626 result = true;
627 break;
628 }
629
630 if (cond & 1)
631 result = !result;
632 return result;
633}
634
635
636bool
637EmulateInstructionARM::EvaluateInstruction ()
638{
639 return false;
640}