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" |
Johnny Chen | 8584c92 | 2011-01-26 01:18:52 +0000 | [diff] [blame] | 11 | #include "ARMDefines.h" |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 12 | #include "ARMUtils.h" |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ConstString.h" |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace lldb; |
| 16 | using namespace lldb_private; |
| 17 | |
| 18 | // ARM constants used during decoding |
| 19 | #define REG_RD 0 |
| 20 | #define LDM_REGLIST 1 |
| 21 | #define PC_REG 15 |
| 22 | #define PC_REGLIST_BIT 0x8000 |
| 23 | |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 24 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 25 | #define ARMv4T (1u << 1) |
| 26 | #define ARMv5T (1u << 2) |
| 27 | #define ARMv5TE (1u << 3) |
| 28 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 29 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 30 | #define ARMv6K (1u << 6) |
| 31 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 32 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 33 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 34 | #define ARMvAll (0xffffffffu) |
| 35 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 36 | typedef enum |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 37 | { |
| 38 | eEncodingA1, |
| 39 | eEncodingA2, |
| 40 | eEncodingA3, |
| 41 | eEncodingA4, |
| 42 | eEncodingA5, |
| 43 | eEncodingT1, |
| 44 | eEncodingT2, |
| 45 | eEncodingT3, |
| 46 | eEncodingT4, |
| 47 | eEncodingT5, |
| 48 | } ARMEncoding; |
| 49 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 50 | typedef enum |
| 51 | { |
| 52 | eSize16, |
| 53 | eSize32 |
| 54 | } ARMInstrSize; |
| 55 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 56 | // Typedef for the callback function used during the emulation. |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 57 | // Pass along (ARMEncoding)encoding as the callback data. |
| 58 | typedef bool (*EmulateCallback) (EmulateInstructionARM *emulator, ARMEncoding encoding); |
| 59 | |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 60 | typedef struct |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 61 | { |
| 62 | uint32_t mask; |
| 63 | uint32_t value; |
| 64 | uint32_t variants; |
| 65 | ARMEncoding encoding; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 66 | ARMInstrSize size; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 67 | EmulateCallback callback; |
Johnny Chen | 4bee8ce | 2011-01-22 00:59:07 +0000 | [diff] [blame] | 68 | const char *name; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 69 | } ARMOpcode; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 70 | |
| 71 | static bool |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 72 | emulate_push (EmulateInstructionARM *emulator, ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 73 | { |
| 74 | #if 0 |
| 75 | // ARM pseudo code... |
| 76 | if (ConditionPassed()) |
| 77 | { |
| 78 | EncodingSpecificOperations(); |
| 79 | NullCheckIfThumbEE(13); |
| 80 | address = SP - 4*BitCount(registers); |
| 81 | |
| 82 | for (i = 0 to 14) |
| 83 | { |
| 84 | if (registers<i> == ’1’) |
| 85 | { |
| 86 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 87 | MemA[address,4] = bits(32) UNKNOWN; |
| 88 | else |
| 89 | MemA[address,4] = R[i]; |
| 90 | address = address + 4; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 95 | MemA[address,4] = PCStoreValue(); |
| 96 | |
| 97 | SP = SP - 4*BitCount(registers); |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | bool success = false; |
| 102 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 103 | if (!success) |
| 104 | return false; |
| 105 | |
| 106 | if (emulator->ConditionPassed()) |
| 107 | { |
| 108 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 109 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 110 | if (!success) |
| 111 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 112 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 113 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 114 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 115 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 116 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 117 | // The M bit represents LR. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 118 | if (Bits32(opcode, 8, 8)) |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 119 | registers |= 0x000eu; |
| 120 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 121 | if (BitCount(registers) < 1) |
| 122 | return false; |
| 123 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 124 | case eEncodingT2: |
| 125 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 126 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 127 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 128 | if (BitCount(registers) < 2) |
| 129 | return false; |
| 130 | break; |
| 131 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 132 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 133 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 134 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 135 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 136 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 137 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 138 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 139 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 140 | // Instead of return false, let's handle the following case as well, |
| 141 | // which amounts to pushing one reg onto the full descending stacks. |
| 142 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 143 | break; |
| 144 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 145 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 146 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 147 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 148 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 149 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 150 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 151 | default: |
| 152 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 153 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 154 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 155 | addr_t addr = sp - sp_offset; |
| 156 | uint32_t i; |
| 157 | |
| 158 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 159 | for (i=0; i<15; ++i) |
| 160 | { |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 161 | if (BitIsSet (registers, 1u << i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 162 | { |
| 163 | context.arg1 = dwarf_r0 + i; // arg1 in the context is the DWARF register number |
| 164 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 165 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 166 | if (!success) |
| 167 | return false; |
| 168 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 169 | return false; |
| 170 | addr += addr_byte_size; |
| 171 | } |
| 172 | } |
| 173 | |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 174 | if (BitIsSet (registers, 1u << 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 175 | { |
| 176 | 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] | 177 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 178 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 179 | if (!success) |
| 180 | return false; |
| 181 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 186 | context.arg0 = eRegisterKindGeneric; |
| 187 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 188 | context.arg2 = -sp_offset; |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 189 | |
| 190 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 191 | return false; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 196 | // 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] | 197 | // ADD (SP plus immediate) |
| 198 | static bool |
| 199 | emulate_add_rd_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 200 | { |
| 201 | #if 0 |
| 202 | // ARM pseudo code... |
| 203 | if (ConditionPassed()) |
| 204 | { |
| 205 | EncodingSpecificOperations(); |
| 206 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 207 | if d == 15 then |
| 208 | ALUWritePC(result); // setflags is always FALSE here |
| 209 | else |
| 210 | R[d] = result; |
| 211 | if setflags then |
| 212 | APSR.N = result<31>; |
| 213 | APSR.Z = IsZeroBit(result); |
| 214 | APSR.C = carry; |
| 215 | APSR.V = overflow; |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | bool success = false; |
| 220 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 221 | if (!success) |
| 222 | return false; |
| 223 | |
| 224 | if (emulator->ConditionPassed()) |
| 225 | { |
| 226 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 227 | if (!success) |
| 228 | return false; |
| 229 | uint32_t Rd; // the destination register |
| 230 | uint32_t imm32; |
| 231 | switch (encoding) { |
| 232 | case eEncodingT1: |
| 233 | Rd = 7; |
| 234 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32) |
| 235 | break; |
| 236 | case eEncodingA1: |
| 237 | Rd = Bits32(opcode, 15, 12); |
| 238 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 239 | break; |
| 240 | default: |
| 241 | return false; |
| 242 | } |
| 243 | addr_t sp_offset = imm32; |
| 244 | addr_t addr = sp + sp_offset; // a pointer to the stack area |
| 245 | |
| 246 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 247 | eRegisterKindGeneric, |
| 248 | LLDB_REGNUM_GENERIC_SP, |
| 249 | sp_offset }; |
| 250 | |
| 251 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr)) |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 257 | // Set r7 or ip to the current stack pointer. |
| 258 | // MOV (register) |
| 259 | static bool |
| 260 | emulate_mov_rd_sp (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 261 | { |
| 262 | #if 0 |
| 263 | // ARM pseudo code... |
| 264 | if (ConditionPassed()) |
| 265 | { |
| 266 | EncodingSpecificOperations(); |
| 267 | result = R[m]; |
| 268 | if d == 15 then |
| 269 | ALUWritePC(result); // setflags is always FALSE here |
| 270 | else |
| 271 | R[d] = result; |
| 272 | if setflags then |
| 273 | APSR.N = result<31>; |
| 274 | APSR.Z = IsZeroBit(result); |
| 275 | // APSR.C unchanged |
| 276 | // APSR.V unchanged |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | bool success = false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 281 | //const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 282 | //if (!success) |
| 283 | // return false; |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 284 | |
| 285 | if (emulator->ConditionPassed()) |
| 286 | { |
| 287 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 288 | if (!success) |
| 289 | return false; |
| 290 | uint32_t Rd; // the destination register |
| 291 | switch (encoding) { |
| 292 | case eEncodingT1: |
| 293 | Rd = 7; |
| 294 | break; |
| 295 | case eEncodingA1: |
| 296 | Rd = 12; |
| 297 | break; |
| 298 | default: |
| 299 | return false; |
| 300 | } |
| 301 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 302 | eRegisterKindGeneric, |
| 303 | LLDB_REGNUM_GENERIC_SP, |
| 304 | 0 }; |
| 305 | |
| 306 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp)) |
| 307 | return false; |
| 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 312 | // Move from high register (r8-r15) to low register (r0-r7). |
| 313 | // MOV (register) |
| 314 | static bool |
| 315 | emulate_mov_low_high (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 316 | { |
| 317 | #if 0 |
| 318 | // ARM pseudo code... |
| 319 | if (ConditionPassed()) |
| 320 | { |
| 321 | EncodingSpecificOperations(); |
| 322 | result = R[m]; |
| 323 | if d == 15 then |
| 324 | ALUWritePC(result); // setflags is always FALSE here |
| 325 | else |
| 326 | R[d] = result; |
| 327 | if setflags then |
| 328 | APSR.N = result<31>; |
| 329 | APSR.Z = IsZeroBit(result); |
| 330 | // APSR.C unchanged |
| 331 | // APSR.V unchanged |
| 332 | } |
| 333 | #endif |
| 334 | |
| 335 | bool success = false; |
| 336 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 337 | if (!success) |
| 338 | return false; |
| 339 | |
| 340 | if (emulator->ConditionPassed()) |
| 341 | { |
| 342 | uint32_t Rm; // the source register |
| 343 | uint32_t Rd; // the destination register |
| 344 | switch (encoding) { |
| 345 | case eEncodingT1: |
| 346 | Rm = Bits32(opcode, 6, 3); |
| 347 | Rd = Bits32(opcode, 2, 1); // bits(7) == 0 |
| 348 | break; |
| 349 | default: |
| 350 | return false; |
| 351 | } |
| 352 | int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 353 | if (!success) |
| 354 | return false; |
| 355 | |
| 356 | // The context specifies that Rm is to be moved into Rd. |
| 357 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 358 | eRegisterKindDWARF, |
| 359 | dwarf_r0 + Rm, |
| 360 | 0 }; |
| 361 | |
| 362 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, reg_value)) |
| 363 | return false; |
| 364 | } |
| 365 | return true; |
| 366 | } |
| 367 | |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 368 | // PC relative immediate load into register, possibly followed by ADD (SP plus register). |
| 369 | // LDR (literal) |
| 370 | static bool |
| 371 | emulate_ldr_rd_pc_rel (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 372 | { |
| 373 | #if 0 |
| 374 | // ARM pseudo code... |
| 375 | if (ConditionPassed()) |
| 376 | { |
| 377 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 378 | base = Align(PC,4); |
| 379 | address = if add then (base + imm32) else (base - imm32); |
| 380 | data = MemU[address,4]; |
| 381 | if t == 15 then |
| 382 | if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 383 | elsif UnalignedSupport() || address<1:0> = ‘00’ then |
| 384 | R[t] = data; |
| 385 | else // Can only apply before ARMv7 |
| 386 | if CurrentInstrSet() == InstrSet_ARM then |
| 387 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 388 | else |
| 389 | R[t] = bits(32) UNKNOWN; |
| 390 | } |
| 391 | #endif |
| 392 | |
| 393 | bool success = false; |
| 394 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 395 | if (!success) |
| 396 | return false; |
| 397 | |
| 398 | if (emulator->ConditionPassed()) |
| 399 | { |
| 400 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 401 | if (!success) |
| 402 | return false; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 403 | |
| 404 | // PC relative immediate load context |
| 405 | EmulateInstruction::Context context = {EmulateInstruction::eContextRegisterPlusOffset, |
| 406 | eRegisterKindGeneric, |
| 407 | LLDB_REGNUM_GENERIC_PC, |
| 408 | 0}; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 409 | uint32_t Rd; // the destination register |
| 410 | uint32_t imm32; // immediate offset from the PC |
| 411 | addr_t addr; // the PC relative address |
| 412 | uint32_t data; // the literal data value from the PC relative load |
| 413 | switch (encoding) { |
| 414 | case eEncodingT1: |
| 415 | Rd = Bits32(opcode, 10, 8); |
| 416 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32); |
| 417 | addr = pc + 4 + imm32; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 418 | context.arg2 = 4 + imm32; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 419 | break; |
| 420 | default: |
| 421 | return false; |
| 422 | } |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 423 | data = emulator->ReadMemoryUnsigned(context, addr, 4, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 424 | if (!success) |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 425 | return false; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 426 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, data)) |
| 427 | return false; |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 432 | // An add operation to adjust the SP. |
| 433 | // ADD (SP plus register) |
| 434 | static bool |
| 435 | emulate_add_sp_rm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 436 | { |
| 437 | #if 0 |
| 438 | // ARM pseudo code... |
| 439 | if (ConditionPassed()) |
| 440 | { |
| 441 | EncodingSpecificOperations(); |
| 442 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 443 | (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’); |
| 444 | if d == 15 then |
| 445 | ALUWritePC(result); // setflags is always FALSE here |
| 446 | else |
| 447 | R[d] = result; |
| 448 | if setflags then |
| 449 | APSR.N = result<31>; |
| 450 | APSR.Z = IsZeroBit(result); |
| 451 | APSR.C = carry; |
| 452 | APSR.V = overflow; |
| 453 | } |
| 454 | #endif |
| 455 | |
| 456 | bool success = false; |
| 457 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 458 | if (!success) |
| 459 | return false; |
| 460 | |
| 461 | if (emulator->ConditionPassed()) |
| 462 | { |
| 463 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 464 | if (!success) |
| 465 | return false; |
| 466 | uint32_t Rm; // the second operand |
| 467 | switch (encoding) { |
| 468 | case eEncodingT2: |
| 469 | Rm = Bits32(opcode, 6, 3); |
| 470 | break; |
| 471 | default: |
| 472 | return false; |
| 473 | } |
| 474 | int32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 475 | if (!success) |
| 476 | return false; |
| 477 | |
| 478 | addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value |
| 479 | |
| 480 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 481 | eRegisterKindGeneric, |
| 482 | LLDB_REGNUM_GENERIC_SP, |
| 483 | reg_value }; |
| 484 | |
| 485 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
| 486 | return false; |
| 487 | } |
| 488 | return true; |
| 489 | } |
| 490 | |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 491 | // Set r7 to point to some ip offset. |
| 492 | // SUB (immediate) |
| 493 | static bool |
| 494 | emulate_sub_r7_ip_imm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 495 | { |
| 496 | #if 0 |
| 497 | // ARM pseudo code... |
| 498 | if (ConditionPassed()) |
| 499 | { |
| 500 | EncodingSpecificOperations(); |
| 501 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 502 | if d == 15 then // Can only occur for ARM encoding |
| 503 | ALUWritePC(result); // setflags is always FALSE here |
| 504 | else |
| 505 | R[d] = result; |
| 506 | if setflags then |
| 507 | APSR.N = result<31>; |
| 508 | APSR.Z = IsZeroBit(result); |
| 509 | APSR.C = carry; |
| 510 | APSR.V = overflow; |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | bool success = false; |
| 515 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 516 | if (!success) |
| 517 | return false; |
| 518 | |
| 519 | if (emulator->ConditionPassed()) |
| 520 | { |
| 521 | const addr_t ip = emulator->ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success); |
| 522 | if (!success) |
| 523 | return false; |
| 524 | uint32_t imm32; |
| 525 | switch (encoding) { |
| 526 | case eEncodingA1: |
| 527 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 528 | break; |
| 529 | default: |
| 530 | return false; |
| 531 | } |
| 532 | addr_t ip_offset = imm32; |
| 533 | addr_t addr = ip - ip_offset; // the adjusted ip value |
| 534 | |
| 535 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 536 | eRegisterKindDWARF, |
| 537 | dwarf_r12, |
| 538 | -ip_offset }; |
| 539 | |
| 540 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr)) |
| 541 | return false; |
| 542 | } |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | // Set ip to point to some stack offset. |
| 547 | // SUB (SP minus immediate) |
| 548 | static bool |
| 549 | emulate_sub_ip_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 550 | { |
| 551 | #if 0 |
| 552 | // ARM pseudo code... |
| 553 | if (ConditionPassed()) |
| 554 | { |
| 555 | EncodingSpecificOperations(); |
| 556 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 557 | if d == 15 then // Can only occur for ARM encoding |
| 558 | ALUWritePC(result); // setflags is always FALSE here |
| 559 | else |
| 560 | R[d] = result; |
| 561 | if setflags then |
| 562 | APSR.N = result<31>; |
| 563 | APSR.Z = IsZeroBit(result); |
| 564 | APSR.C = carry; |
| 565 | APSR.V = overflow; |
| 566 | } |
| 567 | #endif |
| 568 | |
| 569 | bool success = false; |
| 570 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 571 | if (!success) |
| 572 | return false; |
| 573 | |
| 574 | if (emulator->ConditionPassed()) |
| 575 | { |
| 576 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 577 | if (!success) |
| 578 | return false; |
| 579 | uint32_t imm32; |
| 580 | switch (encoding) { |
| 581 | case eEncodingA1: |
| 582 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 583 | break; |
| 584 | default: |
| 585 | return false; |
| 586 | } |
| 587 | addr_t sp_offset = imm32; |
| 588 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 589 | |
| 590 | EmulateInstruction::Context context = { EmulateInstruction::eContextRegisterPlusOffset, |
| 591 | eRegisterKindGeneric, |
| 592 | LLDB_REGNUM_GENERIC_SP, |
| 593 | -sp_offset }; |
| 594 | |
| 595 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr)) |
| 596 | return false; |
| 597 | } |
| 598 | return true; |
| 599 | } |
| 600 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 601 | // A sub operation to adjust the SP -- allocate space for local storage. |
| 602 | static bool |
| 603 | emulate_sub_sp_imm (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 604 | { |
| 605 | #if 0 |
| 606 | // ARM pseudo code... |
| 607 | if (ConditionPassed()) |
| 608 | { |
| 609 | EncodingSpecificOperations(); |
| 610 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 611 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 612 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 613 | else |
| 614 | R[d] = result; |
| 615 | if setflags then |
| 616 | APSR.N = result<31>; |
| 617 | APSR.Z = IsZeroBit(result); |
| 618 | APSR.C = carry; |
| 619 | APSR.V = overflow; |
| 620 | } |
| 621 | #endif |
| 622 | |
| 623 | bool success = false; |
| 624 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 625 | if (!success) |
| 626 | return false; |
| 627 | |
| 628 | if (emulator->ConditionPassed()) |
| 629 | { |
| 630 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 631 | if (!success) |
| 632 | return false; |
| 633 | uint32_t imm32; |
| 634 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 635 | case eEncodingT1: |
| 636 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 637 | case eEncodingT2: |
| 638 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 639 | break; |
| 640 | case eEncodingT3: |
| 641 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 642 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 643 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 644 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 645 | break; |
| 646 | default: |
| 647 | return false; |
| 648 | } |
| 649 | addr_t sp_offset = imm32; |
| 650 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 651 | |
| 652 | EmulateInstruction::Context context = { EmulateInstruction::eContextAdjustStackPointer, |
| 653 | eRegisterKindGeneric, |
| 654 | LLDB_REGNUM_GENERIC_SP, |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 655 | -sp_offset }; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 656 | |
| 657 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
| 658 | return false; |
| 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | // A store operation to the stacks that also updates the SP. |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 664 | static bool |
| 665 | emulate_str_rt_sp (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 666 | { |
| 667 | #if 0 |
| 668 | // ARM pseudo code... |
| 669 | if (ConditionPassed()) |
| 670 | { |
| 671 | EncodingSpecificOperations(); |
| 672 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 673 | address = if index then offset_addr else R[n]; |
| 674 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 675 | if wback then R[n] = offset_addr; |
| 676 | } |
| 677 | #endif |
| 678 | |
| 679 | bool success = false; |
| 680 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 681 | if (!success) |
| 682 | return false; |
| 683 | |
| 684 | if (emulator->ConditionPassed()) |
| 685 | { |
| 686 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 687 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 688 | if (!success) |
| 689 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 690 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 691 | uint32_t imm12; |
| 692 | switch (encoding) { |
| 693 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 694 | Rt = Bits32(opcode, 15, 12); |
| 695 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 696 | break; |
| 697 | default: |
| 698 | return false; |
| 699 | } |
| 700 | addr_t sp_offset = imm12; |
| 701 | addr_t addr = sp - sp_offset; |
| 702 | |
| 703 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 704 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 705 | { |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 706 | context.arg1 = dwarf_r0 + Rt; // arg1 in the context is the DWARF register number |
| 707 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 708 | uint32_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 709 | if (!success) |
| 710 | return false; |
| 711 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, addr_byte_size)) |
| 712 | return false; |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | context.arg1 = dwarf_pc; // arg1 in the context is the DWARF register number |
| 717 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 718 | const uint32_t pc = emulator->ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 719 | if (!success) |
| 720 | return false; |
| 721 | if (!emulator->WriteMemoryUnsigned (context, addr, pc + 8, addr_byte_size)) |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 726 | context.arg0 = eRegisterKindGeneric; |
| 727 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 728 | context.arg2 = -sp_offset; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 729 | |
| 730 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 731 | return false; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 736 | static bool |
| 737 | emulate_vpush (EmulateInstructionARM *emulator, ARMEncoding encoding) |
| 738 | { |
| 739 | #if 0 |
| 740 | // ARM pseudo code... |
| 741 | if (ConditionPassed()) |
| 742 | { |
| 743 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 744 | address = SP - imm32; |
| 745 | SP = SP - imm32; |
| 746 | if single_regs then |
| 747 | for r = 0 to regs-1 |
| 748 | MemA[address,4] = S[d+r]; address = address+4; |
| 749 | else |
| 750 | for r = 0 to regs-1 |
| 751 | // Store as two word-aligned words in the correct order for current endianness. |
| 752 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 753 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 754 | address = address+8; |
| 755 | } |
| 756 | #endif |
| 757 | |
| 758 | bool success = false; |
| 759 | const uint32_t opcode = emulator->OpcodeAsUnsigned (&success); |
| 760 | if (!success) |
| 761 | return false; |
| 762 | |
| 763 | if (emulator->ConditionPassed()) |
| 764 | { |
| 765 | const uint32_t addr_byte_size = emulator->GetAddressByteSize(); |
| 766 | const addr_t sp = emulator->ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 767 | if (!success) |
| 768 | return false; |
| 769 | bool single_regs; |
| 770 | uint32_t d; // UInt(Vd:D) starting register |
| 771 | uint32_t imm32; // stack offset |
| 772 | uint32_t regs; // number of registers |
| 773 | switch (encoding) { |
| 774 | case eEncodingT1: |
| 775 | case eEncodingA1: |
| 776 | single_regs = false; |
| 777 | d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22); |
| 778 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 779 | // If UInt(imm8) is odd, see "FSTMX". |
| 780 | regs = Bits32(opcode, 7, 0) / 2; |
| 781 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 782 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 783 | return false; |
| 784 | break; |
| 785 | case eEncodingT2: |
| 786 | case eEncodingA2: |
| 787 | single_regs = true; |
| 788 | d = Bits32(opcode, 15, 12) << 1 | Bits32(opcode, 22, 22); |
| 789 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 790 | regs = Bits32(opcode, 7, 0); |
| 791 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 792 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 793 | return false; |
| 794 | break; |
| 795 | default: |
| 796 | return false; |
| 797 | } |
| 798 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 799 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 800 | addr_t sp_offset = imm32; |
| 801 | addr_t addr = sp - sp_offset; |
| 802 | uint32_t i; |
| 803 | |
| 804 | EmulateInstruction::Context context = { EmulateInstruction::eContextPushRegisterOnStack, eRegisterKindDWARF, 0, 0 }; |
| 805 | for (i=d; i<regs; ++i) |
| 806 | { |
| 807 | context.arg1 = start_reg + i; // arg1 in the context is the DWARF register number |
| 808 | context.arg2 = addr - sp; // arg2 in the context is the stack pointer offset |
| 809 | // uint64_t to accommodate 64-bit registers. |
| 810 | uint64_t reg_value = emulator->ReadRegisterUnsigned(eRegisterKindDWARF, context.arg1, 0, &success); |
| 811 | if (!success) |
| 812 | return false; |
| 813 | if (!emulator->WriteMemoryUnsigned (context, addr, reg_value, reg_byte_size)) |
| 814 | return false; |
| 815 | addr += reg_byte_size; |
| 816 | } |
| 817 | |
| 818 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 819 | context.arg0 = eRegisterKindGeneric; |
| 820 | context.arg1 = LLDB_REGNUM_GENERIC_SP; |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 821 | context.arg2 = -sp_offset; |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 822 | |
| 823 | if (!emulator->WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
| 824 | return false; |
| 825 | } |
| 826 | return true; |
| 827 | } |
| 828 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 829 | static ARMOpcode g_arm_opcodes[] = |
| 830 | { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 831 | // push register(s) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 832 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, emulate_push, "push <registers>" }, |
| 833 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, emulate_push, "push <register>" }, |
| 834 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 835 | // set r7 to point to a stack offset |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 836 | { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add r7, sp, #<const>" }, |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 837 | { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, emulate_sub_r7_ip_imm, "sub r7, ip, #<const>"}, |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 838 | // set ip to point to a stack offset |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 839 | { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, emulate_mov_rd_sp, "mov ip, sp" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 840 | { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, emulate_add_rd_sp_imm, "add ip, sp, #<const>" }, |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 841 | { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, emulate_sub_ip_sp_imm, "sub ip, sp, #<const>"}, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 842 | |
| 843 | // adjust the stack pointer |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 844 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, emulate_sub_sp_imm, "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 845 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 846 | // push one register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 847 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 848 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, emulate_str_rt_sp, "str Rt, [sp, #-imm12]!" }, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 849 | |
| 850 | // vector push consecutive extension register(s) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 851 | { 0x0fbf0f00, 0x0d2d0b00, ARMv6T2|ARMv7, eEncodingA1, eSize32, emulate_vpush, "vpush.64 <list>"}, |
| 852 | { 0x0fbf0f00, 0x0d2d0a00, ARMv6T2|ARMv7, eEncodingA2, eSize32, emulate_vpush, "vpush.32 <list>"} |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 853 | }; |
| 854 | |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 855 | static ARMOpcode g_thumb_opcodes[] = |
| 856 | { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 857 | // push register(s) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 858 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, emulate_push, "push <registers>" }, |
| 859 | { 0xffff0000, 0xe92d0000, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_push, "push.w <registers>" }, |
| 860 | { 0xffff0fff, 0xf84d0d04, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_push, "push.w <register>" }, |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 861 | // move from high register to low register |
| 862 | { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, emulate_mov_low_high, "mov r0-r7, r8-r15" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 863 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 864 | // set r7 to point to a stack offset |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 865 | { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, emulate_add_rd_sp_imm, "add r7, sp, #imm" }, |
| 866 | { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, emulate_mov_rd_sp, "mov r7, sp" }, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 867 | |
| 868 | // PC relative load into register (see also emulate_add_sp_rm) |
| 869 | { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, emulate_ldr_rd_pc_rel, "ldr <Rd>, [PC, #imm]"}, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 870 | |
| 871 | // adjust the stack pointer |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 872 | { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, emulate_add_sp_rm, "add sp, <Rm>"}, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 873 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, emulate_sub_sp_imm, "add sp, sp, #imm"}, |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 874 | { 0xfbef8f00, 0xf1ad0d00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_sub_sp_imm, "sub.w sp, sp, #<const>"}, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 875 | { 0xfbff8f00, 0xf2ad0d00, ARMv6T2|ARMv7, eEncodingT3, eSize32, emulate_sub_sp_imm, "subw sp, sp, #imm12"}, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 876 | |
| 877 | // vector push consecutive extension register(s) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 878 | { 0xffbf0f00, 0xed2d0b00, ARMv6T2|ARMv7, eEncodingT1, eSize32, emulate_vpush, "vpush.64 <list>"}, |
| 879 | { 0xffbf0f00, 0xed2d0a00, ARMv6T2|ARMv7, eEncodingT2, eSize32, emulate_vpush, "vpush.32 <list>"} |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 880 | }; |
| 881 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 882 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 883 | static const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 884 | |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 885 | bool |
| 886 | EmulateInstructionARM::SetTargetTriple (const ConstString &triple) |
| 887 | { |
| 888 | m_arm_isa = 0; |
| 889 | const char *triple_cstr = triple.GetCString(); |
| 890 | if (triple_cstr) |
| 891 | { |
| 892 | const char *dash = ::strchr (triple_cstr, '-'); |
| 893 | if (dash) |
| 894 | { |
| 895 | std::string arch (triple_cstr, dash); |
| 896 | const char *arch_cstr = arch.c_str(); |
| 897 | if (strcasecmp(arch_cstr, "armv4t") == 0) |
| 898 | m_arm_isa = ARMv4T; |
| 899 | else if (strcasecmp(arch_cstr, "armv4") == 0) |
| 900 | m_arm_isa = ARMv4; |
| 901 | else if (strcasecmp(arch_cstr, "armv5tej") == 0) |
| 902 | m_arm_isa = ARMv5TEJ; |
| 903 | else if (strcasecmp(arch_cstr, "armv5te") == 0) |
| 904 | m_arm_isa = ARMv5TE; |
| 905 | else if (strcasecmp(arch_cstr, "armv5t") == 0) |
| 906 | m_arm_isa = ARMv5T; |
| 907 | else if (strcasecmp(arch_cstr, "armv6k") == 0) |
| 908 | m_arm_isa = ARMv6K; |
| 909 | else if (strcasecmp(arch_cstr, "armv6") == 0) |
| 910 | m_arm_isa = ARMv6; |
| 911 | else if (strcasecmp(arch_cstr, "armv6t2") == 0) |
| 912 | m_arm_isa = ARMv6T2; |
| 913 | else if (strcasecmp(arch_cstr, "armv7") == 0) |
| 914 | m_arm_isa = ARMv7; |
| 915 | else if (strcasecmp(arch_cstr, "armv8") == 0) |
| 916 | m_arm_isa = ARMv8; |
| 917 | } |
| 918 | } |
| 919 | return m_arm_isa != 0; |
| 920 | } |
| 921 | |
| 922 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 923 | bool |
| 924 | EmulateInstructionARM::ReadInstruction () |
| 925 | { |
| 926 | bool success = false; |
| 927 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 928 | if (success) |
| 929 | { |
| 930 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 931 | if (success) |
| 932 | { |
| 933 | Context read_inst_context = {eContextReadOpcode, 0, 0}; |
| 934 | if (m_inst_cpsr & MASK_CPSR_T) |
| 935 | { |
| 936 | m_inst_mode = eModeThumb; |
| 937 | uint32_t thumb_opcode = ReadMemoryUnsigned(read_inst_context, pc, 2, 0, &success); |
| 938 | |
| 939 | if (success) |
| 940 | { |
| 941 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 942 | { |
| 943 | m_inst.opcode_type = eOpcode16; |
| 944 | m_inst.opcode.inst16 = thumb_opcode; |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | m_inst.opcode_type = eOpcode32; |
| 949 | m_inst.opcode.inst32 = (thumb_opcode << 16) | ReadMemoryUnsigned(read_inst_context, pc + 2, 2, 0, &success); |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | else |
| 954 | { |
| 955 | m_inst_mode = eModeARM; |
| 956 | m_inst.opcode_type = eOpcode32; |
| 957 | m_inst.opcode.inst32 = ReadMemoryUnsigned(read_inst_context, pc, 4, 0, &success); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | if (!success) |
| 962 | { |
| 963 | m_inst_mode = eModeInvalid; |
| 964 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 965 | } |
| 966 | return success; |
| 967 | } |
| 968 | |
| 969 | uint32_t |
| 970 | EmulateInstructionARM::CurrentCond () |
| 971 | { |
| 972 | switch (m_inst_mode) |
| 973 | { |
| 974 | default: |
| 975 | case eModeInvalid: |
| 976 | break; |
| 977 | |
| 978 | case eModeARM: |
| 979 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 980 | |
| 981 | case eModeThumb: |
| 982 | return 0x0000000Eu; // Return always for now, we need to handl IT instructions later |
| 983 | } |
| 984 | return UINT32_MAX; // Return invalid value |
| 985 | } |
| 986 | bool |
| 987 | EmulateInstructionARM::ConditionPassed () |
| 988 | { |
| 989 | if (m_inst_cpsr == 0) |
| 990 | return false; |
| 991 | |
| 992 | const uint32_t cond = CurrentCond (); |
| 993 | |
| 994 | if (cond == UINT32_MAX) |
| 995 | return false; |
| 996 | |
| 997 | bool result = false; |
| 998 | switch (UnsignedBits(cond, 3, 1)) |
| 999 | { |
| 1000 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 1001 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 1002 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 1003 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 1004 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 1005 | case 5: |
| 1006 | { |
| 1007 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 1008 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 1009 | result = n == v; |
| 1010 | } |
| 1011 | break; |
| 1012 | case 6: |
| 1013 | { |
| 1014 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 1015 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 1016 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 1017 | } |
| 1018 | break; |
| 1019 | case 7: |
| 1020 | result = true; |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | if (cond & 1) |
| 1025 | result = !result; |
| 1026 | return result; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | bool |
| 1031 | EmulateInstructionARM::EvaluateInstruction () |
| 1032 | { |
| 1033 | return false; |
| 1034 | } |