blob: a3248a9404b5fc76b71e64aaca215bd8cc53749e [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"
Greg Clayton64c84432011-01-21 22:02:52 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20// ARM constants used during decoding
21#define REG_RD 0
22#define LDM_REGLIST 1
23#define PC_REG 15
24#define PC_REGLIST_BIT 0x8000
25
Johnny Chen251af6a2011-01-21 22:47:25 +000026#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +000027#define ARMv4T (1u << 1)
28#define ARMv5T (1u << 2)
29#define ARMv5TE (1u << 3)
30#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +000031#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +000032#define ARMv6K (1u << 6)
33#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +000034#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +000035#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +000036#define ARMvAll (0xffffffffu)
37
Greg Clayton64c84432011-01-21 22:02:52 +000038
Greg Clayton2b8e8b02011-02-01 00:49:32 +000039void
40EmulateInstructionARM::Initialize ()
Johnny Chen7dc60e12011-01-24 19:46:32 +000041{
Greg Clayton2b8e8b02011-02-01 00:49:32 +000042}
Johnny Chen7dc60e12011-01-24 19:46:32 +000043
Greg Clayton2b8e8b02011-02-01 00:49:32 +000044void
45EmulateInstructionARM::Terminate ()
Greg Clayton64c84432011-01-21 22:02:52 +000046{
Greg Clayton2b8e8b02011-02-01 00:49:32 +000047}
48
Greg Clayton64c84432011-01-21 22:02:52 +000049
Johnny Chen08c25e82011-01-31 18:02:28 +000050// Push Multiple Registers stores multiple registers to the stack, storing to
51// consecutive memory locations ending just below the address in SP, and updates
52// SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +000053bool
54EmulateInstructionARM::EmulatePush (ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +000055{
56#if 0
57 // ARM pseudo code...
58 if (ConditionPassed())
59 {
60 EncodingSpecificOperations();
61 NullCheckIfThumbEE(13);
62 address = SP - 4*BitCount(registers);
63
64 for (i = 0 to 14)
65 {
66 if (registers<i> == 1’)
67 {
68 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
69 MemA[address,4] = bits(32) UNKNOWN;
70 else
71 MemA[address,4] = R[i];
72 address = address + 4;
73 }
74 }
75
76 if (registers<15> == 1’) // Only possible for encoding A1 or A2
77 MemA[address,4] = PCStoreValue();
78
79 SP = SP - 4*BitCount(registers);
80 }
81#endif
82
83 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +000084 const uint32_t opcode = OpcodeAsUnsigned (&success);
Greg Clayton64c84432011-01-21 22:02:52 +000085 if (!success)
86 return false;
87
Greg Clayton2b8e8b02011-02-01 00:49:32 +000088 if (ConditionPassed())
Greg Clayton64c84432011-01-21 22:02:52 +000089 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +000090 const uint32_t addr_byte_size = GetAddressByteSize();
91 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +000092 if (!success)
93 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +000094 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +000095 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +000096 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +000097 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +000098 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +000099 // The M bit represents LR.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000100 if (Bits32(opcode, 8, 8))
Johnny Chenef85e912011-01-31 23:07:40 +0000101 registers |= (1u << 14);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000102 // if BitCount(registers) < 1 then UNPREDICTABLE;
103 if (BitCount(registers) < 1)
104 return false;
105 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000106 case eEncodingT2:
107 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000108 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000109 // if BitCount(registers) < 2 then UNPREDICTABLE;
110 if (BitCount(registers) < 2)
111 return false;
112 break;
113 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000114 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000115 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000116 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000117 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000118 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000119 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000120 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000121 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000122 // Instead of return false, let's handle the following case as well,
123 // which amounts to pushing one reg onto the full descending stacks.
124 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000125 break;
126 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000127 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000128 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000129 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000130 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000131 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000132 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000133 default:
134 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000135 }
Johnny Chence1ca772011-01-25 01:13:00 +0000136 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000137 addr_t addr = sp - sp_offset;
138 uint32_t i;
139
140 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
141 for (i=0; i<15; ++i)
142 {
Johnny Chen108d5aa2011-01-26 01:00:55 +0000143 if (BitIsSet (registers, 1u << i))
Greg Clayton64c84432011-01-21 22:02:52 +0000144 {
145 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
146 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000147 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000148 if (!success)
149 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000150 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000151 return false;
152 addr += addr_byte_size;
153 }
154 }
155
Johnny Chen108d5aa2011-01-26 01:00:55 +0000156 if (BitIsSet (registers, 1u << 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000157 {
158 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
Johnny Chen3c75c762011-01-22 00:47:08 +0000159 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000160 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000161 if (!success)
162 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000163 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000164 return false;
165 }
166
167 context.type = EmulateInstruction::eContextAdjustStackPointer;
168 context.arg0 = eRegisterKindGeneric;
169 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000170 context.arg2 = -sp_offset;
Greg Clayton64c84432011-01-21 22:02:52 +0000171
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000172 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Greg Clayton64c84432011-01-21 22:02:52 +0000173 return false;
174 }
175 return true;
176}
177
Johnny Chenef85e912011-01-31 23:07:40 +0000178// Pop Multiple Registers loads multiple registers from the stack, loading from
179// consecutive memory locations staring at the address in SP, and updates
180// SP to point just above the loaded data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000181bool
182EmulateInstructionARM::EmulatePop (ARMEncoding encoding)
Johnny Chenef85e912011-01-31 23:07:40 +0000183{
184#if 0
185 // ARM pseudo code...
186 if (ConditionPassed())
187 {
188 EncodingSpecificOperations(); NullCheckIfThumbEE(13);
189 address = SP;
190 for i = 0 to 14
191 if registers<i> == 1 then
192 R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4;
193 if registers<15> == 1 then
194 if UnalignedAllowed then
195 LoadWritePC(MemU[address,4]);
196 else
197 LoadWritePC(MemA[address,4]);
198 if registers<13> == 0 then SP = SP + 4*BitCount(registers);
199 if registers<13> == 1 then SP = bits(32) UNKNOWN;
200 }
201#endif
202
203 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000204 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenef85e912011-01-31 23:07:40 +0000205 if (!success)
206 return false;
207
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000208 if (ConditionPassed())
Johnny Chenef85e912011-01-31 23:07:40 +0000209 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000210 const uint32_t addr_byte_size = GetAddressByteSize();
211 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000212 if (!success)
213 return false;
214 uint32_t registers = 0;
215 uint32_t Rt; // the destination register
216 switch (encoding) {
217 case eEncodingT1:
218 registers = Bits32(opcode, 7, 0);
219 // The P bit represents PC.
220 if (Bits32(opcode, 8, 8))
221 registers |= (1u << 15);
222 // if BitCount(registers) < 1 then UNPREDICTABLE;
223 if (BitCount(registers) < 1)
224 return false;
225 break;
226 case eEncodingT2:
227 // Ignore bit 13.
228 registers = Bits32(opcode, 15, 0) & ~0x2000;
229 // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE;
230 if (BitCount(registers) < 2 || (Bits32(opcode, 15, 15) && Bits32(opcode, 14, 14)))
231 return false;
232 break;
233 case eEncodingT3:
234 Rt = Bits32(opcode, 15, 12);
235 // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE;
236 if (Rt == dwarf_sp)
237 return false;
238 registers = (1u << Rt);
239 break;
240 case eEncodingA1:
241 registers = Bits32(opcode, 15, 0);
242 // Instead of return false, let's handle the following case as well,
243 // which amounts to popping one reg from the full descending stacks.
244 // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD;
245
246 // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE;
247 if (Bits32(opcode, 13, 13))
248 return false;
249 break;
250 case eEncodingA2:
251 Rt = Bits32(opcode, 15, 12);
252 // if t == 13 then UNPREDICTABLE;
253 if (Rt == dwarf_sp)
254 return false;
255 registers = (1u << Rt);
256 break;
257 default:
258 return false;
259 }
260 addr_t sp_offset = addr_byte_size * BitCount (registers);
261 addr_t addr = sp;
262 uint32_t i, data;
263
264 EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 };
265 for (i=0; i<15; ++i)
266 {
267 if (BitIsSet (registers, 1u << i))
268 {
269 context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number
270 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000271 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000272 if (!success)
273 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000274 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000275 return false;
276 addr += addr_byte_size;
277 }
278 }
279
280 if (BitIsSet (registers, 1u << 15))
281 {
282 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
283 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000284 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000285 if (!success)
286 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000287 if (!WriteRegisterUnsigned(context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000288 return false;
289 addr += addr_byte_size;
290 }
291
292 context.type = EmulateInstruction::eContextAdjustStackPointer;
293 context.arg0 = eRegisterKindGeneric;
294 context.arg1 = LLDB_REGNUM_GENERIC_SP;
295 context.arg2 = sp_offset;
296
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000297 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
Johnny Chenef85e912011-01-31 23:07:40 +0000298 return false;
299 }
300 return true;
301}
302
Johnny Chen5b442b72011-01-27 19:34:30 +0000303// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000304// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000305bool
306EmulateInstructionARM::EmulateAddRdSPImmediate (ARMEncoding encoding)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000307{
308#if 0
309 // ARM pseudo code...
310 if (ConditionPassed())
311 {
312 EncodingSpecificOperations();
313 (result, carry, overflow) = AddWithCarry(SP, imm32, 0’);
314 if d == 15 then
315 ALUWritePC(result); // setflags is always FALSE here
316 else
317 R[d] = result;
318 if setflags then
319 APSR.N = result<31>;
320 APSR.Z = IsZeroBit(result);
321 APSR.C = carry;
322 APSR.V = overflow;
323 }
324#endif
325
326 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000327 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000328 if (!success)
329 return false;
330
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000331 if (ConditionPassed())
Johnny Chenbcec3af2011-01-27 01:26:19 +0000332 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000333 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000334 if (!success)
335 return false;
336 uint32_t Rd; // the destination register
337 uint32_t imm32;
338 switch (encoding) {
339 case eEncodingT1:
340 Rd = 7;
341 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
342 break;
343 case eEncodingA1:
344 Rd = Bits32(opcode, 15, 12);
345 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
346 break;
347 default:
348 return false;
349 }
350 addr_t sp_offset = imm32;
351 addr_t addr = sp + sp_offset; // a pointer to the stack area
352
353 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
354 eRegisterKindGeneric,
355 LLDB_REGNUM_GENERIC_SP,
356 sp_offset };
357
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000358 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
Johnny Chenbcec3af2011-01-27 01:26:19 +0000359 return false;
360 }
361 return true;
362}
363
Johnny Chen2ccad832011-01-28 19:57:25 +0000364// Set r7 or ip to the current stack pointer.
365// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000366bool
367EmulateInstructionARM::EmulateMovRdSP (ARMEncoding encoding)
Johnny Chen2ccad832011-01-28 19:57:25 +0000368{
369#if 0
370 // ARM pseudo code...
371 if (ConditionPassed())
372 {
373 EncodingSpecificOperations();
374 result = R[m];
375 if d == 15 then
376 ALUWritePC(result); // setflags is always FALSE here
377 else
378 R[d] = result;
379 if setflags then
380 APSR.N = result<31>;
381 APSR.Z = IsZeroBit(result);
382 // APSR.C unchanged
383 // APSR.V unchanged
384 }
385#endif
386
387 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000388 //const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000389 //if (!success)
390 // return false;
Johnny Chen2ccad832011-01-28 19:57:25 +0000391
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000392 if (ConditionPassed())
Johnny Chen2ccad832011-01-28 19:57:25 +0000393 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000394 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen2ccad832011-01-28 19:57:25 +0000395 if (!success)
396 return false;
397 uint32_t Rd; // the destination register
398 switch (encoding) {
399 case eEncodingT1:
400 Rd = 7;
401 break;
402 case eEncodingA1:
403 Rd = 12;
404 break;
405 default:
406 return false;
407 }
408 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
409 eRegisterKindGeneric,
410 LLDB_REGNUM_GENERIC_SP,
411 0 };
412
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000413 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp))
Johnny Chen2ccad832011-01-28 19:57:25 +0000414 return false;
415 }
416 return true;
417}
418
Johnny Chen1c13b622011-01-29 00:11:15 +0000419// Move from high register (r8-r15) to low register (r0-r7).
420// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000421bool
422EmulateInstructionARM::EmulateMovLowHigh (ARMEncoding encoding)
Johnny Chen1c13b622011-01-29 00:11:15 +0000423{
424#if 0
425 // ARM pseudo code...
426 if (ConditionPassed())
427 {
428 EncodingSpecificOperations();
429 result = R[m];
430 if d == 15 then
431 ALUWritePC(result); // setflags is always FALSE here
432 else
433 R[d] = result;
434 if setflags then
435 APSR.N = result<31>;
436 APSR.Z = IsZeroBit(result);
437 // APSR.C unchanged
438 // APSR.V unchanged
439 }
440#endif
441
442 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000443 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000444 if (!success)
445 return false;
446
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000447 if (ConditionPassed())
Johnny Chen1c13b622011-01-29 00:11:15 +0000448 {
449 uint32_t Rm; // the source register
450 uint32_t Rd; // the destination register
451 switch (encoding) {
452 case eEncodingT1:
453 Rm = Bits32(opcode, 6, 3);
454 Rd = Bits32(opcode, 2, 1); // bits(7) == 0
455 break;
456 default:
457 return false;
458 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000459 int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000460 if (!success)
461 return false;
462
463 // The context specifies that Rm is to be moved into Rd.
464 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
465 eRegisterKindDWARF,
466 dwarf_r0 + Rm,
467 0 };
468
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000469 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value))
Johnny Chen1c13b622011-01-29 00:11:15 +0000470 return false;
471 }
472 return true;
473}
474
Johnny Chen788e0552011-01-27 22:52:23 +0000475// PC relative immediate load into register, possibly followed by ADD (SP plus register).
476// LDR (literal)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000477bool
478EmulateInstructionARM::EmulateLDRRdPCRelative (ARMEncoding encoding)
Johnny Chen788e0552011-01-27 22:52:23 +0000479{
480#if 0
481 // ARM pseudo code...
482 if (ConditionPassed())
483 {
484 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
485 base = Align(PC,4);
486 address = if add then (base + imm32) else (base - imm32);
487 data = MemU[address,4];
488 if t == 15 then
489 if address<1:0> == 00 then LoadWritePC(data); else UNPREDICTABLE;
490 elsif UnalignedSupport() || address<1:0> = 00 then
491 R[t] = data;
492 else // Can only apply before ARMv7
493 if CurrentInstrSet() == InstrSet_ARM then
494 R[t] = ROR(data, 8*UInt(address<1:0>));
495 else
496 R[t] = bits(32) UNKNOWN;
497 }
498#endif
499
500 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000501 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen788e0552011-01-27 22:52:23 +0000502 if (!success)
503 return false;
504
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000505 if (ConditionPassed())
Johnny Chen788e0552011-01-27 22:52:23 +0000506 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000507 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000508 if (!success)
509 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000510
511 // PC relative immediate load context
512 EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset,
513 eRegisterKindGeneric,
514 LLDB_REGNUM_GENERIC_PC,
515 0};
Johnny Chen788e0552011-01-27 22:52:23 +0000516 uint32_t Rd; // the destination register
517 uint32_t imm32; // immediate offset from the PC
518 addr_t addr; // the PC relative address
519 uint32_t data; // the literal data value from the PC relative load
520 switch (encoding) {
521 case eEncodingT1:
522 Rd = Bits32(opcode, 10, 8);
523 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
524 addr = pc + 4 + imm32;
Johnny Chen809742e2011-01-28 00:32:27 +0000525 context.arg2 = 4 + imm32;
Johnny Chen788e0552011-01-27 22:52:23 +0000526 break;
527 default:
528 return false;
529 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000530 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000531 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000532 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000533 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data))
Johnny Chen788e0552011-01-27 22:52:23 +0000534 return false;
535 }
536 return true;
537}
538
Johnny Chen5b442b72011-01-27 19:34:30 +0000539// An add operation to adjust the SP.
Johnny Chenfdd179e2011-01-31 20:09:28 +0000540// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000541bool
542EmulateInstructionARM::EmulateAddSPImmediate (ARMEncoding encoding)
Johnny Chenfdd179e2011-01-31 20:09:28 +0000543{
544#if 0
545 // ARM pseudo code...
546 if (ConditionPassed())
547 {
548 EncodingSpecificOperations();
549 (result, carry, overflow) = AddWithCarry(SP, imm32, 0’);
550 if d == 15 then // Can only occur for ARM encoding
551 ALUWritePC(result); // setflags is always FALSE here
552 else
553 R[d] = result;
554 if setflags then
555 APSR.N = result<31>;
556 APSR.Z = IsZeroBit(result);
557 APSR.C = carry;
558 APSR.V = overflow;
559 }
560#endif
561
562 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000563 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000564 if (!success)
565 return false;
566
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000567 if (ConditionPassed())
Johnny Chenfdd179e2011-01-31 20:09:28 +0000568 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000569 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000570 if (!success)
571 return false;
572 uint32_t imm32; // the immediate operand
573 switch (encoding) {
574 case eEncodingT2:
575 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
576 break;
577 default:
578 return false;
579 }
580 addr_t sp_offset = imm32;
581 addr_t addr = sp + sp_offset; // the adjusted stack pointer value
582
583 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
584 eRegisterKindGeneric,
585 LLDB_REGNUM_GENERIC_SP,
586 sp_offset };
587
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000588 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chenfdd179e2011-01-31 20:09:28 +0000589 return false;
590 }
591 return true;
592}
593
594// An add operation to adjust the SP.
Johnny Chen5b442b72011-01-27 19:34:30 +0000595// ADD (SP plus register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000596bool
597EmulateInstructionARM::EmulateAddSPRm (ARMEncoding encoding)
Johnny Chen5b442b72011-01-27 19:34:30 +0000598{
599#if 0
600 // ARM pseudo code...
601 if (ConditionPassed())
602 {
603 EncodingSpecificOperations();
604 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
605 (result, carry, overflow) = AddWithCarry(SP, shifted, 0’);
606 if d == 15 then
607 ALUWritePC(result); // setflags is always FALSE here
608 else
609 R[d] = result;
610 if setflags then
611 APSR.N = result<31>;
612 APSR.Z = IsZeroBit(result);
613 APSR.C = carry;
614 APSR.V = overflow;
615 }
616#endif
617
618 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000619 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen5b442b72011-01-27 19:34:30 +0000620 if (!success)
621 return false;
622
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000623 if (ConditionPassed())
Johnny Chen5b442b72011-01-27 19:34:30 +0000624 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000625 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +0000626 if (!success)
627 return false;
628 uint32_t Rm; // the second operand
629 switch (encoding) {
630 case eEncodingT2:
631 Rm = Bits32(opcode, 6, 3);
632 break;
633 default:
634 return false;
635 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000636 int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +0000637 if (!success)
638 return false;
639
640 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
641
642 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
643 eRegisterKindGeneric,
644 LLDB_REGNUM_GENERIC_SP,
645 reg_value };
646
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000647 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen5b442b72011-01-27 19:34:30 +0000648 return false;
649 }
650 return true;
651}
652
Johnny Chen0d0148e2011-01-28 02:26:08 +0000653// Set r7 to point to some ip offset.
654// SUB (immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000655bool
656EmulateInstructionARM::EmulateSubR7IPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +0000657{
658#if 0
659 // ARM pseudo code...
660 if (ConditionPassed())
661 {
662 EncodingSpecificOperations();
663 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
664 if d == 15 then // Can only occur for ARM encoding
665 ALUWritePC(result); // setflags is always FALSE here
666 else
667 R[d] = result;
668 if setflags then
669 APSR.N = result<31>;
670 APSR.Z = IsZeroBit(result);
671 APSR.C = carry;
672 APSR.V = overflow;
673 }
674#endif
675
676 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000677 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000678 if (!success)
679 return false;
680
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000681 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +0000682 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000683 const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000684 if (!success)
685 return false;
686 uint32_t imm32;
687 switch (encoding) {
688 case eEncodingA1:
689 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
690 break;
691 default:
692 return false;
693 }
694 addr_t ip_offset = imm32;
695 addr_t addr = ip - ip_offset; // the adjusted ip value
696
697 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
698 eRegisterKindDWARF,
699 dwarf_r12,
700 -ip_offset };
701
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000702 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +0000703 return false;
704 }
705 return true;
706}
707
708// Set ip to point to some stack offset.
709// SUB (SP minus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000710bool
711EmulateInstructionARM::EmulateSubIPSPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +0000712{
713#if 0
714 // ARM pseudo code...
715 if (ConditionPassed())
716 {
717 EncodingSpecificOperations();
718 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
719 if d == 15 then // Can only occur for ARM encoding
720 ALUWritePC(result); // setflags is always FALSE here
721 else
722 R[d] = result;
723 if setflags then
724 APSR.N = result<31>;
725 APSR.Z = IsZeroBit(result);
726 APSR.C = carry;
727 APSR.V = overflow;
728 }
729#endif
730
731 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000732 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000733 if (!success)
734 return false;
735
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000736 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +0000737 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000738 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +0000739 if (!success)
740 return false;
741 uint32_t imm32;
742 switch (encoding) {
743 case eEncodingA1:
744 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
745 break;
746 default:
747 return false;
748 }
749 addr_t sp_offset = imm32;
750 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
751
752 EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset,
753 eRegisterKindGeneric,
754 LLDB_REGNUM_GENERIC_SP,
755 -sp_offset };
756
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000757 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +0000758 return false;
759 }
760 return true;
761}
762
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000763// A sub operation to adjust the SP -- allocate space for local storage.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000764bool
765EmulateInstructionARM::EmulateSubSPImmdiate (ARMEncoding encoding)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000766{
767#if 0
768 // ARM pseudo code...
769 if (ConditionPassed())
770 {
771 EncodingSpecificOperations();
772 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), 1’);
773 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +0000774 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000775 else
776 R[d] = result;
777 if setflags then
778 APSR.N = result<31>;
779 APSR.Z = IsZeroBit(result);
780 APSR.C = carry;
781 APSR.V = overflow;
782 }
783#endif
784
785 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000786 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000787 if (!success)
788 return false;
789
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000790 if (ConditionPassed())
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000791 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000792 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000793 if (!success)
794 return false;
795 uint32_t imm32;
796 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +0000797 case eEncodingT1:
798 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +0000799 case eEncodingT2:
800 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
801 break;
802 case eEncodingT3:
803 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
804 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000805 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +0000806 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000807 break;
808 default:
809 return false;
810 }
811 addr_t sp_offset = imm32;
812 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
813
814 EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer,
815 eRegisterKindGeneric,
816 LLDB_REGNUM_GENERIC_SP,
Johnny Chen5b442b72011-01-27 19:34:30 +0000817 -sp_offset };
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000818
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000819 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen4c0e0bc2011-01-25 22:45:28 +0000820 return false;
821 }
822 return true;
823}
824
Johnny Chen08c25e82011-01-31 18:02:28 +0000825// A store operation to the stack that also updates the SP.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000826bool
827EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding)
Johnny Chence1ca772011-01-25 01:13:00 +0000828{
829#if 0
830 // ARM pseudo code...
831 if (ConditionPassed())
832 {
833 EncodingSpecificOperations();
834 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
835 address = if index then offset_addr else R[n];
836 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
837 if wback then R[n] = offset_addr;
838 }
839#endif
840
841 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000842 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chence1ca772011-01-25 01:13:00 +0000843 if (!success)
844 return false;
845
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000846 if (ConditionPassed())
Johnny Chence1ca772011-01-25 01:13:00 +0000847 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000848 const uint32_t addr_byte_size = GetAddressByteSize();
849 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +0000850 if (!success)
851 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000852 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +0000853 uint32_t imm12;
854 switch (encoding) {
855 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000856 Rt = Bits32(opcode, 15, 12);
857 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +0000858 break;
859 default:
860 return false;
861 }
862 addr_t sp_offset = imm12;
863 addr_t addr = sp - sp_offset;
864
865 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
Johnny Chen91d99862011-01-25 19:07:04 +0000866 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +0000867 {
Johnny Chen91d99862011-01-25 19:07:04 +0000868 context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number
869 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000870 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +0000871 if (!success)
872 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000873 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +0000874 return false;
875 }
876 else
877 {
878 context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number
879 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000880 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +0000881 if (!success)
882 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000883 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +0000884 return false;
885 }
886
887 context.type = EmulateInstruction::eContextAdjustStackPointer;
888 context.arg0 = eRegisterKindGeneric;
889 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000890 context.arg2 = -sp_offset;
Johnny Chence1ca772011-01-25 01:13:00 +0000891
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000892 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chence1ca772011-01-25 01:13:00 +0000893 return false;
894 }
895 return true;
896}
897
Johnny Chen08c25e82011-01-31 18:02:28 +0000898// Vector Push stores multiple extension registers to the stack.
899// It also updates SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000900bool
901EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding)
Johnny Chen799dfd02011-01-26 23:14:33 +0000902{
903#if 0
904 // ARM pseudo code...
905 if (ConditionPassed())
906 {
907 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
908 address = SP - imm32;
909 SP = SP - imm32;
910 if single_regs then
911 for r = 0 to regs-1
912 MemA[address,4] = S[d+r]; address = address+4;
913 else
914 for r = 0 to regs-1
915 // Store as two word-aligned words in the correct order for current endianness.
916 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
917 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
918 address = address+8;
919 }
920#endif
921
922 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000923 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen799dfd02011-01-26 23:14:33 +0000924 if (!success)
925 return false;
926
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000927 if (ConditionPassed())
Johnny Chen799dfd02011-01-26 23:14:33 +0000928 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000929 const uint32_t addr_byte_size = GetAddressByteSize();
930 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +0000931 if (!success)
932 return false;
933 bool single_regs;
934 uint32_t d; // UInt(Vd:D) starting register
935 uint32_t imm32; // stack offset
936 uint32_t regs; // number of registers
937 switch (encoding) {
938 case eEncodingT1:
939 case eEncodingA1:
940 single_regs = false;
941 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
942 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
943 // If UInt(imm8) is odd, see "FSTMX".
944 regs = Bits32(opcode, 7, 0) / 2;
945 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
946 if (regs == 0 || regs > 16 || (d + regs) > 32)
947 return false;
948 break;
949 case eEncodingT2:
950 case eEncodingA2:
951 single_regs = true;
952 d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22);
953 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
954 regs = Bits32(opcode, 7, 0);
955 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
956 if (regs == 0 || regs > 16 || (d + regs) > 32)
957 return false;
958 break;
959 default:
960 return false;
961 }
962 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
963 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
964 addr_t sp_offset = imm32;
965 addr_t addr = sp - sp_offset;
966 uint32_t i;
967
968 EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 };
969 for (i=d; i<regs; ++i)
970 {
971 context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number
972 context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset
973 // uint64_t to accommodate 64-bit registers.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000974 uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +0000975 if (!success)
976 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000977 if (!WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
Johnny Chen799dfd02011-01-26 23:14:33 +0000978 return false;
979 addr += reg_byte_size;
980 }
981
982 context.type = EmulateInstruction::eContextAdjustStackPointer;
983 context.arg0 = eRegisterKindGeneric;
984 context.arg1 = LLDB_REGNUM_GENERIC_SP;
Johnny Chen5b442b72011-01-27 19:34:30 +0000985 context.arg2 = -sp_offset;
Johnny Chen799dfd02011-01-26 23:14:33 +0000986
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000987 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chen799dfd02011-01-26 23:14:33 +0000988 return false;
989 }
990 return true;
991}
992
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000993EmulateInstructionARM::ARMOpcode*
994EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode)
Greg Clayton64c84432011-01-21 22:02:52 +0000995{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000996 static ARMOpcode
997 g_arm_opcodes[] =
998 {
999 //----------------------------------------------------------------------
1000 // Prologue instructions
1001 //----------------------------------------------------------------------
Johnny Chenfdd179e2011-01-31 20:09:28 +00001002
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001003 // push register(s)
1004 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulatePush, "push <registers>" },
1005 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &lldb_private::EmulateInstructionARM::EmulatePush, "push <register>" },
Johnny Chenbcec3af2011-01-27 01:26:19 +00001006
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001007 // set r7 to point to a stack offset
1008 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #<const>" },
1009 { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateSubR7IPImmediate, "sub r7, ip, #<const>"},
1010 // set ip to point to a stack offset
1011 { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateMovRdSP, "mov ip, sp" },
1012 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateAddRdSPImmediate, "add ip, sp, #<const>" },
1013 { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateSubIPSPImmediate, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001014
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001015 // adjust the stack pointer
1016 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateSubSPImmdiate, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +00001017
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001018 // push one register
1019 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
1020 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +00001021
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001022 // vector push consecutive extension register(s)
1023 { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
1024 { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, &lldb_private::EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenef85e912011-01-31 23:07:40 +00001025
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001026 //----------------------------------------------------------------------
1027 // Epilogue instructions //
1028 //----------------------------------------------------------------------
Johnny Chenef85e912011-01-31 23:07:40 +00001029
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001030 { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &lldb_private::EmulateInstructionARM::EmulatePop, "pop <registers>"},
1031 { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &lldb_private::EmulateInstructionARM::EmulatePop, "pop <register>"}
1032 };
1033 static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
1034
1035 for (size_t i=0; i<k_num_arm_opcodes; ++i)
1036 {
1037 if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value)
1038 return &g_arm_opcodes[i];
1039 }
1040 return NULL;
1041}
Greg Clayton64c84432011-01-21 22:02:52 +00001042
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001043
1044EmulateInstructionARM::ARMOpcode*
1045EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode)
Johnny Chen347320d2011-01-24 23:40:59 +00001046{
Johnny Chenfdd179e2011-01-31 20:09:28 +00001047
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001048 static ARMOpcode
1049 g_thumb_opcodes[] =
1050 {
1051 //----------------------------------------------------------------------
1052 // Prologue instructions
1053 //----------------------------------------------------------------------
Johnny Chenbcec3af2011-01-27 01:26:19 +00001054
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001055 // push register(s)
1056 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulatePush, "push <registers>" },
1057 { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, &lldb_private::EmulateInstructionARM::EmulatePush, "push.w <registers>" },
1058 { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, &lldb_private::EmulateInstructionARM::EmulatePush, "push.w <register>" },
1059 // move from high register to low register
1060 { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulateMovLowHigh, "mov r0-r7, r8-r15" },
Johnny Chen788e0552011-01-27 22:52:23 +00001061
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001062 // set r7 to point to a stack offset
1063 { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #imm" },
1064 { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulateMovRdSP, "mov r7, sp" },
Johnny Chen60c0d622011-01-25 23:49:39 +00001065
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001066 // PC relative load into register (see also EmulateAddSPRm)
1067 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulateLDRRdPCRelative, "ldr <Rd>, [PC, #imm]"},
Johnny Chen799dfd02011-01-26 23:14:33 +00001068
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001069 // adjust the stack pointer
1070 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &lldb_private::EmulateInstructionARM::EmulateAddSPRm, "add sp, <Rm>"},
1071 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulateSubSPImmdiate, "add sp, sp, #imm"},
1072 { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &lldb_private::EmulateInstructionARM::EmulateSubSPImmdiate, "sub.w sp, sp, #<const>"},
1073 { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, &lldb_private::EmulateInstructionARM::EmulateSubSPImmdiate, "subw sp, sp, #imm12"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00001074
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001075 // vector push consecutive extension register(s)
1076 { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, &lldb_private::EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
1077 { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &lldb_private::EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00001078
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001079 //----------------------------------------------------------------------
1080 // Epilogue instructions
1081 //----------------------------------------------------------------------
Johnny Chen347320d2011-01-24 23:40:59 +00001082
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001083 { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &lldb_private::EmulateInstructionARM::EmulateAddSPImmediate, "add sp, #imm"},
1084 { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &lldb_private::EmulateInstructionARM::EmulatePop, "pop <registers>"},
1085 { 0xffff0000, 0xe8bd0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, &lldb_private::EmulateInstructionARM::EmulatePop, "pop.w <registers>" },
1086 { 0xffff0fff, 0xf85d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, &lldb_private::EmulateInstructionARM::EmulatePop, "pop.w <register>" }
1087 };
1088
1089 const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
1090 for (size_t i=0; i<k_num_thumb_opcodes; ++i)
1091 {
1092 if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value)
1093 return &g_thumb_opcodes[i];
1094 }
1095 return NULL;
1096}
Greg Clayton64c84432011-01-21 22:02:52 +00001097
Greg Clayton31e2a382011-01-30 20:03:56 +00001098bool
1099EmulateInstructionARM::SetTargetTriple (const ConstString &triple)
1100{
1101 m_arm_isa = 0;
1102 const char *triple_cstr = triple.GetCString();
1103 if (triple_cstr)
1104 {
1105 const char *dash = ::strchr (triple_cstr, '-');
1106 if (dash)
1107 {
1108 std::string arch (triple_cstr, dash);
1109 const char *arch_cstr = arch.c_str();
1110 if (strcasecmp(arch_cstr, "armv4t") == 0)
1111 m_arm_isa = ARMv4T;
1112 else if (strcasecmp(arch_cstr, "armv4") == 0)
1113 m_arm_isa = ARMv4;
1114 else if (strcasecmp(arch_cstr, "armv5tej") == 0)
1115 m_arm_isa = ARMv5TEJ;
1116 else if (strcasecmp(arch_cstr, "armv5te") == 0)
1117 m_arm_isa = ARMv5TE;
1118 else if (strcasecmp(arch_cstr, "armv5t") == 0)
1119 m_arm_isa = ARMv5T;
1120 else if (strcasecmp(arch_cstr, "armv6k") == 0)
1121 m_arm_isa = ARMv6K;
1122 else if (strcasecmp(arch_cstr, "armv6") == 0)
1123 m_arm_isa = ARMv6;
1124 else if (strcasecmp(arch_cstr, "armv6t2") == 0)
1125 m_arm_isa = ARMv6T2;
1126 else if (strcasecmp(arch_cstr, "armv7") == 0)
1127 m_arm_isa = ARMv7;
1128 else if (strcasecmp(arch_cstr, "armv8") == 0)
1129 m_arm_isa = ARMv8;
1130 }
1131 }
1132 return m_arm_isa != 0;
1133}
1134
1135
Greg Clayton64c84432011-01-21 22:02:52 +00001136bool
1137EmulateInstructionARM::ReadInstruction ()
1138{
1139 bool success = false;
1140 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
1141 if (success)
1142 {
1143 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
1144 if (success)
1145 {
1146 Context read_inst_context = {eContextReadOpcode, 0, 0};
1147 if (m_inst_cpsr & MASK_CPSR_T)
1148 {
1149 m_inst_mode = eModeThumb;
1150 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
1151
1152 if (success)
1153 {
1154 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
1155 {
1156 m_inst.opcode_type = eOpcode16;
1157 m_inst.opcode.inst16 = thumb_opcode;
1158 }
1159 else
1160 {
1161 m_inst.opcode_type = eOpcode32;
1162 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
1163 }
1164 }
1165 }
1166 else
1167 {
1168 m_inst_mode = eModeARM;
1169 m_inst.opcode_type = eOpcode32;
1170 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
1171 }
1172 }
1173 }
1174 if (!success)
1175 {
1176 m_inst_mode = eModeInvalid;
1177 m_inst_pc = LLDB_INVALID_ADDRESS;
1178 }
1179 return success;
1180}
1181
1182uint32_t
1183EmulateInstructionARM::CurrentCond ()
1184{
1185 switch (m_inst_mode)
1186 {
1187 default:
1188 case eModeInvalid:
1189 break;
1190
1191 case eModeARM:
1192 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
1193
1194 case eModeThumb:
1195 return 0x0000000Eu; // Return always for now, we need to handl IT instructions later
1196 }
1197 return UINT32_MAX; // Return invalid value
1198}
1199bool
1200EmulateInstructionARM::ConditionPassed ()
1201{
1202 if (m_inst_cpsr == 0)
1203 return false;
1204
1205 const uint32_t cond = CurrentCond ();
1206
1207 if (cond == UINT32_MAX)
1208 return false;
1209
1210 bool result = false;
1211 switch (UnsignedBits(cond, 3, 1))
1212 {
1213 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
1214 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
1215 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
1216 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
1217 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
1218 case 5:
1219 {
1220 bool n = (m_inst_cpsr & MASK_CPSR_N);
1221 bool v = (m_inst_cpsr & MASK_CPSR_V);
1222 result = n == v;
1223 }
1224 break;
1225 case 6:
1226 {
1227 bool n = (m_inst_cpsr & MASK_CPSR_N);
1228 bool v = (m_inst_cpsr & MASK_CPSR_V);
1229 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
1230 }
1231 break;
1232 case 7:
1233 result = true;
1234 break;
1235 }
1236
1237 if (cond & 1)
1238 result = !result;
1239 return result;
1240}
1241
1242
1243bool
1244EmulateInstructionARM::EvaluateInstruction ()
1245{
1246 return false;
1247}