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