Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1 | //===-- EmulateInstructionARM.cpp -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "EmulateInstructionARM.h" |
Greg Clayton | 8482ded | 2011-02-01 00:04:43 +0000 | [diff] [blame] | 11 | #include "lldb/Core/ConstString.h" |
| 12 | |
Greg Clayton | f29a08f | 2011-02-09 17:41:27 +0000 | [diff] [blame] | 13 | #include "Plugins/Process/Utility/ARMDefines.h" |
| 14 | #include "Plugins/Process/Utility/ARMUtils.h" |
| 15 | #include "Utility/ARM_DWARF_Registers.h" |
| 16 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" // for SignExtend32 template function |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 18 | // and CountTrailingZeros_32 function |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 23 | static inline uint32_t Align(uint32_t val, uint32_t alignment) |
| 24 | { |
| 25 | return alignment * (val / alignment); |
| 26 | } |
| 27 | |
Johnny Chen | 0e00af2 | 2011-02-10 19:40:42 +0000 | [diff] [blame] | 28 | //---------------------------------------------------------------------- |
| 29 | // |
| 30 | // ITSession implementation |
| 31 | // |
| 32 | //---------------------------------------------------------------------- |
| 33 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 34 | // A8.6.50 |
| 35 | // Valid return values are {1, 2, 3, 4}, with 0 signifying an error condition. |
| 36 | static unsigned short CountITSize(unsigned ITMask) { |
| 37 | // First count the trailing zeros of the IT mask. |
| 38 | unsigned TZ = llvm::CountTrailingZeros_32(ITMask); |
| 39 | if (TZ > 3) |
| 40 | { |
| 41 | printf("Encoding error: IT Mask '0000'\n"); |
| 42 | return 0; |
| 43 | } |
| 44 | return (4 - TZ); |
| 45 | } |
| 46 | |
| 47 | // Init ITState. Note that at least one bit is always 1 in mask. |
| 48 | bool ITSession::InitIT(unsigned short bits7_0) |
| 49 | { |
| 50 | ITCounter = CountITSize(Bits32(bits7_0, 3, 0)); |
| 51 | if (ITCounter == 0) |
| 52 | return false; |
| 53 | |
| 54 | // A8.6.50 IT |
| 55 | unsigned short FirstCond = Bits32(bits7_0, 7, 4); |
| 56 | if (FirstCond == 0xF) |
| 57 | { |
| 58 | printf("Encoding error: IT FirstCond '1111'\n"); |
| 59 | return false; |
| 60 | } |
| 61 | if (FirstCond == 0xE && ITCounter != 1) |
| 62 | { |
| 63 | printf("Encoding error: IT FirstCond '1110' && Mask != '1000'\n"); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | ITState = bits7_0; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | // Update ITState if necessary. |
| 72 | void ITSession::ITAdvance() |
| 73 | { |
| 74 | assert(ITCounter); |
| 75 | --ITCounter; |
| 76 | if (ITCounter == 0) |
| 77 | ITState = 0; |
| 78 | else |
| 79 | { |
| 80 | unsigned short NewITState4_0 = Bits32(ITState, 4, 0) << 1; |
| 81 | SetBits32(ITState, 4, 0, NewITState4_0); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Return true if we're inside an IT Block. |
| 86 | bool ITSession::InITBlock() |
| 87 | { |
| 88 | return ITCounter != 0; |
| 89 | } |
| 90 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 91 | // Return true if we're the last instruction inside an IT Block. |
| 92 | bool ITSession::LastInITBlock() |
| 93 | { |
| 94 | return ITCounter == 1; |
| 95 | } |
| 96 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 97 | // Get condition bits for the current thumb instruction. |
| 98 | uint32_t ITSession::GetCond() |
| 99 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 100 | if (InITBlock()) |
| 101 | return Bits32(ITState, 7, 4); |
| 102 | else |
| 103 | return COND_AL; |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 106 | // ARM constants used during decoding |
| 107 | #define REG_RD 0 |
| 108 | #define LDM_REGLIST 1 |
| 109 | #define PC_REG 15 |
| 110 | #define PC_REGLIST_BIT 0x8000 |
| 111 | |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 112 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 113 | #define ARMv4T (1u << 1) |
| 114 | #define ARMv5T (1u << 2) |
| 115 | #define ARMv5TE (1u << 3) |
| 116 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 117 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 118 | #define ARMv6K (1u << 6) |
| 119 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 120 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 121 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 122 | #define ARMvAll (0xffffffffu) |
| 123 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 124 | #define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 125 | #define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 126 | #define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 127 | |
Johnny Chen | 0e00af2 | 2011-02-10 19:40:42 +0000 | [diff] [blame] | 128 | //---------------------------------------------------------------------- |
| 129 | // |
| 130 | // EmulateInstructionARM implementation |
| 131 | // |
| 132 | //---------------------------------------------------------------------- |
| 133 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 134 | void |
| 135 | EmulateInstructionARM::Initialize () |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 136 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 137 | } |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 138 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 139 | void |
| 140 | EmulateInstructionARM::Terminate () |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 141 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 144 | // Write "bits (32) UNKNOWN" to register n. Helper function for many ARM instructions. |
| 145 | bool |
| 146 | EmulateInstructionARM::WriteBits32Unknown (int n) |
| 147 | { |
| 148 | EmulateInstruction::Context context = { EmulateInstruction::eContextWriteRegisterRandomBits, |
| 149 | eRegisterKindDWARF, |
| 150 | dwarf_r0 + n, |
| 151 | 0 }; |
| 152 | |
Johnny Chen | 62ff6f5 | 2011-02-11 18:11:22 +0000 | [diff] [blame] | 153 | bool success; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 154 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 155 | |
| 156 | if (!success) |
| 157 | return false; |
| 158 | |
| 159 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 160 | return false; |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 165 | // Push Multiple Registers stores multiple registers to the stack, storing to |
| 166 | // consecutive memory locations ending just below the address in SP, and updates |
| 167 | // SP to point to the start of the stored data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 168 | bool |
| 169 | EmulateInstructionARM::EmulatePush (ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 170 | { |
| 171 | #if 0 |
| 172 | // ARM pseudo code... |
| 173 | if (ConditionPassed()) |
| 174 | { |
| 175 | EncodingSpecificOperations(); |
| 176 | NullCheckIfThumbEE(13); |
| 177 | address = SP - 4*BitCount(registers); |
| 178 | |
| 179 | for (i = 0 to 14) |
| 180 | { |
| 181 | if (registers<i> == ’1’) |
| 182 | { |
| 183 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 184 | MemA[address,4] = bits(32) UNKNOWN; |
| 185 | else |
| 186 | MemA[address,4] = R[i]; |
| 187 | address = address + 4; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 192 | MemA[address,4] = PCStoreValue(); |
| 193 | |
| 194 | SP = SP - 4*BitCount(registers); |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 199 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 200 | if (!success) |
| 201 | return false; |
| 202 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 203 | if (ConditionPassed()) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 204 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 205 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 206 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 207 | if (!success) |
| 208 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 209 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 210 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 211 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 212 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 213 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 214 | // The M bit represents LR. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 215 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 216 | registers |= (1u << 14); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 217 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 218 | if (BitCount(registers) < 1) |
| 219 | return false; |
| 220 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 221 | case eEncodingT2: |
| 222 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 223 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 224 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 225 | if (BitCount(registers) < 2) |
| 226 | return false; |
| 227 | break; |
| 228 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 229 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 230 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 231 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 232 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 233 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 234 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 235 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 236 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 237 | // Instead of return false, let's handle the following case as well, |
| 238 | // which amounts to pushing one reg onto the full descending stacks. |
| 239 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 240 | break; |
| 241 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 242 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 243 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 244 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 245 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 246 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 247 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 248 | default: |
| 249 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 250 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 251 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 252 | addr_t addr = sp - sp_offset; |
| 253 | uint32_t i; |
| 254 | |
| 255 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 256 | for (i=0; i<15; ++i) |
| 257 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 258 | if (BitIsSet (registers, i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 259 | { |
| 260 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 261 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 262 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 263 | if (!success) |
| 264 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 265 | if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 266 | return false; |
| 267 | addr += addr_byte_size; |
| 268 | } |
| 269 | } |
| 270 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 271 | if (BitIsSet (registers, 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 272 | { |
| 273 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 274 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 275 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 276 | if (!success) |
| 277 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 278 | if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
| 282 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 283 | context.arg0 = eRegisterKindGeneric; |
| 284 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 285 | context.arg2 = -sp_offset; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 286 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 287 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 293 | // Pop Multiple Registers loads multiple registers from the stack, loading from |
| 294 | // consecutive memory locations staring at the address in SP, and updates |
| 295 | // SP to point just above the loaded data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 296 | bool |
| 297 | EmulateInstructionARM::EmulatePop (ARMEncoding encoding) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 298 | { |
| 299 | #if 0 |
| 300 | // ARM pseudo code... |
| 301 | if (ConditionPassed()) |
| 302 | { |
| 303 | EncodingSpecificOperations(); NullCheckIfThumbEE(13); |
| 304 | address = SP; |
| 305 | for i = 0 to 14 |
| 306 | if registers<i> == ‘1’ then |
| 307 | R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4; |
| 308 | if registers<15> == ‘1’ then |
| 309 | if UnalignedAllowed then |
| 310 | LoadWritePC(MemU[address,4]); |
| 311 | else |
| 312 | LoadWritePC(MemA[address,4]); |
| 313 | if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers); |
| 314 | if registers<13> == ‘1’ then SP = bits(32) UNKNOWN; |
| 315 | } |
| 316 | #endif |
| 317 | |
| 318 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 319 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 320 | if (!success) |
| 321 | return false; |
| 322 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 323 | if (ConditionPassed()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 324 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 325 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 326 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 327 | if (!success) |
| 328 | return false; |
| 329 | uint32_t registers = 0; |
| 330 | uint32_t Rt; // the destination register |
| 331 | switch (encoding) { |
| 332 | case eEncodingT1: |
| 333 | registers = Bits32(opcode, 7, 0); |
| 334 | // The P bit represents PC. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 335 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 336 | registers |= (1u << 15); |
| 337 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 338 | if (BitCount(registers) < 1) |
| 339 | return false; |
| 340 | break; |
| 341 | case eEncodingT2: |
| 342 | // Ignore bit 13. |
| 343 | registers = Bits32(opcode, 15, 0) & ~0x2000; |
| 344 | // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 345 | if (BitCount(registers) < 2 || (Bit32(opcode, 15) && Bit32(opcode, 14))) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 346 | return false; |
| 347 | break; |
| 348 | case eEncodingT3: |
| 349 | Rt = Bits32(opcode, 15, 12); |
| 350 | // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE; |
| 351 | if (Rt == dwarf_sp) |
| 352 | return false; |
| 353 | registers = (1u << Rt); |
| 354 | break; |
| 355 | case eEncodingA1: |
| 356 | registers = Bits32(opcode, 15, 0); |
| 357 | // Instead of return false, let's handle the following case as well, |
| 358 | // which amounts to popping one reg from the full descending stacks. |
| 359 | // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD; |
| 360 | |
| 361 | // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 362 | if (Bit32(opcode, 13) && ArchVersion() >= ARMv7) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 363 | return false; |
| 364 | break; |
| 365 | case eEncodingA2: |
| 366 | Rt = Bits32(opcode, 15, 12); |
| 367 | // if t == 13 then UNPREDICTABLE; |
| 368 | if (Rt == dwarf_sp) |
| 369 | return false; |
| 370 | registers = (1u << Rt); |
| 371 | break; |
| 372 | default: |
| 373 | return false; |
| 374 | } |
| 375 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
| 376 | addr_t addr = sp; |
| 377 | uint32_t i, data; |
| 378 | |
| 379 | EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 }; |
| 380 | for (i=0; i<15; ++i) |
| 381 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 382 | if (BitIsSet (registers, i)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 383 | { |
| 384 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 385 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 386 | data = ReadMemoryUnsigned(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 387 | if (!success) |
| 388 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 389 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 390 | return false; |
| 391 | addr += addr_byte_size; |
| 392 | } |
| 393 | } |
| 394 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 395 | if (BitIsSet (registers, 15)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 396 | { |
| 397 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 398 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 399 | data = ReadMemoryUnsigned(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 400 | if (!success) |
| 401 | return false; |
Johnny Chen | f3eaacf | 2011-02-09 19:30:49 +0000 | [diff] [blame] | 402 | // In ARMv5T and above, this is an interworking branch. |
| 403 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 404 | return false; |
| 405 | addr += addr_byte_size; |
| 406 | } |
| 407 | |
| 408 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 409 | context.arg0 = eRegisterKindGeneric; |
| 410 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 411 | context.arg2 = sp_offset; |
| 412 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 413 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 419 | // Set r7 or ip to point to saved value residing within the stack. |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 420 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 421 | bool |
| 422 | EmulateInstructionARM::EmulateAddRdSPImmediate (ARMEncoding encoding) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 423 | { |
| 424 | #if 0 |
| 425 | // ARM pseudo code... |
| 426 | if (ConditionPassed()) |
| 427 | { |
| 428 | EncodingSpecificOperations(); |
| 429 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 430 | if d == 15 then |
| 431 | ALUWritePC(result); // setflags is always FALSE here |
| 432 | else |
| 433 | R[d] = result; |
| 434 | if setflags then |
| 435 | APSR.N = result<31>; |
| 436 | APSR.Z = IsZeroBit(result); |
| 437 | APSR.C = carry; |
| 438 | APSR.V = overflow; |
| 439 | } |
| 440 | #endif |
| 441 | |
| 442 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 443 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 444 | if (!success) |
| 445 | return false; |
| 446 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 447 | if (ConditionPassed()) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 448 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 449 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 450 | if (!success) |
| 451 | return false; |
| 452 | uint32_t Rd; // the destination register |
| 453 | uint32_t imm32; |
| 454 | switch (encoding) { |
| 455 | case eEncodingT1: |
| 456 | Rd = 7; |
| 457 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32) |
| 458 | break; |
| 459 | case eEncodingA1: |
| 460 | Rd = Bits32(opcode, 15, 12); |
| 461 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 462 | break; |
| 463 | default: |
| 464 | return false; |
| 465 | } |
| 466 | addr_t sp_offset = imm32; |
| 467 | addr_t addr = sp + sp_offset; // a pointer to the stack area |
| 468 | |
| 469 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 470 | eRegisterKindGeneric, |
| 471 | LLDB_REGNUM_GENERIC_SP, |
| 472 | sp_offset }; |
| 473 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 474 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr)) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 480 | // Set r7 or ip to the current stack pointer. |
| 481 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 482 | bool |
| 483 | EmulateInstructionARM::EmulateMovRdSP (ARMEncoding encoding) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 484 | { |
| 485 | #if 0 |
| 486 | // ARM pseudo code... |
| 487 | if (ConditionPassed()) |
| 488 | { |
| 489 | EncodingSpecificOperations(); |
| 490 | result = R[m]; |
| 491 | if d == 15 then |
| 492 | ALUWritePC(result); // setflags is always FALSE here |
| 493 | else |
| 494 | R[d] = result; |
| 495 | if setflags then |
| 496 | APSR.N = result<31>; |
| 497 | APSR.Z = IsZeroBit(result); |
| 498 | // APSR.C unchanged |
| 499 | // APSR.V unchanged |
| 500 | } |
| 501 | #endif |
| 502 | |
| 503 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 504 | //const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 505 | //if (!success) |
| 506 | // return false; |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 507 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 508 | if (ConditionPassed()) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 509 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 510 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 511 | if (!success) |
| 512 | return false; |
| 513 | uint32_t Rd; // the destination register |
| 514 | switch (encoding) { |
| 515 | case eEncodingT1: |
| 516 | Rd = 7; |
| 517 | break; |
| 518 | case eEncodingA1: |
| 519 | Rd = 12; |
| 520 | break; |
| 521 | default: |
| 522 | return false; |
| 523 | } |
| 524 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 525 | eRegisterKindGeneric, |
| 526 | LLDB_REGNUM_GENERIC_SP, |
| 527 | 0 }; |
| 528 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 529 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp)) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 535 | // Move from high register (r8-r15) to low register (r0-r7). |
| 536 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 537 | bool |
| 538 | EmulateInstructionARM::EmulateMovLowHigh (ARMEncoding encoding) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 539 | { |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 540 | return EmulateMovRdRm (encoding); |
| 541 | } |
| 542 | |
| 543 | // Move from register to register. |
| 544 | // MOV (register) |
| 545 | bool |
| 546 | EmulateInstructionARM::EmulateMovRdRm (ARMEncoding encoding) |
| 547 | { |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 548 | #if 0 |
| 549 | // ARM pseudo code... |
| 550 | if (ConditionPassed()) |
| 551 | { |
| 552 | EncodingSpecificOperations(); |
| 553 | result = R[m]; |
| 554 | if d == 15 then |
| 555 | ALUWritePC(result); // setflags is always FALSE here |
| 556 | else |
| 557 | R[d] = result; |
| 558 | if setflags then |
| 559 | APSR.N = result<31>; |
| 560 | APSR.Z = IsZeroBit(result); |
| 561 | // APSR.C unchanged |
| 562 | // APSR.V unchanged |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 567 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 568 | if (!success) |
| 569 | return false; |
| 570 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 571 | if (ConditionPassed()) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 572 | { |
| 573 | uint32_t Rm; // the source register |
| 574 | uint32_t Rd; // the destination register |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 575 | bool setflags; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 576 | switch (encoding) { |
| 577 | case eEncodingT1: |
| 578 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 579 | Rd = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 1); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 580 | setflags = false; |
| 581 | break; |
| 582 | case eEncodingT2: |
| 583 | Rm = Bits32(opcode, 5, 3); |
| 584 | Rd = Bits32(opcode, 2, 1); |
| 585 | setflags = true; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 586 | break; |
| 587 | default: |
| 588 | return false; |
| 589 | } |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 590 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 591 | if (!success) |
| 592 | return false; |
| 593 | |
| 594 | // The context specifies that Rm is to be moved into Rd. |
| 595 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 596 | eRegisterKindDWARF, |
| 597 | dwarf_r0 + Rm, |
| 598 | 0 }; |
| 599 | |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 600 | if (Rd == 15) |
| 601 | { |
| 602 | if (!ALUWritePC (context, reg_value)) |
| 603 | return false; |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value)) |
| 608 | return false; |
| 609 | if (setflags) |
| 610 | { |
| 611 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 612 | SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(reg_value, CPSR_N)); |
| 613 | SetBit32(m_new_inst_cpsr, CPSR_Z, reg_value == 0 ? 1 : 0); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 614 | if (m_new_inst_cpsr != m_inst_cpsr) |
| 615 | { |
| 616 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
| 617 | return false; |
| 618 | } |
| 619 | } |
| 620 | } |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 621 | } |
| 622 | return true; |
| 623 | } |
| 624 | |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 625 | // PC relative immediate load into register, possibly followed by ADD (SP plus register). |
| 626 | // LDR (literal) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 627 | bool |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 628 | EmulateInstructionARM::EmulateLDRRtPCRelative (ARMEncoding encoding) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 629 | { |
| 630 | #if 0 |
| 631 | // ARM pseudo code... |
| 632 | if (ConditionPassed()) |
| 633 | { |
| 634 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 635 | base = Align(PC,4); |
| 636 | address = if add then (base + imm32) else (base - imm32); |
| 637 | data = MemU[address,4]; |
| 638 | if t == 15 then |
| 639 | if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 640 | elsif UnalignedSupport() || address<1:0> = ‘00’ then |
| 641 | R[t] = data; |
| 642 | else // Can only apply before ARMv7 |
| 643 | if CurrentInstrSet() == InstrSet_ARM then |
| 644 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 645 | else |
| 646 | R[t] = bits(32) UNKNOWN; |
| 647 | } |
| 648 | #endif |
| 649 | |
| 650 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 651 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 652 | if (!success) |
| 653 | return false; |
| 654 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 655 | if (ConditionPassed()) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 656 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 657 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 658 | if (!success) |
| 659 | return false; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 660 | |
| 661 | // PC relative immediate load context |
| 662 | EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset, |
| 663 | eRegisterKindGeneric, |
| 664 | LLDB_REGNUM_GENERIC_PC, |
| 665 | 0}; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 666 | uint32_t Rt; // the destination register |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 667 | uint32_t imm32; // immediate offset from the PC |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 668 | bool add; // +imm32 or -imm32? |
| 669 | addr_t base; // the base address |
| 670 | addr_t address; // the PC relative address |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 671 | uint32_t data; // the literal data value from the PC relative load |
| 672 | switch (encoding) { |
| 673 | case eEncodingT1: |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 674 | Rt = Bits32(opcode, 10, 8); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 675 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 676 | add = true; |
| 677 | base = Align(pc + 4, 4); |
| 678 | context.arg2 = 4 + imm32; |
| 679 | break; |
| 680 | case eEncodingT2: |
| 681 | Rt = Bits32(opcode, 15, 12); |
| 682 | imm32 = Bits32(opcode, 11, 0) << 2; // imm32 = ZeroExtend(imm12, 32); |
| 683 | add = BitIsSet(opcode, 23); |
| 684 | if (Rt == 15 |
| 685 | && m_it_session.InITBlock() |
| 686 | && !m_it_session.LastInITBlock()) |
| 687 | return false; |
| 688 | base = Align(pc + 4, 4); |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 689 | context.arg2 = 4 + imm32; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 690 | break; |
| 691 | default: |
| 692 | return false; |
| 693 | } |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 694 | |
| 695 | if (add) |
| 696 | address = base + imm32; |
| 697 | else |
| 698 | address = base - imm32; |
| 699 | data = ReadMemoryUnsigned(context, address, 4, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 700 | if (!success) |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 701 | return false; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 702 | |
| 703 | if (Rt == 15) |
| 704 | { |
| 705 | if (Bits32(address, 1, 0) == 0) |
| 706 | { |
| 707 | // In ARMv5T and above, this is an interworking branch. |
| 708 | if (!LoadWritePC(context, data)) |
| 709 | return false; |
| 710 | } |
| 711 | else |
| 712 | return false; |
| 713 | } |
| 714 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 715 | { |
| 716 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 717 | return false; |
| 718 | } |
| 719 | else // We don't handle ARM for now. |
| 720 | return false; |
| 721 | |
| 722 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 723 | return false; |
| 724 | } |
| 725 | return true; |
| 726 | } |
| 727 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 728 | // An add operation to adjust the SP. |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 729 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 730 | bool |
| 731 | EmulateInstructionARM::EmulateAddSPImmediate (ARMEncoding encoding) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 732 | { |
| 733 | #if 0 |
| 734 | // ARM pseudo code... |
| 735 | if (ConditionPassed()) |
| 736 | { |
| 737 | EncodingSpecificOperations(); |
| 738 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 739 | if d == 15 then // Can only occur for ARM encoding |
| 740 | ALUWritePC(result); // setflags is always FALSE here |
| 741 | else |
| 742 | R[d] = result; |
| 743 | if setflags then |
| 744 | APSR.N = result<31>; |
| 745 | APSR.Z = IsZeroBit(result); |
| 746 | APSR.C = carry; |
| 747 | APSR.V = overflow; |
| 748 | } |
| 749 | #endif |
| 750 | |
| 751 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 752 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 753 | if (!success) |
| 754 | return false; |
| 755 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 756 | if (ConditionPassed()) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 757 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 758 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 759 | if (!success) |
| 760 | return false; |
| 761 | uint32_t imm32; // the immediate operand |
| 762 | switch (encoding) { |
| 763 | case eEncodingT2: |
| 764 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
| 765 | break; |
| 766 | default: |
| 767 | return false; |
| 768 | } |
| 769 | addr_t sp_offset = imm32; |
| 770 | addr_t addr = sp + sp_offset; // the adjusted stack pointer value |
| 771 | |
| 772 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 773 | eRegisterKindGeneric, |
| 774 | LLDB_REGNUM_GENERIC_SP, |
| 775 | sp_offset }; |
| 776 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 777 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 778 | return false; |
| 779 | } |
| 780 | return true; |
| 781 | } |
| 782 | |
| 783 | // An add operation to adjust the SP. |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 784 | // ADD (SP plus register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 785 | bool |
| 786 | EmulateInstructionARM::EmulateAddSPRm (ARMEncoding encoding) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 787 | { |
| 788 | #if 0 |
| 789 | // ARM pseudo code... |
| 790 | if (ConditionPassed()) |
| 791 | { |
| 792 | EncodingSpecificOperations(); |
| 793 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 794 | (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’); |
| 795 | if d == 15 then |
| 796 | ALUWritePC(result); // setflags is always FALSE here |
| 797 | else |
| 798 | R[d] = result; |
| 799 | if setflags then |
| 800 | APSR.N = result<31>; |
| 801 | APSR.Z = IsZeroBit(result); |
| 802 | APSR.C = carry; |
| 803 | APSR.V = overflow; |
| 804 | } |
| 805 | #endif |
| 806 | |
| 807 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 808 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 809 | if (!success) |
| 810 | return false; |
| 811 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 812 | if (ConditionPassed()) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 813 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 814 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 815 | if (!success) |
| 816 | return false; |
| 817 | uint32_t Rm; // the second operand |
| 818 | switch (encoding) { |
| 819 | case eEncodingT2: |
| 820 | Rm = Bits32(opcode, 6, 3); |
| 821 | break; |
| 822 | default: |
| 823 | return false; |
| 824 | } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 825 | int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 826 | if (!success) |
| 827 | return false; |
| 828 | |
| 829 | addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value |
| 830 | |
| 831 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 832 | eRegisterKindGeneric, |
| 833 | LLDB_REGNUM_GENERIC_SP, |
| 834 | reg_value }; |
| 835 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 836 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 837 | return false; |
| 838 | } |
| 839 | return true; |
| 840 | } |
| 841 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 842 | // Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine |
| 843 | // at a PC-relative address, and changes instruction set from ARM to Thumb, or |
| 844 | // from Thumb to ARM. |
| 845 | // BLX (immediate) |
| 846 | bool |
| 847 | EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding) |
| 848 | { |
| 849 | #if 0 |
| 850 | // ARM pseudo code... |
| 851 | if (ConditionPassed()) |
| 852 | { |
| 853 | EncodingSpecificOperations(); |
| 854 | if CurrentInstrSet() == InstrSet_ARM then |
| 855 | LR = PC - 4; |
| 856 | else |
| 857 | LR = PC<31:1> : '1'; |
| 858 | if targetInstrSet == InstrSet_ARM then |
| 859 | targetAddress = Align(PC,4) + imm32; |
| 860 | else |
| 861 | targetAddress = PC + imm32; |
| 862 | SelectInstrSet(targetInstrSet); |
| 863 | BranchWritePC(targetAddress); |
| 864 | } |
| 865 | #endif |
| 866 | |
| 867 | bool success = false; |
| 868 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 869 | if (!success) |
| 870 | return false; |
| 871 | |
| 872 | if (ConditionPassed()) |
| 873 | { |
| 874 | EmulateInstruction::Context context = { EmulateInstruction::eContextRelativeBranchImmediate, 0, 0, 0}; |
| 875 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 876 | if (!success) |
| 877 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 878 | addr_t lr; // next instruction address |
| 879 | addr_t target; // target address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 880 | int32_t imm32; // PC-relative offset |
| 881 | switch (encoding) { |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 882 | case eEncodingT1: |
| 883 | { |
| 884 | lr = (pc + 4) | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 885 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 886 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 887 | uint32_t J1 = Bit32(opcode, 13); |
| 888 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 889 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 890 | uint32_t I1 = !(J1 ^ S); |
| 891 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 892 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 893 | imm32 = llvm::SignExtend32<25>(imm25); |
| 894 | target = pc + 4 + imm32; |
| 895 | context.arg1 = 4 + imm32; // signed offset |
| 896 | context.arg2 = eModeThumb; // target instruction set |
| 897 | break; |
| 898 | } |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 899 | case eEncodingT2: |
| 900 | { |
| 901 | lr = (pc + 4) | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 902 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 903 | uint32_t imm10H = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 904 | uint32_t J1 = Bit32(opcode, 13); |
| 905 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 906 | uint32_t imm10L = Bits32(opcode, 10, 1); |
| 907 | uint32_t I1 = !(J1 ^ S); |
| 908 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 909 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10H << 12) | (imm10L << 2); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 910 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 911 | target = Align(pc + 4, 4) + imm32; |
| 912 | context.arg1 = 4 + imm32; // signed offset |
| 913 | context.arg2 = eModeARM; // target instruction set |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 914 | break; |
| 915 | } |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 916 | case eEncodingA1: |
| 917 | lr = pc + 4; // return address |
| 918 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 919 | target = Align(pc + 8, 4) + imm32; |
| 920 | context.arg1 = 8 + imm32; // signed offset |
| 921 | context.arg2 = eModeARM; // target instruction set |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 922 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 923 | case eEncodingA2: |
| 924 | lr = pc + 4; // return address |
| 925 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1); |
| 926 | target = pc + 8 + imm32; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 927 | context.arg1 = 8 + imm32; // signed offset |
| 928 | context.arg2 = eModeThumb; // target instruction set |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 929 | break; |
| 930 | default: |
| 931 | return false; |
| 932 | } |
| 933 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 934 | return false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 935 | if (!BranchWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 936 | return false; |
| 937 | } |
| 938 | return true; |
| 939 | } |
| 940 | |
| 941 | // Branch with Link and Exchange (register) calls a subroutine at an address and |
| 942 | // instruction set specified by a register. |
| 943 | // BLX (register) |
| 944 | bool |
| 945 | EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding) |
| 946 | { |
| 947 | #if 0 |
| 948 | // ARM pseudo code... |
| 949 | if (ConditionPassed()) |
| 950 | { |
| 951 | EncodingSpecificOperations(); |
| 952 | target = R[m]; |
| 953 | if CurrentInstrSet() == InstrSet_ARM then |
| 954 | next_instr_addr = PC - 4; |
| 955 | LR = next_instr_addr; |
| 956 | else |
| 957 | next_instr_addr = PC - 2; |
| 958 | LR = next_instr_addr<31:1> : ‘1’; |
| 959 | BXWritePC(target); |
| 960 | } |
| 961 | #endif |
| 962 | |
| 963 | bool success = false; |
| 964 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 965 | if (!success) |
| 966 | return false; |
| 967 | |
| 968 | if (ConditionPassed()) |
| 969 | { |
| 970 | EmulateInstruction::Context context = { EmulateInstruction::eContextAbsoluteBranchRegister, 0, 0, 0}; |
| 971 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 972 | addr_t lr; // next instruction address |
| 973 | addr_t target; // target address |
| 974 | if (!success) |
| 975 | return false; |
| 976 | uint32_t Rm; // the register with the target address |
| 977 | switch (encoding) { |
| 978 | case eEncodingT1: |
| 979 | lr = (pc + 2) | 1u; // return address |
| 980 | Rm = Bits32(opcode, 6, 3); |
| 981 | // if m == 15 then UNPREDICTABLE; |
| 982 | if (Rm == 15) |
| 983 | return false; |
| 984 | target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 985 | break; |
| 986 | case eEncodingA1: |
| 987 | lr = pc + 4; // return address |
| 988 | Rm = Bits32(opcode, 3, 0); |
| 989 | // if m == 15 then UNPREDICTABLE; |
| 990 | if (Rm == 15) |
| 991 | return false; |
| 992 | target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 993 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 994 | default: |
| 995 | return false; |
| 996 | } |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 997 | context.arg0 = eRegisterKindDWARF; |
| 998 | context.arg1 = dwarf_r0 + Rm; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 999 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1000 | return false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1001 | if (!BXWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1002 | return false; |
| 1003 | } |
| 1004 | return true; |
| 1005 | } |
| 1006 | |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1007 | // Set r7 to point to some ip offset. |
| 1008 | // SUB (immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1009 | bool |
| 1010 | EmulateInstructionARM::EmulateSubR7IPImmediate (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1011 | { |
| 1012 | #if 0 |
| 1013 | // ARM pseudo code... |
| 1014 | if (ConditionPassed()) |
| 1015 | { |
| 1016 | EncodingSpecificOperations(); |
| 1017 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1018 | if d == 15 then // Can only occur for ARM encoding |
| 1019 | ALUWritePC(result); // setflags is always FALSE here |
| 1020 | else |
| 1021 | R[d] = result; |
| 1022 | if setflags then |
| 1023 | APSR.N = result<31>; |
| 1024 | APSR.Z = IsZeroBit(result); |
| 1025 | APSR.C = carry; |
| 1026 | APSR.V = overflow; |
| 1027 | } |
| 1028 | #endif |
| 1029 | |
| 1030 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1031 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1032 | if (!success) |
| 1033 | return false; |
| 1034 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1035 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1036 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1037 | const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1038 | if (!success) |
| 1039 | return false; |
| 1040 | uint32_t imm32; |
| 1041 | switch (encoding) { |
| 1042 | case eEncodingA1: |
| 1043 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1044 | break; |
| 1045 | default: |
| 1046 | return false; |
| 1047 | } |
| 1048 | addr_t ip_offset = imm32; |
| 1049 | addr_t addr = ip - ip_offset; // the adjusted ip value |
| 1050 | |
| 1051 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 1052 | eRegisterKindDWARF, |
| 1053 | dwarf_r12, |
| 1054 | -ip_offset }; |
| 1055 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1056 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1057 | return false; |
| 1058 | } |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
| 1062 | // Set ip to point to some stack offset. |
| 1063 | // SUB (SP minus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1064 | bool |
| 1065 | EmulateInstructionARM::EmulateSubIPSPImmediate (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1066 | { |
| 1067 | #if 0 |
| 1068 | // ARM pseudo code... |
| 1069 | if (ConditionPassed()) |
| 1070 | { |
| 1071 | EncodingSpecificOperations(); |
| 1072 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1073 | if d == 15 then // Can only occur for ARM encoding |
| 1074 | ALUWritePC(result); // setflags is always FALSE here |
| 1075 | else |
| 1076 | R[d] = result; |
| 1077 | if setflags then |
| 1078 | APSR.N = result<31>; |
| 1079 | APSR.Z = IsZeroBit(result); |
| 1080 | APSR.C = carry; |
| 1081 | APSR.V = overflow; |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
| 1085 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1086 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1087 | if (!success) |
| 1088 | return false; |
| 1089 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1090 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1091 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1092 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1093 | if (!success) |
| 1094 | return false; |
| 1095 | uint32_t imm32; |
| 1096 | switch (encoding) { |
| 1097 | case eEncodingA1: |
| 1098 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1099 | break; |
| 1100 | default: |
| 1101 | return false; |
| 1102 | } |
| 1103 | addr_t sp_offset = imm32; |
| 1104 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1105 | |
| 1106 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 1107 | eRegisterKindGeneric, |
| 1108 | LLDB_REGNUM_GENERIC_SP, |
| 1109 | -sp_offset }; |
| 1110 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1111 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1117 | // A sub operation to adjust the SP -- allocate space for local storage. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1118 | bool |
| 1119 | EmulateInstructionARM::EmulateSubSPImmdiate (ARMEncoding encoding) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1120 | { |
| 1121 | #if 0 |
| 1122 | // ARM pseudo code... |
| 1123 | if (ConditionPassed()) |
| 1124 | { |
| 1125 | EncodingSpecificOperations(); |
| 1126 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1127 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1128 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1129 | else |
| 1130 | R[d] = result; |
| 1131 | if setflags then |
| 1132 | APSR.N = result<31>; |
| 1133 | APSR.Z = IsZeroBit(result); |
| 1134 | APSR.C = carry; |
| 1135 | APSR.V = overflow; |
| 1136 | } |
| 1137 | #endif |
| 1138 | |
| 1139 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1140 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1141 | if (!success) |
| 1142 | return false; |
| 1143 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1144 | if (ConditionPassed()) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1145 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1146 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1147 | if (!success) |
| 1148 | return false; |
| 1149 | uint32_t imm32; |
| 1150 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 1151 | case eEncodingT1: |
| 1152 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1153 | case eEncodingT2: |
| 1154 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 1155 | break; |
| 1156 | case eEncodingT3: |
| 1157 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 1158 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1159 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1160 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1161 | break; |
| 1162 | default: |
| 1163 | return false; |
| 1164 | } |
| 1165 | addr_t sp_offset = imm32; |
| 1166 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1167 | |
| 1168 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 1169 | eRegisterKindGeneric, |
| 1170 | LLDB_REGNUM_GENERIC_SP, |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1171 | -sp_offset }; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1172 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1173 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1174 | return false; |
| 1175 | } |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1179 | // A store operation to the stack that also updates the SP. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1180 | bool |
| 1181 | EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1182 | { |
| 1183 | #if 0 |
| 1184 | // ARM pseudo code... |
| 1185 | if (ConditionPassed()) |
| 1186 | { |
| 1187 | EncodingSpecificOperations(); |
| 1188 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 1189 | address = if index then offset_addr else R[n]; |
| 1190 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 1191 | if wback then R[n] = offset_addr; |
| 1192 | } |
| 1193 | #endif |
| 1194 | |
| 1195 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1196 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1197 | if (!success) |
| 1198 | return false; |
| 1199 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1200 | if (ConditionPassed()) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1201 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1202 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1203 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1204 | if (!success) |
| 1205 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1206 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1207 | uint32_t imm12; |
| 1208 | switch (encoding) { |
| 1209 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 1210 | Rt = Bits32(opcode, 15, 12); |
| 1211 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | default: |
| 1214 | return false; |
| 1215 | } |
| 1216 | addr_t sp_offset = imm12; |
| 1217 | addr_t addr = sp - sp_offset; |
| 1218 | |
| 1219 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1220 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1221 | { |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1222 | context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number |
| 1223 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1224 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1225 | if (!success) |
| 1226 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1227 | if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1228 | return false; |
| 1229 | } |
| 1230 | else |
| 1231 | { |
| 1232 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 1233 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1234 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1235 | if (!success) |
| 1236 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1237 | if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1242 | context.arg0 = eRegisterKindGeneric; |
| 1243 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1244 | context.arg2 = -sp_offset; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1245 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1246 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1252 | // Vector Push stores multiple extension registers to the stack. |
| 1253 | // It also updates SP to point to the start of the stored data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1254 | bool |
| 1255 | EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1256 | { |
| 1257 | #if 0 |
| 1258 | // ARM pseudo code... |
| 1259 | if (ConditionPassed()) |
| 1260 | { |
| 1261 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1262 | address = SP - imm32; |
| 1263 | SP = SP - imm32; |
| 1264 | if single_regs then |
| 1265 | for r = 0 to regs-1 |
| 1266 | MemA[address,4] = S[d+r]; address = address+4; |
| 1267 | else |
| 1268 | for r = 0 to regs-1 |
| 1269 | // Store as two word-aligned words in the correct order for current endianness. |
| 1270 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 1271 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 1272 | address = address+8; |
| 1273 | } |
| 1274 | #endif |
| 1275 | |
| 1276 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1277 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1278 | if (!success) |
| 1279 | return false; |
| 1280 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1281 | if (ConditionPassed()) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1282 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1283 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1284 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1285 | if (!success) |
| 1286 | return false; |
| 1287 | bool single_regs; |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1288 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1289 | uint32_t imm32; // stack offset |
| 1290 | uint32_t regs; // number of registers |
| 1291 | switch (encoding) { |
| 1292 | case eEncodingT1: |
| 1293 | case eEncodingA1: |
| 1294 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1295 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1296 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1297 | // If UInt(imm8) is odd, see "FSTMX". |
| 1298 | regs = Bits32(opcode, 7, 0) / 2; |
| 1299 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1300 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1301 | return false; |
| 1302 | break; |
| 1303 | case eEncodingT2: |
| 1304 | case eEncodingA2: |
| 1305 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1306 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1307 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1308 | regs = Bits32(opcode, 7, 0); |
| 1309 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1310 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1311 | return false; |
| 1312 | break; |
| 1313 | default: |
| 1314 | return false; |
| 1315 | } |
| 1316 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1317 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1318 | addr_t sp_offset = imm32; |
| 1319 | addr_t addr = sp - sp_offset; |
| 1320 | uint32_t i; |
| 1321 | |
| 1322 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 1323 | for (i=d; i<regs; ++i) |
| 1324 | { |
| 1325 | context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number |
| 1326 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 1327 | // uint64_t to accommodate 64-bit registers. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1328 | uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1329 | if (!success) |
| 1330 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1331 | if (!WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1332 | return false; |
| 1333 | addr += reg_byte_size; |
| 1334 | } |
| 1335 | |
| 1336 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1337 | context.arg0 = eRegisterKindGeneric; |
| 1338 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1339 | context.arg2 = -sp_offset; |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1340 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1341 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1342 | return false; |
| 1343 | } |
| 1344 | return true; |
| 1345 | } |
| 1346 | |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1347 | // Vector Pop loads multiple extension registers from the stack. |
| 1348 | // It also updates SP to point just above the loaded data. |
| 1349 | bool |
| 1350 | EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding) |
| 1351 | { |
| 1352 | #if 0 |
| 1353 | // ARM pseudo code... |
| 1354 | if (ConditionPassed()) |
| 1355 | { |
| 1356 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1357 | address = SP; |
| 1358 | SP = SP + imm32; |
| 1359 | if single_regs then |
| 1360 | for r = 0 to regs-1 |
| 1361 | S[d+r] = MemA[address,4]; address = address+4; |
| 1362 | else |
| 1363 | for r = 0 to regs-1 |
| 1364 | word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8; |
| 1365 | // Combine the word-aligned words in the correct order for current endianness. |
| 1366 | D[d+r] = if BigEndian() then word1:word2 else word2:word1; |
| 1367 | } |
| 1368 | #endif |
| 1369 | |
| 1370 | bool success = false; |
| 1371 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1372 | if (!success) |
| 1373 | return false; |
| 1374 | |
| 1375 | if (ConditionPassed()) |
| 1376 | { |
| 1377 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1378 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 1379 | if (!success) |
| 1380 | return false; |
| 1381 | bool single_regs; |
| 1382 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
| 1383 | uint32_t imm32; // stack offset |
| 1384 | uint32_t regs; // number of registers |
| 1385 | switch (encoding) { |
| 1386 | case eEncodingT1: |
| 1387 | case eEncodingA1: |
| 1388 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1389 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1390 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1391 | // If UInt(imm8) is odd, see "FLDMX". |
| 1392 | regs = Bits32(opcode, 7, 0) / 2; |
| 1393 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1394 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1395 | return false; |
| 1396 | break; |
| 1397 | case eEncodingT2: |
| 1398 | case eEncodingA2: |
| 1399 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1400 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1401 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1402 | regs = Bits32(opcode, 7, 0); |
| 1403 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1404 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1405 | return false; |
| 1406 | break; |
| 1407 | default: |
| 1408 | return false; |
| 1409 | } |
| 1410 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1411 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1412 | addr_t sp_offset = imm32; |
| 1413 | addr_t addr = sp; |
| 1414 | uint32_t i; |
| 1415 | uint64_t data; // uint64_t to accomodate 64-bit registers. |
| 1416 | |
| 1417 | EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 }; |
| 1418 | for (i=d; i<regs; ++i) |
| 1419 | { |
| 1420 | context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number |
| 1421 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 1422 | data = ReadMemoryUnsigned(context, addr, reg_byte_size, 0, &success); |
| 1423 | if (!success) |
| 1424 | return false; |
| 1425 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data)) |
| 1426 | return false; |
| 1427 | addr += reg_byte_size; |
| 1428 | } |
| 1429 | |
| 1430 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1431 | context.arg0 = eRegisterKindGeneric; |
| 1432 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 1433 | context.arg2 = sp_offset; |
| 1434 | |
| 1435 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
| 1436 | return false; |
| 1437 | } |
| 1438 | return true; |
| 1439 | } |
| 1440 | |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1441 | // SVC (previously SWI) |
| 1442 | bool |
| 1443 | EmulateInstructionARM::EmulateSVC (ARMEncoding encoding) |
| 1444 | { |
| 1445 | #if 0 |
| 1446 | // ARM pseudo code... |
| 1447 | if (ConditionPassed()) |
| 1448 | { |
| 1449 | EncodingSpecificOperations(); |
| 1450 | CallSupervisor(); |
| 1451 | } |
| 1452 | #endif |
| 1453 | |
| 1454 | bool success = false; |
| 1455 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1456 | if (!success) |
| 1457 | return false; |
| 1458 | |
| 1459 | if (ConditionPassed()) |
| 1460 | { |
| 1461 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1462 | addr_t lr; // next instruction address |
| 1463 | if (!success) |
| 1464 | return false; |
| 1465 | uint32_t imm32; // the immediate constant |
| 1466 | uint32_t mode; // ARM or Thumb mode |
| 1467 | switch (encoding) { |
| 1468 | case eEncodingT1: |
| 1469 | lr = (pc + 2) | 1u; // return address |
| 1470 | imm32 = Bits32(opcode, 7, 0); |
| 1471 | mode = eModeThumb; |
| 1472 | break; |
| 1473 | case eEncodingA1: |
| 1474 | lr = pc + 4; // return address |
| 1475 | imm32 = Bits32(opcode, 23, 0); |
| 1476 | mode = eModeARM; |
| 1477 | break; |
| 1478 | default: |
| 1479 | return false; |
| 1480 | } |
| 1481 | EmulateInstruction::Context context = { EmulateInstruction::eContextSupervisorCall, mode, imm32, 0}; |
| 1482 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1483 | return false; |
| 1484 | } |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1488 | // If Then makes up to four following instructions (the IT block) conditional. |
| 1489 | bool |
| 1490 | EmulateInstructionARM::EmulateIT (ARMEncoding encoding) |
| 1491 | { |
| 1492 | #if 0 |
| 1493 | // ARM pseudo code... |
| 1494 | EncodingSpecificOperations(); |
| 1495 | ITSTATE.IT<7:0> = firstcond:mask; |
| 1496 | #endif |
| 1497 | |
| 1498 | bool success = false; |
| 1499 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1500 | if (!success) |
| 1501 | return false; |
| 1502 | |
| 1503 | m_it_session.InitIT(Bits32(opcode, 7, 0)); |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 1507 | // Branch causes a branch to a target address. |
| 1508 | bool |
| 1509 | EmulateInstructionARM::EmulateB (ARMEncoding encoding) |
| 1510 | { |
| 1511 | #if 0 |
| 1512 | // ARM pseudo code... |
| 1513 | if (ConditionPassed()) |
| 1514 | { |
| 1515 | EncodingSpecificOperations(); |
| 1516 | BranchWritePC(PC + imm32); |
| 1517 | } |
| 1518 | #endif |
| 1519 | |
| 1520 | bool success = false; |
| 1521 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1522 | if (!success) |
| 1523 | return false; |
| 1524 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1525 | if (ConditionPassed()) |
| 1526 | { |
| 1527 | EmulateInstruction::Context context = { EmulateInstruction::eContextRelativeBranchImmediate, 0, 0, 0}; |
| 1528 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1529 | if (!success) |
| 1530 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1531 | addr_t target; // target address |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1532 | int32_t imm32; // PC-relative offset |
| 1533 | switch (encoding) { |
| 1534 | case eEncodingT1: |
| 1535 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 1536 | imm32 = llvm::SignExtend32<9>(Bits32(opcode, 7, 0) << 1); |
| 1537 | target = pc + 4 + imm32; |
| 1538 | context.arg1 = 4 + imm32; // signed offset |
| 1539 | context.arg2 = eModeThumb; // target instruction set |
| 1540 | break; |
| 1541 | case eEncodingT2: |
| 1542 | imm32 = llvm::SignExtend32<12>(Bits32(opcode, 10, 0)); |
| 1543 | target = pc + 4 + imm32; |
| 1544 | context.arg1 = 4 + imm32; // signed offset |
| 1545 | context.arg2 = eModeThumb; // target instruction set |
| 1546 | break; |
| 1547 | case eEncodingT3: |
| 1548 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 1549 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1550 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1551 | uint32_t imm6 = Bits32(opcode, 21, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1552 | uint32_t J1 = Bit32(opcode, 13); |
| 1553 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1554 | uint32_t imm11 = Bits32(opcode, 10, 0); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1555 | uint32_t imm21 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1556 | imm32 = llvm::SignExtend32<21>(imm21); |
| 1557 | target = pc + 4 + imm32; |
| 1558 | context.arg1 = eModeThumb; // target instruction set |
| 1559 | context.arg2 = 4 + imm32; // signed offset |
| 1560 | break; |
| 1561 | } |
| 1562 | case eEncodingT4: |
| 1563 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1564 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1565 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1566 | uint32_t J1 = Bit32(opcode, 13); |
| 1567 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1568 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 1569 | uint32_t I1 = !(J1 ^ S); |
| 1570 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1571 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1572 | imm32 = llvm::SignExtend32<25>(imm25); |
| 1573 | target = pc + 4 + imm32; |
| 1574 | context.arg1 = eModeThumb; // target instruction set |
| 1575 | context.arg2 = 4 + imm32; // signed offset |
| 1576 | break; |
| 1577 | } |
| 1578 | case eEncodingA1: |
| 1579 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
| 1580 | target = pc + 8 + imm32; |
| 1581 | context.arg1 = eModeARM; // target instruction set |
| 1582 | context.arg2 = 8 + imm32; // signed offset |
| 1583 | break; |
| 1584 | default: |
| 1585 | return false; |
| 1586 | } |
| 1587 | if (!BranchWritePC(context, target)) |
| 1588 | return false; |
| 1589 | } |
| 1590 | return true; |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1593 | // Compare and Branch on Nonzero and Compare and Branch on Zero compare the value in a register with |
| 1594 | // zero and conditionally branch forward a constant value. They do not affect the condition flags. |
| 1595 | // CBNZ, CBZ |
| 1596 | bool |
| 1597 | EmulateInstructionARM::EmulateCB (ARMEncoding encoding) |
| 1598 | { |
| 1599 | #if 0 |
| 1600 | // ARM pseudo code... |
| 1601 | EncodingSpecificOperations(); |
| 1602 | if nonzero ^ IsZero(R[n]) then |
| 1603 | BranchWritePC(PC + imm32); |
| 1604 | #endif |
| 1605 | |
| 1606 | bool success = false; |
| 1607 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1608 | if (!success) |
| 1609 | return false; |
| 1610 | |
| 1611 | // Read the register value from the operand register Rn. |
| 1612 | uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Bits32(opcode, 2, 0), 0, &success); |
| 1613 | if (!success) |
| 1614 | return false; |
| 1615 | |
| 1616 | EmulateInstruction::Context context = { EmulateInstruction::eContextRelativeBranchImmediate, 0, 0, 0}; |
| 1617 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1618 | if (!success) |
| 1619 | return false; |
| 1620 | |
| 1621 | addr_t target; // target address |
| 1622 | uint32_t imm32; // PC-relative offset to branch forward |
| 1623 | bool nonzero; |
| 1624 | switch (encoding) { |
| 1625 | case eEncodingT1: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1626 | imm32 = Bit32(opcode, 9) << 6 | Bits32(opcode, 7, 3) << 1; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1627 | nonzero = BitIsSet(opcode, 11); |
| 1628 | target = pc + 4 + imm32; |
| 1629 | context.arg1 = 4 + imm32; // signed offset |
| 1630 | context.arg2 = eModeThumb; // target instruction set |
| 1631 | break; |
| 1632 | default: |
| 1633 | return false; |
| 1634 | } |
| 1635 | if (nonzero ^ (reg_val == 0)) |
| 1636 | if (!BranchWritePC(context, target)) |
| 1637 | return false; |
| 1638 | |
| 1639 | return true; |
| 1640 | } |
| 1641 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 1642 | // ADD <Rdn>, <Rm> |
| 1643 | // where <Rdn> the destination register is also the first operand register |
| 1644 | // and <Rm> is the second operand register. |
| 1645 | bool |
| 1646 | EmulateInstructionARM::EmulateAddRdnRm (ARMEncoding encoding) |
| 1647 | { |
| 1648 | #if 0 |
| 1649 | // ARM pseudo code... |
| 1650 | if ConditionPassed() then |
| 1651 | EncodingSpecificOperations(); |
| 1652 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 1653 | (result, carry, overflow) = AddWithCarry(R[n], shifted, '0'); |
| 1654 | if d == 15 then |
| 1655 | ALUWritePC(result); // setflags is always FALSE here |
| 1656 | else |
| 1657 | R[d] = result; |
| 1658 | if setflags then |
| 1659 | APSR.N = result<31>; |
| 1660 | APSR.Z = IsZeroBit(result); |
| 1661 | APSR.C = carry; |
| 1662 | APSR.V = overflow; |
| 1663 | #endif |
| 1664 | |
| 1665 | bool success = false; |
| 1666 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1667 | if (!success) |
| 1668 | return false; |
| 1669 | |
| 1670 | if (ConditionPassed()) |
| 1671 | { |
| 1672 | uint32_t Rd, Rn, Rm; |
| 1673 | //bool setflags = false; |
| 1674 | switch (encoding) |
| 1675 | { |
| 1676 | case eEncodingT2: |
| 1677 | // setflags = FALSE |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1678 | Rd = Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 1679 | Rm = Bits32(opcode, 6, 3); |
| 1680 | if (Rn == 15 && Rm == 15) |
| 1681 | return false; |
| 1682 | break; |
| 1683 | default: |
| 1684 | return false; |
| 1685 | } |
| 1686 | |
| 1687 | int32_t result, val1, val2; |
| 1688 | // Read the first operand. |
| 1689 | if (Rn == 15) |
| 1690 | val1 = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1691 | else |
| 1692 | val1 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 1693 | if (!success) |
| 1694 | return false; |
| 1695 | |
| 1696 | // Read the second operand. |
| 1697 | if (Rm == 15) |
| 1698 | val2 = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1699 | else |
| 1700 | val2 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 1701 | if (!success) |
| 1702 | return false; |
| 1703 | |
| 1704 | result = val1 + val2; |
| 1705 | EmulateInstruction::Context context = { EmulateInstruction::eContextImmediate, |
| 1706 | result, |
| 1707 | 0, |
| 1708 | 0 }; |
| 1709 | |
| 1710 | if (Rd == 15) |
| 1711 | { |
| 1712 | if (!ALUWritePC (context, result)) |
| 1713 | return false; |
| 1714 | } |
| 1715 | else |
| 1716 | { |
| 1717 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, result)) |
| 1718 | return false; |
| 1719 | } |
| 1720 | } |
| 1721 | return true; |
| 1722 | } |
| 1723 | |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 1724 | bool |
| 1725 | EmulateInstructionARM::EmulateCmpRnImm (ARMEncoding encoding) |
| 1726 | { |
| 1727 | #if 0 |
| 1728 | // ARM pseudo code... |
| 1729 | if ConditionPassed() then |
| 1730 | EncodingSpecificOperations(); |
| 1731 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1'); |
| 1732 | APSR.N = result<31>; |
| 1733 | APSR.Z = IsZeroBit(result); |
| 1734 | APSR.C = carry; |
| 1735 | APSR.V = overflow; |
| 1736 | #endif |
| 1737 | |
| 1738 | bool success = false; |
| 1739 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1740 | if (!success) |
| 1741 | return false; |
| 1742 | |
| 1743 | uint32_t Rn; // the first operand |
| 1744 | uint32_t imm32; // the immediate value to be compared with |
| 1745 | switch (encoding) { |
| 1746 | case eEncodingT1: |
| 1747 | Rn = Bits32(opcode, 10, 8); |
| 1748 | imm32 = Bits32(opcode, 7, 0); |
| 1749 | break; |
| 1750 | default: |
| 1751 | return false; |
| 1752 | } |
| 1753 | // Read the register value from the operand register Rn. |
| 1754 | uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 1755 | if (!success) |
| 1756 | return false; |
| 1757 | |
| 1758 | EmulateInstruction::Context context = { EmulateInstruction::eContextImmediate, 0, 0, 0}; |
| 1759 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1); |
| 1760 | m_new_inst_cpsr = m_inst_cpsr; |
| 1761 | SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(res.result, CPSR_N)); |
| 1762 | SetBit32(m_new_inst_cpsr, CPSR_Z, res.result == 0 ? 1 : 0); |
| 1763 | SetBit32(m_new_inst_cpsr, CPSR_C, res.carry_out); |
| 1764 | SetBit32(m_new_inst_cpsr, CPSR_V, res.overflow); |
| 1765 | if (m_new_inst_cpsr != m_inst_cpsr) |
| 1766 | { |
| 1767 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
| 1768 | return false; |
| 1769 | } |
| 1770 | return true; |
| 1771 | } |
| 1772 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1773 | // LDM loads multiple registers from consecutive memory locations, using an |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 1774 | // address from a base register. Optionally the address just above the highest of those locations |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1775 | // can be written back to the base register. |
| 1776 | bool |
| 1777 | EmulateInstructionARM::EmulateLDM (ARMEncoding encoding) |
| 1778 | { |
| 1779 | #if 0 |
| 1780 | // ARM pseudo code... |
| 1781 | if ConditionPassed() |
| 1782 | EncodingSpecificOperations(); NullCheckIfThumbEE (n); |
| 1783 | address = R[n]; |
| 1784 | |
| 1785 | for i = 0 to 14 |
| 1786 | if registers<i> == '1' then |
| 1787 | R[i] = MemA[address, 4]; address = address + 4; |
| 1788 | if registers<15> == '1' then |
| 1789 | LoadWritePC (MemA[address, 4]); |
| 1790 | |
| 1791 | if wback && registers<n> == '0' then R[n] = R[n] + 4 * BitCount (registers); |
| 1792 | if wback && registers<n> == '1' then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 1793 | |
| 1794 | #endif |
| 1795 | |
| 1796 | bool success = false; |
| 1797 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1798 | if (!success) |
| 1799 | return false; |
| 1800 | |
| 1801 | if (ConditionPassed()) |
| 1802 | { |
| 1803 | uint32_t n; |
| 1804 | uint32_t registers = 0; |
| 1805 | bool wback; |
| 1806 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1807 | switch (encoding) |
| 1808 | { |
| 1809 | case eEncodingT1: |
| 1810 | n = Bits32 (opcode, 10, 8); |
| 1811 | registers = Bits32 (opcode, 7, 0); |
| 1812 | wback = BitIsClear (registers, n); |
| 1813 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 1814 | if (BitCount(registers) < 1) |
| 1815 | return false; |
| 1816 | break; |
| 1817 | case eEncodingT2: |
| 1818 | n = Bits32 (opcode, 19, 16); |
| 1819 | registers = Bits32 (opcode, 15, 0); |
| 1820 | wback = BitIsSet (opcode, 21); |
| 1821 | if ((n == 15) |
| 1822 | || (BitCount (registers) < 2) |
| 1823 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 1824 | return false; |
| 1825 | if (BitIsSet (registers, 15) |
| 1826 | && m_it_session.InITBlock() |
| 1827 | && !m_it_session.LastInITBlock()) |
| 1828 | return false; |
| 1829 | if (wback |
| 1830 | && BitIsSet (registers, n)) |
| 1831 | return false; |
| 1832 | break; |
| 1833 | case eEncodingA1: |
| 1834 | n = Bits32 (opcode, 19, 16); |
| 1835 | registers = Bits32 (opcode, 15, 0); |
| 1836 | wback = BitIsSet (opcode, 21); |
| 1837 | if ((n == 15) |
| 1838 | || (BitCount (registers) < 1)) |
| 1839 | return false; |
| 1840 | break; |
| 1841 | default: |
| 1842 | return false; |
| 1843 | } |
| 1844 | |
| 1845 | int32_t offset = 0; |
| 1846 | const addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 1847 | if (!success) |
| 1848 | return false; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 1849 | |
| 1850 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 1851 | eRegisterKindDWARF, |
| 1852 | dwarf_r0 + n, |
| 1853 | offset }; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1854 | |
| 1855 | for (int i = 0; i < 14; ++i) |
| 1856 | { |
| 1857 | if (BitIsSet (registers, i)) |
| 1858 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 1859 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1860 | context.arg2 = offset; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1861 | if (wback && (n == 13)) // Pop Instruction |
| 1862 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 1863 | |
| 1864 | // R[i] = MemA [address, 4]; address = address + 4; |
| 1865 | uint32_t data = ReadMemoryUnsigned (context, base_address + offset, addr_byte_size, 0, &success); |
| 1866 | if (!success) |
| 1867 | return false; |
| 1868 | |
| 1869 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 1870 | return false; |
| 1871 | |
| 1872 | offset += addr_byte_size; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | if (BitIsSet (registers, 15)) |
| 1877 | { |
| 1878 | //LoadWritePC (MemA [address, 4]); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 1879 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1880 | context.arg2 = offset; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1881 | uint32_t data = ReadMemoryUnsigned (context, base_address + offset, addr_byte_size, 0, &success); |
| 1882 | if (!success) |
| 1883 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 1884 | // In ARMv5T and above, this is an interworking branch. |
| 1885 | if (!LoadWritePC(context, data)) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1886 | return false; |
| 1887 | } |
| 1888 | |
| 1889 | if (wback && BitIsClear (registers, n)) |
| 1890 | { |
| 1891 | addr_t offset = addr_byte_size * BitCount (registers); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 1892 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1893 | context.arg2 = offset; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1894 | |
| 1895 | // R[n] = R[n] + 4 * BitCount (registers) |
| 1896 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, base_address + offset)) |
| 1897 | return false; |
| 1898 | } |
| 1899 | if (wback && BitIsSet (registers, n)) |
| 1900 | // R[n] bits(32) UNKNOWN; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 1901 | return WriteBits32Unknown (n); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1902 | } |
| 1903 | return true; |
| 1904 | } |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 1905 | |
| 1906 | // LDMDA loads multiple registers from consecutive memory locations using an address from a base registers. |
| 1907 | // The consecutive memorty locations end at this address and the address just below the lowest of those locations |
| 1908 | // can optionally be written back tot he base registers. |
| 1909 | bool |
| 1910 | EmulateInstructionARM::EmulateLDMDA (ARMEncoding encoding) |
| 1911 | { |
| 1912 | #if 0 |
| 1913 | // ARM pseudo code... |
| 1914 | if ConditionPassed() then |
| 1915 | EncodingSpecificOperations(); |
| 1916 | address = R[n] - 4*BitCount(registers) + 4; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 1917 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 1918 | for i = 0 to 14 |
| 1919 | if registers<i> == ’1’ then |
| 1920 | R[i] = MemA[address,4]; address = address + 4; |
| 1921 | |
| 1922 | if registers<15> == ’1’ then |
| 1923 | LoadWritePC(MemA[address,4]); |
| 1924 | |
| 1925 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 1926 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 1927 | #endif |
| 1928 | |
| 1929 | bool success = false; |
| 1930 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1931 | if (!success) |
| 1932 | return false; |
| 1933 | |
| 1934 | if (ConditionPassed()) |
| 1935 | { |
| 1936 | uint32_t n; |
| 1937 | uint32_t registers = 0; |
| 1938 | bool wback; |
| 1939 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1940 | |
| 1941 | // EncodingSpecificOperations(); |
| 1942 | switch (encoding) |
| 1943 | { |
| 1944 | case eEncodingA1: |
| 1945 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 1946 | n = Bits32 (opcode, 19, 16); |
| 1947 | registers = Bits32 (opcode, 15, 0); |
| 1948 | wback = BitIsSet (opcode, 21); |
| 1949 | |
| 1950 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 1951 | if ((n == 15) || (BitCount (registers) < 1)) |
| 1952 | return false; |
| 1953 | |
| 1954 | break; |
| 1955 | |
| 1956 | default: |
| 1957 | return false; |
| 1958 | } |
| 1959 | // address = R[n] - 4*BitCount(registers) + 4; |
| 1960 | |
| 1961 | int32_t offset = 0; |
| 1962 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 1963 | |
| 1964 | if (!success) |
| 1965 | return false; |
| 1966 | |
| 1967 | address = address - (addr_byte_size * BitCount (registers)) + addr_byte_size; |
| 1968 | |
| 1969 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 1970 | eRegisterKindDWARF, |
| 1971 | dwarf_r0 + n, |
| 1972 | offset }; |
| 1973 | |
| 1974 | // for i = 0 to 14 |
| 1975 | for (int i = 0; i < 14; ++i) |
| 1976 | { |
| 1977 | // if registers<i> == ’1’ then |
| 1978 | if (BitIsSet (registers, i)) |
| 1979 | { |
| 1980 | // R[i] = MemA[address,4]; address = address + 4; |
| 1981 | context.arg2 = offset; |
| 1982 | uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success); |
| 1983 | if (!success) |
| 1984 | return false; |
| 1985 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 1986 | return false; |
| 1987 | offset += addr_byte_size; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | // if registers<15> == ’1’ then |
| 1992 | // LoadWritePC(MemA[address,4]); |
| 1993 | if (BitIsSet (registers, 15)) |
| 1994 | { |
| 1995 | context.arg2 = offset; |
| 1996 | uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success); |
| 1997 | if (!success) |
| 1998 | return false; |
| 1999 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, data)) |
| 2000 | return false; |
| 2001 | } |
| 2002 | |
| 2003 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2004 | if (wback && BitIsClear (registers, n)) |
| 2005 | { |
| 2006 | context.arg2 = offset; |
| 2007 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2008 | if (!success) |
| 2009 | return false; |
| 2010 | addr = addr - (addr_byte_size * BitCount (registers)); |
| 2011 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 2012 | return false; |
| 2013 | } |
| 2014 | |
| 2015 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 2016 | if (wback && BitIsSet (registers, n)) |
| 2017 | return WriteBits32Unknown (n); |
| 2018 | } |
| 2019 | return true; |
| 2020 | } |
| 2021 | |
| 2022 | // LDMDB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 2023 | // consecutive memory lcoations end just below this address, and the address of the lowest of those locations can |
| 2024 | // be optionally written back to the base register. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2025 | bool |
| 2026 | EmulateInstructionARM::EmulateLDMDB (ARMEncoding encoding) |
| 2027 | { |
| 2028 | #if 0 |
| 2029 | // ARM pseudo code... |
| 2030 | if ConditionPassed() then |
| 2031 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 2032 | address = R[n] - 4*BitCount(registers); |
| 2033 | |
| 2034 | for i = 0 to 14 |
| 2035 | if registers<i> == ’1’ then |
| 2036 | R[i] = MemA[address,4]; address = address + 4; |
| 2037 | if registers<15> == ’1’ then |
| 2038 | LoadWritePC(MemA[address,4]); |
| 2039 | |
| 2040 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2041 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2042 | #endif |
| 2043 | |
| 2044 | bool success = false; |
| 2045 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2046 | if (!success) |
| 2047 | return false; |
| 2048 | |
| 2049 | if (ConditionPassed()) |
| 2050 | { |
| 2051 | uint32_t n; |
| 2052 | uint32_t registers = 0; |
| 2053 | bool wback; |
| 2054 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2055 | switch (encoding) |
| 2056 | { |
| 2057 | case eEncodingT1: |
| 2058 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
| 2059 | n = Bits32 (opcode, 19, 16); |
| 2060 | registers = Bits32 (opcode, 15, 0); |
| 2061 | wback = BitIsSet (opcode, 21); |
| 2062 | |
| 2063 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
| 2064 | if ((n == 15) |
| 2065 | || (BitCount (registers) < 2) |
| 2066 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 2067 | return false; |
| 2068 | |
| 2069 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 2070 | if (BitIsSet (registers, 15) |
| 2071 | && m_it_session.InITBlock() |
| 2072 | && !m_it_session.LastInITBlock()) |
| 2073 | return false; |
| 2074 | |
| 2075 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 2076 | if (wback && BitIsSet (registers, n)) |
| 2077 | return false; |
| 2078 | |
| 2079 | break; |
| 2080 | |
| 2081 | case eEncodingA1: |
| 2082 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 2083 | n = Bits32 (opcode, 19, 16); |
| 2084 | registers = Bits32 (opcode, 15, 0); |
| 2085 | wback = BitIsSet (opcode, 21); |
| 2086 | |
| 2087 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 2088 | if ((n == 15) || (BitCount (registers) < 1)) |
| 2089 | return false; |
| 2090 | |
| 2091 | break; |
| 2092 | |
| 2093 | default: |
| 2094 | return false; |
| 2095 | } |
| 2096 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2097 | // address = R[n] - 4*BitCount(registers); |
| 2098 | |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2099 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2100 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2101 | |
| 2102 | if (!success) |
| 2103 | return false; |
| 2104 | |
| 2105 | address = address - (addr_byte_size * BitCount (registers)); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2106 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 2107 | eRegisterKindDWARF, |
| 2108 | dwarf_r0 + n, |
| 2109 | offset }; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2110 | |
| 2111 | for (int i = 0; i < 14; ++i) |
| 2112 | { |
| 2113 | if (BitIsSet (registers, i)) |
| 2114 | { |
| 2115 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2116 | context.arg2 = offset; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2117 | uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success); |
| 2118 | if (!success) |
| 2119 | return false; |
| 2120 | |
| 2121 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 2122 | return false; |
| 2123 | |
| 2124 | offset += addr_byte_size; |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | // if registers<15> == ’1’ then |
| 2129 | // LoadWritePC(MemA[address,4]); |
| 2130 | if (BitIsSet (registers, 15)) |
| 2131 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2132 | context.arg2 = offset; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2133 | uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success); |
| 2134 | if (!success) |
| 2135 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 2136 | // In ARMv5T and above, this is an interworking branch. |
| 2137 | if (!LoadWritePC(context, data)) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2138 | return false; |
| 2139 | } |
| 2140 | |
| 2141 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2142 | if (wback && BitIsClear (registers, n)) |
| 2143 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2144 | context.arg2 = offset; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2145 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2146 | if (!success) |
| 2147 | return false; |
| 2148 | addr = addr - (addr_byte_size * BitCount (registers)); |
| 2149 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 2150 | return false; |
| 2151 | } |
| 2152 | |
| 2153 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2154 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2155 | return WriteBits32Unknown (n); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2156 | } |
| 2157 | return true; |
| 2158 | } |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2159 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2160 | // LDMIB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 2161 | // consecutive memory locations start just above this address, and thea ddress of the last of those locations can |
| 2162 | // optinoally be written back to the base register. |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2163 | bool |
| 2164 | EmulateInstructionARM::EmulateLDMIB (ARMEncoding encoding) |
| 2165 | { |
| 2166 | #if 0 |
| 2167 | if ConditionPassed() then |
| 2168 | EncodingSpecificOperations(); |
| 2169 | address = R[n] + 4; |
| 2170 | |
| 2171 | for i = 0 to 14 |
| 2172 | if registers<i> == ’1’ then |
| 2173 | R[i] = MemA[address,4]; address = address + 4; |
| 2174 | if registers<15> == ’1’ then |
| 2175 | LoadWritePC(MemA[address,4]); |
| 2176 | |
| 2177 | if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 2178 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 2179 | #endif |
| 2180 | |
| 2181 | bool success = false; |
| 2182 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2183 | if (!success) |
| 2184 | return false; |
| 2185 | |
| 2186 | if (ConditionPassed()) |
| 2187 | { |
| 2188 | uint32_t n; |
| 2189 | uint32_t registers = 0; |
| 2190 | bool wback; |
| 2191 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2192 | switch (encoding) |
| 2193 | { |
| 2194 | case eEncodingA1: |
| 2195 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 2196 | n = Bits32 (opcode, 19, 16); |
| 2197 | registers = Bits32 (opcode, 15, 0); |
| 2198 | wback = BitIsSet (opcode, 21); |
| 2199 | |
| 2200 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 2201 | if ((n == 15) || (BitCount (registers) < 1)) |
| 2202 | return false; |
| 2203 | |
| 2204 | break; |
| 2205 | default: |
| 2206 | return false; |
| 2207 | } |
| 2208 | // address = R[n] + 4; |
| 2209 | |
| 2210 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2211 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2212 | |
| 2213 | if (!success) |
| 2214 | return false; |
| 2215 | |
| 2216 | address = address + addr_byte_size; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2217 | |
| 2218 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 2219 | eRegisterKindDWARF, |
| 2220 | dwarf_r0 + n, |
| 2221 | offset }; |
| 2222 | |
| 2223 | for (int i = 0; i < 14; ++i) |
| 2224 | { |
| 2225 | if (BitIsSet (registers, i)) |
| 2226 | { |
| 2227 | // R[i] = MemA[address,4]; address = address + 4; |
| 2228 | |
| 2229 | context.arg2 = offset; |
| 2230 | uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success); |
| 2231 | if (!success) |
| 2232 | return false; |
| 2233 | |
| 2234 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 2235 | return false; |
| 2236 | |
| 2237 | offset += addr_byte_size; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | // if registers<15> == ’1’ then |
| 2242 | // LoadWritePC(MemA[address,4]); |
| 2243 | if (BitIsSet (registers, 15)) |
| 2244 | { |
| 2245 | context.arg2 = offset; |
| 2246 | uint32_t data = ReadMemoryUnsigned (context, address + offset, addr_byte_size, 0, &success); |
| 2247 | if (!success) |
| 2248 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 2249 | // In ARMv5T and above, this is an interworking branch. |
| 2250 | if (!LoadWritePC(context, data)) |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2251 | return false; |
| 2252 | } |
| 2253 | |
| 2254 | // if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 2255 | if (wback && BitIsClear (registers, n)) |
| 2256 | { |
| 2257 | context.arg2 = offset; |
| 2258 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2259 | if (!success) |
| 2260 | return false; |
| 2261 | addr = addr + (addr_byte_size * BitCount (registers)); |
| 2262 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 2263 | return false; |
| 2264 | } |
| 2265 | |
| 2266 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2267 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2268 | return WriteBits32Unknown (n); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2269 | } |
| 2270 | return true; |
| 2271 | } |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2272 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 2273 | // Load Register (immediate) calculates an address from a base register value and |
| 2274 | // an immediate offset, loads a word from memory, and writes to a register. |
| 2275 | // LDR (immediate, Thumb) |
| 2276 | bool |
| 2277 | EmulateInstructionARM::EmulateLDRRtRnImm (ARMEncoding encoding) |
| 2278 | { |
| 2279 | #if 0 |
| 2280 | // ARM pseudo code... |
| 2281 | if (ConditionPassed()) |
| 2282 | { |
| 2283 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 2284 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 2285 | address = if index then offset_addr else R[n]; |
| 2286 | data = MemU[address,4]; |
| 2287 | if wback then R[n] = offset_addr; |
| 2288 | if t == 15 then |
| 2289 | if address<1:0> == '00' then LoadWritePC(data); else UNPREDICTABLE; |
| 2290 | elsif UnalignedSupport() || address<1:0> = '00' then |
| 2291 | R[t] = data; |
| 2292 | else R[t] = bits(32) UNKNOWN; // Can only apply before ARMv7 |
| 2293 | } |
| 2294 | #endif |
| 2295 | |
| 2296 | bool success = false; |
| 2297 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2298 | if (!success) |
| 2299 | return false; |
| 2300 | |
| 2301 | if (ConditionPassed()) |
| 2302 | { |
| 2303 | uint32_t Rt; // the destination register |
| 2304 | uint32_t Rn; // the base register |
| 2305 | uint32_t imm32; // the immediate offset used to form the address |
| 2306 | addr_t offset_addr; // the offset address |
| 2307 | addr_t address; // the calculated address |
| 2308 | uint32_t data; // the literal data value from memory load |
| 2309 | bool add, index, wback; |
| 2310 | switch (encoding) { |
| 2311 | case eEncodingT1: |
| 2312 | Rt = Bits32(opcode, 5, 3); |
| 2313 | Rn = Bits32(opcode, 2, 0); |
| 2314 | imm32 = Bits32(opcode, 10, 6) << 2; // imm32 = ZeroExtend(imm5:'00', 32); |
| 2315 | // index = TRUE; add = TRUE; wback = FALSE |
| 2316 | add = true; |
| 2317 | index = true; |
| 2318 | wback = false; |
| 2319 | break; |
| 2320 | default: |
| 2321 | return false; |
| 2322 | } |
| 2323 | uint32_t base = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 2324 | if (!success) |
| 2325 | return false; |
| 2326 | if (add) |
| 2327 | offset_addr = base + imm32; |
| 2328 | else |
| 2329 | offset_addr = base - imm32; |
| 2330 | |
| 2331 | address = (index ? offset_addr : base); |
| 2332 | |
| 2333 | if (wback) |
| 2334 | { |
| 2335 | EmulateInstruction::Context ctx = { EmulateInstruction::eContextRegisterPlusOffset, |
| 2336 | eRegisterKindDWARF, |
| 2337 | dwarf_r0 + Rn, |
| 2338 | (int32_t) (offset_addr - base)}; |
| 2339 | if (!WriteRegisterUnsigned (ctx, eRegisterKindDWARF, dwarf_r0 + Rn, offset_addr)) |
| 2340 | return false; |
| 2341 | } |
| 2342 | |
| 2343 | // Prepare to write to the Rt register. |
| 2344 | EmulateInstruction::Context context = {EmulateInstruction::eContextImmediate, |
| 2345 | 0, |
| 2346 | 0, |
| 2347 | 0}; |
| 2348 | |
| 2349 | // Read memory from the address. |
| 2350 | data = ReadMemoryUnsigned(context, address, 4, 0, &success); |
| 2351 | if (!success) |
| 2352 | return false; |
| 2353 | context.arg0 = data; |
| 2354 | |
| 2355 | if (Rt == 15) |
| 2356 | { |
| 2357 | if (Bits32(address, 1, 0) == 0) |
| 2358 | { |
| 2359 | if (!LoadWritePC(context, data)) |
| 2360 | return false; |
| 2361 | } |
| 2362 | else |
| 2363 | return false; |
| 2364 | } |
| 2365 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 2366 | { |
| 2367 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 2368 | return false; |
| 2369 | } |
| 2370 | else |
| 2371 | return false; |
| 2372 | } |
| 2373 | return true; |
| 2374 | } |
| 2375 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2376 | EmulateInstructionARM::ARMOpcode* |
| 2377 | EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2378 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2379 | static ARMOpcode |
| 2380 | g_arm_opcodes[] = |
| 2381 | { |
| 2382 | //---------------------------------------------------------------------- |
| 2383 | // Prologue instructions |
| 2384 | //---------------------------------------------------------------------- |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 2385 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2386 | // push register(s) |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2387 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePush, "push <registers>" }, |
| 2388 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePush, "push <register>" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 2389 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2390 | // set r7 to point to a stack offset |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2391 | { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #<const>" }, |
| 2392 | { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubR7IPImmediate, "sub r7, ip, #<const>"}, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 2393 | // copy the stack pointer to ip |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2394 | { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMovRdSP, "mov ip, sp" }, |
| 2395 | { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add ip, sp, #<const>" }, |
| 2396 | { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubIPSPImmediate, "sub ip, sp, #<const>"}, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 2397 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2398 | // adjust the stack pointer |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2399 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 2400 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2401 | // push one register |
| 2402 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2403 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" }, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 2404 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2405 | // vector push consecutive extension register(s) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 2406 | { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 2407 | { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 2408 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2409 | //---------------------------------------------------------------------- |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 2410 | // Epilogue instructions |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2411 | //---------------------------------------------------------------------- |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 2412 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 2413 | // To resolve ambiguity, "blx <label>" should come before "bl <label>". |
| 2414 | { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 2415 | { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 2416 | { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2417 | { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePop, "pop <registers>"}, |
| 2418 | { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePop, "pop <register>"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 2419 | { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 2420 | { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
| 2421 | |
| 2422 | //---------------------------------------------------------------------- |
| 2423 | // Supervisor Call (previously Software Interrupt) |
| 2424 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 2425 | { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"}, |
| 2426 | |
| 2427 | //---------------------------------------------------------------------- |
| 2428 | // Branch instructions |
| 2429 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2430 | { 0x0f000000, 0x0a000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "b #imm24"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 2431 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2432 | //---------------------------------------------------------------------- |
| 2433 | // Load instructions |
| 2434 | //---------------------------------------------------------------------- |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2435 | { 0x0fd00000, 0x08900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2436 | { 0x0fd00000, 0x08100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDA, "ldmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2437 | { 0x0fd00000, 0x09100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
| 2438 | { 0x0fd00000, 0x09900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMIB, "ldmib<c> <Rn<{!} <registers>" } |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2439 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2440 | }; |
| 2441 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 2442 | |
| 2443 | for (size_t i=0; i<k_num_arm_opcodes; ++i) |
| 2444 | { |
| 2445 | if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value) |
| 2446 | return &g_arm_opcodes[i]; |
| 2447 | } |
| 2448 | return NULL; |
| 2449 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2450 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2451 | |
| 2452 | EmulateInstructionARM::ARMOpcode* |
| 2453 | EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode) |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 2454 | { |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 2455 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2456 | static ARMOpcode |
| 2457 | g_thumb_opcodes[] = |
| 2458 | { |
| 2459 | //---------------------------------------------------------------------- |
| 2460 | // Prologue instructions |
| 2461 | //---------------------------------------------------------------------- |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 2462 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2463 | // push register(s) |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2464 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePush, "push <registers>" }, |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 2465 | { 0xffff0000, 0xe92d0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <registers>" }, |
| 2466 | { 0xffff0fff, 0xf84d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <register>" }, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 2467 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2468 | // set r7 to point to a stack offset |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2469 | { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #imm" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 2470 | // copy the stack pointer to r7 |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2471 | { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdSP, "mov r7, sp" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 2472 | // move from high register to low register (comes after "mov r7, sp" to resolve ambiguity) |
| 2473 | { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovLowHigh, "mov r0-r7, r8-r15" }, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 2474 | |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 2475 | // PC-relative load into register (see also EmulateAddSPRm) |
| 2476 | { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr <Rt>, [PC, #imm]"}, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 2477 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2478 | // adjust the stack pointer |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2479 | { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPRm, "add sp, <Rm>"}, |
| 2480 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSubSPImmdiate, "add sp, sp, #imm"}, |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 2481 | { 0xfbef8f00, 0xf1ad0d00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub.w sp, sp, #<const>"}, |
| 2482 | { 0xfbff8f00, 0xf2ad0d00, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "subw sp, sp, #imm12"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 2483 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2484 | // vector push consecutive extension register(s) |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 2485 | { 0xffbf0f00, 0xed2d0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 2486 | { 0xffbf0f00, 0xed2d0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 2487 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2488 | //---------------------------------------------------------------------- |
| 2489 | // Epilogue instructions |
| 2490 | //---------------------------------------------------------------------- |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 2491 | |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2492 | { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPImmediate, "add sp, #imm"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 2493 | { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
| 2494 | // J1 == J2 == 1 |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 2495 | { 0xf800f800, 0xf000f800, ARMV4T_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 2496 | // J1 == J2 == 1 |
| 2497 | { 0xf800e800, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 2498 | { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePop, "pop <registers>"}, |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 2499 | { 0xffff0000, 0xe8bd0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <registers>" }, |
| 2500 | { 0xffff0fff, 0xf85d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <register>" }, |
| 2501 | { 0xffbf0f00, 0xecbd0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
| 2502 | { 0xffbf0f00, 0xecbd0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 2503 | |
| 2504 | //---------------------------------------------------------------------- |
| 2505 | // Supervisor Call (previously Software Interrupt) |
| 2506 | //---------------------------------------------------------------------- |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 2507 | { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"}, |
| 2508 | |
| 2509 | //---------------------------------------------------------------------- |
| 2510 | // If Then makes up to four following instructions conditional. |
| 2511 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 2512 | { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"}, |
| 2513 | |
| 2514 | //---------------------------------------------------------------------- |
| 2515 | // Branch instructions |
| 2516 | //---------------------------------------------------------------------- |
| 2517 | // To resolve ambiguity, "b<c> #imm8" should come after "svc #imm8". |
| 2518 | { 0xfffff000, 0x0000d000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm8 (outside IT)"}, |
| 2519 | { 0xffff8000, 0x0000e000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateB, "b #imm11 (outside or last in IT)"}, |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2520 | { 0xf800d000, 0xf0008000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside IT)"}, |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2521 | { 0xf800d000, 0xf0009000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateB, "b.w #imm8 (outside or last in IT)"}, |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2522 | // compare and branch |
| 2523 | { 0xfffff500, 0x0000b100, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCB, "cb{n}z <Rn>, <label>"}, |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2524 | |
| 2525 | //---------------------------------------------------------------------- |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2526 | // Data-processing instructions |
| 2527 | //---------------------------------------------------------------------- |
| 2528 | // Make sure "add sp, <Rm>" comes before this instruction, so there's no ambiguity decoding the two. |
| 2529 | { 0xffffff00, 0x00004400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdnRm, "add <Rdn>, <Rm>"}, |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 2530 | // move from high register to high register |
| 2531 | { 0xffffff00, 0x00004600, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdRm, "mov<c> <Rd>, <Rm>"}, |
| 2532 | // move from low register to low register |
| 2533 | { 0xffffffc0, 0x00000000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateMovRdRm, "movs <Rd>, <Rm>"}, |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2534 | // compare a register with immediate |
| 2535 | { 0xfffff800, 0x00002800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCmpRnImm, "cmp<c> <Rn>, #imm8"}, |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2536 | |
| 2537 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2538 | // Load instructions |
| 2539 | //---------------------------------------------------------------------- |
| 2540 | { 0xfffff800, 0x0000c800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2541 | { 0xffd02000, 0xe8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c>.w <Rn>{!} <registers>" }, |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 2542 | { 0xffd00000, 0xe9100000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame^] | 2543 | { 0xfffff800, 0x00006800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtRnImm, "ldr<c> <Rt>, [<Rn>{,#imm}]"}, |
| 2544 | // Thumb2 PC-relative load into register |
| 2545 | { 0xff7f0000, 0xf85f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr<c>.w <Rt>, [PC, +/-#imm}]"} |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2546 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 2547 | }; |
| 2548 | |
| 2549 | const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
| 2550 | for (size_t i=0; i<k_num_thumb_opcodes; ++i) |
| 2551 | { |
| 2552 | if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value) |
| 2553 | return &g_thumb_opcodes[i]; |
| 2554 | } |
| 2555 | return NULL; |
| 2556 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2557 | |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 2558 | bool |
| 2559 | EmulateInstructionARM::SetTargetTriple (const ConstString &triple) |
| 2560 | { |
| 2561 | m_arm_isa = 0; |
| 2562 | const char *triple_cstr = triple.GetCString(); |
| 2563 | if (triple_cstr) |
| 2564 | { |
| 2565 | const char *dash = ::strchr (triple_cstr, '-'); |
| 2566 | if (dash) |
| 2567 | { |
| 2568 | std::string arch (triple_cstr, dash); |
| 2569 | const char *arch_cstr = arch.c_str(); |
| 2570 | if (strcasecmp(arch_cstr, "armv4t") == 0) |
| 2571 | m_arm_isa = ARMv4T; |
| 2572 | else if (strcasecmp(arch_cstr, "armv4") == 0) |
| 2573 | m_arm_isa = ARMv4; |
| 2574 | else if (strcasecmp(arch_cstr, "armv5tej") == 0) |
| 2575 | m_arm_isa = ARMv5TEJ; |
| 2576 | else if (strcasecmp(arch_cstr, "armv5te") == 0) |
| 2577 | m_arm_isa = ARMv5TE; |
| 2578 | else if (strcasecmp(arch_cstr, "armv5t") == 0) |
| 2579 | m_arm_isa = ARMv5T; |
| 2580 | else if (strcasecmp(arch_cstr, "armv6k") == 0) |
| 2581 | m_arm_isa = ARMv6K; |
| 2582 | else if (strcasecmp(arch_cstr, "armv6") == 0) |
| 2583 | m_arm_isa = ARMv6; |
| 2584 | else if (strcasecmp(arch_cstr, "armv6t2") == 0) |
| 2585 | m_arm_isa = ARMv6T2; |
| 2586 | else if (strcasecmp(arch_cstr, "armv7") == 0) |
| 2587 | m_arm_isa = ARMv7; |
| 2588 | else if (strcasecmp(arch_cstr, "armv8") == 0) |
| 2589 | m_arm_isa = ARMv8; |
| 2590 | } |
| 2591 | } |
| 2592 | return m_arm_isa != 0; |
| 2593 | } |
| 2594 | |
| 2595 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2596 | bool |
| 2597 | EmulateInstructionARM::ReadInstruction () |
| 2598 | { |
| 2599 | bool success = false; |
| 2600 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 2601 | if (success) |
| 2602 | { |
| 2603 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 2604 | if (success) |
| 2605 | { |
| 2606 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 2607 | if (m_inst_cpsr & MASK_CPSR_T) |
| 2608 | { |
| 2609 | m_inst_mode = eModeThumb; |
| 2610 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 2611 | |
| 2612 | if (success) |
| 2613 | { |
| 2614 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 2615 | { |
| 2616 | m_inst.opcode_type = eOpcode16; |
| 2617 | m_inst.opcode.inst16 = thumb_opcode; |
| 2618 | } |
| 2619 | else |
| 2620 | { |
| 2621 | m_inst.opcode_type = eOpcode32; |
| 2622 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | m_inst_mode = eModeARM; |
| 2629 | m_inst.opcode_type = eOpcode32; |
| 2630 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | if (!success) |
| 2635 | { |
| 2636 | m_inst_mode = eModeInvalid; |
| 2637 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 2638 | } |
| 2639 | return success; |
| 2640 | } |
| 2641 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2642 | uint32_t |
| 2643 | EmulateInstructionARM::ArchVersion () |
| 2644 | { |
| 2645 | return m_arm_isa; |
| 2646 | } |
| 2647 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2648 | bool |
| 2649 | EmulateInstructionARM::ConditionPassed () |
| 2650 | { |
| 2651 | if (m_inst_cpsr == 0) |
| 2652 | return false; |
| 2653 | |
| 2654 | const uint32_t cond = CurrentCond (); |
| 2655 | |
| 2656 | if (cond == UINT32_MAX) |
| 2657 | return false; |
| 2658 | |
| 2659 | bool result = false; |
| 2660 | switch (UnsignedBits(cond, 3, 1)) |
| 2661 | { |
| 2662 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 2663 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 2664 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 2665 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 2666 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 2667 | case 5: |
| 2668 | { |
| 2669 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 2670 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 2671 | result = n == v; |
| 2672 | } |
| 2673 | break; |
| 2674 | case 6: |
| 2675 | { |
| 2676 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 2677 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 2678 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 2679 | } |
| 2680 | break; |
| 2681 | case 7: |
| 2682 | result = true; |
| 2683 | break; |
| 2684 | } |
| 2685 | |
| 2686 | if (cond & 1) |
| 2687 | result = !result; |
| 2688 | return result; |
| 2689 | } |
| 2690 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2691 | uint32_t |
| 2692 | EmulateInstructionARM::CurrentCond () |
| 2693 | { |
| 2694 | switch (m_inst_mode) |
| 2695 | { |
| 2696 | default: |
| 2697 | case eModeInvalid: |
| 2698 | break; |
| 2699 | |
| 2700 | case eModeARM: |
| 2701 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 2702 | |
| 2703 | case eModeThumb: |
| 2704 | // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit |
| 2705 | // 'cond' field of the encoding. |
| 2706 | if (m_inst.opcode_type == eOpcode16 && |
| 2707 | Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d && |
| 2708 | Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f) |
| 2709 | { |
| 2710 | return Bits32(m_inst.opcode.inst16, 11, 7); |
| 2711 | } |
| 2712 | else if (m_inst.opcode_type == eOpcode32 && |
| 2713 | Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e && |
| 2714 | Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 && |
| 2715 | Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 && |
| 2716 | Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d) |
| 2717 | { |
| 2718 | return Bits32(m_inst.opcode.inst32, 25, 22); |
| 2719 | } |
| 2720 | |
| 2721 | return m_it_session.GetCond(); |
| 2722 | } |
| 2723 | return UINT32_MAX; // Return invalid value |
| 2724 | } |
| 2725 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2726 | bool |
| 2727 | EmulateInstructionARM::BranchWritePC (const Context &context, uint32_t addr) |
| 2728 | { |
| 2729 | addr_t target; |
| 2730 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2731 | // Check the current instruction set. |
| 2732 | if (CurrentInstrSet() == eModeARM) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2733 | target = addr & 0xfffffffc; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2734 | else |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2735 | target = addr & 0xfffffffe; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2736 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2737 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2738 | return false; |
| 2739 | |
| 2740 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | // As a side effect, BXWritePC sets context.arg2 to eModeARM or eModeThumb by inspecting addr. |
| 2744 | bool |
| 2745 | EmulateInstructionARM::BXWritePC (Context &context, uint32_t addr) |
| 2746 | { |
| 2747 | addr_t target; |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 2748 | // If the CPSR is changed due to switching between ARM and Thumb ISETSTATE, |
| 2749 | // we want to record it and issue a WriteRegister callback so the clients |
| 2750 | // can track the mode changes accordingly. |
| 2751 | bool cpsr_changed = false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2752 | |
| 2753 | if (BitIsSet(addr, 0)) |
| 2754 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 2755 | if (CurrentInstrSet() != eModeThumb) |
| 2756 | { |
| 2757 | SelectInstrSet(eModeThumb); |
| 2758 | cpsr_changed = true; |
| 2759 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2760 | target = addr & 0xfffffffe; |
| 2761 | context.arg2 = eModeThumb; |
| 2762 | } |
| 2763 | else if (BitIsClear(addr, 1)) |
| 2764 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 2765 | if (CurrentInstrSet() != eModeARM) |
| 2766 | { |
| 2767 | SelectInstrSet(eModeARM); |
| 2768 | cpsr_changed = true; |
| 2769 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2770 | target = addr & 0xfffffffc; |
| 2771 | context.arg2 = eModeARM; |
| 2772 | } |
| 2773 | else |
| 2774 | return false; // address<1:0> == '10' => UNPREDICTABLE |
| 2775 | |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 2776 | if (cpsr_changed) |
| 2777 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 2778 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 2779 | return false; |
| 2780 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2781 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2782 | return false; |
| 2783 | |
| 2784 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2785 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2786 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2787 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions. |
| 2788 | bool |
| 2789 | EmulateInstructionARM::LoadWritePC (Context &context, uint32_t addr) |
| 2790 | { |
| 2791 | if (ArchVersion() >= ARMv5T) |
| 2792 | return BXWritePC(context, addr); |
| 2793 | else |
| 2794 | return BranchWritePC((const Context)context, addr); |
| 2795 | } |
| 2796 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2797 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions and current instruction set. |
| 2798 | bool |
| 2799 | EmulateInstructionARM::ALUWritePC (Context &context, uint32_t addr) |
| 2800 | { |
| 2801 | if (ArchVersion() >= ARMv7 && CurrentInstrSet() == eModeARM) |
| 2802 | return BXWritePC(context, addr); |
| 2803 | else |
| 2804 | return BranchWritePC((const Context)context, addr); |
| 2805 | } |
| 2806 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2807 | EmulateInstructionARM::Mode |
| 2808 | EmulateInstructionARM::CurrentInstrSet () |
| 2809 | { |
| 2810 | return m_inst_mode; |
| 2811 | } |
| 2812 | |
| 2813 | // Set the 'T' bit of our CPSR. The m_inst_mode gets updated when the next |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 2814 | // ReadInstruction() is performed. This function has a side effect of updating |
| 2815 | // the m_new_inst_cpsr member variable if necessary. |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2816 | bool |
| 2817 | EmulateInstructionARM::SelectInstrSet (Mode arm_or_thumb) |
| 2818 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 2819 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2820 | switch (arm_or_thumb) |
| 2821 | { |
| 2822 | default: |
| 2823 | return false; |
| 2824 | eModeARM: |
| 2825 | // Clear the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 2826 | m_new_inst_cpsr &= ~MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | eModeThumb: |
| 2829 | // Set the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 2830 | m_new_inst_cpsr |= MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 2831 | break; |
| 2832 | } |
| 2833 | return true; |
| 2834 | } |
| 2835 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 2836 | // This function returns TRUE if the processor currently provides support for |
| 2837 | // unaligned memory accesses, or FALSE otherwise. This is always TRUE in ARMv7, |
| 2838 | // controllable by the SCTLR.U bit in ARMv6, and always FALSE before ARMv6. |
| 2839 | bool |
| 2840 | EmulateInstructionARM::UnalignedSupport() |
| 2841 | { |
| 2842 | return (ArchVersion() >= ARMv7); |
| 2843 | } |
| 2844 | |
Johnny Chen | bf6ad17 | 2011-02-11 01:29:53 +0000 | [diff] [blame] | 2845 | // The main addition and subtraction instructions can produce status information |
| 2846 | // about both unsigned carry and signed overflow conditions. This status |
| 2847 | // information can be used to synthesize multi-word additions and subtractions. |
| 2848 | EmulateInstructionARM::AddWithCarryResult |
| 2849 | EmulateInstructionARM::AddWithCarry (uint32_t x, uint32_t y, uint8_t carry_in) |
| 2850 | { |
| 2851 | uint32_t result; |
| 2852 | uint8_t carry_out; |
| 2853 | uint8_t overflow; |
| 2854 | |
| 2855 | uint64_t unsigned_sum = x + y + carry_in; |
| 2856 | int64_t signed_sum = (int32_t)x + (int32_t)y + (int32_t)carry_in; |
| 2857 | |
| 2858 | result = UnsignedBits(unsigned_sum, 31, 0); |
| 2859 | carry_out = (result == unsigned_sum ? 0 : 1); |
| 2860 | overflow = ((int32_t)result == signed_sum ? 0 : 1); |
| 2861 | |
| 2862 | AddWithCarryResult res = { result, carry_out, overflow }; |
| 2863 | return res; |
| 2864 | } |
| 2865 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2866 | bool |
| 2867 | EmulateInstructionARM::EvaluateInstruction () |
| 2868 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 2869 | // Advance the ITSTATE bits to their values for the next instruction. |
| 2870 | if (m_inst_mode == eModeThumb && m_it_session.InITBlock()) |
| 2871 | m_it_session.ITAdvance(); |
| 2872 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 2873 | return false; |
| 2874 | } |