blob: a97eacd65505305ed5f3e421ecbbcf1ae42d7f30 [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
Caroline Ticefa172202011-02-11 22:49:54 +000010#include <stdlib.h>
11
Greg Clayton64c84432011-01-21 22:02:52 +000012#include "EmulateInstructionARM.h"
Greg Clayton8482ded2011-02-01 00:04:43 +000013#include "lldb/Core/ConstString.h"
14
Greg Claytonf29a08f2011-02-09 17:41:27 +000015#include "Plugins/Process/Utility/ARMDefines.h"
16#include "Plugins/Process/Utility/ARMUtils.h"
17#include "Utility/ARM_DWARF_Registers.h"
18
Johnny Chen9b8d7832011-02-02 01:13:56 +000019#include "llvm/Support/MathExtras.h" // for SignExtend32 template function
Johnny Chen93070472011-02-04 23:02:47 +000020 // and CountTrailingZeros_32 function
Greg Clayton64c84432011-01-21 22:02:52 +000021
22using namespace lldb;
23using namespace lldb_private;
24
Johnny Chend6c13f02011-02-08 20:36:34 +000025static inline uint32_t Align(uint32_t val, uint32_t alignment)
26{
27 return alignment * (val / alignment);
28}
29
Johnny Chen0e00af22011-02-10 19:40:42 +000030//----------------------------------------------------------------------
31//
32// ITSession implementation
33//
34//----------------------------------------------------------------------
35
Johnny Chen93070472011-02-04 23:02:47 +000036// A8.6.50
37// Valid return values are {1, 2, 3, 4}, with 0 signifying an error condition.
38static unsigned short CountITSize(unsigned ITMask) {
39 // First count the trailing zeros of the IT mask.
40 unsigned TZ = llvm::CountTrailingZeros_32(ITMask);
41 if (TZ > 3)
42 {
43 printf("Encoding error: IT Mask '0000'\n");
44 return 0;
45 }
46 return (4 - TZ);
47}
48
49// Init ITState. Note that at least one bit is always 1 in mask.
50bool ITSession::InitIT(unsigned short bits7_0)
51{
52 ITCounter = CountITSize(Bits32(bits7_0, 3, 0));
53 if (ITCounter == 0)
54 return false;
55
56 // A8.6.50 IT
57 unsigned short FirstCond = Bits32(bits7_0, 7, 4);
58 if (FirstCond == 0xF)
59 {
60 printf("Encoding error: IT FirstCond '1111'\n");
61 return false;
62 }
63 if (FirstCond == 0xE && ITCounter != 1)
64 {
65 printf("Encoding error: IT FirstCond '1110' && Mask != '1000'\n");
66 return false;
67 }
68
69 ITState = bits7_0;
70 return true;
71}
72
73// Update ITState if necessary.
74void ITSession::ITAdvance()
75{
76 assert(ITCounter);
77 --ITCounter;
78 if (ITCounter == 0)
79 ITState = 0;
80 else
81 {
82 unsigned short NewITState4_0 = Bits32(ITState, 4, 0) << 1;
83 SetBits32(ITState, 4, 0, NewITState4_0);
84 }
85}
86
87// Return true if we're inside an IT Block.
88bool ITSession::InITBlock()
89{
90 return ITCounter != 0;
91}
92
Johnny Chenc315f862011-02-05 00:46:10 +000093// Return true if we're the last instruction inside an IT Block.
94bool ITSession::LastInITBlock()
95{
96 return ITCounter == 1;
97}
98
Johnny Chen93070472011-02-04 23:02:47 +000099// Get condition bits for the current thumb instruction.
100uint32_t ITSession::GetCond()
101{
Johnny Chenc315f862011-02-05 00:46:10 +0000102 if (InITBlock())
103 return Bits32(ITState, 7, 4);
104 else
105 return COND_AL;
Johnny Chen93070472011-02-04 23:02:47 +0000106}
107
Greg Clayton64c84432011-01-21 22:02:52 +0000108// ARM constants used during decoding
109#define REG_RD 0
110#define LDM_REGLIST 1
111#define PC_REG 15
112#define PC_REGLIST_BIT 0x8000
113
Johnny Chen251af6a2011-01-21 22:47:25 +0000114#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +0000115#define ARMv4T (1u << 1)
116#define ARMv5T (1u << 2)
117#define ARMv5TE (1u << 3)
118#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +0000119#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +0000120#define ARMv6K (1u << 6)
121#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +0000122#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +0000123#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +0000124#define ARMvAll (0xffffffffu)
125
Johnny Chen9b8d7832011-02-02 01:13:56 +0000126#define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8)
127#define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8)
128#define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8)
Greg Clayton64c84432011-01-21 22:02:52 +0000129
Johnny Chen0e00af22011-02-10 19:40:42 +0000130//----------------------------------------------------------------------
131//
132// EmulateInstructionARM implementation
133//
134//----------------------------------------------------------------------
135
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000136void
137EmulateInstructionARM::Initialize ()
Johnny Chen7dc60e12011-01-24 19:46:32 +0000138{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000139}
Johnny Chen7dc60e12011-01-24 19:46:32 +0000140
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000141void
142EmulateInstructionARM::Terminate ()
Greg Clayton64c84432011-01-21 22:02:52 +0000143{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000144}
145
Caroline Ticefa172202011-02-11 22:49:54 +0000146// Write "bits (32) UNKNOWN" to memory address "address". Helper function for many ARM instructions.
147bool
148EmulateInstructionARM::WriteBits32UnknownToMemory (addr_t address)
149{
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000150 EmulateInstruction::Context context;
151 context.type = EmulateInstruction::eContextWriteMemoryRandomBits;
152 context.SetNoArgs ();
Caroline Ticefa172202011-02-11 22:49:54 +0000153
154 uint32_t random_data = rand ();
155 const uint32_t addr_byte_size = GetAddressByteSize();
156
157 if (!WriteMemoryUnsigned (context, address, random_data, addr_byte_size))
158 return false;
159
160 return true;
161}
162
Caroline Tice713c2662011-02-11 17:59:55 +0000163// Write "bits (32) UNKNOWN" to register n. Helper function for many ARM instructions.
164bool
165EmulateInstructionARM::WriteBits32Unknown (int n)
166{
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000167 EmulateInstruction::Context context;
168 context.type = EmulateInstruction::eContextWriteRegisterRandomBits;
169 context.SetNoArgs ();
Caroline Tice713c2662011-02-11 17:59:55 +0000170
Johnny Chen62ff6f52011-02-11 18:11:22 +0000171 bool success;
Caroline Tice713c2662011-02-11 17:59:55 +0000172 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
173
174 if (!success)
175 return false;
176
177 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
178 return false;
179
180 return true;
181}
182
Johnny Chen08c25e82011-01-31 18:02:28 +0000183// Push Multiple Registers stores multiple registers to the stack, storing to
184// consecutive memory locations ending just below the address in SP, and updates
185// SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000186bool
187EmulateInstructionARM::EmulatePush (ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +0000188{
189#if 0
190 // ARM pseudo code...
191 if (ConditionPassed())
192 {
193 EncodingSpecificOperations();
194 NullCheckIfThumbEE(13);
195 address = SP - 4*BitCount(registers);
196
197 for (i = 0 to 14)
198 {
199 if (registers<i> == ’1’)
200 {
201 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
202 MemA[address,4] = bits(32) UNKNOWN;
203 else
204 MemA[address,4] = R[i];
205 address = address + 4;
206 }
207 }
208
209 if (registers<15> == ’1’) // Only possible for encoding A1 or A2
210 MemA[address,4] = PCStoreValue();
211
212 SP = SP - 4*BitCount(registers);
213 }
214#endif
215
216 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000217 const uint32_t opcode = OpcodeAsUnsigned (&success);
Greg Clayton64c84432011-01-21 22:02:52 +0000218 if (!success)
219 return false;
220
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000221 if (ConditionPassed())
Greg Clayton64c84432011-01-21 22:02:52 +0000222 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000223 const uint32_t addr_byte_size = GetAddressByteSize();
224 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000225 if (!success)
226 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000227 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +0000228 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +0000229 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +0000230 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000231 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000232 // The M bit represents LR.
Johnny Chenbd599902011-02-10 21:39:01 +0000233 if (Bit32(opcode, 8))
Johnny Chenef85e912011-01-31 23:07:40 +0000234 registers |= (1u << 14);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000235 // if BitCount(registers) < 1 then UNPREDICTABLE;
236 if (BitCount(registers) < 1)
237 return false;
238 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000239 case eEncodingT2:
240 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000241 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000242 // if BitCount(registers) < 2 then UNPREDICTABLE;
243 if (BitCount(registers) < 2)
244 return false;
245 break;
246 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000247 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000248 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000249 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000250 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000251 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000252 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000253 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000254 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000255 // Instead of return false, let's handle the following case as well,
256 // which amounts to pushing one reg onto the full descending stacks.
257 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000258 break;
259 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000260 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000261 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000262 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000263 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000264 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000265 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000266 default:
267 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000268 }
Johnny Chence1ca772011-01-25 01:13:00 +0000269 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000270 addr_t addr = sp - sp_offset;
271 uint32_t i;
272
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000273 EmulateInstruction::Context context;
274 context.type = EmulateInstruction::eContextPushRegisterOnStack;
275 Register dwarf_reg;
276 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Greg Clayton64c84432011-01-21 22:02:52 +0000277 for (i=0; i<15; ++i)
278 {
Johnny Chen7c1bf922011-02-08 23:49:37 +0000279 if (BitIsSet (registers, i))
Greg Clayton64c84432011-01-21 22:02:52 +0000280 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000281 dwarf_reg.num = dwarf_r0 + i;
282 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
283 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000284 if (!success)
285 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000286 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000287 return false;
288 addr += addr_byte_size;
289 }
290 }
291
Johnny Chen7c1bf922011-02-08 23:49:37 +0000292 if (BitIsSet (registers, 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000293 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000294 dwarf_reg.num = dwarf_pc;
295 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000296 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000297 if (!success)
298 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000299 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000300 return false;
301 }
302
303 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000304 context.SetImmediateSigned (-sp_offset);
Greg Clayton64c84432011-01-21 22:02:52 +0000305
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000306 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Greg Clayton64c84432011-01-21 22:02:52 +0000307 return false;
308 }
309 return true;
310}
311
Johnny Chenef85e912011-01-31 23:07:40 +0000312// Pop Multiple Registers loads multiple registers from the stack, loading from
313// consecutive memory locations staring at the address in SP, and updates
314// SP to point just above the loaded data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000315bool
316EmulateInstructionARM::EmulatePop (ARMEncoding encoding)
Johnny Chenef85e912011-01-31 23:07:40 +0000317{
318#if 0
319 // ARM pseudo code...
320 if (ConditionPassed())
321 {
322 EncodingSpecificOperations(); NullCheckIfThumbEE(13);
323 address = SP;
324 for i = 0 to 14
325 if registers<i> == ‘1’ then
326 R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4;
327 if registers<15> == ‘1’ then
328 if UnalignedAllowed then
329 LoadWritePC(MemU[address,4]);
330 else
331 LoadWritePC(MemA[address,4]);
332 if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers);
333 if registers<13> == ‘1’ then SP = bits(32) UNKNOWN;
334 }
335#endif
336
337 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000338 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenef85e912011-01-31 23:07:40 +0000339 if (!success)
340 return false;
341
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000342 if (ConditionPassed())
Johnny Chenef85e912011-01-31 23:07:40 +0000343 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000344 const uint32_t addr_byte_size = GetAddressByteSize();
345 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000346 if (!success)
347 return false;
348 uint32_t registers = 0;
349 uint32_t Rt; // the destination register
350 switch (encoding) {
351 case eEncodingT1:
352 registers = Bits32(opcode, 7, 0);
353 // The P bit represents PC.
Johnny Chenbd599902011-02-10 21:39:01 +0000354 if (Bit32(opcode, 8))
Johnny Chenef85e912011-01-31 23:07:40 +0000355 registers |= (1u << 15);
356 // if BitCount(registers) < 1 then UNPREDICTABLE;
357 if (BitCount(registers) < 1)
358 return false;
359 break;
360 case eEncodingT2:
361 // Ignore bit 13.
362 registers = Bits32(opcode, 15, 0) & ~0x2000;
363 // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE;
Johnny Chenbd599902011-02-10 21:39:01 +0000364 if (BitCount(registers) < 2 || (Bit32(opcode, 15) && Bit32(opcode, 14)))
Johnny Chenef85e912011-01-31 23:07:40 +0000365 return false;
Johnny Chen098ae2d2011-02-12 00:50:05 +0000366 // if registers<15> == '1' && InITBlock() && !LastInITBlock() then UNPREDICTABLE;
367 if (BitIsSet(registers, 15) && InITBlock() && !LastInITBlock())
368 return false;
Johnny Chenef85e912011-01-31 23:07:40 +0000369 break;
370 case eEncodingT3:
371 Rt = Bits32(opcode, 15, 12);
372 // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +0000373 if (Rt == 13)
374 return false;
375 if (Rt == 15 && InITBlock() && !LastInITBlock())
Johnny Chenef85e912011-01-31 23:07:40 +0000376 return false;
377 registers = (1u << Rt);
378 break;
379 case eEncodingA1:
380 registers = Bits32(opcode, 15, 0);
381 // Instead of return false, let's handle the following case as well,
382 // which amounts to popping one reg from the full descending stacks.
383 // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD;
384
385 // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +0000386 if (BitIsSet(opcode, 13) && ArchVersion() >= ARMv7)
Johnny Chenef85e912011-01-31 23:07:40 +0000387 return false;
388 break;
389 case eEncodingA2:
390 Rt = Bits32(opcode, 15, 12);
391 // if t == 13 then UNPREDICTABLE;
392 if (Rt == dwarf_sp)
393 return false;
394 registers = (1u << Rt);
395 break;
396 default:
397 return false;
398 }
399 addr_t sp_offset = addr_byte_size * BitCount (registers);
400 addr_t addr = sp;
401 uint32_t i, data;
402
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000403 EmulateInstruction::Context context;
404 context.type = EmulateInstruction::eContextPopRegisterOffStack;
405 Register dwarf_reg;
406 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chenef85e912011-01-31 23:07:40 +0000407 for (i=0; i<15; ++i)
408 {
Johnny Chen7c1bf922011-02-08 23:49:37 +0000409 if (BitIsSet (registers, i))
Johnny Chenef85e912011-01-31 23:07:40 +0000410 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000411 dwarf_reg.num = dwarf_r0 + i;
412 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000413 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000414 if (!success)
415 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000416 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000417 return false;
418 addr += addr_byte_size;
419 }
420 }
421
Johnny Chen7c1bf922011-02-08 23:49:37 +0000422 if (BitIsSet (registers, 15))
Johnny Chenef85e912011-01-31 23:07:40 +0000423 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000424 dwarf_reg.num = dwarf_pc;
425 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000426 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000427 if (!success)
428 return false;
Johnny Chenf3eaacf2011-02-09 19:30:49 +0000429 // In ARMv5T and above, this is an interworking branch.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000430 if (!LoadWritePC(context, data, dwarf_reg))
Johnny Chenef85e912011-01-31 23:07:40 +0000431 return false;
432 addr += addr_byte_size;
433 }
434
435 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000436 context.SetImmediateSigned (sp_offset);
Johnny Chenef85e912011-01-31 23:07:40 +0000437
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000438 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
Johnny Chenef85e912011-01-31 23:07:40 +0000439 return false;
440 }
441 return true;
442}
443
Johnny Chen5b442b72011-01-27 19:34:30 +0000444// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000445// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000446bool
447EmulateInstructionARM::EmulateAddRdSPImmediate (ARMEncoding encoding)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000448{
449#if 0
450 // ARM pseudo code...
451 if (ConditionPassed())
452 {
453 EncodingSpecificOperations();
454 (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’);
455 if d == 15 then
456 ALUWritePC(result); // setflags is always FALSE here
457 else
458 R[d] = result;
459 if setflags then
460 APSR.N = result<31>;
461 APSR.Z = IsZeroBit(result);
462 APSR.C = carry;
463 APSR.V = overflow;
464 }
465#endif
466
467 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000468 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000469 if (!success)
470 return false;
471
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000472 if (ConditionPassed())
Johnny Chenbcec3af2011-01-27 01:26:19 +0000473 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000474 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000475 if (!success)
476 return false;
477 uint32_t Rd; // the destination register
478 uint32_t imm32;
479 switch (encoding) {
480 case eEncodingT1:
481 Rd = 7;
482 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
483 break;
484 case eEncodingA1:
485 Rd = Bits32(opcode, 15, 12);
486 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
487 break;
488 default:
489 return false;
490 }
491 addr_t sp_offset = imm32;
492 addr_t addr = sp + sp_offset; // a pointer to the stack area
493
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000494 EmulateInstruction::Context context;
495 context.type = EmulateInstruction::eContextRegisterPlusOffset;
496 Register sp_reg;
497 sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
498 context.SetRegisterPlusOffset (sp_reg, sp_offset);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000499
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000500 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
Johnny Chenbcec3af2011-01-27 01:26:19 +0000501 return false;
502 }
503 return true;
504}
505
Johnny Chen2ccad832011-01-28 19:57:25 +0000506// Set r7 or ip to the current stack pointer.
507// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000508bool
509EmulateInstructionARM::EmulateMovRdSP (ARMEncoding encoding)
Johnny Chen2ccad832011-01-28 19:57:25 +0000510{
511#if 0
512 // ARM pseudo code...
513 if (ConditionPassed())
514 {
515 EncodingSpecificOperations();
516 result = R[m];
517 if d == 15 then
518 ALUWritePC(result); // setflags is always FALSE here
519 else
520 R[d] = result;
521 if setflags then
522 APSR.N = result<31>;
523 APSR.Z = IsZeroBit(result);
524 // APSR.C unchanged
525 // APSR.V unchanged
526 }
527#endif
528
529 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000530 //const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000531 //if (!success)
532 // return false;
Johnny Chen2ccad832011-01-28 19:57:25 +0000533
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000534 if (ConditionPassed())
Johnny Chen2ccad832011-01-28 19:57:25 +0000535 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000536 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen2ccad832011-01-28 19:57:25 +0000537 if (!success)
538 return false;
539 uint32_t Rd; // the destination register
540 switch (encoding) {
541 case eEncodingT1:
542 Rd = 7;
543 break;
544 case eEncodingA1:
545 Rd = 12;
546 break;
547 default:
548 return false;
549 }
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000550
551 EmulateInstruction::Context context;
552 context.type = EmulateInstruction::eContextRegisterPlusOffset;
553 Register sp_reg;
554 sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
555 context.SetRegisterPlusOffset (sp_reg, 0);
Johnny Chen2ccad832011-01-28 19:57:25 +0000556
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000557 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp))
Johnny Chen2ccad832011-01-28 19:57:25 +0000558 return false;
559 }
560 return true;
561}
562
Johnny Chen1c13b622011-01-29 00:11:15 +0000563// Move from high register (r8-r15) to low register (r0-r7).
564// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000565bool
566EmulateInstructionARM::EmulateMovLowHigh (ARMEncoding encoding)
Johnny Chen1c13b622011-01-29 00:11:15 +0000567{
Johnny Chen338bf542011-02-10 19:29:03 +0000568 return EmulateMovRdRm (encoding);
569}
570
571// Move from register to register.
572// MOV (register)
573bool
574EmulateInstructionARM::EmulateMovRdRm (ARMEncoding encoding)
575{
Johnny Chen1c13b622011-01-29 00:11:15 +0000576#if 0
577 // ARM pseudo code...
578 if (ConditionPassed())
579 {
580 EncodingSpecificOperations();
581 result = R[m];
582 if d == 15 then
583 ALUWritePC(result); // setflags is always FALSE here
584 else
585 R[d] = result;
586 if setflags then
587 APSR.N = result<31>;
588 APSR.Z = IsZeroBit(result);
589 // APSR.C unchanged
590 // APSR.V unchanged
591 }
592#endif
593
594 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000595 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000596 if (!success)
597 return false;
598
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000599 if (ConditionPassed())
Johnny Chen1c13b622011-01-29 00:11:15 +0000600 {
601 uint32_t Rm; // the source register
602 uint32_t Rd; // the destination register
Johnny Chen338bf542011-02-10 19:29:03 +0000603 bool setflags;
Johnny Chen1c13b622011-01-29 00:11:15 +0000604 switch (encoding) {
605 case eEncodingT1:
606 Rm = Bits32(opcode, 6, 3);
Johnny Chenbd599902011-02-10 21:39:01 +0000607 Rd = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 1);
Johnny Chen338bf542011-02-10 19:29:03 +0000608 setflags = false;
609 break;
610 case eEncodingT2:
611 Rm = Bits32(opcode, 5, 3);
612 Rd = Bits32(opcode, 2, 1);
613 setflags = true;
Johnny Chen1c13b622011-01-29 00:11:15 +0000614 break;
615 default:
616 return false;
617 }
Johnny Chen338bf542011-02-10 19:29:03 +0000618 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000619 if (!success)
620 return false;
621
622 // The context specifies that Rm is to be moved into Rd.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000623 EmulateInstruction::Context context;
624 context.type = EmulateInstruction::eContextRegisterPlusOffset;
625 Register dwarf_reg;
626 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm);
627 context.SetRegisterPlusOffset (dwarf_reg, 0);
Johnny Chen1c13b622011-01-29 00:11:15 +0000628
Johnny Chen338bf542011-02-10 19:29:03 +0000629 if (Rd == 15)
630 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000631 if (!ALUWritePC (context, reg_value, dwarf_reg))
Johnny Chen338bf542011-02-10 19:29:03 +0000632 return false;
633 }
634 else
635 {
636 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value))
637 return false;
638 if (setflags)
639 {
640 m_new_inst_cpsr = m_inst_cpsr;
Johnny Chenbd599902011-02-10 21:39:01 +0000641 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(reg_value, CPSR_N));
642 SetBit32(m_new_inst_cpsr, CPSR_Z, reg_value == 0 ? 1 : 0);
Johnny Chen338bf542011-02-10 19:29:03 +0000643 if (m_new_inst_cpsr != m_inst_cpsr)
644 {
645 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
646 return false;
647 }
648 }
649 }
Johnny Chen1c13b622011-01-29 00:11:15 +0000650 }
651 return true;
652}
653
Johnny Chen357c30f2011-02-14 22:04:25 +0000654// Move (immediate) writes an immediate value to the destination register. It
655// can optionally update the condition flags based on the value.
656// MOV (immediate)
657bool
658EmulateInstructionARM::EmulateMovRdImm (ARMEncoding encoding)
659{
660#if 0
661 // ARM pseudo code...
662 if (ConditionPassed())
663 {
664 EncodingSpecificOperations();
665 result = imm32;
666 if d == 15 then // Can only occur for ARM encoding
667 ALUWritePC(result); // setflags is always FALSE here
668 else
669 R[d] = result;
670 if setflags then
671 APSR.N = result<31>;
672 APSR.Z = IsZeroBit(result);
673 APSR.C = carry;
674 // APSR.V unchanged
675 }
676#endif
677 bool success = false;
678 const uint32_t opcode = OpcodeAsUnsigned (&success);
679 if (!success)
680 return false;
681
682 if (ConditionPassed())
683 {
684 uint32_t Rd; // the destination register
685 uint32_t imm12; // some intermediate result
686 uint32_t imm32; // the immediate value to be written to Rd
687 uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C.
688 bool setflags;
689 switch (encoding) {
690 case eEncodingT1:
691 Rd = Bits32(opcode, 11, 8);
692 setflags = !InITBlock();
693 imm32 = Bits32(opcode, 7, 0); // imm32 = ZeroExtend(imm8, 32)
694 carry = Bit32(m_inst_cpsr, CPSR_C);
695 break;
696 case eEncodingT2:
697 Rd = Bits32(opcode, 15, 12);
698 setflags = BitIsSet(opcode, 20);
699 imm12 = Bit32(opcode, 26) << 11 | Bits32(opcode, 14, 12) << 8 | Bits32(opcode, 7, 0);
700 imm32 = ThumbExpandImm_C(imm12, Bit32(m_inst_cpsr, CPSR_C), carry);
Johnny Chen9798cfc2011-02-14 23:33:58 +0000701 if (BadReg(Rd))
702 return false;
Johnny Chen357c30f2011-02-14 22:04:25 +0000703 break;
704 default:
705 return false;
706 }
707 uint32_t result = imm32;
708
709 // The context specifies that an immediate is to be moved into Rd.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000710 EmulateInstruction::Context context;
711 context.type = EmulateInstruction::eContextImmediate;
712 context.SetNoArgs ();
713
714 Register dummy_reg;
715 dummy_reg.SetRegister (eRegisterKindDWARF, dwarf_r0);
716
Johnny Chen357c30f2011-02-14 22:04:25 +0000717 if (Rd == 15)
718 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000719 if (!ALUWritePC (context, result, dummy_reg))
Johnny Chen357c30f2011-02-14 22:04:25 +0000720 return false;
721 }
722 else
723 {
724 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
725 return false;
726 if (setflags)
727 {
728 m_new_inst_cpsr = m_inst_cpsr;
729 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(result, CPSR_N));
730 SetBit32(m_new_inst_cpsr, CPSR_Z, result == 0 ? 1 : 0);
731 SetBit32(m_new_inst_cpsr, CPSR_C, carry);
732 if (m_new_inst_cpsr != m_inst_cpsr)
733 {
734 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
735 return false;
736 }
737 }
738 }
739 }
740 return true;
741}
742
Johnny Chen28070c32011-02-12 01:27:26 +0000743// Bitwise NOT (immediate) writes the bitwise inverse of an immediate value to
744// the destination register. It can optionally update the condition flags based
745// on the value.
746// MVN (immediate)
747bool
748EmulateInstructionARM::EmulateMvnRdImm (ARMEncoding encoding)
749{
750#if 0
751 // ARM pseudo code...
752 if (ConditionPassed())
753 {
754 EncodingSpecificOperations();
755 result = NOT(imm32);
756 if d == 15 then // Can only occur for ARM encoding
757 ALUWritePC(result); // setflags is always FALSE here
758 else
759 R[d] = result;
760 if setflags then
761 APSR.N = result<31>;
762 APSR.Z = IsZeroBit(result);
763 APSR.C = carry;
764 // APSR.V unchanged
765 }
766#endif
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000767 bool success = false;
768 const uint32_t opcode = OpcodeAsUnsigned (&success);
769 if (!success)
770 return false;
771
772 if (ConditionPassed())
773 {
774 uint32_t Rd; // the destination register
Johnny Chen357c30f2011-02-14 22:04:25 +0000775 uint32_t imm12; // the first operand to ThumbExpandImm_C or ARMExpandImm_C
776 uint32_t imm32; // the output after ThumbExpandImm_C or ARMExpandImm_C
777 uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000778 bool setflags;
779 switch (encoding) {
780 case eEncodingT1:
781 Rd = Bits32(opcode, 11, 8);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000782 setflags = BitIsSet(opcode, 20);
Johnny Chen357c30f2011-02-14 22:04:25 +0000783 imm12 = Bit32(opcode, 26) << 11 | Bits32(opcode, 14, 12) << 8 | Bits32(opcode, 7, 0);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000784 imm32 = ThumbExpandImm_C(imm12, Bit32(m_inst_cpsr, CPSR_C), carry);
785 break;
786 case eEncodingA1:
787 Rd = Bits32(opcode, 15, 12);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000788 setflags = BitIsSet(opcode, 20);
Johnny Chen357c30f2011-02-14 22:04:25 +0000789 imm12 = Bits32(opcode, 11, 0);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000790 imm32 = ARMExpandImm_C(imm12, Bit32(m_inst_cpsr, CPSR_C), carry);
791 break;
792 default:
793 return false;
794 }
795 uint32_t result = ~imm32;
796
797 // The context specifies that an immediate is to be moved into Rd.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000798 EmulateInstruction::Context context;
799 context.type = EmulateInstruction::eContextImmediate;
800 context.SetNoArgs ();
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000801
802 if (Rd == 15)
803 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000804 Register dummy_reg;
805 if (!ALUWritePC (context, result, dummy_reg))
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000806 return false;
807 }
808 else
809 {
810 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
811 return false;
812 if (setflags)
813 {
814 m_new_inst_cpsr = m_inst_cpsr;
815 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(result, CPSR_N));
816 SetBit32(m_new_inst_cpsr, CPSR_Z, result == 0 ? 1 : 0);
817 SetBit32(m_new_inst_cpsr, CPSR_C, carry);
818 if (m_new_inst_cpsr != m_inst_cpsr)
819 {
820 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
821 return false;
822 }
823 }
824 }
825 }
826 return true;
Johnny Chen28070c32011-02-12 01:27:26 +0000827}
828
Johnny Chen788e0552011-01-27 22:52:23 +0000829// PC relative immediate load into register, possibly followed by ADD (SP plus register).
830// LDR (literal)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000831bool
Johnny Chenc9de9102011-02-11 19:12:30 +0000832EmulateInstructionARM::EmulateLDRRtPCRelative (ARMEncoding encoding)
Johnny Chen788e0552011-01-27 22:52:23 +0000833{
834#if 0
835 // ARM pseudo code...
836 if (ConditionPassed())
837 {
838 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
839 base = Align(PC,4);
840 address = if add then (base + imm32) else (base - imm32);
841 data = MemU[address,4];
842 if t == 15 then
843 if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE;
844 elsif UnalignedSupport() || address<1:0> = ‘00’ then
845 R[t] = data;
846 else // Can only apply before ARMv7
847 if CurrentInstrSet() == InstrSet_ARM then
848 R[t] = ROR(data, 8*UInt(address<1:0>));
849 else
850 R[t] = bits(32) UNKNOWN;
851 }
852#endif
853
854 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000855 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen788e0552011-01-27 22:52:23 +0000856 if (!success)
857 return false;
858
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000859 if (ConditionPassed())
Johnny Chen788e0552011-01-27 22:52:23 +0000860 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000861 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000862 if (!success)
863 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000864
865 // PC relative immediate load context
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000866 EmulateInstruction::Context context;
867 context.type = EmulateInstruction::eContextRegisterPlusOffset;
868 Register pc_reg;
869 pc_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
870 context.SetRegisterPlusOffset (pc_reg, 0);
871
Johnny Chenc9de9102011-02-11 19:12:30 +0000872 uint32_t Rt; // the destination register
Johnny Chen788e0552011-01-27 22:52:23 +0000873 uint32_t imm32; // immediate offset from the PC
Johnny Chenc9de9102011-02-11 19:12:30 +0000874 bool add; // +imm32 or -imm32?
875 addr_t base; // the base address
876 addr_t address; // the PC relative address
Johnny Chen788e0552011-01-27 22:52:23 +0000877 uint32_t data; // the literal data value from the PC relative load
878 switch (encoding) {
879 case eEncodingT1:
Johnny Chenc9de9102011-02-11 19:12:30 +0000880 Rt = Bits32(opcode, 10, 8);
Johnny Chen788e0552011-01-27 22:52:23 +0000881 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
Johnny Chenc9de9102011-02-11 19:12:30 +0000882 add = true;
883 base = Align(pc + 4, 4);
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000884 context.SetRegisterPlusOffset (pc_reg, 4 + imm32);
Johnny Chenc9de9102011-02-11 19:12:30 +0000885 break;
886 case eEncodingT2:
887 Rt = Bits32(opcode, 15, 12);
888 imm32 = Bits32(opcode, 11, 0) << 2; // imm32 = ZeroExtend(imm12, 32);
889 add = BitIsSet(opcode, 23);
Johnny Chen098ae2d2011-02-12 00:50:05 +0000890 if (Rt == 15 && InITBlock() && !LastInITBlock())
Johnny Chenc9de9102011-02-11 19:12:30 +0000891 return false;
892 base = Align(pc + 4, 4);
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000893 context.SetRegisterPlusOffset (pc_reg, 4 + imm32);
Johnny Chen788e0552011-01-27 22:52:23 +0000894 break;
895 default:
896 return false;
897 }
Johnny Chenc9de9102011-02-11 19:12:30 +0000898
899 if (add)
900 address = base + imm32;
901 else
902 address = base - imm32;
903 data = ReadMemoryUnsigned(context, address, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000904 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000905 return false;
Johnny Chenc9de9102011-02-11 19:12:30 +0000906
907 if (Rt == 15)
908 {
909 if (Bits32(address, 1, 0) == 0)
910 {
911 // In ARMv5T and above, this is an interworking branch.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000912 if (!LoadWritePC(context, data, pc_reg))
Johnny Chenc9de9102011-02-11 19:12:30 +0000913 return false;
914 }
915 else
916 return false;
917 }
918 else if (UnalignedSupport() || Bits32(address, 1, 0) == 0)
919 {
920 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data))
921 return false;
922 }
923 else // We don't handle ARM for now.
924 return false;
925
926 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data))
Johnny Chen788e0552011-01-27 22:52:23 +0000927 return false;
928 }
929 return true;
930}
931
Johnny Chen5b442b72011-01-27 19:34:30 +0000932// An add operation to adjust the SP.
Johnny Chenfdd179e2011-01-31 20:09:28 +0000933// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000934bool
935EmulateInstructionARM::EmulateAddSPImmediate (ARMEncoding encoding)
Johnny Chenfdd179e2011-01-31 20:09:28 +0000936{
937#if 0
938 // ARM pseudo code...
939 if (ConditionPassed())
940 {
941 EncodingSpecificOperations();
942 (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’);
943 if d == 15 then // Can only occur for ARM encoding
944 ALUWritePC(result); // setflags is always FALSE here
945 else
946 R[d] = result;
947 if setflags then
948 APSR.N = result<31>;
949 APSR.Z = IsZeroBit(result);
950 APSR.C = carry;
951 APSR.V = overflow;
952 }
953#endif
954
955 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000956 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000957 if (!success)
958 return false;
959
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000960 if (ConditionPassed())
Johnny Chenfdd179e2011-01-31 20:09:28 +0000961 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000962 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000963 if (!success)
964 return false;
965 uint32_t imm32; // the immediate operand
966 switch (encoding) {
967 case eEncodingT2:
968 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
969 break;
970 default:
971 return false;
972 }
973 addr_t sp_offset = imm32;
974 addr_t addr = sp + sp_offset; // the adjusted stack pointer value
975
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000976 EmulateInstruction::Context context;
977 context.type = EmulateInstruction::eContextAdjustStackPointer;
978 context.SetImmediateSigned (sp_offset);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000979
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000980 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chenfdd179e2011-01-31 20:09:28 +0000981 return false;
982 }
983 return true;
984}
985
986// An add operation to adjust the SP.
Johnny Chen5b442b72011-01-27 19:34:30 +0000987// ADD (SP plus register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000988bool
989EmulateInstructionARM::EmulateAddSPRm (ARMEncoding encoding)
Johnny Chen5b442b72011-01-27 19:34:30 +0000990{
991#if 0
992 // ARM pseudo code...
993 if (ConditionPassed())
994 {
995 EncodingSpecificOperations();
996 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
997 (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’);
998 if d == 15 then
999 ALUWritePC(result); // setflags is always FALSE here
1000 else
1001 R[d] = result;
1002 if setflags then
1003 APSR.N = result<31>;
1004 APSR.Z = IsZeroBit(result);
1005 APSR.C = carry;
1006 APSR.V = overflow;
1007 }
1008#endif
1009
1010 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001011 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen5b442b72011-01-27 19:34:30 +00001012 if (!success)
1013 return false;
1014
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001015 if (ConditionPassed())
Johnny Chen5b442b72011-01-27 19:34:30 +00001016 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001017 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +00001018 if (!success)
1019 return false;
1020 uint32_t Rm; // the second operand
1021 switch (encoding) {
1022 case eEncodingT2:
1023 Rm = Bits32(opcode, 6, 3);
1024 break;
1025 default:
1026 return false;
1027 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001028 int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +00001029 if (!success)
1030 return false;
1031
1032 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
1033
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001034 EmulateInstruction::Context context;
1035 context.type = EmulateInstruction::eContextAdjustStackPointer;
1036 context.SetImmediateSigned (reg_value);
Johnny Chen5b442b72011-01-27 19:34:30 +00001037
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001038 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen5b442b72011-01-27 19:34:30 +00001039 return false;
1040 }
1041 return true;
1042}
1043
Johnny Chen9b8d7832011-02-02 01:13:56 +00001044// Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine
1045// at a PC-relative address, and changes instruction set from ARM to Thumb, or
1046// from Thumb to ARM.
1047// BLX (immediate)
1048bool
1049EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding)
1050{
1051#if 0
1052 // ARM pseudo code...
1053 if (ConditionPassed())
1054 {
1055 EncodingSpecificOperations();
1056 if CurrentInstrSet() == InstrSet_ARM then
1057 LR = PC - 4;
1058 else
1059 LR = PC<31:1> : '1';
1060 if targetInstrSet == InstrSet_ARM then
1061 targetAddress = Align(PC,4) + imm32;
1062 else
1063 targetAddress = PC + imm32;
1064 SelectInstrSet(targetInstrSet);
1065 BranchWritePC(targetAddress);
1066 }
1067#endif
1068
1069 bool success = false;
1070 const uint32_t opcode = OpcodeAsUnsigned (&success);
1071 if (!success)
1072 return false;
1073
1074 if (ConditionPassed())
1075 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001076 EmulateInstruction::Context context;
1077 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001078 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001079 if (!success)
1080 return false;
Johnny Chen53ebab72011-02-08 23:21:57 +00001081 addr_t lr; // next instruction address
1082 addr_t target; // target address
Johnny Chen9b8d7832011-02-02 01:13:56 +00001083 int32_t imm32; // PC-relative offset
1084 switch (encoding) {
Johnny Chend6c13f02011-02-08 20:36:34 +00001085 case eEncodingT1:
1086 {
1087 lr = (pc + 4) | 1u; // return address
Johnny Chenbd599902011-02-10 21:39:01 +00001088 uint32_t S = Bit32(opcode, 26);
Johnny Chend6c13f02011-02-08 20:36:34 +00001089 uint32_t imm10 = Bits32(opcode, 25, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001090 uint32_t J1 = Bit32(opcode, 13);
1091 uint32_t J2 = Bit32(opcode, 11);
Johnny Chend6c13f02011-02-08 20:36:34 +00001092 uint32_t imm11 = Bits32(opcode, 10, 0);
1093 uint32_t I1 = !(J1 ^ S);
1094 uint32_t I2 = !(J2 ^ S);
Johnny Chen53ebab72011-02-08 23:21:57 +00001095 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Johnny Chend6c13f02011-02-08 20:36:34 +00001096 imm32 = llvm::SignExtend32<25>(imm25);
1097 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001098 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen098ae2d2011-02-12 00:50:05 +00001099 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001100 return false;
Johnny Chend6c13f02011-02-08 20:36:34 +00001101 break;
1102 }
Johnny Chen9b8d7832011-02-02 01:13:56 +00001103 case eEncodingT2:
1104 {
1105 lr = (pc + 4) | 1u; // return address
Johnny Chenbd599902011-02-10 21:39:01 +00001106 uint32_t S = Bit32(opcode, 26);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001107 uint32_t imm10H = Bits32(opcode, 25, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001108 uint32_t J1 = Bit32(opcode, 13);
1109 uint32_t J2 = Bit32(opcode, 11);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001110 uint32_t imm10L = Bits32(opcode, 10, 1);
1111 uint32_t I1 = !(J1 ^ S);
1112 uint32_t I2 = !(J2 ^ S);
Johnny Chen53ebab72011-02-08 23:21:57 +00001113 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10H << 12) | (imm10L << 2);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001114 imm32 = llvm::SignExtend32<25>(imm25);
Johnny Chend6c13f02011-02-08 20:36:34 +00001115 target = Align(pc + 4, 4) + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001116 context.SetModeAndImmediateSigned (eModeARM, 4 + imm32);
Johnny Chen098ae2d2011-02-12 00:50:05 +00001117 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001118 return false;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001119 break;
1120 }
Johnny Chenc47d0ca2011-02-08 18:58:31 +00001121 case eEncodingA1:
1122 lr = pc + 4; // return address
1123 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2);
Johnny Chend6c13f02011-02-08 20:36:34 +00001124 target = Align(pc + 8, 4) + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001125 context.SetModeAndImmediateSigned (eModeARM, 8 + imm32);
Johnny Chenc47d0ca2011-02-08 18:58:31 +00001126 break;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001127 case eEncodingA2:
1128 lr = pc + 4; // return address
1129 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1);
1130 target = pc + 8 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001131 context.SetModeAndImmediateSigned (eModeThumb, 8 + imm32);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001132 break;
1133 default:
1134 return false;
1135 }
1136 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1137 return false;
Johnny Chen9ee056b2011-02-08 00:06:35 +00001138 if (!BranchWritePC(context, target))
Johnny Chen9b8d7832011-02-02 01:13:56 +00001139 return false;
1140 }
1141 return true;
1142}
1143
1144// Branch with Link and Exchange (register) calls a subroutine at an address and
1145// instruction set specified by a register.
1146// BLX (register)
1147bool
1148EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding)
1149{
1150#if 0
1151 // ARM pseudo code...
1152 if (ConditionPassed())
1153 {
1154 EncodingSpecificOperations();
1155 target = R[m];
1156 if CurrentInstrSet() == InstrSet_ARM then
1157 next_instr_addr = PC - 4;
1158 LR = next_instr_addr;
1159 else
1160 next_instr_addr = PC - 2;
1161 LR = next_instr_addr<31:1> : ‘1’;
1162 BXWritePC(target);
1163 }
1164#endif
1165
1166 bool success = false;
1167 const uint32_t opcode = OpcodeAsUnsigned (&success);
1168 if (!success)
1169 return false;
1170
1171 if (ConditionPassed())
1172 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001173 EmulateInstruction::Context context;
1174 context.type = EmulateInstruction::eContextAbsoluteBranchRegister;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001175 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1176 addr_t lr; // next instruction address
Johnny Chen9b8d7832011-02-02 01:13:56 +00001177 if (!success)
1178 return false;
1179 uint32_t Rm; // the register with the target address
1180 switch (encoding) {
1181 case eEncodingT1:
1182 lr = (pc + 2) | 1u; // return address
1183 Rm = Bits32(opcode, 6, 3);
1184 // if m == 15 then UNPREDICTABLE;
1185 if (Rm == 15)
1186 return false;
Johnny Chen098ae2d2011-02-12 00:50:05 +00001187 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001188 return false;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001189 break;
1190 case eEncodingA1:
1191 lr = pc + 4; // return address
1192 Rm = Bits32(opcode, 3, 0);
1193 // if m == 15 then UNPREDICTABLE;
1194 if (Rm == 15)
1195 return false;
Johnny Chenb77be412011-02-04 00:40:18 +00001196 break;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001197 default:
1198 return false;
1199 }
Johnny Chenab3b3512011-02-12 00:10:51 +00001200 addr_t target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
1201 if (!success)
1202 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001203 Register dwarf_reg;
1204 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm);
1205 context.SetRegister (dwarf_reg);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001206 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1207 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001208 if (!BXWritePC(context, target, dwarf_reg))
Johnny Chen9b8d7832011-02-02 01:13:56 +00001209 return false;
1210 }
1211 return true;
1212}
1213
Johnny Chenab3b3512011-02-12 00:10:51 +00001214// Branch and Exchange causes a branch to an address and instruction set specified by a register.
1215// BX
1216bool
1217EmulateInstructionARM::EmulateBXRm (ARMEncoding encoding)
1218{
1219#if 0
1220 // ARM pseudo code...
1221 if (ConditionPassed())
1222 {
1223 EncodingSpecificOperations();
1224 BXWritePC(R[m]);
1225 }
1226#endif
1227
1228 bool success = false;
1229 const uint32_t opcode = OpcodeAsUnsigned (&success);
1230 if (!success)
1231 return false;
1232
1233 if (ConditionPassed())
1234 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001235 EmulateInstruction::Context context;
1236 context.type = EmulateInstruction::eContextAbsoluteBranchRegister;
Johnny Chenab3b3512011-02-12 00:10:51 +00001237 uint32_t Rm; // the register with the target address
1238 switch (encoding) {
1239 case eEncodingT1:
1240 Rm = Bits32(opcode, 6, 3);
Johnny Chen098ae2d2011-02-12 00:50:05 +00001241 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001242 return false;
1243 break;
1244 case eEncodingA1:
1245 Rm = Bits32(opcode, 3, 0);
1246 break;
1247 default:
1248 return false;
1249 }
1250 addr_t target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
1251 if (!success)
1252 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001253
1254 Register dwarf_reg;
1255 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm);
1256 if (!BXWritePC(context, target, dwarf_reg))
Johnny Chenab3b3512011-02-12 00:10:51 +00001257 return false;
1258 }
1259 return true;
1260}
1261
Johnny Chen0d0148e2011-01-28 02:26:08 +00001262// Set r7 to point to some ip offset.
1263// SUB (immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001264bool
1265EmulateInstructionARM::EmulateSubR7IPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +00001266{
1267#if 0
1268 // ARM pseudo code...
1269 if (ConditionPassed())
1270 {
1271 EncodingSpecificOperations();
1272 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
1273 if d == 15 then // Can only occur for ARM encoding
1274 ALUWritePC(result); // setflags is always FALSE here
1275 else
1276 R[d] = result;
1277 if setflags then
1278 APSR.N = result<31>;
1279 APSR.Z = IsZeroBit(result);
1280 APSR.C = carry;
1281 APSR.V = overflow;
1282 }
1283#endif
1284
1285 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001286 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001287 if (!success)
1288 return false;
1289
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001290 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +00001291 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001292 const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001293 if (!success)
1294 return false;
1295 uint32_t imm32;
1296 switch (encoding) {
1297 case eEncodingA1:
1298 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
1299 break;
1300 default:
1301 return false;
1302 }
1303 addr_t ip_offset = imm32;
1304 addr_t addr = ip - ip_offset; // the adjusted ip value
1305
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001306 EmulateInstruction::Context context;
1307 context.type = EmulateInstruction::eContextRegisterPlusOffset;
1308 Register dwarf_reg;
1309 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r12);
1310 context.SetRegisterPlusOffset (dwarf_reg, -ip_offset);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001311
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001312 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +00001313 return false;
1314 }
1315 return true;
1316}
1317
1318// Set ip to point to some stack offset.
1319// SUB (SP minus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001320bool
1321EmulateInstructionARM::EmulateSubIPSPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +00001322{
1323#if 0
1324 // ARM pseudo code...
1325 if (ConditionPassed())
1326 {
1327 EncodingSpecificOperations();
1328 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
1329 if d == 15 then // Can only occur for ARM encoding
1330 ALUWritePC(result); // setflags is always FALSE here
1331 else
1332 R[d] = result;
1333 if setflags then
1334 APSR.N = result<31>;
1335 APSR.Z = IsZeroBit(result);
1336 APSR.C = carry;
1337 APSR.V = overflow;
1338 }
1339#endif
1340
1341 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001342 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001343 if (!success)
1344 return false;
1345
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001346 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +00001347 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001348 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001349 if (!success)
1350 return false;
1351 uint32_t imm32;
1352 switch (encoding) {
1353 case eEncodingA1:
1354 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
1355 break;
1356 default:
1357 return false;
1358 }
1359 addr_t sp_offset = imm32;
1360 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
1361
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001362 EmulateInstruction::Context context;
1363 context.type = EmulateInstruction::eContextRegisterPlusOffset;
1364 Register dwarf_reg;
1365 dwarf_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
1366 context.SetRegisterPlusOffset (dwarf_reg, -sp_offset);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001367
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001368 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +00001369 return false;
1370 }
1371 return true;
1372}
1373
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001374// A sub operation to adjust the SP -- allocate space for local storage.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001375bool
1376EmulateInstructionARM::EmulateSubSPImmdiate (ARMEncoding encoding)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001377{
1378#if 0
1379 // ARM pseudo code...
1380 if (ConditionPassed())
1381 {
1382 EncodingSpecificOperations();
1383 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
1384 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +00001385 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001386 else
1387 R[d] = result;
1388 if setflags then
1389 APSR.N = result<31>;
1390 APSR.Z = IsZeroBit(result);
1391 APSR.C = carry;
1392 APSR.V = overflow;
1393 }
1394#endif
1395
1396 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001397 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001398 if (!success)
1399 return false;
1400
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001401 if (ConditionPassed())
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001402 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001403 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001404 if (!success)
1405 return false;
1406 uint32_t imm32;
1407 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +00001408 case eEncodingT1:
1409 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +00001410 case eEncodingT2:
1411 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
1412 break;
1413 case eEncodingT3:
1414 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
1415 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001416 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +00001417 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001418 break;
1419 default:
1420 return false;
1421 }
1422 addr_t sp_offset = imm32;
1423 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
1424
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001425 EmulateInstruction::Context context;
1426 context.type = EmulateInstruction::eContextAdjustStackPointer;
1427 context.SetImmediateSigned (-sp_offset);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001428
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001429 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001430 return false;
1431 }
1432 return true;
1433}
1434
Johnny Chen08c25e82011-01-31 18:02:28 +00001435// A store operation to the stack that also updates the SP.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001436bool
1437EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding)
Johnny Chence1ca772011-01-25 01:13:00 +00001438{
1439#if 0
1440 // ARM pseudo code...
1441 if (ConditionPassed())
1442 {
1443 EncodingSpecificOperations();
1444 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
1445 address = if index then offset_addr else R[n];
1446 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
1447 if wback then R[n] = offset_addr;
1448 }
1449#endif
1450
1451 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001452 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chence1ca772011-01-25 01:13:00 +00001453 if (!success)
1454 return false;
1455
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001456 if (ConditionPassed())
Johnny Chence1ca772011-01-25 01:13:00 +00001457 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001458 const uint32_t addr_byte_size = GetAddressByteSize();
1459 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001460 if (!success)
1461 return false;
Johnny Chen91d99862011-01-25 19:07:04 +00001462 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +00001463 uint32_t imm12;
1464 switch (encoding) {
1465 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +00001466 Rt = Bits32(opcode, 15, 12);
1467 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +00001468 break;
1469 default:
1470 return false;
1471 }
1472 addr_t sp_offset = imm12;
1473 addr_t addr = sp - sp_offset;
1474
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001475 EmulateInstruction::Context context;
1476 context.type = EmulateInstruction::eContextPushRegisterOnStack;
1477 Register dwarf_reg;
1478 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chen91d99862011-01-25 19:07:04 +00001479 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +00001480 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001481 dwarf_reg.num = dwarf_r0 + Rt;
1482 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
1483 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001484 if (!success)
1485 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001486 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +00001487 return false;
1488 }
1489 else
1490 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001491 dwarf_reg.num = dwarf_pc;
1492 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001493 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001494 if (!success)
1495 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001496 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +00001497 return false;
1498 }
1499
1500 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001501 context.SetImmediateSigned (-sp_offset);
Johnny Chence1ca772011-01-25 01:13:00 +00001502
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001503 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chence1ca772011-01-25 01:13:00 +00001504 return false;
1505 }
1506 return true;
1507}
1508
Johnny Chen08c25e82011-01-31 18:02:28 +00001509// Vector Push stores multiple extension registers to the stack.
1510// It also updates SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001511bool
1512EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding)
Johnny Chen799dfd02011-01-26 23:14:33 +00001513{
1514#if 0
1515 // ARM pseudo code...
1516 if (ConditionPassed())
1517 {
1518 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
1519 address = SP - imm32;
1520 SP = SP - imm32;
1521 if single_regs then
1522 for r = 0 to regs-1
1523 MemA[address,4] = S[d+r]; address = address+4;
1524 else
1525 for r = 0 to regs-1
1526 // Store as two word-aligned words in the correct order for current endianness.
1527 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
1528 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
1529 address = address+8;
1530 }
1531#endif
1532
1533 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001534 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001535 if (!success)
1536 return false;
1537
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001538 if (ConditionPassed())
Johnny Chen799dfd02011-01-26 23:14:33 +00001539 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001540 const uint32_t addr_byte_size = GetAddressByteSize();
1541 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001542 if (!success)
1543 return false;
1544 bool single_regs;
Johnny Chen587a0a42011-02-01 18:35:28 +00001545 uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register
Johnny Chen799dfd02011-01-26 23:14:33 +00001546 uint32_t imm32; // stack offset
1547 uint32_t regs; // number of registers
1548 switch (encoding) {
1549 case eEncodingT1:
1550 case eEncodingA1:
1551 single_regs = false;
Johnny Chenbd599902011-02-10 21:39:01 +00001552 d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12);
Johnny Chen799dfd02011-01-26 23:14:33 +00001553 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1554 // If UInt(imm8) is odd, see "FSTMX".
1555 regs = Bits32(opcode, 7, 0) / 2;
1556 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1557 if (regs == 0 || regs > 16 || (d + regs) > 32)
1558 return false;
1559 break;
1560 case eEncodingT2:
1561 case eEncodingA2:
1562 single_regs = true;
Johnny Chenbd599902011-02-10 21:39:01 +00001563 d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22);
Johnny Chen799dfd02011-01-26 23:14:33 +00001564 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1565 regs = Bits32(opcode, 7, 0);
1566 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1567 if (regs == 0 || regs > 16 || (d + regs) > 32)
1568 return false;
1569 break;
1570 default:
1571 return false;
1572 }
1573 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
1574 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
1575 addr_t sp_offset = imm32;
1576 addr_t addr = sp - sp_offset;
1577 uint32_t i;
1578
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001579 EmulateInstruction::Context context;
1580 context.type = EmulateInstruction::eContextPushRegisterOnStack;
1581 Register dwarf_reg;
1582 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chen799dfd02011-01-26 23:14:33 +00001583 for (i=d; i<regs; ++i)
1584 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001585 dwarf_reg.num = start_reg + i;
1586 context.SetRegisterPlusOffset ( dwarf_reg, addr - sp);
Johnny Chen799dfd02011-01-26 23:14:33 +00001587 // uint64_t to accommodate 64-bit registers.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001588 uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001589 if (!success)
1590 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001591 if (!WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
Johnny Chen799dfd02011-01-26 23:14:33 +00001592 return false;
1593 addr += reg_byte_size;
1594 }
1595
1596 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001597 context.SetImmediateSigned (-sp_offset);
Johnny Chen799dfd02011-01-26 23:14:33 +00001598
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001599 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chen799dfd02011-01-26 23:14:33 +00001600 return false;
1601 }
1602 return true;
1603}
1604
Johnny Chen587a0a42011-02-01 18:35:28 +00001605// Vector Pop loads multiple extension registers from the stack.
1606// It also updates SP to point just above the loaded data.
1607bool
1608EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding)
1609{
1610#if 0
1611 // ARM pseudo code...
1612 if (ConditionPassed())
1613 {
1614 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
1615 address = SP;
1616 SP = SP + imm32;
1617 if single_regs then
1618 for r = 0 to regs-1
1619 S[d+r] = MemA[address,4]; address = address+4;
1620 else
1621 for r = 0 to regs-1
1622 word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8;
1623 // Combine the word-aligned words in the correct order for current endianness.
1624 D[d+r] = if BigEndian() then word1:word2 else word2:word1;
1625 }
1626#endif
1627
1628 bool success = false;
1629 const uint32_t opcode = OpcodeAsUnsigned (&success);
1630 if (!success)
1631 return false;
1632
1633 if (ConditionPassed())
1634 {
1635 const uint32_t addr_byte_size = GetAddressByteSize();
1636 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
1637 if (!success)
1638 return false;
1639 bool single_regs;
1640 uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register
1641 uint32_t imm32; // stack offset
1642 uint32_t regs; // number of registers
1643 switch (encoding) {
1644 case eEncodingT1:
1645 case eEncodingA1:
1646 single_regs = false;
Johnny Chenbd599902011-02-10 21:39:01 +00001647 d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12);
Johnny Chen587a0a42011-02-01 18:35:28 +00001648 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1649 // If UInt(imm8) is odd, see "FLDMX".
1650 regs = Bits32(opcode, 7, 0) / 2;
1651 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1652 if (regs == 0 || regs > 16 || (d + regs) > 32)
1653 return false;
1654 break;
1655 case eEncodingT2:
1656 case eEncodingA2:
1657 single_regs = true;
Johnny Chenbd599902011-02-10 21:39:01 +00001658 d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22);
Johnny Chen587a0a42011-02-01 18:35:28 +00001659 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1660 regs = Bits32(opcode, 7, 0);
1661 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1662 if (regs == 0 || regs > 16 || (d + regs) > 32)
1663 return false;
1664 break;
1665 default:
1666 return false;
1667 }
1668 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
1669 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
1670 addr_t sp_offset = imm32;
1671 addr_t addr = sp;
1672 uint32_t i;
1673 uint64_t data; // uint64_t to accomodate 64-bit registers.
1674
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001675 EmulateInstruction::Context context;
1676 context.type = EmulateInstruction::eContextPopRegisterOffStack;
1677 Register dwarf_reg;
1678 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chen587a0a42011-02-01 18:35:28 +00001679 for (i=d; i<regs; ++i)
1680 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001681 dwarf_reg.num = start_reg + i;
1682 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Johnny Chen587a0a42011-02-01 18:35:28 +00001683 data = ReadMemoryUnsigned(context, addr, reg_byte_size, 0, &success);
1684 if (!success)
1685 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001686 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data))
Johnny Chen587a0a42011-02-01 18:35:28 +00001687 return false;
1688 addr += reg_byte_size;
1689 }
1690
1691 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001692 context.SetImmediateSigned (sp_offset);
Johnny Chen587a0a42011-02-01 18:35:28 +00001693
1694 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
1695 return false;
1696 }
1697 return true;
1698}
1699
Johnny Chenb77be412011-02-04 00:40:18 +00001700// SVC (previously SWI)
1701bool
1702EmulateInstructionARM::EmulateSVC (ARMEncoding encoding)
1703{
1704#if 0
1705 // ARM pseudo code...
1706 if (ConditionPassed())
1707 {
1708 EncodingSpecificOperations();
1709 CallSupervisor();
1710 }
1711#endif
1712
1713 bool success = false;
1714 const uint32_t opcode = OpcodeAsUnsigned (&success);
1715 if (!success)
1716 return false;
1717
1718 if (ConditionPassed())
1719 {
1720 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1721 addr_t lr; // next instruction address
1722 if (!success)
1723 return false;
1724 uint32_t imm32; // the immediate constant
1725 uint32_t mode; // ARM or Thumb mode
1726 switch (encoding) {
1727 case eEncodingT1:
1728 lr = (pc + 2) | 1u; // return address
1729 imm32 = Bits32(opcode, 7, 0);
1730 mode = eModeThumb;
1731 break;
1732 case eEncodingA1:
1733 lr = pc + 4; // return address
1734 imm32 = Bits32(opcode, 23, 0);
1735 mode = eModeARM;
1736 break;
1737 default:
1738 return false;
1739 }
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001740
1741 EmulateInstruction::Context context;
1742 context.type = EmulateInstruction::eContextSupervisorCall;
1743 context.SetModeAndImmediate (mode, imm32);
Johnny Chenb77be412011-02-04 00:40:18 +00001744 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1745 return false;
1746 }
1747 return true;
1748}
1749
Johnny Chenc315f862011-02-05 00:46:10 +00001750// If Then makes up to four following instructions (the IT block) conditional.
1751bool
1752EmulateInstructionARM::EmulateIT (ARMEncoding encoding)
1753{
1754#if 0
1755 // ARM pseudo code...
1756 EncodingSpecificOperations();
1757 ITSTATE.IT<7:0> = firstcond:mask;
1758#endif
1759
1760 bool success = false;
1761 const uint32_t opcode = OpcodeAsUnsigned (&success);
1762 if (!success)
1763 return false;
1764
1765 m_it_session.InitIT(Bits32(opcode, 7, 0));
1766 return true;
1767}
1768
Johnny Chen3b620b32011-02-07 20:11:47 +00001769// Branch causes a branch to a target address.
1770bool
1771EmulateInstructionARM::EmulateB (ARMEncoding encoding)
1772{
1773#if 0
1774 // ARM pseudo code...
1775 if (ConditionPassed())
1776 {
1777 EncodingSpecificOperations();
1778 BranchWritePC(PC + imm32);
1779 }
1780#endif
1781
1782 bool success = false;
1783 const uint32_t opcode = OpcodeAsUnsigned (&success);
1784 if (!success)
1785 return false;
1786
Johnny Chen9ee056b2011-02-08 00:06:35 +00001787 if (ConditionPassed())
1788 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001789 EmulateInstruction::Context context;
1790 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
Johnny Chen9ee056b2011-02-08 00:06:35 +00001791 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001792 if (!success)
1793 return false;
Johnny Chen53ebab72011-02-08 23:21:57 +00001794 addr_t target; // target address
Johnny Chen9ee056b2011-02-08 00:06:35 +00001795 int32_t imm32; // PC-relative offset
1796 switch (encoding) {
1797 case eEncodingT1:
1798 // The 'cond' field is handled in EmulateInstructionARM::CurrentCond().
1799 imm32 = llvm::SignExtend32<9>(Bits32(opcode, 7, 0) << 1);
1800 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001801 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001802 break;
1803 case eEncodingT2:
1804 imm32 = llvm::SignExtend32<12>(Bits32(opcode, 10, 0));
1805 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001806 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001807 break;
1808 case eEncodingT3:
1809 // The 'cond' field is handled in EmulateInstructionARM::CurrentCond().
1810 {
Johnny Chenbd599902011-02-10 21:39:01 +00001811 uint32_t S = Bit32(opcode, 26);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001812 uint32_t imm6 = Bits32(opcode, 21, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001813 uint32_t J1 = Bit32(opcode, 13);
1814 uint32_t J2 = Bit32(opcode, 11);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001815 uint32_t imm11 = Bits32(opcode, 10, 0);
Johnny Chen53ebab72011-02-08 23:21:57 +00001816 uint32_t imm21 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001817 imm32 = llvm::SignExtend32<21>(imm21);
1818 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001819 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001820 break;
1821 }
1822 case eEncodingT4:
1823 {
Johnny Chenbd599902011-02-10 21:39:01 +00001824 uint32_t S = Bit32(opcode, 26);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001825 uint32_t imm10 = Bits32(opcode, 25, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001826 uint32_t J1 = Bit32(opcode, 13);
1827 uint32_t J2 = Bit32(opcode, 11);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001828 uint32_t imm11 = Bits32(opcode, 10, 0);
1829 uint32_t I1 = !(J1 ^ S);
1830 uint32_t I2 = !(J2 ^ S);
Johnny Chen53ebab72011-02-08 23:21:57 +00001831 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001832 imm32 = llvm::SignExtend32<25>(imm25);
1833 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001834 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001835 break;
1836 }
1837 case eEncodingA1:
1838 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2);
1839 target = pc + 8 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001840 context.SetModeAndImmediateSigned (eModeARM, 8 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001841 break;
1842 default:
1843 return false;
1844 }
1845 if (!BranchWritePC(context, target))
1846 return false;
1847 }
1848 return true;
Johnny Chen3b620b32011-02-07 20:11:47 +00001849}
1850
Johnny Chen53ebab72011-02-08 23:21:57 +00001851// Compare and Branch on Nonzero and Compare and Branch on Zero compare the value in a register with
1852// zero and conditionally branch forward a constant value. They do not affect the condition flags.
1853// CBNZ, CBZ
1854bool
1855EmulateInstructionARM::EmulateCB (ARMEncoding encoding)
1856{
1857#if 0
1858 // ARM pseudo code...
1859 EncodingSpecificOperations();
1860 if nonzero ^ IsZero(R[n]) then
1861 BranchWritePC(PC + imm32);
1862#endif
1863
1864 bool success = false;
1865 const uint32_t opcode = OpcodeAsUnsigned (&success);
1866 if (!success)
1867 return false;
1868
1869 // Read the register value from the operand register Rn.
1870 uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Bits32(opcode, 2, 0), 0, &success);
1871 if (!success)
1872 return false;
1873
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001874 EmulateInstruction::Context context;
1875 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
Johnny Chen53ebab72011-02-08 23:21:57 +00001876 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1877 if (!success)
1878 return false;
1879
1880 addr_t target; // target address
1881 uint32_t imm32; // PC-relative offset to branch forward
1882 bool nonzero;
1883 switch (encoding) {
1884 case eEncodingT1:
Johnny Chenbd599902011-02-10 21:39:01 +00001885 imm32 = Bit32(opcode, 9) << 6 | Bits32(opcode, 7, 3) << 1;
Johnny Chen53ebab72011-02-08 23:21:57 +00001886 nonzero = BitIsSet(opcode, 11);
1887 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001888 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen53ebab72011-02-08 23:21:57 +00001889 break;
1890 default:
1891 return false;
1892 }
1893 if (nonzero ^ (reg_val == 0))
1894 if (!BranchWritePC(context, target))
1895 return false;
1896
1897 return true;
1898}
1899
Johnny Chen26863dc2011-02-09 23:43:29 +00001900// ADD <Rdn>, <Rm>
1901// where <Rdn> the destination register is also the first operand register
1902// and <Rm> is the second operand register.
1903bool
1904EmulateInstructionARM::EmulateAddRdnRm (ARMEncoding encoding)
1905{
1906#if 0
1907 // ARM pseudo code...
1908 if ConditionPassed() then
1909 EncodingSpecificOperations();
1910 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
1911 (result, carry, overflow) = AddWithCarry(R[n], shifted, '0');
1912 if d == 15 then
1913 ALUWritePC(result); // setflags is always FALSE here
1914 else
1915 R[d] = result;
1916 if setflags then
1917 APSR.N = result<31>;
1918 APSR.Z = IsZeroBit(result);
1919 APSR.C = carry;
1920 APSR.V = overflow;
1921#endif
1922
1923 bool success = false;
1924 const uint32_t opcode = OpcodeAsUnsigned (&success);
1925 if (!success)
1926 return false;
1927
1928 if (ConditionPassed())
1929 {
1930 uint32_t Rd, Rn, Rm;
1931 //bool setflags = false;
1932 switch (encoding)
1933 {
1934 case eEncodingT2:
1935 // setflags = FALSE
Johnny Chenbd599902011-02-10 21:39:01 +00001936 Rd = Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0);
Johnny Chen26863dc2011-02-09 23:43:29 +00001937 Rm = Bits32(opcode, 6, 3);
1938 if (Rn == 15 && Rm == 15)
1939 return false;
1940 break;
1941 default:
1942 return false;
1943 }
1944
1945 int32_t result, val1, val2;
1946 // Read the first operand.
1947 if (Rn == 15)
1948 val1 = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1949 else
1950 val1 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
1951 if (!success)
1952 return false;
1953
1954 // Read the second operand.
1955 if (Rm == 15)
1956 val2 = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1957 else
1958 val2 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
1959 if (!success)
1960 return false;
1961
1962 result = val1 + val2;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001963
1964 EmulateInstruction::Context context;
1965 context.type = EmulateInstruction::eContextImmediate;
1966 context.SetNoArgs ();
1967 Register dummy_reg;
1968 dummy_reg.SetRegister (eRegisterKindDWARF, dwarf_r0);
Johnny Chen26863dc2011-02-09 23:43:29 +00001969
1970 if (Rd == 15)
1971 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001972 if (!ALUWritePC (context, result, dummy_reg))
Johnny Chen26863dc2011-02-09 23:43:29 +00001973 return false;
1974 }
1975 else
1976 {
Johnny Chen33bf6ab2011-02-14 20:39:01 +00001977 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
Johnny Chen26863dc2011-02-09 23:43:29 +00001978 return false;
1979 }
1980 }
1981 return true;
1982}
1983
Johnny Chene4a4d302011-02-11 21:53:58 +00001984// CMP (immediate)
Johnny Chend4dc4442011-02-11 02:02:56 +00001985bool
1986EmulateInstructionARM::EmulateCmpRnImm (ARMEncoding encoding)
1987{
1988#if 0
1989 // ARM pseudo code...
1990 if ConditionPassed() then
1991 EncodingSpecificOperations();
1992 (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1');
1993 APSR.N = result<31>;
1994 APSR.Z = IsZeroBit(result);
1995 APSR.C = carry;
1996 APSR.V = overflow;
1997#endif
1998
1999 bool success = false;
2000 const uint32_t opcode = OpcodeAsUnsigned (&success);
2001 if (!success)
2002 return false;
2003
2004 uint32_t Rn; // the first operand
2005 uint32_t imm32; // the immediate value to be compared with
2006 switch (encoding) {
2007 case eEncodingT1:
2008 Rn = Bits32(opcode, 10, 8);
2009 imm32 = Bits32(opcode, 7, 0);
2010 break;
2011 default:
2012 return false;
2013 }
2014 // Read the register value from the operand register Rn.
2015 uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
2016 if (!success)
2017 return false;
2018
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002019 EmulateInstruction::Context context;
2020 context.type = EmulateInstruction::eContextImmediate;
2021 context.SetNoArgs ();
2022
Johnny Chend4dc4442011-02-11 02:02:56 +00002023 AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1);
2024 m_new_inst_cpsr = m_inst_cpsr;
2025 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(res.result, CPSR_N));
2026 SetBit32(m_new_inst_cpsr, CPSR_Z, res.result == 0 ? 1 : 0);
2027 SetBit32(m_new_inst_cpsr, CPSR_C, res.carry_out);
2028 SetBit32(m_new_inst_cpsr, CPSR_V, res.overflow);
2029 if (m_new_inst_cpsr != m_inst_cpsr)
2030 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002031 EmulateInstruction::Context context;
2032 context.type = EmulateInstruction::eContextImmediate;
2033 context.SetNoArgs ();
Johnny Chend4dc4442011-02-11 02:02:56 +00002034 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
2035 return false;
2036 }
2037 return true;
2038}
2039
Johnny Chene4a4d302011-02-11 21:53:58 +00002040// CMP (register)
2041bool
2042EmulateInstructionARM::EmulateCmpRnRm (ARMEncoding encoding)
2043{
2044#if 0
2045 // ARM pseudo code...
2046 if ConditionPassed() then
2047 EncodingSpecificOperations();
2048 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
2049 (result, carry, overflow) = AddWithCarry(R[n], NOT(shifted), '1');
2050 APSR.N = result<31>;
2051 APSR.Z = IsZeroBit(result);
2052 APSR.C = carry;
2053 APSR.V = overflow;
2054#endif
2055
2056 bool success = false;
2057 const uint32_t opcode = OpcodeAsUnsigned (&success);
2058 if (!success)
2059 return false;
2060
2061 uint32_t Rn; // the first operand
2062 uint32_t Rm; // the second operand
2063 switch (encoding) {
2064 case eEncodingT1:
2065 Rn = Bits32(opcode, 2, 0);
2066 Rm = Bits32(opcode, 5, 3);
2067 break;
2068 case eEncodingT2:
2069 Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0);
2070 Rm = Bits32(opcode, 6, 3);
2071 if (Rn < 8 && Rm < 8)
2072 return false;
2073 if (Rn == 15 || Rm == 15)
2074 return false;
2075 break;
2076 default:
2077 return false;
2078 }
2079 // Read the register value from register Rn.
2080 uint32_t reg_val1 = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
2081 if (!success)
2082 return false;
2083 // Read the register value from register Rm.
2084 // The register value is not being shifted since we don't handle ARM for now.
2085 uint32_t reg_val2 = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
2086 if (!success)
2087 return false;
2088
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002089 EmulateInstruction::Context context;
2090 context.type = EmulateInstruction::eContextImmediate;
2091 context.SetNoArgs();
2092
Johnny Chene4a4d302011-02-11 21:53:58 +00002093 AddWithCarryResult res = AddWithCarry(reg_val1, reg_val2, 1);
2094 m_new_inst_cpsr = m_inst_cpsr;
2095 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(res.result, CPSR_N));
2096 SetBit32(m_new_inst_cpsr, CPSR_Z, res.result == 0 ? 1 : 0);
2097 SetBit32(m_new_inst_cpsr, CPSR_C, res.carry_out);
2098 SetBit32(m_new_inst_cpsr, CPSR_V, res.overflow);
2099 if (m_new_inst_cpsr != m_inst_cpsr)
2100 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002101 EmulateInstruction::Context context;
2102 context.type = EmulateInstruction::eContextImmediate;
2103 context.SetNoArgs ();
Johnny Chene4a4d302011-02-11 21:53:58 +00002104 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
2105 return false;
2106 }
2107 return true;
2108}
2109
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002110// LDM loads multiple registers from consecutive memory locations, using an
Caroline Tice713c2662011-02-11 17:59:55 +00002111// address from a base register. Optionally the address just above the highest of those locations
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002112// can be written back to the base register.
2113bool
2114EmulateInstructionARM::EmulateLDM (ARMEncoding encoding)
2115{
2116#if 0
2117 // ARM pseudo code...
2118 if ConditionPassed()
2119 EncodingSpecificOperations(); NullCheckIfThumbEE (n);
2120 address = R[n];
2121
2122 for i = 0 to 14
2123 if registers<i> == '1' then
2124 R[i] = MemA[address, 4]; address = address + 4;
2125 if registers<15> == '1' then
2126 LoadWritePC (MemA[address, 4]);
2127
2128 if wback && registers<n> == '0' then R[n] = R[n] + 4 * BitCount (registers);
2129 if wback && registers<n> == '1' then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2130
2131#endif
2132
2133 bool success = false;
2134 const uint32_t opcode = OpcodeAsUnsigned (&success);
2135 if (!success)
2136 return false;
2137
2138 if (ConditionPassed())
2139 {
2140 uint32_t n;
2141 uint32_t registers = 0;
2142 bool wback;
2143 const uint32_t addr_byte_size = GetAddressByteSize();
2144 switch (encoding)
2145 {
2146 case eEncodingT1:
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002147 // n = UInt(Rn); registers = ’00000000’:register_list; wback = (registers<n> == ’0’);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002148 n = Bits32 (opcode, 10, 8);
2149 registers = Bits32 (opcode, 7, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002150 registers = registers & 0x00ff; // Make sure the top 8 bits are zeros.
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002151 wback = BitIsClear (registers, n);
2152 // if BitCount(registers) < 1 then UNPREDICTABLE;
2153 if (BitCount(registers) < 1)
2154 return false;
2155 break;
2156 case eEncodingT2:
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002157 // if W == ’1’ && Rn == ’1101’ then SEE POP;
2158 // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002159 n = Bits32 (opcode, 19, 16);
2160 registers = Bits32 (opcode, 15, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002161 registers = registers & 0xdfff; // Make sure bit 13 is zero.
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002162 wback = BitIsSet (opcode, 21);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002163
2164 // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE;
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002165 if ((n == 15)
2166 || (BitCount (registers) < 2)
2167 || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15)))
2168 return false;
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002169
2170 // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +00002171 if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock())
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002172 return false;
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002173
2174 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002175 if (wback
2176 && BitIsSet (registers, n))
2177 return false;
2178 break;
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002179
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002180 case eEncodingA1:
2181 n = Bits32 (opcode, 19, 16);
2182 registers = Bits32 (opcode, 15, 0);
2183 wback = BitIsSet (opcode, 21);
2184 if ((n == 15)
2185 || (BitCount (registers) < 1))
2186 return false;
2187 break;
2188 default:
2189 return false;
2190 }
2191
2192 int32_t offset = 0;
2193 const addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2194 if (!success)
2195 return false;
Caroline Tice85aab332011-02-08 23:56:10 +00002196
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002197 EmulateInstruction::Context context;
2198 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2199 Register dwarf_reg;
2200 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2201 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002202
2203 for (int i = 0; i < 14; ++i)
2204 {
2205 if (BitIsSet (registers, i))
2206 {
Caroline Tice85aab332011-02-08 23:56:10 +00002207 context.type = EmulateInstruction::eContextRegisterPlusOffset;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002208 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002209 if (wback && (n == 13)) // Pop Instruction
2210 context.type = EmulateInstruction::eContextPopRegisterOffStack;
2211
2212 // R[i] = MemA [address, 4]; address = address + 4;
2213 uint32_t data = ReadMemoryUnsigned (context, base_address + offset, addr_byte_size, 0, &success);
2214 if (!success)
2215 return false;
2216
2217 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2218 return false;
2219
2220 offset += addr_byte_size;
2221 }
2222 }
2223
2224 if (BitIsSet (registers, 15))
2225 {
2226 //LoadWritePC (MemA [address, 4]);
Caroline Tice85aab332011-02-08 23:56:10 +00002227 context.type = EmulateInstruction::eContextRegisterPlusOffset;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002228 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002229 uint32_t data = ReadMemoryUnsigned (context, base_address + offset, addr_byte_size, 0, &success);
2230 if (!success)
2231 return false;
Johnny Chene62b50d2011-02-09 22:02:17 +00002232 // In ARMv5T and above, this is an interworking branch.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002233 if (!LoadWritePC(context, data, dwarf_reg))
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002234 return false;
2235 }
2236
2237 if (wback && BitIsClear (registers, n))
2238 {
Caroline Ticefa172202011-02-11 22:49:54 +00002239 // R[n] = R[n] + 4 * BitCount (registers)
2240 int32_t offset = addr_byte_size * BitCount (registers);
2241 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002242 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002243
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002244 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, base_address + offset))
2245 return false;
2246 }
2247 if (wback && BitIsSet (registers, n))
2248 // R[n] bits(32) UNKNOWN;
Caroline Tice713c2662011-02-11 17:59:55 +00002249 return WriteBits32Unknown (n);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002250 }
2251 return true;
2252}
Caroline Tice713c2662011-02-11 17:59:55 +00002253
2254// LDMDA loads multiple registers from consecutive memory locations using an address from a base registers.
2255// The consecutive memorty locations end at this address and the address just below the lowest of those locations
2256// can optionally be written back tot he base registers.
2257bool
2258EmulateInstructionARM::EmulateLDMDA (ARMEncoding encoding)
2259{
2260#if 0
2261 // ARM pseudo code...
2262 if ConditionPassed() then
2263 EncodingSpecificOperations();
2264 address = R[n] - 4*BitCount(registers) + 4;
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002265
Caroline Tice713c2662011-02-11 17:59:55 +00002266 for i = 0 to 14
2267 if registers<i> == ’1’ then
2268 R[i] = MemA[address,4]; address = address + 4;
2269
2270 if registers<15> == ’1’ then
2271 LoadWritePC(MemA[address,4]);
2272
2273 if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2274 if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN;
2275#endif
2276
2277 bool success = false;
2278 const uint32_t opcode = OpcodeAsUnsigned (&success);
2279 if (!success)
2280 return false;
2281
2282 if (ConditionPassed())
2283 {
2284 uint32_t n;
2285 uint32_t registers = 0;
2286 bool wback;
2287 const uint32_t addr_byte_size = GetAddressByteSize();
2288
2289 // EncodingSpecificOperations();
2290 switch (encoding)
2291 {
2292 case eEncodingA1:
2293 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2294 n = Bits32 (opcode, 19, 16);
2295 registers = Bits32 (opcode, 15, 0);
2296 wback = BitIsSet (opcode, 21);
2297
2298 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2299 if ((n == 15) || (BitCount (registers) < 1))
2300 return false;
2301
2302 break;
2303
2304 default:
2305 return false;
2306 }
2307 // address = R[n] - 4*BitCount(registers) + 4;
2308
2309 int32_t offset = 0;
2310 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2311
2312 if (!success)
2313 return false;
2314
2315 address = address - (addr_byte_size * BitCount (registers)) + addr_byte_size;
2316
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002317 EmulateInstruction::Context context;
2318 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2319 Register dwarf_reg;
2320 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2321 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice713c2662011-02-11 17:59:55 +00002322
2323 // for i = 0 to 14
2324 for (int i = 0; i < 14; ++i)
2325 {
2326 // if registers<i> == ’1’ then
2327 if (BitIsSet (registers, i))
2328 {
2329 // R[i] = MemA[address,4]; address = address + 4;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002330 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice713c2662011-02-11 17:59:55 +00002331 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2332 if (!success)
2333 return false;
2334 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2335 return false;
2336 offset += addr_byte_size;
2337 }
2338 }
2339
2340 // if registers<15> == ’1’ then
2341 // LoadWritePC(MemA[address,4]);
2342 if (BitIsSet (registers, 15))
2343 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002344 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice713c2662011-02-11 17:59:55 +00002345 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2346 if (!success)
2347 return false;
Johnny Chen44c10f02011-02-11 19:37:03 +00002348 // In ARMv5T and above, this is an interworking branch.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002349 if (!LoadWritePC(context, data, dwarf_reg))
Caroline Tice713c2662011-02-11 17:59:55 +00002350 return false;
2351 }
2352
2353 // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2354 if (wback && BitIsClear (registers, n))
2355 {
Caroline Tice713c2662011-02-11 17:59:55 +00002356 addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2357 if (!success)
2358 return false;
Caroline Ticefa172202011-02-11 22:49:54 +00002359
2360 offset = (addr_byte_size * BitCount (registers)) * -1;
2361 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002362 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002363 addr = addr + offset;
Caroline Tice713c2662011-02-11 17:59:55 +00002364 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr))
2365 return false;
2366 }
2367
2368 // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN;
2369 if (wback && BitIsSet (registers, n))
2370 return WriteBits32Unknown (n);
2371 }
2372 return true;
2373}
2374
2375// LDMDB loads multiple registers from consecutive memory locations using an address from a base register. The
2376// consecutive memory lcoations end just below this address, and the address of the lowest of those locations can
2377// be optionally written back to the base register.
Caroline Tice0b29e242011-02-08 23:16:02 +00002378bool
2379EmulateInstructionARM::EmulateLDMDB (ARMEncoding encoding)
2380{
2381#if 0
2382 // ARM pseudo code...
2383 if ConditionPassed() then
2384 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
2385 address = R[n] - 4*BitCount(registers);
2386
2387 for i = 0 to 14
2388 if registers<i> == ’1’ then
2389 R[i] = MemA[address,4]; address = address + 4;
2390 if registers<15> == ’1’ then
2391 LoadWritePC(MemA[address,4]);
2392
2393 if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2394 if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2395#endif
2396
2397 bool success = false;
2398 const uint32_t opcode = OpcodeAsUnsigned (&success);
2399 if (!success)
2400 return false;
2401
2402 if (ConditionPassed())
2403 {
2404 uint32_t n;
2405 uint32_t registers = 0;
2406 bool wback;
2407 const uint32_t addr_byte_size = GetAddressByteSize();
2408 switch (encoding)
2409 {
2410 case eEncodingT1:
2411 // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’);
2412 n = Bits32 (opcode, 19, 16);
2413 registers = Bits32 (opcode, 15, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002414 registers = registers & 0xdfff; // Make sure bit 13 is a zero.
Caroline Tice0b29e242011-02-08 23:16:02 +00002415 wback = BitIsSet (opcode, 21);
2416
2417 // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE;
2418 if ((n == 15)
2419 || (BitCount (registers) < 2)
2420 || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15)))
2421 return false;
2422
2423 // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +00002424 if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock())
Caroline Tice0b29e242011-02-08 23:16:02 +00002425 return false;
2426
2427 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
2428 if (wback && BitIsSet (registers, n))
2429 return false;
2430
2431 break;
2432
2433 case eEncodingA1:
2434 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2435 n = Bits32 (opcode, 19, 16);
2436 registers = Bits32 (opcode, 15, 0);
2437 wback = BitIsSet (opcode, 21);
2438
2439 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2440 if ((n == 15) || (BitCount (registers) < 1))
2441 return false;
2442
2443 break;
2444
2445 default:
2446 return false;
2447 }
2448
Caroline Tice713c2662011-02-11 17:59:55 +00002449 // address = R[n] - 4*BitCount(registers);
2450
Caroline Tice0b29e242011-02-08 23:16:02 +00002451 int32_t offset = 0;
Caroline Tice713c2662011-02-11 17:59:55 +00002452 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2453
2454 if (!success)
2455 return false;
2456
2457 address = address - (addr_byte_size * BitCount (registers));
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002458 EmulateInstruction::Context context;
2459 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2460 Register dwarf_reg;
2461 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2462 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice0b29e242011-02-08 23:16:02 +00002463
2464 for (int i = 0; i < 14; ++i)
2465 {
2466 if (BitIsSet (registers, i))
2467 {
2468 // R[i] = MemA[address,4]; address = address + 4;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002469 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice0b29e242011-02-08 23:16:02 +00002470 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2471 if (!success)
2472 return false;
2473
2474 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2475 return false;
2476
2477 offset += addr_byte_size;
2478 }
2479 }
2480
2481 // if registers<15> == ’1’ then
2482 // LoadWritePC(MemA[address,4]);
2483 if (BitIsSet (registers, 15))
2484 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002485 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice0b29e242011-02-08 23:16:02 +00002486 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2487 if (!success)
2488 return false;
Johnny Chene62b50d2011-02-09 22:02:17 +00002489 // In ARMv5T and above, this is an interworking branch.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002490 if (!LoadWritePC(context, data, dwarf_reg))
Caroline Tice0b29e242011-02-08 23:16:02 +00002491 return false;
2492 }
2493
2494 // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2495 if (wback && BitIsClear (registers, n))
2496 {
Caroline Tice0b29e242011-02-08 23:16:02 +00002497 addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2498 if (!success)
2499 return false;
Caroline Ticefa172202011-02-11 22:49:54 +00002500
2501 offset = (addr_byte_size * BitCount (registers)) * -1;
2502 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002503 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002504 addr = addr + offset;
Caroline Tice0b29e242011-02-08 23:16:02 +00002505 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr))
2506 return false;
2507 }
2508
2509 // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2510 if (wback && BitIsSet (registers, n))
Caroline Tice713c2662011-02-11 17:59:55 +00002511 return WriteBits32Unknown (n);
Caroline Tice0b29e242011-02-08 23:16:02 +00002512 }
2513 return true;
2514}
Caroline Tice85aab332011-02-08 23:56:10 +00002515
Caroline Tice713c2662011-02-11 17:59:55 +00002516// LDMIB loads multiple registers from consecutive memory locations using an address from a base register. The
2517// consecutive memory locations start just above this address, and thea ddress of the last of those locations can
2518// optinoally be written back to the base register.
Caroline Tice85aab332011-02-08 23:56:10 +00002519bool
2520EmulateInstructionARM::EmulateLDMIB (ARMEncoding encoding)
2521{
2522#if 0
2523 if ConditionPassed() then
2524 EncodingSpecificOperations();
2525 address = R[n] + 4;
2526
2527 for i = 0 to 14
2528 if registers<i> == ’1’ then
2529 R[i] = MemA[address,4]; address = address + 4;
2530 if registers<15> == ’1’ then
2531 LoadWritePC(MemA[address,4]);
2532
2533 if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers);
2534 if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN;
2535#endif
2536
2537 bool success = false;
2538 const uint32_t opcode = OpcodeAsUnsigned (&success);
2539 if (!success)
2540 return false;
2541
2542 if (ConditionPassed())
2543 {
2544 uint32_t n;
2545 uint32_t registers = 0;
2546 bool wback;
2547 const uint32_t addr_byte_size = GetAddressByteSize();
2548 switch (encoding)
2549 {
2550 case eEncodingA1:
2551 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2552 n = Bits32 (opcode, 19, 16);
2553 registers = Bits32 (opcode, 15, 0);
2554 wback = BitIsSet (opcode, 21);
2555
2556 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2557 if ((n == 15) || (BitCount (registers) < 1))
2558 return false;
2559
2560 break;
2561 default:
2562 return false;
2563 }
2564 // address = R[n] + 4;
2565
2566 int32_t offset = 0;
Caroline Tice713c2662011-02-11 17:59:55 +00002567 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2568
2569 if (!success)
2570 return false;
2571
2572 address = address + addr_byte_size;
Caroline Tice85aab332011-02-08 23:56:10 +00002573
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002574 EmulateInstruction::Context context;
2575 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2576 Register dwarf_reg;
2577 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2578 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice85aab332011-02-08 23:56:10 +00002579
2580 for (int i = 0; i < 14; ++i)
2581 {
2582 if (BitIsSet (registers, i))
2583 {
2584 // R[i] = MemA[address,4]; address = address + 4;
2585
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002586 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice85aab332011-02-08 23:56:10 +00002587 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2588 if (!success)
2589 return false;
2590
2591 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2592 return false;
2593
2594 offset += addr_byte_size;
2595 }
2596 }
2597
2598 // if registers<15> == ’1’ then
2599 // LoadWritePC(MemA[address,4]);
2600 if (BitIsSet (registers, 15))
2601 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002602 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice85aab332011-02-08 23:56:10 +00002603 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2604 if (!success)
2605 return false;
Johnny Chene62b50d2011-02-09 22:02:17 +00002606 // In ARMv5T and above, this is an interworking branch.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002607 if (!LoadWritePC(context, data, dwarf_reg))
Caroline Tice85aab332011-02-08 23:56:10 +00002608 return false;
2609 }
2610
2611 // if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers);
2612 if (wback && BitIsClear (registers, n))
2613 {
Caroline Tice85aab332011-02-08 23:56:10 +00002614 addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2615 if (!success)
2616 return false;
Caroline Ticefa172202011-02-11 22:49:54 +00002617
2618 offset = addr_byte_size * BitCount (registers);
2619 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002620 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002621 addr = addr + offset;
Caroline Tice85aab332011-02-08 23:56:10 +00002622 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr))
2623 return false;
2624 }
2625
2626 // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2627 if (wback && BitIsSet (registers, n))
Caroline Tice713c2662011-02-11 17:59:55 +00002628 return WriteBits32Unknown (n);
Caroline Tice85aab332011-02-08 23:56:10 +00002629 }
2630 return true;
2631}
Caroline Tice0b29e242011-02-08 23:16:02 +00002632
Johnny Chenef21b592011-02-10 01:52:38 +00002633// Load Register (immediate) calculates an address from a base register value and
2634// an immediate offset, loads a word from memory, and writes to a register.
2635// LDR (immediate, Thumb)
2636bool
2637EmulateInstructionARM::EmulateLDRRtRnImm (ARMEncoding encoding)
2638{
2639#if 0
2640 // ARM pseudo code...
2641 if (ConditionPassed())
2642 {
2643 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
2644 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
2645 address = if index then offset_addr else R[n];
2646 data = MemU[address,4];
2647 if wback then R[n] = offset_addr;
2648 if t == 15 then
2649 if address<1:0> == '00' then LoadWritePC(data); else UNPREDICTABLE;
2650 elsif UnalignedSupport() || address<1:0> = '00' then
2651 R[t] = data;
2652 else R[t] = bits(32) UNKNOWN; // Can only apply before ARMv7
2653 }
2654#endif
2655
2656 bool success = false;
2657 const uint32_t opcode = OpcodeAsUnsigned (&success);
2658 if (!success)
2659 return false;
2660
2661 if (ConditionPassed())
2662 {
2663 uint32_t Rt; // the destination register
2664 uint32_t Rn; // the base register
2665 uint32_t imm32; // the immediate offset used to form the address
2666 addr_t offset_addr; // the offset address
2667 addr_t address; // the calculated address
2668 uint32_t data; // the literal data value from memory load
2669 bool add, index, wback;
2670 switch (encoding) {
2671 case eEncodingT1:
2672 Rt = Bits32(opcode, 5, 3);
2673 Rn = Bits32(opcode, 2, 0);
2674 imm32 = Bits32(opcode, 10, 6) << 2; // imm32 = ZeroExtend(imm5:'00', 32);
2675 // index = TRUE; add = TRUE; wback = FALSE
2676 add = true;
2677 index = true;
2678 wback = false;
2679 break;
2680 default:
2681 return false;
2682 }
2683 uint32_t base = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
2684 if (!success)
2685 return false;
2686 if (add)
2687 offset_addr = base + imm32;
2688 else
2689 offset_addr = base - imm32;
2690
2691 address = (index ? offset_addr : base);
2692
2693 if (wback)
2694 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002695 EmulateInstruction::Context ctx;
2696 ctx.type = EmulateInstruction::eContextRegisterPlusOffset;
2697 Register dwarf_reg;
2698 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rn);
2699 ctx.SetRegisterPlusOffset (dwarf_reg, (int32_t) (offset_addr - base));
2700
Johnny Chenef21b592011-02-10 01:52:38 +00002701 if (!WriteRegisterUnsigned (ctx, eRegisterKindDWARF, dwarf_r0 + Rn, offset_addr))
2702 return false;
2703 }
2704
2705 // Prepare to write to the Rt register.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002706 EmulateInstruction::Context context;
2707 context.type = EmulateInstruction::eContextImmediate;
2708 context.SetNoArgs ();
2709 Register dummy_reg;
2710 dummy_reg.SetRegister (eRegisterKindDWARF, dwarf_r0);
Johnny Chenef21b592011-02-10 01:52:38 +00002711
2712 // Read memory from the address.
2713 data = ReadMemoryUnsigned(context, address, 4, 0, &success);
2714 if (!success)
2715 return false;
Johnny Chenef21b592011-02-10 01:52:38 +00002716
2717 if (Rt == 15)
2718 {
2719 if (Bits32(address, 1, 0) == 0)
2720 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002721 if (!LoadWritePC(context, data, dummy_reg))
Johnny Chenef21b592011-02-10 01:52:38 +00002722 return false;
2723 }
2724 else
2725 return false;
2726 }
2727 else if (UnalignedSupport() || Bits32(address, 1, 0) == 0)
2728 {
2729 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data))
2730 return false;
2731 }
2732 else
2733 return false;
2734 }
2735 return true;
2736}
2737
Caroline Ticefa172202011-02-11 22:49:54 +00002738// STM stores multiple registers to consecutive memory locations using an address from a base register. The
2739// consecutive memory locations start at this address, and teh address just above the last of those locations can
2740// optionally be written back to the base register.
2741bool
2742EmulateInstructionARM::EmulateSTM (ARMEncoding encoding)
2743{
2744#if 0
2745 if ConditionPassed() then
2746 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
2747 address = R[n];
2748
2749 for i = 0 to 14
2750 if registers<i> == ’1’ then
2751 if i == n && wback && i != LowestSetBit(registers) then
2752 MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1
2753 else
2754 MemA[address,4] = R[i];
2755 address = address + 4;
2756
2757 if registers<15> == ’1’ then // Only possible for encoding A1
2758 MemA[address,4] = PCStoreValue();
2759 if wback then R[n] = R[n] + 4*BitCount(registers);
2760#endif
2761
2762 bool success = false;
2763 const uint32_t opcode = OpcodeAsUnsigned (&success);
2764 if (!success)
2765 return false;
2766
2767 if (ConditionPassed ())
2768 {
2769 uint32_t n;
2770 uint32_t registers = 0;
2771 bool wback;
2772 const uint32_t addr_byte_size = GetAddressByteSize();
2773
2774 // EncodingSpecificOperations(); NullCheckIfThumbEE(n);
2775 switch (encoding)
2776 {
2777 case eEncodingT1:
2778 // n = UInt(Rn); registers = ’00000000’:register_list; wback = TRUE;
2779 n = Bits32 (opcode, 10, 8);
2780 registers = Bits32 (opcode, 7, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002781 registers = registers & 0x00ff; // Make sure the top 8 bits are zeros.
Caroline Ticefa172202011-02-11 22:49:54 +00002782 wback = true;
2783
2784 // if BitCount(registers) < 1 then UNPREDICTABLE;
2785 if (BitCount (registers) < 1)
2786 return false;
2787
2788 break;
2789
2790 case eEncodingT2:
2791 // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’);
2792 n = Bits32 (opcode, 19, 16);
2793 registers = Bits32 (opcode, 15, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002794 registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros.
Caroline Ticefa172202011-02-11 22:49:54 +00002795 wback = BitIsSet (opcode, 21);
2796
2797 // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE;
2798 if ((n == 15) || (BitCount (registers) < 2))
2799 return false;
2800
2801 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
2802 if (wback && BitIsSet (registers, n))
2803 return false;
2804
2805 break;
2806
2807 case eEncodingA1:
2808 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2809 n = Bits32 (opcode, 19, 16);
2810 registers = Bits32 (opcode, 15, 0);
2811 wback = BitIsSet (opcode, 21);
2812
2813 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2814 if ((n == 15) || (BitCount (registers) < 1))
2815 return false;
2816
2817 break;
2818
2819 default:
2820 return false;
2821 }
2822
2823 // address = R[n];
2824 int32_t offset = 0;
2825 const addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2826 if (!success)
2827 return false;
2828
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002829 EmulateInstruction::Context context;
2830 context.type = EmulateInstruction::eContextRegisterStore;
2831 Register base_reg;
2832 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
Caroline Ticefa172202011-02-11 22:49:54 +00002833
2834 // for i = 0 to 14
2835 for (int i = 0; i < 14; ++i)
2836 {
2837 int lowest_set_bit = 14;
2838 // if registers<i> == ’1’ then
2839 if (BitIsSet (registers, i))
2840 {
2841 if (i < lowest_set_bit)
2842 lowest_set_bit = i;
2843 // if i == n && wback && i != LowestSetBit(registers) then
2844 if ((i == n) && wback && (i != lowest_set_bit))
2845 // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1
2846 WriteBits32UnknownToMemory (address + offset);
2847 else
2848 {
2849 // MemA[address,4] = R[i];
2850 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
2851 if (!success)
2852 return false;
2853
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002854 Register data_reg;
2855 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
2856 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002857 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
2858 return false;
2859 }
2860
2861 // address = address + 4;
2862 offset += addr_byte_size;
2863 }
2864 }
2865
2866 // if registers<15> == ’1’ then // Only possible for encoding A1
2867 // MemA[address,4] = PCStoreValue();
2868 if (BitIsSet (registers, 15))
2869 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002870 Register pc_reg;
2871 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
2872 context.SetRegisterPlusOffset (pc_reg, 8);
Caroline Ticefa172202011-02-11 22:49:54 +00002873 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
2874 if (!success)
2875 return false;
2876
2877 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
2878 return false;
2879 }
2880
2881 // if wback then R[n] = R[n] + 4*BitCount(registers);
2882 if (wback)
2883 {
2884 offset = addr_byte_size * BitCount (registers);
2885 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002886 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002887 addr_t data = address + offset;
2888 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
2889 return false;
2890 }
2891 }
2892 return true;
2893}
2894
Caroline Tice1511f502011-02-15 00:19:42 +00002895// STMDA stores multiple registers to consecutive memory locations using an address from a base register. The
2896// consecutive memory locations end at this address, and the address just below the lowest of those locations can
2897// optionally be written back to the base register.
2898bool
2899EmulateInstructionARM::EmulateSTMDA (ARMEncoding encoding)
2900{
2901#if 0
2902 if ConditionPassed() then
2903 EncodingSpecificOperations();
2904 address = R[n] - 4*BitCount(registers) + 4;
2905
2906 for i = 0 to 14
2907 if registers<i> == ’1’ then
2908 if i == n && wback && i != LowestSetBit(registers) then
2909 MemA[address,4] = bits(32) UNKNOWN;
2910 else
2911 MemA[address,4] = R[i];
2912 address = address + 4;
2913
2914 if registers<15> == ’1’ then
2915 MemA[address,4] = PCStoreValue();
2916
2917 if wback then R[n] = R[n] - 4*BitCount(registers);
2918#endif
2919
2920 bool success = false;
2921 const uint32_t opcode = OpcodeAsUnsigned (&success);
2922 if (!success)
2923 return false;
2924
2925 if (ConditionPassed ())
2926 {
2927 uint32_t n;
2928 uint32_t registers = 0;
2929 bool wback;
2930 const uint32_t addr_byte_size = GetAddressByteSize();
2931
2932 // EncodingSpecificOperations();
2933 switch (encoding)
2934 {
2935 case eEncodingA1:
2936 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2937 n = Bits32 (opcode, 19, 16);
2938 registers = Bits32 (opcode, 15, 0);
2939 wback = BitIsSet (opcode, 21);
2940
2941 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2942 if ((n == 15) || (BitCount (registers) < 1))
2943 return false;
2944 break;
2945 default:
2946 return false;
2947 }
2948
2949 // address = R[n] - 4*BitCount(registers) + 4;
2950 int32_t offset = 0;
2951 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2952 if (!success)
2953 return false;
2954
2955 address = address - (addr_byte_size * BitCount (registers)) + 4;
2956
2957 EmulateInstruction::Context context;
2958 context.type = EmulateInstruction::eContextRegisterStore;
2959 Register base_reg;
2960 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2961
2962 // for i = 0 to 14
2963 for (int i = 0; i < 14; ++i)
2964 {
2965 int lowest_bit_set = 14;
2966 // if registers<i> == ’1’ then
2967 if (BitIsSet (registers, i))
2968 {
2969 if (i < lowest_bit_set)
2970 lowest_bit_set = i;
2971 //if i == n && wback && i != LowestSetBit(registers) then
2972 if ((i == n) && wback && (i != lowest_bit_set))
2973 // MemA[address,4] = bits(32) UNKNOWN;
2974 WriteBits32UnknownToMemory (address + offset);
2975 else
2976 {
2977 // MemA[address,4] = R[i];
2978 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
2979 if (!success)
2980 return false;
2981
2982 Register data_reg;
2983 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
2984 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
2985 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
2986 return false;
2987 }
2988
2989 // address = address + 4;
2990 offset += addr_byte_size;
2991 }
2992 }
2993
2994 // if registers<15> == ’1’ then
2995 // MemA[address,4] = PCStoreValue();
2996 if (BitIsSet (registers, 15))
2997 {
2998 Register pc_reg;
2999 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
3000 context.SetRegisterPlusOffset (pc_reg, 8);
3001 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
3002 if (!success)
3003 return false;
3004
3005 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
3006 return false;
3007 }
3008
3009 // if wback then R[n] = R[n] - 4*BitCount(registers);
3010 if (wback)
3011 {
3012 offset = addr_byte_size * BitCount (registers);
3013 context.type = EmulateInstruction::eContextAdjustBaseRegister;
3014 context.SetImmediateSigned (offset);
3015 addr_t data = address + offset;
3016 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
3017 return false;
3018 }
3019 }
3020 return true;
3021}
3022
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003023// STMDB stores multiple registers to consecutive memory locations using an address from a base register. The
3024// consecutive memory locations end just below this address, and the address of the first of those locations can
3025// optionally be written back to the base register.
3026bool
3027EmulateInstructionARM::EmulateSTMDB (ARMEncoding encoding)
3028{
3029#if 0
3030 if ConditionPassed() then
3031 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
3032 address = R[n] - 4*BitCount(registers);
3033
3034 for i = 0 to 14
3035 if registers<i> == ’1’ then
3036 if i == n && wback && i != LowestSetBit(registers) then
3037 MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1
3038 else
3039 MemA[address,4] = R[i];
3040 address = address + 4;
3041
3042 if registers<15> == ’1’ then // Only possible for encoding A1
3043 MemA[address,4] = PCStoreValue();
3044
3045 if wback then R[n] = R[n] - 4*BitCount(registers);
3046#endif
3047
3048
3049 bool success = false;
3050 const uint32_t opcode = OpcodeAsUnsigned (&success);
3051 if (!success)
3052 return false;
3053
3054 if (ConditionPassed ())
3055 {
3056 uint32_t n;
3057 uint32_t registers = 0;
3058 bool wback;
3059 const uint32_t addr_byte_size = GetAddressByteSize();
3060
3061 // EncodingSpecificOperations(); NullCheckIfThumbEE(n);
3062 switch (encoding)
3063 {
3064 case eEncodingT1:
3065 // if W == ’1’ && Rn == ’1101’ then SEE PUSH;
3066 if ((BitIsSet (opcode, 21)) && (Bits32 (opcode, 19, 16) == 13))
3067 {
3068 // See PUSH
3069 }
3070 // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’);
3071 n = Bits32 (opcode, 19, 16);
3072 registers = Bits32 (opcode, 15, 0);
3073 registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros.
3074 wback = BitIsSet (opcode, 21);
3075 // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE;
3076 if ((n == 15) || BitCount (registers) < 2)
3077 return false;
3078 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
3079 if (wback && BitIsSet (registers, n))
3080 return false;
3081 break;
3082
3083 case eEncodingA1:
3084 // if W == ’1’ && Rn == ’1101’ && BitCount(register_list) >= 2 then SEE PUSH;
3085 if (BitIsSet (opcode, 21) && (Bits32 (opcode, 19, 16) == 13) && BitCount (Bits32 (opcode, 15, 0)) >= 2)
3086 {
3087 // See Push
3088 }
3089 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
3090 n = Bits32 (opcode, 19, 16);
3091 registers = Bits32 (opcode, 15, 0);
3092 wback = BitIsSet (opcode, 21);
3093 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
3094 if ((n == 15) || BitCount (registers) < 1)
3095 return false;
3096 break;
3097
3098 default:
3099 return false;
3100 }
3101
3102 // address = R[n] - 4*BitCount(registers);
3103
3104 int32_t offset = 0;
3105 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
3106 if (!success)
3107 return false;
3108
3109 address = address - (addr_byte_size * BitCount (registers));
3110
3111 EmulateInstruction::Context context;
3112 context.type = EmulateInstruction::eContextRegisterStore;
3113 Register base_reg;
3114 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
3115
3116 // for i = 0 to 14
3117 for (int i = 0; i < 14; ++i)
3118 {
3119 uint32_t lowest_set_bit = 14;
3120 // if registers<i> == ’1’ then
3121 if (BitIsSet (registers, i))
3122 {
3123 if (i < lowest_set_bit)
3124 lowest_set_bit = i;
3125 // if i == n && wback && i != LowestSetBit(registers) then
3126 if ((i == n) && wback && (i != lowest_set_bit))
3127 // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1
3128 WriteBits32UnknownToMemory (address + offset);
3129 else
3130 {
3131 // MemA[address,4] = R[i];
3132 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
3133 if (!success)
3134 return false;
3135
3136 Register data_reg;
3137 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
3138 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
3139 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
3140 return false;
3141 }
3142
3143 // address = address + 4;
3144 offset += addr_byte_size;
3145 }
3146 }
3147
3148 // if registers<15> == ’1’ then // Only possible for encoding A1
3149 // MemA[address,4] = PCStoreValue();
3150 if (BitIsSet (registers, 15))
3151 {
3152 Register pc_reg;
3153 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
3154 context.SetRegisterPlusOffset (pc_reg, 8);
3155 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
3156 if (!success)
3157 return false;
3158
3159 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
3160 return false;
3161 }
3162
3163 // if wback then R[n] = R[n] - 4*BitCount(registers);
3164 if (wback)
3165 {
3166 offset = addr_byte_size * BitCount (registers);
3167 context.type = EmulateInstruction::eContextAdjustBaseRegister;
3168 context.SetImmediateSigned (offset);
3169 addr_t data = address + offset;
3170 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
3171 return false;
3172 }
3173 }
3174 return true;
3175}
3176
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003177EmulateInstructionARM::ARMOpcode*
3178EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode)
Greg Clayton64c84432011-01-21 22:02:52 +00003179{
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003180 static ARMOpcode
3181 g_arm_opcodes[] =
3182 {
3183 //----------------------------------------------------------------------
3184 // Prologue instructions
3185 //----------------------------------------------------------------------
Johnny Chenfdd179e2011-01-31 20:09:28 +00003186
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003187 // push register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00003188 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePush, "push <registers>" },
3189 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePush, "push <register>" },
Johnny Chenbcec3af2011-01-27 01:26:19 +00003190
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003191 // set r7 to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00003192 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #<const>" },
3193 { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubR7IPImmediate, "sub r7, ip, #<const>"},
Johnny Chene7cf4202011-02-10 18:13:23 +00003194 // copy the stack pointer to ip
Johnny Chenc28a76d2011-02-01 18:51:48 +00003195 { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMovRdSP, "mov ip, sp" },
3196 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add ip, sp, #<const>" },
3197 { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubIPSPImmediate, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00003198
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003199 // adjust the stack pointer
Johnny Chenc28a76d2011-02-01 18:51:48 +00003200 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +00003201
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003202 // push one register
3203 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chenc28a76d2011-02-01 18:51:48 +00003204 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +00003205
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003206 // vector push consecutive extension register(s)
Johnny Chen9b8d7832011-02-02 01:13:56 +00003207 { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
3208 { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenef85e912011-01-31 23:07:40 +00003209
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003210 //----------------------------------------------------------------------
Johnny Chen587a0a42011-02-01 18:35:28 +00003211 // Epilogue instructions
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003212 //----------------------------------------------------------------------
Johnny Chenef85e912011-01-31 23:07:40 +00003213
Johnny Chenc28a76d2011-02-01 18:51:48 +00003214 { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePop, "pop <registers>"},
3215 { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePop, "pop <register>"},
Johnny Chen9b8d7832011-02-02 01:13:56 +00003216 { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"},
Johnny Chenb77be412011-02-04 00:40:18 +00003217 { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"},
3218
3219 //----------------------------------------------------------------------
3220 // Supervisor Call (previously Software Interrupt)
3221 //----------------------------------------------------------------------
Johnny Chen3b620b32011-02-07 20:11:47 +00003222 { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"},
3223
3224 //----------------------------------------------------------------------
3225 // Branch instructions
3226 //----------------------------------------------------------------------
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003227 { 0x0f000000, 0x0a000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "b #imm24"},
Johnny Chen383d6292011-02-11 21:23:32 +00003228 // To resolve ambiguity, "blx <label>" should come before "bl <label>".
3229 { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"},
3230 { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"},
3231 { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"},
Johnny Chenab3b3512011-02-12 00:10:51 +00003232 // for example, "bx lr"
3233 { 0x0ffffff0, 0x012fff10, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"},
Johnny Chenb77be412011-02-04 00:40:18 +00003234
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003235 //----------------------------------------------------------------------
Johnny Chen28070c32011-02-12 01:27:26 +00003236 // Data-processing instructions
3237 //----------------------------------------------------------------------
3238 // move bitwise not
3239 { 0x0fef0000, 0x03e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMvnRdImm, "mvn{s} <Rd>, #<const>"},
3240
3241 //----------------------------------------------------------------------
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003242 // Load instructions
3243 //----------------------------------------------------------------------
Caroline Tice0b29e242011-02-08 23:16:02 +00003244 { 0x0fd00000, 0x08900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" },
Caroline Tice713c2662011-02-11 17:59:55 +00003245 { 0x0fd00000, 0x08100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDA, "ldmda<c> <Rn>{!} <registers>" },
Caroline Tice85aab332011-02-08 23:56:10 +00003246 { 0x0fd00000, 0x09100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" },
Caroline Ticefa172202011-02-11 22:49:54 +00003247 { 0x0fd00000, 0x09900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMIB, "ldmib<c> <Rn<{!} <registers>" },
3248
3249 //----------------------------------------------------------------------
3250 // Store instructions
3251 //----------------------------------------------------------------------
Caroline Tice1511f502011-02-15 00:19:42 +00003252 { 0x0fd00000, 0x08800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" },
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003253 { 0x0fd00000, 0x08000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDA, "stmda<c> <Rn>{!} <registers>" },
3254 { 0x0fd00000, 0x09000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }
Caroline Tice1511f502011-02-15 00:19:42 +00003255
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003256
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003257 };
3258 static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
3259
3260 for (size_t i=0; i<k_num_arm_opcodes; ++i)
3261 {
3262 if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value)
3263 return &g_arm_opcodes[i];
3264 }
3265 return NULL;
3266}
Greg Clayton64c84432011-01-21 22:02:52 +00003267
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003268
3269EmulateInstructionARM::ARMOpcode*
3270EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode)
Johnny Chen347320d2011-01-24 23:40:59 +00003271{
Johnny Chenfdd179e2011-01-31 20:09:28 +00003272
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003273 static ARMOpcode
3274 g_thumb_opcodes[] =
3275 {
3276 //----------------------------------------------------------------------
3277 // Prologue instructions
3278 //----------------------------------------------------------------------
Johnny Chenbcec3af2011-01-27 01:26:19 +00003279
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003280 // push register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00003281 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePush, "push <registers>" },
Johnny Chend6c13f02011-02-08 20:36:34 +00003282 { 0xffff0000, 0xe92d0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <registers>" },
3283 { 0xffff0fff, 0xf84d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <register>" },
Johnny Chen788e0552011-01-27 22:52:23 +00003284
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003285 // set r7 to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00003286 { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #imm" },
Johnny Chene7cf4202011-02-10 18:13:23 +00003287 // copy the stack pointer to r7
Johnny Chenc28a76d2011-02-01 18:51:48 +00003288 { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdSP, "mov r7, sp" },
Johnny Chene7cf4202011-02-10 18:13:23 +00003289 // move from high register to low register (comes after "mov r7, sp" to resolve ambiguity)
3290 { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovLowHigh, "mov r0-r7, r8-r15" },
Johnny Chen60c0d622011-01-25 23:49:39 +00003291
Johnny Chenc9de9102011-02-11 19:12:30 +00003292 // PC-relative load into register (see also EmulateAddSPRm)
3293 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr <Rt>, [PC, #imm]"},
Johnny Chen799dfd02011-01-26 23:14:33 +00003294
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003295 // adjust the stack pointer
Johnny Chenc28a76d2011-02-01 18:51:48 +00003296 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPRm, "add sp, <Rm>"},
3297 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSubSPImmdiate, "add sp, sp, #imm"},
Johnny Chend6c13f02011-02-08 20:36:34 +00003298 { 0xfbef8f00, 0xf1ad0d00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub.w sp, sp, #<const>"},
3299 { 0xfbff8f00, 0xf2ad0d00, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "subw sp, sp, #imm12"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00003300
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003301 // vector push consecutive extension register(s)
Johnny Chend6c13f02011-02-08 20:36:34 +00003302 { 0xffbf0f00, 0xed2d0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
3303 { 0xffbf0f00, 0xed2d0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00003304
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003305 //----------------------------------------------------------------------
3306 // Epilogue instructions
3307 //----------------------------------------------------------------------
Johnny Chen347320d2011-01-24 23:40:59 +00003308
Johnny Chenc28a76d2011-02-01 18:51:48 +00003309 { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPImmediate, "add sp, #imm"},
3310 { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePop, "pop <registers>"},
Johnny Chend6c13f02011-02-08 20:36:34 +00003311 { 0xffff0000, 0xe8bd0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <registers>" },
3312 { 0xffff0fff, 0xf85d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <register>" },
3313 { 0xffbf0f00, 0xecbd0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"},
3314 { 0xffbf0f00, 0xecbd0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"},
Johnny Chenb77be412011-02-04 00:40:18 +00003315
3316 //----------------------------------------------------------------------
3317 // Supervisor Call (previously Software Interrupt)
3318 //----------------------------------------------------------------------
Johnny Chenc315f862011-02-05 00:46:10 +00003319 { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"},
3320
3321 //----------------------------------------------------------------------
3322 // If Then makes up to four following instructions conditional.
3323 //----------------------------------------------------------------------
Johnny Chen3b620b32011-02-07 20:11:47 +00003324 { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"},
3325
3326 //----------------------------------------------------------------------
3327 // Branch instructions
3328 //----------------------------------------------------------------------
3329 // To resolve ambiguity, "b<c> #imm8" should come after "svc #imm8".
3330 { 0xfffff000, 0x0000d000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm8 (outside IT)"},
3331 { 0xffff8000, 0x0000e000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateB, "b #imm11 (outside or last in IT)"},
Johnny Chen9ee056b2011-02-08 00:06:35 +00003332 { 0xf800d000, 0xf0008000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside IT)"},
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003333 { 0xf800d000, 0xf0009000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateB, "b.w #imm8 (outside or last in IT)"},
Johnny Chen383d6292011-02-11 21:23:32 +00003334 // J1 == J2 == 1
3335 { 0xf800f800, 0xf000f800, ARMV4T_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"},
3336 // J1 == J2 == 1
3337 { 0xf800e800, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"},
3338 { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"},
Johnny Chenab3b3512011-02-12 00:10:51 +00003339 // for example, "bx lr"
3340 { 0xffffff87, 0x00004700, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"},
Johnny Chen53ebab72011-02-08 23:21:57 +00003341 // compare and branch
3342 { 0xfffff500, 0x0000b100, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCB, "cb{n}z <Rn>, <label>"},
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003343
3344 //----------------------------------------------------------------------
Johnny Chen26863dc2011-02-09 23:43:29 +00003345 // Data-processing instructions
3346 //----------------------------------------------------------------------
3347 // Make sure "add sp, <Rm>" comes before this instruction, so there's no ambiguity decoding the two.
3348 { 0xffffff00, 0x00004400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdnRm, "add <Rdn>, <Rm>"},
Johnny Chen338bf542011-02-10 19:29:03 +00003349 // move from high register to high register
3350 { 0xffffff00, 0x00004600, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdRm, "mov<c> <Rd>, <Rm>"},
3351 // move from low register to low register
3352 { 0xffffffc0, 0x00000000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateMovRdRm, "movs <Rd>, <Rm>"},
Johnny Chen357c30f2011-02-14 22:04:25 +00003353 // move immediate
3354 { 0xfffff800, 0x00002000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdImm, "movs|mov<c> <Rd>, #imm8"},
3355 { 0xfbef8000, 0xf04f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateMovRdImm, "mov{s}<c>.w <Rd>, #<const>"},
Johnny Chen28070c32011-02-12 01:27:26 +00003356 // move bitwise not
3357 { 0xfbef8000, 0xf06f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateMvnRdImm, "mvn{s} <Rd>, #<const>"},
Johnny Chend4dc4442011-02-11 02:02:56 +00003358 // compare a register with immediate
3359 { 0xfffff800, 0x00002800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCmpRnImm, "cmp<c> <Rn>, #imm8"},
Johnny Chene4a4d302011-02-11 21:53:58 +00003360 // compare Rn with Rm (Rn and Rm both from r0-r7)
3361 { 0xffffffc0, 0x00004280, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCmpRnRm, "cmp<c> <Rn>, <Rm>"},
3362 // compare Rn with Rm (Rn and Rm not both from r0-r7)
3363 { 0xffffff00, 0x00004500, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateCmpRnRm, "cmp<c> <Rn>, <Rm>"},
Johnny Chen26863dc2011-02-09 23:43:29 +00003364
3365 //----------------------------------------------------------------------
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003366 // Load instructions
3367 //----------------------------------------------------------------------
3368 { 0xfffff800, 0x0000c800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" },
Caroline Tice0b29e242011-02-08 23:16:02 +00003369 { 0xffd02000, 0xe8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c>.w <Rn>{!} <registers>" },
Johnny Chenef21b592011-02-10 01:52:38 +00003370 { 0xffd00000, 0xe9100000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" },
Johnny Chenc9de9102011-02-11 19:12:30 +00003371 { 0xfffff800, 0x00006800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtRnImm, "ldr<c> <Rt>, [<Rn>{,#imm}]"},
3372 // Thumb2 PC-relative load into register
Caroline Ticefa172202011-02-11 22:49:54 +00003373 { 0xff7f0000, 0xf85f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr<c>.w <Rt>, [PC, +/-#imm}]"},
3374
3375 //----------------------------------------------------------------------
3376 // Store instructions
3377 //----------------------------------------------------------------------
3378 { 0xfffff800, 0x0000c000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" },
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003379 { 0xffd00000, 0xe8800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c>.w <Rn>{!} <registers>" },
3380 { 0xffd00000, 0xe9000000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003381
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003382 };
3383
3384 const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
3385 for (size_t i=0; i<k_num_thumb_opcodes; ++i)
3386 {
3387 if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value)
3388 return &g_thumb_opcodes[i];
3389 }
3390 return NULL;
3391}
Greg Clayton64c84432011-01-21 22:02:52 +00003392
Greg Clayton31e2a382011-01-30 20:03:56 +00003393bool
3394EmulateInstructionARM::SetTargetTriple (const ConstString &triple)
3395{
3396 m_arm_isa = 0;
3397 const char *triple_cstr = triple.GetCString();
3398 if (triple_cstr)
3399 {
3400 const char *dash = ::strchr (triple_cstr, '-');
3401 if (dash)
3402 {
3403 std::string arch (triple_cstr, dash);
3404 const char *arch_cstr = arch.c_str();
3405 if (strcasecmp(arch_cstr, "armv4t") == 0)
3406 m_arm_isa = ARMv4T;
3407 else if (strcasecmp(arch_cstr, "armv4") == 0)
3408 m_arm_isa = ARMv4;
3409 else if (strcasecmp(arch_cstr, "armv5tej") == 0)
3410 m_arm_isa = ARMv5TEJ;
3411 else if (strcasecmp(arch_cstr, "armv5te") == 0)
3412 m_arm_isa = ARMv5TE;
3413 else if (strcasecmp(arch_cstr, "armv5t") == 0)
3414 m_arm_isa = ARMv5T;
3415 else if (strcasecmp(arch_cstr, "armv6k") == 0)
3416 m_arm_isa = ARMv6K;
3417 else if (strcasecmp(arch_cstr, "armv6") == 0)
3418 m_arm_isa = ARMv6;
3419 else if (strcasecmp(arch_cstr, "armv6t2") == 0)
3420 m_arm_isa = ARMv6T2;
3421 else if (strcasecmp(arch_cstr, "armv7") == 0)
3422 m_arm_isa = ARMv7;
3423 else if (strcasecmp(arch_cstr, "armv8") == 0)
3424 m_arm_isa = ARMv8;
3425 }
3426 }
3427 return m_arm_isa != 0;
3428}
3429
3430
Greg Clayton64c84432011-01-21 22:02:52 +00003431bool
3432EmulateInstructionARM::ReadInstruction ()
3433{
3434 bool success = false;
3435 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
3436 if (success)
3437 {
3438 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
3439 if (success)
3440 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003441 Context read_inst_context;
3442 read_inst_context.type = eContextReadOpcode;
3443 read_inst_context.SetNoArgs ();
3444
Greg Clayton64c84432011-01-21 22:02:52 +00003445 if (m_inst_cpsr & MASK_CPSR_T)
3446 {
3447 m_inst_mode = eModeThumb;
3448 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
3449
3450 if (success)
3451 {
3452 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
3453 {
3454 m_inst.opcode_type = eOpcode16;
3455 m_inst.opcode.inst16 = thumb_opcode;
3456 }
3457 else
3458 {
3459 m_inst.opcode_type = eOpcode32;
3460 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
3461 }
3462 }
3463 }
3464 else
3465 {
3466 m_inst_mode = eModeARM;
3467 m_inst.opcode_type = eOpcode32;
3468 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
3469 }
3470 }
3471 }
3472 if (!success)
3473 {
3474 m_inst_mode = eModeInvalid;
3475 m_inst_pc = LLDB_INVALID_ADDRESS;
3476 }
3477 return success;
3478}
3479
Johnny Chenee9b1f72011-02-09 01:00:31 +00003480uint32_t
3481EmulateInstructionARM::ArchVersion ()
3482{
3483 return m_arm_isa;
3484}
3485
Greg Clayton64c84432011-01-21 22:02:52 +00003486bool
3487EmulateInstructionARM::ConditionPassed ()
3488{
3489 if (m_inst_cpsr == 0)
3490 return false;
3491
3492 const uint32_t cond = CurrentCond ();
3493
3494 if (cond == UINT32_MAX)
3495 return false;
3496
3497 bool result = false;
3498 switch (UnsignedBits(cond, 3, 1))
3499 {
3500 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
3501 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
3502 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
3503 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
3504 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
3505 case 5:
3506 {
3507 bool n = (m_inst_cpsr & MASK_CPSR_N);
3508 bool v = (m_inst_cpsr & MASK_CPSR_V);
3509 result = n == v;
3510 }
3511 break;
3512 case 6:
3513 {
3514 bool n = (m_inst_cpsr & MASK_CPSR_N);
3515 bool v = (m_inst_cpsr & MASK_CPSR_V);
3516 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
3517 }
3518 break;
3519 case 7:
3520 result = true;
3521 break;
3522 }
3523
3524 if (cond & 1)
3525 result = !result;
3526 return result;
3527}
3528
Johnny Chen9ee056b2011-02-08 00:06:35 +00003529uint32_t
3530EmulateInstructionARM::CurrentCond ()
3531{
3532 switch (m_inst_mode)
3533 {
3534 default:
3535 case eModeInvalid:
3536 break;
3537
3538 case eModeARM:
3539 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
3540
3541 case eModeThumb:
3542 // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit
3543 // 'cond' field of the encoding.
3544 if (m_inst.opcode_type == eOpcode16 &&
3545 Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d &&
3546 Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f)
3547 {
3548 return Bits32(m_inst.opcode.inst16, 11, 7);
3549 }
3550 else if (m_inst.opcode_type == eOpcode32 &&
3551 Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e &&
3552 Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 &&
3553 Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 &&
3554 Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d)
3555 {
3556 return Bits32(m_inst.opcode.inst32, 25, 22);
3557 }
3558
3559 return m_it_session.GetCond();
3560 }
3561 return UINT32_MAX; // Return invalid value
3562}
3563
Johnny Chen9ee056b2011-02-08 00:06:35 +00003564bool
Johnny Chen098ae2d2011-02-12 00:50:05 +00003565EmulateInstructionARM::InITBlock()
3566{
3567 return CurrentInstrSet() == eModeThumb && m_it_session.InITBlock();
3568}
3569
3570bool
3571EmulateInstructionARM::LastInITBlock()
3572{
3573 return CurrentInstrSet() == eModeThumb && m_it_session.LastInITBlock();
3574}
3575
3576bool
Johnny Chen9ee056b2011-02-08 00:06:35 +00003577EmulateInstructionARM::BranchWritePC (const Context &context, uint32_t addr)
3578{
3579 addr_t target;
3580
Johnny Chenee9b1f72011-02-09 01:00:31 +00003581 // Check the current instruction set.
3582 if (CurrentInstrSet() == eModeARM)
Johnny Chen9ee056b2011-02-08 00:06:35 +00003583 target = addr & 0xfffffffc;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003584 else
Johnny Chen9ee056b2011-02-08 00:06:35 +00003585 target = addr & 0xfffffffe;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003586
Johnny Chen9ee056b2011-02-08 00:06:35 +00003587 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target))
Johnny Chen53ebab72011-02-08 23:21:57 +00003588 return false;
3589
3590 return true;
Johnny Chen9ee056b2011-02-08 00:06:35 +00003591}
3592
3593// As a side effect, BXWritePC sets context.arg2 to eModeARM or eModeThumb by inspecting addr.
3594bool
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003595EmulateInstructionARM::BXWritePC (Context &context, uint32_t addr, Register &reg)
Johnny Chen9ee056b2011-02-08 00:06:35 +00003596{
3597 addr_t target;
Johnny Chen0f309db2011-02-09 19:11:32 +00003598 // If the CPSR is changed due to switching between ARM and Thumb ISETSTATE,
3599 // we want to record it and issue a WriteRegister callback so the clients
3600 // can track the mode changes accordingly.
3601 bool cpsr_changed = false;
Johnny Chen9ee056b2011-02-08 00:06:35 +00003602
3603 if (BitIsSet(addr, 0))
3604 {
Johnny Chen0f309db2011-02-09 19:11:32 +00003605 if (CurrentInstrSet() != eModeThumb)
3606 {
3607 SelectInstrSet(eModeThumb);
3608 cpsr_changed = true;
3609 }
Johnny Chen9ee056b2011-02-08 00:06:35 +00003610 target = addr & 0xfffffffe;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003611 context.SetModeAndRegister (eModeThumb, reg);
Johnny Chen9ee056b2011-02-08 00:06:35 +00003612 }
3613 else if (BitIsClear(addr, 1))
3614 {
Johnny Chen0f309db2011-02-09 19:11:32 +00003615 if (CurrentInstrSet() != eModeARM)
3616 {
3617 SelectInstrSet(eModeARM);
3618 cpsr_changed = true;
3619 }
Johnny Chen9ee056b2011-02-08 00:06:35 +00003620 target = addr & 0xfffffffc;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003621 context.SetModeAndRegister (eModeARM, reg);
Johnny Chen9ee056b2011-02-08 00:06:35 +00003622 }
3623 else
3624 return false; // address<1:0> == '10' => UNPREDICTABLE
3625
Johnny Chen0f309db2011-02-09 19:11:32 +00003626 if (cpsr_changed)
3627 {
Johnny Chen558133b2011-02-09 23:59:17 +00003628 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
Johnny Chen0f309db2011-02-09 19:11:32 +00003629 return false;
3630 }
Johnny Chen9ee056b2011-02-08 00:06:35 +00003631 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target))
Johnny Chen53ebab72011-02-08 23:21:57 +00003632 return false;
3633
3634 return true;
Johnny Chen9ee056b2011-02-08 00:06:35 +00003635}
Greg Clayton64c84432011-01-21 22:02:52 +00003636
Johnny Chenee9b1f72011-02-09 01:00:31 +00003637// Dispatches to either BXWritePC or BranchWritePC based on architecture versions.
3638bool
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003639EmulateInstructionARM::LoadWritePC (Context &context, uint32_t addr, Register &reg)
Johnny Chenee9b1f72011-02-09 01:00:31 +00003640{
3641 if (ArchVersion() >= ARMv5T)
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003642 return BXWritePC(context, addr, reg);
Johnny Chenee9b1f72011-02-09 01:00:31 +00003643 else
3644 return BranchWritePC((const Context)context, addr);
3645}
3646
Johnny Chen26863dc2011-02-09 23:43:29 +00003647// Dispatches to either BXWritePC or BranchWritePC based on architecture versions and current instruction set.
3648bool
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003649EmulateInstructionARM::ALUWritePC (Context &context, uint32_t addr, Register &reg)
Johnny Chen26863dc2011-02-09 23:43:29 +00003650{
3651 if (ArchVersion() >= ARMv7 && CurrentInstrSet() == eModeARM)
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003652 return BXWritePC(context, addr, reg);
Johnny Chen26863dc2011-02-09 23:43:29 +00003653 else
3654 return BranchWritePC((const Context)context, addr);
3655}
3656
Johnny Chenee9b1f72011-02-09 01:00:31 +00003657EmulateInstructionARM::Mode
3658EmulateInstructionARM::CurrentInstrSet ()
3659{
3660 return m_inst_mode;
3661}
3662
3663// Set the 'T' bit of our CPSR. The m_inst_mode gets updated when the next
Johnny Chen558133b2011-02-09 23:59:17 +00003664// ReadInstruction() is performed. This function has a side effect of updating
3665// the m_new_inst_cpsr member variable if necessary.
Johnny Chenee9b1f72011-02-09 01:00:31 +00003666bool
3667EmulateInstructionARM::SelectInstrSet (Mode arm_or_thumb)
3668{
Johnny Chen558133b2011-02-09 23:59:17 +00003669 m_new_inst_cpsr = m_inst_cpsr;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003670 switch (arm_or_thumb)
3671 {
3672 default:
3673 return false;
3674 eModeARM:
3675 // Clear the T bit.
Johnny Chen558133b2011-02-09 23:59:17 +00003676 m_new_inst_cpsr &= ~MASK_CPSR_T;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003677 break;
3678 eModeThumb:
3679 // Set the T bit.
Johnny Chen558133b2011-02-09 23:59:17 +00003680 m_new_inst_cpsr |= MASK_CPSR_T;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003681 break;
3682 }
3683 return true;
3684}
3685
Johnny Chenef21b592011-02-10 01:52:38 +00003686// This function returns TRUE if the processor currently provides support for
3687// unaligned memory accesses, or FALSE otherwise. This is always TRUE in ARMv7,
3688// controllable by the SCTLR.U bit in ARMv6, and always FALSE before ARMv6.
3689bool
3690EmulateInstructionARM::UnalignedSupport()
3691{
3692 return (ArchVersion() >= ARMv7);
3693}
3694
Johnny Chenbf6ad172011-02-11 01:29:53 +00003695// The main addition and subtraction instructions can produce status information
3696// about both unsigned carry and signed overflow conditions. This status
3697// information can be used to synthesize multi-word additions and subtractions.
3698EmulateInstructionARM::AddWithCarryResult
3699EmulateInstructionARM::AddWithCarry (uint32_t x, uint32_t y, uint8_t carry_in)
3700{
3701 uint32_t result;
3702 uint8_t carry_out;
3703 uint8_t overflow;
3704
3705 uint64_t unsigned_sum = x + y + carry_in;
3706 int64_t signed_sum = (int32_t)x + (int32_t)y + (int32_t)carry_in;
3707
3708 result = UnsignedBits(unsigned_sum, 31, 0);
3709 carry_out = (result == unsigned_sum ? 0 : 1);
3710 overflow = ((int32_t)result == signed_sum ? 0 : 1);
3711
3712 AddWithCarryResult res = { result, carry_out, overflow };
3713 return res;
3714}
3715
Greg Clayton64c84432011-01-21 22:02:52 +00003716bool
3717EmulateInstructionARM::EvaluateInstruction ()
3718{
Johnny Chenc315f862011-02-05 00:46:10 +00003719 // Advance the ITSTATE bits to their values for the next instruction.
3720 if (m_inst_mode == eModeThumb && m_it_session.InITBlock())
3721 m_it_session.ITAdvance();
3722
Greg Clayton64c84432011-01-21 22:02:52 +00003723 return false;
3724}