blob: 1797bc152945fa3bb8aab2daf8ea822e10ba1900 [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 Clayton395fc332011-02-15 21:59:32 +000013#include "lldb/Core/ArchSpec.h"
Greg Clayton8482ded2011-02-01 00:04:43 +000014#include "lldb/Core/ConstString.h"
15
Greg Claytonf29a08f2011-02-09 17:41:27 +000016#include "Plugins/Process/Utility/ARMDefines.h"
17#include "Plugins/Process/Utility/ARMUtils.h"
18#include "Utility/ARM_DWARF_Registers.h"
19
Johnny Chen9b8d7832011-02-02 01:13:56 +000020#include "llvm/Support/MathExtras.h" // for SignExtend32 template function
Johnny Chen93070472011-02-04 23:02:47 +000021 // and CountTrailingZeros_32 function
Greg Clayton64c84432011-01-21 22:02:52 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Johnny Chend6c13f02011-02-08 20:36:34 +000026static inline uint32_t Align(uint32_t val, uint32_t alignment)
27{
28 return alignment * (val / alignment);
29}
30
Johnny Chen0e00af22011-02-10 19:40:42 +000031//----------------------------------------------------------------------
32//
33// ITSession implementation
34//
35//----------------------------------------------------------------------
36
Johnny Chen93070472011-02-04 23:02:47 +000037// A8.6.50
38// Valid return values are {1, 2, 3, 4}, with 0 signifying an error condition.
39static unsigned short CountITSize(unsigned ITMask) {
40 // First count the trailing zeros of the IT mask.
41 unsigned TZ = llvm::CountTrailingZeros_32(ITMask);
42 if (TZ > 3)
43 {
44 printf("Encoding error: IT Mask '0000'\n");
45 return 0;
46 }
47 return (4 - TZ);
48}
49
50// Init ITState. Note that at least one bit is always 1 in mask.
51bool ITSession::InitIT(unsigned short bits7_0)
52{
53 ITCounter = CountITSize(Bits32(bits7_0, 3, 0));
54 if (ITCounter == 0)
55 return false;
56
57 // A8.6.50 IT
58 unsigned short FirstCond = Bits32(bits7_0, 7, 4);
59 if (FirstCond == 0xF)
60 {
61 printf("Encoding error: IT FirstCond '1111'\n");
62 return false;
63 }
64 if (FirstCond == 0xE && ITCounter != 1)
65 {
66 printf("Encoding error: IT FirstCond '1110' && Mask != '1000'\n");
67 return false;
68 }
69
70 ITState = bits7_0;
71 return true;
72}
73
74// Update ITState if necessary.
75void ITSession::ITAdvance()
76{
77 assert(ITCounter);
78 --ITCounter;
79 if (ITCounter == 0)
80 ITState = 0;
81 else
82 {
83 unsigned short NewITState4_0 = Bits32(ITState, 4, 0) << 1;
84 SetBits32(ITState, 4, 0, NewITState4_0);
85 }
86}
87
88// Return true if we're inside an IT Block.
89bool ITSession::InITBlock()
90{
91 return ITCounter != 0;
92}
93
Johnny Chenc315f862011-02-05 00:46:10 +000094// Return true if we're the last instruction inside an IT Block.
95bool ITSession::LastInITBlock()
96{
97 return ITCounter == 1;
98}
99
Johnny Chen93070472011-02-04 23:02:47 +0000100// Get condition bits for the current thumb instruction.
101uint32_t ITSession::GetCond()
102{
Johnny Chenc315f862011-02-05 00:46:10 +0000103 if (InITBlock())
104 return Bits32(ITState, 7, 4);
105 else
106 return COND_AL;
Johnny Chen93070472011-02-04 23:02:47 +0000107}
108
Greg Clayton64c84432011-01-21 22:02:52 +0000109// ARM constants used during decoding
110#define REG_RD 0
111#define LDM_REGLIST 1
112#define PC_REG 15
113#define PC_REGLIST_BIT 0x8000
114
Johnny Chen251af6a2011-01-21 22:47:25 +0000115#define ARMv4 (1u << 0)
Greg Clayton64c84432011-01-21 22:02:52 +0000116#define ARMv4T (1u << 1)
117#define ARMv5T (1u << 2)
118#define ARMv5TE (1u << 3)
119#define ARMv5TEJ (1u << 4)
Johnny Chen251af6a2011-01-21 22:47:25 +0000120#define ARMv6 (1u << 5)
Greg Clayton64c84432011-01-21 22:02:52 +0000121#define ARMv6K (1u << 6)
122#define ARMv6T2 (1u << 7)
Johnny Chen251af6a2011-01-21 22:47:25 +0000123#define ARMv7 (1u << 8)
Johnny Chen60c0d622011-01-25 23:49:39 +0000124#define ARMv8 (1u << 9)
Greg Clayton64c84432011-01-21 22:02:52 +0000125#define ARMvAll (0xffffffffu)
126
Johnny Chen9b8d7832011-02-02 01:13:56 +0000127#define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8)
128#define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8)
129#define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8)
Greg Clayton64c84432011-01-21 22:02:52 +0000130
Johnny Chen0e00af22011-02-10 19:40:42 +0000131//----------------------------------------------------------------------
132//
133// EmulateInstructionARM implementation
134//
135//----------------------------------------------------------------------
136
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000137void
138EmulateInstructionARM::Initialize ()
Johnny Chen7dc60e12011-01-24 19:46:32 +0000139{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000140}
Johnny Chen7dc60e12011-01-24 19:46:32 +0000141
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000142void
143EmulateInstructionARM::Terminate ()
Greg Clayton64c84432011-01-21 22:02:52 +0000144{
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000145}
146
Caroline Ticefa172202011-02-11 22:49:54 +0000147// Write "bits (32) UNKNOWN" to memory address "address". Helper function for many ARM instructions.
148bool
149EmulateInstructionARM::WriteBits32UnknownToMemory (addr_t address)
150{
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000151 EmulateInstruction::Context context;
152 context.type = EmulateInstruction::eContextWriteMemoryRandomBits;
153 context.SetNoArgs ();
Caroline Ticefa172202011-02-11 22:49:54 +0000154
155 uint32_t random_data = rand ();
156 const uint32_t addr_byte_size = GetAddressByteSize();
157
158 if (!WriteMemoryUnsigned (context, address, random_data, addr_byte_size))
159 return false;
160
161 return true;
162}
163
Caroline Tice713c2662011-02-11 17:59:55 +0000164// Write "bits (32) UNKNOWN" to register n. Helper function for many ARM instructions.
165bool
166EmulateInstructionARM::WriteBits32Unknown (int n)
167{
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000168 EmulateInstruction::Context context;
169 context.type = EmulateInstruction::eContextWriteRegisterRandomBits;
170 context.SetNoArgs ();
Caroline Tice713c2662011-02-11 17:59:55 +0000171
Johnny Chen62ff6f52011-02-11 18:11:22 +0000172 bool success;
Caroline Tice713c2662011-02-11 17:59:55 +0000173 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
174
175 if (!success)
176 return false;
177
178 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
179 return false;
180
181 return true;
182}
183
Johnny Chen08c25e82011-01-31 18:02:28 +0000184// Push Multiple Registers stores multiple registers to the stack, storing to
185// consecutive memory locations ending just below the address in SP, and updates
186// SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000187bool
188EmulateInstructionARM::EmulatePush (ARMEncoding encoding)
Greg Clayton64c84432011-01-21 22:02:52 +0000189{
190#if 0
191 // ARM pseudo code...
192 if (ConditionPassed())
193 {
194 EncodingSpecificOperations();
195 NullCheckIfThumbEE(13);
196 address = SP - 4*BitCount(registers);
197
198 for (i = 0 to 14)
199 {
200 if (registers<i> == ’1’)
201 {
202 if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1
203 MemA[address,4] = bits(32) UNKNOWN;
204 else
205 MemA[address,4] = R[i];
206 address = address + 4;
207 }
208 }
209
210 if (registers<15> == ’1’) // Only possible for encoding A1 or A2
211 MemA[address,4] = PCStoreValue();
212
213 SP = SP - 4*BitCount(registers);
214 }
215#endif
216
217 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000218 const uint32_t opcode = OpcodeAsUnsigned (&success);
Greg Clayton64c84432011-01-21 22:02:52 +0000219 if (!success)
220 return false;
221
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000222 if (ConditionPassed())
Greg Clayton64c84432011-01-21 22:02:52 +0000223 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000224 const uint32_t addr_byte_size = GetAddressByteSize();
225 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000226 if (!success)
227 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000228 uint32_t registers = 0;
Johnny Chen91d99862011-01-25 19:07:04 +0000229 uint32_t Rt; // the source register
Johnny Chen3c75c762011-01-22 00:47:08 +0000230 switch (encoding) {
Johnny Chenaedde1c2011-01-24 20:38:45 +0000231 case eEncodingT1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000232 registers = Bits32(opcode, 7, 0);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000233 // The M bit represents LR.
Johnny Chenbd599902011-02-10 21:39:01 +0000234 if (Bit32(opcode, 8))
Johnny Chenef85e912011-01-31 23:07:40 +0000235 registers |= (1u << 14);
Johnny Chenaedde1c2011-01-24 20:38:45 +0000236 // if BitCount(registers) < 1 then UNPREDICTABLE;
237 if (BitCount(registers) < 1)
238 return false;
239 break;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000240 case eEncodingT2:
241 // Ignore bits 15 & 13.
Johnny Chen108d5aa2011-01-26 01:00:55 +0000242 registers = Bits32(opcode, 15, 0) & ~0xa000;
Johnny Chen7dc60e12011-01-24 19:46:32 +0000243 // if BitCount(registers) < 2 then UNPREDICTABLE;
244 if (BitCount(registers) < 2)
245 return false;
246 break;
247 case eEncodingT3:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000248 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000249 // if BadReg(t) then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000250 if (BadReg(Rt))
Johnny Chen7dc60e12011-01-24 19:46:32 +0000251 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000252 registers = (1u << Rt);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000253 break;
Johnny Chen3c75c762011-01-22 00:47:08 +0000254 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000255 registers = Bits32(opcode, 15, 0);
Johnny Chena33d4842011-01-24 22:25:48 +0000256 // Instead of return false, let's handle the following case as well,
257 // which amounts to pushing one reg onto the full descending stacks.
258 // if BitCount(register_list) < 2 then SEE STMDB / STMFD;
Johnny Chen3c75c762011-01-22 00:47:08 +0000259 break;
260 case eEncodingA2:
Johnny Chen108d5aa2011-01-26 01:00:55 +0000261 Rt = Bits32(opcode, 15, 12);
Johnny Chen7dc60e12011-01-24 19:46:32 +0000262 // if t == 13 then UNPREDICTABLE;
Johnny Chen91d99862011-01-25 19:07:04 +0000263 if (Rt == dwarf_sp)
Johnny Chen3c75c762011-01-22 00:47:08 +0000264 return false;
Johnny Chen91d99862011-01-25 19:07:04 +0000265 registers = (1u << Rt);
Johnny Chen3c75c762011-01-22 00:47:08 +0000266 break;
Johnny Chence1ca772011-01-25 01:13:00 +0000267 default:
268 return false;
Johnny Chen3c75c762011-01-22 00:47:08 +0000269 }
Johnny Chence1ca772011-01-25 01:13:00 +0000270 addr_t sp_offset = addr_byte_size * BitCount (registers);
Greg Clayton64c84432011-01-21 22:02:52 +0000271 addr_t addr = sp - sp_offset;
272 uint32_t i;
273
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000274 EmulateInstruction::Context context;
275 context.type = EmulateInstruction::eContextPushRegisterOnStack;
276 Register dwarf_reg;
277 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Greg Clayton64c84432011-01-21 22:02:52 +0000278 for (i=0; i<15; ++i)
279 {
Johnny Chen7c1bf922011-02-08 23:49:37 +0000280 if (BitIsSet (registers, i))
Greg Clayton64c84432011-01-21 22:02:52 +0000281 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000282 dwarf_reg.num = dwarf_r0 + i;
283 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
284 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000285 if (!success)
286 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000287 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000288 return false;
289 addr += addr_byte_size;
290 }
291 }
292
Johnny Chen7c1bf922011-02-08 23:49:37 +0000293 if (BitIsSet (registers, 15))
Greg Clayton64c84432011-01-21 22:02:52 +0000294 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000295 dwarf_reg.num = dwarf_pc;
296 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000297 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Greg Clayton64c84432011-01-21 22:02:52 +0000298 if (!success)
299 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000300 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Greg Clayton64c84432011-01-21 22:02:52 +0000301 return false;
302 }
303
304 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000305 context.SetImmediateSigned (-sp_offset);
Greg Clayton64c84432011-01-21 22:02:52 +0000306
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000307 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Greg Clayton64c84432011-01-21 22:02:52 +0000308 return false;
309 }
310 return true;
311}
312
Johnny Chenef85e912011-01-31 23:07:40 +0000313// Pop Multiple Registers loads multiple registers from the stack, loading from
314// consecutive memory locations staring at the address in SP, and updates
315// SP to point just above the loaded data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000316bool
317EmulateInstructionARM::EmulatePop (ARMEncoding encoding)
Johnny Chenef85e912011-01-31 23:07:40 +0000318{
319#if 0
320 // ARM pseudo code...
321 if (ConditionPassed())
322 {
323 EncodingSpecificOperations(); NullCheckIfThumbEE(13);
324 address = SP;
325 for i = 0 to 14
326 if registers<i> == ‘1’ then
327 R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4;
328 if registers<15> == ‘1’ then
329 if UnalignedAllowed then
330 LoadWritePC(MemU[address,4]);
331 else
332 LoadWritePC(MemA[address,4]);
333 if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers);
334 if registers<13> == ‘1’ then SP = bits(32) UNKNOWN;
335 }
336#endif
337
338 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000339 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenef85e912011-01-31 23:07:40 +0000340 if (!success)
341 return false;
342
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000343 if (ConditionPassed())
Johnny Chenef85e912011-01-31 23:07:40 +0000344 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000345 const uint32_t addr_byte_size = GetAddressByteSize();
346 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000347 if (!success)
348 return false;
349 uint32_t registers = 0;
350 uint32_t Rt; // the destination register
351 switch (encoding) {
352 case eEncodingT1:
353 registers = Bits32(opcode, 7, 0);
354 // The P bit represents PC.
Johnny Chenbd599902011-02-10 21:39:01 +0000355 if (Bit32(opcode, 8))
Johnny Chenef85e912011-01-31 23:07:40 +0000356 registers |= (1u << 15);
357 // if BitCount(registers) < 1 then UNPREDICTABLE;
358 if (BitCount(registers) < 1)
359 return false;
360 break;
361 case eEncodingT2:
362 // Ignore bit 13.
363 registers = Bits32(opcode, 15, 0) & ~0x2000;
364 // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE;
Johnny Chenbd599902011-02-10 21:39:01 +0000365 if (BitCount(registers) < 2 || (Bit32(opcode, 15) && Bit32(opcode, 14)))
Johnny Chenef85e912011-01-31 23:07:40 +0000366 return false;
Johnny Chen098ae2d2011-02-12 00:50:05 +0000367 // if registers<15> == '1' && InITBlock() && !LastInITBlock() then UNPREDICTABLE;
368 if (BitIsSet(registers, 15) && InITBlock() && !LastInITBlock())
369 return false;
Johnny Chenef85e912011-01-31 23:07:40 +0000370 break;
371 case eEncodingT3:
372 Rt = Bits32(opcode, 15, 12);
373 // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +0000374 if (Rt == 13)
375 return false;
376 if (Rt == 15 && InITBlock() && !LastInITBlock())
Johnny Chenef85e912011-01-31 23:07:40 +0000377 return false;
378 registers = (1u << Rt);
379 break;
380 case eEncodingA1:
381 registers = Bits32(opcode, 15, 0);
382 // Instead of return false, let's handle the following case as well,
383 // which amounts to popping one reg from the full descending stacks.
384 // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD;
385
386 // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +0000387 if (BitIsSet(opcode, 13) && ArchVersion() >= ARMv7)
Johnny Chenef85e912011-01-31 23:07:40 +0000388 return false;
389 break;
390 case eEncodingA2:
391 Rt = Bits32(opcode, 15, 12);
392 // if t == 13 then UNPREDICTABLE;
393 if (Rt == dwarf_sp)
394 return false;
395 registers = (1u << Rt);
396 break;
397 default:
398 return false;
399 }
400 addr_t sp_offset = addr_byte_size * BitCount (registers);
401 addr_t addr = sp;
402 uint32_t i, data;
403
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000404 EmulateInstruction::Context context;
405 context.type = EmulateInstruction::eContextPopRegisterOffStack;
406 Register dwarf_reg;
407 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chenef85e912011-01-31 23:07:40 +0000408 for (i=0; i<15; ++i)
409 {
Johnny Chen7c1bf922011-02-08 23:49:37 +0000410 if (BitIsSet (registers, i))
Johnny Chenef85e912011-01-31 23:07:40 +0000411 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000412 dwarf_reg.num = dwarf_r0 + i;
413 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000414 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000415 if (!success)
416 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000417 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000418 return false;
419 addr += addr_byte_size;
420 }
421 }
422
Johnny Chen7c1bf922011-02-08 23:49:37 +0000423 if (BitIsSet (registers, 15))
Johnny Chenef85e912011-01-31 23:07:40 +0000424 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000425 dwarf_reg.num = dwarf_pc;
426 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000427 data = ReadMemoryUnsigned(context, addr, 4, 0, &success);
Johnny Chenef85e912011-01-31 23:07:40 +0000428 if (!success)
429 return false;
Johnny Chenf3eaacf2011-02-09 19:30:49 +0000430 // In ARMv5T and above, this is an interworking branch.
Johnny Chen668b4512011-02-15 21:08:58 +0000431 if (!LoadWritePC(context, data))
Johnny Chenef85e912011-01-31 23:07:40 +0000432 return false;
433 addr += addr_byte_size;
434 }
435
436 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000437 context.SetImmediateSigned (sp_offset);
Johnny Chenef85e912011-01-31 23:07:40 +0000438
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000439 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
Johnny Chenef85e912011-01-31 23:07:40 +0000440 return false;
441 }
442 return true;
443}
444
Johnny Chen5b442b72011-01-27 19:34:30 +0000445// Set r7 or ip to point to saved value residing within the stack.
Johnny Chenbcec3af2011-01-27 01:26:19 +0000446// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000447bool
448EmulateInstructionARM::EmulateAddRdSPImmediate (ARMEncoding encoding)
Johnny Chenbcec3af2011-01-27 01:26:19 +0000449{
450#if 0
451 // ARM pseudo code...
452 if (ConditionPassed())
453 {
454 EncodingSpecificOperations();
455 (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’);
456 if d == 15 then
457 ALUWritePC(result); // setflags is always FALSE here
458 else
459 R[d] = result;
460 if setflags then
461 APSR.N = result<31>;
462 APSR.Z = IsZeroBit(result);
463 APSR.C = carry;
464 APSR.V = overflow;
465 }
466#endif
467
468 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000469 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000470 if (!success)
471 return false;
472
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000473 if (ConditionPassed())
Johnny Chenbcec3af2011-01-27 01:26:19 +0000474 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000475 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000476 if (!success)
477 return false;
478 uint32_t Rd; // the destination register
479 uint32_t imm32;
480 switch (encoding) {
481 case eEncodingT1:
482 Rd = 7;
483 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32)
484 break;
485 case eEncodingA1:
486 Rd = Bits32(opcode, 15, 12);
487 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
488 break;
489 default:
490 return false;
491 }
492 addr_t sp_offset = imm32;
493 addr_t addr = sp + sp_offset; // a pointer to the stack area
494
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000495 EmulateInstruction::Context context;
496 context.type = EmulateInstruction::eContextRegisterPlusOffset;
497 Register sp_reg;
498 sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
499 context.SetRegisterPlusOffset (sp_reg, sp_offset);
Johnny Chenbcec3af2011-01-27 01:26:19 +0000500
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000501 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr))
Johnny Chenbcec3af2011-01-27 01:26:19 +0000502 return false;
503 }
504 return true;
505}
506
Johnny Chen2ccad832011-01-28 19:57:25 +0000507// Set r7 or ip to the current stack pointer.
508// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000509bool
510EmulateInstructionARM::EmulateMovRdSP (ARMEncoding encoding)
Johnny Chen2ccad832011-01-28 19:57:25 +0000511{
512#if 0
513 // ARM pseudo code...
514 if (ConditionPassed())
515 {
516 EncodingSpecificOperations();
517 result = R[m];
518 if d == 15 then
519 ALUWritePC(result); // setflags is always FALSE here
520 else
521 R[d] = result;
522 if setflags then
523 APSR.N = result<31>;
524 APSR.Z = IsZeroBit(result);
525 // APSR.C unchanged
526 // APSR.V unchanged
527 }
528#endif
529
530 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000531 //const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000532 //if (!success)
533 // return false;
Johnny Chen2ccad832011-01-28 19:57:25 +0000534
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000535 if (ConditionPassed())
Johnny Chen2ccad832011-01-28 19:57:25 +0000536 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000537 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen2ccad832011-01-28 19:57:25 +0000538 if (!success)
539 return false;
540 uint32_t Rd; // the destination register
541 switch (encoding) {
542 case eEncodingT1:
543 Rd = 7;
544 break;
545 case eEncodingA1:
546 Rd = 12;
547 break;
548 default:
549 return false;
550 }
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000551
552 EmulateInstruction::Context context;
553 context.type = EmulateInstruction::eContextRegisterPlusOffset;
554 Register sp_reg;
555 sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
556 context.SetRegisterPlusOffset (sp_reg, 0);
Johnny Chen2ccad832011-01-28 19:57:25 +0000557
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000558 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp))
Johnny Chen2ccad832011-01-28 19:57:25 +0000559 return false;
560 }
561 return true;
562}
563
Johnny Chen1c13b622011-01-29 00:11:15 +0000564// Move from high register (r8-r15) to low register (r0-r7).
565// MOV (register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000566bool
567EmulateInstructionARM::EmulateMovLowHigh (ARMEncoding encoding)
Johnny Chen1c13b622011-01-29 00:11:15 +0000568{
Johnny Chen338bf542011-02-10 19:29:03 +0000569 return EmulateMovRdRm (encoding);
570}
571
572// Move from register to register.
573// MOV (register)
574bool
575EmulateInstructionARM::EmulateMovRdRm (ARMEncoding encoding)
576{
Johnny Chen1c13b622011-01-29 00:11:15 +0000577#if 0
578 // ARM pseudo code...
579 if (ConditionPassed())
580 {
581 EncodingSpecificOperations();
582 result = R[m];
583 if d == 15 then
584 ALUWritePC(result); // setflags is always FALSE here
585 else
586 R[d] = result;
587 if setflags then
588 APSR.N = result<31>;
589 APSR.Z = IsZeroBit(result);
590 // APSR.C unchanged
591 // APSR.V unchanged
592 }
593#endif
594
595 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000596 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000597 if (!success)
598 return false;
599
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000600 if (ConditionPassed())
Johnny Chen1c13b622011-01-29 00:11:15 +0000601 {
602 uint32_t Rm; // the source register
603 uint32_t Rd; // the destination register
Johnny Chen338bf542011-02-10 19:29:03 +0000604 bool setflags;
Johnny Chen1c13b622011-01-29 00:11:15 +0000605 switch (encoding) {
606 case eEncodingT1:
607 Rm = Bits32(opcode, 6, 3);
Johnny Chenbd599902011-02-10 21:39:01 +0000608 Rd = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 1);
Johnny Chen338bf542011-02-10 19:29:03 +0000609 setflags = false;
610 break;
611 case eEncodingT2:
612 Rm = Bits32(opcode, 5, 3);
613 Rd = Bits32(opcode, 2, 1);
614 setflags = true;
Johnny Chen1c13b622011-01-29 00:11:15 +0000615 break;
616 default:
617 return false;
618 }
Johnny Chen338bf542011-02-10 19:29:03 +0000619 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen1c13b622011-01-29 00:11:15 +0000620 if (!success)
621 return false;
622
623 // The context specifies that Rm is to be moved into Rd.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000624 EmulateInstruction::Context context;
625 context.type = EmulateInstruction::eContextRegisterPlusOffset;
626 Register dwarf_reg;
627 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm);
628 context.SetRegisterPlusOffset (dwarf_reg, 0);
Johnny Chen1c13b622011-01-29 00:11:15 +0000629
Johnny Chen338bf542011-02-10 19:29:03 +0000630 if (Rd == 15)
631 {
Johnny Chen668b4512011-02-15 21:08:58 +0000632 if (!ALUWritePC (context, reg_value))
Johnny Chen338bf542011-02-10 19:29:03 +0000633 return false;
634 }
635 else
636 {
637 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value))
638 return false;
639 if (setflags)
640 {
641 m_new_inst_cpsr = m_inst_cpsr;
Johnny Chenbd599902011-02-10 21:39:01 +0000642 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(reg_value, CPSR_N));
643 SetBit32(m_new_inst_cpsr, CPSR_Z, reg_value == 0 ? 1 : 0);
Johnny Chen338bf542011-02-10 19:29:03 +0000644 if (m_new_inst_cpsr != m_inst_cpsr)
645 {
646 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
647 return false;
648 }
649 }
650 }
Johnny Chen1c13b622011-01-29 00:11:15 +0000651 }
652 return true;
653}
654
Johnny Chen357c30f2011-02-14 22:04:25 +0000655// Move (immediate) writes an immediate value to the destination register. It
656// can optionally update the condition flags based on the value.
657// MOV (immediate)
658bool
659EmulateInstructionARM::EmulateMovRdImm (ARMEncoding encoding)
660{
661#if 0
662 // ARM pseudo code...
663 if (ConditionPassed())
664 {
665 EncodingSpecificOperations();
666 result = imm32;
667 if d == 15 then // Can only occur for ARM encoding
668 ALUWritePC(result); // setflags is always FALSE here
669 else
670 R[d] = result;
671 if setflags then
672 APSR.N = result<31>;
673 APSR.Z = IsZeroBit(result);
674 APSR.C = carry;
675 // APSR.V unchanged
676 }
677#endif
678 bool success = false;
679 const uint32_t opcode = OpcodeAsUnsigned (&success);
680 if (!success)
681 return false;
682
683 if (ConditionPassed())
684 {
685 uint32_t Rd; // the destination register
686 uint32_t imm12; // some intermediate result
687 uint32_t imm32; // the immediate value to be written to Rd
688 uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C.
689 bool setflags;
690 switch (encoding) {
691 case eEncodingT1:
692 Rd = Bits32(opcode, 11, 8);
693 setflags = !InITBlock();
694 imm32 = Bits32(opcode, 7, 0); // imm32 = ZeroExtend(imm8, 32)
695 carry = Bit32(m_inst_cpsr, CPSR_C);
696 break;
697 case eEncodingT2:
698 Rd = Bits32(opcode, 15, 12);
699 setflags = BitIsSet(opcode, 20);
700 imm12 = Bit32(opcode, 26) << 11 | Bits32(opcode, 14, 12) << 8 | Bits32(opcode, 7, 0);
701 imm32 = ThumbExpandImm_C(imm12, Bit32(m_inst_cpsr, CPSR_C), carry);
Johnny Chen9798cfc2011-02-14 23:33:58 +0000702 if (BadReg(Rd))
703 return false;
Johnny Chen357c30f2011-02-14 22:04:25 +0000704 break;
705 default:
706 return false;
707 }
708 uint32_t result = imm32;
709
710 // The context specifies that an immediate is to be moved into Rd.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000711 EmulateInstruction::Context context;
712 context.type = EmulateInstruction::eContextImmediate;
713 context.SetNoArgs ();
714
Johnny Chen357c30f2011-02-14 22:04:25 +0000715 if (Rd == 15)
716 {
Johnny Chen668b4512011-02-15 21:08:58 +0000717 if (!ALUWritePC (context, result))
Johnny Chen357c30f2011-02-14 22:04:25 +0000718 return false;
719 }
720 else
721 {
722 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
723 return false;
724 if (setflags)
725 {
726 m_new_inst_cpsr = m_inst_cpsr;
727 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(result, CPSR_N));
728 SetBit32(m_new_inst_cpsr, CPSR_Z, result == 0 ? 1 : 0);
729 SetBit32(m_new_inst_cpsr, CPSR_C, carry);
730 if (m_new_inst_cpsr != m_inst_cpsr)
731 {
732 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
733 return false;
734 }
735 }
736 }
737 }
738 return true;
739}
740
Johnny Chen28070c32011-02-12 01:27:26 +0000741// Bitwise NOT (immediate) writes the bitwise inverse of an immediate value to
742// the destination register. It can optionally update the condition flags based
743// on the value.
744// MVN (immediate)
745bool
746EmulateInstructionARM::EmulateMvnRdImm (ARMEncoding encoding)
747{
748#if 0
749 // ARM pseudo code...
750 if (ConditionPassed())
751 {
752 EncodingSpecificOperations();
753 result = NOT(imm32);
754 if d == 15 then // Can only occur for ARM encoding
755 ALUWritePC(result); // setflags is always FALSE here
756 else
757 R[d] = result;
758 if setflags then
759 APSR.N = result<31>;
760 APSR.Z = IsZeroBit(result);
761 APSR.C = carry;
762 // APSR.V unchanged
763 }
764#endif
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000765 bool success = false;
766 const uint32_t opcode = OpcodeAsUnsigned (&success);
767 if (!success)
768 return false;
769
770 if (ConditionPassed())
771 {
772 uint32_t Rd; // the destination register
Johnny Chen357c30f2011-02-14 22:04:25 +0000773 uint32_t imm12; // the first operand to ThumbExpandImm_C or ARMExpandImm_C
774 uint32_t imm32; // the output after ThumbExpandImm_C or ARMExpandImm_C
775 uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000776 bool setflags;
777 switch (encoding) {
778 case eEncodingT1:
779 Rd = Bits32(opcode, 11, 8);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000780 setflags = BitIsSet(opcode, 20);
Johnny Chen357c30f2011-02-14 22:04:25 +0000781 imm12 = Bit32(opcode, 26) << 11 | Bits32(opcode, 14, 12) << 8 | Bits32(opcode, 7, 0);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000782 imm32 = ThumbExpandImm_C(imm12, Bit32(m_inst_cpsr, CPSR_C), carry);
783 break;
784 case eEncodingA1:
785 Rd = Bits32(opcode, 15, 12);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000786 setflags = BitIsSet(opcode, 20);
Johnny Chen357c30f2011-02-14 22:04:25 +0000787 imm12 = Bits32(opcode, 11, 0);
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000788 imm32 = ARMExpandImm_C(imm12, Bit32(m_inst_cpsr, CPSR_C), carry);
789 break;
790 default:
791 return false;
792 }
793 uint32_t result = ~imm32;
794
795 // The context specifies that an immediate is to be moved into Rd.
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000796 EmulateInstruction::Context context;
797 context.type = EmulateInstruction::eContextImmediate;
798 context.SetNoArgs ();
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000799
800 if (Rd == 15)
801 {
Johnny Chen668b4512011-02-15 21:08:58 +0000802 if (!ALUWritePC (context, result))
Johnny Chen33bf6ab2011-02-14 20:39:01 +0000803 return false;
804 }
805 else
806 {
807 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
808 return false;
809 if (setflags)
810 {
811 m_new_inst_cpsr = m_inst_cpsr;
812 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(result, CPSR_N));
813 SetBit32(m_new_inst_cpsr, CPSR_Z, result == 0 ? 1 : 0);
814 SetBit32(m_new_inst_cpsr, CPSR_C, carry);
815 if (m_new_inst_cpsr != m_inst_cpsr)
816 {
817 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
818 return false;
819 }
820 }
821 }
822 }
823 return true;
Johnny Chen28070c32011-02-12 01:27:26 +0000824}
825
Johnny Chen788e0552011-01-27 22:52:23 +0000826// PC relative immediate load into register, possibly followed by ADD (SP plus register).
827// LDR (literal)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000828bool
Johnny Chenc9de9102011-02-11 19:12:30 +0000829EmulateInstructionARM::EmulateLDRRtPCRelative (ARMEncoding encoding)
Johnny Chen788e0552011-01-27 22:52:23 +0000830{
831#if 0
832 // ARM pseudo code...
833 if (ConditionPassed())
834 {
835 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
836 base = Align(PC,4);
837 address = if add then (base + imm32) else (base - imm32);
838 data = MemU[address,4];
839 if t == 15 then
840 if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE;
841 elsif UnalignedSupport() || address<1:0> = ‘00’ then
842 R[t] = data;
843 else // Can only apply before ARMv7
844 if CurrentInstrSet() == InstrSet_ARM then
845 R[t] = ROR(data, 8*UInt(address<1:0>));
846 else
847 R[t] = bits(32) UNKNOWN;
848 }
849#endif
850
851 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000852 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen788e0552011-01-27 22:52:23 +0000853 if (!success)
854 return false;
855
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000856 if (ConditionPassed())
Johnny Chen788e0552011-01-27 22:52:23 +0000857 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000858 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000859 if (!success)
860 return false;
Johnny Chen809742e2011-01-28 00:32:27 +0000861
862 // PC relative immediate load context
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000863 EmulateInstruction::Context context;
864 context.type = EmulateInstruction::eContextRegisterPlusOffset;
865 Register pc_reg;
866 pc_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
867 context.SetRegisterPlusOffset (pc_reg, 0);
868
Johnny Chenc9de9102011-02-11 19:12:30 +0000869 uint32_t Rt; // the destination register
Johnny Chen788e0552011-01-27 22:52:23 +0000870 uint32_t imm32; // immediate offset from the PC
Johnny Chenc9de9102011-02-11 19:12:30 +0000871 bool add; // +imm32 or -imm32?
872 addr_t base; // the base address
873 addr_t address; // the PC relative address
Johnny Chen788e0552011-01-27 22:52:23 +0000874 uint32_t data; // the literal data value from the PC relative load
875 switch (encoding) {
876 case eEncodingT1:
Johnny Chenc9de9102011-02-11 19:12:30 +0000877 Rt = Bits32(opcode, 10, 8);
Johnny Chen788e0552011-01-27 22:52:23 +0000878 imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32);
Johnny Chenc9de9102011-02-11 19:12:30 +0000879 add = true;
880 base = Align(pc + 4, 4);
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000881 context.SetRegisterPlusOffset (pc_reg, 4 + imm32);
Johnny Chenc9de9102011-02-11 19:12:30 +0000882 break;
883 case eEncodingT2:
884 Rt = Bits32(opcode, 15, 12);
885 imm32 = Bits32(opcode, 11, 0) << 2; // imm32 = ZeroExtend(imm12, 32);
886 add = BitIsSet(opcode, 23);
Johnny Chen098ae2d2011-02-12 00:50:05 +0000887 if (Rt == 15 && InITBlock() && !LastInITBlock())
Johnny Chenc9de9102011-02-11 19:12:30 +0000888 return false;
889 base = Align(pc + 4, 4);
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000890 context.SetRegisterPlusOffset (pc_reg, 4 + imm32);
Johnny Chen788e0552011-01-27 22:52:23 +0000891 break;
892 default:
893 return false;
894 }
Johnny Chenc9de9102011-02-11 19:12:30 +0000895
896 if (add)
897 address = base + imm32;
898 else
899 address = base - imm32;
900 data = ReadMemoryUnsigned(context, address, 4, 0, &success);
Johnny Chen788e0552011-01-27 22:52:23 +0000901 if (!success)
Johnny Chen809742e2011-01-28 00:32:27 +0000902 return false;
Johnny Chenc9de9102011-02-11 19:12:30 +0000903
904 if (Rt == 15)
905 {
906 if (Bits32(address, 1, 0) == 0)
907 {
908 // In ARMv5T and above, this is an interworking branch.
Johnny Chen668b4512011-02-15 21:08:58 +0000909 if (!LoadWritePC(context, data))
Johnny Chenc9de9102011-02-11 19:12:30 +0000910 return false;
911 }
912 else
913 return false;
914 }
915 else if (UnalignedSupport() || Bits32(address, 1, 0) == 0)
916 {
917 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data))
918 return false;
919 }
920 else // We don't handle ARM for now.
921 return false;
922
923 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data))
Johnny Chen788e0552011-01-27 22:52:23 +0000924 return false;
925 }
926 return true;
927}
928
Johnny Chen5b442b72011-01-27 19:34:30 +0000929// An add operation to adjust the SP.
Johnny Chenfdd179e2011-01-31 20:09:28 +0000930// ADD (SP plus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000931bool
932EmulateInstructionARM::EmulateAddSPImmediate (ARMEncoding encoding)
Johnny Chenfdd179e2011-01-31 20:09:28 +0000933{
934#if 0
935 // ARM pseudo code...
936 if (ConditionPassed())
937 {
938 EncodingSpecificOperations();
939 (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’);
940 if d == 15 then // Can only occur for ARM encoding
941 ALUWritePC(result); // setflags is always FALSE here
942 else
943 R[d] = result;
944 if setflags then
945 APSR.N = result<31>;
946 APSR.Z = IsZeroBit(result);
947 APSR.C = carry;
948 APSR.V = overflow;
949 }
950#endif
951
952 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000953 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000954 if (!success)
955 return false;
956
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000957 if (ConditionPassed())
Johnny Chenfdd179e2011-01-31 20:09:28 +0000958 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000959 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000960 if (!success)
961 return false;
962 uint32_t imm32; // the immediate operand
963 switch (encoding) {
964 case eEncodingT2:
965 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
966 break;
967 default:
968 return false;
969 }
970 addr_t sp_offset = imm32;
971 addr_t addr = sp + sp_offset; // the adjusted stack pointer value
972
Caroline Tice9bfe7f22011-02-14 23:03:21 +0000973 EmulateInstruction::Context context;
974 context.type = EmulateInstruction::eContextAdjustStackPointer;
975 context.SetImmediateSigned (sp_offset);
Johnny Chenfdd179e2011-01-31 20:09:28 +0000976
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000977 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chenfdd179e2011-01-31 20:09:28 +0000978 return false;
979 }
980 return true;
981}
982
983// An add operation to adjust the SP.
Johnny Chen5b442b72011-01-27 19:34:30 +0000984// ADD (SP plus register)
Greg Clayton2b8e8b02011-02-01 00:49:32 +0000985bool
986EmulateInstructionARM::EmulateAddSPRm (ARMEncoding encoding)
Johnny Chen5b442b72011-01-27 19:34:30 +0000987{
988#if 0
989 // ARM pseudo code...
990 if (ConditionPassed())
991 {
992 EncodingSpecificOperations();
993 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
994 (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’);
995 if d == 15 then
996 ALUWritePC(result); // setflags is always FALSE here
997 else
998 R[d] = result;
999 if setflags then
1000 APSR.N = result<31>;
1001 APSR.Z = IsZeroBit(result);
1002 APSR.C = carry;
1003 APSR.V = overflow;
1004 }
1005#endif
1006
1007 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001008 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen5b442b72011-01-27 19:34:30 +00001009 if (!success)
1010 return false;
1011
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001012 if (ConditionPassed())
Johnny Chen5b442b72011-01-27 19:34:30 +00001013 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001014 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +00001015 if (!success)
1016 return false;
1017 uint32_t Rm; // the second operand
1018 switch (encoding) {
1019 case eEncodingT2:
1020 Rm = Bits32(opcode, 6, 3);
1021 break;
1022 default:
1023 return false;
1024 }
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001025 int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
Johnny Chen5b442b72011-01-27 19:34:30 +00001026 if (!success)
1027 return false;
1028
1029 addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value
1030
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001031 EmulateInstruction::Context context;
1032 context.type = EmulateInstruction::eContextAdjustStackPointer;
1033 context.SetImmediateSigned (reg_value);
Johnny Chen5b442b72011-01-27 19:34:30 +00001034
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001035 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen5b442b72011-01-27 19:34:30 +00001036 return false;
1037 }
1038 return true;
1039}
1040
Johnny Chen9b8d7832011-02-02 01:13:56 +00001041// Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine
1042// at a PC-relative address, and changes instruction set from ARM to Thumb, or
1043// from Thumb to ARM.
1044// BLX (immediate)
1045bool
1046EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding)
1047{
1048#if 0
1049 // ARM pseudo code...
1050 if (ConditionPassed())
1051 {
1052 EncodingSpecificOperations();
1053 if CurrentInstrSet() == InstrSet_ARM then
1054 LR = PC - 4;
1055 else
1056 LR = PC<31:1> : '1';
1057 if targetInstrSet == InstrSet_ARM then
1058 targetAddress = Align(PC,4) + imm32;
1059 else
1060 targetAddress = PC + imm32;
1061 SelectInstrSet(targetInstrSet);
1062 BranchWritePC(targetAddress);
1063 }
1064#endif
1065
1066 bool success = false;
1067 const uint32_t opcode = OpcodeAsUnsigned (&success);
1068 if (!success)
1069 return false;
1070
1071 if (ConditionPassed())
1072 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001073 EmulateInstruction::Context context;
1074 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001075 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001076 if (!success)
1077 return false;
Johnny Chen53ebab72011-02-08 23:21:57 +00001078 addr_t lr; // next instruction address
1079 addr_t target; // target address
Johnny Chen9b8d7832011-02-02 01:13:56 +00001080 int32_t imm32; // PC-relative offset
1081 switch (encoding) {
Johnny Chend6c13f02011-02-08 20:36:34 +00001082 case eEncodingT1:
1083 {
1084 lr = (pc + 4) | 1u; // return address
Johnny Chenbd599902011-02-10 21:39:01 +00001085 uint32_t S = Bit32(opcode, 26);
Johnny Chend6c13f02011-02-08 20:36:34 +00001086 uint32_t imm10 = Bits32(opcode, 25, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001087 uint32_t J1 = Bit32(opcode, 13);
1088 uint32_t J2 = Bit32(opcode, 11);
Johnny Chend6c13f02011-02-08 20:36:34 +00001089 uint32_t imm11 = Bits32(opcode, 10, 0);
1090 uint32_t I1 = !(J1 ^ S);
1091 uint32_t I2 = !(J2 ^ S);
Johnny Chen53ebab72011-02-08 23:21:57 +00001092 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Johnny Chend6c13f02011-02-08 20:36:34 +00001093 imm32 = llvm::SignExtend32<25>(imm25);
1094 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001095 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen098ae2d2011-02-12 00:50:05 +00001096 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001097 return false;
Johnny Chend6c13f02011-02-08 20:36:34 +00001098 break;
1099 }
Johnny Chen9b8d7832011-02-02 01:13:56 +00001100 case eEncodingT2:
1101 {
1102 lr = (pc + 4) | 1u; // return address
Johnny Chenbd599902011-02-10 21:39:01 +00001103 uint32_t S = Bit32(opcode, 26);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001104 uint32_t imm10H = Bits32(opcode, 25, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001105 uint32_t J1 = Bit32(opcode, 13);
1106 uint32_t J2 = Bit32(opcode, 11);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001107 uint32_t imm10L = Bits32(opcode, 10, 1);
1108 uint32_t I1 = !(J1 ^ S);
1109 uint32_t I2 = !(J2 ^ S);
Johnny Chen53ebab72011-02-08 23:21:57 +00001110 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10H << 12) | (imm10L << 2);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001111 imm32 = llvm::SignExtend32<25>(imm25);
Johnny Chend6c13f02011-02-08 20:36:34 +00001112 target = Align(pc + 4, 4) + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001113 context.SetModeAndImmediateSigned (eModeARM, 4 + imm32);
Johnny Chen098ae2d2011-02-12 00:50:05 +00001114 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001115 return false;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001116 break;
1117 }
Johnny Chenc47d0ca2011-02-08 18:58:31 +00001118 case eEncodingA1:
1119 lr = pc + 4; // return address
1120 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2);
Johnny Chend6c13f02011-02-08 20:36:34 +00001121 target = Align(pc + 8, 4) + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001122 context.SetModeAndImmediateSigned (eModeARM, 8 + imm32);
Johnny Chenc47d0ca2011-02-08 18:58:31 +00001123 break;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001124 case eEncodingA2:
1125 lr = pc + 4; // return address
1126 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1);
1127 target = pc + 8 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001128 context.SetModeAndImmediateSigned (eModeThumb, 8 + imm32);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001129 break;
1130 default:
1131 return false;
1132 }
1133 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1134 return false;
Johnny Chen9ee056b2011-02-08 00:06:35 +00001135 if (!BranchWritePC(context, target))
Johnny Chen9b8d7832011-02-02 01:13:56 +00001136 return false;
1137 }
1138 return true;
1139}
1140
1141// Branch with Link and Exchange (register) calls a subroutine at an address and
1142// instruction set specified by a register.
1143// BLX (register)
1144bool
1145EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding)
1146{
1147#if 0
1148 // ARM pseudo code...
1149 if (ConditionPassed())
1150 {
1151 EncodingSpecificOperations();
1152 target = R[m];
1153 if CurrentInstrSet() == InstrSet_ARM then
1154 next_instr_addr = PC - 4;
1155 LR = next_instr_addr;
1156 else
1157 next_instr_addr = PC - 2;
1158 LR = next_instr_addr<31:1> : ‘1’;
1159 BXWritePC(target);
1160 }
1161#endif
1162
1163 bool success = false;
1164 const uint32_t opcode = OpcodeAsUnsigned (&success);
1165 if (!success)
1166 return false;
1167
1168 if (ConditionPassed())
1169 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001170 EmulateInstruction::Context context;
1171 context.type = EmulateInstruction::eContextAbsoluteBranchRegister;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001172 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1173 addr_t lr; // next instruction address
Johnny Chen9b8d7832011-02-02 01:13:56 +00001174 if (!success)
1175 return false;
1176 uint32_t Rm; // the register with the target address
1177 switch (encoding) {
1178 case eEncodingT1:
1179 lr = (pc + 2) | 1u; // return address
1180 Rm = Bits32(opcode, 6, 3);
1181 // if m == 15 then UNPREDICTABLE;
1182 if (Rm == 15)
1183 return false;
Johnny Chen098ae2d2011-02-12 00:50:05 +00001184 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001185 return false;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001186 break;
1187 case eEncodingA1:
1188 lr = pc + 4; // return address
1189 Rm = Bits32(opcode, 3, 0);
1190 // if m == 15 then UNPREDICTABLE;
1191 if (Rm == 15)
1192 return false;
Johnny Chenb77be412011-02-04 00:40:18 +00001193 break;
Johnny Chen9b8d7832011-02-02 01:13:56 +00001194 default:
1195 return false;
1196 }
Johnny Chenab3b3512011-02-12 00:10:51 +00001197 addr_t target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
1198 if (!success)
1199 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001200 Register dwarf_reg;
1201 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm);
1202 context.SetRegister (dwarf_reg);
Johnny Chen9b8d7832011-02-02 01:13:56 +00001203 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1204 return false;
Johnny Chen668b4512011-02-15 21:08:58 +00001205 if (!BXWritePC(context, target))
Johnny Chen9b8d7832011-02-02 01:13:56 +00001206 return false;
1207 }
1208 return true;
1209}
1210
Johnny Chenab3b3512011-02-12 00:10:51 +00001211// Branch and Exchange causes a branch to an address and instruction set specified by a register.
1212// BX
1213bool
1214EmulateInstructionARM::EmulateBXRm (ARMEncoding encoding)
1215{
1216#if 0
1217 // ARM pseudo code...
1218 if (ConditionPassed())
1219 {
1220 EncodingSpecificOperations();
1221 BXWritePC(R[m]);
1222 }
1223#endif
1224
1225 bool success = false;
1226 const uint32_t opcode = OpcodeAsUnsigned (&success);
1227 if (!success)
1228 return false;
1229
1230 if (ConditionPassed())
1231 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001232 EmulateInstruction::Context context;
1233 context.type = EmulateInstruction::eContextAbsoluteBranchRegister;
Johnny Chenab3b3512011-02-12 00:10:51 +00001234 uint32_t Rm; // the register with the target address
1235 switch (encoding) {
1236 case eEncodingT1:
1237 Rm = Bits32(opcode, 6, 3);
Johnny Chen098ae2d2011-02-12 00:50:05 +00001238 if (InITBlock() && !LastInITBlock())
Johnny Chenab3b3512011-02-12 00:10:51 +00001239 return false;
1240 break;
1241 case eEncodingA1:
1242 Rm = Bits32(opcode, 3, 0);
1243 break;
1244 default:
1245 return false;
1246 }
1247 addr_t target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
1248 if (!success)
1249 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001250
1251 Register dwarf_reg;
1252 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm);
Johnny Chen668b4512011-02-15 21:08:58 +00001253 context.SetRegister (dwarf_reg);
1254 if (!BXWritePC(context, target))
Johnny Chenab3b3512011-02-12 00:10:51 +00001255 return false;
1256 }
1257 return true;
1258}
1259
Johnny Chen0d0148e2011-01-28 02:26:08 +00001260// Set r7 to point to some ip offset.
1261// SUB (immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001262bool
1263EmulateInstructionARM::EmulateSubR7IPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +00001264{
1265#if 0
1266 // ARM pseudo code...
1267 if (ConditionPassed())
1268 {
1269 EncodingSpecificOperations();
1270 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
1271 if d == 15 then // Can only occur for ARM encoding
1272 ALUWritePC(result); // setflags is always FALSE here
1273 else
1274 R[d] = result;
1275 if setflags then
1276 APSR.N = result<31>;
1277 APSR.Z = IsZeroBit(result);
1278 APSR.C = carry;
1279 APSR.V = overflow;
1280 }
1281#endif
1282
1283 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001284 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001285 if (!success)
1286 return false;
1287
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001288 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +00001289 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001290 const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001291 if (!success)
1292 return false;
1293 uint32_t imm32;
1294 switch (encoding) {
1295 case eEncodingA1:
1296 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
1297 break;
1298 default:
1299 return false;
1300 }
1301 addr_t ip_offset = imm32;
1302 addr_t addr = ip - ip_offset; // the adjusted ip value
1303
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001304 EmulateInstruction::Context context;
1305 context.type = EmulateInstruction::eContextRegisterPlusOffset;
1306 Register dwarf_reg;
1307 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r12);
1308 context.SetRegisterPlusOffset (dwarf_reg, -ip_offset);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001309
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001310 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +00001311 return false;
1312 }
1313 return true;
1314}
1315
1316// Set ip to point to some stack offset.
1317// SUB (SP minus immediate)
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001318bool
1319EmulateInstructionARM::EmulateSubIPSPImmediate (ARMEncoding encoding)
Johnny Chen0d0148e2011-01-28 02:26:08 +00001320{
1321#if 0
1322 // ARM pseudo code...
1323 if (ConditionPassed())
1324 {
1325 EncodingSpecificOperations();
1326 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
1327 if d == 15 then // Can only occur for ARM encoding
1328 ALUWritePC(result); // setflags is always FALSE here
1329 else
1330 R[d] = result;
1331 if setflags then
1332 APSR.N = result<31>;
1333 APSR.Z = IsZeroBit(result);
1334 APSR.C = carry;
1335 APSR.V = overflow;
1336 }
1337#endif
1338
1339 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001340 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001341 if (!success)
1342 return false;
1343
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001344 if (ConditionPassed())
Johnny Chen0d0148e2011-01-28 02:26:08 +00001345 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001346 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001347 if (!success)
1348 return false;
1349 uint32_t imm32;
1350 switch (encoding) {
1351 case eEncodingA1:
1352 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
1353 break;
1354 default:
1355 return false;
1356 }
1357 addr_t sp_offset = imm32;
1358 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
1359
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001360 EmulateInstruction::Context context;
1361 context.type = EmulateInstruction::eContextRegisterPlusOffset;
1362 Register dwarf_reg;
1363 dwarf_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
1364 context.SetRegisterPlusOffset (dwarf_reg, -sp_offset);
Johnny Chen0d0148e2011-01-28 02:26:08 +00001365
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001366 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr))
Johnny Chen0d0148e2011-01-28 02:26:08 +00001367 return false;
1368 }
1369 return true;
1370}
1371
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001372// A sub operation to adjust the SP -- allocate space for local storage.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001373bool
1374EmulateInstructionARM::EmulateSubSPImmdiate (ARMEncoding encoding)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001375{
1376#if 0
1377 // ARM pseudo code...
1378 if (ConditionPassed())
1379 {
1380 EncodingSpecificOperations();
1381 (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’);
1382 if d == 15 then // Can only occur for ARM encoding
Johnny Chen799dfd02011-01-26 23:14:33 +00001383 ALUWritePC(result); // setflags is always FALSE here
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001384 else
1385 R[d] = result;
1386 if setflags then
1387 APSR.N = result<31>;
1388 APSR.Z = IsZeroBit(result);
1389 APSR.C = carry;
1390 APSR.V = overflow;
1391 }
1392#endif
1393
1394 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001395 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001396 if (!success)
1397 return false;
1398
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001399 if (ConditionPassed())
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001400 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001401 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001402 if (!success)
1403 return false;
1404 uint32_t imm32;
1405 switch (encoding) {
Johnny Chene4455022011-01-26 00:08:59 +00001406 case eEncodingT1:
1407 imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32)
Johnny Chen60c0d622011-01-25 23:49:39 +00001408 case eEncodingT2:
1409 imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8)
1410 break;
1411 case eEncodingT3:
1412 imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32)
1413 break;
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001414 case eEncodingA1:
Johnny Chen60c0d622011-01-25 23:49:39 +00001415 imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12)
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001416 break;
1417 default:
1418 return false;
1419 }
1420 addr_t sp_offset = imm32;
1421 addr_t addr = sp - sp_offset; // the adjusted stack pointer value
1422
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001423 EmulateInstruction::Context context;
1424 context.type = EmulateInstruction::eContextAdjustStackPointer;
1425 context.SetImmediateSigned (-sp_offset);
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001426
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001427 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr))
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00001428 return false;
1429 }
1430 return true;
1431}
1432
Johnny Chen08c25e82011-01-31 18:02:28 +00001433// A store operation to the stack that also updates the SP.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001434bool
1435EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding)
Johnny Chence1ca772011-01-25 01:13:00 +00001436{
1437#if 0
1438 // ARM pseudo code...
1439 if (ConditionPassed())
1440 {
1441 EncodingSpecificOperations();
1442 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
1443 address = if index then offset_addr else R[n];
1444 MemU[address,4] = if t == 15 then PCStoreValue() else R[t];
1445 if wback then R[n] = offset_addr;
1446 }
1447#endif
1448
1449 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001450 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chence1ca772011-01-25 01:13:00 +00001451 if (!success)
1452 return false;
1453
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001454 if (ConditionPassed())
Johnny Chence1ca772011-01-25 01:13:00 +00001455 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001456 const uint32_t addr_byte_size = GetAddressByteSize();
1457 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001458 if (!success)
1459 return false;
Johnny Chen91d99862011-01-25 19:07:04 +00001460 uint32_t Rt; // the source register
Johnny Chence1ca772011-01-25 01:13:00 +00001461 uint32_t imm12;
1462 switch (encoding) {
1463 case eEncodingA1:
Johnny Chen108d5aa2011-01-26 01:00:55 +00001464 Rt = Bits32(opcode, 15, 12);
1465 imm12 = Bits32(opcode, 11, 0);
Johnny Chence1ca772011-01-25 01:13:00 +00001466 break;
1467 default:
1468 return false;
1469 }
1470 addr_t sp_offset = imm12;
1471 addr_t addr = sp - sp_offset;
1472
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001473 EmulateInstruction::Context context;
1474 context.type = EmulateInstruction::eContextPushRegisterOnStack;
1475 Register dwarf_reg;
1476 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chen91d99862011-01-25 19:07:04 +00001477 if (Rt != 15)
Johnny Chence1ca772011-01-25 01:13:00 +00001478 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001479 dwarf_reg.num = dwarf_r0 + Rt;
1480 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
1481 uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001482 if (!success)
1483 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001484 if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +00001485 return false;
1486 }
1487 else
1488 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001489 dwarf_reg.num = dwarf_pc;
1490 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001491 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chence1ca772011-01-25 01:13:00 +00001492 if (!success)
1493 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001494 if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size))
Johnny Chence1ca772011-01-25 01:13:00 +00001495 return false;
1496 }
1497
1498 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001499 context.SetImmediateSigned (-sp_offset);
Johnny Chence1ca772011-01-25 01:13:00 +00001500
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001501 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chence1ca772011-01-25 01:13:00 +00001502 return false;
1503 }
1504 return true;
1505}
1506
Johnny Chen08c25e82011-01-31 18:02:28 +00001507// Vector Push stores multiple extension registers to the stack.
1508// It also updates SP to point to the start of the stored data.
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001509bool
1510EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding)
Johnny Chen799dfd02011-01-26 23:14:33 +00001511{
1512#if 0
1513 // ARM pseudo code...
1514 if (ConditionPassed())
1515 {
1516 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
1517 address = SP - imm32;
1518 SP = SP - imm32;
1519 if single_regs then
1520 for r = 0 to regs-1
1521 MemA[address,4] = S[d+r]; address = address+4;
1522 else
1523 for r = 0 to regs-1
1524 // Store as two word-aligned words in the correct order for current endianness.
1525 MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>;
1526 MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>;
1527 address = address+8;
1528 }
1529#endif
1530
1531 bool success = false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001532 const uint32_t opcode = OpcodeAsUnsigned (&success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001533 if (!success)
1534 return false;
1535
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001536 if (ConditionPassed())
Johnny Chen799dfd02011-01-26 23:14:33 +00001537 {
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001538 const uint32_t addr_byte_size = GetAddressByteSize();
1539 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001540 if (!success)
1541 return false;
1542 bool single_regs;
Johnny Chen587a0a42011-02-01 18:35:28 +00001543 uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register
Johnny Chen799dfd02011-01-26 23:14:33 +00001544 uint32_t imm32; // stack offset
1545 uint32_t regs; // number of registers
1546 switch (encoding) {
1547 case eEncodingT1:
1548 case eEncodingA1:
1549 single_regs = false;
Johnny Chenbd599902011-02-10 21:39:01 +00001550 d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12);
Johnny Chen799dfd02011-01-26 23:14:33 +00001551 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1552 // If UInt(imm8) is odd, see "FSTMX".
1553 regs = Bits32(opcode, 7, 0) / 2;
1554 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1555 if (regs == 0 || regs > 16 || (d + regs) > 32)
1556 return false;
1557 break;
1558 case eEncodingT2:
1559 case eEncodingA2:
1560 single_regs = true;
Johnny Chenbd599902011-02-10 21:39:01 +00001561 d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22);
Johnny Chen799dfd02011-01-26 23:14:33 +00001562 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1563 regs = Bits32(opcode, 7, 0);
1564 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1565 if (regs == 0 || regs > 16 || (d + regs) > 32)
1566 return false;
1567 break;
1568 default:
1569 return false;
1570 }
1571 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
1572 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
1573 addr_t sp_offset = imm32;
1574 addr_t addr = sp - sp_offset;
1575 uint32_t i;
1576
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001577 EmulateInstruction::Context context;
1578 context.type = EmulateInstruction::eContextPushRegisterOnStack;
1579 Register dwarf_reg;
1580 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chen799dfd02011-01-26 23:14:33 +00001581 for (i=d; i<regs; ++i)
1582 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001583 dwarf_reg.num = start_reg + i;
1584 context.SetRegisterPlusOffset ( dwarf_reg, addr - sp);
Johnny Chen799dfd02011-01-26 23:14:33 +00001585 // uint64_t to accommodate 64-bit registers.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001586 uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success);
Johnny Chen799dfd02011-01-26 23:14:33 +00001587 if (!success)
1588 return false;
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001589 if (!WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size))
Johnny Chen799dfd02011-01-26 23:14:33 +00001590 return false;
1591 addr += reg_byte_size;
1592 }
1593
1594 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001595 context.SetImmediateSigned (-sp_offset);
Johnny Chen799dfd02011-01-26 23:14:33 +00001596
Greg Clayton2b8e8b02011-02-01 00:49:32 +00001597 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset))
Johnny Chen799dfd02011-01-26 23:14:33 +00001598 return false;
1599 }
1600 return true;
1601}
1602
Johnny Chen587a0a42011-02-01 18:35:28 +00001603// Vector Pop loads multiple extension registers from the stack.
1604// It also updates SP to point just above the loaded data.
1605bool
1606EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding)
1607{
1608#if 0
1609 // ARM pseudo code...
1610 if (ConditionPassed())
1611 {
1612 EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13);
1613 address = SP;
1614 SP = SP + imm32;
1615 if single_regs then
1616 for r = 0 to regs-1
1617 S[d+r] = MemA[address,4]; address = address+4;
1618 else
1619 for r = 0 to regs-1
1620 word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8;
1621 // Combine the word-aligned words in the correct order for current endianness.
1622 D[d+r] = if BigEndian() then word1:word2 else word2:word1;
1623 }
1624#endif
1625
1626 bool success = false;
1627 const uint32_t opcode = OpcodeAsUnsigned (&success);
1628 if (!success)
1629 return false;
1630
1631 if (ConditionPassed())
1632 {
1633 const uint32_t addr_byte_size = GetAddressByteSize();
1634 const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success);
1635 if (!success)
1636 return false;
1637 bool single_regs;
1638 uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register
1639 uint32_t imm32; // stack offset
1640 uint32_t regs; // number of registers
1641 switch (encoding) {
1642 case eEncodingT1:
1643 case eEncodingA1:
1644 single_regs = false;
Johnny Chenbd599902011-02-10 21:39:01 +00001645 d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12);
Johnny Chen587a0a42011-02-01 18:35:28 +00001646 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1647 // If UInt(imm8) is odd, see "FLDMX".
1648 regs = Bits32(opcode, 7, 0) / 2;
1649 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1650 if (regs == 0 || regs > 16 || (d + regs) > 32)
1651 return false;
1652 break;
1653 case eEncodingT2:
1654 case eEncodingA2:
1655 single_regs = true;
Johnny Chenbd599902011-02-10 21:39:01 +00001656 d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22);
Johnny Chen587a0a42011-02-01 18:35:28 +00001657 imm32 = Bits32(opcode, 7, 0) * addr_byte_size;
1658 regs = Bits32(opcode, 7, 0);
1659 // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE;
1660 if (regs == 0 || regs > 16 || (d + regs) > 32)
1661 return false;
1662 break;
1663 default:
1664 return false;
1665 }
1666 uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0;
1667 uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2;
1668 addr_t sp_offset = imm32;
1669 addr_t addr = sp;
1670 uint32_t i;
1671 uint64_t data; // uint64_t to accomodate 64-bit registers.
1672
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001673 EmulateInstruction::Context context;
1674 context.type = EmulateInstruction::eContextPopRegisterOffStack;
1675 Register dwarf_reg;
1676 dwarf_reg.SetRegister (eRegisterKindDWARF, 0);
Johnny Chen587a0a42011-02-01 18:35:28 +00001677 for (i=d; i<regs; ++i)
1678 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001679 dwarf_reg.num = start_reg + i;
1680 context.SetRegisterPlusOffset (dwarf_reg, addr - sp);
Johnny Chen587a0a42011-02-01 18:35:28 +00001681 data = ReadMemoryUnsigned(context, addr, reg_byte_size, 0, &success);
1682 if (!success)
1683 return false;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001684 if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data))
Johnny Chen587a0a42011-02-01 18:35:28 +00001685 return false;
1686 addr += reg_byte_size;
1687 }
1688
1689 context.type = EmulateInstruction::eContextAdjustStackPointer;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001690 context.SetImmediateSigned (sp_offset);
Johnny Chen587a0a42011-02-01 18:35:28 +00001691
1692 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset))
1693 return false;
1694 }
1695 return true;
1696}
1697
Johnny Chenb77be412011-02-04 00:40:18 +00001698// SVC (previously SWI)
1699bool
1700EmulateInstructionARM::EmulateSVC (ARMEncoding encoding)
1701{
1702#if 0
1703 // ARM pseudo code...
1704 if (ConditionPassed())
1705 {
1706 EncodingSpecificOperations();
1707 CallSupervisor();
1708 }
1709#endif
1710
1711 bool success = false;
1712 const uint32_t opcode = OpcodeAsUnsigned (&success);
1713 if (!success)
1714 return false;
1715
1716 if (ConditionPassed())
1717 {
1718 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1719 addr_t lr; // next instruction address
1720 if (!success)
1721 return false;
1722 uint32_t imm32; // the immediate constant
1723 uint32_t mode; // ARM or Thumb mode
1724 switch (encoding) {
1725 case eEncodingT1:
1726 lr = (pc + 2) | 1u; // return address
1727 imm32 = Bits32(opcode, 7, 0);
1728 mode = eModeThumb;
1729 break;
1730 case eEncodingA1:
1731 lr = pc + 4; // return address
1732 imm32 = Bits32(opcode, 23, 0);
1733 mode = eModeARM;
1734 break;
1735 default:
1736 return false;
1737 }
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001738
1739 EmulateInstruction::Context context;
1740 context.type = EmulateInstruction::eContextSupervisorCall;
1741 context.SetModeAndImmediate (mode, imm32);
Johnny Chenb77be412011-02-04 00:40:18 +00001742 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr))
1743 return false;
1744 }
1745 return true;
1746}
1747
Johnny Chenc315f862011-02-05 00:46:10 +00001748// If Then makes up to four following instructions (the IT block) conditional.
1749bool
1750EmulateInstructionARM::EmulateIT (ARMEncoding encoding)
1751{
1752#if 0
1753 // ARM pseudo code...
1754 EncodingSpecificOperations();
1755 ITSTATE.IT<7:0> = firstcond:mask;
1756#endif
1757
1758 bool success = false;
1759 const uint32_t opcode = OpcodeAsUnsigned (&success);
1760 if (!success)
1761 return false;
1762
1763 m_it_session.InitIT(Bits32(opcode, 7, 0));
1764 return true;
1765}
1766
Johnny Chen3b620b32011-02-07 20:11:47 +00001767// Branch causes a branch to a target address.
1768bool
1769EmulateInstructionARM::EmulateB (ARMEncoding encoding)
1770{
1771#if 0
1772 // ARM pseudo code...
1773 if (ConditionPassed())
1774 {
1775 EncodingSpecificOperations();
1776 BranchWritePC(PC + imm32);
1777 }
1778#endif
1779
1780 bool success = false;
1781 const uint32_t opcode = OpcodeAsUnsigned (&success);
1782 if (!success)
1783 return false;
1784
Johnny Chen9ee056b2011-02-08 00:06:35 +00001785 if (ConditionPassed())
1786 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001787 EmulateInstruction::Context context;
1788 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
Johnny Chen9ee056b2011-02-08 00:06:35 +00001789 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001790 if (!success)
1791 return false;
Johnny Chen53ebab72011-02-08 23:21:57 +00001792 addr_t target; // target address
Johnny Chen9ee056b2011-02-08 00:06:35 +00001793 int32_t imm32; // PC-relative offset
1794 switch (encoding) {
1795 case eEncodingT1:
1796 // The 'cond' field is handled in EmulateInstructionARM::CurrentCond().
1797 imm32 = llvm::SignExtend32<9>(Bits32(opcode, 7, 0) << 1);
1798 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001799 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001800 break;
1801 case eEncodingT2:
1802 imm32 = llvm::SignExtend32<12>(Bits32(opcode, 10, 0));
1803 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001804 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001805 break;
1806 case eEncodingT3:
1807 // The 'cond' field is handled in EmulateInstructionARM::CurrentCond().
1808 {
Johnny Chenbd599902011-02-10 21:39:01 +00001809 uint32_t S = Bit32(opcode, 26);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001810 uint32_t imm6 = Bits32(opcode, 21, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001811 uint32_t J1 = Bit32(opcode, 13);
1812 uint32_t J2 = Bit32(opcode, 11);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001813 uint32_t imm11 = Bits32(opcode, 10, 0);
Johnny Chen53ebab72011-02-08 23:21:57 +00001814 uint32_t imm21 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001815 imm32 = llvm::SignExtend32<21>(imm21);
1816 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001817 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001818 break;
1819 }
1820 case eEncodingT4:
1821 {
Johnny Chenbd599902011-02-10 21:39:01 +00001822 uint32_t S = Bit32(opcode, 26);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001823 uint32_t imm10 = Bits32(opcode, 25, 16);
Johnny Chenbd599902011-02-10 21:39:01 +00001824 uint32_t J1 = Bit32(opcode, 13);
1825 uint32_t J2 = Bit32(opcode, 11);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001826 uint32_t imm11 = Bits32(opcode, 10, 0);
1827 uint32_t I1 = !(J1 ^ S);
1828 uint32_t I2 = !(J2 ^ S);
Johnny Chen53ebab72011-02-08 23:21:57 +00001829 uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001830 imm32 = llvm::SignExtend32<25>(imm25);
1831 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001832 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001833 break;
1834 }
1835 case eEncodingA1:
1836 imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2);
1837 target = pc + 8 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001838 context.SetModeAndImmediateSigned (eModeARM, 8 + imm32);
Johnny Chen9ee056b2011-02-08 00:06:35 +00001839 break;
1840 default:
1841 return false;
1842 }
1843 if (!BranchWritePC(context, target))
1844 return false;
1845 }
1846 return true;
Johnny Chen3b620b32011-02-07 20:11:47 +00001847}
1848
Johnny Chen53ebab72011-02-08 23:21:57 +00001849// Compare and Branch on Nonzero and Compare and Branch on Zero compare the value in a register with
1850// zero and conditionally branch forward a constant value. They do not affect the condition flags.
1851// CBNZ, CBZ
1852bool
1853EmulateInstructionARM::EmulateCB (ARMEncoding encoding)
1854{
1855#if 0
1856 // ARM pseudo code...
1857 EncodingSpecificOperations();
1858 if nonzero ^ IsZero(R[n]) then
1859 BranchWritePC(PC + imm32);
1860#endif
1861
1862 bool success = false;
1863 const uint32_t opcode = OpcodeAsUnsigned (&success);
1864 if (!success)
1865 return false;
1866
1867 // Read the register value from the operand register Rn.
1868 uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Bits32(opcode, 2, 0), 0, &success);
1869 if (!success)
1870 return false;
1871
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001872 EmulateInstruction::Context context;
1873 context.type = EmulateInstruction::eContextRelativeBranchImmediate;
Johnny Chen53ebab72011-02-08 23:21:57 +00001874 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1875 if (!success)
1876 return false;
1877
1878 addr_t target; // target address
1879 uint32_t imm32; // PC-relative offset to branch forward
1880 bool nonzero;
1881 switch (encoding) {
1882 case eEncodingT1:
Johnny Chenbd599902011-02-10 21:39:01 +00001883 imm32 = Bit32(opcode, 9) << 6 | Bits32(opcode, 7, 3) << 1;
Johnny Chen53ebab72011-02-08 23:21:57 +00001884 nonzero = BitIsSet(opcode, 11);
1885 target = pc + 4 + imm32;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001886 context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32);
Johnny Chen53ebab72011-02-08 23:21:57 +00001887 break;
1888 default:
1889 return false;
1890 }
1891 if (nonzero ^ (reg_val == 0))
1892 if (!BranchWritePC(context, target))
1893 return false;
1894
1895 return true;
1896}
1897
Johnny Chen26863dc2011-02-09 23:43:29 +00001898// ADD <Rdn>, <Rm>
1899// where <Rdn> the destination register is also the first operand register
1900// and <Rm> is the second operand register.
1901bool
1902EmulateInstructionARM::EmulateAddRdnRm (ARMEncoding encoding)
1903{
1904#if 0
1905 // ARM pseudo code...
1906 if ConditionPassed() then
1907 EncodingSpecificOperations();
1908 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
1909 (result, carry, overflow) = AddWithCarry(R[n], shifted, '0');
1910 if d == 15 then
1911 ALUWritePC(result); // setflags is always FALSE here
1912 else
1913 R[d] = result;
1914 if setflags then
1915 APSR.N = result<31>;
1916 APSR.Z = IsZeroBit(result);
1917 APSR.C = carry;
1918 APSR.V = overflow;
1919#endif
1920
1921 bool success = false;
1922 const uint32_t opcode = OpcodeAsUnsigned (&success);
1923 if (!success)
1924 return false;
1925
1926 if (ConditionPassed())
1927 {
1928 uint32_t Rd, Rn, Rm;
1929 //bool setflags = false;
1930 switch (encoding)
1931 {
1932 case eEncodingT2:
1933 // setflags = FALSE
Johnny Chenbd599902011-02-10 21:39:01 +00001934 Rd = Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0);
Johnny Chen26863dc2011-02-09 23:43:29 +00001935 Rm = Bits32(opcode, 6, 3);
1936 if (Rn == 15 && Rm == 15)
1937 return false;
1938 break;
1939 default:
1940 return false;
1941 }
1942
1943 int32_t result, val1, val2;
1944 // Read the first operand.
1945 if (Rn == 15)
1946 val1 = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1947 else
1948 val1 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
1949 if (!success)
1950 return false;
1951
1952 // Read the second operand.
1953 if (Rm == 15)
1954 val2 = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
1955 else
1956 val2 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
1957 if (!success)
1958 return false;
1959
1960 result = val1 + val2;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00001961
1962 EmulateInstruction::Context context;
1963 context.type = EmulateInstruction::eContextImmediate;
1964 context.SetNoArgs ();
Johnny Chen26863dc2011-02-09 23:43:29 +00001965
1966 if (Rd == 15)
1967 {
Johnny Chen668b4512011-02-15 21:08:58 +00001968 if (!ALUWritePC (context, result))
Johnny Chen26863dc2011-02-09 23:43:29 +00001969 return false;
1970 }
1971 else
1972 {
Johnny Chen33bf6ab2011-02-14 20:39:01 +00001973 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
Johnny Chen26863dc2011-02-09 23:43:29 +00001974 return false;
1975 }
1976 }
1977 return true;
1978}
1979
Johnny Chene4a4d302011-02-11 21:53:58 +00001980// CMP (immediate)
Johnny Chend4dc4442011-02-11 02:02:56 +00001981bool
1982EmulateInstructionARM::EmulateCmpRnImm (ARMEncoding encoding)
1983{
1984#if 0
1985 // ARM pseudo code...
1986 if ConditionPassed() then
1987 EncodingSpecificOperations();
1988 (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1');
1989 APSR.N = result<31>;
1990 APSR.Z = IsZeroBit(result);
1991 APSR.C = carry;
1992 APSR.V = overflow;
1993#endif
1994
1995 bool success = false;
1996 const uint32_t opcode = OpcodeAsUnsigned (&success);
1997 if (!success)
1998 return false;
1999
2000 uint32_t Rn; // the first operand
2001 uint32_t imm32; // the immediate value to be compared with
2002 switch (encoding) {
2003 case eEncodingT1:
2004 Rn = Bits32(opcode, 10, 8);
2005 imm32 = Bits32(opcode, 7, 0);
2006 break;
2007 default:
2008 return false;
2009 }
2010 // Read the register value from the operand register Rn.
2011 uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
2012 if (!success)
2013 return false;
2014
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002015 EmulateInstruction::Context context;
2016 context.type = EmulateInstruction::eContextImmediate;
2017 context.SetNoArgs ();
2018
Johnny Chend4dc4442011-02-11 02:02:56 +00002019 AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1);
2020 m_new_inst_cpsr = m_inst_cpsr;
2021 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(res.result, CPSR_N));
2022 SetBit32(m_new_inst_cpsr, CPSR_Z, res.result == 0 ? 1 : 0);
2023 SetBit32(m_new_inst_cpsr, CPSR_C, res.carry_out);
2024 SetBit32(m_new_inst_cpsr, CPSR_V, res.overflow);
2025 if (m_new_inst_cpsr != m_inst_cpsr)
2026 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002027 EmulateInstruction::Context context;
2028 context.type = EmulateInstruction::eContextImmediate;
2029 context.SetNoArgs ();
Johnny Chend4dc4442011-02-11 02:02:56 +00002030 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
2031 return false;
2032 }
2033 return true;
2034}
2035
Johnny Chene4a4d302011-02-11 21:53:58 +00002036// CMP (register)
2037bool
2038EmulateInstructionARM::EmulateCmpRnRm (ARMEncoding encoding)
2039{
2040#if 0
2041 // ARM pseudo code...
2042 if ConditionPassed() then
2043 EncodingSpecificOperations();
2044 shifted = Shift(R[m], shift_t, shift_n, APSR.C);
2045 (result, carry, overflow) = AddWithCarry(R[n], NOT(shifted), '1');
2046 APSR.N = result<31>;
2047 APSR.Z = IsZeroBit(result);
2048 APSR.C = carry;
2049 APSR.V = overflow;
2050#endif
2051
2052 bool success = false;
2053 const uint32_t opcode = OpcodeAsUnsigned (&success);
2054 if (!success)
2055 return false;
2056
2057 uint32_t Rn; // the first operand
2058 uint32_t Rm; // the second operand
2059 switch (encoding) {
2060 case eEncodingT1:
2061 Rn = Bits32(opcode, 2, 0);
2062 Rm = Bits32(opcode, 5, 3);
2063 break;
2064 case eEncodingT2:
2065 Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0);
2066 Rm = Bits32(opcode, 6, 3);
2067 if (Rn < 8 && Rm < 8)
2068 return false;
2069 if (Rn == 15 || Rm == 15)
2070 return false;
2071 break;
2072 default:
2073 return false;
2074 }
2075 // Read the register value from register Rn.
2076 uint32_t reg_val1 = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
2077 if (!success)
2078 return false;
2079 // Read the register value from register Rm.
2080 // The register value is not being shifted since we don't handle ARM for now.
2081 uint32_t reg_val2 = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
2082 if (!success)
2083 return false;
2084
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002085 EmulateInstruction::Context context;
2086 context.type = EmulateInstruction::eContextImmediate;
2087 context.SetNoArgs();
2088
Johnny Chene4a4d302011-02-11 21:53:58 +00002089 AddWithCarryResult res = AddWithCarry(reg_val1, reg_val2, 1);
2090 m_new_inst_cpsr = m_inst_cpsr;
2091 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(res.result, CPSR_N));
2092 SetBit32(m_new_inst_cpsr, CPSR_Z, res.result == 0 ? 1 : 0);
2093 SetBit32(m_new_inst_cpsr, CPSR_C, res.carry_out);
2094 SetBit32(m_new_inst_cpsr, CPSR_V, res.overflow);
2095 if (m_new_inst_cpsr != m_inst_cpsr)
2096 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002097 EmulateInstruction::Context context;
2098 context.type = EmulateInstruction::eContextImmediate;
2099 context.SetNoArgs ();
Johnny Chene4a4d302011-02-11 21:53:58 +00002100 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
2101 return false;
2102 }
2103 return true;
2104}
2105
Johnny Chen82f16aa2011-02-15 20:10:55 +00002106// Arithmetic Shift Right (immediate) shifts a register value right by an immediate number of bits,
2107// shifting in copies of its sign bit, and writes the result to the destination register. It can
2108// optionally update the condition flags based on the result.
2109bool
2110EmulateInstructionARM::EmulateASRImm (ARMEncoding encoding)
2111{
2112#if 0
2113 // ARM pseudo code...
2114 if ConditionPassed() then
2115 EncodingSpecificOperations();
2116 (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C);
2117 if d == 15 then // Can only occur for ARM encoding
2118 ALUWritePC(result); // setflags is always FALSE here
2119 else
2120 R[d] = result;
2121 if setflags then
2122 APSR.N = result<31>;
2123 APSR.Z = IsZeroBit(result);
2124 APSR.C = carry;
2125 // APSR.V unchanged
2126#endif
2127
2128 bool success = false;
2129 const uint32_t opcode = OpcodeAsUnsigned (&success);
2130 if (!success)
2131 return false;
2132
2133 if (ConditionPassed())
2134 {
2135 uint32_t Rd; // the destination register
2136 uint32_t Rm; // the first operand register
2137 uint32_t imm5; // encoding for the shift amount
2138 uint32_t carry; // the carry bit after the shift operation
2139 bool setflags;
2140 switch (encoding) {
2141 case eEncodingT1:
2142 Rd = Bits32(opcode, 2, 0);
2143 Rm = Bits32(opcode, 5, 3);
2144 setflags = !InITBlock();
2145 imm5 = Bits32(opcode, 10, 6);
2146 break;
2147 case eEncodingT2:
2148 Rd = Bits32(opcode, 11, 8);
2149 Rm = Bits32(opcode, 3, 0);
2150 setflags = BitIsSet(opcode, 20);
2151 imm5 = Bits32(opcode, 14, 12) << 2 | Bits32(opcode, 7, 6);
2152 if (BadReg(Rd) || BadReg(Rm))
2153 return false;
2154 break;
2155 case eEncodingA1:
2156 Rd = Bits32(opcode, 15, 12);
2157 Rm = Bits32(opcode, 3, 0);
2158 setflags = BitIsSet(opcode, 20);
2159 imm5 = Bits32(opcode, 11, 7);
2160 break;
2161 default:
2162 return false;
2163 }
2164
2165 // Get the first operand.
2166 uint32_t value = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success);
2167 if (!success)
2168 return false;
2169
2170 // Decode the shift amount.
2171 uint32_t amt = DecodeImmShift(SRType_ASR, imm5);
2172
2173 uint32_t result = Shift_C(value, SRType_ASR, amt, Bit32(m_inst_cpsr, CPSR_C), carry);
2174
2175 // The context specifies that an immediate is to be moved into Rd.
2176 EmulateInstruction::Context context;
2177 context.type = EmulateInstruction::eContextImmediate;
2178 context.SetNoArgs ();
Johnny Chen82f16aa2011-02-15 20:10:55 +00002179
2180 if (Rd == 15)
2181 {
Johnny Chen668b4512011-02-15 21:08:58 +00002182 if (!ALUWritePC (context, result))
Johnny Chen82f16aa2011-02-15 20:10:55 +00002183 return false;
2184 }
2185 else
2186 {
2187 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result))
2188 return false;
2189 if (setflags)
2190 {
2191 m_new_inst_cpsr = m_inst_cpsr;
2192 SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(result, CPSR_N));
2193 SetBit32(m_new_inst_cpsr, CPSR_Z, result == 0 ? 1 : 0);
2194 SetBit32(m_new_inst_cpsr, CPSR_C, carry);
2195 if (m_new_inst_cpsr != m_inst_cpsr)
2196 {
2197 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
2198 return false;
2199 }
2200 }
2201 }
2202 }
2203 return true;
2204}
2205
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002206// LDM loads multiple registers from consecutive memory locations, using an
Caroline Tice713c2662011-02-11 17:59:55 +00002207// address from a base register. Optionally the address just above the highest of those locations
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002208// can be written back to the base register.
2209bool
2210EmulateInstructionARM::EmulateLDM (ARMEncoding encoding)
2211{
2212#if 0
2213 // ARM pseudo code...
2214 if ConditionPassed()
2215 EncodingSpecificOperations(); NullCheckIfThumbEE (n);
2216 address = R[n];
2217
2218 for i = 0 to 14
2219 if registers<i> == '1' then
2220 R[i] = MemA[address, 4]; address = address + 4;
2221 if registers<15> == '1' then
2222 LoadWritePC (MemA[address, 4]);
2223
2224 if wback && registers<n> == '0' then R[n] = R[n] + 4 * BitCount (registers);
2225 if wback && registers<n> == '1' then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2226
2227#endif
2228
2229 bool success = false;
2230 const uint32_t opcode = OpcodeAsUnsigned (&success);
2231 if (!success)
2232 return false;
2233
2234 if (ConditionPassed())
2235 {
2236 uint32_t n;
2237 uint32_t registers = 0;
2238 bool wback;
2239 const uint32_t addr_byte_size = GetAddressByteSize();
2240 switch (encoding)
2241 {
2242 case eEncodingT1:
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002243 // n = UInt(Rn); registers = ’00000000’:register_list; wback = (registers<n> == ’0’);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002244 n = Bits32 (opcode, 10, 8);
2245 registers = Bits32 (opcode, 7, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002246 registers = registers & 0x00ff; // Make sure the top 8 bits are zeros.
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002247 wback = BitIsClear (registers, n);
2248 // if BitCount(registers) < 1 then UNPREDICTABLE;
2249 if (BitCount(registers) < 1)
2250 return false;
2251 break;
2252 case eEncodingT2:
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002253 // if W == ’1’ && Rn == ’1101’ then SEE POP;
2254 // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002255 n = Bits32 (opcode, 19, 16);
2256 registers = Bits32 (opcode, 15, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002257 registers = registers & 0xdfff; // Make sure bit 13 is zero.
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002258 wback = BitIsSet (opcode, 21);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002259
2260 // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE;
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002261 if ((n == 15)
2262 || (BitCount (registers) < 2)
2263 || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15)))
2264 return false;
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002265
2266 // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +00002267 if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock())
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002268 return false;
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002269
2270 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002271 if (wback
2272 && BitIsSet (registers, n))
2273 return false;
2274 break;
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002275
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002276 case eEncodingA1:
2277 n = Bits32 (opcode, 19, 16);
2278 registers = Bits32 (opcode, 15, 0);
2279 wback = BitIsSet (opcode, 21);
2280 if ((n == 15)
2281 || (BitCount (registers) < 1))
2282 return false;
2283 break;
2284 default:
2285 return false;
2286 }
2287
2288 int32_t offset = 0;
2289 const addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2290 if (!success)
2291 return false;
Caroline Tice85aab332011-02-08 23:56:10 +00002292
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002293 EmulateInstruction::Context context;
2294 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2295 Register dwarf_reg;
2296 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2297 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002298
2299 for (int i = 0; i < 14; ++i)
2300 {
2301 if (BitIsSet (registers, i))
2302 {
Caroline Tice85aab332011-02-08 23:56:10 +00002303 context.type = EmulateInstruction::eContextRegisterPlusOffset;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002304 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002305 if (wback && (n == 13)) // Pop Instruction
2306 context.type = EmulateInstruction::eContextPopRegisterOffStack;
2307
2308 // R[i] = MemA [address, 4]; address = address + 4;
2309 uint32_t data = ReadMemoryUnsigned (context, base_address + offset, addr_byte_size, 0, &success);
2310 if (!success)
2311 return false;
2312
2313 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2314 return false;
2315
2316 offset += addr_byte_size;
2317 }
2318 }
2319
2320 if (BitIsSet (registers, 15))
2321 {
2322 //LoadWritePC (MemA [address, 4]);
Caroline Tice85aab332011-02-08 23:56:10 +00002323 context.type = EmulateInstruction::eContextRegisterPlusOffset;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002324 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002325 uint32_t data = ReadMemoryUnsigned (context, base_address + offset, addr_byte_size, 0, &success);
2326 if (!success)
2327 return false;
Johnny Chene62b50d2011-02-09 22:02:17 +00002328 // In ARMv5T and above, this is an interworking branch.
Johnny Chen668b4512011-02-15 21:08:58 +00002329 if (!LoadWritePC(context, data))
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002330 return false;
2331 }
2332
2333 if (wback && BitIsClear (registers, n))
2334 {
Caroline Ticefa172202011-02-11 22:49:54 +00002335 // R[n] = R[n] + 4 * BitCount (registers)
2336 int32_t offset = addr_byte_size * BitCount (registers);
2337 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002338 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002339
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002340 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, base_address + offset))
2341 return false;
2342 }
2343 if (wback && BitIsSet (registers, n))
2344 // R[n] bits(32) UNKNOWN;
Caroline Tice713c2662011-02-11 17:59:55 +00002345 return WriteBits32Unknown (n);
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002346 }
2347 return true;
2348}
Caroline Tice713c2662011-02-11 17:59:55 +00002349
2350// LDMDA loads multiple registers from consecutive memory locations using an address from a base registers.
2351// The consecutive memorty locations end at this address and the address just below the lowest of those locations
2352// can optionally be written back tot he base registers.
2353bool
2354EmulateInstructionARM::EmulateLDMDA (ARMEncoding encoding)
2355{
2356#if 0
2357 // ARM pseudo code...
2358 if ConditionPassed() then
2359 EncodingSpecificOperations();
2360 address = R[n] - 4*BitCount(registers) + 4;
Caroline Ticeb9f76c32011-02-08 22:24:38 +00002361
Caroline Tice713c2662011-02-11 17:59:55 +00002362 for i = 0 to 14
2363 if registers<i> == ’1’ then
2364 R[i] = MemA[address,4]; address = address + 4;
2365
2366 if registers<15> == ’1’ then
2367 LoadWritePC(MemA[address,4]);
2368
2369 if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2370 if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN;
2371#endif
2372
2373 bool success = false;
2374 const uint32_t opcode = OpcodeAsUnsigned (&success);
2375 if (!success)
2376 return false;
2377
2378 if (ConditionPassed())
2379 {
2380 uint32_t n;
2381 uint32_t registers = 0;
2382 bool wback;
2383 const uint32_t addr_byte_size = GetAddressByteSize();
2384
2385 // EncodingSpecificOperations();
2386 switch (encoding)
2387 {
2388 case eEncodingA1:
2389 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2390 n = Bits32 (opcode, 19, 16);
2391 registers = Bits32 (opcode, 15, 0);
2392 wback = BitIsSet (opcode, 21);
2393
2394 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2395 if ((n == 15) || (BitCount (registers) < 1))
2396 return false;
2397
2398 break;
2399
2400 default:
2401 return false;
2402 }
2403 // address = R[n] - 4*BitCount(registers) + 4;
2404
2405 int32_t offset = 0;
2406 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2407
2408 if (!success)
2409 return false;
2410
2411 address = address - (addr_byte_size * BitCount (registers)) + addr_byte_size;
2412
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002413 EmulateInstruction::Context context;
2414 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2415 Register dwarf_reg;
2416 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2417 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice713c2662011-02-11 17:59:55 +00002418
2419 // for i = 0 to 14
2420 for (int i = 0; i < 14; ++i)
2421 {
2422 // if registers<i> == ’1’ then
2423 if (BitIsSet (registers, i))
2424 {
2425 // R[i] = MemA[address,4]; address = address + 4;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002426 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice713c2662011-02-11 17:59:55 +00002427 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2428 if (!success)
2429 return false;
2430 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2431 return false;
2432 offset += addr_byte_size;
2433 }
2434 }
2435
2436 // if registers<15> == ’1’ then
2437 // LoadWritePC(MemA[address,4]);
2438 if (BitIsSet (registers, 15))
2439 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002440 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice713c2662011-02-11 17:59:55 +00002441 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2442 if (!success)
2443 return false;
Johnny Chen44c10f02011-02-11 19:37:03 +00002444 // In ARMv5T and above, this is an interworking branch.
Johnny Chen668b4512011-02-15 21:08:58 +00002445 if (!LoadWritePC(context, data))
Caroline Tice713c2662011-02-11 17:59:55 +00002446 return false;
2447 }
2448
2449 // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2450 if (wback && BitIsClear (registers, n))
2451 {
Caroline Tice713c2662011-02-11 17:59:55 +00002452 addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2453 if (!success)
2454 return false;
Caroline Ticefa172202011-02-11 22:49:54 +00002455
2456 offset = (addr_byte_size * BitCount (registers)) * -1;
2457 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002458 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002459 addr = addr + offset;
Caroline Tice713c2662011-02-11 17:59:55 +00002460 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr))
2461 return false;
2462 }
2463
2464 // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN;
2465 if (wback && BitIsSet (registers, n))
2466 return WriteBits32Unknown (n);
2467 }
2468 return true;
2469}
2470
2471// LDMDB loads multiple registers from consecutive memory locations using an address from a base register. The
2472// consecutive memory lcoations end just below this address, and the address of the lowest of those locations can
2473// be optionally written back to the base register.
Caroline Tice0b29e242011-02-08 23:16:02 +00002474bool
2475EmulateInstructionARM::EmulateLDMDB (ARMEncoding encoding)
2476{
2477#if 0
2478 // ARM pseudo code...
2479 if ConditionPassed() then
2480 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
2481 address = R[n] - 4*BitCount(registers);
2482
2483 for i = 0 to 14
2484 if registers<i> == ’1’ then
2485 R[i] = MemA[address,4]; address = address + 4;
2486 if registers<15> == ’1’ then
2487 LoadWritePC(MemA[address,4]);
2488
2489 if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2490 if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2491#endif
2492
2493 bool success = false;
2494 const uint32_t opcode = OpcodeAsUnsigned (&success);
2495 if (!success)
2496 return false;
2497
2498 if (ConditionPassed())
2499 {
2500 uint32_t n;
2501 uint32_t registers = 0;
2502 bool wback;
2503 const uint32_t addr_byte_size = GetAddressByteSize();
2504 switch (encoding)
2505 {
2506 case eEncodingT1:
2507 // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’);
2508 n = Bits32 (opcode, 19, 16);
2509 registers = Bits32 (opcode, 15, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002510 registers = registers & 0xdfff; // Make sure bit 13 is a zero.
Caroline Tice0b29e242011-02-08 23:16:02 +00002511 wback = BitIsSet (opcode, 21);
2512
2513 // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE;
2514 if ((n == 15)
2515 || (BitCount (registers) < 2)
2516 || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15)))
2517 return false;
2518
2519 // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE;
Johnny Chen098ae2d2011-02-12 00:50:05 +00002520 if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock())
Caroline Tice0b29e242011-02-08 23:16:02 +00002521 return false;
2522
2523 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
2524 if (wback && BitIsSet (registers, n))
2525 return false;
2526
2527 break;
2528
2529 case eEncodingA1:
2530 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2531 n = Bits32 (opcode, 19, 16);
2532 registers = Bits32 (opcode, 15, 0);
2533 wback = BitIsSet (opcode, 21);
2534
2535 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2536 if ((n == 15) || (BitCount (registers) < 1))
2537 return false;
2538
2539 break;
2540
2541 default:
2542 return false;
2543 }
2544
Caroline Tice713c2662011-02-11 17:59:55 +00002545 // address = R[n] - 4*BitCount(registers);
2546
Caroline Tice0b29e242011-02-08 23:16:02 +00002547 int32_t offset = 0;
Caroline Tice713c2662011-02-11 17:59:55 +00002548 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2549
2550 if (!success)
2551 return false;
2552
2553 address = address - (addr_byte_size * BitCount (registers));
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002554 EmulateInstruction::Context context;
2555 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2556 Register dwarf_reg;
2557 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2558 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice0b29e242011-02-08 23:16:02 +00002559
2560 for (int i = 0; i < 14; ++i)
2561 {
2562 if (BitIsSet (registers, i))
2563 {
2564 // R[i] = MemA[address,4]; address = address + 4;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002565 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice0b29e242011-02-08 23:16:02 +00002566 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2567 if (!success)
2568 return false;
2569
2570 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2571 return false;
2572
2573 offset += addr_byte_size;
2574 }
2575 }
2576
2577 // if registers<15> == ’1’ then
2578 // LoadWritePC(MemA[address,4]);
2579 if (BitIsSet (registers, 15))
2580 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002581 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice0b29e242011-02-08 23:16:02 +00002582 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2583 if (!success)
2584 return false;
Johnny Chene62b50d2011-02-09 22:02:17 +00002585 // In ARMv5T and above, this is an interworking branch.
Johnny Chen668b4512011-02-15 21:08:58 +00002586 if (!LoadWritePC(context, data))
Caroline Tice0b29e242011-02-08 23:16:02 +00002587 return false;
2588 }
2589
2590 // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers);
2591 if (wback && BitIsClear (registers, n))
2592 {
Caroline Tice0b29e242011-02-08 23:16:02 +00002593 addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2594 if (!success)
2595 return false;
Caroline Ticefa172202011-02-11 22:49:54 +00002596
2597 offset = (addr_byte_size * BitCount (registers)) * -1;
2598 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002599 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002600 addr = addr + offset;
Caroline Tice0b29e242011-02-08 23:16:02 +00002601 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr))
2602 return false;
2603 }
2604
2605 // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2606 if (wback && BitIsSet (registers, n))
Caroline Tice713c2662011-02-11 17:59:55 +00002607 return WriteBits32Unknown (n);
Caroline Tice0b29e242011-02-08 23:16:02 +00002608 }
2609 return true;
2610}
Caroline Tice85aab332011-02-08 23:56:10 +00002611
Caroline Tice713c2662011-02-11 17:59:55 +00002612// LDMIB loads multiple registers from consecutive memory locations using an address from a base register. The
2613// consecutive memory locations start just above this address, and thea ddress of the last of those locations can
2614// optinoally be written back to the base register.
Caroline Tice85aab332011-02-08 23:56:10 +00002615bool
2616EmulateInstructionARM::EmulateLDMIB (ARMEncoding encoding)
2617{
2618#if 0
2619 if ConditionPassed() then
2620 EncodingSpecificOperations();
2621 address = R[n] + 4;
2622
2623 for i = 0 to 14
2624 if registers<i> == ’1’ then
2625 R[i] = MemA[address,4]; address = address + 4;
2626 if registers<15> == ’1’ then
2627 LoadWritePC(MemA[address,4]);
2628
2629 if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers);
2630 if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN;
2631#endif
2632
2633 bool success = false;
2634 const uint32_t opcode = OpcodeAsUnsigned (&success);
2635 if (!success)
2636 return false;
2637
2638 if (ConditionPassed())
2639 {
2640 uint32_t n;
2641 uint32_t registers = 0;
2642 bool wback;
2643 const uint32_t addr_byte_size = GetAddressByteSize();
2644 switch (encoding)
2645 {
2646 case eEncodingA1:
2647 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2648 n = Bits32 (opcode, 19, 16);
2649 registers = Bits32 (opcode, 15, 0);
2650 wback = BitIsSet (opcode, 21);
2651
2652 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2653 if ((n == 15) || (BitCount (registers) < 1))
2654 return false;
2655
2656 break;
2657 default:
2658 return false;
2659 }
2660 // address = R[n] + 4;
2661
2662 int32_t offset = 0;
Caroline Tice713c2662011-02-11 17:59:55 +00002663 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2664
2665 if (!success)
2666 return false;
2667
2668 address = address + addr_byte_size;
Caroline Tice85aab332011-02-08 23:56:10 +00002669
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002670 EmulateInstruction::Context context;
2671 context.type = EmulateInstruction::eContextRegisterPlusOffset;
2672 Register dwarf_reg;
2673 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
2674 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice85aab332011-02-08 23:56:10 +00002675
2676 for (int i = 0; i < 14; ++i)
2677 {
2678 if (BitIsSet (registers, i))
2679 {
2680 // R[i] = MemA[address,4]; address = address + 4;
2681
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002682 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice85aab332011-02-08 23:56:10 +00002683 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2684 if (!success)
2685 return false;
2686
2687 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data))
2688 return false;
2689
2690 offset += addr_byte_size;
2691 }
2692 }
2693
2694 // if registers<15> == ’1’ then
2695 // LoadWritePC(MemA[address,4]);
2696 if (BitIsSet (registers, 15))
2697 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002698 context.SetRegisterPlusOffset (dwarf_reg, offset);
Caroline Tice85aab332011-02-08 23:56:10 +00002699 uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success);
2700 if (!success)
2701 return false;
Johnny Chene62b50d2011-02-09 22:02:17 +00002702 // In ARMv5T and above, this is an interworking branch.
Johnny Chen668b4512011-02-15 21:08:58 +00002703 if (!LoadWritePC(context, data))
Caroline Tice85aab332011-02-08 23:56:10 +00002704 return false;
2705 }
2706
2707 // if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers);
2708 if (wback && BitIsClear (registers, n))
2709 {
Caroline Tice85aab332011-02-08 23:56:10 +00002710 addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2711 if (!success)
2712 return false;
Caroline Ticefa172202011-02-11 22:49:54 +00002713
2714 offset = addr_byte_size * BitCount (registers);
2715 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002716 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002717 addr = addr + offset;
Caroline Tice85aab332011-02-08 23:56:10 +00002718 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr))
2719 return false;
2720 }
2721
2722 // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1
2723 if (wback && BitIsSet (registers, n))
Caroline Tice713c2662011-02-11 17:59:55 +00002724 return WriteBits32Unknown (n);
Caroline Tice85aab332011-02-08 23:56:10 +00002725 }
2726 return true;
2727}
Caroline Tice0b29e242011-02-08 23:16:02 +00002728
Johnny Chenef21b592011-02-10 01:52:38 +00002729// Load Register (immediate) calculates an address from a base register value and
2730// an immediate offset, loads a word from memory, and writes to a register.
2731// LDR (immediate, Thumb)
2732bool
2733EmulateInstructionARM::EmulateLDRRtRnImm (ARMEncoding encoding)
2734{
2735#if 0
2736 // ARM pseudo code...
2737 if (ConditionPassed())
2738 {
2739 EncodingSpecificOperations(); NullCheckIfThumbEE(15);
2740 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
2741 address = if index then offset_addr else R[n];
2742 data = MemU[address,4];
2743 if wback then R[n] = offset_addr;
2744 if t == 15 then
2745 if address<1:0> == '00' then LoadWritePC(data); else UNPREDICTABLE;
2746 elsif UnalignedSupport() || address<1:0> = '00' then
2747 R[t] = data;
2748 else R[t] = bits(32) UNKNOWN; // Can only apply before ARMv7
2749 }
2750#endif
2751
2752 bool success = false;
2753 const uint32_t opcode = OpcodeAsUnsigned (&success);
2754 if (!success)
2755 return false;
2756
2757 if (ConditionPassed())
2758 {
2759 uint32_t Rt; // the destination register
2760 uint32_t Rn; // the base register
2761 uint32_t imm32; // the immediate offset used to form the address
2762 addr_t offset_addr; // the offset address
2763 addr_t address; // the calculated address
2764 uint32_t data; // the literal data value from memory load
2765 bool add, index, wback;
2766 switch (encoding) {
2767 case eEncodingT1:
2768 Rt = Bits32(opcode, 5, 3);
2769 Rn = Bits32(opcode, 2, 0);
2770 imm32 = Bits32(opcode, 10, 6) << 2; // imm32 = ZeroExtend(imm5:'00', 32);
2771 // index = TRUE; add = TRUE; wback = FALSE
2772 add = true;
2773 index = true;
2774 wback = false;
2775 break;
2776 default:
2777 return false;
2778 }
2779 uint32_t base = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success);
2780 if (!success)
2781 return false;
2782 if (add)
2783 offset_addr = base + imm32;
2784 else
2785 offset_addr = base - imm32;
2786
2787 address = (index ? offset_addr : base);
2788
2789 if (wback)
2790 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002791 EmulateInstruction::Context ctx;
2792 ctx.type = EmulateInstruction::eContextRegisterPlusOffset;
2793 Register dwarf_reg;
2794 dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rn);
2795 ctx.SetRegisterPlusOffset (dwarf_reg, (int32_t) (offset_addr - base));
2796
Johnny Chenef21b592011-02-10 01:52:38 +00002797 if (!WriteRegisterUnsigned (ctx, eRegisterKindDWARF, dwarf_r0 + Rn, offset_addr))
2798 return false;
2799 }
2800
2801 // Prepare to write to the Rt register.
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002802 EmulateInstruction::Context context;
2803 context.type = EmulateInstruction::eContextImmediate;
2804 context.SetNoArgs ();
Johnny Chenef21b592011-02-10 01:52:38 +00002805
2806 // Read memory from the address.
2807 data = ReadMemoryUnsigned(context, address, 4, 0, &success);
2808 if (!success)
2809 return false;
Johnny Chenef21b592011-02-10 01:52:38 +00002810
2811 if (Rt == 15)
2812 {
2813 if (Bits32(address, 1, 0) == 0)
2814 {
Johnny Chen668b4512011-02-15 21:08:58 +00002815 if (!LoadWritePC(context, data))
Johnny Chenef21b592011-02-10 01:52:38 +00002816 return false;
2817 }
2818 else
2819 return false;
2820 }
2821 else if (UnalignedSupport() || Bits32(address, 1, 0) == 0)
2822 {
2823 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data))
2824 return false;
2825 }
2826 else
2827 return false;
2828 }
2829 return true;
2830}
2831
Caroline Ticeaf556562011-02-15 18:42:15 +00002832// STM (Store Multiple Increment After) stores multiple registers to consecutive memory locations using an address
2833// from a base register. The consecutive memory locations start at this address, and teh address just above the last
2834// of those locations can optionally be written back to the base register.
Caroline Ticefa172202011-02-11 22:49:54 +00002835bool
2836EmulateInstructionARM::EmulateSTM (ARMEncoding encoding)
2837{
2838#if 0
2839 if ConditionPassed() then
2840 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
2841 address = R[n];
2842
2843 for i = 0 to 14
2844 if registers<i> == ’1’ then
2845 if i == n && wback && i != LowestSetBit(registers) then
2846 MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1
2847 else
2848 MemA[address,4] = R[i];
2849 address = address + 4;
2850
2851 if registers<15> == ’1’ then // Only possible for encoding A1
2852 MemA[address,4] = PCStoreValue();
2853 if wback then R[n] = R[n] + 4*BitCount(registers);
2854#endif
2855
2856 bool success = false;
2857 const uint32_t opcode = OpcodeAsUnsigned (&success);
2858 if (!success)
2859 return false;
2860
2861 if (ConditionPassed ())
2862 {
2863 uint32_t n;
2864 uint32_t registers = 0;
2865 bool wback;
2866 const uint32_t addr_byte_size = GetAddressByteSize();
2867
2868 // EncodingSpecificOperations(); NullCheckIfThumbEE(n);
2869 switch (encoding)
2870 {
2871 case eEncodingT1:
2872 // n = UInt(Rn); registers = ’00000000’:register_list; wback = TRUE;
2873 n = Bits32 (opcode, 10, 8);
2874 registers = Bits32 (opcode, 7, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002875 registers = registers & 0x00ff; // Make sure the top 8 bits are zeros.
Caroline Ticefa172202011-02-11 22:49:54 +00002876 wback = true;
2877
2878 // if BitCount(registers) < 1 then UNPREDICTABLE;
2879 if (BitCount (registers) < 1)
2880 return false;
2881
2882 break;
2883
2884 case eEncodingT2:
2885 // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’);
2886 n = Bits32 (opcode, 19, 16);
2887 registers = Bits32 (opcode, 15, 0);
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00002888 registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros.
Caroline Ticefa172202011-02-11 22:49:54 +00002889 wback = BitIsSet (opcode, 21);
2890
2891 // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE;
2892 if ((n == 15) || (BitCount (registers) < 2))
2893 return false;
2894
2895 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
2896 if (wback && BitIsSet (registers, n))
2897 return false;
2898
2899 break;
2900
2901 case eEncodingA1:
2902 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
2903 n = Bits32 (opcode, 19, 16);
2904 registers = Bits32 (opcode, 15, 0);
2905 wback = BitIsSet (opcode, 21);
2906
2907 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
2908 if ((n == 15) || (BitCount (registers) < 1))
2909 return false;
2910
2911 break;
2912
2913 default:
2914 return false;
2915 }
2916
2917 // address = R[n];
2918 int32_t offset = 0;
2919 const addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
2920 if (!success)
2921 return false;
2922
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002923 EmulateInstruction::Context context;
2924 context.type = EmulateInstruction::eContextRegisterStore;
2925 Register base_reg;
2926 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
Caroline Ticefa172202011-02-11 22:49:54 +00002927
2928 // for i = 0 to 14
2929 for (int i = 0; i < 14; ++i)
2930 {
2931 int lowest_set_bit = 14;
2932 // if registers<i> == ’1’ then
2933 if (BitIsSet (registers, i))
2934 {
2935 if (i < lowest_set_bit)
2936 lowest_set_bit = i;
2937 // if i == n && wback && i != LowestSetBit(registers) then
2938 if ((i == n) && wback && (i != lowest_set_bit))
2939 // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1
2940 WriteBits32UnknownToMemory (address + offset);
2941 else
2942 {
2943 // MemA[address,4] = R[i];
2944 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
2945 if (!success)
2946 return false;
2947
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002948 Register data_reg;
2949 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
2950 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002951 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
2952 return false;
2953 }
2954
2955 // address = address + 4;
2956 offset += addr_byte_size;
2957 }
2958 }
2959
2960 // if registers<15> == ’1’ then // Only possible for encoding A1
2961 // MemA[address,4] = PCStoreValue();
2962 if (BitIsSet (registers, 15))
2963 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002964 Register pc_reg;
2965 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
2966 context.SetRegisterPlusOffset (pc_reg, 8);
Caroline Ticefa172202011-02-11 22:49:54 +00002967 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
2968 if (!success)
2969 return false;
2970
2971 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
2972 return false;
2973 }
2974
2975 // if wback then R[n] = R[n] + 4*BitCount(registers);
2976 if (wback)
2977 {
2978 offset = addr_byte_size * BitCount (registers);
2979 context.type = EmulateInstruction::eContextAdjustBaseRegister;
Caroline Tice9bfe7f22011-02-14 23:03:21 +00002980 context.SetImmediateSigned (offset);
Caroline Ticefa172202011-02-11 22:49:54 +00002981 addr_t data = address + offset;
2982 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
2983 return false;
2984 }
2985 }
2986 return true;
2987}
2988
Caroline Ticeaf556562011-02-15 18:42:15 +00002989// STMDA (Store Multiple Decrement After) stores multiple registers to consecutive memory locations using an address
2990// from a base register. The consecutive memory locations end at this address, and the address just below the lowest
2991// of those locations can optionally be written back to the base register.
Caroline Tice1511f502011-02-15 00:19:42 +00002992bool
2993EmulateInstructionARM::EmulateSTMDA (ARMEncoding encoding)
2994{
2995#if 0
2996 if ConditionPassed() then
2997 EncodingSpecificOperations();
2998 address = R[n] - 4*BitCount(registers) + 4;
2999
3000 for i = 0 to 14
3001 if registers<i> == ’1’ then
3002 if i == n && wback && i != LowestSetBit(registers) then
3003 MemA[address,4] = bits(32) UNKNOWN;
3004 else
3005 MemA[address,4] = R[i];
3006 address = address + 4;
3007
3008 if registers<15> == ’1’ then
3009 MemA[address,4] = PCStoreValue();
3010
3011 if wback then R[n] = R[n] - 4*BitCount(registers);
3012#endif
3013
3014 bool success = false;
3015 const uint32_t opcode = OpcodeAsUnsigned (&success);
3016 if (!success)
3017 return false;
3018
3019 if (ConditionPassed ())
3020 {
3021 uint32_t n;
3022 uint32_t registers = 0;
3023 bool wback;
3024 const uint32_t addr_byte_size = GetAddressByteSize();
3025
3026 // EncodingSpecificOperations();
3027 switch (encoding)
3028 {
3029 case eEncodingA1:
3030 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
3031 n = Bits32 (opcode, 19, 16);
3032 registers = Bits32 (opcode, 15, 0);
3033 wback = BitIsSet (opcode, 21);
3034
3035 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
3036 if ((n == 15) || (BitCount (registers) < 1))
3037 return false;
3038 break;
3039 default:
3040 return false;
3041 }
3042
3043 // address = R[n] - 4*BitCount(registers) + 4;
3044 int32_t offset = 0;
3045 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
3046 if (!success)
3047 return false;
3048
3049 address = address - (addr_byte_size * BitCount (registers)) + 4;
3050
3051 EmulateInstruction::Context context;
3052 context.type = EmulateInstruction::eContextRegisterStore;
3053 Register base_reg;
3054 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
3055
3056 // for i = 0 to 14
3057 for (int i = 0; i < 14; ++i)
3058 {
3059 int lowest_bit_set = 14;
3060 // if registers<i> == ’1’ then
3061 if (BitIsSet (registers, i))
3062 {
3063 if (i < lowest_bit_set)
3064 lowest_bit_set = i;
3065 //if i == n && wback && i != LowestSetBit(registers) then
3066 if ((i == n) && wback && (i != lowest_bit_set))
3067 // MemA[address,4] = bits(32) UNKNOWN;
3068 WriteBits32UnknownToMemory (address + offset);
3069 else
3070 {
3071 // MemA[address,4] = R[i];
3072 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
3073 if (!success)
3074 return false;
3075
3076 Register data_reg;
3077 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
3078 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
3079 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
3080 return false;
3081 }
3082
3083 // address = address + 4;
3084 offset += addr_byte_size;
3085 }
3086 }
3087
3088 // if registers<15> == ’1’ then
3089 // MemA[address,4] = PCStoreValue();
3090 if (BitIsSet (registers, 15))
3091 {
3092 Register pc_reg;
3093 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
3094 context.SetRegisterPlusOffset (pc_reg, 8);
3095 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
3096 if (!success)
3097 return false;
3098
3099 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
3100 return false;
3101 }
3102
3103 // if wback then R[n] = R[n] - 4*BitCount(registers);
3104 if (wback)
3105 {
Caroline Ticeaf556562011-02-15 18:42:15 +00003106 offset = (addr_byte_size * BitCount (registers)) * -1;
Caroline Tice1511f502011-02-15 00:19:42 +00003107 context.type = EmulateInstruction::eContextAdjustBaseRegister;
3108 context.SetImmediateSigned (offset);
3109 addr_t data = address + offset;
3110 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
3111 return false;
3112 }
3113 }
3114 return true;
3115}
3116
Caroline Ticeaf556562011-02-15 18:42:15 +00003117// STMDB (Store Multiple Decrement Before) stores multiple registers to consecutive memory locations using an address
3118// from a base register. The consecutive memory locations end just below this address, and the address of the first of
3119// those locations can optionally be written back to the base register.
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003120bool
3121EmulateInstructionARM::EmulateSTMDB (ARMEncoding encoding)
3122{
3123#if 0
3124 if ConditionPassed() then
3125 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
3126 address = R[n] - 4*BitCount(registers);
3127
3128 for i = 0 to 14
3129 if registers<i> == ’1’ then
3130 if i == n && wback && i != LowestSetBit(registers) then
3131 MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1
3132 else
3133 MemA[address,4] = R[i];
3134 address = address + 4;
3135
3136 if registers<15> == ’1’ then // Only possible for encoding A1
3137 MemA[address,4] = PCStoreValue();
3138
3139 if wback then R[n] = R[n] - 4*BitCount(registers);
3140#endif
3141
3142
3143 bool success = false;
3144 const uint32_t opcode = OpcodeAsUnsigned (&success);
3145 if (!success)
3146 return false;
3147
3148 if (ConditionPassed ())
3149 {
3150 uint32_t n;
3151 uint32_t registers = 0;
3152 bool wback;
3153 const uint32_t addr_byte_size = GetAddressByteSize();
3154
3155 // EncodingSpecificOperations(); NullCheckIfThumbEE(n);
3156 switch (encoding)
3157 {
3158 case eEncodingT1:
3159 // if W == ’1’ && Rn == ’1101’ then SEE PUSH;
3160 if ((BitIsSet (opcode, 21)) && (Bits32 (opcode, 19, 16) == 13))
3161 {
3162 // See PUSH
3163 }
3164 // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’);
3165 n = Bits32 (opcode, 19, 16);
3166 registers = Bits32 (opcode, 15, 0);
3167 registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros.
3168 wback = BitIsSet (opcode, 21);
3169 // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE;
3170 if ((n == 15) || BitCount (registers) < 2)
3171 return false;
3172 // if wback && registers<n> == ’1’ then UNPREDICTABLE;
3173 if (wback && BitIsSet (registers, n))
3174 return false;
3175 break;
3176
3177 case eEncodingA1:
3178 // if W == ’1’ && Rn == ’1101’ && BitCount(register_list) >= 2 then SEE PUSH;
3179 if (BitIsSet (opcode, 21) && (Bits32 (opcode, 19, 16) == 13) && BitCount (Bits32 (opcode, 15, 0)) >= 2)
3180 {
3181 // See Push
3182 }
3183 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
3184 n = Bits32 (opcode, 19, 16);
3185 registers = Bits32 (opcode, 15, 0);
3186 wback = BitIsSet (opcode, 21);
3187 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
3188 if ((n == 15) || BitCount (registers) < 1)
3189 return false;
3190 break;
3191
3192 default:
3193 return false;
3194 }
3195
3196 // address = R[n] - 4*BitCount(registers);
3197
3198 int32_t offset = 0;
3199 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
3200 if (!success)
3201 return false;
3202
3203 address = address - (addr_byte_size * BitCount (registers));
3204
3205 EmulateInstruction::Context context;
3206 context.type = EmulateInstruction::eContextRegisterStore;
3207 Register base_reg;
3208 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
3209
3210 // for i = 0 to 14
3211 for (int i = 0; i < 14; ++i)
3212 {
3213 uint32_t lowest_set_bit = 14;
3214 // if registers<i> == ’1’ then
3215 if (BitIsSet (registers, i))
3216 {
3217 if (i < lowest_set_bit)
3218 lowest_set_bit = i;
3219 // if i == n && wback && i != LowestSetBit(registers) then
3220 if ((i == n) && wback && (i != lowest_set_bit))
3221 // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1
3222 WriteBits32UnknownToMemory (address + offset);
3223 else
3224 {
3225 // MemA[address,4] = R[i];
3226 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
3227 if (!success)
3228 return false;
3229
3230 Register data_reg;
3231 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
3232 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
3233 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
3234 return false;
3235 }
3236
3237 // address = address + 4;
3238 offset += addr_byte_size;
3239 }
3240 }
3241
3242 // if registers<15> == ’1’ then // Only possible for encoding A1
3243 // MemA[address,4] = PCStoreValue();
3244 if (BitIsSet (registers, 15))
3245 {
3246 Register pc_reg;
3247 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
3248 context.SetRegisterPlusOffset (pc_reg, 8);
3249 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
3250 if (!success)
3251 return false;
3252
3253 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
3254 return false;
3255 }
3256
3257 // if wback then R[n] = R[n] - 4*BitCount(registers);
3258 if (wback)
3259 {
Caroline Ticeaf556562011-02-15 18:42:15 +00003260 offset = (addr_byte_size * BitCount (registers)) * -1;
3261 context.type = EmulateInstruction::eContextAdjustBaseRegister;
3262 context.SetImmediateSigned (offset);
3263 addr_t data = address + offset;
3264 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
3265 return false;
3266 }
3267 }
3268 return true;
3269}
3270
3271// STMIB (Store Multiple Increment Before) stores multiple registers to consecutive memory locations using an address
3272// from a base register. The consecutive memory locations start just above this address, and the address of the last
3273// of those locations can optionally be written back to the base register.
3274bool
3275EmulateInstructionARM::EmulateSTMIB (ARMEncoding encoding)
3276{
3277#if 0
3278 if ConditionPassed() then
3279 EncodingSpecificOperations();
3280 address = R[n] + 4;
3281
3282 for i = 0 to 14
3283 if registers<i> == ’1’ then
3284 if i == n && wback && i != LowestSetBit(registers) then
3285 MemA[address,4] = bits(32) UNKNOWN;
3286 else
3287 MemA[address,4] = R[i];
3288 address = address + 4;
3289
3290 if registers<15> == ’1’ then
3291 MemA[address,4] = PCStoreValue();
3292
3293 if wback then R[n] = R[n] + 4*BitCount(registers);
3294#endif
3295
3296 bool success = false;
3297 const uint32_t opcode = OpcodeAsUnsigned (&success);
3298 if (!success)
3299 return false;
3300
3301 if (ConditionPassed())
3302 {
3303 uint32_t n;
3304 uint32_t registers = 0;
3305 bool wback;
3306 const uint32_t addr_byte_size = GetAddressByteSize();
3307
3308 // EncodingSpecificOperations();
3309 switch (encoding)
3310 {
3311 case eEncodingA1:
3312 // n = UInt(Rn); registers = register_list; wback = (W == ’1’);
3313 n = Bits32 (opcode, 19, 16);
3314 registers = Bits32 (opcode, 15, 0);
3315 wback = BitIsSet (opcode, 21);
3316
3317 // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE;
3318 if ((n == 15) && (BitCount (registers) < 1))
3319 return false;
3320 break;
3321 default:
3322 return false;
3323 }
3324 // address = R[n] + 4;
3325
3326 int32_t offset = 0;
3327 addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
3328 if (!success)
3329 return false;
3330
3331 address = address + addr_byte_size;
3332
3333 EmulateInstruction::Context context;
3334 context.type = EmulateInstruction::eContextRegisterStore;
3335 Register base_reg;
3336 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
3337
3338 uint32_t lowest_set_bit = 14;
3339 // for i = 0 to 14
3340 for (int i = 0; i < 14; ++i)
3341 {
3342 // if registers<i> == ’1’ then
3343 if (BitIsSet (registers, i))
3344 {
3345 if (i < lowest_set_bit)
3346 lowest_set_bit = i;
3347 // if i == n && wback && i != LowestSetBit(registers) then
3348 if ((i == n) && wback && (i != lowest_set_bit))
3349 // MemA[address,4] = bits(32) UNKNOWN;
3350 WriteBits32UnknownToMemory (address + offset);
3351 // else
3352 else
3353 {
3354 // MemA[address,4] = R[i];
3355 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success);
3356 if (!success)
3357 return false;
3358
3359 Register data_reg;
3360 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i);
3361 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
3362 if (!WriteMemoryUnsigned (context, address + offset, data, addr_byte_size))
3363 return false;
3364 }
3365
3366 // address = address + 4;
3367 offset += addr_byte_size;
3368 }
3369 }
3370
3371 // if registers<15> == ’1’ then
3372 // MemA[address,4] = PCStoreValue();
3373 if (BitIsSet (registers, 15))
3374 {
3375 Register pc_reg;
3376 pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc);
3377 context.SetRegisterPlusOffset (pc_reg, 8);
3378 const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success);
3379 if (!success)
3380 return false;
3381
3382 if (!WriteMemoryUnsigned (context, address + offset, pc + 8, addr_byte_size))
3383 return false;
3384 }
3385
3386 // if wback then R[n] = R[n] + 4*BitCount(registers);
3387 if (wback)
3388 {
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003389 offset = addr_byte_size * BitCount (registers);
3390 context.type = EmulateInstruction::eContextAdjustBaseRegister;
3391 context.SetImmediateSigned (offset);
3392 addr_t data = address + offset;
3393 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data))
3394 return false;
3395 }
3396 }
3397 return true;
3398}
Caroline Tice7fac8572011-02-15 22:53:54 +00003399
3400// STR (store immediate) calcualtes an address from a base register value and an immediate offset, and stores a word
3401// from a register to memory. It can use offset, post-indexed, or pre-indexed addressing.
3402bool
3403EmulateInstructionARM::EmulateSTRThumb (ARMEncoding encoding)
3404{
3405#if 0
3406 if ConditionPassed() then
3407 EncodingSpecificOperations(); NullCheckIfThumbEE(n);
3408 offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
3409 address = if index then offset_addr else R[n];
3410 if UnalignedSupport() || address<1:0> == ’00’ then
3411 MemU[address,4] = R[t];
3412 else // Can only occur before ARMv7
3413 MemU[address,4] = bits(32) UNKNOWN;
3414 if wback then R[n] = offset_addr;
3415#endif
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003416
Caroline Tice7fac8572011-02-15 22:53:54 +00003417 bool success = false;
3418 const uint32_t opcode = OpcodeAsUnsigned (&success);
3419 if (!success)
3420 return false;
3421
3422 if (ConditionPassed())
3423 {
3424 const uint32_t addr_byte_size = GetAddressByteSize();
3425
3426 uint32_t t;
3427 uint32_t n;
3428 uint32_t imm32;
3429 bool index;
3430 bool add;
3431 bool wback;
3432 // EncodingSpecificOperations (); NullCheckIfThumbEE(n);
3433 switch (encoding)
3434 {
3435 case eEncodingT1:
3436 // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5:’00’, 32);
3437 t = Bits32 (opcode, 2, 0);
3438 n = Bits32 (opcode, 5, 3);
3439 imm32 = Bits32 (opcode, 10, 6) << 2;
3440
3441 // index = TRUE; add = TRUE; wback = FALSE;
3442 index = true;
3443 add = false;
3444 wback = false;
3445 break;
3446
3447 case eEncodingT2:
3448 // t = UInt(Rt); n = 13; imm32 = ZeroExtend(imm8:’00’, 32);
3449 t = Bits32 (opcode, 10, 8);
3450 n = 13;
3451 imm32 = Bits32 (opcode, 7, 0) << 2;
3452
3453 // index = TRUE; add = TRUE; wback = FALSE;
3454 index = true;
3455 add = true;
3456 wback = false;
3457 break;
3458
3459 case eEncodingT3:
3460 // if Rn == ’1111’ then UNDEFINED;
3461 if (Bits32 (opcode, 19, 16) == 15)
3462 return false;
3463
3464 // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32);
3465 t = Bits32 (opcode, 15, 12);
3466 n = Bits32 (opcode, 19, 16);
3467 imm32 = Bits32 (opcode, 11, 0);
3468
3469 // index = TRUE; add = TRUE; wback = FALSE;
3470 index = true;
3471 add = true;
3472 wback = false;
3473
3474 // if t == 15 then UNPREDICTABLE;
3475 if (t == 15)
3476 return false;
3477 break;
3478
3479 case eEncodingT4:
3480 // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRT;
3481 // if Rn == ’1101’ && P == ’1’ && U == ’0’ && W == ’1’ && imm8 == ’00000100’ then SEE PUSH;
3482 // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED;
3483 if ((Bits32 (opcode, 19, 16) == 15)
3484 || (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)))
3485 return false;
3486
3487 // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32);
3488 t = Bits32 (opcode, 15, 12);
3489 n = Bits32 (opcode, 19, 16);
3490 imm32 = Bits32 (opcode, 7, 0);
3491
3492 // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’);
3493 index = BitIsSet (opcode, 10);
3494 add = BitIsSet (opcode, 9);
3495 wback = BitIsSet (opcode, 8);
3496
3497 // if t == 15 || (wback && n == t) then UNPREDICTABLE;
3498 if ((t == 15) || (wback && (n == t)))
3499 return false;
3500 break;
3501
3502 default:
3503 return false;
3504 }
3505
3506 addr_t offset_addr;
3507 addr_t address;
3508
3509 // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32);
3510 uint32_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success);
3511 if (!success)
3512 return false;
3513
3514 if (add)
3515 offset_addr = base_address + imm32;
3516 else
3517 offset_addr = base_address - imm32;
3518
3519 // address = if index then offset_addr else R[n];
3520 if (index)
3521 address = offset_addr;
3522 else
3523 address = base_address;
3524
3525 EmulateInstruction::Context context;
3526 context.type = eContextRegisterStore;
3527 Register base_reg;
3528 base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n);
3529
3530 // if UnalignedSupport() || address<1:0> == ’00’ then
3531 if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0)))
3532 {
3533 // MemU[address,4] = R[t];
3534 uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success);
3535 if (!success)
3536 return false;
3537
3538 Register data_reg;
3539 data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t);
3540 int32_t offset = address - base_address;
3541 context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset);
3542 if (!WriteMemoryUnsigned (context, address, data, addr_byte_size))
3543 return false;
3544 }
3545 else
3546 {
3547 // MemU[address,4] = bits(32) UNKNOWN;
3548 WriteBits32UnknownToMemory (address);
3549 }
3550
3551 // if wback then R[n] = offset_addr;
3552 if (wback)
3553 {
3554 context.type = eContextRegisterLoad;
3555 context.SetAddress (offset_addr);
3556 if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr))
3557 return false;
3558 }
3559 }
3560 return true;
3561}
Caroline Ticeaf556562011-02-15 18:42:15 +00003562
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003563EmulateInstructionARM::ARMOpcode*
3564EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode)
Greg Clayton64c84432011-01-21 22:02:52 +00003565{
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003566 static ARMOpcode
3567 g_arm_opcodes[] =
3568 {
3569 //----------------------------------------------------------------------
3570 // Prologue instructions
3571 //----------------------------------------------------------------------
Johnny Chenfdd179e2011-01-31 20:09:28 +00003572
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003573 // push register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00003574 { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePush, "push <registers>" },
3575 { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePush, "push <register>" },
Johnny Chenbcec3af2011-01-27 01:26:19 +00003576
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003577 // set r7 to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00003578 { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #<const>" },
3579 { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubR7IPImmediate, "sub r7, ip, #<const>"},
Johnny Chene7cf4202011-02-10 18:13:23 +00003580 // copy the stack pointer to ip
Johnny Chenc28a76d2011-02-01 18:51:48 +00003581 { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMovRdSP, "mov ip, sp" },
3582 { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add ip, sp, #<const>" },
3583 { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubIPSPImmediate, "sub ip, sp, #<const>"},
Johnny Chen4c0e0bc2011-01-25 22:45:28 +00003584
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003585 // adjust the stack pointer
Johnny Chenc28a76d2011-02-01 18:51:48 +00003586 { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub sp, sp, #<const>"},
Johnny Chence1ca772011-01-25 01:13:00 +00003587
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003588 // push one register
3589 // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH;
Johnny Chenc28a76d2011-02-01 18:51:48 +00003590 { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" },
Johnny Chen799dfd02011-01-26 23:14:33 +00003591
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003592 // vector push consecutive extension register(s)
Johnny Chen9b8d7832011-02-02 01:13:56 +00003593 { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
3594 { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenef85e912011-01-31 23:07:40 +00003595
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003596 //----------------------------------------------------------------------
Johnny Chen587a0a42011-02-01 18:35:28 +00003597 // Epilogue instructions
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003598 //----------------------------------------------------------------------
Johnny Chenef85e912011-01-31 23:07:40 +00003599
Johnny Chenc28a76d2011-02-01 18:51:48 +00003600 { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePop, "pop <registers>"},
3601 { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePop, "pop <register>"},
Johnny Chen9b8d7832011-02-02 01:13:56 +00003602 { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"},
Johnny Chenb77be412011-02-04 00:40:18 +00003603 { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"},
3604
3605 //----------------------------------------------------------------------
3606 // Supervisor Call (previously Software Interrupt)
3607 //----------------------------------------------------------------------
Johnny Chen3b620b32011-02-07 20:11:47 +00003608 { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"},
3609
3610 //----------------------------------------------------------------------
3611 // Branch instructions
3612 //----------------------------------------------------------------------
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003613 { 0x0f000000, 0x0a000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "b #imm24"},
Johnny Chen383d6292011-02-11 21:23:32 +00003614 // To resolve ambiguity, "blx <label>" should come before "bl <label>".
3615 { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"},
3616 { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"},
3617 { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"},
Johnny Chenab3b3512011-02-12 00:10:51 +00003618 // for example, "bx lr"
3619 { 0x0ffffff0, 0x012fff10, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"},
Johnny Chenb77be412011-02-04 00:40:18 +00003620
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003621 //----------------------------------------------------------------------
Johnny Chen28070c32011-02-12 01:27:26 +00003622 // Data-processing instructions
3623 //----------------------------------------------------------------------
3624 // move bitwise not
3625 { 0x0fef0000, 0x03e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMvnRdImm, "mvn{s} <Rd>, #<const>"},
Johnny Chen82f16aa2011-02-15 20:10:55 +00003626 // asr (immediate)
3627 { 0x0fef0070, 0x01a00040, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateASRImm, "asr{s}<c> <Rd>, <Rm>, #imm"},
Johnny Chen28070c32011-02-12 01:27:26 +00003628
3629 //----------------------------------------------------------------------
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003630 // Load instructions
3631 //----------------------------------------------------------------------
Caroline Tice0b29e242011-02-08 23:16:02 +00003632 { 0x0fd00000, 0x08900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" },
Caroline Tice713c2662011-02-11 17:59:55 +00003633 { 0x0fd00000, 0x08100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDA, "ldmda<c> <Rn>{!} <registers>" },
Caroline Tice85aab332011-02-08 23:56:10 +00003634 { 0x0fd00000, 0x09100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" },
Caroline Ticefa172202011-02-11 22:49:54 +00003635 { 0x0fd00000, 0x09900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMIB, "ldmib<c> <Rn<{!} <registers>" },
3636
3637 //----------------------------------------------------------------------
3638 // Store instructions
3639 //----------------------------------------------------------------------
Caroline Tice1511f502011-02-15 00:19:42 +00003640 { 0x0fd00000, 0x08800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" },
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003641 { 0x0fd00000, 0x08000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDA, "stmda<c> <Rn>{!} <registers>" },
Caroline Ticeaf556562011-02-15 18:42:15 +00003642 { 0x0fd00000, 0x09000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" },
3643 { 0x0fd00000, 0x09800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMIB, "stmib<c> <Rn>{!} <registers>" }
Caroline Tice1511f502011-02-15 00:19:42 +00003644
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003645
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003646 };
3647 static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode);
3648
3649 for (size_t i=0; i<k_num_arm_opcodes; ++i)
3650 {
3651 if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value)
3652 return &g_arm_opcodes[i];
3653 }
3654 return NULL;
3655}
Greg Clayton64c84432011-01-21 22:02:52 +00003656
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003657
3658EmulateInstructionARM::ARMOpcode*
3659EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode)
Johnny Chen347320d2011-01-24 23:40:59 +00003660{
Johnny Chenfdd179e2011-01-31 20:09:28 +00003661
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003662 static ARMOpcode
3663 g_thumb_opcodes[] =
3664 {
3665 //----------------------------------------------------------------------
3666 // Prologue instructions
3667 //----------------------------------------------------------------------
Johnny Chenbcec3af2011-01-27 01:26:19 +00003668
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003669 // push register(s)
Johnny Chenc28a76d2011-02-01 18:51:48 +00003670 { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePush, "push <registers>" },
Johnny Chend6c13f02011-02-08 20:36:34 +00003671 { 0xffff0000, 0xe92d0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <registers>" },
3672 { 0xffff0fff, 0xf84d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <register>" },
Johnny Chen788e0552011-01-27 22:52:23 +00003673
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003674 // set r7 to point to a stack offset
Johnny Chenc28a76d2011-02-01 18:51:48 +00003675 { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #imm" },
Johnny Chene7cf4202011-02-10 18:13:23 +00003676 // copy the stack pointer to r7
Johnny Chenc28a76d2011-02-01 18:51:48 +00003677 { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdSP, "mov r7, sp" },
Johnny Chene7cf4202011-02-10 18:13:23 +00003678 // move from high register to low register (comes after "mov r7, sp" to resolve ambiguity)
3679 { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovLowHigh, "mov r0-r7, r8-r15" },
Johnny Chen60c0d622011-01-25 23:49:39 +00003680
Johnny Chenc9de9102011-02-11 19:12:30 +00003681 // PC-relative load into register (see also EmulateAddSPRm)
3682 { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr <Rt>, [PC, #imm]"},
Johnny Chen799dfd02011-01-26 23:14:33 +00003683
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003684 // adjust the stack pointer
Johnny Chenc28a76d2011-02-01 18:51:48 +00003685 { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPRm, "add sp, <Rm>"},
3686 { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSubSPImmdiate, "add sp, sp, #imm"},
Johnny Chend6c13f02011-02-08 20:36:34 +00003687 { 0xfbef8f00, 0xf1ad0d00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub.w sp, sp, #<const>"},
3688 { 0xfbff8f00, 0xf2ad0d00, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "subw sp, sp, #imm12"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00003689
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003690 // vector push consecutive extension register(s)
Johnny Chend6c13f02011-02-08 20:36:34 +00003691 { 0xffbf0f00, 0xed2d0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"},
3692 { 0xffbf0f00, 0xed2d0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"},
Johnny Chenfdd179e2011-01-31 20:09:28 +00003693
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003694 //----------------------------------------------------------------------
3695 // Epilogue instructions
3696 //----------------------------------------------------------------------
Johnny Chen347320d2011-01-24 23:40:59 +00003697
Johnny Chenc28a76d2011-02-01 18:51:48 +00003698 { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPImmediate, "add sp, #imm"},
3699 { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePop, "pop <registers>"},
Johnny Chend6c13f02011-02-08 20:36:34 +00003700 { 0xffff0000, 0xe8bd0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <registers>" },
3701 { 0xffff0fff, 0xf85d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <register>" },
3702 { 0xffbf0f00, 0xecbd0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"},
3703 { 0xffbf0f00, 0xecbd0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"},
Johnny Chenb77be412011-02-04 00:40:18 +00003704
3705 //----------------------------------------------------------------------
3706 // Supervisor Call (previously Software Interrupt)
3707 //----------------------------------------------------------------------
Johnny Chenc315f862011-02-05 00:46:10 +00003708 { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"},
3709
3710 //----------------------------------------------------------------------
3711 // If Then makes up to four following instructions conditional.
3712 //----------------------------------------------------------------------
Johnny Chen3b620b32011-02-07 20:11:47 +00003713 { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"},
3714
3715 //----------------------------------------------------------------------
3716 // Branch instructions
3717 //----------------------------------------------------------------------
3718 // To resolve ambiguity, "b<c> #imm8" should come after "svc #imm8".
3719 { 0xfffff000, 0x0000d000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm8 (outside IT)"},
3720 { 0xffff8000, 0x0000e000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateB, "b #imm11 (outside or last in IT)"},
Johnny Chen9ee056b2011-02-08 00:06:35 +00003721 { 0xf800d000, 0xf0008000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside IT)"},
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003722 { 0xf800d000, 0xf0009000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateB, "b.w #imm8 (outside or last in IT)"},
Johnny Chen383d6292011-02-11 21:23:32 +00003723 // J1 == J2 == 1
3724 { 0xf800f800, 0xf000f800, ARMV4T_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"},
3725 // J1 == J2 == 1
3726 { 0xf800e800, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"},
3727 { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"},
Johnny Chenab3b3512011-02-12 00:10:51 +00003728 // for example, "bx lr"
3729 { 0xffffff87, 0x00004700, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"},
Johnny Chen53ebab72011-02-08 23:21:57 +00003730 // compare and branch
3731 { 0xfffff500, 0x0000b100, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCB, "cb{n}z <Rn>, <label>"},
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003732
3733 //----------------------------------------------------------------------
Johnny Chen26863dc2011-02-09 23:43:29 +00003734 // Data-processing instructions
3735 //----------------------------------------------------------------------
3736 // Make sure "add sp, <Rm>" comes before this instruction, so there's no ambiguity decoding the two.
3737 { 0xffffff00, 0x00004400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdnRm, "add <Rdn>, <Rm>"},
Johnny Chen338bf542011-02-10 19:29:03 +00003738 // move from high register to high register
3739 { 0xffffff00, 0x00004600, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdRm, "mov<c> <Rd>, <Rm>"},
3740 // move from low register to low register
3741 { 0xffffffc0, 0x00000000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateMovRdRm, "movs <Rd>, <Rm>"},
Johnny Chen357c30f2011-02-14 22:04:25 +00003742 // move immediate
3743 { 0xfffff800, 0x00002000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdImm, "movs|mov<c> <Rd>, #imm8"},
3744 { 0xfbef8000, 0xf04f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateMovRdImm, "mov{s}<c>.w <Rd>, #<const>"},
Johnny Chen28070c32011-02-12 01:27:26 +00003745 // move bitwise not
3746 { 0xfbef8000, 0xf06f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateMvnRdImm, "mvn{s} <Rd>, #<const>"},
Johnny Chend4dc4442011-02-11 02:02:56 +00003747 // compare a register with immediate
3748 { 0xfffff800, 0x00002800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCmpRnImm, "cmp<c> <Rn>, #imm8"},
Johnny Chene4a4d302011-02-11 21:53:58 +00003749 // compare Rn with Rm (Rn and Rm both from r0-r7)
3750 { 0xffffffc0, 0x00004280, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCmpRnRm, "cmp<c> <Rn>, <Rm>"},
3751 // compare Rn with Rm (Rn and Rm not both from r0-r7)
3752 { 0xffffff00, 0x00004500, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateCmpRnRm, "cmp<c> <Rn>, <Rm>"},
Johnny Chen82f16aa2011-02-15 20:10:55 +00003753 // asr (immediate)
3754 { 0xfffff800, 0x00001000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateASRImm, "asrs|asr<c> <Rd>, <Rm>, #imm"},
Johnny Chen4d896db2011-02-15 20:14:02 +00003755 { 0xffef8030, 0xea4f0020, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateASRImm, "asr{s}<c>.w <Rd>, <Rm>, #imm"},
Johnny Chen26863dc2011-02-09 23:43:29 +00003756
3757 //----------------------------------------------------------------------
Caroline Ticeb9f76c32011-02-08 22:24:38 +00003758 // Load instructions
3759 //----------------------------------------------------------------------
3760 { 0xfffff800, 0x0000c800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" },
Caroline Tice0b29e242011-02-08 23:16:02 +00003761 { 0xffd02000, 0xe8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c>.w <Rn>{!} <registers>" },
Johnny Chenef21b592011-02-10 01:52:38 +00003762 { 0xffd00000, 0xe9100000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" },
Johnny Chenc9de9102011-02-11 19:12:30 +00003763 { 0xfffff800, 0x00006800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtRnImm, "ldr<c> <Rt>, [<Rn>{,#imm}]"},
3764 // Thumb2 PC-relative load into register
Caroline Ticefa172202011-02-11 22:49:54 +00003765 { 0xff7f0000, 0xf85f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr<c>.w <Rt>, [PC, +/-#imm}]"},
3766
3767 //----------------------------------------------------------------------
3768 // Store instructions
3769 //----------------------------------------------------------------------
3770 { 0xfffff800, 0x0000c000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" },
Caroline Ticeb6f8d7e2011-02-15 18:10:01 +00003771 { 0xffd00000, 0xe8800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c>.w <Rn>{!} <registers>" },
Caroline Tice7fac8572011-02-15 22:53:54 +00003772 { 0xffd00000, 0xe9000000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" },
3773 { 0xfffff800, 0x00006000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt> [<Rn>{,#<imm>}]" },
3774 { 0xfffff800, 0x00009000, ARMV4T_ABOVE, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt> [SP,#<imm>]" },
3775 { 0xfff00000, 0xf8c00000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c>.w <Rt [<Rn>,#<imm12>]" },
3776 { 0xfff00800, 0xf8400800, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt> [<Rn>,#+/-<imm8>]" }
Greg Clayton2b8e8b02011-02-01 00:49:32 +00003777 };
3778
3779 const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode);
3780 for (size_t i=0; i<k_num_thumb_opcodes; ++i)
3781 {
3782 if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value)
3783 return &g_thumb_opcodes[i];
3784 }
3785 return NULL;
3786}
Greg Clayton64c84432011-01-21 22:02:52 +00003787
Greg Clayton31e2a382011-01-30 20:03:56 +00003788bool
Greg Clayton395fc332011-02-15 21:59:32 +00003789EmulateInstructionARM::SetArchitecture (const ArchSpec &arch)
Greg Clayton31e2a382011-01-30 20:03:56 +00003790{
3791 m_arm_isa = 0;
Greg Clayton395fc332011-02-15 21:59:32 +00003792 const char *arch_cstr = arch.AsCString ();
3793 if (arch_cstr)
Greg Clayton31e2a382011-01-30 20:03:56 +00003794 {
Greg Clayton395fc332011-02-15 21:59:32 +00003795 if (0 == ::strcasecmp(arch_cstr, "armv4t")) m_arm_isa = ARMv4T;
3796 else if (0 == ::strcasecmp(arch_cstr, "armv4")) m_arm_isa = ARMv4;
3797 else if (0 == ::strcasecmp(arch_cstr, "armv5tej")) m_arm_isa = ARMv5TEJ;
3798 else if (0 == ::strcasecmp(arch_cstr, "armv5te")) m_arm_isa = ARMv5TE;
3799 else if (0 == ::strcasecmp(arch_cstr, "armv5t")) m_arm_isa = ARMv5T;
3800 else if (0 == ::strcasecmp(arch_cstr, "armv6k")) m_arm_isa = ARMv6K;
3801 else if (0 == ::strcasecmp(arch_cstr, "armv6")) m_arm_isa = ARMv6;
3802 else if (0 == ::strcasecmp(arch_cstr, "armv6t2")) m_arm_isa = ARMv6T2;
3803 else if (0 == ::strcasecmp(arch_cstr, "armv7")) m_arm_isa = ARMv7;
3804 else if (0 == ::strcasecmp(arch_cstr, "armv8")) m_arm_isa = ARMv8;
Greg Clayton31e2a382011-01-30 20:03:56 +00003805 }
3806 return m_arm_isa != 0;
3807}
3808
3809
Greg Clayton64c84432011-01-21 22:02:52 +00003810bool
3811EmulateInstructionARM::ReadInstruction ()
3812{
3813 bool success = false;
3814 m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success);
3815 if (success)
3816 {
3817 addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success);
3818 if (success)
3819 {
Caroline Tice9bfe7f22011-02-14 23:03:21 +00003820 Context read_inst_context;
3821 read_inst_context.type = eContextReadOpcode;
3822 read_inst_context.SetNoArgs ();
3823
Greg Clayton64c84432011-01-21 22:02:52 +00003824 if (m_inst_cpsr & MASK_CPSR_T)
3825 {
3826 m_inst_mode = eModeThumb;
3827 uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success);
3828
3829 if (success)
3830 {
3831 if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0))
3832 {
3833 m_inst.opcode_type = eOpcode16;
3834 m_inst.opcode.inst16 = thumb_opcode;
3835 }
3836 else
3837 {
3838 m_inst.opcode_type = eOpcode32;
3839 m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success);
3840 }
3841 }
3842 }
3843 else
3844 {
3845 m_inst_mode = eModeARM;
3846 m_inst.opcode_type = eOpcode32;
3847 m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success);
3848 }
3849 }
3850 }
3851 if (!success)
3852 {
3853 m_inst_mode = eModeInvalid;
3854 m_inst_pc = LLDB_INVALID_ADDRESS;
3855 }
3856 return success;
3857}
3858
Johnny Chenee9b1f72011-02-09 01:00:31 +00003859uint32_t
3860EmulateInstructionARM::ArchVersion ()
3861{
3862 return m_arm_isa;
3863}
3864
Greg Clayton64c84432011-01-21 22:02:52 +00003865bool
3866EmulateInstructionARM::ConditionPassed ()
3867{
3868 if (m_inst_cpsr == 0)
3869 return false;
3870
3871 const uint32_t cond = CurrentCond ();
3872
3873 if (cond == UINT32_MAX)
3874 return false;
3875
3876 bool result = false;
3877 switch (UnsignedBits(cond, 3, 1))
3878 {
3879 case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break;
3880 case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break;
3881 case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break;
3882 case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break;
3883 case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break;
3884 case 5:
3885 {
3886 bool n = (m_inst_cpsr & MASK_CPSR_N);
3887 bool v = (m_inst_cpsr & MASK_CPSR_V);
3888 result = n == v;
3889 }
3890 break;
3891 case 6:
3892 {
3893 bool n = (m_inst_cpsr & MASK_CPSR_N);
3894 bool v = (m_inst_cpsr & MASK_CPSR_V);
3895 result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0);
3896 }
3897 break;
3898 case 7:
3899 result = true;
3900 break;
3901 }
3902
3903 if (cond & 1)
3904 result = !result;
3905 return result;
3906}
3907
Johnny Chen9ee056b2011-02-08 00:06:35 +00003908uint32_t
3909EmulateInstructionARM::CurrentCond ()
3910{
3911 switch (m_inst_mode)
3912 {
3913 default:
3914 case eModeInvalid:
3915 break;
3916
3917 case eModeARM:
3918 return UnsignedBits(m_inst.opcode.inst32, 31, 28);
3919
3920 case eModeThumb:
3921 // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit
3922 // 'cond' field of the encoding.
3923 if (m_inst.opcode_type == eOpcode16 &&
3924 Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d &&
3925 Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f)
3926 {
3927 return Bits32(m_inst.opcode.inst16, 11, 7);
3928 }
3929 else if (m_inst.opcode_type == eOpcode32 &&
3930 Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e &&
3931 Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 &&
3932 Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 &&
3933 Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d)
3934 {
3935 return Bits32(m_inst.opcode.inst32, 25, 22);
3936 }
3937
3938 return m_it_session.GetCond();
3939 }
3940 return UINT32_MAX; // Return invalid value
3941}
3942
Johnny Chen9ee056b2011-02-08 00:06:35 +00003943bool
Johnny Chen098ae2d2011-02-12 00:50:05 +00003944EmulateInstructionARM::InITBlock()
3945{
3946 return CurrentInstrSet() == eModeThumb && m_it_session.InITBlock();
3947}
3948
3949bool
3950EmulateInstructionARM::LastInITBlock()
3951{
3952 return CurrentInstrSet() == eModeThumb && m_it_session.LastInITBlock();
3953}
3954
3955bool
Johnny Chen9ee056b2011-02-08 00:06:35 +00003956EmulateInstructionARM::BranchWritePC (const Context &context, uint32_t addr)
3957{
3958 addr_t target;
3959
Johnny Chenee9b1f72011-02-09 01:00:31 +00003960 // Check the current instruction set.
3961 if (CurrentInstrSet() == eModeARM)
Johnny Chen9ee056b2011-02-08 00:06:35 +00003962 target = addr & 0xfffffffc;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003963 else
Johnny Chen9ee056b2011-02-08 00:06:35 +00003964 target = addr & 0xfffffffe;
Johnny Chenee9b1f72011-02-09 01:00:31 +00003965
Johnny Chen9ee056b2011-02-08 00:06:35 +00003966 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target))
Johnny Chen53ebab72011-02-08 23:21:57 +00003967 return false;
3968
3969 return true;
Johnny Chen9ee056b2011-02-08 00:06:35 +00003970}
3971
3972// As a side effect, BXWritePC sets context.arg2 to eModeARM or eModeThumb by inspecting addr.
3973bool
Johnny Chen668b4512011-02-15 21:08:58 +00003974EmulateInstructionARM::BXWritePC (Context &context, uint32_t addr)
Johnny Chen9ee056b2011-02-08 00:06:35 +00003975{
3976 addr_t target;
Johnny Chen0f309db2011-02-09 19:11:32 +00003977 // If the CPSR is changed due to switching between ARM and Thumb ISETSTATE,
3978 // we want to record it and issue a WriteRegister callback so the clients
3979 // can track the mode changes accordingly.
3980 bool cpsr_changed = false;
Johnny Chen9ee056b2011-02-08 00:06:35 +00003981
3982 if (BitIsSet(addr, 0))
3983 {
Johnny Chen0f309db2011-02-09 19:11:32 +00003984 if (CurrentInstrSet() != eModeThumb)
3985 {
3986 SelectInstrSet(eModeThumb);
3987 cpsr_changed = true;
3988 }
Johnny Chen9ee056b2011-02-08 00:06:35 +00003989 target = addr & 0xfffffffe;
Johnny Chen668b4512011-02-15 21:08:58 +00003990 context.SetMode (eModeThumb);
Johnny Chen9ee056b2011-02-08 00:06:35 +00003991 }
3992 else if (BitIsClear(addr, 1))
3993 {
Johnny Chen0f309db2011-02-09 19:11:32 +00003994 if (CurrentInstrSet() != eModeARM)
3995 {
3996 SelectInstrSet(eModeARM);
3997 cpsr_changed = true;
3998 }
Johnny Chen9ee056b2011-02-08 00:06:35 +00003999 target = addr & 0xfffffffc;
Johnny Chen668b4512011-02-15 21:08:58 +00004000 context.SetMode (eModeARM);
Johnny Chen9ee056b2011-02-08 00:06:35 +00004001 }
4002 else
4003 return false; // address<1:0> == '10' => UNPREDICTABLE
4004
Johnny Chen0f309db2011-02-09 19:11:32 +00004005 if (cpsr_changed)
4006 {
Johnny Chen558133b2011-02-09 23:59:17 +00004007 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr))
Johnny Chen0f309db2011-02-09 19:11:32 +00004008 return false;
4009 }
Johnny Chen9ee056b2011-02-08 00:06:35 +00004010 if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target))
Johnny Chen53ebab72011-02-08 23:21:57 +00004011 return false;
4012
4013 return true;
Johnny Chen9ee056b2011-02-08 00:06:35 +00004014}
Greg Clayton64c84432011-01-21 22:02:52 +00004015
Johnny Chenee9b1f72011-02-09 01:00:31 +00004016// Dispatches to either BXWritePC or BranchWritePC based on architecture versions.
4017bool
Johnny Chen668b4512011-02-15 21:08:58 +00004018EmulateInstructionARM::LoadWritePC (Context &context, uint32_t addr)
Johnny Chenee9b1f72011-02-09 01:00:31 +00004019{
4020 if (ArchVersion() >= ARMv5T)
Johnny Chen668b4512011-02-15 21:08:58 +00004021 return BXWritePC(context, addr);
Johnny Chenee9b1f72011-02-09 01:00:31 +00004022 else
4023 return BranchWritePC((const Context)context, addr);
4024}
4025
Johnny Chen26863dc2011-02-09 23:43:29 +00004026// Dispatches to either BXWritePC or BranchWritePC based on architecture versions and current instruction set.
4027bool
Johnny Chen668b4512011-02-15 21:08:58 +00004028EmulateInstructionARM::ALUWritePC (Context &context, uint32_t addr)
Johnny Chen26863dc2011-02-09 23:43:29 +00004029{
4030 if (ArchVersion() >= ARMv7 && CurrentInstrSet() == eModeARM)
Johnny Chen668b4512011-02-15 21:08:58 +00004031 return BXWritePC(context, addr);
Johnny Chen26863dc2011-02-09 23:43:29 +00004032 else
4033 return BranchWritePC((const Context)context, addr);
4034}
4035
Johnny Chenee9b1f72011-02-09 01:00:31 +00004036EmulateInstructionARM::Mode
4037EmulateInstructionARM::CurrentInstrSet ()
4038{
4039 return m_inst_mode;
4040}
4041
4042// Set the 'T' bit of our CPSR. The m_inst_mode gets updated when the next
Johnny Chen558133b2011-02-09 23:59:17 +00004043// ReadInstruction() is performed. This function has a side effect of updating
4044// the m_new_inst_cpsr member variable if necessary.
Johnny Chenee9b1f72011-02-09 01:00:31 +00004045bool
4046EmulateInstructionARM::SelectInstrSet (Mode arm_or_thumb)
4047{
Johnny Chen558133b2011-02-09 23:59:17 +00004048 m_new_inst_cpsr = m_inst_cpsr;
Johnny Chenee9b1f72011-02-09 01:00:31 +00004049 switch (arm_or_thumb)
4050 {
4051 default:
4052 return false;
4053 eModeARM:
4054 // Clear the T bit.
Johnny Chen558133b2011-02-09 23:59:17 +00004055 m_new_inst_cpsr &= ~MASK_CPSR_T;
Johnny Chenee9b1f72011-02-09 01:00:31 +00004056 break;
4057 eModeThumb:
4058 // Set the T bit.
Johnny Chen558133b2011-02-09 23:59:17 +00004059 m_new_inst_cpsr |= MASK_CPSR_T;
Johnny Chenee9b1f72011-02-09 01:00:31 +00004060 break;
4061 }
4062 return true;
4063}
4064
Johnny Chenef21b592011-02-10 01:52:38 +00004065// This function returns TRUE if the processor currently provides support for
4066// unaligned memory accesses, or FALSE otherwise. This is always TRUE in ARMv7,
4067// controllable by the SCTLR.U bit in ARMv6, and always FALSE before ARMv6.
4068bool
4069EmulateInstructionARM::UnalignedSupport()
4070{
4071 return (ArchVersion() >= ARMv7);
4072}
4073
Johnny Chenbf6ad172011-02-11 01:29:53 +00004074// The main addition and subtraction instructions can produce status information
4075// about both unsigned carry and signed overflow conditions. This status
4076// information can be used to synthesize multi-word additions and subtractions.
4077EmulateInstructionARM::AddWithCarryResult
4078EmulateInstructionARM::AddWithCarry (uint32_t x, uint32_t y, uint8_t carry_in)
4079{
4080 uint32_t result;
4081 uint8_t carry_out;
4082 uint8_t overflow;
4083
4084 uint64_t unsigned_sum = x + y + carry_in;
4085 int64_t signed_sum = (int32_t)x + (int32_t)y + (int32_t)carry_in;
4086
4087 result = UnsignedBits(unsigned_sum, 31, 0);
4088 carry_out = (result == unsigned_sum ? 0 : 1);
4089 overflow = ((int32_t)result == signed_sum ? 0 : 1);
4090
4091 AddWithCarryResult res = { result, carry_out, overflow };
4092 return res;
4093}
4094
Greg Clayton64c84432011-01-21 22:02:52 +00004095bool
4096EmulateInstructionARM::EvaluateInstruction ()
4097{
Johnny Chenc315f862011-02-05 00:46:10 +00004098 // Advance the ITSTATE bits to their values for the next instruction.
4099 if (m_inst_mode == eModeThumb && m_it_session.InITBlock())
4100 m_it_session.ITAdvance();
4101
Greg Clayton64c84432011-01-21 22:02:52 +00004102 return false;
4103}