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 | |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 12 | #include "EmulateInstructionARM.h" |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ArchSpec.h" |
Greg Clayton | 8482ded | 2011-02-01 00:04:43 +0000 | [diff] [blame] | 14 | #include "lldb/Core/ConstString.h" |
| 15 | |
Greg Clayton | f29a08f | 2011-02-09 17:41:27 +0000 | [diff] [blame] | 16 | #include "Plugins/Process/Utility/ARMDefines.h" |
| 17 | #include "Plugins/Process/Utility/ARMUtils.h" |
| 18 | #include "Utility/ARM_DWARF_Registers.h" |
| 19 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" // for SignExtend32 template function |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 21 | // and CountTrailingZeros_32 function |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 26 | // Convenient macro definitions. |
| 27 | #define APSR_C Bit32(m_inst_cpsr, CPSR_C) |
| 28 | #define APSR_V Bit32(m_inst_cpsr, CPSR_V) |
| 29 | |
Caroline Tice | f55261f | 2011-02-18 22:24:22 +0000 | [diff] [blame] | 30 | #define AlignPC(pc_val) (pc_val & 0xFFFFFFFC) |
| 31 | |
Johnny Chen | 0e00af2 | 2011-02-10 19:40:42 +0000 | [diff] [blame] | 32 | //---------------------------------------------------------------------- |
| 33 | // |
| 34 | // ITSession implementation |
| 35 | // |
| 36 | //---------------------------------------------------------------------- |
| 37 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 38 | // A8.6.50 |
| 39 | // Valid return values are {1, 2, 3, 4}, with 0 signifying an error condition. |
| 40 | static unsigned short CountITSize(unsigned ITMask) { |
| 41 | // First count the trailing zeros of the IT mask. |
| 42 | unsigned TZ = llvm::CountTrailingZeros_32(ITMask); |
| 43 | if (TZ > 3) |
| 44 | { |
| 45 | printf("Encoding error: IT Mask '0000'\n"); |
| 46 | return 0; |
| 47 | } |
| 48 | return (4 - TZ); |
| 49 | } |
| 50 | |
| 51 | // Init ITState. Note that at least one bit is always 1 in mask. |
| 52 | bool ITSession::InitIT(unsigned short bits7_0) |
| 53 | { |
| 54 | ITCounter = CountITSize(Bits32(bits7_0, 3, 0)); |
| 55 | if (ITCounter == 0) |
| 56 | return false; |
| 57 | |
| 58 | // A8.6.50 IT |
| 59 | unsigned short FirstCond = Bits32(bits7_0, 7, 4); |
| 60 | if (FirstCond == 0xF) |
| 61 | { |
| 62 | printf("Encoding error: IT FirstCond '1111'\n"); |
| 63 | return false; |
| 64 | } |
| 65 | if (FirstCond == 0xE && ITCounter != 1) |
| 66 | { |
| 67 | printf("Encoding error: IT FirstCond '1110' && Mask != '1000'\n"); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | ITState = bits7_0; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // Update ITState if necessary. |
| 76 | void ITSession::ITAdvance() |
| 77 | { |
| 78 | assert(ITCounter); |
| 79 | --ITCounter; |
| 80 | if (ITCounter == 0) |
| 81 | ITState = 0; |
| 82 | else |
| 83 | { |
| 84 | unsigned short NewITState4_0 = Bits32(ITState, 4, 0) << 1; |
| 85 | SetBits32(ITState, 4, 0, NewITState4_0); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Return true if we're inside an IT Block. |
| 90 | bool ITSession::InITBlock() |
| 91 | { |
| 92 | return ITCounter != 0; |
| 93 | } |
| 94 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 95 | // Return true if we're the last instruction inside an IT Block. |
| 96 | bool ITSession::LastInITBlock() |
| 97 | { |
| 98 | return ITCounter == 1; |
| 99 | } |
| 100 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 101 | // Get condition bits for the current thumb instruction. |
| 102 | uint32_t ITSession::GetCond() |
| 103 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 104 | if (InITBlock()) |
| 105 | return Bits32(ITState, 7, 4); |
| 106 | else |
| 107 | return COND_AL; |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 110 | // ARM constants used during decoding |
| 111 | #define REG_RD 0 |
| 112 | #define LDM_REGLIST 1 |
| 113 | #define PC_REG 15 |
| 114 | #define PC_REGLIST_BIT 0x8000 |
| 115 | |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 116 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 117 | #define ARMv4T (1u << 1) |
| 118 | #define ARMv5T (1u << 2) |
| 119 | #define ARMv5TE (1u << 3) |
| 120 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 121 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 122 | #define ARMv6K (1u << 6) |
| 123 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 124 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 125 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 126 | #define ARMvAll (0xffffffffu) |
| 127 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 128 | #define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 129 | #define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 130 | #define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 131 | |
Johnny Chen | 0e00af2 | 2011-02-10 19:40:42 +0000 | [diff] [blame] | 132 | //---------------------------------------------------------------------- |
| 133 | // |
| 134 | // EmulateInstructionARM implementation |
| 135 | // |
| 136 | //---------------------------------------------------------------------- |
| 137 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 138 | void |
| 139 | EmulateInstructionARM::Initialize () |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 140 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 141 | } |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 142 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 143 | void |
| 144 | EmulateInstructionARM::Terminate () |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 145 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 148 | // Write "bits (32) UNKNOWN" to memory address "address". Helper function for many ARM instructions. |
| 149 | bool |
| 150 | EmulateInstructionARM::WriteBits32UnknownToMemory (addr_t address) |
| 151 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 152 | EmulateInstruction::Context context; |
| 153 | context.type = EmulateInstruction::eContextWriteMemoryRandomBits; |
| 154 | context.SetNoArgs (); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 155 | |
| 156 | uint32_t random_data = rand (); |
| 157 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 158 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 159 | if (!MemAWrite (context, address, random_data, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 160 | return false; |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 165 | // Write "bits (32) UNKNOWN" to register n. Helper function for many ARM instructions. |
| 166 | bool |
| 167 | EmulateInstructionARM::WriteBits32Unknown (int n) |
| 168 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 169 | EmulateInstruction::Context context; |
| 170 | context.type = EmulateInstruction::eContextWriteRegisterRandomBits; |
| 171 | context.SetNoArgs (); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 172 | |
Johnny Chen | 62ff6f5 | 2011-02-11 18:11:22 +0000 | [diff] [blame] | 173 | bool success; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 174 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 175 | |
| 176 | if (!success) |
| 177 | return false; |
| 178 | |
| 179 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 180 | return false; |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 185 | // Push Multiple Registers stores multiple registers to the stack, storing to |
| 186 | // consecutive memory locations ending just below the address in SP, and updates |
| 187 | // SP to point to the start of the stored data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 188 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 189 | EmulateInstructionARM::EmulatePUSH (ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 190 | { |
| 191 | #if 0 |
| 192 | // ARM pseudo code... |
| 193 | if (ConditionPassed()) |
| 194 | { |
| 195 | EncodingSpecificOperations(); |
| 196 | NullCheckIfThumbEE(13); |
| 197 | address = SP - 4*BitCount(registers); |
| 198 | |
| 199 | for (i = 0 to 14) |
| 200 | { |
| 201 | if (registers<i> == ’1’) |
| 202 | { |
| 203 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 204 | MemA[address,4] = bits(32) UNKNOWN; |
| 205 | else |
| 206 | MemA[address,4] = R[i]; |
| 207 | address = address + 4; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 212 | MemA[address,4] = PCStoreValue(); |
| 213 | |
| 214 | SP = SP - 4*BitCount(registers); |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 219 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 220 | if (!success) |
| 221 | return false; |
| 222 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 223 | if (ConditionPassed()) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 224 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 225 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 226 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 227 | if (!success) |
| 228 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 229 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 230 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 231 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 232 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 233 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 234 | // The M bit represents LR. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 235 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 236 | registers |= (1u << 14); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 237 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 238 | if (BitCount(registers) < 1) |
| 239 | return false; |
| 240 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 241 | case eEncodingT2: |
| 242 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 243 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 244 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 245 | if (BitCount(registers) < 2) |
| 246 | return false; |
| 247 | break; |
| 248 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 249 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 250 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 251 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 252 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 253 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 254 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 255 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 256 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 257 | // Instead of return false, let's handle the following case as well, |
| 258 | // which amounts to pushing one reg onto the full descending stacks. |
| 259 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 260 | break; |
| 261 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 262 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 263 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 264 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 265 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 266 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 267 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 268 | default: |
| 269 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 270 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 271 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 272 | addr_t addr = sp - sp_offset; |
| 273 | uint32_t i; |
| 274 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 275 | EmulateInstruction::Context context; |
| 276 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 277 | Register dwarf_reg; |
| 278 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 279 | for (i=0; i<15; ++i) |
| 280 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 281 | if (BitIsSet (registers, i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 282 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 283 | dwarf_reg.num = dwarf_r0 + i; |
| 284 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
| 285 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 286 | if (!success) |
| 287 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 288 | if (!MemAWrite (context, addr, reg_value, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 289 | return false; |
| 290 | addr += addr_byte_size; |
| 291 | } |
| 292 | } |
| 293 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 294 | if (BitIsSet (registers, 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 295 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 296 | dwarf_reg.num = dwarf_pc; |
| 297 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 298 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 299 | if (!success) |
| 300 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 301 | if (!MemAWrite (context, addr, pc + 8, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
| 305 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 306 | context.SetImmediateSigned (-sp_offset); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 307 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 308 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 314 | // Pop Multiple Registers loads multiple registers from the stack, loading from |
| 315 | // consecutive memory locations staring at the address in SP, and updates |
| 316 | // SP to point just above the loaded data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 317 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 318 | EmulateInstructionARM::EmulatePOP (ARMEncoding encoding) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 319 | { |
| 320 | #if 0 |
| 321 | // ARM pseudo code... |
| 322 | if (ConditionPassed()) |
| 323 | { |
| 324 | EncodingSpecificOperations(); NullCheckIfThumbEE(13); |
| 325 | address = SP; |
| 326 | for i = 0 to 14 |
| 327 | if registers<i> == ‘1’ then |
| 328 | R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4; |
| 329 | if registers<15> == ‘1’ then |
| 330 | if UnalignedAllowed then |
| 331 | LoadWritePC(MemU[address,4]); |
| 332 | else |
| 333 | LoadWritePC(MemA[address,4]); |
| 334 | if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers); |
| 335 | if registers<13> == ‘1’ then SP = bits(32) UNKNOWN; |
| 336 | } |
| 337 | #endif |
| 338 | |
| 339 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 340 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 341 | if (!success) |
| 342 | return false; |
| 343 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 344 | if (ConditionPassed()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 345 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 346 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 347 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 348 | if (!success) |
| 349 | return false; |
| 350 | uint32_t registers = 0; |
| 351 | uint32_t Rt; // the destination register |
| 352 | switch (encoding) { |
| 353 | case eEncodingT1: |
| 354 | registers = Bits32(opcode, 7, 0); |
| 355 | // The P bit represents PC. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 356 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 357 | registers |= (1u << 15); |
| 358 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 359 | if (BitCount(registers) < 1) |
| 360 | return false; |
| 361 | break; |
| 362 | case eEncodingT2: |
| 363 | // Ignore bit 13. |
| 364 | registers = Bits32(opcode, 15, 0) & ~0x2000; |
| 365 | // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 366 | if (BitCount(registers) < 2 || (Bit32(opcode, 15) && Bit32(opcode, 14))) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 367 | return false; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 368 | // if registers<15> == '1' && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 369 | if (BitIsSet(registers, 15) && InITBlock() && !LastInITBlock()) |
| 370 | return false; |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 371 | break; |
| 372 | case eEncodingT3: |
| 373 | Rt = Bits32(opcode, 15, 12); |
| 374 | // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 375 | if (Rt == 13) |
| 376 | return false; |
| 377 | if (Rt == 15 && InITBlock() && !LastInITBlock()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 378 | return false; |
| 379 | registers = (1u << Rt); |
| 380 | break; |
| 381 | case eEncodingA1: |
| 382 | registers = Bits32(opcode, 15, 0); |
| 383 | // Instead of return false, let's handle the following case as well, |
| 384 | // which amounts to popping one reg from the full descending stacks. |
| 385 | // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD; |
| 386 | |
| 387 | // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 388 | if (BitIsSet(opcode, 13) && ArchVersion() >= ARMv7) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 389 | return false; |
| 390 | break; |
| 391 | case eEncodingA2: |
| 392 | Rt = Bits32(opcode, 15, 12); |
| 393 | // if t == 13 then UNPREDICTABLE; |
| 394 | if (Rt == dwarf_sp) |
| 395 | return false; |
| 396 | registers = (1u << Rt); |
| 397 | break; |
| 398 | default: |
| 399 | return false; |
| 400 | } |
| 401 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
| 402 | addr_t addr = sp; |
| 403 | uint32_t i, data; |
| 404 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 405 | EmulateInstruction::Context context; |
| 406 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 407 | Register dwarf_reg; |
| 408 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 409 | for (i=0; i<15; ++i) |
| 410 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 411 | if (BitIsSet (registers, i)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 412 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 413 | dwarf_reg.num = dwarf_r0 + i; |
| 414 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 415 | data = MemARead(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 416 | if (!success) |
| 417 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 418 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 419 | return false; |
| 420 | addr += addr_byte_size; |
| 421 | } |
| 422 | } |
| 423 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 424 | if (BitIsSet (registers, 15)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 425 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 426 | dwarf_reg.num = dwarf_pc; |
| 427 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 428 | data = MemARead(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 429 | if (!success) |
| 430 | return false; |
Johnny Chen | f3eaacf | 2011-02-09 19:30:49 +0000 | [diff] [blame] | 431 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 432 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 433 | return false; |
| 434 | addr += addr_byte_size; |
| 435 | } |
| 436 | |
| 437 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 438 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 439 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 440 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 446 | // 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] | 447 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 448 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 449 | EmulateInstructionARM::EmulateADDRdSPImm (ARMEncoding encoding) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 450 | { |
| 451 | #if 0 |
| 452 | // ARM pseudo code... |
| 453 | if (ConditionPassed()) |
| 454 | { |
| 455 | EncodingSpecificOperations(); |
| 456 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 457 | if d == 15 then |
| 458 | ALUWritePC(result); // setflags is always FALSE here |
| 459 | else |
| 460 | R[d] = result; |
| 461 | if setflags then |
| 462 | APSR.N = result<31>; |
| 463 | APSR.Z = IsZeroBit(result); |
| 464 | APSR.C = carry; |
| 465 | APSR.V = overflow; |
| 466 | } |
| 467 | #endif |
| 468 | |
| 469 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 470 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 471 | if (!success) |
| 472 | return false; |
| 473 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 474 | if (ConditionPassed()) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 475 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 476 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 477 | if (!success) |
| 478 | return false; |
| 479 | uint32_t Rd; // the destination register |
| 480 | uint32_t imm32; |
| 481 | switch (encoding) { |
| 482 | case eEncodingT1: |
| 483 | Rd = 7; |
| 484 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32) |
| 485 | break; |
| 486 | case eEncodingA1: |
| 487 | Rd = Bits32(opcode, 15, 12); |
| 488 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 489 | break; |
| 490 | default: |
| 491 | return false; |
| 492 | } |
| 493 | addr_t sp_offset = imm32; |
| 494 | addr_t addr = sp + sp_offset; // a pointer to the stack area |
| 495 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 496 | EmulateInstruction::Context context; |
| 497 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 498 | Register sp_reg; |
| 499 | sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 500 | context.SetRegisterPlusOffset (sp_reg, sp_offset); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 501 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 502 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr)) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 508 | // Set r7 or ip to the current stack pointer. |
| 509 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 510 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 511 | EmulateInstructionARM::EmulateMOVRdSP (ARMEncoding encoding) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 512 | { |
| 513 | #if 0 |
| 514 | // ARM pseudo code... |
| 515 | if (ConditionPassed()) |
| 516 | { |
| 517 | EncodingSpecificOperations(); |
| 518 | result = R[m]; |
| 519 | if d == 15 then |
| 520 | ALUWritePC(result); // setflags is always FALSE here |
| 521 | else |
| 522 | R[d] = result; |
| 523 | if setflags then |
| 524 | APSR.N = result<31>; |
| 525 | APSR.Z = IsZeroBit(result); |
| 526 | // APSR.C unchanged |
| 527 | // APSR.V unchanged |
| 528 | } |
| 529 | #endif |
| 530 | |
| 531 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 532 | //const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 533 | //if (!success) |
| 534 | // return false; |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 535 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 536 | if (ConditionPassed()) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 537 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 538 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 539 | if (!success) |
| 540 | return false; |
| 541 | uint32_t Rd; // the destination register |
| 542 | switch (encoding) { |
| 543 | case eEncodingT1: |
| 544 | Rd = 7; |
| 545 | break; |
| 546 | case eEncodingA1: |
| 547 | Rd = 12; |
| 548 | break; |
| 549 | default: |
| 550 | return false; |
| 551 | } |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 552 | |
| 553 | EmulateInstruction::Context context; |
| 554 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 555 | Register sp_reg; |
| 556 | sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 557 | context.SetRegisterPlusOffset (sp_reg, 0); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 558 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 559 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp)) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 560 | return false; |
| 561 | } |
| 562 | return true; |
| 563 | } |
| 564 | |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 565 | // Move from high register (r8-r15) to low register (r0-r7). |
| 566 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 567 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 568 | EmulateInstructionARM::EmulateMOVLowHigh (ARMEncoding encoding) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 569 | { |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 570 | return EmulateMOVRdRm (encoding); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // Move from register to register. |
| 574 | // MOV (register) |
| 575 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 576 | EmulateInstructionARM::EmulateMOVRdRm (ARMEncoding encoding) |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 577 | { |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 578 | #if 0 |
| 579 | // ARM pseudo code... |
| 580 | if (ConditionPassed()) |
| 581 | { |
| 582 | EncodingSpecificOperations(); |
| 583 | result = R[m]; |
| 584 | if d == 15 then |
| 585 | ALUWritePC(result); // setflags is always FALSE here |
| 586 | else |
| 587 | R[d] = result; |
| 588 | if setflags then |
| 589 | APSR.N = result<31>; |
| 590 | APSR.Z = IsZeroBit(result); |
| 591 | // APSR.C unchanged |
| 592 | // APSR.V unchanged |
| 593 | } |
| 594 | #endif |
| 595 | |
| 596 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 597 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 598 | if (!success) |
| 599 | return false; |
| 600 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 601 | if (ConditionPassed()) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 602 | { |
| 603 | uint32_t Rm; // the source register |
| 604 | uint32_t Rd; // the destination register |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 605 | bool setflags; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 606 | switch (encoding) { |
| 607 | case eEncodingT1: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 608 | Rd = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 609 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 610 | setflags = false; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 611 | if (Rd == 15 && InITBlock() && !LastInITBlock()) |
| 612 | return false; |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 613 | break; |
| 614 | case eEncodingT2: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 615 | Rd = Bits32(opcode, 2, 0); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 616 | Rm = Bits32(opcode, 5, 3); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 617 | setflags = true; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 618 | if (InITBlock()) |
| 619 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 620 | break; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 621 | case eEncodingT3: |
| 622 | Rd = Bits32(opcode, 11, 8); |
| 623 | Rm = Bits32(opcode, 3, 0); |
| 624 | setflags = BitIsSet(opcode, 20); |
| 625 | // if setflags && (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
| 626 | if (setflags && (BadReg(Rd) || BadReg(Rm))) |
| 627 | return false; |
| 628 | // if !setflags && (d == 15 || m == 15 || (d == 13 && m == 13)) then UNPREDICTABLE; |
| 629 | if (!setflags && (Rd == 15 || Rm == 15 || (Rd == 13 && Rm == 13))) |
| 630 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 631 | default: |
| 632 | return false; |
| 633 | } |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 634 | uint32_t result = ReadCoreReg(Rm, &success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 635 | if (!success) |
| 636 | return false; |
| 637 | |
| 638 | // The context specifies that Rm is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 639 | EmulateInstruction::Context context; |
| 640 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 641 | Register dwarf_reg; |
| 642 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 643 | context.SetRegisterPlusOffset (dwarf_reg, 0); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 644 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 645 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 646 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 651 | // Move (immediate) writes an immediate value to the destination register. It |
| 652 | // can optionally update the condition flags based on the value. |
| 653 | // MOV (immediate) |
| 654 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 655 | EmulateInstructionARM::EmulateMOVRdImm (ARMEncoding encoding) |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 656 | { |
| 657 | #if 0 |
| 658 | // ARM pseudo code... |
| 659 | if (ConditionPassed()) |
| 660 | { |
| 661 | EncodingSpecificOperations(); |
| 662 | result = imm32; |
| 663 | if d == 15 then // Can only occur for ARM encoding |
| 664 | ALUWritePC(result); // setflags is always FALSE here |
| 665 | else |
| 666 | R[d] = result; |
| 667 | if setflags then |
| 668 | APSR.N = result<31>; |
| 669 | APSR.Z = IsZeroBit(result); |
| 670 | APSR.C = carry; |
| 671 | // APSR.V unchanged |
| 672 | } |
| 673 | #endif |
| 674 | bool success = false; |
| 675 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 676 | if (!success) |
| 677 | return false; |
| 678 | |
| 679 | if (ConditionPassed()) |
| 680 | { |
| 681 | uint32_t Rd; // the destination register |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 682 | uint32_t imm32; // the immediate value to be written to Rd |
| 683 | uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C. |
| 684 | bool setflags; |
| 685 | switch (encoding) { |
| 686 | case eEncodingT1: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 687 | Rd = Bits32(opcode, 10, 8); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 688 | setflags = !InITBlock(); |
| 689 | imm32 = Bits32(opcode, 7, 0); // imm32 = ZeroExtend(imm8, 32) |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 690 | carry = APSR_C; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 691 | break; |
| 692 | case eEncodingT2: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 693 | Rd = Bits32(opcode, 11, 8); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 694 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 695 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); |
Johnny Chen | 9798cfc | 2011-02-14 23:33:58 +0000 | [diff] [blame] | 696 | if (BadReg(Rd)) |
| 697 | return false; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 698 | break; |
| 699 | default: |
| 700 | return false; |
| 701 | } |
| 702 | uint32_t result = imm32; |
| 703 | |
| 704 | // The context specifies that an immediate is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 705 | EmulateInstruction::Context context; |
| 706 | context.type = EmulateInstruction::eContextImmediate; |
| 707 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 708 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 709 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 710 | return false; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 711 | } |
| 712 | return true; |
| 713 | } |
| 714 | |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 715 | // Bitwise NOT (immediate) writes the bitwise inverse of an immediate value to |
| 716 | // the destination register. It can optionally update the condition flags based |
| 717 | // on the value. |
| 718 | // MVN (immediate) |
| 719 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 720 | EmulateInstructionARM::EmulateMVNRdImm (ARMEncoding encoding) |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 721 | { |
| 722 | #if 0 |
| 723 | // ARM pseudo code... |
| 724 | if (ConditionPassed()) |
| 725 | { |
| 726 | EncodingSpecificOperations(); |
| 727 | result = NOT(imm32); |
| 728 | if d == 15 then // Can only occur for ARM encoding |
| 729 | ALUWritePC(result); // setflags is always FALSE here |
| 730 | else |
| 731 | R[d] = result; |
| 732 | if setflags then |
| 733 | APSR.N = result<31>; |
| 734 | APSR.Z = IsZeroBit(result); |
| 735 | APSR.C = carry; |
| 736 | // APSR.V unchanged |
| 737 | } |
| 738 | #endif |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 739 | bool success = false; |
| 740 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 741 | if (!success) |
| 742 | return false; |
| 743 | |
| 744 | if (ConditionPassed()) |
| 745 | { |
| 746 | uint32_t Rd; // the destination register |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 747 | uint32_t imm12; // the first operand to ThumbExpandImm_C or ARMExpandImm_C |
| 748 | uint32_t imm32; // the output after ThumbExpandImm_C or ARMExpandImm_C |
| 749 | uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 750 | bool setflags; |
| 751 | switch (encoding) { |
| 752 | case eEncodingT1: |
| 753 | Rd = Bits32(opcode, 11, 8); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 754 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 755 | imm12 = Bit32(opcode, 26) << 11 | Bits32(opcode, 14, 12) << 8 | Bits32(opcode, 7, 0); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 756 | imm32 = ThumbExpandImm_C(imm12, APSR_C, carry); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 757 | break; |
| 758 | case eEncodingA1: |
| 759 | Rd = Bits32(opcode, 15, 12); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 760 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 761 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 762 | imm32 = ARMExpandImm_C(imm12, APSR_C, carry); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 763 | break; |
| 764 | default: |
| 765 | return false; |
| 766 | } |
| 767 | uint32_t result = ~imm32; |
| 768 | |
| 769 | // The context specifies that an immediate is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 770 | EmulateInstruction::Context context; |
| 771 | context.type = EmulateInstruction::eContextImmediate; |
| 772 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 773 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 774 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 775 | return false; |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 776 | } |
| 777 | return true; |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 780 | // PC relative immediate load into register, possibly followed by ADD (SP plus register). |
| 781 | // LDR (literal) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 782 | bool |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 783 | EmulateInstructionARM::EmulateLDRRtPCRelative (ARMEncoding encoding) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 784 | { |
| 785 | #if 0 |
| 786 | // ARM pseudo code... |
| 787 | if (ConditionPassed()) |
| 788 | { |
| 789 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 790 | base = Align(PC,4); |
| 791 | address = if add then (base + imm32) else (base - imm32); |
| 792 | data = MemU[address,4]; |
| 793 | if t == 15 then |
| 794 | if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 795 | elsif UnalignedSupport() || address<1:0> = ‘00’ then |
| 796 | R[t] = data; |
| 797 | else // Can only apply before ARMv7 |
| 798 | if CurrentInstrSet() == InstrSet_ARM then |
| 799 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 800 | else |
| 801 | R[t] = bits(32) UNKNOWN; |
| 802 | } |
| 803 | #endif |
| 804 | |
| 805 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 806 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 807 | if (!success) |
| 808 | return false; |
| 809 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 810 | if (ConditionPassed()) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 811 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 812 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 813 | if (!success) |
| 814 | return false; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 815 | |
| 816 | // PC relative immediate load context |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 817 | EmulateInstruction::Context context; |
| 818 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 819 | Register pc_reg; |
| 820 | pc_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 821 | context.SetRegisterPlusOffset (pc_reg, 0); |
| 822 | |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 823 | uint32_t Rt; // the destination register |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 824 | uint32_t imm32; // immediate offset from the PC |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 825 | bool add; // +imm32 or -imm32? |
| 826 | addr_t base; // the base address |
| 827 | addr_t address; // the PC relative address |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 828 | uint32_t data; // the literal data value from the PC relative load |
| 829 | switch (encoding) { |
| 830 | case eEncodingT1: |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 831 | Rt = Bits32(opcode, 10, 8); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 832 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 833 | add = true; |
| 834 | base = Align(pc + 4, 4); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 835 | context.SetRegisterPlusOffset (pc_reg, 4 + imm32); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 836 | break; |
| 837 | case eEncodingT2: |
| 838 | Rt = Bits32(opcode, 15, 12); |
| 839 | imm32 = Bits32(opcode, 11, 0) << 2; // imm32 = ZeroExtend(imm12, 32); |
| 840 | add = BitIsSet(opcode, 23); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 841 | if (Rt == 15 && InITBlock() && !LastInITBlock()) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 842 | return false; |
| 843 | base = Align(pc + 4, 4); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 844 | context.SetRegisterPlusOffset (pc_reg, 4 + imm32); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 845 | break; |
| 846 | default: |
| 847 | return false; |
| 848 | } |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 849 | |
| 850 | if (add) |
| 851 | address = base + imm32; |
| 852 | else |
| 853 | address = base - imm32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 854 | data = MemURead(context, address, 4, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 855 | if (!success) |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 856 | return false; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 857 | |
| 858 | if (Rt == 15) |
| 859 | { |
| 860 | if (Bits32(address, 1, 0) == 0) |
| 861 | { |
| 862 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 863 | if (!LoadWritePC(context, data)) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | else |
| 867 | return false; |
| 868 | } |
| 869 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 870 | { |
| 871 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 872 | return false; |
| 873 | } |
| 874 | else // We don't handle ARM for now. |
| 875 | return false; |
| 876 | |
| 877 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 878 | return false; |
| 879 | } |
| 880 | return true; |
| 881 | } |
| 882 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 883 | // An add operation to adjust the SP. |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 884 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 885 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 886 | EmulateInstructionARM::EmulateADDSPImm (ARMEncoding encoding) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 887 | { |
| 888 | #if 0 |
| 889 | // ARM pseudo code... |
| 890 | if (ConditionPassed()) |
| 891 | { |
| 892 | EncodingSpecificOperations(); |
| 893 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 894 | if d == 15 then // Can only occur for ARM encoding |
| 895 | ALUWritePC(result); // setflags is always FALSE here |
| 896 | else |
| 897 | R[d] = result; |
| 898 | if setflags then |
| 899 | APSR.N = result<31>; |
| 900 | APSR.Z = IsZeroBit(result); |
| 901 | APSR.C = carry; |
| 902 | APSR.V = overflow; |
| 903 | } |
| 904 | #endif |
| 905 | |
| 906 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 907 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 908 | if (!success) |
| 909 | return false; |
| 910 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 911 | if (ConditionPassed()) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 912 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 913 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 914 | if (!success) |
| 915 | return false; |
| 916 | uint32_t imm32; // the immediate operand |
| 917 | switch (encoding) { |
| 918 | case eEncodingT2: |
| 919 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
| 920 | break; |
| 921 | default: |
| 922 | return false; |
| 923 | } |
| 924 | addr_t sp_offset = imm32; |
| 925 | addr_t addr = sp + sp_offset; // the adjusted stack pointer value |
| 926 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 927 | EmulateInstruction::Context context; |
| 928 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 929 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 930 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 931 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 932 | return false; |
| 933 | } |
| 934 | return true; |
| 935 | } |
| 936 | |
| 937 | // An add operation to adjust the SP. |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 938 | // ADD (SP plus register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 939 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 940 | EmulateInstructionARM::EmulateADDSPRm (ARMEncoding encoding) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 941 | { |
| 942 | #if 0 |
| 943 | // ARM pseudo code... |
| 944 | if (ConditionPassed()) |
| 945 | { |
| 946 | EncodingSpecificOperations(); |
| 947 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 948 | (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’); |
| 949 | if d == 15 then |
| 950 | ALUWritePC(result); // setflags is always FALSE here |
| 951 | else |
| 952 | R[d] = result; |
| 953 | if setflags then |
| 954 | APSR.N = result<31>; |
| 955 | APSR.Z = IsZeroBit(result); |
| 956 | APSR.C = carry; |
| 957 | APSR.V = overflow; |
| 958 | } |
| 959 | #endif |
| 960 | |
| 961 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 962 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 963 | if (!success) |
| 964 | return false; |
| 965 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 966 | if (ConditionPassed()) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 967 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 968 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 969 | if (!success) |
| 970 | return false; |
| 971 | uint32_t Rm; // the second operand |
| 972 | switch (encoding) { |
| 973 | case eEncodingT2: |
| 974 | Rm = Bits32(opcode, 6, 3); |
| 975 | break; |
| 976 | default: |
| 977 | return false; |
| 978 | } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 979 | int32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 980 | if (!success) |
| 981 | return false; |
| 982 | |
| 983 | addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value |
| 984 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 985 | EmulateInstruction::Context context; |
| 986 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 987 | context.SetImmediateSigned (reg_value); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 988 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 989 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 990 | return false; |
| 991 | } |
| 992 | return true; |
| 993 | } |
| 994 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 995 | // Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine |
| 996 | // at a PC-relative address, and changes instruction set from ARM to Thumb, or |
| 997 | // from Thumb to ARM. |
| 998 | // BLX (immediate) |
| 999 | bool |
| 1000 | EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding) |
| 1001 | { |
| 1002 | #if 0 |
| 1003 | // ARM pseudo code... |
| 1004 | if (ConditionPassed()) |
| 1005 | { |
| 1006 | EncodingSpecificOperations(); |
| 1007 | if CurrentInstrSet() == InstrSet_ARM then |
| 1008 | LR = PC - 4; |
| 1009 | else |
| 1010 | LR = PC<31:1> : '1'; |
| 1011 | if targetInstrSet == InstrSet_ARM then |
| 1012 | targetAddress = Align(PC,4) + imm32; |
| 1013 | else |
| 1014 | targetAddress = PC + imm32; |
| 1015 | SelectInstrSet(targetInstrSet); |
| 1016 | BranchWritePC(targetAddress); |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
| 1020 | bool success = false; |
| 1021 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1022 | if (!success) |
| 1023 | return false; |
| 1024 | |
| 1025 | if (ConditionPassed()) |
| 1026 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1027 | EmulateInstruction::Context context; |
| 1028 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1029 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1030 | if (!success) |
| 1031 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1032 | addr_t lr; // next instruction address |
| 1033 | addr_t target; // target address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1034 | int32_t imm32; // PC-relative offset |
| 1035 | switch (encoding) { |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1036 | case eEncodingT1: |
| 1037 | { |
| 1038 | lr = (pc + 4) | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1039 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1040 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1041 | uint32_t J1 = Bit32(opcode, 13); |
| 1042 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1043 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 1044 | uint32_t I1 = !(J1 ^ S); |
| 1045 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1046 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1047 | imm32 = llvm::SignExtend32<25>(imm25); |
| 1048 | target = pc + 4 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1049 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1050 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1051 | return false; |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | } |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1054 | case eEncodingT2: |
| 1055 | { |
| 1056 | lr = (pc + 4) | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1057 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1058 | uint32_t imm10H = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1059 | uint32_t J1 = Bit32(opcode, 13); |
| 1060 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1061 | uint32_t imm10L = Bits32(opcode, 10, 1); |
| 1062 | uint32_t I1 = !(J1 ^ S); |
| 1063 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1064 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10H << 12) | (imm10L << 2); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1065 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1066 | target = Align(pc + 4, 4) + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1067 | context.SetModeAndImmediateSigned (eModeARM, 4 + imm32); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1068 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1069 | return false; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | } |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 1072 | case eEncodingA1: |
| 1073 | lr = pc + 4; // return address |
| 1074 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1075 | target = Align(pc + 8, 4) + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1076 | context.SetModeAndImmediateSigned (eModeARM, 8 + imm32); |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 1077 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1078 | case eEncodingA2: |
| 1079 | lr = pc + 4; // return address |
| 1080 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1); |
| 1081 | target = pc + 8 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1082 | context.SetModeAndImmediateSigned (eModeThumb, 8 + imm32); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | default: |
| 1085 | return false; |
| 1086 | } |
| 1087 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1088 | return false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1089 | if (!BranchWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1090 | return false; |
| 1091 | } |
| 1092 | return true; |
| 1093 | } |
| 1094 | |
| 1095 | // Branch with Link and Exchange (register) calls a subroutine at an address and |
| 1096 | // instruction set specified by a register. |
| 1097 | // BLX (register) |
| 1098 | bool |
| 1099 | EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding) |
| 1100 | { |
| 1101 | #if 0 |
| 1102 | // ARM pseudo code... |
| 1103 | if (ConditionPassed()) |
| 1104 | { |
| 1105 | EncodingSpecificOperations(); |
| 1106 | target = R[m]; |
| 1107 | if CurrentInstrSet() == InstrSet_ARM then |
| 1108 | next_instr_addr = PC - 4; |
| 1109 | LR = next_instr_addr; |
| 1110 | else |
| 1111 | next_instr_addr = PC - 2; |
| 1112 | LR = next_instr_addr<31:1> : ‘1’; |
| 1113 | BXWritePC(target); |
| 1114 | } |
| 1115 | #endif |
| 1116 | |
| 1117 | bool success = false; |
| 1118 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1119 | if (!success) |
| 1120 | return false; |
| 1121 | |
| 1122 | if (ConditionPassed()) |
| 1123 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1124 | EmulateInstruction::Context context; |
| 1125 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1126 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1127 | addr_t lr; // next instruction address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1128 | if (!success) |
| 1129 | return false; |
| 1130 | uint32_t Rm; // the register with the target address |
| 1131 | switch (encoding) { |
| 1132 | case eEncodingT1: |
| 1133 | lr = (pc + 2) | 1u; // return address |
| 1134 | Rm = Bits32(opcode, 6, 3); |
| 1135 | // if m == 15 then UNPREDICTABLE; |
| 1136 | if (Rm == 15) |
| 1137 | return false; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1138 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1139 | return false; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | case eEncodingA1: |
| 1142 | lr = pc + 4; // return address |
| 1143 | Rm = Bits32(opcode, 3, 0); |
| 1144 | // if m == 15 then UNPREDICTABLE; |
| 1145 | if (Rm == 15) |
| 1146 | return false; |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1147 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1148 | default: |
| 1149 | return false; |
| 1150 | } |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1151 | addr_t target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 1152 | if (!success) |
| 1153 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1154 | Register dwarf_reg; |
| 1155 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 1156 | context.SetRegister (dwarf_reg); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1157 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1158 | return false; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1159 | if (!BXWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
| 1162 | return true; |
| 1163 | } |
| 1164 | |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1165 | // Branch and Exchange causes a branch to an address and instruction set specified by a register. |
| 1166 | // BX |
| 1167 | bool |
| 1168 | EmulateInstructionARM::EmulateBXRm (ARMEncoding encoding) |
| 1169 | { |
| 1170 | #if 0 |
| 1171 | // ARM pseudo code... |
| 1172 | if (ConditionPassed()) |
| 1173 | { |
| 1174 | EncodingSpecificOperations(); |
| 1175 | BXWritePC(R[m]); |
| 1176 | } |
| 1177 | #endif |
| 1178 | |
| 1179 | bool success = false; |
| 1180 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1181 | if (!success) |
| 1182 | return false; |
| 1183 | |
| 1184 | if (ConditionPassed()) |
| 1185 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1186 | EmulateInstruction::Context context; |
| 1187 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1188 | uint32_t Rm; // the register with the target address |
| 1189 | switch (encoding) { |
| 1190 | case eEncodingT1: |
| 1191 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1192 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1193 | return false; |
| 1194 | break; |
| 1195 | case eEncodingA1: |
| 1196 | Rm = Bits32(opcode, 3, 0); |
| 1197 | break; |
| 1198 | default: |
| 1199 | return false; |
| 1200 | } |
| 1201 | addr_t target = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 1202 | if (!success) |
| 1203 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1204 | |
| 1205 | Register dwarf_reg; |
| 1206 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1207 | context.SetRegister (dwarf_reg); |
| 1208 | if (!BXWritePC(context, target)) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1209 | return false; |
| 1210 | } |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1214 | // Set r7 to point to some ip offset. |
| 1215 | // SUB (immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1216 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1217 | EmulateInstructionARM::EmulateSUBR7IPImm (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1218 | { |
| 1219 | #if 0 |
| 1220 | // ARM pseudo code... |
| 1221 | if (ConditionPassed()) |
| 1222 | { |
| 1223 | EncodingSpecificOperations(); |
| 1224 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1225 | if d == 15 then // Can only occur for ARM encoding |
| 1226 | ALUWritePC(result); // setflags is always FALSE here |
| 1227 | else |
| 1228 | R[d] = result; |
| 1229 | if setflags then |
| 1230 | APSR.N = result<31>; |
| 1231 | APSR.Z = IsZeroBit(result); |
| 1232 | APSR.C = carry; |
| 1233 | APSR.V = overflow; |
| 1234 | } |
| 1235 | #endif |
| 1236 | |
| 1237 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1238 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1239 | if (!success) |
| 1240 | return false; |
| 1241 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1242 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1243 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1244 | const addr_t ip = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r12, 0, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1245 | if (!success) |
| 1246 | return false; |
| 1247 | uint32_t imm32; |
| 1248 | switch (encoding) { |
| 1249 | case eEncodingA1: |
| 1250 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1251 | break; |
| 1252 | default: |
| 1253 | return false; |
| 1254 | } |
| 1255 | addr_t ip_offset = imm32; |
| 1256 | addr_t addr = ip - ip_offset; // the adjusted ip value |
| 1257 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1258 | EmulateInstruction::Context context; |
| 1259 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1260 | Register dwarf_reg; |
| 1261 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r12); |
| 1262 | context.SetRegisterPlusOffset (dwarf_reg, -ip_offset); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1263 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1264 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | // Set ip to point to some stack offset. |
| 1271 | // SUB (SP minus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1272 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1273 | EmulateInstructionARM::EmulateSUBIPSPImm (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1274 | { |
| 1275 | #if 0 |
| 1276 | // ARM pseudo code... |
| 1277 | if (ConditionPassed()) |
| 1278 | { |
| 1279 | EncodingSpecificOperations(); |
| 1280 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1281 | if d == 15 then // Can only occur for ARM encoding |
| 1282 | ALUWritePC(result); // setflags is always FALSE here |
| 1283 | else |
| 1284 | R[d] = result; |
| 1285 | if setflags then |
| 1286 | APSR.N = result<31>; |
| 1287 | APSR.Z = IsZeroBit(result); |
| 1288 | APSR.C = carry; |
| 1289 | APSR.V = overflow; |
| 1290 | } |
| 1291 | #endif |
| 1292 | |
| 1293 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1294 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1295 | if (!success) |
| 1296 | return false; |
| 1297 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1298 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1299 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1300 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1301 | if (!success) |
| 1302 | return false; |
| 1303 | uint32_t imm32; |
| 1304 | switch (encoding) { |
| 1305 | case eEncodingA1: |
| 1306 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1307 | break; |
| 1308 | default: |
| 1309 | return false; |
| 1310 | } |
| 1311 | addr_t sp_offset = imm32; |
| 1312 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1313 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1314 | EmulateInstruction::Context context; |
| 1315 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1316 | Register dwarf_reg; |
| 1317 | dwarf_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 1318 | context.SetRegisterPlusOffset (dwarf_reg, -sp_offset); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1319 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1320 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1326 | // A sub operation to adjust the SP -- allocate space for local storage. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1327 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1328 | EmulateInstructionARM::EmulateSUBSPImm (ARMEncoding encoding) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1329 | { |
| 1330 | #if 0 |
| 1331 | // ARM pseudo code... |
| 1332 | if (ConditionPassed()) |
| 1333 | { |
| 1334 | EncodingSpecificOperations(); |
| 1335 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1336 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1337 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1338 | else |
| 1339 | R[d] = result; |
| 1340 | if setflags then |
| 1341 | APSR.N = result<31>; |
| 1342 | APSR.Z = IsZeroBit(result); |
| 1343 | APSR.C = carry; |
| 1344 | APSR.V = overflow; |
| 1345 | } |
| 1346 | #endif |
| 1347 | |
| 1348 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1349 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1350 | if (!success) |
| 1351 | return false; |
| 1352 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1353 | if (ConditionPassed()) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1354 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1355 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1356 | if (!success) |
| 1357 | return false; |
| 1358 | uint32_t imm32; |
| 1359 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 1360 | case eEncodingT1: |
| 1361 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1362 | case eEncodingT2: |
| 1363 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 1364 | break; |
| 1365 | case eEncodingT3: |
| 1366 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 1367 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1368 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1369 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | default: |
| 1372 | return false; |
| 1373 | } |
| 1374 | addr_t sp_offset = imm32; |
| 1375 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1376 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1377 | EmulateInstruction::Context context; |
| 1378 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1379 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1380 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1381 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1382 | return false; |
| 1383 | } |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1387 | // A store operation to the stack that also updates the SP. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1388 | bool |
| 1389 | EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1390 | { |
| 1391 | #if 0 |
| 1392 | // ARM pseudo code... |
| 1393 | if (ConditionPassed()) |
| 1394 | { |
| 1395 | EncodingSpecificOperations(); |
| 1396 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 1397 | address = if index then offset_addr else R[n]; |
| 1398 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 1399 | if wback then R[n] = offset_addr; |
| 1400 | } |
| 1401 | #endif |
| 1402 | |
| 1403 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1404 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1405 | if (!success) |
| 1406 | return false; |
| 1407 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1408 | if (ConditionPassed()) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1409 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1410 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1411 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1412 | if (!success) |
| 1413 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1414 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1415 | uint32_t imm12; |
| 1416 | switch (encoding) { |
| 1417 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 1418 | Rt = Bits32(opcode, 15, 12); |
| 1419 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1420 | break; |
| 1421 | default: |
| 1422 | return false; |
| 1423 | } |
| 1424 | addr_t sp_offset = imm12; |
| 1425 | addr_t addr = sp - sp_offset; |
| 1426 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1427 | EmulateInstruction::Context context; |
| 1428 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 1429 | Register dwarf_reg; |
| 1430 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1431 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1432 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1433 | dwarf_reg.num = dwarf_r0 + Rt; |
| 1434 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
| 1435 | uint32_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1436 | if (!success) |
| 1437 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1438 | if (!MemUWrite (context, addr, reg_value, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1439 | return false; |
| 1440 | } |
| 1441 | else |
| 1442 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1443 | dwarf_reg.num = dwarf_pc; |
| 1444 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1445 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1446 | if (!success) |
| 1447 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1448 | if (!MemUWrite (context, addr, pc + 8, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1453 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1454 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1455 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1461 | // Vector Push stores multiple extension registers to the stack. |
| 1462 | // 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] | 1463 | bool |
| 1464 | EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1465 | { |
| 1466 | #if 0 |
| 1467 | // ARM pseudo code... |
| 1468 | if (ConditionPassed()) |
| 1469 | { |
| 1470 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1471 | address = SP - imm32; |
| 1472 | SP = SP - imm32; |
| 1473 | if single_regs then |
| 1474 | for r = 0 to regs-1 |
| 1475 | MemA[address,4] = S[d+r]; address = address+4; |
| 1476 | else |
| 1477 | for r = 0 to regs-1 |
| 1478 | // Store as two word-aligned words in the correct order for current endianness. |
| 1479 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 1480 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 1481 | address = address+8; |
| 1482 | } |
| 1483 | #endif |
| 1484 | |
| 1485 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1486 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1487 | if (!success) |
| 1488 | return false; |
| 1489 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1490 | if (ConditionPassed()) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1491 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1492 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1493 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1494 | if (!success) |
| 1495 | return false; |
| 1496 | bool single_regs; |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1497 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1498 | uint32_t imm32; // stack offset |
| 1499 | uint32_t regs; // number of registers |
| 1500 | switch (encoding) { |
| 1501 | case eEncodingT1: |
| 1502 | case eEncodingA1: |
| 1503 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1504 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1505 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1506 | // If UInt(imm8) is odd, see "FSTMX". |
| 1507 | regs = Bits32(opcode, 7, 0) / 2; |
| 1508 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1509 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1510 | return false; |
| 1511 | break; |
| 1512 | case eEncodingT2: |
| 1513 | case eEncodingA2: |
| 1514 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1515 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1516 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1517 | regs = Bits32(opcode, 7, 0); |
| 1518 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1519 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1520 | return false; |
| 1521 | break; |
| 1522 | default: |
| 1523 | return false; |
| 1524 | } |
| 1525 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1526 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1527 | addr_t sp_offset = imm32; |
| 1528 | addr_t addr = sp - sp_offset; |
| 1529 | uint32_t i; |
| 1530 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1531 | EmulateInstruction::Context context; |
| 1532 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 1533 | Register dwarf_reg; |
| 1534 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1535 | for (i=d; i<regs; ++i) |
| 1536 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1537 | dwarf_reg.num = start_reg + i; |
| 1538 | context.SetRegisterPlusOffset ( dwarf_reg, addr - sp); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1539 | // uint64_t to accommodate 64-bit registers. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1540 | uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1541 | if (!success) |
| 1542 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1543 | if (!MemAWrite (context, addr, reg_value, reg_byte_size)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1544 | return false; |
| 1545 | addr += reg_byte_size; |
| 1546 | } |
| 1547 | |
| 1548 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1549 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1550 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1551 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1552 | return false; |
| 1553 | } |
| 1554 | return true; |
| 1555 | } |
| 1556 | |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1557 | // Vector Pop loads multiple extension registers from the stack. |
| 1558 | // It also updates SP to point just above the loaded data. |
| 1559 | bool |
| 1560 | EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding) |
| 1561 | { |
| 1562 | #if 0 |
| 1563 | // ARM pseudo code... |
| 1564 | if (ConditionPassed()) |
| 1565 | { |
| 1566 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1567 | address = SP; |
| 1568 | SP = SP + imm32; |
| 1569 | if single_regs then |
| 1570 | for r = 0 to regs-1 |
| 1571 | S[d+r] = MemA[address,4]; address = address+4; |
| 1572 | else |
| 1573 | for r = 0 to regs-1 |
| 1574 | word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8; |
| 1575 | // Combine the word-aligned words in the correct order for current endianness. |
| 1576 | D[d+r] = if BigEndian() then word1:word2 else word2:word1; |
| 1577 | } |
| 1578 | #endif |
| 1579 | |
| 1580 | bool success = false; |
| 1581 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1582 | if (!success) |
| 1583 | return false; |
| 1584 | |
| 1585 | if (ConditionPassed()) |
| 1586 | { |
| 1587 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 1588 | const addr_t sp = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, 0, &success); |
| 1589 | if (!success) |
| 1590 | return false; |
| 1591 | bool single_regs; |
| 1592 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
| 1593 | uint32_t imm32; // stack offset |
| 1594 | uint32_t regs; // number of registers |
| 1595 | switch (encoding) { |
| 1596 | case eEncodingT1: |
| 1597 | case eEncodingA1: |
| 1598 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1599 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1600 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1601 | // If UInt(imm8) is odd, see "FLDMX". |
| 1602 | regs = Bits32(opcode, 7, 0) / 2; |
| 1603 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1604 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1605 | return false; |
| 1606 | break; |
| 1607 | case eEncodingT2: |
| 1608 | case eEncodingA2: |
| 1609 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1610 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1611 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1612 | regs = Bits32(opcode, 7, 0); |
| 1613 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1614 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1615 | return false; |
| 1616 | break; |
| 1617 | default: |
| 1618 | return false; |
| 1619 | } |
| 1620 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1621 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1622 | addr_t sp_offset = imm32; |
| 1623 | addr_t addr = sp; |
| 1624 | uint32_t i; |
| 1625 | uint64_t data; // uint64_t to accomodate 64-bit registers. |
| 1626 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1627 | EmulateInstruction::Context context; |
| 1628 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 1629 | Register dwarf_reg; |
| 1630 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1631 | for (i=d; i<regs; ++i) |
| 1632 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1633 | dwarf_reg.num = start_reg + i; |
| 1634 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1635 | data = MemARead(context, addr, reg_byte_size, 0, &success); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1636 | if (!success) |
| 1637 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1638 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data)) |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1639 | return false; |
| 1640 | addr += reg_byte_size; |
| 1641 | } |
| 1642 | |
| 1643 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1644 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
| 1647 | return false; |
| 1648 | } |
| 1649 | return true; |
| 1650 | } |
| 1651 | |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1652 | // SVC (previously SWI) |
| 1653 | bool |
| 1654 | EmulateInstructionARM::EmulateSVC (ARMEncoding encoding) |
| 1655 | { |
| 1656 | #if 0 |
| 1657 | // ARM pseudo code... |
| 1658 | if (ConditionPassed()) |
| 1659 | { |
| 1660 | EncodingSpecificOperations(); |
| 1661 | CallSupervisor(); |
| 1662 | } |
| 1663 | #endif |
| 1664 | |
| 1665 | bool success = false; |
| 1666 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1667 | if (!success) |
| 1668 | return false; |
| 1669 | |
| 1670 | if (ConditionPassed()) |
| 1671 | { |
| 1672 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1673 | addr_t lr; // next instruction address |
| 1674 | if (!success) |
| 1675 | return false; |
| 1676 | uint32_t imm32; // the immediate constant |
| 1677 | uint32_t mode; // ARM or Thumb mode |
| 1678 | switch (encoding) { |
| 1679 | case eEncodingT1: |
| 1680 | lr = (pc + 2) | 1u; // return address |
| 1681 | imm32 = Bits32(opcode, 7, 0); |
| 1682 | mode = eModeThumb; |
| 1683 | break; |
| 1684 | case eEncodingA1: |
| 1685 | lr = pc + 4; // return address |
| 1686 | imm32 = Bits32(opcode, 23, 0); |
| 1687 | mode = eModeARM; |
| 1688 | break; |
| 1689 | default: |
| 1690 | return false; |
| 1691 | } |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1692 | |
| 1693 | EmulateInstruction::Context context; |
| 1694 | context.type = EmulateInstruction::eContextSupervisorCall; |
| 1695 | context.SetModeAndImmediate (mode, imm32); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1696 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1697 | return false; |
| 1698 | } |
| 1699 | return true; |
| 1700 | } |
| 1701 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1702 | // If Then makes up to four following instructions (the IT block) conditional. |
| 1703 | bool |
| 1704 | EmulateInstructionARM::EmulateIT (ARMEncoding encoding) |
| 1705 | { |
| 1706 | #if 0 |
| 1707 | // ARM pseudo code... |
| 1708 | EncodingSpecificOperations(); |
| 1709 | ITSTATE.IT<7:0> = firstcond:mask; |
| 1710 | #endif |
| 1711 | |
| 1712 | bool success = false; |
| 1713 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1714 | if (!success) |
| 1715 | return false; |
| 1716 | |
| 1717 | m_it_session.InitIT(Bits32(opcode, 7, 0)); |
| 1718 | return true; |
| 1719 | } |
| 1720 | |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 1721 | // Branch causes a branch to a target address. |
| 1722 | bool |
| 1723 | EmulateInstructionARM::EmulateB (ARMEncoding encoding) |
| 1724 | { |
| 1725 | #if 0 |
| 1726 | // ARM pseudo code... |
| 1727 | if (ConditionPassed()) |
| 1728 | { |
| 1729 | EncodingSpecificOperations(); |
| 1730 | BranchWritePC(PC + imm32); |
| 1731 | } |
| 1732 | #endif |
| 1733 | |
| 1734 | bool success = false; |
| 1735 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1736 | if (!success) |
| 1737 | return false; |
| 1738 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1739 | if (ConditionPassed()) |
| 1740 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1741 | EmulateInstruction::Context context; |
| 1742 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1743 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1744 | if (!success) |
| 1745 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1746 | addr_t target; // target address |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1747 | int32_t imm32; // PC-relative offset |
| 1748 | switch (encoding) { |
| 1749 | case eEncodingT1: |
| 1750 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 1751 | imm32 = llvm::SignExtend32<9>(Bits32(opcode, 7, 0) << 1); |
| 1752 | target = pc + 4 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1753 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1754 | break; |
| 1755 | case eEncodingT2: |
| 1756 | imm32 = llvm::SignExtend32<12>(Bits32(opcode, 10, 0)); |
| 1757 | target = pc + 4 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1758 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1759 | break; |
| 1760 | case eEncodingT3: |
| 1761 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 1762 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1763 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1764 | uint32_t imm6 = Bits32(opcode, 21, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1765 | uint32_t J1 = Bit32(opcode, 13); |
| 1766 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1767 | uint32_t imm11 = Bits32(opcode, 10, 0); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1768 | uint32_t imm21 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1769 | imm32 = llvm::SignExtend32<21>(imm21); |
| 1770 | target = pc + 4 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1771 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1772 | break; |
| 1773 | } |
| 1774 | case eEncodingT4: |
| 1775 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1776 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1777 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1778 | uint32_t J1 = Bit32(opcode, 13); |
| 1779 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1780 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 1781 | uint32_t I1 = !(J1 ^ S); |
| 1782 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1783 | uint32_t imm25 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1784 | imm32 = llvm::SignExtend32<25>(imm25); |
| 1785 | target = pc + 4 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1786 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1787 | break; |
| 1788 | } |
| 1789 | case eEncodingA1: |
| 1790 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
| 1791 | target = pc + 8 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1792 | context.SetModeAndImmediateSigned (eModeARM, 8 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1793 | break; |
| 1794 | default: |
| 1795 | return false; |
| 1796 | } |
| 1797 | if (!BranchWritePC(context, target)) |
| 1798 | return false; |
| 1799 | } |
| 1800 | return true; |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1803 | // Compare and Branch on Nonzero and Compare and Branch on Zero compare the value in a register with |
| 1804 | // zero and conditionally branch forward a constant value. They do not affect the condition flags. |
| 1805 | // CBNZ, CBZ |
| 1806 | bool |
| 1807 | EmulateInstructionARM::EmulateCB (ARMEncoding encoding) |
| 1808 | { |
| 1809 | #if 0 |
| 1810 | // ARM pseudo code... |
| 1811 | EncodingSpecificOperations(); |
| 1812 | if nonzero ^ IsZero(R[n]) then |
| 1813 | BranchWritePC(PC + imm32); |
| 1814 | #endif |
| 1815 | |
| 1816 | bool success = false; |
| 1817 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1818 | if (!success) |
| 1819 | return false; |
| 1820 | |
| 1821 | // Read the register value from the operand register Rn. |
| 1822 | uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Bits32(opcode, 2, 0), 0, &success); |
| 1823 | if (!success) |
| 1824 | return false; |
| 1825 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1826 | EmulateInstruction::Context context; |
| 1827 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1828 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1829 | if (!success) |
| 1830 | return false; |
| 1831 | |
| 1832 | addr_t target; // target address |
| 1833 | uint32_t imm32; // PC-relative offset to branch forward |
| 1834 | bool nonzero; |
| 1835 | switch (encoding) { |
| 1836 | case eEncodingT1: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1837 | imm32 = Bit32(opcode, 9) << 6 | Bits32(opcode, 7, 3) << 1; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1838 | nonzero = BitIsSet(opcode, 11); |
| 1839 | target = pc + 4 + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1840 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | default: |
| 1843 | return false; |
| 1844 | } |
| 1845 | if (nonzero ^ (reg_val == 0)) |
| 1846 | if (!BranchWritePC(context, target)) |
| 1847 | return false; |
| 1848 | |
| 1849 | return true; |
| 1850 | } |
| 1851 | |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 1852 | // Table Branch Byte causes a PC-relative forward branch using a table of single byte offsets. |
| 1853 | // A base register provides a pointer to the table, and a second register supplies an index into the table. |
| 1854 | // The branch length is twice the value of the byte returned from the table. |
| 1855 | // |
| 1856 | // Table Branch Halfword causes a PC-relative forward branch using a table of single halfword offsets. |
| 1857 | // A base register provides a pointer to the table, and a second register supplies an index into the table. |
| 1858 | // The branch length is twice the value of the halfword returned from the table. |
| 1859 | // TBB, TBH |
| 1860 | bool |
| 1861 | EmulateInstructionARM::EmulateTB (ARMEncoding encoding) |
| 1862 | { |
| 1863 | #if 0 |
| 1864 | // ARM pseudo code... |
| 1865 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 1866 | if is_tbh then |
| 1867 | halfwords = UInt(MemU[R[n]+LSL(R[m],1), 2]); |
| 1868 | else |
| 1869 | halfwords = UInt(MemU[R[n]+R[m], 1]); |
| 1870 | BranchWritePC(PC + 2*halfwords); |
| 1871 | #endif |
| 1872 | |
| 1873 | bool success = false; |
| 1874 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1875 | if (!success) |
| 1876 | return false; |
| 1877 | |
| 1878 | uint32_t Rn; // the base register which contains the address of the table of branch lengths |
| 1879 | uint32_t Rm; // the index register which contains an integer pointing to a byte/halfword in the table |
| 1880 | bool is_tbh; // true if table branch halfword |
| 1881 | switch (encoding) { |
| 1882 | case eEncodingT1: |
| 1883 | Rn = Bits32(opcode, 19, 16); |
| 1884 | Rm = Bits32(opcode, 3, 0); |
| 1885 | is_tbh = BitIsSet(opcode, 4); |
| 1886 | if (Rn == 13 || BadReg(Rm)) |
| 1887 | return false; |
| 1888 | if (InITBlock() && !LastInITBlock()) |
| 1889 | return false; |
| 1890 | break; |
| 1891 | default: |
| 1892 | return false; |
| 1893 | } |
| 1894 | |
| 1895 | // Read the address of the table from the operand register Rn. |
| 1896 | // The PC can be used, in which case the table immediately follows this instruction. |
| 1897 | uint32_t base = |
| 1898 | Rn == 15 ? (ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success) + 4) |
| 1899 | : ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 1900 | if (!success) |
| 1901 | return false; |
| 1902 | |
| 1903 | // the table index |
| 1904 | uint32_t index = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 1905 | if (!success) |
| 1906 | return false; |
| 1907 | |
| 1908 | // the offsetted table address |
| 1909 | addr_t addr = base + (is_tbh ? index*2 : index); |
| 1910 | |
| 1911 | // PC-relative offset to branch forward |
| 1912 | EmulateInstruction::Context context; |
| 1913 | context.type = EmulateInstruction::eContextTableBranchReadMemory; |
Johnny Chen | 104c8b6 | 2011-02-17 23:27:44 +0000 | [diff] [blame] | 1914 | uint32_t offset = MemURead(context, addr, is_tbh ? 2 : 1, 0, &success) * 2; |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 1915 | if (!success) |
| 1916 | return false; |
| 1917 | |
| 1918 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 1919 | if (!success) |
| 1920 | return false; |
| 1921 | |
| 1922 | // target address |
| 1923 | addr_t target = pc + 4 + offset; |
| 1924 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
| 1925 | context.SetModeAndImmediateSigned (eModeThumb, 4 + offset); |
| 1926 | |
| 1927 | if (!BranchWritePC(context, target)) |
| 1928 | return false; |
| 1929 | |
| 1930 | return true; |
| 1931 | } |
| 1932 | |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 1933 | // This instruction adds an immediate value to a register value, and writes the result to the destination |
| 1934 | // register. It can optionally update the condition flags based on the result. |
| 1935 | bool |
| 1936 | EmulateInstructionARM::EmulateADDImmARM (ARMEncoding encoding) |
| 1937 | { |
| 1938 | #if 0 |
| 1939 | // ARM pseudo code... |
| 1940 | if ConditionPassed() then |
| 1941 | EncodingSpecificOperations(); |
| 1942 | (result, carry, overflow) = AddWithCarry(R[n], imm32, '0'); |
| 1943 | if d == 15 then |
| 1944 | ALUWritePC(result); // setflags is always FALSE here |
| 1945 | else |
| 1946 | R[d] = result; |
| 1947 | if setflags then |
| 1948 | APSR.N = result<31>; |
| 1949 | APSR.Z = IsZeroBit(result); |
| 1950 | APSR.C = carry; |
| 1951 | APSR.V = overflow; |
| 1952 | #endif |
| 1953 | |
| 1954 | bool success = false; |
| 1955 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1956 | if (!success) |
| 1957 | return false; |
| 1958 | |
| 1959 | if (ConditionPassed()) |
| 1960 | { |
| 1961 | uint32_t Rd, Rn; |
| 1962 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 1963 | bool setflags; |
| 1964 | switch (encoding) |
| 1965 | { |
| 1966 | case eEncodingA1: |
| 1967 | Rd = Bits32(opcode, 15, 12); |
| 1968 | Rn = Bits32(opcode, 19, 16); |
| 1969 | setflags = BitIsSet(opcode, 20); |
| 1970 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1971 | break; |
| 1972 | default: |
| 1973 | return false; |
| 1974 | } |
| 1975 | |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 1976 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 1977 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 1978 | if (!success) |
| 1979 | return false; |
| 1980 | |
| 1981 | AddWithCarryResult res = AddWithCarry(val1, imm32, 0); |
| 1982 | |
| 1983 | EmulateInstruction::Context context; |
| 1984 | context.type = EmulateInstruction::eContextImmediate; |
| 1985 | context.SetNoArgs (); |
| 1986 | |
| 1987 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 1988 | return false; |
| 1989 | } |
| 1990 | return true; |
| 1991 | } |
| 1992 | |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 1993 | // This instruction adds a register value and an optionally-shifted register value, and writes the result |
| 1994 | // to the destination register. It can optionally update the condition flags based on the result. |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 1995 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 1996 | EmulateInstructionARM::EmulateADDReg (ARMEncoding encoding) |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 1997 | { |
| 1998 | #if 0 |
| 1999 | // ARM pseudo code... |
| 2000 | if ConditionPassed() then |
| 2001 | EncodingSpecificOperations(); |
| 2002 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2003 | (result, carry, overflow) = AddWithCarry(R[n], shifted, '0'); |
| 2004 | if d == 15 then |
| 2005 | ALUWritePC(result); // setflags is always FALSE here |
| 2006 | else |
| 2007 | R[d] = result; |
| 2008 | if setflags then |
| 2009 | APSR.N = result<31>; |
| 2010 | APSR.Z = IsZeroBit(result); |
| 2011 | APSR.C = carry; |
| 2012 | APSR.V = overflow; |
| 2013 | #endif |
| 2014 | |
| 2015 | bool success = false; |
| 2016 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2017 | if (!success) |
| 2018 | return false; |
| 2019 | |
| 2020 | if (ConditionPassed()) |
| 2021 | { |
| 2022 | uint32_t Rd, Rn, Rm; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2023 | ARM_ShifterType shift_t; |
| 2024 | uint32_t shift_n; // the shift applied to the value read from Rm |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2025 | bool setflags; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2026 | switch (encoding) |
| 2027 | { |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2028 | case eEncodingT1: |
| 2029 | Rd = Bits32(opcode, 2, 0); |
| 2030 | Rn = Bits32(opcode, 5, 3); |
| 2031 | Rm = Bits32(opcode, 8, 6); |
| 2032 | setflags = !InITBlock(); |
| 2033 | shift_t = SRType_LSL; |
| 2034 | shift_n = 0; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2035 | case eEncodingT2: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2036 | Rd = Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2037 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2038 | setflags = false; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2039 | shift_t = SRType_LSL; |
| 2040 | shift_n = 0; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2041 | if (Rn == 15 && Rm == 15) |
| 2042 | return false; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2043 | if (Rd == 15 && InITBlock() && !LastInITBlock()) |
| 2044 | return false; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2045 | break; |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2046 | case eEncodingA1: |
| 2047 | Rd = Bits32(opcode, 15, 12); |
| 2048 | Rn = Bits32(opcode, 19, 16); |
| 2049 | Rm = Bits32(opcode, 3, 0); |
| 2050 | setflags = BitIsSet(opcode, 20); |
| 2051 | shift_n = DecodeImmShift(Bits32(opcode, 6, 5), Bits32(opcode, 11, 7), shift_t); |
| 2052 | break; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2053 | default: |
| 2054 | return false; |
| 2055 | } |
| 2056 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2057 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2058 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2059 | if (!success) |
| 2060 | return false; |
| 2061 | |
| 2062 | // Read the second operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2063 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2064 | if (!success) |
| 2065 | return false; |
| 2066 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2067 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2068 | AddWithCarryResult res = AddWithCarry(val1, shifted, 0); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2069 | |
| 2070 | EmulateInstruction::Context context; |
| 2071 | context.type = EmulateInstruction::eContextImmediate; |
| 2072 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2073 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2074 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2075 | return false; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2076 | } |
| 2077 | return true; |
| 2078 | } |
| 2079 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2080 | // CMP (immediate) |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2081 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 2082 | EmulateInstructionARM::EmulateCMPRnImm (ARMEncoding encoding) |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2083 | { |
| 2084 | #if 0 |
| 2085 | // ARM pseudo code... |
| 2086 | if ConditionPassed() then |
| 2087 | EncodingSpecificOperations(); |
| 2088 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1'); |
| 2089 | APSR.N = result<31>; |
| 2090 | APSR.Z = IsZeroBit(result); |
| 2091 | APSR.C = carry; |
| 2092 | APSR.V = overflow; |
| 2093 | #endif |
| 2094 | |
| 2095 | bool success = false; |
| 2096 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2097 | if (!success) |
| 2098 | return false; |
| 2099 | |
| 2100 | uint32_t Rn; // the first operand |
| 2101 | uint32_t imm32; // the immediate value to be compared with |
| 2102 | switch (encoding) { |
| 2103 | case eEncodingT1: |
| 2104 | Rn = Bits32(opcode, 10, 8); |
| 2105 | imm32 = Bits32(opcode, 7, 0); |
| 2106 | break; |
| 2107 | default: |
| 2108 | return false; |
| 2109 | } |
| 2110 | // Read the register value from the operand register Rn. |
| 2111 | uint32_t reg_val = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 2112 | if (!success) |
| 2113 | return false; |
| 2114 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2115 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1); |
| 2116 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2117 | EmulateInstruction::Context context; |
| 2118 | context.type = EmulateInstruction::eContextImmediate; |
| 2119 | context.SetNoArgs (); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2120 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2121 | return false; |
| 2122 | |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2123 | return true; |
| 2124 | } |
| 2125 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2126 | // CMP (register) |
| 2127 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 2128 | EmulateInstructionARM::EmulateCMPRnRm (ARMEncoding encoding) |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2129 | { |
| 2130 | #if 0 |
| 2131 | // ARM pseudo code... |
| 2132 | if ConditionPassed() then |
| 2133 | EncodingSpecificOperations(); |
| 2134 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2135 | (result, carry, overflow) = AddWithCarry(R[n], NOT(shifted), '1'); |
| 2136 | APSR.N = result<31>; |
| 2137 | APSR.Z = IsZeroBit(result); |
| 2138 | APSR.C = carry; |
| 2139 | APSR.V = overflow; |
| 2140 | #endif |
| 2141 | |
| 2142 | bool success = false; |
| 2143 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2144 | if (!success) |
| 2145 | return false; |
| 2146 | |
| 2147 | uint32_t Rn; // the first operand |
| 2148 | uint32_t Rm; // the second operand |
| 2149 | switch (encoding) { |
| 2150 | case eEncodingT1: |
| 2151 | Rn = Bits32(opcode, 2, 0); |
| 2152 | Rm = Bits32(opcode, 5, 3); |
| 2153 | break; |
| 2154 | case eEncodingT2: |
| 2155 | Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
| 2156 | Rm = Bits32(opcode, 6, 3); |
| 2157 | if (Rn < 8 && Rm < 8) |
| 2158 | return false; |
| 2159 | if (Rn == 15 || Rm == 15) |
| 2160 | return false; |
| 2161 | break; |
| 2162 | default: |
| 2163 | return false; |
| 2164 | } |
| 2165 | // Read the register value from register Rn. |
| 2166 | uint32_t reg_val1 = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 2167 | if (!success) |
| 2168 | return false; |
| 2169 | // Read the register value from register Rm. |
| 2170 | // The register value is not being shifted since we don't handle ARM for now. |
| 2171 | uint32_t reg_val2 = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 2172 | if (!success) |
| 2173 | return false; |
| 2174 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2175 | AddWithCarryResult res = AddWithCarry(reg_val1, ~reg_val2, 1); |
| 2176 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2177 | EmulateInstruction::Context context; |
| 2178 | context.type = EmulateInstruction::eContextImmediate; |
| 2179 | context.SetNoArgs(); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2180 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2181 | return false; |
| 2182 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2183 | return true; |
| 2184 | } |
| 2185 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2186 | // Arithmetic Shift Right (immediate) shifts a register value right by an immediate number of bits, |
| 2187 | // shifting in copies of its sign bit, and writes the result to the destination register. It can |
| 2188 | // optionally update the condition flags based on the result. |
| 2189 | bool |
| 2190 | EmulateInstructionARM::EmulateASRImm (ARMEncoding encoding) |
| 2191 | { |
| 2192 | #if 0 |
| 2193 | // ARM pseudo code... |
| 2194 | if ConditionPassed() then |
| 2195 | EncodingSpecificOperations(); |
| 2196 | (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C); |
| 2197 | if d == 15 then // Can only occur for ARM encoding |
| 2198 | ALUWritePC(result); // setflags is always FALSE here |
| 2199 | else |
| 2200 | R[d] = result; |
| 2201 | if setflags then |
| 2202 | APSR.N = result<31>; |
| 2203 | APSR.Z = IsZeroBit(result); |
| 2204 | APSR.C = carry; |
| 2205 | // APSR.V unchanged |
| 2206 | #endif |
| 2207 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2208 | return EmulateShiftImm(encoding, SRType_ASR); |
| 2209 | } |
| 2210 | |
| 2211 | // Arithmetic Shift Right (register) shifts a register value right by a variable number of bits, |
| 2212 | // shifting in copies of its sign bit, and writes the result to the destination register. |
| 2213 | // The variable number of bits is read from the bottom byte of a register. It can optionally update |
| 2214 | // the condition flags based on the result. |
| 2215 | bool |
| 2216 | EmulateInstructionARM::EmulateASRReg (ARMEncoding encoding) |
| 2217 | { |
| 2218 | #if 0 |
| 2219 | // ARM pseudo code... |
| 2220 | if ConditionPassed() then |
| 2221 | EncodingSpecificOperations(); |
| 2222 | shift_n = UInt(R[m]<7:0>); |
| 2223 | (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C); |
| 2224 | R[d] = result; |
| 2225 | if setflags then |
| 2226 | APSR.N = result<31>; |
| 2227 | APSR.Z = IsZeroBit(result); |
| 2228 | APSR.C = carry; |
| 2229 | // APSR.V unchanged |
| 2230 | #endif |
| 2231 | |
| 2232 | return EmulateShiftReg(encoding, SRType_ASR); |
| 2233 | } |
| 2234 | |
| 2235 | // Logical Shift Left (immediate) shifts a register value left by an immediate number of bits, |
| 2236 | // shifting in zeros, and writes the result to the destination register. It can optionally |
| 2237 | // update the condition flags based on the result. |
| 2238 | bool |
| 2239 | EmulateInstructionARM::EmulateLSLImm (ARMEncoding encoding) |
| 2240 | { |
| 2241 | #if 0 |
| 2242 | // ARM pseudo code... |
| 2243 | if ConditionPassed() then |
| 2244 | EncodingSpecificOperations(); |
| 2245 | (result, carry) = Shift_C(R[m], SRType_LSL, shift_n, APSR.C); |
| 2246 | if d == 15 then // Can only occur for ARM encoding |
| 2247 | ALUWritePC(result); // setflags is always FALSE here |
| 2248 | else |
| 2249 | R[d] = result; |
| 2250 | if setflags then |
| 2251 | APSR.N = result<31>; |
| 2252 | APSR.Z = IsZeroBit(result); |
| 2253 | APSR.C = carry; |
| 2254 | // APSR.V unchanged |
| 2255 | #endif |
| 2256 | |
| 2257 | return EmulateShiftImm(encoding, SRType_LSL); |
| 2258 | } |
| 2259 | |
| 2260 | // Logical Shift Left (register) shifts a register value left by a variable number of bits, |
| 2261 | // shifting in zeros, and writes the result to the destination register. The variable number |
| 2262 | // of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2263 | // flags based on the result. |
| 2264 | bool |
| 2265 | EmulateInstructionARM::EmulateLSLReg (ARMEncoding encoding) |
| 2266 | { |
| 2267 | #if 0 |
| 2268 | // ARM pseudo code... |
| 2269 | if ConditionPassed() then |
| 2270 | EncodingSpecificOperations(); |
| 2271 | shift_n = UInt(R[m]<7:0>); |
| 2272 | (result, carry) = Shift_C(R[m], SRType_LSL, shift_n, APSR.C); |
| 2273 | R[d] = result; |
| 2274 | if setflags then |
| 2275 | APSR.N = result<31>; |
| 2276 | APSR.Z = IsZeroBit(result); |
| 2277 | APSR.C = carry; |
| 2278 | // APSR.V unchanged |
| 2279 | #endif |
| 2280 | |
| 2281 | return EmulateShiftReg(encoding, SRType_LSL); |
| 2282 | } |
| 2283 | |
| 2284 | // Logical Shift Right (immediate) shifts a register value right by an immediate number of bits, |
| 2285 | // shifting in zeros, and writes the result to the destination register. It can optionally |
| 2286 | // update the condition flags based on the result. |
| 2287 | bool |
| 2288 | EmulateInstructionARM::EmulateLSRImm (ARMEncoding encoding) |
| 2289 | { |
| 2290 | #if 0 |
| 2291 | // ARM pseudo code... |
| 2292 | if ConditionPassed() then |
| 2293 | EncodingSpecificOperations(); |
| 2294 | (result, carry) = Shift_C(R[m], SRType_LSR, shift_n, APSR.C); |
| 2295 | if d == 15 then // Can only occur for ARM encoding |
| 2296 | ALUWritePC(result); // setflags is always FALSE here |
| 2297 | else |
| 2298 | R[d] = result; |
| 2299 | if setflags then |
| 2300 | APSR.N = result<31>; |
| 2301 | APSR.Z = IsZeroBit(result); |
| 2302 | APSR.C = carry; |
| 2303 | // APSR.V unchanged |
| 2304 | #endif |
| 2305 | |
| 2306 | return EmulateShiftImm(encoding, SRType_LSR); |
| 2307 | } |
| 2308 | |
| 2309 | // Logical Shift Right (register) shifts a register value right by a variable number of bits, |
| 2310 | // shifting in zeros, and writes the result to the destination register. The variable number |
| 2311 | // of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2312 | // flags based on the result. |
| 2313 | bool |
| 2314 | EmulateInstructionARM::EmulateLSRReg (ARMEncoding encoding) |
| 2315 | { |
| 2316 | #if 0 |
| 2317 | // ARM pseudo code... |
| 2318 | if ConditionPassed() then |
| 2319 | EncodingSpecificOperations(); |
| 2320 | shift_n = UInt(R[m]<7:0>); |
| 2321 | (result, carry) = Shift_C(R[m], SRType_LSR, shift_n, APSR.C); |
| 2322 | R[d] = result; |
| 2323 | if setflags then |
| 2324 | APSR.N = result<31>; |
| 2325 | APSR.Z = IsZeroBit(result); |
| 2326 | APSR.C = carry; |
| 2327 | // APSR.V unchanged |
| 2328 | #endif |
| 2329 | |
| 2330 | return EmulateShiftReg(encoding, SRType_LSR); |
| 2331 | } |
| 2332 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2333 | // Rotate Right (immediate) provides the value of the contents of a register rotated by a constant value. |
| 2334 | // The bits that are rotated off the right end are inserted into the vacated bit positions on the left. |
| 2335 | // It can optionally update the condition flags based on the result. |
| 2336 | bool |
| 2337 | EmulateInstructionARM::EmulateRORImm (ARMEncoding encoding) |
| 2338 | { |
| 2339 | #if 0 |
| 2340 | // ARM pseudo code... |
| 2341 | if ConditionPassed() then |
| 2342 | EncodingSpecificOperations(); |
| 2343 | (result, carry) = Shift_C(R[m], SRType_ROR, shift_n, APSR.C); |
| 2344 | if d == 15 then // Can only occur for ARM encoding |
| 2345 | ALUWritePC(result); // setflags is always FALSE here |
| 2346 | else |
| 2347 | R[d] = result; |
| 2348 | if setflags then |
| 2349 | APSR.N = result<31>; |
| 2350 | APSR.Z = IsZeroBit(result); |
| 2351 | APSR.C = carry; |
| 2352 | // APSR.V unchanged |
| 2353 | #endif |
| 2354 | |
| 2355 | return EmulateShiftImm(encoding, SRType_ROR); |
| 2356 | } |
| 2357 | |
| 2358 | // Rotate Right (register) provides the value of the contents of a register rotated by a variable number of bits. |
| 2359 | // The bits that are rotated off the right end are inserted into the vacated bit positions on the left. |
| 2360 | // The variable number of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2361 | // flags based on the result. |
| 2362 | bool |
| 2363 | EmulateInstructionARM::EmulateRORReg (ARMEncoding encoding) |
| 2364 | { |
| 2365 | #if 0 |
| 2366 | // ARM pseudo code... |
| 2367 | if ConditionPassed() then |
| 2368 | EncodingSpecificOperations(); |
| 2369 | shift_n = UInt(R[m]<7:0>); |
| 2370 | (result, carry) = Shift_C(R[m], SRType_ROR, shift_n, APSR.C); |
| 2371 | R[d] = result; |
| 2372 | if setflags then |
| 2373 | APSR.N = result<31>; |
| 2374 | APSR.Z = IsZeroBit(result); |
| 2375 | APSR.C = carry; |
| 2376 | // APSR.V unchanged |
| 2377 | #endif |
| 2378 | |
| 2379 | return EmulateShiftReg(encoding, SRType_ROR); |
| 2380 | } |
| 2381 | |
| 2382 | // Rotate Right with Extend provides the value of the contents of a register shifted right by one place, |
| 2383 | // with the carry flag shifted into bit [31]. |
| 2384 | // |
| 2385 | // RRX can optionally update the condition flags based on the result. |
| 2386 | // In that case, bit [0] is shifted into the carry flag. |
| 2387 | bool |
| 2388 | EmulateInstructionARM::EmulateRRX (ARMEncoding encoding) |
| 2389 | { |
| 2390 | #if 0 |
| 2391 | // ARM pseudo code... |
| 2392 | if ConditionPassed() then |
| 2393 | EncodingSpecificOperations(); |
| 2394 | (result, carry) = Shift_C(R[m], SRType_RRX, 1, APSR.C); |
| 2395 | if d == 15 then // Can only occur for ARM encoding |
| 2396 | ALUWritePC(result); // setflags is always FALSE here |
| 2397 | else |
| 2398 | R[d] = result; |
| 2399 | if setflags then |
| 2400 | APSR.N = result<31>; |
| 2401 | APSR.Z = IsZeroBit(result); |
| 2402 | APSR.C = carry; |
| 2403 | // APSR.V unchanged |
| 2404 | #endif |
| 2405 | |
| 2406 | return EmulateShiftImm(encoding, SRType_RRX); |
| 2407 | } |
| 2408 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2409 | bool |
| 2410 | EmulateInstructionARM::EmulateShiftImm (ARMEncoding encoding, ARM_ShifterType shift_type) |
| 2411 | { |
| 2412 | assert(shift_type == SRType_ASR || shift_type == SRType_LSL || shift_type == SRType_LSR); |
| 2413 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2414 | bool success = false; |
| 2415 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2416 | if (!success) |
| 2417 | return false; |
| 2418 | |
| 2419 | if (ConditionPassed()) |
| 2420 | { |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2421 | uint32_t Rd; // the destination register |
| 2422 | uint32_t Rm; // the first operand register |
| 2423 | uint32_t imm5; // encoding for the shift amount |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2424 | uint32_t carry; // the carry bit after the shift operation |
| 2425 | bool setflags; |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Special case handling! |
| 2428 | // A8.6.139 ROR (immediate) -- Encoding T1 |
| 2429 | if (shift_type == SRType_ROR && encoding == eEncodingT1) |
| 2430 | { |
| 2431 | // Morph the T1 encoding from the ARM Architecture Manual into T2 encoding to |
| 2432 | // have the same decoding of bit fields as the other Thumb2 shift operations. |
| 2433 | encoding = eEncodingT2; |
| 2434 | } |
| 2435 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2436 | switch (encoding) { |
| 2437 | case eEncodingT1: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2438 | // Due to the above special case handling! |
| 2439 | assert(shift_type != SRType_ROR); |
| 2440 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2441 | Rd = Bits32(opcode, 2, 0); |
| 2442 | Rm = Bits32(opcode, 5, 3); |
| 2443 | setflags = !InITBlock(); |
| 2444 | imm5 = Bits32(opcode, 10, 6); |
| 2445 | break; |
| 2446 | case eEncodingT2: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2447 | // A8.6.141 RRX |
| 2448 | assert(shift_type != SRType_RRX); |
| 2449 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2450 | Rd = Bits32(opcode, 11, 8); |
| 2451 | Rm = Bits32(opcode, 3, 0); |
| 2452 | setflags = BitIsSet(opcode, 20); |
| 2453 | imm5 = Bits32(opcode, 14, 12) << 2 | Bits32(opcode, 7, 6); |
| 2454 | if (BadReg(Rd) || BadReg(Rm)) |
| 2455 | return false; |
| 2456 | break; |
| 2457 | case eEncodingA1: |
| 2458 | Rd = Bits32(opcode, 15, 12); |
| 2459 | Rm = Bits32(opcode, 3, 0); |
| 2460 | setflags = BitIsSet(opcode, 20); |
| 2461 | imm5 = Bits32(opcode, 11, 7); |
| 2462 | break; |
| 2463 | default: |
| 2464 | return false; |
| 2465 | } |
| 2466 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2467 | // A8.6.139 ROR (immediate) |
| 2468 | if (shift_type == SRType_ROR && imm5 == 0) |
| 2469 | shift_type = SRType_RRX; |
| 2470 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2471 | // Get the first operand. |
| 2472 | uint32_t value = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 2473 | if (!success) |
| 2474 | return false; |
| 2475 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2476 | // Decode the shift amount if not RRX. |
| 2477 | uint32_t amt = (shift_type == SRType_RRX ? 1 : DecodeImmShift(shift_type, imm5)); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2478 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2479 | uint32_t result = Shift_C(value, shift_type, amt, APSR_C, carry); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2480 | |
| 2481 | // The context specifies that an immediate is to be moved into Rd. |
| 2482 | EmulateInstruction::Context context; |
| 2483 | context.type = EmulateInstruction::eContextImmediate; |
| 2484 | context.SetNoArgs (); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2485 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2486 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2487 | return false; |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2488 | } |
| 2489 | return true; |
| 2490 | } |
| 2491 | |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2492 | bool |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2493 | EmulateInstructionARM::EmulateShiftReg (ARMEncoding encoding, ARM_ShifterType shift_type) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2494 | { |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2495 | assert(shift_type == SRType_ASR || shift_type == SRType_LSL || shift_type == SRType_LSR); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2496 | |
| 2497 | bool success = false; |
| 2498 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2499 | if (!success) |
| 2500 | return false; |
| 2501 | |
| 2502 | if (ConditionPassed()) |
| 2503 | { |
| 2504 | uint32_t Rd; // the destination register |
| 2505 | uint32_t Rn; // the first operand register |
| 2506 | uint32_t Rm; // the register whose bottom byte contains the amount to shift by |
| 2507 | uint32_t carry; // the carry bit after the shift operation |
| 2508 | bool setflags; |
| 2509 | switch (encoding) { |
| 2510 | case eEncodingT1: |
| 2511 | Rd = Bits32(opcode, 2, 0); |
| 2512 | Rn = Rd; |
| 2513 | Rm = Bits32(opcode, 5, 3); |
| 2514 | setflags = !InITBlock(); |
| 2515 | break; |
| 2516 | case eEncodingT2: |
| 2517 | Rd = Bits32(opcode, 11, 8); |
| 2518 | Rn = Bits32(opcode, 19, 16); |
| 2519 | Rm = Bits32(opcode, 3, 0); |
| 2520 | setflags = BitIsSet(opcode, 20); |
| 2521 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 2522 | return false; |
| 2523 | break; |
| 2524 | case eEncodingA1: |
| 2525 | Rd = Bits32(opcode, 15, 12); |
| 2526 | Rn = Bits32(opcode, 3, 0); |
| 2527 | Rm = Bits32(opcode, 11, 8); |
| 2528 | setflags = BitIsSet(opcode, 20); |
| 2529 | if (Rd == 15 || Rn == 15 || Rm == 15) |
| 2530 | return false; |
| 2531 | break; |
| 2532 | default: |
| 2533 | return false; |
| 2534 | } |
| 2535 | |
| 2536 | // Get the first operand. |
| 2537 | uint32_t value = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 2538 | if (!success) |
| 2539 | return false; |
| 2540 | // Get the Rm register content. |
| 2541 | uint32_t val = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + Rm, 0, &success); |
| 2542 | if (!success) |
| 2543 | return false; |
| 2544 | |
| 2545 | // Get the shift amount. |
| 2546 | uint32_t amt = Bits32(val, 7, 0); |
| 2547 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2548 | uint32_t result = Shift_C(value, shift_type, amt, APSR_C, carry); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2549 | |
| 2550 | // The context specifies that an immediate is to be moved into Rd. |
| 2551 | EmulateInstruction::Context context; |
| 2552 | context.type = EmulateInstruction::eContextImmediate; |
| 2553 | context.SetNoArgs (); |
| 2554 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2555 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2556 | return false; |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2557 | } |
| 2558 | return true; |
| 2559 | } |
| 2560 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2561 | // LDM loads multiple registers from consecutive memory locations, using an |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2562 | // address from a base register. Optionally the address just above the highest of those locations |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2563 | // can be written back to the base register. |
| 2564 | bool |
| 2565 | EmulateInstructionARM::EmulateLDM (ARMEncoding encoding) |
| 2566 | { |
| 2567 | #if 0 |
| 2568 | // ARM pseudo code... |
| 2569 | if ConditionPassed() |
| 2570 | EncodingSpecificOperations(); NullCheckIfThumbEE (n); |
| 2571 | address = R[n]; |
| 2572 | |
| 2573 | for i = 0 to 14 |
| 2574 | if registers<i> == '1' then |
| 2575 | R[i] = MemA[address, 4]; address = address + 4; |
| 2576 | if registers<15> == '1' then |
| 2577 | LoadWritePC (MemA[address, 4]); |
| 2578 | |
| 2579 | if wback && registers<n> == '0' then R[n] = R[n] + 4 * BitCount (registers); |
| 2580 | if wback && registers<n> == '1' then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2581 | |
| 2582 | #endif |
| 2583 | |
| 2584 | bool success = false; |
| 2585 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2586 | if (!success) |
| 2587 | return false; |
| 2588 | |
| 2589 | if (ConditionPassed()) |
| 2590 | { |
| 2591 | uint32_t n; |
| 2592 | uint32_t registers = 0; |
| 2593 | bool wback; |
| 2594 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2595 | switch (encoding) |
| 2596 | { |
| 2597 | case eEncodingT1: |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2598 | // n = UInt(Rn); registers = ’00000000’:register_list; wback = (registers<n> == ’0’); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2599 | n = Bits32 (opcode, 10, 8); |
| 2600 | registers = Bits32 (opcode, 7, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2601 | registers = registers & 0x00ff; // Make sure the top 8 bits are zeros. |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2602 | wback = BitIsClear (registers, n); |
| 2603 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 2604 | if (BitCount(registers) < 1) |
| 2605 | return false; |
| 2606 | break; |
| 2607 | case eEncodingT2: |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2608 | // if W == ’1’ && Rn == ’1101’ then SEE POP; |
| 2609 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2610 | n = Bits32 (opcode, 19, 16); |
| 2611 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2612 | registers = registers & 0xdfff; // Make sure bit 13 is zero. |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2613 | wback = BitIsSet (opcode, 21); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2614 | |
| 2615 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2616 | if ((n == 15) |
| 2617 | || (BitCount (registers) < 2) |
| 2618 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 2619 | return false; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2620 | |
| 2621 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 2622 | if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock()) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2623 | return false; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2624 | |
| 2625 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2626 | if (wback |
| 2627 | && BitIsSet (registers, n)) |
| 2628 | return false; |
| 2629 | break; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2630 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2631 | case eEncodingA1: |
| 2632 | n = Bits32 (opcode, 19, 16); |
| 2633 | registers = Bits32 (opcode, 15, 0); |
| 2634 | wback = BitIsSet (opcode, 21); |
| 2635 | if ((n == 15) |
| 2636 | || (BitCount (registers) < 1)) |
| 2637 | return false; |
| 2638 | break; |
| 2639 | default: |
| 2640 | return false; |
| 2641 | } |
| 2642 | |
| 2643 | int32_t offset = 0; |
| 2644 | const addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2645 | if (!success) |
| 2646 | return false; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2647 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2648 | EmulateInstruction::Context context; |
| 2649 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 2650 | Register dwarf_reg; |
| 2651 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 2652 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2653 | |
| 2654 | for (int i = 0; i < 14; ++i) |
| 2655 | { |
| 2656 | if (BitIsSet (registers, i)) |
| 2657 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2658 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2659 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2660 | if (wback && (n == 13)) // Pop Instruction |
| 2661 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 2662 | |
| 2663 | // R[i] = MemA [address, 4]; address = address + 4; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2664 | uint32_t data = MemARead (context, base_address + offset, addr_byte_size, 0, &success); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2665 | if (!success) |
| 2666 | return false; |
| 2667 | |
| 2668 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 2669 | return false; |
| 2670 | |
| 2671 | offset += addr_byte_size; |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | if (BitIsSet (registers, 15)) |
| 2676 | { |
| 2677 | //LoadWritePC (MemA [address, 4]); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2678 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2679 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2680 | uint32_t data = MemARead (context, base_address + offset, addr_byte_size, 0, &success); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2681 | if (!success) |
| 2682 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 2683 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 2684 | if (!LoadWritePC(context, data)) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2685 | return false; |
| 2686 | } |
| 2687 | |
| 2688 | if (wback && BitIsClear (registers, n)) |
| 2689 | { |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 2690 | // R[n] = R[n] + 4 * BitCount (registers) |
| 2691 | int32_t offset = addr_byte_size * BitCount (registers); |
| 2692 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2693 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2694 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2695 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, base_address + offset)) |
| 2696 | return false; |
| 2697 | } |
| 2698 | if (wback && BitIsSet (registers, n)) |
| 2699 | // R[n] bits(32) UNKNOWN; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2700 | return WriteBits32Unknown (n); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2701 | } |
| 2702 | return true; |
| 2703 | } |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2704 | |
| 2705 | // LDMDA loads multiple registers from consecutive memory locations using an address from a base registers. |
| 2706 | // The consecutive memorty locations end at this address and the address just below the lowest of those locations |
| 2707 | // can optionally be written back tot he base registers. |
| 2708 | bool |
| 2709 | EmulateInstructionARM::EmulateLDMDA (ARMEncoding encoding) |
| 2710 | { |
| 2711 | #if 0 |
| 2712 | // ARM pseudo code... |
| 2713 | if ConditionPassed() then |
| 2714 | EncodingSpecificOperations(); |
| 2715 | address = R[n] - 4*BitCount(registers) + 4; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2716 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2717 | for i = 0 to 14 |
| 2718 | if registers<i> == ’1’ then |
| 2719 | R[i] = MemA[address,4]; address = address + 4; |
| 2720 | |
| 2721 | if registers<15> == ’1’ then |
| 2722 | LoadWritePC(MemA[address,4]); |
| 2723 | |
| 2724 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2725 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 2726 | #endif |
| 2727 | |
| 2728 | bool success = false; |
| 2729 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2730 | if (!success) |
| 2731 | return false; |
| 2732 | |
| 2733 | if (ConditionPassed()) |
| 2734 | { |
| 2735 | uint32_t n; |
| 2736 | uint32_t registers = 0; |
| 2737 | bool wback; |
| 2738 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2739 | |
| 2740 | // EncodingSpecificOperations(); |
| 2741 | switch (encoding) |
| 2742 | { |
| 2743 | case eEncodingA1: |
| 2744 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 2745 | n = Bits32 (opcode, 19, 16); |
| 2746 | registers = Bits32 (opcode, 15, 0); |
| 2747 | wback = BitIsSet (opcode, 21); |
| 2748 | |
| 2749 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 2750 | if ((n == 15) || (BitCount (registers) < 1)) |
| 2751 | return false; |
| 2752 | |
| 2753 | break; |
| 2754 | |
| 2755 | default: |
| 2756 | return false; |
| 2757 | } |
| 2758 | // address = R[n] - 4*BitCount(registers) + 4; |
| 2759 | |
| 2760 | int32_t offset = 0; |
| 2761 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2762 | |
| 2763 | if (!success) |
| 2764 | return false; |
| 2765 | |
| 2766 | address = address - (addr_byte_size * BitCount (registers)) + addr_byte_size; |
| 2767 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2768 | EmulateInstruction::Context context; |
| 2769 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 2770 | Register dwarf_reg; |
| 2771 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 2772 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2773 | |
| 2774 | // for i = 0 to 14 |
| 2775 | for (int i = 0; i < 14; ++i) |
| 2776 | { |
| 2777 | // if registers<i> == ’1’ then |
| 2778 | if (BitIsSet (registers, i)) |
| 2779 | { |
| 2780 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2781 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2782 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2783 | if (!success) |
| 2784 | return false; |
| 2785 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 2786 | return false; |
| 2787 | offset += addr_byte_size; |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | // if registers<15> == ’1’ then |
| 2792 | // LoadWritePC(MemA[address,4]); |
| 2793 | if (BitIsSet (registers, 15)) |
| 2794 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2795 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2796 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2797 | if (!success) |
| 2798 | return false; |
Johnny Chen | 44c10f0 | 2011-02-11 19:37:03 +0000 | [diff] [blame] | 2799 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 2800 | if (!LoadWritePC(context, data)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2801 | return false; |
| 2802 | } |
| 2803 | |
| 2804 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2805 | if (wback && BitIsClear (registers, n)) |
| 2806 | { |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2807 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2808 | if (!success) |
| 2809 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 2810 | |
| 2811 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 2812 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2813 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 2814 | addr = addr + offset; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2815 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 2816 | return false; |
| 2817 | } |
| 2818 | |
| 2819 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 2820 | if (wback && BitIsSet (registers, n)) |
| 2821 | return WriteBits32Unknown (n); |
| 2822 | } |
| 2823 | return true; |
| 2824 | } |
| 2825 | |
| 2826 | // LDMDB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 2827 | // consecutive memory lcoations end just below this address, and the address of the lowest of those locations can |
| 2828 | // be optionally written back to the base register. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2829 | bool |
| 2830 | EmulateInstructionARM::EmulateLDMDB (ARMEncoding encoding) |
| 2831 | { |
| 2832 | #if 0 |
| 2833 | // ARM pseudo code... |
| 2834 | if ConditionPassed() then |
| 2835 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 2836 | address = R[n] - 4*BitCount(registers); |
| 2837 | |
| 2838 | for i = 0 to 14 |
| 2839 | if registers<i> == ’1’ then |
| 2840 | R[i] = MemA[address,4]; address = address + 4; |
| 2841 | if registers<15> == ’1’ then |
| 2842 | LoadWritePC(MemA[address,4]); |
| 2843 | |
| 2844 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2845 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2846 | #endif |
| 2847 | |
| 2848 | bool success = false; |
| 2849 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2850 | if (!success) |
| 2851 | return false; |
| 2852 | |
| 2853 | if (ConditionPassed()) |
| 2854 | { |
| 2855 | uint32_t n; |
| 2856 | uint32_t registers = 0; |
| 2857 | bool wback; |
| 2858 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2859 | switch (encoding) |
| 2860 | { |
| 2861 | case eEncodingT1: |
| 2862 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
| 2863 | n = Bits32 (opcode, 19, 16); |
| 2864 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2865 | registers = registers & 0xdfff; // Make sure bit 13 is a zero. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2866 | wback = BitIsSet (opcode, 21); |
| 2867 | |
| 2868 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
| 2869 | if ((n == 15) |
| 2870 | || (BitCount (registers) < 2) |
| 2871 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 2872 | return false; |
| 2873 | |
| 2874 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 2875 | if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock()) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2876 | return false; |
| 2877 | |
| 2878 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 2879 | if (wback && BitIsSet (registers, n)) |
| 2880 | return false; |
| 2881 | |
| 2882 | break; |
| 2883 | |
| 2884 | case eEncodingA1: |
| 2885 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 2886 | n = Bits32 (opcode, 19, 16); |
| 2887 | registers = Bits32 (opcode, 15, 0); |
| 2888 | wback = BitIsSet (opcode, 21); |
| 2889 | |
| 2890 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 2891 | if ((n == 15) || (BitCount (registers) < 1)) |
| 2892 | return false; |
| 2893 | |
| 2894 | break; |
| 2895 | |
| 2896 | default: |
| 2897 | return false; |
| 2898 | } |
| 2899 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2900 | // address = R[n] - 4*BitCount(registers); |
| 2901 | |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2902 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2903 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2904 | |
| 2905 | if (!success) |
| 2906 | return false; |
| 2907 | |
| 2908 | address = address - (addr_byte_size * BitCount (registers)); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2909 | EmulateInstruction::Context context; |
| 2910 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 2911 | Register dwarf_reg; |
| 2912 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 2913 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2914 | |
| 2915 | for (int i = 0; i < 14; ++i) |
| 2916 | { |
| 2917 | if (BitIsSet (registers, i)) |
| 2918 | { |
| 2919 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2920 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2921 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2922 | if (!success) |
| 2923 | return false; |
| 2924 | |
| 2925 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 2926 | return false; |
| 2927 | |
| 2928 | offset += addr_byte_size; |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | // if registers<15> == ’1’ then |
| 2933 | // LoadWritePC(MemA[address,4]); |
| 2934 | if (BitIsSet (registers, 15)) |
| 2935 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2936 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2937 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2938 | if (!success) |
| 2939 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 2940 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 2941 | if (!LoadWritePC(context, data)) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2942 | return false; |
| 2943 | } |
| 2944 | |
| 2945 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2946 | if (wback && BitIsClear (registers, n)) |
| 2947 | { |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2948 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2949 | if (!success) |
| 2950 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 2951 | |
| 2952 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 2953 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2954 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 2955 | addr = addr + offset; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2956 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 2957 | return false; |
| 2958 | } |
| 2959 | |
| 2960 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2961 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2962 | return WriteBits32Unknown (n); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 2963 | } |
| 2964 | return true; |
| 2965 | } |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2966 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2967 | // LDMIB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 2968 | // consecutive memory locations start just above this address, and thea ddress of the last of those locations can |
| 2969 | // optinoally be written back to the base register. |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2970 | bool |
| 2971 | EmulateInstructionARM::EmulateLDMIB (ARMEncoding encoding) |
| 2972 | { |
| 2973 | #if 0 |
| 2974 | if ConditionPassed() then |
| 2975 | EncodingSpecificOperations(); |
| 2976 | address = R[n] + 4; |
| 2977 | |
| 2978 | for i = 0 to 14 |
| 2979 | if registers<i> == ’1’ then |
| 2980 | R[i] = MemA[address,4]; address = address + 4; |
| 2981 | if registers<15> == ’1’ then |
| 2982 | LoadWritePC(MemA[address,4]); |
| 2983 | |
| 2984 | if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 2985 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 2986 | #endif |
| 2987 | |
| 2988 | bool success = false; |
| 2989 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2990 | if (!success) |
| 2991 | return false; |
| 2992 | |
| 2993 | if (ConditionPassed()) |
| 2994 | { |
| 2995 | uint32_t n; |
| 2996 | uint32_t registers = 0; |
| 2997 | bool wback; |
| 2998 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2999 | switch (encoding) |
| 3000 | { |
| 3001 | case eEncodingA1: |
| 3002 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3003 | n = Bits32 (opcode, 19, 16); |
| 3004 | registers = Bits32 (opcode, 15, 0); |
| 3005 | wback = BitIsSet (opcode, 21); |
| 3006 | |
| 3007 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3008 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3009 | return false; |
| 3010 | |
| 3011 | break; |
| 3012 | default: |
| 3013 | return false; |
| 3014 | } |
| 3015 | // address = R[n] + 4; |
| 3016 | |
| 3017 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3018 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3019 | |
| 3020 | if (!success) |
| 3021 | return false; |
| 3022 | |
| 3023 | address = address + addr_byte_size; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3024 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3025 | EmulateInstruction::Context context; |
| 3026 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3027 | Register dwarf_reg; |
| 3028 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3029 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3030 | |
| 3031 | for (int i = 0; i < 14; ++i) |
| 3032 | { |
| 3033 | if (BitIsSet (registers, i)) |
| 3034 | { |
| 3035 | // R[i] = MemA[address,4]; address = address + 4; |
| 3036 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3037 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3038 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3039 | if (!success) |
| 3040 | return false; |
| 3041 | |
| 3042 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3043 | return false; |
| 3044 | |
| 3045 | offset += addr_byte_size; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | // if registers<15> == ’1’ then |
| 3050 | // LoadWritePC(MemA[address,4]); |
| 3051 | if (BitIsSet (registers, 15)) |
| 3052 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3053 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3054 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3055 | if (!success) |
| 3056 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 3057 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3058 | if (!LoadWritePC(context, data)) |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3059 | return false; |
| 3060 | } |
| 3061 | |
| 3062 | // if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 3063 | if (wback && BitIsClear (registers, n)) |
| 3064 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3065 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3066 | if (!success) |
| 3067 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3068 | |
| 3069 | offset = addr_byte_size * BitCount (registers); |
| 3070 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3071 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3072 | addr = addr + offset; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3073 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3078 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3079 | return WriteBits32Unknown (n); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3080 | } |
| 3081 | return true; |
| 3082 | } |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3083 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3084 | // Load Register (immediate) calculates an address from a base register value and |
| 3085 | // an immediate offset, loads a word from memory, and writes to a register. |
| 3086 | // LDR (immediate, Thumb) |
| 3087 | bool |
| 3088 | EmulateInstructionARM::EmulateLDRRtRnImm (ARMEncoding encoding) |
| 3089 | { |
| 3090 | #if 0 |
| 3091 | // ARM pseudo code... |
| 3092 | if (ConditionPassed()) |
| 3093 | { |
| 3094 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 3095 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 3096 | address = if index then offset_addr else R[n]; |
| 3097 | data = MemU[address,4]; |
| 3098 | if wback then R[n] = offset_addr; |
| 3099 | if t == 15 then |
| 3100 | if address<1:0> == '00' then LoadWritePC(data); else UNPREDICTABLE; |
| 3101 | elsif UnalignedSupport() || address<1:0> = '00' then |
| 3102 | R[t] = data; |
| 3103 | else R[t] = bits(32) UNKNOWN; // Can only apply before ARMv7 |
| 3104 | } |
| 3105 | #endif |
| 3106 | |
| 3107 | bool success = false; |
| 3108 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3109 | if (!success) |
| 3110 | return false; |
| 3111 | |
| 3112 | if (ConditionPassed()) |
| 3113 | { |
| 3114 | uint32_t Rt; // the destination register |
| 3115 | uint32_t Rn; // the base register |
| 3116 | uint32_t imm32; // the immediate offset used to form the address |
| 3117 | addr_t offset_addr; // the offset address |
| 3118 | addr_t address; // the calculated address |
| 3119 | uint32_t data; // the literal data value from memory load |
| 3120 | bool add, index, wback; |
| 3121 | switch (encoding) { |
| 3122 | case eEncodingT1: |
| 3123 | Rt = Bits32(opcode, 5, 3); |
| 3124 | Rn = Bits32(opcode, 2, 0); |
| 3125 | imm32 = Bits32(opcode, 10, 6) << 2; // imm32 = ZeroExtend(imm5:'00', 32); |
| 3126 | // index = TRUE; add = TRUE; wback = FALSE |
| 3127 | add = true; |
| 3128 | index = true; |
| 3129 | wback = false; |
| 3130 | break; |
| 3131 | default: |
| 3132 | return false; |
| 3133 | } |
| 3134 | uint32_t base = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 3135 | if (!success) |
| 3136 | return false; |
| 3137 | if (add) |
| 3138 | offset_addr = base + imm32; |
| 3139 | else |
| 3140 | offset_addr = base - imm32; |
| 3141 | |
| 3142 | address = (index ? offset_addr : base); |
| 3143 | |
| 3144 | if (wback) |
| 3145 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3146 | EmulateInstruction::Context ctx; |
| 3147 | ctx.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3148 | Register dwarf_reg; |
| 3149 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rn); |
| 3150 | ctx.SetRegisterPlusOffset (dwarf_reg, (int32_t) (offset_addr - base)); |
| 3151 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3152 | if (!WriteRegisterUnsigned (ctx, eRegisterKindDWARF, dwarf_r0 + Rn, offset_addr)) |
| 3153 | return false; |
| 3154 | } |
| 3155 | |
| 3156 | // Prepare to write to the Rt register. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3157 | EmulateInstruction::Context context; |
| 3158 | context.type = EmulateInstruction::eContextImmediate; |
| 3159 | context.SetNoArgs (); |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3160 | |
| 3161 | // Read memory from the address. |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3162 | data = MemURead(context, address, 4, 0, &success); |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3163 | if (!success) |
| 3164 | return false; |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3165 | |
| 3166 | if (Rt == 15) |
| 3167 | { |
| 3168 | if (Bits32(address, 1, 0) == 0) |
| 3169 | { |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3170 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3171 | return false; |
| 3172 | } |
| 3173 | else |
| 3174 | return false; |
| 3175 | } |
| 3176 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 3177 | { |
| 3178 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 3179 | return false; |
| 3180 | } |
| 3181 | else |
| 3182 | return false; |
| 3183 | } |
| 3184 | return true; |
| 3185 | } |
| 3186 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3187 | // STM (Store Multiple Increment After) stores multiple registers to consecutive memory locations using an address |
| 3188 | // from a base register. The consecutive memory locations start at this address, and teh address just above the last |
| 3189 | // of those locations can optionally be written back to the base register. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3190 | bool |
| 3191 | EmulateInstructionARM::EmulateSTM (ARMEncoding encoding) |
| 3192 | { |
| 3193 | #if 0 |
| 3194 | if ConditionPassed() then |
| 3195 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3196 | address = R[n]; |
| 3197 | |
| 3198 | for i = 0 to 14 |
| 3199 | if registers<i> == ’1’ then |
| 3200 | if i == n && wback && i != LowestSetBit(registers) then |
| 3201 | MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1 |
| 3202 | else |
| 3203 | MemA[address,4] = R[i]; |
| 3204 | address = address + 4; |
| 3205 | |
| 3206 | if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3207 | MemA[address,4] = PCStoreValue(); |
| 3208 | if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3209 | #endif |
| 3210 | |
| 3211 | bool success = false; |
| 3212 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3213 | if (!success) |
| 3214 | return false; |
| 3215 | |
| 3216 | if (ConditionPassed ()) |
| 3217 | { |
| 3218 | uint32_t n; |
| 3219 | uint32_t registers = 0; |
| 3220 | bool wback; |
| 3221 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3222 | |
| 3223 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3224 | switch (encoding) |
| 3225 | { |
| 3226 | case eEncodingT1: |
| 3227 | // n = UInt(Rn); registers = ’00000000’:register_list; wback = TRUE; |
| 3228 | n = Bits32 (opcode, 10, 8); |
| 3229 | registers = Bits32 (opcode, 7, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3230 | registers = registers & 0x00ff; // Make sure the top 8 bits are zeros. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3231 | wback = true; |
| 3232 | |
| 3233 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 3234 | if (BitCount (registers) < 1) |
| 3235 | return false; |
| 3236 | |
| 3237 | break; |
| 3238 | |
| 3239 | case eEncodingT2: |
| 3240 | // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’); |
| 3241 | n = Bits32 (opcode, 19, 16); |
| 3242 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3243 | registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3244 | wback = BitIsSet (opcode, 21); |
| 3245 | |
| 3246 | // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE; |
| 3247 | if ((n == 15) || (BitCount (registers) < 2)) |
| 3248 | return false; |
| 3249 | |
| 3250 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3251 | if (wback && BitIsSet (registers, n)) |
| 3252 | return false; |
| 3253 | |
| 3254 | break; |
| 3255 | |
| 3256 | case eEncodingA1: |
| 3257 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3258 | n = Bits32 (opcode, 19, 16); |
| 3259 | registers = Bits32 (opcode, 15, 0); |
| 3260 | wback = BitIsSet (opcode, 21); |
| 3261 | |
| 3262 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3263 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3264 | return false; |
| 3265 | |
| 3266 | break; |
| 3267 | |
| 3268 | default: |
| 3269 | return false; |
| 3270 | } |
| 3271 | |
| 3272 | // address = R[n]; |
| 3273 | int32_t offset = 0; |
| 3274 | const addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3275 | if (!success) |
| 3276 | return false; |
| 3277 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3278 | EmulateInstruction::Context context; |
| 3279 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3280 | Register base_reg; |
| 3281 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3282 | |
| 3283 | // for i = 0 to 14 |
| 3284 | for (int i = 0; i < 14; ++i) |
| 3285 | { |
| 3286 | int lowest_set_bit = 14; |
| 3287 | // if registers<i> == ’1’ then |
| 3288 | if (BitIsSet (registers, i)) |
| 3289 | { |
| 3290 | if (i < lowest_set_bit) |
| 3291 | lowest_set_bit = i; |
| 3292 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3293 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3294 | // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1 |
| 3295 | WriteBits32UnknownToMemory (address + offset); |
| 3296 | else |
| 3297 | { |
| 3298 | // MemA[address,4] = R[i]; |
| 3299 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3300 | if (!success) |
| 3301 | return false; |
| 3302 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3303 | Register data_reg; |
| 3304 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3305 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3306 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3307 | return false; |
| 3308 | } |
| 3309 | |
| 3310 | // address = address + 4; |
| 3311 | offset += addr_byte_size; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | // if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3316 | // MemA[address,4] = PCStoreValue(); |
| 3317 | if (BitIsSet (registers, 15)) |
| 3318 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3319 | Register pc_reg; |
| 3320 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3321 | context.SetRegisterPlusOffset (pc_reg, 8); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3322 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3323 | if (!success) |
| 3324 | return false; |
| 3325 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3326 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3327 | return false; |
| 3328 | } |
| 3329 | |
| 3330 | // if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3331 | if (wback) |
| 3332 | { |
| 3333 | offset = addr_byte_size * BitCount (registers); |
| 3334 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3335 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3336 | addr_t data = address + offset; |
| 3337 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3338 | return false; |
| 3339 | } |
| 3340 | } |
| 3341 | return true; |
| 3342 | } |
| 3343 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3344 | // STMDA (Store Multiple Decrement After) stores multiple registers to consecutive memory locations using an address |
| 3345 | // from a base register. The consecutive memory locations end at this address, and the address just below the lowest |
| 3346 | // of those locations can optionally be written back to the base register. |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3347 | bool |
| 3348 | EmulateInstructionARM::EmulateSTMDA (ARMEncoding encoding) |
| 3349 | { |
| 3350 | #if 0 |
| 3351 | if ConditionPassed() then |
| 3352 | EncodingSpecificOperations(); |
| 3353 | address = R[n] - 4*BitCount(registers) + 4; |
| 3354 | |
| 3355 | for i = 0 to 14 |
| 3356 | if registers<i> == ’1’ then |
| 3357 | if i == n && wback && i != LowestSetBit(registers) then |
| 3358 | MemA[address,4] = bits(32) UNKNOWN; |
| 3359 | else |
| 3360 | MemA[address,4] = R[i]; |
| 3361 | address = address + 4; |
| 3362 | |
| 3363 | if registers<15> == ’1’ then |
| 3364 | MemA[address,4] = PCStoreValue(); |
| 3365 | |
| 3366 | if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3367 | #endif |
| 3368 | |
| 3369 | bool success = false; |
| 3370 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3371 | if (!success) |
| 3372 | return false; |
| 3373 | |
| 3374 | if (ConditionPassed ()) |
| 3375 | { |
| 3376 | uint32_t n; |
| 3377 | uint32_t registers = 0; |
| 3378 | bool wback; |
| 3379 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3380 | |
| 3381 | // EncodingSpecificOperations(); |
| 3382 | switch (encoding) |
| 3383 | { |
| 3384 | case eEncodingA1: |
| 3385 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3386 | n = Bits32 (opcode, 19, 16); |
| 3387 | registers = Bits32 (opcode, 15, 0); |
| 3388 | wback = BitIsSet (opcode, 21); |
| 3389 | |
| 3390 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3391 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3392 | return false; |
| 3393 | break; |
| 3394 | default: |
| 3395 | return false; |
| 3396 | } |
| 3397 | |
| 3398 | // address = R[n] - 4*BitCount(registers) + 4; |
| 3399 | int32_t offset = 0; |
| 3400 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3401 | if (!success) |
| 3402 | return false; |
| 3403 | |
| 3404 | address = address - (addr_byte_size * BitCount (registers)) + 4; |
| 3405 | |
| 3406 | EmulateInstruction::Context context; |
| 3407 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3408 | Register base_reg; |
| 3409 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3410 | |
| 3411 | // for i = 0 to 14 |
| 3412 | for (int i = 0; i < 14; ++i) |
| 3413 | { |
| 3414 | int lowest_bit_set = 14; |
| 3415 | // if registers<i> == ’1’ then |
| 3416 | if (BitIsSet (registers, i)) |
| 3417 | { |
| 3418 | if (i < lowest_bit_set) |
| 3419 | lowest_bit_set = i; |
| 3420 | //if i == n && wback && i != LowestSetBit(registers) then |
| 3421 | if ((i == n) && wback && (i != lowest_bit_set)) |
| 3422 | // MemA[address,4] = bits(32) UNKNOWN; |
| 3423 | WriteBits32UnknownToMemory (address + offset); |
| 3424 | else |
| 3425 | { |
| 3426 | // MemA[address,4] = R[i]; |
| 3427 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3428 | if (!success) |
| 3429 | return false; |
| 3430 | |
| 3431 | Register data_reg; |
| 3432 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3433 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3434 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3435 | return false; |
| 3436 | } |
| 3437 | |
| 3438 | // address = address + 4; |
| 3439 | offset += addr_byte_size; |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | // if registers<15> == ’1’ then |
| 3444 | // MemA[address,4] = PCStoreValue(); |
| 3445 | if (BitIsSet (registers, 15)) |
| 3446 | { |
| 3447 | Register pc_reg; |
| 3448 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3449 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 3450 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3451 | if (!success) |
| 3452 | return false; |
| 3453 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3454 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3455 | return false; |
| 3456 | } |
| 3457 | |
| 3458 | // if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3459 | if (wback) |
| 3460 | { |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3461 | offset = (addr_byte_size * BitCount (registers)) * -1; |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3462 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 3463 | context.SetImmediateSigned (offset); |
| 3464 | addr_t data = address + offset; |
| 3465 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3466 | return false; |
| 3467 | } |
| 3468 | } |
| 3469 | return true; |
| 3470 | } |
| 3471 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3472 | // STMDB (Store Multiple Decrement Before) stores multiple registers to consecutive memory locations using an address |
| 3473 | // from a base register. The consecutive memory locations end just below this address, and the address of the first of |
| 3474 | // those locations can optionally be written back to the base register. |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3475 | bool |
| 3476 | EmulateInstructionARM::EmulateSTMDB (ARMEncoding encoding) |
| 3477 | { |
| 3478 | #if 0 |
| 3479 | if ConditionPassed() then |
| 3480 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3481 | address = R[n] - 4*BitCount(registers); |
| 3482 | |
| 3483 | for i = 0 to 14 |
| 3484 | if registers<i> == ’1’ then |
| 3485 | if i == n && wback && i != LowestSetBit(registers) then |
| 3486 | MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3487 | else |
| 3488 | MemA[address,4] = R[i]; |
| 3489 | address = address + 4; |
| 3490 | |
| 3491 | if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3492 | MemA[address,4] = PCStoreValue(); |
| 3493 | |
| 3494 | if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3495 | #endif |
| 3496 | |
| 3497 | |
| 3498 | bool success = false; |
| 3499 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3500 | if (!success) |
| 3501 | return false; |
| 3502 | |
| 3503 | if (ConditionPassed ()) |
| 3504 | { |
| 3505 | uint32_t n; |
| 3506 | uint32_t registers = 0; |
| 3507 | bool wback; |
| 3508 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3509 | |
| 3510 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3511 | switch (encoding) |
| 3512 | { |
| 3513 | case eEncodingT1: |
| 3514 | // if W == ’1’ && Rn == ’1101’ then SEE PUSH; |
| 3515 | if ((BitIsSet (opcode, 21)) && (Bits32 (opcode, 19, 16) == 13)) |
| 3516 | { |
| 3517 | // See PUSH |
| 3518 | } |
| 3519 | // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’); |
| 3520 | n = Bits32 (opcode, 19, 16); |
| 3521 | registers = Bits32 (opcode, 15, 0); |
| 3522 | registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros. |
| 3523 | wback = BitIsSet (opcode, 21); |
| 3524 | // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE; |
| 3525 | if ((n == 15) || BitCount (registers) < 2) |
| 3526 | return false; |
| 3527 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3528 | if (wback && BitIsSet (registers, n)) |
| 3529 | return false; |
| 3530 | break; |
| 3531 | |
| 3532 | case eEncodingA1: |
| 3533 | // if W == ’1’ && Rn == ’1101’ && BitCount(register_list) >= 2 then SEE PUSH; |
| 3534 | if (BitIsSet (opcode, 21) && (Bits32 (opcode, 19, 16) == 13) && BitCount (Bits32 (opcode, 15, 0)) >= 2) |
| 3535 | { |
| 3536 | // See Push |
| 3537 | } |
| 3538 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3539 | n = Bits32 (opcode, 19, 16); |
| 3540 | registers = Bits32 (opcode, 15, 0); |
| 3541 | wback = BitIsSet (opcode, 21); |
| 3542 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3543 | if ((n == 15) || BitCount (registers) < 1) |
| 3544 | return false; |
| 3545 | break; |
| 3546 | |
| 3547 | default: |
| 3548 | return false; |
| 3549 | } |
| 3550 | |
| 3551 | // address = R[n] - 4*BitCount(registers); |
| 3552 | |
| 3553 | int32_t offset = 0; |
| 3554 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3555 | if (!success) |
| 3556 | return false; |
| 3557 | |
| 3558 | address = address - (addr_byte_size * BitCount (registers)); |
| 3559 | |
| 3560 | EmulateInstruction::Context context; |
| 3561 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3562 | Register base_reg; |
| 3563 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3564 | |
| 3565 | // for i = 0 to 14 |
| 3566 | for (int i = 0; i < 14; ++i) |
| 3567 | { |
| 3568 | uint32_t lowest_set_bit = 14; |
| 3569 | // if registers<i> == ’1’ then |
| 3570 | if (BitIsSet (registers, i)) |
| 3571 | { |
| 3572 | if (i < lowest_set_bit) |
| 3573 | lowest_set_bit = i; |
| 3574 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3575 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3576 | // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3577 | WriteBits32UnknownToMemory (address + offset); |
| 3578 | else |
| 3579 | { |
| 3580 | // MemA[address,4] = R[i]; |
| 3581 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3582 | if (!success) |
| 3583 | return false; |
| 3584 | |
| 3585 | Register data_reg; |
| 3586 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3587 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3588 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3589 | return false; |
| 3590 | } |
| 3591 | |
| 3592 | // address = address + 4; |
| 3593 | offset += addr_byte_size; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | // if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3598 | // MemA[address,4] = PCStoreValue(); |
| 3599 | if (BitIsSet (registers, 15)) |
| 3600 | { |
| 3601 | Register pc_reg; |
| 3602 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3603 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 3604 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3605 | if (!success) |
| 3606 | return false; |
| 3607 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3608 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3609 | return false; |
| 3610 | } |
| 3611 | |
| 3612 | // if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3613 | if (wback) |
| 3614 | { |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3615 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 3616 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 3617 | context.SetImmediateSigned (offset); |
| 3618 | addr_t data = address + offset; |
| 3619 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3620 | return false; |
| 3621 | } |
| 3622 | } |
| 3623 | return true; |
| 3624 | } |
| 3625 | |
| 3626 | // STMIB (Store Multiple Increment Before) stores multiple registers to consecutive memory locations using an address |
| 3627 | // from a base register. The consecutive memory locations start just above this address, and the address of the last |
| 3628 | // of those locations can optionally be written back to the base register. |
| 3629 | bool |
| 3630 | EmulateInstructionARM::EmulateSTMIB (ARMEncoding encoding) |
| 3631 | { |
| 3632 | #if 0 |
| 3633 | if ConditionPassed() then |
| 3634 | EncodingSpecificOperations(); |
| 3635 | address = R[n] + 4; |
| 3636 | |
| 3637 | for i = 0 to 14 |
| 3638 | if registers<i> == ’1’ then |
| 3639 | if i == n && wback && i != LowestSetBit(registers) then |
| 3640 | MemA[address,4] = bits(32) UNKNOWN; |
| 3641 | else |
| 3642 | MemA[address,4] = R[i]; |
| 3643 | address = address + 4; |
| 3644 | |
| 3645 | if registers<15> == ’1’ then |
| 3646 | MemA[address,4] = PCStoreValue(); |
| 3647 | |
| 3648 | if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3649 | #endif |
| 3650 | |
| 3651 | bool success = false; |
| 3652 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3653 | if (!success) |
| 3654 | return false; |
| 3655 | |
| 3656 | if (ConditionPassed()) |
| 3657 | { |
| 3658 | uint32_t n; |
| 3659 | uint32_t registers = 0; |
| 3660 | bool wback; |
| 3661 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3662 | |
| 3663 | // EncodingSpecificOperations(); |
| 3664 | switch (encoding) |
| 3665 | { |
| 3666 | case eEncodingA1: |
| 3667 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3668 | n = Bits32 (opcode, 19, 16); |
| 3669 | registers = Bits32 (opcode, 15, 0); |
| 3670 | wback = BitIsSet (opcode, 21); |
| 3671 | |
| 3672 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3673 | if ((n == 15) && (BitCount (registers) < 1)) |
| 3674 | return false; |
| 3675 | break; |
| 3676 | default: |
| 3677 | return false; |
| 3678 | } |
| 3679 | // address = R[n] + 4; |
| 3680 | |
| 3681 | int32_t offset = 0; |
| 3682 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3683 | if (!success) |
| 3684 | return false; |
| 3685 | |
| 3686 | address = address + addr_byte_size; |
| 3687 | |
| 3688 | EmulateInstruction::Context context; |
| 3689 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3690 | Register base_reg; |
| 3691 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3692 | |
| 3693 | uint32_t lowest_set_bit = 14; |
| 3694 | // for i = 0 to 14 |
| 3695 | for (int i = 0; i < 14; ++i) |
| 3696 | { |
| 3697 | // if registers<i> == ’1’ then |
| 3698 | if (BitIsSet (registers, i)) |
| 3699 | { |
| 3700 | if (i < lowest_set_bit) |
| 3701 | lowest_set_bit = i; |
| 3702 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3703 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3704 | // MemA[address,4] = bits(32) UNKNOWN; |
| 3705 | WriteBits32UnknownToMemory (address + offset); |
| 3706 | // else |
| 3707 | else |
| 3708 | { |
| 3709 | // MemA[address,4] = R[i]; |
| 3710 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3711 | if (!success) |
| 3712 | return false; |
| 3713 | |
| 3714 | Register data_reg; |
| 3715 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3716 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3717 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3718 | return false; |
| 3719 | } |
| 3720 | |
| 3721 | // address = address + 4; |
| 3722 | offset += addr_byte_size; |
| 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | // if registers<15> == ’1’ then |
| 3727 | // MemA[address,4] = PCStoreValue(); |
| 3728 | if (BitIsSet (registers, 15)) |
| 3729 | { |
| 3730 | Register pc_reg; |
| 3731 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3732 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 3733 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3734 | if (!success) |
| 3735 | return false; |
| 3736 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3737 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3738 | return false; |
| 3739 | } |
| 3740 | |
| 3741 | // if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3742 | if (wback) |
| 3743 | { |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3744 | offset = addr_byte_size * BitCount (registers); |
| 3745 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 3746 | context.SetImmediateSigned (offset); |
| 3747 | addr_t data = address + offset; |
| 3748 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3749 | return false; |
| 3750 | } |
| 3751 | } |
| 3752 | return true; |
| 3753 | } |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 3754 | |
| 3755 | // STR (store immediate) calcualtes an address from a base register value and an immediate offset, and stores a word |
| 3756 | // from a register to memory. It can use offset, post-indexed, or pre-indexed addressing. |
| 3757 | bool |
| 3758 | EmulateInstructionARM::EmulateSTRThumb (ARMEncoding encoding) |
| 3759 | { |
| 3760 | #if 0 |
| 3761 | if ConditionPassed() then |
| 3762 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3763 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 3764 | address = if index then offset_addr else R[n]; |
| 3765 | if UnalignedSupport() || address<1:0> == ’00’ then |
| 3766 | MemU[address,4] = R[t]; |
| 3767 | else // Can only occur before ARMv7 |
| 3768 | MemU[address,4] = bits(32) UNKNOWN; |
| 3769 | if wback then R[n] = offset_addr; |
| 3770 | #endif |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3771 | |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 3772 | bool success = false; |
| 3773 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3774 | if (!success) |
| 3775 | return false; |
| 3776 | |
| 3777 | if (ConditionPassed()) |
| 3778 | { |
| 3779 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3780 | |
| 3781 | uint32_t t; |
| 3782 | uint32_t n; |
| 3783 | uint32_t imm32; |
| 3784 | bool index; |
| 3785 | bool add; |
| 3786 | bool wback; |
| 3787 | // EncodingSpecificOperations (); NullCheckIfThumbEE(n); |
| 3788 | switch (encoding) |
| 3789 | { |
| 3790 | case eEncodingT1: |
| 3791 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5:’00’, 32); |
| 3792 | t = Bits32 (opcode, 2, 0); |
| 3793 | n = Bits32 (opcode, 5, 3); |
| 3794 | imm32 = Bits32 (opcode, 10, 6) << 2; |
| 3795 | |
| 3796 | // index = TRUE; add = TRUE; wback = FALSE; |
| 3797 | index = true; |
| 3798 | add = false; |
| 3799 | wback = false; |
| 3800 | break; |
| 3801 | |
| 3802 | case eEncodingT2: |
| 3803 | // t = UInt(Rt); n = 13; imm32 = ZeroExtend(imm8:’00’, 32); |
| 3804 | t = Bits32 (opcode, 10, 8); |
| 3805 | n = 13; |
| 3806 | imm32 = Bits32 (opcode, 7, 0) << 2; |
| 3807 | |
| 3808 | // index = TRUE; add = TRUE; wback = FALSE; |
| 3809 | index = true; |
| 3810 | add = true; |
| 3811 | wback = false; |
| 3812 | break; |
| 3813 | |
| 3814 | case eEncodingT3: |
| 3815 | // if Rn == ’1111’ then UNDEFINED; |
| 3816 | if (Bits32 (opcode, 19, 16) == 15) |
| 3817 | return false; |
| 3818 | |
| 3819 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 3820 | t = Bits32 (opcode, 15, 12); |
| 3821 | n = Bits32 (opcode, 19, 16); |
| 3822 | imm32 = Bits32 (opcode, 11, 0); |
| 3823 | |
| 3824 | // index = TRUE; add = TRUE; wback = FALSE; |
| 3825 | index = true; |
| 3826 | add = true; |
| 3827 | wback = false; |
| 3828 | |
| 3829 | // if t == 15 then UNPREDICTABLE; |
| 3830 | if (t == 15) |
| 3831 | return false; |
| 3832 | break; |
| 3833 | |
| 3834 | case eEncodingT4: |
| 3835 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRT; |
| 3836 | // if Rn == ’1101’ && P == ’1’ && U == ’0’ && W == ’1’ && imm8 == ’00000100’ then SEE PUSH; |
| 3837 | // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED; |
| 3838 | if ((Bits32 (opcode, 19, 16) == 15) |
| 3839 | || (BitIsClear (opcode, 10) && BitIsClear (opcode, 8))) |
| 3840 | return false; |
| 3841 | |
| 3842 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 3843 | t = Bits32 (opcode, 15, 12); |
| 3844 | n = Bits32 (opcode, 19, 16); |
| 3845 | imm32 = Bits32 (opcode, 7, 0); |
| 3846 | |
| 3847 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 3848 | index = BitIsSet (opcode, 10); |
| 3849 | add = BitIsSet (opcode, 9); |
| 3850 | wback = BitIsSet (opcode, 8); |
| 3851 | |
| 3852 | // if t == 15 || (wback && n == t) then UNPREDICTABLE; |
| 3853 | if ((t == 15) || (wback && (n == t))) |
| 3854 | return false; |
| 3855 | break; |
| 3856 | |
| 3857 | default: |
| 3858 | return false; |
| 3859 | } |
| 3860 | |
| 3861 | addr_t offset_addr; |
| 3862 | addr_t address; |
| 3863 | |
| 3864 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 3865 | uint32_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3866 | if (!success) |
| 3867 | return false; |
| 3868 | |
| 3869 | if (add) |
| 3870 | offset_addr = base_address + imm32; |
| 3871 | else |
| 3872 | offset_addr = base_address - imm32; |
| 3873 | |
| 3874 | // address = if index then offset_addr else R[n]; |
| 3875 | if (index) |
| 3876 | address = offset_addr; |
| 3877 | else |
| 3878 | address = base_address; |
| 3879 | |
| 3880 | EmulateInstruction::Context context; |
| 3881 | context.type = eContextRegisterStore; |
| 3882 | Register base_reg; |
| 3883 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3884 | |
| 3885 | // if UnalignedSupport() || address<1:0> == ’00’ then |
| 3886 | if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 3887 | { |
| 3888 | // MemU[address,4] = R[t]; |
| 3889 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 3890 | if (!success) |
| 3891 | return false; |
| 3892 | |
| 3893 | Register data_reg; |
| 3894 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 3895 | int32_t offset = address - base_address; |
| 3896 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3897 | if (!MemUWrite (context, address, data, addr_byte_size)) |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 3898 | return false; |
| 3899 | } |
| 3900 | else |
| 3901 | { |
| 3902 | // MemU[address,4] = bits(32) UNKNOWN; |
| 3903 | WriteBits32UnknownToMemory (address); |
| 3904 | } |
| 3905 | |
| 3906 | // if wback then R[n] = offset_addr; |
| 3907 | if (wback) |
| 3908 | { |
| 3909 | context.type = eContextRegisterLoad; |
| 3910 | context.SetAddress (offset_addr); |
| 3911 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 3912 | return false; |
| 3913 | } |
| 3914 | } |
| 3915 | return true; |
| 3916 | } |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3917 | |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 3918 | // STR (Store Register) calculates an address from a base register value and an offset register value, stores a |
| 3919 | // word from a register to memory. The offset register value can optionally be shifted. |
| 3920 | bool |
| 3921 | EmulateInstructionARM::EmulateSTRRegister (ARMEncoding encoding) |
| 3922 | { |
| 3923 | #if 0 |
| 3924 | if ConditionPassed() then |
| 3925 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3926 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 3927 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 3928 | address = if index then offset_addr else R[n]; |
| 3929 | if t == 15 then // Only possible for encoding A1 |
| 3930 | data = PCStoreValue(); |
| 3931 | else |
| 3932 | data = R[t]; |
| 3933 | if UnalignedSupport() || address<1:0> == ’00’ || CurrentInstrSet() == InstrSet_ARM then |
| 3934 | MemU[address,4] = data; |
| 3935 | else // Can only occur before ARMv7 |
| 3936 | MemU[address,4] = bits(32) UNKNOWN; |
| 3937 | if wback then R[n] = offset_addr; |
| 3938 | #endif |
| 3939 | |
| 3940 | bool success = false; |
| 3941 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3942 | if (!success) |
| 3943 | return false; |
| 3944 | |
| 3945 | if (ConditionPassed()) |
| 3946 | { |
| 3947 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3948 | |
| 3949 | uint32_t t; |
| 3950 | uint32_t n; |
| 3951 | uint32_t m; |
| 3952 | ARM_ShifterType shift_t; |
| 3953 | uint32_t shift_n; |
| 3954 | bool index; |
| 3955 | bool add; |
| 3956 | bool wback; |
| 3957 | |
| 3958 | // EncodingSpecificOperations (); NullCheckIfThumbEE(n); |
| 3959 | switch (encoding) |
| 3960 | { |
| 3961 | case eEncodingT1: |
| 3962 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 3963 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 3964 | t = Bits32 (opcode, 2, 0); |
| 3965 | n = Bits32 (opcode, 5, 3); |
| 3966 | m = Bits32 (opcode, 8, 6); |
| 3967 | |
| 3968 | // index = TRUE; add = TRUE; wback = FALSE; |
| 3969 | index = true; |
| 3970 | add = true; |
| 3971 | wback = false; |
| 3972 | |
| 3973 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 3974 | shift_t = SRType_LSL; |
| 3975 | shift_n = 0; |
| 3976 | break; |
| 3977 | |
| 3978 | case eEncodingT2: |
| 3979 | // if Rn == ’1111’ then UNDEFINED; |
| 3980 | if (Bits32 (opcode, 19, 16) == 15) |
| 3981 | return false; |
| 3982 | |
| 3983 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 3984 | t = Bits32 (opcode, 15, 12); |
| 3985 | n = Bits32 (opcode, 19, 16); |
| 3986 | m = Bits32 (opcode, 3, 0); |
| 3987 | |
| 3988 | // index = TRUE; add = TRUE; wback = FALSE; |
| 3989 | index = true; |
| 3990 | add = true; |
| 3991 | wback = false; |
| 3992 | |
| 3993 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 3994 | shift_t = SRType_LSL; |
| 3995 | shift_n = Bits32 (opcode, 5, 4); |
| 3996 | |
| 3997 | // if t == 15 || BadReg(m) then UNPREDICTABLE; |
| 3998 | if ((t == 15) || (BadReg (m))) |
| 3999 | return false; |
| 4000 | break; |
| 4001 | |
| 4002 | case eEncodingA1: |
| 4003 | { |
| 4004 | // if P == ’0’ && W == ’1’ then SEE STRT; |
| 4005 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4006 | t = Bits32 (opcode, 15, 12); |
| 4007 | n = Bits32 (opcode, 19, 16); |
| 4008 | m = Bits32 (opcode, 3, 0); |
| 4009 | |
| 4010 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 4011 | index = BitIsSet (opcode, 24); |
| 4012 | add = BitIsSet (opcode, 23); |
| 4013 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 4014 | |
| 4015 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 4016 | uint32_t typ = Bits32 (opcode, 6, 5); |
| 4017 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 4018 | shift_n = DecodeImmShift(typ, imm5, shift_t); |
| 4019 | |
| 4020 | // if m == 15 then UNPREDICTABLE; |
| 4021 | if (m == 15) |
| 4022 | return false; |
| 4023 | |
| 4024 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 4025 | if (wback && ((n == 15) || (n == t))) |
| 4026 | return false; |
| 4027 | |
| 4028 | break; |
| 4029 | } |
| 4030 | default: |
| 4031 | return false; |
| 4032 | } |
| 4033 | |
| 4034 | addr_t offset_addr; |
| 4035 | addr_t address; |
| 4036 | int32_t offset = 0; |
| 4037 | |
| 4038 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4039 | if (!success) |
| 4040 | return false; |
| 4041 | |
| 4042 | uint32_t Rm_data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 4043 | if (!success) |
| 4044 | return false; |
| 4045 | |
| 4046 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4047 | offset = Shift (Rm_data, shift_t, shift_n, APSR_C); |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4048 | |
| 4049 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4050 | if (add) |
| 4051 | offset_addr = base_address + offset; |
| 4052 | else |
| 4053 | offset_addr = base_address - offset; |
| 4054 | |
| 4055 | // address = if index then offset_addr else R[n]; |
| 4056 | if (index) |
| 4057 | address = offset_addr; |
| 4058 | else |
| 4059 | address = base_address; |
| 4060 | |
| 4061 | uint32_t data; |
| 4062 | // if t == 15 then // Only possible for encoding A1 |
| 4063 | if (t == 15) |
| 4064 | // data = PCStoreValue(); |
| 4065 | data = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 4066 | else |
| 4067 | // data = R[t]; |
| 4068 | data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4069 | |
| 4070 | if (!success) |
| 4071 | return false; |
| 4072 | |
| 4073 | EmulateInstruction::Context context; |
| 4074 | context.type = eContextRegisterStore; |
| 4075 | |
| 4076 | // if UnalignedSupport() || address<1:0> == ’00’ || CurrentInstrSet() == InstrSet_ARM then |
| 4077 | if (UnalignedSupport () |
| 4078 | || (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 4079 | || CurrentInstrSet() == eModeARM) |
| 4080 | { |
| 4081 | // MemU[address,4] = data; |
| 4082 | |
| 4083 | Register base_reg; |
| 4084 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4085 | |
| 4086 | Register data_reg; |
| 4087 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4088 | |
| 4089 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - base_address); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4090 | if (!MemUWrite (context, address, data, addr_byte_size)) |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4091 | return false; |
| 4092 | |
| 4093 | } |
| 4094 | else |
| 4095 | // MemU[address,4] = bits(32) UNKNOWN; |
| 4096 | WriteBits32UnknownToMemory (address); |
| 4097 | |
| 4098 | // if wback then R[n] = offset_addr; |
| 4099 | if (wback) |
| 4100 | { |
| 4101 | context.type = eContextRegisterLoad; |
| 4102 | context.SetAddress (offset_addr); |
| 4103 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4104 | return false; |
| 4105 | } |
| 4106 | |
| 4107 | } |
| 4108 | return true; |
| 4109 | } |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4110 | |
| 4111 | bool |
| 4112 | EmulateInstructionARM::EmulateSTRBThumb (ARMEncoding encoding) |
| 4113 | { |
| 4114 | #if 0 |
| 4115 | if ConditionPassed() then |
| 4116 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4117 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4118 | address = if index then offset_addr else R[n]; |
| 4119 | MemU[address,1] = R[t]<7:0>; |
| 4120 | if wback then R[n] = offset_addr; |
| 4121 | #endif |
| 4122 | |
| 4123 | |
| 4124 | bool success = false; |
| 4125 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4126 | if (!success) |
| 4127 | return false; |
| 4128 | |
| 4129 | if (ConditionPassed ()) |
| 4130 | { |
| 4131 | uint32_t t; |
| 4132 | uint32_t n; |
| 4133 | uint32_t imm32; |
| 4134 | bool index; |
| 4135 | bool add; |
| 4136 | bool wback; |
| 4137 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4138 | switch (encoding) |
| 4139 | { |
| 4140 | case eEncodingT1: |
| 4141 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5, 32); |
| 4142 | t = Bits32 (opcode, 2, 0); |
| 4143 | n = Bits32 (opcode, 5, 3); |
| 4144 | imm32 = Bits32 (opcode, 10, 6); |
| 4145 | |
| 4146 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4147 | index = true; |
| 4148 | add = true; |
| 4149 | wback = false; |
| 4150 | break; |
| 4151 | |
| 4152 | case eEncodingT2: |
| 4153 | // if Rn == ’1111’ then UNDEFINED; |
| 4154 | if (Bits32 (opcode, 19, 16) == 15) |
| 4155 | return false; |
| 4156 | |
| 4157 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4158 | t = Bits32 (opcode, 15, 12); |
| 4159 | n = Bits32 (opcode, 19, 16); |
| 4160 | imm32 = Bits32 (opcode, 11, 0); |
| 4161 | |
| 4162 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4163 | index = true; |
| 4164 | add = true; |
| 4165 | wback = false; |
| 4166 | |
| 4167 | // if BadReg(t) then UNPREDICTABLE; |
| 4168 | if (BadReg (t)) |
| 4169 | return false; |
| 4170 | break; |
| 4171 | |
| 4172 | case eEncodingT3: |
| 4173 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRBT; |
| 4174 | // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED; |
| 4175 | if (Bits32 (opcode, 19, 16) == 15) |
| 4176 | return false; |
| 4177 | |
| 4178 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 4179 | t = Bits32 (opcode, 15, 12); |
| 4180 | n = Bits32 (opcode, 19, 16); |
| 4181 | imm32 = Bits32 (opcode, 7, 0); |
| 4182 | |
| 4183 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 4184 | index = BitIsSet (opcode, 10); |
| 4185 | add = BitIsSet (opcode, 9); |
| 4186 | wback = BitIsSet (opcode, 8); |
| 4187 | |
| 4188 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE |
| 4189 | if ((BadReg (t)) || (wback && (n == t))) |
| 4190 | return false; |
| 4191 | break; |
| 4192 | |
| 4193 | default: |
| 4194 | return false; |
| 4195 | } |
| 4196 | |
| 4197 | addr_t offset_addr; |
| 4198 | addr_t address; |
| 4199 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4200 | if (!success) |
| 4201 | return false; |
| 4202 | |
| 4203 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4204 | if (add) |
| 4205 | offset_addr = base_address + imm32; |
| 4206 | else |
| 4207 | offset_addr = base_address - imm32; |
| 4208 | |
| 4209 | // address = if index then offset_addr else R[n]; |
| 4210 | if (index) |
| 4211 | address = offset_addr; |
| 4212 | else |
| 4213 | address = base_address; |
| 4214 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4215 | // MemU[address,1] = R[t]<7:0> |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4216 | Register base_reg; |
| 4217 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4218 | |
| 4219 | Register data_reg; |
| 4220 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4221 | |
| 4222 | EmulateInstruction::Context context; |
| 4223 | context.type = eContextRegisterStore; |
| 4224 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - base_address); |
| 4225 | |
| 4226 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4227 | if (!success) |
| 4228 | return false; |
| 4229 | |
| 4230 | data = Bits32 (data, 7, 0); |
| 4231 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4232 | if (!MemUWrite (context, address, data, 1)) |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4233 | return false; |
| 4234 | |
| 4235 | // if wback then R[n] = offset_addr; |
| 4236 | if (wback) |
| 4237 | { |
| 4238 | context.type = eContextRegisterLoad; |
| 4239 | context.SetAddress (offset_addr); |
| 4240 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4241 | return false; |
| 4242 | } |
| 4243 | |
| 4244 | } |
| 4245 | |
| 4246 | return true; |
| 4247 | } |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4248 | |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4249 | // Add with Carry (immediate) adds an immediate value and the carry flag value to a register value, |
| 4250 | // and writes the result to the destination register. It can optionally update the condition flags |
| 4251 | // based on the result. |
| 4252 | bool |
| 4253 | EmulateInstructionARM::EmulateADCImm (ARMEncoding encoding) |
| 4254 | { |
| 4255 | #if 0 |
| 4256 | // ARM pseudo code... |
| 4257 | if ConditionPassed() then |
| 4258 | EncodingSpecificOperations(); |
| 4259 | (result, carry, overflow) = AddWithCarry(R[n], imm32, APSR.C); |
| 4260 | if d == 15 then // Can only occur for ARM encoding |
| 4261 | ALUWritePC(result); // setflags is always FALSE here |
| 4262 | else |
| 4263 | R[d] = result; |
| 4264 | if setflags then |
| 4265 | APSR.N = result<31>; |
| 4266 | APSR.Z = IsZeroBit(result); |
| 4267 | APSR.C = carry; |
| 4268 | APSR.V = overflow; |
| 4269 | #endif |
| 4270 | |
| 4271 | bool success = false; |
| 4272 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4273 | if (!success) |
| 4274 | return false; |
| 4275 | |
| 4276 | if (ConditionPassed()) |
| 4277 | { |
| 4278 | uint32_t Rd, Rn; |
| 4279 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 4280 | bool setflags; |
| 4281 | switch (encoding) |
| 4282 | { |
| 4283 | case eEncodingT1: |
| 4284 | Rd = Bits32(opcode, 11, 8); |
| 4285 | Rn = Bits32(opcode, 19, 16); |
| 4286 | setflags = BitIsSet(opcode, 20); |
| 4287 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 4288 | if (BadReg(Rd) || BadReg(Rn)) |
| 4289 | return false; |
| 4290 | break; |
| 4291 | case eEncodingA1: |
| 4292 | Rd = Bits32(opcode, 15, 12); |
| 4293 | Rn = Bits32(opcode, 19, 16); |
| 4294 | setflags = BitIsSet(opcode, 20); |
| 4295 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 4296 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4297 | if (Rd == 15 && setflags) |
| 4298 | return false; |
| 4299 | break; |
| 4300 | default: |
| 4301 | return false; |
| 4302 | } |
| 4303 | |
| 4304 | // Read the first operand. |
| 4305 | int32_t val1 = ReadCoreReg(Rn, &success); |
| 4306 | if (!success) |
| 4307 | return false; |
| 4308 | |
| 4309 | AddWithCarryResult res = AddWithCarry(val1, imm32, APSR_C); |
| 4310 | |
| 4311 | EmulateInstruction::Context context; |
| 4312 | context.type = EmulateInstruction::eContextImmediate; |
| 4313 | context.SetNoArgs (); |
| 4314 | |
| 4315 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 4316 | return false; |
| 4317 | } |
| 4318 | return true; |
| 4319 | } |
| 4320 | |
| 4321 | // Add with Carry (register) adds a register value, the carry flag value, and an optionally-shifted |
| 4322 | // register value, and writes the result to the destination register. It can optionally update the |
| 4323 | // condition flags based on the result. |
| 4324 | bool |
| 4325 | EmulateInstructionARM::EmulateADCReg (ARMEncoding encoding) |
| 4326 | { |
| 4327 | #if 0 |
| 4328 | // ARM pseudo code... |
| 4329 | if ConditionPassed() then |
| 4330 | EncodingSpecificOperations(); |
| 4331 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4332 | (result, carry, overflow) = AddWithCarry(R[n], shifted, APSR.C); |
| 4333 | if d == 15 then // Can only occur for ARM encoding |
| 4334 | ALUWritePC(result); // setflags is always FALSE here |
| 4335 | else |
| 4336 | R[d] = result; |
| 4337 | if setflags then |
| 4338 | APSR.N = result<31>; |
| 4339 | APSR.Z = IsZeroBit(result); |
| 4340 | APSR.C = carry; |
| 4341 | APSR.V = overflow; |
| 4342 | #endif |
| 4343 | |
| 4344 | bool success = false; |
| 4345 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4346 | if (!success) |
| 4347 | return false; |
| 4348 | |
| 4349 | if (ConditionPassed()) |
| 4350 | { |
| 4351 | uint32_t Rd, Rn, Rm; |
| 4352 | ARM_ShifterType shift_t; |
| 4353 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 4354 | bool setflags; |
| 4355 | switch (encoding) |
| 4356 | { |
| 4357 | case eEncodingT1: |
| 4358 | Rd = Rn = Bits32(opcode, 2, 0); |
| 4359 | Rm = Bits32(opcode, 5, 3); |
| 4360 | setflags = !InITBlock(); |
| 4361 | shift_t = SRType_LSL; |
| 4362 | shift_n = 0; |
| 4363 | case eEncodingT2: |
| 4364 | Rd = Bits32(opcode, 11, 8); |
| 4365 | Rn = Bits32(opcode, 19, 16); |
| 4366 | Rm = Bits32(opcode, 3, 0); |
| 4367 | setflags = BitIsSet(opcode, 20); |
| 4368 | shift_n = DecodeImmShift(Bits32(opcode, 5, 4), Bits32(opcode, 14, 12)<<2 | Bits32(opcode, 7, 6), shift_t); |
| 4369 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 4370 | return false; |
| 4371 | break; |
| 4372 | case eEncodingA1: |
| 4373 | Rd = Bits32(opcode, 15, 12); |
| 4374 | Rn = Bits32(opcode, 19, 16); |
| 4375 | Rm = Bits32(opcode, 3, 0); |
| 4376 | setflags = BitIsSet(opcode, 20); |
| 4377 | shift_n = DecodeImmShift(Bits32(opcode, 6, 5), Bits32(opcode, 11, 7), shift_t); |
| 4378 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4379 | if (Rd == 15 && setflags) |
| 4380 | return false; |
| 4381 | break; |
| 4382 | default: |
| 4383 | return false; |
| 4384 | } |
| 4385 | |
| 4386 | // Read the first operand. |
| 4387 | int32_t val1 = ReadCoreReg(Rn, &success); |
| 4388 | if (!success) |
| 4389 | return false; |
| 4390 | |
| 4391 | // Read the second operand. |
| 4392 | int32_t val2 = ReadCoreReg(Rm, &success); |
| 4393 | if (!success) |
| 4394 | return false; |
| 4395 | |
| 4396 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 4397 | AddWithCarryResult res = AddWithCarry(val1, shifted, APSR_C); |
| 4398 | |
| 4399 | EmulateInstruction::Context context; |
| 4400 | context.type = EmulateInstruction::eContextImmediate; |
| 4401 | context.SetNoArgs (); |
| 4402 | |
| 4403 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 4404 | return false; |
| 4405 | } |
| 4406 | return true; |
| 4407 | } |
| 4408 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4409 | // This instruction performs a bitwise AND of a register value and an immediate value, and writes the result |
| 4410 | // to the destination register. It can optionally update the condition flags based on the result. |
| 4411 | bool |
| 4412 | EmulateInstructionARM::EmulateANDImm (ARMEncoding encoding) |
| 4413 | { |
| 4414 | #if 0 |
| 4415 | // ARM pseudo code... |
| 4416 | if ConditionPassed() then |
| 4417 | EncodingSpecificOperations(); |
| 4418 | result = R[n] AND imm32; |
| 4419 | if d == 15 then // Can only occur for ARM encoding |
| 4420 | ALUWritePC(result); // setflags is always FALSE here |
| 4421 | else |
| 4422 | R[d] = result; |
| 4423 | if setflags then |
| 4424 | APSR.N = result<31>; |
| 4425 | APSR.Z = IsZeroBit(result); |
| 4426 | APSR.C = carry; |
| 4427 | // APSR.V unchanged |
| 4428 | #endif |
| 4429 | |
| 4430 | bool success = false; |
| 4431 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4432 | if (!success) |
| 4433 | return false; |
| 4434 | |
| 4435 | if (ConditionPassed()) |
| 4436 | { |
| 4437 | uint32_t Rd, Rn; |
| 4438 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 4439 | bool setflags; |
| 4440 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 4441 | switch (encoding) |
| 4442 | { |
| 4443 | case eEncodingT1: |
| 4444 | Rd = Bits32(opcode, 11, 8); |
| 4445 | Rn = Bits32(opcode, 19, 16); |
| 4446 | setflags = BitIsSet(opcode, 20); |
| 4447 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 4448 | // TODO: Emulate TST (immediate) |
| 4449 | if (Rd == 15 && setflags) |
| 4450 | return false; |
| 4451 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn)) |
| 4452 | return false; |
| 4453 | break; |
| 4454 | case eEncodingA1: |
| 4455 | Rd = Bits32(opcode, 15, 12); |
| 4456 | Rn = Bits32(opcode, 19, 16); |
| 4457 | setflags = BitIsSet(opcode, 20); |
| 4458 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 4459 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4460 | if (Rd == 15 && setflags) |
| 4461 | return false; |
| 4462 | break; |
| 4463 | default: |
| 4464 | return false; |
| 4465 | } |
| 4466 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4467 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4468 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4469 | if (!success) |
| 4470 | return false; |
| 4471 | |
| 4472 | uint32_t result = val1 & imm32; |
| 4473 | |
| 4474 | EmulateInstruction::Context context; |
| 4475 | context.type = EmulateInstruction::eContextImmediate; |
| 4476 | context.SetNoArgs (); |
| 4477 | |
| 4478 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 4479 | return false; |
| 4480 | } |
| 4481 | return true; |
| 4482 | } |
| 4483 | |
| 4484 | // This instruction performs a bitwise AND of a register value and an optionally-shifted register value, |
| 4485 | // and writes the result to the destination register. It can optionally update the condition flags |
| 4486 | // based on the result. |
| 4487 | bool |
| 4488 | EmulateInstructionARM::EmulateANDReg (ARMEncoding encoding) |
| 4489 | { |
| 4490 | #if 0 |
| 4491 | // ARM pseudo code... |
| 4492 | if ConditionPassed() then |
| 4493 | EncodingSpecificOperations(); |
| 4494 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 4495 | result = R[n] AND shifted; |
| 4496 | if d == 15 then // Can only occur for ARM encoding |
| 4497 | ALUWritePC(result); // setflags is always FALSE here |
| 4498 | else |
| 4499 | R[d] = result; |
| 4500 | if setflags then |
| 4501 | APSR.N = result<31>; |
| 4502 | APSR.Z = IsZeroBit(result); |
| 4503 | APSR.C = carry; |
| 4504 | // APSR.V unchanged |
| 4505 | #endif |
| 4506 | |
| 4507 | bool success = false; |
| 4508 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4509 | if (!success) |
| 4510 | return false; |
| 4511 | |
| 4512 | if (ConditionPassed()) |
| 4513 | { |
| 4514 | uint32_t Rd, Rn, Rm; |
| 4515 | ARM_ShifterType shift_t; |
| 4516 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 4517 | bool setflags; |
| 4518 | uint32_t carry; |
| 4519 | switch (encoding) |
| 4520 | { |
| 4521 | case eEncodingT1: |
| 4522 | Rd = Rn = Bits32(opcode, 2, 0); |
| 4523 | Rm = Bits32(opcode, 5, 3); |
| 4524 | setflags = !InITBlock(); |
| 4525 | shift_t = SRType_LSL; |
| 4526 | shift_n = 0; |
| 4527 | case eEncodingT2: |
| 4528 | Rd = Bits32(opcode, 11, 8); |
| 4529 | Rn = Bits32(opcode, 19, 16); |
| 4530 | Rm = Bits32(opcode, 3, 0); |
| 4531 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4532 | shift_n = DecodeImmShift(Bits32(opcode, 5, 4), Bits32(opcode, 14, 12)<<2 | Bits32(opcode, 7, 6), shift_t); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4533 | // TODO: Emulate TST (register) |
| 4534 | if (Rd == 15 && setflags) |
| 4535 | return false; |
| 4536 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn) || BadReg(Rm)) |
| 4537 | return false; |
| 4538 | break; |
| 4539 | case eEncodingA1: |
| 4540 | Rd = Bits32(opcode, 15, 12); |
| 4541 | Rn = Bits32(opcode, 19, 16); |
| 4542 | Rm = Bits32(opcode, 3, 0); |
| 4543 | setflags = BitIsSet(opcode, 20); |
| 4544 | shift_n = DecodeImmShift(Bits32(opcode, 6, 5), Bits32(opcode, 11, 7), shift_t); |
| 4545 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4546 | if (Rd == 15 && setflags) |
| 4547 | return false; |
| 4548 | break; |
| 4549 | default: |
| 4550 | return false; |
| 4551 | } |
| 4552 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4553 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4554 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4555 | if (!success) |
| 4556 | return false; |
| 4557 | |
| 4558 | // Read the second operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4559 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4560 | if (!success) |
| 4561 | return false; |
| 4562 | |
| 4563 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 4564 | uint32_t result = val1 & shifted; |
| 4565 | |
| 4566 | EmulateInstruction::Context context; |
| 4567 | context.type = EmulateInstruction::eContextImmediate; |
| 4568 | context.SetNoArgs (); |
| 4569 | |
| 4570 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 4571 | return false; |
| 4572 | } |
| 4573 | return true; |
| 4574 | } |
| 4575 | |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 4576 | // LDR (immediate, ARM) calculates an address from a base register value and an immediate offset, loads a word |
Johnny Chen | e92b27c | 2011-02-18 01:26:39 +0000 | [diff] [blame] | 4577 | // from memory, and writes it to a register. It can use offset, post-indexed, or pre-indexed addressing. |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 4578 | bool |
| 4579 | EmulateInstructionARM::EmulateLDRImmediateARM (ARMEncoding encoding) |
| 4580 | { |
| 4581 | #if 0 |
| 4582 | if ConditionPassed() then |
| 4583 | EncodingSpecificOperations(); |
| 4584 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4585 | address = if index then offset_addr else R[n]; |
| 4586 | data = MemU[address,4]; |
| 4587 | if wback then R[n] = offset_addr; |
| 4588 | if t == 15 then |
| 4589 | if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4590 | elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4591 | R[t] = data; |
| 4592 | else // Can only apply before ARMv7 |
| 4593 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4594 | #endif |
| 4595 | |
| 4596 | bool success = false; |
| 4597 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4598 | if (!success) |
| 4599 | return false; |
| 4600 | |
| 4601 | if (ConditionPassed ()) |
| 4602 | { |
| 4603 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4604 | |
| 4605 | uint32_t t; |
| 4606 | uint32_t n; |
| 4607 | uint32_t imm32; |
| 4608 | bool index; |
| 4609 | bool add; |
| 4610 | bool wback; |
| 4611 | |
| 4612 | switch (encoding) |
| 4613 | { |
| 4614 | case eEncodingA1: |
| 4615 | // if Rn == ’1111’ then SEE LDR (literal); |
| 4616 | // if P == ’0’ && W == ’1’ then SEE LDRT; |
| 4617 | // if Rn == ’1101’ && P == ’0’ && U == ’1’ && W == ’0’ && imm12 == ’000000000100’ then SEE POP; |
| 4618 | // t == UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4619 | t = Bits32 (opcode, 15, 12); |
| 4620 | n = Bits32 (opcode, 19, 16); |
| 4621 | imm32 = Bits32 (opcode, 11, 0); |
| 4622 | |
| 4623 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 4624 | index = BitIsSet (opcode, 24); |
| 4625 | add = BitIsSet (opcode, 23); |
| 4626 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 4627 | |
| 4628 | // if wback && n == t then UNPREDICTABLE; |
| 4629 | if (wback && (n == t)) |
| 4630 | return false; |
| 4631 | |
| 4632 | break; |
| 4633 | |
| 4634 | default: |
| 4635 | return false; |
| 4636 | } |
| 4637 | |
| 4638 | addr_t address; |
| 4639 | addr_t offset_addr; |
| 4640 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4641 | if (!success) |
| 4642 | return false; |
| 4643 | |
| 4644 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4645 | if (add) |
| 4646 | offset_addr = base_address + imm32; |
| 4647 | else |
| 4648 | offset_addr = base_address - imm32; |
| 4649 | |
| 4650 | // address = if index then offset_addr else R[n]; |
| 4651 | if (index) |
| 4652 | address = offset_addr; |
| 4653 | else |
| 4654 | address = base_address; |
| 4655 | |
| 4656 | // data = MemU[address,4]; |
| 4657 | |
| 4658 | Register base_reg; |
| 4659 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4660 | |
| 4661 | EmulateInstruction::Context context; |
| 4662 | context.type = eContextRegisterLoad; |
| 4663 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 4664 | |
| 4665 | uint64_t data = MemURead (context, address, addr_byte_size, 0, &success); |
| 4666 | if (!success) |
| 4667 | return false; |
| 4668 | |
| 4669 | // if wback then R[n] = offset_addr; |
| 4670 | if (wback) |
| 4671 | { |
| 4672 | context.type = eContextAdjustBaseRegister; |
| 4673 | context.SetAddress (offset_addr); |
| 4674 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4675 | return false; |
| 4676 | } |
| 4677 | |
| 4678 | // if t == 15 then |
| 4679 | if (t == 15) |
| 4680 | { |
| 4681 | // if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4682 | if (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 4683 | { |
| 4684 | // LoadWritePC (data); |
| 4685 | context.type = eContextRegisterLoad; |
| 4686 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 4687 | LoadWritePC (context, data); |
| 4688 | } |
| 4689 | else |
| 4690 | return false; |
| 4691 | } |
| 4692 | // elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4693 | else if (UnalignedSupport() || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 4694 | { |
| 4695 | // R[t] = data; |
| 4696 | context.type = eContextRegisterLoad; |
| 4697 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 4698 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 4699 | return false; |
| 4700 | } |
| 4701 | // else // Can only apply before ARMv7 |
| 4702 | else |
| 4703 | { |
| 4704 | // R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4705 | data = ROR (data, Bits32 (address, 1, 0)); |
| 4706 | context.type = eContextRegisterLoad; |
| 4707 | context.SetImmediate (data); |
| 4708 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 4709 | return false; |
| 4710 | } |
| 4711 | |
| 4712 | } |
| 4713 | return true; |
| 4714 | } |
| 4715 | |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 4716 | // LDR (register) calculates an address from a base register value and an offset register value, loads a word |
| 4717 | // from memory, and writes it to a resgister. The offset register value can optionally be shifted. |
| 4718 | bool |
| 4719 | EmulateInstructionARM::EmulateLDRRegister (ARMEncoding encoding) |
| 4720 | { |
| 4721 | #if 0 |
| 4722 | if ConditionPassed() then |
| 4723 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4724 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4725 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4726 | address = if index then offset_addr else R[n]; |
| 4727 | data = MemU[address,4]; |
| 4728 | if wback then R[n] = offset_addr; |
| 4729 | if t == 15 then |
| 4730 | if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4731 | elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4732 | R[t] = data; |
| 4733 | else // Can only apply before ARMv7 |
| 4734 | if CurrentInstrSet() == InstrSet_ARM then |
| 4735 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4736 | else |
| 4737 | R[t] = bits(32) UNKNOWN; |
| 4738 | #endif |
| 4739 | |
| 4740 | bool success = false; |
| 4741 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4742 | if (!success) |
| 4743 | return false; |
| 4744 | |
| 4745 | if (ConditionPassed ()) |
| 4746 | { |
| 4747 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4748 | |
| 4749 | uint32_t t; |
| 4750 | uint32_t n; |
| 4751 | uint32_t m; |
| 4752 | bool index; |
| 4753 | bool add; |
| 4754 | bool wback; |
| 4755 | ARM_ShifterType shift_t; |
| 4756 | uint32_t shift_n; |
| 4757 | |
| 4758 | switch (encoding) |
| 4759 | { |
| 4760 | case eEncodingT1: |
| 4761 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 4762 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4763 | t = Bits32 (opcode, 2, 0); |
| 4764 | n = Bits32 (opcode, 5, 3); |
| 4765 | m = Bits32 (opcode, 8, 6); |
| 4766 | |
| 4767 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4768 | index = true; |
| 4769 | add = true; |
| 4770 | wback = false; |
| 4771 | |
| 4772 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 4773 | shift_t = SRType_LSL; |
| 4774 | shift_n = 0; |
| 4775 | |
| 4776 | break; |
| 4777 | |
| 4778 | case eEncodingT2: |
| 4779 | // if Rn == ’1111’ then SEE LDR (literal); |
| 4780 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4781 | t = Bits32 (opcode, 15, 12); |
| 4782 | n = Bits32 (opcode, 19, 16); |
| 4783 | m = Bits32 (opcode, 3, 0); |
| 4784 | |
| 4785 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4786 | index = true; |
| 4787 | add = true; |
| 4788 | wback = false; |
| 4789 | |
| 4790 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 4791 | shift_t = SRType_LSL; |
| 4792 | shift_n = Bits32 (opcode, 5, 4); |
| 4793 | |
| 4794 | // if BadReg(m) then UNPREDICTABLE; |
| 4795 | if (BadReg (m)) |
| 4796 | return false; |
| 4797 | |
| 4798 | // if t == 15 && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 4799 | if ((t == 15) && InITBlock() && !LastInITBlock()) |
| 4800 | return false; |
| 4801 | |
| 4802 | break; |
| 4803 | |
| 4804 | case eEncodingA1: |
| 4805 | { |
| 4806 | // if P == ’0’ && W == ’1’ then SEE LDRT; |
| 4807 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4808 | t = Bits32 (opcode, 15, 12); |
| 4809 | n = Bits32 (opcode, 19, 16); |
| 4810 | m = Bits32 (opcode, 3, 0); |
| 4811 | |
| 4812 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 4813 | index = BitIsSet (opcode, 24); |
| 4814 | add = BitIsSet (opcode, 23); |
| 4815 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 4816 | |
| 4817 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 4818 | uint32_t type = Bits32 (opcode, 6, 5); |
| 4819 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 4820 | shift_n = DecodeImmShift (type, imm5, shift_t); |
| 4821 | |
| 4822 | // if m == 15 then UNPREDICTABLE; |
| 4823 | if (m == 15) |
| 4824 | return false; |
| 4825 | |
| 4826 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 4827 | if (wback && ((n == 15) || (n == t))) |
| 4828 | return false; |
| 4829 | } |
| 4830 | break; |
| 4831 | |
| 4832 | |
| 4833 | default: |
| 4834 | return false; |
| 4835 | } |
| 4836 | |
| 4837 | uint32_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 4838 | if (!success) |
| 4839 | return false; |
| 4840 | |
| 4841 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4842 | if (!success) |
| 4843 | return false; |
| 4844 | |
| 4845 | addr_t offset_addr; |
| 4846 | addr_t address; |
| 4847 | |
| 4848 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); -- Note "The APSR is an application level alias for the CPSR". |
| 4849 | addr_t offset = Shift (Rm, shift_t, shift_n, Bit32 (m_inst_cpsr, CPSR_C)); |
| 4850 | |
| 4851 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4852 | if (add) |
| 4853 | offset_addr = Rn + offset; |
| 4854 | else |
| 4855 | offset_addr = Rn - offset; |
| 4856 | |
| 4857 | // address = if index then offset_addr else R[n]; |
| 4858 | if (index) |
| 4859 | address = offset_addr; |
| 4860 | else |
| 4861 | address = Rn; |
| 4862 | |
| 4863 | // data = MemU[address,4]; |
| 4864 | Register base_reg; |
| 4865 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4866 | |
| 4867 | EmulateInstruction::Context context; |
| 4868 | context.type = eContextRegisterLoad; |
| 4869 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 4870 | |
| 4871 | uint64_t data = MemURead (context, address, addr_byte_size, 0, &success); |
| 4872 | if (!success) |
| 4873 | return false; |
| 4874 | |
| 4875 | // if wback then R[n] = offset_addr; |
| 4876 | if (wback) |
| 4877 | { |
| 4878 | context.type = eContextAdjustBaseRegister; |
| 4879 | context.SetAddress (offset_addr); |
| 4880 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4881 | return false; |
| 4882 | } |
| 4883 | |
| 4884 | // if t == 15 then |
| 4885 | if (t == 15) |
| 4886 | { |
| 4887 | // if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4888 | if (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 4889 | { |
| 4890 | context.type = eContextRegisterLoad; |
| 4891 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 4892 | LoadWritePC (context, data); |
| 4893 | } |
| 4894 | else |
| 4895 | return false; |
| 4896 | } |
| 4897 | // elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4898 | else if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 4899 | { |
| 4900 | // R[t] = data; |
| 4901 | context.type = eContextRegisterLoad; |
| 4902 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 4903 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 4904 | return false; |
| 4905 | } |
| 4906 | else // Can only apply before ARMv7 |
| 4907 | { |
| 4908 | // if CurrentInstrSet() == InstrSet_ARM then |
| 4909 | if (CurrentInstrSet () == eModeARM) |
| 4910 | { |
| 4911 | // R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4912 | data = ROR (data, Bits32 (address, 1, 0)); |
| 4913 | context.type = eContextRegisterLoad; |
| 4914 | context.SetImmediate (data); |
| 4915 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 4916 | return false; |
| 4917 | } |
| 4918 | else |
| 4919 | { |
| 4920 | // R[t] = bits(32) UNKNOWN; |
| 4921 | WriteBits32Unknown (t); |
| 4922 | } |
| 4923 | } |
| 4924 | } |
| 4925 | return true; |
| 4926 | } |
Caroline Tice | 21b604b | 2011-02-18 21:06:04 +0000 | [diff] [blame] | 4927 | |
| 4928 | // LDRB (immediate, Thumb) |
| 4929 | bool |
| 4930 | EmulateInstructionARM::EmulateLDRBImmediate (ARMEncoding encoding) |
| 4931 | { |
| 4932 | #if 0 |
| 4933 | if ConditionPassed() then |
| 4934 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4935 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4936 | address = if index then offset_addr else R[n]; |
| 4937 | R[t] = ZeroExtend(MemU[address,1], 32); |
| 4938 | if wback then R[n] = offset_addr; |
| 4939 | #endif |
| 4940 | |
| 4941 | bool success = false; |
| 4942 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4943 | if (!success) |
| 4944 | return false; |
| 4945 | |
| 4946 | if (ConditionPassed ()) |
| 4947 | { |
| 4948 | uint32_t t; |
| 4949 | uint32_t n; |
| 4950 | uint32_t imm32; |
| 4951 | bool index; |
| 4952 | bool add; |
| 4953 | bool wback; |
| 4954 | |
| 4955 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4956 | switch (encoding) |
| 4957 | { |
| 4958 | case eEncodingT1: |
| 4959 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5, 32); |
| 4960 | t = Bits32 (opcode, 2, 0); |
| 4961 | n = Bits32 (opcode, 5, 3); |
| 4962 | imm32 = Bits32 (opcode, 10, 6); |
| 4963 | |
| 4964 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4965 | index = true; |
| 4966 | add = true; |
| 4967 | wback= false; |
| 4968 | |
| 4969 | break; |
| 4970 | |
| 4971 | case eEncodingT2: |
| 4972 | // if Rt == ’1111’ then SEE PLD; |
| 4973 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 4974 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4975 | t = Bits32 (opcode, 15, 12); |
| 4976 | n = Bits32 (opcode, 19, 16); |
| 4977 | imm32 = Bits32 (opcode, 11, 0); |
| 4978 | |
| 4979 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4980 | index = true; |
| 4981 | add = true; |
| 4982 | wback = false; |
| 4983 | |
| 4984 | // if t == 13 then UNPREDICTABLE; |
| 4985 | if (t == 13) |
| 4986 | return false; |
| 4987 | |
| 4988 | break; |
| 4989 | |
| 4990 | case eEncodingT3: |
| 4991 | // if Rt == ’1111’ && P == ’1’ && U == ’0’ && W == ’0’ then SEE PLD; |
| 4992 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 4993 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE LDRBT; |
| 4994 | // if P == ’0’ && W == ’0’ then UNDEFINED; |
| 4995 | if (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)) |
| 4996 | return false; |
| 4997 | |
| 4998 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 4999 | t = Bits32 (opcode, 15, 12); |
| 5000 | n = Bits32 (opcode, 19, 16); |
| 5001 | imm32 = Bits32 (opcode, 7, 0); |
| 5002 | |
| 5003 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 5004 | index = BitIsSet (opcode, 10); |
| 5005 | add = BitIsSet (opcode, 9); |
| 5006 | wback = BitIsSet (opcode, 8); |
| 5007 | |
| 5008 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE; |
| 5009 | if (BadReg (t) || (wback && (n == t))) |
| 5010 | return false; |
| 5011 | |
| 5012 | break; |
| 5013 | |
| 5014 | default: |
| 5015 | return false; |
| 5016 | } |
| 5017 | |
| 5018 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5019 | if (!success) |
| 5020 | return false; |
| 5021 | |
| 5022 | addr_t address; |
| 5023 | addr_t offset_addr; |
| 5024 | |
| 5025 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5026 | if (add) |
| 5027 | offset_addr = Rn + imm32; |
| 5028 | else |
| 5029 | offset_addr = Rn - imm32; |
| 5030 | |
| 5031 | // address = if index then offset_addr else R[n]; |
| 5032 | if (index) |
| 5033 | address = offset_addr; |
| 5034 | else |
| 5035 | address = Rn; |
| 5036 | |
| 5037 | // R[t] = ZeroExtend(MemU[address,1], 32); |
| 5038 | Register base_reg; |
| 5039 | Register data_reg; |
| 5040 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5041 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 5042 | |
| 5043 | EmulateInstruction::Context context; |
| 5044 | context.type = eContextRegisterLoad; |
| 5045 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - Rn); |
| 5046 | |
| 5047 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5048 | if (!success) |
| 5049 | return false; |
| 5050 | |
| 5051 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5052 | return false; |
| 5053 | |
| 5054 | // if wback then R[n] = offset_addr; |
| 5055 | if (wback) |
| 5056 | { |
| 5057 | context.type = eContextAdjustBaseRegister; |
| 5058 | context.SetAddress (offset_addr); |
| 5059 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5060 | return false; |
| 5061 | } |
| 5062 | } |
| 5063 | return true; |
| 5064 | } |
Caroline Tice | f55261f | 2011-02-18 22:24:22 +0000 | [diff] [blame] | 5065 | |
| 5066 | // LDRB (literal) calculates an address from the PC value and an immediate offset, loads a byte from memory, |
| 5067 | // zero-extends it to form a 32-bit word and writes it to a register. |
| 5068 | bool |
| 5069 | EmulateInstructionARM::EmulateLDRBLiteral (ARMEncoding encoding) |
| 5070 | { |
| 5071 | #if 0 |
| 5072 | if ConditionPassed() then |
| 5073 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 5074 | base = Align(PC,4); |
| 5075 | address = if add then (base + imm32) else (base - imm32); |
| 5076 | R[t] = ZeroExtend(MemU[address,1], 32); |
| 5077 | #endif |
| 5078 | |
| 5079 | bool success = false; |
| 5080 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5081 | if (!success) |
| 5082 | return false; |
| 5083 | |
| 5084 | if (ConditionPassed ()) |
| 5085 | { |
| 5086 | uint32_t t; |
| 5087 | uint32_t imm32; |
| 5088 | bool add; |
| 5089 | switch (encoding) |
| 5090 | { |
| 5091 | case eEncodingT1: |
| 5092 | // if Rt == ’1111’ then SEE PLD; |
| 5093 | // t = UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 5094 | t = Bits32 (opcode, 15, 12); |
| 5095 | imm32 = Bits32 (opcode, 11, 0); |
| 5096 | add = BitIsSet (opcode, 23); |
| 5097 | |
| 5098 | // if t == 13 then UNPREDICTABLE; |
| 5099 | if (t == 13) |
| 5100 | return false; |
| 5101 | |
| 5102 | break; |
| 5103 | |
| 5104 | case eEncodingA1: |
| 5105 | // t == UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 5106 | t = Bits32 (opcode, 15, 12); |
| 5107 | imm32 = Bits32 (opcode, 11, 0); |
| 5108 | add = BitIsSet (opcode, 23); |
| 5109 | |
| 5110 | // if t == 15 then UNPREDICTABLE; |
| 5111 | if (t == 15) |
| 5112 | return false; |
| 5113 | break; |
| 5114 | |
| 5115 | default: |
| 5116 | return false; |
| 5117 | } |
| 5118 | |
| 5119 | // base = Align(PC,4); |
| 5120 | uint32_t pc_val = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 5121 | if (!success) |
| 5122 | return false; |
| 5123 | |
| 5124 | uint32_t base = AlignPC (pc_val); |
| 5125 | |
| 5126 | addr_t address; |
| 5127 | // address = if add then (base + imm32) else (base - imm32); |
| 5128 | if (add) |
| 5129 | address = base + imm32; |
| 5130 | else |
| 5131 | address = base - imm32; |
| 5132 | |
| 5133 | // R[t] = ZeroExtend(MemU[address,1], 32); |
| 5134 | EmulateInstruction::Context context; |
| 5135 | context.type = eContextRelativeBranchImmediate; |
| 5136 | context.SetImmediate (address - base); |
| 5137 | |
| 5138 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5139 | if (!success) |
| 5140 | return false; |
| 5141 | |
| 5142 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5143 | return false; |
| 5144 | } |
| 5145 | return true; |
| 5146 | } |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame^] | 5147 | |
| 5148 | // LDRB (register) calculates an address from a base register value and an offset rigister value, loads a byte from |
| 5149 | // memory, zero-extends it to form a 32-bit word, and writes it to a register. The offset register value can |
| 5150 | // optionally be shifted. |
| 5151 | bool |
| 5152 | EmulateInstructionARM::EmulateLDRBRegister (ARMEncoding encoding) |
| 5153 | { |
| 5154 | #if 0 |
| 5155 | if ConditionPassed() then |
| 5156 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5157 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5158 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5159 | address = if index then offset_addr else R[n]; |
| 5160 | R[t] = ZeroExtend(MemU[address,1],32); |
| 5161 | if wback then R[n] = offset_addr; |
| 5162 | #endif |
| 5163 | |
| 5164 | bool success = false; |
| 5165 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5166 | if (!success) |
| 5167 | return false; |
| 5168 | |
| 5169 | if (ConditionPassed ()) |
| 5170 | { |
| 5171 | uint32_t t; |
| 5172 | uint32_t n; |
| 5173 | uint32_t m; |
| 5174 | bool index; |
| 5175 | bool add; |
| 5176 | bool wback; |
| 5177 | ARM_ShifterType shift_t; |
| 5178 | uint32_t shift_n; |
| 5179 | |
| 5180 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5181 | switch (encoding) |
| 5182 | { |
| 5183 | case eEncodingT1: |
| 5184 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5185 | t = Bits32 (opcode, 2, 0); |
| 5186 | n = Bits32 (opcode, 5, 3); |
| 5187 | m = Bits32 (opcode, 8, 6); |
| 5188 | |
| 5189 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5190 | index = true; |
| 5191 | add = true; |
| 5192 | wback = false; |
| 5193 | |
| 5194 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 5195 | shift_t = SRType_LSL; |
| 5196 | shift_n = 0; |
| 5197 | break; |
| 5198 | |
| 5199 | case eEncodingT2: |
| 5200 | // if Rt == ’1111’ then SEE PLD; |
| 5201 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 5202 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5203 | t = Bits32 (opcode, 15, 12); |
| 5204 | n = Bits32 (opcode, 19, 16); |
| 5205 | m = Bits32 (opcode, 3, 0); |
| 5206 | |
| 5207 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5208 | index = true; |
| 5209 | add = true; |
| 5210 | wback = false; |
| 5211 | |
| 5212 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 5213 | shift_t = SRType_LSL; |
| 5214 | shift_n = Bits32 (opcode, 5, 4); |
| 5215 | |
| 5216 | // if t == 13 || BadReg(m) then UNPREDICTABLE; |
| 5217 | if ((t == 13) || BadReg (m)) |
| 5218 | return false; |
| 5219 | break; |
| 5220 | |
| 5221 | case eEncodingA1: |
| 5222 | { |
| 5223 | // if P == ’0’ && W == ’1’ then SEE LDRBT; |
| 5224 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5225 | t = Bits32 (opcode, 15, 12); |
| 5226 | n = Bits32 (opcode, 19, 16); |
| 5227 | m = Bits32 (opcode, 3, 0); |
| 5228 | |
| 5229 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 5230 | index = BitIsSet (opcode, 24); |
| 5231 | add = BitIsSet (opcode, 23); |
| 5232 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 5233 | |
| 5234 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 5235 | uint32_t type = Bits32 (opcode, 6, 5); |
| 5236 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 5237 | shift_n = DecodeImmShift (type, imm5, shift_t); |
| 5238 | |
| 5239 | // if t == 15 || m == 15 then UNPREDICTABLE; |
| 5240 | if ((t == 15) || (m == 15)) |
| 5241 | return false; |
| 5242 | |
| 5243 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 5244 | if (wback && ((n == 15) || (n == t))) |
| 5245 | return false; |
| 5246 | } |
| 5247 | break; |
| 5248 | |
| 5249 | default: |
| 5250 | return false; |
| 5251 | } |
| 5252 | |
| 5253 | addr_t offset_addr; |
| 5254 | addr_t address; |
| 5255 | |
| 5256 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5257 | uint32_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 5258 | if (!success) |
| 5259 | return false; |
| 5260 | |
| 5261 | addr_t offset = Shift (Rm, shift_t, shift_n, APSR_C); |
| 5262 | |
| 5263 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5264 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5265 | if (!success) |
| 5266 | return false; |
| 5267 | |
| 5268 | if (add) |
| 5269 | offset_addr = Rn + offset; |
| 5270 | else |
| 5271 | offset_addr = Rn - offset; |
| 5272 | |
| 5273 | // address = if index then offset_addr else R[n]; |
| 5274 | if (index) |
| 5275 | address = offset_addr; |
| 5276 | else |
| 5277 | address = Rn; |
| 5278 | |
| 5279 | // R[t] = ZeroExtend(MemU[address,1],32); |
| 5280 | Register base_reg; |
| 5281 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5282 | |
| 5283 | EmulateInstruction::Context context; |
| 5284 | context.type = eContextRegisterLoad; |
| 5285 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5286 | |
| 5287 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5288 | if (!success) |
| 5289 | return false; |
| 5290 | |
| 5291 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5292 | return false; |
| 5293 | |
| 5294 | // if wback then R[n] = offset_addr; |
| 5295 | if (wback) |
| 5296 | { |
| 5297 | context.type = eContextAdjustBaseRegister; |
| 5298 | context.SetAddress (offset_addr); |
| 5299 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5300 | return false; |
| 5301 | } |
| 5302 | } |
| 5303 | return true; |
| 5304 | } |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5305 | |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5306 | // Bitwise OR (immediate) performs a bitwise (inclusive) OR of a register value and an immediate value, and |
| 5307 | // writes the result to the destination register. It can optionally update the condition flags based |
| 5308 | // on the result. |
| 5309 | bool |
| 5310 | EmulateInstructionARM::EmulateORRImm (ARMEncoding encoding) |
| 5311 | { |
| 5312 | #if 0 |
| 5313 | // ARM pseudo code... |
| 5314 | if ConditionPassed() then |
| 5315 | EncodingSpecificOperations(); |
| 5316 | result = R[n] OR imm32; |
| 5317 | if d == 15 then // Can only occur for ARM encoding |
| 5318 | ALUWritePC(result); // setflags is always FALSE here |
| 5319 | else |
| 5320 | R[d] = result; |
| 5321 | if setflags then |
| 5322 | APSR.N = result<31>; |
| 5323 | APSR.Z = IsZeroBit(result); |
| 5324 | APSR.C = carry; |
| 5325 | // APSR.V unchanged |
| 5326 | #endif |
| 5327 | |
| 5328 | bool success = false; |
| 5329 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5330 | if (!success) |
| 5331 | return false; |
| 5332 | |
| 5333 | if (ConditionPassed()) |
| 5334 | { |
| 5335 | uint32_t Rd, Rn; |
| 5336 | uint32_t imm32; // the immediate value to be ORed to the value obtained from Rn |
| 5337 | bool setflags; |
| 5338 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 5339 | switch (encoding) |
| 5340 | { |
| 5341 | case eEncodingT1: |
| 5342 | Rd = Bits32(opcode, 11, 8); |
| 5343 | Rn = Bits32(opcode, 19, 16); |
| 5344 | setflags = BitIsSet(opcode, 20); |
| 5345 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 5346 | // if Rn == ‘1111’ then SEE MOV (immediate); |
| 5347 | if (Rn == 15) |
| 5348 | return EmulateMOVRdImm(eEncodingT2); |
| 5349 | if (BadReg(Rd) || Rn == 13) |
| 5350 | return false; |
| 5351 | break; |
| 5352 | case eEncodingA1: |
| 5353 | Rd = Bits32(opcode, 15, 12); |
| 5354 | Rn = Bits32(opcode, 19, 16); |
| 5355 | setflags = BitIsSet(opcode, 20); |
| 5356 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 5357 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5358 | if (Rd == 15 && setflags) |
| 5359 | return false; |
| 5360 | break; |
| 5361 | default: |
| 5362 | return false; |
| 5363 | } |
| 5364 | |
| 5365 | // Read the first operand. |
| 5366 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5367 | if (!success) |
| 5368 | return false; |
| 5369 | |
| 5370 | uint32_t result = val1 | imm32; |
| 5371 | |
| 5372 | EmulateInstruction::Context context; |
| 5373 | context.type = EmulateInstruction::eContextImmediate; |
| 5374 | context.SetNoArgs (); |
| 5375 | |
| 5376 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5377 | return false; |
| 5378 | } |
| 5379 | return true; |
| 5380 | } |
| 5381 | |
| 5382 | // Bitwise OR (register) performs a bitwise (inclusive) OR of a register value and an optionally-shifted register |
| 5383 | // value, and writes the result to the destination register. It can optionally update the condition flags based |
| 5384 | // on the result. |
| 5385 | bool |
| 5386 | EmulateInstructionARM::EmulateORRReg (ARMEncoding encoding) |
| 5387 | { |
| 5388 | #if 0 |
| 5389 | // ARM pseudo code... |
| 5390 | if ConditionPassed() then |
| 5391 | EncodingSpecificOperations(); |
| 5392 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 5393 | result = R[n] OR shifted; |
| 5394 | if d == 15 then // Can only occur for ARM encoding |
| 5395 | ALUWritePC(result); // setflags is always FALSE here |
| 5396 | else |
| 5397 | R[d] = result; |
| 5398 | if setflags then |
| 5399 | APSR.N = result<31>; |
| 5400 | APSR.Z = IsZeroBit(result); |
| 5401 | APSR.C = carry; |
| 5402 | // APSR.V unchanged |
| 5403 | #endif |
| 5404 | |
| 5405 | bool success = false; |
| 5406 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5407 | if (!success) |
| 5408 | return false; |
| 5409 | |
| 5410 | if (ConditionPassed()) |
| 5411 | { |
| 5412 | uint32_t Rd, Rn, Rm; |
| 5413 | ARM_ShifterType shift_t; |
| 5414 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 5415 | bool setflags; |
| 5416 | uint32_t carry; |
| 5417 | switch (encoding) |
| 5418 | { |
| 5419 | case eEncodingT1: |
| 5420 | Rd = Rn = Bits32(opcode, 2, 0); |
| 5421 | Rm = Bits32(opcode, 5, 3); |
| 5422 | setflags = !InITBlock(); |
| 5423 | shift_t = SRType_LSL; |
| 5424 | shift_n = 0; |
| 5425 | case eEncodingT2: |
| 5426 | Rd = Bits32(opcode, 11, 8); |
| 5427 | Rn = Bits32(opcode, 19, 16); |
| 5428 | Rm = Bits32(opcode, 3, 0); |
| 5429 | setflags = BitIsSet(opcode, 20); |
| 5430 | shift_n = DecodeImmShift(Bits32(opcode, 5, 4), Bits32(opcode, 14, 12)<<2 | Bits32(opcode, 7, 6), shift_t); |
| 5431 | // if Rn == ‘1111’ then SEE MOV (register); |
| 5432 | if (Rn == 15) |
| 5433 | return EmulateMOVRdRm(eEncodingT3); |
| 5434 | if (BadReg(Rd) || Rn == 13 || BadReg(Rm)) |
| 5435 | return false; |
| 5436 | break; |
| 5437 | case eEncodingA1: |
| 5438 | Rd = Bits32(opcode, 15, 12); |
| 5439 | Rn = Bits32(opcode, 19, 16); |
| 5440 | Rm = Bits32(opcode, 3, 0); |
| 5441 | setflags = BitIsSet(opcode, 20); |
| 5442 | shift_n = DecodeImmShift(Bits32(opcode, 6, 5), Bits32(opcode, 11, 7), shift_t); |
| 5443 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5444 | if (Rd == 15 && setflags) |
| 5445 | return false; |
| 5446 | break; |
| 5447 | default: |
| 5448 | return false; |
| 5449 | } |
| 5450 | |
| 5451 | // Read the first operand. |
| 5452 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5453 | if (!success) |
| 5454 | return false; |
| 5455 | |
| 5456 | // Read the second operand. |
| 5457 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 5458 | if (!success) |
| 5459 | return false; |
| 5460 | |
| 5461 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 5462 | uint32_t result = val1 & shifted; |
| 5463 | |
| 5464 | EmulateInstruction::Context context; |
| 5465 | context.type = EmulateInstruction::eContextImmediate; |
| 5466 | context.SetNoArgs (); |
| 5467 | |
| 5468 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5469 | return false; |
| 5470 | } |
| 5471 | return true; |
| 5472 | } |
| 5473 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5474 | EmulateInstructionARM::ARMOpcode* |
| 5475 | EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5476 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5477 | static ARMOpcode |
| 5478 | g_arm_opcodes[] = |
| 5479 | { |
| 5480 | //---------------------------------------------------------------------- |
| 5481 | // Prologue instructions |
| 5482 | //---------------------------------------------------------------------- |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 5483 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5484 | // push register(s) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5485 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePUSH, "push <registers>" }, |
| 5486 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePUSH, "push <register>" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 5487 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5488 | // set r7 to point to a stack offset |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5489 | { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDRdSPImm, "add r7, sp, #<const>" }, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 5490 | { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBR7IPImm, "sub r7, ip, #<const>"}, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 5491 | // copy the stack pointer to ip |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5492 | { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMOVRdSP, "mov ip, sp" }, |
| 5493 | { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDRdSPImm, "add ip, sp, #<const>" }, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 5494 | { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBIPSPImm, "sub ip, sp, #<const>"}, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 5495 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5496 | // adjust the stack pointer |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 5497 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 5498 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5499 | // push one register |
| 5500 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 5501 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" }, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 5502 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5503 | // vector push consecutive extension register(s) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 5504 | { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 5505 | { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 5506 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5507 | //---------------------------------------------------------------------- |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 5508 | // Epilogue instructions |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5509 | //---------------------------------------------------------------------- |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 5510 | |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5511 | { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePOP, "pop <registers>"}, |
| 5512 | { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePOP, "pop <register>"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 5513 | { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 5514 | { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
| 5515 | |
| 5516 | //---------------------------------------------------------------------- |
| 5517 | // Supervisor Call (previously Software Interrupt) |
| 5518 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 5519 | { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"}, |
| 5520 | |
| 5521 | //---------------------------------------------------------------------- |
| 5522 | // Branch instructions |
| 5523 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5524 | { 0x0f000000, 0x0a000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "b #imm24"}, |
Johnny Chen | 383d629 | 2011-02-11 21:23:32 +0000 | [diff] [blame] | 5525 | // To resolve ambiguity, "blx <label>" should come before "bl <label>". |
| 5526 | { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 5527 | { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 5528 | { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 5529 | // for example, "bx lr" |
| 5530 | { 0x0ffffff0, 0x012fff10, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 5531 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5532 | //---------------------------------------------------------------------- |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 5533 | // Data-processing instructions |
| 5534 | //---------------------------------------------------------------------- |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5535 | // adc (immediate) |
| 5536 | { 0x0fe00000, 0x02a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADCImm, "adc{s}<c> <Rd>, <Rn>, #const"}, |
| 5537 | // adc (register) |
| 5538 | { 0x0fe00010, 0x00a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADCReg, "adc{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 5539 | // add (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5540 | { 0x0fe00000, 0x02800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDImmARM, "add{s}<c> <Rd>, <Rn>, #const"}, |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 5541 | // add (register) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5542 | { 0x0fe00010, 0x00800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDReg, "add{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5543 | // and (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5544 | { 0x0fe00000, 0x02000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateANDImm, "and{s}<c> <Rd>, <Rn>, #const"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5545 | // and (register) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5546 | { 0x0fe00010, 0x00000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateANDReg, "and{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5547 | // orr (immediate) |
| 5548 | { 0x0fe00000, 0x03800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateORRImm, "orr{s}<c> <Rd>, <Rn>, #const"}, |
| 5549 | // orr (register) |
| 5550 | { 0x0fe00010, 0x01800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateORRReg, "orr{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 5551 | // move bitwise not |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5552 | { 0x0fef0000, 0x03e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMVNRdImm, "mvn{s}<c> <Rd>, #<const>"}, |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 5553 | // asr (immediate) |
| 5554 | { 0x0fef0070, 0x01a00040, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateASRImm, "asr{s}<c> <Rd>, <Rm>, #imm"}, |
Johnny Chen | 2ee35bc | 2011-02-16 19:27:43 +0000 | [diff] [blame] | 5555 | // asr (register) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 5556 | { 0x0fef00f0, 0x01a00050, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateASRReg, "asr{s}<c> <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 2ee35bc | 2011-02-16 19:27:43 +0000 | [diff] [blame] | 5557 | // lsl (immediate) |
| 5558 | { 0x0fef0070, 0x01a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSLImm, "lsl{s}<c> <Rd>, <Rm>, #imm"}, |
| 5559 | // lsl (register) |
| 5560 | { 0x0fef00f0, 0x01a00010, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSLReg, "lsl{s}<c> <Rd>, <Rn>, <Rm>"}, |
| 5561 | // lsr (immediate) |
| 5562 | { 0x0fef0070, 0x01a00020, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSRImm, "lsr{s}<c> <Rd>, <Rm>, #imm"}, |
| 5563 | // lsr (register) |
| 5564 | { 0x0fef00f0, 0x01a00050, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSRReg, "lsr{s}<c> <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 5565 | // rrx is a special case encoding of ror (immediate) |
| 5566 | { 0x0fef0ff0, 0x01a00060, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRRX, "rrx{s}<c> <Rd>, <Rm>"}, |
| 5567 | // ror (immediate) |
| 5568 | { 0x0fef0070, 0x01a00060, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRORImm, "ror{s}<c> <Rd>, <Rm>, #imm"}, |
| 5569 | // ror (register) |
| 5570 | { 0x0fef00f0, 0x01a00070, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRORReg, "ror{s}<c> <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 5571 | |
| 5572 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5573 | // Load instructions |
| 5574 | //---------------------------------------------------------------------- |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 5575 | { 0x0fd00000, 0x08900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 5576 | { 0x0fd00000, 0x08100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDA, "ldmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 5577 | { 0x0fd00000, 0x09100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 5578 | { 0x0fd00000, 0x09900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMIB, "ldmib<c> <Rn<{!} <registers>" }, |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 5579 | { 0x0e500000, 0x04100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRImmediateARM, "ldr<c> <Rt> [<Rn> {#+/-<imm12>}]" }, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5580 | { 0x0e500010, 0x06100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRRegister, "ldr<c> <Rt> [<Rn> +/-<Rm> {<shift>}] {!}" }, |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame^] | 5581 | { 0x0e5f0000, 0x045f0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRBLiteral, "ldrb<c> <Rt>, [...]"}, |
| 5582 | { 0xfe500010, 0x06500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c> <Rt>, [<Rn>,+/-<Rm>{, <shift>}]{!}" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 5583 | |
| 5584 | //---------------------------------------------------------------------- |
| 5585 | // Store instructions |
| 5586 | //---------------------------------------------------------------------- |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 5587 | { 0x0fd00000, 0x08800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" }, |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 5588 | { 0x0fd00000, 0x08000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDA, "stmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 5589 | { 0x0fd00000, 0x09000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 5590 | { 0x0fd00000, 0x09800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMIB, "stmib<c> <Rn>{!} <registers>" }, |
| 5591 | { 0x0e500010, 0x06000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRegister, "str<c> <Rt> [<Rn> +/-<Rm> {<shift>}]{!}" } |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 5592 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5593 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5594 | }; |
| 5595 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 5596 | |
| 5597 | for (size_t i=0; i<k_num_arm_opcodes; ++i) |
| 5598 | { |
| 5599 | if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value) |
| 5600 | return &g_arm_opcodes[i]; |
| 5601 | } |
| 5602 | return NULL; |
| 5603 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5604 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5605 | |
| 5606 | EmulateInstructionARM::ARMOpcode* |
| 5607 | EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode) |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 5608 | { |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 5609 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5610 | static ARMOpcode |
| 5611 | g_thumb_opcodes[] = |
| 5612 | { |
| 5613 | //---------------------------------------------------------------------- |
| 5614 | // Prologue instructions |
| 5615 | //---------------------------------------------------------------------- |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 5616 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5617 | // push register(s) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5618 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePUSH, "push <registers>" }, |
| 5619 | { 0xffff0000, 0xe92d0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePUSH, "push.w <registers>" }, |
| 5620 | { 0xffff0fff, 0xf84d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePUSH, "push.w <register>" }, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 5621 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5622 | // set r7 to point to a stack offset |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5623 | { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADDRdSPImm, "add r7, sp, #imm" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 5624 | // copy the stack pointer to r7 |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5625 | { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdSP, "mov r7, sp" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 5626 | // move from high register to low register (comes after "mov r7, sp" to resolve ambiguity) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5627 | { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVLowHigh, "mov r0-r7, r8-r15" }, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 5628 | |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 5629 | // PC-relative load into register (see also EmulateADDSPRm) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 5630 | { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr <Rt>, [PC, #imm]"}, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 5631 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5632 | // adjust the stack pointer |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 5633 | { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDSPRm, "add sp, <Rm>"}, |
| 5634 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSUBSPImm, "add sp, sp, #imm"}, |
| 5635 | { 0xfbef8f00, 0xf1ad0d00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub.w sp, sp, #<const>"}, |
| 5636 | { 0xfbff8f00, 0xf2ad0d00, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "subw sp, sp, #imm12"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 5637 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5638 | // vector push consecutive extension register(s) |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 5639 | { 0xffbf0f00, 0xed2d0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 5640 | { 0xffbf0f00, 0xed2d0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 5641 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5642 | //---------------------------------------------------------------------- |
| 5643 | // Epilogue instructions |
| 5644 | //---------------------------------------------------------------------- |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 5645 | |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 5646 | { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDSPImm, "add sp, #imm"}, |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5647 | { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePOP, "pop <registers>"}, |
| 5648 | { 0xffff0000, 0xe8bd0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePOP, "pop.w <registers>" }, |
| 5649 | { 0xffff0fff, 0xf85d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePOP, "pop.w <register>" }, |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 5650 | { 0xffbf0f00, 0xecbd0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
| 5651 | { 0xffbf0f00, 0xecbd0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 5652 | |
| 5653 | //---------------------------------------------------------------------- |
| 5654 | // Supervisor Call (previously Software Interrupt) |
| 5655 | //---------------------------------------------------------------------- |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 5656 | { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"}, |
| 5657 | |
| 5658 | //---------------------------------------------------------------------- |
| 5659 | // If Then makes up to four following instructions conditional. |
| 5660 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 5661 | { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"}, |
| 5662 | |
| 5663 | //---------------------------------------------------------------------- |
| 5664 | // Branch instructions |
| 5665 | //---------------------------------------------------------------------- |
| 5666 | // To resolve ambiguity, "b<c> #imm8" should come after "svc #imm8". |
| 5667 | { 0xfffff000, 0x0000d000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm8 (outside IT)"}, |
| 5668 | { 0xffff8000, 0x0000e000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateB, "b #imm11 (outside or last in IT)"}, |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5669 | { 0xf800d000, 0xf0008000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside IT)"}, |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5670 | { 0xf800d000, 0xf0009000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateB, "b.w #imm8 (outside or last in IT)"}, |
Johnny Chen | 383d629 | 2011-02-11 21:23:32 +0000 | [diff] [blame] | 5671 | // J1 == J2 == 1 |
| 5672 | { 0xf800f800, 0xf000f800, ARMV4T_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 5673 | // J1 == J2 == 1 |
| 5674 | { 0xf800e800, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 5675 | { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 5676 | // for example, "bx lr" |
| 5677 | { 0xffffff87, 0x00004700, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"}, |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 5678 | // compare and branch |
| 5679 | { 0xfffff500, 0x0000b100, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCB, "cb{n}z <Rn>, <label>"}, |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 5680 | // table branch byte |
| 5681 | { 0xfff0fff0, 0xe8d0f000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTB, "tbb<c> <Rn>, <Rm>"}, |
| 5682 | // table branch halfword |
| 5683 | { 0xfff0fff0, 0xe8d0f010, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTB, "tbh<c> <Rn>, <Rm>, lsl #1"}, |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5684 | |
| 5685 | //---------------------------------------------------------------------- |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 5686 | // Data-processing instructions |
| 5687 | //---------------------------------------------------------------------- |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5688 | // adc (immediate) |
| 5689 | { 0xfbe08000, 0xf1400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateADCImm, "adc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 5690 | // adc (register) |
| 5691 | { 0xffffffc0, 0x00004140, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADCReg, "adcs|adc<c> <Rdn>, <Rm>"}, |
| 5692 | { 0xffe08000, 0xeb400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateADCReg, "adc{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
| 5693 | // add (register) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5694 | { 0xfffffe00, 0x00001800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADDReg, "adds|add<c> <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 5695 | // Make sure "add sp, <Rm>" comes before this instruction, so there's no ambiguity decoding the two. |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5696 | { 0xffffff00, 0x00004400, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDReg, "add<c> <Rdn>, <Rm>"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5697 | // and (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5698 | { 0xfbe08000, 0xf0000000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateANDImm, "and{s}<c> <Rd>, <Rn>, #<const>"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5699 | // and (register) |
| 5700 | { 0xffffffc0, 0x00004000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateANDReg, "ands|and<c> <Rdn>, <Rm>"}, |
| 5701 | { 0xffe08000, 0xea000000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateANDReg, "and{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5702 | // orr (immediate) |
| 5703 | { 0xfbe08000, 0xf0400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateORRImm, "orr{s}<c> <Rd>, <Rn>, #<const>"}, |
| 5704 | // orr (register) |
| 5705 | { 0xffffffc0, 0x00004300, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateORRReg, "orrs|orr<c> <Rdn>, <Rm>"}, |
| 5706 | { 0xffe08000, 0xea400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateORRReg, "orr{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
| 5707 | |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 5708 | // move from high register to high register |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5709 | { 0xffffff00, 0x00004600, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdRm, "mov<c> <Rd>, <Rm>"}, |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 5710 | // move from low register to low register |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5711 | { 0xffffffc0, 0x00000000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateMOVRdRm, "movs <Rd>, <Rm>"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5712 | // mov{s}<c>.w <Rd>, <Rm> |
| 5713 | { 0xffeff0f0, 0xea4f0000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateMOVRdRm, "mov{s}<c>.w <Rd>, <Rm>"}, |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 5714 | // move immediate |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5715 | { 0xfffff800, 0x00002000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdImm, "movs|mov<c> <Rd>, #imm8"}, |
| 5716 | { 0xfbef8000, 0xf04f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateMOVRdImm, "mov{s}<c>.w <Rd>, #<const>"}, |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 5717 | // move bitwise not |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5718 | { 0xfbef8000, 0xf06f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateMVNRdImm, "mvn{s} <Rd>, #<const>"}, |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 5719 | // compare a register with immediate |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5720 | { 0xfffff800, 0x00002800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMPRnImm, "cmp<c> <Rn>, #imm8"}, |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 5721 | // compare Rn with Rm (Rn and Rm both from r0-r7) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5722 | { 0xffffffc0, 0x00004280, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMPRnRm, "cmp<c> <Rn>, <Rm>"}, |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 5723 | // compare Rn with Rm (Rn and Rm not both from r0-r7) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 5724 | { 0xffffff00, 0x00004500, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateCMPRnRm, "cmp<c> <Rn>, <Rm>"}, |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 5725 | // asr (immediate) |
| 5726 | { 0xfffff800, 0x00001000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateASRImm, "asrs|asr<c> <Rd>, <Rm>, #imm"}, |
Johnny Chen | 4d896db | 2011-02-15 20:14:02 +0000 | [diff] [blame] | 5727 | { 0xffef8030, 0xea4f0020, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateASRImm, "asr{s}<c>.w <Rd>, <Rm>, #imm"}, |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 5728 | // asr (register) |
| 5729 | { 0xffffffc0, 0x00004100, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateASRReg, "asrs|asr<c> <Rdn>, <Rm>"}, |
| 5730 | { 0xffe0f0f0, 0xfa40f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateASRReg, "asr{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 2ee35bc | 2011-02-16 19:27:43 +0000 | [diff] [blame] | 5731 | // lsl (immediate) |
| 5732 | { 0xfffff800, 0x00000000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSLImm, "lsls|lsl<c> <Rd>, <Rm>, #imm"}, |
| 5733 | { 0xffef8030, 0xea4f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSLImm, "lsl{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 5734 | // lsl (register) |
| 5735 | { 0xffffffc0, 0x00004080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSLReg, "lsls|lsl<c> <Rdn>, <Rm>"}, |
| 5736 | { 0xffe0f0f0, 0xfa00f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSLReg, "lsl{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
| 5737 | // lsr (immediate) |
| 5738 | { 0xfffff800, 0x00000800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSRImm, "lsrs|lsr<c> <Rd>, <Rm>, #imm"}, |
| 5739 | { 0xffef8030, 0xea4f0010, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSRImm, "lsr{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 5740 | // lsr (register) |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 5741 | { 0xffffffc0, 0x000040c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSRReg, "lsrs|lsr<c> <Rdn>, <Rm>"}, |
Johnny Chen | 2ee35bc | 2011-02-16 19:27:43 +0000 | [diff] [blame] | 5742 | { 0xffe0f0f0, 0xfa20f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSRReg, "lsr{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 5743 | // rrx is a special case encoding of ror (immediate) |
| 5744 | { 0xffeff0f0, 0xea4f0030, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRRX, "rrx{s}<c>.w <Rd>, <Rm>"}, |
| 5745 | // ror (immediate) |
| 5746 | { 0xffef8030, 0xea4f0030, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRORImm, "ror{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 5747 | // ror (register) |
| 5748 | { 0xffffffc0, 0x000041c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateRORReg, "rors|ror<c> <Rdn>, <Rm>"}, |
| 5749 | { 0xffe0f0f0, 0xfa60f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateRORReg, "ror{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 5750 | |
| 5751 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 5752 | // Load instructions |
| 5753 | //---------------------------------------------------------------------- |
| 5754 | { 0xfffff800, 0x0000c800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 5755 | { 0xffd02000, 0xe8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c>.w <Rn>{!} <registers>" }, |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 5756 | { 0xffd00000, 0xe9100000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 5757 | { 0xfffff800, 0x00006800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtRnImm, "ldr<c> <Rt>, [<Rn>{,#imm}]"}, |
| 5758 | // Thumb2 PC-relative load into register |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 5759 | { 0xff7f0000, 0xf85f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr<c>.w <Rt>, [PC, +/-#imm}]"}, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5760 | { 0xfffffe00, 0x00005800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRegister, "ldr<c> <Rt>, [<Rn>, <Rm>]" }, |
| 5761 | { 0xfff00fc0, 0xf8500000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRRegister, "ldr<c>.w <Rt>, [<Rn>,<Rm>{,LSL #<imm2>}]" }, |
Caroline Tice | 21b604b | 2011-02-18 21:06:04 +0000 | [diff] [blame] | 5762 | { 0xfffff800, 0x00007800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c> <Rt>,[<Rn>{,#<imm5>}]" }, |
| 5763 | { 0xfff00000, 0xf8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c>.w <Rt>,[<Rn>{,#<imm12>}]" }, |
| 5764 | { 0xfff00800, 0xf8100800, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c> <Rt>,[>Rn>, #+/-<imm8>]{!}" }, |
Caroline Tice | f55261f | 2011-02-18 22:24:22 +0000 | [diff] [blame] | 5765 | { 0xff7f0000, 0xf81f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRBLiteral, "ldrb<c> <Rt>,[...]" }, |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame^] | 5766 | { 0xfffffe00, 0x00005c00, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c> <Rt>,[<Rn>,<Rm>]" }, |
| 5767 | { 0xfff00fc0, 0xf8100000, ARMV6T2_ABOVE, eEncodingT2, eSize32,&EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c>.w <Rt>,[<Rn>,<Rm>{,LSL #imm2>}]" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 5768 | |
| 5769 | //---------------------------------------------------------------------- |
| 5770 | // Store instructions |
| 5771 | //---------------------------------------------------------------------- |
| 5772 | { 0xfffff800, 0x0000c000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" }, |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 5773 | { 0xffd00000, 0xe8800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c>.w <Rn>{!} <registers>" }, |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 5774 | { 0xffd00000, 0xe9000000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5775 | { 0xfffff800, 0x00006000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [<Rn>{,#<imm>}]" }, |
| 5776 | { 0xfffff800, 0x00009000, ARMV4T_ABOVE, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [SP,#<imm>]" }, |
| 5777 | { 0xfff00000, 0xf8c00000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c>.w <Rt>, [<Rn>,#<imm12>]" }, |
| 5778 | { 0xfff00800, 0xf8400800, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [<Rn>,#+/-<imm8>]" }, |
| 5779 | { 0xfffffe00, 0x00005000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRRegister, "str<c> <Rt> ,{<Rn>, <Rm>]" }, |
| 5780 | { 0xfff00fc0, 0xf8400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTRRegister, "str<c>.w <Rt>, [<Rn>, <Rm> {lsl #imm2>}]" }, |
| 5781 | { 0xfffff800, 0x00007000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c> <Rt>, [<Rn>, #<imm5>]" }, |
| 5782 | { 0xfff00000, 0xf8800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c>.w <Rt>, [<Rn>, #<imm12>]" }, |
| 5783 | { 0xfff00800, 0xf8000800, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c> <Rt> ,[<Rn>, #+/-<imm8>]{!}" } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 5784 | }; |
| 5785 | |
| 5786 | const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
| 5787 | for (size_t i=0; i<k_num_thumb_opcodes; ++i) |
| 5788 | { |
| 5789 | if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value) |
| 5790 | return &g_thumb_opcodes[i]; |
| 5791 | } |
| 5792 | return NULL; |
| 5793 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5794 | |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 5795 | bool |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 5796 | EmulateInstructionARM::SetArchitecture (const ArchSpec &arch) |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 5797 | { |
| 5798 | m_arm_isa = 0; |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 5799 | const char *arch_cstr = arch.AsCString (); |
| 5800 | if (arch_cstr) |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 5801 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 5802 | if (0 == ::strcasecmp(arch_cstr, "armv4t")) m_arm_isa = ARMv4T; |
| 5803 | else if (0 == ::strcasecmp(arch_cstr, "armv4")) m_arm_isa = ARMv4; |
| 5804 | else if (0 == ::strcasecmp(arch_cstr, "armv5tej")) m_arm_isa = ARMv5TEJ; |
| 5805 | else if (0 == ::strcasecmp(arch_cstr, "armv5te")) m_arm_isa = ARMv5TE; |
| 5806 | else if (0 == ::strcasecmp(arch_cstr, "armv5t")) m_arm_isa = ARMv5T; |
| 5807 | else if (0 == ::strcasecmp(arch_cstr, "armv6k")) m_arm_isa = ARMv6K; |
| 5808 | else if (0 == ::strcasecmp(arch_cstr, "armv6")) m_arm_isa = ARMv6; |
| 5809 | else if (0 == ::strcasecmp(arch_cstr, "armv6t2")) m_arm_isa = ARMv6T2; |
| 5810 | else if (0 == ::strcasecmp(arch_cstr, "armv7")) m_arm_isa = ARMv7; |
| 5811 | else if (0 == ::strcasecmp(arch_cstr, "armv8")) m_arm_isa = ARMv8; |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 5812 | } |
| 5813 | return m_arm_isa != 0; |
| 5814 | } |
| 5815 | |
| 5816 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5817 | bool |
| 5818 | EmulateInstructionARM::ReadInstruction () |
| 5819 | { |
| 5820 | bool success = false; |
| 5821 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 5822 | if (success) |
| 5823 | { |
| 5824 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 5825 | if (success) |
| 5826 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 5827 | Context read_inst_context; |
| 5828 | read_inst_context.type = eContextReadOpcode; |
| 5829 | read_inst_context.SetNoArgs (); |
| 5830 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5831 | if (m_inst_cpsr & MASK_CPSR_T) |
| 5832 | { |
| 5833 | m_inst_mode = eModeThumb; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 5834 | uint32_t thumb_opcode = MemARead(read_inst_context, pc, 2, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5835 | |
| 5836 | if (success) |
| 5837 | { |
| 5838 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 5839 | { |
| 5840 | m_inst.opcode_type = eOpcode16; |
| 5841 | m_inst.opcode.inst16 = thumb_opcode; |
| 5842 | } |
| 5843 | else |
| 5844 | { |
| 5845 | m_inst.opcode_type = eOpcode32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 5846 | m_inst.opcode.inst32 = (thumb_opcode << 16) | MemARead(read_inst_context, pc + 2, 2, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5847 | } |
| 5848 | } |
| 5849 | } |
| 5850 | else |
| 5851 | { |
| 5852 | m_inst_mode = eModeARM; |
| 5853 | m_inst.opcode_type = eOpcode32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 5854 | m_inst.opcode.inst32 = MemARead(read_inst_context, pc, 4, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5855 | } |
| 5856 | } |
| 5857 | } |
| 5858 | if (!success) |
| 5859 | { |
| 5860 | m_inst_mode = eModeInvalid; |
| 5861 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 5862 | } |
| 5863 | return success; |
| 5864 | } |
| 5865 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 5866 | uint32_t |
| 5867 | EmulateInstructionARM::ArchVersion () |
| 5868 | { |
| 5869 | return m_arm_isa; |
| 5870 | } |
| 5871 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 5872 | bool |
| 5873 | EmulateInstructionARM::ConditionPassed () |
| 5874 | { |
| 5875 | if (m_inst_cpsr == 0) |
| 5876 | return false; |
| 5877 | |
| 5878 | const uint32_t cond = CurrentCond (); |
| 5879 | |
| 5880 | if (cond == UINT32_MAX) |
| 5881 | return false; |
| 5882 | |
| 5883 | bool result = false; |
| 5884 | switch (UnsignedBits(cond, 3, 1)) |
| 5885 | { |
| 5886 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 5887 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 5888 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 5889 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 5890 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 5891 | case 5: |
| 5892 | { |
| 5893 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 5894 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 5895 | result = n == v; |
| 5896 | } |
| 5897 | break; |
| 5898 | case 6: |
| 5899 | { |
| 5900 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 5901 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 5902 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 5903 | } |
| 5904 | break; |
| 5905 | case 7: |
| 5906 | result = true; |
| 5907 | break; |
| 5908 | } |
| 5909 | |
| 5910 | if (cond & 1) |
| 5911 | result = !result; |
| 5912 | return result; |
| 5913 | } |
| 5914 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5915 | uint32_t |
| 5916 | EmulateInstructionARM::CurrentCond () |
| 5917 | { |
| 5918 | switch (m_inst_mode) |
| 5919 | { |
| 5920 | default: |
| 5921 | case eModeInvalid: |
| 5922 | break; |
| 5923 | |
| 5924 | case eModeARM: |
| 5925 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 5926 | |
| 5927 | case eModeThumb: |
| 5928 | // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit |
| 5929 | // 'cond' field of the encoding. |
| 5930 | if (m_inst.opcode_type == eOpcode16 && |
| 5931 | Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d && |
| 5932 | Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f) |
| 5933 | { |
| 5934 | return Bits32(m_inst.opcode.inst16, 11, 7); |
| 5935 | } |
| 5936 | else if (m_inst.opcode_type == eOpcode32 && |
| 5937 | Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e && |
| 5938 | Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 && |
| 5939 | Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 && |
| 5940 | Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d) |
| 5941 | { |
| 5942 | return Bits32(m_inst.opcode.inst32, 25, 22); |
| 5943 | } |
| 5944 | |
| 5945 | return m_it_session.GetCond(); |
| 5946 | } |
| 5947 | return UINT32_MAX; // Return invalid value |
| 5948 | } |
| 5949 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5950 | bool |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 5951 | EmulateInstructionARM::InITBlock() |
| 5952 | { |
| 5953 | return CurrentInstrSet() == eModeThumb && m_it_session.InITBlock(); |
| 5954 | } |
| 5955 | |
| 5956 | bool |
| 5957 | EmulateInstructionARM::LastInITBlock() |
| 5958 | { |
| 5959 | return CurrentInstrSet() == eModeThumb && m_it_session.LastInITBlock(); |
| 5960 | } |
| 5961 | |
| 5962 | bool |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5963 | EmulateInstructionARM::BranchWritePC (const Context &context, uint32_t addr) |
| 5964 | { |
| 5965 | addr_t target; |
| 5966 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 5967 | // Check the current instruction set. |
| 5968 | if (CurrentInstrSet() == eModeARM) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5969 | target = addr & 0xfffffffc; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 5970 | else |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5971 | target = addr & 0xfffffffe; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 5972 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5973 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 5974 | return false; |
| 5975 | |
| 5976 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | // As a side effect, BXWritePC sets context.arg2 to eModeARM or eModeThumb by inspecting addr. |
| 5980 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 5981 | EmulateInstructionARM::BXWritePC (Context &context, uint32_t addr) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5982 | { |
| 5983 | addr_t target; |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 5984 | // If the CPSR is changed due to switching between ARM and Thumb ISETSTATE, |
| 5985 | // we want to record it and issue a WriteRegister callback so the clients |
| 5986 | // can track the mode changes accordingly. |
| 5987 | bool cpsr_changed = false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5988 | |
| 5989 | if (BitIsSet(addr, 0)) |
| 5990 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 5991 | if (CurrentInstrSet() != eModeThumb) |
| 5992 | { |
| 5993 | SelectInstrSet(eModeThumb); |
| 5994 | cpsr_changed = true; |
| 5995 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5996 | target = addr & 0xfffffffe; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 5997 | context.SetMode (eModeThumb); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 5998 | } |
| 5999 | else if (BitIsClear(addr, 1)) |
| 6000 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 6001 | if (CurrentInstrSet() != eModeARM) |
| 6002 | { |
| 6003 | SelectInstrSet(eModeARM); |
| 6004 | cpsr_changed = true; |
| 6005 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6006 | target = addr & 0xfffffffc; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 6007 | context.SetMode (eModeARM); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6008 | } |
| 6009 | else |
| 6010 | return false; // address<1:0> == '10' => UNPREDICTABLE |
| 6011 | |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 6012 | if (cpsr_changed) |
| 6013 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 6014 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 6015 | return false; |
| 6016 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6017 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 6018 | return false; |
| 6019 | |
| 6020 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6021 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6022 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6023 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions. |
| 6024 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 6025 | EmulateInstructionARM::LoadWritePC (Context &context, uint32_t addr) |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6026 | { |
| 6027 | if (ArchVersion() >= ARMv5T) |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 6028 | return BXWritePC(context, addr); |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6029 | else |
| 6030 | return BranchWritePC((const Context)context, addr); |
| 6031 | } |
| 6032 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 6033 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions and current instruction set. |
| 6034 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 6035 | EmulateInstructionARM::ALUWritePC (Context &context, uint32_t addr) |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 6036 | { |
| 6037 | if (ArchVersion() >= ARMv7 && CurrentInstrSet() == eModeARM) |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 6038 | return BXWritePC(context, addr); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 6039 | else |
| 6040 | return BranchWritePC((const Context)context, addr); |
| 6041 | } |
| 6042 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6043 | EmulateInstructionARM::Mode |
| 6044 | EmulateInstructionARM::CurrentInstrSet () |
| 6045 | { |
| 6046 | return m_inst_mode; |
| 6047 | } |
| 6048 | |
| 6049 | // Set the 'T' bit of our CPSR. The m_inst_mode gets updated when the next |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 6050 | // ReadInstruction() is performed. This function has a side effect of updating |
| 6051 | // the m_new_inst_cpsr member variable if necessary. |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6052 | bool |
| 6053 | EmulateInstructionARM::SelectInstrSet (Mode arm_or_thumb) |
| 6054 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 6055 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6056 | switch (arm_or_thumb) |
| 6057 | { |
| 6058 | default: |
| 6059 | return false; |
| 6060 | eModeARM: |
| 6061 | // Clear the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 6062 | m_new_inst_cpsr &= ~MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6063 | break; |
| 6064 | eModeThumb: |
| 6065 | // Set the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 6066 | m_new_inst_cpsr |= MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6067 | break; |
| 6068 | } |
| 6069 | return true; |
| 6070 | } |
| 6071 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 6072 | // This function returns TRUE if the processor currently provides support for |
| 6073 | // unaligned memory accesses, or FALSE otherwise. This is always TRUE in ARMv7, |
| 6074 | // controllable by the SCTLR.U bit in ARMv6, and always FALSE before ARMv6. |
| 6075 | bool |
| 6076 | EmulateInstructionARM::UnalignedSupport() |
| 6077 | { |
| 6078 | return (ArchVersion() >= ARMv7); |
| 6079 | } |
| 6080 | |
Johnny Chen | bf6ad17 | 2011-02-11 01:29:53 +0000 | [diff] [blame] | 6081 | // The main addition and subtraction instructions can produce status information |
| 6082 | // about both unsigned carry and signed overflow conditions. This status |
| 6083 | // information can be used to synthesize multi-word additions and subtractions. |
| 6084 | EmulateInstructionARM::AddWithCarryResult |
| 6085 | EmulateInstructionARM::AddWithCarry (uint32_t x, uint32_t y, uint8_t carry_in) |
| 6086 | { |
| 6087 | uint32_t result; |
| 6088 | uint8_t carry_out; |
| 6089 | uint8_t overflow; |
| 6090 | |
| 6091 | uint64_t unsigned_sum = x + y + carry_in; |
| 6092 | int64_t signed_sum = (int32_t)x + (int32_t)y + (int32_t)carry_in; |
| 6093 | |
| 6094 | result = UnsignedBits(unsigned_sum, 31, 0); |
| 6095 | carry_out = (result == unsigned_sum ? 0 : 1); |
| 6096 | overflow = ((int32_t)result == signed_sum ? 0 : 1); |
| 6097 | |
| 6098 | AddWithCarryResult res = { result, carry_out, overflow }; |
| 6099 | return res; |
| 6100 | } |
| 6101 | |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6102 | uint32_t |
| 6103 | EmulateInstructionARM::ReadCoreReg(uint32_t regnum, bool *success) |
| 6104 | { |
| 6105 | uint32_t val; |
| 6106 | if (regnum == 15) |
| 6107 | { |
| 6108 | val = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, success); |
| 6109 | if (CurrentInstrSet() == eModeThumb) |
| 6110 | val += 4; |
| 6111 | else |
| 6112 | val += 8; |
| 6113 | } |
| 6114 | else |
| 6115 | val = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + regnum, 0, success); |
| 6116 | |
| 6117 | return val; |
| 6118 | } |
| 6119 | |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 6120 | // Write the result to the ARM core register Rd, and optionally update the |
| 6121 | // condition flags based on the result. |
| 6122 | // |
| 6123 | // This helper method tries to encapsulate the following pseudocode from the |
| 6124 | // ARM Architecture Reference Manual: |
| 6125 | // |
| 6126 | // if d == 15 then // Can only occur for encoding A1 |
| 6127 | // ALUWritePC(result); // setflags is always FALSE here |
| 6128 | // else |
| 6129 | // R[d] = result; |
| 6130 | // if setflags then |
| 6131 | // APSR.N = result<31>; |
| 6132 | // APSR.Z = IsZeroBit(result); |
| 6133 | // APSR.C = carry; |
| 6134 | // // APSR.V unchanged |
| 6135 | // |
| 6136 | // In the above case, the API client does not pass in the overflow arg, which |
| 6137 | // defaults to ~0u. |
| 6138 | bool |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 6139 | EmulateInstructionARM::WriteCoreRegOptionalFlags (Context &context, |
| 6140 | const uint32_t result, |
| 6141 | const uint32_t Rd, |
| 6142 | bool setflags, |
| 6143 | const uint32_t carry, |
| 6144 | const uint32_t overflow) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 6145 | { |
| 6146 | if (Rd == 15) |
| 6147 | { |
| 6148 | if (!ALUWritePC (context, result)) |
| 6149 | return false; |
| 6150 | } |
| 6151 | else |
| 6152 | { |
| 6153 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result)) |
| 6154 | return false; |
| 6155 | if (setflags) |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 6156 | return WriteFlags (context, result, carry, overflow); |
| 6157 | } |
| 6158 | return true; |
| 6159 | } |
| 6160 | |
| 6161 | // This helper method tries to encapsulate the following pseudocode from the |
| 6162 | // ARM Architecture Reference Manual: |
| 6163 | // |
| 6164 | // APSR.N = result<31>; |
| 6165 | // APSR.Z = IsZeroBit(result); |
| 6166 | // APSR.C = carry; |
| 6167 | // APSR.V = overflow |
| 6168 | // |
| 6169 | // Default arguments can be specified for carry and overflow parameters, which means |
| 6170 | // not to update the respective flags. |
| 6171 | bool |
| 6172 | EmulateInstructionARM::WriteFlags (Context &context, |
| 6173 | const uint32_t result, |
| 6174 | const uint32_t carry, |
| 6175 | const uint32_t overflow) |
| 6176 | { |
| 6177 | m_new_inst_cpsr = m_inst_cpsr; |
| 6178 | SetBit32(m_new_inst_cpsr, CPSR_N, Bit32(result, CPSR_N)); |
| 6179 | SetBit32(m_new_inst_cpsr, CPSR_Z, result == 0 ? 1 : 0); |
| 6180 | if (carry != ~0u) |
| 6181 | SetBit32(m_new_inst_cpsr, CPSR_C, carry); |
| 6182 | if (overflow != ~0u) |
| 6183 | SetBit32(m_new_inst_cpsr, CPSR_V, overflow); |
| 6184 | if (m_new_inst_cpsr != m_inst_cpsr) |
| 6185 | { |
| 6186 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
| 6187 | return false; |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 6188 | } |
| 6189 | return true; |
| 6190 | } |
| 6191 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6192 | bool |
| 6193 | EmulateInstructionARM::EvaluateInstruction () |
| 6194 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 6195 | // Advance the ITSTATE bits to their values for the next instruction. |
| 6196 | if (m_inst_mode == eModeThumb && m_it_session.InITBlock()) |
| 6197 | m_it_session.ITAdvance(); |
| 6198 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6199 | return false; |
| 6200 | } |