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 | |
Johnny Chen | 8584c92 | 2011-01-26 01:18:52 +0000 | [diff] [blame] | 13 | #include "ARMDefines.h" |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 14 | #include "ARMUtils.h" |
Greg Clayton | 8482ded | 2011-02-01 00:04:43 +0000 | [diff] [blame] | 15 | #include "ARM_DWARF_Registers.h" |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MathExtras.h" // for SignExtend32 template function |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 17 | // and CountTrailingZeros_32 function |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 22 | // A8.6.50 |
| 23 | // Valid return values are {1, 2, 3, 4}, with 0 signifying an error condition. |
| 24 | static unsigned short CountITSize(unsigned ITMask) { |
| 25 | // First count the trailing zeros of the IT mask. |
| 26 | unsigned TZ = llvm::CountTrailingZeros_32(ITMask); |
| 27 | if (TZ > 3) |
| 28 | { |
| 29 | printf("Encoding error: IT Mask '0000'\n"); |
| 30 | return 0; |
| 31 | } |
| 32 | return (4 - TZ); |
| 33 | } |
| 34 | |
| 35 | // Init ITState. Note that at least one bit is always 1 in mask. |
| 36 | bool ITSession::InitIT(unsigned short bits7_0) |
| 37 | { |
| 38 | ITCounter = CountITSize(Bits32(bits7_0, 3, 0)); |
| 39 | if (ITCounter == 0) |
| 40 | return false; |
| 41 | |
| 42 | // A8.6.50 IT |
| 43 | unsigned short FirstCond = Bits32(bits7_0, 7, 4); |
| 44 | if (FirstCond == 0xF) |
| 45 | { |
| 46 | printf("Encoding error: IT FirstCond '1111'\n"); |
| 47 | return false; |
| 48 | } |
| 49 | if (FirstCond == 0xE && ITCounter != 1) |
| 50 | { |
| 51 | printf("Encoding error: IT FirstCond '1110' && Mask != '1000'\n"); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | ITState = bits7_0; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | // Update ITState if necessary. |
| 60 | void ITSession::ITAdvance() |
| 61 | { |
| 62 | assert(ITCounter); |
| 63 | --ITCounter; |
| 64 | if (ITCounter == 0) |
| 65 | ITState = 0; |
| 66 | else |
| 67 | { |
| 68 | unsigned short NewITState4_0 = Bits32(ITState, 4, 0) << 1; |
| 69 | SetBits32(ITState, 4, 0, NewITState4_0); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Return true if we're inside an IT Block. |
| 74 | bool ITSession::InITBlock() |
| 75 | { |
| 76 | return ITCounter != 0; |
| 77 | } |
| 78 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 79 | // Return true if we're the last instruction inside an IT Block. |
| 80 | bool ITSession::LastInITBlock() |
| 81 | { |
| 82 | return ITCounter == 1; |
| 83 | } |
| 84 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 85 | // Get condition bits for the current thumb instruction. |
| 86 | uint32_t ITSession::GetCond() |
| 87 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 88 | if (InITBlock()) |
| 89 | return Bits32(ITState, 7, 4); |
| 90 | else |
| 91 | return COND_AL; |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 94 | // ARM constants used during decoding |
| 95 | #define REG_RD 0 |
| 96 | #define LDM_REGLIST 1 |
| 97 | #define PC_REG 15 |
| 98 | #define PC_REGLIST_BIT 0x8000 |
| 99 | |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 100 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 101 | #define ARMv4T (1u << 1) |
| 102 | #define ARMv5T (1u << 2) |
| 103 | #define ARMv5TE (1u << 3) |
| 104 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 105 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 106 | #define ARMv6K (1u << 6) |
| 107 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 108 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 109 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 110 | #define ARMvAll (0xffffffffu) |
| 111 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 112 | #define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 113 | #define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 114 | #define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 115 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 116 | void |
| 117 | EmulateInstructionARM::Initialize () |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 118 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 119 | } |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 120 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 121 | void |
| 122 | EmulateInstructionARM::Terminate () |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 123 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 126 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 127 | // Push Multiple Registers stores multiple registers to the stack, storing to |
| 128 | // consecutive memory locations ending just below the address in SP, and updates |
| 129 | // SP to point to the start of the stored data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 130 | bool |
| 131 | EmulateInstructionARM::EmulatePush (ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 132 | { |
| 133 | #if 0 |
| 134 | // ARM pseudo code... |
| 135 | if (ConditionPassed()) |
| 136 | { |
| 137 | EncodingSpecificOperations(); |
| 138 | NullCheckIfThumbEE(13); |
| 139 | address = SP - 4*BitCount(registers); |
| 140 | |
| 141 | for (i = 0 to 14) |
| 142 | { |
| 143 | if (registers<i> == ’1’) |
| 144 | { |
| 145 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 146 | MemA[address,4] = bits(32) UNKNOWN; |
| 147 | else |
| 148 | MemA[address,4] = R[i]; |
| 149 | address = address + 4; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 154 | MemA[address,4] = PCStoreValue(); |
| 155 | |
| 156 | SP = SP - 4*BitCount(registers); |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 161 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 162 | if (!success) |
| 163 | return false; |
| 164 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 165 | if (ConditionPassed()) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 166 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 167 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 168 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 169 | if (!success) |
| 170 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 171 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 172 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 173 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 174 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 175 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 176 | // The M bit represents LR. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 177 | if (Bits32(opcode, 8, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 178 | registers |= (1u << 14); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 179 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 180 | if (BitCount(registers) < 1) |
| 181 | return false; |
| 182 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 183 | case eEncodingT2: |
| 184 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 185 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 186 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 187 | if (BitCount(registers) < 2) |
| 188 | return false; |
| 189 | break; |
| 190 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 191 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 192 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 193 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 194 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 195 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 196 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 197 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 198 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 199 | // Instead of return false, let's handle the following case as well, |
| 200 | // which amounts to pushing one reg onto the full descending stacks. |
| 201 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 202 | break; |
| 203 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 204 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 205 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 206 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 207 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 208 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 209 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 210 | default: |
| 211 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 212 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 213 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 214 | addr_t addr = sp - sp_offset; |
| 215 | uint32_t i; |
| 216 | |
| 217 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 218 | for (i=0; i<15; ++i) |
| 219 | { |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 220 | if (BitIsSet (registers, 1u << i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 221 | { |
| 222 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 223 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 224 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 225 | if (!success) |
| 226 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 227 | if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 228 | return false; |
| 229 | addr += addr_byte_size; |
| 230 | } |
| 231 | } |
| 232 | |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 233 | if (BitIsSet (registers, 1u << 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 234 | { |
| 235 | 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] | 236 | 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] | 237 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 238 | if (!success) |
| 239 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 240 | if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
| 244 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 245 | context.arg0 = eRegisterKindGeneric; |
| 246 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 247 | context.arg2 = -sp_offset; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 248 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 249 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 255 | // Pop Multiple Registers loads multiple registers from the stack, loading from |
| 256 | // consecutive memory locations staring at the address in SP, and updates |
| 257 | // SP to point just above the loaded data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 258 | bool |
| 259 | EmulateInstructionARM::EmulatePop (ARMEncoding encoding) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 260 | { |
| 261 | #if 0 |
| 262 | // ARM pseudo code... |
| 263 | if (ConditionPassed()) |
| 264 | { |
| 265 | EncodingSpecificOperations(); NullCheckIfThumbEE(13); |
| 266 | address = SP; |
| 267 | for i = 0 to 14 |
| 268 | if registers<i> == ‘1’ then |
| 269 | R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4; |
| 270 | if registers<15> == ‘1’ then |
| 271 | if UnalignedAllowed then |
| 272 | LoadWritePC(MemU[address,4]); |
| 273 | else |
| 274 | LoadWritePC(MemA[address,4]); |
| 275 | if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers); |
| 276 | if registers<13> == ‘1’ then SP = bits(32) UNKNOWN; |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 281 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 282 | if (!success) |
| 283 | return false; |
| 284 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 285 | if (ConditionPassed()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 286 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 287 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 288 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 289 | if (!success) |
| 290 | return false; |
| 291 | uint32_t registers = 0; |
| 292 | uint32_t Rt; // the destination register |
| 293 | switch (encoding) { |
| 294 | case eEncodingT1: |
| 295 | registers = Bits32(opcode, 7, 0); |
| 296 | // The P bit represents PC. |
| 297 | if (Bits32(opcode, 8, 8)) |
| 298 | registers |= (1u << 15); |
| 299 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 300 | if (BitCount(registers) < 1) |
| 301 | return false; |
| 302 | break; |
| 303 | case eEncodingT2: |
| 304 | // Ignore bit 13. |
| 305 | registers = Bits32(opcode, 15, 0) & ~0x2000; |
| 306 | // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE; |
| 307 | if (BitCount(registers) < 2 || (Bits32(opcode, 15, 15) && Bits32(opcode, 14, 14))) |
| 308 | return false; |
| 309 | break; |
| 310 | case eEncodingT3: |
| 311 | Rt = Bits32(opcode, 15, 12); |
| 312 | // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE; |
| 313 | if (Rt == dwarf_sp) |
| 314 | return false; |
| 315 | registers = (1u << Rt); |
| 316 | break; |
| 317 | case eEncodingA1: |
| 318 | registers = Bits32(opcode, 15, 0); |
| 319 | // Instead of return false, let's handle the following case as well, |
| 320 | // which amounts to popping one reg from the full descending stacks. |
| 321 | // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD; |
| 322 | |
| 323 | // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE; |
| 324 | if (Bits32(opcode, 13, 13)) |
| 325 | return false; |
| 326 | break; |
| 327 | case eEncodingA2: |
| 328 | Rt = Bits32(opcode, 15, 12); |
| 329 | // if t == 13 then UNPREDICTABLE; |
| 330 | if (Rt == dwarf_sp) |
| 331 | return false; |
| 332 | registers = (1u << Rt); |
| 333 | break; |
| 334 | default: |
| 335 | return false; |
| 336 | } |
| 337 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
| 338 | addr_t addr = sp; |
| 339 | uint32_t i, data; |
| 340 | |
| 341 | EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 }; |
| 342 | for (i=0; i<15; ++i) |
| 343 | { |
| 344 | if (BitIsSet (registers, 1u << i)) |
| 345 | { |
| 346 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 347 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 348 | data = ReadMemoryUnsigned(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 349 | if (!success) |
| 350 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 351 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 352 | return false; |
| 353 | addr += addr_byte_size; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (BitIsSet (registers, 1u << 15)) |
| 358 | { |
| 359 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 360 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 361 | data = ReadMemoryUnsigned(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 362 | if (!success) |
| 363 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 364 | if (!WriteRegisterUnsigned(context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 365 | return false; |
| 366 | addr += addr_byte_size; |
| 367 | } |
| 368 | |
| 369 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 370 | context.arg0 = eRegisterKindGeneric; |
| 371 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 372 | context.arg2 = sp_offset; |
| 373 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 374 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 380 | // 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] | 381 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 382 | bool |
| 383 | EmulateInstructionARM::EmulateAddRdSPImmediate (ARMEncoding encoding) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 384 | { |
| 385 | #if 0 |
| 386 | // ARM pseudo code... |
| 387 | if (ConditionPassed()) |
| 388 | { |
| 389 | EncodingSpecificOperations(); |
| 390 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 391 | if d == 15 then |
| 392 | ALUWritePC(result); // setflags is always FALSE here |
| 393 | else |
| 394 | R[d] = result; |
| 395 | if setflags then |
| 396 | APSR.N = result<31>; |
| 397 | APSR.Z = IsZeroBit(result); |
| 398 | APSR.C = carry; |
| 399 | APSR.V = overflow; |
| 400 | } |
| 401 | #endif |
| 402 | |
| 403 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 404 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 405 | if (!success) |
| 406 | return false; |
| 407 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 408 | if (ConditionPassed()) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 409 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 410 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 411 | if (!success) |
| 412 | return false; |
| 413 | uint32_t Rd; // the destination register |
| 414 | uint32_t imm32; |
| 415 | switch (encoding) { |
| 416 | case eEncodingT1: |
| 417 | Rd = 7; |
| 418 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32) |
| 419 | break; |
| 420 | case eEncodingA1: |
| 421 | Rd = Bits32(opcode, 15, 12); |
| 422 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 423 | break; |
| 424 | default: |
| 425 | return false; |
| 426 | } |
| 427 | addr_t sp_offset = imm32; |
| 428 | addr_t addr = sp + sp_offset; // a pointer to the stack area |
| 429 | |
| 430 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 431 | eRegisterKindGeneric, |
| 432 | LLDB_REGNUM_GENERIC_SP, |
| 433 | sp_offset }; |
| 434 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 435 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr)) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | return true; |
| 439 | } |
| 440 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 441 | // Set r7 or ip to the current stack pointer. |
| 442 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 443 | bool |
| 444 | EmulateInstructionARM::EmulateMovRdSP (ARMEncoding encoding) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 445 | { |
| 446 | #if 0 |
| 447 | // ARM pseudo code... |
| 448 | if (ConditionPassed()) |
| 449 | { |
| 450 | EncodingSpecificOperations(); |
| 451 | result = R[m]; |
| 452 | if d == 15 then |
| 453 | ALUWritePC(result); // setflags is always FALSE here |
| 454 | else |
| 455 | R[d] = result; |
| 456 | if setflags then |
| 457 | APSR.N = result<31>; |
| 458 | APSR.Z = IsZeroBit(result); |
| 459 | // APSR.C unchanged |
| 460 | // APSR.V unchanged |
| 461 | } |
| 462 | #endif |
| 463 | |
| 464 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 465 | //const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 466 | //if (!success) |
| 467 | // return false; |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 468 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 469 | if (ConditionPassed()) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 470 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 471 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 472 | if (!success) |
| 473 | return false; |
| 474 | uint32_t Rd; // the destination register |
| 475 | switch (encoding) { |
| 476 | case eEncodingT1: |
| 477 | Rd = 7; |
| 478 | break; |
| 479 | case eEncodingA1: |
| 480 | Rd = 12; |
| 481 | break; |
| 482 | default: |
| 483 | return false; |
| 484 | } |
| 485 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 486 | eRegisterKindGeneric, |
| 487 | LLDB_REGNUM_GENERIC_SP, |
| 488 | 0 }; |
| 489 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 490 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp)) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 496 | // Move from high register (r8-r15) to low register (r0-r7). |
| 497 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 498 | bool |
| 499 | EmulateInstructionARM::EmulateMovLowHigh (ARMEncoding encoding) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 500 | { |
| 501 | #if 0 |
| 502 | // ARM pseudo code... |
| 503 | if (ConditionPassed()) |
| 504 | { |
| 505 | EncodingSpecificOperations(); |
| 506 | result = R[m]; |
| 507 | if d == 15 then |
| 508 | ALUWritePC(result); // setflags is always FALSE here |
| 509 | else |
| 510 | R[d] = result; |
| 511 | if setflags then |
| 512 | APSR.N = result<31>; |
| 513 | APSR.Z = IsZeroBit(result); |
| 514 | // APSR.C unchanged |
| 515 | // APSR.V unchanged |
| 516 | } |
| 517 | #endif |
| 518 | |
| 519 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 520 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 521 | if (!success) |
| 522 | return false; |
| 523 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 524 | if (ConditionPassed()) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 525 | { |
| 526 | uint32_t Rm; // the source register |
| 527 | uint32_t Rd; // the destination register |
| 528 | switch (encoding) { |
| 529 | case eEncodingT1: |
| 530 | Rm = Bits32(opcode, 6, 3); |
| 531 | Rd = Bits32(opcode, 2, 1); // bits(7) == 0 |
| 532 | break; |
| 533 | default: |
| 534 | return false; |
| 535 | } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 536 | int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 537 | if (!success) |
| 538 | return false; |
| 539 | |
| 540 | // The context specifies that Rm is to be moved into Rd. |
| 541 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 542 | eRegisterKindDWARF, |
| 543 | dwarf_r0 + Rm, |
| 544 | 0 }; |
| 545 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 546 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value)) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | return true; |
| 550 | } |
| 551 | |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 552 | // PC relative immediate load into register, possibly followed by ADD (SP plus register). |
| 553 | // LDR (literal) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 554 | bool |
| 555 | EmulateInstructionARM::EmulateLDRRdPCRelative (ARMEncoding encoding) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 556 | { |
| 557 | #if 0 |
| 558 | // ARM pseudo code... |
| 559 | if (ConditionPassed()) |
| 560 | { |
| 561 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 562 | base = Align(PC,4); |
| 563 | address = if add then (base + imm32) else (base - imm32); |
| 564 | data = MemU[address,4]; |
| 565 | if t == 15 then |
| 566 | if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 567 | elsif UnalignedSupport() || address<1:0> = ‘00’ then |
| 568 | R[t] = data; |
| 569 | else // Can only apply before ARMv7 |
| 570 | if CurrentInstrSet() == InstrSet_ARM then |
| 571 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 572 | else |
| 573 | R[t] = bits(32) UNKNOWN; |
| 574 | } |
| 575 | #endif |
| 576 | |
| 577 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 578 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 579 | if (!success) |
| 580 | return false; |
| 581 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 582 | if (ConditionPassed()) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 583 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 584 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 585 | if (!success) |
| 586 | return false; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 587 | |
| 588 | // PC relative immediate load context |
| 589 | EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset, |
| 590 | eRegisterKindGeneric, |
| 591 | LLDB_REGNUM_GENERIC_PC, |
| 592 | 0}; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 593 | uint32_t Rd; // the destination register |
| 594 | uint32_t imm32; // immediate offset from the PC |
| 595 | addr_t addr; // the PC relative address |
| 596 | uint32_t data; // the literal data value from the PC relative load |
| 597 | switch (encoding) { |
| 598 | case eEncodingT1: |
| 599 | Rd = Bits32(opcode, 10, 8); |
| 600 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32); |
| 601 | addr = pc + 4 + imm32; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 602 | context.arg2 = 4 + imm32; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 603 | break; |
| 604 | default: |
| 605 | return false; |
| 606 | } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 607 | data = ReadMemoryUnsigned(context, addr, 4, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 608 | if (!success) |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 609 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 610 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data)) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | return true; |
| 614 | } |
| 615 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 616 | // An add operation to adjust the SP. |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 617 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 618 | bool |
| 619 | EmulateInstructionARM::EmulateAddSPImmediate (ARMEncoding encoding) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 620 | { |
| 621 | #if 0 |
| 622 | // ARM pseudo code... |
| 623 | if (ConditionPassed()) |
| 624 | { |
| 625 | EncodingSpecificOperations(); |
| 626 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 627 | if d == 15 then // Can only occur for ARM encoding |
| 628 | ALUWritePC(result); // setflags is always FALSE here |
| 629 | else |
| 630 | R[d] = result; |
| 631 | if setflags then |
| 632 | APSR.N = result<31>; |
| 633 | APSR.Z = IsZeroBit(result); |
| 634 | APSR.C = carry; |
| 635 | APSR.V = overflow; |
| 636 | } |
| 637 | #endif |
| 638 | |
| 639 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 640 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 641 | if (!success) |
| 642 | return false; |
| 643 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 644 | if (ConditionPassed()) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 645 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 646 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 647 | if (!success) |
| 648 | return false; |
| 649 | uint32_t imm32; // the immediate operand |
| 650 | switch (encoding) { |
| 651 | case eEncodingT2: |
| 652 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
| 653 | break; |
| 654 | default: |
| 655 | return false; |
| 656 | } |
| 657 | addr_t sp_offset = imm32; |
| 658 | addr_t addr = sp + sp_offset; // the adjusted stack pointer value |
| 659 | |
| 660 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 661 | eRegisterKindGeneric, |
| 662 | LLDB_REGNUM_GENERIC_SP, |
| 663 | sp_offset }; |
| 664 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 665 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | // An add operation to adjust the SP. |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 672 | // ADD (SP plus register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 673 | bool |
| 674 | EmulateInstructionARM::EmulateAddSPRm (ARMEncoding encoding) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 675 | { |
| 676 | #if 0 |
| 677 | // ARM pseudo code... |
| 678 | if (ConditionPassed()) |
| 679 | { |
| 680 | EncodingSpecificOperations(); |
| 681 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 682 | (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’); |
| 683 | if d == 15 then |
| 684 | ALUWritePC(result); // setflags is always FALSE here |
| 685 | else |
| 686 | R[d] = result; |
| 687 | if setflags then |
| 688 | APSR.N = result<31>; |
| 689 | APSR.Z = IsZeroBit(result); |
| 690 | APSR.C = carry; |
| 691 | APSR.V = overflow; |
| 692 | } |
| 693 | #endif |
| 694 | |
| 695 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 696 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 697 | if (!success) |
| 698 | return false; |
| 699 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 700 | if (ConditionPassed()) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 701 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 702 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 703 | if (!success) |
| 704 | return false; |
| 705 | uint32_t Rm; // the second operand |
| 706 | switch (encoding) { |
| 707 | case eEncodingT2: |
| 708 | Rm = Bits32(opcode, 6, 3); |
| 709 | break; |
| 710 | default: |
| 711 | return false; |
| 712 | } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 713 | int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 714 | if (!success) |
| 715 | return false; |
| 716 | |
| 717 | addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value |
| 718 | |
| 719 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 720 | eRegisterKindGeneric, |
| 721 | LLDB_REGNUM_GENERIC_SP, |
| 722 | reg_value }; |
| 723 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 724 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 725 | return false; |
| 726 | } |
| 727 | return true; |
| 728 | } |
| 729 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 730 | // Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine |
| 731 | // at a PC-relative address, and changes instruction set from ARM to Thumb, or |
| 732 | // from Thumb to ARM. |
| 733 | // BLX (immediate) |
| 734 | bool |
| 735 | EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding) |
| 736 | { |
| 737 | #if 0 |
| 738 | // ARM pseudo code... |
| 739 | if (ConditionPassed()) |
| 740 | { |
| 741 | EncodingSpecificOperations(); |
| 742 | if CurrentInstrSet() == InstrSet_ARM then |
| 743 | LR = PC - 4; |
| 744 | else |
| 745 | LR = PC<31:1> : '1'; |
| 746 | if targetInstrSet == InstrSet_ARM then |
| 747 | targetAddress = Align(PC,4) + imm32; |
| 748 | else |
| 749 | targetAddress = PC + imm32; |
| 750 | SelectInstrSet(targetInstrSet); |
| 751 | BranchWritePC(targetAddress); |
| 752 | } |
| 753 | #endif |
| 754 | |
| 755 | bool success = false; |
| 756 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 757 | if (!success) |
| 758 | return false; |
| 759 | |
| 760 | if (ConditionPassed()) |
| 761 | { |
| 762 | EmulateInstruction::Context context = { EmulateInstruction::eContextRelativeBranchImmediate, 0, 0, 0}; |
| 763 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 764 | addr_t lr; // next instruction address |
| 765 | addr_t target; // target address |
| 766 | if (!success) |
| 767 | return false; |
| 768 | int32_t imm32; // PC-relative offset |
| 769 | switch (encoding) { |
| 770 | case eEncodingT2: |
| 771 | { |
| 772 | lr = (pc + 4) | 1u; // return address |
| 773 | uint32_t S = Bits32(opcode, 26, 26); |
| 774 | uint32_t imm10H = Bits32(opcode, 25, 16); |
| 775 | uint32_t J1 = Bits32(opcode, 13, 13); |
| 776 | uint32_t J2 = Bits32(opcode, 11, 11); |
| 777 | uint32_t imm10L = Bits32(opcode, 10, 1); |
| 778 | uint32_t I1 = !(J1 ^ S); |
| 779 | uint32_t I2 = !(J2 ^ S); |
| 780 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10H << 12) + (imm10L << 2); |
| 781 | imm32 = llvm::SignExtend32<25>(imm25); |
| 782 | target = (pc & 0xfffffffc) + 4 + imm32; |
| 783 | context.arg1 = eModeARM; // target instruction set |
| 784 | context.arg2 = 4 + imm32; // signed offset |
| 785 | break; |
| 786 | } |
| 787 | case eEncodingA2: |
| 788 | lr = pc + 4; // return address |
| 789 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1); |
| 790 | target = pc + 8 + imm32; |
| 791 | context.arg1 = eModeThumb; // target instruction set |
| 792 | context.arg2 = 8 + imm32; // signed offset |
| 793 | break; |
| 794 | default: |
| 795 | return false; |
| 796 | } |
| 797 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 798 | return false; |
| 799 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
| 800 | return false; |
| 801 | } |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | // Branch with Link and Exchange (register) calls a subroutine at an address and |
| 806 | // instruction set specified by a register. |
| 807 | // BLX (register) |
| 808 | bool |
| 809 | EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding) |
| 810 | { |
| 811 | #if 0 |
| 812 | // ARM pseudo code... |
| 813 | if (ConditionPassed()) |
| 814 | { |
| 815 | EncodingSpecificOperations(); |
| 816 | target = R[m]; |
| 817 | if CurrentInstrSet() == InstrSet_ARM then |
| 818 | next_instr_addr = PC - 4; |
| 819 | LR = next_instr_addr; |
| 820 | else |
| 821 | next_instr_addr = PC - 2; |
| 822 | LR = next_instr_addr<31:1> : ‘1’; |
| 823 | BXWritePC(target); |
| 824 | } |
| 825 | #endif |
| 826 | |
| 827 | bool success = false; |
| 828 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 829 | if (!success) |
| 830 | return false; |
| 831 | |
| 832 | if (ConditionPassed()) |
| 833 | { |
| 834 | EmulateInstruction::Context context = { EmulateInstruction::eContextAbsoluteBranchRegister, 0, 0, 0}; |
| 835 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 836 | addr_t lr; // next instruction address |
| 837 | addr_t target; // target address |
| 838 | if (!success) |
| 839 | return false; |
| 840 | uint32_t Rm; // the register with the target address |
| 841 | switch (encoding) { |
| 842 | case eEncodingT1: |
| 843 | lr = (pc + 2) | 1u; // return address |
| 844 | Rm = Bits32(opcode, 6, 3); |
| 845 | // if m == 15 then UNPREDICTABLE; |
| 846 | if (Rm == 15) |
| 847 | return false; |
| 848 | target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 849 | break; |
| 850 | case eEncodingA1: |
| 851 | lr = pc + 4; // return address |
| 852 | Rm = Bits32(opcode, 3, 0); |
| 853 | // if m == 15 then UNPREDICTABLE; |
| 854 | if (Rm == 15) |
| 855 | return false; |
| 856 | target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 857 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 858 | default: |
| 859 | return false; |
| 860 | } |
| 861 | bool toThumb; |
| 862 | if (BitIsSet(target, 0)) |
| 863 | toThumb = true; |
| 864 | else if (BitIsClear(target, 1)) |
| 865 | toThumb = false; |
| 866 | else |
| 867 | return false; // address<1:0> == ‘10’ => UNPREDICTABLE |
| 868 | context.arg0 = eRegisterKindDWARF; |
| 869 | context.arg1 = dwarf_r0 + Rm; |
| 870 | context.arg2 = toThumb ? eModeThumb : eModeARM; |
| 871 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 872 | return false; |
| 873 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
| 874 | return false; |
| 875 | } |
| 876 | return true; |
| 877 | } |
| 878 | |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 879 | // Set r7 to point to some ip offset. |
| 880 | // SUB (immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 881 | bool |
| 882 | EmulateInstructionARM::EmulateSubR7IPImmediate (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 883 | { |
| 884 | #if 0 |
| 885 | // ARM pseudo code... |
| 886 | if (ConditionPassed()) |
| 887 | { |
| 888 | EncodingSpecificOperations(); |
| 889 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 890 | if d == 15 then // Can only occur for ARM encoding |
| 891 | ALUWritePC(result); // setflags is always FALSE here |
| 892 | else |
| 893 | R[d] = result; |
| 894 | if setflags then |
| 895 | APSR.N = result<31>; |
| 896 | APSR.Z = IsZeroBit(result); |
| 897 | APSR.C = carry; |
| 898 | APSR.V = overflow; |
| 899 | } |
| 900 | #endif |
| 901 | |
| 902 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 903 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 904 | if (!success) |
| 905 | return false; |
| 906 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 907 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 908 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 909 | const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 910 | if (!success) |
| 911 | return false; |
| 912 | uint32_t imm32; |
| 913 | switch (encoding) { |
| 914 | case eEncodingA1: |
| 915 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 916 | break; |
| 917 | default: |
| 918 | return false; |
| 919 | } |
| 920 | addr_t ip_offset = imm32; |
| 921 | addr_t addr = ip - ip_offset; // the adjusted ip value |
| 922 | |
| 923 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 924 | eRegisterKindDWARF, |
| 925 | dwarf_r12, |
| 926 | -ip_offset }; |
| 927 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 928 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 929 | return false; |
| 930 | } |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | // Set ip to point to some stack offset. |
| 935 | // SUB (SP minus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 936 | bool |
| 937 | EmulateInstructionARM::EmulateSubIPSPImmediate (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 938 | { |
| 939 | #if 0 |
| 940 | // ARM pseudo code... |
| 941 | if (ConditionPassed()) |
| 942 | { |
| 943 | EncodingSpecificOperations(); |
| 944 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 945 | if d == 15 then // Can only occur for ARM encoding |
| 946 | ALUWritePC(result); // setflags is always FALSE here |
| 947 | else |
| 948 | R[d] = result; |
| 949 | if setflags then |
| 950 | APSR.N = result<31>; |
| 951 | APSR.Z = IsZeroBit(result); |
| 952 | APSR.C = carry; |
| 953 | APSR.V = overflow; |
| 954 | } |
| 955 | #endif |
| 956 | |
| 957 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 958 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 959 | if (!success) |
| 960 | return false; |
| 961 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 962 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 963 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 964 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 965 | if (!success) |
| 966 | return false; |
| 967 | uint32_t imm32; |
| 968 | switch (encoding) { |
| 969 | case eEncodingA1: |
| 970 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 971 | break; |
| 972 | default: |
| 973 | return false; |
| 974 | } |
| 975 | addr_t sp_offset = imm32; |
| 976 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 977 | |
| 978 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 979 | eRegisterKindGeneric, |
| 980 | LLDB_REGNUM_GENERIC_SP, |
| 981 | -sp_offset }; |
| 982 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 983 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 984 | return false; |
| 985 | } |
| 986 | return true; |
| 987 | } |
| 988 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 989 | // A sub operation to adjust the SP -- allocate space for local storage. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 990 | bool |
| 991 | EmulateInstructionARM::EmulateSubSPImmdiate (ARMEncoding encoding) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 992 | { |
| 993 | #if 0 |
| 994 | // ARM pseudo code... |
| 995 | if (ConditionPassed()) |
| 996 | { |
| 997 | EncodingSpecificOperations(); |
| 998 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 999 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1000 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1001 | else |
| 1002 | R[d] = result; |
| 1003 | if setflags then |
| 1004 | APSR.N = result<31>; |
| 1005 | APSR.Z = IsZeroBit(result); |
| 1006 | APSR.C = carry; |
| 1007 | APSR.V = overflow; |
| 1008 | } |
| 1009 | #endif |
| 1010 | |
| 1011 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1012 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1013 | if (!success) |
| 1014 | return false; |
| 1015 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1016 | if (ConditionPassed()) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1017 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1018 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1019 | if (!success) |
| 1020 | return false; |
| 1021 | uint32_t imm32; |
| 1022 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 1023 | case eEncodingT1: |
| 1024 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1025 | case eEncodingT2: |
| 1026 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 1027 | break; |
| 1028 | case eEncodingT3: |
| 1029 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 1030 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1031 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1032 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1033 | break; |
| 1034 | default: |
| 1035 | return false; |
| 1036 | } |
| 1037 | addr_t sp_offset = imm32; |
| 1038 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1039 | |
| 1040 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 1041 | eRegisterKindGeneric, |
| 1042 | LLDB_REGNUM_GENERIC_SP, |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1043 | -sp_offset }; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1044 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1045 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | } |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1051 | // A store operation to the stack that also updates the SP. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1052 | bool |
| 1053 | EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1054 | { |
| 1055 | #if 0 |
| 1056 | // ARM pseudo code... |
| 1057 | if (ConditionPassed()) |
| 1058 | { |
| 1059 | EncodingSpecificOperations(); |
| 1060 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 1061 | address = if index then offset_addr else R[n]; |
| 1062 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 1063 | if wback then R[n] = offset_addr; |
| 1064 | } |
| 1065 | #endif |
| 1066 | |
| 1067 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1068 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1069 | if (!success) |
| 1070 | return false; |
| 1071 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1072 | if (ConditionPassed()) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1073 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1074 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1075 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1076 | if (!success) |
| 1077 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1078 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1079 | uint32_t imm12; |
| 1080 | switch (encoding) { |
| 1081 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 1082 | Rt = Bits32(opcode, 15, 12); |
| 1083 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | default: |
| 1086 | return false; |
| 1087 | } |
| 1088 | addr_t sp_offset = imm12; |
| 1089 | addr_t addr = sp - sp_offset; |
| 1090 | |
| 1091 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1092 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1093 | { |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1094 | context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number |
| 1095 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1096 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1097 | if (!success) |
| 1098 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1099 | if (!WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1100 | return false; |
| 1101 | } |
| 1102 | else |
| 1103 | { |
| 1104 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 1105 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1106 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1107 | if (!success) |
| 1108 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1109 | if (!WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1110 | return false; |
| 1111 | } |
| 1112 | |
| 1113 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1114 | context.arg0 = eRegisterKindGeneric; |
| 1115 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1116 | context.arg2 = -sp_offset; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1117 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1118 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1124 | // Vector Push stores multiple extension registers to the stack. |
| 1125 | // 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] | 1126 | bool |
| 1127 | EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1128 | { |
| 1129 | #if 0 |
| 1130 | // ARM pseudo code... |
| 1131 | if (ConditionPassed()) |
| 1132 | { |
| 1133 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1134 | address = SP - imm32; |
| 1135 | SP = SP - imm32; |
| 1136 | if single_regs then |
| 1137 | for r = 0 to regs-1 |
| 1138 | MemA[address,4] = S[d+r]; address = address+4; |
| 1139 | else |
| 1140 | for r = 0 to regs-1 |
| 1141 | // Store as two word-aligned words in the correct order for current endianness. |
| 1142 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 1143 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 1144 | address = address+8; |
| 1145 | } |
| 1146 | #endif |
| 1147 | |
| 1148 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1149 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1150 | if (!success) |
| 1151 | return false; |
| 1152 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1153 | if (ConditionPassed()) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1154 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1155 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1156 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1157 | if (!success) |
| 1158 | return false; |
| 1159 | bool single_regs; |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1160 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1161 | uint32_t imm32; // stack offset |
| 1162 | uint32_t regs; // number of registers |
| 1163 | switch (encoding) { |
| 1164 | case eEncodingT1: |
| 1165 | case eEncodingA1: |
| 1166 | single_regs = false; |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1167 | d = Bits32(opcode, 22, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1168 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1169 | // If UInt(imm8) is odd, see "FSTMX". |
| 1170 | regs = Bits32(opcode, 7, 0) / 2; |
| 1171 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1172 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1173 | return false; |
| 1174 | break; |
| 1175 | case eEncodingT2: |
| 1176 | case eEncodingA2: |
| 1177 | single_regs = true; |
| 1178 | d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22); |
| 1179 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1180 | regs = Bits32(opcode, 7, 0); |
| 1181 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1182 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1183 | return false; |
| 1184 | break; |
| 1185 | default: |
| 1186 | return false; |
| 1187 | } |
| 1188 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1189 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1190 | addr_t sp_offset = imm32; |
| 1191 | addr_t addr = sp - sp_offset; |
| 1192 | uint32_t i; |
| 1193 | |
| 1194 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 1195 | for (i=d; i<regs; ++i) |
| 1196 | { |
| 1197 | context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number |
| 1198 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 1199 | // uint64_t to accommodate 64-bit registers. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1200 | uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1201 | if (!success) |
| 1202 | return false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1203 | if (!WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1204 | return false; |
| 1205 | addr += reg_byte_size; |
| 1206 | } |
| 1207 | |
| 1208 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1209 | context.arg0 = eRegisterKindGeneric; |
| 1210 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1211 | context.arg2 = -sp_offset; |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1212 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1213 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1214 | return false; |
| 1215 | } |
| 1216 | return true; |
| 1217 | } |
| 1218 | |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1219 | // Vector Pop loads multiple extension registers from the stack. |
| 1220 | // It also updates SP to point just above the loaded data. |
| 1221 | bool |
| 1222 | EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding) |
| 1223 | { |
| 1224 | #if 0 |
| 1225 | // ARM pseudo code... |
| 1226 | if (ConditionPassed()) |
| 1227 | { |
| 1228 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1229 | address = SP; |
| 1230 | SP = SP + imm32; |
| 1231 | if single_regs then |
| 1232 | for r = 0 to regs-1 |
| 1233 | S[d+r] = MemA[address,4]; address = address+4; |
| 1234 | else |
| 1235 | for r = 0 to regs-1 |
| 1236 | word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8; |
| 1237 | // Combine the word-aligned words in the correct order for current endianness. |
| 1238 | D[d+r] = if BigEndian() then word1:word2 else word2:word1; |
| 1239 | } |
| 1240 | #endif |
| 1241 | |
| 1242 | bool success = false; |
| 1243 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1244 | if (!success) |
| 1245 | return false; |
| 1246 | |
| 1247 | if (ConditionPassed()) |
| 1248 | { |
| 1249 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1250 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 1251 | if (!success) |
| 1252 | return false; |
| 1253 | bool single_regs; |
| 1254 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
| 1255 | uint32_t imm32; // stack offset |
| 1256 | uint32_t regs; // number of registers |
| 1257 | switch (encoding) { |
| 1258 | case eEncodingT1: |
| 1259 | case eEncodingA1: |
| 1260 | single_regs = false; |
| 1261 | d = Bits32(opcode, 22, 22) << 4 | Bits32(opcode, 15, 12); |
| 1262 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1263 | // If UInt(imm8) is odd, see "FLDMX". |
| 1264 | regs = Bits32(opcode, 7, 0) / 2; |
| 1265 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1266 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1267 | return false; |
| 1268 | break; |
| 1269 | case eEncodingT2: |
| 1270 | case eEncodingA2: |
| 1271 | single_regs = true; |
| 1272 | d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22); |
| 1273 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1274 | regs = Bits32(opcode, 7, 0); |
| 1275 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1276 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1277 | return false; |
| 1278 | break; |
| 1279 | default: |
| 1280 | return false; |
| 1281 | } |
| 1282 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1283 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1284 | addr_t sp_offset = imm32; |
| 1285 | addr_t addr = sp; |
| 1286 | uint32_t i; |
| 1287 | uint64_t data; // uint64_t to accomodate 64-bit registers. |
| 1288 | |
| 1289 | EmulateInstruction::Context context = { EmulateInstruction::eContextPopRegisterOffStack, eRegisterKindDWARF, 0, 0 }; |
| 1290 | for (i=d; i<regs; ++i) |
| 1291 | { |
| 1292 | context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number |
| 1293 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 1294 | data = ReadMemoryUnsigned(context, addr, reg_byte_size, 0, &success); |
| 1295 | if (!success) |
| 1296 | return false; |
| 1297 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, context.arg1, data)) |
| 1298 | return false; |
| 1299 | addr += reg_byte_size; |
| 1300 | } |
| 1301 | |
| 1302 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1303 | context.arg0 = eRegisterKindGeneric; |
| 1304 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
| 1305 | context.arg2 = sp_offset; |
| 1306 | |
| 1307 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
| 1308 | return false; |
| 1309 | } |
| 1310 | return true; |
| 1311 | } |
| 1312 | |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1313 | // SVC (previously SWI) |
| 1314 | bool |
| 1315 | EmulateInstructionARM::EmulateSVC (ARMEncoding encoding) |
| 1316 | { |
| 1317 | #if 0 |
| 1318 | // ARM pseudo code... |
| 1319 | if (ConditionPassed()) |
| 1320 | { |
| 1321 | EncodingSpecificOperations(); |
| 1322 | CallSupervisor(); |
| 1323 | } |
| 1324 | #endif |
| 1325 | |
| 1326 | bool success = false; |
| 1327 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1328 | if (!success) |
| 1329 | return false; |
| 1330 | |
| 1331 | if (ConditionPassed()) |
| 1332 | { |
| 1333 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1334 | addr_t lr; // next instruction address |
| 1335 | if (!success) |
| 1336 | return false; |
| 1337 | uint32_t imm32; // the immediate constant |
| 1338 | uint32_t mode; // ARM or Thumb mode |
| 1339 | switch (encoding) { |
| 1340 | case eEncodingT1: |
| 1341 | lr = (pc + 2) | 1u; // return address |
| 1342 | imm32 = Bits32(opcode, 7, 0); |
| 1343 | mode = eModeThumb; |
| 1344 | break; |
| 1345 | case eEncodingA1: |
| 1346 | lr = pc + 4; // return address |
| 1347 | imm32 = Bits32(opcode, 23, 0); |
| 1348 | mode = eModeARM; |
| 1349 | break; |
| 1350 | default: |
| 1351 | return false; |
| 1352 | } |
| 1353 | EmulateInstruction::Context context = { EmulateInstruction::eContextSupervisorCall, mode, imm32, 0}; |
| 1354 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1355 | return false; |
| 1356 | } |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1360 | // If Then makes up to four following instructions (the IT block) conditional. |
| 1361 | bool |
| 1362 | EmulateInstructionARM::EmulateIT (ARMEncoding encoding) |
| 1363 | { |
| 1364 | #if 0 |
| 1365 | // ARM pseudo code... |
| 1366 | EncodingSpecificOperations(); |
| 1367 | ITSTATE.IT<7:0> = firstcond:mask; |
| 1368 | #endif |
| 1369 | |
| 1370 | bool success = false; |
| 1371 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1372 | if (!success) |
| 1373 | return false; |
| 1374 | |
| 1375 | m_it_session.InitIT(Bits32(opcode, 7, 0)); |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1379 | EmulateInstructionARM::ARMOpcode* |
| 1380 | EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1381 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1382 | static ARMOpcode |
| 1383 | g_arm_opcodes[] = |
| 1384 | { |
| 1385 | //---------------------------------------------------------------------- |
| 1386 | // Prologue instructions |
| 1387 | //---------------------------------------------------------------------- |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1388 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1389 | // push register(s) |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1390 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePush, "push <registers>" }, |
| 1391 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePush, "push <register>" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 1392 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1393 | // set r7 to point to a stack offset |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1394 | { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #<const>" }, |
| 1395 | { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubR7IPImmediate, "sub r7, ip, #<const>"}, |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1396 | // set ip to point to a stack offset |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1397 | { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMovRdSP, "mov ip, sp" }, |
| 1398 | { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add ip, sp, #<const>" }, |
| 1399 | { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubIPSPImmediate, "sub ip, sp, #<const>"}, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1400 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1401 | // adjust the stack pointer |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1402 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1403 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1404 | // push one register |
| 1405 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1406 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" }, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1407 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1408 | // vector push consecutive extension register(s) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1409 | { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 1410 | { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 1411 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1412 | //---------------------------------------------------------------------- |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1413 | // Epilogue instructions |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1414 | //---------------------------------------------------------------------- |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 1415 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1416 | // To resolve ambiguity, "blx <label>" should come before "bl <label>". |
| 1417 | { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 1418 | { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 1419 | { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1420 | { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePop, "pop <registers>"}, |
| 1421 | { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePop, "pop <register>"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1422 | { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1423 | { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
| 1424 | |
| 1425 | //---------------------------------------------------------------------- |
| 1426 | // Supervisor Call (previously Software Interrupt) |
| 1427 | //---------------------------------------------------------------------- |
| 1428 | { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"} |
| 1429 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1430 | }; |
| 1431 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 1432 | |
| 1433 | for (size_t i=0; i<k_num_arm_opcodes; ++i) |
| 1434 | { |
| 1435 | if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value) |
| 1436 | return &g_arm_opcodes[i]; |
| 1437 | } |
| 1438 | return NULL; |
| 1439 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1440 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1441 | |
| 1442 | EmulateInstructionARM::ARMOpcode* |
| 1443 | EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode) |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 1444 | { |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1445 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1446 | static ARMOpcode |
| 1447 | g_thumb_opcodes[] = |
| 1448 | { |
| 1449 | //---------------------------------------------------------------------- |
| 1450 | // Prologue instructions |
| 1451 | //---------------------------------------------------------------------- |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 1452 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1453 | // push register(s) |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1454 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePush, "push <registers>" }, |
| 1455 | { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <registers>" }, |
| 1456 | { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePush, "push.w <register>" }, |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1457 | // move from high register to low register |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1458 | { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovLowHigh, "mov r0-r7, r8-r15" }, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1459 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1460 | // set r7 to point to a stack offset |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1461 | { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateAddRdSPImmediate, "add r7, sp, #imm" }, |
| 1462 | { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMovRdSP, "mov r7, sp" }, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1463 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1464 | // PC relative load into register (see also EmulateAddSPRm) |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1465 | { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRdPCRelative, "ldr <Rd>, [PC, #imm]"}, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1466 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1467 | // adjust the stack pointer |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1468 | { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPRm, "add sp, <Rm>"}, |
| 1469 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSubSPImmdiate, "add sp, sp, #imm"}, |
| 1470 | { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "sub.w sp, sp, #<const>"}, |
| 1471 | { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSubSPImmdiate, "subw sp, sp, #imm12"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1472 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1473 | // vector push consecutive extension register(s) |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1474 | { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 1475 | { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1476 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1477 | //---------------------------------------------------------------------- |
| 1478 | // Epilogue instructions |
| 1479 | //---------------------------------------------------------------------- |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 1480 | |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1481 | { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateAddSPImmediate, "add sp, #imm"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1482 | { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
| 1483 | // J1 == J2 == 1 |
| 1484 | { 0xf800e801, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 1485 | { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePop, "pop <registers>"}, |
| 1486 | { 0xffff0000, 0xe8bd0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <registers>" }, |
| 1487 | { 0xffff0fff, 0xf85d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePop, "pop.w <register>" }, |
| 1488 | { 0xffbf0f00, 0xecbd0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1489 | { 0xffbf0f00, 0xecbd0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
| 1490 | |
| 1491 | //---------------------------------------------------------------------- |
| 1492 | // Supervisor Call (previously Software Interrupt) |
| 1493 | //---------------------------------------------------------------------- |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1494 | { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"}, |
| 1495 | |
| 1496 | //---------------------------------------------------------------------- |
| 1497 | // If Then makes up to four following instructions conditional. |
| 1498 | //---------------------------------------------------------------------- |
| 1499 | { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"} |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1500 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1501 | }; |
| 1502 | |
| 1503 | const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
| 1504 | for (size_t i=0; i<k_num_thumb_opcodes; ++i) |
| 1505 | { |
| 1506 | if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value) |
| 1507 | return &g_thumb_opcodes[i]; |
| 1508 | } |
| 1509 | return NULL; |
| 1510 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1511 | |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 1512 | bool |
| 1513 | EmulateInstructionARM::SetTargetTriple (const ConstString &triple) |
| 1514 | { |
| 1515 | m_arm_isa = 0; |
| 1516 | const char *triple_cstr = triple.GetCString(); |
| 1517 | if (triple_cstr) |
| 1518 | { |
| 1519 | const char *dash = ::strchr (triple_cstr, '-'); |
| 1520 | if (dash) |
| 1521 | { |
| 1522 | std::string arch (triple_cstr, dash); |
| 1523 | const char *arch_cstr = arch.c_str(); |
| 1524 | if (strcasecmp(arch_cstr, "armv4t") == 0) |
| 1525 | m_arm_isa = ARMv4T; |
| 1526 | else if (strcasecmp(arch_cstr, "armv4") == 0) |
| 1527 | m_arm_isa = ARMv4; |
| 1528 | else if (strcasecmp(arch_cstr, "armv5tej") == 0) |
| 1529 | m_arm_isa = ARMv5TEJ; |
| 1530 | else if (strcasecmp(arch_cstr, "armv5te") == 0) |
| 1531 | m_arm_isa = ARMv5TE; |
| 1532 | else if (strcasecmp(arch_cstr, "armv5t") == 0) |
| 1533 | m_arm_isa = ARMv5T; |
| 1534 | else if (strcasecmp(arch_cstr, "armv6k") == 0) |
| 1535 | m_arm_isa = ARMv6K; |
| 1536 | else if (strcasecmp(arch_cstr, "armv6") == 0) |
| 1537 | m_arm_isa = ARMv6; |
| 1538 | else if (strcasecmp(arch_cstr, "armv6t2") == 0) |
| 1539 | m_arm_isa = ARMv6T2; |
| 1540 | else if (strcasecmp(arch_cstr, "armv7") == 0) |
| 1541 | m_arm_isa = ARMv7; |
| 1542 | else if (strcasecmp(arch_cstr, "armv8") == 0) |
| 1543 | m_arm_isa = ARMv8; |
| 1544 | } |
| 1545 | } |
| 1546 | return m_arm_isa != 0; |
| 1547 | } |
| 1548 | |
| 1549 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1550 | bool |
| 1551 | EmulateInstructionARM::ReadInstruction () |
| 1552 | { |
| 1553 | bool success = false; |
| 1554 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 1555 | if (success) |
| 1556 | { |
| 1557 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 1558 | if (success) |
| 1559 | { |
| 1560 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 1561 | if (m_inst_cpsr & MASK_CPSR_T) |
| 1562 | { |
| 1563 | m_inst_mode = eModeThumb; |
| 1564 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 1565 | |
| 1566 | if (success) |
| 1567 | { |
| 1568 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 1569 | { |
| 1570 | m_inst.opcode_type = eOpcode16; |
| 1571 | m_inst.opcode.inst16 = thumb_opcode; |
| 1572 | } |
| 1573 | else |
| 1574 | { |
| 1575 | m_inst.opcode_type = eOpcode32; |
| 1576 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | m_inst_mode = eModeARM; |
| 1583 | m_inst.opcode_type = eOpcode32; |
| 1584 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | if (!success) |
| 1589 | { |
| 1590 | m_inst_mode = eModeInvalid; |
| 1591 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 1592 | } |
| 1593 | return success; |
| 1594 | } |
| 1595 | |
| 1596 | uint32_t |
| 1597 | EmulateInstructionARM::CurrentCond () |
| 1598 | { |
| 1599 | switch (m_inst_mode) |
| 1600 | { |
| 1601 | default: |
| 1602 | case eModeInvalid: |
| 1603 | break; |
| 1604 | |
| 1605 | case eModeARM: |
| 1606 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 1607 | |
| 1608 | case eModeThumb: |
Johnny Chen | 8f128eb | 2011-02-05 01:39:52 +0000 | [diff] [blame] | 1609 | // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit |
| 1610 | // 'cond' field of the encoding. |
| 1611 | if (m_inst.opcode_type == eOpcode16 && |
| 1612 | Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d && |
| 1613 | Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f) |
| 1614 | { |
| 1615 | return Bits32(m_inst.opcode.inst16, 11, 7); |
| 1616 | } |
| 1617 | else if (m_inst.opcode_type == eOpcode32 && |
| 1618 | Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e && |
| 1619 | Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 && |
| 1620 | Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 && |
| 1621 | Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d) |
| 1622 | { |
| 1623 | return Bits32(m_inst.opcode.inst32, 25, 22); |
| 1624 | } |
| 1625 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1626 | return m_it_session.GetCond(); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1627 | } |
| 1628 | return UINT32_MAX; // Return invalid value |
| 1629 | } |
| 1630 | bool |
| 1631 | EmulateInstructionARM::ConditionPassed () |
| 1632 | { |
| 1633 | if (m_inst_cpsr == 0) |
| 1634 | return false; |
| 1635 | |
| 1636 | const uint32_t cond = CurrentCond (); |
| 1637 | |
| 1638 | if (cond == UINT32_MAX) |
| 1639 | return false; |
| 1640 | |
| 1641 | bool result = false; |
| 1642 | switch (UnsignedBits(cond, 3, 1)) |
| 1643 | { |
| 1644 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 1645 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 1646 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 1647 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 1648 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 1649 | case 5: |
| 1650 | { |
| 1651 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 1652 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 1653 | result = n == v; |
| 1654 | } |
| 1655 | break; |
| 1656 | case 6: |
| 1657 | { |
| 1658 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 1659 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 1660 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 1661 | } |
| 1662 | break; |
| 1663 | case 7: |
| 1664 | result = true; |
| 1665 | break; |
| 1666 | } |
| 1667 | |
| 1668 | if (cond & 1) |
| 1669 | result = !result; |
| 1670 | return result; |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | bool |
| 1675 | EmulateInstructionARM::EvaluateInstruction () |
| 1676 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1677 | // Advance the ITSTATE bits to their values for the next instruction. |
| 1678 | if (m_inst_mode == eModeThumb && m_it_session.InITBlock()) |
| 1679 | m_it_session.ITAdvance(); |
| 1680 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 1681 | return false; |
| 1682 | } |