blob: faa1d39d1e17cd0d94eb789daf407ae06dda88e6 [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"
Greg Clayton8482ded2011-02-01 00:04:43 +000011#include "lldb/Core/ConstString.h"
12
Johnny Chen8584c922011-01-26 01:18:52 +000013#include "ARMDefines.h"
Johnny Chen4baf2e32011-01-24 18:24:53 +000014#include "ARMUtils.h"
Greg Clayton8482ded2011-02-01 00:04:43 +000015#include "ARM_DWARF_Registers.h"
Johnny Chen9b8d7832011-02-02 01:13:56 +000016#include "llvm/Support/MathExtras.h" // for SignExtend32 template function
Johnny Chen93070472011-02-04 23:02:47 +000017 // and CountTrailingZeros_32 function
Greg Clayton64c84432011-01-21 22:02:52 +000018
19using namespace lldb;
20using namespace lldb_private;
21
Johnny Chen93070472011-02-04 23:02:47 +000022// A8.6.50
23// Valid return values are {1, 2, 3, 4}, with 0 signifying an error condition.
24static unsigned short CountITSize(unsigned ITMask) {
25 // First count the trailing zeros of the IT mask.
26 unsigned TZ = llvm::CountTrailingZeros_32(ITMask);
27 if (TZ > 3)
28 {
29 printf("Encoding error: IT Mask '0000'\n");
30 return 0;
31 }
32 return (4 - TZ);
33}
34
35// Init ITState. Note that at least one bit is always 1 in mask.
36bool ITSession::InitIT(unsigned short bits7_0)
37{
38 ITCounter = CountITSize(Bits32(bits7_0, 3, 0));
39 if (ITCounter == 0)
40 return false;
41
42 // A8.6.50 IT
43 unsigned short FirstCond = Bits32(bits7_0, 7, 4);
44 if (FirstCond == 0xF)
45 {
46 printf("Encoding error: IT FirstCond '1111'\n");
47 return false;
48 }
49 if (FirstCond == 0xE && ITCounter != 1)
50 {
51 printf("Encoding error: IT FirstCond '1110' && Mask != '1000'\n");
52 return false;
53 }
54
55 ITState = bits7_0;
56 return true;
57}
58
59// Update ITState if necessary.
60void ITSession::ITAdvance()
61{
62 assert(ITCounter);
63 --ITCounter;
64 if (ITCounter == 0)
65 ITState = 0;
66 else
67 {
68 unsigned short NewITState4_0 = Bits32(ITState, 4, 0) << 1;
69 SetBits32(ITState, 4, 0, NewITState4_0);
70 }
71}
72
73// Return true if we're inside an IT Block.
74bool ITSession::InITBlock()
75{
76 return ITCounter != 0;
77}
78
Johnny Chenc315f862011-02-05 00:46:10 +000079// Return true if we're the last instruction inside an IT Block.
80bool ITSession::LastInITBlock()
81{
82 return ITCounter == 1;
83}
84
Johnny Chen93070472011-02-04 23:02:47 +000085// Get condition bits for the current thumb instruction.
86uint32_t ITSession::GetCond()
87{
Johnny Chenc315f862011-02-05 00:46:10 +000088 if (InITBlock())
89 return Bits32(ITState, 7, 4);
90 else
91 return COND_AL;
Johnny Chen93070472011-02-04 23:02:47 +000092}
93
Greg Clayton64c84432011-01-21 22:02:52 +000094// ARM constants used during decoding
95#define REG_RD 0
96#define LDM_REGLIST 1
97#define PC_REG 15
98#define PC_REGLIST_BIT 0x8000
99
Johnny Chen251af6a2011-01-21 22:47:25 +0000100#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +0000101#define ARMv4T (1u << 1)
102#define ARMv5T (1u << 2)
103#define ARMv5TE (1u << 3)
104#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +0000105#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +0000106#define ARMv6K (1u << 6)
107#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +0000108#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +0000109#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +0000110#define ARMvAll (0xffffffffu)
111
Johnny Chen9b8d7832011-02-02 01:13:56 +0000112#define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8)
113#define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8)
114#define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8)
Greg Clayton64c84432011-01-21 22:02:52 +0000115
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000116void
117EmulateInstructionARM::Initialize ()
Johnny Chen7dc60e12011-01-24 19:46:32 +0000118{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000119}
Johnny Chen7dc60e12011-01-24 19:46:32 +0000120
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000121void
122EmulateInstructionARM::Terminate ()
Greg Clayton64c84432011-01-21 22:02:52 +0000123{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000124}
125
Greg Clayton64c84432011-01-21 22:02:52 +0000126
Johnny Chen08c25e82011-01-31 18:02:28 +0000127// Push Multiple Registers stores multiple registers to the stack, storing to
128// consecutive memory locations ending just below the address in SP, and updates
129// SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000130bool
131EmulateInstructionARM::EmulatePush (ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +0000132{
133#if 0
134 // ARM pseudo code...
135 if (ConditionPassed())
136 {
137 EncodingSpecificOperations();
138 NullCheckIfThumbEE(13);
139 address = SP - 4*BitCount(registers);
140
141 for (i = 0 to 14)
142 {
143 if (registers<i> == ’1’)
144 {
145 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
146 MemA[address,4] = bits(32) UNKNOWN;
147 else
148 MemA[address,4] = R[i];
149 address = address + 4;
150 }
151 }
152
153 if (registers<15> == ’1’) // Only possible for encoding A1 or A2
154 MemA[address,4] = PCStoreValue();
155
156 SP = SP - 4*BitCount(registers);
157 }
158#endif
159
160 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000161 const uint32_t opcode = OpcodeAsUnsigned (&success);
Greg Clayton64c84432011-01-21 22:02:52 +0000162 if (!success)
163 return false;
164
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000165 if (ConditionPassed())
Greg Clayton64c84432011-01-21 22:02:52 +0000166 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000167 const uint32_t addr_byte_size = GetAddressByteSize();
168 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000169 if (!success)
170 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000171 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +0000172 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +0000173 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +0000174 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000175 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000176 // The M bit represents LR.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000177 if (Bits32(opcode, 8, 8))
Johnny Chenef85e912011-01-31 23:07:40 +0000178 registers |= (1u << 14);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000179 // if BitCount(registers) < 1 then UNPREDICTABLE;
180 if (BitCount(registers) < 1)
181 return false;
182 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000183 case eEncodingT2:
184 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000185 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000186 // if BitCount(registers) < 2 then UNPREDICTABLE;
187 if (BitCount(registers) < 2)
188 return false;
189 break;
190 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000191 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000192 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000193 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000194 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000195 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000196 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000197 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000198 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000199 // Instead of return false, let's handle the following case as well,
200 // which amounts to pushing one reg onto the full descending stacks.
201 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000202 break;
203 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000204 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000205 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000206 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000207 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000208 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000209 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000210 default:
211 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000212 }
Johnny Chence1ca772011-01-25 01:13:00 +0000213 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000214 addr_t addr = sp - sp_offset;
215 uint32_t i;
216
217 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
218 for (i=0; i<15; ++i)
219 {
Johnny Chen108d5aa2011-01-26 01:00:55 +0000220 if (BitIsSet (registers, 1u << i))
Greg Clayton64c84432011-01-21 22:02:52 +0000221 {
222 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
223 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000224 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000225 if (!success)
226 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000227 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000228 return false;
229 addr += addr_byte_size;
230 }
231 }
232
Johnny Chen108d5aa2011-01-26 01:00:55 +0000233 if (BitIsSet (registers, 1u << 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000234 {
235 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000236 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000237 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000238 if (!success)
239 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000240 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000241 return false;
242 }
243
244 context.type = EmulateInstruction::eContextAdjustStackPointer;
245 context.arg0 = eRegisterKindGeneric;
246 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000247 context.arg2 = -sp_offset;
Greg Clayton64c84432011-01-21 22:02:52 +0000248
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000249 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Greg Clayton64c84432011-01-21 22:02:52 +0000250 return false;
251 }
252 return true;
253}
254
Johnny Chenef85e912011-01-31 23:07:40 +0000255// Pop Multiple Registers loads multiple registers from the stack, loading from
256// consecutive memory locations staring at the address in SP, and updates
257// SP to point just above the loaded data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000258bool
259EmulateInstructionARM::EmulatePop (ARMEncoding encoding)
Johnny Chenef85e912011-01-31 23:07:40 +0000260{
261#if 0
262 // ARM pseudo code...
263 if (ConditionPassed())
264 {
265 EncodingSpecificOperations(); NullCheckIfThumbEE(13);
266 address = SP;
267 for i = 0 to 14
268 if registers<i> == ‘1’ then
269 R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4;
270 if registers<15> == ‘1’ then
271 if UnalignedAllowed then
272 LoadWritePC(MemU[address,4]);
273 else
274 LoadWritePC(MemA[address,4]);
275 if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers);
276 if registers<13> == ‘1’ then SP = bits(32) UNKNOWN;
277 }
278#endif
279
280 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000281 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenef85e912011-01-31 23:07:40 +0000282 if (!success)
283 return false;
284
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000285 if (ConditionPassed())
Johnny Chenef85e912011-01-31 23:07:40 +0000286 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000287 const uint32_t addr_byte_size = GetAddressByteSize();
288 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000289 if (!success)
290 return false;
291 uint32_t registers = 0;
292 uint32_t Rt; // the destination register
293 switch (encoding) {
294 case eEncodingT1:
295 registers = Bits32(opcode, 7, 0);
296 // The P bit represents PC.
297 if (Bits32(opcode, 8, 8))
298 registers |= (1u << 15);
299 // if BitCount(registers) < 1 then UNPREDICTABLE;
300 if (BitCount(registers) < 1)
301 return false;
302 break;
303 case eEncodingT2:
304 // Ignore bit 13.
305 registers = Bits32(opcode, 15, 0) & ~0x2000;
306 // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE;
307 if (BitCount(registers) < 2 || (Bits32(opcode, 15, 15) && Bits32(opcode, 14, 14)))
308 return false;
309 break;
310 case eEncodingT3:
311 Rt = Bits32(opcode, 15, 12);
312 // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE;
313 if (Rt == dwarf_sp)
314 return false;
315 registers = (1u << Rt);
316 break;
317 case eEncodingA1:
318 registers = Bits32(opcode, 15, 0);
319 // Instead of return false, let's handle the following case as well,
320 // which amounts to popping one reg from the full descending stacks.
321 // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD;
322
323 // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE;
324 if (Bits32(opcode, 13, 13))
325 return false;
326 break;
327 case eEncodingA2:
328 Rt = Bits32(opcode, 15, 12);
329 // if t == 13 then UNPREDICTABLE;
330 if (Rt == dwarf_sp)
331 return false;
332 registers = (1u << Rt);
333 break;
334 default:
335 return false;
336 }
337 addr_t sp_offset = addr_byte_size * BitCount (registers);
338 addr_t addr = sp;
339 uint32_t i, data;
340
341 EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 };
342 for (i=0; i<15; ++i)
343 {
344 if (BitIsSet (registers, 1u << i))
345 {
346 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
347 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000348 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000349 if (!success)
350 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000351 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000352 return false;
353 addr += addr_byte_size;
354 }
355 }
356
357 if (BitIsSet (registers, 1u << 15))
358 {
359 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
360 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000361 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000362 if (!success)
363 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000364 if (!WriteRegisterUnsigned(context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000365 return false;
366 addr += addr_byte_size;
367 }
368
369 context.type = EmulateInstruction::eContextAdjustStackPointer;
370 context.arg0 = eRegisterKindGeneric;
371 context.arg1 = LLDB_REGNUM_GENERIC_SP;
372 context.arg2 = sp_offset;
373
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000374 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
Johnny Chenef85e912011-01-31 23:07:40 +0000375 return false;
376 }
377 return true;
378}
379
Johnny Chen5b442b72011-01-27 19:34:30 +0000380// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000381// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000382bool
383EmulateInstructionARM::EmulateAddRdSPImmediate (ARMEncoding encoding)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000384{
385#if 0
386 // ARM pseudo code...
387 if (ConditionPassed())
388 {
389 EncodingSpecificOperations();
390 (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’);
391 if d == 15 then
392 ALUWritePC(result); // setflags is always FALSE here
393 else
394 R[d] = result;
395 if setflags then
396 APSR.N = result<31>;
397 APSR.Z = IsZeroBit(result);
398 APSR.C = carry;
399 APSR.V = overflow;
400 }
401#endif
402
403 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000404 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000405 if (!success)
406 return false;
407
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000408 if (ConditionPassed())
Johnny Chenbcec3af2011-01-27 01:26:19 +0000409 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000410 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000411 if (!success)
412 return false;
413 uint32_t Rd; // the destination register
414 uint32_t imm32;
415 switch (encoding) {
416 case eEncodingT1:
417 Rd = 7;
418 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
419 break;
420 case eEncodingA1:
421 Rd = Bits32(opcode, 15, 12);
422 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
423 break;
424 default:
425 return false;
426 }
427 addr_t sp_offset = imm32;
428 addr_t addr = sp + sp_offset; // a pointer to the stack area
429
430 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
431 eRegisterKindGeneric,
432 LLDB_REGNUM_GENERIC_SP,
433 sp_offset };
434
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000435 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
Johnny Chenbcec3af2011-01-27 01:26:19 +0000436 return false;
437 }
438 return true;
439}
440
Johnny Chen2ccad832011-01-28 19:57:25 +0000441// Set r7 or ip to the current stack pointer.
442// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000443bool
444EmulateInstructionARM::EmulateMovRdSP (ARMEncoding encoding)
Johnny Chen2ccad832011-01-28 19:57:25 +0000445{
446#if 0
447 // ARM pseudo code...
448 if (ConditionPassed())
449 {
450 EncodingSpecificOperations();
451 result = R[m];
452 if d == 15 then
453 ALUWritePC(result); // setflags is always FALSE here
454 else
455 R[d] = result;
456 if setflags then
457 APSR.N = result<31>;
458 APSR.Z = IsZeroBit(result);
459 // APSR.C unchanged
460 // APSR.V unchanged
461 }
462#endif
463
464 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000465 //const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000466 //if (!success)
467 // return false;
Johnny Chen2ccad832011-01-28 19:57:25 +0000468
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000469 if (ConditionPassed())
Johnny Chen2ccad832011-01-28 19:57:25 +0000470 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000471 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen2ccad832011-01-28 19:57:25 +0000472 if (!success)
473 return false;
474 uint32_t Rd; // the destination register
475 switch (encoding) {
476 case eEncodingT1:
477 Rd = 7;
478 break;
479 case eEncodingA1:
480 Rd = 12;
481 break;
482 default:
483 return false;
484 }
485 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
486 eRegisterKindGeneric,
487 LLDB_REGNUM_GENERIC_SP,
488 0 };
489
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000490 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp))
Johnny Chen2ccad832011-01-28 19:57:25 +0000491 return false;
492 }
493 return true;
494}
495
Johnny Chen1c13b622011-01-29 00:11:15 +0000496// Move from high register (r8-r15) to low register (r0-r7).
497// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000498bool
499EmulateInstructionARM::EmulateMovLowHigh (ARMEncoding encoding)
Johnny Chen1c13b622011-01-29 00:11:15 +0000500{
501#if 0
502 // ARM pseudo code...
503 if (ConditionPassed())
504 {
505 EncodingSpecificOperations();
506 result = R[m];
507 if d == 15 then
508 ALUWritePC(result); // setflags is always FALSE here
509 else
510 R[d] = result;
511 if setflags then
512 APSR.N = result<31>;
513 APSR.Z = IsZeroBit(result);
514 // APSR.C unchanged
515 // APSR.V unchanged
516 }
517#endif
518
519 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000520 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000521 if (!success)
522 return false;
523
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000524 if (ConditionPassed())
Johnny Chen1c13b622011-01-29 00:11:15 +0000525 {
526 uint32_t Rm; // the source register
527 uint32_t Rd; // the destination register
528 switch (encoding) {
529 case eEncodingT1:
530 Rm = Bits32(opcode, 6, 3);
531 Rd = Bits32(opcode, 2, 1); // bits(7) == 0
532 break;
533 default:
534 return false;
535 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000536 int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000537 if (!success)
538 return false;
539
540 // The context specifies that Rm is to be moved into Rd.
541 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
542 eRegisterKindDWARF,
543 dwarf_r0 + Rm,
544 0 };
545
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000546 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value))
Johnny Chen1c13b622011-01-29 00:11:15 +0000547 return false;
548 }
549 return true;
550}
551
Johnny Chen788e0552011-01-27 22:52:23 +0000552// PC relative immediate load into register, possibly followed by ADD (SP plus register).
553// LDR (literal)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000554bool
555EmulateInstructionARM::EmulateLDRRdPCRelative (ARMEncoding encoding)
Johnny Chen788e0552011-01-27 22:52:23 +0000556{
557#if 0
558 // ARM pseudo code...
559 if (ConditionPassed())
560 {
561 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
562 base = Align(PC,4);
563 address = if add then (base + imm32) else (base - imm32);
564 data = MemU[address,4];
565 if t == 15 then
566 if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE;
567 elsif UnalignedSupport() || address<1:0> = ‘00’ then
568 R[t] = data;
569 else // Can only apply before ARMv7
570 if CurrentInstrSet() == InstrSet_ARM then
571 R[t] = ROR(data, 8*UInt(address<1:0>));
572 else
573 R[t] = bits(32) UNKNOWN;
574 }
575#endif
576
577 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000578 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen788e0552011-01-27 22:52:23 +0000579 if (!success)
580 return false;
581
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000582 if (ConditionPassed())
Johnny Chen788e0552011-01-27 22:52:23 +0000583 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000584 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000585 if (!success)
586 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000587
588 // PC relative immediate load context
589 EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset,
590 eRegisterKindGeneric,
591 LLDB_REGNUM_GENERIC_PC,
592 0};
Johnny Chen788e0552011-01-27 22:52:23 +0000593 uint32_t Rd; // the destination register
594 uint32_t imm32; // immediate offset from the PC
595 addr_t addr; // the PC relative address
596 uint32_t data; // the literal data value from the PC relative load
597 switch (encoding) {
598 case eEncodingT1:
599 Rd = Bits32(opcode, 10, 8);
600 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
601 addr = pc + 4 + imm32;
Johnny Chen809742e2011-01-28 00:32:27 +0000602 context.arg2 = 4 + imm32;
Johnny Chen788e0552011-01-27 22:52:23 +0000603 break;
604 default:
605 return false;
606 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000607 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000608 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000609 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000610 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data))
Johnny Chen788e0552011-01-27 22:52:23 +0000611 return false;
612 }
613 return true;
614}
615
Johnny Chen5b442b72011-01-27 19:34:30 +0000616// An add operation to adjust the SP.
Johnny Chenfdd179e2011-01-31 20:09:28 +0000617// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000618bool
619EmulateInstructionARM::EmulateAddSPImmediate (ARMEncoding encoding)
Johnny Chenfdd179e2011-01-31 20:09:28 +0000620{
621#if 0
622 // ARM pseudo code...
623 if (ConditionPassed())
624 {
625 EncodingSpecificOperations();
626 (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’);
627 if d == 15 then // Can only occur for ARM encoding
628 ALUWritePC(result); // setflags is always FALSE here
629 else
630 R[d] = result;
631 if setflags then
632 APSR.N = result<31>;
633 APSR.Z = IsZeroBit(result);
634 APSR.C = carry;
635 APSR.V = overflow;
636 }
637#endif
638
639 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000640 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000641 if (!success)
642 return false;
643
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000644 if (ConditionPassed())
Johnny Chenfdd179e2011-01-31 20:09:28 +0000645 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000646 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000647 if (!success)
648 return false;
649 uint32_t imm32; // the immediate operand
650 switch (encoding) {
651 case eEncodingT2:
652 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
653 break;
654 default:
655 return false;
656 }
657 addr_t sp_offset = imm32;
658 addr_t addr = sp + sp_offset; // the adjusted stack pointer value
659
660 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
661 eRegisterKindGeneric,
662 LLDB_REGNUM_GENERIC_SP,
663 sp_offset };
664
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000665 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chenfdd179e2011-01-31 20:09:28 +0000666 return false;
667 }
668 return true;
669}
670
671// An add operation to adjust the SP.
Johnny Chen5b442b72011-01-27 19:34:30 +0000672// ADD (SP plus register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000673bool
674EmulateInstructionARM::EmulateAddSPRm (ARMEncoding encoding)
Johnny Chen5b442b72011-01-27 19:34:30 +0000675{
676#if 0
677 // ARM pseudo code...
678 if (ConditionPassed())
679 {
680 EncodingSpecificOperations();
681 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
682 (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’);
683 if d == 15 then
684 ALUWritePC(result); // setflags is always FALSE here
685 else
686 R[d] = result;
687 if setflags then
688 APSR.N = result<31>;
689 APSR.Z = IsZeroBit(result);
690 APSR.C = carry;
691 APSR.V = overflow;
692 }
693#endif
694
695 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000696 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen5b442b72011-01-27 19:34:30 +0000697 if (!success)
698 return false;
699
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000700 if (ConditionPassed())
Johnny Chen5b442b72011-01-27 19:34:30 +0000701 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000702 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +0000703 if (!success)
704 return false;
705 uint32_t Rm; // the second operand
706 switch (encoding) {
707 case eEncodingT2:
708 Rm = Bits32(opcode, 6, 3);
709 break;
710 default:
711 return false;
712 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000713 int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +0000714 if (!success)
715 return false;
716
717 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
718
719 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
720 eRegisterKindGeneric,
721 LLDB_REGNUM_GENERIC_SP,
722 reg_value };
723
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000724 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen5b442b72011-01-27 19:34:30 +0000725 return false;
726 }
727 return true;
728}
729
Johnny Chen9b8d7832011-02-02 01:13:56 +0000730// Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine
731// at a PC-relative address, and changes instruction set from ARM to Thumb, or
732// from Thumb to ARM.
733// BLX (immediate)
734bool
735EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding)
736{
737#if 0
738 // ARM pseudo code...
739 if (ConditionPassed())
740 {
741 EncodingSpecificOperations();
742 if CurrentInstrSet() == InstrSet_ARM then
743 LR = PC - 4;
744 else
745 LR = PC<31:1> : '1';
746 if targetInstrSet == InstrSet_ARM then
747 targetAddress = Align(PC,4) + imm32;
748 else
749 targetAddress = PC + imm32;
750 SelectInstrSet(targetInstrSet);
751 BranchWritePC(targetAddress);
752 }
753#endif
754
755 bool success = false;
756 const uint32_t opcode = OpcodeAsUnsigned (&success);
757 if (!success)
758 return false;
759
760 if (ConditionPassed())
761 {
762 EmulateInstruction::Context context = { EmulateInstruction::eContextRelativeBranchImmediate, 0, 0, 0};
763 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
764 addr_t lr; // next instruction address
765 addr_t target; // target address
766 if (!success)
767 return false;
768 int32_t imm32; // PC-relative offset
769 switch (encoding) {
770 case eEncodingT2:
771 {
772 lr = (pc + 4) | 1u; // return address
773 uint32_t S = Bits32(opcode, 26, 26);
774 uint32_t imm10H = Bits32(opcode, 25, 16);
775 uint32_t J1 = Bits32(opcode, 13, 13);
776 uint32_t J2 = Bits32(opcode, 11, 11);
777 uint32_t imm10L = Bits32(opcode, 10, 1);
778 uint32_t I1 = !(J1 ^ S);
779 uint32_t I2 = !(J2 ^ S);
780 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10H << 12) + (imm10L << 2);
781 imm32 = llvm::SignExtend32<25>(imm25);
782 target = (pc & 0xfffffffc) + 4 + imm32;
783 context.arg1 = eModeARM; // target instruction set
784 context.arg2 = 4 + imm32; // signed offset
785 break;
786 }
787 case eEncodingA2:
788 lr = pc + 4; // return address
789 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1);
790 target = pc + 8 + imm32;
791 context.arg1 = eModeThumb; // target instruction set
792 context.arg2 = 8 + imm32; // signed offset
793 break;
794 default:
795 return false;
796 }
797 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
798 return false;
799 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target))
800 return false;
801 }
802 return true;
803}
804
805// Branch with Link and Exchange (register) calls a subroutine at an address and
806// instruction set specified by a register.
807// BLX (register)
808bool
809EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding)
810{
811#if 0
812 // ARM pseudo code...
813 if (ConditionPassed())
814 {
815 EncodingSpecificOperations();
816 target = R[m];
817 if CurrentInstrSet() == InstrSet_ARM then
818 next_instr_addr = PC - 4;
819 LR = next_instr_addr;
820 else
821 next_instr_addr = PC - 2;
822 LR = next_instr_addr<31:1> : ‘1’;
823 BXWritePC(target);
824 }
825#endif
826
827 bool success = false;
828 const uint32_t opcode = OpcodeAsUnsigned (&success);
829 if (!success)
830 return false;
831
832 if (ConditionPassed())
833 {
834 EmulateInstruction::Context context = { EmulateInstruction::eContextAbsoluteBranchRegister, 0, 0, 0};
835 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
836 addr_t lr; // next instruction address
837 addr_t target; // target address
838 if (!success)
839 return false;
840 uint32_t Rm; // the register with the target address
841 switch (encoding) {
842 case eEncodingT1:
843 lr = (pc + 2) | 1u; // return address
844 Rm = Bits32(opcode, 6, 3);
845 // if m == 15 then UNPREDICTABLE;
846 if (Rm == 15)
847 return false;
848 target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
849 break;
850 case eEncodingA1:
851 lr = pc + 4; // return address
852 Rm = Bits32(opcode, 3, 0);
853 // if m == 15 then UNPREDICTABLE;
854 if (Rm == 15)
855 return false;
856 target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chenb77be412011-02-04 00:40:18 +0000857 break;
Johnny Chen9b8d7832011-02-02 01:13:56 +0000858 default:
859 return false;
860 }
861 bool toThumb;
862 if (BitIsSet(target, 0))
863 toThumb = true;
864 else if (BitIsClear(target, 1))
865 toThumb = false;
866 else
867 return false; // address<1:0> == ‘10’ => UNPREDICTABLE
868 context.arg0 = eRegisterKindDWARF;
869 context.arg1 = dwarf_r0 + Rm;
870 context.arg2 = toThumb ? eModeThumb : eModeARM;
871 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
872 return false;
873 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target))
874 return false;
875 }
876 return true;
877}
878
Johnny Chen0d0148e2011-01-28 02:26:08 +0000879// Set r7 to point to some ip offset.
880// SUB (immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000881bool
882EmulateInstructionARM::EmulateSubR7IPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +0000883{
884#if 0
885 // ARM pseudo code...
886 if (ConditionPassed())
887 {
888 EncodingSpecificOperations();
889 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
890 if d == 15 then // Can only occur for ARM encoding
891 ALUWritePC(result); // setflags is always FALSE here
892 else
893 R[d] = result;
894 if setflags then
895 APSR.N = result<31>;
896 APSR.Z = IsZeroBit(result);
897 APSR.C = carry;
898 APSR.V = overflow;
899 }
900#endif
901
902 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000903 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000904 if (!success)
905 return false;
906
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000907 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +0000908 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000909 const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000910 if (!success)
911 return false;
912 uint32_t imm32;
913 switch (encoding) {
914 case eEncodingA1:
915 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
916 break;
917 default:
918 return false;
919 }
920 addr_t ip_offset = imm32;
921 addr_t addr = ip - ip_offset; // the adjusted ip value
922
923 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
924 eRegisterKindDWARF,
925 dwarf_r12,
926 -ip_offset };
927
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000928 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +0000929 return false;
930 }
931 return true;
932}
933
934// Set ip to point to some stack offset.
935// SUB (SP minus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000936bool
937EmulateInstructionARM::EmulateSubIPSPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +0000938{
939#if 0
940 // ARM pseudo code...
941 if (ConditionPassed())
942 {
943 EncodingSpecificOperations();
944 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
945 if d == 15 then // Can only occur for ARM encoding
946 ALUWritePC(result); // setflags is always FALSE here
947 else
948 R[d] = result;
949 if setflags then
950 APSR.N = result<31>;
951 APSR.Z = IsZeroBit(result);
952 APSR.C = carry;
953 APSR.V = overflow;
954 }
955#endif
956
957 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000958 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000959 if (!success)
960 return false;
961
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000962 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +0000963 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000964 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000965 if (!success)
966 return false;
967 uint32_t imm32;
968 switch (encoding) {
969 case eEncodingA1:
970 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
971 break;
972 default:
973 return false;
974 }
975 addr_t sp_offset = imm32;
976 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
977
978 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
979 eRegisterKindGeneric,
980 LLDB_REGNUM_GENERIC_SP,
981 -sp_offset };
982
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000983 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +0000984 return false;
985 }
986 return true;
987}
988
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000989// A sub operation to adjust the SP -- allocate space for local storage.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000990bool
991EmulateInstructionARM::EmulateSubSPImmdiate (ARMEncoding encoding)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000992{
993#if 0
994 // ARM pseudo code...
995 if (ConditionPassed())
996 {
997 EncodingSpecificOperations();
998 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
999 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +00001000 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001001 else
1002 R[d] = result;
1003 if setflags then
1004 APSR.N = result<31>;
1005 APSR.Z = IsZeroBit(result);
1006 APSR.C = carry;
1007 APSR.V = overflow;
1008 }
1009#endif
1010
1011 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001012 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001013 if (!success)
1014 return false;
1015
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001016 if (ConditionPassed())
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001017 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001018 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001019 if (!success)
1020 return false;
1021 uint32_t imm32;
1022 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +00001023 case eEncodingT1:
1024 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +00001025 case eEncodingT2:
1026 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
1027 break;
1028 case eEncodingT3:
1029 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
1030 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001031 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +00001032 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001033 break;
1034 default:
1035 return false;
1036 }
1037 addr_t sp_offset = imm32;
1038 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
1039
1040 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
1041 eRegisterKindGeneric,
1042 LLDB_REGNUM_GENERIC_SP,
Johnny Chen5b442b72011-01-27 19:34:30 +00001043 -sp_offset };
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001044
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001045 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001046 return false;
1047 }
1048 return true;
1049}
1050
Johnny Chen08c25e82011-01-31 18:02:28 +00001051// A store operation to the stack that also updates the SP.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001052bool
1053EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding)
Johnny Chence1ca772011-01-25 01:13:00 +00001054{
1055#if 0
1056 // ARM pseudo code...
1057 if (ConditionPassed())
1058 {
1059 EncodingSpecificOperations();
1060 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
1061 address = if index then offset_addr else R[n];
1062 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
1063 if wback then R[n] = offset_addr;
1064 }
1065#endif
1066
1067 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001068 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chence1ca772011-01-25 01:13:00 +00001069 if (!success)
1070 return false;
1071
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001072 if (ConditionPassed())
Johnny Chence1ca772011-01-25 01:13:00 +00001073 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001074 const uint32_t addr_byte_size = GetAddressByteSize();
1075 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001076 if (!success)
1077 return false;
Johnny Chen91d99862011-01-25 19:07:04 +00001078 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +00001079 uint32_t imm12;
1080 switch (encoding) {
1081 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +00001082 Rt = Bits32(opcode, 15, 12);
1083 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +00001084 break;
1085 default:
1086 return false;
1087 }
1088 addr_t sp_offset = imm12;
1089 addr_t addr = sp - sp_offset;
1090
1091 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
Johnny Chen91d99862011-01-25 19:07:04 +00001092 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +00001093 {
Johnny Chen91d99862011-01-25 19:07:04 +00001094 context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number
1095 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001096 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001097 if (!success)
1098 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001099 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +00001100 return false;
1101 }
1102 else
1103 {
1104 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
1105 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001106 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001107 if (!success)
1108 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001109 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +00001110 return false;
1111 }
1112
1113 context.type = EmulateInstruction::eContextAdjustStackPointer;
1114 context.arg0 = eRegisterKindGeneric;
1115 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +00001116 context.arg2 = -sp_offset;
Johnny Chence1ca772011-01-25 01:13:00 +00001117
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001118 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chence1ca772011-01-25 01:13:00 +00001119 return false;
1120 }
1121 return true;
1122}
1123
Johnny Chen08c25e82011-01-31 18:02:28 +00001124// Vector Push stores multiple extension registers to the stack.
1125// It also updates SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001126bool
1127EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding)
Johnny Chen799dfd02011-01-26 23:14:33 +00001128{
1129#if 0
1130 // ARM pseudo code...
1131 if (ConditionPassed())
1132 {
1133 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
1134 address = SP - imm32;
1135 SP = SP - imm32;
1136 if single_regs then
1137 for r = 0 to regs-1
1138 MemA[address,4] = S[d+r]; address = address+4;
1139 else
1140 for r = 0 to regs-1
1141 // Store as two word-aligned words in the correct order for current endianness.
1142 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
1143 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
1144 address = address+8;
1145 }
1146#endif
1147
1148 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001149 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001150 if (!success)
1151 return false;
1152
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001153 if (ConditionPassed())
Johnny Chen799dfd02011-01-26 23:14:33 +00001154 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001155 const uint32_t addr_byte_size = GetAddressByteSize();
1156 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001157 if (!success)
1158 return false;
1159 bool single_regs;
Johnny Chen587a0a42011-02-01 18:35:28 +00001160 uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register
Johnny Chen799dfd02011-01-26 23:14:33 +00001161 uint32_t imm32; // stack offset
1162 uint32_t regs; // number of registers
1163 switch (encoding) {
1164 case eEncodingT1:
1165 case eEncodingA1:
1166 single_regs = false;
Johnny Chen587a0a42011-02-01 18:35:28 +00001167 d = Bits32(opcode, 22, 22) << 4 | Bits32(opcode, 15, 12);
Johnny Chen799dfd02011-01-26 23:14:33 +00001168 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1169 // If UInt(imm8) is odd, see "FSTMX".
1170 regs = Bits32(opcode, 7, 0) / 2;
1171 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1172 if (regs == 0 || regs > 16 || (d + regs) > 32)
1173 return false;
1174 break;
1175 case eEncodingT2:
1176 case eEncodingA2:
1177 single_regs = true;
1178 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
1179 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1180 regs = Bits32(opcode, 7, 0);
1181 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1182 if (regs == 0 || regs > 16 || (d + regs) > 32)
1183 return false;
1184 break;
1185 default:
1186 return false;
1187 }
1188 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
1189 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
1190 addr_t sp_offset = imm32;
1191 addr_t addr = sp - sp_offset;
1192 uint32_t i;
1193
1194 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
1195 for (i=d; i<regs; ++i)
1196 {
1197 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
1198 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
1199 // uint64_t to accommodate 64-bit registers.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001200 uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001201 if (!success)
1202 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001203 if (!WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
Johnny Chen799dfd02011-01-26 23:14:33 +00001204 return false;
1205 addr += reg_byte_size;
1206 }
1207
1208 context.type = EmulateInstruction::eContextAdjustStackPointer;
1209 context.arg0 = eRegisterKindGeneric;
1210 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +00001211 context.arg2 = -sp_offset;
Johnny Chen799dfd02011-01-26 23:14:33 +00001212
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001213 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chen799dfd02011-01-26 23:14:33 +00001214 return false;
1215 }
1216 return true;
1217}
1218
Johnny Chen587a0a42011-02-01 18:35:28 +00001219// Vector Pop loads multiple extension registers from the stack.
1220// It also updates SP to point just above the loaded data.
1221bool
1222EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding)
1223{
1224#if 0
1225 // ARM pseudo code...
1226 if (ConditionPassed())
1227 {
1228 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
1229 address = SP;
1230 SP = SP + imm32;
1231 if single_regs then
1232 for r = 0 to regs-1
1233 S[d+r] = MemA[address,4]; address = address+4;
1234 else
1235 for r = 0 to regs-1
1236 word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8;
1237 // Combine the word-aligned words in the correct order for current endianness.
1238 D[d+r] = if BigEndian() then word1:word2 else word2:word1;
1239 }
1240#endif
1241
1242 bool success = false;
1243 const uint32_t opcode = OpcodeAsUnsigned (&success);
1244 if (!success)
1245 return false;
1246
1247 if (ConditionPassed())
1248 {
1249 const uint32_t addr_byte_size = GetAddressByteSize();
1250 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
1251 if (!success)
1252 return false;
1253 bool single_regs;
1254 uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register
1255 uint32_t imm32; // stack offset
1256 uint32_t regs; // number of registers
1257 switch (encoding) {
1258 case eEncodingT1:
1259 case eEncodingA1:
1260 single_regs = false;
1261 d = Bits32(opcode, 22, 22) << 4 | Bits32(opcode, 15, 12);
1262 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1263 // If UInt(imm8) is odd, see "FLDMX".
1264 regs = Bits32(opcode, 7, 0) / 2;
1265 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1266 if (regs == 0 || regs > 16 || (d + regs) > 32)
1267 return false;
1268 break;
1269 case eEncodingT2:
1270 case eEncodingA2:
1271 single_regs = true;
1272 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
1273 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1274 regs = Bits32(opcode, 7, 0);
1275 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1276 if (regs == 0 || regs > 16 || (d + regs) > 32)
1277 return false;
1278 break;
1279 default:
1280 return false;
1281 }
1282 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
1283 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
1284 addr_t sp_offset = imm32;
1285 addr_t addr = sp;
1286 uint32_t i;
1287 uint64_t data; // uint64_t to accomodate 64-bit registers.
1288
1289 EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 };
1290 for (i=d; i<regs; ++i)
1291 {
1292 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
1293 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
1294 data = ReadMemoryUnsigned(context, addr, reg_byte_size, 0, &success);
1295 if (!success)
1296 return false;
1297 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data))
1298 return false;
1299 addr += reg_byte_size;
1300 }
1301
1302 context.type = EmulateInstruction::eContextAdjustStackPointer;
1303 context.arg0 = eRegisterKindGeneric;
1304 context.arg1 = LLDB_REGNUM_GENERIC_SP;
1305 context.arg2 = sp_offset;
1306
1307 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
1308 return false;
1309 }
1310 return true;
1311}
1312
Johnny Chenb77be412011-02-04 00:40:18 +00001313// SVC (previously SWI)
1314bool
1315EmulateInstructionARM::EmulateSVC (ARMEncoding encoding)
1316{
1317#if 0
1318 // ARM pseudo code...
1319 if (ConditionPassed())
1320 {
1321 EncodingSpecificOperations();
1322 CallSupervisor();
1323 }
1324#endif
1325
1326 bool success = false;
1327 const uint32_t opcode = OpcodeAsUnsigned (&success);
1328 if (!success)
1329 return false;
1330
1331 if (ConditionPassed())
1332 {
1333 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1334 addr_t lr; // next instruction address
1335 if (!success)
1336 return false;
1337 uint32_t imm32; // the immediate constant
1338 uint32_t mode; // ARM or Thumb mode
1339 switch (encoding) {
1340 case eEncodingT1:
1341 lr = (pc + 2) | 1u; // return address
1342 imm32 = Bits32(opcode, 7, 0);
1343 mode = eModeThumb;
1344 break;
1345 case eEncodingA1:
1346 lr = pc + 4; // return address
1347 imm32 = Bits32(opcode, 23, 0);
1348 mode = eModeARM;
1349 break;
1350 default:
1351 return false;
1352 }
1353 EmulateInstruction::Context context = { EmulateInstruction::eContextSupervisorCall, mode, imm32, 0};
1354 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1355 return false;
1356 }
1357 return true;
1358}
1359
Johnny Chenc315f862011-02-05 00:46:10 +00001360// If Then makes up to four following instructions (the IT block) conditional.
1361bool
1362EmulateInstructionARM::EmulateIT (ARMEncoding encoding)
1363{
1364#if 0
1365 // ARM pseudo code...
1366 EncodingSpecificOperations();
1367 ITSTATE.IT<7:0> = firstcond:mask;
1368#endif
1369
1370 bool success = false;
1371 const uint32_t opcode = OpcodeAsUnsigned (&success);
1372 if (!success)
1373 return false;
1374
1375 m_it_session.InitIT(Bits32(opcode, 7, 0));
1376 return true;
1377}
1378
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001379EmulateInstructionARM::ARMOpcode*
1380EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode)
Greg Clayton64c84432011-01-21 22:02:52 +00001381{
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001382 static ARMOpcode
1383 g_arm_opcodes[] =
1384 {
1385 //----------------------------------------------------------------------
1386 // Prologue instructions
1387 //----------------------------------------------------------------------
Johnny Chenfdd179e2011-01-31 20:09:28 +00001388
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001389 // push register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00001390 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePush, "push <registers>" },
1391 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePush, "push <register>" },
Johnny Chenbcec3af2011-01-27 01:26:19 +00001392
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001393 // set r7 to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00001394 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #<const>" },
1395 { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubR7IPImmediate, "sub r7, ip, #<const>"},
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001396 // set ip to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00001397 { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMovRdSP, "mov ip, sp" },
1398 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add ip, sp, #<const>" },
1399 { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubIPSPImmediate, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001400
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001401 // adjust the stack pointer
Johnny Chenc28a76d2011-02-01 18:51:48 +00001402 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +00001403
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001404 // push one register
1405 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chenc28a76d2011-02-01 18:51:48 +00001406 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +00001407
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001408 // vector push consecutive extension register(s)
Johnny Chen9b8d7832011-02-02 01:13:56 +00001409 { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
1410 { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenef85e912011-01-31 23:07:40 +00001411
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001412 //----------------------------------------------------------------------
Johnny Chen587a0a42011-02-01 18:35:28 +00001413 // Epilogue instructions
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001414 //----------------------------------------------------------------------
Johnny Chenef85e912011-01-31 23:07:40 +00001415
Johnny Chen9b8d7832011-02-02 01:13:56 +00001416 // To resolve ambiguity, "blx <label>" should come before "bl <label>".
1417 { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"},
1418 { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"},
1419 { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"},
Johnny Chenc28a76d2011-02-01 18:51:48 +00001420 { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePop, "pop <registers>"},
1421 { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePop, "pop <register>"},
Johnny Chen9b8d7832011-02-02 01:13:56 +00001422 { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"},
Johnny Chenb77be412011-02-04 00:40:18 +00001423 { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"},
1424
1425 //----------------------------------------------------------------------
1426 // Supervisor Call (previously Software Interrupt)
1427 //----------------------------------------------------------------------
1428 { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"}
1429
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001430 };
1431 static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
1432
1433 for (size_t i=0; i<k_num_arm_opcodes; ++i)
1434 {
1435 if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value)
1436 return &g_arm_opcodes[i];
1437 }
1438 return NULL;
1439}
Greg Clayton64c84432011-01-21 22:02:52 +00001440
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001441
1442EmulateInstructionARM::ARMOpcode*
1443EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode)
Johnny Chen347320d2011-01-24 23:40:59 +00001444{
Johnny Chenfdd179e2011-01-31 20:09:28 +00001445
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001446 static ARMOpcode
1447 g_thumb_opcodes[] =
1448 {
1449 //----------------------------------------------------------------------
1450 // Prologue instructions
1451 //----------------------------------------------------------------------
Johnny Chenbcec3af2011-01-27 01:26:19 +00001452
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001453 // push register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00001454 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePush, "push <registers>" },
1455 { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <registers>" },
1456 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <register>" },
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001457 // move from high register to low register
Johnny Chenc28a76d2011-02-01 18:51:48 +00001458 { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovLowHigh, "mov r0-r7, r8-r15" },
Johnny Chen788e0552011-01-27 22:52:23 +00001459
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001460 // set r7 to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00001461 { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #imm" },
1462 { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdSP, "mov r7, sp" },
Johnny Chen60c0d622011-01-25 23:49:39 +00001463
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001464 // PC relative load into register (see also EmulateAddSPRm)
Johnny Chenc28a76d2011-02-01 18:51:48 +00001465 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRdPCRelative, "ldr <Rd>, [PC, #imm]"},
Johnny Chen799dfd02011-01-26 23:14:33 +00001466
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001467 // adjust the stack pointer
Johnny Chenc28a76d2011-02-01 18:51:48 +00001468 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPRm, "add sp, <Rm>"},
1469 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSubSPImmdiate, "add sp, sp, #imm"},
1470 { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub.w sp, sp, #<const>"},
1471 { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "subw sp, sp, #imm12"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00001472
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001473 // vector push consecutive extension register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00001474 { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
1475 { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00001476
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001477 //----------------------------------------------------------------------
1478 // Epilogue instructions
1479 //----------------------------------------------------------------------
Johnny Chen347320d2011-01-24 23:40:59 +00001480
Johnny Chenc28a76d2011-02-01 18:51:48 +00001481 { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPImmediate, "add sp, #imm"},
Johnny Chen9b8d7832011-02-02 01:13:56 +00001482 { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"},
1483 // J1 == J2 == 1
1484 { 0xf800e801, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"},
Johnny Chenc28a76d2011-02-01 18:51:48 +00001485 { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePop, "pop <registers>"},
1486 { 0xffff0000, 0xe8bd0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <registers>" },
1487 { 0xffff0fff, 0xf85d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <register>" },
1488 { 0xffbf0f00, 0xecbd0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"},
Johnny Chenb77be412011-02-04 00:40:18 +00001489 { 0xffbf0f00, 0xecbd0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"},
1490
1491 //----------------------------------------------------------------------
1492 // Supervisor Call (previously Software Interrupt)
1493 //----------------------------------------------------------------------
Johnny Chenc315f862011-02-05 00:46:10 +00001494 { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"},
1495
1496 //----------------------------------------------------------------------
1497 // If Then makes up to four following instructions conditional.
1498 //----------------------------------------------------------------------
1499 { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"}
Johnny Chenb77be412011-02-04 00:40:18 +00001500
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001501 };
1502
1503 const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
1504 for (size_t i=0; i<k_num_thumb_opcodes; ++i)
1505 {
1506 if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value)
1507 return &g_thumb_opcodes[i];
1508 }
1509 return NULL;
1510}
Greg Clayton64c84432011-01-21 22:02:52 +00001511
Greg Clayton31e2a382011-01-30 20:03:56 +00001512bool
1513EmulateInstructionARM::SetTargetTriple (const ConstString &triple)
1514{
1515 m_arm_isa = 0;
1516 const char *triple_cstr = triple.GetCString();
1517 if (triple_cstr)
1518 {
1519 const char *dash = ::strchr (triple_cstr, '-');
1520 if (dash)
1521 {
1522 std::string arch (triple_cstr, dash);
1523 const char *arch_cstr = arch.c_str();
1524 if (strcasecmp(arch_cstr, "armv4t") == 0)
1525 m_arm_isa = ARMv4T;
1526 else if (strcasecmp(arch_cstr, "armv4") == 0)
1527 m_arm_isa = ARMv4;
1528 else if (strcasecmp(arch_cstr, "armv5tej") == 0)
1529 m_arm_isa = ARMv5TEJ;
1530 else if (strcasecmp(arch_cstr, "armv5te") == 0)
1531 m_arm_isa = ARMv5TE;
1532 else if (strcasecmp(arch_cstr, "armv5t") == 0)
1533 m_arm_isa = ARMv5T;
1534 else if (strcasecmp(arch_cstr, "armv6k") == 0)
1535 m_arm_isa = ARMv6K;
1536 else if (strcasecmp(arch_cstr, "armv6") == 0)
1537 m_arm_isa = ARMv6;
1538 else if (strcasecmp(arch_cstr, "armv6t2") == 0)
1539 m_arm_isa = ARMv6T2;
1540 else if (strcasecmp(arch_cstr, "armv7") == 0)
1541 m_arm_isa = ARMv7;
1542 else if (strcasecmp(arch_cstr, "armv8") == 0)
1543 m_arm_isa = ARMv8;
1544 }
1545 }
1546 return m_arm_isa != 0;
1547}
1548
1549
Greg Clayton64c84432011-01-21 22:02:52 +00001550bool
1551EmulateInstructionARM::ReadInstruction ()
1552{
1553 bool success = false;
1554 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
1555 if (success)
1556 {
1557 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
1558 if (success)
1559 {
1560 Context read_inst_context = {eContextReadOpcode, 0, 0};
1561 if (m_inst_cpsr & MASK_CPSR_T)
1562 {
1563 m_inst_mode = eModeThumb;
1564 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
1565
1566 if (success)
1567 {
1568 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
1569 {
1570 m_inst.opcode_type = eOpcode16;
1571 m_inst.opcode.inst16 = thumb_opcode;
1572 }
1573 else
1574 {
1575 m_inst.opcode_type = eOpcode32;
1576 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
1577 }
1578 }
1579 }
1580 else
1581 {
1582 m_inst_mode = eModeARM;
1583 m_inst.opcode_type = eOpcode32;
1584 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
1585 }
1586 }
1587 }
1588 if (!success)
1589 {
1590 m_inst_mode = eModeInvalid;
1591 m_inst_pc = LLDB_INVALID_ADDRESS;
1592 }
1593 return success;
1594}
1595
1596uint32_t
1597EmulateInstructionARM::CurrentCond ()
1598{
1599 switch (m_inst_mode)
1600 {
1601 default:
1602 case eModeInvalid:
1603 break;
1604
1605 case eModeARM:
1606 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
1607
1608 case eModeThumb:
Johnny Chenc315f862011-02-05 00:46:10 +00001609 return m_it_session.GetCond();
Greg Clayton64c84432011-01-21 22:02:52 +00001610 }
1611 return UINT32_MAX; // Return invalid value
1612}
1613bool
1614EmulateInstructionARM::ConditionPassed ()
1615{
1616 if (m_inst_cpsr == 0)
1617 return false;
1618
1619 const uint32_t cond = CurrentCond ();
1620
1621 if (cond == UINT32_MAX)
1622 return false;
1623
1624 bool result = false;
1625 switch (UnsignedBits(cond, 3, 1))
1626 {
1627 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
1628 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
1629 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
1630 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
1631 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
1632 case 5:
1633 {
1634 bool n = (m_inst_cpsr & MASK_CPSR_N);
1635 bool v = (m_inst_cpsr & MASK_CPSR_V);
1636 result = n == v;
1637 }
1638 break;
1639 case 6:
1640 {
1641 bool n = (m_inst_cpsr & MASK_CPSR_N);
1642 bool v = (m_inst_cpsr & MASK_CPSR_V);
1643 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
1644 }
1645 break;
1646 case 7:
1647 result = true;
1648 break;
1649 }
1650
1651 if (cond & 1)
1652 result = !result;
1653 return result;
1654}
1655
1656
1657bool
1658EmulateInstructionARM::EvaluateInstruction ()
1659{
Johnny Chenc315f862011-02-05 00:46:10 +00001660 // Advance the ITSTATE bits to their values for the next instruction.
1661 if (m_inst_mode == eModeThumb && m_it_session.InITBlock())
1662 m_it_session.ITAdvance();
1663
Greg Clayton64c84432011-01-21 22:02:52 +00001664 return false;
1665}